Assimilate the open/flock combination used several times into its own

function.
This commit is contained in:
dholland 2011-05-24 06:58:07 +00:00
parent 2b880b64f4
commit fe8f2f2d61
1 changed files with 29 additions and 17 deletions

View File

@ -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> * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
@ -28,7 +28,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef LINT #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 #endif
#include <sys/types.h> #include <sys/types.h>
@ -122,6 +122,25 @@ static bool_t rmtcr_outval;
static unsigned long rmtcr_port; static unsigned long rmtcr_port;
static SVCXPRT *udptransp, *tcptransp; 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 // logging
@ -221,15 +240,15 @@ makelock(struct _dom_binding *ypdb)
(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR, (void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
ypdb->dom_domain, ypdb->dom_vers); 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); (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; return -1;
} }
}
#if O_SHLOCK == 0
(void)flock(fd, LOCK_SH);
#endif
return fd; return fd;
} }
@ -730,16 +749,13 @@ direct_set(char *buf, int outlen, struct _dom_binding *ypdb)
(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR, (void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
ypdb->dom_domain, ypdb->dom_vers); 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)); yp_log(LOG_WARNING, "%s: %s", path, strerror(errno));
been_ypset = 0; been_ypset = 0;
return -1; return -1;
} }
#if O_SHLOCK == 0
(void)flock(fd, LOCK_SH);
#endif
/* Read the binding file... */ /* Read the binding file... */
iov[0].iov_base = &(dummy_svc.xp_port); iov[0].iov_base = &(dummy_svc.xp_port);
iov[0].iov_len = sizeof(dummy_svc.xp_port); iov[0].iov_len = sizeof(dummy_svc.xp_port);
@ -1135,14 +1151,10 @@ main(int argc, char *argv[])
/* initialise syslog */ /* initialise syslog */
openlog("ypbind", LOG_PERROR | LOG_PID, LOG_DAEMON); 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) if (lockfd == -1)
err(1, "Cannot create %s", _PATH_YPBIND_LOCK); err(1, "Cannot create %s", _PATH_YPBIND_LOCK);
#if O_SHLOCK == 0
(void)flock(lockfd, LOCK_SH);
#endif
(void)pmap_unset(YPBINDPROG, YPBINDVERS); (void)pmap_unset(YPBINDPROG, YPBINDVERS);
udptransp = svcudp_create(RPC_ANYSOCK); udptransp = svcudp_create(RPC_ANYSOCK);