Free all resources used by the given kwsysProcess instance that were allocated by kwsysProcess_Execute. */
| 1587 | /* Free all resources used by the given kwsysProcess instance that were |
| 1588 | allocated by kwsysProcess_Execute. */ |
| 1589 | static void kwsysProcessCleanup(kwsysProcess* cp, int error) |
| 1590 | { |
| 1591 | int i; |
| 1592 | |
| 1593 | if (error) { |
| 1594 | /* We are cleaning up due to an error. Report the error message |
| 1595 | if one has not been provided already. */ |
| 1596 | if (cp->ErrorMessage[0] == 0) { |
| 1597 | strncpy(cp->ErrorMessage, strerror(errno), KWSYSPE_PIPE_BUFFER_SIZE); |
| 1598 | } |
| 1599 | |
| 1600 | /* Set the error state. */ |
| 1601 | cp->State = kwsysProcess_State_Error; |
| 1602 | |
| 1603 | /* Kill any children already started. */ |
| 1604 | if (cp->ForkPIDs) { |
| 1605 | int status; |
| 1606 | for (i = 0; i < cp->NumberOfCommands; ++i) { |
| 1607 | if (cp->ForkPIDs[i]) { |
| 1608 | /* Kill the child. */ |
| 1609 | kwsysProcessKill(cp->ForkPIDs[i]); |
| 1610 | |
| 1611 | /* Reap the child. Keep trying until the call is not |
| 1612 | interrupted. */ |
| 1613 | while ((waitpid(cp->ForkPIDs[i], &status, 0) < 0) && |
| 1614 | (errno == EINTR)) { |
| 1615 | } |
| 1616 | } |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | /* Restore the working directory. */ |
| 1621 | if (cp->RealWorkingDirectory) { |
| 1622 | while ((chdir(cp->RealWorkingDirectory) < 0) && (errno == EINTR)) { |
| 1623 | } |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | /* If not creating a detached child, remove this object from the |
| 1628 | global set of process objects that wish to be notified when a |
| 1629 | child exits. */ |
| 1630 | if (!cp->OptionDetach) { |
| 1631 | kwsysProcessesRemove(cp); |
| 1632 | } |
| 1633 | |
| 1634 | /* Free memory. */ |
| 1635 | if (cp->ForkPIDs) { |
| 1636 | kwsysProcessVolatileFree(cp->ForkPIDs); |
| 1637 | cp->ForkPIDs = 0; |
| 1638 | } |
| 1639 | if (cp->RealWorkingDirectory) { |
| 1640 | free(cp->RealWorkingDirectory); |
| 1641 | cp->RealWorkingDirectory = 0; |
| 1642 | } |
| 1643 | |
| 1644 | /* Close pipe handles. */ |
| 1645 | for (i = 0; i < KWSYSPE_PIPE_COUNT; ++i) { |
| 1646 | kwsysProcessCleanupDescriptor(&cp->PipeReadEnds[i]); |
no test coverage detected
searching dependent graphs…