From b087998d1ba90dd1ddb6bfdb17873dc3e7392798 Mon Sep 17 00:00:00 2001 From: Darren Tucker Date: Mon, 26 Feb 2018 14:27:02 +1100 Subject: Import flock() compat from NetBSD. From NetBSD's src/trunk/tools/compat/flock.c, no OpenSSH changes yet. --- openbsd-compat/bsd-flock.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 openbsd-compat/bsd-flock.c (limited to 'openbsd-compat/bsd-flock.c') diff --git a/openbsd-compat/bsd-flock.c b/openbsd-compat/bsd-flock.c new file mode 100644 index 000000000..4a51ebe13 --- /dev/null +++ b/openbsd-compat/bsd-flock.c @@ -0,0 +1,76 @@ +/* $NetBSD: flock.c,v 1.6 2008/04/28 20:24:12 martin Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Todd Vierling. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Emulate flock() with fcntl(), where available. + * Otherwise, don't do locking; just pretend success. + */ + +#include "nbtool_config.h" + +#if !HAVE_FLOCK +#include +#include + +int flock(int fd, int op) { + int rc = 0; + +#if defined(F_SETLK) && defined(F_SETLKW) + struct flock fl = {0}; + + switch (op & (LOCK_EX|LOCK_SH|LOCK_UN)) { + case LOCK_EX: + fl.l_type = F_WRLCK; + break; + + case LOCK_SH: + fl.l_type = F_RDLCK; + break; + + case LOCK_UN: + fl.l_type = F_UNLCK; + break; + + default: + errno = EINVAL; + return -1; + } + + fl.l_whence = SEEK_SET; + rc = fcntl(fd, op & LOCK_NB ? F_SETLK : F_SETLKW, &fl); + + if (rc && (errno == EAGAIN)) + errno = EWOULDBLOCK; +#endif + + return rc; +} +#endif -- cgit v1.2.3 From cd3ab57f9b388f8b1abf601dc4d78ff82d83b75e Mon Sep 17 00:00:00 2001 From: Darren Tucker Date: Mon, 26 Feb 2018 14:37:06 +1100 Subject: Hook up flock() compat code. Also a couple of minor changes: fail if we can't lock instead of silently succeeding, and apply a couple of minor style fixes. --- configure.ac | 1 + openbsd-compat/Makefile.in | 2 +- openbsd-compat/bsd-flock.c | 11 ++++++++--- openbsd-compat/bsd-misc.h | 8 ++++++++ 4 files changed, 18 insertions(+), 4 deletions(-) (limited to 'openbsd-compat/bsd-flock.c') diff --git a/configure.ac b/configure.ac index a70d40628..35e2e8d1d 100644 --- a/configure.ac +++ b/configure.ac @@ -1723,6 +1723,7 @@ AC_CHECK_FUNCS([ \ explicit_bzero \ fchmod \ fchown \ + flock \ freeaddrinfo \ freezero \ fstatfs \ diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in index 053f2ef2a..71fcdb127 100644 --- a/openbsd-compat/Makefile.in +++ b/openbsd-compat/Makefile.in @@ -16,7 +16,7 @@ LDFLAGS=-L. @LDFLAGS@ OPENBSD=base64.o basename.o bcrypt_pbkdf.o bindresvport.o blowfish.o daemon.o dirname.o fmt_scaled.o getcwd.o getgrouplist.o getopt_long.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o pwcache.o readpassphrase.o reallocarray.o realpath.o recallocarray.o rresvport.o setenv.o setproctitle.o sha1.o sha2.o rmd160.o md5.o sigact.o strcasestr.o strlcat.o strlcpy.o strmode.o strnlen.o strptime.o strsep.o strtonum.o strtoll.o strtoul.o strtoull.o timingsafe_bcmp.o vis.o blowfish.o bcrypt_pbkdf.o explicit_bzero.o freezero.o -COMPAT=arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cygwin_util.o bsd-getpeereid.o getrrsetbyname-ldns.o bsd-err.o bsd-getpagesize.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-malloc.o bsd-setres_id.o bsd-signal.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xcrypt.o kludge-fd_set.o +COMPAT=arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cygwin_util.o bsd-getpeereid.o getrrsetbyname-ldns.o bsd-err.o bsd-flock.o bsd-getpagesize.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-malloc.o bsd-setres_id.o bsd-signal.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xcrypt.o kludge-fd_set.o PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-net.o port-uw.o diff --git a/openbsd-compat/bsd-flock.c b/openbsd-compat/bsd-flock.c index 4a51ebe13..bf4d8a658 100644 --- a/openbsd-compat/bsd-flock.c +++ b/openbsd-compat/bsd-flock.c @@ -34,13 +34,15 @@ * Otherwise, don't do locking; just pretend success. */ -#include "nbtool_config.h" +#include "includes.h" -#if !HAVE_FLOCK +#ifndef HAVE_FLOCK #include #include -int flock(int fd, int op) { +int +flock(int fd, int op) +{ int rc = 0; #if defined(F_SETLK) && defined(F_SETLKW) @@ -69,6 +71,9 @@ int flock(int fd, int op) { if (rc && (errno == EAGAIN)) errno = EWOULDBLOCK; +#else + rc = -1 + errno = ENOSYS; #endif return rc; diff --git a/openbsd-compat/bsd-misc.h b/openbsd-compat/bsd-misc.h index af2ccdae2..2a73ae45b 100644 --- a/openbsd-compat/bsd-misc.h +++ b/openbsd-compat/bsd-misc.h @@ -145,4 +145,12 @@ int raise(int); pid_t getsid(pid_t); #endif +#ifndef HAVE_FLOCK +# define LOCK_SH 0x01 +# define LOCK_EX 0x02 +# define LOCK_NB 0x04 +# define LOCK_UN 0x08 +int flock(int, int); +#endif + #endif /* _BSD_MISC_H */ -- cgit v1.2.3 From 5aea4aa522f61bb2f34c3055a7de203909dfae77 Mon Sep 17 00:00:00 2001 From: Darren Tucker Date: Mon, 26 Feb 2018 14:39:14 +1100 Subject: typo: missing ; --- openbsd-compat/bsd-flock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'openbsd-compat/bsd-flock.c') diff --git a/openbsd-compat/bsd-flock.c b/openbsd-compat/bsd-flock.c index bf4d8a658..9b15d1eaf 100644 --- a/openbsd-compat/bsd-flock.c +++ b/openbsd-compat/bsd-flock.c @@ -72,7 +72,7 @@ flock(int fd, int op) if (rc && (errno == EAGAIN)) errno = EWOULDBLOCK; #else - rc = -1 + rc = -1; errno = ENOSYS; #endif -- cgit v1.2.3