From 2fa51f1843a693bd380832937520c314c34f9b54 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 15 Apr 2022 15:57:57 +0200 Subject: [PATCH] Add $EUID and use it in fish_is_root_user Fixes #8866 --- doc_src/language.rst | 4 ++++ share/functions/fish_is_root_user.fish | 4 ++++ src/env.cpp | 1 + 3 files changed, 9 insertions(+) diff --git a/doc_src/language.rst b/doc_src/language.rst index 8d0029480..ccd9e3ee2 100644 --- a/doc_src/language.rst +++ b/doc_src/language.rst @@ -1416,6 +1416,10 @@ Fish also provides additional information through the values of certain environm the current username. This variable can be changed. +.. ENVVAR:: EUID + + the current effective user id, set by fish at startup. This variable can be changed. + .. envvar:: version the version of the currently running fish (also available as ``FISH_VERSION`` for backward compatibility). diff --git a/share/functions/fish_is_root_user.fish b/share/functions/fish_is_root_user.fish index 812a6a041..47e4643f7 100644 --- a/share/functions/fish_is_root_user.fish +++ b/share/functions/fish_is_root_user.fish @@ -2,6 +2,10 @@ # else if the user is root. function fish_is_root_user --description "Check if the user is root" + if test "$EUID" = 0 2>/dev/null + return 0 + end + if contains -- $USER root toor Administrator return 0 end diff --git a/src/env.cpp b/src/env.cpp index 49fc2c75a..eacf24fa7 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -294,6 +294,7 @@ void env_init(const struct config_paths_t *paths, bool do_uvars, bool default_pa // so we work around it by resetting $USER. // TODO: Figure out if that su actually checks if username == "root"(as the man page says) or // UID == 0. + vars.set_one(L"EUID", ENV_GLOBAL, to_string(static_cast(geteuid()))); uid_t uid = getuid(); setup_user(uid == 0);