Skip to content

Commit c9fd441

Browse files
committed
tmpfiles: fix f/F to apply ownership when writing content
When f or F types write content to a file, the mode and ownership specified in the config should be applied. Previously, ownership was only applied when create() was used (i.e., when no argument was specified). Now we explicitly apply mode and ownership after writing content to the file.
1 parent 7206f74 commit c9fd441

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/tmpfiles.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,6 @@ static void tmpfiles(char *line)
601601
mkparent(path, 0755);
602602
if (type[1] == '+' || type[0] == 'F') {
603603
/* f+/F will create or truncate the file */
604-
if (!arg) {
605-
rc = create(path, mode ?: 0644, user, group);
606-
break;
607-
}
608604
fp = fopen(path, "w+");
609605
} else {
610606
/* f will create the file if it doesn't exist */
@@ -613,8 +609,20 @@ static void tmpfiles(char *line)
613609
}
614610

615611
if (fp) {
612+
int uid, gid;
613+
616614
write_arg(fp, arg);
617615
rc = fclose(fp);
616+
617+
/* Apply mode and ownership */
618+
if (mode)
619+
chmod(path, mode);
620+
uid = parse_uid(user);
621+
gid = parse_gid(group);
622+
if (gid < 0)
623+
gid = 0;
624+
if (uid >= 0 && chown(path, uid, gid))
625+
warn("Failed chown(%s, %d, %d)", path, uid, gid);
618626
}
619627
break;
620628
case 'l': /* Finit extension, like 'L' but only if target exists */

0 commit comments

Comments
 (0)