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