1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: logging.c |
2 | * File: logging.c |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * $Id: logging.C,v 1.4 2004/01/31 00:20:21 pcg Exp $ |
|
|
5 | * |
4 | * |
6 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
7 | * Copyright (c) 1992 John Bovey <jdb@ukc.ac.uk> |
6 | * Copyright (c) 1992 John Bovey <jdb@ukc.ac.uk> |
8 | * - original version |
7 | * - original version |
9 | * Copyright (c) 1993 lipka |
8 | * Copyright (c) 1993 lipka |
… | |
… | |
58 | */ |
57 | */ |
59 | void |
58 | void |
60 | rxvt_term::makeutent (const char *pty, const char *hostname) |
59 | rxvt_term::makeutent (const char *pty, const char *hostname) |
61 | { |
60 | { |
62 | #ifdef HAVE_STRUCT_UTMP |
61 | #ifdef HAVE_STRUCT_UTMP |
63 | struct utmp *ut = &(this->ut); |
62 | struct utmp *ut = &(this->ut); |
64 | #endif |
63 | #endif |
65 | #ifdef HAVE_STRUCT_UTMPX |
64 | #ifdef HAVE_STRUCT_UTMPX |
66 | struct utmpx *utx = &(this->utx); |
65 | struct utmpx *utx = &(this->utx); |
67 | #endif |
66 | #endif |
68 | #ifdef HAVE_UTMP_PID |
67 | #ifdef HAVE_UTMP_PID |
|
|
68 | int i; |
|
|
69 | #endif |
|
|
70 | char ut_id[5]; |
|
|
71 | struct passwd *pwent = getpwuid(getuid()); |
|
|
72 | |
|
|
73 | if (!STRNCMP(pty, "/dev/", 5)) |
|
|
74 | pty += 5; /* skip /dev/ prefix */ |
|
|
75 | |
|
|
76 | if (!STRNCMP(pty, "pty", 3) || !STRNCMP(pty, "tty", 3)) |
|
|
77 | { |
|
|
78 | STRNCPY(ut_id, (pty + 3), sizeof(ut_id)); |
|
|
79 | } |
|
|
80 | #ifdef HAVE_UTMP_PID |
|
|
81 | else if (sscanf(pty, "pts/%d", &i) == 1) |
|
|
82 | sprintf(ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ |
|
|
83 | #endif |
|
|
84 | else if (STRNCMP(pty, "pty", 3) && STRNCMP(pty, "tty", 3)) |
|
|
85 | { |
|
|
86 | rxvt_print_error("can't parse tty name \"%s\"", pty); |
|
|
87 | return; |
|
|
88 | } |
|
|
89 | |
|
|
90 | #ifdef HAVE_STRUCT_UTMP |
|
|
91 | MEMSET(ut, 0, sizeof(struct utmp)); |
|
|
92 | # ifdef HAVE_UTMP_PID |
|
|
93 | setutent(); |
|
|
94 | STRNCPY(ut->ut_id, ut_id, sizeof(ut->ut_id)); |
|
|
95 | ut->ut_type = DEAD_PROCESS; |
|
|
96 | getutid(ut); /* position to entry in utmp file */ |
|
|
97 | STRNCPY(ut_id, ut_id, sizeof(ut_id)); |
|
|
98 | # endif |
|
|
99 | #endif |
|
|
100 | |
|
|
101 | #ifdef HAVE_STRUCT_UTMPX |
|
|
102 | MEMSET(utx, 0, sizeof(struct utmpx)); |
|
|
103 | setutxent(); |
|
|
104 | STRNCPY(utx->ut_id, ut_id, sizeof(utx->ut_id)); |
|
|
105 | utx->ut_type = DEAD_PROCESS; |
|
|
106 | getutxid(utx); /* position to entry in utmp file */ |
|
|
107 | STRNCPY(ut_id, ut_id, sizeof(ut_id)); |
|
|
108 | #endif |
|
|
109 | |
|
|
110 | #ifdef HAVE_STRUCT_UTMP |
|
|
111 | STRNCPY(ut->ut_line, pty, sizeof(ut->ut_line)); |
|
|
112 | ut->ut_time = time(NULL); |
|
|
113 | # ifdef HAVE_UTMP_PID |
|
|
114 | STRNCPY(ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", |
|
|
115 | sizeof(ut->ut_user)); |
|
|
116 | STRNCPY(ut->ut_id, ut_id, sizeof(ut->ut_id)); |
|
|
117 | ut->ut_time = time(NULL); |
|
|
118 | ut->ut_pid = cmd_pid; |
|
|
119 | # ifdef HAVE_UTMP_HOST |
|
|
120 | STRNCPY(ut->ut_host, hostname, sizeof(ut->ut_host)); |
|
|
121 | # endif |
|
|
122 | ut->ut_type = USER_PROCESS; |
|
|
123 | pututline(ut); |
|
|
124 | endutent(); /* close the file */ |
|
|
125 | utmp_pos = 0; |
|
|
126 | # else |
|
|
127 | STRNCPY(ut->ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?", |
|
|
128 | sizeof(ut->ut_name)); |
|
|
129 | # ifdef HAVE_UTMP_HOST |
|
|
130 | STRNCPY(ut->ut_host, hostname, sizeof(ut->ut_host)); |
|
|
131 | # endif |
|
|
132 | # endif |
|
|
133 | #endif |
|
|
134 | |
|
|
135 | #ifdef HAVE_STRUCT_UTMPX |
|
|
136 | STRNCPY(utx->ut_line, pty, sizeof(utx->ut_line)); |
|
|
137 | STRNCPY(utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", |
|
|
138 | sizeof(utx->ut_user)); |
|
|
139 | STRNCPY(utx->ut_id, ut_id, sizeof(utx->ut_id)); |
|
|
140 | utx->ut_session = getsid(0); |
|
|
141 | utx->ut_tv.tv_sec = time(NULL); |
|
|
142 | utx->ut_tv.tv_usec = 0; |
|
|
143 | utx->ut_pid = cmd_pid; |
|
|
144 | # ifdef HAVE_UTMPX_HOST |
|
|
145 | STRNCPY(utx->ut_host, hostname, sizeof(utx->ut_host)); |
|
|
146 | # if 0 |
|
|
147 | { |
|
|
148 | char *colon; |
|
|
149 | |
|
|
150 | if ((colon = STRRCHR(ut->ut_host, ':')) != NULL) |
|
|
151 | *colon = '\0'; |
|
|
152 | } |
|
|
153 | # endif |
|
|
154 | # endif |
|
|
155 | utx->ut_type = USER_PROCESS; |
|
|
156 | pututxline(utx); |
|
|
157 | endutxent(); /* close the file */ |
|
|
158 | utmp_pos = 0; |
|
|
159 | #endif |
|
|
160 | |
|
|
161 | #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) |
|
|
162 | |
|
|
163 | { |
69 | int i; |
164 | int i; |
70 | #endif |
165 | # ifdef HAVE_TTYSLOT |
71 | char ut_id[5]; |
166 | i = ttyslot(); |
72 | struct passwd *pwent = getpwuid(getuid()); |
167 | if (rxvt_write_bsd_utmp(i, ut)) |
73 | |
|
|
74 | if (!STRNCMP(pty, "/dev/", 5)) |
|
|
75 | pty += 5; /* skip /dev/ prefix */ |
|
|
76 | |
|
|
77 | if (!STRNCMP(pty, "pty", 3) || !STRNCMP(pty, "tty", 3)) { |
|
|
78 | STRNCPY(ut_id, (pty + 3), sizeof(ut_id)); |
|
|
79 | } |
|
|
80 | #ifdef HAVE_UTMP_PID |
|
|
81 | else if (sscanf(pty, "pts/%d", &i) == 1) |
|
|
82 | sprintf(ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ |
|
|
83 | #endif |
|
|
84 | else if (STRNCMP(pty, "pty", 3) && STRNCMP(pty, "tty", 3)) { |
|
|
85 | rxvt_print_error("can't parse tty name \"%s\"", pty); |
|
|
86 | return; |
|
|
87 | } |
|
|
88 | |
|
|
89 | #ifdef HAVE_STRUCT_UTMP |
|
|
90 | MEMSET(ut, 0, sizeof(struct utmp)); |
|
|
91 | # ifdef HAVE_UTMP_PID |
|
|
92 | setutent(); |
|
|
93 | STRNCPY(ut->ut_id, ut_id, sizeof(ut->ut_id)); |
|
|
94 | ut->ut_type = DEAD_PROCESS; |
|
|
95 | getutid(ut); /* position to entry in utmp file */ |
|
|
96 | STRNCPY(ut_id, ut_id, sizeof(ut_id)); |
|
|
97 | # endif |
|
|
98 | #endif |
|
|
99 | |
|
|
100 | #ifdef HAVE_STRUCT_UTMPX |
|
|
101 | MEMSET(utx, 0, sizeof(struct utmpx)); |
|
|
102 | setutxent(); |
|
|
103 | STRNCPY(utx->ut_id, ut_id, sizeof(utx->ut_id)); |
|
|
104 | utx->ut_type = DEAD_PROCESS; |
|
|
105 | getutxid(utx); /* position to entry in utmp file */ |
|
|
106 | STRNCPY(ut_id, ut_id, sizeof(ut_id)); |
|
|
107 | #endif |
|
|
108 | |
|
|
109 | #ifdef HAVE_STRUCT_UTMP |
|
|
110 | STRNCPY(ut->ut_line, pty, sizeof(ut->ut_line)); |
|
|
111 | ut->ut_time = time(NULL); |
|
|
112 | # ifdef HAVE_UTMP_PID |
|
|
113 | STRNCPY(ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", |
|
|
114 | sizeof(ut->ut_user)); |
|
|
115 | STRNCPY(ut->ut_id, ut_id, sizeof(ut->ut_id)); |
|
|
116 | ut->ut_time = time(NULL); |
|
|
117 | ut->ut_pid = cmd_pid; |
|
|
118 | # ifdef HAVE_UTMP_HOST |
|
|
119 | STRNCPY(ut->ut_host, hostname, sizeof(ut->ut_host)); |
|
|
120 | # endif |
|
|
121 | ut->ut_type = USER_PROCESS; |
|
|
122 | pututline(ut); |
|
|
123 | endutent(); /* close the file */ |
|
|
124 | utmp_pos = 0; |
168 | utmp_pos = i; |
125 | # else |
169 | # else |
126 | STRNCPY(ut->ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?", |
|
|
127 | sizeof(ut->ut_name)); |
|
|
128 | # ifdef HAVE_UTMP_HOST |
|
|
129 | STRNCPY(ut->ut_host, hostname, sizeof(ut->ut_host)); |
|
|
130 | # endif |
|
|
131 | # endif |
|
|
132 | #endif |
|
|
133 | |
|
|
134 | #ifdef HAVE_STRUCT_UTMPX |
|
|
135 | STRNCPY(utx->ut_line, pty, sizeof(utx->ut_line)); |
|
|
136 | STRNCPY(utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", |
|
|
137 | sizeof(utx->ut_user)); |
|
|
138 | STRNCPY(utx->ut_id, ut_id, sizeof(utx->ut_id)); |
|
|
139 | utx->ut_session = getsid(0); |
|
|
140 | utx->ut_tv.tv_sec = time(NULL); |
|
|
141 | utx->ut_tv.tv_usec = 0; |
|
|
142 | utx->ut_pid = cmd_pid; |
|
|
143 | # ifdef HAVE_UTMPX_HOST |
|
|
144 | STRNCPY(utx->ut_host, hostname, sizeof(utx->ut_host)); |
|
|
145 | # if 0 |
|
|
146 | { |
|
|
147 | char *colon; |
|
|
148 | |
|
|
149 | if ((colon = STRRCHR(ut->ut_host, ':')) != NULL) |
|
|
150 | *colon = '\0'; |
|
|
151 | } |
|
|
152 | # endif |
|
|
153 | # endif |
|
|
154 | utx->ut_type = USER_PROCESS; |
|
|
155 | pututxline(utx); |
|
|
156 | endutxent(); /* close the file */ |
|
|
157 | utmp_pos = 0; |
|
|
158 | #endif |
|
|
159 | |
|
|
160 | #if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID) |
|
|
161 | { |
|
|
162 | int i; |
|
|
163 | # ifdef HAVE_TTYSLOT |
|
|
164 | i = ttyslot(); |
|
|
165 | if (rxvt_write_bsd_utmp(i, ut)) |
|
|
166 | utmp_pos = i; |
|
|
167 | # 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) |
|
|
173 | { |
171 | char buf[256], name[256]; |
174 | char buf[256], name[256]; |
172 | |
175 | |
173 | buf[sizeof(buf) - 1] = '\0'; |
176 | buf[sizeof(buf) - 1] = '\0'; |
174 | for (i = 1; (fgets(buf, sizeof(buf) - 1, fd0) != NULL);) { |
177 | for (i = 1; (fgets(buf, sizeof(buf) - 1, fd0) != NULL);) |
|
|
178 | { |
175 | if (*buf == '#' || sscanf(buf, "%s", name) != 1) |
179 | if (*buf == '#' || sscanf(buf, "%s", name) != 1) |
176 | continue; |
180 | continue; |
177 | if (!STRCMP(ut->ut_line, name)) { |
181 | if (!STRCMP(ut->ut_line, name)) |
|
|
182 | { |
178 | if (!rxvt_write_bsd_utmp(i, ut)) |
183 | if (!rxvt_write_bsd_utmp(i, ut)) |
179 | i = 0; |
184 | i = 0; |
180 | utmp_pos = i; |
185 | utmp_pos = i; |
181 | fclose(fd0); |
186 | fclose(fd0); |
182 | break; |
187 | break; |
183 | } |
188 | } |
184 | i++; |
189 | i++; |
185 | } |
190 | } |
186 | fclose(fd0); |
191 | fclose(fd0); |
187 | } |
|
|
188 | # endif |
|
|
189 | } |
192 | } |
|
|
193 | # endif |
|
|
194 | |
|
|
195 | } |
190 | #endif |
196 | #endif |
191 | |
197 | |
192 | #ifdef WTMP_SUPPORT |
198 | #ifdef WTMP_SUPPORT |
193 | # ifdef WTMP_ONLY_ON_LOGIN |
199 | # ifdef WTMP_ONLY_ON_LOGIN |
194 | if (Options & Opt_loginShell) |
200 | if (Options & Opt_loginShell) |
195 | # endif |
201 | # endif |
196 | { |
202 | { |
197 | # ifdef HAVE_STRUCT_UTMP |
203 | # ifdef HAVE_STRUCT_UTMP |
198 | # ifdef HAVE_UPDWTMP |
204 | # ifdef HAVE_UPDWTMP |
199 | updwtmp(RXVT_WTMP_FILE, ut); |
205 | updwtmp(RXVT_WTMP_FILE, ut); |
200 | # else |
206 | # else |
201 | rxvt_update_wtmp(RXVT_WTMP_FILE, ut); |
207 | rxvt_update_wtmp(RXVT_WTMP_FILE, ut); |
202 | # endif |
208 | # endif |
203 | # endif |
209 | # endif |
204 | # ifdef HAVE_STRUCT_UTMPX |
210 | # ifdef HAVE_STRUCT_UTMPX |
205 | updwtmpx(RXVT_WTMPX_FILE, utx); |
211 | updwtmpx(RXVT_WTMPX_FILE, utx); |
206 | # endif |
212 | # endif |
207 | } |
213 | } |
208 | #endif |
214 | #endif |
209 | #if defined(LASTLOG_SUPPORT) && defined(RXVT_LASTLOG_FILE) |
215 | #if defined(LASTLOG_SUPPORT) && defined(RXVT_LASTLOG_FILE) |
210 | if (Options & Opt_loginShell) |
216 | if (Options & Opt_loginShell) |
211 | rxvt_update_lastlog(RXVT_LASTLOG_FILE, pty, hostname); |
217 | rxvt_update_lastlog(RXVT_LASTLOG_FILE, pty, hostname); |
212 | #endif |
218 | #endif |
213 | } |
219 | } |
214 | |
220 | |
215 | /* ------------------------------------------------------------------------- */ |
221 | /* ------------------------------------------------------------------------- */ |
216 | /* |
222 | /* |
… | |
… | |
218 | */ |
224 | */ |
219 | void |
225 | void |
220 | rxvt_term::cleanutent () |
226 | rxvt_term::cleanutent () |
221 | { |
227 | { |
222 | #ifdef HAVE_STRUCT_UTMP |
228 | #ifdef HAVE_STRUCT_UTMP |
223 | struct utmp *ut = &(this->ut); |
229 | struct utmp *ut = &(this->ut); |
224 | #endif |
230 | #endif |
225 | #ifdef HAVE_STRUCT_UTMPX |
231 | #ifdef HAVE_STRUCT_UTMPX |
226 | struct utmpx *tmputx, *utx = &(this->utx); |
232 | struct utmpx *tmputx, *utx = &(this->utx); |
227 | #endif |
233 | #endif |
228 | |
234 | |
229 | #ifdef HAVE_STRUCT_UTMP |
235 | #ifdef HAVE_STRUCT_UTMP |
230 | # ifdef HAVE_UTMP_PID |
236 | # ifdef HAVE_UTMP_PID |
231 | MEMSET(ut, 0, sizeof(struct utmp)); |
237 | MEMSET(ut, 0, sizeof(struct utmp)); |
232 | setutent(); |
238 | setutent(); |
233 | STRNCPY(ut->ut_id, ut_id, sizeof(ut->ut_id)); |
239 | STRNCPY(ut->ut_id, ut_id, sizeof(ut->ut_id)); |
234 | ut->ut_type = USER_PROCESS; |
240 | ut->ut_type = USER_PROCESS; |
235 | { |
241 | { |
236 | struct utmp *tmput = getutid(ut); |
242 | struct utmp *tmput = getutid(ut); |
237 | |
243 | |
238 | if (tmput) /* position to entry in utmp file */ |
244 | if (tmput) /* position to entry in utmp file */ |
239 | ut = tmput; |
245 | ut = tmput; |
240 | } |
246 | } |
241 | ut->ut_type = DEAD_PROCESS; |
247 | ut->ut_type = DEAD_PROCESS; |
242 | # else |
248 | # else |
243 | MEMSET(ut->ut_name, 0, sizeof(ut->ut_name)); |
249 | MEMSET(ut->ut_name, 0, sizeof(ut->ut_name)); |
244 | # ifdef HAVE_UTMP_HOST |
250 | # ifdef HAVE_UTMP_HOST |
245 | MEMSET(ut->ut_host, 0, sizeof(ut->ut_host)); |
251 | MEMSET(ut->ut_host, 0, sizeof(ut->ut_host)); |
246 | # endif |
|
|
247 | # endif |
252 | # endif |
|
|
253 | # endif |
248 | ut->ut_time = time(NULL); |
254 | ut->ut_time = time(NULL); |
249 | #endif |
255 | #endif |
250 | |
256 | |
251 | #ifdef HAVE_STRUCT_UTMPX |
257 | #ifdef HAVE_STRUCT_UTMPX |
252 | MEMSET(utx, 0, sizeof(struct utmpx)); |
258 | MEMSET(utx, 0, sizeof(struct utmpx)); |
253 | setutxent(); |
259 | setutxent(); |
254 | STRNCPY(utx->ut_id, ut_id, sizeof(utx->ut_id)); |
260 | STRNCPY(utx->ut_id, ut_id, sizeof(utx->ut_id)); |
255 | utx->ut_type = USER_PROCESS; |
261 | utx->ut_type = USER_PROCESS; |
256 | if ((tmputx = getutxid(utx))) /* position to entry in utmp file */ |
262 | if ((tmputx = getutxid(utx))) /* position to entry in utmp file */ |
257 | utx = tmputx; |
263 | utx = tmputx; |
258 | utx->ut_type = DEAD_PROCESS; |
264 | utx->ut_type = DEAD_PROCESS; |
259 | utx->ut_session = getsid(0); |
265 | utx->ut_session = getsid(0); |
260 | utx->ut_tv.tv_sec = time(NULL); |
266 | utx->ut_tv.tv_sec = time(NULL); |
261 | utx->ut_tv.tv_usec = 0; |
267 | utx->ut_tv.tv_usec = 0; |
262 | #endif |
268 | #endif |
263 | |
269 | |
264 | /* |
270 | /* |
265 | * Write ending wtmp entry |
271 | * Write ending wtmp entry |
266 | */ |
272 | */ |
267 | #ifdef WTMP_SUPPORT |
273 | #ifdef WTMP_SUPPORT |
268 | # ifdef WTMP_ONLY_ON_LOGIN |
274 | # ifdef WTMP_ONLY_ON_LOGIN |
269 | if (Options & Opt_loginShell) |
275 | if (Options & Opt_loginShell) |
270 | # endif |
276 | # endif |
271 | { |
277 | { |
272 | # ifdef HAVE_STRUCT_UTMP |
278 | # ifdef HAVE_STRUCT_UTMP |
273 | # ifdef HAVE_UPDWTMP |
279 | # ifdef HAVE_UPDWTMP |
274 | updwtmp(RXVT_WTMP_FILE, ut); |
280 | updwtmp(RXVT_WTMP_FILE, ut); |
275 | # else |
281 | # else |
276 | rxvt_update_wtmp(RXVT_WTMP_FILE, ut); |
282 | rxvt_update_wtmp(RXVT_WTMP_FILE, ut); |
277 | # endif |
283 | # endif |
278 | # endif |
284 | # endif |
279 | # ifdef HAVE_STRUCT_UTMPX |
285 | # ifdef HAVE_STRUCT_UTMPX |
280 | updwtmpx(RXVT_WTMPX_FILE, utx); |
286 | updwtmpx(RXVT_WTMPX_FILE, utx); |
281 | # endif |
|
|
282 | } |
|
|
283 | #endif |
287 | # endif |
|
|
288 | } |
|
|
289 | #endif |
284 | |
290 | |
285 | /* |
291 | /* |
286 | * Write utmp entry |
292 | * Write utmp entry |
287 | */ |
293 | */ |
288 | #ifdef HAVE_STRUCT_UTMP |
294 | #ifdef HAVE_STRUCT_UTMP |
289 | # ifdef HAVE_UTMP_PID |
295 | # ifdef HAVE_UTMP_PID |
290 | if (ut->ut_pid == cmd_pid) |
296 | if (ut->ut_pid == cmd_pid) |
291 | pututline(ut); |
297 | pututline(ut); |
292 | endutent(); |
298 | endutent(); |
293 | # else |
299 | # else |
294 | MEMSET(ut, 0, sizeof(struct utmp)); |
300 | MEMSET(ut, 0, sizeof(struct utmp)); |
295 | rxvt_write_bsd_utmp(utmp_pos, ut); |
301 | rxvt_write_bsd_utmp(utmp_pos, ut); |
296 | # endif |
|
|
297 | #endif |
302 | # endif |
|
|
303 | #endif |
298 | #ifdef HAVE_STRUCT_UTMPX |
304 | #ifdef HAVE_STRUCT_UTMPX |
299 | if (utx->ut_pid == cmd_pid) |
305 | if (utx->ut_pid == cmd_pid) |
300 | pututxline(utx); |
306 | pututxline(utx); |
301 | endutxent(); |
307 | endutxent(); |
302 | #endif |
308 | #endif |
303 | } |
309 | } |
304 | |
310 | |
305 | /* ------------------------------------------------------------------------- */ |
311 | /* ------------------------------------------------------------------------- */ |
306 | /* |
312 | /* |
… | |
… | |
309 | #ifdef HAVE_UTMP_H |
315 | #ifdef HAVE_UTMP_H |
310 | /* INTPROTO */ |
316 | /* INTPROTO */ |
311 | int |
317 | int |
312 | rxvt_write_bsd_utmp(int utmp_pos, struct utmp *wu) |
318 | rxvt_write_bsd_utmp(int utmp_pos, struct utmp *wu) |
313 | { |
319 | { |
314 | int fd; |
320 | int fd; |
315 | |
321 | |
316 | if (utmp_pos <= 0 || (fd = open(RXVT_UTMP_FILE, O_WRONLY)) == -1) |
322 | if (utmp_pos <= 0 || (fd = open(RXVT_UTMP_FILE, O_WRONLY)) == -1) |
317 | return 0; |
323 | return 0; |
318 | |
324 | |
319 | if (lseek(fd, (off_t) (utmp_pos * sizeof(struct utmp)), SEEK_SET) != -1) |
325 | if (lseek(fd, (off_t) (utmp_pos * sizeof(struct utmp)), SEEK_SET) != -1) |
320 | write(fd, wu, sizeof(struct utmp)); |
326 | write(fd, wu, sizeof(struct utmp)); |
321 | close(fd); |
327 | close(fd); |
322 | return 1; |
328 | return 1; |
323 | } |
329 | } |
324 | #endif |
330 | #endif |
325 | |
331 | |
326 | /* ------------------------------------------------------------------------- */ |
332 | /* ------------------------------------------------------------------------- */ |
327 | /* |
333 | /* |
… | |
… | |
330 | #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) |
336 | #if defined(WTMP_SUPPORT) && !defined(HAVE_UPDWTMP) |
331 | /* INTPROTO */ |
337 | /* INTPROTO */ |
332 | void |
338 | void |
333 | rxvt_update_wtmp(const char *fname, const struct utmp *putmp) |
339 | rxvt_update_wtmp(const char *fname, const struct utmp *putmp) |
334 | { |
340 | { |
335 | int fd, gotlock, retry; |
341 | int fd, gotlock, retry; |
336 | struct flock lck; /* fcntl locking scheme */ |
342 | struct flock lck; /* fcntl locking scheme */ |
337 | struct stat sbuf; |
343 | struct stat sbuf; |
338 | |
344 | |
339 | if ((fd = open(fname, O_WRONLY | O_APPEND, 0)) < 0) |
345 | if ((fd = open(fname, O_WRONLY | O_APPEND, 0)) < 0) |
340 | return; |
346 | return; |
341 | |
347 | |
342 | lck.l_whence = SEEK_END; /* start lock at current eof */ |
348 | lck.l_whence = SEEK_END; /* start lock at current eof */ |
343 | lck.l_len = 0; /* end at ``largest possible eof'' */ |
349 | lck.l_len = 0; /* end at ``largest possible eof'' */ |
344 | lck.l_start = 0; |
350 | lck.l_start = 0; |
345 | lck.l_type = F_WRLCK; /* we want a write lock */ |
351 | lck.l_type = F_WRLCK; /* we want a write lock */ |
346 | |
352 | |
347 | /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */ |
353 | /* attempt lock with F_SETLK; F_SETLKW would cause a deadlock! */ |
348 | for (retry = 10, gotlock = 0; retry--;) |
354 | for (retry = 10, gotlock = 0; retry--;) |
349 | if (fcntl(fd, F_SETLK, &lck) != -1) { |
355 | if (fcntl(fd, F_SETLK, &lck) != -1) |
|
|
356 | { |
350 | gotlock = 1; |
357 | gotlock = 1; |
351 | break; |
358 | break; |
|
|
359 | } |
352 | } else if (errno != EAGAIN && errno != EACCES) |
360 | else if (errno != EAGAIN && errno != EACCES) |
353 | break; |
361 | break; |
|
|
362 | if (!gotlock) |
354 | if (!gotlock) { /* give it up */ |
363 | { /* give it up */ |
355 | close(fd); |
364 | close(fd); |
356 | return; |
365 | return; |
357 | } |
366 | } |
358 | if (fstat(fd, &sbuf) == 0) |
367 | if (fstat(fd, &sbuf) == 0) |
359 | if (write(fd, putmp, sizeof(struct utmp)) != sizeof(struct utmp)) |
368 | if (write(fd, putmp, sizeof(struct utmp)) != sizeof(struct utmp)) |
360 | ftruncate(fd, sbuf.st_size); /* remove bad writes */ |
369 | ftruncate(fd, sbuf.st_size); /* remove bad writes */ |
361 | |
370 | |
362 | lck.l_type = F_UNLCK; /* unlocking the file */ |
371 | lck.l_type = F_UNLCK; /* unlocking the file */ |
363 | fcntl(fd, F_SETLK, &lck); |
372 | fcntl(fd, F_SETLK, &lck); |
364 | close(fd); |
373 | close(fd); |
365 | } |
374 | } |
366 | #endif |
375 | #endif |
367 | |
376 | |
368 | /* ------------------------------------------------------------------------- */ |
377 | /* ------------------------------------------------------------------------- */ |
369 | #ifdef LASTLOG_SUPPORT |
378 | #ifdef LASTLOG_SUPPORT |
370 | /* INTPROTO */ |
379 | /* INTPROTO */ |
371 | void |
380 | void |
372 | rxvt_update_lastlog(const char *fname, const char *pty, const char *host) |
381 | rxvt_update_lastlog(const char *fname, const char *pty, const char *host) |
373 | { |
382 | { |
374 | # ifdef HAVE_STRUCT_LASTLOGX |
383 | # ifdef HAVE_STRUCT_LASTLOGX |
375 | struct lastlogx llx; |
384 | struct lastlogx llx; |
376 | # endif |
385 | # endif |
377 | # ifdef HAVE_STRUCT_LASTLOG |
386 | # ifdef HAVE_STRUCT_LASTLOG |
378 | int fd; |
387 | int fd; |
379 | struct lastlog ll; |
388 | struct lastlog ll; |
380 | # ifdef LASTLOG_IS_DIR |
389 | # ifdef LASTLOG_IS_DIR |
381 | char lastlogfile[256]; |
390 | char lastlogfile[256]; |
382 | # endif |
391 | # endif |
383 | struct passwd *pwent; |
392 | struct passwd *pwent; |
384 | # endif |
393 | # endif |
385 | |
394 | |
386 | # ifdef HAVE_STRUCT_LASTLOGX |
395 | # ifdef HAVE_STRUCT_LASTLOGX |
387 | MEMSET(&llx, 0, sizeof(llx)); |
396 | MEMSET(&llx, 0, sizeof(llx)); |
388 | llx.ll_tv.tv_sec = time(NULL); |
397 | llx.ll_tv.tv_sec = time(NULL); |
389 | llx.ll_tv.tv_usec = 0; |
398 | llx.ll_tv.tv_usec = 0; |
390 | STRNCPY(llx.ll_line, pty, sizeof(llx.ll_line)); |
399 | STRNCPY(llx.ll_line, pty, sizeof(llx.ll_line)); |
391 | STRNCPY(llx.ll_host, host, sizeof(llx.ll_host)); |
400 | STRNCPY(llx.ll_host, host, sizeof(llx.ll_host)); |
392 | updlastlogx(RXVT_LASTLOGX_FILE, getuid(), &llx); |
401 | updlastlogx(RXVT_LASTLOGX_FILE, getuid(), &llx); |
393 | # endif |
402 | # endif |
394 | |
403 | |
395 | # ifdef HAVE_STRUCT_LASTLOG |
404 | # ifdef HAVE_STRUCT_LASTLOG |
396 | pwent = getpwuid(getuid()); |
405 | pwent = getpwuid(getuid()); |
397 | if (!pwent) { |
406 | if (!pwent) |
|
|
407 | { |
398 | rxvt_print_error("no entry in password file"); |
408 | rxvt_print_error("no entry in password file"); |
399 | return; |
409 | return; |
400 | } |
410 | } |
401 | MEMSET(&ll, 0, sizeof(ll)); |
411 | MEMSET(&ll, 0, sizeof(ll)); |
402 | ll.ll_time = time(NULL); |
412 | ll.ll_time = time(NULL); |
403 | STRNCPY(ll.ll_line, pty, sizeof(ll.ll_line)); |
413 | STRNCPY(ll.ll_line, pty, sizeof(ll.ll_line)); |
404 | STRNCPY(ll.ll_host, host, sizeof(ll.ll_host)); |
414 | STRNCPY(ll.ll_host, host, sizeof(ll.ll_host)); |
405 | # ifdef LASTLOG_IS_DIR |
415 | # ifdef LASTLOG_IS_DIR |
406 | sprintf(lastlogfile, "%.*s/%.*s", |
416 | sprintf(lastlogfile, "%.*s/%.*s", |
407 | sizeof(lastlogfile) - sizeof(pwent->pw_name) - 2, fname, |
417 | sizeof(lastlogfile) - sizeof(pwent->pw_name) - 2, fname, |
408 | sizeof(pwent->pw_name), |
418 | sizeof(pwent->pw_name), |
409 | (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown" |
419 | (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown" |
410 | : pwent->pw_name); |
420 | : pwent->pw_name); |
411 | if ((fd = open(lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0) { |
421 | if ((fd = open(lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0) |
|
|
422 | { |
412 | write(fd, &ll, sizeof(ll)); |
423 | write(fd, &ll, sizeof(ll)); |
413 | close(fd); |
424 | close(fd); |
414 | } |
425 | } |
415 | # else |
426 | # else |
416 | if ((fd = open(fname, O_RDWR)) != -1) { |
427 | if ((fd = open(fname, O_RDWR)) != -1) |
|
|
428 | { |
417 | if (lseek(fd, (off_t) ((long)pwent->pw_uid * sizeof(ll)), |
429 | if (lseek(fd, (off_t) ((long)pwent->pw_uid * sizeof(ll)), |
418 | SEEK_SET) != -1) |
430 | SEEK_SET) != -1) |
419 | write(fd, &ll, sizeof(ll)); |
431 | write(fd, &ll, sizeof(ll)); |
420 | close(fd); |
432 | close(fd); |
421 | } |
433 | } |
422 | # endif /* LASTLOG_IS_DIR */ |
434 | # endif /* LASTLOG_IS_DIR */ |
423 | # endif /* HAVE_STRUCT_LASTLOG */ |
435 | # endif /* HAVE_STRUCT_LASTLOG */ |
424 | } |
436 | } |
425 | #endif /* LASTLOG_SUPPORT */ |
437 | #endif /* LASTLOG_SUPPORT */ |