1 /* 2 * Copyright (c) 1994 Christopher G. Demetriou. 3 * @(#)Copyright (c) 1994, Simon J. Gerraty. 4 * 5 * This is free software. It comes with NO WARRANTY. 6 * Permission to use, modify and distribute this source code 7 * is granted subject to the following conditions. 8 * 1/ that the above copyright notice and this notice 9 * are preserved in all copies and that due credit be given 10 * to the author. 11 * 2/ that any changes to this code are clearly commented 12 * as such so that the author does not get blamed for bugs 13 * other than his own. 14 */ 15 16 #ifndef lint 17 static const char rcsid[] = 18 "$FreeBSD$"; 19 #endif /* not lint */ 20 21 #include <sys/types.h> 22 #include <sys/file.h> 23 #include <sys/time.h> 24 #include <err.h> 25 #include <errno.h> 26 #include <langinfo.h> 27 #include <locale.h> 28 #include <pwd.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <unistd.h> 33 #include <utmp.h> 34 35 /* 36 * this is for our list of currently logged in sessions 37 */ 38 struct utmp_list { 39 struct utmp_list *next; 40 struct utmp usr; 41 }; 42 43 /* 44 * this is for our list of users that are accumulating time. 45 */ 46 struct user_list { 47 struct user_list *next; 48 char name[UT_NAMESIZE+1]; 49 time_t secs; 50 }; 51 52 /* 53 * this is for chosing whether to ignore a login 54 */ 55 struct tty_list { 56 struct tty_list *next; 57 char name[UT_LINESIZE+3]; 58 int len; 59 int ret; 60 }; 61 62 /* 63 * globals - yes yuk 64 */ 65 #ifdef CONSOLE_TTY 66 static char *Console = CONSOLE_TTY; 67 #endif 68 static time_t Total = 0; 69 static time_t FirstTime = 0; 70 static int Flags = 0; 71 static struct user_list *Users = NULL; 72 static struct tty_list *Ttys = NULL; 73 74 #define NEW(type) (type *)malloc(sizeof (type)) 75 76 #define AC_W 1 /* not _PATH_WTMP */ 77 #define AC_D 2 /* daily totals (ignore -p) */ 78 #define AC_P 4 /* per-user totals */ 79 #define AC_U 8 /* specified users only */ 80 #define AC_T 16 /* specified ttys only */ 81 82 #ifdef DEBUG 83 static int Debug = 0; 84 #endif 85 86 int main __P((int, char **)); 87 int ac __P((FILE *)); 88 struct tty_list *add_tty __P((char *)); 89 int do_tty __P((char *)); 90 FILE *file __P((const char *)); 91 struct utmp_list *log_in __P((struct utmp_list *, struct utmp *)); 92 struct utmp_list *log_out __P((struct utmp_list *, struct utmp *)); 93 int on_console __P((struct utmp_list *)); 94 void show __P((const char *, time_t)); 95 void show_today __P((struct user_list *, struct utmp_list *, 96 time_t)); 97 void show_users __P((struct user_list *)); 98 struct user_list *update_user __P((struct user_list *, char *, time_t)); 99 void usage __P((void)); 100 101 /* 102 * open wtmp or die 103 */ 104 FILE * 105 file(name) 106 const char *name; 107 { 108 FILE *fp; 109 110 if ((fp = fopen(name, "r")) == NULL) 111 err(1, "%s", name); 112 /* in case we want to discriminate */ 113 if (strcmp(_PATH_WTMP, name)) 114 Flags |= AC_W; 115 return fp; 116 } 117 118 struct tty_list * 119 add_tty(name) 120 char *name; 121 { 122 struct tty_list *tp; 123 register char *rcp; 124 125 Flags |= AC_T; 126 127 if ((tp = NEW(struct tty_list)) == NULL) 128 errx(1, "malloc failed"); 129 tp->len = 0; /* full match */ 130 tp->ret = 1; /* do if match */ 131 if (*name == '!') { /* don't do if match */ 132 tp->ret = 0; 133 name++; 134 } 135 strlcpy(tp->name, name, sizeof (tp->name)); 136 if ((rcp = strchr(tp->name, '*')) != NULL) { /* wild card */ 137 *rcp = '\0'; 138 tp->len = strlen(tp->name); /* match len bytes only */ 139 } 140 tp->next = Ttys; 141 Ttys = tp; 142 return Ttys; 143 } 144 145 /* 146 * should we process the named tty? 147 */ 148 int 149 do_tty(name) 150 char *name; 151 { 152 struct tty_list *tp; 153 int def_ret = 0; 154 155 for (tp = Ttys; tp != NULL; tp = tp->next) { 156 if (tp->ret == 0) /* specific don't */ 157 def_ret = 1; /* default do */ 158 if (tp->len != 0) { 159 if (strncmp(name, tp->name, tp->len) == 0) 160 return tp->ret; 161 } else { 162 if (strncmp(name, tp->name, sizeof (tp->name)) == 0) 163 return tp->ret; 164 } 165 } 166 return def_ret; 167 } 168 169 #ifdef CONSOLE_TTY 170 /* 171 * is someone logged in on Console? 172 */ 173 int 174 on_console(head) 175 struct utmp_list *head; 176 { 177 struct utmp_list *up; 178 179 for (up = head; up; up = up->next) { 180 if (strncmp(up->usr.ut_line, Console, 181 sizeof (up->usr.ut_line)) == 0) 182 return 1; 183 } 184 return 0; 185 } 186 #endif 187 188 /* 189 * update user's login time 190 */ 191 struct user_list * 192 update_user(head, name, secs) 193 struct user_list *head; 194 char *name; 195 time_t secs; 196 { 197 struct user_list *up; 198 199 for (up = head; up != NULL; up = up->next) { 200 if (strncmp(up->name, name, UT_NAMESIZE) == 0) { 201 up->secs += secs; 202 Total += secs; 203 return head; 204 } 205 } 206 /* 207 * not found so add new user unless specified users only 208 */ 209 if (Flags & AC_U) 210 return head; 211 212 if ((up = NEW(struct user_list)) == NULL) 213 errx(1, "malloc failed"); 214 up->next = head; 215 strlcpy(up->name, name, sizeof (up->name)); 216 up->secs = secs; 217 Total += secs; 218 return up; 219 } 220 221 int 222 main(argc, argv) 223 int argc; 224 char **argv; 225 { 226 FILE *fp; 227 int c; 228 229 (void) setlocale(LC_TIME, ""); 230 231 fp = NULL; 232 while ((c = getopt(argc, argv, "Dc:dpt:w:")) != -1) { 233 switch (c) { 234 #ifdef DEBUG 235 case 'D': 236 Debug++; 237 break; 238 #endif 239 case 'c': 240 #ifdef CONSOLE_TTY 241 Console = optarg; 242 #else 243 usage(); /* XXX */ 244 #endif 245 break; 246 case 'd': 247 Flags |= AC_D; 248 break; 249 case 'p': 250 Flags |= AC_P; 251 break; 252 case 't': /* only do specified ttys */ 253 add_tty(optarg); 254 break; 255 case 'w': 256 fp = file(optarg); 257 break; 258 case '?': 259 default: 260 usage(); 261 break; 262 } 263 } 264 if (optind < argc) { 265 /* 266 * initialize user list 267 */ 268 for (; optind < argc; optind++) { 269 Users = update_user(Users, argv[optind], 0L); 270 } 271 Flags |= AC_U; /* freeze user list */ 272 } 273 if (Flags & AC_D) 274 Flags &= ~AC_P; 275 if (fp == NULL) { 276 /* 277 * if _PATH_WTMP does not exist, exit quietly 278 */ 279 if (access(_PATH_WTMP, 0) != 0 && errno == ENOENT) 280 return 0; 281 282 fp = file(_PATH_WTMP); 283 } 284 ac(fp); 285 286 return 0; 287 } 288 289 /* 290 * print login time in decimal hours 291 */ 292 void 293 show(name, secs) 294 const char *name; 295 time_t secs; 296 { 297 (void)printf("\t%-*s %8.2f\n", UT_NAMESIZE, name, 298 ((double)secs / 3600)); 299 } 300 301 void 302 show_users(list) 303 struct user_list *list; 304 { 305 struct user_list *lp; 306 307 for (lp = list; lp; lp = lp->next) 308 show(lp->name, lp->secs); 309 } 310 311 /* 312 * print total login time for 24hr period in decimal hours 313 */ 314 void 315 show_today(users, logins, secs) 316 struct user_list *users; 317 struct utmp_list *logins; 318 time_t secs; 319 { 320 struct user_list *up; 321 struct utmp_list *lp; 322 char date[64]; 323 time_t yesterday = secs - 1; 324 static int d_first = -1; 325 326 if (d_first < 0) 327 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 328 (void)strftime(date, sizeof (date), 329 d_first ? "%e %b total" : "%b %e total", 330 localtime(&yesterday)); 331 332 /* restore the missing second */ 333 yesterday++; 334 335 for (lp = logins; lp != NULL; lp = lp->next) { 336 secs = yesterday - lp->usr.ut_time; 337 Users = update_user(Users, lp->usr.ut_name, secs); 338 lp->usr.ut_time = yesterday; /* as if they just logged in */ 339 } 340 secs = 0; 341 for (up = users; up != NULL; up = up->next) { 342 secs += up->secs; 343 up->secs = 0; /* for next day */ 344 } 345 if (secs) 346 (void)printf("%s %11.2f\n", date, ((double)secs / 3600)); 347 } 348 349 /* 350 * log a user out and update their times. 351 * if ut_line is "~", we log all users out as the system has 352 * been shut down. 353 */ 354 struct utmp_list * 355 log_out(head, up) 356 struct utmp_list *head; 357 struct utmp *up; 358 { 359 struct utmp_list *lp, *lp2, *tlp; 360 time_t secs; 361 362 for (lp = head, lp2 = NULL; lp != NULL; ) 363 if (*up->ut_line == '~' || strncmp(lp->usr.ut_line, up->ut_line, 364 sizeof (up->ut_line)) == 0) { 365 secs = up->ut_time - lp->usr.ut_time; 366 Users = update_user(Users, lp->usr.ut_name, secs); 367 #ifdef DEBUG 368 if (Debug) 369 printf("%-.*s %-.*s: %-.*s logged out (%2d:%02d:%02d)\n", 370 19, ctime(&up->ut_time), 371 sizeof (lp->usr.ut_line), lp->usr.ut_line, 372 sizeof (lp->usr.ut_name), lp->usr.ut_name, 373 secs / 3600, (secs % 3600) / 60, secs % 60); 374 #endif 375 /* 376 * now lose it 377 */ 378 tlp = lp; 379 lp = lp->next; 380 if (tlp == head) 381 head = lp; 382 else if (lp2 != NULL) 383 lp2->next = lp; 384 free(tlp); 385 } else { 386 lp2 = lp; 387 lp = lp->next; 388 } 389 return head; 390 } 391 392 393 /* 394 * if do_tty says ok, login a user 395 */ 396 struct utmp_list * 397 log_in(head, up) 398 struct utmp_list *head; 399 struct utmp *up; 400 { 401 struct utmp_list *lp; 402 403 /* 404 * this could be a login. if we're not dealing with 405 * the console name, say it is. 406 * 407 * If we are, and if ut_host==":0.0" we know that it 408 * isn't a real login. _But_ if we have not yet recorded 409 * someone being logged in on Console - due to the wtmp 410 * file starting after they logged in, we'll pretend they 411 * logged in, at the start of the wtmp file. 412 */ 413 414 #ifdef CONSOLE_TTY 415 if (up->ut_host[0] == ':') { 416 /* 417 * SunOS 4.0.2 does not treat ":0.0" as special but we 418 * do. 419 */ 420 if (on_console(head)) 421 return head; 422 /* 423 * ok, no recorded login, so they were here when wtmp 424 * started! Adjust ut_time! 425 */ 426 up->ut_time = FirstTime; 427 /* 428 * this allows us to pick the right logout 429 */ 430 strlcpy(up->ut_line, Console, sizeof (up->ut_line)); 431 } 432 #endif 433 /* 434 * If we are doing specified ttys only, we ignore 435 * anything else. 436 */ 437 if (Flags & AC_T) 438 if (!do_tty(up->ut_line)) 439 return head; 440 441 /* 442 * go ahead and log them in 443 */ 444 if ((lp = NEW(struct utmp_list)) == NULL) 445 errx(1, "malloc failed"); 446 lp->next = head; 447 head = lp; 448 memmove((char *)&lp->usr, (char *)up, sizeof (struct utmp)); 449 #ifdef DEBUG 450 if (Debug) { 451 printf("%-.*s %-.*s: %-.*s logged in", 19, 452 ctime(&lp->usr.ut_time), sizeof (up->ut_line), 453 up->ut_line, sizeof (up->ut_name), up->ut_name); 454 if (*up->ut_host) 455 printf(" (%-.*s)", sizeof (up->ut_host), up->ut_host); 456 putchar('\n'); 457 } 458 #endif 459 return head; 460 } 461 462 int 463 ac(fp) 464 FILE *fp; 465 { 466 struct utmp_list *lp, *head = NULL; 467 struct utmp usr; 468 struct tm *ltm; 469 time_t secs; 470 int day = -1; 471 472 while (fread((char *)&usr, sizeof(usr), 1, fp) == 1) { 473 if (!FirstTime) 474 FirstTime = usr.ut_time; 475 if (Flags & AC_D) { 476 time_t t = int_to_time(usr.ut_time); 477 ltm = localtime(&t); 478 if (day >= 0 && day != ltm->tm_yday) { 479 day = ltm->tm_yday; 480 /* 481 * print yesterday's total 482 */ 483 secs = usr.ut_time; 484 secs -= ltm->tm_sec; 485 secs -= 60 * ltm->tm_min; 486 secs -= 3600 * ltm->tm_hour; 487 show_today(Users, head, secs); 488 } else 489 day = ltm->tm_yday; 490 } 491 switch(*usr.ut_line) { 492 case '|': 493 secs = usr.ut_time; 494 break; 495 case '{': 496 secs -= usr.ut_time; 497 /* 498 * adjust time for those logged in 499 */ 500 for (lp = head; lp != NULL; lp = lp->next) 501 lp->usr.ut_time -= secs; 502 break; 503 case '~': /* reboot or shutdown */ 504 head = log_out(head, &usr); 505 FirstTime = usr.ut_time; /* shouldn't be needed */ 506 break; 507 default: 508 /* 509 * if they came in on tty[p-sP-S]*, then it is only 510 * a login session if the ut_host field is non-empty 511 */ 512 if (*usr.ut_name) { 513 if (strncmp(usr.ut_line, "tty", 3) == 0 || 514 strchr("pqrsPQRS", usr.ut_line[3]) != 0 || 515 *usr.ut_host != '\0') 516 head = log_in(head, &usr); 517 } else 518 head = log_out(head, &usr); 519 break; 520 } 521 } 522 (void)fclose(fp); 523 if (!(Flags & AC_W)) 524 usr.ut_time = time((time_t *)0); 525 (void)strcpy(usr.ut_line, "~"); 526 527 if (Flags & AC_D) { 528 time_t t = int_to_time(usr.ut_time); 529 ltm = localtime(&t); 530 if (day >= 0 && day != ltm->tm_yday) { 531 /* 532 * print yesterday's total 533 */ 534 secs = usr.ut_time; 535 secs -= ltm->tm_sec; 536 secs -= 60 * ltm->tm_min; 537 secs -= 3600 * ltm->tm_hour; 538 show_today(Users, head, secs); 539 } 540 } 541 /* 542 * anyone still logged in gets time up to now 543 */ 544 head = log_out(head, &usr); 545 546 if (Flags & AC_D) 547 show_today(Users, head, time((time_t *)0)); 548 else { 549 if (Flags & AC_P) 550 show_users(Users); 551 show("total", Total); 552 } 553 return 0; 554 } 555 556 void 557 usage() 558 { 559 (void)fprintf(stderr, 560 #ifdef CONSOLE_TTY 561 "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n"); 562 #else 563 "ac [-dp] [-t tty] [-w wtmp] [users ...]\n"); 564 #endif 565 exit(1); 566 } 567