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