This is the mail archive of the libc-hacker@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: A patch for nanosleep


> 
> On Tue, 17 Nov 1998 09:29:20 -0800 (PST), H.J. Lu wrote:
> >
> >Here is an optimized nanosleep.c for glibc 2.
> 
> [...]
> 
> >      /* Should we really block SIGCHLD?  */
> >      ignored = (oact.sa_handler == SIG_IGN);
> >
> >      if (!ignored)
> >	/* Restore the original signal mask.  */
> >	(void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
> >
> >      ret = __syscall_nanosleep (req, rem);
> >
> >      if (ignored)
> 
> These 'if' conditions are backward.
> 

I don't think so. If SIGCHLD is ignored, we have to block SIGCHLD in
nanosleep. Otherwise, we have to restore the original signal mask.
Here is an update. I hope it is easier to understand.

-- 
H.J. Lu (hjl@gnu.org)
----
/* Copyright (C) 1998 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., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>

extern int __syscall_nanosleep (const struct timespec *req,
				struct timespec *rem);

int
__nanosleep(const struct timespec *req, struct timespec *rem)
{
  sigset_t set, oset;
  struct sigaction oact;
  int saved_errno;
  int ret;

  /* Linux will wake up the system call, nanosleep, when SIGCHLD
     arrives even if SIGCHLD is ignored. We have to deal with it
     in libc. We block SIGCHLD first.  */
  if (sigemptyset (&set) < 0 ||
      sigaddset (&set, SIGCHLD) < 0 ||
      sigprocmask (SIG_BLOCK, &set, &oset))
    return -1;

  /* If SIGCHLD is already blocked, we don't have to do anything.  */
  if (!sigismember (&oset, SIGCHLD))
    {
      /* We get the signal handler for SIGCHLD. */
      if (sigaction (SIGCHLD, (struct sigaction *) NULL, &oact) < 0)
	{
	  saved_errno = errno;
	  /* Restore the original signal mask.  */
	  (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
	  __set_errno (saved_errno);
	  return -1;
	}

      if (oact.sa_handler == SIG_IGN)
	{
	  /* We should leave SIGCHLD blocked.  */
	  ret = __syscall_nanosleep (req, rem);

	  saved_errno = errno;
	  /* Restore the original signal mask.  */
	  (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
	  __set_errno (saved_errno);
	}
      else
	{
	  /* We should unblock SIGCHLD. Restore the original signal
	     mask.  */
	  (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
	  ret = __syscall_nanosleep (req, rem);
	}
    }
  else
    ret = __syscall_nanosleep (req, rem);

  return ret;
}

weak_alias (__nanosleep, nanosleep)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]