[9] | 1 | /*
|
---|
| 2 | shutdown - close down the system graciously
|
---|
| 3 |
|
---|
| 4 | Author: Edvard Tuinder <v892231@si.hhs.NL>
|
---|
| 5 |
|
---|
| 6 | This program informs the users that the system is going
|
---|
| 7 | down, when and why. After that a shutdown notice is written in
|
---|
| 8 | both /usr/adm/wtmp and by syslog(3). Then reboot(2) is called
|
---|
| 9 | to really close the system.
|
---|
| 10 |
|
---|
| 11 | This actually is a ``nice'' halt(8).
|
---|
| 12 |
|
---|
| 13 | Options are supposed to be as with BSD
|
---|
| 14 | -h: shutdown and halt the system
|
---|
| 15 | -r: shutdown and reboot
|
---|
| 16 | -k: stop an already running shutdown
|
---|
| 17 | -o: obsolete: not implemented
|
---|
| 18 |
|
---|
| 19 | New Minix options:
|
---|
| 20 | -C: crash check, i.e. is the last wtmp entry a shutdown entry?
|
---|
| 21 | -x: let the monitor execute the given code
|
---|
| 22 | -R: reset the system
|
---|
| 23 | */
|
---|
| 24 |
|
---|
| 25 | #define _POSIX_SOURCE 1
|
---|
| 26 | #include <sys/types.h>
|
---|
| 27 | #include <sys/stat.h>
|
---|
| 28 | #include <stdio.h>
|
---|
| 29 | #include <ctype.h>
|
---|
| 30 | #include <fcntl.h>
|
---|
| 31 | #include <time.h>
|
---|
| 32 | #include <stdlib.h>
|
---|
| 33 | #include <signal.h>
|
---|
| 34 | #include <string.h>
|
---|
| 35 | #include <unistd.h>
|
---|
| 36 | #include <utmp.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #undef WTMP
|
---|
| 39 |
|
---|
| 40 | static char WTMP[] = "/usr/adm/wtmp";
|
---|
| 41 | static char SHUT_PID[] = "/usr/run/shutdown.pid";
|
---|
| 42 | static char NOLOGIN[] = "/etc/nologin";
|
---|
| 43 |
|
---|
| 44 | #ifndef __STDC__
|
---|
| 45 | #define inform_user_time inf_time
|
---|
| 46 | #define inform_user inf_user
|
---|
| 47 | #endif
|
---|
| 48 |
|
---|
| 49 | void usage _ARGS(( void ));
|
---|
| 50 | void write_pid _ARGS(( void ));
|
---|
| 51 | int inform_user_time _ARGS(( void ));
|
---|
| 52 | void inform_user _ARGS(( void ));
|
---|
| 53 | void terminate _ARGS(( void ));
|
---|
| 54 | void wall _ARGS(( char *when, char *extra ));
|
---|
| 55 | int crash_check _ARGS(( void ));
|
---|
| 56 | void parse_time _ARGS(( char *arg ));
|
---|
| 57 | void get_message _ARGS(( void ));
|
---|
| 58 | void main _ARGS(( int argc, char *argv[] ));
|
---|
| 59 | char *itoa _ARGS(( int n ));
|
---|
| 60 |
|
---|
| 61 | long wait_time=0L;
|
---|
| 62 | char message[1024];
|
---|
| 63 | char info[80];
|
---|
| 64 | int reboot_flag='h'; /* default is halt */
|
---|
| 65 | char *reboot_code=""; /* optional monitor code */
|
---|
| 66 | int info_min, info_hour;
|
---|
| 67 | char *prog;
|
---|
| 68 |
|
---|
| 69 | void parse_time (arg)
|
---|
| 70 | char *arg;
|
---|
| 71 | {
|
---|
| 72 | char *p = arg;
|
---|
| 73 | int hours, minutes;
|
---|
| 74 | time_t now;
|
---|
| 75 | struct tm *tm;
|
---|
| 76 | int delta = 0;
|
---|
| 77 | int bad = 0;
|
---|
| 78 |
|
---|
| 79 | if (p[0] == '+') { delta = 1; p++; }
|
---|
| 80 |
|
---|
| 81 | hours = strtoul(p, &p, 10);
|
---|
| 82 | if (*p == 0 && delta) {
|
---|
| 83 | minutes = hours;
|
---|
| 84 | hours = 0;
|
---|
| 85 | } else {
|
---|
| 86 | if (*p != ':' && *p != '.')
|
---|
| 87 | bad = 1;
|
---|
| 88 | else
|
---|
| 89 | p++;
|
---|
| 90 | minutes = strtoul(p, &p, 10);
|
---|
| 91 | if (*p != 0) bad = 1;
|
---|
| 92 | }
|
---|
| 93 | if (bad) {
|
---|
| 94 | fprintf(stderr,"Invalid time specification `%s'\n",arg);
|
---|
| 95 | usage();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | time(&now);
|
---|
| 99 | tm = localtime(&now);
|
---|
| 100 |
|
---|
| 101 | if (!delta) {
|
---|
| 102 | hours -= tm->tm_hour;
|
---|
| 103 | minutes -= tm->tm_min;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | if (minutes < 0) {
|
---|
| 107 | minutes += 60;
|
---|
| 108 | hours--;
|
---|
| 109 | }
|
---|
| 110 | if (hours < 0) hours += 24; /* Time after midnight. */
|
---|
| 111 |
|
---|
| 112 | tm->tm_hour += hours;
|
---|
| 113 | tm->tm_min += minutes;
|
---|
| 114 | (void) mktime(tm);
|
---|
| 115 | info_hour = tm->tm_hour;
|
---|
| 116 | info_min = tm->tm_min;
|
---|
| 117 |
|
---|
| 118 | sprintf(info,
|
---|
| 119 | "The system will shutdown in %d hour%s and %d minute%s at %02d:%02d\n\n",
|
---|
| 120 | hours,hours==1?"":"s",minutes,minutes==1?"":"s",info_hour,info_min);
|
---|
| 121 |
|
---|
| 122 | wait_time += hours * 3600 + minutes * 60;
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | void main(argc,argv)
|
---|
| 127 | int argc;
|
---|
| 128 | char *argv[];
|
---|
| 129 | {
|
---|
| 130 | int i, now = 0, nologin = 0, want_terminate = 0, want_message = 0, check = 0;
|
---|
| 131 | char *opt;
|
---|
| 132 | int tty;
|
---|
| 133 | static char HALT1[] = "-?";
|
---|
| 134 | static char *HALT[] = { "shutdown", HALT1, NULL, NULL };
|
---|
| 135 |
|
---|
| 136 | /* Parse options. */
|
---|
| 137 | for (i = 1; i < argc && argv[i][0] == '-'; i++) {
|
---|
| 138 | if (argv[i][1] == '-' && argv[i][2] == 0) {
|
---|
| 139 | /* -- */
|
---|
| 140 | i++;
|
---|
| 141 | break;
|
---|
| 142 | }
|
---|
| 143 | for (opt = argv[i] + 1; *opt != 0; opt++) {
|
---|
| 144 | switch (*opt) {
|
---|
| 145 | case 'k':
|
---|
| 146 | want_terminate = 1;
|
---|
| 147 | break;
|
---|
| 148 | case 'h':
|
---|
| 149 | case 'r':
|
---|
| 150 | case 'x':
|
---|
| 151 | reboot_flag = *opt;
|
---|
| 152 | if (reboot_flag == 'x') {
|
---|
| 153 | if (*++opt == 0) {
|
---|
| 154 | if (++i == argc) {
|
---|
| 155 | fprintf (stderr,"shutdown: option '-x' requires an argument\n");
|
---|
| 156 | usage();
|
---|
| 157 | }
|
---|
| 158 | opt=argv[i];
|
---|
| 159 | }
|
---|
| 160 | reboot_code=opt;
|
---|
| 161 | opt="";
|
---|
| 162 | }
|
---|
| 163 | break;
|
---|
| 164 | case 'R':
|
---|
| 165 | reboot_flag = 'R';
|
---|
| 166 | break;
|
---|
| 167 | case 'm':
|
---|
| 168 | want_message = 1;
|
---|
| 169 | break;
|
---|
| 170 | case 'C':
|
---|
| 171 | check = 1;
|
---|
| 172 | break;
|
---|
| 173 | default:
|
---|
| 174 | fprintf (stderr,"shutdown: invalid option '-%c'\n",*opt);
|
---|
| 175 | usage();
|
---|
| 176 | break;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | if ((argc - i) > 2) usage();
|
---|
| 181 |
|
---|
| 182 | if (check) exit(crash_check() ? 0 : 2);
|
---|
| 183 |
|
---|
| 184 | if (i == argc) {
|
---|
| 185 | /* No timespec, assume "now". */
|
---|
| 186 | now = 1;
|
---|
| 187 | } else {
|
---|
| 188 | if (!strcmp(argv[i], "now"))
|
---|
| 189 | now++;
|
---|
| 190 | else
|
---|
| 191 | parse_time(argv[i]);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | if ((argc - i) == 2) {
|
---|
| 195 | /* One line message */
|
---|
| 196 | strcat(message, argv[i+1]);
|
---|
| 197 | strcat(message, "\n");
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | if (want_terminate) terminate();
|
---|
| 201 | if (want_message) get_message();
|
---|
| 202 |
|
---|
| 203 | puts(info);
|
---|
| 204 |
|
---|
| 205 | prog = strrchr(*argv,'/');
|
---|
| 206 | if (prog == (char *)0)
|
---|
| 207 | prog = *argv;
|
---|
| 208 | else
|
---|
| 209 | prog++;
|
---|
| 210 |
|
---|
| 211 | if (!now) {
|
---|
| 212 | /* Daemonize. */
|
---|
| 213 | switch (fork()) {
|
---|
| 214 | case 0:
|
---|
| 215 | break;
|
---|
| 216 | case -1:
|
---|
| 217 | fprintf(stderr, "%s: can't fork\n", prog);
|
---|
| 218 | exit(1);
|
---|
| 219 | default:
|
---|
| 220 | exit(0);
|
---|
| 221 | }
|
---|
| 222 | /* Detach from the terminal (if any). */
|
---|
| 223 | if ((tty = open("/dev/tty", O_RDONLY)) != -1) {
|
---|
| 224 | close(tty);
|
---|
| 225 | setsid();
|
---|
| 226 | }
|
---|
| 227 | write_pid();
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | for (;;) {
|
---|
| 231 | if (wait_time <= 5 * 60 && !nologin && !now) {
|
---|
| 232 | close(creat(NOLOGIN,00644));
|
---|
| 233 | nologin = 1;
|
---|
| 234 | }
|
---|
| 235 | if (wait_time <= 60) break;
|
---|
| 236 | if(inform_user_time())
|
---|
| 237 | inform_user();
|
---|
| 238 | sleep (60);
|
---|
| 239 | wait_time -= 60;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | if (!now) {
|
---|
| 243 | inform_user();
|
---|
| 244 | sleep (30); /* Last minute before shutdown */
|
---|
| 245 | wait_time -= 30;
|
---|
| 246 | inform_user();
|
---|
| 247 | sleep (30); /* Last 30 seconds before shutdown */
|
---|
| 248 | }
|
---|
| 249 | wait_time = 0;
|
---|
| 250 | inform_user();
|
---|
| 251 |
|
---|
| 252 | unlink(SHUT_PID); /* No way of stopping anymore */
|
---|
| 253 | unlink(NOLOGIN);
|
---|
| 254 |
|
---|
| 255 | HALT[1][1] = reboot_flag;
|
---|
| 256 | if (reboot_flag == 'x') HALT[2] = reboot_code;
|
---|
| 257 | #if __minix_vmd
|
---|
| 258 | execv("/usr/sbin/halt", HALT);
|
---|
| 259 | #else
|
---|
| 260 | execv("/usr/bin/halt", HALT);
|
---|
| 261 | #endif
|
---|
| 262 | if (errno != ENOENT)
|
---|
| 263 | fprintf(stderr, "Can't execute 'halt': %s\n", strerror(errno));
|
---|
| 264 |
|
---|
| 265 | sleep(2);
|
---|
| 266 | reboot(RBT_HALT);
|
---|
| 267 | fprintf(stderr, "Reboot call failed: %s\n", strerror(errno));
|
---|
| 268 | exit(1);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | void usage()
|
---|
| 272 | {
|
---|
| 273 | fputs("Usage: shutdown [-hrRmk] [-x code] [time [message]]\n", stderr);
|
---|
| 274 | fputs(" -h -> halt system after shutdown\n", stderr);
|
---|
| 275 | fputs(" -r -> reboot system after shutdown\n", stderr);
|
---|
| 276 | fputs(" -R -> reset system after shutdown\n", stderr);
|
---|
| 277 | fputs(" -x -> return to the monitor doing...\n", stderr);
|
---|
| 278 | fputs(" -m -> read a shutdown message from standard input\n", stderr);
|
---|
| 279 | fputs(" -k -> stop an already running shutdown\n", stderr);
|
---|
| 280 | fputs(" code -> boot monitor code to be executed\n", stderr);
|
---|
| 281 | fputs(" time -> keyword ``now'', minutes before shutdown ``+5'',\n", stderr);
|
---|
| 282 | fputs(" or absolute time specification ``11:20''\n", stderr);
|
---|
| 283 | fputs(" message -> short shutdown message\n", stderr);
|
---|
| 284 | exit(1);
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | void terminate()
|
---|
| 288 | {
|
---|
| 289 | FILE *in;
|
---|
| 290 | pid_t pid;
|
---|
| 291 | char c_pid[5];
|
---|
| 292 | char buf[80];
|
---|
| 293 |
|
---|
| 294 | in = fopen(SHUT_PID,"r");
|
---|
| 295 | if (in == (FILE *)0) {
|
---|
| 296 | fputs ("Can't get pid of shutdown process, probably not running shutdown\n", stderr);
|
---|
| 297 | exit(1);
|
---|
| 298 | }
|
---|
| 299 | fgets(c_pid,5,in);
|
---|
| 300 | fclose(in);
|
---|
| 301 | pid = atoi(c_pid);
|
---|
| 302 | if (kill(pid,9) == -1)
|
---|
| 303 | fputs("Can't kill the shutdown process, probably not running anymore\n",stderr);
|
---|
| 304 | else
|
---|
| 305 | puts("Shutdown process terminated");
|
---|
| 306 | unlink(SHUT_PID);
|
---|
| 307 | unlink(NOLOGIN);
|
---|
| 308 | #ifdef not_very_useful
|
---|
| 309 | in = fopen (SHUT_LOG,"a");
|
---|
| 310 | if (in == (FILE *)0)
|
---|
| 311 | exit(0);
|
---|
| 312 | sprintf (buf, "Shutdown with pid %d terminated\n",pid);
|
---|
| 313 | fputs(buf,in);
|
---|
| 314 | fclose(in);
|
---|
| 315 | #endif
|
---|
| 316 | exit(0);
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | void get_message()
|
---|
| 320 | {
|
---|
| 321 | char line[80];
|
---|
| 322 | int max_lines=12;
|
---|
| 323 |
|
---|
| 324 | puts ("Type your message. End with ^D at an empty line");
|
---|
| 325 | fputs ("shutdown> ",stdout);fflush(stdout);
|
---|
| 326 | while (fgets(line,80,stdin) != (char *)0) {
|
---|
| 327 | strcat (message,line);
|
---|
| 328 | bzero(line,strlen(line));
|
---|
| 329 | fputs ("shutdown> ",stdout);fflush(stdout);
|
---|
| 330 | }
|
---|
| 331 | putc('\n',stdout);fflush(stdout);
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | int inform_user_time()
|
---|
| 335 | {
|
---|
| 336 | int min;
|
---|
| 337 |
|
---|
| 338 | min = wait_time /60;
|
---|
| 339 |
|
---|
| 340 | if (min == 60 || min == 30 || min == 15 || min == 10 || min <= 5)
|
---|
| 341 | return 1;
|
---|
| 342 | else
|
---|
| 343 | return 0;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | void inform_user()
|
---|
| 347 | {
|
---|
| 348 | int hour, minute;
|
---|
| 349 | char mes[80];
|
---|
| 350 |
|
---|
| 351 | hour = 0;
|
---|
| 352 | minute = wait_time / 60;
|
---|
| 353 | while (minute >= 60) {
|
---|
| 354 | minute -= 60;
|
---|
| 355 | hour++;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | if (hour)
|
---|
| 359 | sprintf(mes,
|
---|
| 360 | "\nThe system will shutdown in %d hour%s and %d minute%s at %.02d:%.02d\n\n",
|
---|
| 361 | hour,hour==1?"":"s",minute,minute==1?"":"s",info_hour,info_min);
|
---|
| 362 | else
|
---|
| 363 | if (minute > 1)
|
---|
| 364 | sprintf(mes,
|
---|
| 365 | "\nThe system will shutdown in %d minutes at %.02d:%.02d\n\n",
|
---|
| 366 | minute,info_hour,info_min);
|
---|
| 367 | else
|
---|
| 368 | if (wait_time > 1)
|
---|
| 369 | sprintf(mes,
|
---|
| 370 | "\nThe system will shutdown in %d seconds\n\n",
|
---|
| 371 | wait_time);
|
---|
| 372 | else
|
---|
| 373 | sprintf(mes,
|
---|
| 374 | "\nThe system will shutdown NOW\n\n");
|
---|
| 375 |
|
---|
| 376 | wall(mes,message);
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | void write_pid()
|
---|
| 380 | {
|
---|
| 381 | char pid[5];
|
---|
| 382 | int fd;
|
---|
| 383 |
|
---|
| 384 | fd = creat(SHUT_PID,00600);
|
---|
| 385 | if (!fd)
|
---|
| 386 | return;
|
---|
| 387 | strncpy (pid,itoa(getpid()), sizeof(pid));
|
---|
| 388 | write (fd,pid,sizeof(pid));
|
---|
| 389 | close(fd);
|
---|
| 390 | return;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | int crash_check()
|
---|
| 394 | {
|
---|
| 395 | struct utmp last;
|
---|
| 396 | int fd, crashed;
|
---|
| 397 | struct stat st;
|
---|
| 398 |
|
---|
| 399 | if (stat(WTMP, &st) < 0 || st.st_size == 0) return 0;
|
---|
| 400 | if ((fd = open(WTMP, O_RDONLY)) < 0) return 0;
|
---|
| 401 |
|
---|
| 402 | crashed = (lseek(fd, - (off_t) sizeof(last), SEEK_END) == -1
|
---|
| 403 | || read(fd, (void *) &last, sizeof(last)) != sizeof(last)
|
---|
| 404 | || last.ut_line[0] != '~'
|
---|
| 405 | || (strncmp(last.ut_user, "shutdown", sizeof(last.ut_user))
|
---|
| 406 | && strncmp(last.ut_user, "halt", sizeof(last.ut_user))));
|
---|
| 407 | close(fd);
|
---|
| 408 | return crashed;
|
---|
| 409 | }
|
---|