| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 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 |
|
| 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) |
| 45 |
{ |
| 46 |
close(updater_pipes[0]); |
| 47 |
close(updater_pipes[1]); |
| 48 |
} |
| 49 |
else if (pid == 0) |
| 50 |
{ |
| 51 |
close(updater_pipes[1]); |
| 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); |
| 60 |
} |
| 61 |
#ifdef __APPLE__ |
| 62 |
if (chdir("../..") == -1) { |
| 63 |
_exit(1); |
| 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); |
| 70 |
} |
| 71 |
else |
| 72 |
{ |
| 73 |
|
| 74 |
close(updater_pipes[0]); |
| 75 |
} |
| 76 |
} |
| 77 |
#endif |
| 78 |
} |
| 79 |
|
| 80 |
#endif |