ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/logging.C
Revision: 1.21
Committed: Sat Jan 14 10:17:40 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.20: +4 -4 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*--------------------------------*-C-*---------------------------------*
2 pcg 1.9 * File: logging.C
3 pcg 1.1 *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6     * Copyright (c) 1992 John Bovey <jdb@ukc.ac.uk>
7     * - original version
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>
16     * - lastlog support
17 pcg 1.9 * Copyright (c) 2004 Marc Lehmann <pcg@goof.com>
18 pcg 1.1 *
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.
23     *
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.
28     *
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     /*----------------------------------------------------------------------*
34     * Public:
35     * extern void cleanutent (void);
36     * extern void makeutent (const char * pty, const char * hostname);
37     *
38     * Private:
39     * rxvt_update_wtmp ();
40     *----------------------------------------------------------------------*/
41    
42 root 1.20 #include "../config.h"
43     #include "rxvt.h"
44 pcg 1.1 #include "logging.h"
45 root 1.20
46 pcg 1.1 #ifdef UTMP_SUPPORT
47    
48 root 1.16 #if HAVE_STRUCT_UTMP
49 root 1.14 int rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu);
50     void rxvt_update_wtmp (const char *fname, const struct utmp *putmp);
51 root 1.16 #endif
52    
53 root 1.14 void rxvt_update_lastlog (const char *fname, const char *pty, const char *host);
54    
55 pcg 1.1 /*
56     * BSD style utmp entry
57     * ut_line, ut_name, ut_host, ut_time
58     * SYSV style utmp (and utmpx) entry
59     * ut_user, ut_id, ut_line, ut_pid, ut_type, ut_exit, ut_time
60     */
61    
62     /* ------------------------------------------------------------------------- */
63    
64     /*
65     * make and write utmp and wtmp entries
66     */
67     void
68 pcg 1.4 rxvt_term::makeutent (const char *pty, const char *hostname)
69 pcg 1.1 {
70     #ifdef HAVE_STRUCT_UTMP
71 root 1.12 struct utmp *ut = &this->ut;
72 pcg 1.1 #endif
73     #ifdef HAVE_STRUCT_UTMPX
74 root 1.12 struct utmpx *utx = &this->utx;
75 pcg 1.1 #endif
76     #ifdef HAVE_UTMP_PID
77 root 1.12 int i;
78 pcg 1.1 #endif
79 root 1.17 struct passwd *pwent = getpwuid (getuid ());
80 pcg 1.1
81 root 1.11 if (!strncmp (pty, "/dev/", 5))
82 pcg 1.6 pty += 5; /* skip /dev/ prefix */
83 pcg 1.1
84 root 1.13 #ifdef HAVE_UTMP_PID
85 root 1.11 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
86 root 1.13 strncpy (ut_id, pty + 3, sizeof (ut_id));
87 pcg 1.7 else if (sscanf (pty, "pts/%d", &i) == 1)
88     sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */
89 root 1.11 else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3))
90 pcg 1.6 {
91 pcg 1.8 rxvt_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty);
92 pcg 1.6 return;
93 pcg 1.1 }
94 root 1.13 #endif
95 pcg 1.1
96     #ifdef HAVE_STRUCT_UTMP
97 root 1.11 memset (ut, 0, sizeof (struct utmp));
98 pcg 1.1 # ifdef HAVE_UTMP_PID
99 pcg 1.7 setutent ();
100 root 1.11 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
101 pcg 1.6 ut->ut_type = DEAD_PROCESS;
102 pcg 1.7 getutid (ut); /* position to entry in utmp file */
103 root 1.21 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
104 pcg 1.1 # endif
105     #endif
106    
107     #ifdef HAVE_STRUCT_UTMPX
108 root 1.11 memset (utx, 0, sizeof (struct utmpx));
109 pcg 1.7 setutxent ();
110 root 1.11 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
111 pcg 1.6 utx->ut_type = DEAD_PROCESS;
112 pcg 1.7 getutxid (utx); /* position to entry in utmp file */
113 root 1.21 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
114 pcg 1.1 #endif
115    
116     #ifdef HAVE_STRUCT_UTMP
117 root 1.11 strncpy (ut->ut_line, pty, sizeof (ut->ut_line));
118 pcg 1.7 ut->ut_time = time (NULL);
119 pcg 1.1 # ifdef HAVE_UTMP_PID
120 root 1.11 strncpy (ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
121 pcg 1.7 sizeof (ut->ut_user));
122 root 1.11 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
123 pcg 1.7 ut->ut_time = time (NULL);
124 pcg 1.6 ut->ut_pid = cmd_pid;
125 pcg 1.1 # ifdef HAVE_UTMP_HOST
126 root 1.11 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
127 pcg 1.1 # endif
128 pcg 1.6 ut->ut_type = USER_PROCESS;
129 pcg 1.7 pututline (ut);
130     endutent (); /* close the file */
131 pcg 1.6 utmp_pos = 0;
132 pcg 1.1 # else
133 root 1.11 strncpy (ut->ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
134 pcg 1.7 sizeof (ut->ut_name));
135 pcg 1.1 # ifdef HAVE_UTMP_HOST
136 root 1.11 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
137 pcg 1.1 # endif
138     # endif
139     #endif
140    
141     #ifdef HAVE_STRUCT_UTMPX
142 root 1.11 strncpy (utx->ut_line, pty, sizeof (utx->ut_line));
143     strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
144 pcg 1.7 sizeof (utx->ut_user));
145 root 1.11 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
146 root 1.15 # if HAVE_UTMPX_SESSION
147 pcg 1.7 utx->ut_session = getsid (0);
148 root 1.15 # endif
149 pcg 1.7 utx->ut_tv.tv_sec = time (NULL);
150 pcg 1.6 utx->ut_tv.tv_usec = 0;
151     utx->ut_pid = cmd_pid;
152 pcg 1.1 # ifdef HAVE_UTMPX_HOST
153 root 1.11 strncpy (utx->ut_host, hostname, sizeof (utx->ut_host));
154 pcg 1.1 # if 0
155 pcg 1.6 {
156     char *colon;
157 pcg 1.1
158 root 1.11 if ((colon = strrchr (ut->ut_host, ':')) != NULL)
159 pcg 1.6 *colon = '\0';
160     }
161 pcg 1.1 # endif
162     # endif
163 pcg 1.6 utx->ut_type = USER_PROCESS;
164 pcg 1.7 pututxline (utx);
165     endutxent (); /* close the file */
166 pcg 1.6 utmp_pos = 0;
167 pcg 1.1 #endif
168    
169     #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
170 pcg 1.6
171     {
172     int i;
173 pcg 1.1 # ifdef HAVE_TTYSLOT
174 pcg 1.7 i = ttyslot ();
175     if (rxvt_write_bsd_utmp (i, ut))
176 pcg 1.6 utmp_pos = i;
177 pcg 1.1 # else
178 pcg 1.6 FILE *fd0;
179 pcg 1.1
180 pcg 1.7 if ((fd0 = fopen (TTYTAB_FILENAME, "r")) != NULL)
181 pcg 1.6 {
182 root 1.18 char buf[256], name[256];
183 pcg 1.6
184 pcg 1.7 buf[sizeof (buf) - 1] = '\0';
185     for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);)
186 pcg 1.6 {
187 pcg 1.7 if (*buf == '#' || sscanf (buf, "%s", name) != 1)
188 pcg 1.6 continue;
189 root 1.11 if (!strcmp (ut->ut_line, name))
190 pcg 1.6 {
191 pcg 1.7 if (!rxvt_write_bsd_utmp (i, ut))
192 pcg 1.6 i = 0;
193     utmp_pos = i;
194 pcg 1.7 fclose (fd0);
195 pcg 1.6 break;
196     }
197     i++;
198     }
199 pcg 1.7 fclose (fd0);
200 pcg 1.6 }
201     # endif
202 pcg 1.1
203 pcg 1.6 }
204 pcg 1.1 #endif
205    
206     #ifdef WTMP_SUPPORT
207     # ifdef WTMP_ONLY_ON_LOGIN
208 root 1.19 if (OPTION (Opt_loginShell))
209 pcg 1.1 # endif
210     {
211     # ifdef HAVE_STRUCT_UTMP
212     # ifdef HAVE_UPDWTMP
213 pcg 1.7 updwtmp (RXVT_WTMP_FILE, ut);
214 pcg 1.1 # else
215 pcg 1.7 rxvt_update_wtmp (RXVT_WTMP_FILE, ut);
216 pcg 1.1 # endif
217     # endif
218     # ifdef HAVE_STRUCT_UTMPX
219 root 1.15 # if HAVE_UPDWTMPX
220 pcg 1.7 updwtmpx (RXVT_WTMPX_FILE, utx);
221 root 1.16 # else
222     pututxline (utx);
223 root 1.15 # endif
224 pcg 1.1 # endif
225     }
226     #endif
227     #if defined(LASTLOG_SUPPORT) && defined(RXVT_LASTLOG_FILE)
228 root 1.19 if (OPTION (Opt_loginShell))
229 pcg 1.7 rxvt_update_lastlog (RXVT_LASTLOG_FILE, pty, hostname);
230 pcg 1.1 #endif
231     }
232    
233     /* ------------------------------------------------------------------------- */
234     /*
235     * remove utmp and wtmp entries
236     */
237     void
238 pcg 1.4 rxvt_term::cleanutent ()
239 pcg 1.1 {
240     #ifdef HAVE_STRUCT_UTMP
241 root 1.12 struct utmp *ut = &this->ut;
242 pcg 1.1 #endif
243     #ifdef HAVE_STRUCT_UTMPX
244 root 1.12 struct utmpx *tmputx, *utx = &this->utx;
245 pcg 1.1 #endif
246    
247     #ifdef HAVE_STRUCT_UTMP
248     # ifdef HAVE_UTMP_PID
249 root 1.11 memset (ut, 0, sizeof (struct utmp));
250 pcg 1.7 setutent ();
251 root 1.21 strncpy (ut->ut_id, this->ut_id, sizeof (ut->ut_id));
252 pcg 1.6 ut->ut_type = USER_PROCESS;
253     {
254 pcg 1.7 struct utmp *tmput = getutid (ut);
255 pcg 1.6
256     if (tmput) /* position to entry in utmp file */
257     ut = tmput;
258     }
259     ut->ut_type = DEAD_PROCESS;
260     # else
261 root 1.11 memset (ut->ut_name, 0, sizeof (ut->ut_name));
262 pcg 1.1 # ifdef HAVE_UTMP_HOST
263 root 1.11 memset (ut->ut_host, 0, sizeof (ut->ut_host));
264 pcg 1.1 # endif
265     # endif
266 pcg 1.7 ut->ut_time = time (NULL);
267 pcg 1.1 #endif
268    
269     #ifdef HAVE_STRUCT_UTMPX
270 root 1.11 memset (utx, 0, sizeof (struct utmpx));
271 pcg 1.7 setutxent ();
272 root 1.21 strncpy (utx->ut_id, this->ut_id, sizeof (utx->ut_id));
273 pcg 1.6 utx->ut_type = USER_PROCESS;
274 pcg 1.7 if ((tmputx = getutxid (utx))) /* position to entry in utmp file */
275 pcg 1.6 utx = tmputx;
276     utx->ut_type = DEAD_PROCESS;
277 root 1.15 # if HAVE_UTMPX_SESSION
278 pcg 1.7 utx->ut_session = getsid (0);
279 root 1.15 # endif
280 pcg 1.7 utx->ut_tv.tv_sec = time (NULL);
281 pcg 1.6 utx->ut_tv.tv_usec = 0;
282 pcg 1.1 #endif
283    
284 pcg 1.6 /*
285     * Write ending wtmp entry
286     */
287 pcg 1.1 #ifdef WTMP_SUPPORT
288     # ifdef WTMP_ONLY_ON_LOGIN
289 root 1.19 if (OPTION (Opt_loginShell))
290 pcg 1.1 # endif
291     {
292     # ifdef HAVE_STRUCT_UTMP
293     # ifdef HAVE_UPDWTMP
294 pcg 1.7 updwtmp (RXVT_WTMP_FILE, ut);
295 pcg 1.1 # else
296 pcg 1.7 rxvt_update_wtmp (RXVT_WTMP_FILE, ut);
297 pcg 1.1 # endif
298     # endif
299     # ifdef HAVE_STRUCT_UTMPX
300 root 1.15 # if HAVE_UPDWTMPX
301 pcg 1.7 updwtmpx (RXVT_WTMPX_FILE, utx);
302 root 1.16 # else
303     pututxline (utx);
304 root 1.15 # endif
305 pcg 1.1 # endif
306     }
307     #endif
308    
309 pcg 1.6 /*
310     * Write utmp entry
311     */
312 pcg 1.1 #ifdef HAVE_STRUCT_UTMP
313     # ifdef HAVE_UTMP_PID
314 pcg 1.6 if (ut->ut_pid == cmd_pid)
315 pcg 1.7 pututline (ut);
316     endutent ();
317 pcg 1.1 # else
318 root 1.11 memset (ut, 0, sizeof (struct utmp));
319 pcg 1.7 rxvt_write_bsd_utmp (utmp_pos, ut);
320 pcg 1.1 # endif
321     #endif
322     #ifdef HAVE_STRUCT_UTMPX
323 pcg 1.6 if (utx->ut_pid == cmd_pid)
324 pcg 1.7 pututxline (utx);
325     endutxent ();
326 pcg 1.1 #endif
327     }
328    
329     /* ------------------------------------------------------------------------- */
330     /*
331     * Write a BSD style utmp entry
332     */
333     #ifdef HAVE_UTMP_H
334     int
335 pcg 1.7 rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu)
336 pcg 1.1 {
337 pcg 1.6 int fd;
338 pcg 1.1
339 pcg 1.7 if (utmp_pos <= 0 || (fd = open (RXVT_UTMP_FILE, O_WRONLY)) == -1)
340 pcg 1.6 return 0;
341 pcg 1.1
342 pcg 1.7 if (lseek (fd, (off_t) (utmp_pos * sizeof (struct utmp)), SEEK_SET) != -1)
343     write (fd, wu, sizeof (struct utmp));
344     close (fd);
345 pcg 1.6 return 1;
346 pcg 1.1 }
347     #endif
348    
349     /* ------------------------------------------------------------------------- */
350     /*
351     * Update a BSD style wtmp entry
352     */
353 root 1.16 #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP)
354 pcg 1.1 void
355 pcg 1.7 rxvt_update_wtmp (const char *fname, const struct utmp *putmp)
356 pcg 1.1 {
357 pcg 1.6 int fd, gotlock, retry;
358     struct flock lck; /* fcntl locking scheme */
359     struct stat sbuf;
360    
361 pcg 1.7 if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0)
362 pcg 1.6 return;
363    
364     lck.l_whence = SEEK_END; /* start lock at current eof */
365     lck.l_len = 0; /* end at ``largest possible eof'' */
366     lck.l_start = 0;
367     lck.l_type = F_WRLCK; /* we want a write lock */
368    
369     /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */
370     for (retry = 10, gotlock = 0; retry--;)
371 pcg 1.7 if (fcntl (fd, F_SETLK, &lck) != -1)
372 pcg 1.6 {
373     gotlock = 1;
374     break;
375     }
376     else if (errno != EAGAIN && errno != EACCES)
377     break;
378     if (!gotlock)
379     { /* give it up */
380 pcg 1.7 close (fd);
381 pcg 1.6 return;
382     }
383 pcg 1.7 if (fstat (fd, &sbuf) == 0)
384     if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp))
385     ftruncate (fd, sbuf.st_size); /* remove bad writes */
386 pcg 1.6
387     lck.l_type = F_UNLCK; /* unlocking the file */
388 pcg 1.7 fcntl (fd, F_SETLK, &lck);
389     close (fd);
390 pcg 1.1 }
391     #endif
392    
393     /* ------------------------------------------------------------------------- */
394     #ifdef LASTLOG_SUPPORT
395     void
396 pcg 1.7 rxvt_update_lastlog (const char *fname, const char *pty, const char *host)
397 pcg 1.1 {
398     # ifdef HAVE_STRUCT_LASTLOGX
399 pcg 1.6 struct lastlogx llx;
400 pcg 1.1 # endif
401     # ifdef HAVE_STRUCT_LASTLOG
402 pcg 1.6 int fd;
403     struct lastlog ll;
404 pcg 1.1 # ifdef LASTLOG_IS_DIR
405 pcg 1.6 char lastlogfile[256];
406 pcg 1.1 # endif
407 pcg 1.6 struct passwd *pwent;
408 pcg 1.1 # endif
409    
410     # ifdef HAVE_STRUCT_LASTLOGX
411 root 1.11 memset (&llx, 0, sizeof (llx));
412 pcg 1.7 llx.ll_tv.tv_sec = time (NULL);
413 pcg 1.6 llx.ll_tv.tv_usec = 0;
414 root 1.11 strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
415     strncpy (llx.ll_host, host, sizeof (llx.ll_host));
416 pcg 1.7 updlastlogx (RXVT_LASTLOGX_FILE, getuid (), &llx);
417 pcg 1.1 # endif
418    
419     # ifdef HAVE_STRUCT_LASTLOG
420 pcg 1.7 pwent = getpwuid (getuid ());
421 pcg 1.6 if (!pwent)
422     {
423 pcg 1.8 rxvt_warn ("no entry in password file, not updating lastlog.\n");
424 pcg 1.6 return;
425 pcg 1.1 }
426 pcg 1.8
427 root 1.11 memset (&ll, 0, sizeof (ll));
428 pcg 1.7 ll.ll_time = time (NULL);
429 root 1.11 strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
430     strncpy (ll.ll_host, host, sizeof (ll.ll_host));
431 pcg 1.1 # ifdef LASTLOG_IS_DIR
432 pcg 1.7 sprintf (lastlogfile, "%.*s/%.*s",
433     sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2, fname,
434     sizeof (pwent->pw_name),
435 pcg 1.6 (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown"
436     : pwent->pw_name);
437 pcg 1.7 if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0)
438 pcg 1.6 {
439 pcg 1.7 write (fd, &ll, sizeof (ll));
440     close (fd);
441 pcg 1.1 }
442     # else
443 pcg 1.7 if ((fd = open (fname, O_RDWR)) != -1)
444 pcg 1.6 {
445 pcg 1.7 if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)),
446 pcg 1.6 SEEK_SET) != -1)
447 pcg 1.7 write (fd, &ll, sizeof (ll));
448     close (fd);
449 pcg 1.1 }
450     # endif /* LASTLOG_IS_DIR */
451     # endif /* HAVE_STRUCT_LASTLOG */
452     }
453     #endif /* LASTLOG_SUPPORT */
454     /* ------------------------------------------------------------------------- */
455    
456     #endif /* UTMP_SUPPORT */