summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorPhilip Guenther <guenther@cvs.openbsd.org>2014-10-26 22:16:17 +0000
committerPhilip Guenther <guenther@cvs.openbsd.org>2014-10-26 22:16:17 +0000
commit8b5a64863ab127c99460637ff9d9e878a39bbe23 (patch)
tree3ac15a62c39791cc30cefece76cb02899fd9ecb2 /usr.sbin
parent7e11a14625fdf34c2da0d307fed5964aa71af961 (diff)
Use socket(SOCK_CLOEXEC), open(O_CLOEXEC), and fcntl(F_DUPFD_CLOEXEC)
instead of calling fcntl(F_SETFD) later. ok otto@ millert@
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/apmd/apmd.c7
-rw-r--r--usr.sbin/cron/misc.c15
2 files changed, 11 insertions, 11 deletions
diff --git a/usr.sbin/apmd/apmd.c b/usr.sbin/apmd/apmd.c
index 46bf47f6cba..be67d71b103 100644
--- a/usr.sbin/apmd/apmd.c
+++ b/usr.sbin/apmd/apmd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: apmd.c,v 1.71 2014/10/17 07:41:40 jmc Exp $ */
+/* $OpenBSD: apmd.c,v 1.72 2014/10/26 22:16:16 guenther Exp $ */
/*
* Copyright (c) 1995, 1996 John T. Kohl
@@ -211,7 +211,7 @@ bind_socket(const char *sockname)
mode_t old_umask;
int sock;
- sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (sock == -1)
error("cannot create local socket", NULL);
@@ -441,9 +441,6 @@ main(int argc, char *argv[])
sock_fd = bind_socket(sockname);
- if (fcntl(sock_fd, F_SETFD, FD_CLOEXEC) == -1)
- error("cannot set close-on-exec for the socket", NULL);
-
power_status(ctl_fd, 1, &pinfo);
if (statonly)
diff --git a/usr.sbin/cron/misc.c b/usr.sbin/cron/misc.c
index 8edf62c35a6..c2b14431921 100644
--- a/usr.sbin/cron/misc.c
+++ b/usr.sbin/cron/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.47 2014/08/15 03:51:40 guenther Exp $ */
+/* $OpenBSD: misc.c,v 1.48 2014/10/26 22:16:16 guenther Exp $ */
/* Copyright 1988,1990,1993,1994 by Paul Vixie
* All rights reserved
@@ -248,8 +248,9 @@ acquire_daemonlock(int closeflag) {
if (fd == -1) {
pidfile = _PATH_CRON_PID;
- if ((fd = open(pidfile, O_RDWR|O_CREAT|O_EXLOCK|O_NONBLOCK,
- 0644)) == -1) {
+ fd = open(pidfile,
+ O_RDWR|O_CREAT|O_EXLOCK|O_NONBLOCK|O_CLOEXEC, 0644);
+ if (fd == -1) {
int save_errno = errno;
if (errno != EWOULDBLOCK) {
@@ -281,7 +282,10 @@ acquire_daemonlock(int closeflag) {
}
/* fd must be > STDERR_FILENO since we dup fd 0-2 to /dev/null */
if (fd <= STDERR_FILENO) {
- if (dup2(fd, STDERR_FILENO + 1) < 0) {
+ int newfd;
+
+ newfd = fcntl(fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
+ if (newfd < 0) {
snprintf(buf, sizeof buf,
"can't dup pid fd: %s", strerror(errno));
fprintf(stderr, "%s: %s\n", ProgramName, buf);
@@ -289,9 +293,8 @@ acquire_daemonlock(int closeflag) {
exit(EXIT_FAILURE);
}
close(fd);
- fd = STDERR_FILENO + 1;
+ fd = newfd;
}
- (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
}
snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());