util: use extract_first_word() instead of strsep()

This commit is contained in:
Yu Watanabe 2019-06-20 06:51:34 +09:00
parent fe2e4b6961
commit 31a9be2372
2 changed files with 36 additions and 22 deletions

View File

@ -91,10 +91,9 @@ int cg_read_event(
const char *controller,
const char *path,
const char *event,
char **val) {
char **ret) {
_cleanup_free_ char *events = NULL, *content = NULL;
char *p, *line;
int r;
r = cg_get_path(controller, path, "cgroup.events", &events);
@ -105,22 +104,33 @@ int cg_read_event(
if (r < 0)
return r;
p = content;
while ((line = strsep(&p, "\n"))) {
char *key;
for (const char *p = content;;) {
_cleanup_free_ char *line = NULL, *key = NULL, *val = NULL;
const char *q;
key = strsep(&line, " ");
if (!key || !line)
r = extract_first_word(&p, &line, "\n", 0);
if (r < 0)
return r;
if (r == 0)
return -ENOENT;
q = line;
r = extract_first_word(&q, &key, " ", 0);
if (r < 0)
return r;
if (r == 0)
return -EINVAL;
if (strcmp(key, event))
if (!streq(key, event))
continue;
*val = strdup(line);
val = strdup(q);
if (!val)
return -ENOMEM;
*ret = TAKE_PTR(val);
return 0;
}
return -ENOENT;
}
bool cg_ns_supported(void) {

View File

@ -198,7 +198,6 @@ static int detect_vm_xen(void) {
/* Returns -errno, or 0 for domU, or 1 for dom0 */
static int detect_vm_xen_dom0(void) {
_cleanup_free_ char *domcap = NULL;
char *cap, *i;
int r;
r = read_one_line_file(PATH_FEATURES, &domcap);
@ -229,17 +228,22 @@ static int detect_vm_xen_dom0(void) {
if (r < 0)
return r;
i = domcap;
while ((cap = strsep(&i, ",")))
if (streq(cap, "control_d"))
break;
if (!cap) {
log_debug("Virtualization XEN DomU found (/proc/xen/capabilities)");
return 0;
}
for (const char *i = domcap;;) {
_cleanup_free_ char *cap = NULL;
log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
return 1;
r = extract_first_word(&i, &cap, ",", 0);
if (r < 0)
return r;
if (r == 0) {
log_debug("Virtualization XEN DomU found (/proc/xen/capabilities)");
return 0;
}
if (streq(cap, "control_d")) {
log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
return 1;
}
}
}
static int detect_vm_hypervisor(void) {