]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/ctrlaltdel.c
Make the ways of using output stream consistent in usage()
[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>
e52b58e6
CTV
13#include <unistd.h>
14#include <sys/reboot.h>
7eda085c 15#include "nls.h"
eb76ca98 16#include "c.h"
efb8854f 17#include "closestream.h"
82cac348 18#include "pathnames.h"
741d478e 19#include "path.h"
6dbe3af9 20
e52b58e6
CTV
21#define LINUX_REBOOT_CMD_CAD_ON 0x89ABCDEF
22#define LINUX_REBOOT_CMD_CAD_OFF 0x00000000
23
86be6a32 24static void __attribute__((__noreturn__)) usage(void)
0c3e5202 25{
86be6a32
RM
26 FILE *out = stdout;
27 fputs(USAGE_HEADER, out);
b73142b9 28 fprintf(out, _(" %s hard|soft\n"), program_invocation_short_name);
451dbcfa 29
86be6a32 30 fputs(USAGE_SEPARATOR, out);
8137c98f 31 fprintf(out, _("Set the function of the Ctrl-Alt-Del combination.\n"));
451dbcfa 32
86be6a32 33 fputs(USAGE_OPTIONS, out);
bad4c729
MY
34 fprintf(out, USAGE_HELP_OPTIONS(16));
35 fprintf(out, USAGE_MAN_TAIL("ctrlaltdel(8)"));
86be6a32 36 exit(EXIT_SUCCESS);
028f7ed8
SK
37}
38
82cac348
SK
39static int get_cad(void)
40{
9ce2fef0
KZ
41 uint64_t val;
42
43 if (ul_path_read_u64(NULL, &val, _PATH_PROC_CTRL_ALT_DEL) != 0)
44 err(EXIT_FAILURE, _("cannot read %s"), _PATH_PROC_CTRL_ALT_DEL);
82cac348 45
82cac348
SK
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"));
741d478e 55 warnx(_("unexpected value in %s: %ju"), _PATH_PROC_CTRL_ALT_DEL, val);
82cac348
SK
56 return EXIT_FAILURE;
57 }
58 return EXIT_SUCCESS;
59}
60
61static int set_cad(const char *arg)
028f7ed8 62{
8137c98f 63 unsigned int cmd;
82cac348
SK
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 }
e52b58e6 77 if (reboot(cmd) < 0) {
dcb87944 78 warn("reboot");
82cac348
SK
79 return EXIT_FAILURE;
80 }
81 return EXIT_SUCCESS;
82}
83
84int main(int argc, char **argv)
85{
86 int ch, ret;
028f7ed8
SK
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
7eda085c
KZ
93 setlocale(LC_ALL, "");
94 bindtextdomain(PACKAGE, LOCALEDIR);
95 textdomain(PACKAGE);
2c308875 96 close_stdout_atexit();
6dbe3af9 97
028f7ed8
SK
98 while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
99 switch (ch) {
100 case 'V':
2c308875 101 print_version(EXIT_SUCCESS);
028f7ed8 102 case 'h':
86be6a32 103 usage();
028f7ed8 104 default:
677ec86c 105 errtryhelp(EXIT_FAILURE);
028f7ed8
SK
106 }
107
8137c98f 108 if (argc < 2)
82cac348 109 ret = get_cad();
8137c98f 110 else
82cac348
SK
111 ret = set_cad(argv[1]);
112 return ret;
0c3e5202 113}