--- rxvt-unicode/src/logging.C 2005/11/08 17:35:28 1.18 +++ rxvt-unicode/src/logging.C 2007/12/12 09:44:52 1.45 @@ -1,4 +1,7 @@ -/*--------------------------------*-C-*---------------------------------* +// This file is part of libptytty. Do not make local modifications. +// http://software.schmorp.de/pkg/libptytty + +/*----------------------------------------------------------------------* * File: logging.C *----------------------------------------------------------------------* * @@ -14,7 +17,8 @@ * - extensive modifications * Copyright (c) 1999 D J Hawkey Jr * - lastlog support - * Copyright (c) 2004 Marc Lehmann + * Copyright (c) 2004-2006 Marc Lehmann + * Copyright (c) 2006 Emanuele Giaquinta * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,26 +34,20 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *----------------------------------------------------------------------*/ -/*----------------------------------------------------------------------* - * Public: - * extern void cleanutent (void); - * extern void makeutent (const char * pty, const char * hostname); - * - * Private: - * rxvt_update_wtmp (); - *----------------------------------------------------------------------*/ -#include "../config.h" /* NECESSARY */ -#include "rxvt.h" /* NECESSARY */ -#include "logging.h" -#ifdef UTMP_SUPPORT +#include "../config.h" -#if HAVE_STRUCT_UTMP -int rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu); -void rxvt_update_wtmp (const char *fname, const struct utmp *putmp); -#endif +#include "ptytty.h" + +#if UTMP_SUPPORT + +#include -void rxvt_update_lastlog (const char *fname, const char *pty, const char *host); +#include +#include +#include +#include +#include /* * BSD style utmp entry @@ -59,35 +57,171 @@ */ /* ------------------------------------------------------------------------- */ +/* + * Write a BSD style utmp entry + */ +#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) +static int +write_bsd_utmp (int utmp_pos, struct utmp *wu) +{ + int fd; + + if (utmp_pos <= 0 || (fd = open (UTMP_FILE, O_WRONLY)) == -1) + return 0; + + if (lseek (fd, (off_t) (utmp_pos * sizeof (struct utmp)), SEEK_SET) != -1) + write (fd, wu, sizeof (struct utmp)); + close (fd); + return 1; +} +#endif + +/* ------------------------------------------------------------------------- */ +/* + * Update a BSD style wtmp entry + */ +#if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP) +static void +update_wtmp (const char *fname, const struct utmp *putmp) +{ + int fd, gotlock, retry; + struct flock lck; /* fcntl locking scheme */ + struct stat sbuf; + + if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0) + return; + + lck.l_whence = SEEK_END; /* start lock at current eof */ + lck.l_len = 0; /* end at ``largest possible eof'' */ + lck.l_start = 0; + lck.l_type = F_WRLCK; /* we want a write lock */ + + /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */ + for (retry = 10, gotlock = 0; retry--;) + if (fcntl (fd, F_SETLK, &lck) != -1) + { + gotlock = 1; + break; + } + else if (errno != EAGAIN && errno != EACCES) + break; + if (!gotlock) + { + /* give it up */ + close (fd); + return; + } + if (fstat (fd, &sbuf) == 0) + if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp)) + ftruncate (fd, sbuf.st_size); /* remove bad writes */ + + lck.l_type = F_UNLCK; /* unlocking the file */ + fcntl (fd, F_SETLK, &lck); + close (fd); +} +#endif + +/* ------------------------------------------------------------------------- */ +#ifdef LASTLOG_SUPPORT +static void +update_lastlog (const char *fname, const char *pty, const char *host) +{ +# if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX) + struct lastlogx llx; +# endif +# ifdef HAVE_STRUCT_LASTLOG + int fd; + struct lastlog ll; + char lastlogfile[256]; + struct passwd *pwent; + struct stat st; +# endif + +# if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX) + memset (&llx, 0, sizeof (llx)); + llx.ll_tv.tv_sec = time (NULL); + llx.ll_tv.tv_usec = 0; + strncpy (llx.ll_line, pty, sizeof (llx.ll_line)); + strncpy (llx.ll_host, host, sizeof (llx.ll_host)); + updlastlogx (LASTLOGX_FILE, getuid (), &llx); +# endif + +# ifdef HAVE_STRUCT_LASTLOG + pwent = getpwuid (getuid ()); + if (!pwent) + { + ptytty_warn ("no entry in password file, not updating lastlog.\n", 0); + return; + } + + memset (&ll, 0, sizeof (ll)); + ll.ll_time = time (NULL); + strncpy (ll.ll_line, pty, sizeof (ll.ll_line)); + strncpy (ll.ll_host, host, sizeof (ll.ll_host)); + if (stat (fname, &st) != 0) + return; + if (S_ISDIR (st.st_mode)) + { + sprintf (lastlogfile, "%.*s/%.*s", + (int)(sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2), fname, + (int)sizeof (pwent->pw_name), + (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown" + : pwent->pw_name); + if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0) + { + write (fd, &ll, sizeof (ll)); + close (fd); + } + } + else if (S_ISREG (st.st_mode)) + if ((fd = open (fname, O_RDWR)) != -1) + { + if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)), + SEEK_SET) != -1) + write (fd, &ll, sizeof (ll)); + close (fd); + } +# endif /* HAVE_STRUCT_LASTLOG */ +} +#endif /* LASTLOG_SUPPORT */ + +/* ------------------------------------------------------------------------- */ /* * make and write utmp and wtmp entries */ void -rxvt_term::makeutent (const char *pty, const char *hostname) +ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname) { + const char *pty = name; + + if (!pty || !*pty) + return; + + this->cmd_pid = cmd_pid; + this->login_shell = login_shell; + #ifdef HAVE_STRUCT_UTMP struct utmp *ut = &this->ut; #endif #ifdef HAVE_STRUCT_UTMPX struct utmpx *utx = &this->utx; #endif -#ifdef HAVE_UTMP_PID int i; -#endif struct passwd *pwent = getpwuid (getuid ()); + const char *name = (pwent && pwent->pw_name) ? pwent->pw_name : "?"; if (!strncmp (pty, "/dev/", 5)) pty += 5; /* skip /dev/ prefix */ -#ifdef HAVE_UTMP_PID +#if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX) if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3)) strncpy (ut_id, pty + 3, sizeof (ut_id)); else if (sscanf (pty, "pts/%d", &i) == 1) sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ - else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3)) + else { - rxvt_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty); + ptytty_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty); return; } #endif @@ -99,7 +233,6 @@ strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); ut->ut_type = DEAD_PROCESS; getutid (ut); /* position to entry in utmp file */ - strncpy (ut_id, ut_id, sizeof (ut_id)); # endif #endif @@ -109,38 +242,30 @@ strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); utx->ut_type = DEAD_PROCESS; getutxid (utx); /* position to entry in utmp file */ - strncpy (ut_id, ut_id, sizeof (ut_id)); #endif #ifdef HAVE_STRUCT_UTMP strncpy (ut->ut_line, pty, sizeof (ut->ut_line)); +# ifdef HAVE_UTMP_HOST + strncpy (ut->ut_host, hostname, sizeof (ut->ut_host)); +# endif ut->ut_time = time (NULL); # ifdef HAVE_UTMP_PID - strncpy (ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", - sizeof (ut->ut_user)); + strncpy (ut->ut_user, name, sizeof (ut->ut_user)); strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); - ut->ut_time = time (NULL); ut->ut_pid = cmd_pid; -# ifdef HAVE_UTMP_HOST - strncpy (ut->ut_host, hostname, sizeof (ut->ut_host)); -# endif ut->ut_type = USER_PROCESS; pututline (ut); endutent (); /* close the file */ utmp_pos = 0; # else - strncpy (ut->ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?", - sizeof (ut->ut_name)); -# ifdef HAVE_UTMP_HOST - strncpy (ut->ut_host, hostname, sizeof (ut->ut_host)); -# endif + strncpy (ut->ut_name, name, sizeof (ut->ut_name)); # endif #endif #ifdef HAVE_STRUCT_UTMPX strncpy (utx->ut_line, pty, sizeof (utx->ut_line)); - strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", - sizeof (utx->ut_user)); + strncpy (utx->ut_user, name, sizeof (utx->ut_user)); strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); # if HAVE_UTMPX_SESSION utx->ut_session = getsid (0); @@ -166,66 +291,43 @@ #endif #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) - { - int i; -# ifdef HAVE_TTYSLOT +# if 1 + int fdstdin = dup (STDIN_FILENO); + dup2 (tty, STDIN_FILENO); + i = ttyslot (); - if (rxvt_write_bsd_utmp (i, ut)) + if (write_bsd_utmp (i, ut)) utmp_pos = i; -# else - FILE *fd0; - - if ((fd0 = fopen (TTYTAB_FILENAME, "r")) != NULL) - { - char buf[256], name[256]; - buf[sizeof (buf) - 1] = '\0'; - for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);) - { - if (*buf == '#' || sscanf (buf, "%s", name) != 1) - continue; - if (!strcmp (ut->ut_line, name)) - { - if (!rxvt_write_bsd_utmp (i, ut)) - i = 0; - utmp_pos = i; - fclose (fd0); - break; - } - i++; - } - fclose (fd0); - } + dup2 (fdstdin, STDIN_FILENO); + close (fdstdin); # endif - } #endif #ifdef WTMP_SUPPORT -# ifdef WTMP_ONLY_ON_LOGIN - if (options & Opt_loginShell) -# endif +#ifdef LOG_ONLY_ON_LOGIN + if (login_shell) +#endif { # ifdef HAVE_STRUCT_UTMP # ifdef HAVE_UPDWTMP - updwtmp (RXVT_WTMP_FILE, ut); + updwtmp (WTMP_FILE, ut); # else - rxvt_update_wtmp (RXVT_WTMP_FILE, ut); + update_wtmp (WTMP_FILE, ut); # endif # endif -# ifdef HAVE_STRUCT_UTMPX -# if HAVE_UPDWTMPX - updwtmpx (RXVT_WTMPX_FILE, utx); -# else - pututxline (utx); -# endif +# if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX) + updwtmpx (WTMPX_FILE, utx); # endif } #endif -#if defined(LASTLOG_SUPPORT) && defined(RXVT_LASTLOG_FILE) - if (options & Opt_loginShell) - rxvt_update_lastlog (RXVT_LASTLOG_FILE, pty, hostname); +#if defined(LASTLOG_SUPPORT) && defined(LASTLOG_FILE) +#ifdef LOG_ONLY_ON_LOGIN + if (login_shell) +#endif + update_lastlog (LASTLOG_FILE, pty, hostname); #endif } @@ -234,10 +336,13 @@ * remove utmp and wtmp entries */ void -rxvt_term::cleanutent () +ptytty_unix::logout () { + if (!cmd_pid) + return; + #ifdef HAVE_STRUCT_UTMP - struct utmp *ut = &this->ut; + struct utmp *tmput, *ut = &this->ut; #endif #ifdef HAVE_STRUCT_UTMPX struct utmpx *tmputx, *utx = &this->utx; @@ -247,14 +352,10 @@ # ifdef HAVE_UTMP_PID memset (ut, 0, sizeof (struct utmp)); setutent (); - strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); + strncpy (ut->ut_id, this->ut_id, sizeof (ut->ut_id)); ut->ut_type = USER_PROCESS; - { - struct utmp *tmput = getutid (ut); - - if (tmput) /* position to entry in utmp file */ - ut = tmput; - } + if ((tmput = getutid (ut))) /* position to entry in utmp file */ + ut = tmput; ut->ut_type = DEAD_PROCESS; # else memset (ut->ut_name, 0, sizeof (ut->ut_name)); @@ -268,7 +369,7 @@ #ifdef HAVE_STRUCT_UTMPX memset (utx, 0, sizeof (struct utmpx)); setutxent (); - strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); + strncpy (utx->ut_id, this->ut_id, sizeof (utx->ut_id)); utx->ut_type = USER_PROCESS; if ((tmputx = getutxid (utx))) /* position to entry in utmp file */ utx = tmputx; @@ -284,23 +385,19 @@ * Write ending wtmp entry */ #ifdef WTMP_SUPPORT -# ifdef WTMP_ONLY_ON_LOGIN - if (options & Opt_loginShell) -# endif +#ifdef LOG_ONLY_ON_LOGIN + if (login_shell) +#endif { # ifdef HAVE_STRUCT_UTMP # ifdef HAVE_UPDWTMP - updwtmp (RXVT_WTMP_FILE, ut); + updwtmp (WTMP_FILE, ut); # else - rxvt_update_wtmp (RXVT_WTMP_FILE, ut); + update_wtmp (WTMP_FILE, ut); # endif # endif -# ifdef HAVE_STRUCT_UTMPX -# if HAVE_UPDWTMPX - updwtmpx (RXVT_WTMPX_FILE, utx); -# else - pututxline (utx); -# endif +# if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX) + updwtmpx (WTMPX_FILE, utx); # endif } #endif @@ -315,7 +412,7 @@ endutent (); # else memset (ut, 0, sizeof (struct utmp)); - rxvt_write_bsd_utmp (utmp_pos, ut); + write_bsd_utmp (utmp_pos, ut); # endif #endif #ifdef HAVE_STRUCT_UTMPX @@ -323,133 +420,14 @@ pututxline (utx); endutxent (); #endif -} -/* ------------------------------------------------------------------------- */ -/* - * Write a BSD style utmp entry - */ -#ifdef HAVE_UTMP_H -int -rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu) -{ - int fd; - - if (utmp_pos <= 0 || (fd = open (RXVT_UTMP_FILE, O_WRONLY)) == -1) - return 0; - - if (lseek (fd, (off_t) (utmp_pos * sizeof (struct utmp)), SEEK_SET) != -1) - write (fd, wu, sizeof (struct utmp)); - close (fd); - return 1; + cmd_pid = 0; } -#endif -/* ------------------------------------------------------------------------- */ -/* - * Update a BSD style wtmp entry - */ -#if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP) +#else void -rxvt_update_wtmp (const char *fname, const struct utmp *putmp) +ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname) { - int fd, gotlock, retry; - struct flock lck; /* fcntl locking scheme */ - struct stat sbuf; - - if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0) - return; - - lck.l_whence = SEEK_END; /* start lock at current eof */ - lck.l_len = 0; /* end at ``largest possible eof'' */ - lck.l_start = 0; - lck.l_type = F_WRLCK; /* we want a write lock */ - - /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */ - for (retry = 10, gotlock = 0; retry--;) - if (fcntl (fd, F_SETLK, &lck) != -1) - { - gotlock = 1; - break; - } - else if (errno != EAGAIN && errno != EACCES) - break; - if (!gotlock) - { /* give it up */ - close (fd); - return; - } - if (fstat (fd, &sbuf) == 0) - if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp)) - ftruncate (fd, sbuf.st_size); /* remove bad writes */ - - lck.l_type = F_UNLCK; /* unlocking the file */ - fcntl (fd, F_SETLK, &lck); - close (fd); } #endif -/* ------------------------------------------------------------------------- */ -#ifdef LASTLOG_SUPPORT -void -rxvt_update_lastlog (const char *fname, const char *pty, const char *host) -{ -# ifdef HAVE_STRUCT_LASTLOGX - struct lastlogx llx; -# endif -# ifdef HAVE_STRUCT_LASTLOG - int fd; - struct lastlog ll; -# ifdef LASTLOG_IS_DIR - char lastlogfile[256]; -# endif - struct passwd *pwent; -# endif - -# ifdef HAVE_STRUCT_LASTLOGX - memset (&llx, 0, sizeof (llx)); - llx.ll_tv.tv_sec = time (NULL); - llx.ll_tv.tv_usec = 0; - strncpy (llx.ll_line, pty, sizeof (llx.ll_line)); - strncpy (llx.ll_host, host, sizeof (llx.ll_host)); - updlastlogx (RXVT_LASTLOGX_FILE, getuid (), &llx); -# endif - -# ifdef HAVE_STRUCT_LASTLOG - pwent = getpwuid (getuid ()); - if (!pwent) - { - rxvt_warn ("no entry in password file, not updating lastlog.\n"); - return; - } - - memset (&ll, 0, sizeof (ll)); - ll.ll_time = time (NULL); - strncpy (ll.ll_line, pty, sizeof (ll.ll_line)); - strncpy (ll.ll_host, host, sizeof (ll.ll_host)); -# ifdef LASTLOG_IS_DIR - sprintf (lastlogfile, "%.*s/%.*s", - sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2, fname, - sizeof (pwent->pw_name), - (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown" - : pwent->pw_name); - if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0) - { - write (fd, &ll, sizeof (ll)); - close (fd); - } -# else - if ((fd = open (fname, O_RDWR)) != -1) - { - if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)), - SEEK_SET) != -1) - write (fd, &ll, sizeof (ll)); - close (fd); - } -# endif /* LASTLOG_IS_DIR */ -# endif /* HAVE_STRUCT_LASTLOG */ -} -#endif /* LASTLOG_SUPPORT */ -/* ------------------------------------------------------------------------- */ - -#endif /* UTMP_SUPPORT */