aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/timeout.c2
-rw-r--r--include/mingw.h2
-rw-r--r--win32/process.c48
3 files changed, 19 insertions, 33 deletions
diff --git a/coreutils/timeout.c b/coreutils/timeout.c
index 1a9660183..06c134a37 100644
--- a/coreutils/timeout.c
+++ b/coreutils/timeout.c
@@ -52,7 +52,7 @@ HANDLE child = INVALID_HANDLE_VALUE;
52static void kill_child(void) 52static void kill_child(void)
53{ 53{
54 if (child != INVALID_HANDLE_VALUE) { 54 if (child != INVALID_HANDLE_VALUE) {
55 kill_SIGTERM_by_handle(child, 128+SIGTERM); 55 kill_SIGTERM_by_handle(child);
56 } 56 }
57} 57}
58#endif 58#endif
diff --git a/include/mingw.h b/include/mingw.h
index c75b06330..a67b161c7 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -502,7 +502,7 @@ int mingw_execve(const char *cmd, char *const *argv, char *const *envp);
502#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':') 502#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
503#define is_absolute_path(path) ((path)[0] == '/' || (path)[0] == '\\' || has_dos_drive_prefix(path)) 503#define is_absolute_path(path) ((path)[0] == '/' || (path)[0] == '\\' || has_dos_drive_prefix(path))
504 504
505int kill_SIGTERM_by_handle(HANDLE process, int exit_code); 505int kill_SIGTERM_by_handle(HANDLE process);
506 506
507#define find_mount_point(n, s) find_mount_point(n) 507#define find_mount_point(n, s) find_mount_point(n)
508 508
diff --git a/win32/process.c b/win32/process.c
index 6224ce362..ac63a9c58 100644
--- a/win32/process.c
+++ b/win32/process.c
@@ -624,9 +624,9 @@ void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
624 * of the indicated process. Zero indicates the current process; negative 624 * of the indicated process. Zero indicates the current process; negative
625 * indicates the process with process ID -pid. 625 * indicates the process with process ID -pid.
626 */ 626 */
627typedef int (*kill_callback)(pid_t pid, int exit_code); 627typedef int (*kill_callback)(pid_t pid, int sig);
628 628
629static int kill_pids(pid_t pid, int exit_code, kill_callback killer) 629static int kill_pids(pid_t pid, int sig, kill_callback killer)
630{ 630{
631 DWORD pids[16384]; 631 DWORD pids[16384];
632 int max_len = sizeof(pids) / sizeof(*pids), i, len, ret = 0; 632 int max_len = sizeof(pids) / sizeof(*pids), i, len, ret = 0;
@@ -684,7 +684,7 @@ static int kill_pids(pid_t pid, int exit_code, kill_callback killer)
684 684
685 for (i = len - 1; i >= 0; i--) { 685 for (i = len - 1; i >= 0; i--) {
686 SetLastError(0); 686 SetLastError(0);
687 if (killer(pids[i], exit_code)) { 687 if (killer(pids[i], sig)) {
688 errno = err_win_to_posix(); 688 errno = err_win_to_posix();
689 ret = -1; 689 ret = -1;
690 } 690 }
@@ -725,14 +725,14 @@ static inline int process_architecture_matches_current(HANDLE process)
725 * http://www.drdobbs.com/a-safer-alternative-to-terminateprocess/184416547 725 * http://www.drdobbs.com/a-safer-alternative-to-terminateprocess/184416547
726 * 726 *
727 */ 727 */
728int kill_SIGTERM_by_handle(HANDLE process, int exit_code) 728int kill_SIGTERM_by_handle(HANDLE process)
729{ 729{
730 DWORD code; 730 DWORD code;
731 int ret = 0; 731 int ret = 0;
732 732
733 if (GetExitCodeProcess(process, &code) && code == STILL_ACTIVE) { 733 if (GetExitCodeProcess(process, &code) && code == STILL_ACTIVE) {
734 DECLARE_PROC_ADDR(DWORD, ExitProcess, LPVOID); 734 DECLARE_PROC_ADDR(DWORD, ExitProcess, LPVOID);
735 PVOID arg = (PVOID)(intptr_t)exit_code; 735 PVOID arg = (PVOID)(intptr_t)(128 + SIGTERM);
736 DWORD thread_id; 736 DWORD thread_id;
737 HANDLE thread; 737 HANDLE thread;
738 738
@@ -754,7 +754,7 @@ int kill_SIGTERM_by_handle(HANDLE process, int exit_code)
754 return ret; 754 return ret;
755} 755}
756 756
757static int kill_SIGTERM(pid_t pid, int exit_code) 757static int kill_SIGTERM(pid_t pid, int sig UNUSED_PARAM)
758{ 758{
759 HANDLE process; 759 HANDLE process;
760 760
@@ -765,52 +765,38 @@ static int kill_SIGTERM(pid_t pid, int exit_code)
765 return -1; 765 return -1;
766 } 766 }
767 767
768 return kill_SIGTERM_by_handle(process, exit_code); 768 return kill_SIGTERM_by_handle(process);
769} 769}
770 770
771/* 771/*
772 * This way of terminating processes is not gentle: they get no chance to 772 * This way of terminating processes is not gentle: they get no chance to
773 * clean up after themselves (closing file handles, removing .lock files, 773 * clean up after themselves (closing file handles, removing .lock files,
774 * terminating spawned processes (if any), etc). 774 * terminating spawned processes (if any), etc).
775 *
776 * If the signal isn't SIGKILL just check if the target process exists.
775 */ 777 */
776static int kill_SIGKILL(pid_t pid, int exit_code) 778static int kill_SIGKILL(pid_t pid, int sig)
777{ 779{
778 HANDLE process; 780 HANDLE process;
779 int ret; 781 int ret = 0;
780 782
781 if (!(process=OpenProcess(PROCESS_TERMINATE, FALSE, pid))) { 783 if (!(process=OpenProcess(PROCESS_TERMINATE, FALSE, pid))) {
782 return -1; 784 return -1;
783 } 785 }
784 786
785 ret = !TerminateProcess(process, exit_code); 787 if (sig == SIGKILL)
788 ret = !TerminateProcess(process, 128 + SIGKILL);
786 CloseHandle(process); 789 CloseHandle(process);
787 790
788 return ret; 791 return ret;
789} 792}
790 793
791static int kill_SIGTEST(pid_t pid, int exit_code UNUSED_PARAM)
792{
793 HANDLE process;
794
795 if (!(process=OpenProcess(PROCESS_TERMINATE, FALSE, pid))) {
796 return -1;
797 }
798
799 CloseHandle(process);
800 return 0;
801}
802
803int kill(pid_t pid, int sig) 794int kill(pid_t pid, int sig)
804{ 795{
805 if (sig == SIGTERM) { 796 if (sig == SIGTERM)
806 return kill_pids(pid, 128+sig, kill_SIGTERM); 797 return kill_pids(pid, sig, kill_SIGTERM);
807 } 798 else if (sig == SIGKILL || sig == 0)
808 else if (sig == SIGKILL) { 799 return kill_pids(pid, sig, kill_SIGKILL);
809 return kill_pids(pid, 128+sig, kill_SIGKILL);
810 }
811 else if (sig == 0) {
812 return kill_pids(pid, 128+sig, kill_SIGTEST);
813 }
814 800
815 errno = EINVAL; 801 errno = EINVAL;
816 return -1; 802 return -1;