diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-10-10 14:52:47 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-10-10 14:52:47 +0000 |
commit | 2378353eab064ed06166e025a1df9d4a23efe6c4 (patch) | |
tree | f2418dbd6d50c4fe93da623e8b572fd725058450 /src/util/os_priority.c | |
parent | 474111bf7923be9458ee84c6dcc577261aa65fea (diff) | |
download | gnunet-2378353eab064ed06166e025a1df9d4a23efe6c4.tar.gz gnunet-2378353eab064ed06166e025a1df9d4a23efe6c4.zip |
extending process API to allow obtaining status code in blocking fashion
Diffstat (limited to 'src/util/os_priority.c')
-rw-r--r-- | src/util/os_priority.c | 66 |
1 files changed, 61 insertions, 5 deletions
diff --git a/src/util/os_priority.c b/src/util/os_priority.c index 9b1ec0963..4b1dbd491 100644 --- a/src/util/os_priority.c +++ b/src/util/os_priority.c | |||
@@ -1592,19 +1592,21 @@ GNUNET_OS_start_process_s (int pipe_control, | |||
1592 | * @param proc process ID | 1592 | * @param proc process ID |
1593 | * @param type status type | 1593 | * @param type status type |
1594 | * @param code return code/signal number | 1594 | * @param code return code/signal number |
1595 | * @param options WNOHANG if non-blocking is desired | ||
1595 | * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise | 1596 | * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise |
1596 | */ | 1597 | */ |
1597 | int | 1598 | static int |
1598 | GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, | 1599 | process_status (struct GNUNET_OS_Process *proc, |
1599 | enum GNUNET_OS_ProcessStatusType *type, | 1600 | enum GNUNET_OS_ProcessStatusType *type, |
1600 | unsigned long *code) | 1601 | unsigned long *code, |
1602 | int options) | ||
1601 | { | 1603 | { |
1602 | #ifndef MINGW | 1604 | #ifndef MINGW |
1603 | int status; | 1605 | int status; |
1604 | int ret; | 1606 | int ret; |
1605 | 1607 | ||
1606 | GNUNET_assert (0 != proc); | 1608 | GNUNET_assert (0 != proc); |
1607 | ret = waitpid (proc->pid, &status, WNOHANG); | 1609 | ret = waitpid (proc->pid, &status, options); |
1608 | if (ret < 0) | 1610 | if (ret < 0) |
1609 | { | 1611 | { |
1610 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, | 1612 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, |
@@ -1650,6 +1652,10 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, | |||
1650 | *code = 0; | 1652 | *code = 0; |
1651 | } | 1653 | } |
1652 | #else | 1654 | #else |
1655 | #ifndef WNOHANG | ||
1656 | #define WNOHANG 42 /* just a flag for W32, purely internal at this point */ | ||
1657 | #endif | ||
1658 | |||
1653 | HANDLE h; | 1659 | HANDLE h; |
1654 | DWORD c, error_code, ret; | 1660 | DWORD c, error_code, ret; |
1655 | 1661 | ||
@@ -1665,6 +1671,14 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, | |||
1665 | if (h == NULL) | 1671 | if (h == NULL) |
1666 | h = GetCurrentProcess (); | 1672 | h = GetCurrentProcess (); |
1667 | 1673 | ||
1674 | if (WNOHANG != options) | ||
1675 | { | ||
1676 | if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE)) | ||
1677 | { | ||
1678 | SetErrnoFromWinError (GetLastError ()); | ||
1679 | return GNUNET_SYSERR; | ||
1680 | } | ||
1681 | } | ||
1668 | SetLastError (0); | 1682 | SetLastError (0); |
1669 | ret = GetExitCodeProcess (h, &c); | 1683 | ret = GetExitCodeProcess (h, &c); |
1670 | error_code = GetLastError (); | 1684 | error_code = GetLastError (); |
@@ -1689,6 +1703,48 @@ GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, | |||
1689 | 1703 | ||
1690 | 1704 | ||
1691 | /** | 1705 | /** |
1706 | * Retrieve the status of a process, waiting on him if dead. | ||
1707 | * Nonblocking version. | ||
1708 | * | ||
1709 | * @param proc process ID | ||
1710 | * @param type status type | ||
1711 | * @param code return code/signal number | ||
1712 | * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise | ||
1713 | */ | ||
1714 | int | ||
1715 | GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, | ||
1716 | enum GNUNET_OS_ProcessStatusType *type, | ||
1717 | unsigned long *code) | ||
1718 | { | ||
1719 | return process_status (proc, | ||
1720 | type, | ||
1721 | code, | ||
1722 | WNOHANG); | ||
1723 | } | ||
1724 | |||
1725 | |||
1726 | /** | ||
1727 | * Retrieve the status of a process, waiting on him if dead. | ||
1728 | * Blocking version. | ||
1729 | * | ||
1730 | * @param proc pointer to process structure | ||
1731 | * @param type status type | ||
1732 | * @param code return code/signal number | ||
1733 | * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise | ||
1734 | */ | ||
1735 | int | ||
1736 | GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc, | ||
1737 | enum GNUNET_OS_ProcessStatusType *type, | ||
1738 | unsigned long *code) | ||
1739 | { | ||
1740 | return process_status (proc, | ||
1741 | type, | ||
1742 | code, | ||
1743 | 0); | ||
1744 | } | ||
1745 | |||
1746 | |||
1747 | /** | ||
1692 | * Wait for a process to terminate. The return code is discarded. | 1748 | * Wait for a process to terminate. The return code is discarded. |
1693 | * You must not use #GNUNET_OS_process_status() on the same process | 1749 | * You must not use #GNUNET_OS_process_status() on the same process |
1694 | * after calling this function! This function is blocking and should | 1750 | * after calling this function! This function is blocking and should |