Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/ypbind Assimilate the open/flock combination used s...



details:   https://anonhg.NetBSD.org/src/rev/750c3c86f2d8
branches:  trunk
changeset: 765302:750c3c86f2d8
user:      dholland <dholland%NetBSD.org@localhost>
date:      Tue May 24 06:58:07 2011 +0000

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

diffstat:

 usr.sbin/ypbind/ypbind.c |  46 +++++++++++++++++++++++++++++-----------------
 1 files changed, 29 insertions(+), 17 deletions(-)

diffs (100 lines):

diff -r 44d3956cac85 -r 750c3c86f2d8 usr.sbin/ypbind/ypbind.c
--- a/usr.sbin/ypbind/ypbind.c  Tue May 24 06:57:55 2011 +0000
+++ b/usr.sbin/ypbind/ypbind.c  Tue May 24 06:58:07 2011 +0000
@@ -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 <deraadt%fsa.ca@localhost>
@@ -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);



Home | Main Index | Thread Index | Old Index