X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=miscutils%2Fdevfsd.c;h=cd94869ae63f888b86ccb5690cfb4c5a21acbafb;hb=bfc3d82256c45fb1e6e04ca85fac8a65b34b72a1;hp=0aec94232f8c522d51dd95cca6182371fa20558c;hpb=1fc6238ffab2cbd09b9ce573b99d82f3eaeb5b77;p=oweals%2Fbusybox.git diff --git a/miscutils/devfsd.c b/miscutils/devfsd.c index 0aec94232..cd94869ae 100644 --- a/miscutils/devfsd.c +++ b/miscutils/devfsd.c @@ -234,27 +234,21 @@ static char get_old_ide_name(unsigned , unsigned); static char *write_old_sd_name(char *, unsigned, unsigned, const char *); /* busybox functions */ -static void msg_logger(int pri, const char * fmt, ...)__attribute__((format(printf, 2, 3))); -static void msg_logger_and_die(int pri, const char * fmt, ...)__attribute__((noreturn, format(printf, 2, 3))); -static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag); -static void fork_and_execute(int die, char *arg0, char **arg); -static int get_uid_gid(int, const char *); +static int get_uid_gid(int flag, const char *string); static void safe_memcpy(char * dest, const char * src, int len); -static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr); -static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr); +static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr); +static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr); /* Structs and vars */ static struct config_entry_struct *first_config = NULL; static struct config_entry_struct *last_config = NULL; -static const char *mount_point = NULL; +static char *mount_point = NULL; static volatile int caught_signal = FALSE; static volatile int caught_sighup = FALSE; -static struct initial_symlink_struct -{ +static struct initial_symlink_struct { const char *dest; const char *name; -} initial_symlinks[] = -{ +} initial_symlinks[] = { {"/proc/self/fd", "fd"}, {"fd/0", "stdin"}, {"fd/1", "stdout"}, @@ -262,12 +256,10 @@ static struct initial_symlink_struct {NULL, NULL}, }; -static struct event_type -{ +static struct event_type { unsigned int type; /* The DEVFSD_NOTIFY_* value */ const char *config_name; /* The name used in the config file */ -} event_types[] = -{ +} event_types[] = { {DEVFSD_NOTIFY_REGISTERED, "REGISTER"}, {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"}, {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"}, @@ -281,101 +273,33 @@ static struct event_type /* Busybox messages */ -static const char * const bb_msg_proto_rev = "protocol revision"; -static const char * const bb_msg_bad_config = "bad %s config file: %s"; -static const char * const bb_msg_small_buffer = "buffer too small"; -static const char * const bb_msg_variable_not_found = "variable: %s not found"; - -/* Busybox functions */ -static void msg_logger(int pri, const char * fmt, ...) -{ - va_list ap; - int ret; - - va_start(ap, fmt); - ret = access("/dev/log", F_OK); - if (ret == 0) { - openlog(applet_name, 0, LOG_DAEMON); - vsyslog(pri , fmt, ap); - /* Man: A trailing newline is added when needed. */ - closelog(); - } - /* ENABLE_DEVFSD_VERBOSE is always enabled if msg_logger is used */ - if ((ENABLE_DEVFSD_VERBOSE && ret) || ENABLE_DEBUG) { - bb_error_msg(fmt, ap); - } - va_end(ap); -} - -static void msg_logger_and_die(int pri, const char* fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - msg_logger(pri, fmt, ap); - va_end(ap); - exit(EXIT_FAILURE); -} +static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision"; +static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s"; +static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small"; +static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found"; /* Busybox stuff */ -#if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG) -#define devfsd_error_msg(fmt, args...) bb_error_msg(fmt, ## args) -#define devfsd_perror_msg_and_die(fmt, args...) bb_perror_msg_and_die(fmt, ## args) -#define devfsd_error_msg_and_die(fmt, args...) bb_error_msg_and_die(fmt, ## args) -#if defined(CONFIG_DEBUG) -#define debug_msg_logger(x, fmt, args...) msg_logger(x, fmt, ## args) -#else -#define debug_msg_logger(x, fmt, args...) -#endif +#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG +#define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args) +#define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args) +#define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args) +#define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args) +#define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args) #else -#define debug_msg_logger(x, fmt, args...) +#define info_logger(p, fmt, args...) #define msg_logger(p, fmt, args...) #define msg_logger_and_die(p, fmt, args...) exit(1) -#define devfsd_perror_msg_and_die(fmt, args...) exit(1) -#define devfsd_error_msg_and_die(fmt, args...) exit(1) -#define devfsd_error_msg(fmt, args...) +#define error_logger(p, fmt, args...) +#define error_logger_and_die(p, fmt, args...) exit(1) #endif -static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag) -{ - if (ioctl(fd, request, event_mask_flag) == -1) - msg_logger_and_die(LOG_ERR, "ioctl"); -} - -static void fork_and_execute(int die, char *arg0, char **arg) -{ - switch (fork()) { - case 0: - /* Child */ - break; - case -1: - /* Parent: Error : die or return */ - msg_logger(LOG_ERR,(char *)bb_msg_memory_exhausted); - if (die) - exit(EXIT_FAILURE); - return; - default: - /* Parent : ok : return or exit */ - if (arg0 != NULL) { - wait(NULL); - return; - } - exit(EXIT_SUCCESS); - } - /* Child : if arg0 != NULL do execvp */ - if (arg0 != NULL) { - BB_EXECVP(arg0, arg); - msg_logger_and_die(LOG_ERR, "execvp"); - } -} - static void safe_memcpy(char *dest, const char *src, int len) { memcpy(dest , src, len); dest[len] = '\0'; } -static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr) +static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr) { if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c') return 2 + addendum; @@ -388,7 +312,7 @@ static unsigned int scan_dev_name_common(const char *d, unsigned int n, int adde return 0; } -static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr) +static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr) { if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') { if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n' @@ -412,7 +336,7 @@ static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr) /* Public functions follow */ -int devfsd_main(int argc, char **argv); +int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int devfsd_main(int argc, char **argv) { int print_version = FALSE; @@ -430,51 +354,42 @@ int devfsd_main(int argc, char **argv) for (count = 2; count < argc; ++count) { if (argv[count][0] == '-') { if (argv[count][1] == 'v' && !argv[count][2]) /* -v */ - print_version = TRUE; + print_version = TRUE; else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f' - && argv[count][2] == 'g' && !argv[count][3]) /* -fg */ - do_daemon = FALSE; + && argv[count][2] == 'g' && !argv[count][3]) /* -fg */ + do_daemon = FALSE; else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n' - && argv[count][2] == 'p' && !argv[count][3]) /* -np */ - no_polling = TRUE; + && argv[count][2] == 'p' && !argv[count][3]) /* -np */ + no_polling = TRUE; else bb_show_usage(); } } - /* strip last / from mount point, so we don't need to check for it later */ - while (argv[1][1] != '\0' && argv[1][strlen(argv[1]) - 1] == '/') - argv[1][strlen(argv[1]) - 1] = '\0'; - - mount_point = argv[1]; + mount_point = bb_simplify_path(argv[1]); - if (chdir(mount_point) != 0) - devfsd_perror_msg_and_die(mount_point); + xchdir(mount_point); fd = xopen(".devfsd", O_RDONLY); - - if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) - devfsd_perror_msg_and_die("FD_CLOEXEC"); - - if (ioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev) == -1) - msg_logger_and_die(LOG_ERR, "ioctl"); + close_on_exec_on(fd); + xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev); /*setup initial entries */ - for (curr = initial_symlinks; curr->dest != NULL; ++curr) + for (curr = initial_symlinks; curr->dest != NULL; ++curr) symlink(curr->dest, curr->name); /* NB: The check for CONFIG_FILE is done in read_config_file() */ - if (print_version ||(DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) { + if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) { printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n", applet_name, DEVFSD_VERSION, bb_msg_proto_rev, - DEVFSD_PROTOCOL_REVISION_DAEMON,bb_msg_proto_rev, proto_rev); + DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev); if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev); exit(EXIT_SUCCESS); /* -v */ } /* Tell kernel we are special(i.e. we get to see hidden entries) */ - do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, 0); + xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0); sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; @@ -482,7 +397,7 @@ int devfsd_main(int argc, char **argv) /* Set up SIGHUP and SIGUSR1 handlers */ new_action.sa_handler = signal_handler; if (sigaction(SIGHUP, &new_action, NULL) != 0 || sigaction(SIGUSR1, &new_action, NULL) != 0) - devfsd_error_msg_and_die("sigaction"); + bb_error_msg_and_die("sigaction"); printf("%s v%s started for %s\n",applet_name, DEVFSD_VERSION, mount_point); @@ -494,11 +409,19 @@ int devfsd_main(int argc, char **argv) if (ENABLE_DEVFSD_FG_NP && no_polling) exit(0); + + if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG) + logmode = LOGMODE_BOTH; + else if (do_daemon == TRUE) + logmode = LOGMODE_SYSLOG; + /* This is the default */ + /*else + logmode = LOGMODE_STDIO; */ + if (do_daemon) { /* Release so that the child can grab it */ - do_ioctl_and_die(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0); - fork_and_execute(DIE, NULL, NULL); - setsid(); /* Prevent hangups and become pgrp leader */ + xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0); + bb_daemonize_or_rexec(0, argv); } else if (ENABLE_DEVFSD_FG_NP) { setpgid(0, 0); /* Become process group leader */ } @@ -509,8 +432,9 @@ int devfsd_main(int argc, char **argv) free_config(); read_config_file((char*)CONFIG_FILE, FALSE, &event_mask); if (do_scan) - dir_operation(SERVICE,mount_point,0,NULL); + dir_operation(SERVICE, mount_point, 0, NULL); } + if (ENABLE_FEATURE_CLEAN_UP) free(mount_point); } /* End Function main */ @@ -529,26 +453,23 @@ static void read_config_file(char *path, int optional, unsigned long *event_mask FILE *fp; char buf[STRING_LENGTH]; char *line = NULL; - - debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, path); + char *p; if (stat(path, &statbuf) == 0) { /* Don't read 0 length files: ignored */ /*if (statbuf.st_size == 0) return;*/ if (S_ISDIR(statbuf.st_mode)) { - /* strip last / from dirname so we don't need to check for it later */ - while (path && path[1] != '\0' && path[strlen(path) - 1] == '/') - path[strlen(path) - 1] = '\0'; - - dir_operation(READ_CONFIG, path, 0, event_mask); + p = bb_simplify_path(path); + dir_operation(READ_CONFIG, p, 0, event_mask); + free(p); return; } if ((fp = fopen(path, "r")) != NULL) { while (fgets(buf, STRING_LENGTH, fp) != NULL) { /* Skip whitespace */ - for (line = buf; isspace(*line); ++line) - /*VOID*/; + line = buf; + line = skip_whitespace(line); if (line[0] == '\0' || line[0] == '#') continue; process_config_line(line, event_mask); @@ -559,8 +480,8 @@ static void read_config_file(char *path, int optional, unsigned long *event_mask } } else { read_config_file_err: - if (optional == 0 && errno == ENOENT) - msg_logger_and_die(LOG_ERR, "read config file: %s: %m", path); + if (optional == 0 && errno == ENOENT) + error_logger_and_die(LOG_ERR, "read config file: %s", path); } } /* End Function read_config_file */ @@ -581,23 +502,21 @@ static void process_config_line(const char *line, unsigned long *event_mask) int i; /* !!!! Only Uppercase Keywords in devsfd.conf */ - static const char *const options[] = { - "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE", - "RESTORE", "PERMISSIONS", "MODLOAD", "EXECUTE", - "COPY", "IGNORE", "MKOLDCOMPAT", "MKNEWCOMPAT", - "RMOLDCOMPAT", "RMNEWCOMPAT", 0 - }; - - debug_msg_logger(LOG_INFO, __FUNCTION__); - - for (count = 0; count < MAX_ARGS; ++count) p[count][0] = '\0'; + static const char options[] ALIGN1 = + "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0" + "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0" + "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0" + "RMOLDCOMPAT\0""RMNEWCOMPAT\0"; + + for (count = 0; count < MAX_ARGS; ++count) + p[count][0] = '\0'; num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s", when, name, what, p[0], p[1], p[2], p[3], p[4], p[5], p[6]); - i = index_in_str_array(options, when); + i = index_in_strings(options, when); - /*"CLEAR_CONFIG"*/ + /* "CLEAR_CONFIG" */ if (i == 0) { free_config(); *event_mask = 0; @@ -610,20 +529,19 @@ static void process_config_line(const char *line, unsigned long *event_mask) /* "INCLUDE" & "OPTIONAL_INCLUDE" */ if (i == 1 || i == 2) { st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL); - msg_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name); + info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name); read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask); return; } /* "RESTORE" */ if (i == 3) { - dir_operation(RESTORE,name, strlen(name),NULL); + dir_operation(RESTORE, name, strlen(name),NULL); return; } if (num_args < 3) goto process_config_line_err; - new = xmalloc(sizeof *new); - memset(new, 0, sizeof *new); + new = xzalloc(sizeof *new); for (count = 0; event_types[count].config_name != NULL; ++count) { if (strcasecmp(when, event_types[count].config_name) != 0) @@ -632,18 +550,18 @@ static void process_config_line(const char *line, unsigned long *event_mask) break; } if (event_types[count].config_name == NULL) { - msg="WHEN in"; + msg = "WHEN in"; goto process_config_line_err; } - i = index_in_str_array(options, what); + i = index_in_strings(options, what); switch (i) { case 4: /* "PERMISSIONS" */ new->action.what = AC_PERMISSIONS; /* Get user and group */ if ((ptr = strchr(p[0], '.')) == NULL) { - msg="UID.GID"; + msg = "UID.GID"; goto process_config_line_err; /*"missing '.' in UID.GID"*/ } @@ -695,7 +613,7 @@ static void process_config_line(const char *line, unsigned long *event_mask) new->action.what = i - 2; break; default: - msg ="WHAT in"; + msg = "WHAT in"; goto process_config_line_err; /*esac*/ } /* switch (i) */ @@ -710,7 +628,8 @@ static void process_config_line(const char *line, unsigned long *event_mask) last_config->next = new; last_config = new; return; -process_config_line_err: + + process_config_line_err: msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line); } /* End Function process_config_line */ @@ -723,13 +642,9 @@ static int do_servicing(int fd, unsigned long event_mask) { ssize_t bytes; struct devfsd_notify_struct info; - unsigned long tmp_event_mask; - debug_msg_logger(LOG_INFO, __FUNCTION__); - - /* Tell devfs what events we care about */ - tmp_event_mask = event_mask; - do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, tmp_event_mask); + /* (void*) cast is only in order to match prototype */ + xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask); while (!caught_signal) { errno = 0; bytes = read(fd,(char *) &info, sizeof info); @@ -761,14 +676,13 @@ static void service_name(const struct devfsd_notify_struct *info) regmatch_t mbuf[MAX_SUBEXPR]; struct config_entry_struct *entry; - debug_msg_logger(LOG_INFO, __FUNCTION__); if (ENABLE_DEBUG && info->overrun_count > 0) - debug_msg_logger(LOG_ERR, "lost %u events", info->overrun_count); + msg_logger(LOG_ERR, "lost %u events", info->overrun_count); /* Discard lookups on "/dev/log" and "/dev/initctl" */ - if (info->type == DEVFSD_NOTIFY_LOOKUP + if (info->type == DEVFSD_NOTIFY_LOOKUP && ((info->devname[0] == 'l' && info->devname[1] == 'o' - && info->devname[2] == 'g' && !info->devname[3]) + && info->devname[2] == 'g' && !info->devname[3]) || (info->devname[0] == 'i' && info->devname[1] == 'n' && info->devname[2] == 'i' && info->devname[3] == 't' && info->devname[4] == 'c' && info->devname[5] == 't' @@ -778,14 +692,12 @@ static void service_name(const struct devfsd_notify_struct *info) for (entry = first_config; entry != NULL; entry = entry->next) { /* First check if action matches the type, then check if name matches */ - if (info->type != entry->action.when + if (info->type != entry->action.when || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0) continue; for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n) /* VOID */; - debug_msg_logger(LOG_INFO, "%s: action.what %d", __FUNCTION__, entry->action.what); - switch (entry->action.what) { case AC_PERMISSIONS: action_permissions(info, entry); @@ -825,13 +737,11 @@ static void action_permissions(const struct devfsd_notify_struct *info, { struct stat statbuf; - debug_msg_logger(LOG_INFO, __FUNCTION__); - - if (stat(info->devname, &statbuf) != 0 || - chmod(info->devname,(statbuf.st_mode & S_IFMT) |(entry->u.permissions.mode & ~S_IFMT)) != 0 || - chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0) { - msg_logger(LOG_ERR, "Can't chmod or chown: %s: %m",info->devname); - } + if (stat(info->devname, &statbuf) != 0 + || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 + || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0 + ) + error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname); } /* End Function action_permissions */ static void action_modload(const struct devfsd_notify_struct *info, @@ -843,18 +753,16 @@ static void action_modload(const struct devfsd_notify_struct *info, */ { char *argv[6]; - char device[STRING_LENGTH]; - argv[0] =(char*)MODPROBE; - argv[1] =(char*)MODPROBE_SWITCH_1; /* "-k" */ - argv[2] =(char*)MODPROBE_SWITCH_2; /* "-C" */ - argv[3] =(char*)CONFIG_MODULES_DEVFS; - argv[4] = device; + argv[0] = (char*)MODPROBE; + argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */ + argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */ + argv[3] = (char*)CONFIG_MODULES_DEVFS; + argv[4] = concat_path_file("/dev", info->devname); /* device */ argv[5] = NULL; - snprintf(device, sizeof(device), "/dev/%s", info->devname); - debug_msg_logger(LOG_INFO, "%s: %s %s %s %s %s",__FUNCTION__, argv[0],argv[1],argv[2],argv[3],argv[4]); - fork_and_execute(DIE, argv[0], argv); + wait4pid(xspawn(argv)); + free(argv[4]); } /* End Function action_modload */ static void action_execute(const struct devfsd_notify_struct *info, @@ -874,7 +782,6 @@ static void action_execute(const struct devfsd_notify_struct *info, char *argv[MAX_ARGS + 1]; char largv[MAX_ARGS + 1][STRING_LENGTH]; - debug_msg_logger(LOG_INFO ,__FUNCTION__); gv_info.info = info; gv_info.devname = info->devname; snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname); @@ -886,7 +793,7 @@ static void action_execute(const struct devfsd_notify_struct *info, argv[count] = largv[count]; } argv[count] = NULL; - fork_and_execute(NO_DIE, argv[0], argv); + wait4pid(spawn(argv)); } /* End Function action_execute */ @@ -908,8 +815,6 @@ static void action_copy(const struct devfsd_notify_struct *info, char source[STRING_LENGTH], destination[STRING_LENGTH]; int ret = 0; - debug_msg_logger(LOG_INFO, __FUNCTION__); - dest_stat.st_mode = 0; if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode)) @@ -935,8 +840,8 @@ static void action_copy(const struct devfsd_notify_struct *info, else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX)) new_mode |= S_ISVTX; ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat); - if (ENABLE_DEBUG && ret &&(errno != EEXIST)) - debug_msg_logger(LOG_ERR, "copy_inode: %s to %s: %m", source, destination); + if (ENABLE_DEBUG && ret && (errno != EEXIST)) + error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination); } /* End Function action_copy */ static void action_compat(const struct devfsd_notify_struct *info, unsigned int action) @@ -949,7 +854,7 @@ static void action_compat(const struct devfsd_notify_struct *info, unsigned int int ret; const char *compat_name = NULL; const char *dest_name = info->devname; - char *ptr=NULL; + const char *ptr; char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH]; int mode, host, bus, target, lun; unsigned int i; @@ -977,17 +882,15 @@ static void action_compat(const struct devfsd_notify_struct *info, unsigned int break; case AC_MKNEWCOMPAT: case AC_RMNEWCOMPAT: - ptr = strrchr(info->devname, '/') + 1; - i=scan_dev_name(info->devname, info->namelen, ptr); - - debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i); + ptr = bb_basename(info->devname); + i = scan_dev_name(info->devname, info->namelen, ptr); /* nothing found */ - if (i==0 || i > 9) + if (i == 0 || i > 9) return; - sscanf(info->devname +((i<6)?5:4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun); - snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname +((i>5)?4:0)); + sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun); + snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0)); dest_name = dest_buf; compat_name = compat_buf; @@ -1010,7 +913,7 @@ static void action_compat(const struct devfsd_notify_struct *info, unsigned int mode = ptr[2] - 107; /* 1 or 2 */ if (ptr[2] == 'a') mode = 3; - sprintf(compat_buf, fmt [i], host, bus, target, lun, mode, rewind_); + sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_); } /* 9 == ide/host/mt */ @@ -1022,8 +925,6 @@ static void action_compat(const struct devfsd_notify_struct *info, unsigned int if (compat_name == NULL) return; - debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, compat_name); - /* Now decide what to do with it */ switch (action) { case AC_MKOLDCOMPAT: @@ -1034,7 +935,7 @@ static void action_compat(const struct devfsd_notify_struct *info, unsigned int case AC_RMNEWCOMPAT: ret = unlink(compat_name); if (ENABLE_DEBUG && ret) - debug_msg_logger(LOG_ERR, "unlink: %s: %m", compat_name); + error_logger(LOG_ERR, "unlink: %s", compat_name); break; /*esac*/ } /* switch (action) */ @@ -1042,16 +943,14 @@ static void action_compat(const struct devfsd_notify_struct *info, unsigned int static void restore(char *spath, struct stat source_stat, int rootlen) { - char dpath[STRING_LENGTH]; + char *dpath; struct stat dest_stat; - debug_msg_logger(LOG_INFO, __FUNCTION__); - dest_stat.st_mode = 0; - snprintf(dpath, sizeof dpath, "%s%s", mount_point, spath + rootlen); + dpath = concat_path_file(mount_point, spath + rootlen); lstat(dpath, &dest_stat); - - if (S_ISLNK(source_stat.st_mode) ||(source_stat.st_mode & S_ISVTX)) + free(dpath); + if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX)) copy_inode(dpath, &dest_stat,(source_stat.st_mode & ~S_ISVTX) , spath, &source_stat); if (S_ISDIR(source_stat.st_mode)) @@ -1077,8 +976,6 @@ static int copy_inode(const char *destpath, const struct stat *dest_stat, struct sockaddr_un un_addr; char symlink_val[STRING_LENGTH]; - debug_msg_logger(LOG_INFO, __FUNCTION__); - if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) { /* Same type */ if (S_ISLNK(source_stat->st_mode)) { @@ -1150,8 +1047,6 @@ static void free_config(void) struct config_entry_struct *c_entry; void *next; - debug_msg_logger(LOG_INFO, __FUNCTION__); - for (c_entry = first_config; c_entry != NULL; c_entry = next) { unsigned int count; @@ -1184,11 +1079,6 @@ static int get_uid_gid(int flag, const char *string) if (ENABLE_DEVFSD_VERBOSE) msg = "user"; - debug_msg_logger(LOG_INFO, __FUNCTION__); - - if (ENABLE_DEBUG && flag != UID && flag != GID) - msg_logger_and_die(LOG_ERR,"%s: flag != UID && flag != GID", __FUNCTION__); - if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1]))) return atoi(string); @@ -1214,8 +1104,6 @@ static mode_t get_mode(const char *string) mode_t mode; int i; - debug_msg_logger(LOG_INFO, __FUNCTION__); - if (isdigit(string[0])) return strtoul(string, NULL, 8); if (strlen(string) != 9) @@ -1226,7 +1114,7 @@ static mode_t get_mode(const char *string) while (i > 0) { if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x') mode += i; - i = i/2; + i = i / 2; string++; } return mode; @@ -1234,41 +1122,39 @@ static mode_t get_mode(const char *string) static void signal_handler(int sig) { - debug_msg_logger(LOG_INFO, __FUNCTION__); - caught_signal = TRUE; if (sig == SIGHUP) caught_sighup = TRUE; - msg_logger(LOG_INFO, "Caught signal %d", sig); + info_logger(LOG_INFO, "Caught signal %d", sig); } /* End Function signal_handler */ static const char *get_variable(const char *variable, void *info) { + static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */ + + char hostname[STRING_LENGTH]; struct get_variable_info *gv_info = info; - static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH]; - const char *field_names[] = { "hostname", "mntpt", "devpath", "devname", - "uid", "gid", "mode", hostname, mount_point, - gv_info->devpath, gv_info->devname, 0 }; + const char *field_names[] = { + "hostname", "mntpt", "devpath", "devname", + "uid", "gid", "mode", hostname, mount_point, + gv_info->devpath, gv_info->devname, NULL + }; int i; - debug_msg_logger(LOG_INFO, __FUNCTION__); - if (gethostname(hostname, STRING_LENGTH - 1) != 0) - msg_logger_and_die(LOG_ERR, "gethostname: %m"); - /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */ - hostname[STRING_LENGTH - 1] = '\0'; + error_logger_and_die(LOG_ERR, "gethostname"); + + hostname[STRING_LENGTH - 1] = '\0'; /* index_in_str_array returns i>=0 */ i = index_in_str_array(field_names, variable); if (i > 6 || i < 0 || (i > 1 && gv_info == NULL)) - return NULL; - if (i >= 0 && i <= 3) { - debug_msg_logger(LOG_INFO, "%s: i=%d %s", __FUNCTION__, i, field_names[i + 7]); + return NULL; + if (i >= 0 && i <= 3) return field_names[i + 7]; - } if (i == 4) sprintf(sbuf, "%u", gv_info->info->uid); @@ -1276,9 +1162,6 @@ static const char *get_variable(const char *variable, void *info) sprintf(sbuf, "%u", gv_info->info->gid); else if (i == 6) sprintf(sbuf, "%o", gv_info->info->mode); - - debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, sbuf); - return sbuf; } /* End Function get_variable */ @@ -1286,8 +1169,6 @@ static void service(struct stat statbuf, char *path) { struct devfsd_notify_struct info; - debug_msg_logger(LOG_INFO, __FUNCTION__); - memset(&info, 0, sizeof info); info.type = DEVFSD_NOTIFY_REGISTERED; info.mode = statbuf.st_mode; @@ -1314,37 +1195,30 @@ static void dir_operation(int type, const char * dir_name, int var, unsigned lon struct stat statbuf; DIR *dp; struct dirent *de; - char path[STRING_LENGTH]; + char *path; - debug_msg_logger(LOG_INFO, __FUNCTION__); - - if ((dp = opendir(dir_name))==NULL) { - debug_msg_logger(LOG_ERR, "opendir: %s: %m", dir_name); + if ((dp = warn_opendir(dir_name)) == NULL) return; - } while ((de = readdir(dp)) != NULL) { if (de->d_name && DOT_OR_DOTDOT(de->d_name)) continue; - snprintf(path, sizeof(path), "%s/%s", dir_name, de->d_name); - debug_msg_logger(LOG_ERR, "%s: %s", __FUNCTION__, path); - - if (lstat(path, &statbuf) != 0) { - debug_msg_logger(LOG_ERR, "%s: %s: %m", __FUNCTION__, path); - continue; - } - switch (type) { - case SERVICE: - service(statbuf, path); - break; - case RESTORE: - restore(path, statbuf, var); - break; - case READ_CONFIG: - read_config_file(path, var, event_mask); - break; + path = concat_path_file(dir_name, de->d_name); + if (lstat(path, &statbuf) == 0) { + switch (type) { + case SERVICE: + service(statbuf, path); + break; + case RESTORE: + restore(path, statbuf, var); + break; + case READ_CONFIG: + read_config_file(path, var, event_mask); + break; + } } + free(path); } closedir(dp); } /* End Function do_scan_and_service */ @@ -1356,16 +1230,12 @@ static int mksymlink(const char *oldpath, const char *newpath) [RETURNS] 0 on success, else -1. */ { - debug_msg_logger(LOG_INFO, __FUNCTION__); - if (!make_dir_tree(newpath)) return -1; if (symlink(oldpath, newpath) != 0) { - if (errno != EEXIST) { - debug_msg_logger(LOG_ERR, "%s: %s to %s: %m", __FUNCTION__, oldpath, newpath); + if (errno != EEXIST) return -1; - } } return 0; } /* End Function mksymlink */ @@ -1377,12 +1247,8 @@ static int make_dir_tree(const char *path) [RETURNS] TRUE on success, else FALSE. */ { - debug_msg_logger(LOG_INFO, __FUNCTION__); - - if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1) { - debug_msg_logger(LOG_ERR, "%s: %s: %m",__FUNCTION__, path); + if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1) return FALSE; - } return TRUE; } /* End Function make_dir_tree */ @@ -1409,8 +1275,6 @@ static int expand_expression(char *output, unsigned int outsize, { char temp[STRING_LENGTH]; - debug_msg_logger(LOG_INFO, __FUNCTION__); - if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info)) return FALSE; expand_regexp(output, outsize, temp, devname, ex, numexp); @@ -1438,8 +1302,6 @@ static void expand_regexp(char *output, size_t outsize, const char *input, const char last_exp = '0' - 1 + numex; int c = -1; - debug_msg_logger(LOG_INFO, __FUNCTION__); - /* Guarantee NULL termination by writing an explicit '\0' character into the very last byte */ if (outsize) @@ -1529,7 +1391,7 @@ const char *get_old_name(const char *devname, unsigned int namelen, */ { const char *compat_name = NULL; - char *ptr; + const char *ptr; struct translate_struct *trans; unsigned int i; char mode; @@ -1555,8 +1417,6 @@ const char *get_old_name(const char *devname, unsigned int namelen, NULL }; - debug_msg_logger(LOG_INFO, __FUNCTION__); - for (trans = translate_table; trans->match != NULL; ++trans) { len = strlen(trans->match); @@ -1568,7 +1428,7 @@ const char *get_old_name(const char *devname, unsigned int namelen, } } - ptr = (strrchr(devname, '/') + 1); + ptr = bb_basename(devname); i = scan_dev_name(devname, namelen, ptr); if (i > 0 && i < 13) @@ -1576,15 +1436,13 @@ const char *get_old_name(const char *devname, unsigned int namelen, else return NULL; - debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i); - /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */ if (i == 1 || i == 3 || i == 10) sprintf(buffer, fmt[i], minor); /* 2 ==scsi/disc, 4 == scsi/part */ if (i == 2 || i == 4) - compat_name = write_old_sd_name(buffer, major, minor,((i == 2)?"":(ptr + 4))); + compat_name = write_old_sd_name(buffer, major, minor,((i == 2) ? "" : (ptr + 4))); /* 5 == scsi/mt */ if (i == 5) { @@ -1617,10 +1475,6 @@ const char *get_old_name(const char *devname, unsigned int namelen, indexx = atoi(devname + 5); sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]); } - - if (ENABLE_DEBUG && compat_name != NULL) - msg_logger(LOG_INFO, "%s: compat_name %s", __FUNCTION__, compat_name); - return compat_name; } /* End Function get_old_name */ @@ -1635,8 +1489,6 @@ static char get_old_ide_name(unsigned int major, unsigned int minor) char c = 'a'; /* 97 */ int i = IDE0_MAJOR; - debug_msg_logger(LOG_INFO, __FUNCTION__); - /* I hope it works like the previous code as it saves a few bytes. Tito ;P */ do { if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR @@ -1671,8 +1523,6 @@ static char *write_old_sd_name(char *buffer, { unsigned int disc_index; - debug_msg_logger(LOG_INFO, __FUNCTION__); - if (major == 8) { sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part); return buffer; @@ -1716,8 +1566,6 @@ int st_expr_expand(char *output, unsigned int length, const char *input, struct passwd *pwent; char buffer[BUFFER_SIZE], tmp[STRING_LENGTH]; - debug_msg_logger(LOG_INFO, __FUNCTION__); - if (length > BUFFER_SIZE) length = BUFFER_SIZE; for (; TRUE; ++input) { @@ -1734,7 +1582,7 @@ int st_expr_expand(char *output, unsigned int length, const char *input, if (isspace(ch) ||(ch == '/') ||(ch == '\0')) { /* User's own home directory: leave separator for next time */ if ((env = getenv("HOME")) == NULL) { - msg_logger(LOG_INFO, bb_msg_variable_not_found, "HOME"); + info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME"); return FALSE; } len = strlen(env); @@ -1746,14 +1594,14 @@ int st_expr_expand(char *output, unsigned int length, const char *input, } /* Someone else's home directory */ for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr) - /* VOID */ ; + /* VOID */; len = ptr - input; if (len >= sizeof tmp) goto st_expr_expand_out; safe_memcpy(tmp, input, len); input = ptr - 1; if ((pwent = getpwnam(tmp)) == NULL) { - msg_logger(LOG_INFO, "no pwent for: %s", tmp); + info_logger(LOG_INFO, "no pwent for: %s", tmp); return FALSE; } len = strlen(pwent->pw_dir); @@ -1778,7 +1626,7 @@ int st_expr_expand(char *output, unsigned int length, const char *input, } return FALSE; st_expr_expand_out: - msg_logger(LOG_INFO, bb_msg_small_buffer); + info_logger(LOG_INFO, bb_msg_small_buffer); return FALSE; } /* End Function st_expr_expand */ @@ -1809,8 +1657,6 @@ static const char *expand_variable(char *buffer, unsigned int length, const char *env, *ptr; char tmp[STRING_LENGTH]; - debug_msg_logger(LOG_INFO, __FUNCTION__); - ch = input[0]; if (ch == '$') { /* Special case for "$$": PID */ @@ -1827,7 +1673,7 @@ static const char *expand_variable(char *buffer, unsigned int length, if (ch != '{') { /* Simple variable expansion */ for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr) - /* VOID */ ; + /* VOID */; len = ptr - input; if ((size_t)len >= sizeof tmp) goto expand_variable_out; @@ -1835,7 +1681,7 @@ static const char *expand_variable(char *buffer, unsigned int length, safe_memcpy(tmp, input, len); input = ptr - 1; if ((env = get_variable_v2(tmp, func, info)) == NULL) { - msg_logger(LOG_INFO, bb_msg_variable_not_found, tmp); + info_logger(LOG_INFO, bb_msg_variable_not_found, tmp); return NULL; } len = strlen(env); @@ -1863,7 +1709,7 @@ static const char *expand_variable(char *buffer, unsigned int length, return input + len; } if (ch != ':' || ptr[1] != '-') { - msg_logger(LOG_INFO, "illegal char in var name"); + info_logger(LOG_INFO, "illegal char in var name"); return NULL; } /* It's that handy "${var:-word}" expression. Check if var is defined */ @@ -1886,7 +1732,7 @@ static const char *expand_variable(char *buffer, unsigned int length, --open_braces; break; case '\0': - msg_logger(LOG_INFO,"\"}\" not found in: %s", input); + info_logger(LOG_INFO,"\"}\" not found in: %s", input); return NULL; default: break; @@ -1925,7 +1771,7 @@ static const char *expand_variable(char *buffer, unsigned int length, *out_pos += len; return input; expand_variable_out: - msg_logger(LOG_INFO, bb_msg_small_buffer); + info_logger(LOG_INFO, bb_msg_small_buffer); return NULL; } /* End Function expand_variable */ @@ -1943,8 +1789,6 @@ static const char *get_variable_v2(const char *variable, { const char *value; - debug_msg_logger(LOG_INFO, __FUNCTION__); - if (func != NULL) { value = (*func)(variable, info); if (value != NULL)