]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Add a pselect(2) shim for systems that lack support for it.
authorRoy Marples <roy@marples.name>
Sat, 10 Nov 2012 16:38:53 +0000 (16:38 +0000)
committerRoy Marples <roy@marples.name>
Sat, 10 Nov 2012 16:38:53 +0000 (16:38 +0000)
When building, warn that it's not entirely race condition safe.

compat/pselect.c [new file with mode: 0644]
compat/pselect.h [new file with mode: 0644]
configure

diff --git a/compat/pselect.c b/compat/pselect.c
new file mode 100644 (file)
index 0000000..7930215
--- /dev/null
@@ -0,0 +1,65 @@
+/* 
+ * dhcpcd - DHCP client daemon
+ * Copyright (c) 2006-2012 Roy Marples <roy@marples.name>
+ * 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 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 AUTHOR 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.
+ */
+
+#include <sys/time.h>
+#include <sys/types.h>
+
+#include <signal.h>
+#include <unistd.h>
+
+#include "pselect.h"
+
+#warning "This pselect(2) implementation is not entirely race condition safe."
+#warning "Only operating system support for pselect(2) can correct this."
+
+int
+pselect(int nfds,
+    fd_set *restrict readfds, 
+    fd_set *restrict writefds,
+    fd_set *restrict errorfds,
+    const struct timespec *restrict timeout,
+    const sigset_t *restrict newset)
+{
+       int r;
+       sigset_t oldset;
+       struct timeval saved_timeout;
+
+       if (newset && sigprocmask(SIG_SETMASK, newset, &oldset) == -1)
+               return -1;
+
+       if (timeout) {
+               saved_timeout.tv_sec = timeout->tv_sec;
+               saved_timeout.tv_usec = timeout->tv_nsec / 1000;
+               r = select(nfds, readfds, writefds, errorfds, &saved_timeout);
+       } else
+               r = select(nfds, readfds, writefds, errorfds, NULL);
+
+       if (newset && sigprocmask(SIG_SETMASK, &oldset, NULL) == -1)
+               return -1;
+
+       return r;
+}
diff --git a/compat/pselect.h b/compat/pselect.h
new file mode 100644 (file)
index 0000000..2d61ce3
--- /dev/null
@@ -0,0 +1,40 @@
+/* 
+ * dhcpcd - DHCP client daemon
+ * Copyright (c) 2006-2012 Roy Marples <roy@marples.name>
+ * 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 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 AUTHOR 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.
+ */
+
+#ifndef PSELECT_H
+#define PSELECT_H
+
+#include <sys/select.h>
+#include <sys/time.h>
+
+#include <signal.h>
+
+int pselect(int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
+       const struct timespec *restrict,
+       const sigset_t *restrict);
+
+#endif
index 233f23c18349d6142d6a997a325232139d234caf..32a4dfd024fe37450c0d49fc0f80e2df81885dcb 100755 (executable)
--- a/configure
+++ b/configure
@@ -50,6 +50,8 @@ for x do
        --without-closefrom) CLOSEFROM=no;;
        --without-getline) GETLINE=no;;
        --without-strlcpy) STRLCPY=no;;
+        --without-posix_spawn) POSIX_SPAWN=no;;
+        --without-pselect) PSELECT=no;;
        --serviceexists) SERVICEEXISTS=$var;;
        --servicecmd) SERVICECMD=$var;;
        --servicestatus) SERVICESTATUS=$var;;
@@ -437,6 +439,29 @@ else
        echo "#include <spawn.h>" >>$CONFIG_H
 fi
 
+if [ -z "$PSELECT" ]; then
+       printf "Testing for pselect ... "
+       cat <<EOF >_pselect.c
+#include <sys/select.h>
+#include <stdlib.h>
+int main(void) {
+       pselect(0, NULL, NULL, NULL, NULL, NULL);
+       return 0;
+}
+EOF
+       if $XCC _pselect.c -o _pselect 2>/dev/null; then
+               PSELECT=yes
+       else
+               PSELECT=no
+       fi
+       echo "$PSELECT"
+       rm -f _pselect.c _pselect
+fi
+if [ "$PSELECT" = no ]; then
+       echo "COMPAT_SRCS+=     compat/pselect.c" >>$CONFIG_MK
+       echo "#include \"compat/pselect.h\"" >>$CONFIG_H
+fi
+
 if [ -z "$SERVICECMD" ]; then
        printf "Checking for OpenRC ... "
        if [ -x /sbin/rc-service ]; then