mirror of
https://github.com/troglobit/finit.git
synced 2026-11-30 21:18:22 +00:00
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:
1 parent
ff95ebeb91
commit
a5710125f6
2 files changed
+255
-199
No files matched your search
+1
-1
@@ -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();
|
||||||
|
|||||||
+254
-198
@@ -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,213 +320,217 @@ static void tmpfiles(char *line)
|
|||||||
strc = stat(path, &st);
|
strc = stat(path, &st);
|
||||||
|
|
||||||
// file and directory removal logic
|
// file and directory removal logic
|
||||||
switch (type[0]) {
|
if (r_flag) {
|
||||||
case 'b':
|
switch (type[0]) {
|
||||||
case 'c':
|
case 'b':
|
||||||
case 'C':
|
case 'c':
|
||||||
case 'd':
|
case 'C':
|
||||||
case 'D':
|
case 'd':
|
||||||
case 'e':
|
case 'D':
|
||||||
case 'f':
|
case 'e':
|
||||||
case 'F':
|
case 'f':
|
||||||
case 'l': /* Finit extension, like 'L' but only if target exists */
|
case 'F':
|
||||||
case 'L':
|
case 'l': /* Finit extension, like 'L' but only if target exists */
|
||||||
case 'p':
|
case 'L':
|
||||||
break;
|
case 'p':
|
||||||
case 'r':
|
break;
|
||||||
rc = glob_do(path, erase);
|
case 'r':
|
||||||
if (rc && errno == ENOENT)
|
rc = glob_do(path, erase);
|
||||||
rc = 0;
|
if (rc && errno == ENOENT)
|
||||||
break;
|
rc = 0;
|
||||||
case 'R':
|
break;
|
||||||
rc = glob_do(path, rmrf);
|
case 'R':
|
||||||
break;
|
rc = glob_do(path, rmrf);
|
||||||
case 'w':
|
break;
|
||||||
break;
|
case 'w':
|
||||||
case 'X':
|
break;
|
||||||
case 'x':
|
case 'X':
|
||||||
dbg("Unsupported x/X command, ignoring %s, no support for clean at runtime.", path);
|
case 'x':
|
||||||
break;
|
dbg("Unsupported x/X command, ignoring %s, no support for clean at runtime.", path);
|
||||||
case 'Z':
|
break;
|
||||||
case 'z':
|
case 'Z':
|
||||||
break;
|
case 'z':
|
||||||
default:
|
break;
|
||||||
errx(1, "Unsupported tmpfiles command '%s'", type);
|
default:
|
||||||
return;
|
errx(1, "Unsupported tmpfiles command '%s'", type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// file & directory creation logic
|
// file & directory creation logic
|
||||||
switch (type[0]) {
|
if (c_flag) {
|
||||||
case 'b':
|
switch (type[0]) {
|
||||||
rc = parse_mm(arg, &major, &minor);
|
case 'b':
|
||||||
if (rc)
|
rc = parse_mm(arg, &major, &minor);
|
||||||
break;
|
if (rc)
|
||||||
if (!strc) {
|
|
||||||
if (type[1] != '+')
|
|
||||||
break;
|
|
||||||
erase(path);
|
|
||||||
}
|
|
||||||
mkparent(path, 0755);
|
|
||||||
rc = blkdev(path, mode ?: 0644, major, minor);
|
|
||||||
break;
|
|
||||||
case 'c':
|
|
||||||
rc = parse_mm(arg, &major, &minor);
|
|
||||||
if (rc)
|
|
||||||
break;
|
|
||||||
if (!strc) {
|
|
||||||
if (type[1] != '+')
|
|
||||||
break;
|
|
||||||
erase(path);
|
|
||||||
}
|
|
||||||
mkparent(path, 0755);
|
|
||||||
rc = chardev(path, mode ?: 0644, major, minor);
|
|
||||||
break;
|
|
||||||
case 'C':
|
|
||||||
if (!arg) {
|
|
||||||
paste(buf, sizeof(buf), "/usr/share/factory", path);
|
|
||||||
arg = buf;
|
|
||||||
}
|
|
||||||
if (!strc) {
|
|
||||||
struct dirent **namelist;
|
|
||||||
int num;
|
|
||||||
|
|
||||||
if (!S_ISDIR(st.st_mode))
|
|
||||||
break;
|
|
||||||
num = scandir(path, &namelist, NULL, NULL);
|
|
||||||
free(namelist);
|
|
||||||
if (num >= 3)
|
|
||||||
break; /* not empty */
|
|
||||||
}
|
|
||||||
if (fisdir(arg) && !fisslashdir(arg)) {
|
|
||||||
size_t len = strlen(arg) + 2;
|
|
||||||
|
|
||||||
dst = malloc(len);
|
|
||||||
if (!dst)
|
|
||||||
break;
|
|
||||||
snprintf(dst, len, "%s/", arg);
|
|
||||||
arg = dst;
|
|
||||||
}
|
|
||||||
mkparent(path, 0755);
|
|
||||||
rc = rsync(arg, path, LITE_FOPT_KEEP_MTIME, NULL);
|
|
||||||
if (rc && errno == ENOENT)
|
|
||||||
rc = 0;
|
|
||||||
break;
|
|
||||||
case 'd':
|
|
||||||
case 'D':
|
|
||||||
mkparent(path, 0755);
|
|
||||||
rc = mksubsys(path, mode ?: 0755, user, group);
|
|
||||||
break;
|
|
||||||
case 'e':
|
|
||||||
if (glob(path, GLOB_NOESCAPE, NULL, &gl))
|
|
||||||
break;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < gl.gl_pathc; i++)
|
|
||||||
rc += mksubsys(gl.gl_pathv[i], mode ?: 0755, user, group);
|
|
||||||
break;
|
|
||||||
case 'f':
|
|
||||||
case 'F':
|
|
||||||
mkparent(path, 0755);
|
|
||||||
if (type[1] == '+' || type[0] == 'F') {
|
|
||||||
/* f+/F will create or truncate the file */
|
|
||||||
if (!arg) {
|
|
||||||
rc = create(path, mode ?: 0644, user, group);
|
|
||||||
break;
|
break;
|
||||||
|
if (!strc) {
|
||||||
|
if (type[1] != '+')
|
||||||
|
break;
|
||||||
|
erase(path);
|
||||||
}
|
}
|
||||||
fp = fopen(path, "w+");
|
mkparent(path, 0755);
|
||||||
} else {
|
rc = blkdev(path, mode ?: 0644, major, minor);
|
||||||
/* f will create the file if it doesn't exist */
|
|
||||||
if (strc)
|
|
||||||
fp = fopen(path, "w");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fp) {
|
|
||||||
write_arg(fp, arg);
|
|
||||||
rc = fclose(fp);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'l': /* Finit extension, like 'L' but only if target exists */
|
|
||||||
if (!arg) {
|
|
||||||
paste(buf, sizeof(buf), "/usr/share/factory", path);
|
|
||||||
if (stat(buf, &ast))
|
|
||||||
break;
|
|
||||||
} else if (arg[0] != '/') {
|
|
||||||
char *tmp;
|
|
||||||
|
|
||||||
tmp = dirname(strdupa(path));
|
|
||||||
paste(buf, sizeof(buf), tmp, arg);
|
|
||||||
dst = realpath(buf, NULL);
|
|
||||||
if (!dst)
|
|
||||||
break;
|
|
||||||
if (stat(dst, &ast))
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
if (stat(arg, &ast))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
/* fallthrough */
|
|
||||||
case 'L':
|
|
||||||
if (!strc) {
|
|
||||||
if (type[1] != '+')
|
|
||||||
break;
|
|
||||||
rmrf(path);
|
|
||||||
}
|
|
||||||
mkparent(path, 0755);
|
|
||||||
if (!arg) {
|
|
||||||
paste(buf, sizeof(buf), "/usr/share/factory", path);
|
|
||||||
arg = buf;
|
|
||||||
}
|
|
||||||
rc = ln(arg, path);
|
|
||||||
if (rc && errno == EEXIST)
|
|
||||||
rc = 0;
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
if (!strc) {
|
|
||||||
if (type[1] != '+')
|
|
||||||
break;
|
|
||||||
erase(path);
|
|
||||||
}
|
|
||||||
mkparent(path, 0755);
|
|
||||||
rc = mkfifo(path, mode ?: 0644);
|
|
||||||
break;
|
|
||||||
case 'r':
|
|
||||||
case 'R':
|
|
||||||
break;
|
|
||||||
case 'w':
|
|
||||||
if (!arg)
|
|
||||||
break;
|
break;
|
||||||
|
case 'c':
|
||||||
if (glob(path, GLOB_NOESCAPE, NULL, &gl))
|
rc = parse_mm(arg, &major, &minor);
|
||||||
|
if (rc)
|
||||||
|
break;
|
||||||
|
if (!strc) {
|
||||||
|
if (type[1] != '+')
|
||||||
|
break;
|
||||||
|
erase(path);
|
||||||
|
}
|
||||||
|
mkparent(path, 0755);
|
||||||
|
rc = chardev(path, mode ?: 0644, major, minor);
|
||||||
break;
|
break;
|
||||||
|
case 'C':
|
||||||
|
if (!arg) {
|
||||||
|
paste(buf, sizeof(buf), "/usr/share/factory", path);
|
||||||
|
arg = buf;
|
||||||
|
}
|
||||||
|
if (!strc) {
|
||||||
|
struct dirent **namelist;
|
||||||
|
int num;
|
||||||
|
|
||||||
|
if (!S_ISDIR(st.st_mode))
|
||||||
|
break;
|
||||||
|
num = scandir(path, &namelist, NULL, NULL);
|
||||||
|
free(namelist);
|
||||||
|
if (num >= 3)
|
||||||
|
break; /* not empty */
|
||||||
|
}
|
||||||
|
if (fisdir(arg) && !fisslashdir(arg)) {
|
||||||
|
size_t len = strlen(arg) + 2;
|
||||||
|
|
||||||
|
dst = malloc(len);
|
||||||
|
if (!dst)
|
||||||
|
break;
|
||||||
|
snprintf(dst, len, "%s/", arg);
|
||||||
|
arg = dst;
|
||||||
|
}
|
||||||
|
mkparent(path, 0755);
|
||||||
|
rc = rsync(arg, path, LITE_FOPT_KEEP_MTIME, NULL);
|
||||||
|
if (rc && errno == ENOENT)
|
||||||
|
rc = 0;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
case 'D':
|
||||||
|
mkparent(path, 0755);
|
||||||
|
rc = mksubsys(path, mode ?: 0755, user, group);
|
||||||
|
break;
|
||||||
|
case 'e':
|
||||||
|
if (glob(path, GLOB_NOESCAPE, NULL, &gl))
|
||||||
|
break;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < gl.gl_pathc; i++)
|
||||||
|
rc += mksubsys(gl.gl_pathv[i], mode ?: 0755, user, group);
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
case 'F':
|
||||||
|
mkparent(path, 0755);
|
||||||
|
if (type[1] == '+' || type[0] == 'F') {
|
||||||
|
/* f+/F will create or truncate the file */
|
||||||
|
if (!arg) {
|
||||||
|
rc = create(path, mode ?: 0644, user, group);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
fp = fopen(path, "w+");
|
||||||
|
} else {
|
||||||
|
/* f will create the file if it doesn't exist */
|
||||||
|
if (strc)
|
||||||
|
fp = fopen(path, "w");
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < gl.gl_pathc; i++) {
|
|
||||||
fp = fopen(gl.gl_pathv[i], type[1] == '+' ? "a" : "w");
|
|
||||||
if (fp) {
|
if (fp) {
|
||||||
write_arg(fp, arg);
|
write_arg(fp, arg);
|
||||||
rc = fclose(fp);
|
rc = fclose(fp);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'X':
|
|
||||||
case 'x':
|
|
||||||
dbg("Unsupported x/X command, ignoring %s, no support for clean at runtime.", path);
|
|
||||||
break;
|
|
||||||
case 'Z':
|
|
||||||
opts = "-R";
|
|
||||||
/* fallthrough */
|
|
||||||
case 'z':
|
|
||||||
if (!whichp("restorecon"))
|
|
||||||
break;
|
|
||||||
if (glob(path, GLOB_NOESCAPE, NULL, &gl))
|
|
||||||
break;
|
break;
|
||||||
|
case 'l': /* Finit extension, like 'L' but only if target exists */
|
||||||
|
if (!arg) {
|
||||||
|
paste(buf, sizeof(buf), "/usr/share/factory", path);
|
||||||
|
if (stat(buf, &ast))
|
||||||
|
break;
|
||||||
|
} else if (arg[0] != '/') {
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
for (size_t i = 0; i < gl.gl_pathc; i++) {
|
tmp = dirname(strdupa(path));
|
||||||
snprintf(buf, sizeof(buf), "restorecon %s %s", opts, gl.gl_pathv[i]);
|
paste(buf, sizeof(buf), tmp, arg);
|
||||||
run(buf, "restorecon");
|
dst = realpath(buf, NULL);
|
||||||
|
if (!dst)
|
||||||
|
break;
|
||||||
|
if (stat(dst, &ast))
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
if (stat(arg, &ast))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* fallthrough */
|
||||||
|
case 'L':
|
||||||
|
if (!strc) {
|
||||||
|
if (type[1] != '+')
|
||||||
|
break;
|
||||||
|
rmrf(path);
|
||||||
|
}
|
||||||
|
mkparent(path, 0755);
|
||||||
|
if (!arg) {
|
||||||
|
paste(buf, sizeof(buf), "/usr/share/factory", path);
|
||||||
|
arg = buf;
|
||||||
|
}
|
||||||
|
rc = ln(arg, path);
|
||||||
|
if (rc && errno == EEXIST)
|
||||||
|
rc = 0;
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
if (!strc) {
|
||||||
|
if (type[1] != '+')
|
||||||
|
break;
|
||||||
|
erase(path);
|
||||||
|
}
|
||||||
|
mkparent(path, 0755);
|
||||||
|
rc = mkfifo(path, mode ?: 0644);
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
case 'R':
|
||||||
|
break;
|
||||||
|
case 'w':
|
||||||
|
if (!arg)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (glob(path, GLOB_NOESCAPE, NULL, &gl))
|
||||||
|
break;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < gl.gl_pathc; i++) {
|
||||||
|
fp = fopen(gl.gl_pathv[i], type[1] == '+' ? "a" : "w");
|
||||||
|
if (fp) {
|
||||||
|
write_arg(fp, arg);
|
||||||
|
rc = fclose(fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'X':
|
||||||
|
case 'x':
|
||||||
|
dbg("Unsupported x/X command, ignoring %s, no support for clean at runtime.", path);
|
||||||
|
break;
|
||||||
|
case 'Z':
|
||||||
|
opts = "-R";
|
||||||
|
/* fallthrough */
|
||||||
|
case 'z':
|
||||||
|
if (!whichp("restorecon"))
|
||||||
|
break;
|
||||||
|
if (glob(path, GLOB_NOESCAPE, NULL, &gl))
|
||||||
|
break;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < gl.gl_pathc; i++) {
|
||||||
|
snprintf(buf, sizeof(buf), "restorecon %s %s", opts, gl.gl_pathv[i]);
|
||||||
|
run(buf, "restorecon");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
errx(1, "Unsupported tmpfiles command '%s'", type);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
default:
|
|
||||||
errx(1, "Unsupported tmpfiles command '%s'", type);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dst)
|
if (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)
|
||||||
* Only the three last tmpfiles.d/ directories are defined in
|
{
|
||||||
* tmpfiles.d(5) as system search paths. Finit adds two more
|
fprintf(stderr,
|
||||||
* before that to have Finit specific ones sorted first, and
|
"Usage: tmpfiles [COMMAND...]\n"
|
||||||
* a configure prefix specific one after that for user needs.
|
"\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[])
|
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
|
||||||
|
* tmpfiles.d(5) as system search paths. Finit adds two more
|
||||||
|
* before that to have Finit specific ones sorted first, and
|
||||||
|
* a configure prefix specific one after that for user needs.
|
||||||
|
*/
|
||||||
|
|
||||||
/* in priority order */
|
/* in priority order */
|
||||||
char *dir[] = {
|
char *dir[] = {
|
||||||
FINIT_TMPFILES "/*.conf",
|
FINIT_TMPFILES "/*.conf",
|
||||||
|
|||||||
Reference in new issue
Block a user