From 7de194280368a478f348e0069b9f929fc3165b46 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 23 Dec 2016 07:37:18 +0100 Subject: [PATCH] Only the tty1 login session powers off. --- sh/sh.c | 3 +++ utils/command-not-found.c | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/sh/sh.c b/sh/sh.c index 4595ee0f..3ff1cc62 100644 --- a/sh/sh.c +++ b/sh/sh.c @@ -1551,6 +1551,9 @@ bool does_line_editing_need_another_line(void* ctx, const char* line) bool is_outermost_shell(void) { + char* name = ttyname(0); + if ( !name || strcmp(name, "/dev/tty1") != 0 ) + return false; const char* shlvl_str = getenv("SHLVL"); if ( !shlvl_str ) return true; diff --git a/utils/command-not-found.c b/utils/command-not-found.c index 194f43b2..b1803b69 100644 --- a/utils/command-not-found.c +++ b/utils/command-not-found.c @@ -19,11 +19,24 @@ * of a typo. */ +#include #include #include #include #include +const char* tty_name(void) +{ + int tty_fd = open("/dev/tty", O_RDONLY); + const char* result = NULL; + if ( 0 <= tty_fd ) + { + result = ttyname(tty_fd); + close(tty_fd); + } + return result ? result : "/dev/tty"; +} + void suggest_editor(const char* filename) { fprintf(stderr, "No command '%s' found, did you mean:\n", filename); @@ -63,7 +76,11 @@ void suggest_logout(const char* filename) void suggest_poweroff(const char* filename) { fprintf(stderr, "No command '%s' found, did you mean:\n", filename); - if ( getenv("LOGIN_PID") ) + if ( strcmp(tty_name(), "/dev/tty1") != 0 ) + { + fprintf(stderr, " Powering off on /dev/tty1.\n"); + } + else if ( getenv("LOGIN_PID") ) { fprintf(stderr, " Exiting your shell normally to logout.\n"); fprintf(stderr, " Login as user 'poweroff' to power off computer.\n"); @@ -77,7 +94,11 @@ void suggest_poweroff(const char* filename) void suggest_reboot(const char* filename) { fprintf(stderr, "No command '%s' found, did you mean:\n", filename); - if ( getenv("LOGIN_PID") ) + if ( strcmp(tty_name(), "/dev/tty1") != 0 ) + { + fprintf(stderr, " Rebooting on /dev/tty1.\n"); + } + else if ( getenv("LOGIN_PID") ) { fprintf(stderr, " Exiting your shell normally to logout.\n"); fprintf(stderr, " Login as user 'reboot' to reboot computer.\n");