summaryrefslogtreecommitdiff
path: root/utils/vmpi/WaitAndRestart/WaitAndRestart.cpp
blob: c9ef1e075a0bada1f0bf8f3e5b9594b5ddd01949 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
// WaitAndRestart.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "tier1/strtools.h"
#include "vmpi_defs.h"


void PrintLog( const char *pMsg, ... )
{
#ifdef VMPI_SERVICE_LOGS
	char str[4096];
	va_list marker;
	
	va_start( marker, pMsg );
	_vsnprintf( str, sizeof( str ), pMsg, marker );
	va_end( marker );
	
	printf( "%s", str );
	
	static FILE *fp = fopen( "c:\\vmpi_WaitAndRestart.log", "wt" );
	if ( fp )
	{
		fprintf( fp, "%s", str );
		fflush( fp );
	}
#endif
}


char* GetLastErrorString()
{
	static char err[2048];
	
	LPVOID lpMsgBuf;
	FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL );
	strncpy( err, (char*)lpMsgBuf, sizeof( err ) );
	LocalFree( lpMsgBuf );

	err[ sizeof( err ) - 1 ] = 0;
	return err;
}


int main( int argc, char* argv[] )
{
Sleep(5000);
	if ( argc < 4 )
	{
		PrintLog( "WaitAndRestart <seconds to wait> <working directory> command line...\n" );
		return 1;
	}

	PrintLog( "WaitAndRestart <seconds to wait> <working directory> command line...\n" );


	const char *pTimeToWait = argv[1];
	const char *pWorkingDir = argv[2];
	
	// If a * precedes the time-to-wait arg, then it's a process ID and we wait for that process to exit.
	if ( pTimeToWait[0] == '*' )
	{
		++pTimeToWait;
		DWORD dwProcessId;
		sscanf( pTimeToWait, "%lu", &dwProcessId );
		
		PrintLog( "Waiting for process %lu to exit. Press a key to cancel...\n", dwProcessId );
		
		HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | SYNCHRONIZE, false, dwProcessId );
		if ( hProcess )
		{
			while ( 1 )
			{
				DWORD val = WaitForSingleObject( hProcess, 100 );
				if ( val == WAIT_OBJECT_0 )
				{
					break;
				}
				else if ( val == WAIT_ABANDONED )
				{
					PrintLog( "Got WAIT_ABANDONED (error). Waiting 5 seconds, then continuing.\n" );
					Sleep( 5000 );
					break;
				}

				if ( kbhit() )
					return 2;
			}
			PrintLog( "Process %lu terminated. Continuing.\n", dwProcessId );
		}
		else
		{
			PrintLog( "Process %lu not running. Continuing.\n", dwProcessId );
		}
		
		CloseHandle( hProcess );
	}
	else
	{
		DWORD timeToWait = (DWORD)atoi( argv[1] );
		
		PrintLog( "\n\nWaiting for %d seconds to launch ' ", timeToWait );
		PrintLog( "%s> ", pWorkingDir );
		for ( int i=3; i < argc; i++ )
		{
			PrintLog( "%s ", argv[i] );
		}
		PrintLog( "'\n\nPress a key to cancel... " );

		DWORD startTime = GetTickCount();
		while ( GetTickCount() - startTime < (timeToWait*1000) )
		{
			if ( kbhit() )
				return 2;
			
			Sleep( 100 );
		}
	}

	// Ok, launch it!
	char commandLine[1024] = {0};
	for ( int i=3; i < argc; i++ )
	{
		Q_strncat( commandLine, "\"", sizeof( commandLine ), COPY_ALL_CHARACTERS );
		Q_strncat( commandLine, argv[i], sizeof( commandLine ), COPY_ALL_CHARACTERS );
		Q_strncat( commandLine, "\" ", sizeof( commandLine ), COPY_ALL_CHARACTERS );
	}
	
	STARTUPINFO si;
	memset( &si, 0, sizeof( si ) );
	si.cb = sizeof( si );

	PROCESS_INFORMATION pi;
	memset( &pi, 0, sizeof( pi ) );

	if ( CreateProcess( 
		NULL, 
		commandLine, 
		NULL,						// security
		NULL,
		FALSE,
		0,							// flags
		NULL,						// environment
		pWorkingDir,						// current directory
		&si,
		&pi ) )
	{
		PrintLog( "Process started.\n" );
		CloseHandle( pi.hThread );	// We don't care what the process does.
		CloseHandle( pi.hProcess );
	}
	else
	{
		PrintLog( "CreateProcess error!\n%s", GetLastErrorString() );
	}

	return 0;
}