mirror of
https://github.com/troglobit/finit.git
synced 2026-10-01 05:22:48 +07:00
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:
+10
-15
@@ -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);
|
||||
|
||||
+27
-14
@@ -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;
|
||||
}
|
||||
|
||||
+4
-1
@@ -45,7 +45,10 @@ extern char *prognm;
|
||||
#include <lite/conio.h>
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user