svn commit: trunk/busybox: applets libbb shell

vda at busybox.net vda at busybox.net
Sat Apr 14 13:22:10 UTC 2007


Author: vda
Date: 2007-04-14 06:22:09 -0700 (Sat, 14 Apr 2007)
New Revision: 18439

Log:
lineedit: nuke two unused variables and code which sets them
applets: do not even try to read config if run by real root
msh: use named constants (O_RDONLY etc) in open() instead of magic numbers,
     other minor code size reduction.


Modified:
   trunk/busybox/applets/applets.c
   trunk/busybox/libbb/lineedit.c
   trunk/busybox/shell/msh.c


Changeset:
Modified: trunk/busybox/applets/applets.c
===================================================================
--- trunk/busybox/applets/applets.c	2007-04-14 11:16:29 UTC (rev 18438)
+++ trunk/busybox/applets/applets.c	2007-04-14 13:22:09 UTC (rev 18439)
@@ -48,15 +48,16 @@
 /* The -1 arises because of the {0,NULL,0,-1} entry. */
 const unsigned short NUM_APPLETS = sizeof(applets) / sizeof(applets[0]) - 1;
 
-
 const struct bb_applet *current_applet;
 const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
 #if !BB_MMU
 bool re_execed;
 #endif
 
+#if ENABLE_FEATURE_SUID
+static uid_t ruid;  /* real uid */
+#endif
 
-
 #if ENABLE_FEATURE_SUID_CONFIG
 
 /* applets[] is const, so we have to define this "override" structure */
@@ -143,6 +144,10 @@
 
 	assert(!suid_config); /* Should be set to NULL by bss init. */
 
+	ruid = getuid();
+	if (ruid == 0) /* run by root - don't need to even read config file */
+		return;
+
 	if ((stat(config_file, &st) != 0)       /* No config file? */
 	 || !S_ISREG(st.st_mode)                /* Not a regular file? */
 	 || (st.st_uid != 0)                    /* Not owned by root? */
@@ -324,16 +329,22 @@
 	}
 }
 #else
-#define parse_config_file() ((void)0)
+static inline void parse_config_file(void)
+{
+	ruid = getuid();
+}
 #endif /* FEATURE_SUID_CONFIG */
 
 
 #if ENABLE_FEATURE_SUID
 static void check_suid(const struct bb_applet *applet)
 {
-	uid_t ruid = getuid();               /* real [ug]id */
-	uid_t rgid = getgid();
+	uid_t rgid;  /* real gid */
 
+	if (ruid == 0) /* set by parse_config_file() */
+		return; /* run by root - no need to check more */
+	rgid = getgid();
+
 #if ENABLE_FEATURE_SUID_CONFIG
 	if (suid_cfg_readable) {
 		struct BB_suid_config *sct;
@@ -387,7 +398,7 @@
 		if (geteuid())
 			bb_error_msg_and_die("applet requires root privileges!");
 	} else if (applet->need_suid == _BB_SUID_NEVER) {
-		xsetgid(rgid);                          /* drop all privileges */
+		xsetgid(rgid);  /* drop all privileges */
 		xsetuid(ruid);
 	}
 }
@@ -636,8 +647,7 @@
 	if (s)
 		applet_name = s + 1;
 
-	if (ENABLE_FEATURE_SUID_CONFIG)
-		parse_config_file();
+	parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
 
 	/* Set locale for everybody except 'init' */
 	if (ENABLE_LOCALE_SUPPORT && getpid() != 1)

Modified: trunk/busybox/libbb/lineedit.c
===================================================================
--- trunk/busybox/libbb/lineedit.c	2007-04-14 11:16:29 UTC (rev 18438)
+++ trunk/busybox/libbb/lineedit.c	2007-04-14 13:22:09 UTC (rev 18439)
@@ -85,11 +85,6 @@
 static char *home_pwd_buf = (char*)"";
 #endif
 
-#if ENABLE_FEATURE_TAB_COMPLETION
-static int my_uid;
-static int my_gid;
-#endif
-
 /* Put 'command_ps[cursor]', cursor++.
  * Advance cursor on screen. If we reached right margin, scroll text up
  * and remove terminal margin effect by printing 'next_char' */
@@ -1312,10 +1307,6 @@
 		}
 	}
 #endif
-#if ENABLE_FEATURE_TAB_COMPLETION
-	my_uid = getuid();
-	my_gid = getgid();
-#endif
 	/* Print out the command prompt */
 	parse_prompt(prompt);
 

Modified: trunk/busybox/shell/msh.c
===================================================================
--- trunk/busybox/shell/msh.c	2007-04-14 11:16:29 UTC (rev 18438)
+++ trunk/busybox/shell/msh.c	2007-04-14 13:22:09 UTC (rev 18439)
@@ -152,16 +152,15 @@
 /*
  * values returned by wait
  */
-#define	WAITSIG(s)  ((s)&0177)
-#define	WAITVAL(s)  (((s)>>8)&0377)
-#define	WAITCORE(s) (((s)&0200)!=0)
+#define	WAITSIG(s)  ((s) & 0177)
+#define	WAITVAL(s)  (((s) >> 8) & 0377)
+#define	WAITCORE(s) (((s) & 0200) != 0)
 
 /*
  * library and system definitions
  */
 typedef void xint;				/* base type of jmp_buf, for not broken compilers */
 
-
 /*
  * shell components
  */
@@ -170,7 +169,6 @@
 #define	NOWORDS	((char **)NULL)
 #define	NOPIPE	((int *)NULL)
 
-
 /*
  * redirection
  */
@@ -250,21 +248,20 @@
 /*
  * actions determining the environment of a process
  */
-#define	BIT(i)	(1<<(i))
-#define	FEXEC	BIT(0)			/* execute without forking */
+#define FEXEC    1      /* execute without forking */
 
-#define AREASIZE	(90000)
+#define AREASIZE (90000)
 
 /*
  * flags to control evaluation of words
  */
-#define	DOSUB	 1				/* interpret $, `, and quotes */
-#define	DOBLANK	 2				/* perform blank interpretation */
-#define	DOGLOB	 4				/* interpret [?* */
-#define	DOKEY	 8				/* move words with `=' to 2nd arg. list */
-#define	DOTRIM	 16				/* trim resulting string */
+#define DOSUB    1      /* interpret $, `, and quotes */
+#define DOBLANK  2      /* perform blank interpretation */
+#define DOGLOB   4      /* interpret [?* */
+#define DOKEY    8      /* move words with `=' to 2nd arg. list */
+#define DOTRIM   16     /* trim resulting string */
 
-#define	DOALL	(DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
+#define DOALL    (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
 
 
 /* PROTOTYPES */
@@ -333,13 +330,13 @@
 
 /* -------- area stuff -------- */
 
-#define	REGSIZE	  sizeof(struct region)
-#define GROWBY	  (256)
-/* #define	SHRINKBY   (64) */
-#undef	SHRINKBY
-#define FREE	  (32767)
-#define BUSY	  (0)
-#define	ALIGN	  (sizeof(int)-1)
+#define REGSIZE   sizeof(struct region)
+#define GROWBY    (256)
+/* #define SHRINKBY (64) */
+#undef  SHRINKBY
+#define FREE      (32767)
+#define BUSY      (0)
+#define ALIGN     (sizeof(int)-1)
 
 
 struct region {
@@ -1313,7 +1310,7 @@
 	f = 0;
 	if (NOT_LONE_DASH(s)) {
 		DBGPRINTF(("NEWFILE: s is %s\n", s));
-		f = open(s, 0);
+		f = open(s, O_RDONLY);
 		if (f < 0) {
 			prs(s);
 			err(": cannot open");
@@ -2554,7 +2551,7 @@
 				interactive = 0;
 				if (pin == NULL) {
 					close(0);
-					open(bb_dev_null, 0);
+					open(bb_dev_null, O_RDONLY);
 				}
 				_exit(execute(t->left, pin, pout, FEXEC));
 			}
@@ -2734,7 +2731,8 @@
 	resetsig = 0;
 	rv = -1;					/* system-detected error */
 	if (t->type == TCOM) {
-		while (*wp++ != NULL);
+		while (*wp++ != NULL)
+			continue;
 		cp = *wp;
 
 		/* strip all initial assignments */
@@ -2747,7 +2745,7 @@
 
 		if (cp == NULL && t->ioact == NULL) {
 			while ((cp = *owp++) != NULL && assign(cp, COPYV))
-				/**/;
+				continue;
 			DBGPRINTF(("FORKEXEC: returning setstatus()\n"));
 			return setstatus(0);
 		}
@@ -2932,7 +2930,7 @@
 	}
 	switch (iop->io_flag) {
 	case IOREAD:
-		u = open(cp, 0);
+		u = open(cp, O_RDONLY);
 		break;
 
 	case IOHERE:
@@ -2942,7 +2940,7 @@
 		break;
 
 	case IOWRITE | IOCAT:
-		u = open(cp, 1);
+		u = open(cp, O_WRONLY);
 		if (u >= 0) {
 			lseek(u, (long) 0, SEEK_END);
 			break;
@@ -3346,7 +3344,7 @@
 		for (i = 0; (*tp++ = cp[i++]) != '\0';);
 
 		/* Original code */
-		i = open(e.linep, 0);
+		i = open(e.linep, O_RDONLY);
 		if (i >= 0) {
 			exstat = 0;
 			maltmp = remap(i);
@@ -5098,7 +5096,7 @@
 
 	DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll));
 
-	hf = open(hname, 0);
+	hf = open(hname, O_RDONLY);
 	if (hf < 0)
 		return -1;
 
@@ -5122,7 +5120,7 @@
 		} else
 			unlink(tname);
 		close(tf);
-		tf = open(tname, 0);
+		tf = open(tname, O_RDONLY);
 		unlink(tname);
 		return tf;
 	}
@@ -5214,10 +5212,11 @@
 
 	path = lookup("PATH");
 	if (path->value == null) {
+		/* Can be merged with same string elsewhere in bbox */
 		if (geteuid() == 0)
-			setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
+			setval(path, "/sbin:/usr/sbin:/bin:/usr/bin");
 		else
-			setval(path, "/bin:/usr/bin");
+			setval(path, "/sbin:/usr/sbin:/bin:/usr/bin" + sizeof("/sbin:/usr/sbin"));
 	}
 	export(path);
 
@@ -5329,10 +5328,10 @@
 	signal(SIGQUIT, qflag);
 	if (name && name[0] == '-') {
 		interactive++;
-		f = open(".profile", 0);
+		f = open(".profile", O_RDONLY);
 		if (f >= 0)
 			next(remap(f));
-		f = open("/etc/profile", 0);
+		f = open("/etc/profile", O_RDONLY);
 		if (f >= 0)
 			next(remap(f));
 	}




More information about the busybox-cvs mailing list