]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/ctrlaltdel.c
sys-utils: cleanup license lines, add SPDX
[thirdparty/util-linux.git] / sys-utils / ctrlaltdel.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * Copyright (C) 1992 Peter Orbaek <poe@daimi.aau.dk>
10 * Copyright (C) 1992-1993 Rickard E. Faith <faith@cs.unc.edu>
11 *
12 * Set the function of the Ctrl-Alt-Del combination
13 */
14 #include <getopt.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <unistd.h>
20 #include <sys/reboot.h>
21 #include "nls.h"
22 #include "c.h"
23 #include "closestream.h"
24 #include "pathnames.h"
25 #include "path.h"
26
27 #define LINUX_REBOOT_CMD_CAD_ON 0x89ABCDEF
28 #define LINUX_REBOOT_CMD_CAD_OFF 0x00000000
29
30 static void __attribute__((__noreturn__)) usage(void)
31 {
32 FILE *out = stdout;
33 fputs(USAGE_HEADER, out);
34 fprintf(out, _(" %s hard|soft\n"), program_invocation_short_name);
35
36 fputs(USAGE_SEPARATOR, out);
37 fprintf(out, _("Set the function of the Ctrl-Alt-Del combination.\n"));
38
39 fputs(USAGE_OPTIONS, out);
40 fprintf(out, USAGE_HELP_OPTIONS(16));
41 fprintf(out, USAGE_MAN_TAIL("ctrlaltdel(8)"));
42 exit(EXIT_SUCCESS);
43 }
44
45 static int get_cad(void)
46 {
47 uint64_t val;
48
49 if (ul_path_read_u64(NULL, &val, _PATH_PROC_CTRL_ALT_DEL) != 0)
50 err(EXIT_FAILURE, _("cannot read %s"), _PATH_PROC_CTRL_ALT_DEL);
51
52 switch (val) {
53 case 0:
54 fputs("soft\n", stdout);
55 break;
56 case 1:
57 fputs("hard\n", stdout);
58 break;
59 default:
60 printf("%s hard\n", _("implicit"));
61 warnx(_("unexpected value in %s: %ju"), _PATH_PROC_CTRL_ALT_DEL, val);
62 return EXIT_FAILURE;
63 }
64 return EXIT_SUCCESS;
65 }
66
67 static int set_cad(const char *arg)
68 {
69 unsigned int cmd;
70
71 if (geteuid()) {
72 warnx(_("You must be root to set the Ctrl-Alt-Del behavior"));
73 return EXIT_FAILURE;
74 }
75 if (!strcmp("hard", arg))
76 cmd = LINUX_REBOOT_CMD_CAD_ON;
77 else if (!strcmp("soft", arg))
78 cmd = LINUX_REBOOT_CMD_CAD_OFF;
79 else {
80 warnx(_("unknown argument: %s"), arg);
81 return EXIT_FAILURE;
82 }
83 if (reboot(cmd) < 0) {
84 warn("reboot");
85 return EXIT_FAILURE;
86 }
87 return EXIT_SUCCESS;
88 }
89
90 int main(int argc, char **argv)
91 {
92 int ch, ret;
93 static const struct option longopts[] = {
94 {"version", no_argument, NULL, 'V'},
95 {"help", no_argument, NULL, 'h'},
96 {NULL, 0, NULL, 0}
97 };
98
99 setlocale(LC_ALL, "");
100 bindtextdomain(PACKAGE, LOCALEDIR);
101 textdomain(PACKAGE);
102 close_stdout_atexit();
103
104 while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
105 switch (ch) {
106 case 'V':
107 print_version(EXIT_SUCCESS);
108 case 'h':
109 usage();
110 default:
111 errtryhelp(EXIT_FAILURE);
112 }
113
114 if (argc < 2)
115 ret = get_cad();
116 else
117 ret = set_cad(argv[1]);
118 return ret;
119 }