]> git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/last.c
Imported from util-linux-2.10s 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(void);
94 static void addarg(int, char *);
95 static void hostconv(char *);
96 static void onintr(int);
97 static int want(struct utmp *, int);
98 TTY *addtty(char *);
99 static char *ttyconv(char *);
100
101 int
102 main(int argc, char **argv) {
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 static void
163 print_partial_line(struct utmp *bp) {
164 char *ct;
165
166 ct = ctime(&bp->ut_time);
167 printf("%-*.*s %-*.*s ", nmax, nmax, bp->ut_name,
168 lmax, lmax, bp->ut_line);
169
170 if (dolong) {
171 if (bp->ut_addr) {
172 struct in_addr foo;
173 foo.s_addr = bp->ut_addr;
174 printf("%-*.*s ", hmax, hmax, inet_ntoa(foo));
175 } else {
176 printf("%-*.*s ", hmax, hmax, "");
177 }
178 } else {
179 printf("%-*.*s ", hmax, hmax, bp->ut_host);
180 }
181
182 if (doyear) {
183 printf("%10.10s %4.4s %5.5s ", ct, ct + 20, ct + 11);
184 } else {
185 printf("%10.10s %5.5s ", ct, ct + 11);
186 }
187 }
188
189 /*
190 * wtmp --
191 * read through the wtmp file
192 */
193 static void
194 wtmp(void) {
195 register struct utmp *bp; /* current structure */
196 register TTY *T; /* tty list entry */
197 long delta; /* time difference */
198 char *crmsg = NULL;
199 char *ct = NULL;
200 struct utmp **utmplist = NULL;
201 int listlen = 0;
202 int listnr = 0;
203 int i;
204
205 utmpname(file);
206
207 (void)time(&utmpbuf.ut_time);
208 (void)signal(SIGINT, onintr);
209 (void)signal(SIGQUIT, onintr);
210
211 setutent();
212 while((bp = getutent())) {
213 if(listnr >= listlen) {
214 listlen += 10;
215 listlen *= 2; /* avoid quadratic behaviour */
216 utmplist = realloc(utmplist, sizeof(bp) * listlen);
217 }
218
219 utmplist[listnr] = malloc(sizeof(*bp));
220 memcpy(utmplist[listnr++], bp, sizeof(*bp));
221 }
222 endutent();
223
224 if(listnr)
225 ct = ctime(&utmplist[0]->ut_time);
226
227 for(i = listnr - 1; i >= 0; i--) {
228 bp = utmplist[i];
229 /*
230 * if the terminal line is '~', the machine stopped.
231 * see utmp(5) for more info.
232 */
233 if (!strncmp(bp->ut_line, "~", LMAX)) {
234 /*
235 * utmp(5) also mentions that the user
236 * name should be 'shutdown' or 'reboot'.
237 * Not checking the name causes e.g. runlevel
238 * changes to be displayed as 'crash'. -thaele
239 */
240 if (!strncmp(bp->ut_user, "reboot", NMAX) ||
241 !strncmp(bp->ut_user, "shutdown", NMAX)) {
242 /* everybody just logged out */
243 for (T = ttylist; T; T = T->next)
244 T->logout = -bp->ut_time;
245 }
246
247 currentout = -bp->ut_time;
248 crmsg = strncmp(bp->ut_name, "shutdown", NMAX) ? "crash" : "down ";
249 if (!bp->ut_name[0])
250 (void)strcpy(bp->ut_name, "reboot");
251 if (want(bp, NO)) {
252 ct = ctime(&bp->ut_time);
253 if(bp->ut_type != LOGIN_PROCESS) {
254 print_partial_line(bp);
255 putchar('\n');
256 }
257 if (maxrec && !--maxrec)
258 return;
259 }
260 continue;
261 }
262 /* find associated tty */
263 for (T = ttylist;; T = T->next) {
264 if (!T) {
265 /* add new one */
266 T = addtty(bp->ut_line);
267 break;
268 }
269 if (!strncmp(T->tty, bp->ut_line, LMAX))
270 break;
271 }
272 if (bp->ut_name[0] && bp->ut_type != LOGIN_PROCESS
273 && bp->ut_type != DEAD_PROCESS
274 && want(bp, YES)) {
275
276 print_partial_line(bp);
277
278 if (!T->logout)
279 puts(_(" still logged in"));
280 else {
281 if (T->logout < 0) {
282 T->logout = -T->logout;
283 printf("- %s", crmsg);
284 }
285 else
286 printf("- %5.5s", ctime(&T->logout)+11);
287 delta = T->logout - bp->ut_time;
288 if (delta < SECDAY)
289 printf(" (%5.5s)\n", asctime(gmtime(&delta))+11);
290 else
291 printf(" (%ld+%5.5s)\n", delta / SECDAY, asctime(gmtime(&delta))+11);
292 }
293 if (maxrec != -1 && !--maxrec)
294 return;
295 }
296 T->logout = bp->ut_time;
297 utmpbuf.ut_time = bp->ut_time;
298 free(bp);
299 }
300 if(utmplist) free(utmplist);
301 if(ct) printf(_("\nwtmp begins %s"), ct); /* ct already ends in \n */
302 }
303
304 /*
305 * want --
306 * see if want this entry
307 */
308 static int
309 want(struct utmp *bp, int check) {
310 register ARG *step;
311
312 if (check) {
313 /*
314 * when uucp and ftp log in over a network, the entry in
315 * the utmp file is the name plus their process id. See
316 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
317 */
318 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
319 bp->ut_line[3] = '\0';
320 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
321 bp->ut_line[4] = '\0';
322 }
323 if (!arglist)
324 return(YES);
325
326 for (step = arglist; step; step = step->next)
327 switch(step->type) {
328 case HOST_TYPE:
329 if (!strncmp(step->name, bp->ut_host, HMAX))
330 return(YES);
331 break;
332 case TTY_TYPE:
333 if (!strncmp(step->name, bp->ut_line, LMAX))
334 return(YES);
335 break;
336 case USER_TYPE:
337 if (!strncmp(step->name, bp->ut_name, NMAX))
338 return(YES);
339 break;
340 case INET_TYPE:
341 if (bp->ut_addr == inet_addr(step->name))
342 return(YES);
343 break;
344 }
345 return(NO);
346 }
347
348 /*
349 * addarg --
350 * add an entry to a linked list of arguments
351 */
352 static void
353 addarg(int type, char *arg) {
354 register ARG *cur;
355
356 if (!(cur = (ARG *)malloc((unsigned int)sizeof(ARG)))) {
357 fputs(_("last: malloc failure.\n"), stderr);
358 exit(1);
359 }
360 cur->next = arglist;
361 cur->type = type;
362 cur->name = arg;
363 arglist = cur;
364 }
365
366 /*
367 * addtty --
368 * add an entry to a linked list of ttys
369 */
370 TTY *
371 addtty(char *ttyname) {
372 register TTY *cur;
373
374 if (!(cur = (TTY *)malloc((unsigned int)sizeof(TTY)))) {
375 fputs(_("last: malloc failure.\n"), stderr);
376 exit(1);
377 }
378 cur->next = ttylist;
379 cur->logout = currentout;
380 memcpy(cur->tty, ttyname, LMAX);
381 return(ttylist = cur);
382 }
383
384 /*
385 * hostconv --
386 * convert the hostname to search pattern; if the supplied host name
387 * has a domain attached that is the same as the current domain, rip
388 * off the domain suffix since that's what login(1) does.
389 */
390 static void
391 hostconv(char *arg) {
392 static int first = 1;
393 static char *hostdot,
394 name[MAXHOSTNAMELEN];
395 char *argdot;
396
397 if (!(argdot = strchr(arg, '.')))
398 return;
399 if (first) {
400 first = 0;
401 if (gethostname(name, sizeof(name))) {
402 perror(_("last: gethostname"));
403 exit(1);
404 }
405 hostdot = strchr(name, '.');
406 }
407 if (hostdot && !strcmp(hostdot, argdot))
408 *argdot = '\0';
409 }
410
411 /*
412 * ttyconv --
413 * convert tty to correct name.
414 */
415 static char *
416 ttyconv(char *arg) {
417 char *mval;
418
419 /*
420 * kludge -- we assume that all tty's end with
421 * a two character suffix.
422 */
423 if (strlen(arg) == 2) {
424 /* either 6 for "ttyxx" or 8 for "console" */
425 if (!(mval = malloc((unsigned int)8))) {
426 fputs(_("last: malloc failure.\n"), stderr);
427 exit(1);
428 }
429 if (!strcmp(arg, "co"))
430 (void)strcpy(mval, "console");
431 else {
432 (void)strcpy(mval, "tty");
433 (void)strcpy(mval + 3, arg);
434 }
435 return(mval);
436 }
437 if (!strncmp(arg, "/dev/", sizeof("/dev/") - 1))
438 return(arg + 5);
439 return(arg);
440 }
441
442 /*
443 * onintr --
444 * on interrupt, we inform the user how far we've gotten
445 */
446 static void
447 onintr(int signo) {
448 char *ct;
449
450 ct = ctime(&utmpbuf.ut_time);
451 printf(_("\ninterrupted %10.10s %5.5s \n"), ct, ct + 11);
452 if (signo == SIGINT)
453 exit(1);
454 (void)fflush(stdout); /* fix required for rsh */
455 }