Skip to content

Commit a8b5f8b

Browse files
committed
Add function get_null_fd(), to reduce code redundancy.
1 parent aec7d2f commit a8b5f8b

5 files changed

Lines changed: 32 additions & 21 deletions

File tree

extras/ezio/ezio-term.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ main(int argc, char *argv[])
106106
argv += optind;
107107

108108
/* Make sure that the ezio3 terminfo entry is available. */
109-
dummy_fd = open("/dev/null", O_RDWR);
109+
dummy_fd = get_null_fd();
110110
if (dummy_fd >= 0) {
111111
if (setupterm("ezio3", dummy_fd, &retval) == ERR) {
112112
if (retval == 0) {
@@ -118,9 +118,6 @@ main(int argc, char *argv[])
118118
}
119119
}
120120
del_curterm(cur_term);
121-
close(dummy_fd);
122-
} else {
123-
ovs_error(errno, "failed to open /dev/null");
124121
}
125122

126123
/* Lock serial port. */

lib/process.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ process_start(char **argv,
187187
unblock_sigchld(&oldsigs);
188188
for (fd = 0; fd < fd_max; fd++) {
189189
if (is_member(fd, null_fds, n_null_fds)) {
190+
/* We can't use get_null_fd() here because we might have
191+
* already closed its fd. */
190192
int nullfd = open("/dev/null", O_RDWR);
191193
dup2(nullfd, fd);
192194
close(nullfd);

lib/socket-util.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,24 @@ guess_netmask(uint32_t ip)
299299
: htonl(0)); /* ??? */
300300
}
301301

302+
/* Returns a readable and writable fd for /dev/null, if successful, otherwise
303+
* a negative errno value. The caller must not close the returned fd (because
304+
* the same fd will be handed out to subsequent callers). */
305+
int
306+
get_null_fd(void)
307+
{
308+
static int null_fd = -1;
309+
if (null_fd < 0) {
310+
null_fd = open("/dev/null", O_RDWR);
311+
if (null_fd < 0) {
312+
int error = errno;
313+
VLOG_ERR("could not open /dev/null: %s", strerror(error));
314+
return -error;
315+
}
316+
}
317+
return null_fd;
318+
}
319+
302320
int
303321
read_fully(int fd, void *p_, size_t size, size_t *bytes_read)
304322
{

lib/socket-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ int make_unix_socket(int style, bool nonblock, bool passcred,
3232
const char *bind_path, const char *connect_path);
3333
int get_unix_name_len(socklen_t sun_len);
3434
uint32_t guess_netmask(uint32_t ip);
35+
int get_null_fd(void);
3536

3637
int read_fully(int fd, void *, size_t, size_t *bytes_read);
3738
int write_fully(int fd, const void *, size_t, size_t *bytes_written);

secchan/executer.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ struct executer {
7171
};
7272

7373
/* File descriptors for waking up when a child dies. */
74-
static int signal_fds[2];
75-
76-
/* File descriptor for /dev/null. */
77-
static int null_fd = -1;
74+
static int signal_fds[2] = {-1, -1};
7875

7976
static void send_child_status(struct rconn *, uint32_t xid, uint32_t status,
8077
const void *data, size_t size);
@@ -205,9 +202,9 @@ executer_handle_request(struct executer *e, struct rconn *rconn,
205202
* subprocesses at once? Would also want to catch fatal signals and
206203
* kill them at the same time though. */
207204
fatal_signal_fork();
208-
dup2(null_fd, 0);
205+
dup2(get_null_fd(), 0);
209206
dup2(output_fds[1], 1);
210-
dup2(null_fd, 2);
207+
dup2(get_null_fd(), 2);
211208
max_fds = get_max_fds();
212209
for (i = 3; i < max_fds; i++) {
213210
close(i);
@@ -448,24 +445,20 @@ executer_create(const char *command_acl, const char *command_dir,
448445
struct sigaction sa;
449446

450447
*executerp = NULL;
451-
if (null_fd == -1) {
448+
if (signal_fds[0] == -1) {
449+
/* Make sure we can get a fd for /dev/null. */
450+
int null_fd = get_null_fd();
451+
if (null_fd < 0) {
452+
return -null_fd;
453+
}
454+
452455
/* Create pipe for notifying us that SIGCHLD was invoked. */
453456
if (pipe(signal_fds)) {
454457
VLOG_ERR("pipe failed: %s", strerror(errno));
455458
return errno;
456459
}
457460
set_nonblocking(signal_fds[0]);
458461
set_nonblocking(signal_fds[1]);
459-
460-
/* Open /dev/null. */
461-
null_fd = open("/dev/null", O_RDWR);
462-
if (null_fd < 0) {
463-
int error = errno;
464-
VLOG_ERR("could not open /dev/null: %s", strerror(error));
465-
close(signal_fds[0]);
466-
close(signal_fds[1]);
467-
return error;
468-
}
469462
}
470463

471464
/* Set up signal handler. */

0 commit comments

Comments
 (0)