1 /*--------------------------------*-C-*---------------------------------*
3 *----------------------------------------------------------------------*
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 1992 John Bovey <jdb@ukc.ac.uk>
8 * Copyright (c) 1993 lipka
9 * Copyright (c) 1993 Brian Stempien <stempien@cs.wmich.edu>
10 * Copyright (c) 1995 Raul Garcia Garcia <rgg@tid.es>
11 * Copyright (c) 1995 Piet W. Plomp <piet@idefix.icce.rug.nl>
12 * Copyright (c) 1997 Raul Garcia Garcia <rgg@tid.es>
13 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
14 * - extensive modifications
15 * Copyright (c) 1999 D J Hawkey Jr <hawkeyd@visi.com>
17 * Copyright (c) 2004 Marc Lehmann <pcg@goof.com>
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 *----------------------------------------------------------------------*/
33 /*----------------------------------------------------------------------*
35 * extern void cleanutent (void);
36 * extern void makeutent (const char * pty, const char * hostname);
39 * rxvt_update_wtmp ();
40 *----------------------------------------------------------------------*/
42 #include "../config.h" /* NECESSARY */
43 #include "rxvt.h" /* NECESSARY */
48 * BSD style utmp entry
49 * ut_line, ut_name, ut_host, ut_time
50 * SYSV style utmp (and utmpx) entry
51 * ut_user, ut_id, ut_line, ut_pid, ut_type, ut_exit, ut_time
54 /* ------------------------------------------------------------------------- */
57 * make and write utmp and wtmp entries
60 rxvt_term::makeutent (const char *pty, const char *hostname)
62 #ifdef HAVE_STRUCT_UTMP
63 struct utmp *ut = &this->ut;
65 #ifdef HAVE_STRUCT_UTMPX
66 struct utmpx *utx = &this->utx;
71 struct passwd *pwent = getpwuid (getuid ());
73 if (!strncmp (pty, "/dev/", 5))
74 pty += 5; /* skip /dev/ prefix */
76 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
77 strncpy (ut_id, (pty + 3), sizeof (ut_id));
79 else if (sscanf (pty, "pts/%d", &i) == 1)
80 sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */
82 else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3))
84 rxvt_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty);
88 #ifdef HAVE_STRUCT_UTMP
89 memset (ut, 0, sizeof (struct utmp));
92 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
93 ut->ut_type = DEAD_PROCESS;
94 getutid (ut); /* position to entry in utmp file */
95 strncpy (ut_id, ut_id, sizeof (ut_id));
99 #ifdef HAVE_STRUCT_UTMPX
100 memset (utx, 0, sizeof (struct utmpx));
102 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
103 utx->ut_type = DEAD_PROCESS;
104 getutxid (utx); /* position to entry in utmp file */
105 strncpy (ut_id, ut_id, sizeof (ut_id));
108 #ifdef HAVE_STRUCT_UTMP
109 strncpy (ut->ut_line, pty, sizeof (ut->ut_line));
110 ut->ut_time = time (NULL);
111 # ifdef HAVE_UTMP_PID
112 strncpy (ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
113 sizeof (ut->ut_user));
114 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
115 ut->ut_time = time (NULL);
116 ut->ut_pid = cmd_pid;
117 # ifdef HAVE_UTMP_HOST
118 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
120 ut->ut_type = USER_PROCESS;
122 endutent (); /* close the file */
125 strncpy (ut->ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
126 sizeof (ut->ut_name));
127 # ifdef HAVE_UTMP_HOST
128 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
133 #ifdef HAVE_STRUCT_UTMPX
134 strncpy (utx->ut_line, pty, sizeof (utx->ut_line));
135 strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
136 sizeof (utx->ut_user));
137 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
138 utx->ut_session = getsid (0);
139 utx->ut_tv.tv_sec = time (NULL);
140 utx->ut_tv.tv_usec = 0;
141 utx->ut_pid = cmd_pid;
142 # ifdef HAVE_UTMPX_HOST
143 strncpy (utx->ut_host, hostname, sizeof (utx->ut_host));
148 if ((colon = strrchr (ut->ut_host, ':')) != NULL)
153 utx->ut_type = USER_PROCESS;
155 endutxent (); /* close the file */
159 #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
165 if (rxvt_write_bsd_utmp (i, ut))
170 if ((fd0 = fopen (TTYTAB_FILENAME, "r")) != NULL)
172 char buf[256], name[256];
174 buf[sizeof (buf) - 1] = '\0';
175 for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);)
177 if (*buf == '#' || sscanf (buf, "%s", name) != 1)
179 if (!strcmp (ut->ut_line, name))
181 if (!rxvt_write_bsd_utmp (i, ut))
197 # ifdef WTMP_ONLY_ON_LOGIN
198 if (options & Opt_loginShell)
201 # ifdef HAVE_STRUCT_UTMP
203 updwtmp (RXVT_WTMP_FILE, ut);
205 rxvt_update_wtmp (RXVT_WTMP_FILE, ut);
208 # ifdef HAVE_STRUCT_UTMPX
209 updwtmpx (RXVT_WTMPX_FILE, utx);
213 #if defined(LASTLOG_SUPPORT) && defined(RXVT_LASTLOG_FILE)
214 if (options & Opt_loginShell)
215 rxvt_update_lastlog (RXVT_LASTLOG_FILE, pty, hostname);
219 /* ------------------------------------------------------------------------- */
221 * remove utmp and wtmp entries
224 rxvt_term::cleanutent ()
226 #ifdef HAVE_STRUCT_UTMP
227 struct utmp *ut = &this->ut;
229 #ifdef HAVE_STRUCT_UTMPX
230 struct utmpx *tmputx, *utx = &this->utx;
233 #ifdef HAVE_STRUCT_UTMP
234 # ifdef HAVE_UTMP_PID
235 memset (ut, 0, sizeof (struct utmp));
237 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
238 ut->ut_type = USER_PROCESS;
240 struct utmp *tmput = getutid (ut);
242 if (tmput) /* position to entry in utmp file */
245 ut->ut_type = DEAD_PROCESS;
247 memset (ut->ut_name, 0, sizeof (ut->ut_name));
248 # ifdef HAVE_UTMP_HOST
249 memset (ut->ut_host, 0, sizeof (ut->ut_host));
252 ut->ut_time = time (NULL);
255 #ifdef HAVE_STRUCT_UTMPX
256 memset (utx, 0, sizeof (struct utmpx));
258 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
259 utx->ut_type = USER_PROCESS;
260 if ((tmputx = getutxid (utx))) /* position to entry in utmp file */
262 utx->ut_type = DEAD_PROCESS;
263 utx->ut_session = getsid (0);
264 utx->ut_tv.tv_sec = time (NULL);
265 utx->ut_tv.tv_usec = 0;
269 * Write ending wtmp entry
272 # ifdef WTMP_ONLY_ON_LOGIN
273 if (options & Opt_loginShell)
276 # ifdef HAVE_STRUCT_UTMP
278 updwtmp (RXVT_WTMP_FILE, ut);
280 rxvt_update_wtmp (RXVT_WTMP_FILE, ut);
283 # ifdef HAVE_STRUCT_UTMPX
284 updwtmpx (RXVT_WTMPX_FILE, utx);
292 #ifdef HAVE_STRUCT_UTMP
293 # ifdef HAVE_UTMP_PID
294 if (ut->ut_pid == cmd_pid)
298 memset (ut, 0, sizeof (struct utmp));
299 rxvt_write_bsd_utmp (utmp_pos, ut);
302 #ifdef HAVE_STRUCT_UTMPX
303 if (utx->ut_pid == cmd_pid)
309 /* ------------------------------------------------------------------------- */
311 * Write a BSD style utmp entry
316 rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu)
320 if (utmp_pos <= 0 || (fd = open (RXVT_UTMP_FILE, O_WRONLY)) == -1)
323 if (lseek (fd, (off_t) (utmp_pos * sizeof (struct utmp)), SEEK_SET) != -1)
324 write (fd, wu, sizeof (struct utmp));
330 /* ------------------------------------------------------------------------- */
332 * Update a BSD style wtmp entry
334 #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP)
337 rxvt_update_wtmp (const char *fname, const struct utmp *putmp)
339 int fd, gotlock, retry;
340 struct flock lck; /* fcntl locking scheme */
343 if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0)
346 lck.l_whence = SEEK_END; /* start lock at current eof */
347 lck.l_len = 0; /* end at ``largest possible eof'' */
349 lck.l_type = F_WRLCK; /* we want a write lock */
351 /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */
352 for (retry = 10, gotlock = 0; retry--;)
353 if (fcntl (fd, F_SETLK, &lck) != -1)
358 else if (errno != EAGAIN && errno != EACCES)
365 if (fstat (fd, &sbuf) == 0)
366 if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp))
367 ftruncate (fd, sbuf.st_size); /* remove bad writes */
369 lck.l_type = F_UNLCK; /* unlocking the file */
370 fcntl (fd, F_SETLK, &lck);
375 /* ------------------------------------------------------------------------- */
376 #ifdef LASTLOG_SUPPORT
379 rxvt_update_lastlog (const char *fname, const char *pty, const char *host)
381 # ifdef HAVE_STRUCT_LASTLOGX
384 # ifdef HAVE_STRUCT_LASTLOG
387 # ifdef LASTLOG_IS_DIR
388 char lastlogfile[256];
390 struct passwd *pwent;
393 # ifdef HAVE_STRUCT_LASTLOGX
394 memset (&llx, 0, sizeof (llx));
395 llx.ll_tv.tv_sec = time (NULL);
396 llx.ll_tv.tv_usec = 0;
397 strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
398 strncpy (llx.ll_host, host, sizeof (llx.ll_host));
399 updlastlogx (RXVT_LASTLOGX_FILE, getuid (), &llx);
402 # ifdef HAVE_STRUCT_LASTLOG
403 pwent = getpwuid (getuid ());
406 rxvt_warn ("no entry in password file, not updating lastlog.\n");
410 memset (&ll, 0, sizeof (ll));
411 ll.ll_time = time (NULL);
412 strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
413 strncpy (ll.ll_host, host, sizeof (ll.ll_host));
414 # ifdef LASTLOG_IS_DIR
415 sprintf (lastlogfile, "%.*s/%.*s",
416 sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2, fname,
417 sizeof (pwent->pw_name),
418 (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown"
420 if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0)
422 write (fd, &ll, sizeof (ll));
426 if ((fd = open (fname, O_RDWR)) != -1)
428 if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)),
430 write (fd, &ll, sizeof (ll));
433 # endif /* LASTLOG_IS_DIR */
434 # endif /* HAVE_STRUCT_LASTLOG */
436 #endif /* LASTLOG_SUPPORT */
437 /* ------------------------------------------------------------------------- */
439 #endif /* UTMP_SUPPORT */