commit d24ddaa6a9de118f70658f7fd917c37cc787b425 from: Thomas Adam date: Sat Feb 26 15:40:12 2022 UTC portable: include support for MacOS This adds support for MacOS (Monteray onward, although should work on older MacOSes). The BSD wrapper for poll/ppoll has been lifted from the work the folks over at OpenSSH have done -- thanks! commit - 9d70a0bf7236fdcfbe7186f0d90a5a15f5a10b18 commit + d24ddaa6a9de118f70658f7fd917c37cc787b425 blob - 0b90a45a21dee8de83282118e67277aef5bd3370 blob + ce2d4cc88d90c3611d0543805a07ce183e3989d7 --- compat/Makefile.am +++ compat/Makefile.am @@ -2,7 +2,6 @@ noinst_LIBRARIES = libopenbsd-compat.a include $(top_builddir)/Makefile.common - libopenbsd_compat_a_SOURCES = \ asprintf.c \ base64.c \ @@ -17,8 +16,6 @@ libopenbsd_compat_a_SOURCES = \ merge.c \ reallocarray.c \ recallocarray.c \ - strlcat.c \ - strlcpy.c \ strndup.c \ strnlen.c \ strsep.c \ @@ -26,8 +23,18 @@ libopenbsd_compat_a_SOURCES = \ imsg.h \ queue.h \ tree.h -if HOST_FREEBSD -else + +# For MacOS, don't build the compat versions of strl{cat,cpy}, but do for all +# other systems. +if !HOST_DARWIN +libopenbsd_compat_a_SOURCES += strlcat.c strlcpy.c +endif + +if HOST_DARWIN +libopenbsd_compat_a_SOURCES += uuid.c bsd-poll.c bsd-poll.h +endif + +if HOST_LINUX libopenbsd_compat_a_SOURCES += uuid.c endif @@ -39,4 +46,5 @@ EXTRA_DIST = \ $(top_srcdir)/include/got_compat.h \ imsg.h \ queue.h \ - tree.h + tree.h \ + bsd-poll.h blob - /dev/null blob + 6276286954e17d1e3e2f6c42868c4f3c734eac38 (mode 644) --- /dev/null +++ compat/bsd-poll.c @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2004, 2005, 2007 Darren Tucker (dtucker at zip com au). + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifdef HAVE_SYS_SELECT_H +# include +#endif + +#include +#include +#include +#include "bsd-poll.h" + +#ifndef MAX +#define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b)) +#endif + +/* + * A minimal implementation of ppoll(2), built on top of pselect(2). + * + * Only supports POLLIN, POLLOUT and POLLPRI flags in pfd.events and + * revents. Notably POLLERR, POLLHUP and POLLNVAL are not supported. + * + * Supports pfd.fd = -1 meaning "unused" although it's not standard. + */ + +int +ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmoutp, + const sigset_t *sigmask) +{ + nfds_t i; + int saved_errno, ret, fd, maxfd = 0; + fd_set *readfds = NULL, *writefds = NULL, *exceptfds = NULL; + size_t nmemb; + + for (i = 0; i < nfds; i++) { + fd = fds[i].fd; + if (fd != -1 && fd >= FD_SETSIZE) { + errno = EINVAL; + return -1; + } + maxfd = MAX(maxfd, fd); + } + + nmemb = howmany(maxfd + 1 , NFDBITS); + if ((readfds = calloc(nmemb, sizeof(fd_mask))) == NULL || + (writefds = calloc(nmemb, sizeof(fd_mask))) == NULL || + (exceptfds = calloc(nmemb, sizeof(fd_mask))) == NULL) { + saved_errno = ENOMEM; + ret = -1; + goto out; + } + + /* populate event bit vectors for the events we're interested in */ + for (i = 0; i < nfds; i++) { + fd = fds[i].fd; + if (fd == -1) + continue; + if (fds[i].events & POLLIN) + FD_SET(fd, readfds); + if (fds[i].events & POLLOUT) + FD_SET(fd, writefds); + if (fds[i].events & POLLPRI) + FD_SET(fd, exceptfds); + } + + ret = pselect(maxfd + 1, readfds, writefds, exceptfds, tmoutp, sigmask); + saved_errno = errno; + + /* scan through select results and set poll() flags */ + for (i = 0; i < nfds; i++) { + fd = fds[i].fd; + fds[i].revents = 0; + if (fd == -1) + continue; + if (FD_ISSET(fd, readfds)) + fds[i].revents |= POLLIN; + if (FD_ISSET(fd, writefds)) + fds[i].revents |= POLLOUT; + if (FD_ISSET(fd, exceptfds)) + fds[i].revents |= POLLPRI; + } + +out: + free(readfds); + free(writefds); + free(exceptfds); + if (ret == -1) + errno = saved_errno; + return ret; +} + +#if 0 +int +poll(struct pollfd *fds, nfds_t nfds, int timeout) +{ + struct timespec ts, *tsp = NULL; + + /* poll timeout is msec, ppoll is timespec (sec + nsec) */ + if (timeout >= 0) { + ts.tv_sec = timeout / 1000; + ts.tv_nsec = (timeout % 1000) * 1000000; + tsp = &ts; + } + + return ppoll(fds, nfds, tsp, NULL); +} +#endif blob - /dev/null blob + 7d00d7ae1a3330abe640f705fb93441f19a1415b (mode 644) --- /dev/null +++ compat/bsd-poll.h @@ -0,0 +1,59 @@ +/* $OpenBSD: poll.h,v 1.11 2003/12/10 23:10:08 millert Exp $ */ + +/* + * Copyright (c) 1996 Theo de Raadt + * All rights reserved. + * + * 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 AUTHOR ``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 AUTHOR 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. + */ + +/* OPENBSD ORIGINAL: sys/sys/poll.h */ + +#ifndef _COMPAT_POLL_H_ +#define _COMPAT_POLL_H_ + +#include +#include +#include +#include +#include + +#ifndef HAVE_STRUCT_POLLFD_FD +#define POLLIN 0x0001 +#define POLLPRI 0x0002 +#define POLLOUT 0x0004 +#define POLLERR 0x0008 +#define POLLHUP 0x0010 +#define POLLNVAL 0x0020 +#if 0 +/* the following are currently not implemented */ +#define POLLRDNORM 0x0040 +#define POLLNORM POLLRDNORM +#define POLLWRNORM POLLOUT +#define POLLRDBAND 0x0080 +#define POLLWRBAND 0x0100 +#endif +#endif /* !HAVE_STRUCT_POLLFD_FD */ + +typedef unsigned int nfds_t; + +int ppoll(struct pollfd *, nfds_t, const struct timespec *, const sigset_t *); +#endif /* !_COMPAT_POLL_H_ */ blob - 84d736bf96aa79d1cf9cc867ebaec29949c808cc blob + 02490499adb28ffd6366dab21a4eeefd8f2af8ff --- compat/recallocarray.c +++ compat/recallocarray.c @@ -73,7 +73,11 @@ recallocarray(void *ptr, size_t oldnmemb, size_t newnm } else memcpy(newptr, ptr, newsize); +#ifdef __APPLE__ + memset_s(ptr, oldsize, 0, oldsize); +#else explicit_bzero(ptr, oldsize); +#endif free(ptr); return newptr; blob - a6a9e4ac68ebe429ef54422735a95b074e10badc blob + c1dadc78e3d333acc31fde5ea029c5ecc6f5f3dc --- configure.ac +++ configure.ac @@ -47,12 +47,14 @@ AC_CHECK_HEADERS([ \ netdb.h \ netinet/in.h \ paths.h \ + poll.h \ stddef.h \ stdint.h \ stdlib.h \ string.h \ sys/ioctl.h \ sys/param.h \ + sys/poll.h \ sys/socket.h \ sys/time.h \ sys/tree.h \ @@ -154,6 +156,10 @@ case "$host_os" in *freebsd*) AC_MSG_RESULT(freebsd) PLATFORM=freebsd + ;; + *darwin*) + AC_MSG_RESULT(darwin) + PLATFORM=darwin ;; *) AC_MSG_RESULT(unknown) @@ -163,6 +169,7 @@ esac AC_SUBST(PLATFORM) AM_CONDITIONAL([HOST_FREEBSD], [test "$PLATFORM" = "freebsd"]) AM_CONDITIONAL([HOST_LINUX], [test "$PLATFORM" = "linux"]) +AM_CONDITIONAL([HOST_DARWIN], [test "$PLATFORM" = "darwin"]) # Landlock detection. AC_MSG_CHECKING([for landlock]) @@ -226,7 +233,10 @@ AC_SEARCH_LIBS(uuid_create, , AC_DEFINE(HAVE_BSD_UUID) AC_SEARCH_LIBS(uuid_create, found_uuid=no, found_uuid=yes) AC_SEARCH_LIBS(mergesort, , AC_DEFINE(HAVE_BSD_MERGESORT)) -if test "x$found_uuid" = "xyes"; then +# Don't define HAVE_BSD_UUID on darwin (Apple) as this breaks the BSD API. +# Instead, use the UUID implementation wrapper that's in compat/ plus uuid +# ossp +if test "x$found_uuid" = "xyes" -a "x$PLATFORM" != "darwin"; then AC_DEFINE(HAVE_BSD_UUID) else PKG_CHECK_MODULES( blob - 9fc89b9dcace2ed2e34babe66f47c77677e29969 blob + 7d9db3e5a84ef970dfadb93d80e42e1ed2d4cefe --- include/got_compat.h +++ include/got_compat.h @@ -6,7 +6,33 @@ #include #if defined(__FreeBSD__) #include -#else +#elif defined(__APPLE__) +#include +#include +#include "compat/bsd-poll.h" + +#define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */ + +#define htobe16(x) OSSwapHostToBigInt16(x) +#define htole16(x) OSSwapHostToLittleInt16(x) +#define be16toh(x) OSSwapBigToHostInt16(x) +#define le16toh(x) OSSwapLittleToHostInt16(x) + +#define htobe32(x) OSSwapHostToBigInt32(x) +#define htole32(x) OSSwapHostToLittleInt32(x) +#define be32toh(x) OSSwapBigToHostInt32(x) +#define le32toh(x) OSSwapLittleToHostInt32(x) + +#define htobe64(x) OSSwapHostToBigInt64(x) +#define htole64(x) OSSwapHostToLittleInt64(x) +#define be64toh(x) OSSwapBigToHostInt64(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) + +#define st_atim st_atimespec +#define st_ctim st_ctimespec +#define st_mtim st_mtimespec + +#else /* Linux, etc... */ #include #endif @@ -113,11 +139,19 @@ void uuid_to_string(uuid_t *, char **, uint32_t *); #include "compat/imsg.h" #endif -#ifdef HAVE_LIBCRYPTO +/* Include Apple-specific headers when libcrypto is in use. */ +#if defined(HAVE_LIBCRYPTO) && defined(__APPLE__) +#define COMMON_DIGEST_FOR_OPENSSL +#include +#endif + +#if defined(HAVE_LIBCRYPTO) && !defined(__APPLE__) #include -#else +#elif !defined(__APPLE__) #include +#endif +#if !defined(HAVE_LIBCRYPTO) || defined(__APPLE__) #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1) @@ -145,8 +179,7 @@ int getdtablecount(void); #ifndef HAVE_CLOSEFROM /* closefrom.c */ -//void closefrom(int); -#define closefrom(fd) (closefrom(fd), 0) +void closefrom(int); #endif #if defined (__FreeBSD__) @@ -218,7 +251,6 @@ void *recallocarray(void *, size_t, size_t, size_t); int fmt_scaled(long long, char *); int scan_scaled(char *, long long *); #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */ - #endif #ifndef HAVE_LIBBSD blob - a52c4af1a9366a4832e5efcd79cba83fcce4b8dd blob + 27730ae459b928b0915680bfbb23bad789387ecd --- tog/tog.c +++ tog/tog.c @@ -19,7 +19,7 @@ #include #include -#if defined(__FreeBSD__) +#if defined(__FreeBSD__) || defined(__APPLE__) #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */ #endif #include