Skip to content

Commit

Permalink
Merge pull request #6479 from garlick/macos5
Browse files Browse the repository at this point in the history
fix macos portability issues (fifth batch)
  • Loading branch information
mergify[bot] authored Dec 5, 2024
2 parents 2e7b064 + a941e1f commit 02cae96
Show file tree
Hide file tree
Showing 22 changed files with 201 additions and 18 deletions.
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ AC_CHECK_FUNCS( \
strncasecmp \
setlocale \
uselocale \
inotify_init1 \
)
# See src/common/libmissing/Makefile.am
AC_REPLACE_FUNCS( \
Expand All @@ -245,6 +246,7 @@ X_AC_CHECK_COND_LIB(dl, dlerror)
X_AC_MALLOC
AC_CHECK_LIB(m, floor)
AC_SEARCH_LIBS(epoll_create1, epoll-shim)
AM_CONDITIONAL([HAVE_INOTIFY], [test "x$ac_cv_func_inotify_init1" = xyes])

AC_MSG_CHECKING([for pthread_setname_np with tid parameter])
AC_COMPILE_IFELSE(
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/flux.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ bool flux_is_installed (void)
{
int rc = executable_is_intree ();
if (rc < 0)
log_err_exit ("Failed to to determine if flux is installed");
log_err_exit ("Failed to determine if flux is installed");
return (rc == 0);
}

Expand Down
3 changes: 3 additions & 0 deletions src/common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ libflux_optparse_la_LIBADD = \
$(builddir)/liblsd/liblsd.la \
$(builddir)/libutil/fsd.lo \
$(builddir)/libutil/parse_size.lo \
$(builddir)/libutil/fdutils.lo \
$(builddir)/libmissing/libmissing.la \
$(LIBPTHREAD)
libflux_optparse_la_LDFLAGS = \
Expand Down Expand Up @@ -179,6 +180,7 @@ flux_libpmi_la_LIBADD = \
$(builddir)/libpmi/libpmi_client.la \
$(builddir)/libpmi/libpmi_common.la \
$(builddir)/libutil/aux.lo \
$(builddir)/libutil/fdutils.lo \
$(builddir)/libmissing/libmissing.la

flux_libpmi_la_LDFLAGS = \
Expand All @@ -195,6 +197,7 @@ flux_libpmi2_la_LIBADD = \
$(builddir)/libpmi/libpmi_client.la \
$(builddir)/libpmi/libpmi_common.la \
$(builddir)/libutil/aux.lo \
$(builddir)/libutil/fdutils.lo \
$(builddir)/libmissing/libmissing.la
flux_libpmi2_la_LDFLAGS = \
-export-symbols-regex "^(PMI2_|flux_pmi_library|__asan)" \
Expand Down
7 changes: 7 additions & 0 deletions src/common/libflux/test/reactor.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <sys/socket.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <math.h>
#include <flux/core.h>

Expand Down Expand Up @@ -104,7 +105,13 @@ static void test_fd (flux_reactor_t *reactor)
int fd[2];
flux_watcher_t *r, *w;

#ifdef SOCK_NONBLOCK
ok (socketpair (PF_LOCAL, SOCK_STREAM|SOCK_NONBLOCK, 0, fd) == 0,
#else
ok (socketpair (PF_LOCAL, SOCK_STREAM, 0, fd) == 0
&& fd_set_nonblocking (fd[0]) >= 0
&& fd_set_nonblocking (fd[1]) >= 0,
#endif
"fd: successfully created non-blocking socketpair");
r = flux_fd_watcher_create (reactor, fd[0], FLUX_POLLIN, fdreader, NULL);
w = flux_fd_watcher_create (reactor, fd[1], FLUX_POLLOUT, fdwriter, NULL);
Expand Down
3 changes: 2 additions & 1 deletion src/common/libmissing/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ AM_CFLAGS = \
$(CODE_COVERAGE_CFLAGS)

AM_CPPFLAGS = \
$(CODE_COVERAGE_CPPFLAGS)
$(CODE_COVERAGE_CPPFLAGS) \
-I$(top_srcdir)

noinst_LTLIBRARIES = libmissing.la

Expand Down
18 changes: 15 additions & 3 deletions src/common/libmissing/pipe2.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@
#include <fcntl.h>
#include <errno.h>

#include "src/common/libutil/fdutils.h"

#include "pipe2.h"

static int setflags (int fd, int flags)
{
int oflags;
if ((oflags = fcntl (fd, F_GETFL)) < 0
|| fcntl (fd, F_SETFL, oflags | flags) < 0)
int valid_flags = O_CLOEXEC | O_NONBLOCK;

if ((flags & ~valid_flags)) {
errno = EINVAL;
return -1;
}
if ((flags & O_CLOEXEC)) {
if (fd_set_cloexec (fd) < 0)
return -1;
}
if ((flags & O_NONBLOCK)) {
if (fd_set_nonblocking (fd) < 0)
return -1;
}
return 0;
}

Expand Down
3 changes: 3 additions & 0 deletions src/common/libsubprocess/test/bulk-exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include <unistd.h>
#include <assert.h>
#include <signal.h>
#ifndef HAVE_GET_CURRENT_DIR_NAME
#include "src/common/libmissing/get_current_dir_name.h"
#endif

#include <flux/core.h>
#include <flux/idset.h>
Expand Down
24 changes: 19 additions & 5 deletions src/common/libsubprocess/test/fbuf_watcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ static void buffer_read_overflow (flux_reactor_t *r, flux_watcher_t *w,
return;
}

int create_socketpair_nonblock (int *fd)
{
#ifdef SOCK_NONBLOCK
if (socketpair (PF_LOCAL, SOCK_STREAM|SOCK_NONBLOCK, 0, fd) < 0)
return -1;
#else
if (socketpair (PF_LOCAL, SOCK_STREAM, 0, fd) < 0
|| fd_set_nonblocking (fd[0]) < 0
|| fd_set_nonblocking (fd[1]) < 0)
return -1;
#endif
return 0;
}

static void test_buffer (flux_reactor_t *reactor)
{
int errnum = 0;
Expand All @@ -282,7 +296,7 @@ static void test_buffer (flux_reactor_t *reactor)
int count;
char buf[1024];

ok (socketpair (PF_LOCAL, SOCK_STREAM|SOCK_NONBLOCK, 0, fd) == 0,
ok (create_socketpair_nonblock (fd) == 0,
"buffer: successfully created socketpair");

/* read buffer test */
Expand Down Expand Up @@ -862,7 +876,7 @@ static void test_buffer_refcnt (flux_reactor_t *reactor)

/* read buffer decref test - other end closes stream */

ok (socketpair (PF_LOCAL, SOCK_STREAM|SOCK_NONBLOCK, 0, fd) == 0,
ok (create_socketpair_nonblock (fd) == 0,
"buffer decref: successfully created socketpair");

bfc.count = 0;
Expand Down Expand Up @@ -906,7 +920,7 @@ static void test_buffer_corner_case (flux_reactor_t *reactor)

/* read buffer corner case test - other end closes stream */

ok (socketpair (PF_LOCAL, SOCK_STREAM|SOCK_NONBLOCK, 0, fd) == 0,
ok (create_socketpair_nonblock (fd) == 0,
"buffer corner case: successfully created socketpair");

bfc.count = 0;
Expand Down Expand Up @@ -948,7 +962,7 @@ static void test_buffer_corner_case (flux_reactor_t *reactor)

/* read line buffer corner case test - other end closes stream */

ok (socketpair (PF_LOCAL, SOCK_STREAM|SOCK_NONBLOCK, 0, fd) == 0,
ok (create_socketpair_nonblock (fd) == 0,
"buffer corner case: successfully created socketpair");

bfc.count = 0;
Expand Down Expand Up @@ -985,7 +999,7 @@ static void test_buffer_corner_case (flux_reactor_t *reactor)

/* read line buffer corner case test - left over data not a line */

ok (socketpair (PF_LOCAL, SOCK_STREAM|SOCK_NONBLOCK, 0, fd) == 0,
ok (create_socketpair_nonblock (fd) == 0,
"buffer corner case: successfully created socketpair");

bfc.count = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/common/libsubprocess/test/iochan.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct iochan_ctx {
const char *name;
};

extern char **environ;

const int linesize = 80;
const char *test_fdcopy = TEST_SUBPROCESS_DIR "test_fdcopy";

Expand Down
2 changes: 2 additions & 0 deletions src/common/libsubprocess/test/iostress.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ struct iostress_ctx {
const char *name;
};

extern char **environ;

static void iostress_timer_cb (flux_reactor_t *r,
flux_watcher_t *w,
int revents,
Expand Down
3 changes: 3 additions & 0 deletions src/common/libsubprocess/test/remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "config.h"
#endif
#include <unistd.h> // environ def
#include <signal.h>
#include <jansson.h>
#include <flux/core.h>

Expand Down Expand Up @@ -53,6 +54,8 @@ struct simple_ctx {
struct simple_scorecard scorecard;
};

extern char **environ;

void corner_case_test (flux_t *h)
{
char *true_av[] = { "/bin/true", NULL };
Expand Down
50 changes: 48 additions & 2 deletions src/common/libutil/intree.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <libgen.h> /* dirname(3) */
#include <pthread.h>

#include "src/common/libutil/errno_safe.h"
#include "ccan/str/str.h"


Expand All @@ -38,6 +39,52 @@ static char *strip_trailing_dot_libs (char *dir)
return (dir);
}

#if __APPLE__
#include <mach-o/dyld.h>
static char *nsget_wrap (void)
{
char *path = NULL;
uint32_t size = 0;

(void)_NSGetExecutablePath (path, &size); // get the required buffer size
if (!(path = calloc (1, size))
|| _NSGetExecutablePath (path, &size) < 0) {
ERRNO_SAFE_WRAP (free, path);
return NULL;
}
return path;
}
static int executable_self (char *buf, size_t bufsize)
{
char *path;
char *newpath = NULL;
int rc = -1;

if (!(path = nsget_wrap ())
|| !(newpath = realpath (path, NULL))) {
goto done;
}
if (strlcpy (buf, newpath, bufsize) >= bufsize) {
errno = EOVERFLOW;
goto done;
}
rc = 0;
done:
ERRNO_SAFE_WRAP (free, newpath);
ERRNO_SAFE_WRAP (free, path);
return rc;
}
#else
static int executable_self (char *buf, size_t bufsize)
{
ssize_t len = readlink ("/proc/self/exe", buf, bufsize - 1);
if (len < 0)
return -1;
buf[len] = '\0';
return 0;
}
#endif

/* Return directory containing this executable.
*/
const char *executable_selfdir (void)
Expand All @@ -47,8 +94,7 @@ const char *executable_selfdir (void)
static char *current_exe_dir = NULL;
pthread_mutex_lock (&selfdir_lock);
if (!current_exe_dir) {
memset (current_exe_path, 0, sizeof (current_exe_path));
if (readlink ("/proc/self/exe", current_exe_path, MAXPATHLEN - 1) < 0)
if (executable_self (current_exe_path, sizeof (current_exe_path)) < 0)
goto out;
current_exe_dir = strip_trailing_dot_libs (dirname (current_exe_path));
}
Expand Down
10 changes: 9 additions & 1 deletion src/common/libutil/test/fdwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,22 @@ static void set_fd (void *data, int fd)
fds[fd]++;
}

static void set_fd_if_open (void *data, int fd)
{
if (fcntl (fd, F_GETFL) < 0 && errno == EBADF)
return;
int *fds = data;
fds[fd]++;
}

static int * get_open_fds (int maxfd)
{
/* Valgrind may show open fds > maxfd, so double the space
* allocated so we don't overflow when run under valgrind.
*/
int * fds = calloc (maxfd * 2, sizeof (int));
if (fds)
ok (fdwalk (set_fd, fds) == 0,
ok (fdwalk (set_fd_if_open, fds) == 0,
"fdwalk () worked");
return fds;
}
Expand Down
57 changes: 57 additions & 0 deletions src/common/libutil/test/intree.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,63 @@

#define NTHREADS 16

/* pthread_barrier_t is an optional POSIX feature and is not present
* on macos. If unavailable, a simple replacement is provided below.
*/
#if !defined _POSIX_BARRIERS || _POSIX_BARRIERS <= 0

typedef struct {
int max;
int count;
pthread_mutex_t lock;
pthread_cond_t cond;
} pthread_barrier_t;

#ifndef PTHREAD_BARRIER_SERIAL_THREAD
#define PTHREAD_BARRIER_SERIAL_THREAD -1
#endif

int pthread_barrier_destroy (pthread_barrier_t *barrier)
{
if (!barrier)
return EINVAL;
pthread_mutex_destroy (&barrier->lock);
pthread_cond_destroy (&barrier->cond);
return 0;
}

int pthread_barrier_init (pthread_barrier_t *barrier,
void *barrier_attr,
unsigned count)
{
if (!barrier || barrier_attr != NULL)
return EINVAL;
barrier->max = count;
barrier->count = 0;
pthread_mutex_init (&barrier->lock, NULL);
pthread_cond_init (&barrier->cond, NULL);
return 0;
}

int pthread_barrier_wait (pthread_barrier_t *barrier)
{
int ret = EINVAL;
if (barrier) {
pthread_mutex_lock (&barrier->lock);
if (++barrier->count == barrier->max) {
pthread_cond_broadcast (&barrier->cond);
ret = PTHREAD_BARRIER_SERIAL_THREAD;
}
else if (barrier->count < barrier->max) {
pthread_cond_wait (&barrier->cond, &barrier->lock);
ret = 0;
}
pthread_mutex_unlock (&barrier->lock);
}
return ret;
}
#endif

static pthread_barrier_t barrier;

static void *thd_intree (void *arg)
Expand Down
5 changes: 4 additions & 1 deletion src/shell/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ flux_shell_SOURCES = \
taskmap/hostfile.c \
signal.c \
files.c \
oom.c \
hwloc.c \
rexec.c

if HAVE_INOTIFY
flux_shell_SOURCES += oom.c
endif

flux_shell_LDADD = \
$(builddir)/libshell.la \
$(builddir)/libmpir.la \
Expand Down
Loading

0 comments on commit 02cae96

Please sign in to comment.