]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/ctrlaltdel.c
sys-utils: cleanup license lines, add SPDX
[thirdparty/util-linux.git] / sys-utils / ctrlaltdel.c
CommitLineData
6dbe3af9 1/*
9abd5e4b
KZ
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
6dbe3af9 13 */
028f7ed8 14#include <getopt.h>
6dbe3af9 15#include <stdio.h>
66ee8158 16#include <stdlib.h>
6dbe3af9 17#include <string.h>
0c3e5202 18#include <errno.h>
e52b58e6
CTV
19#include <unistd.h>
20#include <sys/reboot.h>
7eda085c 21#include "nls.h"
eb76ca98 22#include "c.h"
efb8854f 23#include "closestream.h"
82cac348 24#include "pathnames.h"
741d478e 25#include "path.h"
6dbe3af9 26
e52b58e6
CTV
27#define LINUX_REBOOT_CMD_CAD_ON 0x89ABCDEF
28#define LINUX_REBOOT_CMD_CAD_OFF 0x00000000
29
86be6a32 30static void __attribute__((__noreturn__)) usage(void)
0c3e5202 31{
86be6a32
RM
32 FILE *out = stdout;
33 fputs(USAGE_HEADER, out);
b73142b9 34 fprintf(out, _(" %s hard|soft\n"), program_invocation_short_name);
451dbcfa 35
86be6a32 36 fputs(USAGE_SEPARATOR, out);
8137c98f 37 fprintf(out, _("Set the function of the Ctrl-Alt-Del combination.\n"));
451dbcfa 38
86be6a32 39 fputs(USAGE_OPTIONS, out);
bad4c729
MY
40 fprintf(out, USAGE_HELP_OPTIONS(16));
41 fprintf(out, USAGE_MAN_TAIL("ctrlaltdel(8)"));
86be6a32 42 exit(EXIT_SUCCESS);
028f7ed8
SK
43}
44
82cac348
SK
45static int get_cad(void)
46{
9ce2fef0
KZ
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);
82cac348 51
82cac348
SK
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"));
741d478e 61 warnx(_("unexpected value in %s: %ju"), _PATH_PROC_CTRL_ALT_DEL, val);
82cac348
SK
62 return EXIT_FAILURE;
63 }
64 return EXIT_SUCCESS;
65}
66
67static int set_cad(const char *arg)
028f7ed8 68{
8137c98f 69 unsigned int cmd;
82cac348
SK
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 }
e52b58e6 83 if (reboot(cmd) < 0) {
dcb87944 84 warn("reboot");
82cac348
SK
85 return EXIT_FAILURE;
86 }
87 return EXIT_SUCCESS;
88}
89
90int main(int argc, char **argv)
91{
92 int ch, ret;
028f7ed8
SK
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
7eda085c
KZ
99 setlocale(LC_ALL, "");
100 bindtextdomain(PACKAGE, LOCALEDIR);
101 textdomain(PACKAGE);
2c308875 102 close_stdout_atexit();
6dbe3af9 103
028f7ed8
SK
104 while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
105 switch (ch) {
106 case 'V':
2c308875 107 print_version(EXIT_SUCCESS);
028f7ed8 108 case 'h':
86be6a32 109 usage();
028f7ed8 110 default:
677ec86c 111 errtryhelp(EXIT_FAILURE);
028f7ed8
SK
112 }
113
8137c98f 114 if (argc < 2)
82cac348 115 ret = get_cad();
8137c98f 116 else
82cac348
SK
117 ret = set_cad(argv[1]);
118 return ret;
0c3e5202 119}