| 2677 | } |
| 2678 | |
| 2679 | static int kwsysProcessesAdd(kwsysProcess* cp) |
| 2680 | { |
| 2681 | /* Create a pipe through which the signal handler can notify the |
| 2682 | given process object that a child has exited. */ |
| 2683 | { |
| 2684 | /* Create the pipe. */ |
| 2685 | int p[2]; |
| 2686 | if (pipe(p KWSYSPE_VMS_NONBLOCK) < 0) { |
| 2687 | return 0; |
| 2688 | } |
| 2689 | |
| 2690 | /* Store the pipes now to be sure they are cleaned up later. */ |
| 2691 | cp->PipeReadEnds[KWSYSPE_PIPE_SIGNAL] = p[0]; |
| 2692 | cp->SignalPipe = p[1]; |
| 2693 | |
| 2694 | /* Switch the pipe to non-blocking mode so that reading a byte can |
| 2695 | be an atomic test-and-set. */ |
| 2696 | if (!kwsysProcessSetNonBlocking(p[0]) || |
| 2697 | !kwsysProcessSetNonBlocking(p[1])) { |
| 2698 | return 0; |
| 2699 | } |
| 2700 | |
| 2701 | /* The children do not need this pipe. Set close-on-exec flag on |
| 2702 | the pipe's ends. */ |
| 2703 | if ((fcntl(p[0], F_SETFD, FD_CLOEXEC) < 0) || |
| 2704 | (fcntl(p[1], F_SETFD, FD_CLOEXEC) < 0)) { |
| 2705 | return 0; |
| 2706 | } |
| 2707 | } |
| 2708 | |
| 2709 | /* Attempt to add the given signal pipe to the signal handler set. */ |
| 2710 | { |
| 2711 | |
| 2712 | /* Make sure there is enough space for the new signal pipe. */ |
| 2713 | kwsysProcessInstances oldProcesses = kwsysProcesses; |
| 2714 | kwsysProcessInstances newProcesses = oldProcesses; |
| 2715 | if (oldProcesses.Count == oldProcesses.Size) { |
| 2716 | /* Start with enough space for a small number of process instances |
| 2717 | and double the size each time more is needed. */ |
| 2718 | newProcesses.Size = oldProcesses.Size ? oldProcesses.Size * 2 : 4; |
| 2719 | |
| 2720 | /* Try allocating the new block of memory. */ |
| 2721 | if ((newProcesses.Processes = ((kwsysProcess**)malloc( |
| 2722 | (size_t)(newProcesses.Size) * sizeof(kwsysProcess*))))) { |
| 2723 | /* Copy the old pipe set to the new memory. */ |
| 2724 | if (oldProcesses.Count > 0) { |
| 2725 | memcpy(newProcesses.Processes, oldProcesses.Processes, |
| 2726 | ((size_t)(oldProcesses.Count) * sizeof(kwsysProcess*))); |
| 2727 | } |
| 2728 | } else { |
| 2729 | /* Failed to allocate memory for the new signal pipe set. */ |
| 2730 | return 0; |
| 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | /* Append the new signal pipe to the set. */ |
| 2735 | newProcesses.Processes[newProcesses.Count++] = cp; |
| 2736 |
no test coverage detected
searching dependent graphs…