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