diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2016-10-22 16:11:41 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2016-10-22 16:15:46 -0400 |
commit | fb4650aa346a57da3d20525ca2a3124099a1a307 (patch) | |
tree | ce7365ba81a89f676a3435138340fe24b6ea546c /src | |
parent | d7f69e16f1a5b84e9acf1771a9b53da3787ae79d (diff) |
tree-wide: use startswith return value to avoid hardcoded offset
I think it's an antipattern to have to count the number of bytes in
the prefix by hand. We should do this automatically to avoid wasting
programmer time, and possible errors. I didn't any offsets that were
wrong, so this change is mostly to make future development easier.
Diffstat (limited to 'src')
-rw-r--r-- | src/core/cgroup.c | 10 | ||||
-rw-r--r-- | src/core/manager.c | 111 | ||||
-rw-r--r-- | src/cryptsetup/cryptsetup.c | 105 |
3 files changed, 112 insertions, 114 deletions
diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 23a92f9651..09bbe038e9 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -928,7 +928,7 @@ static void cgroup_context_apply(Unit *u, CGroupMask mask, ManagerState state) { } LIST_FOREACH(device_allow, a, c->device_allow) { - char acc[4]; + char acc[4], *val; unsigned k = 0; if (a->r) @@ -945,10 +945,10 @@ static void cgroup_context_apply(Unit *u, CGroupMask mask, ManagerState state) { if (startswith(a->path, "/dev/")) whitelist_device(path, a->path, acc); - else if (startswith(a->path, "block-")) - whitelist_major(path, a->path + 6, 'b', acc); - else if (startswith(a->path, "char-")) - whitelist_major(path, a->path + 5, 'c', acc); + else if ((val = startswith(a->path, "block-"))) + whitelist_major(path, val, 'b', acc); + else if ((val = startswith(a->path, "char-"))) + whitelist_major(path, val, 'c', acc); else log_unit_debug(u, "Ignoring device %s while writing cgroup attribute.", a->path); } diff --git a/src/core/manager.c b/src/core/manager.c index 65f163de31..0549f5ef1b 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -2582,6 +2582,7 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { for (;;) { char line[LINE_MAX], *l; + const char *val; if (!fgets(line, sizeof(line), f)) { if (feof(f)) @@ -2598,63 +2599,63 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { if (l[0] == 0) break; - if (startswith(l, "current-job-id=")) { + if ((val = startswith(l, "current-job-id="))) { uint32_t id; - if (safe_atou32(l+15, &id) < 0) - log_debug("Failed to parse current job id value %s", l+15); + if (safe_atou32(val, &id) < 0) + log_debug("Failed to parse current job id value %s", val); else m->current_job_id = MAX(m->current_job_id, id); - } else if (startswith(l, "n-installed-jobs=")) { + } else if ((val = startswith(l, "n-installed-jobs="))) { uint32_t n; - if (safe_atou32(l+17, &n) < 0) - log_debug("Failed to parse installed jobs counter %s", l+17); + if (safe_atou32(val, &n) < 0) + log_debug("Failed to parse installed jobs counter %s", val); else m->n_installed_jobs += n; - } else if (startswith(l, "n-failed-jobs=")) { + } else if ((val = startswith(l, "n-failed-jobs="))) { uint32_t n; - if (safe_atou32(l+14, &n) < 0) - log_debug("Failed to parse failed jobs counter %s", l+14); + if (safe_atou32(val, &n) < 0) + log_debug("Failed to parse failed jobs counter %s", val); else m->n_failed_jobs += n; - } else if (startswith(l, "taint-usr=")) { + } else if ((val = startswith(l, "taint-usr="))) { int b; - b = parse_boolean(l+10); + b = parse_boolean(val); if (b < 0) - log_debug("Failed to parse taint /usr flag %s", l+10); + log_debug("Failed to parse taint /usr flag %s", val); else m->taint_usr = m->taint_usr || b; - } else if (startswith(l, "firmware-timestamp=")) - dual_timestamp_deserialize(l+19, &m->firmware_timestamp); - else if (startswith(l, "loader-timestamp=")) - dual_timestamp_deserialize(l+17, &m->loader_timestamp); - else if (startswith(l, "kernel-timestamp=")) - dual_timestamp_deserialize(l+17, &m->kernel_timestamp); - else if (startswith(l, "initrd-timestamp=")) - dual_timestamp_deserialize(l+17, &m->initrd_timestamp); - else if (startswith(l, "userspace-timestamp=")) - dual_timestamp_deserialize(l+20, &m->userspace_timestamp); - else if (startswith(l, "finish-timestamp=")) - dual_timestamp_deserialize(l+17, &m->finish_timestamp); - else if (startswith(l, "security-start-timestamp=")) - dual_timestamp_deserialize(l+25, &m->security_start_timestamp); - else if (startswith(l, "security-finish-timestamp=")) - dual_timestamp_deserialize(l+26, &m->security_finish_timestamp); - else if (startswith(l, "generators-start-timestamp=")) - dual_timestamp_deserialize(l+27, &m->generators_start_timestamp); - else if (startswith(l, "generators-finish-timestamp=")) - dual_timestamp_deserialize(l+28, &m->generators_finish_timestamp); - else if (startswith(l, "units-load-start-timestamp=")) - dual_timestamp_deserialize(l+27, &m->units_load_start_timestamp); - else if (startswith(l, "units-load-finish-timestamp=")) - dual_timestamp_deserialize(l+28, &m->units_load_finish_timestamp); + } else if ((val = startswith(l, "firmware-timestamp="))) + dual_timestamp_deserialize(val, &m->firmware_timestamp); + else if ((val = startswith(l, "loader-timestamp="))) + dual_timestamp_deserialize(val, &m->loader_timestamp); + else if ((val = startswith(l, "kernel-timestamp="))) + dual_timestamp_deserialize(val, &m->kernel_timestamp); + else if ((val = startswith(l, "initrd-timestamp="))) + dual_timestamp_deserialize(val, &m->initrd_timestamp); + else if ((val = startswith(l, "userspace-timestamp="))) + dual_timestamp_deserialize(val, &m->userspace_timestamp); + else if ((val = startswith(l, "finish-timestamp="))) + dual_timestamp_deserialize(val, &m->finish_timestamp); + else if ((val = startswith(l, "security-start-timestamp="))) + dual_timestamp_deserialize(val, &m->security_start_timestamp); + else if ((val = startswith(l, "security-finish-timestamp="))) + dual_timestamp_deserialize(val, &m->security_finish_timestamp); + else if ((val = startswith(l, "generators-start-timestamp="))) + dual_timestamp_deserialize(val, &m->generators_start_timestamp); + else if ((val = startswith(l, "generators-finish-timestamp="))) + dual_timestamp_deserialize(val, &m->generators_finish_timestamp); + else if ((val = startswith(l, "units-load-start-timestamp="))) + dual_timestamp_deserialize(val, &m->units_load_start_timestamp); + else if ((val = startswith(l, "units-load-finish-timestamp="))) + dual_timestamp_deserialize(val, &m->units_load_finish_timestamp); else if (startswith(l, "env=")) { _cleanup_free_ char *uce = NULL; char **e; @@ -2672,21 +2673,21 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { strv_free(m->environment); m->environment = e; - } else if (startswith(l, "notify-fd=")) { + } else if ((val = startswith(l, "notify-fd="))) { int fd; - if (safe_atoi(l + 10, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) - log_debug("Failed to parse notify fd: %s", l + 10); + if (safe_atoi(val, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) + log_debug("Failed to parse notify fd: %s", val); else { m->notify_event_source = sd_event_source_unref(m->notify_event_source); safe_close(m->notify_fd); m->notify_fd = fdset_remove(fds, fd); } - } else if (startswith(l, "notify-socket=")) { + } else if ((val = startswith(l, "notify-socket="))) { char *n; - n = strdup(l+14); + n = strdup(val); if (!n) { r = -ENOMEM; goto finish; @@ -2695,22 +2696,22 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { free(m->notify_socket); m->notify_socket = n; - } else if (startswith(l, "cgroups-agent-fd=")) { + } else if ((val = startswith(l, "cgroups-agent-fd="))) { int fd; - if (safe_atoi(l + 17, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) - log_debug("Failed to parse cgroups agent fd: %s", l + 10); + if (safe_atoi(val, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) + log_debug("Failed to parse cgroups agent fd: %s", val); else { m->cgroups_agent_event_source = sd_event_source_unref(m->cgroups_agent_event_source); safe_close(m->cgroups_agent_fd); m->cgroups_agent_fd = fdset_remove(fds, fd); } - } else if (startswith(l, "user-lookup=")) { + } else if ((val = startswith(l, "user-lookup="))) { int fd0, fd1; - if (sscanf(l + 12, "%i %i", &fd0, &fd1) != 2 || fd0 < 0 || fd1 < 0 || fd0 == fd1 || !fdset_contains(fds, fd0) || !fdset_contains(fds, fd1)) - log_debug("Failed to parse user lookup fd: %s", l + 12); + if (sscanf(val, "%i %i", &fd0, &fd1) != 2 || fd0 < 0 || fd1 < 0 || fd0 == fd1 || !fdset_contains(fds, fd0) || !fdset_contains(fds, fd1)) + log_debug("Failed to parse user lookup fd: %s", val); else { m->user_lookup_event_source = sd_event_source_unref(m->user_lookup_event_source); safe_close_pair(m->user_lookup_fds); @@ -2718,15 +2719,15 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { m->user_lookup_fds[1] = fdset_remove(fds, fd1); } - } else if (startswith(l, "dynamic-user=")) - dynamic_user_deserialize_one(m, l + 13, fds); - else if (startswith(l, "destroy-ipc-uid=")) - manager_deserialize_uid_refs_one(m, l + 16); - else if (startswith(l, "destroy-ipc-gid=")) - manager_deserialize_gid_refs_one(m, l + 16); - else if (startswith(l, "subscribed=")) { + } else if ((val = startswith(l, "dynamic-user="))) + dynamic_user_deserialize_one(m, val, fds); + else if ((val = startswith(l, "destroy-ipc-uid="))) + manager_deserialize_uid_refs_one(m, val); + else if ((val = startswith(l, "destroy-ipc-gid="))) + manager_deserialize_gid_refs_one(m, val); + else if ((val = startswith(l, "subscribed="))) { - if (strv_extend(&m->deserialized_subscribed, l+11) < 0) + if (strv_extend(&m->deserialized_subscribed, val) < 0) log_oom(); } else if (!startswith(l, "kdbus-fd=")) /* ignore this one */ diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 9927621ea0..1e17fbbb03 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -68,26 +68,25 @@ static usec_t arg_timeout = 0; */ static int parse_one_option(const char *option) { + const char *val; + int r; + assert(option); /* Handled outside of this tool */ if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail")) return 0; - if (startswith(option, "cipher=")) { - char *t; - - t = strdup(option+7); - if (!t) + if ((val = startswith(option, "cipher="))) { + r = free_and_strdup(&arg_cipher, val); + if (r < 0) return log_oom(); - free(arg_cipher); - arg_cipher = t; - - } else if (startswith(option, "size=")) { + } else if ((val = startswith(option, "size="))) { - if (safe_atou(option+5, &arg_key_size) < 0) { - log_error("size= parse failure, ignoring."); + r = safe_atou(val, &arg_key_size); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } @@ -98,68 +97,67 @@ static int parse_one_option(const char *option) { arg_key_size /= 8; - } else if (startswith(option, "key-slot=")) { + } else if ((val = startswith(option, "key-slot="))) { arg_type = CRYPT_LUKS1; - if (safe_atoi(option+9, &arg_key_slot) < 0) { - log_error("key-slot= parse failure, ignoring."); + r = safe_atoi(val, &arg_key_slot); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } - } else if (startswith(option, "tcrypt-keyfile=")) { + } else if ((val = startswith(option, "tcrypt-keyfile="))) { arg_type = CRYPT_TCRYPT; - if (path_is_absolute(option+15)) { - if (strv_extend(&arg_tcrypt_keyfiles, option + 15) < 0) + if (path_is_absolute(val)) { + if (strv_extend(&arg_tcrypt_keyfiles, val) < 0) return log_oom(); } else - log_error("Key file path '%s' is not absolute. Ignoring.", option+15); + log_error("Key file path \"%s\" is not absolute. Ignoring.", val); - } else if (startswith(option, "keyfile-size=")) { + } else if ((val = startswith(option, "keyfile-size="))) { - if (safe_atou(option+13, &arg_keyfile_size) < 0) { - log_error("keyfile-size= parse failure, ignoring."); + r = safe_atou(val, &arg_keyfile_size); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } - } else if (startswith(option, "keyfile-offset=")) { + } else if ((val = startswith(option, "keyfile-offset="))) { - if (safe_atou(option+15, &arg_keyfile_offset) < 0) { - log_error("keyfile-offset= parse failure, ignoring."); + r = safe_atou(val, &arg_keyfile_offset); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } - } else if (startswith(option, "hash=")) { - char *t; - - t = strdup(option+5); - if (!t) + } else if ((val = startswith(option, "hash="))) { + r = free_and_strdup(&arg_hash, val); + if (r < 0) return log_oom(); - free(arg_hash); - arg_hash = t; - - } else if (startswith(option, "header=")) { + } else if ((val = startswith(option, "header="))) { arg_type = CRYPT_LUKS1; - if (!path_is_absolute(option+7)) { - log_error("Header path '%s' is not absolute, refusing.", option+7); + if (!path_is_absolute(val)) { + log_error("Header path \"%s\" is not absolute, refusing.", val); return -EINVAL; } if (arg_header) { - log_error("Duplicate header= options, refusing."); + log_error("Duplicate header= option, refusing."); return -EINVAL; } - arg_header = strdup(option+7); + arg_header = strdup(val); if (!arg_header) return log_oom(); - } else if (startswith(option, "tries=")) { + } else if ((val = startswith(option, "tries="))) { - if (safe_atou(option+6, &arg_tries) < 0) { - log_error("tries= parse failure, ignoring."); + r = safe_atou(val, &arg_tries); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } @@ -181,29 +179,28 @@ static int parse_one_option(const char *option) { arg_tcrypt_system = true; } else if (STR_IN_SET(option, "plain", "swap", "tmp")) arg_type = CRYPT_PLAIN; - else if (startswith(option, "timeout=")) { + else if ((val = startswith(option, "timeout="))) { - if (parse_sec(option+8, &arg_timeout) < 0) { - log_error("timeout= parse failure, ignoring."); + r = parse_sec(val, &arg_timeout); + if (r < 0) { + log_error_errno(r, "Failed to parse %s, ignoring: %m", option); return 0; } - } else if (startswith(option, "offset=")) { + } else if ((val = startswith(option, "offset="))) { - if (safe_atou64(option+7, &arg_offset) < 0) { - log_error("offset= parse failure, refusing."); - return -EINVAL; - } + r = safe_atou64(val, &arg_offset); + if (r < 0) + return log_error_errno(r, "Failed to parse %s: %m", option); - } else if (startswith(option, "skip=")) { + } else if ((val = startswith(option, "skip="))) { - if (safe_atou64(option+5, &arg_skip) < 0) { - log_error("skip= parse failure, refusing."); - return -EINVAL; - } + r = safe_atou64(val, &arg_skip); + if (r < 0) + return log_error_errno(r, "Failed to parse %s: %m", option); } else if (!streq(option, "none")) - log_error("Encountered unknown /etc/crypttab option '%s', ignoring.", option); + log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option); return 0; } |