Module Name:    src
Committed By:   dholland
Date:           Tue May 24 06:58:07 UTC 2011

Modified Files:
        src/usr.sbin/ypbind: ypbind.c

Log Message:
Assimilate the open/flock combination used several times into its own
function.


To generate a diff of this commit:
cvs rdiff -u -r1.72 -r1.73 src/usr.sbin/ypbind/ypbind.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ypbind/ypbind.c
diff -u src/usr.sbin/ypbind/ypbind.c:1.72 src/usr.sbin/ypbind/ypbind.c:1.73
--- src/usr.sbin/ypbind/ypbind.c:1.72	Tue May 24 06:57:55 2011
+++ src/usr.sbin/ypbind/ypbind.c	Tue May 24 06:58:07 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: ypbind.c,v 1.72 2011/05/24 06:57:55 dholland Exp $	*/
+/*	$NetBSD: ypbind.c,v 1.73 2011/05/24 06:58:07 dholland Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993 Theo de Raadt <dera...@fsa.ca>
@@ -28,7 +28,7 @@
 
 #include <sys/cdefs.h>
 #ifndef LINT
-__RCSID("$NetBSD: ypbind.c,v 1.72 2011/05/24 06:57:55 dholland Exp $");
+__RCSID("$NetBSD: ypbind.c,v 1.73 2011/05/24 06:58:07 dholland Exp $");
 #endif
 
 #include <sys/types.h>
@@ -123,6 +123,25 @@
 static SVCXPRT *udptransp, *tcptransp;
 
 ////////////////////////////////////////////////////////////
+// utilities
+
+static int
+open_locked(const char *path, int flags, mode_t mode)
+{
+	int fd;
+
+	fd = open(path, flags|O_SHLOCK, mode);
+	if (fd < 0) {
+		return -1;
+	}
+#if O_SHLOCK == 0
+	/* dholland 20110522 wouldn't it be better to check this for error? */
+	(void)flock(fd, LOCK_SH);
+#endif
+	return fd;
+}
+
+////////////////////////////////////////////////////////////
 // logging
 
 #ifdef DEBUG
@@ -221,15 +240,15 @@
 	(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
 	    ypdb->dom_domain, ypdb->dom_vers);
 
-	if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
+	fd = open_locked(path, O_CREAT|O_RDWR|O_TRUNC, 0644);
+	if (fd == -1) {
 		(void)mkdir(BINDINGDIR, 0755);
-		if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
+		fd = open_locked(path, O_CREAT|O_RDWR|O_TRUNC, 0644);
+		if (fd == -1) {
 			return -1;
+		}
 	}
 
-#if O_SHLOCK == 0
-	(void)flock(fd, LOCK_SH);
-#endif
 	return fd;
 }
 
@@ -730,16 +749,13 @@
 	(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
 	    ypdb->dom_domain, ypdb->dom_vers);
 
-	if ((fd = open(path, O_SHLOCK|O_RDONLY, 0644)) == -1) {
+	fd = open_locked(path, O_RDONLY, 0644);
+	if (fd == -1) {
 		yp_log(LOG_WARNING, "%s: %s", path, strerror(errno));
 		been_ypset = 0;
 		return -1;
 	}
 
-#if O_SHLOCK == 0
-	(void)flock(fd, LOCK_SH);
-#endif
-
 	/* Read the binding file... */
 	iov[0].iov_base = &(dummy_svc.xp_port);
 	iov[0].iov_len = sizeof(dummy_svc.xp_port);
@@ -1135,14 +1151,10 @@
 	/* initialise syslog */
 	openlog("ypbind", LOG_PERROR | LOG_PID, LOG_DAEMON);
 
-	lockfd = open(_PATH_YPBIND_LOCK, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644);
+	lockfd = open_locked(_PATH_YPBIND_LOCK, O_CREAT|O_RDWR|O_TRUNC, 0644);
 	if (lockfd == -1)
 		err(1, "Cannot create %s", _PATH_YPBIND_LOCK);
 
-#if O_SHLOCK == 0
-	(void)flock(lockfd, LOCK_SH);
-#endif
-
 	(void)pmap_unset(YPBINDPROG, YPBINDVERS);
 
 	udptransp = svcudp_create(RPC_ANYSOCK);

Reply via email to