summaryrefslogtreecommitdiff
path: root/hurd
diff options
context:
space:
mode:
Diffstat (limited to 'hurd')
-rw-r--r--hurd/Makefile7
-rw-r--r--hurd/Versions30
-rw-r--r--hurd/ctty-input.c16
-rw-r--r--hurd/ctty-output.c16
-rw-r--r--hurd/dtable.c2
-rw-r--r--hurd/exc2signal.c52
-rw-r--r--hurd/fcntl-internal.h60
-rw-r--r--hurd/hurd.h4
-rw-r--r--hurd/hurd/fd.h47
-rw-r--r--hurd/hurd/port.h43
-rw-r--r--hurd/hurd/signal.h94
-rw-r--r--hurd/hurd/sigpreempt.h4
-rw-r--r--hurd/hurd/threadvar.h89
-rw-r--r--hurd/hurd/userlink.h20
-rw-r--r--hurd/hurdauth.c1
-rw-r--r--hurd/hurdexec.c11
-rw-r--r--hurd/hurdfault.c4
-rw-r--r--hurd/hurdfchdir.c1
-rw-r--r--hurd/hurdinit.c2
-rw-r--r--hurd/hurdioctl.c1
-rw-r--r--hurd/hurdlock.c247
-rw-r--r--hurd/hurdlock.h117
-rw-r--r--hurd/hurdlookup.c2
-rw-r--r--hurd/hurdmalloc.c15
-rw-r--r--hurd/hurdmalloc.h4
-rw-r--r--hurd/hurdmsg.c24
-rw-r--r--hurd/hurdpid.c3
-rw-r--r--hurd/hurdselect.c262
-rw-r--r--hurd/hurdsig.c584
-rw-r--r--hurd/hurdsock.c5
-rw-r--r--hurd/hurdstartup.c1
-rw-r--r--hurd/intern-fd.c1
-rw-r--r--hurd/lookup-at.c13
-rw-r--r--hurd/lookup-retry.c38
-rw-r--r--hurd/setauth.c6
-rw-r--r--hurd/seteuids.c1
-rw-r--r--hurd/sigunwind.c4
-rw-r--r--hurd/sysvshm.c98
-rw-r--r--hurd/sysvshm.h52
-rw-r--r--hurd/thread-cancel.c3
40 files changed, 1515 insertions, 469 deletions
diff --git a/hurd/Makefile b/hurd/Makefile
index 1e7d808ea8..122a5d63ac 100644
--- a/hurd/Makefile
+++ b/hurd/Makefile
@@ -29,11 +29,12 @@ inline-headers = hurd.h $(addprefix hurd/,fd.h signal.h \
# The RPC interfaces go in a separate library.
interface-library := libhurduser
user-interfaces := $(addprefix hurd/,\
- auth startup \
+ auth auth_request auth_reply startup \
process process_request \
msg msg_reply msg_request \
exec exec_startup crash interrupt \
- fs fsys io term tioctl socket ifsock \
+ fs fsys io io_reply io_request \
+ term tioctl socket ifsock \
login password pfinet \
)
server-interfaces := hurd/msg faultexc
@@ -54,6 +55,8 @@ routines = hurdstartup hurdinit \
vpprintf \
ports-get ports-set hurdports hurdmsg \
errno-loc \
+ sysvshm \
+ hurdlock \
$(sig) $(dtable) $(inlines) port-cleanup report-wait xattr
sig = hurdsig hurdfault siginfo hurd-raise preempt-sig \
trampoline longjmp-ts catch-exc exc2signal hurdkill sigunwind \
diff --git a/hurd/Versions b/hurd/Versions
index 77f5b4271e..7502d32a22 100644
--- a/hurd/Versions
+++ b/hurd/Versions
@@ -4,14 +4,9 @@ libc {
_end;
# variables used in macros & inline functions
- __hurd_sigthread_stack_base; __hurd_sigthread_stack_end;
- __hurd_sigthread_variables;
__hurd_threadvar_max;
__hurd_threadvar_stack_mask; __hurd_threadvar_stack_offset;
- # functions used in macros & inline functions
- __hurd_errno_location;
-
# functions used in libmachuser and libhurduser
_S_catch_exception_raise;
_S_catch_exception_raise_state;
@@ -129,10 +124,26 @@ libc {
# functions used in macros & inline functions
__errno_location;
}
+ GLIBC_2.15 {
+ # functions used by libpthread and <hurd/signal.h>
+ _hurd_sigstate_set_global_rcv;
+ _hurd_sigstate_lock;
+ _hurd_sigstate_pending;
+ _hurd_sigstate_unlock;
+ _hurd_sigstate_delete;
+ GLIBC_2.19 {
+ # These always existed as inlines but the real functions were not exported.
+ _hurd_fd_error_signal; _hurd_fd_error;
+ __hurd_dfail; __hurd_sockfail;
+ __hurd_threadvar_location_from_sp;
+ __hurd_threadvar_location;
+ _hurd_userlink_link; _hurd_userlink_unlink; _hurd_userlink_clear;
+ }
HURD_CTHREADS_0.3 {
# weak refs to libthreads functions that libc calls iff libthreads in use
cthread_fork; cthread_detach;
+ pthread_getattr_np; pthread_attr_getstack;
# variables used for detecting cthreads
_cthread_exit_routine; _cthread_init_routine;
@@ -141,4 +152,13 @@ libc {
cthread_keycreate; cthread_getspecific; cthread_setspecific;
__libc_getspecific;
}
+
+ GLIBC_PRIVATE {
+ # Used by other libs.
+ lll_xwait; lll_timed_wait; lll_timed_xwait;
+ __lll_abstimed_wait; __lll_abstimed_xwait;
+ __lll_abstimed_lock; lll_robust_lock;
+ __lll_robust_abstimed_lock; lll_robust_trylock;
+ lll_set_wake; lll_robust_unlock; lll_requeue;
+ }
}
diff --git a/hurd/ctty-input.c b/hurd/ctty-input.c
index 25463fd0b0..b17e43750f 100644
--- a/hurd/ctty-input.c
+++ b/hurd/ctty-input.c
@@ -43,12 +43,15 @@ _hurd_ctty_input (io_t port, io_t ctty, error_t (*rpc) (io_t))
else
{
struct hurd_sigstate *ss = _hurd_self_sigstate ();
- __spin_lock (&ss->lock);
+ struct sigaction *actions;
+
+ _hurd_sigstate_lock (ss);
+ actions = _hurd_sigstate_actions (ss);
if (__sigismember (&ss->blocked, SIGTTIN) ||
- ss->actions[SIGTTIN].sa_handler == SIG_IGN)
+ actions[SIGTTIN].sa_handler == SIG_IGN)
/* We are blocking or ignoring SIGTTIN. Just fail. */
err = EIO;
- __spin_unlock (&ss->lock);
+ _hurd_sigstate_unlock (ss);
if (err == EBACKGROUND)
{
@@ -65,10 +68,11 @@ _hurd_ctty_input (io_t port, io_t ctty, error_t (*rpc) (io_t))
SIGTTIN or resumed after being stopped. Now this is
still a "system call", so check to see if we should
restart it. */
- __spin_lock (&ss->lock);
- if (!(ss->actions[SIGTTIN].sa_flags & SA_RESTART))
+ _hurd_sigstate_lock (ss);
+ actions = _hurd_sigstate_actions (ss);
+ if (!(actions[SIGTTIN].sa_flags & SA_RESTART))
err = EINTR;
- __spin_unlock (&ss->lock);
+ _hurd_sigstate_unlock (ss);
}
}
}
diff --git a/hurd/ctty-output.c b/hurd/ctty-output.c
index 41286fb556..0cbe2e09ef 100644
--- a/hurd/ctty-output.c
+++ b/hurd/ctty-output.c
@@ -34,16 +34,19 @@ _hurd_ctty_output (io_t port, io_t ctty, error_t (*rpc) (io_t))
do
{
+ struct sigaction *actions;
+
/* Don't use the ctty io port if we are blocking or ignoring
SIGTTOU. We redo this check at the top of the loop in case
the signal handler changed the state. */
- __spin_lock (&ss->lock);
+ _hurd_sigstate_lock (ss);
+ actions = _hurd_sigstate_actions (ss);
if (__sigismember (&ss->blocked, SIGTTOU) ||
- ss->actions[SIGTTOU].sa_handler == SIG_IGN)
+ actions[SIGTTOU].sa_handler == SIG_IGN)
err = EIO;
else
err = 0;
- __spin_unlock (&ss->lock);
+ _hurd_sigstate_unlock (ss);
if (err)
return (*rpc) (port);
@@ -70,10 +73,11 @@ _hurd_ctty_output (io_t port, io_t ctty, error_t (*rpc) (io_t))
SIGTTOU or resumed after being stopped. Now this is
still a "system call", so check to see if we should
restart it. */
- __spin_lock (&ss->lock);
- if (!(ss->actions[SIGTTOU].sa_flags & SA_RESTART))
+ _hurd_sigstate_lock (ss);
+ actions = _hurd_sigstate_actions (ss);
+ if (!(actions[SIGTTOU].sa_flags & SA_RESTART))
err = EINTR;
- __spin_unlock (&ss->lock);
+ _hurd_sigstate_unlock (ss);
}
}
/* If the last RPC generated a SIGTTOU, loop to try it again. */
diff --git a/hurd/dtable.c b/hurd/dtable.c
index 32ec49536a..73e60280ef 100644
--- a/hurd/dtable.c
+++ b/hurd/dtable.c
@@ -235,6 +235,7 @@ ctty_new_pgrp (void)
__mutex_unlock (&_hurd_dtable_lock);
HURD_CRITICAL_END;
+ /* TODO: handle EINTR! */
(void) &ctty_new_pgrp; /* Avoid "defined but not used" warning. */
}
@@ -297,6 +298,7 @@ reauth_dtable (void)
__mutex_unlock (&_hurd_dtable_lock);
HURD_CRITICAL_END;
+ /* TODO: handle EINTR! */
(void) &reauth_dtable; /* Avoid "defined but not used" warning. */
}
diff --git a/hurd/exc2signal.c b/hurd/exc2signal.c
index 3a2ec08124..2027630d56 100644
--- a/hurd/exc2signal.c
+++ b/hurd/exc2signal.c
@@ -17,6 +17,7 @@
<http://www.gnu.org/licenses/>. */
#include <hurd.h>
+#include <hurd/signal.h>
/* This file must be modified with machine-dependent details. */
#error "need to write sysdeps/mach/hurd/MACHINE/exc2signal.c"
@@ -24,47 +25,66 @@
/* Translate the Mach exception codes, as received in an `exception_raise' RPC,
into a signal number and signal subcode. */
-void
-_hurd_exception2signal (int exception, int code, int subcode,
- int *signo, int *sigcode, int *error)
+static void
+exception2signal (struct hurd_signal_detail *detail, int *signo, int posix)
{
- *error = 0;
+ detail->error = 0;
- switch (exception)
+ switch (detail->exc)
{
default:
*signo = SIGIOT;
- *sigcode = exception;
+ detail->code = detail->exc;
break;
case EXC_BAD_ACCESS:
- if (code == KERN_PROTECTION_FAILURE)
- *signo = SIGSEGV;
- else
- *signo = SIGBUS;
- *sigcode = subcode;
- *error = code;
+ switch (detail->exc_code)
+ {
+ case KERN_PROTECTION_FAILURE:
+ *signo = SIGSEGV;
+ detail->code = posix ? SEGV_ACCERR : detail->exc_subcode;
+ break;
+
+ default:
+ *signo = SIGBUS;
+ detail->code = 0;
+ break;
+ }
+ detail->error = detail->exc_code;
break;
case EXC_BAD_INSTRUCTION:
*signo = SIGILL;
- *sigcode = 0;
+ detail->code = 0;
break;
case EXC_ARITHMETIC:
*signo = SIGFPE;
- *sigcode = 0;
+ detail->code = 0;
break;
case EXC_EMULATION:
case EXC_SOFTWARE:
*signo = SIGEMT;
- *sigcode = 0;
+ detail->code = 0;
break;
case EXC_BREAKPOINT:
*signo = SIGTRAP;
- *sigcode = 0;
+ detail->code = 0;
break;
}
}
+
+void
+_hurd_exception2signal (struct hurd_signal_detail *detail, int *signo)
+{
+ exception2signal (detail, signo, 1);
+}
+
+void
+_hurd_exception2signal_legacy (struct hurd_signal_detail *detail, int *signo)
+{
+ exception2signal (detail, signo, 0);
+}
+
diff --git a/hurd/fcntl-internal.h b/hurd/fcntl-internal.h
new file mode 100644
index 0000000000..5cd4542b7c
--- /dev/null
+++ b/hurd/fcntl-internal.h
@@ -0,0 +1,60 @@
+/* Copyright (C) 2008-2015 Free Software Foundation, Inc.
+
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+
+#include <fcntl.h>
+#include <sys/socket.h>
+#include <verify.h>
+
+/* Do some compile-time checks for the SOCK_* constants, which we rely on. */
+verify (SOCK_CLOEXEC == O_CLOEXEC);
+verify ((SOCK_MAX | SOCK_TYPE_MASK) == SOCK_TYPE_MASK);
+verify ((SOCK_CLOEXEC & SOCK_TYPE_MASK) == 0);
+verify ((SOCK_NONBLOCK & SOCK_TYPE_MASK) == 0);
+
+
+/* Helper functions for translating between O_* and SOCK_* flags. */
+
+__extern_always_inline
+int
+sock_to_o_flags (int in)
+{
+ int out = 0;
+
+ if (in & SOCK_NONBLOCK)
+ out |= O_NONBLOCK;
+ /* Others are passed through unfiltered. */
+ out |= in & ~(SOCK_NONBLOCK);
+
+ return out;
+}
+
+__extern_always_inline
+int
+o_to_sock_flags (int in)
+{
+ int out = 0;
+
+ if (in & O_NONBLOCK)
+ out |= SOCK_NONBLOCK;
+ /* Others are passed through unfiltered. */
+ out |= in & ~(O_NONBLOCK);
+
+ return out;
+}
diff --git a/hurd/hurd.h b/hurd/hurd.h
index 022abb59bf..3d99c27ce5 100644
--- a/hurd/hurd.h
+++ b/hurd/hurd.h
@@ -46,6 +46,9 @@
#define _HURD_H_EXTERN_INLINE __extern_inline
#endif
+int __hurd_fail (error_t err);
+
+#ifdef __USE_EXTERN_INLINES
_HURD_H_EXTERN_INLINE int
__hurd_fail (error_t err)
{
@@ -75,6 +78,7 @@ __hurd_fail (error_t err)
errno = err;
return -1;
}
+#endif
/* Basic ports and info, initialized by startup. */
diff --git a/hurd/hurd/fd.h b/hurd/hurd/fd.h
index 54951024a4..e75cb146fa 100644
--- a/hurd/hurd/fd.h
+++ b/hurd/hurd/fd.h
@@ -26,6 +26,7 @@
#include <hurd/hurd_types.h>
#include <hurd/port.h>
#include <sys/socket.h>
+#include <fcntl.h>
/* Structure representing a file descriptor. */
@@ -58,6 +59,10 @@ extern struct mutex _hurd_dtable_lock; /* Locks those two variables. */
NULL. The cell is unlocked; when ready to use it, lock it and check for
it being unused. */
+struct hurd_fd *_hurd_fd_get (int fd);
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_FD_H_EXTERN_INLINE struct hurd_fd *
_hurd_fd_get (int fd)
{
@@ -90,6 +95,8 @@ _hurd_fd_get (int fd)
return descriptor;
}
+# endif
+#endif
/* Evaluate EXPR with the variable `descriptor' bound to a pointer to the
@@ -137,6 +144,9 @@ _hurd_fd_get (int fd)
/* Check if ERR should generate a signal.
Returns the signal to take, or zero if none. */
+int _hurd_fd_error_signal (error_t err);
+
+#ifdef __USE_EXTERN_INLINES
_HURD_FD_H_EXTERN_INLINE int
_hurd_fd_error_signal (error_t err)
{
@@ -153,11 +163,15 @@ _hurd_fd_error_signal (error_t err)
return 0;
}
}
+#endif
/* Handle an error from an RPC on a file descriptor's port. You should
always use this function to handle errors from RPCs made on file
descriptor ports. Some errors are translated into signals. */
+error_t _hurd_fd_error (int fd, error_t err);
+
+#ifdef __USE_EXTERN_INLINES
_HURD_FD_H_EXTERN_INLINE error_t
_hurd_fd_error (int fd, error_t err)
{
@@ -170,20 +184,28 @@ _hurd_fd_error (int fd, error_t err)
}
return err;
}
+#endif
/* Handle error code ERR from an RPC on file descriptor FD's port.
Set `errno' to the appropriate error code, and always return -1. */
+int __hurd_dfail (int fd, error_t err);
+
+#ifdef __USE_EXTERN_INLINES
_HURD_FD_H_EXTERN_INLINE int
__hurd_dfail (int fd, error_t err)
{
errno = _hurd_fd_error (fd, err);
return -1;
}
+#endif
/* Likewise, but do not raise SIGPIPE on EPIPE if flags contain
MSG_NOSIGNAL. */
+int __hurd_sockfail (int fd, int flags, error_t err);
+
+#ifdef __USE_EXTERN_INLINES
_HURD_FD_H_EXTERN_INLINE int
__hurd_sockfail (int fd, int flags, error_t err)
{
@@ -192,6 +214,7 @@ __hurd_sockfail (int fd, int flags, error_t err)
errno = err;
return -1;
}
+#endif
/* Set up *FD to have PORT its server port, doing appropriate ctty magic.
Does no locking or unlocking. */
@@ -254,6 +277,30 @@ extern int _hurd_select (int nfds, struct pollfd *pollfds,
const struct timespec *timeout,
const sigset_t *sigmask);
+/* Apply AT_FLAGS on FLAGS, in preparation for calling
+ __hurd_file_name_lookup. */
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
+_HURD_FD_H_EXTERN_INLINE error_t
+__hurd_at_flags (int *at_flags, int *flags)
+{
+ if ((*at_flags & AT_SYMLINK_FOLLOW) && (*at_flags & AT_SYMLINK_NOFOLLOW))
+ return EINVAL;
+
+ *flags |= (*at_flags & AT_SYMLINK_NOFOLLOW) ? O_NOLINK : 0;
+ *at_flags &= ~AT_SYMLINK_NOFOLLOW;
+ if (*at_flags & AT_SYMLINK_FOLLOW)
+ *flags &= ~O_NOLINK;
+ *at_flags &= ~AT_SYMLINK_FOLLOW;
+ if (*at_flags != 0)
+ return EINVAL;
+
+ return 0;
+}
+# endif
+#endif
+
/* Variant of file_name_lookup used in *at function implementations.
AT_FLAGS may only contain AT_SYMLINK_FOLLOW or AT_SYMLINK_NOFOLLOW,
which will remove and add O_NOLINK from FLAGS respectively.
diff --git a/hurd/hurd/port.h b/hurd/hurd/port.h
index 4fea1aa4d2..8afee90e29 100644
--- a/hurd/hurd/port.h
+++ b/hurd/hurd/port.h
@@ -60,6 +60,10 @@ struct hurd_port
/* Initialize *PORT to INIT. */
+void _hurd_port_init (struct hurd_port *port, mach_port_t init);
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_PORT_H_EXTERN_INLINE void
_hurd_port_init (struct hurd_port *port, mach_port_t init)
{
@@ -67,6 +71,8 @@ _hurd_port_init (struct hurd_port *port, mach_port_t init)
port->users = NULL;
port->port = init;
}
+# endif
+#endif
/* Cleanup function for non-local exits. */
@@ -75,6 +81,12 @@ extern void _hurd_port_cleanup (void *, jmp_buf, int);
/* Get a reference to *PORT, which is locked.
Pass return value and LINK to _hurd_port_free when done. */
+mach_port_t
+_hurd_port_locked_get (struct hurd_port *port,
+ struct hurd_userlink *link);
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_PORT_H_EXTERN_INLINE mach_port_t
_hurd_port_locked_get (struct hurd_port *port,
struct hurd_userlink *link)
@@ -90,9 +102,17 @@ _hurd_port_locked_get (struct hurd_port *port,
__spin_unlock (&port->lock);
return result;
}
+# endif
+#endif
/* Same, but locks PORT first. */
+mach_port_t
+_hurd_port_get (struct hurd_port *port,
+ struct hurd_userlink *link);
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_PORT_H_EXTERN_INLINE mach_port_t
_hurd_port_get (struct hurd_port *port,
struct hurd_userlink *link)
@@ -104,10 +124,19 @@ _hurd_port_get (struct hurd_port *port,
HURD_CRITICAL_END;
return result;
}
+# endif
+#endif
/* Free a reference gotten with `USED_PORT = _hurd_port_get (PORT, LINK);' */
+void
+_hurd_port_free (struct hurd_port *port,
+ struct hurd_userlink *link,
+ mach_port_t used_port);
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_PORT_H_EXTERN_INLINE void
_hurd_port_free (struct hurd_port *port,
struct hurd_userlink *link,
@@ -127,11 +156,17 @@ _hurd_port_free (struct hurd_port *port,
if (dealloc)
__mach_port_deallocate (__mach_task_self (), used_port);
}
+# endif
+#endif
/* Set *PORT's port to NEWPORT. NEWPORT's reference is consumed by PORT->port.
PORT->lock is locked. */
+void _hurd_port_locked_set (struct hurd_port *port, mach_port_t newport);
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_PORT_H_EXTERN_INLINE void
_hurd_port_locked_set (struct hurd_port *port, mach_port_t newport)
{
@@ -142,9 +177,15 @@ _hurd_port_locked_set (struct hurd_port *port, mach_port_t newport)
if (old != MACH_PORT_NULL)
__mach_port_deallocate (__mach_task_self (), old);
}
+# endif
+#endif
/* Same, but locks PORT first. */
+void _hurd_port_set (struct hurd_port *port, mach_port_t newport);
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_PORT_H_EXTERN_INLINE void
_hurd_port_set (struct hurd_port *port, mach_port_t newport)
{
@@ -153,6 +194,8 @@ _hurd_port_set (struct hurd_port *port, mach_port_t newport)
_hurd_port_locked_set (port, newport);
HURD_CRITICAL_END;
}
+# endif
+#endif
#endif /* hurd/port.h */
diff --git a/hurd/hurd/signal.h b/hurd/hurd/signal.h
index 96f42d5064..aad9ac44ce 100644
--- a/hurd/hurd/signal.h
+++ b/hurd/hurd/signal.h
@@ -40,7 +40,6 @@
#include <cthreads.h> /* For `struct mutex'. */
#include <setjmp.h> /* For `jmp_buf'. */
#include <spin-lock.h>
-#include <hurd/threadvar.h> /* We cache sigstate in a threadvar. */
struct hurd_signal_preemptor; /* <hurd/sigpreempt.h> */
@@ -64,12 +63,20 @@ struct hurd_sigstate
spin_lock_t lock; /* Locks most of the rest of the structure. */
+ /* The signal state holds a reference on the thread port. */
thread_t thread;
+
struct hurd_sigstate *next; /* Linked-list of thread sigstates. */
sigset_t blocked; /* What signals are blocked. */
sigset_t pending; /* Pending signals, possibly blocked. */
+
+ /* Signal handlers. ACTIONS[0] is used to mark the threads with POSIX
+ semantics: if sa_handler is SIG_IGN instead of SIG_DFL, this thread
+ will receive global signals and use the process-wide action vector
+ instead of this one. */
struct sigaction actions[NSIG];
+
struct sigaltstack sigaltstack;
/* Chain of thread-local signal preemptors; see <hurd/sigpreempt.h>.
@@ -112,7 +119,9 @@ extern struct hurd_sigstate *_hurd_sigstates;
extern struct mutex _hurd_siglock; /* Locks _hurd_sigstates. */
-/* Get the sigstate of a given thread, taking its lock. */
+/* Get the sigstate of a given thread. If there was no sigstate for
+ the thread, one is created, and the thread gains a reference. If
+ the given thread is MACH_PORT_NULL, return the global sigstate. */
extern struct hurd_sigstate *_hurd_thread_sigstate (thread_t);
@@ -125,19 +134,46 @@ extern struct hurd_sigstate *_hurd_self_sigstate (void)
by different threads. */
__attribute__ ((__const__));
+/* Process-wide signal state. */
+
+extern struct hurd_sigstate *_hurd_global_sigstate;
+
+/* Mark the given thread as a process-wide signal receiver. */
+
+extern void _hurd_sigstate_set_global_rcv (struct hurd_sigstate *ss);
+
+/* A thread can either use its own action vector and pending signal set
+ or use the global ones, depending on wether it has been marked as a
+ global receiver. The accessors below take that into account. */
+
+extern void _hurd_sigstate_lock (struct hurd_sigstate *ss);
+extern struct sigaction *_hurd_sigstate_actions (struct hurd_sigstate *ss);
+extern sigset_t _hurd_sigstate_pending (const struct hurd_sigstate *ss);
+extern void _hurd_sigstate_unlock (struct hurd_sigstate *ss);
+
+/* Used by libpthread to remove stale sigstate structures. */
+extern void _hurd_sigstate_delete (thread_t thread);
+
#ifndef _HURD_SIGNAL_H_EXTERN_INLINE
#define _HURD_SIGNAL_H_EXTERN_INLINE __extern_inline
#endif
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_SIGNAL_H_EXTERN_INLINE struct hurd_sigstate *
_hurd_self_sigstate (void)
{
- struct hurd_sigstate **location = (struct hurd_sigstate **)
- (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE);
+ struct hurd_sigstate **location = &THREAD_SELF->_hurd_sigstate;
if (*location == NULL)
- *location = _hurd_thread_sigstate (__mach_thread_self ());
+ {
+ thread_t self = __mach_thread_self ();
+ *location = _hurd_thread_sigstate (self);
+ __mach_port_deallocate (__mach_task_self (), self);
+ }
return *location;
}
+# endif
+#endif
/* Thread listening on our message port; also called the "signal thread". */
@@ -148,12 +184,6 @@ extern thread_t _hurd_msgport_thread;
extern mach_port_t _hurd_msgport;
-
-/* Thread to receive process-global signals. */
-
-extern thread_t _hurd_sigthread;
-
-
/* Resource limit on core file size. Enforced by hurdsig.c. */
extern int _hurd_core_limit;
@@ -164,19 +194,34 @@ extern int _hurd_core_limit;
interrupted lest the signal handler try to take the same lock and
deadlock result. */
+void *_hurd_critical_section_lock (void);
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_SIGNAL_H_EXTERN_INLINE void *
_hurd_critical_section_lock (void)
{
- struct hurd_sigstate **location = (struct hurd_sigstate **)
- (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE);
- struct hurd_sigstate *ss = *location;
+ struct hurd_sigstate **location;
+ struct hurd_sigstate *ss;
+
+#ifdef __LIBC_NO_TLS
+ if (__LIBC_NO_TLS())
+ /* TLS is currently initializing, no need to enter critical section. */
+ return NULL;
+#endif
+
+ location = &THREAD_SELF->_hurd_sigstate;
+ ss = *location;
if (ss == NULL)
{
+ thread_t self = __mach_thread_self ();
+
/* The thread variable is unset; this must be the first time we've
asked for it. In this case, the critical section flag cannot
possible already be set. Look up our sigstate structure the slow
way. */
- ss = *location = _hurd_thread_sigstate (__mach_thread_self ());
+ ss = *location = _hurd_thread_sigstate (self);
+ __mach_port_deallocate (__mach_task_self (), self);
}
if (! __spin_try_lock (&ss->critical_section_lock))
@@ -188,7 +233,13 @@ _hurd_critical_section_lock (void)
_hurd_critical_section_unlock to unlock it. */
return ss;
}
+# endif
+#endif
+
+void _hurd_critical_section_unlock (void *our_lock);
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_SIGNAL_H_EXTERN_INLINE void
_hurd_critical_section_unlock (void *our_lock)
{
@@ -200,10 +251,10 @@ _hurd_critical_section_unlock (void *our_lock)
/* It was us who acquired the critical section lock. Unlock it. */
struct hurd_sigstate *ss = (struct hurd_sigstate *) our_lock;
sigset_t pending;
- __spin_lock (&ss->lock);
+ _hurd_sigstate_lock (ss);
__spin_unlock (&ss->critical_section_lock);
- pending = ss->pending & ~ss->blocked;
- __spin_unlock (&ss->lock);
+ pending = _hurd_sigstate_pending(ss) & ~ss->blocked;
+ _hurd_sigstate_unlock (ss);
if (! __sigisemptyset (&pending))
/* There are unblocked signals pending, which weren't
delivered because we were in the critical section.
@@ -211,6 +262,8 @@ _hurd_critical_section_unlock (void *our_lock)
__msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
}
}
+# endif
+#endif
/* Convenient macros for simple uses of critical sections.
These two must be used as a pair at the same C scoping level. */
@@ -241,6 +294,11 @@ extern int _hurd_raise_signal (struct hurd_sigstate *ss, int signo,
extern void _hurd_exception2signal (struct hurd_signal_detail *detail,
int *signo);
+/* Translate a Mach exception into a signal with a legacy sigcode. */
+
+extern void _hurd_exception2signal_legacy (struct hurd_signal_detail *detail,
+ int *signo);
+
/* Make the thread described by SS take the signal described by SIGNO and
DETAIL. If the process is traced, this will in fact stop with a SIGNO
diff --git a/hurd/hurd/sigpreempt.h b/hurd/hurd/sigpreempt.h
index 3a1eaf2e9f..75dcd59e00 100644
--- a/hurd/hurd/sigpreempt.h
+++ b/hurd/hurd/sigpreempt.h
@@ -46,9 +46,9 @@ struct hurd_signal_preemptor
struct hurd_signal_preemptor *next; /* List structure. */
};
-#define HURD_PREEMPT_SIGNAL_P(preemptor, signo, sigcode) \
+#define HURD_PREEMPT_SIGNAL_P(preemptor, signo, address) \
(((preemptor)->signals & sigmask (signo)) && \
- (sigcode) >= (preemptor)->first && (sigcode) <= (preemptor)->last)
+ (address) >= (preemptor)->first && (address) <= (preemptor)->last)
/* Signal preemptors applying to all threads; locked by _hurd_siglock. */
diff --git a/hurd/hurd/threadvar.h b/hurd/hurd/threadvar.h
index b7b2a041ef..ff43598d2c 100644
--- a/hurd/hurd/threadvar.h
+++ b/hurd/hurd/threadvar.h
@@ -20,97 +20,22 @@
#define _HURD_THREADVAR_H
#include <features.h>
+#include <tls.h>
-/* The per-thread variables are found by ANDing this mask
- with the value of the stack pointer and then adding this offset.
-
- In the multi-threaded case, cthreads initialization sets
- __hurd_threadvar_stack_mask to ~(cthread_stack_size - 1), a mask which
- finds the base of the fixed-size cthreads stack; and
- __hurd_threadvar_stack_offset to a small offset that skips the data
- cthreads itself maintains at the base of each thread's stack.
-
- In the single-threaded case, __hurd_threadvar_stack_mask is zero, so the
- stack pointer is ignored; and __hurd_threadvar_stack_offset gives the
- address of a small allocated region which contains the variables for the
- single thread. */
-
-extern unsigned long int __hurd_threadvar_stack_mask;
-extern unsigned long int __hurd_threadvar_stack_offset;
-
-/* A special case must always be made for the signal thread. Even when there
- is only one user thread and an allocated region can be used for the user
- thread's variables, the signal thread needs to have its own location for
- per-thread variables. The variables __hurd_sigthread_stack_base and
+/* The variables __hurd_sigthread_stack_base and
__hurd_sigthread_stack_end define the bounds of the stack used by the
signal thread, so that thread can always be specifically identified. */
extern unsigned long int __hurd_sigthread_stack_base;
extern unsigned long int __hurd_sigthread_stack_end;
-extern unsigned long int *__hurd_sigthread_variables;
-/* At the location described by the two variables above,
- there are __hurd_threadvar_max `unsigned long int's of per-thread data. */
+/* We do not use threadvars any more, this is kept as zero for compatibility with cthreads */
+extern unsigned long int __hurd_threadvar_stack_mask;
+extern unsigned long int __hurd_threadvar_stack_offset;
extern unsigned int __hurd_threadvar_max;
-/* These values are the indices for the standard per-thread variables. */
-enum __hurd_threadvar_index
- {
- _HURD_THREADVAR_MIG_REPLY, /* Reply port for MiG user stub functions. */
- _HURD_THREADVAR_ERRNO, /* `errno' value for this thread. */
- _HURD_THREADVAR_SIGSTATE, /* This thread's `struct hurd_sigstate'. */
- _HURD_THREADVAR_DYNAMIC_USER, /* Dynamically-assigned user variables. */
- _HURD_THREADVAR_MALLOC, /* For use of malloc. */
- _HURD_THREADVAR_DL_ERROR, /* For use of -ldl and dynamic linker. */
- _HURD_THREADVAR_RPC_VARS, /* For state of RPC functions. */
- _HURD_THREADVAR_LOCALE, /* For thread-local locale setting. */
- _HURD_THREADVAR_CTYPE_B, /* Cache of thread-local locale data. */
- _HURD_THREADVAR_CTYPE_TOLOWER, /* Cache of thread-local locale data. */
- _HURD_THREADVAR_CTYPE_TOUPPER, /* Cache of thread-local locale data. */
- _HURD_THREADVAR_MAX /* Default value for __hurd_threadvar_max. */
- };
-
-
-#ifndef _HURD_THREADVAR_H_EXTERN_INLINE
-#define _HURD_THREADVAR_H_EXTERN_INLINE __extern_inline
-#endif
-
-/* Return the location of the value for the per-thread variable with index
- INDEX used by the thread whose stack pointer is SP. */
-
-extern unsigned long int *__hurd_threadvar_location_from_sp
- (enum __hurd_threadvar_index __index, void *__sp);
-_HURD_THREADVAR_H_EXTERN_INLINE unsigned long int *
-__hurd_threadvar_location_from_sp (enum __hurd_threadvar_index __index,
- void *__sp)
-{
- unsigned long int __stack = (unsigned long int) __sp;
- return &((__stack >= __hurd_sigthread_stack_base &&
- __stack < __hurd_sigthread_stack_end)
- ? __hurd_sigthread_variables
- : (unsigned long int *) ((__stack & __hurd_threadvar_stack_mask) +
- __hurd_threadvar_stack_offset))[__index];
-}
-
-#include <machine-sp.h> /* Define __thread_stack_pointer. */
-
-/* Return the location of the current thread's value for the
- per-thread variable with index INDEX. */
-
-extern unsigned long int *
-__hurd_threadvar_location (enum __hurd_threadvar_index __index) __THROW
- /* This declaration tells the compiler that the value is constant
- given the same argument. We assume this won't be called twice from
- the same stack frame by different threads. */
- __attribute__ ((__const__));
-
-_HURD_THREADVAR_H_EXTERN_INLINE unsigned long int *
-__hurd_threadvar_location (enum __hurd_threadvar_index __index)
-{
- return __hurd_threadvar_location_from_sp (__index,
- __thread_stack_pointer ());
-}
-
+extern mach_port_t __hurd_reply_port0;
+#define __hurd_local_reply_port (*(__LIBC_NO_TLS() ? &__hurd_reply_port0 : &THREAD_SELF->reply_port))
#endif /* hurd/threadvar.h */
diff --git a/hurd/hurd/userlink.h b/hurd/hurd/userlink.h
index 275ce0cdd8..d2610d7561 100644
--- a/hurd/hurd/userlink.h
+++ b/hurd/hurd/userlink.h
@@ -76,6 +76,12 @@ struct hurd_userlink
/* Attach LINK to the chain of users at *CHAINP. */
+void
+_hurd_userlink_link (struct hurd_userlink **chainp,
+ struct hurd_userlink *link);
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_USERLINK_H_EXTERN_INLINE void
_hurd_userlink_link (struct hurd_userlink **chainp,
struct hurd_userlink *link)
@@ -96,11 +102,17 @@ _hurd_userlink_link (struct hurd_userlink **chainp,
link->thread.prevp = thread_chainp;
*thread_chainp = link;
}
+# endif
+#endif
/* Detach LINK from its chain. Returns nonzero iff this was the
last user of the resource and it should be deallocated. */
+int _hurd_userlink_unlink (struct hurd_userlink *link);
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_USERLINK_H_EXTERN_INLINE int
_hurd_userlink_unlink (struct hurd_userlink *link)
{
@@ -123,6 +135,8 @@ _hurd_userlink_unlink (struct hurd_userlink *link)
return dealloc;
}
+# endif
+#endif
/* Clear all users from *CHAINP. Call this when the resource *CHAINP
@@ -131,6 +145,10 @@ _hurd_userlink_unlink (struct hurd_userlink *link)
value is zero, someone is still using the resource and they will
deallocate it when they are finished. */
+int _hurd_userlink_clear (struct hurd_userlink **chainp);
+
+#if defined __USE_EXTERN_INLINES && defined _LIBC
+# if IS_IN (libc)
_HURD_USERLINK_H_EXTERN_INLINE int
_hurd_userlink_clear (struct hurd_userlink **chainp)
{
@@ -143,5 +161,7 @@ _hurd_userlink_clear (struct hurd_userlink **chainp)
*chainp = NULL;
return 0;
}
+# endif
+#endif
#endif /* hurd/userlink.h */
diff --git a/hurd/hurdauth.c b/hurd/hurdauth.c
index 7ecba39c8d..695e3abbbb 100644
--- a/hurd/hurdauth.c
+++ b/hurd/hurdauth.c
@@ -223,6 +223,7 @@ _S_msg_del_auth (mach_port_t me,
}
__mutex_unlock (&_hurd_id.lock);
HURD_CRITICAL_END;
+ /* TODO: handle EINTR */
if (err)
return err;
diff --git a/hurd/hurdexec.c b/hurd/hurdexec.c
index 9978607be6..7f1df34101 100644
--- a/hurd/hurdexec.c
+++ b/hurd/hurdexec.c
@@ -104,15 +104,15 @@ _hurd_exec (task_t task, file_t file,
ss = _hurd_self_sigstate ();
- assert (! __spin_lock_locked (&ss->critical_section_lock));
__spin_lock (&ss->critical_section_lock);
- __spin_lock (&ss->lock);
+ _hurd_sigstate_lock (ss);
+ struct sigaction *actions = _hurd_sigstate_actions (ss);
ints[INIT_SIGMASK] = ss->blocked;
- ints[INIT_SIGPENDING] = ss->pending;
+ ints[INIT_SIGPENDING] = _hurd_sigstate_pending (ss);
ints[INIT_SIGIGN] = 0;
for (i = 1; i < NSIG; ++i)
- if (ss->actions[i].sa_handler == SIG_IGN)
+ if (actions[i].sa_handler == SIG_IGN)
ints[INIT_SIGIGN] |= __sigmask (i);
/* We hold the sigstate lock until the exec has failed so that no signal
@@ -123,7 +123,7 @@ _hurd_exec (task_t task, file_t file,
critical section flag avoids anything we call trying to acquire the
sigstate lock. */
- __spin_unlock (&ss->lock);
+ _hurd_sigstate_unlock (ss);
/* Pack up the descriptor table to give the new program. */
__mutex_lock (&_hurd_dtable_lock);
@@ -394,6 +394,7 @@ _hurd_exec (task_t task, file_t file,
/* Safe to let signals happen now. */
_hurd_critical_section_unlock (ss);
+ /* FIXME: handle EINTR */
outargs:
free (args);
diff --git a/hurd/hurdfault.c b/hurd/hurdfault.c
index 9016227fc9..1618692231 100644
--- a/hurd/hurdfault.c
+++ b/hurd/hurdfault.c
@@ -70,7 +70,7 @@ _hurdsig_fault_catch_exception_raise (mach_port_t port,
codes into a signal number and subcode. */
_hurd_exception2signal (&d, &signo);
- return HURD_PREEMPT_SIGNAL_P (&_hurdsig_fault_preemptor, signo, d.code)
+ return HURD_PREEMPT_SIGNAL_P (&_hurdsig_fault_preemptor, signo, d.exc_subcode)
? 0 : EGREGIOUS;
}
@@ -204,6 +204,8 @@ _hurdsig_fault_init (void)
/* This state will be restored when we fault.
It runs the function above. */
memset (&state, 0, sizeof state);
+
+ MACHINE_THREAD_STATE_FIX_NEW (&state);
MACHINE_THREAD_STATE_SET_PC (&state, faulted);
MACHINE_THREAD_STATE_SET_SP (&state, faultstack, sizeof faultstack);
diff --git a/hurd/hurdfchdir.c b/hurd/hurdfchdir.c
index d771667345..027da108c5 100644
--- a/hurd/hurdfchdir.c
+++ b/hurd/hurdfchdir.c
@@ -53,6 +53,7 @@ _hurd_change_directory_port_from_fd (struct hurd_port *portcell, int fd)
}));
HURD_CRITICAL_END;
+ /* TODO:handle EINTR */
return ret;
}
diff --git a/hurd/hurdinit.c b/hurd/hurdinit.c
index 02d0134519..a310404f68 100644
--- a/hurd/hurdinit.c
+++ b/hurd/hurdinit.c
@@ -174,7 +174,7 @@ _hurd_new_proc_init (char **argv,
/* This process is "traced", meaning it should stop on signals or exec.
We are all set up now to handle signals. Stop ourselves, to inform
our parent (presumably a debugger) that the exec has completed. */
- __msg_sig_post (_hurd_msgport, SIGTRAP, 0, __mach_task_self ());
+ __msg_sig_post (_hurd_msgport, SIGTRAP, TRAP_TRACE, __mach_task_self ());
}
#include <shlib-compat.h>
diff --git a/hurd/hurdioctl.c b/hurd/hurdioctl.c
index 974787ed8b..c36dd613cf 100644
--- a/hurd/hurdioctl.c
+++ b/hurd/hurdioctl.c
@@ -211,6 +211,7 @@ install_ctty (mach_port_t cttyid)
__mutex_lock (&_hurd_dtable_lock);
_hurd_locked_install_cttyid (cttyid);
HURD_CRITICAL_END;
+ /* TODO: handle EINTR! */
}
diff --git a/hurd/hurdlock.c b/hurd/hurdlock.c
new file mode 100644
index 0000000000..4059b7926c
--- /dev/null
+++ b/hurd/hurdlock.c
@@ -0,0 +1,247 @@
+/* Copyright (C) 1999-2016 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include "hurdlock.h"
+#include <hurd.h>
+#include <time.h>
+#include <errno.h>
+
+int lll_xwait (void *ptr, int lo, int hi, int flags)
+{
+ return (__gsync_wait (__mach_task_self (),
+ (vm_offset_t)ptr, lo, hi, 0, flags | GSYNC_QUAD));
+}
+
+int lll_timed_wait (void *ptr, int val, int mlsec, int flags)
+{
+ return (__gsync_wait (__mach_task_self (),
+ (vm_offset_t)ptr, val, 0, mlsec, flags | GSYNC_TIMED));
+}
+
+int lll_timed_xwait (void *ptr, int lo,
+ int hi, int mlsec, int flags)
+{
+ return (__gsync_wait (__mach_task_self (), (vm_offset_t)ptr,
+ lo, hi, mlsec, flags | GSYNC_TIMED | GSYNC_QUAD));
+}
+
+/* Convert an absolute timeout in nanoseconds to a relative
+ * timeout in milliseconds. */
+static inline int __attribute__ ((gnu_inline))
+compute_reltime (const struct timespec *abstime, clockid_t clk)
+{
+ struct timespec ts;
+ __clock_gettime (clk, &ts);
+
+ ts.tv_sec = abstime->tv_sec - ts.tv_sec;
+ ts.tv_nsec = abstime->tv_nsec - ts.tv_nsec;
+
+ if (ts.tv_nsec < 0)
+ {
+ --ts.tv_sec;
+ ts.tv_nsec += 1000000000;
+ }
+
+ return (ts.tv_sec < 0 ? -1 :
+ (int)(ts.tv_sec * 1000 + ts.tv_nsec / 1000000));
+}
+
+int __lll_abstimed_wait (void *ptr, int val,
+ const struct timespec *tsp, int flags, int clk)
+{
+ int mlsec = compute_reltime (tsp, clk);
+ return (mlsec < 0 ? KERN_TIMEDOUT :
+ lll_timed_wait (ptr, val, mlsec, flags));
+}
+
+int __lll_abstimed_xwait (void *ptr, int lo, int hi,
+ const struct timespec *tsp, int flags, int clk)
+{
+ int mlsec = compute_reltime (tsp, clk);
+ return (mlsec < 0 ? KERN_TIMEDOUT :
+ lll_timed_xwait (ptr, lo, hi, mlsec, flags));
+}
+
+int __lll_abstimed_lock (void *ptr,
+ const struct timespec *tsp, int flags, int clk)
+{
+ if (lll_trylock (ptr) == 0)
+ return (0);
+
+ while (1)
+ {
+ if (atomic_exchange_acq ((int *)ptr, 2) == 0)
+ return (0);
+ else if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
+ return (EINVAL);
+
+ int mlsec = compute_reltime (tsp, clk);
+ if (mlsec < 0 || lll_timed_wait (ptr,
+ 2, mlsec, flags) == KERN_TIMEDOUT)
+ return (ETIMEDOUT);
+ }
+}
+
+void lll_set_wake (void *ptr, int val, int flags)
+{
+ __gsync_wake (__mach_task_self (),
+ (vm_offset_t)ptr, val, flags | GSYNC_MUTATE);
+}
+
+void lll_requeue (void *src, void *dst, int wake_one, int flags)
+{
+ __gsync_requeue (__mach_task_self (), (vm_offset_t)src,
+ (vm_offset_t)dst, (boolean_t)wake_one, flags);
+}
+
+/* Robust locks. */
+
+extern int __getpid (void) __attribute__ ((const));
+extern task_t __pid2task (int);
+
+/* Test if a given process id is still valid. */
+static inline int valid_pid (int pid)
+{
+ task_t task = __pid2task (pid);
+ if (task == MACH_PORT_NULL)
+ return (0);
+
+ __mach_port_deallocate (__mach_task_self (), task);
+ return (1);
+}
+
+/* Robust locks have currently no support from the kernel; they
+ * are simply implemented with periodic polling. When sleeping, the
+ * maximum blocking time is determined by this constant. */
+#define MAX_WAIT_TIME 1500
+
+int lll_robust_lock (void *ptr, int flags)
+{
+ int *iptr = (int *)ptr;
+ int id = __getpid ();
+ int wait_time = 25;
+ unsigned int val;
+
+ /* Try to set the lock word to our PID if it's clear. Otherwise,
+ * mark it as having waiters. */
+ while (1)
+ {
+ val = *iptr;
+ if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
+ return (0);
+ else if (atomic_compare_and_exchange_bool_acq (iptr,
+ val | LLL_WAITERS, val) == 0)
+ break;
+ }
+
+ for (id |= LLL_WAITERS ; ; )
+ {
+ val = *iptr;
+ if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
+ return (0);
+ else if (val && !valid_pid (val & LLL_OWNER_MASK))
+ {
+ if (atomic_compare_and_exchange_bool_acq (iptr, id, val) == 0)
+ return (EOWNERDEAD);
+ }
+ else
+ {
+ lll_timed_wait (iptr, val, wait_time, flags);
+ if (wait_time < MAX_WAIT_TIME)
+ wait_time <<= 1;
+ }
+ }
+}
+
+int __lll_robust_abstimed_lock (void *ptr,
+ const struct timespec *tsp, int flags, int clk)
+{
+ int *iptr = (int *)ptr;
+ int id = __getpid ();
+ int wait_time = 25;
+ unsigned int val;
+
+ while (1)
+ {
+ val = *iptr;
+ if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
+ return (0);
+ else if (atomic_compare_and_exchange_bool_acq (iptr,
+ val | LLL_WAITERS, val) == 0)
+ break;
+ }
+
+ for (id |= LLL_WAITERS ; ; )
+ {
+ val = *iptr;
+ if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
+ return (0);
+ else if (val && !valid_pid (val & LLL_OWNER_MASK))
+ {
+ if (atomic_compare_and_exchange_bool_acq (iptr, id, val) == 0)
+ return (EOWNERDEAD);
+ }
+ else
+ {
+ int mlsec = compute_reltime (tsp, clk);
+ if (mlsec < 0)
+ return (ETIMEDOUT);
+ else if (mlsec > wait_time)
+ mlsec = wait_time;
+
+ int res = lll_timed_wait (iptr, val, mlsec, flags);
+ if (res == KERN_TIMEDOUT)
+ return (ETIMEDOUT);
+ else if (wait_time < MAX_WAIT_TIME)
+ wait_time <<= 1;
+ }
+ }
+}
+
+int lll_robust_trylock (void *ptr)
+{
+ int *iptr = (int *)ptr;
+ int id = __getpid ();
+ unsigned int val = *iptr;
+
+ if (!val)
+ {
+ if (atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
+ return (0);
+ }
+ else if (!valid_pid (val & LLL_OWNER_MASK) &&
+ atomic_compare_and_exchange_bool_acq (iptr, id, val) == 0)
+ return (EOWNERDEAD);
+
+ return (EBUSY);
+}
+
+void lll_robust_unlock (void *ptr, int flags)
+{
+ unsigned int val = atomic_load_relaxed((unsigned int *)ptr);
+ while (1)
+ {
+ if (val & LLL_WAITERS)
+ {
+ lll_set_wake (ptr, 0, flags);
+ break;
+ }
+ else if (atomic_compare_exchange_weak_release ((unsigned int *)ptr, &val, 0))
+ break;
+ }
+}
+
diff --git a/hurd/hurdlock.h b/hurd/hurdlock.h
new file mode 100644
index 0000000000..405ffaf5f9
--- /dev/null
+++ b/hurd/hurdlock.h
@@ -0,0 +1,117 @@
+/* Copyright (C) 1999-2016 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#ifndef _HURD_LOCK_H
+#define _HURD_LOCK_H 1
+
+#include <mach/lowlevellock.h>
+
+struct timespec;
+
+/* Flags for robust locks. */
+#define LLL_WAITERS (1U << 31)
+#define LLL_DEAD_OWNER (1U << 30)
+
+#define LLL_OWNER_MASK ~(LLL_WAITERS | LLL_DEAD_OWNER)
+
+/* Wait on 64-bit address PTR, without blocking if its contents
+ * are different from the pair <LO, HI>. */
+extern int lll_xwait (void *__ptr, int __lo,
+ int __hi, int __flags);
+
+/* Same as 'lll_wait', but only block for MLSEC milliseconds. */
+extern int lll_timed_wait (void *__ptr, int __val,
+ int __mlsec, int __flags);
+
+/* Same as 'lll_xwait', but only block for MLSEC milliseconds. */
+extern int lll_timed_xwait (void *__ptr, int __lo,
+ int __hi, int __mlsec, int __flags);
+
+/* Same as 'lll_wait', but only block until TSP elapses,
+ * using clock CLK. */
+extern int __lll_abstimed_wait (void *__ptr, int __val,
+ const struct timespec *__tsp, int __flags, int __clk);
+
+/* Same as 'lll_xwait', but only block until TSP elapses,
+ * using clock CLK. */
+extern int __lll_abstimed_xwait (void *__ptr, int __lo, int __hi,
+ const struct timespec *__tsp, int __flags, int __clk);
+
+/* Same as 'lll_lock', but return with an error if TSP elapses,
+ * using clock CLK. */
+extern int __lll_abstimed_lock (void *__ptr,
+ const struct timespec *__tsp, int __flags, int __clk);
+
+/* Acquire the lock at PTR, but return with an error if
+ * the process containing the owner thread dies. */
+extern int lll_robust_lock (void *__ptr, int __flags);
+
+/* Same as 'lll_robust_lock', but only block until TSP
+ * elapses, using clock CLK. */
+extern int __lll_robust_abstimed_lock (void *__ptr,
+ const struct timespec *__tsp, int __flags, int __clk);
+
+/* Same as 'lll_robust_lock', but return with an error
+ * if the lock cannot be acquired without blocking. */
+extern int lll_robust_trylock (void *__ptr);
+
+/* Wake one or more threads waiting on address PTR,
+ * setting its value to VAL before doing so. */
+extern void lll_set_wake (void *__ptr, int __val, int __flags);
+
+/* Release the robust lock at PTR. */
+extern void lll_robust_unlock (void *__ptr, int __flags);
+
+/* Rearrange threads waiting on address SRC to instead wait on
+ * DST, waking one of them if WAIT_ONE is non-zero. */
+extern void lll_requeue (void *__src, void *__dst,
+ int __wake_one, int __flags);
+
+/* The following are hacks that allow us to simulate optional
+ * parameters in C, to avoid having to pass the clock id for
+ * every one of these calls, defaulting to CLOCK_REALTIME if
+ * no argument is passed. */
+
+#define lll_abstimed_wait(ptr, val, tsp, flags, ...) \
+ ({ \
+ const clockid_t __clk[] = { CLOCK_REALTIME, ##__VA_ARGS__ }; \
+ __lll_abstimed_wait ((ptr), (val), (tsp), (flags), \
+ __clk[sizeof (__clk) / sizeof (__clk[0]) - 1]); \
+ })
+
+#define lll_abstimed_xwait(ptr, lo, hi, tsp, flags, ...) \
+ ({ \
+ const clockid_t __clk[] = { CLOCK_REALTIME, ##__VA_ARGS__ }; \
+ __lll_abstimed_xwait ((ptr), (lo), (hi), (tsp), (flags), \
+ __clk[sizeof (__clk) / sizeof (__clk[0]) - 1]); \
+ })
+
+#define lll_abstimed_lock(ptr, tsp, flags, ...) \
+ ({ \
+ const clockid_t __clk[] = { CLOCK_REALTIME, ##__VA_ARGS__ }; \
+ __lll_abstimed_lock ((ptr), (tsp), (flags), \
+ __clk[sizeof (__clk) / sizeof (__clk[0]) - 1]); \
+ })
+
+#define lll_robust_abstimed_lock(ptr, tsp, flags, ...) \
+ ({ \
+ const clockid_t __clk[] = { CLOCK_REALTIME, ##__VA_ARGS__ }; \
+ __lll_robust_abstimed_lock ((ptr), (tsp), (flags), \
+ __clk[sizeof (__clk) / sizeof (__clk[0]) - 1]); \
+ })
+
+#endif
diff --git a/hurd/hurdlookup.c b/hurd/hurdlookup.c
index dbff009539..bd720c27ec 100644
--- a/hurd/hurdlookup.c
+++ b/hurd/hurdlookup.c
@@ -72,7 +72,7 @@ __hurd_file_name_lookup (error_t (*use_init_port)
if (flags & O_NOFOLLOW) /* See lookup-retry.c about O_NOFOLLOW. */
flags |= O_NOTRANS;
- if (flags & O_DIRECTORY)
+ if (flags & O_DIRECTORY && !(flags & O_NOFOLLOW))
{
/* The caller wants to require that the file we look up is a directory.
We can do this without an extra RPC by appending a trailing slash
diff --git a/hurd/hurdmalloc.c b/hurd/hurdmalloc.c
index 58c29fa07d..65fb959d84 100644
--- a/hurd/hurdmalloc.c
+++ b/hurd/hurdmalloc.c
@@ -405,8 +405,8 @@ print_malloc_free_list (void)
}
#endif /* DEBUG */
-static void
-malloc_fork_prepare(void)
+void
+_hurd_malloc_fork_prepare(void)
/*
* Prepare the malloc module for a fork by insuring that no thread is in a
* malloc critical section.
@@ -419,8 +419,8 @@ malloc_fork_prepare(void)
}
}
-static void
-malloc_fork_parent(void)
+void
+_hurd_malloc_fork_parent(void)
/*
* Called in the parent process after a fork() to resume normal operation.
*/
@@ -432,8 +432,8 @@ malloc_fork_parent(void)
}
}
-static void
-malloc_fork_child(void)
+void
+_hurd_malloc_fork_child(void)
/*
* Called in the child process after a fork() to resume normal operation.
*/
@@ -446,7 +446,4 @@ malloc_fork_child(void)
}
-text_set_element (_hurd_fork_prepare_hook, malloc_fork_prepare);
-text_set_element (_hurd_fork_parent_hook, malloc_fork_parent);
-text_set_element (_hurd_fork_child_hook, malloc_fork_child);
text_set_element (_hurd_preinit_hook, malloc_init);
diff --git a/hurd/hurdmalloc.h b/hurd/hurdmalloc.h
index e11c208a8f..ab572e2814 100644
--- a/hurd/hurdmalloc.h
+++ b/hurd/hurdmalloc.h
@@ -12,6 +12,10 @@ extern void *_hurd_malloc (size_t);
extern void *_hurd_realloc (void *, size_t);
extern void _hurd_free (void *);
+extern void _hurd_malloc_fork_prepare(void);
+extern void _hurd_malloc_fork_parent(void);
+extern void _hurd_malloc_fork_child(void);
+
#define malloc _hurd_malloc
#define realloc _hurd_realloc
#define free _hurd_free
diff --git a/hurd/hurdmsg.c b/hurd/hurdmsg.c
index 65f6edb1fa..5204a82c2a 100644
--- a/hurd/hurdmsg.c
+++ b/hurd/hurdmsg.c
@@ -121,17 +121,9 @@ get_int (int which, int *value)
case INIT_UMASK:
*value = _hurd_umask;
return 0;
- case INIT_SIGMASK:
- {
- struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
- __spin_lock (&ss->lock);
- *value = ss->blocked;
- __spin_unlock (&ss->lock);
- return 0;
- }
case INIT_SIGPENDING:
{
- struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
+ struct hurd_sigstate *ss = _hurd_global_sigstate;
__spin_lock (&ss->lock);
*value = ss->pending;
__spin_unlock (&ss->lock);
@@ -139,7 +131,7 @@ get_int (int which, int *value)
}
case INIT_SIGIGN:
{
- struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
+ struct hurd_sigstate *ss = _hurd_global_sigstate;
sigset_t ign;
int sig;
__spin_lock (&ss->lock);
@@ -207,17 +199,9 @@ set_int (int which, int value)
return 0;
/* These are pretty odd things to do. But you asked for it. */
- case INIT_SIGMASK:
- {
- struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
- __spin_lock (&ss->lock);
- ss->blocked = value;
- __spin_unlock (&ss->lock);
- return 0;
- }
case INIT_SIGPENDING:
{
- struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
+ struct hurd_sigstate *ss = _hurd_global_sigstate;
__spin_lock (&ss->lock);
ss->pending = value;
__spin_unlock (&ss->lock);
@@ -225,7 +209,7 @@ set_int (int which, int value)
}
case INIT_SIGIGN:
{
- struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread);
+ struct hurd_sigstate *ss = _hurd_global_sigstate;
int sig;
const sigset_t ign = value;
__spin_lock (&ss->lock);
diff --git a/hurd/hurdpid.c b/hurd/hurdpid.c
index e5cda8c2f0..859a7744ad 100644
--- a/hurd/hurdpid.c
+++ b/hurd/hurdpid.c
@@ -16,6 +16,8 @@
<http://www.gnu.org/licenses/>. */
#include <hurd.h>
+#include <lowlevellock.h>
+
pid_t _hurd_pid, _hurd_ppid, _hurd_pgrp;
int _hurd_orphaned;
@@ -66,6 +68,7 @@ _S_msg_proc_newids (mach_port_t me,
/* Notify any waiting user threads that the id change as been completed. */
++_hurd_pids_changed_stamp;
+ lll_wake (&_hurd_pids_changed_stamp, GSYNC_BROADCAST);
return 0;
}
diff --git a/hurd/hurdselect.c b/hurd/hurdselect.c
index d5ba826d16..9004a9050c 100644
--- a/hurd/hurdselect.c
+++ b/hurd/hurdselect.c
@@ -16,14 +16,17 @@
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
+#include <sys/time.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <hurd.h>
#include <hurd/fd.h>
+#include <hurd/io_request.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
+#include <limits.h>
/* All user select types. */
#define SELECT_ALL (SELECT_READ | SELECT_WRITE | SELECT_URG)
@@ -31,6 +34,7 @@
/* Used to record that a particular select rpc returned. Must be distinct
from SELECT_ALL (which better not have the high bit set). */
#define SELECT_RETURNED ((SELECT_ALL << 1) & ~SELECT_ALL)
+#define SELECT_ERROR (SELECT_RETURNED << 1)
/* Check the first NFDS descriptors either in POLLFDS (if nonnnull) or in
each of READFDS, WRITEFDS, EXCEPTFDS that is nonnull. If TIMEOUT is not
@@ -44,11 +48,13 @@ _hurd_select (int nfds,
{
int i;
mach_port_t portset;
- int got;
+ int got, ready;
error_t err;
fd_set rfds, wfds, xfds;
int firstfd, lastfd;
- mach_msg_timeout_t to = 0;
+ mach_msg_id_t reply_msgid;
+ mach_msg_timeout_t to;
+ struct timespec ts;
struct
{
struct hurd_userlink ulink;
@@ -56,6 +62,7 @@ _hurd_select (int nfds,
mach_port_t io_port;
int type;
mach_port_t reply_port;
+ int error;
} d[nfds];
sigset_t oset;
@@ -73,16 +80,39 @@ _hurd_select (int nfds,
return -1;
}
- if (timeout != NULL)
+#define IO_SELECT_REPLY_MSGID (21012 + 100) /* XXX */
+#define IO_SELECT_TIMEOUT_REPLY_MSGID (21031 + 100) /* XXX */
+
+ if (timeout == NULL)
+ reply_msgid = IO_SELECT_REPLY_MSGID;
+ else
{
- if (timeout->tv_sec < 0 || timeout->tv_nsec < 0)
+ struct timeval now;
+
+ if (timeout->tv_sec < 0 || timeout->tv_nsec < 0 ||
+ timeout->tv_nsec >= 1000000000)
{
errno = EINVAL;
return -1;
}
- to = (timeout->tv_sec * 1000 +
- (timeout->tv_nsec + 999999) / 1000000);
+ err = __gettimeofday(&now, NULL);
+ if (err)
+ return -1;
+
+ ts.tv_sec = now.tv_sec + timeout->tv_sec;
+ ts.tv_nsec = now.tv_usec * 1000 + timeout->tv_nsec;
+
+ if (ts.tv_nsec >= 1000000000)
+ {
+ ts.tv_sec++;
+ ts.tv_nsec -= 1000000000;
+ }
+
+ if (ts.tv_sec < 0)
+ ts.tv_sec = LONG_MAX; /* XXX */
+
+ reply_msgid = IO_SELECT_TIMEOUT_REPLY_MSGID;
}
if (sigmask && __sigprocmask (SIG_SETMASK, sigmask, &oset))
@@ -90,6 +120,7 @@ _hurd_select (int nfds,
if (pollfds)
{
+ int error = 0;
/* Collect interesting descriptors from the user's `pollfd' array.
We do a first pass that reads the user's array before taking
any locks. The second pass then only touches our own stack,
@@ -123,28 +154,47 @@ _hurd_select (int nfds,
if (fd < _hurd_dtablesize)
{
d[i].cell = _hurd_dtable[fd];
- d[i].io_port = _hurd_port_get (&d[i].cell->port, &d[i].ulink);
- if (d[i].io_port != MACH_PORT_NULL)
- continue;
+ if (d[i].cell != NULL)
+ {
+ d[i].io_port = _hurd_port_get (&d[i].cell->port,
+ &d[i].ulink);
+ if (d[i].io_port != MACH_PORT_NULL)
+ continue;
+ }
}
- /* If one descriptor is bogus, we fail completely. */
- while (i-- > 0)
- if (d[i].type != 0)
- _hurd_port_free (&d[i].cell->port,
- &d[i].ulink, d[i].io_port);
- break;
+ /* Bogus descriptor, make it EBADF already. */
+ d[i].error = EBADF;
+ d[i].type = SELECT_ERROR;
+ error = 1;
}
__mutex_unlock (&_hurd_dtable_lock);
HURD_CRITICAL_END;
- if (i < nfds)
+ if (error)
{
- if (sigmask)
- __sigprocmask (SIG_SETMASK, &oset, NULL);
- errno = EBADF;
- return -1;
+ /* Set timeout to 0. */
+ struct timeval now;
+ err = __gettimeofday(&now, NULL);
+ if (err)
+ {
+ /* Really bad luck. */
+ err = errno;
+ HURD_CRITICAL_BEGIN;
+ __mutex_lock (&_hurd_dtable_lock);
+ while (i-- > 0)
+ if (d[i].type & ~SELECT_ERROR != 0)
+ _hurd_port_free (&d[i].cell->port, &d[i].ulink,
+ d[i].io_port);
+ __mutex_unlock (&_hurd_dtable_lock);
+ HURD_CRITICAL_END;
+ errno = err;
+ return -1;
+ }
+ ts.tv_sec = now.tv_sec;
+ ts.tv_nsec = now.tv_usec * 1000;
+ reply_msgid = IO_SELECT_TIMEOUT_REPLY_MSGID;
}
lastfd = i - 1;
@@ -171,9 +221,6 @@ _hurd_select (int nfds,
HURD_CRITICAL_BEGIN;
__mutex_lock (&_hurd_dtable_lock);
- if (nfds > _hurd_dtablesize)
- nfds = _hurd_dtablesize;
-
/* Collect the ports for interesting FDs. */
firstfd = lastfd = -1;
for (i = 0; i < nfds; ++i)
@@ -188,9 +235,15 @@ _hurd_select (int nfds,
d[i].type = type;
if (type)
{
- d[i].cell = _hurd_dtable[i];
- d[i].io_port = _hurd_port_get (&d[i].cell->port, &d[i].ulink);
- if (d[i].io_port == MACH_PORT_NULL)
+ if (i < _hurd_dtablesize)
+ {
+ d[i].cell = _hurd_dtable[i];
+ if (d[i].cell != NULL)
+ d[i].io_port = _hurd_port_get (&d[i].cell->port,
+ &d[i].ulink);
+ }
+ if (i >= _hurd_dtablesize || d[i].cell == NULL ||
+ d[i].io_port == MACH_PORT_NULL)
{
/* If one descriptor is bogus, we fail completely. */
while (i-- > 0)
@@ -215,6 +268,9 @@ _hurd_select (int nfds,
errno = EBADF;
return -1;
}
+
+ if (nfds > _hurd_dtablesize)
+ nfds = _hurd_dtablesize;
}
@@ -232,19 +288,19 @@ _hurd_select (int nfds,
portset = MACH_PORT_NULL;
for (i = firstfd; i <= lastfd; ++i)
- if (d[i].type)
+ if (!(d[i].type & ~SELECT_ERROR))
+ d[i].reply_port = MACH_PORT_NULL;
+ else
{
int type = d[i].type;
d[i].reply_port = __mach_reply_port ();
- err = __io_select (d[i].io_port, d[i].reply_port,
- /* Poll only if there's a single descriptor. */
- (firstfd == lastfd) ? to : 0,
- &type);
- switch (err)
+ if (timeout == NULL)
+ err = __io_select_request (d[i].io_port, d[i].reply_port, type);
+ else
+ err = __io_select_timeout_request (d[i].io_port, d[i].reply_port,
+ ts, type);
+ if (!err)
{
- case MACH_RCV_TIMED_OUT:
- /* No immediate response. This is normal. */
- err = 0;
if (firstfd == lastfd)
/* When there's a single descriptor, we don't need a
portset, so just pretend we have one, but really
@@ -265,32 +321,23 @@ _hurd_select (int nfds,
__mach_port_move_member (__mach_task_self (),
d[i].reply_port, portset);
}
- break;
-
- default:
- /* No other error should happen. Callers of select
- don't expect to see errors, so we simulate
- readiness of the erring object and the next call
- hopefully will get the error again. */
- type = SELECT_ALL;
- /* FALLTHROUGH */
-
- case 0:
- /* We got an answer. */
- if ((type & SELECT_ALL) == 0)
- /* Bogus answer; treat like an error, as a fake positive. */
- type = SELECT_ALL;
-
- /* This port is already ready already. */
- d[i].type &= type;
- d[i].type |= SELECT_RETURNED;
+ }
+ else
+ {
+ /* No error should happen, but record it for later
+ processing. */
+ d[i].error = err;
+ d[i].type |= SELECT_ERROR;
++got;
- break;
}
_hurd_port_free (&d[i].cell->port, &d[i].ulink, d[i].io_port);
}
}
+ /* GOT is the number of replies (or errors), while READY is the number of
+ replies with at least one type bit set. */
+ ready = 0;
+
/* Now wait for reply messages. */
if (!err && got == 0)
{
@@ -332,22 +379,35 @@ _hurd_select (int nfds,
} success;
#endif
} msg;
- mach_msg_option_t options = (timeout == NULL ? 0 : MACH_RCV_TIMEOUT);
+ mach_msg_option_t options;
error_t msgerr;
+
+ /* We rely on servers to implement the timeout, but when there are none,
+ do it on the client side. */
+ if (timeout != NULL && firstfd == -1)
+ {
+ options = MACH_RCV_TIMEOUT;
+ to = timeout->tv_sec * 1000 + (timeout->tv_nsec + 999999) / 1000000;
+ }
+ else
+ {
+ options = 0;
+ to = MACH_MSG_TIMEOUT_NONE;
+ }
+
while ((msgerr = __mach_msg (&msg.head,
MACH_RCV_MSG | MACH_RCV_INTERRUPT | options,
0, sizeof msg, portset, to,
MACH_PORT_NULL)) == MACH_MSG_SUCCESS)
{
/* We got a message. Decode it. */
-#define IO_SELECT_REPLY_MSGID (21012 + 100) /* XXX */
#ifdef MACH_MSG_TYPE_BIT
const union typeword inttype =
{ type:
{ MACH_MSG_TYPE_INTEGER_T, sizeof (integer_t) * 8, 1, 1, 0, 0 }
};
#endif
- if (msg.head.msgh_id == IO_SELECT_REPLY_MSGID &&
+ if (msg.head.msgh_id == reply_msgid &&
msg.head.msgh_size >= sizeof msg.error &&
!(msg.head.msgh_bits & MACH_MSGH_BITS_COMPLEX) &&
#ifdef MACH_MSG_TYPE_BIT
@@ -365,16 +425,18 @@ _hurd_select (int nfds,
err = EINTR;
goto poll;
}
+ /* Keep in mind msg.success.result can be 0 if a timeout
+ occurred. */
if (msg.error.err ||
- msg.head.msgh_size != sizeof msg.success ||
#ifdef MACH_MSG_TYPE_BIT
msg.success.result_type.word != inttype.word ||
#endif
- (msg.success.result & SELECT_ALL) == 0)
+ msg.head.msgh_size != sizeof msg.success)
{
- /* Error or bogus reply. Simulate readiness. */
+ /* Error or bogus reply. */
+ if (!msg.error.err)
+ msg.error.err = EIO;
__mach_msg_destroy (&msg.head);
- msg.success.result = SELECT_ALL;
}
/* Look up the respondent's reply port and record its
@@ -386,7 +448,19 @@ _hurd_select (int nfds,
if (d[i].type
&& d[i].reply_port == msg.head.msgh_local_port)
{
- d[i].type &= msg.success.result;
+ if (msg.error.err)
+ {
+ d[i].error = msg.error.err;
+ d[i].type = SELECT_ERROR;
+ ++ready;
+ }
+ else
+ {
+ d[i].type &= msg.success.result;
+ if (d[i].type)
+ ++ready;
+ }
+
d[i].type |= SELECT_RETURNED;
++got;
}
@@ -411,7 +485,7 @@ _hurd_select (int nfds,
/* Interruption on our side (e.g. signal reception). */
err = EINTR;
- if (got)
+ if (ready)
/* At least one descriptor is known to be ready now, so we will
return success. */
err = 0;
@@ -419,7 +493,7 @@ _hurd_select (int nfds,
if (firstfd != -1)
for (i = firstfd; i <= lastfd; ++i)
- if (d[i].type)
+ if (d[i].reply_port != MACH_PORT_NULL)
__mach_port_destroy (__mach_task_self (), d[i].reply_port);
if (firstfd == -1 || (firstfd != lastfd && portset != MACH_PORT_NULL))
/* Destroy PORTSET, but only if it's not actually the reply port for a
@@ -441,23 +515,37 @@ _hurd_select (int nfds,
int type = d[i].type;
int_fast16_t revents = 0;
- if (type & SELECT_RETURNED)
- {
- if (type & SELECT_READ)
- revents |= POLLIN;
- if (type & SELECT_WRITE)
- revents |= POLLOUT;
- if (type & SELECT_URG)
- revents |= POLLPRI;
- }
+ if (type & SELECT_ERROR)
+ switch (d[i].error)
+ {
+ case EPIPE:
+ revents = POLLHUP;
+ break;
+ case EBADF:
+ revents = POLLNVAL;
+ break;
+ default:
+ revents = POLLERR;
+ break;
+ }
+ else
+ if (type & SELECT_RETURNED)
+ {
+ if (type & SELECT_READ)
+ revents |= POLLIN;
+ if (type & SELECT_WRITE)
+ revents |= POLLOUT;
+ if (type & SELECT_URG)
+ revents |= POLLPRI;
+ }
pollfds[i].revents = revents;
}
else
{
- /* Below we recalculate GOT to include an increment for each operation
+ /* Below we recalculate READY to include an increment for each operation
allowed on each fd. */
- got = 0;
+ ready = 0;
/* Set the user bitarrays. We only ever have to clear bits, as all
desired ones are initially set. */
@@ -469,16 +557,30 @@ _hurd_select (int nfds,
if ((type & SELECT_RETURNED) == 0)
type = 0;
+ /* Callers of select don't expect to see errors, so we simulate
+ readiness of the erring object and the next call hopefully
+ will get the error again. */
+ if (type & SELECT_ERROR)
+ {
+ type = 0;
+ if (readfds != NULL && FD_ISSET (i, readfds))
+ type |= SELECT_READ;
+ if (writefds != NULL && FD_ISSET (i, writefds))
+ type |= SELECT_WRITE;
+ if (exceptfds != NULL && FD_ISSET (i, exceptfds))
+ type |= SELECT_URG;
+ }
+
if (type & SELECT_READ)
- got++;
+ ready++;
else if (readfds)
FD_CLR (i, readfds);
if (type & SELECT_WRITE)
- got++;
+ ready++;
else if (writefds)
FD_CLR (i, writefds);
if (type & SELECT_URG)
- got++;
+ ready++;
else if (exceptfds)
FD_CLR (i, exceptfds);
}
@@ -487,5 +589,5 @@ _hurd_select (int nfds,
if (sigmask && __sigprocmask (SIG_SETMASK, &oset, NULL))
return -1;
- return got;
+ return ready;
}
diff --git a/hurd/hurdsig.c b/hurd/hurdsig.c
index fe2eeabfe5..fd787c6c1b 100644
--- a/hurd/hurdsig.c
+++ b/hurd/hurdsig.c
@@ -20,6 +20,7 @@
#include <string.h>
#include <cthreads.h> /* For `struct mutex'. */
+#include <pthread.h>
#include <mach.h>
#include <mach/thread_switch.h>
@@ -42,17 +43,16 @@ mach_port_t _hurd_msgport;
/* Thread listening on it. */
thread_t _hurd_msgport_thread;
-/* Thread which receives task-global signals. */
-thread_t _hurd_sigthread;
-
/* These are set up by _hurdsig_init. */
unsigned long int __hurd_sigthread_stack_base;
unsigned long int __hurd_sigthread_stack_end;
-unsigned long int *__hurd_sigthread_variables;
/* Linked-list of per-thread signal state. */
struct hurd_sigstate *_hurd_sigstates;
+/* Sigstate for the task-global signals. */
+struct hurd_sigstate *_hurd_global_sigstate;
+
/* Timeout for RPC's after interrupt_operation. */
mach_msg_timeout_t _hurd_interrupted_rpc_timeout = 3000;
@@ -81,7 +81,7 @@ _hurd_thread_sigstate (thread_t thread)
{
ss = malloc (sizeof (*ss));
if (ss == NULL)
- __libc_fatal ("hurd: Can't allocate thread sigstate\n");
+ __libc_fatal ("hurd: Can't allocate sigstate\n");
ss->thread = thread;
__spin_lock_init (&ss->lock);
@@ -94,16 +94,21 @@ _hurd_thread_sigstate (thread_t thread)
ss->intr_port = MACH_PORT_NULL;
ss->context = NULL;
- /* Initialize the sigaction vector from the default signal receiving
- thread's state, and its from the system defaults. */
- if (thread == _hurd_sigthread)
- default_sigaction (ss->actions);
+ if (thread == MACH_PORT_NULL)
+ {
+ /* Process-wide sigstate, use the system defaults. */
+ default_sigaction (ss->actions);
+
+ /* The global sigstate is not added to the _hurd_sigstates list.
+ It is created with _hurd_thread_sigstate (MACH_PORT_NULL)
+ but should be accessed through _hurd_global_sigstate. */
+ }
else
{
- struct hurd_sigstate *s;
- for (s = _hurd_sigstates; s != NULL; s = s->next)
- if (s->thread == _hurd_sigthread)
- break;
+ error_t err;
+
+ /* Use the global actions as a default for new threads. */
+ struct hurd_sigstate *s = _hurd_global_sigstate;
if (s)
{
__spin_lock (&s->lock);
@@ -112,14 +117,118 @@ _hurd_thread_sigstate (thread_t thread)
}
else
default_sigaction (ss->actions);
- }
- ss->next = _hurd_sigstates;
- _hurd_sigstates = ss;
+ ss->next = _hurd_sigstates;
+ _hurd_sigstates = ss;
+
+ err = __mach_port_mod_refs (__mach_task_self (), thread,
+ MACH_PORT_RIGHT_SEND, 1);
+ if (err)
+ __libc_fatal ("hurd: Can't add reference on Mach thread\n");
+ }
}
__mutex_unlock (&_hurd_siglock);
return ss;
}
+
+/* Destroy a sigstate structure. Called by libpthread just before the
+ * corresponding thread is terminated. */
+void
+_hurd_sigstate_delete (thread_t thread)
+{
+ struct hurd_sigstate **ssp, *ss;
+
+ __mutex_lock (&_hurd_siglock);
+ for (ssp = &_hurd_sigstates; *ssp; ssp = &(*ssp)->next)
+ if ((*ssp)->thread == thread)
+ break;
+
+ ss = *ssp;
+ if (ss)
+ *ssp = ss->next;
+
+ __mutex_unlock (&_hurd_siglock);
+ if (ss)
+ {
+ if (ss->thread != MACH_PORT_NULL)
+ __mach_port_deallocate (__mach_task_self (), ss->thread);
+
+ free (ss);
+ }
+}
+
+/* Make SS a global receiver, with pthread signal semantics. */
+void
+_hurd_sigstate_set_global_rcv (struct hurd_sigstate *ss)
+{
+ assert (ss->thread != MACH_PORT_NULL);
+ ss->actions[0].sa_handler = SIG_IGN;
+}
+
+/* Check whether SS is a global receiver. */
+static int
+sigstate_is_global_rcv (const struct hurd_sigstate *ss)
+{
+ return (_hurd_global_sigstate != NULL)
+ && (ss->actions[0].sa_handler == SIG_IGN);
+}
+
+/* Lock/unlock a hurd_sigstate structure. If the accessors below require
+ it, the global sigstate will be locked as well. */
+void
+_hurd_sigstate_lock (struct hurd_sigstate *ss)
+{
+ if (sigstate_is_global_rcv (ss))
+ __spin_lock (&_hurd_global_sigstate->lock);
+ __spin_lock (&ss->lock);
+}
+void
+_hurd_sigstate_unlock (struct hurd_sigstate *ss)
+{
+ __spin_unlock (&ss->lock);
+ if (sigstate_is_global_rcv (ss))
+ __spin_unlock (&_hurd_global_sigstate->lock);
+}
+
+/* Retreive a thread's full set of pending signals, including the global
+ ones if appropriate. SS must be locked. */
+sigset_t
+_hurd_sigstate_pending (const struct hurd_sigstate *ss)
+{
+ sigset_t pending = ss->pending;
+ if (sigstate_is_global_rcv (ss))
+ __sigorset (&pending, &pending, &_hurd_global_sigstate->pending);
+ return pending;
+}
+
+/* Clear a pending signal and return the associated detailed
+ signal information. SS must be locked, and must have signal SIGNO
+ pending, either directly or through the global sigstate. */
+static struct hurd_signal_detail
+sigstate_clear_pending (struct hurd_sigstate *ss, int signo)
+{
+ if (sigstate_is_global_rcv (ss)
+ && __sigismember (&_hurd_global_sigstate->pending, signo))
+ {
+ __sigdelset (&_hurd_global_sigstate->pending, signo);
+ return _hurd_global_sigstate->pending_data[signo];
+ }
+
+ assert (__sigismember (&ss->pending, signo));
+ __sigdelset (&ss->pending, signo);
+ return ss->pending_data[signo];
+}
+
+/* Retreive a thread's action vector. SS must be locked. */
+struct sigaction *
+_hurd_sigstate_actions (struct hurd_sigstate *ss)
+{
+ if (sigstate_is_global_rcv (ss))
+ return _hurd_global_sigstate->actions;
+ else
+ return ss->actions;
+}
+
/* Signal delivery itself is on this page. */
@@ -214,6 +323,8 @@ static void
abort_thread (struct hurd_sigstate *ss, struct machine_thread_all_state *state,
void (*reply) (void))
{
+ assert (ss->thread != MACH_PORT_NULL);
+
if (!(state->set & THREAD_ABORTED))
{
error_t err = __thread_abort (ss->thread);
@@ -234,14 +345,14 @@ abort_thread (struct hurd_sigstate *ss, struct machine_thread_all_state *state,
that this location can be set without faulting, or else return NULL. */
static mach_port_t *
-interrupted_reply_port_location (struct machine_thread_all_state *thread_state,
+interrupted_reply_port_location (thread_t thread,
+ struct machine_thread_all_state *thread_state,
int sigthread)
{
- mach_port_t *portloc = (mach_port_t *) __hurd_threadvar_location_from_sp
- (_HURD_THREADVAR_MIG_REPLY, (void *) thread_state->basic.SP);
+ mach_port_t *portloc = &THREAD_TCB(thread, thread_state)->reply_port;
if (sigthread && _hurdsig_catch_memory_fault (portloc))
- /* Faulted trying to read the stack. */
+ /* Faulted trying to read the TCB. */
return NULL;
/* Fault now if this pointer is bogus. */
@@ -323,7 +434,8 @@ _hurdsig_abort_rpcs (struct hurd_sigstate *ss, int signo, int sigthread,
our nonzero return tells the trampoline code to finish the message
receive operation before running the handler. */
- mach_port_t *reply = interrupted_reply_port_location (state,
+ mach_port_t *reply = interrupted_reply_port_location (ss->thread,
+ state,
sigthread);
error_t err = __interrupt_operation (intr_port, _hurdsig_interrupt_timeout);
@@ -353,7 +465,7 @@ _hurdsig_abort_rpcs (struct hurd_sigstate *ss, int signo, int sigthread,
call above will retry their RPCs unless we clear SS->intr_port.
So we clear it for the thread taking a signal when SA_RESTART is
clear, so that its call returns EINTR. */
- if (! signo || !(ss->actions[signo].sa_flags & SA_RESTART))
+ if (! signo || !(_hurd_sigstate_actions (ss) [signo].sa_flags & SA_RESTART))
ss->intr_port = MACH_PORT_NULL;
}
@@ -441,6 +553,30 @@ abort_all_rpcs (int signo, struct machine_thread_all_state *state, int live)
}
}
+/* Wake up any sigsuspend call that is blocking SS->thread. SS must be
+ locked. */
+static void
+wake_sigsuspend (struct hurd_sigstate *ss)
+{
+ error_t err;
+ mach_msg_header_t msg;
+
+ if (ss->suspended == MACH_PORT_NULL)
+ return;
+
+ /* There is a sigsuspend waiting. Tell it to wake up. */
+ msg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_MAKE_SEND, 0);
+ msg.msgh_remote_port = ss->suspended;
+ msg.msgh_local_port = MACH_PORT_NULL;
+ /* These values do not matter. */
+ msg.msgh_id = 8675309; /* Jenny, Jenny. */
+ ss->suspended = MACH_PORT_NULL;
+ err = __mach_msg (&msg, MACH_SEND_MSG, sizeof msg, 0,
+ MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
+ MACH_PORT_NULL);
+ assert_perror (err);
+}
+
struct hurd_signal_preemptor *_hurdsig_preemptors = 0;
sigset_t _hurdsig_preempted_set;
@@ -451,35 +587,20 @@ weak_alias (_hurdsig_preemptors, _hurdsig_preempters)
#define STOPSIGS (sigmask (SIGTTIN) | sigmask (SIGTTOU) | \
sigmask (SIGSTOP) | sigmask (SIGTSTP))
-/* Deliver a signal. SS is not locked. */
-void
-_hurd_internal_post_signal (struct hurd_sigstate *ss,
- int signo, struct hurd_signal_detail *detail,
- mach_port_t reply_port,
- mach_msg_type_name_t reply_port_type,
- int untraced)
+/* Actual delivery of a single signal. Called with SS unlocked. When
+ the signal is delivered, return SS, locked (or, if SS was originally
+ _hurd_global_sigstate, the sigstate of the actual thread the signal
+ was delivered to). If the signal is being traced, return NULL with
+ SS unlocked. */
+static struct hurd_sigstate *
+post_signal (struct hurd_sigstate *ss,
+ int signo, struct hurd_signal_detail *detail,
+ int untraced, void (*reply) (void))
{
- error_t err;
struct machine_thread_all_state thread_state;
enum { stop, ignore, core, term, handle } act;
- sighandler_t handler;
- sigset_t pending;
int ss_suspended;
- /* Reply to this sig_post message. */
- __typeof (__msg_sig_post_reply) *reply_rpc
- = (untraced ? __msg_sig_post_untraced_reply : __msg_sig_post_reply);
- void reply (void)
- {
- error_t err;
- if (reply_port == MACH_PORT_NULL)
- return;
- err = (*reply_rpc) (reply_port, reply_port_type, 0);
- reply_port = MACH_PORT_NULL;
- if (err != MACH_SEND_INVALID_DEST) /* Ignore dead reply port. */
- assert_perror (err);
- }
-
/* Mark the signal as pending. */
void mark_pending (void)
{
@@ -524,8 +645,12 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
assert_perror (err);
for (i = 0; i < nthreads; ++i)
{
- if (threads[i] != _hurd_msgport_thread &&
- (act != handle || threads[i] != ss->thread))
+ if (act == handle && threads[i] == ss->thread)
+ {
+ /* The thread that will run the handler is kept suspended. */
+ ss_suspended = 1;
+ }
+ else if (threads[i] != _hurd_msgport_thread)
{
err = __thread_resume (threads[i]);
assert_perror (err);
@@ -538,27 +663,58 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
(vm_address_t) threads,
nthreads * sizeof *threads);
_hurd_stopped = 0;
- if (act == handle)
- /* The thread that will run the handler is already suspended. */
- ss_suspended = 1;
}
+ error_t err;
+ sighandler_t handler;
+
if (signo == 0)
{
if (untraced)
- /* This is PTRACE_CONTINUE. */
- resume ();
+ {
+ /* This is PTRACE_CONTINUE. */
+ act = ignore;
+ resume ();
+ }
/* This call is just to check for pending signals. */
- __spin_lock (&ss->lock);
- goto check_pending_signals;
+ _hurd_sigstate_lock (ss);
+ return ss;
}
- post_signal:
-
thread_state.set = 0; /* We know nothing. */
- __spin_lock (&ss->lock);
+ _hurd_sigstate_lock (ss);
+
+ /* If this is a global signal, try to find a thread ready to accept
+ it right away. This is especially important for untraced signals,
+ since going through the global pending mask would de-untrace them. */
+ if (ss->thread == MACH_PORT_NULL)
+ {
+ struct hurd_sigstate *rss;
+
+ __mutex_lock (&_hurd_siglock);
+ for (rss = _hurd_sigstates; rss != NULL; rss = rss->next)
+ {
+ if (! sigstate_is_global_rcv (rss))
+ continue;
+
+ /* The global sigstate is already locked. */
+ __spin_lock (&rss->lock);
+ if (! __sigismember (&rss->blocked, signo))
+ {
+ ss = rss;
+ break;
+ }
+ __spin_unlock (&rss->lock);
+ }
+ __mutex_unlock (&_hurd_siglock);
+ }
+
+ /* We want the preemptors to be able to update the blocking mask
+ without affecting the delivery of this signal, so we save the
+ current value to test against later. */
+ sigset_t blocked = ss->blocked;
/* Check for a preempted signal. Preempted signals can arrive during
critical sections. */
@@ -567,7 +723,7 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
{ /* PE cannot be null. */
do
{
- if (HURD_PREEMPT_SIGNAL_P (pe, signo, detail->code))
+ if (HURD_PREEMPT_SIGNAL_P (pe, signo, detail->exc_subcode))
{
if (pe->preemptor)
{
@@ -616,12 +772,12 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
mark_pending ();
else
suspend ();
- __spin_unlock (&ss->lock);
+ _hurd_sigstate_unlock (ss);
reply ();
- return;
+ return NULL;
}
- handler = ss->actions[signo].sa_handler;
+ handler = _hurd_sigstate_actions (ss) [signo].sa_handler;
if (handler == SIG_DFL)
/* Figure out the default action for this signal. */
@@ -714,9 +870,7 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
}
/* Handle receipt of a blocked signal, or any signal while stopped. */
- if (act != ignore && /* Signals ignored now are forgotten now. */
- __sigismember (&ss->blocked, signo) ||
- (signo != SIGKILL && _hurd_stopped))
+ if (__sigismember (&blocked, signo) || (signo != SIGKILL && _hurd_stopped))
{
mark_pending ();
act = ignore;
@@ -751,6 +905,7 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
now's the time to set it going. */
if (ss_suspended)
{
+ assert (ss->thread != MACH_PORT_NULL);
err = __thread_resume (ss->thread);
assert_perror (err);
ss_suspended = 0;
@@ -795,6 +950,8 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
struct sigcontext *scp, ocontext;
int wait_for_reply, state_changed;
+ assert (ss->thread != MACH_PORT_NULL);
+
/* Stop the thread and abort its pending RPC operations. */
if (! ss_suspended)
{
@@ -835,7 +992,8 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
if (! machine_get_basic_state (ss->thread, &thread_state))
goto sigbomb;
- loc = interrupted_reply_port_location (&thread_state, 1);
+ loc = interrupted_reply_port_location (ss->thread,
+ &thread_state, 1);
if (loc && *loc != MACH_PORT_NULL)
/* This is the reply port for the context which called
sigreturn. Since we are abandoning that context entirely
@@ -861,7 +1019,7 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
as a unit. */
crit ? 0 : signo, 1,
&thread_state, &state_changed,
- &reply)
+ reply)
!= MACH_PORT_NULL);
if (crit)
@@ -901,7 +1059,8 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
{
/* Fetch the thread variable for the MiG reply port,
and set it to MACH_PORT_NULL. */
- mach_port_t *loc = interrupted_reply_port_location (&thread_state,
+ mach_port_t *loc = interrupted_reply_port_location (ss->thread,
+ &thread_state,
1);
if (loc)
{
@@ -929,23 +1088,28 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
}
}
+ struct sigaction *action = & _hurd_sigstate_actions (ss) [signo];
+
/* Backdoor extra argument to signal handler. */
scp->sc_error = detail->error;
/* Block requested signals while running the handler. */
scp->sc_mask = ss->blocked;
- __sigorset (&ss->blocked, &ss->blocked, &ss->actions[signo].sa_mask);
+ __sigorset (&ss->blocked, &ss->blocked, &action->sa_mask);
/* Also block SIGNO unless we're asked not to. */
- if (! (ss->actions[signo].sa_flags & (SA_RESETHAND | SA_NODEFER)))
+ if (! (action->sa_flags & (SA_RESETHAND | SA_NODEFER)))
__sigaddset (&ss->blocked, signo);
/* Reset to SIG_DFL if requested. SIGILL and SIGTRAP cannot
be automatically reset when delivered; the system silently
enforces this restriction. */
- if (ss->actions[signo].sa_flags & SA_RESETHAND
+ if (action->sa_flags & SA_RESETHAND
&& signo != SIGILL && signo != SIGTRAP)
- ss->actions[signo].sa_handler = SIG_DFL;
+ action->sa_handler = SIG_DFL;
+
+ /* Any sigsuspend call must return after the handler does. */
+ wake_sigsuspend (ss);
/* Start the thread running the handler (or possibly waiting for an
RPC reply before running the handler). */
@@ -960,95 +1124,134 @@ _hurd_internal_post_signal (struct hurd_sigstate *ss,
}
}
- /* The signal has either been ignored or is now being handled. We can
- consider it delivered and reply to the killer. */
- reply ();
+ return ss;
+}
- /* We get here unless the signal was fatal. We still hold SS->lock.
- Check for pending signals, and loop to post them. */
- {
- /* Return nonzero if SS has any signals pending we should worry about.
- We don't worry about any pending signals if we are stopped, nor if
- SS is in a critical section. We are guaranteed to get a sig_post
- message before any of them become deliverable: either the SIGCONT
- signal, or a sig_post with SIGNO==0 as an explicit poll when the
- thread finishes its critical section. */
- inline int signals_pending (void)
+/* Return the set of pending signals in SS which should be delivered. */
+static sigset_t
+pending_signals (struct hurd_sigstate *ss)
+{
+ /* We don't worry about any pending signals if we are stopped, nor if
+ SS is in a critical section. We are guaranteed to get a sig_post
+ message before any of them become deliverable: either the SIGCONT
+ signal, or a sig_post with SIGNO==0 as an explicit poll when the
+ thread finishes its critical section. */
+ if (_hurd_stopped || __spin_lock_locked (&ss->critical_section_lock))
+ return 0;
+
+ return _hurd_sigstate_pending (ss) & ~ss->blocked;
+}
+
+/* Post the specified pending signals in SS and return 1. If one of
+ them is traced, abort immediately and return 0. SS must be locked on
+ entry and will be unlocked in all cases. */
+static int
+post_pending (struct hurd_sigstate *ss, sigset_t pending, void (*reply) (void))
+{
+ int signo;
+ struct hurd_signal_detail detail;
+
+ /* Make sure SS corresponds to an actual thread, since we assume it won't
+ change in post_signal. */
+ assert (ss->thread != MACH_PORT_NULL);
+
+ for (signo = 1; signo < NSIG; ++signo)
+ if (__sigismember (&pending, signo))
{
- if (_hurd_stopped || __spin_lock_locked (&ss->critical_section_lock))
+ detail = sigstate_clear_pending (ss, signo);
+ _hurd_sigstate_unlock (ss);
+
+ /* Will reacquire the lock, except if the signal is traced. */
+ if (! post_signal (ss, signo, &detail, 0, reply))
return 0;
- return pending = ss->pending & ~ss->blocked;
}
- check_pending_signals:
- untraced = 0;
+ /* No more signals pending; SS->lock is still locked. */
+ _hurd_sigstate_unlock (ss);
- if (signals_pending ())
- {
- for (signo = 1; signo < NSIG; ++signo)
- if (__sigismember (&pending, signo))
- {
- deliver_pending:
- __sigdelset (&ss->pending, signo);
- *detail = ss->pending_data[signo];
- __spin_unlock (&ss->lock);
- goto post_signal;
- }
- }
+ return 1;
+}
- /* No pending signals left undelivered for this thread.
- If we were sent signal 0, we need to check for pending
- signals for all threads. */
- if (signo == 0)
- {
- __spin_unlock (&ss->lock);
- __mutex_lock (&_hurd_siglock);
- for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
- {
- __spin_lock (&ss->lock);
- for (signo = 1; signo < NSIG; ++signo)
- if (__sigismember (&ss->pending, signo)
- && (!__sigismember (&ss->blocked, signo)
- /* We "deliver" immediately pending blocked signals whose
- action might be to ignore, so that if ignored they are
- dropped right away. */
- || ss->actions[signo].sa_handler == SIG_IGN
- || ss->actions[signo].sa_handler == SIG_DFL))
- {
- mutex_unlock (&_hurd_siglock);
- goto deliver_pending;
- }
- __spin_unlock (&ss->lock);
- }
- __mutex_unlock (&_hurd_siglock);
- }
- else
- {
- /* No more signals pending; SS->lock is still locked.
- Wake up any sigsuspend call that is blocking SS->thread. */
- if (ss->suspended != MACH_PORT_NULL)
- {
- /* There is a sigsuspend waiting. Tell it to wake up. */
- error_t err;
- mach_msg_header_t msg;
- msg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_MAKE_SEND, 0);
- msg.msgh_remote_port = ss->suspended;
- msg.msgh_local_port = MACH_PORT_NULL;
- /* These values do not matter. */
- msg.msgh_id = 8675309; /* Jenny, Jenny. */
- ss->suspended = MACH_PORT_NULL;
- err = __mach_msg (&msg, MACH_SEND_MSG, sizeof msg, 0,
- MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
- MACH_PORT_NULL);
- assert_perror (err);
- }
- __spin_unlock (&ss->lock);
- }
- }
+/* Post all the pending signals of all threads and return 1. If a traced
+ signal is encountered, abort immediately and return 0. */
+static int
+post_all_pending_signals (void (*reply) (void))
+{
+ struct hurd_sigstate *ss;
+ sigset_t pending = 0;
+
+ for (;;)
+ {
+ __mutex_lock (&_hurd_siglock);
+ for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
+ {
+ _hurd_sigstate_lock (ss);
+
+ pending = pending_signals (ss);
+ if (pending)
+ /* post_pending() below will unlock SS. */
+ break;
+
+ _hurd_sigstate_unlock (ss);
+ }
+ __mutex_unlock (&_hurd_siglock);
- /* All pending signals delivered to all threads.
- Now we can send the reply message even for signal 0. */
- reply ();
+ if (! pending)
+ return 1;
+ if (! post_pending (ss, pending, reply))
+ return 0;
+ }
+}
+
+/* Deliver a signal. SS is not locked. */
+void
+_hurd_internal_post_signal (struct hurd_sigstate *ss,
+ int signo, struct hurd_signal_detail *detail,
+ mach_port_t reply_port,
+ mach_msg_type_name_t reply_port_type,
+ int untraced)
+{
+ /* Reply to this sig_post message. */
+ __typeof (__msg_sig_post_reply) *reply_rpc
+ = (untraced ? __msg_sig_post_untraced_reply : __msg_sig_post_reply);
+ void reply (void)
+ {
+ error_t err;
+ if (reply_port == MACH_PORT_NULL)
+ return;
+ err = (*reply_rpc) (reply_port, reply_port_type, 0);
+ reply_port = MACH_PORT_NULL;
+ if (err != MACH_SEND_INVALID_DEST) /* Ignore dead reply port. */
+ assert_perror (err);
+ }
+
+ ss = post_signal (ss, signo, detail, untraced, reply);
+ if (! ss)
+ return;
+
+ /* The signal was neither fatal nor traced. We still hold SS->lock. */
+ if (signo != 0 && ss->thread != MACH_PORT_NULL)
+ {
+ /* The signal has either been ignored or is now being handled. We can
+ consider it delivered and reply to the killer. */
+ reply ();
+
+ /* Post any pending signals for this thread. */
+ if (! post_pending (ss, pending_signals (ss), reply))
+ return;
+ }
+ else
+ {
+ /* If this was a process-wide signal or a poll request, we need
+ to check for pending signals for all threads. */
+ _hurd_sigstate_unlock (ss);
+ if (! post_all_pending_signals (reply))
+ return;
+
+ /* All pending signals delivered to all threads.
+ Now we can send the reply message even for signal 0. */
+ reply ();
+ }
}
/* Decide whether REFPORT enables the sender to send us a SIGNO signal.
@@ -1167,9 +1370,10 @@ _S_msg_sig_post (mach_port_t me,
d.code = sigcode;
d.exc = 0;
- /* Post the signal to the designated signal-receiving thread. This will
- reply when the signal can be considered delivered. */
- _hurd_internal_post_signal (_hurd_thread_sigstate (_hurd_sigthread),
+ /* Post the signal to a global receiver thread (or mark it pending in
+ the global sigstate). This will reply when the signal can be
+ considered delivered. */
+ _hurd_internal_post_signal (_hurd_global_sigstate,
signo, &d, reply_port, reply_port_type,
0); /* Stop if traced. */
@@ -1197,7 +1401,7 @@ _S_msg_sig_post_untraced (mach_port_t me,
/* Post the signal to the designated signal-receiving thread. This will
reply when the signal can be considered delivered. */
- _hurd_internal_post_signal (_hurd_thread_sigstate (_hurd_sigthread),
+ _hurd_internal_post_signal (_hurd_global_sigstate,
signo, &d, reply_port, reply_port_type,
1); /* Untraced flag. */
@@ -1208,8 +1412,8 @@ extern void __mig_init (void *);
#include <mach/task_special_ports.h>
-/* Initialize the message port and _hurd_sigthread and start the signal
- thread. */
+/* Initialize the message port, _hurd_global_sigstate, and start the
+ signal thread. */
void
_hurdsig_init (const int *intarray, size_t intarraysize)
@@ -1232,30 +1436,41 @@ _hurdsig_init (const int *intarray, size_t intarraysize)
MACH_MSG_TYPE_MAKE_SEND);
assert_perror (err);
+ /* Initialize the global signal state. */
+ _hurd_global_sigstate = _hurd_thread_sigstate (MACH_PORT_NULL);
+
+ /* We block all signals, and let actual threads pull them from the
+ pending mask. */
+ __sigfillset(& _hurd_global_sigstate->blocked);
+
/* Initialize the main thread's signal state. */
ss = _hurd_self_sigstate ();
- /* Copy inherited values from our parent (or pre-exec process state)
- into the signal settings of the main thread. */
+ /* Mark it as a process-wide signal receiver. Threads in this set use
+ the common action vector in _hurd_global_sigstate. */
+ _hurd_sigstate_set_global_rcv (ss);
+
+ /* Copy inherited signal settings from our parent (or pre-exec process
+ state) */
if (intarraysize > INIT_SIGMASK)
ss->blocked = intarray[INIT_SIGMASK];
if (intarraysize > INIT_SIGPENDING)
- ss->pending = intarray[INIT_SIGPENDING];
+ _hurd_global_sigstate->pending = intarray[INIT_SIGPENDING];
if (intarraysize > INIT_SIGIGN && intarray[INIT_SIGIGN] != 0)
{
int signo;
for (signo = 1; signo < NSIG; ++signo)
if (intarray[INIT_SIGIGN] & __sigmask(signo))
- ss->actions[signo].sa_handler = SIG_IGN;
+ _hurd_global_sigstate->actions[signo].sa_handler = SIG_IGN;
}
- /* Set the default thread to receive task-global signals
- to this one, the main (first) user thread. */
- _hurd_sigthread = ss->thread;
-
/* Start the signal thread listening on the message port. */
- if (__hurd_threadvar_stack_mask == 0)
+#pragma weak cthread_fork
+#pragma weak cthread_detach
+#pragma weak pthread_getattr_np
+#pragma weak pthread_attr_getstack
+ if (!cthread_fork)
{
err = __thread_create (__mach_task_self (), &_hurd_msgport_thread);
assert_perror (err);
@@ -1266,16 +1481,10 @@ _hurdsig_init (const int *intarray, size_t intarraysize)
(vm_address_t *) &__hurd_sigthread_stack_base,
&stacksize);
assert_perror (err);
+ err = __mach_setup_tls (_hurd_msgport_thread);
+ assert_perror (err);
__hurd_sigthread_stack_end = __hurd_sigthread_stack_base + stacksize;
- __hurd_sigthread_variables =
- malloc (__hurd_threadvar_max * sizeof (unsigned long int));
- if (__hurd_sigthread_variables == NULL)
- __libc_fatal ("hurd: Can't allocate threadvars for signal thread\n");
- memset (__hurd_sigthread_variables, 0,
- __hurd_threadvar_max * sizeof (unsigned long int));
- __hurd_sigthread_variables[_HURD_THREADVAR_LOCALE]
- = (unsigned long int) &_nl_global_locale;
/* Reinitialize the MiG support routines so they will use a per-thread
variable for the cached reply port. */
@@ -1286,6 +1495,7 @@ _hurdsig_init (const int *intarray, size_t intarraysize)
}
else
{
+ cthread_t thread;
/* When cthreads is being used, we need to make the signal thread a
proper cthread. Otherwise it cannot use mutex_lock et al, which
will be the cthreads versions. Various of the message port RPC
@@ -1295,9 +1505,20 @@ _hurdsig_init (const int *intarray, size_t intarraysize)
we'll let the signal thread's per-thread variables be found as for
any normal cthread, and just leave the magic __hurd_sigthread_*
values all zero so they'll be ignored. */
-#pragma weak cthread_fork
-#pragma weak cthread_detach
- cthread_detach (cthread_fork ((cthread_fn_t) &_hurd_msgport_receive, 0));
+ cthread_detach (thread = cthread_fork ((cthread_fn_t) &_hurd_msgport_receive, 0));
+
+ if (pthread_getattr_np)
+ {
+ /* Record stack layout for fork() */
+ pthread_attr_t attr;
+ void *addr;
+ size_t size;
+
+ pthread_getattr_np ((pthread_t) thread, &attr);
+ pthread_attr_getstack (&attr, &addr, &size);
+ __hurd_sigthread_stack_base = (uintptr_t) addr;
+ __hurd_sigthread_stack_end = __hurd_sigthread_stack_base + size;
+ }
/* XXX We need the thread port for the signal thread further on
in this thread (see hurdfault.c:_hurdsigfault_init).
@@ -1342,6 +1563,7 @@ reauth_proc (mach_port_t new)
if (! HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC],
__proc_reauthenticate (port, ref,
MACH_MSG_TYPE_MAKE_SEND) ||
+ /* FIXME: handle EINTR */
__auth_user_authenticate (new, ref,
MACH_MSG_TYPE_MAKE_SEND,
&ignore))
@@ -1350,14 +1572,14 @@ reauth_proc (mach_port_t new)
__mach_port_destroy (__mach_task_self (), ref);
/* Set the owner of the process here too. */
- mutex_lock (&_hurd_id.lock);
+ __mutex_lock (&_hurd_id.lock);
if (!_hurd_check_ids ())
HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC],
__proc_setowner (port,
(_hurd_id.gen.nuids
? _hurd_id.gen.uids[0] : 0),
!_hurd_id.gen.nuids));
- mutex_unlock (&_hurd_id.lock);
+ __mutex_unlock (&_hurd_id.lock);
(void) &reauth_proc; /* Silence compiler warning. */
}
diff --git a/hurd/hurdsock.c b/hurd/hurdsock.c
index 1aaec27acd..03d45e78a1 100644
--- a/hurd/hurdsock.c
+++ b/hurd/hurdsock.c
@@ -52,6 +52,7 @@ _hurd_socket_server (int domain, int dead)
return MACH_PORT_NULL;
}
+retry:
HURD_CRITICAL_BEGIN;
__mutex_lock (&lock);
@@ -102,6 +103,10 @@ _hurd_socket_server (int domain, int dead)
__mutex_unlock (&lock);
HURD_CRITICAL_END;
+ if (!server && errno == EINTR)
+ /* Got a signal while inside an RPC of the critical section, retry again */
+ goto retry;
+
return server;
}
diff --git a/hurd/hurdstartup.c b/hurd/hurdstartup.c
index 9de8f2a898..edfd96fcbd 100644
--- a/hurd/hurdstartup.c
+++ b/hurd/hurdstartup.c
@@ -23,7 +23,6 @@
#include <hurd.h>
#include <hurd/exec_startup.h>
#include <sysdep.h>
-#include <hurd/threadvar.h>
#include <unistd.h>
#include <elf.h>
#include <set-hooks.h>
diff --git a/hurd/intern-fd.c b/hurd/intern-fd.c
index 239bb8a874..91158f25c0 100644
--- a/hurd/intern-fd.c
+++ b/hurd/intern-fd.c
@@ -37,6 +37,7 @@ _hurd_intern_fd (io_t port, int flags, int dealloc)
__spin_unlock (&d->port.lock);
}
HURD_CRITICAL_END;
+ /* FIXME: handle EINTR! */
if (d == NULL)
{
diff --git a/hurd/lookup-at.c b/hurd/lookup-at.c
index 3528ba8ac3..5b03a4c9d0 100644
--- a/hurd/lookup-at.c
+++ b/hurd/lookup-at.c
@@ -29,16 +29,9 @@ __file_name_lookup_at (int fd, int at_flags,
error_t err;
file_t result;
- if ((at_flags & AT_SYMLINK_FOLLOW) && (at_flags & AT_SYMLINK_NOFOLLOW))
- return (__hurd_fail (EINVAL), MACH_PORT_NULL);
-
- flags |= (at_flags & AT_SYMLINK_NOFOLLOW) ? O_NOLINK : 0;
- at_flags &= ~AT_SYMLINK_NOFOLLOW;
- if (at_flags & AT_SYMLINK_FOLLOW)
- flags &= ~O_NOLINK;
- at_flags &= ~AT_SYMLINK_FOLLOW;
- if (at_flags != 0)
- return (__hurd_fail (EINVAL), MACH_PORT_NULL);
+ err = __hurd_at_flags (&at_flags, &flags);
+ if (err)
+ return (__hurd_fail (err), MACH_PORT_NULL);
if (fd == AT_FDCWD || file_name[0] == '/')
return __file_name_lookup (file_name, flags, mode);
diff --git a/hurd/lookup-retry.c b/hurd/lookup-retry.c
index aee2ba8f93..d37295972b 100644
--- a/hurd/lookup-retry.c
+++ b/hurd/lookup-retry.c
@@ -127,7 +127,7 @@ __hurd_file_name_lookup_retry (error_t (*use_init_port)
{
/* In Linux, O_NOFOLLOW means to reject symlinks. If we
did an O_NOLINK lookup above and io_stat here to check
- for S_IFLNK, a translator like firmlink could easily
+ for S_IFLNK only, a translator like firmlink could easily
spoof this check by not showing S_IFLNK, but in fact
redirecting the lookup to some other name
(i.e. opening the very same holes a symlink would).
@@ -145,23 +145,29 @@ __hurd_file_name_lookup_retry (error_t (*use_init_port)
one exception to our general translator-based rule. */
struct stat64 st;
err = __io_stat (*result, &st);
- if (!err
- && (st.st_mode & (S_IPTRANS|S_IATRANS)))
+ if (!err)
{
- if (st.st_uid != 0)
- err = ENOENT;
- else if (st.st_mode & S_IPTRANS)
+ if (flags & O_DIRECTORY && !S_ISDIR(st.st_mode))
+ err = ENOTDIR;
+ if (S_ISLNK(st.st_mode))
+ err = ELOOP;
+ else if (st.st_mode & (S_IPTRANS|S_IATRANS))
{
- char buf[1024];
- char *trans = buf;
- size_t translen = sizeof buf;
- err = __file_get_translator (*result,
- &trans, &translen);
- if (!err
- && translen > sizeof _HURD_SYMLINK
- && !memcmp (trans,
- _HURD_SYMLINK, sizeof _HURD_SYMLINK))
- err = ENOENT;
+ if (st.st_uid != 0)
+ err = ELOOP;
+ else if (st.st_mode & S_IPTRANS)
+ {
+ char buf[1024];
+ char *trans = buf;
+ size_t translen = sizeof buf;
+ err = __file_get_translator (*result,
+ &trans, &translen);
+ if (!err
+ && translen > sizeof _HURD_SYMLINK
+ && !memcmp (trans,
+ _HURD_SYMLINK, sizeof _HURD_SYMLINK))
+ err = ELOOP;
+ }
}
}
}
diff --git a/hurd/setauth.c b/hurd/setauth.c
index d38fdddd52..a8a625d65a 100644
--- a/hurd/setauth.c
+++ b/hurd/setauth.c
@@ -18,14 +18,13 @@
#include <hurd.h>
#include <hurd/port.h>
#include <hurd/id.h>
+#include <hurdlock.h>
#include "set-hooks.h"
/* Things in the library which want to be run when the auth port changes. */
DEFINE_HOOK (_hurd_reauth_hook, (auth_t new_auth));
-#include <cthreads.h>
-static struct mutex reauth_lock = MUTEX_INITIALIZER;
-
+static unsigned int reauth_lock = LLL_INITIALIZER;
/* Set the auth port to NEW, and reauthenticate
everything used by the library. */
@@ -108,6 +107,7 @@ _hurd_setauth (auth_t new)
__mutex_unlock (&reauth_lock);
HURD_CRITICAL_END;
+ /* FIXME: handle EINTR! */
return 0;
}
diff --git a/hurd/seteuids.c b/hurd/seteuids.c
index 21e3431e2a..c09fb0eb4a 100644
--- a/hurd/seteuids.c
+++ b/hurd/seteuids.c
@@ -47,6 +47,7 @@ seteuids (int n, const uid_t *uids)
}
__mutex_unlock (&_hurd_id.lock);
HURD_CRITICAL_END;
+ /* FIXME: handle EINTR! */
if (err)
return __hurd_fail (err);
diff --git a/hurd/sigunwind.c b/hurd/sigunwind.c
index 1f224621bb..a999173c50 100644
--- a/hurd/sigunwind.c
+++ b/hurd/sigunwind.c
@@ -18,6 +18,7 @@
#include <hurd.h>
#include <thread_state.h>
+#include <hurd/threadvar.h>
#include <jmpbuf-unwind.h>
#include <assert.h>
#include <stdint.h>
@@ -38,8 +39,7 @@ _hurdsig_longjmp_from_handler (void *data, jmp_buf env, int val)
{
/* Destroy the MiG reply port used by the signal handler, and restore
the reply port in use by the thread when interrupted. */
- mach_port_t *reply_port =
- (mach_port_t *) __hurd_threadvar_location (_HURD_THREADVAR_MIG_REPLY);
+ mach_port_t *reply_port = &__hurd_local_reply_port;
if (*reply_port)
{
mach_port_t port = *reply_port;
diff --git a/hurd/sysvshm.c b/hurd/sysvshm.c
new file mode 100644
index 0000000000..e049345135
--- /dev/null
+++ b/hurd/sysvshm.c
@@ -0,0 +1,98 @@
+/* SysV shared memory for Hurd.
+ Copyright (C) 2005-2015 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stddef.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <sys/shm.h>
+#include <hurdlock.h>
+
+
+/* Description of an shm attachment. */
+struct sysvshm_attach
+{
+ /* Linked list. */
+ struct sysvshm_attach *next;
+
+ /* Map address. */
+ void *addr;
+
+ /* Map size. */
+ size_t size;
+};
+
+/* List of attachments. */
+static struct sysvshm_attach *sysvshm_list;
+
+/* A lock to protect the linked list of shared memory attachments. */
+static unsigned int sysvshm_lock = LLL_INITIALIZER;
+
+
+/* Adds a segment attachment. */
+error_t
+__sysvshm_add (void *addr, size_t size)
+{
+ struct sysvshm_attach *shm;
+
+ shm = malloc (sizeof (*shm));
+ if (shm == NULL)
+ return errno;
+
+ __mutex_lock (&sysvshm_lock);
+ shm->addr = addr;
+ shm->size = size;
+ shm->next = sysvshm_list;
+ sysvshm_list = shm;
+ __mutex_unlock (&sysvshm_lock);
+
+ return 0;
+}
+
+/* Removes a segment attachment. On success, returns 0 and sets *SIZE to its
+ size. Returns EINVAL if not found. */
+error_t
+__sysvshm_remove (void *addr, size_t *size)
+{
+ struct sysvshm_attach *shm;
+ struct sysvshm_attach **pshm = &sysvshm_list;
+
+ __mutex_lock (&sysvshm_lock);
+ shm = sysvshm_list;
+ while (shm != NULL)
+ {
+ shm = *pshm;
+ if (shm->addr == addr)
+ {
+ *pshm = shm->next;
+ *size = shm->size;
+ __mutex_unlock (&sysvshm_lock);
+ free (shm);
+ return 0;
+ }
+ pshm = &shm->next;
+ shm = shm->next;
+ }
+ __mutex_unlock (&sysvshm_lock);
+ return EINVAL;
+}
diff --git a/hurd/sysvshm.h b/hurd/sysvshm.h
new file mode 100644
index 0000000000..8b9c29ff46
--- /dev/null
+++ b/hurd/sysvshm.h
@@ -0,0 +1,52 @@
+/* SysV shared memory for Hurd.
+ Copyright (C) 2005-2015 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#ifndef _HURD_SYSVSHM_H
+#define _HURD_SYSVSHM_H
+
+#include <paths.h>
+#include <hurd.h>
+
+/* The area (from top to bottom) that is used for private keys. These
+ are all keys that have the second highest bit set. */
+#define SHM_PRIV_KEY_START INT_MAX
+#define SHM_PRIV_KEY_END ((INT_MAX / 2) + 1)
+
+#define SHM_PREFIX "sysvshm-"
+#define SHM_DIR _PATH_DEV "shm/"
+
+/* The maximum number of characters in a shared memory segment file name.
+ 32 is the max number of characters in a 128 bit number in hex. */
+#if __WORDSIZE > 128
+#error Need to increase SHM_NAMEMAX.
+#else
+#define SHM_NAMEMAX (sizeof (SHM_PREFIX) - 1 + 32 + 1)
+#endif
+
+/* Use this with printf and its variants. */
+#define SHM_NAMEPRI SHM_PREFIX "%0x"
+
+
+/* Adds a segment attachment. */
+error_t __sysvshm_add (void *addr, size_t size);
+
+/* Removes a segment attachment. On success, returns 0 and sets *SIZE to its
+ size. Returns EINVAL if not found. */
+error_t __sysvshm_remove (void *addr, size_t *size);
+
+#endif /* sysvshm.h */
diff --git a/hurd/thread-cancel.c b/hurd/thread-cancel.c
index 6cf5e752b7..ef2b8a028c 100644
--- a/hurd/thread-cancel.c
+++ b/hurd/thread-cancel.c
@@ -51,7 +51,6 @@ hurd_thread_cancel (thread_t thread)
return 0;
}
- assert (! __spin_lock_locked (&ss->critical_section_lock));
__spin_lock (&ss->critical_section_lock);
__spin_lock (&ss->lock);
err = __thread_suspend (thread);
@@ -80,6 +79,7 @@ hurd_thread_cancel (thread_t thread)
}
_hurd_critical_section_unlock (ss);
+ /* FIXME: handle EINTR */
return err;
}
@@ -91,7 +91,6 @@ hurd_check_cancel (void)
int cancel;
__spin_lock (&ss->lock);
- assert (! __spin_lock_locked (&ss->critical_section_lock));
cancel = ss->cancel;
ss->cancel = 0;
__spin_unlock (&ss->lock);