svn commit: trunk/busybox: include libbb loginutils

vda at busybox.net vda at busybox.net
Mon Sep 10 13:15:30 UTC 2007


Author: vda
Date: 2007-09-10 06:15:28 -0700 (Mon, 10 Sep 2007)
New Revision: 19813

Log:
setup_environment: code shrink
run_shell: mark as NORETURN
setup_environment, run_shell: add usage comments
login: add FIXME :(

function                                             old     new   delta
UNSPEC_print                                          64      66      +2
sulogin_main                                         509     506      -3
mkfs_minix_main                                     3070    3067      -3
login_main                                          1615    1612      -3
su_main                                              461     448     -13
setup_environment                                    261     206     -55
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/5 up/down: 2/-77)             Total: -75 bytes
   text    data     bss     dec     hex filename
 772578    1051   10724  784353   bf7e1 busybox_old
 772502    1051   10724  784277   bf795 busybox_unstripped



Modified:
   trunk/busybox/include/libbb.h
   trunk/busybox/libbb/setup_environment.c
   trunk/busybox/loginutils/login.c
   trunk/busybox/loginutils/su.c
   trunk/busybox/loginutils/sulogin.c


Changeset:
Modified: trunk/busybox/include/libbb.h
===================================================================
--- trunk/busybox/include/libbb.h	2007-09-10 12:23:27 UTC (rev 19812)
+++ trunk/busybox/include/libbb.h	2007-09-10 13:15:28 UTC (rev 19813)
@@ -780,6 +780,7 @@
 extern void bb_do_delay(int seconds);
 extern void change_identity(const struct passwd *pw);
 extern const char *change_identity_e2str(const struct passwd *pw);
+extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) ATTRIBUTE_NORETURN;
 extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args);
 #if ENABLE_SELINUX
 extern void renew_current_security_context(void);
@@ -790,6 +791,21 @@
 #endif
 extern void selinux_or_die(void);
 extern int restricted_shell(const char *shell);
+
+/* setup_environment:
+ * if loginshell = 1: cd(pw->pw_dir), clear environment, then set
+ *   TERM=(old value)
+ *   USER=pw->pw_name, LOGNAME=pw->pw_name
+ *   PATH=bb_default_[root_]path
+ *   HOME=pw->pw_dir
+ *   SHELL=shell
+ * else if changeenv = 1:
+ *   if not root (if pw->pw_uid != 0):
+ *     USER=pw->pw_name, LOGNAME=pw->pw_name
+ *   HOME=pw->pw_dir
+ *   SHELL=shell
+ * else does nothing
+ */
 extern void setup_environment(const char *shell, int loginshell, int changeenv, const struct passwd *pw);
 extern int correct_password(const struct passwd *pw);
 /* Returns a ptr to static storage */

Modified: trunk/busybox/libbb/setup_environment.c
===================================================================
--- trunk/busybox/libbb/setup_environment.c	2007-09-10 12:23:27 UTC (rev 19812)
+++ trunk/busybox/libbb/setup_environment.c	2007-09-10 13:15:28 UTC (rev 19813)
@@ -36,36 +36,35 @@
 		const char *term;
 
 		/* Change the current working directory to be the home directory
-		 * of the user.  It is a fatal error for this process to be unable
-		 * to change to that directory.  There is no "default" home
-		 * directory.
-		 * Some systems default to HOME=/
-		 */
+		 * of the user */
 		if (chdir(pw->pw_dir)) {
 			xchdir("/");
 			fputs("warning: cannot change to home directory\n", stderr);
 		}
 
-		/* Leave TERM unchanged.  Set HOME, SHELL, USER, LOGNAME, PATH.
+		/* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH.
 		   Unset all other environment variables.  */
 		term = getenv("TERM");
 		clearenv();
 		if (term)
 			xsetenv("TERM", term);
-		xsetenv("HOME",    pw->pw_dir);
-		xsetenv("SHELL",   shell);
-		xsetenv("USER",    pw->pw_name);
-		xsetenv("LOGNAME", pw->pw_name);
-		xsetenv("PATH",   (pw->pw_uid ? bb_default_path : bb_default_root_path));
+		xsetenv("PATH", (pw->pw_uid ? bb_default_path : bb_default_root_path));
+		goto shortcut;
+		// No, gcc (4.2.1) is not clever enougn to do it itself.
+		//xsetenv("USER",    pw->pw_name);
+		//xsetenv("LOGNAME", pw->pw_name);
+		//xsetenv("HOME",    pw->pw_dir);
+		//xsetenv("SHELL",   shell);
 	}
 	else if (changeenv) {
 		/* Set HOME, SHELL, and if not becoming a super-user,
 		   USER and LOGNAME.  */
-		xsetenv("HOME",  pw->pw_dir);
-		xsetenv("SHELL", shell);
 		if (pw->pw_uid) {
+ shortcut:
 			xsetenv("USER",    pw->pw_name);
 			xsetenv("LOGNAME", pw->pw_name);
 		}
+		xsetenv("HOME",    pw->pw_dir);
+		xsetenv("SHELL",   shell);
 	}
 }

Modified: trunk/busybox/loginutils/login.c
===================================================================
--- trunk/busybox/loginutils/login.c	2007-09-10 12:23:27 UTC (rev 19812)
+++ trunk/busybox/loginutils/login.c	2007-09-10 13:15:28 UTC (rev 19813)
@@ -432,7 +432,9 @@
 	tmp = pw->pw_shell;
 	if (!tmp || !*tmp)
 		tmp = DEFAULT_SHELL;
+	/* setup_environment params: shell, loginshell, changeenv, pw */
 	setup_environment(tmp, 1, !(opt & LOGIN_OPT_p), pw);
+	/* FIXME: login shell = 1 -> 3rd parameter is ignored! */
 
 	motd();
 
@@ -463,7 +465,8 @@
 	 * should it leave SIGINT etc enabled or disabled? */
 	signal(SIGINT, SIG_DFL);
 
-	run_shell(tmp, 1, 0, 0);	/* exec the shell finally */
+	/* Exec login shell with no additional parameters */
+	run_shell(tmp, 1, NULL, NULL);
 
-	return EXIT_FAILURE;
+	/* return EXIT_FAILURE; - not reached */
 }

Modified: trunk/busybox/loginutils/su.c
===================================================================
--- trunk/busybox/loginutils/su.c	2007-09-10 12:23:27 UTC (rev 19812)
+++ trunk/busybox/loginutils/su.c	2007-09-10 13:15:28 UTC (rev 19813)
@@ -36,7 +36,7 @@
 	/* get user if specified */
 	if (argc) {
 		opt_username = argv[0];
-//		argc--;
+		//argc--; - not used below anyway
 		argv++;
 	}
 
@@ -86,18 +86,19 @@
 		   compromise the account by allowing access with a standard
 		   shell.  */
 		bb_error_msg("using restricted shell");
-		opt_shell = 0;
+		opt_shell = NULL;
 	}
 #endif
 	if (!opt_shell)
 		opt_shell = pw->pw_shell;
 
 	change_identity(pw);
+	/* setup_environment params: shell, loginshell, changeenv, pw */
 	setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), pw);
 	USE_SELINUX(set_current_security_context(NULL);)
 
 	/* Never returns */
 	run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
 
-	return EXIT_FAILURE;
+	/* return EXIT_FAILURE; - not reached */
 }

Modified: trunk/busybox/loginutils/sulogin.c
===================================================================
--- trunk/busybox/loginutils/sulogin.c	2007-09-10 12:23:27 UTC (rev 19812)
+++ trunk/busybox/loginutils/sulogin.c	2007-09-10 13:15:28 UTC (rev 19813)
@@ -112,14 +112,15 @@
 	USE_SELINUX(renew_current_security_context());
 
 	shell = getenv("SUSHELL");
-	if (!shell) shell = getenv("sushell");
+	if (!shell)
+		shell = getenv("sushell");
 	if (!shell) {
 		shell = "/bin/sh";
 		if (pwd->pw_shell[0])
 			shell = pwd->pw_shell;
 	}
-	run_shell(shell, 1, 0, 0);
-	/* never returns */
+	/* Exec login shell with no additional parameters. Never returns. */
+	run_shell(shell, 1, NULL, NULL);
 
 auth_error:
 	bb_error_msg_and_die("no password entry for 'root'");




More information about the busybox-cvs mailing list