diff options
-rw-r--r-- | src/libudev/libudev-device.c | 57 | ||||
-rw-r--r-- | src/libudev/libudev-private.h | 2 | ||||
-rw-r--r-- | src/shared/util.c | 43 | ||||
-rw-r--r-- | src/shared/util.h | 3 | ||||
-rw-r--r-- | src/udev/udev-rules.c | 35 |
5 files changed, 91 insertions, 49 deletions
diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c index 9f1787b395..cab52a959b 100644 --- a/src/libudev/libudev-device.c +++ b/src/libudev/libudev-device.c @@ -37,6 +37,7 @@ #include "libudev-private.h" static int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode); +static struct udev_list_entry *udev_device_add_property_internal(struct udev_device *udev_device, const char *key, const char *value); /** * SECTION:libudev-device @@ -123,7 +124,7 @@ static int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long udev_device->seqnum = seqnum; snprintf(num, sizeof(num), "%llu", seqnum); - udev_device_add_property(udev_device, "SEQNUM", num); + udev_device_add_property_internal(udev_device, "SEQNUM", num); return 0; } @@ -140,7 +141,7 @@ static int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex) udev_device->ifindex = ifindex; snprintf(num, sizeof(num), "%d", ifindex); - udev_device_add_property(udev_device, "IFINDEX", num); + udev_device_add_property_internal(udev_device, "IFINDEX", num); return 0; } @@ -168,9 +169,9 @@ static int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum) udev_device->devnum = devnum; snprintf(num, sizeof(num), "%u", major(devnum)); - udev_device_add_property(udev_device, "MAJOR", num); + udev_device_add_property_internal(udev_device, "MAJOR", num); snprintf(num, sizeof(num), "%u", minor(devnum)); - udev_device_add_property(udev_device, "MINOR", num); + udev_device_add_property_internal(udev_device, "MINOR", num); return 0; } @@ -187,7 +188,7 @@ static int udev_device_set_devpath_old(struct udev_device *udev_device, const ch udev_device->devpath_old = strdup(devpath_old); if (udev_device->devpath_old == NULL) return -ENOMEM; - udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old); + udev_device_add_property_internal(udev_device, "DEVPATH_OLD", udev_device->devpath_old); pos = strrchr(udev_device->devpath_old, '/'); if (pos == NULL) @@ -224,7 +225,7 @@ static int udev_device_set_driver(struct udev_device *udev_device, const char *d if (udev_device->driver == NULL) return -ENOMEM; udev_device->driver_set = true; - udev_device_add_property(udev_device, "DRIVER", udev_device->driver); + udev_device_add_property_internal(udev_device, "DRIVER", udev_device->driver); return 0; } @@ -254,7 +255,7 @@ static int udev_device_set_devtype(struct udev_device *udev_device, const char * if (udev_device->devtype == NULL) return -ENOMEM; udev_device->devtype_set = true; - udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype); + udev_device_add_property_internal(udev_device, "DEVTYPE", udev_device->devtype); return 0; } @@ -265,7 +266,7 @@ static int udev_device_set_subsystem(struct udev_device *udev_device, const char if (udev_device->subsystem == NULL) return -ENOMEM; udev_device->subsystem_set = true; - udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem); + udev_device_add_property_internal(udev_device, "SUBSYSTEM", udev_device->subsystem); return 0; } @@ -323,7 +324,7 @@ static int udev_device_set_devnode_mode(struct udev_device *udev_device, mode_t udev_device->devnode_mode = mode; snprintf(num, sizeof(num), "%#o", mode); - udev_device_add_property(udev_device, "DEVMODE", num); + udev_device_add_property_internal(udev_device, "DEVMODE", num); return 0; } @@ -340,7 +341,7 @@ static int udev_device_set_devnode_uid(struct udev_device *udev_device, uid_t ui udev_device->devnode_uid = uid; snprintf(num, sizeof(num), "%u", uid); - udev_device_add_property(udev_device, "DEVUID", num); + udev_device_add_property_internal(udev_device, "DEVUID", num); return 0; } @@ -357,11 +358,11 @@ static int udev_device_set_devnode_gid(struct udev_device *udev_device, gid_t gi udev_device->devnode_gid = gid; snprintf(num, sizeof(num), "%u", gid); - udev_device_add_property(udev_device, "DEVGID", num); + udev_device_add_property_internal(udev_device, "DEVGID", num); return 0; } -struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value) +static struct udev_list_entry *udev_device_add_property_internal(struct udev_device *udev_device, const char *key, const char *value) { udev_device->envp_uptodate = false; if (value == NULL) { @@ -376,6 +377,20 @@ struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device return udev_list_entry_add(&udev_device->properties_list, key, value); } + +int udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value) +{ + struct udev_list_entry *property; + + property = udev_device_add_property_internal(udev_device, key, value); + + /* store in db, skip private keys */ + if (key[0] != '.') + udev_list_entry_set_num(property, true); + + return 0; +} + static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property) { char name[UTIL_LINE_SIZE]; @@ -389,7 +404,7 @@ static struct udev_list_entry *udev_device_add_property_from_string(struct udev_ val = &val[1]; if (val[0] == '\0') val = NULL; - return udev_device_add_property(udev_device, name, val); + return udev_device_add_property_internal(udev_device, name, val); } static int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath) @@ -402,7 +417,7 @@ static int udev_device_set_syspath(struct udev_device *udev_device, const char * if (udev_device->syspath == NULL) return -ENOMEM; udev_device->devpath = udev_device->syspath + strlen("/sys"); - udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath); + udev_device_add_property_internal(udev_device, "DEVPATH", udev_device->devpath); pos = strrchr(udev_device->syspath, '/'); if (pos == NULL) @@ -1335,7 +1350,7 @@ _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct ud l = strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL); udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) l = strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL); - udev_device_add_property(udev_device, "DEVLINKS", symlinks); + udev_device_add_property_internal(udev_device, "DEVLINKS", symlinks); } } if (!udev_device->tags_uptodate) { @@ -1350,7 +1365,7 @@ _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct ud l = strpcpyl(&s, sizeof(tags), ":", NULL); udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device)) l = strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL); - udev_device_add_property(udev_device, "TAGS", tags); + udev_device_add_property_internal(udev_device, "TAGS", tags); } } return udev_list_get_entry(&udev_device->properties_list); @@ -1412,7 +1427,7 @@ void udev_device_set_usec_initialized(struct udev_device *udev_device, usec_t us udev_device->usec_initialized = usec_initialized; snprintf(num, sizeof(num), USEC_FMT, usec_initialized); - udev_device_add_property(udev_device, "USEC_INITIALIZED", num); + udev_device_add_property_internal(udev_device, "USEC_INITIALIZED", num); } /** @@ -1656,7 +1671,7 @@ static int udev_device_set_devnode(struct udev_device *udev_device, const char * } if (udev_device->devnode == NULL) return -ENOMEM; - udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode); + udev_device_add_property_internal(udev_device, "DEVNAME", udev_device->devnode); return 0; } @@ -1883,7 +1898,7 @@ int udev_device_set_action(struct udev_device *udev_device, const char *action) udev_device->action = strdup(action); if (udev_device->action == NULL) return -ENOMEM; - udev_device_add_property(udev_device, "ACTION", udev_device->action); + udev_device_add_property_internal(udev_device, "ACTION", udev_device->action); return 0; } @@ -1946,8 +1961,8 @@ int udev_device_rename(struct udev_device *udev_device, const char *name) interface = udev_device_get_property_value(udev_device, "INTERFACE"); if (interface) { /* like DEVPATH_OLD, INTERFACE_OLD is not saved to the db, but only stays around for the current event */ - udev_device_add_property(udev_device, "INTERFACE_OLD", interface); - udev_device_add_property(udev_device, "INTERFACE", name); + udev_device_add_property_internal(udev_device, "INTERFACE_OLD", interface); + udev_device_add_property_internal(udev_device, "INTERFACE", name); } return 0; diff --git a/src/libudev/libudev-private.h b/src/libudev/libudev-private.h index 6f79b999ce..db64890ff9 100644 --- a/src/libudev/libudev-private.h +++ b/src/libudev/libudev-private.h @@ -45,7 +45,7 @@ gid_t udev_device_get_devnode_gid(struct udev_device *udev_device); int udev_device_rename(struct udev_device *udev_device, const char *new_name); int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink); void udev_device_cleanup_devlinks_list(struct udev_device *udev_device); -struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value); +int udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value); void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property); int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device); char **udev_device_get_properties_envp(struct udev_device *udev_device); diff --git a/src/shared/util.c b/src/shared/util.c index f68b687006..d1fd3c80f3 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -1001,6 +1001,49 @@ int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { return 0; } +int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) { + _cleanup_close_ int fd; + int r; + + assert(path); + + if (parents) + mkdir_parents(path, 0755); + + fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode > 0 ? mode : 0644); + if (fd < 0) + return -errno; + + if (mode > 0) { + r = fchmod(fd, mode); + if (r < 0) + return -errno; + } + + if (uid != UID_INVALID || gid != GID_INVALID) { + r = fchown(fd, uid, gid); + if (r < 0) + return -errno; + } + + if (stamp != USEC_INFINITY) { + struct timespec ts[2]; + + timespec_store(&ts[0], stamp); + ts[1] = ts[0]; + r = futimens(fd, ts); + } else + r = futimens(fd, NULL); + if (r < 0) + return -errno; + + return 0; +} + +int touch(const char *path) { + return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, 0); +} + bool null_or_empty(struct stat *st) { assert(st); diff --git a/src/shared/util.h b/src/shared/util.h index 55701cf7a5..fc4f9f2e22 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -282,6 +282,9 @@ char* dirname_malloc(const char *path); int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid); +int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode); +int touch(const char *path); + bool null_or_empty(struct stat *st) _pure_; int null_or_empty_path(const char *fn); int null_or_empty_fd(int fd); diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index 54c865f143..5bf66fb8ce 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -611,10 +611,7 @@ static int import_property_from_string(struct udev_device *dev, char *line) { val++; } - entry = udev_device_add_property(dev, key, val); - /* store in db, skip private keys */ - if (key[0] != '.') - udev_list_entry_set_num(entry, true); + udev_device_add_property(dev, key, val); return 0; } @@ -675,12 +672,7 @@ static int import_parent_into_properties(struct udev_device *dev, const char *fi const char *val = udev_list_entry_get_value(list_entry); if (fnmatch(filter, key, 0) == 0) { - struct udev_list_entry *entry; - - entry = udev_device_add_property(dev, key, val); - /* store in db, skip private keys */ - if (key[0] != '.') - udev_list_entry_set_num(entry, true); + udev_device_add_property(dev, key, val); } } return 0; @@ -2172,12 +2164,9 @@ int udev_rules_apply_to_event(struct udev_rules *rules, const char *value; value = udev_device_get_property_value(event->dev_db, key); - if (value != NULL) { - struct udev_list_entry *entry; - - entry = udev_device_add_property(event->dev, key, value); - udev_list_entry_set_num(entry, true); - } else { + if (value != NULL) + udev_device_add_property(event->dev, key, value); + else { if (cur->key.op != OP_NOMATCH) goto nomatch; } @@ -2197,13 +2186,10 @@ int udev_rules_apply_to_event(struct udev_rules *rules, pos = strstr(cmdline, key); if (pos != NULL) { - struct udev_list_entry *entry; - pos += strlen(key); if (pos[0] == '\0' || isspace(pos[0])) { /* we import simple flags as 'FLAG=1' */ - entry = udev_device_add_property(event->dev, key, "1"); - udev_list_entry_set_num(entry, true); + udev_device_add_property(event->dev, key, "1"); imported = true; } else if (pos[0] == '=') { const char *value; @@ -2213,8 +2199,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, while (pos[0] != '\0' && !isspace(pos[0])) pos++; pos[0] = '\0'; - entry = udev_device_add_property(event->dev, key, value); - udev_list_entry_set_num(entry, true); + udev_device_add_property(event->dev, key, value); imported = true; } } @@ -2387,7 +2372,6 @@ int udev_rules_apply_to_event(struct udev_rules *rules, char *value = rules_str(rules, cur->key.value_off); char value_new[UTIL_NAME_SIZE]; const char *value_old = NULL; - struct udev_list_entry *entry; if (value[0] == '\0') { if (cur->key.op == OP_ADD) @@ -2407,10 +2391,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, } else udev_event_apply_format(event, value, value_new, sizeof(value_new)); - entry = udev_device_add_property(event->dev, name, value_new); - /* store in db, skip private keys */ - if (name[0] != '.') - udev_list_entry_set_num(entry, true); + udev_device_add_property(event->dev, name, value_new); break; } case TK_A_TAG: { |