Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.0 KiB

  1. /***
  2. *wait.c - wait for child process to terminate
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _wait() - wait for child process to terminate
  8. *
  9. *Revision History:
  10. * 06-08-89 PHG Module created, based on asm version
  11. * 03-08-90 GJF Made calling type _CALLTYPE2 (for now), added #include
  12. * <cruntime.h> and fixed the copyright. Also, cleaned up
  13. * the formatting a bit.
  14. * 04-02-90 GJF Now _CALLTYPE1.
  15. * 07-24-90 SBM Removed '32' from API names
  16. * 09-27-90 GJF New-style function declarators.
  17. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  18. * 12-06-90 SRW Added _CRUISER_ and _WIN32 conditionals.
  19. * 01-17-91 GJF ANSI naming
  20. * 02-18-91 SRW Fixed code to close process handle. [_WIN32_]
  21. * 04-26-91 SRW Removed level 3 warnings [_WIN32_]
  22. * 12-17-91 GJF Fixed _cwait for Win32. However, _wait is still
  23. * broken [_WIN32_].
  24. * 07-21-92 GJF Removed _wait for Win32, not implemented and no good
  25. * way to implement.
  26. * 12-14-92 GJF For Win32, map ERROR_INVALID_HANDLE to ECHILD.
  27. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  28. * 09-06-94 CFW Remove Cruiser support.
  29. * 02-06-98 GJF Changes for Win64: changed return and an arg type to
  30. * intptr_t.
  31. *
  32. *******************************************************************************/
  33. #include <cruntime.h>
  34. #include <oscalls.h>
  35. #include <process.h>
  36. #include <errno.h>
  37. #include <internal.h>
  38. #include <stdlib.h>
  39. /***
  40. *int _cwait(stat_loc, process_id, action_code) - wait for specific child
  41. * process
  42. *
  43. *Purpose:
  44. * The function _cwait() suspends the calling-process until the specified
  45. * child-process terminates. If the specifed child-process terminated
  46. * prior to the call to _cwait(), return is immediate.
  47. *
  48. *Entry:
  49. * int *stat_loc - pointer to where status is stored or NULL
  50. * process_id - specific process id to be interrogated (0 means any)
  51. * action_code - specific action to perform on process ID
  52. * either _WAIT_CHILD or _WAIT_GRANDCHILD
  53. *
  54. *Exit:
  55. * process ID of terminated child or -1 on error
  56. *
  57. * *stat_loc is updated to contain the following:
  58. * Normal termination: lo-byte = 0, hi-byte = child exit code
  59. * Abnormal termination: lo-byte = term status, hi-byte = 0
  60. *
  61. *Exceptions:
  62. *
  63. *******************************************************************************/
  64. intptr_t __cdecl _cwait (
  65. int *stat_loc,
  66. intptr_t process_id,
  67. int action_code
  68. )
  69. {
  70. intptr_t retval;
  71. int retstatus;
  72. unsigned long oserror;
  73. DBG_UNREFERENCED_PARAMETER(action_code);
  74. /* Explicitly check for process_id being -1 or -2. In Windows NT,
  75. * -1 is a handle on the current process, -2 is a handle on the
  76. * current thread, and it is perfectly legal to to wait (forever)
  77. * on either */
  78. if ( (process_id == -1) || (process_id == -2) ) {
  79. errno = ECHILD;
  80. return -1;
  81. }
  82. /* wait for child process, then fetch its exit code */
  83. if ( (WaitForSingleObject((HANDLE)process_id, (DWORD)(-1L)) == 0) &&
  84. GetExitCodeProcess((HANDLE)process_id, (LPDWORD)&retstatus) ) {
  85. retval = process_id;
  86. }
  87. else {
  88. /* one of the API calls failed. map the error and set up to
  89. return failure. note the invalid handle error is mapped in-
  90. line to ECHILD */
  91. if ( (oserror = GetLastError()) == ERROR_INVALID_HANDLE ) {
  92. errno = ECHILD;
  93. _doserrno = oserror;
  94. }
  95. else
  96. _dosmaperr(GetLastError());
  97. retval = -1;
  98. retstatus = -1;
  99. }
  100. CloseHandle((HANDLE)process_id);
  101. if (stat_loc != NULL)
  102. *stat_loc = retstatus;
  103. return retval;
  104. }