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.44 by ayin, Tue Dec 11 21:49:12 2007 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>
41 45
42#include <sys/types.h> 46#include <sys/types.h>
43#include <sys/stat.h> 47#include <sys/stat.h>
44#include <sys/fcntl.h> 48#include <sys/fcntl.h>
45#include <unistd.h> 49#include <unistd.h>
46#include <time.h> 50#include <time.h>
47
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 51
59/* 52/*
60 * BSD style utmp entry 53 * BSD style utmp entry
61 * ut_line, ut_name, ut_host, ut_time 54 * ut_line, ut_name, ut_host, ut_time
62 * SYSV style utmp (and utmpx) entry 55 * SYSV style utmp (and utmpx) entry
63 * 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
64 */ 57 */
65 58
66/* ------------------------------------------------------------------------- */ 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/* ------------------------------------------------------------------------- */
67 189
68/* 190/*
69 * make and write utmp and wtmp entries 191 * make and write utmp and wtmp entries
70 */ 192 */
71void 193void
85#ifdef HAVE_STRUCT_UTMPX 207#ifdef HAVE_STRUCT_UTMPX
86 struct utmpx *utx = &this->utx; 208 struct utmpx *utx = &this->utx;
87#endif 209#endif
88 int i; 210 int i;
89 struct passwd *pwent = getpwuid (getuid ()); 211 struct passwd *pwent = getpwuid (getuid ());
212 const char *name = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
90 213
91 if (!strncmp (pty, "/dev/", 5)) 214 if (!strncmp (pty, "/dev/", 5))
92 pty += 5; /* skip /dev/ prefix */ 215 pty += 5; /* skip /dev/ prefix */
93 216
94#if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX) 217#if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
95 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3)) 218 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
96 strncpy (ut_id, pty + 3, sizeof (ut_id)); 219 strncpy (ut_id, pty + 3, sizeof (ut_id));
97 else if (sscanf (pty, "pts/%d", &i) == 1) 220 else if (sscanf (pty, "pts/%d", &i) == 1)
98 sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ 221 sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */
99 else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3)) 222 else
100 { 223 {
101 ptytty_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);
102 return; 225 return;
103 } 226 }
104#endif 227#endif
108# ifdef HAVE_UTMP_PID 231# ifdef HAVE_UTMP_PID
109 setutent (); 232 setutent ();
110 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); 233 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
111 ut->ut_type = DEAD_PROCESS; 234 ut->ut_type = DEAD_PROCESS;
112 getutid (ut); /* position to entry in utmp file */ 235 getutid (ut); /* position to entry in utmp file */
113 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
114# endif 236# endif
115#endif 237#endif
116 238
117#ifdef HAVE_STRUCT_UTMPX 239#ifdef HAVE_STRUCT_UTMPX
118 memset (utx, 0, sizeof (struct utmpx)); 240 memset (utx, 0, sizeof (struct utmpx));
119 setutxent (); 241 setutxent ();
120 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); 242 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
121 utx->ut_type = DEAD_PROCESS; 243 utx->ut_type = DEAD_PROCESS;
122 getutxid (utx); /* position to entry in utmp file */ 244 getutxid (utx); /* position to entry in utmp file */
123 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
124#endif 245#endif
125 246
126#ifdef HAVE_STRUCT_UTMP 247#ifdef HAVE_STRUCT_UTMP
127 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
128 ut->ut_time = time (NULL); 252 ut->ut_time = time (NULL);
129# ifdef HAVE_UTMP_PID 253# ifdef HAVE_UTMP_PID
130 strncpy (ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", 254 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)); 255 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
133 ut->ut_time = time (NULL);
134 ut->ut_pid = cmd_pid; 256 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; 257 ut->ut_type = USER_PROCESS;
139 pututline (ut); 258 pututline (ut);
140 endutent (); /* close the file */ 259 endutent (); /* close the file */
141 utmp_pos = 0; 260 utmp_pos = 0;
142# else 261# 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)); 262 strncpy (ut->ut_name, name, sizeof (ut->ut_name));
147# endif
148# endif 263# endif
149#endif 264#endif
150 265
151#ifdef HAVE_STRUCT_UTMPX 266#ifdef HAVE_STRUCT_UTMPX
152 strncpy (utx->ut_line, pty, sizeof (utx->ut_line)); 267 strncpy (utx->ut_line, pty, sizeof (utx->ut_line));
153 strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", 268 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)); 269 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
156# if HAVE_UTMPX_SESSION 270# if HAVE_UTMPX_SESSION
157 utx->ut_session = getsid (0); 271 utx->ut_session = getsid (0);
158# endif 272# endif
159 utx->ut_tv.tv_sec = time (NULL); 273 utx->ut_tv.tv_sec = time (NULL);
176 utmp_pos = 0; 290 utmp_pos = 0;
177#endif 291#endif
178 292
179#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) 293#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
180 { 294 {
181# ifdef HAVE_TTYSLOT 295# if 1
182 int fdstdin = dup (STDIN_FILENO); 296 int fdstdin = dup (STDIN_FILENO);
183 dup2 (tty, STDIN_FILENO); 297 dup2 (tty, STDIN_FILENO);
184 298
185 i = ttyslot (); 299 i = ttyslot ();
186 if (write_bsd_utmp (i, ut)) 300 if (write_bsd_utmp (i, ut))
187 utmp_pos = i; 301 utmp_pos = i;
188 302
189 dup2 (fdstdin, STDIN_FILENO); 303 dup2 (fdstdin, STDIN_FILENO);
190 close (fdstdin); 304 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 305# endif
216 } 306 }
217#endif 307#endif
218 308
219#ifdef WTMP_SUPPORT 309#ifdef WTMP_SUPPORT
332#endif 422#endif
333 423
334 cmd_pid = 0; 424 cmd_pid = 0;
335} 425}
336 426
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 427#else
464void 428void
465ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname) 429ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
466{ 430{
467} 431}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines