]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
1 | /* |
2 | * ctrlaltdel.c - Set the function of the Ctrl-Alt-Del combination | |
3 | * Created 4-Jul-92 by Peter Orbaek <poe@daimi.aau.dk> | |
b50945d4 | 4 | * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL> |
7eda085c | 5 | * - added Native Language Support |
6dbe3af9 KZ |
6 | */ |
7 | ||
028f7ed8 | 8 | #include <getopt.h> |
6dbe3af9 | 9 | #include <stdio.h> |
66ee8158 | 10 | #include <stdlib.h> |
6dbe3af9 | 11 | #include <string.h> |
0c3e5202 | 12 | #include <errno.h> |
e52b58e6 CTV |
13 | #include <unistd.h> |
14 | #include <sys/reboot.h> | |
7eda085c | 15 | #include "nls.h" |
eb76ca98 | 16 | #include "c.h" |
efb8854f | 17 | #include "closestream.h" |
82cac348 | 18 | #include "pathnames.h" |
741d478e | 19 | #include "path.h" |
6dbe3af9 | 20 | |
e52b58e6 CTV |
21 | #define LINUX_REBOOT_CMD_CAD_ON 0x89ABCDEF |
22 | #define LINUX_REBOOT_CMD_CAD_OFF 0x00000000 | |
23 | ||
86be6a32 | 24 | static void __attribute__((__noreturn__)) usage(void) |
0c3e5202 | 25 | { |
86be6a32 RM |
26 | FILE *out = stdout; |
27 | fputs(USAGE_HEADER, out); | |
b73142b9 | 28 | fprintf(out, _(" %s hard|soft\n"), program_invocation_short_name); |
451dbcfa | 29 | |
86be6a32 | 30 | fputs(USAGE_SEPARATOR, out); |
8137c98f | 31 | fprintf(out, _("Set the function of the Ctrl-Alt-Del combination.\n")); |
451dbcfa | 32 | |
86be6a32 RM |
33 | fputs(USAGE_OPTIONS, out); |
34 | fputs(USAGE_HELP, out); | |
35 | fputs(USAGE_VERSION,out); | |
6f162034 | 36 | fprintf(out, USAGE_MAN_TAIL("ctrlaltdel(8)")); |
86be6a32 | 37 | exit(EXIT_SUCCESS); |
028f7ed8 SK |
38 | } |
39 | ||
82cac348 SK |
40 | static int get_cad(void) |
41 | { | |
741d478e | 42 | uint64_t val = path_read_u64(_PATH_PROC_CTRL_ALT_DEL); |
82cac348 | 43 | |
82cac348 SK |
44 | switch (val) { |
45 | case 0: | |
46 | fputs("soft\n", stdout); | |
47 | break; | |
48 | case 1: | |
49 | fputs("hard\n", stdout); | |
50 | break; | |
51 | default: | |
52 | printf("%s hard\n", _("implicit")); | |
741d478e | 53 | warnx(_("unexpected value in %s: %ju"), _PATH_PROC_CTRL_ALT_DEL, val); |
82cac348 SK |
54 | return EXIT_FAILURE; |
55 | } | |
56 | return EXIT_SUCCESS; | |
57 | } | |
58 | ||
59 | static int set_cad(const char *arg) | |
028f7ed8 | 60 | { |
8137c98f | 61 | unsigned int cmd; |
82cac348 SK |
62 | |
63 | if (geteuid()) { | |
64 | warnx(_("You must be root to set the Ctrl-Alt-Del behavior")); | |
65 | return EXIT_FAILURE; | |
66 | } | |
67 | if (!strcmp("hard", arg)) | |
68 | cmd = LINUX_REBOOT_CMD_CAD_ON; | |
69 | else if (!strcmp("soft", arg)) | |
70 | cmd = LINUX_REBOOT_CMD_CAD_OFF; | |
71 | else { | |
72 | warnx(_("unknown argument: %s"), arg); | |
73 | return EXIT_FAILURE; | |
74 | } | |
e52b58e6 | 75 | if (reboot(cmd) < 0) { |
82cac348 SK |
76 | warnx("reboot"); |
77 | return EXIT_FAILURE; | |
78 | } | |
79 | return EXIT_SUCCESS; | |
80 | } | |
81 | ||
82 | int main(int argc, char **argv) | |
83 | { | |
84 | int ch, ret; | |
028f7ed8 SK |
85 | static const struct option longopts[] = { |
86 | {"version", no_argument, NULL, 'V'}, | |
87 | {"help", no_argument, NULL, 'h'}, | |
88 | {NULL, 0, NULL, 0} | |
89 | }; | |
90 | ||
7eda085c KZ |
91 | setlocale(LC_ALL, ""); |
92 | bindtextdomain(PACKAGE, LOCALEDIR); | |
93 | textdomain(PACKAGE); | |
efb8854f | 94 | atexit(close_stdout); |
6dbe3af9 | 95 | |
028f7ed8 SK |
96 | while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) |
97 | switch (ch) { | |
98 | case 'V': | |
99 | printf(UTIL_LINUX_VERSION); | |
100 | return EXIT_SUCCESS; | |
101 | case 'h': | |
86be6a32 | 102 | usage(); |
028f7ed8 | 103 | default: |
677ec86c | 104 | errtryhelp(EXIT_FAILURE); |
028f7ed8 SK |
105 | } |
106 | ||
8137c98f | 107 | if (argc < 2) |
82cac348 | 108 | ret = get_cad(); |
8137c98f | 109 | else |
82cac348 SK |
110 | ret = set_cad(argv[1]); |
111 | return ret; | |
0c3e5202 | 112 | } |