cleanup comments and coding style and rewrite the mdev.conf parser to use common busybox functions and call strdup() less often ... saves a ~few hundred bytes

This commit is contained in:
Mike Frysinger 2007-11-19 09:04:22 +00:00
parent ed6a49c657
commit c881c733bb

View File

@ -35,154 +35,146 @@ static void make_device(char *path, int delete)
/* Try to read major/minor string. Note that the kernel puts \n after /* Try to read major/minor string. Note that the kernel puts \n after
* the data, so we don't need to worry about null terminating the string * the data, so we don't need to worry about null terminating the string
* because sscanf() will stop at the first nondigit, which \n is. We * because sscanf() will stop at the first nondigit, which \n is. We
* also depend on path having writeable space after it. */ * also depend on path having writeable space after it.
*/
if (!delete) { if (!delete) {
strcat(path, "/dev"); strcat(path, "/dev");
len = open_read_close(path, temp + 1, 64); len = open_read_close(path, temp + 1, 64);
*temp++ = 0; *temp++ = 0;
if (len < 1) return; if (len < 1)
return;
} }
/* Determine device name, type, major and minor */ /* Determine device name, type, major and minor */
device_name = bb_basename(path); device_name = bb_basename(path);
type = path[5]=='c' ? S_IFCHR : S_IFBLK; type = (path[5] == 'c' ? S_IFCHR : S_IFBLK);
/* If we have a config file, look up permissions for this device */
if (ENABLE_FEATURE_MDEV_CONF) { if (ENABLE_FEATURE_MDEV_CONF) {
char *conf, *pos, *end; FILE *fp;
int line, fd; char *line, *vline;
size_t lineno = 0;
/* mmap the config file */ /* If we have a config file, look up the user settings */
fd = open("/etc/mdev.conf", O_RDONLY); fp = fopen_or_warn("/etc/mdev.conf", "r");
if (fd < 0) if (!fp)
goto end_parse;
len = xlseek(fd, 0, SEEK_END);
conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (!conf)
goto end_parse; goto end_parse;
line = 0; while ((vline = line = xmalloc_getline(fp)) != NULL) {
/* Loop through lines in mmaped file*/
for (pos=conf; pos-conf<len;) {
int field; int field;
char *end2;
line++; /* A pristine copy for command execution. */
/* find end of this line */ char *orig_line;
for (end=pos; end-conf<len && *end!='\n'; end++) if (ENABLE_FEATURE_MDEV_EXEC)
; orig_line = xstrdup(line);
++lineno;
/* Three fields: regex, uid:gid, mode */ /* Three fields: regex, uid:gid, mode */
for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC); for (field = 0; field < (3 + ENABLE_FEATURE_MDEV_EXEC); ++field) {
field++)
{ /* Find a non-empty field */
/* Skip whitespace */ char *val;
while (pos<end && isspace(*pos)) pos++; do {
if (pos==end || *pos=='#') break; val = strtok(vline, " \t");
for (end2=pos; vline = NULL;
end2<end && !isspace(*end2) && *end2!='#'; end2++) } while (val && !*val);
; if (!val)
break;
if (field == 0) { if (field == 0) {
/* Regex to match this device */
char *regex = xstrndup(pos, end2-pos); /* Regex to match this device */
regex_t match; regex_t match;
regmatch_t off; regmatch_t off;
int result; int result;
/* Is this it? */ /* Is this it? */
xregcomp(&match,regex, REG_EXTENDED); xregcomp(&match, val, REG_EXTENDED);
result = regexec(&match, device_name, 1, &off, 0); result = regexec(&match, device_name, 1, &off, 0);
regfree(&match); regfree(&match);
free(regex);
/* If not this device, skip rest of line */ /* If not this device, skip rest of line */
if (result || off.rm_so if (result || off.rm_so || off.rm_eo != strlen(device_name))
|| off.rm_eo != strlen(device_name)) goto next_line;
break;
}
if (field == 1) {
/* uid:gid */
char *s, *s2; } else if (field == 1) {
/* Find : */ /* uid:gid device ownership */
for (s=pos; s<end2 && *s!=':'; s++) struct passwd *pass;
; struct group *grp;
if (s == end2) break;
char *str_uid = val;
char *str_gid = strchr(val, ':');
if (str_gid)
*str_gid = '\0', ++str_gid;
/* Parse UID */ /* Parse UID */
uid = strtoul(pos, &s2, 10); pass = getpwnam(str_uid);
if (s != s2) { if (pass)
struct passwd *pass;
char *_unam = xstrndup(pos, s-pos);
pass = getpwnam(_unam);
free(_unam);
if (!pass) break;
uid = pass->pw_uid; uid = pass->pw_uid;
} else
s++; uid = strtoul(str_uid, NULL, 10);
/* parse GID */
gid = strtoul(s, &s2, 10);
if (end2 != s2) {
struct group *grp;
char *_grnam = xstrndup(s, end2-s);
grp = getgrnam(_grnam);
free(_grnam);
if (!grp) break;
gid = grp->gr_gid;
}
}
if (field == 2) {
/* mode */
mode = strtoul(pos, &pos, 8); /* parse GID */
if (pos != end2) break; grp = getgrnam(str_gid);
} if (grp)
if (ENABLE_FEATURE_MDEV_EXEC && field == 3) { gid = grp->gr_gid;
// Command to run else
gid = strtoul(str_gid, NULL, 10);
} else if (field == 2) {
/* Mode device permissions */
mode = strtoul(val, NULL, 8);
} else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
/* Optional command to run */
const char *s = "@$*"; const char *s = "@$*";
const char *s2; const char *s2 = strchr(s, *val);
s2 = strchr(s, *pos++);
if (!s2) { if (!s2) {
// Force error /* Force error */
field = 1; field = 1;
break; break;
} }
if ((s2-s+1) & (1<<delete))
command = xstrndup(pos, end-pos);
}
pos = end2; /* Correlate the position in the "@$*" with the delete
* step so that we get the proper behavior.
*/
if ((s2 - s + 1) & (1 << delete))
command = xstrdup(orig_line + (val + 1 - line));
}
} }
/* Did everything parse happily? */ /* Did everything parse happily? */
if (field <= 2)
bb_error_msg_and_die("bad line %i", lineno);
if (field > 2) break; next_line:
if (field) bb_error_msg_and_die("bad line %d",line); free(line);
if (ENABLE_FEATURE_MDEV_EXEC)
/* Next line */ free(orig_line);
pos = ++end;
} }
munmap(conf, len);
if (ENABLE_FEATURE_CLEAN_UP)
fclose(fp);
end_parse: /* nothing */ ; end_parse: /* nothing */ ;
} }
umask(0); umask(0);
if (!delete) { if (!delete) {
if (sscanf(temp, "%d:%d", &major, &minor) != 2) return; if (sscanf(temp, "%d:%d", &major, &minor) != 2)
return;
if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST) if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
bb_perror_msg_and_die("mknod %s", device_name); bb_perror_msg_and_die("mknod %s", device_name);
if (major == root_major && minor == root_minor) if (major == root_major && minor == root_minor)
symlink(device_name, "root"); symlink(device_name, "root");
if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid); if (ENABLE_FEATURE_MDEV_CONF)
chown(device_name, uid, gid);
} }
if (command) { if (command) {
/* setenv will leak memory, so use putenv */ /* setenv will leak memory, so use putenv */
@ -195,7 +187,8 @@ static void make_device(char *path, int delete)
free(s); free(s);
free(command); free(command);
} }
if (delete) unlink(device_name); if (delete)
remove_file(device_name, FILEUTILS_FORCE);
} }
/* File callback for /sys/ traversal */ /* File callback for /sys/ traversal */
@ -296,9 +289,12 @@ int mdev_main(int argc, char **argv)
xchdir("/dev"); xchdir("/dev");
/* Scan */
if (argc == 2 && !strcmp(argv[1],"-s")) { if (argc == 2 && !strcmp(argv[1],"-s")) {
/* Scan:
* mdev -s
*/
struct stat st; struct stat st;
xstat("/", &st); xstat("/", &st);
@ -313,9 +309,14 @@ int mdev_main(int argc, char **argv)
ACTION_RECURSE | ACTION_FOLLOWLINKS, ACTION_RECURSE | ACTION_FOLLOWLINKS,
fileAction, dirAction, temp, 0); fileAction, dirAction, temp, 0);
/* Hotplug */
} else { } else {
/* Hotplug:
* env ACTION=... DEVPATH=... mdev
* ACTION can be "add" or "remove"
* DEVPATH is like "/block/sda" or "/class/input/mice"
*/
action = getenv("ACTION"); action = getenv("ACTION");
env_path = getenv("DEVPATH"); env_path = getenv("DEVPATH");
if (!action || !env_path) if (!action || !env_path)
@ -332,6 +333,8 @@ int mdev_main(int argc, char **argv)
} }
} }
if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp); if (ENABLE_FEATURE_CLEAN_UP)
RELEASE_CONFIG_BUFFER(temp);
return 0; return 0;
} }