Add new process to global process list. The Ctrl handler will wait for the process to exit before it returns. Do not close the process handle until after calling kwsysProcessesRemove. The newProcessGroup parameter must be set if the process was created with CREATE_NEW_PROCESS_GROUP. */
| 2649 | until after calling kwsysProcessesRemove. The newProcessGroup parameter |
| 2650 | must be set if the process was created with CREATE_NEW_PROCESS_GROUP. */ |
| 2651 | static int kwsysProcessesAdd(HANDLE hProcess, DWORD dwProcessid, |
| 2652 | int newProcessGroup) |
| 2653 | { |
| 2654 | if (!kwsysProcessesInitialize() || !hProcess || |
| 2655 | hProcess == INVALID_HANDLE_VALUE) { |
| 2656 | return 0; |
| 2657 | } |
| 2658 | |
| 2659 | /* Enter the critical section. */ |
| 2660 | EnterCriticalSection(&kwsysProcesses.Lock); |
| 2661 | |
| 2662 | /* Make sure there is enough space for the new process handle. */ |
| 2663 | if (kwsysProcesses.Count == kwsysProcesses.Size) { |
| 2664 | size_t newSize; |
| 2665 | kwsysProcessInstance* newArray; |
| 2666 | /* Start with enough space for a small number of process handles |
| 2667 | and double the size each time more is needed. */ |
| 2668 | newSize = kwsysProcesses.Size ? kwsysProcesses.Size * 2 : 4; |
| 2669 | |
| 2670 | /* Try allocating the new block of memory. */ |
| 2671 | if ((newArray = (kwsysProcessInstance*)malloc( |
| 2672 | newSize * sizeof(kwsysProcessInstance)))) { |
| 2673 | /* Copy the old process handles to the new memory. */ |
| 2674 | if (kwsysProcesses.Count > 0) { |
| 2675 | memcpy(newArray, kwsysProcesses.Processes, |
| 2676 | kwsysProcesses.Count * sizeof(kwsysProcessInstance)); |
| 2677 | } |
| 2678 | } else { |
| 2679 | /* Failed to allocate memory for the new process handle set. */ |
| 2680 | LeaveCriticalSection(&kwsysProcesses.Lock); |
| 2681 | return 0; |
| 2682 | } |
| 2683 | |
| 2684 | /* Free original array. */ |
| 2685 | free(kwsysProcesses.Processes); |
| 2686 | |
| 2687 | /* Update original structure with new allocation. */ |
| 2688 | kwsysProcesses.Size = newSize; |
| 2689 | kwsysProcesses.Processes = newArray; |
| 2690 | } |
| 2691 | |
| 2692 | /* Append the new process information to the set. */ |
| 2693 | kwsysProcesses.Processes[kwsysProcesses.Count].hProcess = hProcess; |
| 2694 | kwsysProcesses.Processes[kwsysProcesses.Count].dwProcessId = dwProcessid; |
| 2695 | kwsysProcesses.Processes[kwsysProcesses.Count++].NewProcessGroup = |
| 2696 | newProcessGroup; |
| 2697 | |
| 2698 | /* Leave critical section and return success. */ |
| 2699 | LeaveCriticalSection(&kwsysProcesses.Lock); |
| 2700 | |
| 2701 | return 1; |
| 2702 | } |
| 2703 | |
| 2704 | /* Removes process to global process list. */ |
| 2705 | static void kwsysProcessesRemove(HANDLE hProcess) |
no test coverage detected
searching dependent graphs…