wait.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * wait definitions for NOLIBC
  4. * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
  5. */
  6. /* make sure to include all global symbols */
  7. #include "../nolibc.h"
  8. #ifndef _NOLIBC_SYS_WAIT_H
  9. #define _NOLIBC_SYS_WAIT_H
  10. #include "../arch.h"
  11. #include "../std.h"
  12. #include "../types.h"
  13. /*
  14. * pid_t wait(int *status);
  15. * pid_t waitpid(pid_t pid, int *status, int options);
  16. * int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
  17. */
  18. static __attribute__((unused))
  19. int sys_waitid(int which, pid_t pid, siginfo_t *infop, int options, struct rusage *rusage)
  20. {
  21. return my_syscall5(__NR_waitid, which, pid, infop, options, rusage);
  22. }
  23. static __attribute__((unused))
  24. int waitid(int which, pid_t pid, siginfo_t *infop, int options)
  25. {
  26. return __sysret(sys_waitid(which, pid, infop, options, NULL));
  27. }
  28. static __attribute__((unused))
  29. pid_t waitpid(pid_t pid, int *status, int options)
  30. {
  31. int idtype, ret;
  32. siginfo_t info;
  33. pid_t id;
  34. if (pid == INT_MIN) {
  35. SET_ERRNO(ESRCH);
  36. return -1;
  37. } else if (pid < -1) {
  38. idtype = P_PGID;
  39. id = -pid;
  40. } else if (pid == -1) {
  41. idtype = P_ALL;
  42. id = 0;
  43. } else if (pid == 0) {
  44. idtype = P_PGID;
  45. id = 0;
  46. } else {
  47. idtype = P_PID;
  48. id = pid;
  49. }
  50. options |= WEXITED;
  51. ret = waitid(idtype, id, &info, options);
  52. if (ret)
  53. return -1;
  54. switch (info.si_code) {
  55. case 0:
  56. if (status)
  57. *status = 0;
  58. break;
  59. case CLD_EXITED:
  60. if (status)
  61. *status = (info.si_status & 0xff) << 8;
  62. break;
  63. case CLD_KILLED:
  64. if (status)
  65. *status = info.si_status & 0x7f;
  66. break;
  67. case CLD_DUMPED:
  68. if (status)
  69. *status = (info.si_status & 0x7f) | 0x80;
  70. break;
  71. case CLD_STOPPED:
  72. case CLD_TRAPPED:
  73. if (status)
  74. *status = (info.si_status << 8) + 0x7f;
  75. break;
  76. case CLD_CONTINUED:
  77. if (status)
  78. *status = 0xffff;
  79. break;
  80. default:
  81. return -1;
  82. }
  83. return info.si_pid;
  84. }
  85. static __attribute__((unused))
  86. pid_t wait(int *status)
  87. {
  88. return waitpid(-1, status, 0);
  89. }
  90. #endif /* _NOLIBC_SYS_WAIT_H */