Skip to content

Commit 5262448

Browse files
committed
Ensure that standard file descriptors are open
- this will avoid unwillingly using fd in the 0..2 range - closes: #185
1 parent 231c23f commit 5262448

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/udev/udevd.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,22 @@ int main(int argc, char *argv[]) {
11351135
struct epoll_event ep_worker = { .events = EPOLLIN };
11361136
int r = 0, one = 1;
11371137

1138+
/*
1139+
* Ensure no file descriptor unwillingly falls in the 0..2 range
1140+
* Warning: DO NOT OPEN FILES BEFORE THE 3 OPENS BELOW
1141+
*/
1142+
int fd_stdi = open("/dev/null", O_RDWR);
1143+
int fd_stdo = open("/dev/null", O_RDWR);
1144+
int fd_stde = open("/dev/null", O_RDWR);
1145+
1146+
/* Close only the ones that are safe to close */
1147+
if (fd_stdi > STDERR_FILENO)
1148+
close(fd_stdi);
1149+
if (fd_stdo > STDERR_FILENO)
1150+
close(fd_stdo);
1151+
if (fd_stde > STDERR_FILENO)
1152+
close(fd_stde);
1153+
11381154
udev = udev_new();
11391155
if (!udev) {
11401156
r = log_error_errno(errno, "could not allocate udev context: %m");

0 commit comments

Comments
 (0)