libbb: invert the meaning of SETUP_ENV_NO_CHDIR -> SETUP_ENV_CHDIR

Double negatives are hard to grok.

function                                             old     new   delta
login_main                                           986     988      +2
su_main                                              474     470      -4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 2/-4)               Total: -2 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2022-01-13 12:50:48 +01:00
parent b3eec1651f
commit 931c55f9e2
8 changed files with 22 additions and 17 deletions

View File

@ -1726,7 +1726,7 @@ extern void selinux_or_die(void) FAST_FUNC;
/* setup_environment:
* if !SETUP_ENV_NO_CHDIR:
* if SETUP_ENV_CHDIR:
* if cd(pw->pw_dir): ok: else if SETUP_ENV_TO_TMP: cd(/tmp) else: cd(/) or die
* if SETUP_ENV_CLEARENV: cd(pw->pw_dir), clear environment, then set
* TERM=(old value)
@ -1734,7 +1734,7 @@ extern void selinux_or_die(void) FAST_FUNC;
* PATH=bb_default_[root_]path
* HOME=pw->pw_dir
* SHELL=shell
* else if SETUP_ENV_CHANGEENV:
* else if SETUP_ENV_CHANGEENV | SETUP_ENV_CHANGEENV_LOGNAME:
* if not root (if pw->pw_uid != 0) or if SETUP_ENV_CHANGEENV_LOGNAME:
* USER=pw->pw_name, LOGNAME=pw->pw_name
* HOME=pw->pw_dir
@ -1748,7 +1748,7 @@ extern void selinux_or_die(void) FAST_FUNC;
#define SETUP_ENV_CHANGEENV_LOGNAME (1 << 1)
#define SETUP_ENV_CLEARENV (1 << 2)
#define SETUP_ENV_TO_TMP (1 << 3)
#define SETUP_ENV_NO_CHDIR (1 << 4)
#define SETUP_ENV_CHDIR (1 << 4)
void setup_environment(const char *shell, int flags, const struct passwd *pw) FAST_FUNC;
void nuke_str(char *str) FAST_FUNC;
#if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM

View File

@ -36,7 +36,7 @@ void FAST_FUNC setup_environment(const char *shell, int flags, const struct pass
/* Change the current working directory to be the home directory
* of the user */
if (!(flags & SETUP_ENV_NO_CHDIR)) {
if (flags & SETUP_ENV_CHDIR) {
if (chdir(pw->pw_dir) != 0) {
bb_error_msg("can't change directory to '%s'", pw->pw_dir);
xchdir((flags & SETUP_ENV_TO_TMP) ? "/tmp" : "/");
@ -59,7 +59,8 @@ void FAST_FUNC setup_environment(const char *shell, int flags, const struct pass
//xsetenv("LOGNAME", pw->pw_name);
//xsetenv("HOME", pw->pw_dir);
//xsetenv("SHELL", shell);
} else if (flags & SETUP_ENV_CHANGEENV) {
} else
if (flags & (SETUP_ENV_CHANGEENV|SETUP_ENV_CHANGEENV_LOGNAME)) {
/* Set HOME, SHELL, and if not becoming a super-user
* or if SETUP_ENV_CHANGEENV_LOGNAME, USER and LOGNAME. */
if ((flags & SETUP_ENV_CHANGEENV_LOGNAME) || pw->pw_uid != 0) {

View File

@ -564,7 +564,9 @@ int login_main(int argc UNUSED_PARAM, char **argv)
change_identity(pw);
setup_environment(pw->pw_shell,
(!(opt & LOGIN_OPT_p) * SETUP_ENV_CLEARENV) + SETUP_ENV_CHANGEENV,
(!(opt & LOGIN_OPT_p) * SETUP_ENV_CLEARENV)
+ SETUP_ENV_CHANGEENV
+ SETUP_ENV_CHDIR,
pw);
#if ENABLE_PAM

View File

@ -176,9 +176,8 @@ int su_main(int argc UNUSED_PARAM, char **argv)
change_identity(pw);
setup_environment(opt_shell,
((flags & SU_OPT_l) / SU_OPT_l * SETUP_ENV_CLEARENV)
+ (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV)
+ (!(flags & SU_OPT_l) * SETUP_ENV_NO_CHDIR),
((flags & SU_OPT_l) ? (SETUP_ENV_CLEARENV + SETUP_ENV_CHDIR) : 0)
+ (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV),
pw);
IF_SELINUX(set_current_security_context(NULL);)

View File

@ -94,10 +94,13 @@ int sulogin_main(int argc UNUSED_PARAM, char **argv)
shell = pwd->pw_shell;
/* util-linux 2.36.1 compat: cd to root's HOME, set a few envvars */
setup_environment(shell, SETUP_ENV_CHANGEENV | SETUP_ENV_CHANGEENV_LOGNAME, pwd);
setup_environment(shell, 0
+ SETUP_ENV_CHANGEENV_LOGNAME
+ SETUP_ENV_CHDIR
, pwd);
// no SETUP_ENV_CLEARENV
// SETUP_ENV_CHANGEENV[+LOGNAME] - set HOME, SHELL, USER,and LOGNAME
// no SETUP_ENV_NO_CHDIR - IOW: cd to $HOME
// SETUP_ENV_CHANGEENV_LOGNAME - set HOME, SHELL, USER,and LOGNAME
// SETUP_ENV_CHDIR - cd to $HOME
/* util-linux 2.36.1 compat: steal ctty if we don't have it yet
* (yes, util-linux uses force=1) */

View File

@ -55,7 +55,7 @@ static void edit_file(const struct passwd *pas, const char *file)
/* initgroups, setgid, setuid */
change_identity(pas);
setup_environment(pas->pw_shell,
SETUP_ENV_CHANGEENV | SETUP_ENV_TO_TMP,
SETUP_ENV_CHANGEENV | SETUP_ENV_TO_TMP | SETUP_ENV_CHDIR,
pas);
ptr = getenv("VISUAL");
if (!ptr) {

View File

@ -10791,7 +10791,7 @@ preadfd(void)
write(STDOUT_FILENO, "^C", 2);
raise(SIGINT);
/* raise(SIGINT) did not work! (e.g. if SIGINT
* is SIG_INGed on startup, it stays SIG_IGNed)
* is SIG_IGNed on startup, it stays SIG_IGNed)
*/
if (trap[SIGINT]) {
buf[0] = '\n';

View File

@ -10361,7 +10361,7 @@ int hush_main(int argc, char **argv)
//it ignores TERM:
// bash -i -c 'kill $$; echo ALIVE'
// ALIVE
//it resets SIG_INGed HUP to SIG_DFL:
//it resets SIG_IGNed HUP to SIG_DFL:
// trap '' hup; bash -i -c 'kill -hup $$; echo ALIVE'
// Hangup [the message is not printed by bash, it's the shell which started it]
//is talkative about jobs and exiting: