initial checkin

This commit is contained in:
gdr 1997-09-21 06:02:56 +00:00
parent 740fda6275
commit 1554cedb32
5 changed files with 552 additions and 0 deletions

113
lib/libc/gen/getlogin.c Normal file
View File

@ -0,0 +1,113 @@
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: getlogin.c,v 1.1 1997/09/21 06:02:56 gdr Exp $
*
* This file is formatted for tab stops every 8 columns.
*/
#ifdef __ORCAC__
segment "libc_gen__";
#endif
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getlogin.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <pwd.h>
#include <utmp.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
int _logname_valid; /* known to setlogin() */
static char logname[MAXLOGNAME + 1];
/*
* We don't have the _getlogin(2) and setlogin(2) kernel traps for
* GNO; fake them out.
*/
#ifdef __GNO__
static int
_getlogin (char *buffer, int length)
{
struct passwd *pw;
int len;
if ((pw = getpwuid(getuid())) == NULL) {
return -1;
}
len = strlen(pw->pw_name);
if (len > length) {
return -1;
}
strcpy(buffer, pw->pw_name);
len = strlen(logname);
return 0;
}
int
setlogin (const char *name)
{
if (name == NULL) {
errno = EFAULT;
return -1;
}
if (getuid() != 0) {
errno = EPERM;
return -1;
}
if (strlen(name) > MAXLOGNAME) {
errno = EINVAL;
return -1;
}
strcpy(logname, name);
_logname_valid = 1;
return 0;
}
#endif
char *
getlogin(void)
{
if (_logname_valid == 0) {
if (_getlogin(logname, sizeof(logname) - 1) < 0)
return ((char *)NULL);
_logname_valid = 1;
}
return (*logname ? logname : (char *)NULL);
}

90
lib/libutil/logout.c Normal file
View File

@ -0,0 +1,90 @@
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: logout.c,v 1.1 1997/09/21 05:59:42 gdr Exp $
*
* This file is formatted for tab stops every 8 columns.
*/
#ifdef __ORCAC__
segment "libutil___";
#endif
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)logout.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/time.h>
#include <fcntl.h>
#include <utmp.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
typedef struct utmp UTMP;
#ifdef TESTRUN
#undef _PATH_UTMP
#undef _PATH_WTMP
#define _PATH_UTMP "/tmp/utmp"
#define _PATH_WTMP "/tmp/wtmp"
#endif
int
logout(register char *line)
{
register int fd;
UTMP ut;
int rval;
#ifdef __ORCAC__
if ((fd = open(_PATH_UTMP, O_RDWR)) < 0)
#else
if ((fd = open(_PATH_UTMP, O_RDWR, 0)) < 0)
#endif
return(0);
rval = 0;
while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) {
if (!ut.ut_name[0] || strncmp(ut.ut_line, line, UT_LINESIZE))
continue;
bzero(ut.ut_name, UT_NAMESIZE);
bzero(ut.ut_host, UT_HOSTSIZE);
(void)time(&ut.ut_time);
(void)lseek(fd, -(off_t)sizeof(UTMP), L_INCR);
(void)write(fd, &ut, sizeof(UTMP));
rval = 1;
}
(void)close(fd);
return(rval);
}

93
lib/libutil/logwtmp.c Normal file
View File

@ -0,0 +1,93 @@
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: logwtmp.c,v 1.1 1997/09/21 05:59:42 gdr Exp $
*
* This file is formatted for tab stops every 8 columns.
*/
#ifdef __ORCAC__
segment "libutil___";
#endif
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)logwtmp.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utmp.h>
#include <time.h>
#include <string.h>
#ifdef TESTRUN
#undef _PATH_UTMP
#undef _PATH_WTMP
#define _PATH_UTMP "/tmp/utmp"
#define _PATH_WTMP "/tmp/wtmp"
#endif
void
logwtmp(char *line, char *name, char *host)
{
struct utmp ut;
struct stat buf;
int fd;
/*
* Provide just a stub for now so that Tilghman can get
* up and running. Take this return out when all the other
* components that require this data are fixed.
*/
return;
#ifdef __ORCAC__
if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND)) < 0)
#else
if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
#endif
return;
if (fstat(fd, &buf) == 0) {
(void) strncpy(ut.ut_line, line, sizeof(ut.ut_line));
(void) strncpy(ut.ut_name, name, sizeof(ut.ut_name));
(void) strncpy(ut.ut_host, host, sizeof(ut.ut_host));
(void) time(&ut.ut_time);
if (write(fd, (char *)&ut, sizeof(struct utmp)) !=
sizeof(struct utmp))
(void) ftruncate(fd, buf.st_size);
}
(void) close(fd);
}

131
lib/libutil/pty.c Normal file
View File

@ -0,0 +1,131 @@
/*-
* Copyright (c) 1990, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: pty.c,v 1.1 1997/09/21 05:59:42 gdr Exp $
*
* This file is formatted for tab stops every 8 columns.
*/
#ifdef __ORCAC__
segment "libutil___";
#endif
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)pty.c 8.3 (Berkeley) 5/16/94";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
int
openpty(int *amaster, int *aslave, char *name, struct termios *termp,
struct winsize *winp)
{
static char line[] = "/dev/ptyXX";
register const char *cp1, *cp2;
register int master, slave, ttygid;
struct group *gr;
if ((gr = getgrnam("tty")) != NULL)
ttygid = gr->gr_gid;
else
ttygid = -1;
for (cp1 = "pqrsPQRS"; *cp1; cp1++) {
line[8] = *cp1;
for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) {
line[5] = 'p';
line[9] = *cp2;
if ((master = open(line, O_RDWR, 0)) == -1) {
if (errno == ENOENT)
return (-1); /* out of ptys */
} else {
line[5] = 't';
(void) chown(line, getuid(), ttygid);
(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
(void) revoke(line);
if ((slave = open(line, O_RDWR, 0)) != -1) {
*amaster = master;
*aslave = slave;
if (name)
strcpy(name, line);
if (termp)
(void) tcsetattr(slave,
TCSAFLUSH, termp);
if (winp)
(void) ioctl(slave, TIOCSWINSZ,
(char *)winp);
return (0);
}
(void) close(master);
}
}
}
errno = ENOENT; /* out of ptys */
return (-1);
}
int
forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp)
{
int master, slave, pid;
if (openpty(&master, &slave, name, termp, winp) == -1)
return (-1);
switch (pid = fork()) {
case -1:
return (-1);
case 0:
/*
* child
*/
(void) close(master);
login_tty(slave);
return (0);
}
/*
* parent
*/
*amaster = master;
(void) close(slave);
return (pid);
}

125
lib/libutil/setproc.c Normal file
View File

@ -0,0 +1,125 @@
/*
* Copyright (c) 1995 Peter Wemm <peter@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice immediately at the beginning of the file, without modification,
* this list of conditions, and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. This work was done expressly for inclusion into FreeBSD. Other use
* is permitted provided this notation is included.
* 4. Absolutely no warranty of function or purpose is made by the author
* Peter Wemm.
* 5. Modifications may be freely made to this file providing the above
* conditions are met.
*
* This file is formatted for tab stops every 8 columns.
*
* $Id: setproc.c,v 1.1 1997/09/21 05:59:42 gdr Exp $
*/
#ifdef __ORCAC__
segment "libutil___";
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <sys/exec.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/pmap.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
* Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
* in different locations.
* 1: old_ps_strings at the very top of the stack.
* 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
* 3: ps_strings at the very top of the stack.
* This attempts to support a kernel built in the #2 and #3 era.
*/
struct old_ps_strings {
char *old_ps_argvstr;
int old_ps_nargvstr;
char *old_ps_envstr;
int old_ps_nenvstr;
};
#define OLD_PS_STRINGS ((struct old_ps_strings *) \
(USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
#if defined(__STDC__) /* from other parts of sendmail */
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#define SPT_BUFSIZE 2048 /* from other parts of sendmail */
extern char * __progname; /* is this defined in a .h anywhere? */
void
#if defined(__STDC__)
setproctitle(const char *fmt, ...)
#else
setproctitle(fmt, va_alist)
const char *fmt;
va_dcl
#endif
{
char *p;
int len;
static char buf[SPT_BUFSIZE];
static char *ps_argv[2];
va_list ap;
#if defined(__STDC__)
va_start(ap, fmt);
#else
va_start(ap);
#endif
buf[sizeof(buf) - 1] = '\0';
if (fmt) {
/* print program name heading for grep */
(void) snprintf(buf, sizeof(buf) - 1, "%s: ", __progname);
/*
* can't use return from sprintf, as that is the count of how
* much it wanted to write, not how much it actually did.
*/
len = strlen(buf);
/* print the argument string */
(void) vsnprintf(buf + len, sizeof(buf) - 1 - len, fmt, ap);
} else {
/* Idea from NetBSD - reset the title on fmt == NULL */
strncpy(buf, __progname, sizeof(buf) - 1);
}
va_end(ap);
/* PS_STRINGS points to zeroed memory on a style #2 kernel */
if (PS_STRINGS->ps_argvstr) {
/* style #3 */
ps_argv[0] = buf;
ps_argv[1] = NULL;
PS_STRINGS->ps_nargvstr = 1;
PS_STRINGS->ps_argvstr = ps_argv;
} else {
/* style #2 */
OLD_PS_STRINGS->old_ps_nargvstr = 1;
OLD_PS_STRINGS->old_ps_argvstr = buf;
}
}