+/* Simulate a Control-C event to our console (shared with the app). */
+int kill_console(char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVICE_STATUS *service_status, HANDLE process_handle, unsigned long pid) {
+ unsigned long ret;
+
+ /* Check we loaded AttachConsole(). */
+ if (! imports.AttachConsole) return 4;
+
+ /* Try to attach to the process's console. */
+ if (! imports.AttachConsole(pid)) {
+ ret = GetLastError();
+
+ switch (ret) {
+ case ERROR_INVALID_HANDLE:
+ /* The app doesn't have a console. */
+ return 1;
+
+ case ERROR_GEN_FAILURE:
+ /* The app already exited. */
+ return 2;
+
+ case ERROR_ACCESS_DENIED:
+ default:
+ /* We already have a console. */
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_ATTACHCONSOLE_FAILED, service_name, error_string(ret), 0);
+ return 3;
+ }
+ }
+
+ /* Ignore the event ourselves. */
+ ret = 0;
+ if (! SetConsoleCtrlHandler(0, TRUE)) {
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETCONSOLECTRLHANDLER_FAILED, service_name, error_string(GetLastError()), 0);
+ ret = 4;
+ }
+
+ /* Send the event. */
+ if (! ret) {
+ if (! GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)) {
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GENERATECONSOLECTRLEVENT_FAILED, service_name, error_string(GetLastError()), 0);
+ ret = 5;
+ }
+ }
+
+ /* Detach from the console. */
+ if (! FreeConsole()) {
+ log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_FREECONSOLE_FAILED, service_name, error_string(GetLastError()), 0);
+ }
+
+ /* Wait for process to exit. */
+ if (await_shutdown(__FUNCTION__, service_name, service_handle, service_status, process_handle, kill_console_delay)) ret = 6;
+
+ return ret;
+}
+
+void kill_process_tree(char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVICE_STATUS *service_status, unsigned long stop_method, unsigned long pid, unsigned long exitcode, unsigned long ppid, FILETIME *parent_creation_time, FILETIME *parent_exit_time) {