#include <sys/wait.h>
#include <sys/stat.h>
+#include "libsysfs/sysfs/libsysfs.h"
#include "list.h"
#include "udev.h"
#include "udev_version.h"
#include "logging.h"
#include "namedev.h"
-#include "libsysfs/libsysfs.h"
#include "klibc_fixups.h"
static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
static char *get_default_owner(void)
{
if (strlen(default_owner_str) == 0)
- strncpy(default_owner_str, "root", OWNER_SIZE);
+ strfieldcpy(default_owner_str, "root");
return default_owner_str;
}
static char *get_default_group(void)
{
if (strlen(default_group_str) == 0)
- strncpy(default_group_str, "root", GROUP_SIZE);
+ strfieldcpy(default_group_str, "root");
return default_group_str;
}
return -1;
}
-static void apply_format(struct udevice *udev, unsigned char *string, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
+static void apply_format(struct udevice *udev, char *string, size_t maxsize,
+ struct sysfs_class_device *class_dev,
+ struct sysfs_device *sysfs_device)
{
char temp[NAME_SIZE];
char temp1[NAME_SIZE];
pos = string;
while (1) {
- pos = strchr(pos, '%');
+ pos = strchr(string, '%');
if (pos != NULL) {
pos[0] = '\0';
tail = pos+1;
case 'b':
if (strlen(udev->bus_id) == 0)
break;
- strcat(pos, udev->bus_id);
+ strnfieldcat(string, udev->bus_id, maxsize);
dbg("substitute bus_id '%s'", udev->bus_id);
break;
case 'k':
if (strlen(udev->kernel_name) == 0)
break;
- strcat(pos, udev->kernel_name);
+ strnfieldcat(string, udev->kernel_name, maxsize);
dbg("substitute kernel name '%s'", udev->kernel_name);
break;
case 'n':
if (strlen(udev->kernel_number) == 0)
break;
- strcat(pos, udev->kernel_number);
+ strnfieldcat(string, udev->kernel_number, maxsize);
dbg("substitute kernel number '%s'", udev->kernel_number);
break;
case 'm':
if (attr != NULL)
i = atoi(attr);
if (i > 0) {
- strncpy(temp1, udev->program_result, sizeof(temp1));
+ strfieldcpy(temp1, udev->program_result);
pos2 = temp1;
while (i) {
i--;
}
}
if (pos3) {
- strcat(pos, pos3);
+ strnfieldcat(string, pos3, maxsize);
dbg("substitute part of result string '%s'", pos3);
}
} else {
- strcat(pos, udev->program_result);
+ strnfieldcat(string, udev->program_result, maxsize);
dbg("substitute result string '%s'", udev->program_result);
}
break;
dbg("sysfa attribute '%s' not found", attr);
break;
}
- strcpy(pos, tmpattr->value);
+ strnfieldcat(string, tmpattr->value, maxsize);
dbg("substitute sysfs value '%s'", tmpattr->value);
} else {
dbg("missing attribute");
}
break;
case '%':
- strcat(pos, "%");
+ strnfieldcat(string, "%", maxsize);
break;
default:
dbg("unknown substitution type '%%%c'", c);
break;
}
- strcat(pos, tail);
+ strnfieldcat(string, tail, maxsize);
}
}
int fds[2];
pid_t pid;
int value_set = 0;
- char buffer[256];
+ char buffer[255];
char *pos;
char *args[PROGRAM_MAXARG];
int i;
case 0:
/* child */
close(STDOUT_FILENO);
- dup(fds[1]); /* dup write side of pipe to STDOUT */
+
+ /* dup write side of pipe to STDOUT */
+ dup(fds[1]);
+
+ /* copy off our path to use incase we have too many args */
+ strnfieldcpy(buffer, path, sizeof(buffer));
+
if (strchr(path, ' ')) {
/* exec with arguments */
pos = path;
break;
}
if (args[i]) {
- dbg("too many args - %d", i);
- args[i] = NULL;
+ dbg("too many args - %d, using subshell instead '%s'", i, buffer);
+ retval = execl("/bin/sh", "sh", "-c", buffer, NULL);
+ } else {
+ dbg("execute program '%s'", path);
+ retval = execv(args[0], args);
}
- retval = execv(args[0], args);
} else {
retval = execv(path, main_argv);
}
- dbg("child execve failed");
+ info(FIELD_PROGRAM " execution of '%s' failed", path);
exit(1);
case -1:
dbg("fork failed");
strncpy(value, buffer, len);
pos = value + strlen(value)-1;
if (pos[0] == '\n')
- pos[0] = '\0';
+ pos[0] = '\0';
dbg("result is '%s'", value);
}
}
/* execute external program */
if (dev->program[0] != '\0') {
dbg("check " FIELD_PROGRAM);
- apply_format(udev, dev->program, class_dev, sysfs_device);
+ apply_format(udev, dev->program, sizeof(dev->program),
+ class_dev, sysfs_device);
if (execute_program(dev->program, udev->program_result, NAME_SIZE) != 0) {
dbg(FIELD_PROGRAM " returned nozero");
goto try_parent;
found:
/* substitute placeholder */
- apply_format(udev, udev->name, class_dev, sysfs_device);
- apply_format(udev, udev->symlink, class_dev, sysfs_device);
+ apply_format(udev, udev->name, sizeof(udev->name),
+ class_dev, sysfs_device);
+ apply_format(udev, udev->symlink, sizeof(udev->symlink),
+ class_dev, sysfs_device);
udev->partitions = dev->partitions;
done:
perm = find_perm(udev->name);
} else {
/* no matching perms found :( */
udev->mode = get_default_mode();
- strncpy(udev->owner, get_default_owner(), OWNER_SIZE);
- strncpy(udev->group, get_default_group(), GROUP_SIZE);
+ strfieldcpy(udev->owner, get_default_owner());
+ strfieldcpy(udev->group, get_default_group());
}
dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
udev->name, udev->owner, udev->group, udev->mode);