Ensure that standard file descriptors are open

- this will avoid unwillingly using fd in the 0..2 range
- closes: #185
This commit is contained in:
Boian Bonev 2023-10-03 22:41:28 +03:00
parent 231c23fbbc
commit 5262448fd9
No known key found for this signature in database
GPG Key ID: 1365720913D2F22D

View File

@ -1135,6 +1135,22 @@ int main(int argc, char *argv[]) {
struct epoll_event ep_worker = { .events = EPOLLIN };
int r = 0, one = 1;
/*
* Ensure no file descriptor unwillingly falls in the 0..2 range
* Warning: DO NOT OPEN FILES BEFORE THE 3 OPENS BELOW
*/
int fd_stdi = open("/dev/null", O_RDWR);
int fd_stdo = open("/dev/null", O_RDWR);
int fd_stde = open("/dev/null", O_RDWR);
/* Close only the ones that are safe to close */
if (fd_stdi > STDERR_FILENO)
close(fd_stdi);
if (fd_stdo > STDERR_FILENO)
close(fd_stdo);
if (fd_stde > STDERR_FILENO)
close(fd_stde);
udev = udev_new();
if (!udev) {
r = log_error_errno(errno, "could not allocate udev context: %m");