Back to index

glibc  2.9
sigwait.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997,1998,2000,2002-2004,2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <signal.h>
00021 #define __need_NULL
00022 #include <stddef.h>
00023 #include <string.h>
00024 
00025 #include <sysdep-cancel.h>
00026 #include <sys/syscall.h>
00027 #include <bp-checks.h>
00028 
00029 #ifdef __NR_rt_sigtimedwait
00030 
00031 /* Return any pending signal or wait for one for the given time.  */
00032 static int
00033 do_sigwait (const sigset_t *set, int *sig)
00034 {
00035   int ret;
00036 
00037 #ifdef SIGCANCEL
00038   sigset_t tmpset;
00039   if (set != NULL
00040       && (__builtin_expect (__sigismember (set, SIGCANCEL), 0)
00041 # ifdef SIGSETXID
00042          || __builtin_expect (__sigismember (set, SIGSETXID), 0)
00043 # endif
00044          ))
00045     {
00046       /* Create a temporary mask without the bit for SIGCANCEL set.  */
00047       // We are not copying more than we have to.
00048       memcpy (&tmpset, set, _NSIG / 8);
00049       __sigdelset (&tmpset, SIGCANCEL);
00050 # ifdef SIGSETXID
00051       __sigdelset (&tmpset, SIGSETXID);
00052 # endif
00053       set = &tmpset;
00054     }
00055 #endif
00056 
00057   /* XXX The size argument hopefully will have to be changed to the
00058      real size of the user-level sigset_t.  */
00059 #ifdef INTERNAL_SYSCALL
00060   INTERNAL_SYSCALL_DECL (err);
00061   do
00062     ret = INTERNAL_SYSCALL (rt_sigtimedwait, err, 4, CHECK_SIGSET (set),
00063                          NULL, NULL, _NSIG / 8);
00064   while (INTERNAL_SYSCALL_ERROR_P (ret, err)
00065         && INTERNAL_SYSCALL_ERRNO (ret, err) == EINTR);
00066   if (! INTERNAL_SYSCALL_ERROR_P (ret, err))
00067     {
00068       *sig = ret;
00069       ret = 0;
00070     }
00071   else
00072     ret = INTERNAL_SYSCALL_ERRNO (ret, err);
00073 #else
00074   do
00075     ret = INLINE_SYSCALL (rt_sigtimedwait, 4, CHECK_SIGSET (set),
00076                        NULL, NULL, _NSIG / 8);
00077   while (ret == -1 && errno == EINTR);
00078   if (ret != -1)
00079     {
00080       *sig = ret;
00081       ret = 0;
00082     }
00083   else
00084     ret = errno;
00085 #endif
00086 
00087   return ret;
00088 }
00089 
00090 int
00091 __sigwait (set, sig)
00092      const sigset_t *set;
00093      int *sig;
00094 {
00095   if (SINGLE_THREAD_P)
00096     return do_sigwait (set, sig);
00097 
00098   int oldtype = LIBC_CANCEL_ASYNC ();
00099 
00100   int result = do_sigwait (set, sig);
00101 
00102   LIBC_CANCEL_RESET (oldtype);
00103 
00104   return result;
00105 }
00106 libc_hidden_def (__sigwait)
00107 weak_alias (__sigwait, sigwait)
00108 #else
00109 # include <sysdeps/posix/sigwait.c>
00110 #endif
00111 strong_alias (__sigwait, __libc_sigwait)