util: various optimizations, using join()
This commit is contained in:
parent
911a4828e0
commit
70132bd042
|
@ -482,13 +482,26 @@ finish:
|
||||||
|
|
||||||
int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
|
int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
|
||||||
const char *p;
|
const char *p;
|
||||||
char *mp;
|
char *t;
|
||||||
int r;
|
|
||||||
static __thread bool good = false;
|
static __thread bool good = false;
|
||||||
|
|
||||||
assert(controller);
|
assert(controller);
|
||||||
assert(fs);
|
assert(fs);
|
||||||
|
|
||||||
|
if (!good) {
|
||||||
|
int r;
|
||||||
|
|
||||||
|
r = path_is_mount_point("/sys/fs/cgroup");
|
||||||
|
if (r <= 0)
|
||||||
|
return r < 0 ? r : -ENOENT;
|
||||||
|
|
||||||
|
/* Cache this to save a few stat()s */
|
||||||
|
good = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isempty(controller))
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
/* This is a very minimal lookup from controller names to
|
/* This is a very minimal lookup from controller names to
|
||||||
* paths. Since we have mounted most hierarchies ourselves
|
* paths. Since we have mounted most hierarchies ourselves
|
||||||
* should be kinda safe, but eventually we might want to
|
* should be kinda safe, but eventually we might want to
|
||||||
|
@ -502,34 +515,22 @@ int cg_get_path(const char *controller, const char *path, const char *suffix, ch
|
||||||
else
|
else
|
||||||
p = controller;
|
p = controller;
|
||||||
|
|
||||||
if (asprintf(&mp, "/sys/fs/cgroup/%s", p) < 0)
|
if (path && suffix)
|
||||||
|
t = join("/sys/fs/cgroup/", p, "/", path, "/", suffix, NULL);
|
||||||
|
else if (path)
|
||||||
|
t = join("/sys/fs/cgroup/", p, "/", path, NULL);
|
||||||
|
else if (suffix)
|
||||||
|
t = join("/sys/fs/cgroup/", p, "/", suffix, NULL);
|
||||||
|
else
|
||||||
|
t = join("/sys/fs/cgroup/", p, NULL);
|
||||||
|
|
||||||
|
if (!t)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
if (!good) {
|
path_kill_slashes(t);
|
||||||
if ((r = path_is_mount_point(mp)) <= 0) {
|
|
||||||
free(mp);
|
|
||||||
return r < 0 ? r : -ENOENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Cache this to save a few stat()s */
|
*fs = t;
|
||||||
good = true;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
if (path && suffix)
|
|
||||||
r = asprintf(fs, "%s/%s/%s", mp, path, suffix);
|
|
||||||
else if (path)
|
|
||||||
r = asprintf(fs, "%s/%s", mp, path);
|
|
||||||
else if (suffix)
|
|
||||||
r = asprintf(fs, "%s/%s", mp, suffix);
|
|
||||||
else {
|
|
||||||
path_kill_slashes(mp);
|
|
||||||
*fs = mp;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(mp);
|
|
||||||
path_kill_slashes(*fs);
|
|
||||||
return r < 0 ? -ENOMEM : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int cg_trim(const char *controller, const char *path, bool delete_root) {
|
int cg_trim(const char *controller, const char *path, bool delete_root) {
|
||||||
|
|
|
@ -2988,7 +2988,8 @@ static int service_enumerate(Manager *m) {
|
||||||
|
|
||||||
free(path);
|
free(path);
|
||||||
path = NULL;
|
path = NULL;
|
||||||
if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
|
path = join(*p, "/", rcnd_table[i].path, NULL);
|
||||||
|
if (!path) {
|
||||||
r = -ENOMEM;
|
r = -ENOMEM;
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,8 +167,6 @@ char *unit_name_change_suffix(const char *n, const char *suffix) {
|
||||||
}
|
}
|
||||||
|
|
||||||
char *unit_name_build(const char *prefix, const char *instance, const char *suffix) {
|
char *unit_name_build(const char *prefix, const char *instance, const char *suffix) {
|
||||||
char *r;
|
|
||||||
|
|
||||||
assert(prefix);
|
assert(prefix);
|
||||||
assert(unit_prefix_is_valid(prefix));
|
assert(unit_prefix_is_valid(prefix));
|
||||||
assert(!instance || unit_instance_is_valid(instance));
|
assert(!instance || unit_instance_is_valid(instance));
|
||||||
|
@ -177,10 +175,7 @@ char *unit_name_build(const char *prefix, const char *instance, const char *suff
|
||||||
if (!instance)
|
if (!instance)
|
||||||
return strappend(prefix, suffix);
|
return strappend(prefix, suffix);
|
||||||
|
|
||||||
if (asprintf(&r, "%s@%s%s", prefix, instance, suffix) < 0)
|
return join(prefix, "@", instance, suffix, NULL);
|
||||||
return NULL;
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static char* do_escape(const char *f, char *t) {
|
static char* do_escape(const char *f, char *t) {
|
||||||
|
|
Loading…
Reference in a new issue