Try to kill the process tree gracefully.
[nssm.git] / process.cpp
1 #include "nssm.h"
2
3 /* Send some window messages and hope the window respects one or more. */
4 int CALLBACK kill_window(HWND window, LPARAM arg) {
5   kill_t *k = (kill_t *) arg;
6
7   unsigned long pid;
8   if (! GetWindowThreadProcessId(window, &pid)) return 1;
9   if (pid != k->pid) return 1;
10
11   /* First try sending WM_CLOSE to request that the window close. */
12   k->signalled |= PostMessage(window, WM_CLOSE, k->exitcode, 0);
13
14   /*
15     Then tell the window that the user is logging off and it should exit
16     without worrying about saving any data.
17   */
18   k->signalled |= PostMessage(window, WM_ENDSESSION, 1, ENDSESSION_CLOSEAPP | ENDSESSION_CRITICAL | ENDSESSION_LOGOFF);
19
20   return 1;
21 }
22
23 /*
24   Try to post a message to the message queues of threads associated with the
25   given process ID.  Not all threads have message queues so there's no
26   guarantee of success, and we don't want to be left waiting for unsignalled
27   processes so this function returns only true if at least one thread was
28   successfully prodded.
29 */
30 int kill_threads(char *service_name, kill_t *k) {
31   int ret = 0;
32
33   /* Get a snapshot of all threads in the system. */
34   HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
35   if (! snapshot) {
36     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATETOOLHELP32SNAPSHOT_THREAD_FAILED, service_name, GetLastError(), 0);
37     return 0;
38   }
39
40   THREADENTRY32 te;
41   ZeroMemory(&te, sizeof(te));
42   te.dwSize = sizeof(te);
43
44   if (! Thread32First(snapshot, &te)) {
45     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_THREAD_ENUMERATE_FAILED, service_name, GetLastError(), 0);
46     return 0;
47   }
48
49   /* This thread belongs to the doomed process so signal it. */
50   if (te.th32OwnerProcessID == k->pid) {
51     ret |= PostThreadMessage(te.th32ThreadID, WM_QUIT, k->exitcode, 0);
52   }
53
54   while (true) {
55     /* Try to get the next thread. */
56     if (! Thread32Next(snapshot, &te)) {
57       unsigned long error = GetLastError();
58       if (error == ERROR_NO_MORE_FILES) break;
59       log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_THREAD_ENUMERATE_FAILED, service_name, GetLastError(), 0);
60       return ret;
61     }
62
63     if (te.th32OwnerProcessID == k->pid) {
64       ret |= PostThreadMessage(te.th32ThreadID, WM_QUIT, k->exitcode, 0);
65     }
66   }
67
68   return ret;
69 }
70
71 /* Give the process a chance to die gracefully. */
72 int kill_process(char *service_name, HANDLE process_handle, unsigned long pid, unsigned long exitcode) {
73   /* Shouldn't happen. */
74   if (! pid) return 1;
75
76   kill_t k = { pid, exitcode, 0 };
77
78   /*
79     Try to post messages to the windows belonging to the given process ID.
80     If the process is a console application it won't have any windows so there's
81     no guarantee of success.
82   */
83   EnumWindows((WNDENUMPROC) kill_window, (LPARAM) &k);
84   if (k.signalled) {
85     if (! WaitForSingleObject(process_handle, NSSM_KILL_WINDOW_GRACE_PERIOD)) return 1;
86   }
87
88   /*
89     Try to post messages to any thread message queues associated with the
90     process.  Console applications might have them (but probably won't) so
91     there's still no guarantee of success.
92   */
93   if (kill_threads(service_name, &k)) {
94     if (! WaitForSingleObject(process_handle, NSSM_KILL_THREADS_GRACE_PERIOD)) return 1;
95   }
96
97   /* We tried being nice.  Time for extreme prejudice. */
98   return TerminateProcess(process_handle, exitcode);
99 }
100
101 void kill_process_tree(char *service_name, unsigned long pid, unsigned long exitcode, unsigned long ppid) {
102   log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILLING, service_name, pid, exitcode, 0);
103
104   /* Shouldn't happen. */
105   if (! pid) return;
106
107   /* Get a snapshot of all processes in the system. */
108   HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
109   if (! snapshot) {
110     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATETOOLHELP32SNAPSHOT_PROCESS_FAILED, service_name, GetLastError(), 0);
111     return;
112   }
113
114   PROCESSENTRY32 pe;
115   ZeroMemory(&pe, sizeof(pe));
116   pe.dwSize = sizeof(pe);
117
118   if (! Process32First(snapshot, &pe)) {
119     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_PROCESS_ENUMERATE_FAILED, service_name, GetLastError(), 0);
120     return;
121   }
122
123   /* This is a child of the doomed process so kill it. */
124   if (pe.th32ParentProcessID == pid) kill_process_tree(service_name, pe.th32ProcessID, exitcode, ppid);
125
126   while (true) {
127     /* Try to get the next process. */
128     if (! Process32Next(snapshot, &pe)) {
129       unsigned long ret = GetLastError();
130       if (ret == ERROR_NO_MORE_FILES) break;
131       log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_PROCESS_ENUMERATE_FAILED, service_name, GetLastError(), 0);
132       return;
133     }
134
135     if (pe.th32ParentProcessID == pid) kill_process_tree(service_name, pe.th32ProcessID, exitcode, ppid);
136   }
137
138   /* We will need a process handle in order to call TerminateProcess() later. */
139   HANDLE process_handle = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE, false, pid);
140   if (! process_handle) {
141     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENPROCESS_FAILED, pid, service_name, GetLastError(), 0);
142     return;
143   }
144
145   log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILL_PROCESS_TREE, pid, ppid, service_name, 0);
146   if (! kill_process(service_name, process_handle, pid, exitcode)) {
147     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_TERMINATEPROCESS_FAILED, pid, service_name, GetLastError(), 0);
148     return;
149   }
150 }