File: sys_autoupdater.c

package info (click to toggle)
openmohaa 0.81.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 29,124 kB
  • sloc: ansic: 270,865; cpp: 250,173; sh: 234; asm: 141; xml: 64; makefile: 7
file content (86 lines) | stat: -rw-r--r-- 2,308 bytes parent folder | download | duplicates (2)
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
/*
The code in this file is in the public domain. The rest of ioquake3
is licensed under the GPLv2. Do not mingle code, please!
*/

#ifdef USE_AUTOUPDATER
#  ifndef AUTOUPDATER_BIN
#    error The build system should have defined AUTOUPDATER_BIN
#  endif

#  ifdef _WIN32
#    define WIN32_LEAN_AND_MEAN 1
#    include <windows.h>
#  else
#    include <unistd.h>
#  endif

#  include <stdio.h>
#  include <string.h>
#endif

void Sys_LaunchAutoupdater(int argc, char **argv)
{
#ifdef USE_AUTOUPDATER
	#ifdef _WIN32
	{
		/* We don't need the Unix pipe() tapdance here because Windows lets children wait on parent processes. */
		PROCESS_INFORMATION procinfo;
		STARTUPINFO startinfo;
		char cmdline[128];
		memset(&procinfo, '\0', sizeof (procinfo));
		memset(&startinfo, '\0', sizeof (startinfo));
		startinfo.cb = sizeof (startinfo);
		Com_sprintf(cmdline, sizeof(cmdline), "" AUTOUPDATER_BIN " --waitpid %u", (unsigned int) GetCurrentProcessId());

		if (CreateProcessA(AUTOUPDATER_BIN, cmdline, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &startinfo, &procinfo))
		{
			/* close handles now so child cleans up immediately if nothing to do */
			CloseHandle(procinfo.hProcess);
			CloseHandle(procinfo.hThread);
		}
	}
	#else
	int updater_pipes[2];
	if (pipe(updater_pipes) == 0)
	{
		pid_t pid = fork();
		if (pid == -1)  /* failure, oh well. */
		{
			close(updater_pipes[0]);
			close(updater_pipes[1]);
		}
		else if (pid == 0)  /* child process */
		{
			close(updater_pipes[1]);  /* don't need write end. */
			if (dup2(updater_pipes[0], 3) != -1)
			{
				char pidstr[64];
				char *ptr = strrchr(argv[0], '/');
				if (ptr)
					*ptr = '\0';
				if (chdir(argv[0]) == -1) {
					_exit(1);  /* oh well. */
				}
				#ifdef __APPLE__
				if (chdir("../..") == -1) {  /* put this at base of app bundle so paths make sense later. */
					_exit(1);  /* oh well. */
				}
				#endif
				snprintf(pidstr, sizeof (pidstr), "%lld", (long long) getppid());
				execl(AUTOUPDATER_BIN, AUTOUPDATER_BIN, "--waitpid", pidstr, NULL);
			}
			_exit(0);  /* oh well. */
		}
		else   /* parent process */
		{
			/* leave the write end open until we terminate so updater can block on it. */
			close(updater_pipes[0]);
		}
	}
	#endif
#endif

	(void) argc; (void) argv;  /* possibly unused. Pacify compilers. */
}