]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/ctrlaltdel.c
Merge branch 'remove-geteuid-ctrlaltdel' of https://github.com/JJ-Meng/util-linux
[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 (!strcmp("hard", arg))
72 cmd = LINUX_REBOOT_CMD_CAD_ON;
73 else if (!strcmp("soft", arg))
74 cmd = LINUX_REBOOT_CMD_CAD_OFF;
75 else {
76 warnx(_("unknown argument: %s"), arg);
77 return EXIT_FAILURE;
78 }
79 if (reboot(cmd) < 0) {
80 warn("reboot");
81 return EXIT_FAILURE;
82 }
83 return EXIT_SUCCESS;
84 }
85
86 int main(int argc, char **argv)
87 {
88 int ch, ret;
89 static const struct option longopts[] = {
90 {"version", no_argument, NULL, 'V'},
91 {"help", no_argument, NULL, 'h'},
92 {NULL, 0, NULL, 0}
93 };
94
95 setlocale(LC_ALL, "");
96 bindtextdomain(PACKAGE, LOCALEDIR);
97 textdomain(PACKAGE);
98 close_stdout_atexit();
99
100 while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
101 switch (ch) {
102 case 'V':
103 print_version(EXIT_SUCCESS);
104 case 'h':
105 usage();
106 default:
107 errtryhelp(EXIT_FAILURE);
108 }
109
110 if (argc < 2)
111 ret = get_cad();
112 else
113 ret = set_cad(argv[1]);
114 return ret;
115 }