From 5f9468e99649c0daa5cacfe180fbd0e061df6fc3 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sat, 14 Apr 2007 13:22:09 +0000 Subject: 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. --- applets/applets.c | 26 ++++++++++++++------- libbb/lineedit.c | 9 -------- shell/msh.c | 67 +++++++++++++++++++++++++++---------------------------- 3 files changed, 51 insertions(+), 51 deletions(-) diff --git a/applets/applets.c b/applets/applets.c index fb37fbea5..13b406ae5 100644 --- a/applets/applets.c +++ b/applets/applets.c @@ -48,14 +48,15 @@ static const char usage_messages[] = "" /* 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 @@ -143,6 +144,10 @@ static void parse_config_file(void) 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,15 +329,21 @@ static void parse_config_file(void) } } #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) { @@ -387,7 +398,7 @@ static void check_suid(const struct bb_applet *applet) 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 @@ int main(int argc, char **argv) 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) diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 59baf23a8..4fc5e7a61 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -85,11 +85,6 @@ static char *user_buf = (char*)""; 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' */ @@ -1311,10 +1306,6 @@ int read_line_input(const char* prompt, char* command, int maxsize, line_input_t home_pwd_buf = xstrdup(entry->pw_dir); } } -#endif -#if ENABLE_FEATURE_TAB_COMPLETION - my_uid = getuid(); - my_gid = getgid(); #endif /* Print out the command prompt */ parse_prompt(prompt); diff --git a/shell/msh.c b/shell/msh.c index 861abe234..ae18061d6 100644 --- a/shell/msh.c +++ b/shell/msh.c @@ -152,16 +152,15 @@ int mshdbg_rc = 0; /* * 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 @@ typedef void xint; /* base type of jmp_buf, for not broken compilers */ #define NOWORDS ((char **)NULL) #define NOPIPE ((int *)NULL) - /* * redirection */ @@ -250,21 +248,20 @@ static const char *const T_CMD_NAMES[] = { /* * 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 @@ static void runtrap(int i); /* -------- 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 @@ static int newfile(char *s) 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 @@ static int execute(struct op *t, int *pin, int *pout, int act) 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 @@ static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp) 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 @@ static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp) 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 @@ static int iosetup(struct ioword *iop, int pipein, int pipeout) } switch (iop->io_flag) { case IOREAD: - u = open(cp, 0); + u = open(cp, O_RDONLY); break; case IOHERE: @@ -2942,7 +2940,7 @@ static int iosetup(struct ioword *iop, int pipein, int pipeout) 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 @@ static int dodot(struct op *t) 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 @@ static int herein(char *hname, int xdoll) 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 @@ static int herein(char *hname, int xdoll) } else unlink(tname); close(tf); - tf = open(tname, 0); + tf = open(tname, O_RDONLY); unlink(tname); return tf; } @@ -5214,10 +5212,11 @@ int msh_main(int argc, char **argv) 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 @@ int msh_main(int argc, char **argv) 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)); } -- cgit v1.2.3