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