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 <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2021-03-18 18:25:11 +01:00
1 parent 74d715243c
commit 6e24f2ff13
3 files changed
+41 -30

No files matched your search

+10 -15
View File
@@ -41,24 +41,18 @@ static uev_t cgw;
static void group_init(char *path, int leaf, int weight) static void group_init(char *path, int leaf, int weight)
{ {
char file[strlen(path) + 64];
if (mkdir(path, 0755) && EEXIST != errno) { if (mkdir(path, 0755) && EEXIST != errno) {
_pe("Failed creating cgroup %s", path); _pe("Failed creating cgroup %s", path);
return; return;
} }
/* enable detected controllers on domain groups */ /* enable detected controllers on domain groups */
if (!leaf) { if (!leaf)
paste(file, sizeof(file), path, "cgroup.subtree_control"); fnwrite(controllers, "%s/cgroup.subtree_control", path);
echo(file, 0, controllers);
}
/* set initial group weight */ /* set initial group weight */
if (weight) { if (weight)
paste(file, sizeof(file), path, "cpu.weight"); fnwrite(str("%d", weight), "%s/cgroup.subtree_control", path);
echo(file, 0, "%d", weight);
}
} }
static int cgroup_leaf_init(char *group, char *name, int pid) 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; return 1;
} }
/* create and initialize new group */
snprintf(path, sizeof(path), "/sys/fs/cgroup/%s/%s", group, name); snprintf(path, sizeof(path), "/sys/fs/cgroup/%s/%s", group, name);
group_init(path, 1, 0); group_init(path, 1, 0);
strlcat(path, "/cgroup.procs", sizeof(path)); /* move process to new group */
echo(path, 0, "%d", pid); 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); return iwatch_add(&iw_cgroup, path, 0);
} }
@@ -207,7 +202,7 @@ void cgroup_init(uev_ctx_t *ctx)
fclose(fp); fclose(fp);
/* Enable all controllers */ /* 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 */ /* Default groups, PID 1, services, and user/login processes */
group_init(FINIT_CGPATH "/init", 1, 100); 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); group_init(FINIT_CGPATH "/system", 0, 9800);
/* Move ourselves to init */ /* Move ourselves to init */
echo(FINIT_CGPATH "/init/cgroup.procs", 0, "1"); fnwrite("1", FINIT_CGPATH "/init/cgroup.procs");
/* prepare cgroup.events watcher */ /* prepare cgroup.events watcher */
fd = iwatch_init(&iw_cgroup); fd = iwatch_init(&iw_cgroup);
+27 -14
View File
@@ -56,28 +56,41 @@ char *progname(char *arg0)
return prognm; 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; va_list ap;
FILE *fp; FILE *fp;
if (!file) if (!value) {
fp = stdout; errno = EINVAL;
else return -1;
fp = fopen(file, append ? "a" : "w"); }
va_start(ap, fmt);
vsnprintf(path, sizeof(path), fmt, ap);
va_end(ap);
fp = fopen(path, "w");
if (!fp) if (!fp)
return -1; return -1;
if (fmt) {
va_start(ap, fmt);
vfprintf(fp, fmt, ap);
va_end(ap);
}
/* echo(1) always adds a newline */ /* echo(1) always adds a newline */
fprintf(fp, "\n"); fputs(value, fp);
if (file) fputs("\n", fp);
fclose(fp); fclose(fp);
return 0; return 0;
} }
+4 -1
View File
@@ -45,7 +45,10 @@ extern char *prognm;
#include <lite/conio.h> #include <lite/conio.h>
char *progname (char *arg0); 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 has_utmp (void);
int strtobytes (char *arg); int strtobytes (char *arg);
void do_sleep (unsigned int sec); void do_sleep (unsigned int sec);