]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - sys-utils/ctrlaltdel.c
Merge branch 'symver' of https://github.com/nekopsykose/util-linux
[thirdparty/util-linux.git] / sys-utils / ctrlaltdel.c
index 438892585d0bf79f1589781ab299f9e2ad2a2e0c..1601154f858f7f3a5c3f67d1d4fe5f526c03789b 100644 (file)
@@ -1,40 +1,53 @@
 /*
- * ctrlaltdel.c - Set the function of the Ctrl-Alt-Del combination
- * Created 4-Jul-92 by Peter Orbaek <poe@daimi.aau.dk>
- * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
- * - added Native Language Support
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Copyright (C) 1992 Peter Orbaek <poe@daimi.aau.dk>
+ * Copyright (C) 1992-1993 Rickard E. Faith <faith@cs.unc.edu>
+ *
+ * Set the function of the Ctrl-Alt-Del combination
  */
-
 #include <getopt.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#include "linux_reboot.h"
+#include <unistd.h>
+#include <sys/reboot.h>
 #include "nls.h"
 #include "c.h"
 #include "closestream.h"
 #include "pathnames.h"
 #include "path.h"
 
-static void __attribute__ ((__noreturn__)) usage(FILE * out)
+#define LINUX_REBOOT_CMD_CAD_ON 0x89ABCDEF
+#define LINUX_REBOOT_CMD_CAD_OFF 0x00000000
+
+static void __attribute__((__noreturn__)) usage(void)
 {
-       fprintf(out, USAGE_HEADER);
+       FILE *out = stdout;
+       fputs(USAGE_HEADER, out);
        fprintf(out, _(" %s hard|soft\n"), program_invocation_short_name);
 
-       fprintf(out, USAGE_SEPARATOR);
+       fputs(USAGE_SEPARATOR, out);
        fprintf(out, _("Set the function of the Ctrl-Alt-Del combination.\n"));
 
-       fprintf(out, USAGE_OPTIONS);
-       fprintf(out, USAGE_HELP);
-       fprintf(out, USAGE_VERSION);
+       fputs(USAGE_OPTIONS, out);
+       fprintf(out, USAGE_HELP_OPTIONS(16));
        fprintf(out, USAGE_MAN_TAIL("ctrlaltdel(8)"));
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       exit(EXIT_SUCCESS);
 }
 
 static int get_cad(void)
 {
-       uint64_t val = path_read_u64(_PATH_PROC_CTRL_ALT_DEL);
+       uint64_t val;
+
+       if (ul_path_read_u64(NULL, &val, _PATH_PROC_CTRL_ALT_DEL) != 0)
+               err(EXIT_FAILURE, _("cannot read %s"), _PATH_PROC_CTRL_ALT_DEL);
 
        switch (val) {
        case 0:
@@ -55,10 +68,6 @@ static int set_cad(const char *arg)
 {
        unsigned int cmd;
 
-       if (geteuid()) {
-               warnx(_("You must be root to set the Ctrl-Alt-Del behavior"));
-               return EXIT_FAILURE;
-       }
        if (!strcmp("hard", arg))
                cmd = LINUX_REBOOT_CMD_CAD_ON;
        else if (!strcmp("soft", arg))
@@ -67,8 +76,8 @@ static int set_cad(const char *arg)
                warnx(_("unknown argument: %s"), arg);
                return EXIT_FAILURE;
        }
-       if (my_reboot(cmd) < 0) {
-               warnx("reboot");
+       if (reboot(cmd) < 0) {
+               warn("reboot");
                return EXIT_FAILURE;
        }
        return EXIT_SUCCESS;
@@ -86,17 +95,16 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
                switch (ch) {
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                case 'h':
-                       usage(stdout);
+                       usage();
                default:
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
 
        if (argc < 2)