waitid.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2007 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include <features.h>
  8. #if defined __USE_SVID || defined __USE_XOPEN
  9. # include <string.h>
  10. # include <sys/types.h>
  11. # include <sys/wait.h>
  12. # include <sys/syscall.h>
  13. # ifdef __NR_waitid
  14. /* The waitid() POSIX interface takes 4 arguments, but the kernel function
  15. * actually takes 5. The fifth is a pointer to struct rusage. Make sure
  16. * we pass NULL rather than letting whatever was in the register bleed up.
  17. */
  18. #define __NR_waitid5 __NR_waitid
  19. static _syscall5(int, waitid5, idtype_t, idtype, id_t, id, siginfo_t*, infop,
  20. int, options, struct rusage*, ru)
  21. # endif
  22. /* libc_hidden_proto(waitpid) */
  23. int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options)
  24. {
  25. # ifdef __NR_waitid
  26. return waitid5(idtype, id, infop, options, NULL);
  27. # else
  28. switch (idtype) {
  29. case P_PID:
  30. if (id <= 0)
  31. goto invalid;
  32. break;
  33. case P_PGID:
  34. if (id < 0 || id == 1)
  35. goto invalid;
  36. id = -id;
  37. break;
  38. case P_ALL:
  39. id = -1;
  40. break;
  41. default:
  42. invalid:
  43. __set_errno(EINVAL);
  44. return -1;
  45. }
  46. memset(infop, 0, sizeof *infop);
  47. infop->si_pid = waitpid(id, &infop->si_status, options
  48. # ifdef WEXITED
  49. &~ WEXITED
  50. # endif
  51. );
  52. if (infop->si_pid < 0)
  53. return infop->si_pid;
  54. return 0;
  55. # endif
  56. }
  57. #endif