~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysql-test/lib/My/SafeProcess/safe_kill_win.cc

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2004 MySQL AB
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
 
 
28
int main(int argc, const char** argv )
 
29
{
 
30
  DWORD pid= -1;
 
31
  HANDLE shutdown_event;
 
32
  char safe_process_name[32]= {0};
 
33
  int retry_open_event= 2;
 
34
  /* Ignore any signals */
 
35
  signal(SIGINT,   SIG_IGN);
 
36
  signal(SIGBREAK, SIG_IGN);
 
37
  signal(SIGTERM,  SIG_IGN);
 
38
 
 
39
  if (argc != 2) {
 
40
    fprintf(stderr, "safe_kill <pid>\n");
 
41
    exit(2);
 
42
  }
 
43
  pid= atoi(argv[1]);
 
44
 
 
45
  _snprintf(safe_process_name, sizeof(safe_process_name),
 
46
            "safe_process[%d]", pid);
 
47
 
 
48
  /* Open the event to signal */
 
49
  while ((shutdown_event=
 
50
          OpenEvent(EVENT_MODIFY_STATE, FALSE, safe_process_name)) == NULL)
 
51
  {
 
52
     /*
 
53
      Check if the process is alive, otherwise there is really
 
54
      no sense to retry the open of the event
 
55
     */
 
56
    HANDLE process;
 
57
    DWORD exit_code;
 
58
    process= OpenProcess(SYNCHRONIZE| PROCESS_QUERY_INFORMATION, FALSE, pid);
 
59
    if (!process)
 
60
    {
 
61
      /* Already died */
 
62
      exit(1);
 
63
    }
 
64
 
 
65
    if (!GetExitCodeProcess(process,&exit_code))
 
66
    {
 
67
       fprintf(stderr,  "GetExitCodeProcess failed, pid= %d, err= %d\n",
 
68
         pid, GetLastError());
 
69
       exit(1);
 
70
    }
 
71
 
 
72
    if (exit_code != STILL_ACTIVE)
 
73
    {
 
74
       /* Already died */
 
75
       CloseHandle(process);
 
76
       exit(2);
 
77
    }
 
78
 
 
79
    CloseHandle(process);
 
80
 
 
81
    if (retry_open_event--)
 
82
      Sleep(100);
 
83
    else
 
84
    {
 
85
      fprintf(stderr, "Failed to open shutdown_event '%s', error: %d\n",
 
86
              safe_process_name, GetLastError());
 
87
      exit(3);
 
88
    }
 
89
  }
 
90
 
 
91
  if(SetEvent(shutdown_event) == 0)
 
92
  {
 
93
    fprintf(stderr, "Failed to signal shutdown_event '%s', error: %d\n",
 
94
            safe_process_name, GetLastError());
 
95
    CloseHandle(shutdown_event);
 
96
    exit(4);
 
97
  }
 
98
  CloseHandle(shutdown_event);
 
99
  exit(0);
 
100
}
 
101