浏览代码

Added fork() emulation using CreateProcess() in MinGW

Ross Smith II 16 年之前
父节点
当前提交
e69889803a
共有 2 个文件被更改,包括 33 次插入0 次删除
  1. 5 0
      ChangeLog
  2. 28 0
      src/RequestGroupMan.cc

+ 5 - 0
ChangeLog

@@ -1,3 +1,8 @@
+2009-06-01  Ross Smith II  <aria2spam at smithii dot com>
+
+	Added fork() emulation using CreateProcess() in MinGW
+	* src/RequestGroupMan.cc
+	
 2009-06-01  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	* Release 1.4.0

+ 28 - 0
src/RequestGroupMan.cc

@@ -197,6 +197,7 @@ static void executeHook(const std::string& command, int gid)
 {
   LogFactory::getInstance()->info("Executing user command: %s %d",
 				  command.c_str(), gid);
+#ifndef __MINGW32__
   pid_t cpid = fork();
   if(cpid == -1) {
     LogFactory::getInstance()->error("fork() failed."
@@ -206,6 +207,33 @@ static void executeHook(const std::string& command, int gid)
     perror(("Could not execute user command: "+command).c_str());
     _exit(1);
   }
+#else
+  PROCESS_INFORMATION pi;
+  STARTUPINFO si;
+
+  memset(&si, 0, sizeof (si));
+  si.cb = sizeof(STARTUPINFO);
+
+  memset(&pi, 0, sizeof (pi));
+
+  std::string cmdline = command + " " + Util::itos(gid);
+
+  DWORD rc = CreateProcess(
+    NULL,
+    (LPSTR)cmdline.c_str(),
+    NULL,
+    NULL,
+    true,
+    NULL,
+    NULL,
+    0,
+    &si,
+    &pi);
+
+  if(!rc)
+    LogFactory::getInstance()->error("CreateProcess() failed."
+				     " Cannot execute user command.");
+#endif 
 }
 
 static void executeStartHook