From 82cac348896156ae081f9e99fafa28f393fa9115 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Tue, 13 Oct 2015 11:52:55 +0100 Subject: [PATCH] ctrlaltdel: display setting when ran without arguments This is more useful than printing an error. Signed-off-by: Sami Kerola --- include/pathnames.h | 3 ++ sys-utils/ctrlaltdel.8 | 5 ++- sys-utils/ctrlaltdel.c | 70 +++++++++++++++++++++++++++++++++--------- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/include/pathnames.h b/include/pathnames.h index e68ac7df2d..81efcfa810 100644 --- a/include/pathnames.h +++ b/include/pathnames.h @@ -208,5 +208,8 @@ /* logger paths */ #define _PATH_DEVLOG "/dev/log" +/* ctrlaltdel paths */ +#define _PATH_PROC_CTRL_ALT_DEL "/proc/sys/kernel/ctrl-alt-del" + #endif /* PATHNAMES_H */ diff --git a/sys-utils/ctrlaltdel.8 b/sys-utils/ctrlaltdel.8 index 4be6513729..170597f09c 100644 --- a/sys-utils/ctrlaltdel.8 +++ b/sys-utils/ctrlaltdel.8 @@ -27,8 +27,11 @@ program must support this feature. Since there are now several programs in the Linux community, please consult the documentation for the version that you are currently using. .PP +When command is ran without arguments it will display current function in +use. +.PP .B ctrlaltdel -is usually used in the +function is usually set in the .I /etc/rc.local file. .SH OPTIONS diff --git a/sys-utils/ctrlaltdel.c b/sys-utils/ctrlaltdel.c index 64de94f79e..ffdad6a8a7 100644 --- a/sys-utils/ctrlaltdel.c +++ b/sys-utils/ctrlaltdel.c @@ -14,6 +14,7 @@ #include "nls.h" #include "c.h" #include "closestream.h" +#include "pathnames.h" static void __attribute__ ((__noreturn__)) usage(FILE * out) { @@ -30,10 +31,59 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out) exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); } -int main(int argc, char **argv) +static int get_cad(void) +{ + FILE *fp; + int val; + + if (!(fp = fopen(_PATH_PROC_CTRL_ALT_DEL, "r"))) { + warn("%s", _PATH_PROC_CTRL_ALT_DEL); + return EXIT_FAILURE; + } + if (fscanf(fp, "%d", &val) != 1) + val = -1; + fclose(fp); + switch (val) { + case 0: + fputs("soft\n", stdout); + break; + case 1: + fputs("hard\n", stdout); + break; + default: + printf("%s hard\n", _("implicit")); + warnx(_("unexpected value in %s: %d"), _PATH_PROC_CTRL_ALT_DEL, val); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} + +static int set_cad(const char *arg) { - int ch; 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)) + cmd = LINUX_REBOOT_CMD_CAD_OFF; + else { + warnx(_("unknown argument: %s"), arg); + return EXIT_FAILURE; + } + if (my_reboot(cmd) < 0) { + warnx("reboot"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} + +int main(int argc, char **argv) +{ + int ch, ret; static const struct option longopts[] = { {"version", no_argument, NULL, 'V'}, {"help", no_argument, NULL, 'h'}, @@ -56,19 +106,9 @@ int main(int argc, char **argv) usage(stderr); } - if (geteuid()) - errx(EXIT_FAILURE, - _("You must be root to set the Ctrl-Alt-Del behavior")); - if (argc < 2) - errx(EXIT_FAILURE, _("not enough arguments")); - if (!strcmp("hard", argv[1])) - cmd = LINUX_REBOOT_CMD_CAD_ON; - else if (!strcmp("soft", argv[1])) - cmd = LINUX_REBOOT_CMD_CAD_OFF; + ret = get_cad(); else - errx(EXIT_FAILURE, _("unknown argument: %s"), argv[1]); - if (my_reboot(cmd) < 0) - err(EXIT_FAILURE, "reboot"); - return EXIT_SUCCESS; + ret = set_cad(argv[1]); + return ret; } -- 2.39.2