2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2006-12-13 16:04:50 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2006-12-13 16:04:50 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
2006-12-13 16:04:50 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2006-12-13 16:04:50 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2006-12-13 16:04:50 +00:00
|
|
|
|
2006-12-14 20:44:39 +00:00
|
|
|
// Program to install files as part of auto-update.
|
2007-04-09 02:17:26 +00:00
|
|
|
// Run in a directory that contains the new files.
|
2006-12-14 20:44:39 +00:00
|
|
|
// Arguments:
|
|
|
|
//
|
|
|
|
// --install_dir X copy files to X (required)
|
|
|
|
// --run_manager when done, run Manager
|
|
|
|
// --run_as_service when done, run core client as service
|
2007-04-09 02:17:26 +00:00
|
|
|
//
|
|
|
|
// What it does:
|
|
|
|
// 1) wait for a mutex, to ensure that the core client has exited
|
|
|
|
// 2) create or empty out a "previous-version" dir
|
|
|
|
// 3) copy files from install dir to previous-version dir
|
|
|
|
// 4) copy files from current dir to install dir
|
|
|
|
// 5) run the new core client and/or manager
|
|
|
|
//
|
|
|
|
// If we get an error in 2) or 3):
|
|
|
|
// 6) run the (old) core client and/or manager
|
|
|
|
// We pass it a "--run_from_updater" option; this causes it to
|
|
|
|
// mark the update as failed, so it won't try again.
|
|
|
|
// If we get an error in 4) or 5):
|
|
|
|
// 7) copy files from previous-version dir back to install dir
|
|
|
|
// 8) run the old core client and/or manager
|
|
|
|
|
2006-12-13 16:04:50 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef _WIN32
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2009-02-26 00:23:23 +00:00
|
|
|
#include <cerrno>
|
2006-12-13 16:04:50 +00:00
|
|
|
#endif
|
|
|
|
|
2007-04-09 02:17:26 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2006-12-13 16:04:50 +00:00
|
|
|
#include "filesys.h"
|
2006-12-14 00:42:43 +00:00
|
|
|
#include "util.h"
|
2007-10-09 13:28:00 +00:00
|
|
|
#include "sandbox.h"
|
2007-04-09 02:17:26 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2006-12-13 16:04:50 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define CORE_NAME "boinc.exe"
|
|
|
|
#define MANAGER_NAME "boincmgr.exe"
|
|
|
|
#else
|
2006-12-14 00:42:43 +00:00
|
|
|
#define CORE_NAME "boinc_client"
|
2006-12-13 16:04:50 +00:00
|
|
|
#define MANAGER_NAME "boinc_mgr"
|
|
|
|
#endif
|
|
|
|
|
2006-12-14 20:44:39 +00:00
|
|
|
char* install_dir;
|
2007-04-09 02:17:26 +00:00
|
|
|
char prev_dir[1024];
|
2006-12-14 20:44:39 +00:00
|
|
|
|
2006-12-13 16:04:50 +00:00
|
|
|
int prepare_prev_dir() {
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (is_dir(prev_dir)) {
|
|
|
|
retval = clean_out_dir(prev_dir);
|
|
|
|
if (retval) return retval;
|
|
|
|
} else {
|
|
|
|
retval = boinc_mkdir(prev_dir);
|
|
|
|
if (retval) return retval;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-20 23:16:30 +00:00
|
|
|
int move_file(const char* file, char* old_dir, char* new_dir) {
|
2012-05-09 16:11:50 +00:00
|
|
|
char old_path[MAXPATHLEN], new_path[MAXPATHLEN];
|
2007-04-09 02:17:26 +00:00
|
|
|
sprintf(old_path, "%s/%s", old_dir, file);
|
|
|
|
sprintf(new_path, "%s/%s", new_dir, file);
|
|
|
|
int retval = boinc_rename(old_path, new_path);
|
|
|
|
fprintf(stderr, "rename %s to %s\n", old_path, new_path);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "couldn't rename %s to %s\n", old_path, new_path);
|
|
|
|
}
|
|
|
|
return retval;
|
2006-12-13 16:04:50 +00:00
|
|
|
}
|
|
|
|
|
2007-04-09 02:17:26 +00:00
|
|
|
// try to move all; return 0 if moved all
|
|
|
|
//
|
2007-06-20 23:16:30 +00:00
|
|
|
int move_files(vector<const char*> files, char* old_dir, char* new_dir) {
|
2007-04-09 02:17:26 +00:00
|
|
|
int retval = 0;
|
|
|
|
for (unsigned int i=0; i<files.size(); i++) {
|
|
|
|
int ret = move_file(files[i], old_dir, new_dir);
|
|
|
|
if (ret) retval = ret;
|
|
|
|
}
|
|
|
|
return retval;
|
2006-12-13 16:04:50 +00:00
|
|
|
}
|
|
|
|
|
2007-04-09 02:17:26 +00:00
|
|
|
// NOTE: this program must always (re)start the core client before exiting
|
|
|
|
|
2006-12-13 16:04:50 +00:00
|
|
|
int main(int argc, char** argv) {
|
2006-12-14 00:42:43 +00:00
|
|
|
int i, retval, argc2;
|
2012-05-09 16:11:50 +00:00
|
|
|
char filepath[MAXPATHLEN];
|
2007-04-09 02:17:26 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE core_pid, mgr_pid;
|
|
|
|
#else
|
|
|
|
int core_pid, mgr_pid;
|
|
|
|
#endif
|
2006-12-13 16:04:50 +00:00
|
|
|
bool run_as_service = false;
|
|
|
|
bool run_manager = false;
|
2006-12-13 18:48:12 +00:00
|
|
|
char* argv2[10];
|
2007-04-09 02:17:26 +00:00
|
|
|
char cur_dir[1024];
|
|
|
|
bool new_version_installed = false;
|
2007-06-20 23:16:30 +00:00
|
|
|
vector<const char*> files;
|
2006-12-13 16:04:50 +00:00
|
|
|
|
2006-12-14 20:44:39 +00:00
|
|
|
install_dir = 0;
|
2006-12-13 16:04:50 +00:00
|
|
|
for (i=1; i<argc; i++) {
|
2006-12-14 00:42:43 +00:00
|
|
|
printf("updater: argv[%d] is %s\n", i, argv[i]);
|
2006-12-13 16:04:50 +00:00
|
|
|
if (!strcmp(argv[i], "--run_as_service")) {
|
|
|
|
run_as_service = true;
|
|
|
|
} else if (!strcmp(argv[i], "--run_manager")) {
|
|
|
|
run_manager = true;
|
2006-12-14 20:44:39 +00:00
|
|
|
} else if (!strcmp(argv[i], "--install_dir")) {
|
|
|
|
install_dir = argv[++i];
|
2006-12-13 16:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
2006-12-14 20:44:39 +00:00
|
|
|
if (!install_dir) {
|
|
|
|
fprintf(stderr, "updater: install dir not specified\n");
|
2007-04-09 02:17:26 +00:00
|
|
|
retval = 1;
|
|
|
|
goto restart;
|
2006-12-14 20:44:39 +00:00
|
|
|
}
|
2007-04-09 02:17:26 +00:00
|
|
|
sprintf(prev_dir, "%s/prev_version", install_dir);
|
|
|
|
boinc_getcwd(cur_dir);
|
|
|
|
printf("%s\n", cur_dir);
|
2006-12-13 16:04:50 +00:00
|
|
|
|
2006-12-14 20:44:39 +00:00
|
|
|
wait_client_mutex(install_dir, 30);
|
2006-12-13 16:04:50 +00:00
|
|
|
retval = prepare_prev_dir();
|
2007-04-09 02:17:26 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "couldn't prepare prev_dir %s: %d\n", prev_dir, retval);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
|
|
|
files.push_back(CORE_NAME);
|
|
|
|
files.push_back(MANAGER_NAME);
|
|
|
|
|
|
|
|
// save existing files to "previous version" dir
|
|
|
|
//
|
|
|
|
retval = move_files(files, install_dir, prev_dir);
|
|
|
|
if (retval) {
|
|
|
|
move_files(files, prev_dir, install_dir);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = move_files(files, cur_dir, install_dir);
|
|
|
|
if (retval) {
|
|
|
|
move_files(files, prev_dir, install_dir);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_version_installed = true;
|
|
|
|
retval = 0;
|
|
|
|
|
|
|
|
restart:
|
|
|
|
argv2[0] = CORE_NAME;
|
|
|
|
argv2[1] = "--run_by_updater";
|
|
|
|
argv2[2] = 0;
|
|
|
|
argc2 = 2;
|
2007-06-20 16:27:27 +00:00
|
|
|
sprintf(filepath, "%s/%s", install_dir, CORE_NAME);
|
|
|
|
retval = run_program(install_dir, filepath, argc2, argv2, 5, core_pid);
|
2007-04-09 02:17:26 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "failed to run core client (%d); backing out\n", retval);
|
|
|
|
if (new_version_installed) {
|
|
|
|
retval = move_files(files, prev_dir, install_dir);
|
|
|
|
if (retval) exit(retval); // abandon ship
|
|
|
|
new_version_installed = false;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
exit(retval);
|
2006-12-13 18:48:12 +00:00
|
|
|
}
|
|
|
|
if (run_manager) {
|
|
|
|
argv2[0] = MANAGER_NAME;
|
|
|
|
argv2[1] = 0;
|
2006-12-14 00:42:43 +00:00
|
|
|
argc2 = 1;
|
2007-06-20 16:27:27 +00:00
|
|
|
sprintf(filepath, "%s/%s", install_dir, MANAGER_NAME);
|
|
|
|
retval = run_program(install_dir, filepath, argc2, argv2, 5, mgr_pid);
|
2007-04-09 02:17:26 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "failed to run manager (%d); backing out\n", retval);
|
|
|
|
if (new_version_installed) {
|
|
|
|
kill_program(core_pid);
|
|
|
|
retval = move_files(files, prev_dir, install_dir);
|
|
|
|
if (retval) exit(retval);
|
|
|
|
new_version_installed = false;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
exit(retval);
|
|
|
|
}
|
2006-12-13 18:48:12 +00:00
|
|
|
}
|
2006-12-13 16:04:50 +00:00
|
|
|
}
|