1 #include <errno.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <sys/reboot.h> 5 #include <unistd.h> 6 7 int reboot_main(int argc, char *argv[]) 8 { 9 int ret; 10 int nosync = 0; 11 int poweroff = 0; 12 13 opterr = 0; 14 do { 15 int c; 16 17 c = getopt(argc, argv, "np"); 18 19 if (c == EOF) { 20 break; 21 } 22 23 switch (c) { 24 case 'n': 25 nosync = 1; 26 break; 27 case 'p': 28 poweroff = 1; 29 break; 30 case '?': 31 fprintf(stderr, "usage: %s [-n] [-p] [rebootcommand]\n", argv[0]); 32 exit(EXIT_FAILURE); 33 } 34 } while (1); 35 36 if(argc > optind + 1) { 37 fprintf(stderr, "%s: too many arguments\n", argv[0]); 38 exit(EXIT_FAILURE); 39 } 40 41 if(!nosync) 42 sync(); 43 44 if(poweroff) 45 ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF, NULL); 46 else if(argc > optind) 47 ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, argv[optind]); 48 else 49 ret = reboot(RB_AUTOBOOT); 50 if(ret < 0) { 51 perror("reboot"); 52 exit(EXIT_FAILURE); 53 } 54 fprintf(stderr, "reboot returned\n"); 55 return 0; 56 } 57