tmpfiles: add --create and --remove flags

Allow execution of tmpfiles to specify which mode(s) of operation to run in.
This commit is contained in:
Aaron Andersen committed 2025-06-02 19:27:24 -04:00
1 parent ff95ebeb91
commit a5710125f6
2 files changed
+59 -3

No files matched your search

+1 -1
View File
@@ -162,7 +162,7 @@ static void setup(void *arg)
kernel_links(); kernel_links();
/* Create all system tmpfiles.d(5) */ /* Create all system tmpfiles.d(5) */
run_interactive(_PATH_TMPFILES, "Creating required directories and files"); run_interactive(_PATH_TMPFILES " --create", "Creating required directories and files");
/* Set BOOT_TIME UTMP entry */ /* Set BOOT_TIME UTMP entry */
utmp_set_boot(); utmp_set_boot();
+58 -2
View File
@@ -25,6 +25,7 @@
#include <dirent.h> #include <dirent.h>
#include <ftw.h> #include <ftw.h>
#include <getopt.h>
#include <glob.h> #include <glob.h>
#include <libgen.h> #include <libgen.h>
#include <grp.h> #include <grp.h>
@@ -65,6 +66,9 @@ int prevlevel;
int cmdlevel; int cmdlevel;
int kerndebug; int kerndebug;
int c_flag = 0;
int r_flag = 0;
static int glob_do(const char *path, int (*cb)(const char *)) static int glob_do(const char *path, int (*cb)(const char *))
{ {
int rc = 0; int rc = 0;
@@ -316,6 +320,7 @@ static void tmpfiles(char *line)
strc = stat(path, &st); strc = stat(path, &st);
// file and directory removal logic // file and directory removal logic
if (r_flag) {
switch (type[0]) { switch (type[0]) {
case 'b': case 'b':
case 'c': case 'c':
@@ -350,8 +355,10 @@ static void tmpfiles(char *line)
errx(1, "Unsupported tmpfiles command '%s'", type); errx(1, "Unsupported tmpfiles command '%s'", type);
return; return;
} }
}
// file & directory creation logic // file & directory creation logic
if (c_flag) {
switch (type[0]) { switch (type[0]) {
case 'b': case 'b':
rc = parse_mm(arg, &major, &minor); rc = parse_mm(arg, &major, &minor);
@@ -524,6 +531,7 @@ static void tmpfiles(char *line)
errx(1, "Unsupported tmpfiles command '%s'", type); errx(1, "Unsupported tmpfiles command '%s'", type);
return; return;
} }
}
if (dst) if (dst)
free(dst); free(dst);
@@ -532,14 +540,62 @@ static void tmpfiles(char *line)
warn("Failed %s operation on path %s", type, path); warn("Failed %s operation on path %s", type, path);
} }
static int usage(int rc)
{
fprintf(stderr,
"Usage: tmpfiles [COMMAND...]\n"
"\n"
"Commands:\n"
" -c, --create Create files and directories\n"
" -r, --remove Remove files and directories marked for removal\n"
" -h, --help This help text\n"
"\n");
return rc;
}
int main(int argc, char *argv[])
{
struct option long_options[] = {
{ "create", 0, NULL, 'c' },
{ "remove", 0, NULL, 'r' },
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
int c;
while ((c = getopt_long(argc, argv, "crh?", long_options, NULL)) != EOF) {
switch(c) {
case 'c':
c_flag = 1;
break;
case 'r':
r_flag = 1;
break;
case 'h':
case '?':
return usage(0);
default:
return usage(1);
}
}
if (c_flag + r_flag == 0) {
fprintf(stderr, "You need to specify at least one of --create or --remove.\n");
return 1;
}
/* /*
* Only the three last tmpfiles.d/ directories are defined in * Only the three last tmpfiles.d/ directories are defined in
* tmpfiles.d(5) as system search paths. Finit adds two more * tmpfiles.d(5) as system search paths. Finit adds two more
* before that to have Finit specific ones sorted first, and * before that to have Finit specific ones sorted first, and
* a configure prefix specific one after that for user needs. * a configure prefix specific one after that for user needs.
*/ */
int main(int argc, char *argv[])
{
/* in priority order */ /* in priority order */
char *dir[] = { char *dir[] = {
FINIT_TMPFILES "/*.conf", FINIT_TMPFILES "/*.conf",