2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2004-08-11 11:30:25 +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.
|
2005-01-20 23:22:22 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +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/>.
|
2004-08-11 11:30:25 +00:00
|
|
|
|
|
|
|
// initialization and starting of applications
|
|
|
|
|
|
|
|
#include "cpp.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "boinc_win.h"
|
2008-02-15 15:52:16 +00:00
|
|
|
#include "win_util.h"
|
2004-08-11 11:30:25 +00:00
|
|
|
#else
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-08-16 11:31:59 +00:00
|
|
|
#if HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
2004-08-11 11:30:25 +00:00
|
|
|
#endif
|
2004-08-11 15:27:46 +00:00
|
|
|
#if HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
2004-08-16 11:31:59 +00:00
|
|
|
#if HAVE_SYS_IPC_H
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#endif
|
2004-08-11 15:27:46 +00:00
|
|
|
#if HAVE_SYS_WAIT_H
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
2004-08-11 11:30:25 +00:00
|
|
|
#include <unistd.h>
|
2004-08-12 13:02:25 +00:00
|
|
|
#include <cerrno>
|
2008-01-18 02:08:25 +00:00
|
|
|
#include <sys/stat.h>
|
2008-07-24 19:05:26 +00:00
|
|
|
#include <string>
|
2004-08-11 11:30:25 +00:00
|
|
|
#endif
|
|
|
|
|
2005-12-02 22:29:35 +00:00
|
|
|
#ifdef __EMX__
|
|
|
|
#include <process.h>
|
|
|
|
#endif
|
|
|
|
|
2007-10-24 00:44:42 +00:00
|
|
|
#if (defined (__APPLE__) && (defined(__i386__) || defined(__x86_64__)))
|
2006-02-06 23:44:05 +00:00
|
|
|
#include <mach-o/loader.h>
|
|
|
|
#include <mach-o/fat.h>
|
|
|
|
#include <mach/machine.h>
|
|
|
|
#include <libkern/OSByteOrder.h>
|
|
|
|
#endif
|
|
|
|
|
2007-09-26 10:16:04 +00:00
|
|
|
#if(!defined (_WIN32) && !defined (__EMX__))
|
2007-08-28 06:12:48 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2008-07-01 21:27:25 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
using std::vector;
|
2008-07-21 16:25:03 +00:00
|
|
|
using std::string;
|
2004-08-11 11:30:25 +00:00
|
|
|
|
|
|
|
#include "filesys.h"
|
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "util.h"
|
2007-02-21 16:26:51 +00:00
|
|
|
#include "str_util.h"
|
2004-08-11 11:30:25 +00:00
|
|
|
#include "shmem.h"
|
|
|
|
#include "client_msgs.h"
|
|
|
|
#include "client_state.h"
|
|
|
|
#include "file_names.h"
|
2008-02-01 22:08:48 +00:00
|
|
|
#include "base64.h"
|
2007-10-05 16:47:07 +00:00
|
|
|
#include "sandbox.h"
|
2009-02-26 00:23:23 +00:00
|
|
|
#include "unix_util.h"
|
|
|
|
|
2008-03-11 03:59:35 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2008-03-10 22:52:43 +00:00
|
|
|
#include "proc_control.h"
|
2008-03-11 03:59:35 +00:00
|
|
|
#endif
|
2004-08-11 11:30:25 +00:00
|
|
|
|
|
|
|
#include "app.h"
|
|
|
|
|
2008-05-02 15:59:37 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2008-05-11 23:11:16 +00:00
|
|
|
// Dynamically link to these functions at runtime;
|
|
|
|
// otherwise BOINC cannot run on Win98
|
2008-05-02 15:59:37 +00:00
|
|
|
|
|
|
|
// CreateEnvironmentBlock
|
|
|
|
typedef BOOL (WINAPI *tCEB)(LPVOID *lpEnvironment, HANDLE hToken, BOOL bInherit);
|
|
|
|
// DestroyEnvironmentBlock
|
|
|
|
typedef BOOL (WINAPI *tDEB)(LPVOID lpEnvironment);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
// Goes through an array of strings, and prints each string
|
|
|
|
//
|
2006-11-20 17:12:55 +00:00
|
|
|
#ifndef _WIN32
|
2006-06-22 19:40:30 +00:00
|
|
|
static void debug_print_argv(char** argv) {
|
2006-09-07 20:39:25 +00:00
|
|
|
msg_printf(0, MSG_INFO, "[task_debug] Arguments:");
|
2008-03-19 16:54:36 +00:00
|
|
|
for (int i=0; argv[i]; i++) {
|
2006-06-22 19:40:30 +00:00
|
|
|
msg_printf(0, MSG_INFO,
|
2006-09-07 20:39:25 +00:00
|
|
|
"[task_debug] argv[%d]: %s\n", i, argv[i]
|
2004-08-11 11:30:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2006-11-20 17:12:55 +00:00
|
|
|
#endif
|
2004-08-11 11:30:25 +00:00
|
|
|
|
2009-03-06 23:10:45 +00:00
|
|
|
// for apps that use coprocessors, reserve the instances,
|
|
|
|
// and append "--device x" to the command line
|
2008-07-21 16:25:03 +00:00
|
|
|
//
|
2009-03-06 23:10:45 +00:00
|
|
|
static void coproc_cmdline(
|
|
|
|
COPROC* coproc, ACTIVE_TASK* atp, int ninstances, char* cmdline
|
|
|
|
) {
|
|
|
|
unsigned int i;
|
|
|
|
int j, k;
|
|
|
|
vector<ACTIVE_TASK*> tasks_using_coproc;
|
|
|
|
|
|
|
|
// make a list of the executing tasks (other than this) using this coproc
|
|
|
|
//
|
|
|
|
for (i=0; i<gstate.active_tasks.active_tasks.size(); i++) {
|
|
|
|
ACTIVE_TASK* p = gstate.active_tasks.active_tasks[i];
|
|
|
|
if (p == atp) continue;
|
|
|
|
if (p->task_state() != PROCESS_EXECUTING) continue;
|
|
|
|
if (p->app_version->coprocs.lookup(coproc->type)) {
|
|
|
|
tasks_using_coproc.push_back(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// scan the coproc's owner array,
|
|
|
|
// clearing any entries not in the above list
|
|
|
|
//
|
|
|
|
for (j=0; j<coproc->count; j++) {
|
|
|
|
if (coproc->owner[j]) {
|
|
|
|
bool found = false;
|
2009-03-09 20:36:22 +00:00
|
|
|
for (i=0; i<tasks_using_coproc.size(); i++) {
|
|
|
|
if (coproc->owner[j] == tasks_using_coproc[i]) {
|
2009-03-06 23:10:45 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
coproc->owner[j] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reserve instances for this job
|
|
|
|
//
|
2008-07-21 16:25:03 +00:00
|
|
|
char buf[256];
|
2009-03-06 23:10:45 +00:00
|
|
|
k = 0;
|
|
|
|
for (j=0; j<ninstances; j++) {
|
|
|
|
while (1) {
|
|
|
|
if (k == coproc->count) {
|
|
|
|
msg_printf(atp->result->project, MSG_INTERNAL_ERROR,
|
|
|
|
"Can't find free %s", coproc->type
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (coproc->owner[k] == NULL) {
|
|
|
|
sprintf(buf, " --device %d", k);
|
|
|
|
strcat(cmdline, buf);
|
|
|
|
coproc->owner[k++] = atp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
k++;
|
2008-07-21 16:25:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-11 23:11:16 +00:00
|
|
|
// Make a unique key for core/app shared memory segment.
|
|
|
|
// Windows: also create and attach to the segment.
|
2004-08-31 21:02:17 +00:00
|
|
|
//
|
|
|
|
int ACTIVE_TASK::get_shmem_seg_name() {
|
|
|
|
#ifdef _WIN32
|
2008-05-11 23:11:16 +00:00
|
|
|
int i;
|
|
|
|
char seg_name[256];
|
2004-08-31 21:02:17 +00:00
|
|
|
|
2008-06-20 21:33:02 +00:00
|
|
|
bool try_global = (sandbox_account_service_token != NULL);
|
2004-08-31 21:02:17 +00:00
|
|
|
for (i=0; i<1024; i++) {
|
2008-05-11 23:11:16 +00:00
|
|
|
sprintf(seg_name, "%sboinc_%d", SHM_PREFIX, i);
|
2008-08-16 20:59:53 +00:00
|
|
|
shm_handle = create_shmem(
|
2008-06-20 21:33:02 +00:00
|
|
|
seg_name, sizeof(SHARED_MEM), (void**)&app_client_shm.shm,
|
|
|
|
try_global
|
2008-05-11 23:11:16 +00:00
|
|
|
);
|
2008-08-16 20:59:53 +00:00
|
|
|
if (shm_handle) break;
|
2004-08-31 21:02:17 +00:00
|
|
|
}
|
2008-08-16 20:59:53 +00:00
|
|
|
if (!shm_handle) return ERR_SHMGET;
|
2008-05-11 23:11:16 +00:00
|
|
|
sprintf(shmem_seg_name, "boinc_%d", i);
|
2004-11-02 23:12:29 +00:00
|
|
|
#else
|
2004-08-31 21:02:17 +00:00
|
|
|
char init_data_path[256];
|
2007-09-26 10:16:04 +00:00
|
|
|
#ifndef __EMX__
|
|
|
|
// shmem_seg_name is not used with mmap() shared memory
|
|
|
|
if (app_version->api_major_version() >= 6) {
|
|
|
|
shmem_seg_name = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2006-03-03 21:34:03 +00:00
|
|
|
sprintf(init_data_path, "%s/%s", slot_dir, INIT_DATA_FILE);
|
2005-06-25 05:26:54 +00:00
|
|
|
|
2007-09-21 15:07:15 +00:00
|
|
|
// ftok() only works if there's a file at the given location
|
|
|
|
//
|
2005-06-25 05:26:54 +00:00
|
|
|
FILE* f = boinc_fopen(init_data_path, "w");
|
|
|
|
if (f) fclose(f);
|
2007-05-23 20:36:49 +00:00
|
|
|
shmem_seg_name = ftok(init_data_path, 1);
|
2005-06-24 08:12:11 +00:00
|
|
|
if (shmem_seg_name == -1) return ERR_SHMEM_NAME;
|
2004-08-31 21:02:17 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
// write the app init file.
|
|
|
|
// This is done before starting the app,
|
|
|
|
// and when project prefs have changed during app execution
|
|
|
|
//
|
2004-08-31 21:02:17 +00:00
|
|
|
int ACTIVE_TASK::write_app_init_file() {
|
|
|
|
APP_INIT_DATA aid;
|
2004-08-11 11:30:25 +00:00
|
|
|
FILE *f;
|
|
|
|
char init_data_path[256], project_dir[256], project_path[256];
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
memset(&aid, 0, sizeof(aid));
|
|
|
|
|
2005-08-31 00:18:36 +00:00
|
|
|
aid.major_version = BOINC_MAJOR_VERSION;
|
|
|
|
aid.minor_version = BOINC_MINOR_VERSION;
|
|
|
|
aid.release = BOINC_RELEASE;
|
2005-10-12 18:40:53 +00:00
|
|
|
aid.app_version = app_version->version_num;
|
2004-08-11 11:30:25 +00:00
|
|
|
safe_strcpy(aid.app_name, wup->app->name);
|
2006-05-22 09:54:31 +00:00
|
|
|
safe_strcpy(aid.symstore, wup->project->symstore);
|
2006-05-28 14:45:29 +00:00
|
|
|
safe_strcpy(aid.acct_mgr_url, gstate.acct_mgr_info.acct_mgr_url);
|
2004-08-11 11:30:25 +00:00
|
|
|
if (wup->project->project_specific_prefs.length()) {
|
2005-02-18 23:43:28 +00:00
|
|
|
aid.project_preferences = strdup(wup->project->project_specific_prefs.c_str());
|
2004-08-11 11:30:25 +00:00
|
|
|
}
|
2009-02-11 23:01:21 +00:00
|
|
|
aid.hostid = wup->project->hostid;
|
|
|
|
safe_strcpy(aid.user_name, wup->project->user_name);
|
|
|
|
safe_strcpy(aid.team_name, wup->project->team_name);
|
2007-03-13 19:33:27 +00:00
|
|
|
get_project_dir(wup->project, project_dir, sizeof(project_dir));
|
2004-08-11 11:30:25 +00:00
|
|
|
relative_to_absolute(project_dir, project_path);
|
|
|
|
strcpy(aid.project_dir, project_path);
|
|
|
|
relative_to_absolute("", aid.boinc_dir);
|
|
|
|
strcpy(aid.authenticator, wup->project->authenticator);
|
|
|
|
aid.slot = slot;
|
|
|
|
strcpy(aid.wu_name, wup->name);
|
|
|
|
aid.user_total_credit = wup->project->user_total_credit;
|
|
|
|
aid.user_expavg_credit = wup->project->user_expavg_credit;
|
|
|
|
aid.host_total_credit = wup->project->host_total_credit;
|
|
|
|
aid.host_expavg_credit = wup->project->host_expavg_credit;
|
2006-06-26 17:09:23 +00:00
|
|
|
double rrs = gstate.runnable_resource_share();
|
|
|
|
if (rrs) {
|
|
|
|
aid.resource_share_fraction = wup->project->resource_share/rrs;
|
|
|
|
} else {
|
|
|
|
aid.resource_share_fraction = 1;
|
|
|
|
}
|
2006-06-09 02:33:00 +00:00
|
|
|
aid.rsc_fpops_est = wup->rsc_fpops_est;
|
|
|
|
aid.rsc_fpops_bound = wup->rsc_fpops_bound;
|
|
|
|
aid.rsc_memory_bound = wup->rsc_memory_bound;
|
|
|
|
aid.rsc_disk_bound = wup->rsc_disk_bound;
|
2008-06-12 19:05:14 +00:00
|
|
|
aid.computation_deadline = result->computation_deadline();
|
2009-03-20 17:58:51 +00:00
|
|
|
int nprocs = (result->avp->ncudas)?coproc_cuda->count:gstate.ncpus;
|
|
|
|
aid.checkpoint_period = nprocs*gstate.global_prefs.disk_interval;
|
2004-08-11 11:30:25 +00:00
|
|
|
aid.fraction_done_start = 0;
|
|
|
|
aid.fraction_done_end = 1;
|
2004-08-31 21:02:17 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
strcpy(aid.shmem_seg_name, shmem_seg_name);
|
|
|
|
#else
|
|
|
|
aid.shmem_seg_name = shmem_seg_name;
|
2004-08-11 11:30:25 +00:00
|
|
|
#endif
|
|
|
|
// wu_cpu_time is the CPU time at start of session,
|
|
|
|
// not the checkpoint CPU time
|
|
|
|
// At the start of an episode these are equal, but not in the middle!
|
|
|
|
//
|
|
|
|
aid.wu_cpu_time = episode_start_cpu_time;
|
|
|
|
|
2006-03-03 21:34:03 +00:00
|
|
|
sprintf(init_data_path, "%s/%s", slot_dir, INIT_DATA_FILE);
|
2004-08-11 11:30:25 +00:00
|
|
|
f = boinc_fopen(init_data_path, "w");
|
|
|
|
if (!f) {
|
2007-01-25 23:39:06 +00:00
|
|
|
msg_printf(wup->project, MSG_INTERNAL_ERROR,
|
2006-01-17 22:48:09 +00:00
|
|
|
"Failed to open init file %s",
|
2004-08-11 11:30:25 +00:00
|
|
|
init_data_path
|
|
|
|
);
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
aid.host_info = gstate.host_info;
|
2004-10-06 19:09:37 +00:00
|
|
|
aid.global_prefs = gstate.global_prefs;
|
2005-02-19 00:14:16 +00:00
|
|
|
aid.proxy_info = gstate.proxy_info;
|
2004-08-11 11:30:25 +00:00
|
|
|
retval = write_init_data_file(f, aid);
|
|
|
|
fclose(f);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-03-27 18:25:29 +00:00
|
|
|
static int make_soft_link(PROJECT* project, char* link_path, char* rel_file_path) {
|
|
|
|
FILE *fp = boinc_fopen(link_path, "w");
|
|
|
|
if (!fp) {
|
|
|
|
msg_printf(project, MSG_INTERNAL_ERROR,
|
|
|
|
"Can't create link file %s", link_path
|
|
|
|
);
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
fprintf(fp, "<soft_link>%s</soft_link>\n", rel_file_path);
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-19 16:54:36 +00:00
|
|
|
// set up a file reference, given a slot dir and project dir.
|
|
|
|
// This means:
|
|
|
|
// 1) copy the file to slot dir, if reference is by copy
|
2008-07-04 19:41:58 +00:00
|
|
|
// 2) else make a soft link
|
2004-08-11 12:15:31 +00:00
|
|
|
//
|
|
|
|
static int setup_file(
|
2008-03-19 16:54:36 +00:00
|
|
|
PROJECT* project, FILE_INFO* fip, FILE_REF& fref,
|
2006-10-11 16:32:59 +00:00
|
|
|
char* file_path, char* slot_dir, bool input
|
2004-08-11 12:15:31 +00:00
|
|
|
) {
|
2008-03-19 18:24:42 +00:00
|
|
|
char link_path[256], rel_file_path[256];
|
2004-08-11 12:15:31 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
sprintf(link_path,
|
2006-03-03 21:34:03 +00:00
|
|
|
"%s/%s",
|
|
|
|
slot_dir, strlen(fref.open_name)?fref.open_name:fip->name
|
2004-08-11 12:15:31 +00:00
|
|
|
);
|
2008-03-19 18:24:42 +00:00
|
|
|
sprintf(rel_file_path, "../../%s", file_path );
|
2006-10-11 16:32:59 +00:00
|
|
|
|
2008-03-19 16:54:36 +00:00
|
|
|
// if anonymous platform, this is called even if not first time,
|
|
|
|
// so link may already be there
|
|
|
|
//
|
|
|
|
if (input && project->anonymous_platform && boinc_file_exists(link_path)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-11 12:15:31 +00:00
|
|
|
if (fref.copy_file) {
|
2006-10-11 16:32:59 +00:00
|
|
|
if (input) {
|
|
|
|
retval = boinc_copy(file_path, link_path);
|
|
|
|
if (retval) {
|
2008-03-19 16:54:36 +00:00
|
|
|
msg_printf(project, MSG_INTERNAL_ERROR,
|
2008-06-26 03:50:03 +00:00
|
|
|
"Can't copy %s to %s: %s", file_path, link_path,
|
|
|
|
boincerror(retval)
|
2006-10-11 16:32:59 +00:00
|
|
|
);
|
|
|
|
return retval;
|
|
|
|
}
|
2009-01-16 06:41:52 +00:00
|
|
|
#ifdef SANDBOX
|
|
|
|
return set_to_project_group(link_path);
|
|
|
|
#endif
|
2004-08-11 12:15:31 +00:00
|
|
|
}
|
2006-10-11 16:32:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-19 16:54:36 +00:00
|
|
|
#ifdef _WIN32
|
2008-03-27 18:25:29 +00:00
|
|
|
retval = make_soft_link(project, link_path, rel_file_path);
|
|
|
|
if (retval) return retval;
|
2008-03-19 16:54:36 +00:00
|
|
|
#else
|
2008-04-14 04:04:45 +00:00
|
|
|
if (project->use_symlinks) {
|
|
|
|
retval = symlink(rel_file_path, link_path);
|
|
|
|
} else {
|
2008-03-27 18:25:29 +00:00
|
|
|
retval = make_soft_link(project, link_path, rel_file_path);
|
2008-03-19 16:54:36 +00:00
|
|
|
}
|
2008-04-14 04:04:45 +00:00
|
|
|
if (retval) return retval;
|
2008-03-19 16:54:36 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SANDBOX
|
|
|
|
return set_to_project_group(link_path);
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ACTIVE_TASK::link_user_files() {
|
|
|
|
PROJECT* project = wup->project;
|
|
|
|
unsigned int i;
|
|
|
|
FILE_REF fref;
|
|
|
|
FILE_INFO* fip;
|
|
|
|
char file_path[1024];
|
|
|
|
|
|
|
|
for (i=0; i<project->user_files.size(); i++) {
|
|
|
|
fref = project->user_files[i];
|
|
|
|
fip = fref.file_info;
|
|
|
|
if (fip->status != FILE_PRESENT) continue;
|
|
|
|
get_pathname(fip, file_path, sizeof(file_path));
|
|
|
|
setup_file(project, fip, fref, file_path, slot_dir, true);
|
2004-08-11 12:15:31 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-06-14 18:04:12 +00:00
|
|
|
int ACTIVE_TASK::copy_output_files() {
|
|
|
|
char slotfile[256], projfile[256];
|
|
|
|
unsigned int i;
|
|
|
|
for (i=0; i<result->output_files.size(); i++) {
|
|
|
|
FILE_REF& fref = result->output_files[i];
|
|
|
|
if (!fref.copy_file) continue;
|
|
|
|
FILE_INFO* fip = fref.file_info;
|
|
|
|
sprintf(slotfile, "%s/%s", slot_dir, fref.open_name);
|
2007-03-13 19:33:27 +00:00
|
|
|
get_pathname(fip, projfile, sizeof(projfile));
|
2006-06-14 18:04:12 +00:00
|
|
|
int retval = boinc_rename(slotfile, projfile);
|
|
|
|
if (retval) {
|
2007-01-25 23:39:06 +00:00
|
|
|
msg_printf(wup->project, MSG_INTERNAL_ERROR,
|
2008-06-26 03:50:03 +00:00
|
|
|
"Can't rename output file %s to %s: %s",
|
|
|
|
fip->name, projfile, boincerror(retval)
|
2006-06-14 18:04:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
// Start a task in a slot directory.
|
|
|
|
// This includes setting up soft links,
|
|
|
|
// passing preferences, and starting the process
|
|
|
|
//
|
|
|
|
// Current dir is top-level BOINC dir
|
|
|
|
//
|
2006-06-20 19:34:42 +00:00
|
|
|
// postcondition:
|
|
|
|
// If any error occurs
|
|
|
|
// ACTIVE_TASK::task_state is PROCESS_COULDNT_START
|
|
|
|
// report_result_error() is called
|
|
|
|
// else
|
|
|
|
// ACTIVE_TASK::task_state is PROCESS_EXECUTING
|
2004-10-07 19:18:37 +00:00
|
|
|
//
|
2004-08-11 11:30:25 +00:00
|
|
|
int ACTIVE_TASK::start(bool first_time) {
|
2004-08-11 12:15:31 +00:00
|
|
|
char exec_name[256], file_path[256], buf[256], exec_path[256];
|
2008-07-21 16:25:03 +00:00
|
|
|
char cmdline[8192];
|
2004-08-11 11:30:25 +00:00
|
|
|
unsigned int i;
|
2004-08-11 12:15:31 +00:00
|
|
|
FILE_REF fref;
|
2004-08-11 11:30:25 +00:00
|
|
|
FILE_INFO* fip;
|
|
|
|
int retval;
|
2008-07-21 19:56:01 +00:00
|
|
|
|
2009-01-22 19:51:04 +00:00
|
|
|
// if this job less than one CPU, run it at above idle priority
|
2008-09-26 22:56:42 +00:00
|
|
|
//
|
2009-01-22 19:51:04 +00:00
|
|
|
bool high_priority = (app_version->avg_ncpus < 1);
|
2008-09-26 22:56:42 +00:00
|
|
|
|
2006-08-02 16:57:09 +00:00
|
|
|
if (first_time && log_flags.task) {
|
2009-02-23 04:54:04 +00:00
|
|
|
msg_printf(wup->project, MSG_INFO,
|
2006-08-02 16:57:09 +00:00
|
|
|
"Starting %s", result->name
|
|
|
|
);
|
|
|
|
}
|
2006-09-07 17:59:34 +00:00
|
|
|
if (log_flags.cpu_sched) {
|
2009-02-23 04:54:04 +00:00
|
|
|
msg_printf(wup->project, MSG_INFO,
|
2006-09-07 20:39:25 +00:00
|
|
|
"[cpu_sched] Starting %s%s", result->name, first_time?" (initial)":"(resume)"
|
2006-06-22 19:40:30 +00:00
|
|
|
);
|
|
|
|
}
|
2004-08-11 11:30:25 +00:00
|
|
|
|
2006-06-20 17:36:28 +00:00
|
|
|
if (wup->project->verify_files_on_app_start) {
|
2008-02-03 21:46:30 +00:00
|
|
|
fip=0;
|
2007-11-07 19:32:32 +00:00
|
|
|
retval = gstate.input_files_available(result, true, &fip);
|
2006-06-20 19:34:42 +00:00
|
|
|
if (retval) {
|
2007-11-07 19:32:32 +00:00
|
|
|
if (fip) {
|
|
|
|
snprintf(
|
|
|
|
buf, sizeof(buf), "Input file %s missing or invalid: %d", fip->name, retval
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
strcpy(buf, "Input file missing or invalid");
|
|
|
|
}
|
2006-06-20 19:34:42 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2006-06-20 17:36:28 +00:00
|
|
|
}
|
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
current_cpu_time = checkpoint_cpu_time;
|
|
|
|
episode_start_cpu_time = checkpoint_cpu_time;
|
|
|
|
|
2007-09-21 15:07:15 +00:00
|
|
|
graphics_request_queue.init(result->name); // reset message queues
|
|
|
|
process_control_queue.init(result->name);
|
2006-12-09 00:10:53 +00:00
|
|
|
|
2004-08-31 21:02:17 +00:00
|
|
|
if (!app_client_shm.shm) {
|
|
|
|
retval = get_shmem_seg_name();
|
|
|
|
if (retval) {
|
2006-06-20 19:34:42 +00:00
|
|
|
sprintf(buf,
|
|
|
|
"Can't get shared memory segment name: %s",
|
|
|
|
boincerror(retval)
|
2004-08-31 21:02:17 +00:00
|
|
|
);
|
2006-06-20 19:34:42 +00:00
|
|
|
goto error;
|
2004-08-31 21:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-01 04:59:32 +00:00
|
|
|
|
2006-12-09 00:10:53 +00:00
|
|
|
// this must go AFTER creating shmem name,
|
2005-06-25 05:26:54 +00:00
|
|
|
// since the shmem name is part of the file
|
|
|
|
//
|
|
|
|
retval = write_app_init_file();
|
2006-06-20 19:34:42 +00:00
|
|
|
if (retval) {
|
2008-04-06 03:15:36 +00:00
|
|
|
sprintf(buf, "Can't write init file: %d", retval);
|
2006-06-20 19:34:42 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2005-06-25 05:26:54 +00:00
|
|
|
|
2004-08-11 12:15:31 +00:00
|
|
|
// set up applications files
|
2004-08-11 11:30:25 +00:00
|
|
|
//
|
2005-02-11 18:26:59 +00:00
|
|
|
strcpy(exec_name, "");
|
2004-08-11 11:30:25 +00:00
|
|
|
for (i=0; i<app_version->app_files.size(); i++) {
|
2005-02-18 23:50:38 +00:00
|
|
|
fref = app_version->app_files[i];
|
2004-08-11 11:30:25 +00:00
|
|
|
fip = fref.file_info;
|
2007-03-13 19:33:27 +00:00
|
|
|
get_pathname(fip, file_path, sizeof(file_path));
|
2004-08-11 11:30:25 +00:00
|
|
|
if (fref.main_program) {
|
2005-04-06 19:41:31 +00:00
|
|
|
if (is_image_file(fip->name)) {
|
2006-06-20 19:34:42 +00:00
|
|
|
sprintf(buf, "Main program %s is an image file", fip->name);
|
|
|
|
retval = ERR_NO_SIGNATURE;
|
|
|
|
goto error;
|
2005-04-06 19:41:31 +00:00
|
|
|
}
|
2005-03-08 00:23:58 +00:00
|
|
|
if (!fip->executable && !wup->project->anonymous_platform) {
|
2006-06-20 19:34:42 +00:00
|
|
|
sprintf(buf, "Main program %s is not executable", fip->name);
|
|
|
|
retval = ERR_NO_SIGNATURE;
|
|
|
|
goto error;
|
2005-02-11 18:26:59 +00:00
|
|
|
}
|
2004-08-11 11:30:25 +00:00
|
|
|
safe_strcpy(exec_name, fip->name);
|
|
|
|
safe_strcpy(exec_path, file_path);
|
|
|
|
}
|
2005-09-08 17:13:31 +00:00
|
|
|
// anonymous platform may use different files than
|
2008-03-19 16:54:36 +00:00
|
|
|
// when the result was started, so link files even if not first time
|
2005-09-08 17:13:31 +00:00
|
|
|
//
|
2005-09-06 12:58:56 +00:00
|
|
|
if (first_time || wup->project->anonymous_platform) {
|
2008-03-19 16:54:36 +00:00
|
|
|
retval = setup_file(result->project, fip, fref, file_path, slot_dir, true);
|
2006-06-20 19:34:42 +00:00
|
|
|
if (retval) {
|
|
|
|
strcpy(buf, "Can't link input file");
|
|
|
|
goto error;
|
|
|
|
}
|
2004-08-11 11:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
2005-02-11 18:26:59 +00:00
|
|
|
if (!strlen(exec_name)) {
|
2006-06-20 19:34:42 +00:00
|
|
|
strcpy(buf, "No main program specified");
|
|
|
|
retval = ERR_NOT_FOUND;
|
|
|
|
goto error;
|
2005-02-11 18:26:59 +00:00
|
|
|
}
|
2004-08-11 11:30:25 +00:00
|
|
|
|
2006-04-28 17:23:00 +00:00
|
|
|
// set up input, output files
|
2004-08-11 11:30:25 +00:00
|
|
|
//
|
2006-04-28 17:23:00 +00:00
|
|
|
if (first_time) {
|
|
|
|
for (i=0; i<wup->input_files.size(); i++) {
|
|
|
|
fref = wup->input_files[i];
|
|
|
|
fip = fref.file_info;
|
2007-03-13 19:33:27 +00:00
|
|
|
get_pathname(fref.file_info, file_path, sizeof(file_path));
|
2008-03-19 16:54:36 +00:00
|
|
|
retval = setup_file(result->project, fip, fref, file_path, slot_dir, true);
|
2006-06-20 19:34:42 +00:00
|
|
|
if (retval) {
|
|
|
|
strcpy(buf, "Can't link input file");
|
|
|
|
goto error;
|
|
|
|
}
|
2004-08-11 11:30:25 +00:00
|
|
|
}
|
2006-04-28 17:23:00 +00:00
|
|
|
for (i=0; i<result->output_files.size(); i++) {
|
|
|
|
fref = result->output_files[i];
|
2006-06-14 18:04:12 +00:00
|
|
|
if (fref.copy_file) continue;
|
2006-04-28 17:23:00 +00:00
|
|
|
fip = fref.file_info;
|
2007-03-13 19:33:27 +00:00
|
|
|
get_pathname(fref.file_info, file_path, sizeof(file_path));
|
2008-03-19 16:54:36 +00:00
|
|
|
retval = setup_file(result->project, fip, fref, file_path, slot_dir, false);
|
2006-06-20 19:34:42 +00:00
|
|
|
if (retval) {
|
|
|
|
strcpy(buf, "Can't link output file");
|
|
|
|
goto error;
|
|
|
|
}
|
2004-08-11 11:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
link_user_files();
|
|
|
|
|
2007-05-29 21:45:01 +00:00
|
|
|
if (gstate.exit_before_start) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
PROCESS_INFORMATION process_info;
|
|
|
|
STARTUPINFO startup_info;
|
2008-02-15 15:52:16 +00:00
|
|
|
LPVOID environment_block = NULL;
|
2004-08-11 11:30:25 +00:00
|
|
|
char slotdirpath[256];
|
2005-02-17 21:18:17 +00:00
|
|
|
char error_msg[1024];
|
2008-05-20 21:47:02 +00:00
|
|
|
char error_msg2[1024];
|
2004-08-11 11:30:25 +00:00
|
|
|
|
|
|
|
memset(&process_info, 0, sizeof(process_info));
|
|
|
|
memset(&startup_info, 0, sizeof(startup_info));
|
2007-05-15 20:29:26 +00:00
|
|
|
startup_info.cb = sizeof(startup_info);
|
2008-02-15 15:52:16 +00:00
|
|
|
|
2007-09-21 15:07:15 +00:00
|
|
|
// suppress 2-sec rotating hourglass cursor on startup
|
2008-02-15 15:52:16 +00:00
|
|
|
//
|
|
|
|
startup_info.dwFlags = STARTF_FORCEOFFFEEDBACK;
|
2007-05-15 20:29:26 +00:00
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
app_client_shm.reset_msgs();
|
|
|
|
|
2008-01-13 04:16:58 +00:00
|
|
|
if (config.run_apps_manually) {
|
2008-08-16 20:59:53 +00:00
|
|
|
// fill in core client's PID so we won't think app has exited
|
|
|
|
//
|
2008-01-13 04:16:58 +00:00
|
|
|
pid = GetCurrentProcessId();
|
|
|
|
pid_handle = GetCurrentProcess();
|
|
|
|
set_task_state(PROCESS_EXECUTING, "start");
|
|
|
|
return 0;
|
|
|
|
}
|
2004-08-11 11:30:25 +00:00
|
|
|
|
2008-07-21 16:25:03 +00:00
|
|
|
sprintf(cmdline, "%s %s %s",
|
2008-07-21 19:56:01 +00:00
|
|
|
exec_path, wup->command_line.c_str(), app_version->cmdline
|
2008-07-21 16:25:03 +00:00
|
|
|
);
|
2009-03-06 23:10:45 +00:00
|
|
|
if (coproc_cuda && app_version->ncudas) {
|
|
|
|
coproc_cmdline(coproc_cuda, this, app_version->ncudas, cmdline);
|
|
|
|
}
|
2008-07-21 16:25:03 +00:00
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
relative_to_absolute(slot_dir, slotdirpath);
|
2005-02-17 21:18:17 +00:00
|
|
|
bool success = false;
|
2008-11-19 20:20:04 +00:00
|
|
|
int prio_mask = high_priority?BELOW_NORMAL_PRIORITY_CLASS:IDLE_PRIORITY_CLASS;
|
2008-03-06 17:18:59 +00:00
|
|
|
|
2005-02-17 21:18:17 +00:00
|
|
|
for (i=0; i<5; i++) {
|
2008-03-13 21:18:04 +00:00
|
|
|
if (sandbox_account_service_token != NULL) {
|
2008-05-02 15:59:37 +00:00
|
|
|
// Find CreateEnvironmentBlock/DestroyEnvironmentBlock pointers
|
|
|
|
tCEB pCEB = NULL;
|
|
|
|
tDEB pDEB = NULL;
|
|
|
|
HMODULE hUserEnvLib = NULL;
|
|
|
|
|
|
|
|
hUserEnvLib = LoadLibrary("userenv.dll");
|
|
|
|
if (hUserEnvLib) {
|
|
|
|
pCEB = (tCEB) GetProcAddress(hUserEnvLib, "CreateEnvironmentBlock");
|
|
|
|
pDEB = (tDEB) GetProcAddress(hUserEnvLib, "DestroyEnvironmentBlock");
|
|
|
|
}
|
2008-02-15 15:52:16 +00:00
|
|
|
|
2008-05-02 15:59:37 +00:00
|
|
|
if (!pCEB(&environment_block, sandbox_account_service_token, FALSE)) {
|
2008-02-15 15:52:16 +00:00
|
|
|
if (log_flags.task) {
|
|
|
|
windows_error_string(error_msg, sizeof(error_msg));
|
2009-02-23 04:54:04 +00:00
|
|
|
msg_printf(wup->project, MSG_INFO,
|
2008-02-15 15:52:16 +00:00
|
|
|
"Process environment block creation failed: %s", error_msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-01 22:08:48 +00:00
|
|
|
if (CreateProcessAsUser(
|
2008-03-13 21:18:04 +00:00
|
|
|
sandbox_account_service_token,
|
2008-02-01 22:08:48 +00:00
|
|
|
exec_path,
|
2008-07-21 16:25:03 +00:00
|
|
|
cmdline,
|
2008-02-01 22:08:48 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
FALSE,
|
2008-09-26 22:56:42 +00:00
|
|
|
CREATE_NEW_PROCESS_GROUP|CREATE_NO_WINDOW|prio_mask|CREATE_UNICODE_ENVIRONMENT,
|
2008-02-15 15:52:16 +00:00
|
|
|
environment_block,
|
2008-02-01 22:08:48 +00:00
|
|
|
slotdirpath,
|
|
|
|
&startup_info,
|
|
|
|
&process_info
|
|
|
|
)) {
|
|
|
|
success = true;
|
|
|
|
break;
|
2008-02-15 15:52:16 +00:00
|
|
|
} else {
|
|
|
|
windows_error_string(error_msg, sizeof(error_msg));
|
|
|
|
msg_printf(wup->project, MSG_INTERNAL_ERROR,
|
|
|
|
"Process creation failed: %s", error_msg
|
|
|
|
);
|
2008-02-01 22:08:48 +00:00
|
|
|
}
|
2008-02-15 15:52:16 +00:00
|
|
|
|
2008-05-02 15:59:37 +00:00
|
|
|
if (!pDEB(environment_block)) {
|
2008-02-15 15:52:16 +00:00
|
|
|
if (log_flags.task) {
|
2008-05-20 21:47:02 +00:00
|
|
|
windows_error_string(error_msg, sizeof(error_msg2));
|
2009-02-23 04:54:04 +00:00
|
|
|
msg_printf(wup->project, MSG_INFO,
|
2008-05-20 21:47:02 +00:00
|
|
|
"Process environment block cleanup failed: %s",
|
|
|
|
error_msg2
|
2008-02-15 15:52:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-02 15:59:37 +00:00
|
|
|
if (hUserEnvLib) {
|
|
|
|
pCEB = NULL;
|
|
|
|
pDEB = NULL;
|
|
|
|
FreeLibrary(hUserEnvLib);
|
|
|
|
}
|
|
|
|
|
2008-02-01 22:08:48 +00:00
|
|
|
} else {
|
|
|
|
if (CreateProcess(
|
|
|
|
exec_path,
|
2008-07-21 16:25:03 +00:00
|
|
|
cmdline,
|
2008-02-01 22:08:48 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
FALSE,
|
2008-09-26 22:56:42 +00:00
|
|
|
CREATE_NEW_PROCESS_GROUP|CREATE_NO_WINDOW|prio_mask,
|
2008-02-01 22:08:48 +00:00
|
|
|
NULL,
|
|
|
|
slotdirpath,
|
|
|
|
&startup_info,
|
|
|
|
&process_info
|
|
|
|
)) {
|
|
|
|
success = true;
|
|
|
|
break;
|
2008-02-15 15:52:16 +00:00
|
|
|
} else {
|
|
|
|
windows_error_string(error_msg, sizeof(error_msg));
|
|
|
|
msg_printf(wup->project, MSG_INTERNAL_ERROR,
|
|
|
|
"Process creation failed: %s", error_msg
|
|
|
|
);
|
2008-02-01 22:08:48 +00:00
|
|
|
}
|
2005-02-17 21:18:17 +00:00
|
|
|
}
|
|
|
|
boinc_sleep(drand());
|
|
|
|
}
|
2008-03-06 17:18:59 +00:00
|
|
|
|
2005-02-17 21:18:17 +00:00
|
|
|
if (!success) {
|
2006-06-20 19:34:42 +00:00
|
|
|
sprintf(buf, "CreateProcess() failed - %s", error_msg);
|
|
|
|
retval = ERR_EXEC;
|
|
|
|
goto error;
|
2004-08-11 11:30:25 +00:00
|
|
|
}
|
|
|
|
pid = process_info.dwProcessId;
|
|
|
|
pid_handle = process_info.hProcess;
|
2008-12-24 23:19:40 +00:00
|
|
|
CloseHandle(process_info.hThread); // thread handle is not used
|
2005-12-02 22:29:35 +00:00
|
|
|
#elif defined(__EMX__)
|
|
|
|
|
2007-09-21 15:07:15 +00:00
|
|
|
char* argv[100];
|
|
|
|
char current_dir[_MAX_PATH];
|
2005-12-02 22:29:35 +00:00
|
|
|
|
|
|
|
// Set up core/app shared memory seg if needed
|
|
|
|
//
|
|
|
|
if (!app_client_shm.shm) {
|
|
|
|
retval = create_shmem(
|
|
|
|
shmem_seg_name, sizeof(SHARED_MEM), (void**)&app_client_shm.shm
|
|
|
|
);
|
|
|
|
if (retval) {
|
2007-06-25 11:46:15 +00:00
|
|
|
return retval;
|
2005-12-02 22:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
app_client_shm.reset_msgs();
|
|
|
|
|
2007-09-21 15:07:15 +00:00
|
|
|
// save current dir
|
2008-07-10 21:57:18 +00:00
|
|
|
getcwd(current_dir, sizeof(current_dir));
|
2007-09-21 15:07:15 +00:00
|
|
|
|
|
|
|
// chdir() into the slot directory
|
|
|
|
//
|
|
|
|
retval = chdir(slot_dir);
|
|
|
|
if (retval) {
|
|
|
|
sprintf(buf, "Can't change directory: %s", slot_dir, boincerror(retval));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hook up stderr to a specially-named file
|
|
|
|
//
|
|
|
|
//freopen(STDERR_FILE, "a", stderr);
|
|
|
|
|
|
|
|
argv[0] = exec_name;
|
|
|
|
char cmdline[8192];
|
|
|
|
strcpy(cmdline, wup->command_line.c_str());
|
2008-03-27 18:25:29 +00:00
|
|
|
if (strlen(result->cmdline)) {
|
|
|
|
strcat(cmdline, " ");
|
|
|
|
strcat(cmdline, result->cmdline);
|
|
|
|
}
|
2007-09-21 15:07:15 +00:00
|
|
|
parse_command_line(cmdline, argv+1);
|
2006-06-22 19:40:30 +00:00
|
|
|
if (log_flags.task_debug) {
|
|
|
|
debug_print_argv(argv);
|
|
|
|
}
|
2007-09-21 15:07:15 +00:00
|
|
|
sprintf(buf, "../../%s", exec_path );
|
|
|
|
pid = spawnv(P_NOWAIT, buf, argv);
|
|
|
|
if (pid == -1) {
|
|
|
|
sprintf(buf, "Process creation failed: %s\n", buf, boincerror(retval));
|
|
|
|
chdir(current_dir);
|
2006-06-20 19:34:42 +00:00
|
|
|
retval = ERR_EXEC;
|
|
|
|
goto error;
|
2007-09-21 15:07:15 +00:00
|
|
|
}
|
2005-12-02 22:29:35 +00:00
|
|
|
|
2007-09-21 15:07:15 +00:00
|
|
|
// restore current dir
|
|
|
|
chdir(current_dir);
|
2006-06-22 19:40:30 +00:00
|
|
|
|
|
|
|
if (log_flags.task_debug) {
|
2009-02-23 04:54:04 +00:00
|
|
|
msg_printf(wup->project, MSG_INFO,
|
2006-09-07 20:39:25 +00:00
|
|
|
"[task_debug] ACTIVE_TASK::start(): forked process: pid %d\n", pid
|
2006-06-22 19:40:30 +00:00
|
|
|
);
|
|
|
|
}
|
2005-12-02 22:29:35 +00:00
|
|
|
|
2008-11-14 22:08:50 +00:00
|
|
|
if (setpriority(PRIO_PROCESS, pid,
|
|
|
|
high_priority?PROCESS_MEDIUM_PRIORITY:PROCESS_IDLE_PRIORITY)
|
|
|
|
) {
|
|
|
|
perror("setpriority");
|
2005-12-02 22:29:35 +00:00
|
|
|
}
|
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
#else
|
2006-11-10 17:55:22 +00:00
|
|
|
// Unix/Linux/Mac case
|
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
char* argv[100];
|
2008-07-10 21:57:18 +00:00
|
|
|
char current_dir[1024];
|
|
|
|
|
|
|
|
getcwd(current_dir, sizeof(current_dir));
|
2004-08-11 11:30:25 +00:00
|
|
|
|
2004-08-31 21:02:17 +00:00
|
|
|
// Set up core/app shared memory seg if needed
|
2004-08-11 11:30:25 +00:00
|
|
|
//
|
2004-08-31 21:02:17 +00:00
|
|
|
if (!app_client_shm.shm) {
|
2007-09-26 10:16:04 +00:00
|
|
|
if (app_version->api_major_version() >= 6) {
|
|
|
|
// Use mmap() shared memory
|
|
|
|
sprintf(buf, "%s/%s", slot_dir, MMAPPED_FILE_NAME);
|
|
|
|
if (g_use_sandbox) {
|
|
|
|
if (!boinc_file_exists(buf)) {
|
|
|
|
int fd = open(buf, O_RDWR | O_CREAT, 0660);
|
|
|
|
if (fd >= 0) {
|
|
|
|
close (fd);
|
2007-10-05 16:47:07 +00:00
|
|
|
#ifdef SANDBOX
|
2007-09-26 10:16:04 +00:00
|
|
|
set_to_project_group(buf);
|
2007-10-05 16:47:07 +00:00
|
|
|
#endif
|
2007-09-26 10:16:04 +00:00
|
|
|
}
|
2007-08-28 06:12:48 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-26 10:16:04 +00:00
|
|
|
retval = create_shmem_mmap(
|
|
|
|
buf, sizeof(SHARED_MEM), (void**)&app_client_shm.shm
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Use shmget() shared memory
|
|
|
|
retval = create_shmem(
|
|
|
|
shmem_seg_name, sizeof(SHARED_MEM), gstate.boinc_project_gid,
|
|
|
|
(void**)&app_client_shm.shm
|
|
|
|
);
|
|
|
|
|
|
|
|
if (retval) {
|
|
|
|
needs_shmem = true;
|
2008-01-13 00:12:14 +00:00
|
|
|
destroy_shmem(shmem_seg_name);
|
2007-09-26 10:16:04 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2004-08-31 21:02:17 +00:00
|
|
|
}
|
2007-06-25 11:46:15 +00:00
|
|
|
needs_shmem = false;
|
2004-08-11 11:30:25 +00:00
|
|
|
}
|
|
|
|
app_client_shm.reset_msgs();
|
|
|
|
|
2007-10-24 00:44:42 +00:00
|
|
|
#if (defined (__APPLE__) && (defined(__i386__) || defined(__x86_64__)))
|
2006-02-06 23:44:05 +00:00
|
|
|
// PowerPC apps emulated on i386 Macs crash if running graphics
|
|
|
|
powerpc_emulated_on_i386 = ! is_native_i386_app(exec_path);
|
|
|
|
#endif
|
2008-01-13 00:12:14 +00:00
|
|
|
if (config.run_apps_manually) {
|
|
|
|
pid = getpid(); // use the client's PID
|
|
|
|
set_task_state(PROCESS_EXECUTING, "start");
|
|
|
|
return 0;
|
|
|
|
}
|
2004-08-11 11:30:25 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid == -1) {
|
2006-06-20 19:34:42 +00:00
|
|
|
sprintf(buf, "fork() failed: %s", strerror(errno));
|
|
|
|
retval = ERR_FORK;
|
|
|
|
goto error;
|
2004-08-11 11:30:25 +00:00
|
|
|
}
|
|
|
|
if (pid == 0) {
|
|
|
|
// from here on we're running in a new process.
|
2006-10-11 16:32:59 +00:00
|
|
|
// If an error happens,
|
|
|
|
// exit nonzero so that the core client knows there was a problem.
|
2004-08-11 11:30:25 +00:00
|
|
|
|
2008-05-09 17:05:26 +00:00
|
|
|
// don't pass stdout to the app
|
2008-05-09 16:27:20 +00:00
|
|
|
//
|
2008-05-09 17:05:26 +00:00
|
|
|
int fd = open("/dev/null", O_RDWR);
|
2008-05-13 21:46:50 +00:00
|
|
|
dup2(fd, STDOUT_FILENO);
|
2008-05-09 17:05:26 +00:00
|
|
|
close(fd);
|
2008-05-09 16:27:20 +00:00
|
|
|
|
2008-08-01 22:50:07 +00:00
|
|
|
// add to library path:
|
|
|
|
// - the project dir (../../projects/X)
|
|
|
|
// - the slot dir (.)
|
|
|
|
// - the BOINC dir (../..)
|
|
|
|
// We use relative paths in case higher-level dirs
|
|
|
|
// are not readable to the account under which app runs
|
2004-08-11 11:30:25 +00:00
|
|
|
//
|
2006-10-11 16:32:59 +00:00
|
|
|
char libpath[8192];
|
2007-03-13 19:33:27 +00:00
|
|
|
get_project_dir(wup->project, buf, sizeof(buf));
|
2008-08-28 18:41:18 +00:00
|
|
|
char* p = getenv("LD_LIBRARY_PATH");
|
|
|
|
if (p) {
|
|
|
|
sprintf(libpath, "%s:../../%s:.:../..", p, buf);
|
|
|
|
} else {
|
|
|
|
sprintf(libpath, "../../%s:.:../..", buf);
|
|
|
|
}
|
2006-10-11 16:32:59 +00:00
|
|
|
setenv("LD_LIBRARY_PATH", libpath, 1);
|
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
retval = chdir(slot_dir);
|
|
|
|
if (retval) {
|
|
|
|
perror("chdir");
|
2005-10-05 20:39:00 +00:00
|
|
|
fflush(NULL);
|
2007-04-05 23:15:20 +00:00
|
|
|
_exit(errno);
|
2004-08-11 11:30:25 +00:00
|
|
|
}
|
|
|
|
|
2007-02-02 17:12:07 +00:00
|
|
|
#if 0
|
2007-01-29 22:57:10 +00:00
|
|
|
// set stack size limit to the max.
|
|
|
|
// Some BOINC apps have reported problems with exceeding
|
|
|
|
// small stack limits (e.g. 8 MB)
|
|
|
|
// and it seems like the best thing to raise it as high as possible
|
|
|
|
//
|
|
|
|
struct rlimit rlim;
|
2007-01-31 21:55:12 +00:00
|
|
|
#define MIN_STACK_LIMIT 64000000
|
2007-01-29 22:57:10 +00:00
|
|
|
getrlimit(RLIMIT_STACK, &rlim);
|
2007-01-31 21:55:12 +00:00
|
|
|
if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur <= MIN_STACK_LIMIT) {
|
|
|
|
if (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_max > MIN_STACK_LIMIT) {
|
|
|
|
rlim.rlim_cur = MIN_STACK_LIMIT;
|
|
|
|
} else {
|
|
|
|
rlim.rlim_cur = rlim.rlim_max;
|
|
|
|
}
|
|
|
|
setrlimit(RLIMIT_STACK, &rlim);
|
2007-01-30 18:31:07 +00:00
|
|
|
}
|
2007-02-02 17:12:07 +00:00
|
|
|
#endif
|
2007-01-29 22:57:10 +00:00
|
|
|
|
2004-08-11 11:30:25 +00:00
|
|
|
// hook up stderr to a specially-named file
|
|
|
|
//
|
|
|
|
freopen(STDERR_FILE, "a", stderr);
|
|
|
|
|
2006-04-14 17:55:02 +00:00
|
|
|
#ifdef HAVE_SETPRIORITY
|
2008-11-14 22:08:50 +00:00
|
|
|
if (setpriority(PRIO_PROCESS, 0,
|
|
|
|
high_priority?PROCESS_MEDIUM_PRIORITY:PROCESS_IDLE_PRIORITY)
|
|
|
|
) {
|
|
|
|
perror("setpriority");
|
2006-04-14 17:55:02 +00:00
|
|
|
}
|
|
|
|
#endif
|
2008-07-21 16:25:03 +00:00
|
|
|
sprintf(cmdline, "%s %s", wup->command_line.c_str(), app_version->cmdline);
|
2009-03-06 23:10:45 +00:00
|
|
|
if (coproc_cuda && app_version->ncudas) {
|
|
|
|
coproc_cmdline(coproc_cuda, this, app_version->ncudas, cmdline);
|
|
|
|
}
|
2006-03-03 21:34:03 +00:00
|
|
|
sprintf(buf, "../../%s", exec_path );
|
2006-08-01 12:36:19 +00:00
|
|
|
if (g_use_sandbox) {
|
|
|
|
char switcher_path[100];
|
|
|
|
sprintf(switcher_path, "../../%s/%s", SWITCHER_DIR, SWITCHER_FILE_NAME);
|
|
|
|
argv[0] = SWITCHER_FILE_NAME;
|
|
|
|
argv[1] = buf;
|
|
|
|
argv[2] = exec_name;
|
|
|
|
parse_command_line(cmdline, argv+3);
|
|
|
|
if (log_flags.task_debug) {
|
|
|
|
debug_print_argv(argv);
|
|
|
|
}
|
2008-01-18 02:08:25 +00:00
|
|
|
// Files written by projects have user boinc_project and group boinc_project,
|
|
|
|
// so they must be world-readable so BOINC CLient can read them
|
|
|
|
umask(2);
|
2006-08-01 12:36:19 +00:00
|
|
|
retval = execv(switcher_path, argv);
|
|
|
|
} else {
|
|
|
|
argv[0] = exec_name;
|
|
|
|
parse_command_line(cmdline, argv+1);
|
|
|
|
retval = execv(buf, argv);
|
2006-06-23 20:05:12 +00:00
|
|
|
}
|
2007-01-25 23:39:06 +00:00
|
|
|
msg_printf(wup->project, MSG_INTERNAL_ERROR,
|
2006-07-01 05:32:02 +00:00
|
|
|
"Process creation (%s) failed: %s, errno=%d\n", buf, boincerror(retval), errno
|
2004-08-11 11:30:25 +00:00
|
|
|
);
|
|
|
|
perror("execv");
|
2005-10-05 20:39:00 +00:00
|
|
|
fflush(NULL);
|
2004-08-11 11:30:25 +00:00
|
|
|
_exit(errno);
|
|
|
|
}
|
|
|
|
|
2006-06-22 19:40:30 +00:00
|
|
|
if (log_flags.task_debug) {
|
2009-02-23 04:54:04 +00:00
|
|
|
msg_printf(wup->project, MSG_INFO,
|
2006-09-07 20:39:25 +00:00
|
|
|
"[task_debug] ACTIVE_TASK::start(): forked process: pid %d\n", pid
|
2006-06-22 19:40:30 +00:00
|
|
|
);
|
|
|
|
}
|
2004-08-11 11:30:25 +00:00
|
|
|
|
|
|
|
#endif
|
2007-01-24 21:20:57 +00:00
|
|
|
set_task_state(PROCESS_EXECUTING, "start");
|
2004-08-11 11:30:25 +00:00
|
|
|
return 0;
|
2006-06-20 19:34:42 +00:00
|
|
|
|
|
|
|
// go here on error; "buf" contains error message, "retval" is nonzero
|
|
|
|
//
|
|
|
|
error:
|
2008-07-21 19:56:01 +00:00
|
|
|
|
2007-09-21 15:07:15 +00:00
|
|
|
// if something failed, it's possible that the executable was munged.
|
|
|
|
// Verify it to trigger another download.
|
|
|
|
//
|
|
|
|
gstate.input_files_available(result, true);
|
2006-06-20 19:34:42 +00:00
|
|
|
gstate.report_result_error(*result, buf);
|
2007-01-24 21:20:57 +00:00
|
|
|
set_task_state(PROCESS_COULDNT_START, "start");
|
2006-06-20 19:34:42 +00:00
|
|
|
return retval;
|
2004-08-11 11:30:25 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:18:37 +00:00
|
|
|
// Resume the task if it was previously running; otherwise start it
|
|
|
|
// Postcondition: "state" is set correctly
|
2004-08-11 11:30:25 +00:00
|
|
|
//
|
2007-02-02 17:12:07 +00:00
|
|
|
int ACTIVE_TASK::resume_or_start(bool first_time) {
|
2005-02-18 23:50:38 +00:00
|
|
|
const char* str = "??";
|
2004-08-11 11:30:25 +00:00
|
|
|
int retval;
|
|
|
|
|
2007-01-24 21:20:57 +00:00
|
|
|
switch (task_state()) {
|
2004-08-23 22:06:48 +00:00
|
|
|
case PROCESS_UNINITIALIZED:
|
2007-02-02 17:12:07 +00:00
|
|
|
if (first_time) {
|
2004-08-11 11:30:25 +00:00
|
|
|
retval = start(true);
|
2004-08-24 17:58:51 +00:00
|
|
|
str = "Starting";
|
2004-08-11 11:30:25 +00:00
|
|
|
} else {
|
|
|
|
retval = start(false);
|
2004-08-24 17:58:51 +00:00
|
|
|
str = "Restarting";
|
2004-08-11 11:30:25 +00:00
|
|
|
}
|
2007-06-25 11:46:15 +00:00
|
|
|
if ((retval == ERR_SHMGET) || (retval == ERR_SHMAT)) {
|
|
|
|
return retval;
|
|
|
|
}
|
2004-10-07 19:18:37 +00:00
|
|
|
if (retval) {
|
2007-01-24 21:20:57 +00:00
|
|
|
set_task_state(PROCESS_COULDNT_START, "resume_or_start1");
|
2004-10-07 19:18:37 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2004-08-23 22:06:48 +00:00
|
|
|
break;
|
|
|
|
case PROCESS_SUSPENDED:
|
2004-08-11 11:30:25 +00:00
|
|
|
retval = unsuspend();
|
|
|
|
if (retval) {
|
2007-01-25 23:39:06 +00:00
|
|
|
msg_printf(wup->project, MSG_INTERNAL_ERROR,
|
2006-01-17 22:48:09 +00:00
|
|
|
"Couldn't resume task %s", result->name
|
2004-08-11 11:30:25 +00:00
|
|
|
);
|
2007-01-24 21:20:57 +00:00
|
|
|
set_task_state(PROCESS_COULDNT_START, "resume_or_start2");
|
2004-08-11 11:30:25 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2004-08-24 17:58:51 +00:00
|
|
|
str = "Resuming";
|
2004-08-23 22:06:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2007-01-25 23:39:06 +00:00
|
|
|
msg_printf(result->project, MSG_INTERNAL_ERROR,
|
2007-01-24 21:20:57 +00:00
|
|
|
"Unexpected state %d for task %s", task_state(), result->name
|
2004-08-23 22:06:48 +00:00
|
|
|
);
|
|
|
|
return 0;
|
2004-08-11 11:30:25 +00:00
|
|
|
}
|
2006-05-21 22:03:36 +00:00
|
|
|
if (log_flags.task) {
|
|
|
|
msg_printf(result->project, MSG_INFO,
|
|
|
|
"%s task %s using %s version %d",
|
|
|
|
str,
|
|
|
|
result->name,
|
|
|
|
app_version->app->name,
|
|
|
|
app_version->version_num
|
|
|
|
);
|
|
|
|
}
|
2004-08-11 11:30:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-24 00:44:42 +00:00
|
|
|
#if (defined (__APPLE__) && (defined(__i386__) || defined(__x86_64__)))
|
2006-02-06 23:44:05 +00:00
|
|
|
|
|
|
|
union headeru {
|
|
|
|
fat_header fat;
|
|
|
|
mach_header mach;
|
|
|
|
};
|
|
|
|
|
2007-06-29 15:45:15 +00:00
|
|
|
// Read the mach-o headers to determine the architectures
|
|
|
|
// supported by executable file.
|
2008-02-20 12:04:56 +00:00
|
|
|
// Returns 1 if application can run natively on i386 / x86_64 Macs, else returns 0.
|
2007-06-29 15:45:15 +00:00
|
|
|
//
|
2006-02-06 23:44:05 +00:00
|
|
|
int ACTIVE_TASK::is_native_i386_app(char* exec_path) {
|
|
|
|
FILE *f;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
headeru myHeader;
|
|
|
|
fat_arch fatHeader;
|
|
|
|
|
|
|
|
uint32_t n, i, len;
|
2008-02-20 12:04:56 +00:00
|
|
|
uint32_t theMagic;
|
|
|
|
integer_t theType;
|
2006-02-06 23:44:05 +00:00
|
|
|
|
|
|
|
f = boinc_fopen(exec_path, "rb");
|
2007-06-29 15:45:15 +00:00
|
|
|
if (!f) {
|
2006-02-06 23:44:05 +00:00
|
|
|
return result; // Should never happen
|
2007-06-29 15:45:15 +00:00
|
|
|
}
|
2006-02-06 23:44:05 +00:00
|
|
|
|
|
|
|
myHeader.fat.magic = 0;
|
|
|
|
myHeader.fat.nfat_arch = 0;
|
|
|
|
|
|
|
|
fread(&myHeader, 1, sizeof(fat_header), f);
|
2008-02-20 12:04:56 +00:00
|
|
|
theMagic = myHeader.mach.magic;
|
|
|
|
switch (theMagic) {
|
|
|
|
case MH_CIGAM:
|
2006-02-06 23:44:05 +00:00
|
|
|
case MH_MAGIC:
|
2008-02-20 12:04:56 +00:00
|
|
|
case MH_MAGIC_64:
|
|
|
|
case MH_CIGAM_64:
|
|
|
|
theType = myHeader.mach.cputype;
|
|
|
|
if ((theMagic == MH_CIGAM) || (theMagic == MH_CIGAM_64)) {
|
|
|
|
theType = OSSwapInt32(theType);
|
|
|
|
}
|
|
|
|
if ((theType == CPU_TYPE_I386) || (theType == CPU_TYPE_X86_64)) {
|
|
|
|
result = 1; // Single-architecture i386or x86_64 file
|
2007-06-29 15:45:15 +00:00
|
|
|
}
|
2006-02-06 23:44:05 +00:00
|
|
|
break;
|
2008-02-20 12:04:56 +00:00
|
|
|
case FAT_MAGIC:
|
2006-02-06 23:44:05 +00:00
|
|
|
case FAT_CIGAM:
|
2008-02-20 12:04:56 +00:00
|
|
|
n = myHeader.fat.nfat_arch;
|
|
|
|
if (theMagic == FAT_CIGAM) {
|
|
|
|
n = OSSwapInt32(myHeader.fat.nfat_arch);
|
|
|
|
}
|
2007-06-29 15:45:15 +00:00
|
|
|
// Multiple architecture (fat) file
|
2006-02-06 23:44:05 +00:00
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
len = fread(&fatHeader, 1, sizeof(fat_arch), f);
|
2007-06-29 15:45:15 +00:00
|
|
|
if (len < sizeof(fat_arch)) {
|
2006-02-06 23:44:05 +00:00
|
|
|
break; // Should never happen
|
2007-06-29 15:45:15 +00:00
|
|
|
}
|
2008-02-20 12:04:56 +00:00
|
|
|
theType = fatHeader.cputype;
|
|
|
|
if (theMagic == FAT_CIGAM) {
|
|
|
|
theType = OSSwapInt32(theType);
|
|
|
|
}
|
|
|
|
if ((theType == CPU_TYPE_I386) || (theType == CPU_TYPE_X86_64)) {
|
2006-02-06 23:44:05 +00:00
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (f);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_be8bae8cbb = "$Id$";
|