Update lua versions
[ryzomcore.git] / nel / tools / misc / exec_timeout / exec_timeout.cpp
blob1469b1af6273c407502ffe3a4c20f3f06649cc45
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "nel/misc/debug.h"
18 #include <windows.h>
19 #include <string>
21 using namespace std;
23 int main(int argc, char* argv[])
25 // Help ?
26 if (argc<3)
28 printf ("exec_timeout [timeout(ms)] [prog.exe] [arg0] [arg1]...\nReturn 0:error, 1:ok, 2:timeout\n");
29 return 0;
31 else
33 // String
34 string argu;
36 // Build the argus
37 for (int i=2; i<argc; i++)
39 argu += argv[i];
40 if (i<argc-1)
41 argu += " ";
44 // Process information
45 PROCESS_INFORMATION process_info;
47 // Timeout
48 DWORD timeout = atoi (argv[1]);
50 // Startup info
51 STARTUPINFO startupInfo;
52 GetStartupInfo (&startupInfo);
54 // Exec
55 if (CreateProcess( NULL, (char*)(argu.c_str()), NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP, NULL, NULL, &startupInfo, &process_info))
57 // Return message
58 switch (WaitForSingleObject ( process_info.hProcess, timeout))
60 case WAIT_OBJECT_0:
61 return 1;
62 case WAIT_ABANDONED:
63 return 0;
64 case WAIT_TIMEOUT:
65 if (TerminateProcess (process_info.hProcess, 0))
67 nlwarning ("ERROR: Timeout in process %s", (char*)(argu.c_str()));
68 return 2;
70 else
71 nlwarning ("ERROR: Error while terminate current process %s", (char*)(argu.c_str()));
72 return 0;
75 else
76 nlwarning ("ERROR: Error can't exec process %s", (char*)(argu.c_str()));
77 return 0;
80 // Ok
81 return 1;