X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=udev_utils.c;h=e6580d2c0acdca0404e17b2cddb1eee19ec0ad6d;hb=69f57c6a2b94ae3b70a0ca43b46c30f8dce0a295;hp=aa5c75f74d02a29c466a0185a8bbb9126cd1ed42;hpb=45a7b668eca398831c0cfc39e4237494b3d603e9;p=elogind.git diff --git a/udev_utils.c b/udev_utils.c index aa5c75f74..e6580d2c0 100644 --- a/udev_utils.c +++ b/udev_utils.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -46,22 +47,26 @@ void udev_init_device(struct udevice *udev, const char* devpath, const char *sub strfieldcpy(udev->subsystem, subsystem); if (strcmp(udev->subsystem, "block") == 0) - udev->type = 'b'; + udev->type = BLOCK; else if (strcmp(udev->subsystem, "net") == 0) - udev->type = 'n'; + udev->type = NET; else if (strncmp(udev->devpath, "/block/", 7) == 0) - udev->type = 'b'; + udev->type = BLOCK; else if (strncmp(udev->devpath, "/class/net/", 11) == 0) - udev->type = 'n'; + udev->type = NET; else if (strncmp(udev->devpath, "/class/", 7) == 0) - udev->type = 'c'; + udev->type = CLASS; + + udev->mode = 0660; + strcpy(udev->owner, "root"); + strcpy(udev->group, "root"); } -int kernel_release_satisfactory(int version, int patchlevel, int sublevel) +int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel) { - static int kversion = 0; - static int kpatchlevel; - static int ksublevel; + static unsigned int kversion = 0; + static unsigned int kpatchlevel; + static unsigned int ksublevel; if (kversion == 0) { struct utsname uts; @@ -107,6 +112,66 @@ int create_path(const char *path) return mkdir(p, 0755); } +/* Reset permissions on the device node, before unlinking it to make sure, + * that permisions of possible hard links will be removed too. + */ +int unlink_secure(const char *filename) +{ + int retval; + + retval = chown(filename, 0, 0); + if (retval) + dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno)); + + retval = chmod(filename, 0000); + if (retval) + dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno)); + + retval = unlink(filename); + if (errno == ENOENT) + retval = 0; + + if (retval) + dbg("unlink(%s) failed with error '%s'", filename, strerror(errno)); + + return retval; +} + +int parse_get_pair(char **orig_string, char **left, char **right) +{ + char *temp; + char *string = *orig_string; + + if (!string) + return -ENODEV; + + /* eat any whitespace */ + while (isspace(*string) || *string == ',') + ++string; + + /* split based on '=' */ + temp = strsep(&string, "="); + *left = temp; + if (!string) + return -ENODEV; + + /* take the right side and strip off the '"' */ + while (isspace(*string)) + ++string; + if (*string == '"') + ++string; + else + return -ENODEV; + + temp = strsep(&string, "\""); + if (!string || *temp == '\0') + return -ENODEV; + *right = temp; + *orig_string = string; + + return 0; +} + int file_map(const char *filename, char **buf, size_t *bufsize) { struct stat stats; @@ -139,22 +204,32 @@ void file_unmap(char *buf, size_t bufsize) munmap(buf, bufsize); } -size_t buf_get_line(char *buf, size_t buflen, size_t cur) +/* return number of chars until the next newline, skip escaped newline */ +size_t buf_get_line(const char *buf, size_t buflen, size_t cur) { - size_t count = 0; + int escape = 0; + size_t count; + + for (count = cur; count < buflen; count++) { + if (!escape && buf[count] == '\n') + break; - for (count = cur; count < buflen && buf[count] != '\n'; count++); + if (buf[count] == '\\') + escape = 1; + else + escape = 0; + } return count - cur; } void no_trailing_slash(char *path) { - int len; + size_t len; len = strlen(path); - if (len > 0 && path[len-1] == '/') - path[len-1] = '\0'; + while (len > 0 && path[len-1] == '/') + path[--len] = '\0'; } struct files {