… | |
… | |
37 | * |
37 | * |
38 | * Private: |
38 | * Private: |
39 | * rxvt_update_wtmp (); |
39 | * rxvt_update_wtmp (); |
40 | *----------------------------------------------------------------------*/ |
40 | *----------------------------------------------------------------------*/ |
41 | |
41 | |
42 | #include "../config.h" /* NECESSARY */ |
42 | #include "../config.h" |
43 | #include "rxvt.h" /* NECESSARY */ |
43 | #include "rxvt.h" |
44 | #include "logging.h" |
44 | #include "logging.h" |
|
|
45 | |
45 | #ifdef UTMP_SUPPORT |
46 | #ifdef UTMP_SUPPORT |
|
|
47 | |
|
|
48 | #if HAVE_STRUCT_UTMP |
|
|
49 | static int rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu); |
|
|
50 | static void rxvt_update_wtmp (const char *fname, const struct utmp *putmp); |
|
|
51 | #endif |
|
|
52 | |
|
|
53 | static void rxvt_update_lastlog (const char *fname, const char *pty, const char *host); |
46 | |
54 | |
47 | /* |
55 | /* |
48 | * BSD style utmp entry |
56 | * BSD style utmp entry |
49 | * ut_line, ut_name, ut_host, ut_time |
57 | * ut_line, ut_name, ut_host, ut_time |
50 | * SYSV style utmp (and utmpx) entry |
58 | * SYSV style utmp (and utmpx) entry |
… | |
… | |
63 | struct utmp *ut = &this->ut; |
71 | struct utmp *ut = &this->ut; |
64 | #endif |
72 | #endif |
65 | #ifdef HAVE_STRUCT_UTMPX |
73 | #ifdef HAVE_STRUCT_UTMPX |
66 | struct utmpx *utx = &this->utx; |
74 | struct utmpx *utx = &this->utx; |
67 | #endif |
75 | #endif |
68 | #ifdef HAVE_UTMP_PID |
|
|
69 | int i; |
76 | int i; |
70 | #endif |
|
|
71 | struct passwd *pwent = getpwuid (getuid ()); |
77 | struct passwd *pwent = getpwuid (getuid ()); |
72 | |
78 | |
73 | if (!strncmp (pty, "/dev/", 5)) |
79 | if (!strncmp (pty, "/dev/", 5)) |
74 | pty += 5; /* skip /dev/ prefix */ |
80 | pty += 5; /* skip /dev/ prefix */ |
75 | |
81 | |
76 | #ifdef HAVE_UTMP_PID |
82 | #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX) |
77 | if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3)) |
83 | if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3)) |
78 | strncpy (ut_id, pty + 3, sizeof (ut_id)); |
84 | strncpy (ut_id, pty + 3, sizeof (ut_id)); |
79 | else if (sscanf (pty, "pts/%d", &i) == 1) |
85 | else if (sscanf (pty, "pts/%d", &i) == 1) |
80 | sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ |
86 | sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ |
81 | else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3)) |
87 | else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3)) |
… | |
… | |
90 | # ifdef HAVE_UTMP_PID |
96 | # ifdef HAVE_UTMP_PID |
91 | setutent (); |
97 | setutent (); |
92 | strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); |
98 | strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); |
93 | ut->ut_type = DEAD_PROCESS; |
99 | ut->ut_type = DEAD_PROCESS; |
94 | getutid (ut); /* position to entry in utmp file */ |
100 | getutid (ut); /* position to entry in utmp file */ |
95 | strncpy (ut_id, ut_id, sizeof (ut_id)); |
101 | strncpy (this->ut_id, ut_id, sizeof (this->ut_id)); |
96 | # endif |
102 | # endif |
97 | #endif |
103 | #endif |
98 | |
104 | |
99 | #ifdef HAVE_STRUCT_UTMPX |
105 | #ifdef HAVE_STRUCT_UTMPX |
100 | memset (utx, 0, sizeof (struct utmpx)); |
106 | memset (utx, 0, sizeof (struct utmpx)); |
101 | setutxent (); |
107 | setutxent (); |
102 | strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); |
108 | strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); |
103 | utx->ut_type = DEAD_PROCESS; |
109 | utx->ut_type = DEAD_PROCESS; |
104 | getutxid (utx); /* position to entry in utmp file */ |
110 | getutxid (utx); /* position to entry in utmp file */ |
105 | strncpy (ut_id, ut_id, sizeof (ut_id)); |
111 | strncpy (this->ut_id, ut_id, sizeof (this->ut_id)); |
106 | #endif |
112 | #endif |
107 | |
113 | |
108 | #ifdef HAVE_STRUCT_UTMP |
114 | #ifdef HAVE_STRUCT_UTMP |
109 | strncpy (ut->ut_line, pty, sizeof (ut->ut_line)); |
115 | strncpy (ut->ut_line, pty, sizeof (ut->ut_line)); |
110 | ut->ut_time = time (NULL); |
116 | ut->ut_time = time (NULL); |
… | |
… | |
133 | #ifdef HAVE_STRUCT_UTMPX |
139 | #ifdef HAVE_STRUCT_UTMPX |
134 | strncpy (utx->ut_line, pty, sizeof (utx->ut_line)); |
140 | strncpy (utx->ut_line, pty, sizeof (utx->ut_line)); |
135 | strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", |
141 | strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", |
136 | sizeof (utx->ut_user)); |
142 | sizeof (utx->ut_user)); |
137 | strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); |
143 | strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); |
|
|
144 | # if HAVE_UTMPX_SESSION |
138 | utx->ut_session = getsid (0); |
145 | utx->ut_session = getsid (0); |
|
|
146 | # endif |
139 | utx->ut_tv.tv_sec = time (NULL); |
147 | utx->ut_tv.tv_sec = time (NULL); |
140 | utx->ut_tv.tv_usec = 0; |
148 | utx->ut_tv.tv_usec = 0; |
141 | utx->ut_pid = cmd_pid; |
149 | utx->ut_pid = cmd_pid; |
142 | # ifdef HAVE_UTMPX_HOST |
150 | # ifdef HAVE_UTMPX_HOST |
143 | strncpy (utx->ut_host, hostname, sizeof (utx->ut_host)); |
151 | strncpy (utx->ut_host, hostname, sizeof (utx->ut_host)); |
… | |
… | |
157 | #endif |
165 | #endif |
158 | |
166 | |
159 | #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) |
167 | #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) |
160 | |
168 | |
161 | { |
169 | { |
162 | int i; |
|
|
163 | # ifdef HAVE_TTYSLOT |
170 | # ifdef HAVE_TTYSLOT |
164 | i = ttyslot (); |
171 | i = ttyslot (); |
165 | if (rxvt_write_bsd_utmp (i, ut)) |
172 | if (rxvt_write_bsd_utmp (i, ut)) |
166 | utmp_pos = i; |
173 | utmp_pos = i; |
167 | # else |
174 | # else |
168 | FILE *fd0; |
175 | FILE *fd0; |
169 | |
176 | |
170 | if ((fd0 = fopen (TTYTAB_FILENAME, "r")) != NULL) |
177 | if ((fd0 = fopen (TTYTAB_FILENAME, "r")) != NULL) |
171 | { |
178 | { |
172 | char buf[256], name[256]; |
179 | char buf[256], name[256]; |
173 | |
180 | |
174 | buf[sizeof (buf) - 1] = '\0'; |
181 | buf[sizeof (buf) - 1] = '\0'; |
175 | for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);) |
182 | for (i = 1; (fgets (buf, sizeof (buf) - 1, fd0) != NULL);) |
176 | { |
183 | { |
177 | if (*buf == '#' || sscanf (buf, "%s", name) != 1) |
184 | if (*buf == '#' || sscanf (buf, "%s", name) != 1) |
… | |
… | |
187 | i++; |
194 | i++; |
188 | } |
195 | } |
189 | fclose (fd0); |
196 | fclose (fd0); |
190 | } |
197 | } |
191 | # endif |
198 | # endif |
192 | |
|
|
193 | } |
199 | } |
194 | #endif |
200 | #endif |
195 | |
201 | |
196 | #ifdef WTMP_SUPPORT |
202 | #ifdef WTMP_SUPPORT |
197 | # ifdef WTMP_ONLY_ON_LOGIN |
203 | # ifdef WTMP_ONLY_ON_LOGIN |
198 | if (options & Opt_loginShell) |
204 | if (OPTION (Opt_loginShell)) |
199 | # endif |
205 | # endif |
200 | { |
206 | { |
201 | # ifdef HAVE_STRUCT_UTMP |
207 | # ifdef HAVE_STRUCT_UTMP |
202 | # ifdef HAVE_UPDWTMP |
208 | # ifdef HAVE_UPDWTMP |
203 | updwtmp (RXVT_WTMP_FILE, ut); |
209 | updwtmp (RXVT_WTMP_FILE, ut); |
204 | # else |
210 | # else |
205 | rxvt_update_wtmp (RXVT_WTMP_FILE, ut); |
211 | rxvt_update_wtmp (RXVT_WTMP_FILE, ut); |
206 | # endif |
212 | # endif |
207 | # endif |
213 | # endif |
208 | # ifdef HAVE_STRUCT_UTMPX |
214 | # if defined(HAVE_STRUCT_UTMPX) && defined(HAVE_UPDWTMPX) |
209 | updwtmpx (RXVT_WTMPX_FILE, utx); |
215 | updwtmpx (RXVT_WTMPX_FILE, utx); |
210 | # endif |
216 | # endif |
211 | } |
217 | } |
212 | #endif |
218 | #endif |
213 | #if defined(LASTLOG_SUPPORT) && defined(RXVT_LASTLOG_FILE) |
219 | #if defined(LASTLOG_SUPPORT) && defined(RXVT_LASTLOG_FILE) |
214 | if (options & Opt_loginShell) |
220 | if (OPTION (Opt_loginShell)) |
215 | rxvt_update_lastlog (RXVT_LASTLOG_FILE, pty, hostname); |
221 | rxvt_update_lastlog (RXVT_LASTLOG_FILE, pty, hostname); |
216 | #endif |
222 | #endif |
217 | } |
223 | } |
218 | |
224 | |
219 | /* ------------------------------------------------------------------------- */ |
225 | /* ------------------------------------------------------------------------- */ |
… | |
… | |
232 | |
238 | |
233 | #ifdef HAVE_STRUCT_UTMP |
239 | #ifdef HAVE_STRUCT_UTMP |
234 | # ifdef HAVE_UTMP_PID |
240 | # ifdef HAVE_UTMP_PID |
235 | memset (ut, 0, sizeof (struct utmp)); |
241 | memset (ut, 0, sizeof (struct utmp)); |
236 | setutent (); |
242 | setutent (); |
237 | strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); |
243 | strncpy (ut->ut_id, this->ut_id, sizeof (ut->ut_id)); |
238 | ut->ut_type = USER_PROCESS; |
244 | ut->ut_type = USER_PROCESS; |
239 | { |
245 | { |
240 | struct utmp *tmput = getutid (ut); |
246 | struct utmp *tmput = getutid (ut); |
241 | |
247 | |
242 | if (tmput) /* position to entry in utmp file */ |
248 | if (tmput) /* position to entry in utmp file */ |
… | |
… | |
253 | #endif |
259 | #endif |
254 | |
260 | |
255 | #ifdef HAVE_STRUCT_UTMPX |
261 | #ifdef HAVE_STRUCT_UTMPX |
256 | memset (utx, 0, sizeof (struct utmpx)); |
262 | memset (utx, 0, sizeof (struct utmpx)); |
257 | setutxent (); |
263 | setutxent (); |
258 | strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); |
264 | strncpy (utx->ut_id, this->ut_id, sizeof (utx->ut_id)); |
259 | utx->ut_type = USER_PROCESS; |
265 | utx->ut_type = USER_PROCESS; |
260 | if ((tmputx = getutxid (utx))) /* position to entry in utmp file */ |
266 | if ((tmputx = getutxid (utx))) /* position to entry in utmp file */ |
261 | utx = tmputx; |
267 | utx = tmputx; |
262 | utx->ut_type = DEAD_PROCESS; |
268 | utx->ut_type = DEAD_PROCESS; |
|
|
269 | # if HAVE_UTMPX_SESSION |
263 | utx->ut_session = getsid (0); |
270 | utx->ut_session = getsid (0); |
|
|
271 | # endif |
264 | utx->ut_tv.tv_sec = time (NULL); |
272 | utx->ut_tv.tv_sec = time (NULL); |
265 | utx->ut_tv.tv_usec = 0; |
273 | utx->ut_tv.tv_usec = 0; |
266 | #endif |
274 | #endif |
267 | |
275 | |
268 | /* |
276 | /* |
269 | * Write ending wtmp entry |
277 | * Write ending wtmp entry |
270 | */ |
278 | */ |
271 | #ifdef WTMP_SUPPORT |
279 | #ifdef WTMP_SUPPORT |
272 | # ifdef WTMP_ONLY_ON_LOGIN |
280 | # ifdef WTMP_ONLY_ON_LOGIN |
273 | if (options & Opt_loginShell) |
281 | if (OPTION (Opt_loginShell)) |
274 | # endif |
282 | # endif |
275 | { |
283 | { |
276 | # ifdef HAVE_STRUCT_UTMP |
284 | # ifdef HAVE_STRUCT_UTMP |
277 | # ifdef HAVE_UPDWTMP |
285 | # ifdef HAVE_UPDWTMP |
278 | updwtmp (RXVT_WTMP_FILE, ut); |
286 | updwtmp (RXVT_WTMP_FILE, ut); |
279 | # else |
287 | # else |
280 | rxvt_update_wtmp (RXVT_WTMP_FILE, ut); |
288 | rxvt_update_wtmp (RXVT_WTMP_FILE, ut); |
281 | # endif |
289 | # endif |
282 | # endif |
290 | # endif |
283 | # ifdef HAVE_STRUCT_UTMPX |
291 | # ifdef HAVE_STRUCT_UTMPX |
|
|
292 | # if HAVE_UPDWTMPX |
284 | updwtmpx (RXVT_WTMPX_FILE, utx); |
293 | updwtmpx (RXVT_WTMPX_FILE, utx); |
|
|
294 | # else |
|
|
295 | pututxline (utx); |
|
|
296 | # endif |
285 | # endif |
297 | # endif |
286 | } |
298 | } |
287 | #endif |
299 | #endif |
288 | |
300 | |
289 | /* |
301 | /* |
… | |
… | |
308 | |
320 | |
309 | /* ------------------------------------------------------------------------- */ |
321 | /* ------------------------------------------------------------------------- */ |
310 | /* |
322 | /* |
311 | * Write a BSD style utmp entry |
323 | * Write a BSD style utmp entry |
312 | */ |
324 | */ |
313 | #ifdef HAVE_UTMP_H |
325 | #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) |
314 | /* INTPROTO */ |
326 | static int |
315 | int |
|
|
316 | rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu) |
327 | rxvt_write_bsd_utmp (int utmp_pos, struct utmp *wu) |
317 | { |
328 | { |
318 | int fd; |
329 | int fd; |
319 | |
330 | |
320 | if (utmp_pos <= 0 || (fd = open (RXVT_UTMP_FILE, O_WRONLY)) == -1) |
331 | if (utmp_pos <= 0 || (fd = open (RXVT_UTMP_FILE, O_WRONLY)) == -1) |
… | |
… | |
329 | |
340 | |
330 | /* ------------------------------------------------------------------------- */ |
341 | /* ------------------------------------------------------------------------- */ |
331 | /* |
342 | /* |
332 | * Update a BSD style wtmp entry |
343 | * Update a BSD style wtmp entry |
333 | */ |
344 | */ |
334 | #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) |
345 | #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) && defined(HAVE_STRUCT_UTMP) |
335 | /* INTPROTO */ |
346 | static void |
336 | void |
|
|
337 | rxvt_update_wtmp (const char *fname, const struct utmp *putmp) |
347 | rxvt_update_wtmp (const char *fname, const struct utmp *putmp) |
338 | { |
348 | { |
339 | int fd, gotlock, retry; |
349 | int fd, gotlock, retry; |
340 | struct flock lck; /* fcntl locking scheme */ |
350 | struct flock lck; /* fcntl locking scheme */ |
341 | struct stat sbuf; |
351 | struct stat sbuf; |
… | |
… | |
372 | } |
382 | } |
373 | #endif |
383 | #endif |
374 | |
384 | |
375 | /* ------------------------------------------------------------------------- */ |
385 | /* ------------------------------------------------------------------------- */ |
376 | #ifdef LASTLOG_SUPPORT |
386 | #ifdef LASTLOG_SUPPORT |
377 | /* INTPROTO */ |
387 | static void |
378 | void |
|
|
379 | rxvt_update_lastlog (const char *fname, const char *pty, const char *host) |
388 | rxvt_update_lastlog (const char *fname, const char *pty, const char *host) |
380 | { |
389 | { |
381 | # ifdef HAVE_STRUCT_LASTLOGX |
390 | # ifdef HAVE_STRUCT_LASTLOGX |
382 | struct lastlogx llx; |
391 | struct lastlogx llx; |
383 | # endif |
392 | # endif |