waitid.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options)
  23. {
  24. # ifdef __NR_waitid
  25. return waitid5(idtype, id, infop, options, NULL);
  26. # else
  27. switch (idtype) {
  28. case P_PID:
  29. if (id <= 0)
  30. goto invalid;
  31. break;
  32. case P_PGID:
  33. if (id < 0 || id == 1)
  34. goto invalid;
  35. id = -id;
  36. break;
  37. case P_ALL:
  38. id = -1;
  39. break;
  40. default:
  41. invalid:
  42. __set_errno(EINVAL);
  43. return -1;
  44. }
  45. memset(infop, 0, sizeof *infop);
  46. infop->si_pid = waitpid(id, &infop->si_status, options
  47. # ifdef WEXITED
  48. &~ WEXITED
  49. # endif
  50. );
  51. if (infop->si_pid < 0)
  52. return infop->si_pid;
  53. return 0;
  54. # endif
  55. }
  56. #endif