Debian Codegraph @

code/sys/sys_autoupdater.c

raw symbols SCIP
1 /*
2 The code in this file is in the public domain. The rest of ioquake3
3 is licensed under the GPLv2. Do not mingle code, please!
4 */
5
6 #ifdef USE_AUTOUPDATER
7 # ifndef AUTOUPDATER_BIN
8 # error The build system should have defined AUTOUPDATER_BIN
9 # endif
10
11 # ifdef _WIN32
12 # define WIN32_LEAN_AND_MEAN 1
13 # include <windows.h>
14 # else
15 # include <unistd.h>
16 # endif
17
18 # include <stdio.h>
19 # include <string.h>
20
21 void Sys_LaunchAutoupdater(int argc, char **argv)
22 {
23 #ifdef _WIN32
24 /* We don't need the Unix pipe() tapdance here because Windows lets children wait on parent processes. */
25 PROCESS_INFORMATION procinfo;
26 STARTUPINFO startinfo;
27 char cmdline[128];
28 memset(&procinfo, '\0', sizeof (procinfo));
29 memset(&startinfo, '\0', sizeof (startinfo));
30 startinfo.cb = sizeof (startinfo);
31 sprintf(cmdline, "" AUTOUPDATER_BIN " --waitpid %u", (unsigned int) GetCurrentProcessId());
32
33 if (CreateProcessA(AUTOUPDATER_BIN, cmdline, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &startinfo, &procinfo))
34 {
35 /* close handles now so child cleans up immediately if nothing to do */
36 CloseHandle(procinfo.hProcess);
37 CloseHandle(procinfo.hThread);
38 }
39 #else
40 int updater_pipes[2];
41 if (pipe(updater_pipes) == 0)
42 {
43 pid_t pid = fork();
44 if (pid == -1) /* failure, oh well. */
45 {
46 close(updater_pipes[0]);
47 close(updater_pipes[1]);
48 }
49 else if (pid == 0) /* child process */
50 {
51 close(updater_pipes[1]); /* don't need write end. */
52 if (dup2(updater_pipes[0], 3) != -1)
53 {
54 char pidstr[64];
55 char *ptr = strrchr(argv[0], '/');
56 if (ptr)
57 *ptr = '\0';
58 if (chdir(argv[0]) == -1) {
59 _exit(1); /* oh well. */
60 }
61 #ifdef __APPLE__
62 if (chdir("../..") == -1) { /* put this at base of app bundle so paths make sense later. */
63 _exit(1); /* oh well. */
64 }
65 #endif
66 snprintf(pidstr, sizeof (pidstr), "%lld", (long long) getppid());
67 execl(AUTOUPDATER_BIN, AUTOUPDATER_BIN, "--waitpid", pidstr, NULL);
68 }
69 _exit(0); /* oh well. */
70 }
71 else /* parent process */
72 {
73 /* leave the write end open until we terminate so updater can block on it. */
74 close(updater_pipes[0]);
75 }
76 }
77 #endif
78 }
79
80 #endif /* USE_AUTOUPDATER */