applets.c, xfunc.c: style cleanup

This commit is contained in:
Denis Vlasenko 2007-03-24 12:08:36 +00:00
parent ce074df814
commit 1b6fa4c57c
2 changed files with 86 additions and 85 deletions

View File

@ -48,16 +48,14 @@ static const char usage_messages[] =
static struct BB_applet *applet_using; static struct BB_applet *applet_using;
/* The -1 arises because of the {0,NULL,0,-1} entry above. */ /* The -1 arises because of the {0,NULL,0,-1} entry. */
const unsigned short NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1); const unsigned short NUM_APPLETS = (sizeof(applets) / sizeof(struct BB_applet) - 1);
#ifdef CONFIG_FEATURE_SUID_CONFIG #if ENABLE_FEATURE_SUID_CONFIG
#include <ctype.h> #include <ctype.h>
#define CONFIG_FILE "/etc/busybox.conf"
/* applets [] is const, so we have to define this "override" structure */ /* applets [] is const, so we have to define this "override" structure */
static struct BB_suid_config static struct BB_suid_config
{ {
@ -114,7 +112,7 @@ static char *get_trimmed_slice(char *s, char *e)
#define parse_error(x) do { errmsg = x; goto pe_label; } while(0) #define parse_error(x) do { errmsg = x; goto pe_label; } while(0)
/* Don't depend on the tools to combine strings. */ /* Don't depend on the tools to combine strings. */
static const char config_file[] = CONFIG_FILE; static const char config_file[] = "/etc/busybox.conf";
/* There are 4 chars + 1 nul for each of user/group/other. */ /* There are 4 chars + 1 nul for each of user/group/other. */
static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-"; static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-";
@ -185,7 +183,8 @@ static void parse_config_file(void)
/* Trim leading and trailing whitespace, ignoring comments, and /* Trim leading and trailing whitespace, ignoring comments, and
* check if the resulting string is empty. */ * check if the resulting string is empty. */
if (!*(s = get_trimmed_slice(s, strchrnul(s, '#')))) { s = get_trimmed_slice(s, strchrnul(s, '#'));
if (!*s) {
continue; continue;
} }
@ -195,7 +194,8 @@ static void parse_config_file(void)
/* Unlike the old code, we ignore leading and trailing /* Unlike the old code, we ignore leading and trailing
* whitespace for the section name. We also require that * whitespace for the section name. We also require that
* there are no stray characters after the closing bracket. */ * there are no stray characters after the closing bracket. */
if (!(e = strchr(s, ']')) /* Missing right bracket? */ e = strchr(s, ']');
if (!e /* Missing right bracket? */
|| e[1] /* Trailing characters? */ || e[1] /* Trailing characters? */
|| !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */ || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
) { ) {
@ -224,7 +224,8 @@ static void parse_config_file(void)
* where both key and value could contain inner whitespace. */ * where both key and value could contain inner whitespace. */
/* First get the key (an applet name in our case). */ /* First get the key (an applet name in our case). */
if (!!(e = strchr(s, '='))) { e = strchr(s, '=');
if (e) {
s = get_trimmed_slice(s, e); s = get_trimmed_slice(s, e);
} }
if (!e || !*s) { /* Missing '=' or empty key. */ if (!e || !*s) { /* Missing '=' or empty key. */
@ -235,7 +236,8 @@ static void parse_config_file(void)
* applet is currently built in and ignore it otherwise. * applet is currently built in and ignore it otherwise.
* Note: This can hide config file bugs which only pop * Note: This can hide config file bugs which only pop
* up when the busybox configuration is changed. */ * up when the busybox configuration is changed. */
if ((applet = find_applet_by_name(s))) { applet = find_applet_by_name(s);
if (applet) {
/* Note: We currently don't check for duplicates! /* Note: We currently don't check for duplicates!
* The last config line for each applet will be the * The last config line for each applet will be the
* one used since we insert at the head of the list. * one used since we insert at the head of the list.
@ -250,9 +252,10 @@ static void parse_config_file(void)
e = skip_whitespace(e+1); e = skip_whitespace(e+1);
for (i=0 ; i < 3 ; i++) { for (i = 0; i < 3; i++) {
const char *q; const char *q;
if (!*(q = strchrnul(mode_chars + 5*i, *e++))) { q = strchrnul(mode_chars + 5*i, *e++);
if (!*q) {
parse_error("mode"); parse_error("mode");
} }
/* Adjust by -i to account for nul. */ /* Adjust by -i to account for nul. */
@ -268,15 +271,12 @@ static void parse_config_file(void)
if ((s == e) || !(e = strchr(s, '.'))) { if ((s == e) || !(e = strchr(s, '.'))) {
parse_error("<uid>.<gid>"); parse_error("<uid>.<gid>");
} }
*e++ = 0; *e++ = '\0';
/* We can't use get_ug_id here since it would exit() /* We can't use get_ug_id here since it would exit()
* if a uid or gid was not found. Oh well... */ * if a uid or gid was not found. Oh well... */
{ sct->m_uid = bb_strtoul(s, NULL, 10);
char *e2; if (errno) {
sct->m_uid = strtoul(s, &e2, 10);
if (*e2 || (s == e2)) {
struct passwd *pwd = getpwnam(s); struct passwd *pwd = getpwnam(s);
if (!pwd) { if (!pwd) {
parse_error("user"); parse_error("user");
@ -284,16 +284,16 @@ static void parse_config_file(void)
sct->m_uid = pwd->pw_uid; sct->m_uid = pwd->pw_uid;
} }
sct->m_gid = strtoul(e, &e2, 10); sct->m_gid = bb_strtoul(e, NULL, 10);
if (*e2 || (e == e2)) { if (errno) {
struct group *grp; struct group *grp;
if (!(grp = getgrnam(e))) { grp = getgrnam(e);
if (!grp) {
parse_error("group"); parse_error("group");
} }
sct->m_gid = grp->gr_gid; sct->m_gid = grp->gr_gid;
} }
} }
}
continue; continue;
} }
@ -327,23 +327,27 @@ static void parse_config_file(void)
#define parse_config_file() ((void)0) #define parse_config_file() ((void)0)
#endif /* CONFIG_FEATURE_SUID_CONFIG */ #endif /* CONFIG_FEATURE_SUID_CONFIG */
#ifdef CONFIG_FEATURE_SUID #if ENABLE_FEATURE_SUID
static void check_suid(struct BB_applet *applet) static void check_suid(struct BB_applet *applet)
{ {
uid_t ruid = getuid(); /* real [ug]id */ uid_t ruid = getuid(); /* real [ug]id */
uid_t rgid = getgid(); uid_t rgid = getgid();
#ifdef CONFIG_FEATURE_SUID_CONFIG #if ENABLE_FEATURE_SUID_CONFIG
if (suid_cfg_readable) { if (suid_cfg_readable) {
struct BB_suid_config *sct; struct BB_suid_config *sct;
mode_t m;
for (sct = suid_config; sct; sct = sct->m_next) { for (sct = suid_config; sct; sct = sct->m_next) {
if (sct->m_applet == applet) if (sct->m_applet == applet)
break; goto found;
} }
if (sct) { /* default: drop all privileges */
mode_t m = sct->m_mode; xsetgid(rgid);
xsetuid(ruid);
return;
found:
m = sct->m_mode;
if (sct->m_uid == ruid) if (sct->m_uid == ruid)
/* same uid */ /* same uid */
m >>= 6; m >>= 6;
@ -364,26 +368,23 @@ static void check_suid(struct BB_applet *applet)
if (sct->m_mode & S_ISUID) xsetuid(sct->m_uid); if (sct->m_mode & S_ISUID) xsetuid(sct->m_uid);
else xsetuid(ruid); /* no suid -> drop */ else xsetuid(ruid); /* no suid -> drop */
} }
} else {
/* default: drop all privileges */
xsetgid(rgid);
xsetuid(ruid);
}
return; return;
} else { }
#ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET #if !ENABLE_FEATURE_SUID_CONFIG_QUIET
static int onetime = 0; {
static smallint onetime = 0;
if (!onetime) { if (!onetime) {
onetime = 1; onetime = 1;
fprintf(stderr, "Using fallback suid method\n"); fprintf(stderr, "Using fallback suid method\n");
} }
#endif
} }
#endif
#endif #endif
if (applet->need_suid == _BB_SUID_ALWAYS) { if (applet->need_suid == _BB_SUID_ALWAYS) {
if (geteuid()) bb_error_msg_and_die("applet requires root privileges!"); if (geteuid())
bb_error_msg_and_die("applet requires root privileges!");
} else if (applet->need_suid == _BB_SUID_NEVER) { } else if (applet->need_suid == _BB_SUID_NEVER) {
xsetgid(rgid); /* drop all privileges */ xsetgid(rgid); /* drop all privileges */
xsetuid(ruid); xsetuid(ruid);
@ -395,7 +396,7 @@ static void check_suid(struct BB_applet *applet)
#ifdef CONFIG_FEATURE_COMPRESS_USAGE #if ENABLE_FEATURE_COMPRESS_USAGE
#include "usage_compressed.h" #include "usage_compressed.h"
#include "unarchive.h" #include "unarchive.h"
@ -405,7 +406,7 @@ static const char *unpack_usage_messages(void)
int input[2], output[2], pid; int input[2], output[2], pid;
char *buf; char *buf;
if(pipe(input) < 0 || pipe(output) < 0) if (pipe(input) < 0 || pipe(output) < 0)
exit(1); exit(1);
pid = fork(); pid = fork();
@ -480,8 +481,6 @@ void run_applet_by_name(const char *name, int argc, char **argv)
if (ENABLE_FEATURE_SUID_CONFIG) if (ENABLE_FEATURE_SUID_CONFIG)
parse_config_file(); parse_config_file();
if (!strncmp(name, "busybox", 7))
exit(busybox_main(argc, argv));
/* Do a binary search to find the applet entry given the name. */ /* Do a binary search to find the applet entry given the name. */
applet_using = find_applet_by_name(name); applet_using = find_applet_by_name(name);
if (applet_using) { if (applet_using) {
@ -492,4 +491,6 @@ void run_applet_by_name(const char *name, int argc, char **argv)
check_suid(applet_using); check_suid(applet_using);
exit(applet_using->main(argc, argv)); exit(applet_using->main(argc, argv));
} }
if (!strncmp(name, "busybox", 7))
exit(busybox_main(argc, argv));
} }

View File

@ -251,15 +251,15 @@ void smart_ulltoa5(unsigned long long ul, char buf[5])
fmt = " 123456789"; fmt = " 123456789";
if (!idx) { // 9999 or less: use 1234 format if (!idx) { // 9999 or less: use 1234 format
c = buf[0] = " 123456789"[v/10000]; c = buf[0] = " 123456789"[v/10000];
if (c!=' ') fmt = "0123456789"; if (c != ' ') fmt = "0123456789";
c = buf[1] = fmt[v/1000%10]; c = buf[1] = fmt[v/1000%10];
if (c!=' ') fmt = "0123456789"; if (c != ' ') fmt = "0123456789";
buf[2] = fmt[v/100%10]; buf[2] = fmt[v/100%10];
buf[3] = "0123456789"[v/10%10]; buf[3] = "0123456789"[v/10%10];
} else { } else {
if (v>=10*10) { // scaled value is >=10: use 123M format if (v >= 10*10) { // scaled value is >=10: use 123M format
c = buf[0] = " 123456789"[v/1000]; c = buf[0] = " 123456789"[v/1000];
if (c!=' ') fmt = "0123456789"; if (c != ' ') fmt = "0123456789";
buf[1] = fmt[v/100%10]; buf[1] = fmt[v/100%10];
buf[2] = "0123456789"[v/10%10]; buf[2] = "0123456789"[v/10%10];
} else { // scaled value is <10: use 1.2M format } else { // scaled value is <10: use 1.2M format
@ -483,10 +483,9 @@ DIR *warn_opendir(const char *path)
{ {
DIR *dp; DIR *dp;
if ((dp = opendir(path)) == NULL) { dp = opendir(path);
if (!dp)
bb_perror_msg("cannot open '%s'", path); bb_perror_msg("cannot open '%s'", path);
return NULL;
}
return dp; return dp;
} }
@ -495,7 +494,8 @@ DIR *xopendir(const char *path)
{ {
DIR *dp; DIR *dp;
if ((dp = opendir(path)) == NULL) dp = opendir(path);
if (!dp)
bb_perror_msg_and_die("cannot open '%s'", path); bb_perror_msg_and_die("cannot open '%s'", path);
return dp; return dp;
} }