jilles pointed out that O_CLOEXEC could be used in the open(2) flags

rather than using fcntl(2) later, and in addition to saving a system
call, removes a possible race with fork/exec from threads or signal
handlers.
This commit is contained in:
Guy Helmer 2012-01-11 16:35:26 +00:00
parent 844d43d94b
commit 656b6da7e3
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=229966

View File

@ -117,7 +117,7 @@ pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
* pidfile_write() can be called multiple times.
*/
fd = flopen(pfh->pf_path,
O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode);
if (fd == -1) {
if (errno == EWOULDBLOCK && pidptr != NULL) {
count = 20;
@ -138,19 +138,6 @@ pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
return (NULL);
}
/*
* Prevent the file descriptor from escaping to other
* programs via exec(3).
*/
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
error = errno;
unlink(pfh->pf_path);
close(fd);
free(pfh);
errno = error;
return (NULL);
}
/*
* Remember file information, so in pidfile_write() we are sure we write
* to the proper descriptor.