diff options
-rw-r--r-- | win32/process.c | 53 |
1 files changed, 27 insertions, 26 deletions
diff --git a/win32/process.c b/win32/process.c index bf98ef746..caea87492 100644 --- a/win32/process.c +++ b/win32/process.c | |||
@@ -812,10 +812,18 @@ static int kill_signal(pid_t pid, int sig) | |||
812 | * indicates the current process; negative indicates the process with | 812 | * indicates the current process; negative indicates the process with |
813 | * process ID -pid. | 813 | * process ID -pid. |
814 | */ | 814 | */ |
815 | static int kill_pids(pid_t pid, int sig) | 815 | int kill(pid_t pid, int sig) |
816 | { | 816 | { |
817 | DWORD pids[16384]; | 817 | DWORD *pids; |
818 | int max_len = sizeof(pids) / sizeof(*pids), i, len, ret = 0; | 818 | int max_len, i, len, ret = 0; |
819 | |||
820 | if (!is_valid_signal(sig)) { | ||
821 | errno = EINVAL; | ||
822 | return -1; | ||
823 | } | ||
824 | |||
825 | max_len = NPIDS; | ||
826 | pids = xmalloc(sizeof(*pids) * max_len); | ||
819 | 827 | ||
820 | if(pid > 0) | 828 | if(pid > 0) |
821 | pids[0] = (DWORD)pid; | 829 | pids[0] = (DWORD)pid; |
@@ -836,33 +844,34 @@ static int kill_pids(pid_t pid, int sig) | |||
836 | */ | 844 | */ |
837 | if (pid <= 0) { | 845 | if (pid <= 0) { |
838 | HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | 846 | HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
847 | PROCESSENTRY32 entry; | ||
848 | int pid_added; | ||
839 | 849 | ||
840 | if (snapshot == INVALID_HANDLE_VALUE) { | 850 | if (snapshot == INVALID_HANDLE_VALUE) { |
841 | errno = err_win_to_posix(); | 851 | errno = err_win_to_posix(); |
852 | free(pids); | ||
842 | return -1; | 853 | return -1; |
843 | } | 854 | } |
844 | 855 | ||
845 | for (;;) { | 856 | entry.dwSize = sizeof(entry); |
846 | PROCESSENTRY32 entry; | 857 | pid_added = TRUE; |
847 | int orig_len = len; | 858 | while (pid_added && Process32First(snapshot, &entry)) { |
848 | 859 | pid_added = FALSE; | |
849 | memset(&entry, 0, sizeof(entry)); | ||
850 | entry.dwSize = sizeof(entry); | ||
851 | |||
852 | if (!Process32First(snapshot, &entry)) | ||
853 | break; | ||
854 | 860 | ||
855 | do { | 861 | do { |
856 | for (i = len - 1; i >= 0; i--) { | 862 | for (i = len - 1; i >= 0; i--) { |
857 | if (pids[i] == entry.th32ProcessID) | 863 | if (pids[i] == entry.th32ProcessID) |
858 | break; | 864 | break; |
859 | if (pids[i] == entry.th32ParentProcessID) | 865 | if (pids[i] == entry.th32ParentProcessID) { |
866 | if (len == max_len) { | ||
867 | max_len += NPIDS; | ||
868 | pids = xrealloc(pids, sizeof(*pids) * max_len); | ||
869 | } | ||
860 | pids[len++] = entry.th32ProcessID; | 870 | pids[len++] = entry.th32ProcessID; |
871 | pid_added = TRUE; | ||
872 | } | ||
861 | } | 873 | } |
862 | } while (len < max_len && Process32Next(snapshot, &entry)); | 874 | } while (Process32Next(snapshot, &entry)); |
863 | |||
864 | if (orig_len == len || len >= max_len) | ||
865 | break; | ||
866 | } | 875 | } |
867 | 876 | ||
868 | CloseHandle(snapshot); | 877 | CloseHandle(snapshot); |
@@ -875,6 +884,7 @@ static int kill_pids(pid_t pid, int sig) | |||
875 | ret = -1; | 884 | ret = -1; |
876 | } | 885 | } |
877 | } | 886 | } |
887 | free(pids); | ||
878 | 888 | ||
879 | return ret; | 889 | return ret; |
880 | } | 890 | } |
@@ -916,12 +926,3 @@ int exit_code_to_posix(DWORD exit_code) | |||
916 | return 128 + WTERMSIG(status); | 926 | return 128 + WTERMSIG(status); |
917 | return WEXITSTATUS(status); | 927 | return WEXITSTATUS(status); |
918 | } | 928 | } |
919 | |||
920 | int kill(pid_t pid, int sig) | ||
921 | { | ||
922 | if (is_valid_signal(sig)) | ||
923 | return kill_pids(pid, sig); | ||
924 | |||
925 | errno = EINVAL; | ||
926 | return -1; | ||
927 | } | ||