2003-09-10 23:51:59 +08:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1990, 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* CV 2003-09-10: Cygwin specific changes applied. Code simplified just
|
2004-05-29 03:50:07 +08:00
|
|
|
* for Cygwin alone.
|
2003-09-10 23:51:59 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2003-11-07 05:31:24 +08:00
|
|
|
#include <stdio.h>
|
2003-09-10 23:51:59 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <utmp.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <fcntl.h>
|
2011-02-02 23:17:14 +08:00
|
|
|
#include <err.h>
|
2011-11-08 14:26:15 +08:00
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2003-12-24 00:26:31 +08:00
|
|
|
#include "cygtls.h"
|
2003-09-10 23:51:59 +08:00
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
daemon (int nochdir, int noclose)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
switch (fork ())
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* This sleep avoids a race condition which kills the
|
2013-04-23 17:44:36 +08:00
|
|
|
child process if parent is started by a service process.
|
2003-09-10 23:51:59 +08:00
|
|
|
FIXME: Is that still true? */
|
|
|
|
Sleep (1000L);
|
|
|
|
_exit (0);
|
|
|
|
}
|
|
|
|
if (setsid () == -1)
|
|
|
|
return -1;
|
|
|
|
if (!nochdir)
|
|
|
|
chdir ("/");
|
|
|
|
if (!noclose && (fd = open (_PATH_DEVNULL, O_RDWR, 0)) >= 0)
|
|
|
|
{
|
|
|
|
dup2 (fd, STDIN_FILENO);
|
|
|
|
dup2 (fd, STDOUT_FILENO);
|
|
|
|
dup2 (fd, STDERR_FILENO);
|
|
|
|
if (fd > 2)
|
|
|
|
close (fd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
login_tty (int fd)
|
|
|
|
{
|
|
|
|
char *fdname;
|
|
|
|
int newfd;
|
|
|
|
|
|
|
|
if (setsid () == -1)
|
|
|
|
return -1;
|
|
|
|
if ((fdname = ttyname (fd)))
|
|
|
|
{
|
|
|
|
if (fd != STDIN_FILENO)
|
2004-05-29 03:50:07 +08:00
|
|
|
close (STDIN_FILENO);
|
2003-09-10 23:51:59 +08:00
|
|
|
if (fd != STDOUT_FILENO)
|
2004-05-29 03:50:07 +08:00
|
|
|
close (STDOUT_FILENO);
|
2003-09-10 23:51:59 +08:00
|
|
|
if (fd != STDERR_FILENO)
|
2004-05-29 03:50:07 +08:00
|
|
|
close (STDERR_FILENO);
|
2003-09-10 23:51:59 +08:00
|
|
|
newfd = open (fdname, O_RDWR);
|
|
|
|
close (newfd);
|
|
|
|
}
|
|
|
|
dup2 (fd, STDIN_FILENO);
|
|
|
|
dup2 (fd, STDOUT_FILENO);
|
|
|
|
dup2 (fd, STDERR_FILENO);
|
|
|
|
if (fd > 2)
|
|
|
|
close (fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
2009-12-26 14:20:56 +08:00
|
|
|
openpty (int *amaster, int *aslave, char *name, const struct termios *termp,
|
|
|
|
const struct winsize *winp)
|
2003-09-10 23:51:59 +08:00
|
|
|
{
|
|
|
|
int master, slave;
|
2007-11-29 00:45:34 +08:00
|
|
|
char pts[TTY_NAME_MAX];
|
2003-09-10 23:51:59 +08:00
|
|
|
|
|
|
|
if ((master = open ("/dev/ptmx", O_RDWR | O_NOCTTY)) >= 0)
|
|
|
|
{
|
|
|
|
grantpt (master);
|
|
|
|
unlockpt (master);
|
2013-04-23 17:44:36 +08:00
|
|
|
__ptsname (pts, cygheap->fdtab[master]->get_minor ());
|
2003-09-10 23:51:59 +08:00
|
|
|
revoke (pts);
|
|
|
|
if ((slave = open (pts, O_RDWR | O_NOCTTY)) >= 0)
|
2004-05-29 03:50:07 +08:00
|
|
|
{
|
2003-09-10 23:51:59 +08:00
|
|
|
if (amaster)
|
|
|
|
*amaster = master;
|
|
|
|
if (name)
|
|
|
|
strcpy (name, pts);
|
|
|
|
if (termp)
|
|
|
|
tcsetattr (slave, TCSAFLUSH, termp);
|
|
|
|
if (winp)
|
|
|
|
ioctl (slave, TIOCSWINSZ, (char *) winp);
|
2011-11-08 14:26:15 +08:00
|
|
|
/* The man page doesn't say that aslave can be NULL but we have
|
|
|
|
allowed it for years. As of 2011-11-08 we now avoid a handle
|
|
|
|
leak in this case. */
|
|
|
|
if (aslave)
|
|
|
|
*aslave = slave;
|
|
|
|
else
|
|
|
|
close (slave);
|
2003-09-10 23:51:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close (master);
|
|
|
|
}
|
|
|
|
set_errno (ENOENT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
2009-12-26 14:20:56 +08:00
|
|
|
forkpty (int *amaster, char *name, const struct termios *termp,
|
|
|
|
const struct winsize *winp)
|
2003-09-10 23:51:59 +08:00
|
|
|
{
|
|
|
|
int master, slave, pid;
|
|
|
|
|
|
|
|
if (openpty (&master, &slave, name, termp, winp) == -1)
|
|
|
|
return -1;
|
|
|
|
switch (pid = fork ())
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
case 0:
|
|
|
|
close (master);
|
|
|
|
login_tty (slave);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (amaster)
|
|
|
|
*amaster = master;
|
|
|
|
close (slave);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2003-11-07 05:31:24 +08:00
|
|
|
extern "C" char *__progname;
|
|
|
|
|
|
|
|
static void
|
|
|
|
_vwarnx (const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: ", __progname);
|
|
|
|
vfprintf (stderr, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
vwarn (const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
_vwarnx (fmt, ap);
|
|
|
|
fprintf (stderr, ": %s", strerror (get_errno ()));
|
|
|
|
fputc ('\n', stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
vwarnx (const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
_vwarnx (fmt, ap);
|
|
|
|
fputc ('\n', stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
warn (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
vwarn (fmt, ap);
|
2009-10-14 12:17:05 +08:00
|
|
|
va_end (ap);
|
2003-11-07 05:31:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
warnx (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
vwarnx (fmt, ap);
|
2009-10-14 12:17:05 +08:00
|
|
|
va_end (ap);
|
2003-11-07 05:31:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
verr (int eval, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
vwarn (fmt, ap);
|
|
|
|
exit (eval);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
verrx (int eval, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
vwarnx (fmt, ap);
|
|
|
|
exit (eval);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
err (int eval, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
vwarn (fmt, ap);
|
2009-10-14 12:17:05 +08:00
|
|
|
va_end (ap);
|
2003-11-07 05:31:24 +08:00
|
|
|
exit (eval);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
errx (int eval, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
vwarnx (fmt, ap);
|
2009-10-14 12:17:05 +08:00
|
|
|
va_end (ap);
|
2003-11-07 05:31:24 +08:00
|
|
|
exit (eval);
|
|
|
|
}
|
2003-11-18 01:25:59 +08:00
|
|
|
|
|
|
|
extern "C" const char *
|
|
|
|
getprogname (void)
|
|
|
|
{
|
|
|
|
return __progname;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
setprogname (const char *newprogname)
|
|
|
|
{
|
2005-07-03 10:40:30 +08:00
|
|
|
myfault efault;
|
|
|
|
if (!efault.faulted (EFAULT))
|
2003-11-18 01:25:59 +08:00
|
|
|
{
|
|
|
|
/* Per BSD man page, setprogname keeps a pointer to the last
|
2004-05-29 03:50:07 +08:00
|
|
|
path component of the argument. It does *not* copy the
|
2003-11-18 01:25:59 +08:00
|
|
|
argument before. */
|
|
|
|
__progname = strrchr (newprogname, '/');
|
|
|
|
if (__progname)
|
2004-05-29 03:50:07 +08:00
|
|
|
++__progname;
|
2003-11-18 01:25:59 +08:00
|
|
|
else
|
|
|
|
__progname = (char *)newprogname;
|
|
|
|
}
|
|
|
|
}
|
2004-09-10 16:30:51 +08:00
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
logwtmp (const char *line, const char *user, const char *host)
|
|
|
|
{
|
|
|
|
struct utmp ut;
|
|
|
|
memset (&ut, 0, sizeof ut);
|
|
|
|
ut.ut_type = USER_PROCESS;
|
|
|
|
ut.ut_pid = getpid ();
|
|
|
|
if (line)
|
|
|
|
strncpy (ut.ut_line, line, sizeof ut.ut_line);
|
|
|
|
time (&ut.ut_time);
|
|
|
|
if (user)
|
|
|
|
strncpy (ut.ut_user, user, sizeof ut.ut_user);
|
|
|
|
if (host)
|
|
|
|
strncpy (ut.ut_host, host, sizeof ut.ut_host);
|
|
|
|
updwtmp (_PATH_WTMP, &ut);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
2008-07-30 12:58:24 +08:00
|
|
|
login (const struct utmp *ut)
|
2004-09-10 16:30:51 +08:00
|
|
|
{
|
|
|
|
pututline (ut);
|
|
|
|
endutent ();
|
|
|
|
updwtmp (_PATH_WTMP, ut);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
2008-07-30 12:58:24 +08:00
|
|
|
logout (const char *line)
|
2004-09-10 16:30:51 +08:00
|
|
|
{
|
|
|
|
struct utmp ut_buf, *ut;
|
|
|
|
|
|
|
|
memset (&ut_buf, 0, sizeof ut_buf);
|
|
|
|
strncpy (ut_buf.ut_line, line, sizeof ut_buf.ut_line);
|
|
|
|
setutent ();
|
|
|
|
ut = getutline (&ut_buf);
|
|
|
|
|
|
|
|
if (ut)
|
|
|
|
{
|
|
|
|
ut->ut_type = DEAD_PROCESS;
|
|
|
|
memset (ut->ut_user, 0, sizeof ut->ut_user);
|
|
|
|
memset (ut->ut_host, 0, sizeof ut->ut_host);
|
|
|
|
time (&ut->ut_time);
|
|
|
|
debug_printf ("set logout time for %s", line);
|
|
|
|
pututline (ut);
|
|
|
|
endutent ();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|