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.40 by root, Sun Jan 22 12:23:55 2006 UTC vs.
Revision 1.47 by root, Sun May 3 21:48:53 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines