diff --git a/bsd-compat/bsd-compat.h b/bsd-compat/bsd-compat.h index a969e07..446fdf2 100644 --- a/bsd-compat/bsd-compat.h +++ b/bsd-compat/bsd-compat.h @@ -21,4 +21,16 @@ size_t strlcpy(char *, const char *, size_t); long long strtonum(const char *, long long, long long, const char **); #endif +#ifndef HAVE_SOCK_CLOEXEC +#define strtonum _sndio_strtonum +long long strtonum(const char *, long long, long long, const char **); +#endif + +#ifndef HAVE_CLOCK_GETTIME +#define CLOCK_MONOTONIC 0 +#define clock_gettime _sndio_clock_gettime +struct timespec; +int clock_gettime(int, struct timespec *); +#endif + #endif /* !defined(BSD_COMPAT_H) */ diff --git a/bsd-compat/clock_gettime.c b/bsd-compat/clock_gettime.c new file mode 100644 index 0000000..92a8cfd --- /dev/null +++ b/bsd-compat/clock_gettime.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2017 Alexandre Ratchov + * + * 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. + */ +#include +#include +#include "bsd-compat.h" + +#ifndef HAVE_CLOCK_GETTIME + +int +_sndio_clock_gettime(int timer, struct timespec *ts) +{ + struct timeval tv; + + if (timer != CLOCK_MONOTONIC) { + errno = EINVAL; + return -1; + } + + if (gettimeofday(&tv, NULL) < 0) + return -1; + + ts->tv_sec = tv.tv_sec; + ts->tv_nsec = tv.tv_usec * 1000; + return 0; +} +#endif diff --git a/configure b/configure index 21a546b..021a7cb 100755 --- a/configure +++ b/configure @@ -57,33 +57,37 @@ case `uname` in alsa=yes ldadd="-lrt" user=sndiod - so="$so libsndio.so" - defs='-D_GNU_SOURCE -DDEV_RANDOM=\\"/dev/urandom\\"' + so_link="libsndio.so" + defs='-D_GNU_SOURCE -DDEV_RANDOM=\\"/dev/urandom\\" \\\ + -DHAVE_SOCK_CLOEXEC' ;; NetBSD) sun=no rmidi=yes user=_sndio - so="$so libsndio.so" + so_link="libsndio.so" defs='-DHAVE_ARC4RANDOM -DHAVE_ISSETUGID \\\ - -DHAVE_STRLCAT -DHAVE_STRLCPY' + -DHAVE_STRLCAT -DHAVE_STRLCPY -DHAVE_SOCK_CLOEXEC' ;; OpenBSD) sun=yes rmidi=yes user=_sndio defs='-DHAVE_ARC4RANDOM -DHAVE_ISSETUGID \\\ - -DHAVE_STRLCAT -DHAVE_STRLCPY -DHAVE_STRTONUM' + -DHAVE_STRLCAT -DHAVE_STRLCPY -DHAVE_STRTONUM -DHAVE_SOCK_CLOEXEC' ;; DragonFly|FreeBSD) umidi=yes user=_sndio - so="$so libsndio.so" + so_link="libsndio.so" defs='-DHAVE_ARC4RANDOM -DHAVE_ISSETUGID \\\ - -DHAVE_STRLCAT -DHAVE_STRLCPY -DHAVE_STRTONUM' - oss=yes - mandir=${prefix}/man + -DHAVE_STRLCAT -DHAVE_STRLCPY -DHAVE_STRTONUM -DHAVE_SOCK_CLOEXEC' ;; + Darwin) + rmidi=no + so="libsndio.\${MAJ}.\${MIN}.dylib" + defs='-DHAVE_ARC4RANDOM -DHAVE_ISSETUGID \\\ + -DHAVE_STRLCAT -DHAVE_STRLCPY' esac # shell word separator (none) @@ -244,6 +248,7 @@ do -e "s:@defs@:$defs:" \ -e "s:@ldadd@:$ldadd:" \ -e "s:@so@:$so:" \ + -e "s:@so_link@:$so_link:" \ -e "s:@vars@:${vars}:" \ -e "s:@precision@:$precision:" \ -e "s:@user@:$user:" \ diff --git a/libsndio/Makefile.in b/libsndio/Makefile.in index 8b63da2..733bbae 100644 --- a/libsndio/Makefile.in +++ b/libsndio/Makefile.in @@ -14,7 +14,7 @@ LDADD = @ldadd@ SO_CFLAGS = -fPIC # extra compiler flags to produce a shared library with the given name -SO_LDFLAGS = -shared -Wl,-soname=libsndio.so.${MAJ}.${MIN} +SO_LDFLAGS = -shared #-Wl,-soname=libsndio.so.${MAJ}.${MIN} # variables defined on configure script command line (if any) @vars@ @@ -45,6 +45,7 @@ MAN7 = sndio.7 MAJ = 6 MIN = 1 SO = @so@ +SO_LINK = @so_link@ all: ${SO} @@ -54,7 +55,7 @@ install: mkdir -p ${DESTDIR}${MAN3_DIR} mkdir -p ${DESTDIR}${MAN7_DIR} cp sndio.h ${DESTDIR}${INCLUDE_DIR} - cp -R ${SO} ${DESTDIR}${LIB_DIR} + cp -R ${SO} ${SO_LINK} ${DESTDIR}${LIB_DIR} cp sio_open.3 ${DESTDIR}${MAN3_DIR} ln -sf sio_open.3 ${DESTDIR}${MAN3_DIR}/sio_close.3 ln -sf sio_open.3 ${DESTDIR}${MAN3_DIR}/sio_setpar.3 @@ -84,7 +85,7 @@ install: uninstall: rm -f ${DESTDIR}${INCLUDE_DIR}/sndio.h - cd ${DESTDIR}${LIB_DIR} && rm -f ${SO} + cd ${DESTDIR}${LIB_DIR} && rm -f ${SO} ${SO_LINK} cd ${DESTDIR}${MAN3_DIR} && rm -f ${MAN3} cd ${DESTDIR}${MAN7_DIR} && rm -f ${MAN7} @@ -100,16 +101,16 @@ clean: OBJS = debug.o aucat.o \ mio.o mio_rmidi.o mio_alsa.o mio_aucat.o \ sio.o sio_alsa.o sio_aucat.o sio_oss.o sio_sun.o \ -issetugid.o strlcat.o strlcpy.o strtonum.o +issetugid.o strlcat.o strlcpy.o strtonum.o clock_gettime.o .c.o: ${CC} ${CFLAGS} ${SO_CFLAGS} -I. ${INCLUDE} ${DEFS} -o $@ -c $< -libsndio.so.${MAJ}.${MIN}: ${OBJS} - ${CC} ${LDFLAGS} ${SO_CFLAGS} ${SO_LDFLAGS} -o libsndio.so.${MAJ}.${MIN} ${OBJS} ${LDADD} +${SO}: ${OBJS} + ${CC} ${LDFLAGS} ${SO_CFLAGS} ${SO_LDFLAGS} -o ${SO} ${OBJS} ${LDADD} -libsndio.so: - ln -sf libsndio.so.${MAJ}.${MIN} libsndio.so +${SO_LINK}: + ln -sf ${SO} ${SO_LINK} issetugid.o: ../bsd-compat/issetugid.c ${CC} ${CFLAGS} ${SO_CFLAGS} ${INCLUDE} ${DEFS} -c -o issetugid.o ../bsd-compat/issetugid.c @@ -123,6 +124,9 @@ strlcpy.o: ../bsd-compat/strlcpy.c strtonum.o: ../bsd-compat/strtonum.c ${CC} ${CFLAGS} ${SO_CFLAGS} ${INCLUDE} ${DEFS} -c -o strtonum.o ../bsd-compat/strtonum.c +clock_gettime.o: ../bsd-compat/clock_gettime.c + ${CC} ${CFLAGS} ${SO_CFLAGS} ${INCLUDE} ${DEFS} -c -o clock_gettime.o ../bsd-compat/clock_gettime.c + aucat.o: aucat.c aucat.h amsg.h debug.h \ ../bsd-compat/bsd-compat.h debug.o: debug.c debug.h ../bsd-compat/bsd-compat.h diff --git a/libsndio/aucat.c b/libsndio/aucat.c index 10ddce4..1f7ef2d 100644 --- a/libsndio/aucat.c +++ b/libsndio/aucat.c @@ -342,6 +342,25 @@ done: return 1; } +static int +socket_cloexec(int f, int t, int p) +{ +#ifdef SOCK_CLOEXEC + return socket(f, t | SOCK_CLOEXEC, p); +#else + int s; + + s = socket(f, t, p); + if (s < 0) + return -1; + if (fcntl(s, F_SETFL, FD_CLOEXEC) < 0) { + close(s); + return -1; + } + return s; +#endif +} + static int aucat_connect_tcp(struct aucat *hdl, char *host, unsigned int unit) { @@ -360,7 +379,7 @@ aucat_connect_tcp(struct aucat *hdl, char *host, unsigned int unit) } s = -1; for (ai = ailist; ai != NULL; ai = ai->ai_next) { - s = socket(ai->ai_family, ai->ai_socktype | SOCK_CLOEXEC, + s = socket_cloexec(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (s < 0) { DPERROR("socket"); @@ -402,7 +421,7 @@ aucat_connect_un(struct aucat *hdl, unsigned int unit) snprintf(ca.sun_path, sizeof(ca.sun_path), SOCKPATH_DIR "-%u/" SOCKPATH_FILE "%u", uid, unit); ca.sun_family = AF_UNIX; - s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + s = socket_cloexec(AF_UNIX, SOCK_STREAM, 0); if (s < 0) return 0; while (connect(s, (struct sockaddr *)&ca, len) < 0) { diff --git a/sndiod/file.c b/sndiod/file.c index 4153a6c..e1f5c55 100644 --- a/sndiod/file.c +++ b/sndiod/file.c @@ -53,6 +53,7 @@ #include #include #include +#include "bsd-compat.h" #include "file.h" #include "utils.h"