List:Commits« Previous MessageNext Message »
From:msvensson Date:December 6 2007 3:27pm
Subject:bk commit into 5.1 tree (msvensson:1.2552)
View as plain text  
Below is the list of changes that have just been committed into a local
5.1 repository of msvensson. When msvensson does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet@stripped, 2007-12-06 15:27:01+01:00, msvensson@stripped +4 -0
  Move safe_process*.cc to mysql-test/lib/My/SafeProcess/

  extra/CMakeLists.txt@stripped, 2007-12-06 15:26:59+01:00, msvensson@stripped +0 -3
    Move safe_process*.cc to mysql-test/lib/My/SafeProcess/

  extra/Makefile.am@stripped, 2007-12-06 15:26:59+01:00, msvensson@stripped +1 -1
    Move safe_process*.cc to mysql-test/lib/My/SafeProcess/

  mysql-test/lib/My/SafeProcess/safe_kill_win.cc@stripped, 2007-12-06 15:25:47+01:00,
msvensson@stripped +0 -0
    Rename: extra/safe_kill_win.cc -> mysql-test/lib/My/SafeProcess/safe_kill_win.cc

  mysql-test/lib/My/SafeProcess/safe_process_win.cc@stripped, 2007-12-06 15:25:42+01:00,
msvensson@stripped +0 -0
    Rename: extra/safe_process_win.cc ->
mysql-test/lib/My/SafeProcess/safe_process_win.cc

diff -Nrup a/extra/CMakeLists.txt b/extra/CMakeLists.txt
--- a/extra/CMakeLists.txt	2007-11-08 14:04:45 +01:00
+++ b/extra/CMakeLists.txt	2007-12-06 15:26:59 +01:00
@@ -47,9 +47,6 @@ TARGET_LINK_LIBRARIES(perror strings mys
 ADD_EXECUTABLE(replace replace.c)
 TARGET_LINK_LIBRARIES(replace strings mysys debug dbug wsock32)
 
-ADD_EXECUTABLE(my_safe_process safe_process_win.cc)
-ADD_EXECUTABLE(my_safe_kill safe_kill_win.cc)
-
 IF(EMBED_MANIFESTS)
   MYSQL_EMBED_MANIFEST("myTest" "asInvoker")
 ENDIF(EMBED_MANIFESTS)
diff -Nrup a/extra/Makefile.am b/extra/Makefile.am
--- a/extra/Makefile.am	2007-11-08 14:04:45 +01:00
+++ b/extra/Makefile.am	2007-12-06 15:26:59 +01:00
@@ -46,7 +46,7 @@ bin_PROGRAMS =		replace perror resolveip
 			resolve_stack_dump mysql_waitpid innochecksum
 noinst_PROGRAMS =	charset2html
 EXTRA_PROGRAMS =	comp_err
-EXTRA_DIST =	        CMakeLists.txt safe_process_win.cc safe_kill_win.cc
+EXTRA_DIST =	        CMakeLists.txt
 
 perror.o: perror.c 
 	$(COMPILE) @ndbcluster_includes@ $(LM_CFLAGS) -c $<
diff -Nrup a/extra/safe_kill_win.cc b/extra/safe_kill_win.cc
--- a/extra/safe_kill_win.cc	2007-11-05 15:02:24 +01:00
+++ /dev/null	Wed Dec 31 16:00:00 196900
@@ -1,56 +0,0 @@
-/* Copyright (C) 2004 MySQL AB
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; version 2 of the License.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
-
-
-/*
-  Utility program used to signal a safe_process it's time to shutdown
-
-  Usage:
-    safe_kill <pid>
-*/
-
-#include <windows.h>
-#include <stdio.h>
-
-int main(int argc, const char** argv )
-{
-  DWORD pid= -1;
-  HANDLE shutdown_event;
-  char safe_process_name[32]= {0};
-
-  if (argc != 2) {
-    fprintf(stderr, "safe_kill <pid>\n");
-    exit(2);
-  }
-  pid= atoi(argv[1]);
-
-  _snprintf(safe_process_name, sizeof(safe_process_name), "safe_process[%d]", pid);
-
-  /* Open the event to signal */
-  if ((shutdown_event=
-    OpenEvent(EVENT_MODIFY_STATE, FALSE, safe_process_name)) == NULL){
-    fprintf(stderr, "Failed to open shutdown_event\n");
-    exit(1);
-  }
-
-  if(SetEvent(shutdown_event) == 0) {
-    fprintf(stderr, "Failed to signal shutdown_event\n");
-    CloseHandle(shutdown_event);
-    exit(1);
-  }
-  CloseHandle(shutdown_event);
-  exit(0);
-}
-
diff -Nrup a/extra/safe_process_win.cc b/extra/safe_process_win.cc
--- a/extra/safe_process_win.cc	2007-10-30 19:06:40 +01:00
+++ /dev/null	Wed Dec 31 16:00:00 196900
@@ -1,312 +0,0 @@
-/* Copyright (C) 2004 MySQL AB
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; version 2 of the License.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
-
-
-/*
-  Utility program that encapsulates process creation, monitoring
-  and bulletproof process cleanup
-
-  Usage:
-    safe_process [options to safe_process] -- progname arg1 ... argn
-
-  To safeguard mysqld you would invoke safe_process with a few options
-  for safe_process itself followed by a double dash to indicate start
-  of the command line for the program you really want to start
-
-  $> safe_process --output=output.log -- mysqld --datadir=var/data1 ...
-
-  This would redirect output to output.log and then start mysqld,
-  once it has done that it will continue to monitor the child as well
-  as the parent.
-
-  The safe_process then checks the follwing things:
-  1. Child exits, propagate the childs return code to the parent
-     by exiting with the same return code as the child.
-
-  2. Parent dies, immediately kill the child and exit, thus the
-     parent does not need to properly cleanup any child, it is handled
-     automatically.
-
-  3. Signal's recieced by the process will trigger same action as 2)
-
-  4. The named event "safe_process[pid]" can be signaled and will
-     trigger same action as 2)
-
-  WARNING! Be careful when using ProcessExplorer, since it will open
-           a handle to each process(and maybe also the Job), the process
-           spawned by safe_process will not be closed off when safe_process
-           is killed.
-*/
-
-/* Requires Windows 2000 or higher */
-#define _WIN32_WINNT 0x0500
-
-#include <windows.h>
-#include <stdio.h>
-#include <tlhelp32.h>
-#include <signal.h>
-
-static int verbose= 0;
-static char safe_process_name[32]= {0};
-
-static void message(const char* fmt, ...)
-{
-  if (!verbose)
-    return;
-  va_list args;
-  fprintf(stderr, "%s: ", safe_process_name);
-  va_start(args, fmt);
-  vfprintf(stderr, fmt, args);
-  fprintf(stderr, "\n");
-  va_end(args);
-  fflush(stderr);
-}
-
-
-static void die(const char* fmt, ...)
-{
-  va_list args;
-  fprintf(stderr, "%s: FATAL ERROR, ", safe_process_name);
-  va_start(args, fmt);
-  vfprintf(stderr, fmt, args);
-  fprintf(stderr, "\n");
-  va_end(args);
-  if (int last_err= GetLastError())
-    fprintf(stderr, "error: %d, %s\n", last_err, strerror(last_err));
-  exit(1);
-}
-
-
-DWORD get_parent_pid(DWORD pid)
-{
-  HANDLE snapshot;
-  DWORD parent_pid= -1;
-  PROCESSENTRY32 pe32;
-  pe32.dwSize= sizeof(PROCESSENTRY32);
-
-  snapshot= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
-  if (snapshot == INVALID_HANDLE_VALUE)
-    die("CreateToolhelp32Snapshot failed");
-
-  if (!Process32First(snapshot, &pe32))
-  {
-    CloseHandle(snapshot);
-    die("Process32First failed");
-  }
-
-  do
-  {
-    if (pe32.th32ProcessID == pid)
-      parent_pid= pe32.th32ParentProcessID;
-  } while(Process32Next( snapshot, &pe32));
-  CloseHandle(snapshot);
-
-  if (parent_pid == -1)
-    die("Could not find parent pid");
-
-  return parent_pid;
-}
-
-
-enum {
-  PARENT,
-  CHILD,
-  EVENT,
-  NUM_HANDLES
-};
-
-
-HANDLE shutdown_event;
-void handle_signal (int signal)
-{
-  message("Got signal: %d", signal);
-  if(SetEvent(shutdown_event) == 0) {
-    /* exit safe_process and (hopefully) kill off the child */
-    die("Failed to SetEvent");
-  }
-}
-
-
-int main(int argc, const char** argv )
-{
-  char child_args[4096]= {0};
-  DWORD pid= GetCurrentProcessId();
-  DWORD parent_pid= get_parent_pid(pid);
-  HANDLE job_handle;
-  HANDLE wait_handles[NUM_HANDLES]= {0};
-  PROCESS_INFORMATION process_info= {0};
-
-  sprintf(safe_process_name, "safe_process[%d]", pid);
-
-  /* Create an event for the signal handler */
-  if ((shutdown_event=
-       CreateEvent(NULL, TRUE, FALSE, safe_process_name)) == NULL)
-    die("Failed to create shutdown_event");
-  wait_handles[EVENT]= shutdown_event;
-
-  signal(SIGINT,   handle_signal);
-  signal(SIGBREAK, handle_signal);
-  signal(SIGTERM,  handle_signal);
-
-  message("Started");
-
-  /* Parse arguments */
-  for (int i= 1; i < argc; i++) {
-    const char* arg= argv[i];
-	char* to= child_args;
-    if (strcmp(arg, "--") == 0 && strlen(arg) == 2) {
-      /* Got the "--" delimiter */
-      if (i >= argc)
-        die("No real args -> nothing to do");
-      /* Copy the remaining args to child_arg */
-      for (int j= i+1; j < argc; j++) {
-        to+= _snprintf(to, child_args + sizeof(child_args) - to, "%s ", argv[j]);
-      }
-      break;
-    } else {
-      if ( strcmp(arg, "--verbose") == 0 )
-        verbose++;
-	  else if ( strncmp(arg, "--parent-pid", 10) == 0 )
-	  {
-	    /* Override parent_pid with a value provided by user */
-		const char* start;
-        if ((start= strstr(arg, "=")) == NULL)
-		  die("Could not find start of option value in '%s'", arg);
-		start++; /* Step past = */
-		if ((parent_pid= atoi(start)) == 0)
-		  die("Invalid value '%s' passed to --parent-id", start);
-	  }
-      else
-        die("Unknown option: %s", arg);
-    }
-  }
-  if (*child_args == '\0')
-    die("nothing to do");
-
-  /* Open a handle to the parent process */
-  message("parent_pid: %d", parent_pid);
-  if (parent_pid == pid)
-    die("parent_pid is equal to own pid!");
-
-  if ((wait_handles[PARENT]=
-       OpenProcess(SYNCHRONIZE, FALSE, parent_pid)) == NULL)
-    die("Failed to open parent process with pid: %d", parent_pid);
-
-  /* Create the child process in a job */
-  JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };
-  STARTUPINFO                          si   = { 0 };
-  si.cb = sizeof(si);
-
-  /*
-    Create the job object to make it possible to kill the process
-    and all of it's children in one go
-  */
-  if ((job_handle= CreateJobObject(NULL, NULL)) == NULL)
-    die("CreateJobObject failed");
-
-  /*
-    Make all processes associated with the job terminate when the
-    last handle to the job is closed.
-  */
-  jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
-  if (SetInformationJobObject(job_handle, JobObjectExtendedLimitInformation,
-                              &jeli, sizeof(jeli)) == 0)
-    message("SetInformationJobObject failed, continue anyway...");
-
-#if 0
-  /* Setup stdin, stdout and stderr redirect */
-  si.dwFlags= STARTF_USESTDHANDLES;
-  si.hStdInput= GetStdHandle(STD_INPUT_HANDLE);
-  si.hStdOutput= GetStdHandle(STD_OUTPUT_HANDLE);
-  si.hStdError= GetStdHandle(STD_ERROR_HANDLE);
-#endif
-
-  /*
-    Create the process suspended to make sure it's assigned to the
-    Job before it creates any process of it's own
-  */
-  if (CreateProcess(NULL, (LPSTR)child_args,
-                    NULL,
-                    NULL,
-                    TRUE, /* inherit handles */
-                    CREATE_SUSPENDED,
-                    NULL,
-                    NULL,
-                    &si,
-                    &process_info) == 0)
-    die("CreateProcess failed");
-
-  if (AssignProcessToJobObject(job_handle, process_info.hProcess) == 0)
-  {
-    TerminateProcess(process_info.hProcess, 200);
-    die("AssignProcessToJobObject failed");
-  }
-  ResumeThread(process_info.hThread);
-  CloseHandle(process_info.hThread);
-
-  wait_handles[CHILD]= process_info.hProcess;
-
-  message("Started child %d", process_info.dwProcessId);
-
-  /* Monitor loop */
-  DWORD child_exit_code= 1;
-  DWORD wait_res= WaitForMultipleObjects(NUM_HANDLES, wait_handles,
-                                         FALSE, INFINITE);
-  switch (wait_res)
-  {
-    case WAIT_OBJECT_0 + PARENT:
-      message("Parent exit");
-      break;
-    case WAIT_OBJECT_0 + CHILD:
-      if (GetExitCodeProcess(wait_handles[CHILD], &child_exit_code) == 0)
-        message("Child exit: could not get exit_code");
-      else
-        message("Child exit: exit_code: %d", child_exit_code);
-      break;
-    case WAIT_OBJECT_0 + EVENT:
-      message("Wake up from shutdown_event");
-      break;
-
-    default:
-      message("Unexpected result %d from WaitForMultipleObjects", wait_res);
-      break;
-  }
-  message("Exiting, child: %d", process_info.dwProcessId);
-
-  if (TerminateJobObject(job_handle, 201) == 0)
-    message("TerminateJobObject failed");
-  CloseHandle(job_handle);
-
-  if (wait_res != WAIT_OBJECT_0 + CHILD)
-  {
-	/* The child has not yet returned, wait for it */
-    message("waiting for child to exit");
-    if ((wait_res= WaitForSingleObject(wait_handles[CHILD], INFINITE)) != WAIT_OBJECT_0)
-	{
-      message("child wait failed: %d", wait_res);
-	}
-	else
-	{
-	  message("child wait succeeded");
-	}
-    /* Child's exit code should now be 201, no need to get it */
-  }
-
-  for (int i= 0; i < NUM_HANDLES; i++)
-    CloseHandle(wait_handles[i]);
-
-  exit(child_exit_code);
-}
-
diff -Nrup a/mysql-test/lib/My/SafeProcess/safe_kill_win.cc
b/mysql-test/lib/My/SafeProcess/safe_kill_win.cc
--- /dev/null	Wed Dec 31 16:00:00 196900
+++ b/mysql-test/lib/My/SafeProcess/safe_kill_win.cc	2007-12-06 15:25:47 +01:00
@@ -0,0 +1,56 @@
+/* Copyright (C) 2004 MySQL AB
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; version 2 of the License.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
+
+
+/*
+  Utility program used to signal a safe_process it's time to shutdown
+
+  Usage:
+    safe_kill <pid>
+*/
+
+#include <windows.h>
+#include <stdio.h>
+
+int main(int argc, const char** argv )
+{
+  DWORD pid= -1;
+  HANDLE shutdown_event;
+  char safe_process_name[32]= {0};
+
+  if (argc != 2) {
+    fprintf(stderr, "safe_kill <pid>\n");
+    exit(2);
+  }
+  pid= atoi(argv[1]);
+
+  _snprintf(safe_process_name, sizeof(safe_process_name), "safe_process[%d]", pid);
+
+  /* Open the event to signal */
+  if ((shutdown_event=
+    OpenEvent(EVENT_MODIFY_STATE, FALSE, safe_process_name)) == NULL){
+    fprintf(stderr, "Failed to open shutdown_event\n");
+    exit(1);
+  }
+
+  if(SetEvent(shutdown_event) == 0) {
+    fprintf(stderr, "Failed to signal shutdown_event\n");
+    CloseHandle(shutdown_event);
+    exit(1);
+  }
+  CloseHandle(shutdown_event);
+  exit(0);
+}
+
diff -Nrup a/mysql-test/lib/My/SafeProcess/safe_process_win.cc
b/mysql-test/lib/My/SafeProcess/safe_process_win.cc
--- /dev/null	Wed Dec 31 16:00:00 196900
+++ b/mysql-test/lib/My/SafeProcess/safe_process_win.cc	2007-12-06 15:25:42 +01:00
@@ -0,0 +1,312 @@
+/* Copyright (C) 2004 MySQL AB
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; version 2 of the License.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
+
+
+/*
+  Utility program that encapsulates process creation, monitoring
+  and bulletproof process cleanup
+
+  Usage:
+    safe_process [options to safe_process] -- progname arg1 ... argn
+
+  To safeguard mysqld you would invoke safe_process with a few options
+  for safe_process itself followed by a double dash to indicate start
+  of the command line for the program you really want to start
+
+  $> safe_process --output=output.log -- mysqld --datadir=var/data1 ...
+
+  This would redirect output to output.log and then start mysqld,
+  once it has done that it will continue to monitor the child as well
+  as the parent.
+
+  The safe_process then checks the follwing things:
+  1. Child exits, propagate the childs return code to the parent
+     by exiting with the same return code as the child.
+
+  2. Parent dies, immediately kill the child and exit, thus the
+     parent does not need to properly cleanup any child, it is handled
+     automatically.
+
+  3. Signal's recieced by the process will trigger same action as 2)
+
+  4. The named event "safe_process[pid]" can be signaled and will
+     trigger same action as 2)
+
+  WARNING! Be careful when using ProcessExplorer, since it will open
+           a handle to each process(and maybe also the Job), the process
+           spawned by safe_process will not be closed off when safe_process
+           is killed.
+*/
+
+/* Requires Windows 2000 or higher */
+#define _WIN32_WINNT 0x0500
+
+#include <windows.h>
+#include <stdio.h>
+#include <tlhelp32.h>
+#include <signal.h>
+
+static int verbose= 0;
+static char safe_process_name[32]= {0};
+
+static void message(const char* fmt, ...)
+{
+  if (!verbose)
+    return;
+  va_list args;
+  fprintf(stderr, "%s: ", safe_process_name);
+  va_start(args, fmt);
+  vfprintf(stderr, fmt, args);
+  fprintf(stderr, "\n");
+  va_end(args);
+  fflush(stderr);
+}
+
+
+static void die(const char* fmt, ...)
+{
+  va_list args;
+  fprintf(stderr, "%s: FATAL ERROR, ", safe_process_name);
+  va_start(args, fmt);
+  vfprintf(stderr, fmt, args);
+  fprintf(stderr, "\n");
+  va_end(args);
+  if (int last_err= GetLastError())
+    fprintf(stderr, "error: %d, %s\n", last_err, strerror(last_err));
+  exit(1);
+}
+
+
+DWORD get_parent_pid(DWORD pid)
+{
+  HANDLE snapshot;
+  DWORD parent_pid= -1;
+  PROCESSENTRY32 pe32;
+  pe32.dwSize= sizeof(PROCESSENTRY32);
+
+  snapshot= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+  if (snapshot == INVALID_HANDLE_VALUE)
+    die("CreateToolhelp32Snapshot failed");
+
+  if (!Process32First(snapshot, &pe32))
+  {
+    CloseHandle(snapshot);
+    die("Process32First failed");
+  }
+
+  do
+  {
+    if (pe32.th32ProcessID == pid)
+      parent_pid= pe32.th32ParentProcessID;
+  } while(Process32Next( snapshot, &pe32));
+  CloseHandle(snapshot);
+
+  if (parent_pid == -1)
+    die("Could not find parent pid");
+
+  return parent_pid;
+}
+
+
+enum {
+  PARENT,
+  CHILD,
+  EVENT,
+  NUM_HANDLES
+};
+
+
+HANDLE shutdown_event;
+void handle_signal (int signal)
+{
+  message("Got signal: %d", signal);
+  if(SetEvent(shutdown_event) == 0) {
+    /* exit safe_process and (hopefully) kill off the child */
+    die("Failed to SetEvent");
+  }
+}
+
+
+int main(int argc, const char** argv )
+{
+  char child_args[4096]= {0};
+  DWORD pid= GetCurrentProcessId();
+  DWORD parent_pid= get_parent_pid(pid);
+  HANDLE job_handle;
+  HANDLE wait_handles[NUM_HANDLES]= {0};
+  PROCESS_INFORMATION process_info= {0};
+
+  sprintf(safe_process_name, "safe_process[%d]", pid);
+
+  /* Create an event for the signal handler */
+  if ((shutdown_event=
+       CreateEvent(NULL, TRUE, FALSE, safe_process_name)) == NULL)
+    die("Failed to create shutdown_event");
+  wait_handles[EVENT]= shutdown_event;
+
+  signal(SIGINT,   handle_signal);
+  signal(SIGBREAK, handle_signal);
+  signal(SIGTERM,  handle_signal);
+
+  message("Started");
+
+  /* Parse arguments */
+  for (int i= 1; i < argc; i++) {
+    const char* arg= argv[i];
+	char* to= child_args;
+    if (strcmp(arg, "--") == 0 && strlen(arg) == 2) {
+      /* Got the "--" delimiter */
+      if (i >= argc)
+        die("No real args -> nothing to do");
+      /* Copy the remaining args to child_arg */
+      for (int j= i+1; j < argc; j++) {
+        to+= _snprintf(to, child_args + sizeof(child_args) - to, "%s ", argv[j]);
+      }
+      break;
+    } else {
+      if ( strcmp(arg, "--verbose") == 0 )
+        verbose++;
+	  else if ( strncmp(arg, "--parent-pid", 10) == 0 )
+	  {
+	    /* Override parent_pid with a value provided by user */
+		const char* start;
+        if ((start= strstr(arg, "=")) == NULL)
+		  die("Could not find start of option value in '%s'", arg);
+		start++; /* Step past = */
+		if ((parent_pid= atoi(start)) == 0)
+		  die("Invalid value '%s' passed to --parent-id", start);
+	  }
+      else
+        die("Unknown option: %s", arg);
+    }
+  }
+  if (*child_args == '\0')
+    die("nothing to do");
+
+  /* Open a handle to the parent process */
+  message("parent_pid: %d", parent_pid);
+  if (parent_pid == pid)
+    die("parent_pid is equal to own pid!");
+
+  if ((wait_handles[PARENT]=
+       OpenProcess(SYNCHRONIZE, FALSE, parent_pid)) == NULL)
+    die("Failed to open parent process with pid: %d", parent_pid);
+
+  /* Create the child process in a job */
+  JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };
+  STARTUPINFO                          si   = { 0 };
+  si.cb = sizeof(si);
+
+  /*
+    Create the job object to make it possible to kill the process
+    and all of it's children in one go
+  */
+  if ((job_handle= CreateJobObject(NULL, NULL)) == NULL)
+    die("CreateJobObject failed");
+
+  /*
+    Make all processes associated with the job terminate when the
+    last handle to the job is closed.
+  */
+  jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
+  if (SetInformationJobObject(job_handle, JobObjectExtendedLimitInformation,
+                              &jeli, sizeof(jeli)) == 0)
+    message("SetInformationJobObject failed, continue anyway...");
+
+#if 0
+  /* Setup stdin, stdout and stderr redirect */
+  si.dwFlags= STARTF_USESTDHANDLES;
+  si.hStdInput= GetStdHandle(STD_INPUT_HANDLE);
+  si.hStdOutput= GetStdHandle(STD_OUTPUT_HANDLE);
+  si.hStdError= GetStdHandle(STD_ERROR_HANDLE);
+#endif
+
+  /*
+    Create the process suspended to make sure it's assigned to the
+    Job before it creates any process of it's own
+  */
+  if (CreateProcess(NULL, (LPSTR)child_args,
+                    NULL,
+                    NULL,
+                    TRUE, /* inherit handles */
+                    CREATE_SUSPENDED,
+                    NULL,
+                    NULL,
+                    &si,
+                    &process_info) == 0)
+    die("CreateProcess failed");
+
+  if (AssignProcessToJobObject(job_handle, process_info.hProcess) == 0)
+  {
+    TerminateProcess(process_info.hProcess, 200);
+    die("AssignProcessToJobObject failed");
+  }
+  ResumeThread(process_info.hThread);
+  CloseHandle(process_info.hThread);
+
+  wait_handles[CHILD]= process_info.hProcess;
+
+  message("Started child %d", process_info.dwProcessId);
+
+  /* Monitor loop */
+  DWORD child_exit_code= 1;
+  DWORD wait_res= WaitForMultipleObjects(NUM_HANDLES, wait_handles,
+                                         FALSE, INFINITE);
+  switch (wait_res)
+  {
+    case WAIT_OBJECT_0 + PARENT:
+      message("Parent exit");
+      break;
+    case WAIT_OBJECT_0 + CHILD:
+      if (GetExitCodeProcess(wait_handles[CHILD], &child_exit_code) == 0)
+        message("Child exit: could not get exit_code");
+      else
+        message("Child exit: exit_code: %d", child_exit_code);
+      break;
+    case WAIT_OBJECT_0 + EVENT:
+      message("Wake up from shutdown_event");
+      break;
+
+    default:
+      message("Unexpected result %d from WaitForMultipleObjects", wait_res);
+      break;
+  }
+  message("Exiting, child: %d", process_info.dwProcessId);
+
+  if (TerminateJobObject(job_handle, 201) == 0)
+    message("TerminateJobObject failed");
+  CloseHandle(job_handle);
+
+  if (wait_res != WAIT_OBJECT_0 + CHILD)
+  {
+	/* The child has not yet returned, wait for it */
+    message("waiting for child to exit");
+    if ((wait_res= WaitForSingleObject(wait_handles[CHILD], INFINITE)) != WAIT_OBJECT_0)
+	{
+      message("child wait failed: %d", wait_res);
+	}
+	else
+	{
+	  message("child wait succeeded");
+	}
+    /* Child's exit code should now be 201, no need to get it */
+  }
+
+  for (int i= 0; i < NUM_HANDLES; i++)
+    CloseHandle(wait_handles[i]);
+
+  exit(child_exit_code);
+}
+
Thread
bk commit into 5.1 tree (msvensson:1.2552)msvensson6 Dec