Skip to content

Commit 22bfd3a

Browse files
committed
librc: clear dirfds when creating new svcdir
if for whatever reason, /run/openrc exists in the rootfs before first boot, we'd still end up with an invalid fd. while users are not really supposed to have anything in /run (under the tmpfs mount), should they do, it's better to not fail clearing the dirfds as we create svcdir also means we don't need a new librc api function to clear it, for now at least.
1 parent 25d9331 commit 22bfd3a

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/librc/librc-depend.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,9 @@ rc_deptree_update_needed(time_t *newest, char *file)
693693

694694
/* Quick test to see if anything we use has changed and we have
695695
* data in our deptree. */
696-
if (mkdir(rc_svcdir(), 0755) != 0 && errno != EEXIST)
696+
if (mkdir(rc_svcdir(), 0755) == 0)
697+
clear_dirfds(); /* clear our cached dirfds if we created a new svcdir, as a sanity check */
698+
else if (errno != EEXIST)
697699
fprintf(stderr, "mkdir '%s': %s\n", rc_svcdir(), strerror(errno));
698700

699701
if (fstatat(rc_dirfd(RC_DIR_SVCDIR), "deptree", &buf, 0) == 0) {

src/librc/librc.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ int rc_dirfd(enum rc_dir dir) {
8080
return dirfds[dir];
8181
}
8282

83+
void clear_dirfds(void) {
84+
for (size_t i = 0; i < RC_DIR_MAX; i++) {
85+
/* dirfd is zero-initialized, though zero is a valid
86+
* FD, dirfd should never contain FDs which are <= 2. */
87+
if (dirfds[i] <= 0)
88+
continue;
89+
90+
close(dirfds[i]);
91+
dirfds[i] = -1;
92+
}
93+
}
94+
8395
#define LS_INITD 0x01
8496
#define LS_DIR 0x02
8597
static RC_STRINGLIST *
@@ -633,15 +645,7 @@ rc_set_user(void)
633645
rc_dirs.scriptdirs[SCRIPTDIR_USR] = rc_dirs.usrconfdir;
634646
rc_dirs.scriptdirs[SCRIPTDIR_SVC] = rc_dirs.svcdir;
635647

636-
for (size_t i = 0; i < RC_DIR_MAX; i++) {
637-
/* dirfd is zero-initialized, though zero is a valid
638-
* FD, dirfd should never contain FDs which are <= 2. */
639-
if (dirfds[i] <= 0)
640-
continue;
641-
642-
close(dirfds[i]);
643-
dirfds[i] = -1;
644-
}
648+
clear_dirfds();
645649
}
646650

647651
const char * const *

src/librc/librc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,6 @@ static const char *const dirnames[RC_DIR_SYS_MAX] =
7373
};
7474

7575
RC_STRINGLIST *config_list(int dirfd, const char *pathname);
76+
void clear_dirfds(void);
7677

7778
#endif

0 commit comments

Comments
 (0)