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