Skip to content

Commit

Permalink
global: use dev_null_fd instead of opening /dev/null
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen committed Nov 16, 2016
1 parent f3fc24f commit 2481c30
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 43 deletions.
32 changes: 9 additions & 23 deletions src/lib-program-client/program-client-local.c
Expand Up @@ -52,40 +52,26 @@ void exec_child(const char *bin_path, const char *const *args, const char *const

/* Setup stdin/stdout */

if (in_fd < 0) {
in_fd = open("/dev/null", O_RDONLY);

if (in_fd == -1)
i_fatal("open(/dev/null) failed: %m");
}
if (out_fd < 0) {
out_fd = open("/dev/null", O_WRONLY);

if (out_fd == -1)
i_fatal("open(/dev/null) failed: %m");
}
if (in_fd < 0)
in_fd = dev_null_fd;
if (out_fd < 0)
out_fd = dev_null_fd;

if (in_fd != STDIN_FILENO && dup2(in_fd, STDIN_FILENO) < 0)
i_fatal("dup2(stdin) failed: %m");
if (out_fd != STDOUT_FILENO && dup2(out_fd, STDOUT_FILENO) < 0)
i_fatal("dup2(stdout) failed: %m");

if (in_fd != STDIN_FILENO && close(in_fd) < 0)
if (in_fd != STDIN_FILENO && in_fd != dev_null_fd && close(in_fd) < 0)
i_error("close(in_fd) failed: %m");
if (out_fd != STDOUT_FILENO && (out_fd != in_fd) && close(out_fd) < 0)
if (out_fd != STDOUT_FILENO && out_fd != dev_null_fd &&
(out_fd != in_fd) && close(out_fd) < 0)
i_error("close(out_fd) failed: %m");

/* Drop stderr if requested */
if (drop_stderr) {
int err_fd = open("/dev/null", O_WRONLY);
if (err_fd == -1)
i_fatal("open(/dev/null) failed: %m");
if (err_fd != STDERR_FILENO) {
if (dup2(err_fd, STDERR_FILENO) < 0)
i_fatal("dup2(stderr) failed: %m");
if (close(err_fd) < 0)
i_error("close(err_fd) failed: %m");
}
if (dup2(dev_null_fd, STDERR_FILENO) < 0)
i_fatal("dup2(stderr) failed: %m");
}

/* Setup extra fds */
Expand Down
9 changes: 2 additions & 7 deletions src/lib/module-dir.c
Expand Up @@ -149,25 +149,20 @@ static void *quiet_dlopen(const char *path, int flags)
return dlopen(path, flags);
#else
void *handle;
int fd, fd_null;
int fd;

/* OpenBSD likes to print all "undefined symbol" errors to stderr.
Hide them by sending them to /dev/null. */
fd_null = open("/dev/null", O_WRONLY);
if (fd_null == -1)
i_fatal("open(/dev/null) failed: %m");
fd = dup(STDERR_FILENO);
if (fd == -1)
i_fatal("dup() failed: %m");
if (dup2(fd_null, STDERR_FILENO) < 0)
if (dup2(dev_null_fd, STDERR_FILENO) < 0)
i_fatal("dup2() failed: %m");
handle = dlopen(path, flags);
if (dup2(fd, STDERR_FILENO) < 0)
i_fatal("dup2() failed: %m");
if (close(fd) < 0)
i_error("close() failed: %m");
if (close(fd_null) < 0)
i_error("close() failed: %m");
return handle;
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/master/common.h
Expand Up @@ -9,7 +9,7 @@ extern uid_t master_uid;
extern gid_t master_gid;
extern bool core_dumps_disabled;
extern const char *ssl_manual_key_password;
extern int null_fd, global_master_dead_pipe_fd[2];
extern int global_master_dead_pipe_fd[2];
extern struct service_list *services;
extern bool startup_finished;

Expand Down
16 changes: 5 additions & 11 deletions src/master/main.c
Expand Up @@ -43,7 +43,7 @@ uid_t master_uid;
gid_t master_gid;
bool core_dumps_disabled;
const char *ssl_manual_key_password;
int null_fd, global_master_dead_pipe_fd[2];
int global_master_dead_pipe_fd[2];
struct service_list *services;
bool startup_finished = FALSE;

Expand Down Expand Up @@ -826,12 +826,6 @@ int main(int argc, char *argv[])
i_fatal("Unknown argument: --%s", argv[optind]);
}

null_fd = open("/dev/null", O_WRONLY);
if (null_fd == -1)
i_fatal("Can't open /dev/null: %m");
fd_close_on_exec(null_fd, TRUE);
i_assert(null_fd > STDERR_FILENO);

if (pipe(global_master_dead_pipe_fd) < 0)
i_fatal("pipe() failed: %m");
fd_close_on_exec(global_master_dead_pipe_fd[0], TRUE);
Expand All @@ -843,10 +837,10 @@ int main(int argc, char *argv[])
t_askpass("Give the password for SSL keys: ");
}

if (dup2(null_fd, STDIN_FILENO) < 0)
i_fatal("dup2(null_fd) failed: %m");
if (!foreground && dup2(null_fd, STDOUT_FILENO) < 0)
i_fatal("dup2(null_fd) failed: %m");
if (dup2(dev_null_fd, STDIN_FILENO) < 0)
i_fatal("dup2(dev_null_fd) failed: %m");
if (!foreground && dup2(dev_null_fd, STDOUT_FILENO) < 0)
i_fatal("dup2(dev_null_fd) failed: %m");

pidfile_path =
i_strconcat(set->base_dir, "/"MASTER_PID_FILE_NAME, NULL);
Expand Down
2 changes: 1 addition & 1 deletion src/master/service-process.c
Expand Up @@ -122,7 +122,7 @@ service_dup_fds(struct service *service)
case SERVICE_TYPE_LOG:
case SERVICE_TYPE_ANVIL:
case SERVICE_TYPE_CONFIG:
dup2_append(&dups, null_fd, MASTER_ANVIL_FD);
dup2_append(&dups, dev_null_fd, MASTER_ANVIL_FD);
break;
case SERVICE_TYPE_UNKNOWN:
case SERVICE_TYPE_LOGIN:
Expand Down

0 comments on commit 2481c30

Please sign in to comment.