From 6e24f2ff13810fbd79e2c84d0284e30cde20b582 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 18 Mar 2021 14:32:55 +0100 Subject: [PATCH] echo(): refactor and rename helper function -> fnwrite() More often than not, the file to write to in sysfs changes rather than the value. This patch changes echo() into a fnwrite(), flipping what is vsnprintf()'ed, and adds a stupid str() function that converts any value (float/int/double/uint64_t) to a static string buffer. Signed-off-by: Joachim Wiberg --- src/cgroup.c | 25 ++++++++++--------------- src/util.c | 41 +++++++++++++++++++++++++++-------------- src/util.h | 5 ++++- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/cgroup.c b/src/cgroup.c index d32323f3..ac77f2ea 100644 --- a/src/cgroup.c +++ b/src/cgroup.c @@ -41,24 +41,18 @@ static uev_t cgw; static void group_init(char *path, int leaf, int weight) { - char file[strlen(path) + 64]; - if (mkdir(path, 0755) && EEXIST != errno) { _pe("Failed creating cgroup %s", path); return; } /* enable detected controllers on domain groups */ - if (!leaf) { - paste(file, sizeof(file), path, "cgroup.subtree_control"); - echo(file, 0, controllers); - } + if (!leaf) + fnwrite(controllers, "%s/cgroup.subtree_control", path); /* set initial group weight */ - if (weight) { - paste(file, sizeof(file), path, "cpu.weight"); - echo(file, 0, "%d", weight); - } + if (weight) + fnwrite(str("%d", weight), "%s/cgroup.subtree_control", path); } static int cgroup_leaf_init(char *group, char *name, int pid) @@ -70,13 +64,14 @@ static int cgroup_leaf_init(char *group, char *name, int pid) return 1; } + /* create and initialize new group */ snprintf(path, sizeof(path), "/sys/fs/cgroup/%s/%s", group, name); group_init(path, 1, 0); - strlcat(path, "/cgroup.procs", sizeof(path)); - echo(path, 0, "%d", pid); + /* move process to new group */ + fnwrite(str("%d", pid), "%s/cgroup.procs", path); - snprintf(path, sizeof(path), "/sys/fs/cgroup/%s/%s/cgroup.events", group, name); + strlcat(path, "/cgroup.events", sizeof(path)); return iwatch_add(&iw_cgroup, path, 0); } @@ -207,7 +202,7 @@ void cgroup_init(uev_ctx_t *ctx) fclose(fp); /* Enable all controllers */ - echo(FINIT_CGPATH "/cgroup.subtree_control", 0, controllers); + fnwrite(controllers, FINIT_CGPATH "/cgroup.subtree_control"); /* Default groups, PID 1, services, and user/login processes */ group_init(FINIT_CGPATH "/init", 1, 100); @@ -215,7 +210,7 @@ void cgroup_init(uev_ctx_t *ctx) group_init(FINIT_CGPATH "/system", 0, 9800); /* Move ourselves to init */ - echo(FINIT_CGPATH "/init/cgroup.procs", 0, "1"); + fnwrite("1", FINIT_CGPATH "/init/cgroup.procs"); /* prepare cgroup.events watcher */ fd = iwatch_init(&iw_cgroup); diff --git a/src/util.c b/src/util.c index ac9b4926..52eb381f 100644 --- a/src/util.c +++ b/src/util.c @@ -56,28 +56,41 @@ char *progname(char *arg0) return prognm; } -int echo(char *file, int append, char *fmt, ...) +char *str(char *fmt, ...) { + static char buf[32]; + va_list ap; + + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + + return buf; +} + +int fnwrite(char *value, char *fmt, ...) +{ + char path[256]; va_list ap; FILE *fp; - if (!file) - fp = stdout; - else - fp = fopen(file, append ? "a" : "w"); + if (!value) { + errno = EINVAL; + return -1; + } + + va_start(ap, fmt); + vsnprintf(path, sizeof(path), fmt, ap); + va_end(ap); + + fp = fopen(path, "w"); if (!fp) return -1; - if (fmt) { - va_start(ap, fmt); - vfprintf(fp, fmt, ap); - va_end(ap); - } - /* echo(1) always adds a newline */ - fprintf(fp, "\n"); - if (file) - fclose(fp); + fputs(value, fp); + fputs("\n", fp); + fclose(fp); return 0; } diff --git a/src/util.h b/src/util.h index 93db68e0..17f15c7b 100644 --- a/src/util.h +++ b/src/util.h @@ -45,7 +45,10 @@ extern char *prognm; #include char *progname (char *arg0); -int echo (char *file, int append, char *fmt, ...); + +char *str (char *fmt, ...); +int fnwrite (char *value, char *fmt, ...); + int has_utmp (void); int strtobytes (char *arg); void do_sleep (unsigned int sec);