ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/logging.C
(Generate patch)

Comparing rxvt-unicode/src/logging.C (file contents):
Revision 1.37 by root, Sun Jan 22 01:00:46 2006 UTC vs.
Revision 1.45 by ayin, Wed Dec 12 09:44:52 2007 UTC

16 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com> 16 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
17 * - extensive modifications 17 * - extensive modifications
18 * Copyright (c) 1999 D J Hawkey Jr <hawkeyd@visi.com> 18 * Copyright (c) 1999 D J Hawkey Jr <hawkeyd@visi.com>
19 * - lastlog support 19 * - lastlog support
20 * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com> 20 * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com>
21 * Copyright (c) 2006 Emanuele Giaquinta <e.giaquinta@glauco.it>
21 * 22 *
22 * This program is free software; you can redistribute it and/or modify 23 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by 24 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or 25 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version. 26 * (at your option) any later version.
38 39
39#include "ptytty.h" 40#include "ptytty.h"
40 41
41#if UTMP_SUPPORT 42#if UTMP_SUPPORT
42 43
43#if HAVE_STRUCT_UTMP 44#include <cstdio>
44static int write_bsd_utmp (int utmp_pos, struct utmp *wu);
45static void update_wtmp (const char *fname, const struct utmp *putmp);
46#endif
47 45
48static void update_lastlog (const char *fname, const char *pty, const char *host); 46#include <sys/types.h>
47#include <sys/stat.h>
48#include <fcntl.h>
49#include <unistd.h>
50#include <time.h>
49 51
50/* 52/*
51 * BSD style utmp entry 53 * BSD style utmp entry
52 * ut_line, ut_name, ut_host, ut_time 54 * ut_line, ut_name, ut_host, ut_time
53 * SYSV style utmp (and utmpx) entry 55 * SYSV style utmp (and utmpx) entry
54 * ut_user, ut_id, ut_line, ut_pid, ut_type, ut_exit, ut_time 56 * ut_user, ut_id, ut_line, ut_pid, ut_type, ut_exit, ut_time
55 */ 57 */
56 58
57/* ------------------------------------------------------------------------- */ 59/* ------------------------------------------------------------------------- */
60/*
61 * Write a BSD style utmp entry
62 */
63#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
64static int
65write_bsd_utmp (int utmp_pos, struct utmp *wu)
66{
67 int fd;
68
69 if (utmp_pos <= 0 || (fd = open (UTMP_FILE, O_WRONLY)) == -1)
70 return 0;
71
72 if (lseek (fd, (off_t) (utmp_pos * sizeof (struct utmp)), SEEK_SET) != -1)
73 write (fd, wu, sizeof (struct utmp));
74 close (fd);
75 return 1;
76}
77#endif
78
79/* ------------------------------------------------------------------------- */
80/*
81 * Update a BSD style wtmp entry
82 */
83#if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP)
84static void
85update_wtmp (const char *fname, const struct utmp *putmp)
86{
87 int fd, gotlock, retry;
88 struct flock lck; /* fcntl locking scheme */
89 struct stat sbuf;
90
91 if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0)
92 return;
93
94 lck.l_whence = SEEK_END; /* start lock at current eof */
95 lck.l_len = 0; /* end at ``largest possible eof'' */
96 lck.l_start = 0;
97 lck.l_type = F_WRLCK; /* we want a write lock */
98
99 /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */
100 for (retry = 10, gotlock = 0; retry--;)
101 if (fcntl (fd, F_SETLK, &lck) != -1)
102 {
103 gotlock = 1;
104 break;
105 }
106 else if (errno != EAGAIN && errno != EACCES)
107 break;
108 if (!gotlock)
109 {
110 /* give it up */
111 close (fd);
112 return;
113 }
114 if (fstat (fd, &sbuf) == 0)
115 if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp))
116 ftruncate (fd, sbuf.st_size); /* remove bad writes */
117
118 lck.l_type = F_UNLCK; /* unlocking the file */
119 fcntl (fd, F_SETLK, &lck);
120 close (fd);
121}
122#endif
123
124/* ------------------------------------------------------------------------- */
125#ifdef LASTLOG_SUPPORT
126static void
127update_lastlog (const char *fname, const char *pty, const char *host)
128{
129# if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
130 struct lastlogx llx;
131# endif
132# ifdef HAVE_STRUCT_LASTLOG
133 int fd;
134 struct lastlog ll;
135 char lastlogfile[256];
136 struct passwd *pwent;
137 struct stat st;
138# endif
139
140# if defined(HAVE_STRUCT_LASTLOGX) && defined(HAVE_UPDLASTLOGX)
141 memset (&llx, 0, sizeof (llx));
142 llx.ll_tv.tv_sec = time (NULL);
143 llx.ll_tv.tv_usec = 0;
144 strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
145 strncpy (llx.ll_host, host, sizeof (llx.ll_host));
146 updlastlogx (LASTLOGX_FILE, getuid (), &llx);
147# endif
148
149# ifdef HAVE_STRUCT_LASTLOG
150 pwent = getpwuid (getuid ());
151 if (!pwent)
152 {
153 ptytty_warn ("no entry in password file, not updating lastlog.\n", 0);
154 return;
155 }
156
157 memset (&ll, 0, sizeof (ll));
158 ll.ll_time = time (NULL);
159 strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
160 strncpy (ll.ll_host, host, sizeof (ll.ll_host));
161 if (stat (fname, &st) != 0)
162 return;
163 if (S_ISDIR (st.st_mode))
164 {
165 sprintf (lastlogfile, "%.*s/%.*s",
166 (int)(sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2), fname,
167 (int)sizeof (pwent->pw_name),
168 (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown"
169 : pwent->pw_name);
170 if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0)
171 {
172 write (fd, &ll, sizeof (ll));
173 close (fd);
174 }
175 }
176 else if (S_ISREG (st.st_mode))
177 if ((fd = open (fname, O_RDWR)) != -1)
178 {
179 if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)),
180 SEEK_SET) != -1)
181 write (fd, &ll, sizeof (ll));
182 close (fd);
183 }
184# endif /* HAVE_STRUCT_LASTLOG */
185}
186#endif /* LASTLOG_SUPPORT */
187
188/* ------------------------------------------------------------------------- */
58 189
59/* 190/*
60 * make and write utmp and wtmp entries 191 * make and write utmp and wtmp entries
61 */ 192 */
62void 193void
76#ifdef HAVE_STRUCT_UTMPX 207#ifdef HAVE_STRUCT_UTMPX
77 struct utmpx *utx = &this->utx; 208 struct utmpx *utx = &this->utx;
78#endif 209#endif
79 int i; 210 int i;
80 struct passwd *pwent = getpwuid (getuid ()); 211 struct passwd *pwent = getpwuid (getuid ());
212 const char *name = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
81 213
82 if (!strncmp (pty, "/dev/", 5)) 214 if (!strncmp (pty, "/dev/", 5))
83 pty += 5; /* skip /dev/ prefix */ 215 pty += 5; /* skip /dev/ prefix */
84 216
85#if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX) 217#if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
86 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3)) 218 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
87 strncpy (ut_id, pty + 3, sizeof (ut_id)); 219 strncpy (ut_id, pty + 3, sizeof (ut_id));
88 else if (sscanf (pty, "pts/%d", &i) == 1) 220 else if (sscanf (pty, "pts/%d", &i) == 1)
89 sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ 221 sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */
90 else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3)) 222 else
91 { 223 {
92 warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty); 224 ptytty_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty);
93 return; 225 return;
94 } 226 }
95#endif 227#endif
96 228
97#ifdef HAVE_STRUCT_UTMP 229#ifdef HAVE_STRUCT_UTMP
99# ifdef HAVE_UTMP_PID 231# ifdef HAVE_UTMP_PID
100 setutent (); 232 setutent ();
101 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); 233 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
102 ut->ut_type = DEAD_PROCESS; 234 ut->ut_type = DEAD_PROCESS;
103 getutid (ut); /* position to entry in utmp file */ 235 getutid (ut); /* position to entry in utmp file */
104 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
105# endif 236# endif
106#endif 237#endif
107 238
108#ifdef HAVE_STRUCT_UTMPX 239#ifdef HAVE_STRUCT_UTMPX
109 memset (utx, 0, sizeof (struct utmpx)); 240 memset (utx, 0, sizeof (struct utmpx));
110 setutxent (); 241 setutxent ();
111 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); 242 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
112 utx->ut_type = DEAD_PROCESS; 243 utx->ut_type = DEAD_PROCESS;
113 getutxid (utx); /* position to entry in utmp file */ 244 getutxid (utx); /* position to entry in utmp file */
114 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
115#endif 245#endif
116 246
117#ifdef HAVE_STRUCT_UTMP 247#ifdef HAVE_STRUCT_UTMP
118 strncpy (ut->ut_line, pty, sizeof (ut->ut_line)); 248 strncpy (ut->ut_line, pty, sizeof (ut->ut_line));
249# ifdef HAVE_UTMP_HOST
250 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
251# endif
119 ut->ut_time = time (NULL); 252 ut->ut_time = time (NULL);
120# ifdef HAVE_UTMP_PID 253# ifdef HAVE_UTMP_PID
121 strncpy (ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", 254 strncpy (ut->ut_user, name, sizeof (ut->ut_user));
122 sizeof (ut->ut_user));
123 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); 255 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
124 ut->ut_time = time (NULL);
125 ut->ut_pid = cmd_pid; 256 ut->ut_pid = cmd_pid;
126# ifdef HAVE_UTMP_HOST
127 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
128# endif
129 ut->ut_type = USER_PROCESS; 257 ut->ut_type = USER_PROCESS;
130 pututline (ut); 258 pututline (ut);
131 endutent (); /* close the file */ 259 endutent (); /* close the file */
132 utmp_pos = 0; 260 utmp_pos = 0;
133# else 261# else
134 strncpy (ut->ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
135 sizeof (ut->ut_name));
136# ifdef HAVE_UTMP_HOST
137 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host)); 262 strncpy (ut->ut_name, name, sizeof (ut->ut_name));
138# endif
139# endif 263# endif
140#endif 264#endif
141 265
142#ifdef HAVE_STRUCT_UTMPX 266#ifdef HAVE_STRUCT_UTMPX
143 strncpy (utx->ut_line, pty, sizeof (utx->ut_line)); 267 strncpy (utx->ut_line, pty, sizeof (utx->ut_line));
144 strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", 268 strncpy (utx->ut_user, name, sizeof (utx->ut_user));
145 sizeof (utx->ut_user));
146 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); 269 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
147# if HAVE_UTMPX_SESSION 270# if HAVE_UTMPX_SESSION
148 utx->ut_session = getsid (0); 271 utx->ut_session = getsid (0);
149# endif 272# endif
150 utx->ut_tv.tv_sec = time (NULL); 273 utx->ut_tv.tv_sec = time (NULL);
167 utmp_pos = 0; 290 utmp_pos = 0;
168#endif 291#endif
169 292
170#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) 293#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
171 { 294 {
172# ifdef HAVE_TTYSLOT 295# if 1
173 int fdstdin = dup (STDIN_FILENO); 296 int fdstdin = dup (STDIN_FILENO);
174 dup2 (tty, STDIN_FILENO); 297 dup2 (tty, STDIN_FILENO);
175 298
176 i = ttyslot (); 299 i = ttyslot ();
177 if (write_bsd_utmp (i, ut)) 300 if (write_bsd_utmp (i, ut))
178 utmp_pos = i; 301 utmp_pos = i;
179 302
180 dup2 (fdstdin, STDIN_FILENO); 303 dup2 (fdstdin, STDIN_FILENO);
181 close (fdstdin); 304 close (fdstdin);
182# else
183 FILE *fd0;
184
185 if ((fd0 = fopen (TTYTAB_FILENAME, "r")) != NULL)
186 {
187 char buf[256], name[256];
188
189 buf[sizeof (buf) - 1] = '\0';
190 for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);)
191 {
192 if (*buf == '#' || sscanf (buf, "%s", name) != 1)
193 continue;
194 if (!strcmp (ut->ut_line, name))
195 {
196 if (!write_bsd_utmp (i, ut))
197 i = 0;
198 utmp_pos = i;
199 fclose (fd0);
200 break;
201 }
202 i++;
203 }
204 fclose (fd0);
205 }
206# endif 305# endif
207 } 306 }
208#endif 307#endif
209 308
210#ifdef WTMP_SUPPORT 309#ifdef WTMP_SUPPORT
323#endif 422#endif
324 423
325 cmd_pid = 0; 424 cmd_pid = 0;
326} 425}
327 426
328/* ------------------------------------------------------------------------- */
329/*
330 * Write a BSD style utmp entry
331 */
332#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
333static int
334write_bsd_utmp (int utmp_pos, struct utmp *wu)
335{
336 int fd;
337
338 if (utmp_pos <= 0 || (fd = open (UTMP_FILE, O_WRONLY)) == -1)
339 return 0;
340
341 if (lseek (fd, (off_t) (utmp_pos * sizeof (struct utmp)), SEEK_SET) != -1)
342 write (fd, wu, sizeof (struct utmp));
343 close (fd);
344 return 1;
345}
346#endif
347
348/* ------------------------------------------------------------------------- */
349/*
350 * Update a BSD style wtmp entry
351 */
352#if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP)
353static void
354update_wtmp (const char *fname, const struct utmp *putmp)
355{
356 int fd, gotlock, retry;
357 struct flock lck; /* fcntl locking scheme */
358 struct stat sbuf;
359
360 if ((fd = open (fname, O_WRONLY | O_APPEND, 0)) < 0)
361 return;
362
363 lck.l_whence = SEEK_END; /* start lock at current eof */
364 lck.l_len = 0; /* end at ``largest possible eof'' */
365 lck.l_start = 0;
366 lck.l_type = F_WRLCK; /* we want a write lock */
367
368 /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */
369 for (retry = 10, gotlock = 0; retry--;)
370 if (fcntl (fd, F_SETLK, &lck) != -1)
371 {
372 gotlock = 1;
373 break;
374 }
375 else if (errno != EAGAIN && errno != EACCES)
376 break;
377 if (!gotlock)
378 { /* give it up */
379 close (fd);
380 return;
381 }
382 if (fstat (fd, &sbuf) == 0)
383 if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp))
384 ftruncate (fd, sbuf.st_size); /* remove bad writes */
385
386 lck.l_type = F_UNLCK; /* unlocking the file */
387 fcntl (fd, F_SETLK, &lck);
388 close (fd);
389}
390#endif
391
392/* ------------------------------------------------------------------------- */
393#ifdef LASTLOG_SUPPORT
394static void
395update_lastlog (const char *fname, const char *pty, const char *host)
396{
397# ifdef HAVE_STRUCT_LASTLOGX
398 struct lastlogx llx;
399# endif
400# ifdef HAVE_STRUCT_LASTLOG
401 int fd;
402 struct lastlog ll;
403# ifdef LASTLOG_IS_DIR
404 char lastlogfile[256];
405# endif
406 struct passwd *pwent;
407# endif
408
409# ifdef HAVE_STRUCT_LASTLOGX
410 memset (&llx, 0, sizeof (llx));
411 llx.ll_tv.tv_sec = time (NULL);
412 llx.ll_tv.tv_usec = 0;
413 strncpy (llx.ll_line, pty, sizeof (llx.ll_line));
414 strncpy (llx.ll_host, host, sizeof (llx.ll_host));
415 updlastlogx (LASTLOGX_FILE, getuid (), &llx);
416# endif
417
418# ifdef HAVE_STRUCT_LASTLOG
419 pwent = getpwuid (getuid ());
420 if (!pwent)
421 {
422 warn ("no entry in password file, not updating lastlog.\n");
423 return;
424 }
425
426 memset (&ll, 0, sizeof (ll));
427 ll.ll_time = time (NULL);
428 strncpy (ll.ll_line, pty, sizeof (ll.ll_line));
429 strncpy (ll.ll_host, host, sizeof (ll.ll_host));
430# ifdef LASTLOG_IS_DIR
431 sprintf (lastlogfile, "%.*s/%.*s",
432 sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2, fname,
433 sizeof (pwent->pw_name),
434 (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown"
435 : pwent->pw_name);
436 if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0)
437 {
438 write (fd, &ll, sizeof (ll));
439 close (fd);
440 }
441# else
442 if ((fd = open (fname, O_RDWR)) != -1)
443 {
444 if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)),
445 SEEK_SET) != -1)
446 write (fd, &ll, sizeof (ll));
447 close (fd);
448 }
449# endif /* LASTLOG_IS_DIR */
450# endif /* HAVE_STRUCT_LASTLOG */
451}
452#endif /* LASTLOG_SUPPORT */
453
454#else 427#else
455void 428void
456ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname) 429ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
457{ 430{
458} 431}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines