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


> Hi,
> 
> I have forwarded a few emails regarding the nanosleep bug in glibc. I
> am enclosing a testcase and a patch. Basically, the Linux kernel
> doesn't really ignore SIGCHLD for nanosleep. We have to block it
> in glibc if it is ignored by the process.
> 
> BTW, I didn't change linuxthreads. I am not sure what it should do
> with the ignored SIGCHLD.
> 
> Thanks.
> 
> 
> -- 
> H.J. Lu (hjl@gnu.org)

Here is an optimized nanosleep.c for glibc 2.


H.J.
----
/* 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 ignored;
  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;
	}

      /* 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)
	{
	  saved_errno = errno;
	  /* Restore the original signal mask.  */
	  (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
	  __set_errno (saved_errno);
	}
    }
  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]