--- rxvt-unicode/src/logging.C 2005/02/04 04:16:05 1.13 +++ rxvt-unicode/src/logging.C 2006/01/18 16:34:53 1.34 @@ -14,7 +14,7 @@ * - extensive modifications * Copyright (c) 1999 D J Hawkey Jr * - lastlog support - * Copyright (c) 2004 Marc Lehmann + * Copyright (c) 2004-2006 Marc Lehmann * * 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,19 +30,19 @@ * 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" +#include "rxvt.h" +#include "ptytty.h" + +#if UTMP_SUPPORT + +#if HAVE_STRUCT_UTMP +static int rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu); +static void rxvt_update_wtmp (const char *fname, const struct utmp *putmp); +#endif + +static void rxvt_update_lastlog (const char *fname, const char *pty, const char *host); /* * BSD style utmp entry @@ -57,23 +57,29 @@ * make and write utmp and wtmp entries */ void -rxvt_term::makeutent (const char *pty, const char *hostname) +rxvt_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 ()); + struct passwd *pwent = getpwuid (getuid ()); 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) @@ -92,7 +98,7 @@ 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)); + strncpy (this->ut_id, ut_id, sizeof (this->ut_id)); # endif #endif @@ -102,7 +108,7 @@ 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)); + strncpy (this->ut_id, ut_id, sizeof (this->ut_id)); #endif #ifdef HAVE_STRUCT_UTMP @@ -135,7 +141,9 @@ strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", sizeof (utx->ut_user)); strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); +# if HAVE_UTMPX_SESSION utx->ut_session = getsid (0); +# endif utx->ut_tv.tv_sec = time (NULL); utx->ut_tv.tv_usec = 0; utx->ut_pid = cmd_pid; @@ -157,19 +165,23 @@ #endif #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) - { - int i; # ifdef HAVE_TTYSLOT + int fdstdin = dup (STDIN_FILENO); + dup2 (tty, STDIN_FILENO); + i = ttyslot (); if (rxvt_write_bsd_utmp (i, ut)) utmp_pos = i; + + dup2 (fdstdin, STDIN_FILENO); + close (fdstdin); # else - FILE *fd0; + FILE *fd0; if ((fd0 = fopen (TTYTAB_FILENAME, "r")) != NULL) { - char buf[256], name[256]; + char buf[256], name[256]; buf[sizeof (buf) - 1] = '\0'; for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);) @@ -189,14 +201,13 @@ fclose (fd0); } # 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 @@ -205,13 +216,15 @@ rxvt_update_wtmp (RXVT_WTMP_FILE, ut); # endif # endif -# ifdef HAVE_STRUCT_UTMPX +# if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX) updwtmpx (RXVT_WTMPX_FILE, utx); # endif } #endif #if defined(LASTLOG_SUPPORT) && defined(RXVT_LASTLOG_FILE) - if (options & Opt_loginShell) +#ifdef LOG_ONLY_ON_LOGIN + if (login_shell) +#endif rxvt_update_lastlog (RXVT_LASTLOG_FILE, pty, hostname); #endif } @@ -221,10 +234,13 @@ * remove utmp and wtmp entries */ void -rxvt_term::cleanutent () +rxvt_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; @@ -234,14 +250,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)); @@ -255,12 +267,14 @@ #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; utx->ut_type = DEAD_PROCESS; +# if HAVE_UTMPX_SESSION utx->ut_session = getsid (0); +# endif utx->ut_tv.tv_sec = time (NULL); utx->ut_tv.tv_usec = 0; #endif @@ -269,9 +283,9 @@ * 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 @@ -280,7 +294,7 @@ rxvt_update_wtmp (RXVT_WTMP_FILE, ut); # endif # endif -# ifdef HAVE_STRUCT_UTMPX +# if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX) updwtmpx (RXVT_WTMPX_FILE, utx); # endif } @@ -304,15 +318,16 @@ pututxline (utx); endutxent (); #endif + + cmd_pid = 0; } /* ------------------------------------------------------------------------- */ /* * Write a BSD style utmp entry */ -#ifdef HAVE_UTMP_H -/* INTPROTO */ -int +#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) +static int rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu) { int fd; @@ -331,9 +346,8 @@ /* * Update a BSD style wtmp entry */ -#if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) -/* INTPROTO */ -void +#if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP) +static void rxvt_update_wtmp (const char *fname, const struct utmp *putmp) { int fd, gotlock, retry; @@ -374,8 +388,7 @@ /* ------------------------------------------------------------------------- */ #ifdef LASTLOG_SUPPORT -/* INTPROTO */ -void +static void rxvt_update_lastlog (const char *fname, const char *pty, const char *host) { # ifdef HAVE_STRUCT_LASTLOGX