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
|
2003-08-21 23:44:57 +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.
|
2003-08-21 23:44:57 +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.
|
2002-08-05 00:29:34 +00:00
|
|
|
//
|
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/>.
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2010-01-01 02:50:56 +00:00
|
|
|
// The BOINC API and runtime system.
|
|
|
|
//
|
|
|
|
// Notes:
|
|
|
|
// 1) Thread structure:
|
|
|
|
// Sequential apps
|
|
|
|
// Unix
|
|
|
|
// getting CPU time and suspend/resume have to be done
|
|
|
|
// in the worker thread, so we use a SIGALRM signal handler.
|
|
|
|
// However, many library functions and system calls
|
|
|
|
// are not "asynch signal safe": see, e.g.
|
|
|
|
// http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04_03
|
|
|
|
// (e.g. sprintf() in a signal handler hangs Mac OS X)
|
|
|
|
// so we do as little as possible in the signal handler,
|
|
|
|
// and do the rest in a separate "timer thread".
|
|
|
|
// Win
|
|
|
|
// the timer thread does everything
|
2013-06-05 22:22:39 +00:00
|
|
|
// Multi-thread apps:
|
2010-01-01 02:50:56 +00:00
|
|
|
// Unix:
|
|
|
|
// fork
|
|
|
|
// original process runs timer loop:
|
|
|
|
// handle suspend/resume/quit, heartbeat (use signals)
|
|
|
|
// new process call boinc_init_options() with flags to
|
|
|
|
// send status messages and handle checkpoint stuff,
|
|
|
|
// and returns from boinc_init_parallel()
|
2012-10-11 16:41:31 +00:00
|
|
|
// NOTE: THIS DOESN'T RESPECT CRITICAL SECTIONS.
|
|
|
|
// NEED TO MASK SIGNALS IN CHILD DURING CRITICAL SECTIONS
|
2010-01-01 02:50:56 +00:00
|
|
|
// Win:
|
|
|
|
// like sequential case, except suspend/resume must enumerate
|
|
|
|
// all threads (except timer) and suspend/resume them all
|
|
|
|
//
|
|
|
|
// 2) All variables that are accessed by two threads (i.e. worker and timer)
|
|
|
|
// MUST be declared volatile.
|
|
|
|
//
|
|
|
|
// 3) For compatibility with C, we use int instead of bool various places
|
|
|
|
//
|
2012-10-11 16:41:31 +00:00
|
|
|
// 4) We must periodically check that the client is still alive and exit if not.
|
|
|
|
// Originally this was done using heartbeat msgs from client.
|
|
|
|
// This is unreliable, e.g. if the client is blocked for a long time.
|
|
|
|
// As of Oct 11 2012 we use a different mechanism:
|
|
|
|
// the client passes its PID and we periodically check whether it exists.
|
|
|
|
// But we need to support the heartbeat mechanism also for compatibility.
|
|
|
|
//
|
2010-01-01 02:50:56 +00:00
|
|
|
// Terminology:
|
|
|
|
// The processing of a result can be divided
|
|
|
|
// into multiple "episodes" (executions of the app),
|
|
|
|
// each of which resumes from the checkpointed state of the previous episode.
|
|
|
|
// Unless otherwise noted, "CPU time" refers to the sum over all episodes
|
|
|
|
// (not counting the part after the last checkpoint in an episode).
|
|
|
|
|
2011-09-02 20:47:05 +00:00
|
|
|
|
2005-07-14 16:46:38 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__) && !defined(_BOINC_WIN_) && !defined(_AFX_STDAFX_H_)
|
2004-06-16 21:34:31 +00:00
|
|
|
#include "boinc_win.h"
|
2005-07-14 16:46:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2004-12-30 03:03:26 +00:00
|
|
|
#include "version.h"
|
2010-01-01 05:12:03 +00:00
|
|
|
#include "win_util.h"
|
2004-06-16 21:34:31 +00:00
|
|
|
#else
|
2004-12-14 20:28:13 +00:00
|
|
|
#include "config.h"
|
2004-06-16 12:01:49 +00:00
|
|
|
#include <cstdlib>
|
2008-02-27 23:26:38 +00:00
|
|
|
#include <cstring>
|
2004-06-16 12:01:49 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdarg>
|
2004-06-12 18:44:53 +00:00
|
|
|
#include <sys/types.h>
|
2005-03-19 21:39:17 +00:00
|
|
|
#include <errno.h>
|
2002-08-05 00:29:34 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
2005-05-23 00:07:29 +00:00
|
|
|
#include <sys/resource.h>
|
2010-01-01 05:53:38 +00:00
|
|
|
#include <sys/wait.h>
|
2005-01-27 09:19:42 +00:00
|
|
|
#include <pthread.h>
|
2013-01-11 15:33:05 +00:00
|
|
|
#include <vector>
|
2005-12-02 22:29:35 +00:00
|
|
|
#ifndef __EMX__
|
2005-01-27 09:19:42 +00:00
|
|
|
#include <sched.h>
|
2005-12-02 22:29:35 +00:00
|
|
|
#endif
|
2004-06-16 21:34:31 +00:00
|
|
|
#endif
|
2004-03-05 04:37:53 +00:00
|
|
|
|
2011-04-14 22:25:38 +00:00
|
|
|
#include "app_ipc.h"
|
|
|
|
#include "common_defs.h"
|
2004-03-24 07:39:24 +00:00
|
|
|
#include "diagnostics.h"
|
2011-04-14 22:25:38 +00:00
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
#include "mem_usage.h"
|
2002-08-05 00:29:34 +00:00
|
|
|
#include "parse.h"
|
2011-09-02 18:23:13 +00:00
|
|
|
#include "proc_control.h"
|
2003-03-17 19:24:38 +00:00
|
|
|
#include "shmem.h"
|
2013-06-07 00:31:46 +00:00
|
|
|
#include "str_replace.h"
|
2007-10-31 20:52:25 +00:00
|
|
|
#include "str_util.h"
|
2011-04-14 22:25:38 +00:00
|
|
|
#include "util.h"
|
2004-10-26 21:12:00 +00:00
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#include "boinc_api.h"
|
|
|
|
|
2011-09-02 20:47:05 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2013-07-10 00:43:19 +00:00
|
|
|
//#define DEBUG_BOINC_API
|
2009-03-20 23:02:31 +00:00
|
|
|
|
2006-03-23 11:38:49 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include "mac_backtrace.h"
|
2008-01-08 18:58:44 +00:00
|
|
|
#define GETRUSAGE_IN_TIMER_THREAD
|
|
|
|
// call getrusage() in the timer thread,
|
|
|
|
// rather than in the worker thread's signal handler
|
|
|
|
// (which can cause crashes on Mac)
|
|
|
|
// If you want, you can set this for Linux too:
|
|
|
|
// CPPFLAGS=-DGETRUSAGE_IN_TIMER_THREAD
|
2006-03-23 11:38:49 +00:00
|
|
|
#endif
|
|
|
|
|
2013-11-05 17:19:27 +00:00
|
|
|
const char* api_version = "API_VERSION_" PACKAGE_VERSION;
|
2011-09-12 03:48:16 +00:00
|
|
|
static APP_INIT_DATA aid;
|
2005-03-05 23:12:31 +00:00
|
|
|
static FILE_LOCK file_lock;
|
2005-03-19 21:39:17 +00:00
|
|
|
APP_CLIENT_SHM* app_client_shm = 0;
|
2005-03-21 19:57:44 +00:00
|
|
|
static volatile int time_until_checkpoint;
|
2004-10-26 20:33:16 +00:00
|
|
|
// time until enable checkpoint
|
2006-12-09 00:10:53 +00:00
|
|
|
static volatile double fraction_done;
|
|
|
|
static volatile double last_checkpoint_cpu_time;
|
|
|
|
static volatile bool ready_to_checkpoint = false;
|
2010-01-01 02:50:56 +00:00
|
|
|
static volatile int in_critical_section = 0;
|
2005-03-19 21:39:17 +00:00
|
|
|
static volatile double last_wu_cpu_time;
|
2010-01-01 02:50:56 +00:00
|
|
|
static volatile bool standalone = false;
|
2006-12-09 00:10:53 +00:00
|
|
|
static volatile double initial_wu_cpu_time;
|
2005-03-19 21:39:17 +00:00
|
|
|
static volatile bool have_new_trickle_up = false;
|
2005-04-28 23:19:58 +00:00
|
|
|
static volatile bool have_trickle_down = true;
|
2014-07-04 18:11:38 +00:00
|
|
|
// set if the client notified us of a trickle-down.
|
|
|
|
// init to true so the first call to boinc_receive_trickle_down()
|
|
|
|
// will scan the slot dir for old trickle-down files
|
|
|
|
static volatile bool handle_trickle_downs = false;
|
|
|
|
// whether we should check for notifications of trickle_downs
|
|
|
|
// and file upload status.
|
|
|
|
// set by boinc_receive_trickle_down() and boinc_upload_file().
|
2012-10-11 16:41:31 +00:00
|
|
|
static volatile int heartbeat_giveup_count;
|
2014-05-30 19:29:08 +00:00
|
|
|
// interrupt count value at which to give up on client
|
2005-02-16 23:30:29 +00:00
|
|
|
#ifdef _WIN32
|
2005-03-19 21:39:17 +00:00
|
|
|
static volatile int nrunning_ticks = 0;
|
2005-02-16 23:30:29 +00:00
|
|
|
#endif
|
2005-07-23 08:10:39 +00:00
|
|
|
static volatile int interrupt_count = 0;
|
|
|
|
// number of timer interrupts
|
|
|
|
// used to measure elapsed time in a way that's
|
|
|
|
// not affected by user changing system clock,
|
2007-11-29 19:48:04 +00:00
|
|
|
// and doesn't have big jump after hibernation
|
2009-07-01 17:35:56 +00:00
|
|
|
static volatile int running_interrupt_count = 0;
|
|
|
|
// number of timer interrupts while not suspended.
|
|
|
|
// Used to compute elapsed time
|
2013-07-04 23:00:10 +00:00
|
|
|
static volatile bool finishing;
|
|
|
|
// used for worker/timer synch during boinc_finish();
|
2005-08-17 22:08:35 +00:00
|
|
|
static int want_network = 0;
|
|
|
|
static int have_network = 1;
|
2012-05-20 23:02:12 +00:00
|
|
|
static double bytes_sent = 0;
|
|
|
|
static double bytes_received = 0;
|
2013-07-04 23:00:10 +00:00
|
|
|
bool boinc_disable_timer_thread = false;
|
2007-09-25 16:04:45 +00:00
|
|
|
// simulate unresponsive app by setting to true (debugging)
|
2007-06-08 07:55:27 +00:00
|
|
|
static FUNC_PTR timer_callback = 0;
|
2012-01-13 19:00:16 +00:00
|
|
|
char web_graphics_url[256];
|
|
|
|
bool send_web_graphics_url = false;
|
|
|
|
char remote_desktop_addr[256];
|
|
|
|
bool send_remote_desktop_addr = false;
|
2012-08-10 22:20:41 +00:00
|
|
|
int app_min_checkpoint_period = 0;
|
|
|
|
// min checkpoint period requested by app
|
2006-05-11 18:28:35 +00:00
|
|
|
|
2007-12-21 20:57:48 +00:00
|
|
|
#define TIMER_PERIOD 0.1
|
2013-07-04 01:08:09 +00:00
|
|
|
// Sleep interval for timer thread;
|
|
|
|
// determines max rate of handling messages from client.
|
|
|
|
// Unix: period of worker-thread timer interrupts.
|
2007-12-21 20:57:48 +00:00
|
|
|
#define TIMERS_PER_SEC 10
|
2013-07-04 01:08:09 +00:00
|
|
|
// reciprocal of TIMER_PERIOD
|
2005-03-15 20:23:24 +00:00
|
|
|
// This determines the resolution of fraction done and CPU time reporting
|
2013-07-04 01:08:09 +00:00
|
|
|
// to the client, and of checkpoint enabling.
|
2012-10-11 16:41:31 +00:00
|
|
|
#define HEARTBEAT_GIVEUP_SECS 30
|
|
|
|
#define HEARTBEAT_GIVEUP_COUNT ((int)(HEARTBEAT_GIVEUP_SECS/TIMER_PERIOD))
|
2014-05-30 19:29:08 +00:00
|
|
|
// quit if no heartbeat from client in this #interrupts
|
2007-12-21 20:57:48 +00:00
|
|
|
#define LOCKFILE_TIMEOUT_PERIOD 35
|
|
|
|
// quit if we cannot aquire slot lock file in this #secs after startup
|
2004-02-06 09:34:00 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2005-06-08 20:28:05 +00:00
|
|
|
static HANDLE hSharedMem;
|
|
|
|
HANDLE worker_thread_handle;
|
2005-01-28 01:58:11 +00:00
|
|
|
// used to suspend worker thread, and to measure its CPU time
|
2010-01-16 23:52:05 +00:00
|
|
|
DWORD timer_thread_id;
|
2005-08-11 18:18:03 +00:00
|
|
|
#else
|
2008-01-13 00:12:14 +00:00
|
|
|
static volatile bool worker_thread_exit_flag = false;
|
|
|
|
static volatile int worker_thread_exit_status;
|
|
|
|
// the above are used by the timer thread to tell
|
|
|
|
// the worker thread to exit
|
2013-09-10 18:29:34 +00:00
|
|
|
static pthread_t worker_thread_handle;
|
2005-08-11 18:18:03 +00:00
|
|
|
static pthread_t timer_thread_handle;
|
2008-01-08 18:58:44 +00:00
|
|
|
#ifndef GETRUSAGE_IN_TIMER_THREAD
|
2007-12-21 19:23:22 +00:00
|
|
|
static struct rusage worker_thread_ru;
|
2004-02-06 09:34:00 +00:00
|
|
|
#endif
|
2008-01-03 20:43:48 +00:00
|
|
|
#endif
|
2004-02-06 09:34:00 +00:00
|
|
|
|
2004-08-03 09:50:24 +00:00
|
|
|
static BOINC_OPTIONS options;
|
2011-06-07 05:16:12 +00:00
|
|
|
volatile BOINC_STATUS boinc_status;
|
2004-08-03 09:50:24 +00:00
|
|
|
|
2005-04-27 06:55:28 +00:00
|
|
|
// vars related to intermediate file upload
|
|
|
|
struct UPLOAD_FILE_STATUS {
|
|
|
|
std::string name;
|
|
|
|
int status;
|
|
|
|
};
|
|
|
|
static bool have_new_upload_file;
|
|
|
|
static std::vector<UPLOAD_FILE_STATUS> upload_file_status;
|
|
|
|
|
2010-08-11 22:02:41 +00:00
|
|
|
static int resume_activities();
|
|
|
|
static void boinc_exit(int);
|
2008-01-09 17:32:02 +00:00
|
|
|
static void block_sigalrm();
|
|
|
|
static int start_worker_signals();
|
2007-09-27 13:09:48 +00:00
|
|
|
|
2010-09-15 23:03:30 +00:00
|
|
|
char* boinc_msg_prefix(char* sbuf, int len) {
|
2009-03-27 23:42:47 +00:00
|
|
|
char buf[256];
|
2010-12-01 18:04:18 +00:00
|
|
|
struct tm tm;
|
2010-12-07 14:23:37 +00:00
|
|
|
struct tm *tmp = &tm;
|
2010-12-01 18:04:18 +00:00
|
|
|
int n;
|
2010-12-01 15:25:07 +00:00
|
|
|
|
2010-12-01 18:04:18 +00:00
|
|
|
time_t x = time(0);
|
|
|
|
if (x == -1) {
|
2013-06-03 22:04:20 +00:00
|
|
|
strlcpy(sbuf, "time() failed", len);
|
2010-12-01 15:25:07 +00:00
|
|
|
return sbuf;
|
|
|
|
}
|
2010-12-02 19:38:25 +00:00
|
|
|
#ifdef _WIN32
|
2010-12-07 14:23:37 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
if ((tmp = localtime(&x)) == NULL) {
|
|
|
|
#else
|
2010-12-02 19:38:25 +00:00
|
|
|
if (localtime_s(&tm, &x) == EINVAL) {
|
2010-12-07 14:23:37 +00:00
|
|
|
#endif
|
2010-12-02 19:38:25 +00:00
|
|
|
#else
|
2010-12-01 18:04:18 +00:00
|
|
|
if (localtime_r(&x, &tm) == NULL) {
|
2010-12-02 19:38:25 +00:00
|
|
|
#endif
|
2013-06-03 22:04:20 +00:00
|
|
|
strlcpy(sbuf, "localtime() failed", len);
|
2010-12-01 15:25:07 +00:00
|
|
|
return sbuf;
|
|
|
|
}
|
2010-12-07 14:23:37 +00:00
|
|
|
if (strftime(buf, sizeof(buf)-1, "%H:%M:%S", tmp) == 0) {
|
2013-06-03 22:04:20 +00:00
|
|
|
strlcpy(sbuf, "strftime() failed", len);
|
2010-12-01 15:25:07 +00:00
|
|
|
return sbuf;
|
|
|
|
}
|
2009-03-27 23:42:47 +00:00
|
|
|
#ifdef _WIN32
|
2010-12-01 18:04:18 +00:00
|
|
|
n = _snprintf(sbuf, len, "%s (%d):", buf, GetCurrentProcessId());
|
2009-03-27 23:42:47 +00:00
|
|
|
#else
|
2010-12-01 18:04:18 +00:00
|
|
|
n = snprintf(sbuf, len, "%s (%d):", buf, getpid());
|
2009-03-27 23:42:47 +00:00
|
|
|
#endif
|
2010-12-01 18:04:18 +00:00
|
|
|
if (n < 0) {
|
2013-06-03 22:04:20 +00:00
|
|
|
strlcpy(sbuf, "sprintf() failed", len);
|
2010-12-01 15:25:07 +00:00
|
|
|
return sbuf;
|
2010-12-01 18:04:18 +00:00
|
|
|
}
|
|
|
|
sbuf[len-1] = 0; // just in case
|
2009-03-27 23:42:47 +00:00
|
|
|
return sbuf;
|
|
|
|
}
|
|
|
|
|
2005-03-29 23:13:28 +00:00
|
|
|
static int setup_shared_mem() {
|
2010-06-17 17:41:27 +00:00
|
|
|
char buf[256];
|
2005-03-29 23:13:28 +00:00
|
|
|
if (standalone) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s Standalone mode, so not using shared memory.\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2005-03-29 23:13:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
app_client_shm = new APP_CLIENT_SHM;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
sprintf(buf, "%s%s", SHM_PREFIX, aid.shmem_seg_name);
|
|
|
|
hSharedMem = attach_shmem(buf, (void**)&app_client_shm->shm);
|
|
|
|
if (hSharedMem == NULL) {
|
|
|
|
delete app_client_shm;
|
|
|
|
app_client_shm = NULL;
|
|
|
|
}
|
2009-01-23 20:30:34 +00:00
|
|
|
#else
|
|
|
|
#ifdef __EMX__
|
|
|
|
if (attach_shmem(aid.shmem_seg_name, (void**)&app_client_shm->shm)) {
|
|
|
|
delete app_client_shm;
|
|
|
|
app_client_shm = NULL;
|
|
|
|
}
|
2007-08-27 09:17:38 +00:00
|
|
|
#else
|
2007-09-26 10:17:11 +00:00
|
|
|
if (aid.shmem_seg_name == -1) {
|
|
|
|
// Version 6 Unix/Linux/Mac client
|
- server code: at some point I made a global var "SCHED_CONFIG config",
mostly so that the parse function could assume
that everything was initially zero.
However, various back-end functions pass around SCHED_CONFIG&
as an argument (also named "config").
This creates a shadow, which is always bad.
Worse is the possibility that some projects have back-end programs
that have a SCHED_CONFIG variable that's automatic,
and therefore isn't zero initially,
and therefore isn't parsing correctly.
To fix this, I changed the 2 vectors in SCHED_CONFIG into pointers,
and have the parse routine zero the structure.
I was tempted to remove the SCHED_CONFIG& args to back-end functions,
but this would have broken some projects' code.
I did, however, change the name from config to config_loc
to avoid shadowing.
Also fixed various other compiler warnings.
svn path=/trunk/boinc/; revision=15541
2008-07-02 17:24:53 +00:00
|
|
|
if (attach_shmem_mmap(MMAPPED_FILE_NAME, (void**)&app_client_shm->shm)) {
|
2007-09-26 10:17:11 +00:00
|
|
|
delete app_client_shm;
|
|
|
|
app_client_shm = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
2009-01-23 20:30:34 +00:00
|
|
|
// version 5 Unix/Linux/Mac client
|
2007-09-26 10:17:11 +00:00
|
|
|
if (attach_shmem(aid.shmem_seg_name, (void**)&app_client_shm->shm)) {
|
|
|
|
delete app_client_shm;
|
|
|
|
app_client_shm = NULL;
|
|
|
|
}
|
2005-03-29 23:13:28 +00:00
|
|
|
}
|
2009-01-23 20:30:34 +00:00
|
|
|
#endif
|
2007-08-27 09:17:38 +00:00
|
|
|
#endif // ! _WIN32
|
2005-03-29 23:13:28 +00:00
|
|
|
if (app_client_shm == NULL) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-05 22:22:39 +00:00
|
|
|
// a mutex for data structures shared between time and worker threads
|
|
|
|
//
|
|
|
|
#ifdef _WIN32
|
|
|
|
static HANDLE mutex;
|
|
|
|
static void init_mutex() {
|
2013-07-10 00:43:19 +00:00
|
|
|
mutex = CreateMutex(NULL, FALSE, NULL);
|
2013-06-05 22:22:39 +00:00
|
|
|
}
|
|
|
|
static inline void acquire_mutex() {
|
|
|
|
WaitForSingleObject(mutex, INFINITE);
|
|
|
|
}
|
|
|
|
static inline void release_mutex() {
|
|
|
|
ReleaseMutex(mutex);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static void init_mutex() {}
|
|
|
|
static inline void acquire_mutex() {
|
2013-07-25 21:14:16 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
|
|
|
char buf[256];
|
|
|
|
fprintf(stderr, "%s acquiring mutex\n",
|
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
|
|
|
);
|
|
|
|
#endif
|
2013-06-05 22:22:39 +00:00
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
}
|
|
|
|
static inline void release_mutex() {
|
2013-07-25 21:14:16 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
|
|
|
char buf[256];
|
|
|
|
fprintf(stderr, "%s releasing mutex\n",
|
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
|
|
|
);
|
|
|
|
#endif
|
2013-06-05 22:22:39 +00:00
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-27 18:25:29 +00:00
|
|
|
// Return CPU time of process.
|
2005-08-11 18:18:03 +00:00
|
|
|
//
|
2007-06-08 07:55:27 +00:00
|
|
|
double boinc_worker_thread_cpu_time() {
|
2007-10-16 23:02:13 +00:00
|
|
|
double cpu;
|
2005-03-29 23:13:28 +00:00
|
|
|
#ifdef _WIN32
|
2006-05-01 18:34:08 +00:00
|
|
|
int retval;
|
2009-03-24 17:18:09 +00:00
|
|
|
retval = boinc_process_cpu_time(GetCurrentProcess(), cpu);
|
2006-05-01 05:54:52 +00:00
|
|
|
if (retval) {
|
2005-03-29 23:13:28 +00:00
|
|
|
cpu = nrunning_ticks * TIMER_PERIOD; // for Win9x
|
|
|
|
}
|
|
|
|
#else
|
2008-01-08 18:58:44 +00:00
|
|
|
#ifdef GETRUSAGE_IN_TIMER_THREAD
|
2008-01-03 20:43:48 +00:00
|
|
|
struct rusage worker_thread_ru;
|
|
|
|
getrusage(RUSAGE_SELF, &worker_thread_ru);
|
|
|
|
#endif
|
2007-06-08 07:55:27 +00:00
|
|
|
cpu = (double)worker_thread_ru.ru_utime.tv_sec
|
|
|
|
+ (((double)worker_thread_ru.ru_utime.tv_usec)/1000000.0);
|
|
|
|
cpu += (double)worker_thread_ru.ru_stime.tv_sec
|
|
|
|
+ (((double)worker_thread_ru.ru_stime.tv_usec)/1000000.0);
|
2005-03-29 23:13:28 +00:00
|
|
|
#endif
|
2007-10-16 23:02:13 +00:00
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
return cpu;
|
2005-03-29 23:13:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 19:29:08 +00:00
|
|
|
// Communicate to the client (via shared mem)
|
2008-01-08 18:58:44 +00:00
|
|
|
// the current CPU time and fraction done.
|
2007-10-30 23:03:21 +00:00
|
|
|
// NOTE: various bugs could cause some of these FP numbers to be enormous,
|
|
|
|
// possibly overflowing the buffer.
|
|
|
|
// So use strlcat() instead of strcat()
|
2005-03-29 23:13:28 +00:00
|
|
|
//
|
2008-01-08 18:58:44 +00:00
|
|
|
// This is called only from the timer thread (so no need for synch)
|
|
|
|
//
|
2006-09-05 19:00:59 +00:00
|
|
|
static bool update_app_progress(double cpu_t, double cp_cpu_t) {
|
2007-11-07 18:53:36 +00:00
|
|
|
char msg_buf[MSG_CHANNEL_SIZE], buf[256];
|
2005-03-29 23:13:28 +00:00
|
|
|
|
2005-07-07 20:32:28 +00:00
|
|
|
if (standalone) return true;
|
2005-03-29 23:13:28 +00:00
|
|
|
|
2005-08-11 18:18:03 +00:00
|
|
|
sprintf(msg_buf,
|
2007-11-07 18:53:36 +00:00
|
|
|
"<current_cpu_time>%e</current_cpu_time>\n"
|
|
|
|
"<checkpoint_cpu_time>%e</checkpoint_cpu_time>\n",
|
2006-06-26 17:09:23 +00:00
|
|
|
cpu_t, cp_cpu_t
|
2005-08-11 18:18:03 +00:00
|
|
|
);
|
2005-08-17 22:08:35 +00:00
|
|
|
if (want_network) {
|
2013-06-05 16:11:20 +00:00
|
|
|
strlcat(msg_buf, "<want_network>1</want_network>\n", sizeof(msg_buf));
|
2005-08-17 22:08:35 +00:00
|
|
|
}
|
2005-03-29 23:13:28 +00:00
|
|
|
if (fraction_done >= 0) {
|
|
|
|
double range = aid.fraction_done_end - aid.fraction_done_start;
|
|
|
|
double fdone = aid.fraction_done_start + fraction_done*range;
|
2007-11-07 18:53:36 +00:00
|
|
|
sprintf(buf, "<fraction_done>%e</fraction_done>\n", fdone);
|
2013-06-05 16:11:20 +00:00
|
|
|
strlcat(msg_buf, buf, sizeof(msg_buf));
|
2005-03-29 23:13:28 +00:00
|
|
|
}
|
2012-05-20 23:02:12 +00:00
|
|
|
if (bytes_sent) {
|
|
|
|
sprintf(buf, "<bytes_sent>%f</bytes_sent>\n", bytes_sent);
|
2013-06-05 16:11:20 +00:00
|
|
|
strlcat(msg_buf, buf, sizeof(msg_buf));
|
2005-11-03 05:31:21 +00:00
|
|
|
}
|
2012-05-20 23:02:12 +00:00
|
|
|
if (bytes_received) {
|
|
|
|
sprintf(buf, "<bytes_received>%f</bytes_received>\n", bytes_received);
|
2013-06-05 16:11:20 +00:00
|
|
|
strlcat(msg_buf, buf, sizeof(msg_buf));
|
2005-11-03 05:31:21 +00:00
|
|
|
}
|
2005-03-30 21:11:49 +00:00
|
|
|
return app_client_shm->shm->app_status.send_msg(msg_buf);
|
2005-03-29 23:13:28 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 22:25:38 +00:00
|
|
|
static void handle_heartbeat_msg() {
|
|
|
|
char buf[MSG_CHANNEL_SIZE];
|
|
|
|
double dtemp;
|
|
|
|
bool btemp;
|
|
|
|
|
|
|
|
if (app_client_shm->shm->heartbeat.get_msg(buf)) {
|
|
|
|
boinc_status.network_suspended = false;
|
|
|
|
if (match_tag(buf, "<heartbeat/>")) {
|
2012-10-11 16:41:31 +00:00
|
|
|
heartbeat_giveup_count = interrupt_count + HEARTBEAT_GIVEUP_COUNT;
|
2011-04-14 22:25:38 +00:00
|
|
|
}
|
|
|
|
if (parse_double(buf, "<wss>", dtemp)) {
|
|
|
|
boinc_status.working_set_size = dtemp;
|
|
|
|
}
|
|
|
|
if (parse_double(buf, "<max_wss>", dtemp)) {
|
|
|
|
boinc_status.max_working_set_size = dtemp;
|
|
|
|
}
|
|
|
|
if (parse_bool(buf, "suspend_network", btemp)) {
|
|
|
|
boinc_status.network_suspended = btemp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 16:41:31 +00:00
|
|
|
static bool client_dead() {
|
2013-11-05 16:24:24 +00:00
|
|
|
char buf[256];
|
2012-12-29 19:47:53 +00:00
|
|
|
bool dead;
|
2012-10-11 16:41:31 +00:00
|
|
|
if (aid.client_pid) {
|
|
|
|
// check every 10 sec
|
|
|
|
//
|
|
|
|
if (interrupt_count%(TIMERS_PER_SEC*10)) return false;
|
|
|
|
#ifdef _WIN32
|
2013-07-09 21:44:13 +00:00
|
|
|
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, aid.client_pid);
|
2013-11-05 16:24:24 +00:00
|
|
|
// If the process exists but is running under a different user account (boinc_master)
|
|
|
|
// then the handle returned is NULL and GetLastError() returns ERROR_ACCESS_DENIED.
|
|
|
|
//
|
|
|
|
if ((h == NULL) && (GetLastError() != ERROR_ACCESS_DENIED)) {
|
2013-07-09 21:27:36 +00:00
|
|
|
dead = true;
|
|
|
|
} else {
|
2013-11-05 16:24:24 +00:00
|
|
|
if (h) CloseHandle(h);
|
2013-07-09 21:27:36 +00:00
|
|
|
dead = false;
|
2012-10-11 16:41:31 +00:00
|
|
|
}
|
|
|
|
#else
|
2012-12-28 07:44:19 +00:00
|
|
|
int retval = kill(aid.client_pid, 0);
|
2012-12-29 19:47:53 +00:00
|
|
|
dead = (retval == -1 && errno == ESRCH);
|
2012-10-11 16:41:31 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
2012-12-29 19:47:53 +00:00
|
|
|
dead = (interrupt_count > heartbeat_giveup_count);
|
|
|
|
}
|
|
|
|
if (dead) {
|
|
|
|
boinc_msg_prefix(buf, sizeof(buf));
|
|
|
|
fputs(buf, stderr); // don't use fprintf() here
|
|
|
|
if (aid.client_pid) {
|
|
|
|
fputs(" BOINC client no longer exists - exiting\n", stderr);
|
|
|
|
} else {
|
|
|
|
fputs(" No heartbeat from client for 30 sec - exiting\n", stderr);
|
|
|
|
}
|
|
|
|
return true;
|
2012-10-11 16:41:31 +00:00
|
|
|
}
|
2012-12-29 19:47:53 +00:00
|
|
|
return false;
|
2012-10-11 16:41:31 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 22:25:38 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
// For multithread apps on Unix, the main process executes the following.
|
|
|
|
//
|
|
|
|
static void parallel_master(int child_pid) {
|
|
|
|
char buf[MSG_CHANNEL_SIZE];
|
|
|
|
int exit_status;
|
|
|
|
while (1) {
|
|
|
|
boinc_sleep(TIMER_PERIOD);
|
|
|
|
interrupt_count++;
|
|
|
|
if (app_client_shm) {
|
|
|
|
handle_heartbeat_msg();
|
|
|
|
if (app_client_shm->shm->process_control_request.get_msg(buf)) {
|
|
|
|
if (match_tag(buf, "<suspend/>")) {
|
|
|
|
kill(child_pid, SIGSTOP);
|
|
|
|
} else if (match_tag(buf, "<resume/>")) {
|
|
|
|
kill(child_pid, SIGCONT);
|
|
|
|
} else if (match_tag(buf, "<quit/>")) {
|
|
|
|
kill(child_pid, SIGKILL);
|
|
|
|
exit(0);
|
|
|
|
} else if (match_tag(buf, "<abort/>")) {
|
|
|
|
kill(child_pid, SIGKILL);
|
|
|
|
exit(EXIT_ABORTED_BY_CLIENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 16:41:31 +00:00
|
|
|
if (client_dead()) {
|
2011-04-14 22:25:38 +00:00
|
|
|
kill(child_pid, SIGKILL);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (interrupt_count % TIMERS_PER_SEC) continue;
|
|
|
|
if (waitpid(child_pid, &exit_status, WNOHANG) == child_pid) break;
|
|
|
|
}
|
|
|
|
boinc_finish(exit_status);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-10-26 21:12:00 +00:00
|
|
|
int boinc_init() {
|
2006-04-19 23:28:08 +00:00
|
|
|
int retval;
|
2006-05-01 21:36:38 +00:00
|
|
|
if (!diagnostics_is_initialized()) {
|
2007-06-08 07:38:17 +00:00
|
|
|
retval = boinc_init_diagnostics(BOINC_DIAG_DEFAULTS);
|
2006-04-19 23:28:08 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
}
|
2004-12-13 19:14:54 +00:00
|
|
|
boinc_options_defaults(options);
|
2006-03-13 20:45:08 +00:00
|
|
|
return boinc_init_options(&options);
|
2004-10-26 21:12:00 +00:00
|
|
|
}
|
2004-11-22 19:17:13 +00:00
|
|
|
|
2006-03-13 20:45:08 +00:00
|
|
|
int boinc_init_options(BOINC_OPTIONS* opt) {
|
2004-10-26 21:48:37 +00:00
|
|
|
int retval;
|
2011-04-14 22:25:38 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
if (options.multi_thread) {
|
|
|
|
int child_pid = fork();
|
|
|
|
if (child_pid) {
|
|
|
|
// original process - master
|
|
|
|
//
|
|
|
|
options.send_status_msgs = false;
|
2011-06-12 20:58:43 +00:00
|
|
|
retval = boinc_init_options_general(options);
|
2011-04-14 22:25:38 +00:00
|
|
|
if (retval) {
|
|
|
|
kill(child_pid, SIGKILL);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
parallel_master(child_pid);
|
|
|
|
}
|
|
|
|
// new process - slave
|
|
|
|
//
|
|
|
|
options.main_program = false;
|
|
|
|
options.check_heartbeat = false;
|
|
|
|
options.handle_process_control = false;
|
|
|
|
options.multi_thread = false;
|
|
|
|
options.multi_process = false;
|
|
|
|
return boinc_init_options(&options);
|
|
|
|
}
|
|
|
|
#endif
|
2006-03-13 20:45:08 +00:00
|
|
|
retval = boinc_init_options_general(*opt);
|
2004-10-26 21:48:37 +00:00
|
|
|
if (retval) return retval;
|
2008-01-09 17:32:02 +00:00
|
|
|
retval = start_timer_thread();
|
|
|
|
if (retval) return retval;
|
|
|
|
#ifndef _WIN32
|
|
|
|
retval = start_worker_signals();
|
|
|
|
if (retval) return retval;
|
|
|
|
#endif
|
|
|
|
return 0;
|
2004-10-26 21:48:37 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 22:25:38 +00:00
|
|
|
int boinc_init_parallel() {
|
2011-06-12 20:58:43 +00:00
|
|
|
BOINC_OPTIONS _options;
|
|
|
|
boinc_options_defaults(_options);
|
|
|
|
_options.multi_thread = true;
|
|
|
|
return boinc_init_options(&_options);
|
2011-04-14 22:25:38 +00:00
|
|
|
}
|
|
|
|
|
2012-08-10 22:20:41 +00:00
|
|
|
static int min_checkpoint_period() {
|
|
|
|
int x = (int)aid.checkpoint_period;
|
|
|
|
if (app_min_checkpoint_period > x) {
|
|
|
|
x = app_min_checkpoint_period;
|
|
|
|
}
|
|
|
|
if (x == 0) x = DEFAULT_CHECKPOINT_PERIOD;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_set_min_checkpoint_period(int x) {
|
|
|
|
app_min_checkpoint_period = x;
|
|
|
|
if (x > time_until_checkpoint) {
|
|
|
|
time_until_checkpoint = x;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-26 21:48:37 +00:00
|
|
|
int boinc_init_options_general(BOINC_OPTIONS& opt) {
|
2004-02-06 09:34:00 +00:00
|
|
|
int retval;
|
2010-06-17 17:41:27 +00:00
|
|
|
char buf[256];
|
2004-08-03 09:50:24 +00:00
|
|
|
options = opt;
|
2004-02-06 09:34:00 +00:00
|
|
|
|
2010-01-01 02:50:56 +00:00
|
|
|
if (!diagnostics_is_initialized()) {
|
|
|
|
retval = boinc_init_diagnostics(BOINC_DIAG_DEFAULTS);
|
|
|
|
if (retval) return retval;
|
|
|
|
}
|
|
|
|
|
2005-03-19 21:39:17 +00:00
|
|
|
boinc_status.no_heartbeat = false;
|
|
|
|
boinc_status.suspended = false;
|
|
|
|
boinc_status.quit_request = false;
|
2006-02-03 20:48:48 +00:00
|
|
|
boinc_status.abort_request = false;
|
2004-08-03 09:50:24 +00:00
|
|
|
|
|
|
|
if (options.main_program) {
|
2004-07-03 20:03:16 +00:00
|
|
|
// make sure we're the only app running in this slot
|
2004-06-29 04:34:55 +00:00
|
|
|
//
|
2005-03-05 23:12:31 +00:00
|
|
|
retval = file_lock.lock(LOCKFILE);
|
2004-07-03 20:03:16 +00:00
|
|
|
if (retval) {
|
|
|
|
// give any previous occupant a chance to timeout and exit
|
|
|
|
//
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s Can't acquire lockfile (%d) - waiting %ds\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)),
|
2009-03-23 17:26:20 +00:00
|
|
|
retval, LOCKFILE_TIMEOUT_PERIOD
|
|
|
|
);
|
2007-12-21 20:57:48 +00:00
|
|
|
boinc_sleep(LOCKFILE_TIMEOUT_PERIOD);
|
2005-03-05 23:12:31 +00:00
|
|
|
retval = file_lock.lock(LOCKFILE);
|
2004-07-03 20:03:16 +00:00
|
|
|
}
|
|
|
|
if (retval) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s Can't acquire lockfile (%d) - exiting\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)),
|
2009-03-27 23:42:47 +00:00
|
|
|
retval
|
|
|
|
);
|
2009-03-23 17:00:34 +00:00
|
|
|
#ifdef _WIN32
|
2010-09-15 23:03:30 +00:00
|
|
|
char buf2[256];
|
2013-01-08 05:36:44 +00:00
|
|
|
windows_format_error_string(GetLastError(), buf2, 256);
|
2010-09-15 23:03:30 +00:00
|
|
|
fprintf(stderr, "%s Error: %s\n", boinc_msg_prefix(buf, sizeof(buf)), buf2);
|
2009-03-23 17:00:34 +00:00
|
|
|
#endif
|
2010-01-11 20:52:55 +00:00
|
|
|
// if we can't acquire the lock file there must be
|
|
|
|
// another app instance running in this slot.
|
|
|
|
// If we exit(0), the client will keep restarting us.
|
|
|
|
// Instead, tell the client not to restart us for 10 min.
|
|
|
|
//
|
2015-06-24 19:25:33 +00:00
|
|
|
boinc_temporary_exit(600,
|
|
|
|
"Waiting to acquire slot directory lock. Another instance may be running."
|
|
|
|
);
|
2004-07-03 20:03:16 +00:00
|
|
|
}
|
2004-06-29 04:34:55 +00:00
|
|
|
}
|
|
|
|
|
2004-02-06 09:34:00 +00:00
|
|
|
retval = boinc_parse_init_data_file();
|
2004-05-21 20:25:12 +00:00
|
|
|
if (retval) {
|
|
|
|
standalone = true;
|
2004-06-03 04:25:44 +00:00
|
|
|
} else {
|
|
|
|
retval = setup_shared_mem();
|
|
|
|
if (retval) {
|
2006-10-31 00:29:30 +00:00
|
|
|
fprintf(stderr,
|
2009-03-27 23:42:47 +00:00
|
|
|
"%s Can't set up shared mem: %d. Will run in standalone mode.\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), retval
|
2006-10-31 00:29:30 +00:00
|
|
|
);
|
2004-06-03 04:25:44 +00:00
|
|
|
standalone = true;
|
|
|
|
}
|
2004-05-21 20:25:12 +00:00
|
|
|
}
|
|
|
|
|
2004-02-06 09:34:00 +00:00
|
|
|
// copy the WU CPU time to a separate var,
|
|
|
|
// since we may reread the structure again later.
|
|
|
|
//
|
|
|
|
initial_wu_cpu_time = aid.wu_cpu_time;
|
|
|
|
|
2004-05-21 20:25:12 +00:00
|
|
|
fraction_done = -1;
|
2012-08-10 22:20:41 +00:00
|
|
|
time_until_checkpoint = min_checkpoint_period();
|
2004-02-06 09:34:00 +00:00
|
|
|
last_checkpoint_cpu_time = aid.wu_cpu_time;
|
|
|
|
last_wu_cpu_time = aid.wu_cpu_time;
|
|
|
|
|
2007-11-29 19:48:04 +00:00
|
|
|
if (standalone) {
|
|
|
|
options.check_heartbeat = false;
|
|
|
|
}
|
2012-10-11 16:41:31 +00:00
|
|
|
heartbeat_giveup_count = interrupt_count + HEARTBEAT_GIVEUP_COUNT;
|
2004-06-29 01:15:51 +00:00
|
|
|
|
2013-06-05 22:22:39 +00:00
|
|
|
init_mutex();
|
|
|
|
|
2004-08-03 09:50:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-02-06 09:34:00 +00:00
|
|
|
|
2006-02-03 20:48:48 +00:00
|
|
|
int boinc_get_status(BOINC_STATUS *s) {
|
|
|
|
s->no_heartbeat = boinc_status.no_heartbeat;
|
|
|
|
s->suspended = boinc_status.suspended;
|
|
|
|
s->quit_request = boinc_status.quit_request;
|
2006-10-03 15:43:38 +00:00
|
|
|
s->reread_init_data_file = boinc_status.reread_init_data_file;
|
2006-02-03 20:48:48 +00:00
|
|
|
s->abort_request = boinc_status.abort_request;
|
2006-10-03 15:43:38 +00:00
|
|
|
s->working_set_size = boinc_status.working_set_size;
|
|
|
|
s->max_working_set_size = boinc_status.max_working_set_size;
|
2011-03-20 07:04:32 +00:00
|
|
|
s->network_suspended = boinc_status.network_suspended;
|
2004-02-06 09:34:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-27 06:55:28 +00:00
|
|
|
// if we have any new trickle-ups or file upload requests,
|
|
|
|
// send a message describing them
|
|
|
|
//
|
2004-08-03 09:50:24 +00:00
|
|
|
static void send_trickle_up_msg() {
|
2005-04-27 06:55:28 +00:00
|
|
|
char buf[MSG_CHANNEL_SIZE];
|
2011-10-08 16:33:31 +00:00
|
|
|
if (standalone) return;
|
2005-04-27 06:55:28 +00:00
|
|
|
strcpy(buf, "");
|
2004-08-03 09:50:24 +00:00
|
|
|
if (have_new_trickle_up) {
|
2005-04-27 06:55:28 +00:00
|
|
|
strcat(buf, "<have_new_trickle_up/>\n");
|
|
|
|
}
|
|
|
|
if (have_new_upload_file) {
|
|
|
|
strcat(buf, "<have_new_upload_file/>\n");
|
|
|
|
}
|
|
|
|
if (strlen(buf)) {
|
2014-07-04 18:11:38 +00:00
|
|
|
BOINCINFO("Sending Trickle Up Message");
|
2005-04-27 06:55:28 +00:00
|
|
|
if (app_client_shm->shm->trickle_up.send_msg(buf)) {
|
2004-08-03 09:50:24 +00:00
|
|
|
have_new_trickle_up = false;
|
2005-04-27 06:55:28 +00:00
|
|
|
have_new_upload_file = false;
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-02-06 09:34:00 +00:00
|
|
|
|
2014-05-30 19:29:08 +00:00
|
|
|
// NOTE: a non-zero status tells the client that we're exiting with
|
2005-01-19 15:54:04 +00:00
|
|
|
// an "unrecoverable error", which will be reported back to server.
|
2005-03-05 23:12:31 +00:00
|
|
|
// A zero exit-status tells the client we've successfully finished the result.
|
|
|
|
//
|
2014-09-16 06:13:32 +00:00
|
|
|
int boinc_finish_message(int status, const char* msg, bool is_notice) {
|
2010-06-17 17:41:27 +00:00
|
|
|
char buf[256];
|
2008-01-08 18:58:44 +00:00
|
|
|
fraction_done = 1;
|
2010-09-15 23:03:30 +00:00
|
|
|
fprintf(stderr,
|
2014-04-17 04:59:41 +00:00
|
|
|
"%s called boinc_finish(%d)\n",
|
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), status
|
2010-09-15 23:03:30 +00:00
|
|
|
);
|
2013-07-04 23:00:10 +00:00
|
|
|
finishing = true;
|
2008-01-08 18:58:44 +00:00
|
|
|
boinc_sleep(2.0); // let the timer thread send final messages
|
2013-07-04 23:00:10 +00:00
|
|
|
boinc_disable_timer_thread = true; // then disable it
|
2005-03-30 21:11:49 +00:00
|
|
|
|
2014-09-16 06:13:32 +00:00
|
|
|
if (options.main_program) {
|
2004-08-03 09:50:24 +00:00
|
|
|
FILE* f = fopen(BOINC_FINISH_CALLED_FILE, "w");
|
2014-09-16 06:13:32 +00:00
|
|
|
if (f) {
|
|
|
|
fprintf(f, "%d\n", status);
|
|
|
|
if (msg) {
|
|
|
|
fprintf(f, "%s\n%s\n", msg, is_notice?"notice":"");
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
2005-01-08 01:17:51 +00:00
|
|
|
|
2005-01-19 15:54:04 +00:00
|
|
|
boinc_exit(status);
|
|
|
|
|
2007-06-28 22:51:51 +00:00
|
|
|
return 0; // never reached
|
2005-03-05 23:12:31 +00:00
|
|
|
}
|
2005-01-19 15:54:04 +00:00
|
|
|
|
2014-09-16 06:13:32 +00:00
|
|
|
int boinc_finish(int status) {
|
|
|
|
return boinc_finish_message(status, NULL, false);
|
|
|
|
}
|
|
|
|
|
2014-05-28 18:05:56 +00:00
|
|
|
int boinc_temporary_exit(int delay, const char* reason, bool is_notice) {
|
2009-12-13 05:16:40 +00:00
|
|
|
FILE* f = fopen(TEMPORARY_EXIT_FILE, "w");
|
|
|
|
if (!f) {
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
fprintf(f, "%d\n", delay);
|
2012-02-22 22:56:05 +00:00
|
|
|
if (reason) {
|
|
|
|
fprintf(f, "%s\n", reason);
|
2014-05-28 18:05:56 +00:00
|
|
|
if (is_notice) {
|
|
|
|
fprintf(f, "notice\n");
|
|
|
|
}
|
2012-02-22 22:56:05 +00:00
|
|
|
}
|
2009-12-13 05:16:40 +00:00
|
|
|
fclose(f);
|
|
|
|
boinc_exit(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-05 23:12:31 +00:00
|
|
|
// unlock the lockfile and call the appropriate exit function
|
2008-01-13 00:12:14 +00:00
|
|
|
// Unix: called only from the worker thread.
|
|
|
|
// Win: called from the worker or timer thread.
|
2005-03-05 23:12:31 +00:00
|
|
|
//
|
2008-01-17 18:09:30 +00:00
|
|
|
// make static eventually
|
2008-09-22 20:33:59 +00:00
|
|
|
//
|
2008-01-17 18:09:30 +00:00
|
|
|
void boinc_exit(int status) {
|
2009-03-23 17:26:20 +00:00
|
|
|
int retval;
|
2010-06-24 05:11:34 +00:00
|
|
|
char buf[256];
|
2010-06-17 17:41:27 +00:00
|
|
|
|
2009-03-23 17:00:34 +00:00
|
|
|
if (options.main_program && file_lock.locked) {
|
2009-03-23 17:26:20 +00:00
|
|
|
retval = file_lock.unlock(LOCKFILE);
|
|
|
|
if (retval) {
|
|
|
|
#ifdef _WIN32
|
2013-01-08 05:36:44 +00:00
|
|
|
windows_format_error_string(GetLastError(), buf, 256);
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s Can't unlock lockfile (%d): %s\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), retval, buf
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2009-03-23 17:26:20 +00:00
|
|
|
#else
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr,
|
2010-09-15 23:03:30 +00:00
|
|
|
"%s Can't unlock lockfile (%d)\n",
|
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), retval
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2009-03-23 17:26:20 +00:00
|
|
|
perror("file unlock failed");
|
|
|
|
#endif
|
|
|
|
}
|
2008-09-22 20:33:59 +00:00
|
|
|
}
|
2005-01-19 15:54:04 +00:00
|
|
|
|
2011-04-14 22:25:38 +00:00
|
|
|
// kill any processes the app may have created
|
|
|
|
//
|
|
|
|
if (options.multi_process) {
|
2011-04-14 22:48:04 +00:00
|
|
|
kill_descendants();
|
2011-04-14 22:25:38 +00:00
|
|
|
}
|
|
|
|
|
2006-05-17 07:44:54 +00:00
|
|
|
boinc_finish_diag();
|
|
|
|
|
2007-11-29 19:48:04 +00:00
|
|
|
// various platforms have problems shutting down a process
|
|
|
|
// while other threads are still executing,
|
|
|
|
// or triggering endless exit()/atexit() loops.
|
|
|
|
//
|
2006-06-07 08:56:55 +00:00
|
|
|
BOINCINFO("Exit Status: %d", status);
|
2011-03-28 17:53:43 +00:00
|
|
|
fflush(NULL);
|
|
|
|
|
Safe exit checking for CUDA applications under windows. In application you
could use the following for safe exit checking.
#ifdef _WIN32
//Jason: Safe exit check macro to play nicer with Cuda & MS-CRT
#ifdef USE_CUDA
#define SAFE_EXIT_CHECK do { \
if (worker_thread_exit_request) { \
fprintf(stderr,"-> Worker received exit request, syncing Cuda...");
cudaThreadSynchronize(); fprintf(stderr,"Done.\n"); \
fprintf(stderr," Worker Freeing Cuda data..."); cudaAcc_free();
fprintf(stderr,"Done.\n"); \
fprintf(stderr," Worker Acknowledging exit request, spinning->\n");
worker_thread_exit_ack = true; \
while (1) Sleep(10); \
} \
} while (0);
#else
#define SAFE_EXIT_CHECK do { \
if (worker_thread_exit_request) { \
fprintf(stderr," Worker Acknowledging exit request, spinning-> ");
worker_thread_exit_ack = true; \
while (1) Sleep(10); \
} \
} while (0);
#endif
#else // Linux or other probably have their own safe exit handling, defined as
blank, do nothing
#define SAFE_EXIT_CHECK
#endif
and install at the top of the cffft loop, and more locations if desired:
SAFE_EXIT_CHECK;
I'd like to implement these as BOINC API functions, but have not yet done so.
svn path=/trunk/boinc/; revision=23646
2011-06-07 01:46:14 +00:00
|
|
|
#if defined(_WIN32)
|
2011-09-11 22:22:55 +00:00
|
|
|
// Halt all the threads and clean up.
|
2006-03-23 03:05:59 +00:00
|
|
|
TerminateProcess(GetCurrentProcess(), status);
|
2009-03-20 21:05:26 +00:00
|
|
|
// note: the above CAN return!
|
|
|
|
Sleep(1000);
|
|
|
|
DebugBreak();
|
2006-03-23 03:05:59 +00:00
|
|
|
#elif defined(__APPLE_CC__)
|
|
|
|
// stops endless exit()/atexit() loops.
|
2005-03-05 23:12:31 +00:00
|
|
|
_exit(status);
|
2005-01-19 15:54:04 +00:00
|
|
|
#else
|
2008-06-23 19:51:07 +00:00
|
|
|
// arrange to exit with given status even if errors happen
|
|
|
|
// in atexit() functions
|
|
|
|
//
|
|
|
|
set_signal_exit_code(status);
|
2005-03-05 23:12:31 +00:00
|
|
|
exit(status);
|
2005-01-08 01:17:51 +00:00
|
|
|
#endif
|
2005-03-05 23:12:31 +00:00
|
|
|
}
|
2004-02-06 09:34:00 +00:00
|
|
|
|
2012-05-20 23:02:12 +00:00
|
|
|
void boinc_network_usage(double sent, double received) {
|
|
|
|
bytes_sent = sent;
|
|
|
|
bytes_received = received;
|
|
|
|
}
|
|
|
|
|
2006-01-06 09:02:26 +00:00
|
|
|
int boinc_is_standalone() {
|
|
|
|
if (standalone) return 1;
|
|
|
|
return 0;
|
2004-02-06 09:34:00 +00:00
|
|
|
}
|
|
|
|
|
2008-01-13 00:12:14 +00:00
|
|
|
static void exit_from_timer_thread(int status) {
|
2009-03-23 17:26:20 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
2010-06-17 17:41:27 +00:00
|
|
|
char buf[256];
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s exit_from_timer_thread(%d) called\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), status
|
2009-03-20 23:02:31 +00:00
|
|
|
);
|
|
|
|
#endif
|
2008-01-13 00:12:14 +00:00
|
|
|
#ifdef _WIN32
|
2010-03-02 18:58:34 +00:00
|
|
|
// TerminateProcess() doesn't work if there are suspended threads?
|
|
|
|
if (boinc_status.suspended) {
|
2010-03-05 14:14:36 +00:00
|
|
|
resume_activities();
|
2010-03-02 18:58:34 +00:00
|
|
|
}
|
2008-01-13 00:12:14 +00:00
|
|
|
// this seems to work OK on Windows
|
|
|
|
//
|
|
|
|
boinc_exit(status);
|
|
|
|
#else
|
|
|
|
// but on Unix there are synchronization problems;
|
|
|
|
// set a flag telling the worker thread to exit
|
|
|
|
//
|
|
|
|
worker_thread_exit_status = status;
|
|
|
|
worker_thread_exit_flag = true;
|
|
|
|
pthread_exit(NULL);
|
|
|
|
#endif
|
|
|
|
}
|
2004-02-06 09:34:00 +00:00
|
|
|
|
|
|
|
// parse the init data file.
|
|
|
|
// This is done at startup, and also if a "reread prefs" message is received
|
|
|
|
//
|
|
|
|
int boinc_parse_init_data_file() {
|
|
|
|
FILE* f;
|
|
|
|
int retval;
|
2010-06-17 17:41:27 +00:00
|
|
|
char buf[256];
|
2004-02-06 09:34:00 +00:00
|
|
|
|
2008-11-11 23:39:00 +00:00
|
|
|
if (aid.project_preferences) {
|
|
|
|
free(aid.project_preferences);
|
|
|
|
aid.project_preferences = NULL;
|
|
|
|
}
|
2010-01-19 18:43:17 +00:00
|
|
|
aid.clear();
|
2004-10-14 22:01:05 +00:00
|
|
|
aid.checkpoint_period = DEFAULT_CHECKPOINT_PERIOD;
|
|
|
|
|
2004-04-02 08:07:12 +00:00
|
|
|
if (!boinc_file_exists(INIT_DATA_FILE)) {
|
2004-10-14 22:01:05 +00:00
|
|
|
fprintf(stderr,
|
2009-03-27 23:42:47 +00:00
|
|
|
"%s Can't open init data file - running in standalone mode\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
2004-10-14 22:01:05 +00:00
|
|
|
);
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
f = boinc_fopen(INIT_DATA_FILE, "r");
|
|
|
|
retval = parse_init_data_file(f, aid);
|
|
|
|
fclose(f);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr,
|
2009-03-27 23:42:47 +00:00
|
|
|
"%s Can't parse init data file - running in standalone mode\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
2004-10-14 22:01:05 +00:00
|
|
|
);
|
|
|
|
return retval;
|
2004-02-06 09:34:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-03 06:11:34 +00:00
|
|
|
// used by wrappers
|
|
|
|
//
|
2011-09-02 20:47:05 +00:00
|
|
|
int boinc_report_app_status_aux(
|
2004-12-09 01:07:01 +00:00
|
|
|
double cpu_time,
|
|
|
|
double checkpoint_cpu_time,
|
2011-09-02 20:47:05 +00:00
|
|
|
double _fraction_done,
|
2011-09-16 19:16:12 +00:00
|
|
|
int other_pid,
|
2012-05-20 23:02:12 +00:00
|
|
|
double _bytes_sent,
|
|
|
|
double _bytes_received
|
2004-12-09 01:07:01 +00:00
|
|
|
) {
|
2013-05-21 17:26:15 +00:00
|
|
|
char msg_buf[MSG_CHANNEL_SIZE], buf[1024];
|
2006-06-16 21:19:00 +00:00
|
|
|
if (standalone) return 0;
|
|
|
|
|
2004-12-09 01:07:01 +00:00
|
|
|
sprintf(msg_buf,
|
2011-05-17 00:31:41 +00:00
|
|
|
"<current_cpu_time>%e</current_cpu_time>\n"
|
|
|
|
"<checkpoint_cpu_time>%e</checkpoint_cpu_time>\n"
|
|
|
|
"<fraction_done>%e</fraction_done>\n",
|
2004-12-09 01:07:01 +00:00
|
|
|
cpu_time,
|
|
|
|
checkpoint_cpu_time,
|
2005-02-16 23:30:29 +00:00
|
|
|
_fraction_done
|
2004-12-09 01:07:01 +00:00
|
|
|
);
|
2011-09-16 19:16:12 +00:00
|
|
|
if (other_pid) {
|
|
|
|
sprintf(buf, "<other_pid>%d</other_pid>\n", other_pid);
|
|
|
|
strcat(msg_buf, buf);
|
2011-09-02 20:47:05 +00:00
|
|
|
}
|
2012-05-20 23:02:12 +00:00
|
|
|
if (_bytes_sent) {
|
|
|
|
sprintf(buf, "<bytes_sent>%f</bytes_sent>\n", _bytes_sent);
|
2011-09-16 19:16:12 +00:00
|
|
|
strcat(msg_buf, buf);
|
|
|
|
}
|
2012-05-20 23:02:12 +00:00
|
|
|
if (_bytes_received) {
|
|
|
|
sprintf(buf, "<bytes_received>%f</bytes_received>\n", _bytes_received);
|
2011-09-16 19:16:12 +00:00
|
|
|
strcat(msg_buf, buf);
|
|
|
|
}
|
|
|
|
if (app_client_shm->shm->app_status.send_msg(msg_buf)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ERR_WRITE;
|
2004-12-09 01:07:01 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 20:47:05 +00:00
|
|
|
int boinc_report_app_status(
|
|
|
|
double cpu_time,
|
|
|
|
double checkpoint_cpu_time,
|
|
|
|
double _fraction_done
|
|
|
|
){
|
|
|
|
return boinc_report_app_status_aux(
|
2011-09-16 19:16:12 +00:00
|
|
|
cpu_time, checkpoint_cpu_time, _fraction_done, 0, 0, 0
|
2011-09-02 20:47:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2006-06-09 02:33:00 +00:00
|
|
|
int boinc_get_init_data_p(APP_INIT_DATA* app_init_data) {
|
|
|
|
*app_init_data = aid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-09 23:34:11 +00:00
|
|
|
int boinc_get_init_data(APP_INIT_DATA& app_init_data) {
|
|
|
|
app_init_data = aid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-11-06 20:27:07 +00:00
|
|
|
int boinc_wu_cpu_time(double& cpu_t) {
|
|
|
|
cpu_t = last_wu_cpu_time;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-12-02 22:51:11 +00:00
|
|
|
|
2013-07-04 01:08:09 +00:00
|
|
|
// Suspend this job.
|
|
|
|
// Can be called from either timer or worker thread.
|
2013-06-05 22:22:39 +00:00
|
|
|
//
|
|
|
|
static int suspend_activities(bool called_from_worker) {
|
2013-07-04 23:00:10 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
|
|
|
char log_buf[256];
|
|
|
|
fprintf(stderr, "%s suspend_activities() called from %s\n",
|
|
|
|
boinc_msg_prefix(log_buf, sizeof(log_buf)),
|
|
|
|
called_from_worker?"worker thread":"timer thread"
|
|
|
|
);
|
|
|
|
#endif
|
2006-02-23 07:54:11 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-12 00:05:40 +00:00
|
|
|
static vector<int> pids;
|
2012-03-08 22:42:44 +00:00
|
|
|
if (options.multi_thread) {
|
2013-07-04 01:08:09 +00:00
|
|
|
if (pids.size() == 0) {
|
|
|
|
pids.push_back(GetCurrentProcessId());
|
|
|
|
}
|
2012-12-12 00:05:40 +00:00
|
|
|
suspend_or_resume_threads(pids, timer_thread_id, false, true);
|
2012-03-08 22:42:44 +00:00
|
|
|
} else {
|
|
|
|
SuspendThread(worker_thread_handle);
|
2006-02-23 07:54:11 +00:00
|
|
|
}
|
2011-04-14 22:25:38 +00:00
|
|
|
#else
|
2012-03-08 22:42:44 +00:00
|
|
|
if (options.multi_process) {
|
2012-12-12 00:05:40 +00:00
|
|
|
suspend_or_resume_descendants(false);
|
2011-04-14 22:25:38 +00:00
|
|
|
}
|
2013-06-05 22:22:39 +00:00
|
|
|
// if called from worker thread, sleep until suspension is over
|
|
|
|
// if called from time thread, don't need to do anything;
|
|
|
|
// suspension is done by signal handler in worker thread
|
|
|
|
//
|
|
|
|
if (called_from_worker) {
|
|
|
|
while (boinc_status.suspended) {
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
}
|
2006-02-23 07:54:11 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-17 18:09:30 +00:00
|
|
|
int resume_activities() {
|
2013-07-04 23:00:10 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
|
|
|
char log_buf[256];
|
|
|
|
fprintf(stderr, "%s resume_activities()\n",
|
|
|
|
boinc_msg_prefix(log_buf, sizeof(log_buf))
|
|
|
|
);
|
|
|
|
#endif
|
2006-02-23 07:54:11 +00:00
|
|
|
#ifdef _WIN32
|
2012-12-12 00:05:40 +00:00
|
|
|
static vector<int> pids;
|
2012-03-08 22:42:44 +00:00
|
|
|
if (options.multi_thread) {
|
2012-12-12 00:05:40 +00:00
|
|
|
if (pids.size() == 0) pids.push_back(GetCurrentProcessId());
|
|
|
|
suspend_or_resume_threads(pids, timer_thread_id, true, true);
|
2012-03-08 22:42:44 +00:00
|
|
|
} else {
|
|
|
|
ResumeThread(worker_thread_handle);
|
2006-02-23 07:54:11 +00:00
|
|
|
}
|
2011-04-14 22:25:38 +00:00
|
|
|
#else
|
|
|
|
if (options.multi_process) {
|
2012-12-12 00:05:40 +00:00
|
|
|
suspend_or_resume_descendants(true);
|
2011-04-14 22:25:38 +00:00
|
|
|
}
|
2006-02-23 07:54:11 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-12 18:35:19 +00:00
|
|
|
|
2005-04-27 06:55:28 +00:00
|
|
|
static void handle_upload_file_status() {
|
2012-05-09 16:11:50 +00:00
|
|
|
char path[MAXPATHLEN], buf[256], log_name[256], *p, log_buf[256];
|
2005-04-27 06:55:28 +00:00
|
|
|
std::string filename;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
relative_to_absolute("", path);
|
|
|
|
DirScanner dirscan(path);
|
|
|
|
while (dirscan.scan(filename)) {
|
2013-06-03 22:04:20 +00:00
|
|
|
strlcpy(buf, filename.c_str(), sizeof(buf));
|
2005-04-28 23:19:58 +00:00
|
|
|
if (strstr(buf, UPLOAD_FILE_STATUS_PREFIX) != buf) continue;
|
2013-06-03 22:04:20 +00:00
|
|
|
strlcpy(log_name, buf+strlen(UPLOAD_FILE_STATUS_PREFIX), sizeof(log_name));
|
2005-04-27 06:55:28 +00:00
|
|
|
FILE* f = boinc_fopen(filename.c_str(), "r");
|
2005-04-28 23:19:58 +00:00
|
|
|
if (!f) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s handle_file_upload_status: can't open %s\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), filename.c_str()
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2005-04-28 23:19:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-05-21 17:26:15 +00:00
|
|
|
p = fgets(buf, sizeof(buf), f);
|
2005-04-28 23:19:58 +00:00
|
|
|
fclose(f);
|
2007-03-04 20:52:31 +00:00
|
|
|
if (p && parse_int(buf, "<status>", status)) {
|
2005-04-28 23:19:58 +00:00
|
|
|
UPLOAD_FILE_STATUS uf;
|
|
|
|
uf.name = std::string(log_name);
|
|
|
|
uf.status = status;
|
|
|
|
upload_file_status.push_back(uf);
|
|
|
|
} else {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr, "%s handle_upload_file_status: can't parse %s\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(log_buf, sizeof(log_buf)), buf
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2005-04-28 23:19:58 +00:00
|
|
|
}
|
2005-04-27 06:55:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 18:11:38 +00:00
|
|
|
// handle trickle and file upload status messages
|
2005-04-27 06:55:28 +00:00
|
|
|
//
|
2004-08-03 09:50:24 +00:00
|
|
|
static void handle_trickle_down_msg() {
|
|
|
|
char buf[MSG_CHANNEL_SIZE];
|
2004-07-08 03:38:52 +00:00
|
|
|
if (app_client_shm->shm->trickle_down.get_msg(buf)) {
|
2006-06-07 08:56:55 +00:00
|
|
|
BOINCINFO("Received Trickle Down Message");
|
2004-07-08 03:38:52 +00:00
|
|
|
if (match_tag(buf, "<have_trickle_down/>")) {
|
|
|
|
have_trickle_down = true;
|
|
|
|
}
|
2005-04-28 23:19:58 +00:00
|
|
|
if (match_tag(buf, "<upload_file_status/>")) {
|
2005-04-27 06:55:28 +00:00
|
|
|
handle_upload_file_status();
|
|
|
|
}
|
2004-06-29 01:15:51 +00:00
|
|
|
}
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 19:46:49 +00:00
|
|
|
// This flag is set of we get a suspend request while in a critical section,
|
|
|
|
// and options.direct_process_action is set.
|
|
|
|
// As soon as we're not in the critical section we'll do the suspend.
|
|
|
|
//
|
|
|
|
static bool suspend_request = false;
|
|
|
|
|
2008-01-13 00:12:14 +00:00
|
|
|
// runs in timer thread
|
|
|
|
//
|
2004-08-03 09:50:24 +00:00
|
|
|
static void handle_process_control_msg() {
|
2010-06-21 23:36:01 +00:00
|
|
|
char buf[MSG_CHANNEL_SIZE];
|
2004-07-08 03:38:52 +00:00
|
|
|
if (app_client_shm->shm->process_control_request.get_msg(buf)) {
|
2013-06-05 22:22:39 +00:00
|
|
|
acquire_mutex();
|
2009-03-23 17:26:20 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
2013-07-04 23:00:10 +00:00
|
|
|
char log_buf[256];
|
2010-06-17 17:41:27 +00:00
|
|
|
fprintf(stderr, "%s got process control msg %s\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(log_buf, sizeof(log_buf)), buf
|
2010-06-17 17:41:27 +00:00
|
|
|
);
|
2009-03-20 23:02:31 +00:00
|
|
|
#endif
|
2004-07-08 03:38:52 +00:00
|
|
|
if (match_tag(buf, "<suspend/>")) {
|
2012-03-08 22:42:44 +00:00
|
|
|
BOINCINFO("Received suspend message");
|
2012-03-09 19:46:49 +00:00
|
|
|
if (options.direct_process_action) {
|
|
|
|
if (in_critical_section) {
|
|
|
|
suspend_request = true;
|
|
|
|
} else {
|
2012-03-08 22:42:44 +00:00
|
|
|
boinc_status.suspended = true;
|
2012-03-09 19:46:49 +00:00
|
|
|
suspend_request = false;
|
2013-06-05 22:22:39 +00:00
|
|
|
suspend_activities(false);
|
2012-03-09 19:46:49 +00:00
|
|
|
}
|
2011-06-07 05:16:12 +00:00
|
|
|
} else {
|
2012-03-09 19:46:49 +00:00
|
|
|
boinc_status.suspended = true;
|
2011-06-07 05:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-03 09:50:24 +00:00
|
|
|
if (match_tag(buf, "<resume/>")) {
|
2012-03-08 22:42:44 +00:00
|
|
|
BOINCINFO("Received resume message");
|
2012-09-30 20:30:58 +00:00
|
|
|
if (options.direct_process_action) {
|
|
|
|
if (boinc_status.suspended) {
|
|
|
|
resume_activities();
|
|
|
|
} else if (suspend_request) {
|
|
|
|
suspend_request = false;
|
|
|
|
}
|
2011-06-07 05:16:12 +00:00
|
|
|
}
|
2012-03-09 19:46:49 +00:00
|
|
|
boinc_status.suspended = false;
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
|
|
|
|
2011-06-07 05:16:12 +00:00
|
|
|
if (boinc_status.quit_request || match_tag(buf, "<quit/>")) {
|
2006-10-03 15:43:38 +00:00
|
|
|
BOINCINFO("Received quit message");
|
2004-12-05 23:52:17 +00:00
|
|
|
boinc_status.quit_request = true;
|
2011-06-07 05:16:12 +00:00
|
|
|
if (!in_critical_section && options.direct_process_action) {
|
2008-01-13 00:12:14 +00:00
|
|
|
exit_from_timer_thread(0);
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
2004-07-08 03:38:52 +00:00
|
|
|
}
|
2011-06-07 05:16:12 +00:00
|
|
|
if (boinc_status.abort_request || match_tag(buf, "<abort/>")) {
|
2006-10-03 15:43:38 +00:00
|
|
|
BOINCINFO("Received abort message");
|
2006-02-03 20:48:48 +00:00
|
|
|
boinc_status.abort_request = true;
|
2011-06-07 05:16:12 +00:00
|
|
|
if (!in_critical_section && options.direct_process_action) {
|
2006-05-08 18:35:56 +00:00
|
|
|
diagnostics_set_aborted_via_gui();
|
2006-03-23 20:23:22 +00:00
|
|
|
#if defined(_WIN32)
|
2006-03-21 12:04:14 +00:00
|
|
|
// Cause a controlled assert and dump the callstacks.
|
|
|
|
DebugBreak();
|
2006-03-23 20:23:22 +00:00
|
|
|
#elif defined(__APPLE__)
|
2006-03-23 11:38:49 +00:00
|
|
|
PrintBacktrace();
|
|
|
|
#endif
|
2013-07-10 00:43:19 +00:00
|
|
|
release_mutex();
|
2008-06-18 16:43:05 +00:00
|
|
|
exit_from_timer_thread(EXIT_ABORTED_BY_CLIENT);
|
2006-02-03 20:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
2005-05-20 00:48:52 +00:00
|
|
|
if (match_tag(buf, "<reread_app_info/>")) {
|
|
|
|
boinc_status.reread_init_data_file = true;
|
|
|
|
}
|
2005-08-17 22:08:35 +00:00
|
|
|
if (match_tag(buf, "<network_available/>")) {
|
|
|
|
have_network = 1;
|
|
|
|
}
|
2013-06-05 22:22:39 +00:00
|
|
|
release_mutex();
|
2012-03-08 22:42:44 +00:00
|
|
|
}
|
2004-06-29 01:15:51 +00:00
|
|
|
}
|
|
|
|
|
2008-01-08 18:58:44 +00:00
|
|
|
// timer handler; runs in the timer thread
|
2007-06-08 07:55:27 +00:00
|
|
|
//
|
|
|
|
static void timer_handler() {
|
2012-08-13 18:23:20 +00:00
|
|
|
char buf[512];
|
2013-07-10 00:43:19 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
|
|
|
fprintf(stderr, "%s timer handler: disabled %s; in critical section %s; finishing %s\n",
|
|
|
|
boinc_msg_prefix(buf, sizeof(buf)),
|
|
|
|
boinc_disable_timer_thread?"yes":"no",
|
|
|
|
in_critical_section?"yes":"no",
|
|
|
|
finishing?"yes":"no"
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
if (boinc_disable_timer_thread) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-04 23:00:10 +00:00
|
|
|
if (finishing) {
|
2014-03-03 23:42:04 +00:00
|
|
|
if (options.send_status_msgs) {
|
|
|
|
double cur_cpu = boinc_worker_thread_cpu_time();
|
|
|
|
last_wu_cpu_time = cur_cpu + initial_wu_cpu_time;
|
|
|
|
update_app_progress(last_wu_cpu_time, last_checkpoint_cpu_time);
|
|
|
|
}
|
2013-07-04 23:00:10 +00:00
|
|
|
boinc_disable_timer_thread = true;
|
|
|
|
return;
|
|
|
|
}
|
2005-07-23 08:10:39 +00:00
|
|
|
interrupt_count++;
|
2009-07-01 17:35:56 +00:00
|
|
|
if (!boinc_status.suspended) {
|
|
|
|
running_interrupt_count++;
|
|
|
|
}
|
2014-05-30 19:29:08 +00:00
|
|
|
// handle messages from the client
|
2004-07-09 15:29:18 +00:00
|
|
|
//
|
2004-06-29 01:15:51 +00:00
|
|
|
if (app_client_shm) {
|
2008-07-03 19:40:49 +00:00
|
|
|
if (options.check_heartbeat) {
|
|
|
|
handle_heartbeat_msg();
|
|
|
|
}
|
2014-07-04 18:11:38 +00:00
|
|
|
if (handle_trickle_downs) {
|
2004-08-03 09:50:24 +00:00
|
|
|
handle_trickle_down_msg();
|
|
|
|
}
|
2011-06-07 05:16:12 +00:00
|
|
|
if (options.handle_process_control) {
|
2007-03-15 19:08:22 +00:00
|
|
|
handle_process_control_msg();
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
2004-06-29 01:15:51 +00:00
|
|
|
}
|
2007-12-21 20:57:48 +00:00
|
|
|
if (interrupt_count % TIMERS_PER_SEC) return;
|
|
|
|
|
2009-03-23 17:26:20 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
2013-07-10 00:43:19 +00:00
|
|
|
fprintf(stderr, "%s 1 sec elapsed - doing slow actions\n", boinc_msg_prefix(buf, sizeof(buf)));
|
2009-03-20 23:02:31 +00:00
|
|
|
#endif
|
|
|
|
|
2012-01-13 19:00:16 +00:00
|
|
|
// here if we're at a one-second boundary; do slow stuff
|
2007-12-21 20:57:48 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
if (!ready_to_checkpoint) {
|
|
|
|
time_until_checkpoint -= 1;
|
|
|
|
if (time_until_checkpoint <= 0) {
|
|
|
|
ready_to_checkpoint = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 19:29:08 +00:00
|
|
|
// see if the client has died, which means we need to die too
|
2005-08-12 21:24:07 +00:00
|
|
|
// (unless we're in a critical section)
|
2004-07-09 15:29:18 +00:00
|
|
|
//
|
2014-10-31 17:37:56 +00:00
|
|
|
if (options.check_heartbeat) {
|
2012-10-11 16:41:31 +00:00
|
|
|
if (client_dead()) {
|
2013-07-10 00:43:19 +00:00
|
|
|
fprintf(stderr, "%s timer handler: client dead, exiting\n",
|
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
|
|
|
);
|
2014-10-31 17:37:56 +00:00
|
|
|
if (options.direct_process_action && !in_critical_section) {
|
2008-01-13 00:12:14 +00:00
|
|
|
exit_from_timer_thread(0);
|
2004-08-03 09:50:24 +00:00
|
|
|
} else {
|
2005-03-15 20:23:24 +00:00
|
|
|
boinc_status.no_heartbeat = true;
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
2004-06-29 01:15:51 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-31 17:37:56 +00:00
|
|
|
|
2007-03-29 18:33:28 +00:00
|
|
|
// don't bother reporting CPU time etc. if we're suspended
|
|
|
|
//
|
|
|
|
if (options.send_status_msgs && !boinc_status.suspended) {
|
2008-06-12 18:35:19 +00:00
|
|
|
double cur_cpu = boinc_worker_thread_cpu_time();
|
|
|
|
last_wu_cpu_time = cur_cpu + initial_wu_cpu_time;
|
|
|
|
update_app_progress(last_wu_cpu_time, last_checkpoint_cpu_time);
|
2004-08-03 09:50:24 +00:00
|
|
|
}
|
2008-02-21 10:50:59 +00:00
|
|
|
|
2014-07-04 18:11:38 +00:00
|
|
|
if (have_new_trickle_up || have_new_upload_file) {
|
2004-08-03 09:50:24 +00:00
|
|
|
send_trickle_up_msg();
|
2004-07-08 03:38:52 +00:00
|
|
|
}
|
2007-06-08 07:55:27 +00:00
|
|
|
if (timer_callback) {
|
|
|
|
timer_callback();
|
|
|
|
}
|
2012-01-13 19:00:16 +00:00
|
|
|
|
|
|
|
// send graphics-related messages
|
|
|
|
//
|
|
|
|
if (send_web_graphics_url && !app_client_shm->shm->graphics_reply.has_msg()) {
|
|
|
|
sprintf(buf,
|
|
|
|
"<web_graphics_url>%s</web_graphics_url>",
|
|
|
|
web_graphics_url
|
|
|
|
);
|
|
|
|
app_client_shm->shm->graphics_reply.send_msg(buf);
|
|
|
|
send_web_graphics_url = false;
|
|
|
|
}
|
|
|
|
if (send_remote_desktop_addr && !app_client_shm->shm->graphics_reply.has_msg()) {
|
|
|
|
sprintf(buf,
|
|
|
|
"<remote_desktop_addr>%s</remote_desktop_addr>",
|
|
|
|
remote_desktop_addr
|
|
|
|
);
|
|
|
|
app_client_shm->shm->graphics_reply.send_msg(buf);
|
|
|
|
send_remote_desktop_addr = false;
|
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2006-05-11 18:28:35 +00:00
|
|
|
#ifdef _WIN32
|
2006-05-15 21:01:35 +00:00
|
|
|
|
2008-03-29 22:07:50 +00:00
|
|
|
DWORD WINAPI timer_thread(void *) {
|
2007-08-10 01:51:14 +00:00
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
while (1) {
|
2008-03-29 22:07:50 +00:00
|
|
|
Sleep((int)(TIMER_PERIOD*1000));
|
2007-06-08 07:55:27 +00:00
|
|
|
timer_handler();
|
2006-05-15 21:01:35 +00:00
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
// poor man's CPU time accounting for Win9x
|
|
|
|
//
|
|
|
|
if (!boinc_status.suspended) {
|
|
|
|
nrunning_ticks++;
|
2006-05-15 17:14:15 +00:00
|
|
|
}
|
2006-05-15 21:01:35 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2006-05-15 17:14:15 +00:00
|
|
|
|
2008-01-09 17:32:02 +00:00
|
|
|
static void* timer_thread(void*) {
|
2005-08-11 21:26:02 +00:00
|
|
|
block_sigalrm();
|
2005-05-22 00:13:16 +00:00
|
|
|
while(1) {
|
2005-07-05 10:23:20 +00:00
|
|
|
boinc_sleep(TIMER_PERIOD);
|
2007-06-08 07:55:27 +00:00
|
|
|
timer_handler();
|
2005-05-22 00:13:16 +00:00
|
|
|
}
|
2005-11-21 18:34:44 +00:00
|
|
|
return 0;
|
2005-05-22 00:13:16 +00:00
|
|
|
}
|
2005-08-11 18:18:03 +00:00
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
// This SIGALRM handler gets handled only by the worker thread.
|
|
|
|
// It gets CPU time and implements sleeping.
|
|
|
|
// It must call only signal-safe functions, and must not do FP math
|
|
|
|
//
|
2008-01-09 17:32:02 +00:00
|
|
|
static void worker_signal_handler(int) {
|
2008-01-08 18:58:44 +00:00
|
|
|
#ifndef GETRUSAGE_IN_TIMER_THREAD
|
2008-01-07 23:10:17 +00:00
|
|
|
getrusage(RUSAGE_SELF, &worker_thread_ru);
|
|
|
|
#endif
|
2008-01-13 00:12:14 +00:00
|
|
|
if (worker_thread_exit_flag) {
|
|
|
|
boinc_exit(worker_thread_exit_status);
|
|
|
|
}
|
2007-03-15 19:08:22 +00:00
|
|
|
if (options.direct_process_action) {
|
2008-02-06 20:39:10 +00:00
|
|
|
while (boinc_status.suspended && in_critical_section==0) {
|
2013-07-30 18:04:09 +00:00
|
|
|
#ifdef ANDROID
|
2013-09-10 18:29:34 +00:00
|
|
|
// per-thread signal masking doesn't work
|
|
|
|
// on old (pre-4.1) versions of Android.
|
|
|
|
// If we're handling this signal in the timer thread,
|
|
|
|
// send signal explicitly to worker thread.
|
2013-07-30 18:04:09 +00:00
|
|
|
//
|
2013-08-07 16:11:45 +00:00
|
|
|
if (pthread_self() == timer_thread_handle) {
|
2013-09-10 18:29:34 +00:00
|
|
|
pthread_kill(worker_thread_handle, SIGALRM);
|
|
|
|
return;
|
2013-08-02 20:43:58 +00:00
|
|
|
}
|
2013-07-30 18:04:09 +00:00
|
|
|
#endif
|
2007-03-15 19:08:22 +00:00
|
|
|
sleep(1); // don't use boinc_sleep() because it does FP math
|
2005-08-11 18:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-15 21:01:35 +00:00
|
|
|
|
2005-05-22 23:12:09 +00:00
|
|
|
#endif
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2006-05-11 18:28:35 +00:00
|
|
|
|
2007-11-29 19:48:04 +00:00
|
|
|
// Called from the worker thread; create the timer thread
|
2005-01-28 01:58:11 +00:00
|
|
|
//
|
2008-01-13 04:16:58 +00:00
|
|
|
int start_timer_thread() {
|
2010-06-17 17:41:27 +00:00
|
|
|
char buf[256];
|
2005-01-28 01:58:11 +00:00
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
#ifdef _WIN32
|
2006-05-15 21:01:35 +00:00
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
// get the worker thread handle
|
2006-06-12 20:09:19 +00:00
|
|
|
//
|
2005-01-28 01:58:11 +00:00
|
|
|
DuplicateHandle(
|
|
|
|
GetCurrentProcess(),
|
|
|
|
GetCurrentThread(),
|
|
|
|
GetCurrentProcess(),
|
|
|
|
&worker_thread_handle,
|
|
|
|
0,
|
|
|
|
FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS
|
|
|
|
);
|
2003-03-18 19:37:09 +00:00
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
// Create the timer thread
|
2006-06-12 20:09:19 +00:00
|
|
|
//
|
2010-01-01 02:50:56 +00:00
|
|
|
if (!CreateThread(NULL, 0, timer_thread, 0, 0, &timer_thread_id)) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s start_timer_thread(): CreateThread() failed, errno %d\n",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), errno
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2008-01-09 17:32:02 +00:00
|
|
|
return errno;
|
2006-05-11 18:28:35 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 00:49:50 +00:00
|
|
|
if (!options.normal_thread_priority) {
|
|
|
|
// lower our (worker thread) priority
|
|
|
|
//
|
|
|
|
SetThreadPriority(worker_thread_handle, THREAD_PRIORITY_IDLE);
|
|
|
|
}
|
2005-05-22 00:13:16 +00:00
|
|
|
#else
|
2013-09-10 18:29:34 +00:00
|
|
|
worker_thread_handle = pthread_self();
|
2007-06-13 19:09:46 +00:00
|
|
|
pthread_attr_t thread_attrs;
|
|
|
|
pthread_attr_init(&thread_attrs);
|
2012-06-21 19:13:49 +00:00
|
|
|
pthread_attr_setstacksize(&thread_attrs, 32768);
|
2010-08-07 03:33:25 +00:00
|
|
|
int retval = pthread_create(&timer_thread_handle, &thread_attrs, timer_thread, NULL);
|
2005-05-22 00:13:16 +00:00
|
|
|
if (retval) {
|
2009-03-27 23:42:47 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s start_timer_thread(): pthread_create(): %d",
|
2010-09-15 23:03:30 +00:00
|
|
|
boinc_msg_prefix(buf, sizeof(buf)), retval
|
2009-03-27 23:42:47 +00:00
|
|
|
);
|
2006-06-12 20:09:19 +00:00
|
|
|
return retval;
|
2005-05-22 00:13:16 +00:00
|
|
|
}
|
2008-01-09 17:32:02 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-11 18:18:03 +00:00
|
|
|
|
2008-01-09 17:32:02 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
// set up a periodic SIGALRM, to be handled by the worker thread
|
|
|
|
//
|
|
|
|
static int start_worker_signals() {
|
|
|
|
int retval;
|
2005-07-05 10:23:20 +00:00
|
|
|
struct sigaction sa;
|
|
|
|
itimerval value;
|
2005-08-11 18:18:03 +00:00
|
|
|
sa.sa_handler = worker_signal_handler;
|
2005-07-05 10:23:20 +00:00
|
|
|
sa.sa_flags = SA_RESTART;
|
2005-10-02 18:23:38 +00:00
|
|
|
sigemptyset(&sa.sa_mask);
|
2005-07-05 10:23:20 +00:00
|
|
|
retval = sigaction(SIGALRM, &sa, NULL);
|
|
|
|
if (retval) {
|
2008-01-09 17:32:02 +00:00
|
|
|
perror("boinc start_timer_thread() sigaction");
|
2005-07-05 10:23:20 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2007-12-21 20:57:48 +00:00
|
|
|
value.it_value.tv_sec = 0;
|
|
|
|
value.it_value.tv_usec = (int)(TIMER_PERIOD*1e6);
|
2005-07-05 10:23:20 +00:00
|
|
|
value.it_interval = value.it_value;
|
|
|
|
retval = setitimer(ITIMER_REAL, &value, NULL);
|
|
|
|
if (retval) {
|
2008-01-09 17:32:02 +00:00
|
|
|
perror("boinc start_timer_thread() setitimer");
|
|
|
|
return retval;
|
2005-07-05 10:23:20 +00:00
|
|
|
}
|
2008-01-09 17:32:02 +00:00
|
|
|
return 0;
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
2008-01-09 17:32:02 +00:00
|
|
|
#endif
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2004-07-06 04:01:15 +00:00
|
|
|
int boinc_send_trickle_up(char* variety, char* p) {
|
2004-04-23 00:05:16 +00:00
|
|
|
FILE* f = boinc_fopen(TRICKLE_UP_FILENAME, "wb");
|
2004-01-04 06:48:40 +00:00
|
|
|
if (!f) return ERR_FOPEN;
|
2004-07-06 04:01:15 +00:00
|
|
|
fprintf(f, "<variety>%s</variety>\n", variety);
|
2014-07-28 15:58:02 +00:00
|
|
|
size_t n = 1;
|
|
|
|
if (strlen(p)) {
|
|
|
|
n = fwrite(p, strlen(p), 1, f);
|
|
|
|
}
|
2004-01-04 06:48:40 +00:00
|
|
|
fclose(f);
|
|
|
|
if (n != 1) return ERR_WRITE;
|
2004-04-23 00:05:16 +00:00
|
|
|
have_new_trickle_up = true;
|
2004-01-04 06:48:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-11-06 20:27:07 +00:00
|
|
|
|
2004-12-22 15:02:03 +00:00
|
|
|
int boinc_time_to_checkpoint() {
|
2005-01-19 15:54:04 +00:00
|
|
|
if (ready_to_checkpoint) {
|
2008-02-06 20:39:10 +00:00
|
|
|
boinc_begin_critical_section();
|
2005-05-22 23:00:35 +00:00
|
|
|
return 1;
|
2003-11-06 20:27:07 +00:00
|
|
|
}
|
2004-12-22 15:02:03 +00:00
|
|
|
return 0;
|
2003-11-06 20:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_checkpoint_completed() {
|
2004-12-05 23:52:17 +00:00
|
|
|
double cur_cpu;
|
2007-06-08 07:55:27 +00:00
|
|
|
cur_cpu = boinc_worker_thread_cpu_time();
|
2003-11-06 20:27:07 +00:00
|
|
|
last_wu_cpu_time = cur_cpu + aid.wu_cpu_time;
|
|
|
|
last_checkpoint_cpu_time = last_wu_cpu_time;
|
2012-08-10 22:20:41 +00:00
|
|
|
time_until_checkpoint = min_checkpoint_period();
|
2008-02-06 20:39:10 +00:00
|
|
|
boinc_end_critical_section();
|
2003-11-06 20:27:07 +00:00
|
|
|
ready_to_checkpoint = false;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-12 21:24:07 +00:00
|
|
|
void boinc_begin_critical_section() {
|
2013-07-04 23:00:10 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
|
|
|
char buf[256];
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s begin_critical_section\n",
|
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
|
|
|
);
|
|
|
|
#endif
|
2008-02-06 20:39:10 +00:00
|
|
|
in_critical_section++;
|
2005-08-12 21:24:07 +00:00
|
|
|
}
|
2007-03-15 19:08:22 +00:00
|
|
|
|
2005-08-12 21:24:07 +00:00
|
|
|
void boinc_end_critical_section() {
|
2013-07-04 23:00:10 +00:00
|
|
|
#ifdef DEBUG_BOINC_API
|
|
|
|
char buf[256];
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s end_critical_section\n",
|
|
|
|
boinc_msg_prefix(buf, sizeof(buf))
|
|
|
|
);
|
|
|
|
#endif
|
2008-02-06 20:39:10 +00:00
|
|
|
in_critical_section--;
|
|
|
|
if (in_critical_section < 0) {
|
|
|
|
in_critical_section = 0; // just in case
|
|
|
|
}
|
2013-06-05 22:42:17 +00:00
|
|
|
|
|
|
|
if (in_critical_section) return;
|
|
|
|
|
|
|
|
// We're out of the critical section.
|
2013-06-05 22:22:39 +00:00
|
|
|
// See if we got suspend/quit/abort while in critical section,
|
2013-06-05 22:42:17 +00:00
|
|
|
// and handle them here.
|
2013-06-05 22:22:39 +00:00
|
|
|
//
|
2013-06-05 22:42:17 +00:00
|
|
|
if (options.direct_process_action) {
|
2014-10-31 17:37:56 +00:00
|
|
|
if (boinc_status.no_heartbeat) {
|
|
|
|
boinc_exit(0);
|
|
|
|
}
|
|
|
|
if (boinc_status.quit_request) {
|
|
|
|
boinc_exit(0);
|
|
|
|
}
|
|
|
|
if (boinc_status.abort_request) {
|
|
|
|
boinc_exit(EXIT_ABORTED_BY_CLIENT);
|
|
|
|
}
|
2013-06-05 22:22:39 +00:00
|
|
|
acquire_mutex();
|
2013-06-05 22:42:17 +00:00
|
|
|
if (suspend_request) {
|
|
|
|
suspend_request = false;
|
|
|
|
boinc_status.suspended = true;
|
2013-07-04 01:08:09 +00:00
|
|
|
release_mutex();
|
2013-06-05 22:42:17 +00:00
|
|
|
suspend_activities(true);
|
2013-07-04 01:08:09 +00:00
|
|
|
} else {
|
|
|
|
release_mutex();
|
2013-06-05 22:42:17 +00:00
|
|
|
}
|
2013-06-05 22:22:39 +00:00
|
|
|
}
|
2005-08-12 21:24:07 +00:00
|
|
|
}
|
|
|
|
|
2003-11-06 20:27:07 +00:00
|
|
|
int boinc_fraction_done(double x) {
|
|
|
|
fraction_done = x;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-07-02 05:06:30 +00:00
|
|
|
|
2006-01-06 23:38:49 +00:00
|
|
|
int boinc_receive_trickle_down(char* buf, int len) {
|
2004-07-02 05:06:30 +00:00
|
|
|
std::string filename;
|
2012-05-09 16:11:50 +00:00
|
|
|
char path[MAXPATHLEN];
|
2004-08-03 09:50:24 +00:00
|
|
|
|
2014-07-04 18:11:38 +00:00
|
|
|
handle_trickle_downs = true;
|
2004-08-03 09:50:24 +00:00
|
|
|
|
2004-07-02 05:06:30 +00:00
|
|
|
if (have_trickle_down) {
|
2004-08-03 09:50:24 +00:00
|
|
|
relative_to_absolute("", path);
|
2004-07-06 04:01:15 +00:00
|
|
|
DirScanner dirscan(path);
|
2004-07-02 05:06:30 +00:00
|
|
|
while (dirscan.scan(filename)) {
|
|
|
|
if (strstr(filename.c_str(), "trickle_down")) {
|
|
|
|
strncpy(buf, filename.c_str(), len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2004-10-22 05:46:25 +00:00
|
|
|
have_trickle_down = false;
|
2004-07-02 05:06:30 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-04-27 06:55:28 +00:00
|
|
|
int boinc_upload_file(std::string& name) {
|
|
|
|
char buf[256];
|
2005-04-28 23:19:58 +00:00
|
|
|
std::string pname;
|
|
|
|
int retval;
|
2005-04-27 06:55:28 +00:00
|
|
|
|
2005-04-28 23:19:58 +00:00
|
|
|
retval = boinc_resolve_filename_s(name.c_str(), pname);
|
|
|
|
if (retval) return retval;
|
|
|
|
sprintf(buf, "%s%s", UPLOAD_FILE_REQ_PREFIX, name.c_str());
|
2005-04-27 06:55:28 +00:00
|
|
|
FILE* f = boinc_fopen(buf, "w");
|
|
|
|
if (!f) return ERR_FOPEN;
|
|
|
|
have_new_upload_file = true;
|
2005-05-04 22:38:14 +00:00
|
|
|
fclose(f);
|
2014-07-04 18:11:38 +00:00
|
|
|
|
|
|
|
// file upload status messages are on same channel as
|
|
|
|
// trickle down messages, so listen to that channel
|
|
|
|
//
|
|
|
|
handle_trickle_downs = true;
|
|
|
|
|
2005-04-27 06:55:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_upload_status(std::string& name) {
|
|
|
|
for (unsigned int i=0; i<upload_file_status.size(); i++) {
|
|
|
|
UPLOAD_FILE_STATUS& ufs = upload_file_status[i];
|
|
|
|
if (ufs.name == name) {
|
|
|
|
return ufs.status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2005-08-17 22:08:35 +00:00
|
|
|
void boinc_need_network() {
|
|
|
|
want_network = 1;
|
|
|
|
have_network = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_network_poll() {
|
|
|
|
return have_network?0:1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void boinc_network_done() {
|
|
|
|
want_network = 0;
|
|
|
|
}
|
|
|
|
|
2005-08-11 21:26:02 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
// block SIGALRM, so that the worker thread will be forced to handle it
|
|
|
|
//
|
2008-01-09 17:32:02 +00:00
|
|
|
static void block_sigalrm() {
|
2005-08-11 21:26:02 +00:00
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGALRM);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &mask, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-06-08 07:55:27 +00:00
|
|
|
void boinc_register_timer_callback(FUNC_PTR p) {
|
|
|
|
timer_callback = p;
|
|
|
|
}
|
|
|
|
|
2007-11-30 04:14:48 +00:00
|
|
|
double boinc_get_fraction_done() {
|
|
|
|
return fraction_done;
|
|
|
|
}
|
|
|
|
|
2009-07-01 17:35:56 +00:00
|
|
|
double boinc_elapsed_time() {
|
|
|
|
return running_interrupt_count*TIMER_PERIOD;
|
|
|
|
}
|
2011-12-26 03:10:42 +00:00
|
|
|
|
|
|
|
void boinc_web_graphics_url(char* url) {
|
2012-01-12 20:57:23 +00:00
|
|
|
if (standalone) return;
|
2013-06-03 22:04:20 +00:00
|
|
|
strlcpy(web_graphics_url, url, sizeof(web_graphics_url));
|
2012-01-13 19:00:16 +00:00
|
|
|
send_web_graphics_url = true;
|
2012-01-12 20:57:23 +00:00
|
|
|
}
|
2012-01-13 15:21:00 +00:00
|
|
|
|
2012-01-13 19:00:16 +00:00
|
|
|
void boinc_remote_desktop_addr(char* addr) {
|
2012-01-13 15:21:00 +00:00
|
|
|
if (standalone) return;
|
2013-06-03 22:04:20 +00:00
|
|
|
strlcpy(remote_desktop_addr, addr, sizeof(remote_desktop_addr));
|
2012-01-13 19:00:16 +00:00
|
|
|
send_remote_desktop_addr = true;
|
2012-01-13 15:21:00 +00:00
|
|
|
}
|