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.12 by root, Wed Jan 19 15:20:47 2005 UTC vs.
Revision 1.26 by ayin, Tue Jan 17 09:31:24 2006 UTC

28 * 28 *
29 * You should have received a copy of the GNU General Public License 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 30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 *----------------------------------------------------------------------*/ 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 33
42#include "../config.h" /* NECESSARY */ 34#include "../config.h"
43#include "rxvt.h" /* NECESSARY */ 35#include "rxvt.h"
44#include "logging.h" 36#include "logging.h"
37
45#ifdef UTMP_SUPPORT 38#ifdef UTMP_SUPPORT
39
40#if HAVE_STRUCT_UTMP
41static int rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu);
42static void rxvt_update_wtmp (const char *fname, const struct utmp *putmp);
43#endif
44
45static void rxvt_update_lastlog (const char *fname, const char *pty, const char *host);
46 46
47/* 47/*
48 * BSD style utmp entry 48 * BSD style utmp entry
49 * ut_line, ut_name, ut_host, ut_time 49 * ut_line, ut_name, ut_host, ut_time
50 * SYSV style utmp (and utmpx) entry 50 * SYSV style utmp (and utmpx) entry
55 55
56/* 56/*
57 * make and write utmp and wtmp entries 57 * make and write utmp and wtmp entries
58 */ 58 */
59void 59void
60rxvt_term::makeutent (const char *pty, const char *hostname) 60rxvt_session::login (const char *pty, int cmd_pid, bool login_shell, const char *hostname)
61{ 61{
62 this->cmd_pid = cmd_pid;
63 this->login_shell = login_shell;
64
62#ifdef HAVE_STRUCT_UTMP 65#ifdef HAVE_STRUCT_UTMP
63 struct utmp *ut = &this->ut; 66 struct utmp *ut = &this->ut;
64#endif 67#endif
65#ifdef HAVE_STRUCT_UTMPX 68#ifdef HAVE_STRUCT_UTMPX
66 struct utmpx *utx = &this->utx; 69 struct utmpx *utx = &this->utx;
67#endif 70#endif
68#ifdef HAVE_UTMP_PID
69 int i; 71 int i;
70#endif
71 struct passwd *pwent = getpwuid (getuid ()); 72 struct passwd *pwent = getpwuid (getuid ());
72 73
73 if (!strncmp (pty, "/dev/", 5)) 74 if (!strncmp (pty, "/dev/", 5))
74 pty += 5; /* skip /dev/ prefix */ 75 pty += 5; /* skip /dev/ prefix */
75 76
77#if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
76 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3)) 78 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
77 strncpy (ut_id, (pty + 3), sizeof (ut_id)); 79 strncpy (ut_id, pty + 3, sizeof (ut_id));
78#ifdef HAVE_UTMP_PID
79 else if (sscanf (pty, "pts/%d", &i) == 1) 80 else if (sscanf (pty, "pts/%d", &i) == 1)
80 sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ 81 sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */
81#endif
82 else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3)) 82 else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3))
83 { 83 {
84 rxvt_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty); 84 rxvt_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty);
85 return; 85 return;
86 } 86 }
87#endif
87 88
88#ifdef HAVE_STRUCT_UTMP 89#ifdef HAVE_STRUCT_UTMP
89 memset (ut, 0, sizeof (struct utmp)); 90 memset (ut, 0, sizeof (struct utmp));
90# ifdef HAVE_UTMP_PID 91# ifdef HAVE_UTMP_PID
91 setutent (); 92 setutent ();
92 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); 93 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
93 ut->ut_type = DEAD_PROCESS; 94 ut->ut_type = DEAD_PROCESS;
94 getutid (ut); /* position to entry in utmp file */ 95 getutid (ut); /* position to entry in utmp file */
95 strncpy (ut_id, ut_id, sizeof (ut_id)); 96 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
96# endif 97# endif
97#endif 98#endif
98 99
99#ifdef HAVE_STRUCT_UTMPX 100#ifdef HAVE_STRUCT_UTMPX
100 memset (utx, 0, sizeof (struct utmpx)); 101 memset (utx, 0, sizeof (struct utmpx));
101 setutxent (); 102 setutxent ();
102 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); 103 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
103 utx->ut_type = DEAD_PROCESS; 104 utx->ut_type = DEAD_PROCESS;
104 getutxid (utx); /* position to entry in utmp file */ 105 getutxid (utx); /* position to entry in utmp file */
105 strncpy (ut_id, ut_id, sizeof (ut_id)); 106 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
106#endif 107#endif
107 108
108#ifdef HAVE_STRUCT_UTMP 109#ifdef HAVE_STRUCT_UTMP
109 strncpy (ut->ut_line, pty, sizeof (ut->ut_line)); 110 strncpy (ut->ut_line, pty, sizeof (ut->ut_line));
110 ut->ut_time = time (NULL); 111 ut->ut_time = time (NULL);
133#ifdef HAVE_STRUCT_UTMPX 134#ifdef HAVE_STRUCT_UTMPX
134 strncpy (utx->ut_line, pty, sizeof (utx->ut_line)); 135 strncpy (utx->ut_line, pty, sizeof (utx->ut_line));
135 strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", 136 strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
136 sizeof (utx->ut_user)); 137 sizeof (utx->ut_user));
137 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); 138 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
139# if HAVE_UTMPX_SESSION
138 utx->ut_session = getsid (0); 140 utx->ut_session = getsid (0);
141# endif
139 utx->ut_tv.tv_sec = time (NULL); 142 utx->ut_tv.tv_sec = time (NULL);
140 utx->ut_tv.tv_usec = 0; 143 utx->ut_tv.tv_usec = 0;
141 utx->ut_pid = cmd_pid; 144 utx->ut_pid = cmd_pid;
142# ifdef HAVE_UTMPX_HOST 145# ifdef HAVE_UTMPX_HOST
143 strncpy (utx->ut_host, hostname, sizeof (utx->ut_host)); 146 strncpy (utx->ut_host, hostname, sizeof (utx->ut_host));
157#endif 160#endif
158 161
159#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) 162#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
160 163
161 { 164 {
162 int i;
163# ifdef HAVE_TTYSLOT 165# ifdef HAVE_TTYSLOT
164 i = ttyslot (); 166 i = ttyslot ();
165 if (rxvt_write_bsd_utmp (i, ut)) 167 if (rxvt_write_bsd_utmp (i, ut))
166 utmp_pos = i; 168 utmp_pos = i;
167# else 169# else
168 FILE *fd0; 170 FILE *fd0;
169 171
170 if ((fd0 = fopen (TTYTAB_FILENAME, "r")) != NULL) 172 if ((fd0 = fopen (TTYTAB_FILENAME, "r")) != NULL)
171 { 173 {
172 char buf[256], name[256]; 174 char buf[256], name[256];
173 175
174 buf[sizeof (buf) - 1] = '\0'; 176 buf[sizeof (buf) - 1] = '\0';
175 for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);) 177 for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);)
176 { 178 {
177 if (*buf == '#' || sscanf (buf, "%s", name) != 1) 179 if (*buf == '#' || sscanf (buf, "%s", name) != 1)
187 i++; 189 i++;
188 } 190 }
189 fclose (fd0); 191 fclose (fd0);
190 } 192 }
191# endif 193# endif
192
193 } 194 }
194#endif 195#endif
195 196
196#ifdef WTMP_SUPPORT 197#ifdef WTMP_SUPPORT
197# ifdef WTMP_ONLY_ON_LOGIN 198# ifdef WTMP_ONLY_ON_LOGIN
198 if (options & Opt_loginShell) 199 if (login_shell)
199# endif 200# endif
200 { 201 {
201# ifdef HAVE_STRUCT_UTMP 202# ifdef HAVE_STRUCT_UTMP
202# ifdef HAVE_UPDWTMP 203# ifdef HAVE_UPDWTMP
203 updwtmp (RXVT_WTMP_FILE, ut); 204 updwtmp (RXVT_WTMP_FILE, ut);
204# else 205# else
205 rxvt_update_wtmp (RXVT_WTMP_FILE, ut); 206 rxvt_update_wtmp (RXVT_WTMP_FILE, ut);
206# endif 207# endif
207# endif 208# endif
208# ifdef HAVE_STRUCT_UTMPX 209# if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
209 updwtmpx (RXVT_WTMPX_FILE, utx); 210 updwtmpx (RXVT_WTMPX_FILE, utx);
210# endif 211# endif
211 } 212 }
212#endif 213#endif
213#if defined(LASTLOG_SUPPORT) && defined(RXVT_LASTLOG_FILE) 214#if defined(LASTLOG_SUPPORT) && defined(RXVT_LASTLOG_FILE)
214 if (options & Opt_loginShell) 215 if (login_shell)
215 rxvt_update_lastlog (RXVT_LASTLOG_FILE, pty, hostname); 216 rxvt_update_lastlog (RXVT_LASTLOG_FILE, pty, hostname);
216#endif 217#endif
217} 218}
218 219
219/* ------------------------------------------------------------------------- */ 220/* ------------------------------------------------------------------------- */
220/* 221/*
221 * remove utmp and wtmp entries 222 * remove utmp and wtmp entries
222 */ 223 */
223void 224void
224rxvt_term::cleanutent () 225rxvt_session::logout ()
225{ 226{
226#ifdef HAVE_STRUCT_UTMP 227#ifdef HAVE_STRUCT_UTMP
227 struct utmp *ut = &this->ut; 228 struct utmp *tmput, *ut = &this->ut;
228#endif 229#endif
229#ifdef HAVE_STRUCT_UTMPX 230#ifdef HAVE_STRUCT_UTMPX
230 struct utmpx *tmputx, *utx = &this->utx; 231 struct utmpx *tmputx, *utx = &this->utx;
231#endif 232#endif
232 233
233#ifdef HAVE_STRUCT_UTMP 234#ifdef HAVE_STRUCT_UTMP
234# ifdef HAVE_UTMP_PID 235# ifdef HAVE_UTMP_PID
235 memset (ut, 0, sizeof (struct utmp)); 236 memset (ut, 0, sizeof (struct utmp));
236 setutent (); 237 setutent ();
237 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); 238 strncpy (ut->ut_id, this->ut_id, sizeof (ut->ut_id));
238 ut->ut_type = USER_PROCESS; 239 ut->ut_type = USER_PROCESS;
239 {
240 struct utmp *tmput = getutid (ut);
241
242 if (tmput) /* position to entry in utmp file */ 240 if ((tmput = getutid (ut))) /* position to entry in utmp file */
243 ut = tmput; 241 ut = tmput;
244 }
245 ut->ut_type = DEAD_PROCESS; 242 ut->ut_type = DEAD_PROCESS;
246# else 243# else
247 memset (ut->ut_name, 0, sizeof (ut->ut_name)); 244 memset (ut->ut_name, 0, sizeof (ut->ut_name));
248# ifdef HAVE_UTMP_HOST 245# ifdef HAVE_UTMP_HOST
249 memset (ut->ut_host, 0, sizeof (ut->ut_host)); 246 memset (ut->ut_host, 0, sizeof (ut->ut_host));
253#endif 250#endif
254 251
255#ifdef HAVE_STRUCT_UTMPX 252#ifdef HAVE_STRUCT_UTMPX
256 memset (utx, 0, sizeof (struct utmpx)); 253 memset (utx, 0, sizeof (struct utmpx));
257 setutxent (); 254 setutxent ();
258 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); 255 strncpy (utx->ut_id, this->ut_id, sizeof (utx->ut_id));
259 utx->ut_type = USER_PROCESS; 256 utx->ut_type = USER_PROCESS;
260 if ((tmputx = getutxid (utx))) /* position to entry in utmp file */ 257 if ((tmputx = getutxid (utx))) /* position to entry in utmp file */
261 utx = tmputx; 258 utx = tmputx;
262 utx->ut_type = DEAD_PROCESS; 259 utx->ut_type = DEAD_PROCESS;
260# if HAVE_UTMPX_SESSION
263 utx->ut_session = getsid (0); 261 utx->ut_session = getsid (0);
262# endif
264 utx->ut_tv.tv_sec = time (NULL); 263 utx->ut_tv.tv_sec = time (NULL);
265 utx->ut_tv.tv_usec = 0; 264 utx->ut_tv.tv_usec = 0;
266#endif 265#endif
267 266
268 /* 267 /*
269 * Write ending wtmp entry 268 * Write ending wtmp entry
270 */ 269 */
271#ifdef WTMP_SUPPORT 270#ifdef WTMP_SUPPORT
272# ifdef WTMP_ONLY_ON_LOGIN 271# ifdef WTMP_ONLY_ON_LOGIN
273 if (options & Opt_loginShell) 272 if (login_shell)
274# endif 273# endif
275 { 274 {
276# ifdef HAVE_STRUCT_UTMP 275# ifdef HAVE_STRUCT_UTMP
277# ifdef HAVE_UPDWTMP 276# ifdef HAVE_UPDWTMP
278 updwtmp (RXVT_WTMP_FILE, ut); 277 updwtmp (RXVT_WTMP_FILE, ut);
279# else 278# else
280 rxvt_update_wtmp (RXVT_WTMP_FILE, ut); 279 rxvt_update_wtmp (RXVT_WTMP_FILE, ut);
281# endif 280# endif
282# endif 281# endif
283# ifdef HAVE_STRUCT_UTMPX 282# if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX)
284 updwtmpx (RXVT_WTMPX_FILE, utx); 283 updwtmpx (RXVT_WTMPX_FILE, utx);
285# endif 284# endif
286 } 285 }
287#endif 286#endif
288 287
308 307
309/* ------------------------------------------------------------------------- */ 308/* ------------------------------------------------------------------------- */
310/* 309/*
311 * Write a BSD style utmp entry 310 * Write a BSD style utmp entry
312 */ 311 */
313#ifdef HAVE_UTMP_H 312#if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
314/* INTPROTO */ 313static int
315int
316rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu) 314rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu)
317{ 315{
318 int fd; 316 int fd;
319 317
320 if (utmp_pos <= 0 || (fd = open (RXVT_UTMP_FILE, O_WRONLY)) == -1) 318 if (utmp_pos <= 0 || (fd = open (RXVT_UTMP_FILE, O_WRONLY)) == -1)
329 327
330/* ------------------------------------------------------------------------- */ 328/* ------------------------------------------------------------------------- */
331/* 329/*
332 * Update a BSD style wtmp entry 330 * Update a BSD style wtmp entry
333 */ 331 */
334#if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) 332#if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP)
335/* INTPROTO */ 333static void
336void
337rxvt_update_wtmp (const char *fname, const struct utmp *putmp) 334rxvt_update_wtmp (const char *fname, const struct utmp *putmp)
338{ 335{
339 int fd, gotlock, retry; 336 int fd, gotlock, retry;
340 struct flock lck; /* fcntl locking scheme */ 337 struct flock lck; /* fcntl locking scheme */
341 struct stat sbuf; 338 struct stat sbuf;
372} 369}
373#endif 370#endif
374 371
375/* ------------------------------------------------------------------------- */ 372/* ------------------------------------------------------------------------- */
376#ifdef LASTLOG_SUPPORT 373#ifdef LASTLOG_SUPPORT
377/* INTPROTO */ 374static void
378void
379rxvt_update_lastlog (const char *fname, const char *pty, const char *host) 375rxvt_update_lastlog (const char *fname, const char *pty, const char *host)
380{ 376{
381# ifdef HAVE_STRUCT_LASTLOGX 377# ifdef HAVE_STRUCT_LASTLOGX
382 struct lastlogx llx; 378 struct lastlogx llx;
383# endif 379# endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines