diff options
author | Roland McGrath <roland@gnu.org> | 1995-02-18 01:27:10 +0000 |
---|---|---|
committer | Roland McGrath <roland@gnu.org> | 1995-02-18 01:27:10 +0000 |
commit | 28f540f45bbacd939bfd07f213bcad2bf730b1bf (patch) | |
tree | 15f07c4c43d635959c6afee96bde71fb1b3614ee /hurd/hurd |
initial import
Diffstat (limited to 'hurd/hurd')
-rw-r--r-- | hurd/hurd/fd.h | 222 | ||||
-rw-r--r-- | hurd/hurd/id.h | 55 | ||||
-rw-r--r-- | hurd/hurd/ioctl.h | 71 | ||||
-rw-r--r-- | hurd/hurd/port.h | 152 | ||||
-rw-r--r-- | hurd/hurd/resource.h | 50 | ||||
-rw-r--r-- | hurd/hurd/signal.h | 391 | ||||
-rw-r--r-- | hurd/hurd/threadvar.h | 107 | ||||
-rw-r--r-- | hurd/hurd/userlink.h | 105 |
8 files changed, 1153 insertions, 0 deletions
diff --git a/hurd/hurd/fd.h b/hurd/hurd/fd.h new file mode 100644 index 0000000000..4747c785a5 --- /dev/null +++ b/hurd/hurd/fd.h @@ -0,0 +1,222 @@ +/* File descriptors. +Copyright (C) 1993, 1994, 1995 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 Library General Public License as +published by the Free Software Foundation; either version 2 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#ifndef _HURD_FD_H + +#define _HURD_FD_H 1 +#include <features.h> + +#include <hurd/hurd_types.h> +#include <hurd/port.h> + + +/* Structure representing a file descriptor. */ + +struct hurd_fd + { + struct hurd_port port; /* io server port. */ + int flags; /* fcntl flags; locked by port.lock. */ + + /* Normal port to the ctty. When `port' is our ctty, this is a port to + the same io object but which never returns EBACKGROUND; when not, + this is nil. */ + struct hurd_port ctty; + }; + + +/* Current file descriptor table. */ + +extern int _hurd_dtablesize; +extern struct hurd_fd **_hurd_dtable; +extern struct mutex _hurd_dtable_lock; /* Locks those two variables. */ + +#include <hurd/signal.h> +#include <lock-intern.h> + +#ifndef _EXTERN_INLINE +#define _EXTERN_INLINE extern __inline +#endif + +/* Returns the descriptor cell for FD. If FD is invalid or unused, return + NULL. The cell is unlocked; when ready to use it, lock it and check for + it being unused. */ + +_EXTERN_INLINE struct hurd_fd * +_hurd_fd_get (int fd) +{ + struct hurd_fd *descriptor; + + __mutex_lock (&_hurd_dtable_lock); + if (fd < 0 || fd >= _hurd_dtablesize) + descriptor = NULL; + else + { + struct hurd_fd *cell = _hurd_dtable[fd]; + if (cell == NULL) + /* No descriptor allocated at this index. */ + descriptor = NULL; + else + { + __spin_lock (&cell->port.lock); + if (cell->port.port == MACH_PORT_NULL) + /* The descriptor at this index has no port in it. + This happens if it existed before but was closed. */ + descriptor = NULL; + else + descriptor = cell; + __spin_unlock (&cell->port.lock); + } + } + __mutex_unlock (&_hurd_dtable_lock); + + return descriptor; +} + + +/* Evaluate EXPR with the variable `descriptor' bound to a pointer to the + file descriptor structure for FD. */ + +#define HURD_FD_USE(fd, expr) \ + ({ struct hurd_fd *descriptor = _hurd_fd_get (fd); \ + descriptor == NULL ? EBADF : (expr); }) + +/* Evaluate EXPR with the variable `port' bound to the port to FD, and + `ctty' bound to the ctty port. */ + +#define HURD_DPORT_USE(fd, expr) \ + HURD_FD_USE ((fd), HURD_FD_PORT_USE (descriptor, (expr))) + +/* Likewise, but FD is a pointer to the file descriptor structure. */ + +#define HURD_FD_PORT_USE(fd, expr) \ + ({ error_t __result; \ + struct hurd_fd *const __d = (fd); \ + struct hurd_userlink __ulink, __ctty_ulink; \ + io_t port, ctty; \ + void *crit = _hurd_critical_section_lock (); \ + __spin_lock (&__d->port.lock); \ + if (__d->port.port == MACH_PORT_NULL) \ + { \ + __spin_unlock (&__d->port.lock); \ + _hurd_critical_section_unlock (crit); \ + __result = EBADF; \ + } \ + else \ + { \ + ctty = _hurd_port_get (&__d->ctty, &__ctty_ulink); \ + port = _hurd_port_locked_get (&__d->port, &__ulink); \ + _hurd_critical_section_unlock (crit); \ + __result = (expr); \ + _hurd_port_free (&__d->port, &__ulink, port); \ + if (ctty != MACH_PORT_NULL) \ + _hurd_port_free (&__d->ctty, &__ctty_ulink, ctty); \ + } \ + __result; }) + +#include <errno.h> + +/* Check if ERR should generate a signal. + Returns the signal to take, or zero if none. */ + +_EXTERN_INLINE error_t +_hurd_fd_error_signal (error_t err) +{ + switch (err) + { + case EMACH_SEND_INVALID_DEST: + case EMIG_SERVER_DIED: + /* The server has disappeared! */ + return SIGLOST; + case EPIPE: + return SIGPIPE; + default: + /* Having a default case avoids -Wenum-switch warnings. */ + return 0; + } +} + +/* 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. */ + +_EXTERN_INLINE error_t +_hurd_fd_error (int fd, error_t err) +{ + int signo = _hurd_fd_error_signal (err); + if (signo) + _hurd_raise_signal (NULL, signo, fd, err); + return err; +} + +/* Handle error code ERR from an RPC on file descriptor FD's port. + Set `errno' to the appropriate error code, and always return -1. */ + +_EXTERN_INLINE int +__hurd_dfail (int fd, error_t err) +{ + errno = _hurd_fd_error (fd, err); + return -1; +} + +/* Set up *FD to have PORT its server port, doing appropriate ctty magic. + Does no locking or unlocking. */ + +extern void _hurd_port2fd (struct hurd_fd *fd, io_t port, int flags); + +/* Allocate a new file descriptor and install PORT in it (doing any + appropriate ctty magic); consumes a user reference on PORT. FLAGS are + as for `open'; only O_IGNORE_CTTY is meaningful, but all are saved. + + If the descriptor table is full, set errno, and return -1. + If DEALLOC is nonzero, deallocate PORT first. */ + +extern int _hurd_intern_fd (io_t port, int flags, int dealloc); + +/* Allocate a new file descriptor in the table and return it, locked. The + new descriptor number will be no less than FIRST_FD. If the table is + full, set errno to EMFILE and return NULL. If FIRST_FD is negative or + bigger than the size of the table, set errno to EINVAL and return NULL. */ + +extern struct hurd_fd *_hurd_alloc_fd (int *fd_ptr, int first_fd); + +/* Allocate a new file descriptor structure and initialize its port cells + with PORT and CTTY. (This does not affect the descriptor table.) */ + +extern struct hurd_fd *_hurd_new_fd (io_t port, io_t ctty); + +/* Close a file descriptor, making it available for future reallocation. */ + +extern error_t _hurd_fd_close (struct hurd_fd *fd); + +/* Read and write data from a file descriptor; just like `read' and `write'. + If successful, stores the amount actually read or written in *NBYTES. */ + +extern error_t _hurd_fd_read (struct hurd_fd *fd, void *buf, size_t *nbytes); +extern error_t _hurd_fd_write (struct hurd_fd *fd, + const void *buf, size_t *nbytes); + + +/* Call *RPC on PORT and/or CTTY; if a call on CTTY returns EBACKGROUND, + generate SIGTTIN/SIGTTOU or EIO as appropriate. */ + +extern error_t _hurd_ctty_input (io_t port, io_t ctty, error_t (*rpc) (io_t)); +extern error_t _hurd_ctty_output (io_t port, io_t ctty, error_t (*rpc) (io_t)); + + +#endif /* hurd/fd.h */ diff --git a/hurd/hurd/id.h b/hurd/hurd/id.h new file mode 100644 index 0000000000..7a50081038 --- /dev/null +++ b/hurd/hurd/id.h @@ -0,0 +1,55 @@ +/* User and group IDs. +Copyright (C) 1993, 1994 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 Library General Public License as +published by the Free Software Foundation; either version 2 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#ifndef _HURD_ID_H + +#define _HURD_ID_H 1 +#include <features.h> + +#include <cthreads.h> /* For `struct mutex'. */ + +/* Structure describing authorization data for the process. */ + +struct hurd_id_data + { + struct mutex lock; + + int valid; /* If following data are up to date. */ + + struct + { + uid_t *uids; + gid_t *gids; + mach_msg_type_number_t nuids, ngids; + } gen, aux; + + auth_t rid_auth; /* Cache used by access. */ + }; + +/* Current data. */ + +extern struct hurd_id_data _hurd_id; + + +/* Update _hurd_id (caller should be holding the lock). */ + +extern error_t _hurd_check_ids (void); + + +#endif /* hurd/id.h */ diff --git a/hurd/hurd/ioctl.h b/hurd/hurd/ioctl.h new file mode 100644 index 0000000000..cc83433c17 --- /dev/null +++ b/hurd/hurd/ioctl.h @@ -0,0 +1,71 @@ +/* User-registered handlers for specific `ioctl' requests. +Copyright (C) 1993, 1994, 1995 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 Library General Public License as +published by the Free Software Foundation; either version 2 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#ifndef _HURD_IOCTL_H +#define _HURD_IOCTL_H 1 + +#define __need___va_list +#include <stdarg.h> + + +/* Type of handler function, called like ioctl to do its entire job. */ +typedef int (*ioctl_handler_t) (int fd, int request, void *arg); + +/* Structure that records an ioctl handler. */ +struct ioctl_handler + { + int first_request, last_request; /* Range of handled request values. */ + + /* Handler function, called like ioctl to do its entire job. */ + ioctl_handler_t handler; + + struct ioctl_handler *next; /* Next handler. */ + }; + + +/* Register HANDLER to handle ioctls with REQUEST values between + FIRST_REQUEST and LAST_REQUEST inclusive. Returns zero if successful. + Return nonzero and sets `errno' for an error. */ + +extern int hurd_register_ioctl_handler (int first_request, int last_request, + ioctl_handler_t handler); + + +/* Define a library-internal handler for ioctl commands between FIRST and + LAST inclusive. The last element gratuitously references HANDLER to + avoid `defined but not used' warnings. */ + +#define _HURD_HANDLE_IOCTLS(handler, first, last) \ + static const struct ioctl_handler handler##_ioctl_handler = \ + { (first), (last), (int (*) (int, int, void *)) (handler), \ + (&(handler), &(handler##_ioctl_handler), NULL) }; \ + text_set_element (_hurd_ioctl_handler_lists, ##handler##_ioctl_handler) + +/* Define a library-internal handler for a single ioctl command. */ + +#define _HURD_HANDLE_IOCTL(handler, ioctl) \ + _HURD_HANDLE_IOCTLS (handler, (ioctl), (ioctl)) + + +/* Lookup the handler for the given ioctl request. */ + +ioctl_handler_t _hurd_lookup_ioctl_handler (int request); + + +#endif /* hurd/ioctl.h */ diff --git a/hurd/hurd/port.h b/hurd/hurd/port.h new file mode 100644 index 0000000000..a057503d4a --- /dev/null +++ b/hurd/hurd/port.h @@ -0,0 +1,152 @@ +/* Lightweight user references for ports. +Copyright (C) 1993, 1994 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 Library General Public License as +published by the Free Software Foundation; either version 2 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#ifndef _HURD_PORT_H + +#define _HURD_PORT_H 1 +#include <features.h> + +#include <mach.h> +#include <hurd/userlink.h> +#include <spin-lock.h> +#include <hurd/signal.h> + + +/* Structure describing a cell containing a port. With the lock held, a + user extracts PORT, and attaches his own link (in local storage) to the + USERS chain. PORT can then safely be used. When PORT is no longer + needed, with the lock held, the user removes his link from the chain. + If his link is the last, and PORT has changed since he fetched it, the + user deallocates the port he used. See <hurd/userlink.h>. */ + +struct hurd_port + { + spin_lock_t lock; /* Locks rest. */ + struct hurd_userlink *users; /* Chain of users; see below. */ + mach_port_t port; /* Port. */ + }; + + +/* Evaluate EXPR with the variable `port' bound to the port in PORTCELL. */ + +#define HURD_PORT_USE(portcell, expr) \ + ({ struct hurd_port *const __p = (portcell); \ + struct hurd_userlink __link; \ + const mach_port_t port = _hurd_port_get (__p, &__link); \ + __typeof(expr) __result = (expr); \ + _hurd_port_free (__p, &__link, port); \ + __result; }) + + +#ifndef _EXTERN_INLINE +#define _EXTERN_INLINE extern __inline +#endif + + +/* Initialize *PORT to INIT. */ + +_EXTERN_INLINE void +_hurd_port_init (struct hurd_port *port, mach_port_t init) +{ + __spin_lock_init (&port->lock); + port->users = NULL; + port->port = init; +} + + +/* Get a reference to *PORT, which is locked. + Pass return value and LINK to _hurd_port_free when done. */ + +_EXTERN_INLINE mach_port_t +_hurd_port_locked_get (struct hurd_port *port, + struct hurd_userlink *link) +{ + mach_port_t result; + result = port->port; + if (result != MACH_PORT_NULL) + _hurd_userlink_link (&port->users, link); + __spin_unlock (&port->lock); + return result; +} + +/* Same, but locks PORT first. */ + +_EXTERN_INLINE mach_port_t +_hurd_port_get (struct hurd_port *port, + struct hurd_userlink *link) +{ + mach_port_t result; + HURD_CRITICAL_BEGIN; + __spin_lock (&port->lock); + result = _hurd_port_locked_get (port, link); + HURD_CRITICAL_END; + return result; +} + + +/* Free a reference gotten with `USED_PORT = _hurd_port_get (PORT, LINK);' */ + +_EXTERN_INLINE void +_hurd_port_free (struct hurd_port *port, + struct hurd_userlink *link, + mach_port_t used_port) +{ + int dealloc; + if (used_port == MACH_PORT_NULL) + /* When we fetch an empty port cell with _hurd_port_get, + it does not link us on the users chain, since there is + no shared resource. */ + return; + HURD_CRITICAL_BEGIN; + __spin_lock (&port->lock); + dealloc = _hurd_userlink_unlink (link); + __spin_unlock (&port->lock); + HURD_CRITICAL_END; + if (dealloc) + __mach_port_deallocate (__mach_task_self (), used_port); +} + + +/* Set *PORT's port to NEWPORT. NEWPORT's reference is consumed by PORT->port. + PORT->lock is locked. */ + +_EXTERN_INLINE void +_hurd_port_locked_set (struct hurd_port *port, mach_port_t newport) +{ + mach_port_t old; + old = _hurd_userlink_clear (&port->users) ? port->port : MACH_PORT_NULL; + port->port = newport; + __spin_unlock (&port->lock); + if (old != MACH_PORT_NULL) + __mach_port_deallocate (__mach_task_self (), old); +} + +/* Same, but locks PORT first. */ + +_EXTERN_INLINE void +_hurd_port_set (struct hurd_port *port, mach_port_t newport) +{ + HURD_CRITICAL_BEGIN; + __spin_lock (&port->lock); + _hurd_port_locked_set (port, newport); + HURD_CRITICAL_END; +} + + +#endif /* hurd/port.h */ diff --git a/hurd/hurd/resource.h b/hurd/hurd/resource.h new file mode 100644 index 0000000000..ad2a61ab42 --- /dev/null +++ b/hurd/hurd/resource.h @@ -0,0 +1,50 @@ +/* Resource limits for the Hurd. +Copyright (C) 1994 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 Library General Public License as +published by the Free Software Foundation; either version 2 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#ifndef _HURD_RESOURCE_H +#define _HURD_RESOURCE_H + +#include <sys/types.h> +#include <sys/resource.h> +#include <errno.h> +#include <hurd/process.h> + +/* This array contains the current resource limits for the process. */ +extern struct rlimit _hurd_rlimits[RLIM_NLIMITS]; +extern struct mutex _hurd_rlimit_lock; /* Locks _hurd_rlimits. */ + + +/* Helper function for getpriority and setpriority. Maps FN over all the + processes specified by WHICH and WHO. PI is non-null if a + proc_getprocinfo was already done; FN may use *PI arbitrarily, it is + reset on the next call. Returns FN's result the first time it returns + nonzero. If FN never returns nonzero, this returns zero. */ +extern error_t _hurd_priority_which_map (enum __priority_which which, int who, + error_t (*fn) (pid_t pid, + struct procinfo *pi)); + +/* Convert between Mach priority values and the priority + values used by getpriority, setpriority, and nice. */ +#define MACH_PRIORITY_TO_NICE(prio) (2 * ((prio) - 12)) +#define NICE_TO_MACH_PRIORITY(nice) (12 + ((nice) / 2)) + + + + +#endif diff --git a/hurd/hurd/signal.h b/hurd/hurd/signal.h new file mode 100644 index 0000000000..76007d5037 --- /dev/null +++ b/hurd/hurd/signal.h @@ -0,0 +1,391 @@ +/* Implementing POSIX.1 signals under the Hurd. +Copyright (C) 1993, 1994, 1995 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 Library General Public License as +published by the Free Software Foundation; either version 2 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#ifndef _HURD_SIGNAL_H + +#define _HURD_SIGNAL_H 1 +#include <features.h> +/* Make sure <signal.h> is going to define NSIG. */ +#ifndef __USE_GNU +#error "Must have `_GNU_SOURCE' feature test macro to use this file" +#endif + +#define __need_NULL +#include <stddef.h> + +#include <mach/mach_types.h> +#include <mach/port.h> +#include <mach/message.h> +#include <hurd/hurd_types.h> +#include <signal.h> +#include <errno.h> +#include <hurd/msg.h> + +#include <cthreads.h> /* For `struct mutex'. */ +#include <spin-lock.h> +#include <hurd/threadvar.h> /* We cache sigstate in a threadvar. */ + + +/* Per-thread signal state. */ + +struct hurd_sigstate + { + spin_lock_t lock; /* Locks most of the rest of the structure. */ + + int critical_section; /* Nonzero if in critical section. */ + + 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. */ + struct sigaction actions[NSIG]; + struct sigaltstack sigaltstack; + struct + { + /* For each signal that may be pending, the + sigcode and error code to deliver it with. */ + long int code; + error_t error; + } pending_data[NSIG]; + + /* If `suspended' is set when this thread gets a signal, + the signal thread sends an empty message to it. */ + mach_port_t suspended; + + /* The following members are not locked. They are used only by this + thread, or by the signal thread with this thread suspended. */ + + volatile mach_port_t intr_port; /* Port interruptible RPC was sent on. */ + + /* If this is not null, the thread is in sigreturn awaiting delivery of + pending signals. This context (the machine-dependent portions only) + will be passed to sigreturn after running the handler for a pending + signal, instead of examining the thread state. */ + struct sigcontext *context; + }; + +/* Linked list of states of all threads whose state has been asked for. */ + +extern struct hurd_sigstate *_hurd_sigstates; + +extern struct mutex _hurd_siglock; /* Locks _hurd_sigstates. */ + +/* Get the sigstate of a given thread, taking its lock. */ + +extern struct hurd_sigstate *_hurd_thread_sigstate (thread_t); + +/* Get the sigstate of the current thread. + This uses a per-thread variable to optimize the lookup. */ +_EXTERN_INLINE struct hurd_sigstate * +_hurd_self_sigstate (void) +{ + struct hurd_sigstate **location = + (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE); + if (*location == NULL) + *location = _hurd_thread_sigstate (__mach_thread_self ()); + return *location; +} + +/* Thread listening on our message port; also called the "signal thread". */ + +extern thread_t _hurd_msgport_thread; + +/* Our message port. We hold the receive right and _hurd_msgport_thread + listens for messages on it. We also hold a send right, for convenience. */ + +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; + +/* Critical sections. + + A critical section is a section of code which cannot safely be interrupted + to run a signal handler; for example, code that holds any lock cannot be + interrupted lest the signal handler try to take the same lock and + deadlock result. */ + +_EXTERN_INLINE void * +_hurd_critical_section_lock (void) +{ + struct hurd_sigstate **location = + (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE); + struct hurd_sigstate *ss = *location; + if (ss == NULL) + /* 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; this locks the sigstate lock. */ + ss = *location = _hurd_thread_sigstate (__mach_thread_self ()); + else + __spin_lock (&ss->lock); + + if (ss->critical_section) + { + /* We are already in a critical section, so do nothing. */ + __spin_unlock (&ss->lock); + return NULL; + } + + /* Set the critical section flag so no signal handler will run. */ + ss->critical_section = 1; + __spin_unlock (&ss->lock); + + /* Return our sigstate pointer; this will be passed to + _hurd_critical_section_unlock to clear the critical section flag. */ + return ss; +} + +_EXTERN_INLINE void +_hurd_critical_section_unlock (void *our_lock) +{ + if (our_lock == NULL) + /* The critical section lock was held when we began. Do nothing. */ + return; + else + { + /* It was us who acquired the critical section lock. Clear the + critical section flag. */ + struct hurd_sigstate *ss = our_lock; + sigset_t pending; + __spin_lock (&ss->lock); + ss->critical_section = 0; + pending = ss->pending & ~ss->blocked; + __spin_unlock (&ss->lock); + if (pending) + /* There are unblocked signals pending, which weren't + delivered because we were in the critical section. + Tell the signal thread to deliver them now. */ + __msg_sig_post (_hurd_msgport, 0, __mach_task_self ()); + } +} + +/* Convenient macros for simple uses of critical sections. + These two must be used as a pair at the same C scoping level. */ + +#define HURD_CRITICAL_BEGIN \ + { void *__hurd_critical__ = _hurd_critical_section_lock () +#define HURD_CRITICAL_END \ + _hurd_critical_section_unlock (__hurd_critical__); } while (0) + +/* Initialize the signal code, and start the signal thread. */ + +extern void _hurdsig_init (void); + +/* Initialize proc server-assisted fault recovery for the signal thread. */ + +extern void _hurdsig_fault_init (void); + +/* Raise a signal as described by SIGNO, SIGCODE and SIGERROR, on the + thread whose sigstate SS points to. If SS is a null pointer, this + instead affects the calling thread. */ + +extern void _hurd_raise_signal (struct hurd_sigstate *ss, + int signo, long int sigcode, int sigerror); + +/* Translate a Mach exception into a signal (machine-dependent). */ + +extern void _hurd_exception2signal (int exception, int code, int subcode, + int *signo, long int *sigcode, int *error); + + +/* Make the thread described by SS take the signal described by SIGNO and + SIGCODE. If the process is traced, this will in fact stop with a SIGNO + as the stop signal unless UNTRACED is nonzero. When the signal can be + considered delivered, sends a sig_post reply message on REPLY_PORT + indicating success. SS is not locked. */ + +extern void _hurd_internal_post_signal (struct hurd_sigstate *ss, + int signo, long int sigcode, int error, + mach_port_t reply_port, + mach_msg_type_name_t reply_port_type, + int untraced); + +/* Set up STATE and SS to handle signal SIGNO by running HANDLER. If + RPC_WAIT is nonzero, the thread needs to wait for a pending RPC to + finish before running the signal handler. The handler is passed SIGNO, + SIGCODE, and the returned `struct sigcontext' (which resides on the + stack the handler will use, and which describes the state of the thread + encoded in STATE before running the handler). */ + +struct machine_thread_all_state; +extern struct sigcontext * +_hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler, + int signo, long int sigcode, + int rpc_wait, struct machine_thread_all_state *state); + +/* Function run by the signal thread to receive from the signal port. */ + +extern void _hurd_msgport_receive (void); + +/* STATE describes a thread that had intr_port set (meaning it was inside + HURD_EINTR_RPC), after it has been thread_abort'd. It it looks to have + just completed a mach_msg_trap system call that returned + MACH_RCV_INTERRUPTED, return nonzero and set *PORT to the receive right + being waited on. */ + +extern int _hurdsig_rcv_interrupted_p (struct machine_thread_all_state *state, + mach_port_t *port); + +/* Set up STATE with a thread state that, when resumed, is + like `longjmp (_hurd_sigthread_fault_env, 1)'. */ + +extern void _hurd_initialize_fault_recovery_state (void *state); + + +/* Function run for SIGINFO when its action is SIG_DFL and the current + process is the session leader. */ + +extern void _hurd_siginfo_handler (int); + + +/* Perform interruptible RPC CALL on PORT. + The call should use + The args in CALL should be constant or local variable refs. + They may be evaluated many times, and must not change. + PORT must not be deallocated before this RPC is finished. */ +#define HURD_EINTR_RPC(port, call) \ + ({ \ + __label__ __do_call; /* Give this label block scope. */ \ + error_t __err; \ + struct hurd_sigstate *__ss = _hurd_self_sigstate (); \ + __do_call: \ + /* Tell the signal thread that we are doing an interruptible RPC on \ + this port. If we get a signal and should return EINTR, the signal \ + thread will set this variable to MACH_PORT_NULL. The RPC might \ + return EINTR when some other thread gets a signal, in which case we \ + want to restart our call. */ \ + __ss->intr_port = (port); \ + /* A signal may arrive here, after intr_port is set, but before the \ + mach_msg system call. The signal handler might do an interruptible \ + RPC, and clobber intr_port; then it would not be set properly when \ + we actually did send the RPC, and a later signal wouldn't interrupt \ + that RPC. So, _hurd_setup_sighandler saves intr_port in the \ + sigcontext, and sigreturn restores it. */ \ + switch (__err = (call)) \ + { \ + case EINTR: /* RPC went out and was interrupted. */ \ + case MACH_SEND_INTERRUPTED: /* RPC didn't get out. */ \ + if (__ss->intr_port != MACH_PORT_NULL) \ + /* If this signal was for us and it should interrupt calls, the \ + signal thread will have cleared SS->intr_port. Since it's not \ + cleared, the signal was for another thread, or SA_RESTART is \ + set. Restart the interrupted call. */ \ + goto __do_call; \ + /* FALLTHROUGH */ \ + case MACH_RCV_PORT_DIED: \ + /* Server didn't respond to interrupt_operation, \ + so the signal thread destroyed the reply port. */ \ + __err = EINTR; \ + break; \ + default: /* Quiet -Wswitch-enum. */ \ + } \ + __ss->intr_port = MACH_PORT_NULL; \ + __err; \ + }) \ + + +/* Mask of signals that cannot be caught, blocked, or ignored. */ +#define _SIG_CANT_MASK (__sigmask (SIGSTOP) | __sigmask (SIGKILL)) + +/* Do an RPC to a process's message port. + + Each argument is an expression which returns an error code; each + expression may be evaluated several times. FETCH_MSGPORT_EXPR should + fetch the appropriate message port and store it in the local variable + `msgport'; it will be deallocated after use. FETCH_REFPORT_EXPR should + fetch the appropriate message port and store it in the local variable + `refport' (if no reference port is needed in the call, then + FETCH_REFPORT_EXPR should be simply KERN_SUCCESS or 0); if + DEALLOC_REFPORT evaluates to nonzero it will be deallocated after use, + otherwise the FETCH_REFPORT_EXPR must take care of user references to + `refport'. RPC_EXPR should perform the desired RPC operation using + `msgport' and `refport'. + + The reason for the complexity is that a process's message port and + reference port may change between fetching those ports and completing an + RPC using them (usually they change only when a process execs). The RPC + will fail with MACH_SEND_INVALID_DEST if the msgport dies before we can + send the RPC request; or with MIG_SERVER_DIED if the msgport was + destroyed after we sent the RPC request but before it was serviced. In + either of these cases, we retry the entire operation, discarding the old + message and reference ports and fetch them anew. */ + +#define HURD_MSGPORT_RPC(fetch_msgport_expr, \ + fetch_refport_expr, dealloc_refport, \ + rpc_expr) \ +({ \ + error_t __err; \ + mach_port_t msgport, refport = MACH_PORT_NULL; \ + do \ + { \ + /* Get the message port. */ \ + if (__err = (fetch_msgport_expr)) \ + break; \ + /* Get the reference port. */ \ + if (__err = (fetch_refport_expr)) \ + { \ + /* Couldn't get it; deallocate MSGPORT and fail. */ \ + __mach_port_deallocate (__mach_task_self (), msgport); \ + break; \ + } \ + __err = (rpc_expr); \ + __mach_port_deallocate (__mach_task_self (), msgport); \ + if ((dealloc_refport) && refport != MACH_PORT_NULL) \ + __mach_port_deallocate (__mach_task_self (), refport); \ + } while (__err == MACH_SEND_INVALID_DEST || \ + __err == MIG_SERVER_DIED); \ + __err; \ +}) + +/* Some other parts of the library need to preempt signals, to detect + errors that should not result in a POSIX signal. For example, when + some mapped region of memory is used, an extraneous SIGSEGV might be + generated when the mapping server returns an error for a page fault. */ + +struct hurd_signal_preempt + { + /* Function to examine a thread receiving a given signal. The handler + is called even for blocked signals. This function is run in the + signal thread, with THREAD's sigstate locked; it should be as simple + and robust as possible. THREAD is the thread which is about to + receive the signal. SIGNO and SIGCODE would be passed to the normal + handler. + + If the return value is SIG_DFL, normal signal processing continues. + If it is SIG_IGN, the signal is ignored. + Any other value is used in place of the normal handler. */ + sighandler_t (*handler) (thread_t thread, + int signo, long int sigcode, int sigerror); + long int first, last; /* Range of sigcodes this handler wants. */ + struct hurd_signal_preempt *next; /* Next handler on the chain. */ + }; + +extern struct hurd_signal_preempt *_hurd_signal_preempt[NSIG]; +extern struct mutex _hurd_signal_preempt_lock; + + +#endif /* hurd/signal.h */ diff --git a/hurd/hurd/threadvar.h b/hurd/hurd/threadvar.h new file mode 100644 index 0000000000..eab133a2f6 --- /dev/null +++ b/hurd/hurd/threadvar.h @@ -0,0 +1,107 @@ +/* Internal per-thread variables for the Hurd. +Copyright (C) 1994 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 Library General Public License as +published by the Free Software Foundation; either version 2 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#ifndef _HURD_THREADVAR_H +#define _HURD_THREADVAR_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 + __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. */ +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_MAX /* Default value for __hurd_threadvar_max. */ + }; + + +#ifndef _EXTERN_INLINE +#define _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_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_INLINE unsigned long int * +__hurd_threadvar_location (enum __hurd_threadvar_index __index) +{ + return __hurd_threadvar_location_from_sp (__index, + __thread_stack_pointer ()); +} + +/* Return the current thread's location for `errno'. + The syntax of this function allows redeclarations like `int errno'. */ +_EXTERN_INLINE int * +__hurd_errno_location (void) +{ + return (int *) __hurd_threadvar_location (_HURD_THREADVAR_ERRNO); +} + + +#endif /* hurd/threadvar.h */ diff --git a/hurd/hurd/userlink.h b/hurd/hurd/userlink.h new file mode 100644 index 0000000000..337d46aef6 --- /dev/null +++ b/hurd/hurd/userlink.h @@ -0,0 +1,105 @@ +/* Support for chains recording users of a resource; `struct hurd_userlink'. +Copyright (C) 1994 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 Library General Public License as +published by the Free Software Foundation; either version 2 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with the GNU C Library; see the file COPYING.LIB. If +not, write to the Free Software Foundation, Inc., 675 Mass Ave, +Cambridge, MA 02139, USA. */ + +#ifndef _HURD_USERLINK_H + +#define _HURD_USERLINK_H 1 +#include <features.h> + +#define __need_NULL +#include <stddef.h> + + +/* This structure is simply a doubly-linked list. Users of a given + resource are recorded by their presence in a list associated with that + resource. A user attaches his own link (in local storage) to a shared + chain at the time he begins using some resource. When finished with + that resource, the user removes his link from the chain. If his link is + the last (there are no other users of the resource), and his chain has + been detached from the shared cell (the resource in the cell has been + replaced), then the user deallocates the resource that he used. */ + +struct hurd_userlink + { + struct hurd_userlink *next, **prevp; + }; + + +#ifndef _EXTERN_INLINE +#define _EXTERN_INLINE extern __inline +#endif + + +/* Attach LINK to the chain of users at *CHAINP. */ + +_EXTERN_INLINE void +_hurd_userlink_link (struct hurd_userlink **chainp, + struct hurd_userlink *link) +{ + link->next = *chainp; + if (link->next) + link->next->prevp = &link->next; + link->prevp = chainp; + *chainp = link; +} + + +/* Detach LINK from its chain. If the return value is nonzero, the caller + should deallocate the resource he started using after attaching LINK to + the chain it's on. If the return value is zero, then someone else is + still using the resource. */ + +_EXTERN_INLINE int +_hurd_userlink_unlink (struct hurd_userlink *link) +{ + /* The caller should deallocate the resource he used if his chain has + been detached from the cell (and thus has a nil `prevp'), and there is + no next link representing another user reference to the same resource. */ + int dealloc = ! link->next && ! link->prevp; + + /* Remove our link from the chain of current users. */ + if (link->prevp) + *link->prevp = link->next; + if (link->next) + link->next->prevp = link->prevp; + + return dealloc; +} + + +/* Clear all users from *CHAINP. Call this when the resource *CHAINP + protects is changing. If the return value is nonzero, no users are on + the chain and the caller should deallocate the resource. If the return + value is zero, someone is still using the resource and they will + deallocate it when they are finished. */ + +_EXTERN_INLINE int +_hurd_userlink_clear (struct hurd_userlink **chainp) +{ + if (*chainp == NULL) + return 1; + + /* Detach the chain of current users from the cell. The last user to + remove his link from that chain will deallocate the old resource. */ + (*chainp)->prevp = NULL; + *chainp = NULL; + return 0; +} + +#endif /* hurd/userlink.h */ |