|
|
1.1 ! root 1: /* ! 2: * Halt ! 3: */ ! 4: #include <stdio.h> ! 5: #include <sys/reboot.h> ! 6: #include <sys/types.h> ! 7: #include <time.h> ! 8: #include <errno.h> ! 9: #include <signal.h> ! 10: ! 11: #define SHUTDOWNLOG "/usr/adm/shutdownlog" ! 12: ! 13: main(argc, argv) ! 14: int argc; ! 15: char **argv; ! 16: { ! 17: int howto; ! 18: char *ttyn = (char *)ttyname(2); ! 19: register i; ! 20: register qflag; ! 21: ! 22: howto = RB_HALT; ! 23: argc--, argv++; ! 24: while (argc > 0) { ! 25: if (!strcmp(*argv, "-n")) ! 26: howto |= RB_NOSYNC; ! 27: else if (!strcmp(*argv, "-y")) ! 28: ttyn = 0; ! 29: else if (!strcmp(*argv, "-q")) ! 30: qflag++; ! 31: else { ! 32: fprintf(stderr, "usage: halt [ -n ]\n"); ! 33: exit(1); ! 34: } ! 35: argc--, argv++; ! 36: } ! 37: if (ttyn && *(ttyn+strlen("/dev/tty")) == 'd') { ! 38: fprintf(stderr, "halt: dangerous on a dialup; use ``halt -y'' if you are really sure\n"); ! 39: exit(1); ! 40: } ! 41: ! 42: if (kill(1, SIGTSTP) == -1) { ! 43: fprintf(stderr, "reboot: can't idle init\n"); ! 44: exit(1); ! 45: } ! 46: ! 47: if (!qflag) for (i = 1; ; i++) { ! 48: if (kill(-1, SIGKILL) == -1) { ! 49: extern int errno; ! 50: ! 51: if (errno == ESRCH) ! 52: break; ! 53: ! 54: perror("reboot: kill"); ! 55: kill(1, SIGHUP); ! 56: exit(1); ! 57: } ! 58: if (i > 5) { ! 59: fprintf(stderr, "CAUTION: some process(es) wouldn't die\n"); ! 60: break; ! 61: } ! 62: setalarm(2 * i); ! 63: pause(); ! 64: } ! 65: ! 66: if ((howto & RB_NOSYNC) == 0) ! 67: log_entry(); ! 68: if (!qflag) { ! 69: if ((howto & RB_NOSYNC)==0) { ! 70: markdown(); ! 71: sync(); ! 72: sync(); ! 73: } ! 74: setalarm(5); ! 75: pause(); ! 76: } ! 77: syscall(55, howto); ! 78: perror("reboot"); ! 79: } ! 80: ! 81: dingdong() ! 82: { ! 83: /* RRRIIINNNGGG RRRIIINNNGGG */ ! 84: } ! 85: ! 86: setalarm(n) ! 87: { ! 88: signal(SIGALRM, dingdong); ! 89: alarm(n); ! 90: } ! 91: ! 92: #include <utmp.h> ! 93: #define SCPYN(a, b) strncpy(a, b, sizeof(a)) ! 94: char wtmpf[] = "/usr/adm/wtmp"; ! 95: struct utmp wtmp; ! 96: ! 97: markdown() ! 98: { ! 99: register f = open(wtmpf, 1); ! 100: if (f >= 0) { ! 101: lseek(f, 0L, 2); ! 102: SCPYN(wtmp.ut_line, "~"); ! 103: SCPYN(wtmp.ut_name, "shutdown"); ! 104: time(&wtmp.ut_time); ! 105: write(f, (char *)&wtmp, sizeof(wtmp)); ! 106: close(f); ! 107: } ! 108: } ! 109: ! 110: char *days[] = { ! 111: "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ! 112: }; ! 113: ! 114: char *months[] = { ! 115: "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", ! 116: "Oct", "Nov", "Dec" ! 117: }; ! 118: ! 119: log_entry() ! 120: { ! 121: FILE *fp; ! 122: struct tm *tm, *localtime(); ! 123: time_t now; ! 124: ! 125: time(&now); ! 126: tm = localtime(&now); ! 127: fp = fopen(SHUTDOWNLOG, "a"); ! 128: if (fp == NULL) ! 129: return; ! 130: fseek(fp, 0L, 2); ! 131: fprintf(fp, "%02d:%02d %s %s %2d, %4d. Halted.\n", tm->tm_hour, ! 132: tm->tm_min, days[tm->tm_wday], months[tm->tm_mon], ! 133: tm->tm_mday, tm->tm_year + 1900); ! 134: fclose(fp); ! 135: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.