]> git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/last.c
Imported from util-linux-2.9v tarball.
[thirdparty/util-linux.git] / login-utils / last.c
1 /*
2 * Berkeley last for Linux. Currently maintained by poe@daimi.aau.dk at
3 * ftp://ftp.daimi.aau.dk/pub/linux/poe/admutil*
4 *
5 * Copyright (c) 1987 Regents of the University of California.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 */
20
21 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
22 * - added Native Language Support
23 */
24
25 /*
26 * last
27 */
28 #include <sys/param.h>
29 #include <sys/stat.h>
30 #include <sys/file.h>
31 #include <sys/types.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <time.h>
35 #include <utmp.h>
36 #include <stdio.h>
37 #include <getopt.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44
45 #include "pathnames.h"
46 #include "nls.h"
47
48 #define SECDAY (24*60*60) /* seconds in a day */
49 #define NO 0 /* false/no */
50 #define YES 1 /* true/yes */
51
52 static struct utmp utmpbuf;
53
54 #define HMAX (int)sizeof(utmpbuf.ut_host) /* size of utmp host field */
55 #define LMAX (int)sizeof(utmpbuf.ut_line) /* size of utmp tty field */
56 #define NMAX (int)sizeof(utmpbuf.ut_name) /* size of utmp name field */
57
58 #ifndef MIN
59 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
60 #endif
61
62 /* maximum sizes used for printing */
63 /* probably we want a two-pass version that computes the right length */
64 int hmax = MIN(HMAX, 16);
65 int lmax = MIN(LMAX, 8);
66 int nmax = MIN(NMAX, 16);
67
68 typedef struct arg {
69 char *name; /* argument */
70 #define HOST_TYPE -2
71 #define TTY_TYPE -3
72 #define USER_TYPE -4
73 #define INET_TYPE -5
74 int type; /* type of arg */
75 struct arg *next; /* linked list pointer */
76 } ARG;
77 ARG *arglist; /* head of linked list */
78
79 typedef struct ttytab {
80 long logout; /* log out time */
81 char tty[LMAX + 1]; /* terminal name */
82 struct ttytab *next; /* linked list pointer */
83 } TTY;
84 TTY *ttylist; /* head of linked list */
85
86 static long currentout, /* current logout value */
87 maxrec; /* records to display */
88 static char *file = _PATH_WTMP; /* wtmp file */
89
90 static int doyear = 0; /* output year in dates */
91 static int dolong = 0; /* print also ip-addr */
92
93 static void wtmp(), addarg(), hostconv();
94 static int want();
95 TTY *addtty();
96 static char *ttyconv();
97
98 int
99 main(argc, argv)
100 int argc;
101 char **argv;
102 {
103 extern int optind;
104 extern char *optarg;
105 int ch;
106
107 setlocale(LC_ALL, "");
108 bindtextdomain(PACKAGE, LOCALEDIR);
109 textdomain(PACKAGE);
110
111 while ((ch = getopt(argc, argv, "0123456789yli:f:h:t:")) != EOF)
112 switch((char)ch) {
113 case '0': case '1': case '2': case '3': case '4':
114 case '5': case '6': case '7': case '8': case '9':
115 /*
116 * kludge: last was originally designed to take
117 * a number after a dash.
118 */
119 if (!maxrec)
120 maxrec = atol(argv[optind - 1] + 1);
121 break;
122 case 'f':
123 file = optarg;
124 break;
125 case 'h':
126 hostconv(optarg);
127 addarg(HOST_TYPE, optarg);
128 break;
129 case 't':
130 addarg(TTY_TYPE, ttyconv(optarg));
131 break;
132 case 'y':
133 doyear = 1;
134 break;
135 case 'l':
136 dolong = 1;
137 break;
138 case 'i':
139 addarg(INET_TYPE, optarg);
140 break;
141 case '?':
142 default:
143 fputs(_("usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n"), stderr);
144 exit(1);
145 }
146 for (argv += optind; *argv; ++argv) {
147 #define COMPATIBILITY
148 #ifdef COMPATIBILITY
149 /* code to allow "last p5" to work */
150 addarg(TTY_TYPE, ttyconv(*argv));
151 #endif
152 addarg(USER_TYPE, *argv);
153 }
154 wtmp();
155 exit(0);
156 }
157
158 /*
159 * print_partial_line --
160 * print the first part of each output line according to specified format
161 */
162 void
163 print_partial_line(bp)
164 struct utmp *bp;
165 {
166 char *ct;
167
168 ct = ctime(&bp->ut_time);
169 printf("%-*.*s %-*.*s ", nmax, nmax, bp->ut_name,
170 lmax, lmax, bp->ut_line);
171
172 if (dolong) {
173 if (bp->ut_addr) {
174 struct in_addr foo;
175 foo.s_addr = bp->ut_addr;
176 printf("%-*.*s ", hmax, hmax, inet_ntoa(foo));
177 } else {
178 printf("%-*.*s ", hmax, hmax, "");
179 }
180 } else {
181 printf("%-*.*s ", hmax, hmax, bp->ut_host);
182 }
183
184 if (doyear) {
185 printf("%10.10s %4.4s %5.5s ", ct, ct + 20, ct + 11);
186 } else {
187 printf("%10.10s %5.5s ", ct, ct + 11);
188 }
189 }
190
191 /*
192 * wtmp --
193 * read through the wtmp file
194 */
195 static void
196 wtmp()
197 {
198 register struct utmp *bp; /* current structure */
199 register TTY *T; /* tty list entry */
200 long delta; /* time difference */
201 void onintr();
202 char *crmsg = NULL;
203 char *ct = NULL;
204 struct utmp **utmplist = NULL;
205 int listlen = 0;
206 int listnr = 0;
207 int i;
208
209 utmpname(file);
210
211 (void)time(&utmpbuf.ut_time);
212 (void)signal(SIGINT, onintr);
213 (void)signal(SIGQUIT, onintr);
214
215 setutent();
216 while((bp = getutent())) {
217 if(listnr >= listlen) {
218 listlen += 10;
219 listlen *= 2; /* avoid quadratic behaviour */
220 utmplist = realloc(utmplist, sizeof(bp) * listlen);
221 }
222
223 utmplist[listnr] = malloc(sizeof(*bp));
224 memcpy(utmplist[listnr++], bp, sizeof(*bp));
225 }
226 endutent();
227
228 if(listnr)
229 ct = ctime(&utmplist[0]->ut_time);
230
231 for(i = listnr - 1; i >= 0; i--) {
232 bp = utmplist[i];
233 /*
234 * if the terminal line is '~', the machine stopped.
235 * see utmp(5) for more info.
236 */
237 if (!strncmp(bp->ut_line, "~", LMAX)) {
238 /*
239 * utmp(5) also mentions that the user
240 * name should be 'shutdown' or 'reboot'.
241 * Not checking the name causes e.g. runlevel
242 * changes to be displayed as 'crash'. -thaele
243 */
244 if (!strncmp(bp->ut_user, "reboot", NMAX) ||
245 !strncmp(bp->ut_user, "shutdown", NMAX)) {
246 /* everybody just logged out */
247 for (T = ttylist; T; T = T->next)
248 T->logout = -bp->ut_time;
249 }
250
251 currentout = -bp->ut_time;
252 crmsg = strncmp(bp->ut_name, "shutdown", NMAX) ? "crash" : "down ";
253 if (!bp->ut_name[0])
254 (void)strcpy(bp->ut_name, "reboot");
255 if (want(bp, NO)) {
256 ct = ctime(&bp->ut_time);
257 if(bp->ut_type != LOGIN_PROCESS) {
258 print_partial_line(bp);
259 putchar('\n');
260 }
261 if (maxrec && !--maxrec)
262 return;
263 }
264 continue;
265 }
266 /* find associated tty */
267 for (T = ttylist;; T = T->next) {
268 if (!T) {
269 /* add new one */
270 T = addtty(bp->ut_line);
271 break;
272 }
273 if (!strncmp(T->tty, bp->ut_line, LMAX))
274 break;
275 }
276 if (bp->ut_name[0] && bp->ut_type != LOGIN_PROCESS
277 && bp->ut_type != DEAD_PROCESS
278 && want(bp, YES)) {
279
280 print_partial_line(bp);
281
282 if (!T->logout)
283 puts(_(" still logged in"));
284 else {
285 if (T->logout < 0) {
286 T->logout = -T->logout;
287 printf("- %s", crmsg);
288 }
289 else
290 printf("- %5.5s", ctime(&T->logout)+11);
291 delta = T->logout - bp->ut_time;
292 if (delta < SECDAY)
293 printf(" (%5.5s)\n", asctime(gmtime(&delta))+11);
294 else
295 printf(" (%ld+%5.5s)\n", delta / SECDAY, asctime(gmtime(&delta))+11);
296 }
297 if (maxrec != -1 && !--maxrec)
298 return;
299 }
300 T->logout = bp->ut_time;
301 utmpbuf.ut_time = bp->ut_time;
302 free(bp);
303 }
304 if(utmplist) free(utmplist);
305 if(ct) printf(_("\nwtmp begins %s"), ct); /* ct already ends in \n */
306 }
307
308 /*
309 * want --
310 * see if want this entry
311 */
312 static int
313 want(bp, check)
314 register struct utmp *bp;
315 int check;
316 {
317 register ARG *step;
318
319 if (check) {
320 /*
321 * when uucp and ftp log in over a network, the entry in
322 * the utmp file is the name plus their process id. See
323 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
324 */
325 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
326 bp->ut_line[3] = '\0';
327 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
328 bp->ut_line[4] = '\0';
329 }
330 if (!arglist)
331 return(YES);
332
333 for (step = arglist; step; step = step->next)
334 switch(step->type) {
335 case HOST_TYPE:
336 if (!strncmp(step->name, bp->ut_host, HMAX))
337 return(YES);
338 break;
339 case TTY_TYPE:
340 if (!strncmp(step->name, bp->ut_line, LMAX))
341 return(YES);
342 break;
343 case USER_TYPE:
344 if (!strncmp(step->name, bp->ut_name, NMAX))
345 return(YES);
346 break;
347 case INET_TYPE:
348 if (bp->ut_addr == inet_addr(step->name))
349 return(YES);
350 break;
351 }
352 return(NO);
353 }
354
355 /*
356 * addarg --
357 * add an entry to a linked list of arguments
358 */
359 static void
360 addarg(type, arg)
361 int type;
362 char *arg;
363 {
364 register ARG *cur;
365
366 if (!(cur = (ARG *)malloc((unsigned int)sizeof(ARG)))) {
367 fputs(_("last: malloc failure.\n"), stderr);
368 exit(1);
369 }
370 cur->next = arglist;
371 cur->type = type;
372 cur->name = arg;
373 arglist = cur;
374 }
375
376 /*
377 * addtty --
378 * add an entry to a linked list of ttys
379 */
380 TTY *
381 addtty(ttyname)
382 char *ttyname;
383 {
384 register TTY *cur;
385
386 if (!(cur = (TTY *)malloc((unsigned int)sizeof(TTY)))) {
387 fputs(_("last: malloc failure.\n"), stderr);
388 exit(1);
389 }
390 cur->next = ttylist;
391 cur->logout = currentout;
392 memcpy(cur->tty, ttyname, LMAX);
393 return(ttylist = cur);
394 }
395
396 /*
397 * hostconv --
398 * convert the hostname to search pattern; if the supplied host name
399 * has a domain attached that is the same as the current domain, rip
400 * off the domain suffix since that's what login(1) does.
401 */
402 static void
403 hostconv(arg)
404 char *arg;
405 {
406 static int first = 1;
407 static char *hostdot,
408 name[MAXHOSTNAMELEN];
409 char *argdot;
410
411 if (!(argdot = strchr(arg, '.')))
412 return;
413 if (first) {
414 first = 0;
415 if (gethostname(name, sizeof(name))) {
416 perror(_("last: gethostname"));
417 exit(1);
418 }
419 hostdot = strchr(name, '.');
420 }
421 if (hostdot && !strcmp(hostdot, argdot))
422 *argdot = '\0';
423 }
424
425 /*
426 * ttyconv --
427 * convert tty to correct name.
428 */
429 static char *
430 ttyconv(arg)
431 char *arg;
432 {
433 char *mval;
434
435 /*
436 * kludge -- we assume that all tty's end with
437 * a two character suffix.
438 */
439 if (strlen(arg) == 2) {
440 /* either 6 for "ttyxx" or 8 for "console" */
441 if (!(mval = malloc((unsigned int)8))) {
442 fputs(_("last: malloc failure.\n"), stderr);
443 exit(1);
444 }
445 if (!strcmp(arg, "co"))
446 (void)strcpy(mval, "console");
447 else {
448 (void)strcpy(mval, "tty");
449 (void)strcpy(mval + 3, arg);
450 }
451 return(mval);
452 }
453 if (!strncmp(arg, "/dev/", sizeof("/dev/") - 1))
454 return(arg + 5);
455 return(arg);
456 }
457
458 /*
459 * onintr --
460 * on interrupt, we inform the user how far we've gotten
461 */
462 void
463 onintr(signo)
464 int signo;
465 {
466 char *ct;
467
468 ct = ctime(&utmpbuf.ut_time);
469 printf(_("\ninterrupted %10.10s %5.5s \n"), ct, ct + 11);
470 if (signo == SIGINT)
471 exit(1);
472 (void)fflush(stdout); /* fix required for rsh */
473 }