--- rxvt-unicode/src/logging.C 2004/02/01 01:34:41 1.6 +++ rxvt-unicode/src/logging.C 2006/01/18 08:40:44 1.32 @@ -1,5 +1,5 @@ /*--------------------------------*-C-*---------------------------------* - * File: logging.c + * File: logging.C *----------------------------------------------------------------------* * * All portions of code are copyright by their respective author/s. @@ -14,6 +14,7 @@ * - extensive modifications * Copyright (c) 1999 D J Hawkey Jr * - lastlog support + * 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 @@ -29,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 @@ -56,165 +57,171 @@ * 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); + struct utmp *ut = &this->ut; #endif #ifdef HAVE_STRUCT_UTMPX - struct utmpx *utx = &(this->utx); -#endif -#ifdef HAVE_UTMP_PID - int i; + struct utmpx *utx = &this->utx; #endif - char ut_id[5]; - struct passwd *pwent = getpwuid(getuid()); + int i; + struct passwd *pwent = getpwuid (getuid ()); - if (!STRNCMP(pty, "/dev/", 5)) + if (!strncmp (pty, "/dev/", 5)) pty += 5; /* skip /dev/ prefix */ - if (!STRNCMP(pty, "pty", 3) || !STRNCMP(pty, "tty", 3)) - { - STRNCPY(ut_id, (pty + 3), sizeof(ut_id)); - } -#ifdef HAVE_UTMP_PID - else if (sscanf(pty, "pts/%d", &i) == 1) - sprintf(ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ -#endif - else if (STRNCMP(pty, "pty", 3) && STRNCMP(pty, "tty", 3)) +#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)) { - rxvt_print_error("can't parse tty name \"%s\"", pty); + rxvt_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty); return; } +#endif #ifdef HAVE_STRUCT_UTMP - MEMSET(ut, 0, sizeof(struct utmp)); + memset (ut, 0, sizeof (struct utmp)); # ifdef HAVE_UTMP_PID - setutent(); - STRNCPY(ut->ut_id, ut_id, sizeof(ut->ut_id)); + setutent (); + 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)); + getutid (ut); /* position to entry in utmp file */ + strncpy (this->ut_id, ut_id, sizeof (this->ut_id)); # endif #endif #ifdef HAVE_STRUCT_UTMPX - MEMSET(utx, 0, sizeof(struct utmpx)); - setutxent(); - STRNCPY(utx->ut_id, ut_id, sizeof(utx->ut_id)); + memset (utx, 0, sizeof (struct utmpx)); + setutxent (); + 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)); + getutxid (utx); /* position to entry in utmp file */ + strncpy (this->ut_id, ut_id, sizeof (this->ut_id)); #endif #ifdef HAVE_STRUCT_UTMP - STRNCPY(ut->ut_line, pty, sizeof(ut->ut_line)); - ut->ut_time = time(NULL); + strncpy (ut->ut_line, pty, sizeof (ut->ut_line)); + 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_id, ut_id, sizeof(ut->ut_id)); - ut->ut_time = time(NULL); + strncpy (ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_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)); + strncpy (ut->ut_host, hostname, sizeof (ut->ut_host)); # endif ut->ut_type = USER_PROCESS; - pututline(ut); - endutent(); /* close the file */ + 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)); + 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)); + strncpy (ut->ut_host, hostname, sizeof (ut->ut_host)); # endif # 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_id, ut_id, sizeof(utx->ut_id)); - utx->ut_session = getsid(0); - utx->ut_tv.tv_sec = time(NULL); + 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_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; # ifdef HAVE_UTMPX_HOST - STRNCPY(utx->ut_host, hostname, sizeof(utx->ut_host)); + strncpy (utx->ut_host, hostname, sizeof (utx->ut_host)); # if 0 { char *colon; - if ((colon = STRRCHR(ut->ut_host, ':')) != NULL) + if ((colon = strrchr (ut->ut_host, ':')) != NULL) *colon = '\0'; } # endif # endif utx->ut_type = USER_PROCESS; - pututxline(utx); - endutxent(); /* close the file */ + pututxline (utx); + endutxent (); /* close the file */ utmp_pos = 0; #endif #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) - { - int i; # ifdef HAVE_TTYSLOT - i = ttyslot(); - if (rxvt_write_bsd_utmp(i, ut)) + int fdstdin = dup (STDIN_FILENO); + dup2 (pty->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) + 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);) + buf[sizeof (buf) - 1] = '\0'; + for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);) { - if (*buf == '#' || sscanf(buf, "%s", name) != 1) + if (*buf == '#' || sscanf (buf, "%s", name) != 1) continue; - if (!STRCMP(ut->ut_line, name)) + if (!strcmp (ut->ut_line, name)) { - if (!rxvt_write_bsd_utmp(i, ut)) + if (!rxvt_write_bsd_utmp (i, ut)) i = 0; utmp_pos = i; - fclose(fd0); + fclose (fd0); break; } i++; } - fclose(fd0); + fclose (fd0); } # endif - } #endif #ifdef WTMP_SUPPORT -# ifdef WTMP_ONLY_ON_LOGIN - if (Options & Opt_loginShell) -# endif + if (login_shell) { # ifdef HAVE_STRUCT_UTMP # ifdef HAVE_UPDWTMP - updwtmp(RXVT_WTMP_FILE, ut); + updwtmp (RXVT_WTMP_FILE, ut); # else - rxvt_update_wtmp(RXVT_WTMP_FILE, ut); + rxvt_update_wtmp (RXVT_WTMP_FILE, ut); # endif # endif -# ifdef HAVE_STRUCT_UTMPX - updwtmpx(RXVT_WTMPX_FILE, utx); +# 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) - rxvt_update_lastlog(RXVT_LASTLOG_FILE, pty, hostname); + if (login_shell) + rxvt_update_lastlog (RXVT_LASTLOG_FILE, pty, hostname); #endif } @@ -223,47 +230,48 @@ * 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); + struct utmpx *tmputx, *utx = &this->utx; #endif #ifdef HAVE_STRUCT_UTMP # ifdef HAVE_UTMP_PID - MEMSET(ut, 0, sizeof(struct utmp)); - setutent(); - STRNCPY(ut->ut_id, ut_id, sizeof(ut->ut_id)); + memset (ut, 0, sizeof (struct utmp)); + setutent (); + 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)); + memset (ut->ut_name, 0, sizeof (ut->ut_name)); # ifdef HAVE_UTMP_HOST - MEMSET(ut->ut_host, 0, sizeof(ut->ut_host)); + memset (ut->ut_host, 0, sizeof (ut->ut_host)); # endif # endif - ut->ut_time = time(NULL); + ut->ut_time = time (NULL); #endif #ifdef HAVE_STRUCT_UTMPX - MEMSET(utx, 0, sizeof(struct utmpx)); - setutxent(); - STRNCPY(utx->ut_id, ut_id, sizeof(utx->ut_id)); + memset (utx, 0, sizeof (struct utmpx)); + setutxent (); + 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 */ + if ((tmputx = getutxid (utx))) /* position to entry in utmp file */ utx = tmputx; utx->ut_type = DEAD_PROCESS; - utx->ut_session = getsid(0); - utx->ut_tv.tv_sec = time(NULL); +# if HAVE_UTMPX_SESSION + utx->ut_session = getsid (0); +# endif + utx->ut_tv.tv_sec = time (NULL); utx->ut_tv.tv_usec = 0; #endif @@ -271,19 +279,17 @@ * Write ending wtmp entry */ #ifdef WTMP_SUPPORT -# ifdef WTMP_ONLY_ON_LOGIN - if (Options & Opt_loginShell) -# endif + if (login_shell) { # ifdef HAVE_STRUCT_UTMP # ifdef HAVE_UPDWTMP - updwtmp(RXVT_WTMP_FILE, ut); + updwtmp (RXVT_WTMP_FILE, ut); # else - rxvt_update_wtmp(RXVT_WTMP_FILE, ut); + rxvt_update_wtmp (RXVT_WTMP_FILE, ut); # endif # endif -# ifdef HAVE_STRUCT_UTMPX - updwtmpx(RXVT_WTMPX_FILE, utx); +# if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX) + updwtmpx (RXVT_WTMPX_FILE, utx); # endif } #endif @@ -294,37 +300,38 @@ #ifdef HAVE_STRUCT_UTMP # ifdef HAVE_UTMP_PID if (ut->ut_pid == cmd_pid) - pututline(ut); - endutent(); + pututline (ut); + endutent (); # else - MEMSET(ut, 0, sizeof(struct utmp)); - rxvt_write_bsd_utmp(utmp_pos, ut); + memset (ut, 0, sizeof (struct utmp)); + rxvt_write_bsd_utmp (utmp_pos, ut); # endif #endif #ifdef HAVE_STRUCT_UTMPX if (utx->ut_pid == cmd_pid) - pututxline(utx); - endutxent(); + pututxline (utx); + endutxent (); #endif + + cmd_pid = 0; } /* ------------------------------------------------------------------------- */ /* * Write a BSD style utmp entry */ -#ifdef HAVE_UTMP_H -/* INTPROTO */ -int -rxvt_write_bsd_utmp(int utmp_pos, struct utmp *wu) +#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) +static 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) + 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); + if (lseek (fd, (off_t) (utmp_pos * sizeof (struct utmp)), SEEK_SET) != -1) + write (fd, wu, sizeof (struct utmp)); + close (fd); return 1; } #endif @@ -333,16 +340,15 @@ /* * Update a BSD style wtmp entry */ -#if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) -/* INTPROTO */ -void -rxvt_update_wtmp(const char *fname, const struct utmp *putmp) +#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; struct flock lck; /* fcntl locking scheme */ struct stat sbuf; - if ((fd = open(fname, O_WRONLY | O_APPEND, 0)) < 0) + if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0) return; lck.l_whence = SEEK_END; /* start lock at current eof */ @@ -352,7 +358,7 @@ /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */ for (retry = 10, gotlock = 0; retry--;) - if (fcntl(fd, F_SETLK, &lck) != -1) + if (fcntl (fd, F_SETLK, &lck) != -1) { gotlock = 1; break; @@ -361,24 +367,23 @@ break; if (!gotlock) { /* give it up */ - close(fd); + 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 */ + 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); + fcntl (fd, F_SETLK, &lck); + close (fd); } #endif /* ------------------------------------------------------------------------- */ #ifdef LASTLOG_SUPPORT -/* INTPROTO */ -void -rxvt_update_lastlog(const char *fname, const char *pty, const char *host) +static void +rxvt_update_lastlog (const char *fname, const char *pty, const char *host) { # ifdef HAVE_STRUCT_LASTLOGX struct lastlogx llx; @@ -393,43 +398,44 @@ # endif # ifdef HAVE_STRUCT_LASTLOGX - MEMSET(&llx, 0, sizeof(llx)); - llx.ll_tv.tv_sec = time(NULL); + 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); + 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()); + pwent = getpwuid (getuid ()); if (!pwent) { - rxvt_print_error("no entry in password file"); + 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)); + + 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), + 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) + if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0) { - write(fd, &ll, sizeof(ll)); - close(fd); + write (fd, &ll, sizeof (ll)); + close (fd); } # else - if ((fd = open(fname, O_RDWR)) != -1) + if ((fd = open (fname, O_RDWR)) != -1) { - if (lseek(fd, (off_t) ((long)pwent->pw_uid * sizeof(ll)), + if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)), SEEK_SET) != -1) - write(fd, &ll, sizeof(ll)); - close(fd); + write (fd, &ll, sizeof (ll)); + close (fd); } # endif /* LASTLOG_IS_DIR */ # endif /* HAVE_STRUCT_LASTLOG */