MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
safe_kill_win.cc
1 /* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 
17 /*
18  Utility program used to signal a safe_process it's time to shutdown
19 
20  Usage:
21  safe_kill <pid>
22 */
23 
24 #include <windows.h>
25 #include <stdio.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 
29 int main(int argc, const char** argv )
30 {
31  DWORD pid= -1;
32  HANDLE shutdown_event;
33  char safe_process_name[32]= {0};
34  int retry_open_event= 2;
35  /* Ignore any signals */
36  signal(SIGINT, SIG_IGN);
37  signal(SIGBREAK, SIG_IGN);
38  signal(SIGTERM, SIG_IGN);
39 
40  if (argc != 2) {
41  fprintf(stderr, "safe_kill <pid>\n");
42  exit(2);
43  }
44  pid= atoi(argv[1]);
45 
46  _snprintf(safe_process_name, sizeof(safe_process_name),
47  "safe_process[%d]", pid);
48 
49  /* Open the event to signal */
50  while ((shutdown_event=
51  OpenEvent(EVENT_MODIFY_STATE, FALSE, safe_process_name)) == NULL)
52  {
53  /*
54  Check if the process is alive, otherwise there is really
55  no sense to retry the open of the event
56  */
57  HANDLE process;
58  DWORD exit_code;
59  process= OpenProcess(SYNCHRONIZE| PROCESS_QUERY_INFORMATION, FALSE, pid);
60  if (!process)
61  {
62  /* Already died */
63  exit(1);
64  }
65 
66  if (!GetExitCodeProcess(process,&exit_code))
67  {
68  fprintf(stderr, "GetExitCodeProcess failed, pid= %d, err= %d\n",
69  pid, GetLastError());
70  exit(1);
71  }
72 
73  if (exit_code != STILL_ACTIVE)
74  {
75  /* Already died */
76  CloseHandle(process);
77  exit(2);
78  }
79 
80  CloseHandle(process);
81 
82  if (retry_open_event--)
83  Sleep(100);
84  else
85  {
86  fprintf(stderr, "Failed to open shutdown_event '%s', error: %d\n",
87  safe_process_name, GetLastError());
88  exit(3);
89  }
90  }
91 
92  if(SetEvent(shutdown_event) == 0)
93  {
94  fprintf(stderr, "Failed to signal shutdown_event '%s', error: %d\n",
95  safe_process_name, GetLastError());
96  CloseHandle(shutdown_event);
97  exit(4);
98  }
99  CloseHandle(shutdown_event);
100  exit(0);
101 }
102