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 * $FreeBSD: src/usr.sbin/ac/ac.c,v 1.14.2.2 2002/03/12 19:55:04 phantom Exp $ 16 * $DragonFly: src/usr.sbin/ac/ac.c,v 1.2 2003/06/17 04:29:52 dillon Exp $ 17 */ 18 19 #include <sys/types.h> 20 #include <sys/file.h> 21 #include <sys/time.h> 22 #include <err.h> 23 #include <errno.h> 24 #include <langinfo.h> 25 #include <locale.h> 26 #include <pwd.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <unistd.h> 31 #include <utmp.h> 32 33 /* 34 * this is for our list of currently logged in sessions 35 */ 36 struct utmp_list { 37 struct utmp_list *next; 38 struct utmp usr; 39 }; 40 41 /* 42 * this is for our list of users that are accumulating time. 43 */ 44 struct user_list { 45 struct user_list *next; 46 char name[UT_NAMESIZE+1]; 47 time_t secs; 48 }; 49 50 /* 51 * this is for chosing whether to ignore a login 52 */ 53 struct tty_list { 54 struct tty_list *next; 55 char name[UT_LINESIZE+3]; 56 int len; 57 int ret; 58 }; 59 60 /* 61 * globals - yes yuk 62 */ 63 #ifdef CONSOLE_TTY 64 static char *Console = CONSOLE_TTY; 65 #endif 66 static time_t Total = 0; 67 static time_t FirstTime = 0; 68 static int Flags = 0; 69 static struct user_list *Users = NULL; 70 static struct tty_list *Ttys = NULL; 71 72 #define NEW(type) (type *)malloc(sizeof (type)) 73 74 #define AC_W 1 /* not _PATH_WTMP */ 75 #define AC_D 2 /* daily totals (ignore -p) */ 76 #define AC_P 4 /* per-user totals */ 77 #define AC_U 8 /* specified users only */ 78 #define AC_T 16 /* specified ttys only */ 79 80 #ifdef DEBUG 81 static int Debug = 0; 82 #endif 83 84 int main __P((int, char **)); 85 int ac __P((FILE *)); 86 struct tty_list *add_tty __P((char *)); 87 int do_tty __P((char *)); 88 FILE *file __P((char *)); 89 struct utmp_list *log_in __P((struct utmp_list *, struct utmp *)); 90 struct utmp_list *log_out __P((struct utmp_list *, struct utmp *)); 91 int on_console __P((struct utmp_list *)); 92 void show __P((char *, time_t)); 93 void show_today __P((struct user_list *, struct utmp_list *, 94 time_t)); 95 void show_users __P((struct user_list *)); 96 struct user_list *update_user __P((struct user_list *, char *, time_t)); 97 void usage __P((void)); 98 99 /* 100 * open wtmp or die 101 */ 102 FILE * 103 file(name) 104 char *name; 105 { 106 FILE *fp; 107 108 if ((fp = fopen(name, "r")) == NULL) 109 err(1, "%s", name); 110 /* in case we want to discriminate */ 111 if (strcmp(_PATH_WTMP, name)) 112 Flags |= AC_W; 113 return fp; 114 } 115 116 struct tty_list * 117 add_tty(name) 118 char *name; 119 { 120 struct tty_list *tp; 121 register char *rcp; 122 123 Flags |= AC_T; 124 125 if ((tp = NEW(struct tty_list)) == NULL) 126 errx(1, "malloc failed"); 127 tp->len = 0; /* full match */ 128 tp->ret = 1; /* do if match */ 129 if (*name == '!') { /* don't do if match */ 130 tp->ret = 0; 131 name++; 132 } 133 (void)strncpy(tp->name, name, sizeof (tp->name) - 1); 134 tp->name[sizeof (tp->name) - 1] = '\0'; 135 if ((rcp = strchr(tp->name, '*')) != NULL) { /* wild card */ 136 *rcp = '\0'; 137 tp->len = strlen(tp->name); /* match len bytes only */ 138 } 139 tp->next = Ttys; 140 Ttys = tp; 141 return Ttys; 142 } 143 144 /* 145 * should we process the named tty? 146 */ 147 int 148 do_tty(name) 149 char *name; 150 { 151 struct tty_list *tp; 152 int def_ret = 0; 153 154 for (tp = Ttys; tp != NULL; tp = tp->next) { 155 if (tp->ret == 0) /* specific don't */ 156 def_ret = 1; /* default do */ 157 if (tp->len != 0) { 158 if (strncmp(name, tp->name, tp->len) == 0) 159 return tp->ret; 160 } else { 161 if (strncmp(name, tp->name, sizeof (tp->name)) == 0) 162 return tp->ret; 163 } 164 } 165 return def_ret; 166 } 167 168 #ifdef CONSOLE_TTY 169 /* 170 * is someone logged in on Console? 171 */ 172 int 173 on_console(head) 174 struct utmp_list *head; 175 { 176 struct utmp_list *up; 177 178 for (up = head; up; up = up->next) { 179 if (strncmp(up->usr.ut_line, Console, 180 sizeof (up->usr.ut_line)) == 0) 181 return 1; 182 } 183 return 0; 184 } 185 #endif 186 187 /* 188 * update user's login time 189 */ 190 struct user_list * 191 update_user(head, name, secs) 192 struct user_list *head; 193 char *name; 194 time_t secs; 195 { 196 struct user_list *up; 197 198 for (up = head; up != NULL; up = up->next) { 199 if (strncmp(up->name, name, UT_NAMESIZE) == 0) { 200 up->secs += secs; 201 Total += secs; 202 return head; 203 } 204 } 205 /* 206 * not found so add new user unless specified users only 207 */ 208 if (Flags & AC_U) 209 return head; 210 211 if ((up = NEW(struct user_list)) == NULL) 212 errx(1, "malloc failed"); 213 up->next = head; 214 (void)strncpy(up->name, name, sizeof (up->name) - 1); 215 up->name[sizeof (up->name) - 1] = '\0'; /* paranoid! */ 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 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 (void)strncpy(up->ut_line, Console, sizeof (up->ut_line) - 1); 431 up->ut_line[sizeof (up->ut_line) - 1] = '\0'; /* paranoid! */ 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 ltm = localtime(&usr.ut_time); 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 ltm = localtime(&usr.ut_time); 529 if (day >= 0 && day != ltm->tm_yday) { 530 /* 531 * print yesterday's total 532 */ 533 secs = usr.ut_time; 534 secs -= ltm->tm_sec; 535 secs -= 60 * ltm->tm_min; 536 secs -= 3600 * ltm->tm_hour; 537 show_today(Users, head, secs); 538 } 539 } 540 /* 541 * anyone still logged in gets time up to now 542 */ 543 head = log_out(head, &usr); 544 545 if (Flags & AC_D) 546 show_today(Users, head, time((time_t *)0)); 547 else { 548 if (Flags & AC_P) 549 show_users(Users); 550 show("total", Total); 551 } 552 return 0; 553 } 554 555 void 556 usage() 557 { 558 (void)fprintf(stderr, 559 #ifdef CONSOLE_TTY 560 "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n"); 561 #else 562 "ac [-dp] [-t tty] [-w wtmp] [users ...]\n"); 563 #endif 564 exit(1); 565 } 566