2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2004-11-14 08:29:32 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This 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 2.1 of the License, or (at your option) any later version.
|
2003-07-03 00:48:43 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// 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-04-30 22:22:54 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2003-07-01 20:37:09 +00:00
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-16 23:29:48 +00:00
|
|
|
#define M_LN2 0.693147180559945309417
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
|
|
|
|
2004-03-08 05:17:56 +00:00
|
|
|
#ifndef _WIN32
|
2004-06-13 01:28:54 +00:00
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cmath>
|
|
|
|
#include <algorithm>
|
2004-06-13 01:47:03 +00:00
|
|
|
#include <iterator>
|
2004-06-13 22:34:07 +00:00
|
|
|
#include <iostream>
|
2004-06-13 01:28:54 +00:00
|
|
|
#include <fstream>
|
2004-06-13 22:43:52 +00:00
|
|
|
#include <cctype>
|
2002-12-16 23:51:33 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2004-03-04 11:41:43 +00:00
|
|
|
#include <sys/time.h>
|
2004-05-24 21:26:25 +00:00
|
|
|
#include <sys/resource.h>
|
2004-03-04 11:41:43 +00:00
|
|
|
#include <unistd.h>
|
2003-02-14 19:02:44 +00:00
|
|
|
#ifdef HAVE_SYS_FILE_H
|
2003-02-14 07:45:43 +00:00
|
|
|
#include <sys/file.h>
|
2003-02-14 19:02:44 +00:00
|
|
|
#endif
|
2004-03-08 05:17:56 +00:00
|
|
|
#endif
|
2002-06-06 18:50:12 +00:00
|
|
|
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2004-08-05 18:58:22 +00:00
|
|
|
#include "filesys.h"
|
2002-08-09 21:43:19 +00:00
|
|
|
#include "util.h"
|
2002-07-11 01:09:53 +00:00
|
|
|
|
2004-04-30 23:18:56 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
#include "fcgi_stdio.h"
|
|
|
|
#endif
|
|
|
|
|
2004-06-30 22:27:19 +00:00
|
|
|
using std::min;
|
2004-06-30 22:16:26 +00:00
|
|
|
using std::string;
|
|
|
|
|
2004-06-09 20:04:09 +00:00
|
|
|
#if !defined(HAVE_STRLCPY)
|
|
|
|
size_t strlcpy(char *dst, const char *src, size_t size) {
|
|
|
|
size_t ret = strlen(src);
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
size_t len = (ret >= size) ? size-1 : ret;
|
|
|
|
memcpy(dst, src, len);
|
|
|
|
dst[len] = '\0';
|
2004-06-09 02:34:13 +00:00
|
|
|
}
|
2004-06-09 20:04:09 +00:00
|
|
|
|
|
|
|
return ret;
|
2004-06-09 02:34:13 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-09 20:04:09 +00:00
|
|
|
#if !defined(HAVE_STRLCAT)
|
|
|
|
size_t strlcat(char *dst, const char *src, size_t size) {
|
|
|
|
size_t dst_len = strlen(dst);
|
|
|
|
size_t src_len = strlen(src);
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
size_t len = (src_len >= size-dst_len) ? (size-dst_len-1) : src_len;
|
|
|
|
memcpy(&dst[dst_len], src, len);
|
|
|
|
dst[dst_len + len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst_len + src_len;
|
|
|
|
}
|
|
|
|
#endif // !HAVE_STRLCAT
|
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
// Converts a double precision time (where the value of 1 represents
|
|
|
|
// a day) into a string. smallest_timescale determines the smallest
|
|
|
|
// unit of time division used
|
|
|
|
// smallest_timescale: 0=seconds, 1=minutes, 2=hours, 3=days, 4=years
|
|
|
|
//
|
2003-10-08 18:15:14 +00:00
|
|
|
int ndays_to_string (double x, int smallest_timescale, char *buf) {
|
2002-09-16 20:11:52 +00:00
|
|
|
double years, days, hours, minutes, seconds;
|
|
|
|
char year_buf[64], day_buf[16], hour_buf[16], min_buf[16], sec_buf[16];
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2003-10-21 04:06:55 +00:00
|
|
|
if (x < 0 || buf == NULL) return ERR_NULL;
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
years = x / 365.25;
|
|
|
|
days = fmod(x, 365.25);
|
|
|
|
hours = fmod(x*24, 24);
|
|
|
|
minutes = fmod(x*24*60, 60);
|
|
|
|
seconds = fmod(x*24*60*60, 60);
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
if (smallest_timescale==4) {
|
|
|
|
sprintf( year_buf, "%.3f yr ", years );
|
|
|
|
} else if (years > 1 && smallest_timescale < 4) {
|
|
|
|
sprintf( year_buf, "%d yr ", (int)years );
|
|
|
|
} else {
|
|
|
|
strcpy( year_buf, "" );
|
|
|
|
}
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
if (smallest_timescale==3) {
|
2003-04-01 18:49:54 +00:00
|
|
|
sprintf( day_buf, "%.2f day%s ", days, (days>1?"s":"") );
|
2002-09-16 20:11:52 +00:00
|
|
|
} else if (days > 1 && smallest_timescale < 3) {
|
2003-04-01 18:49:54 +00:00
|
|
|
sprintf( day_buf, "%d day%s ", (int)days, (days>1?"s":"") );
|
2002-09-16 20:11:52 +00:00
|
|
|
} else {
|
|
|
|
strcpy( day_buf, "" );
|
|
|
|
}
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
if (smallest_timescale==2) {
|
|
|
|
sprintf( hour_buf, "%.2f hr ", hours );
|
|
|
|
} else if (hours > 1 && smallest_timescale < 2) {
|
|
|
|
sprintf( hour_buf, "%d hr ", (int)hours );
|
|
|
|
} else {
|
|
|
|
strcpy( hour_buf, "" );
|
|
|
|
}
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
if (smallest_timescale==1) {
|
|
|
|
sprintf( min_buf, "%.2f min ", minutes );
|
|
|
|
} else if (minutes > 1 && smallest_timescale < 1) {
|
|
|
|
sprintf( min_buf, "%d min ", (int)minutes );
|
|
|
|
} else {
|
|
|
|
strcpy( min_buf, "" );
|
|
|
|
}
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
if (smallest_timescale==0) {
|
|
|
|
sprintf( sec_buf, "%.2f sec ", seconds );
|
|
|
|
} else if (seconds > 1 && smallest_timescale < 0) {
|
|
|
|
sprintf( sec_buf, "%d sec ", (int)seconds );
|
|
|
|
} else {
|
|
|
|
strcpy( sec_buf, "" );
|
|
|
|
}
|
|
|
|
// the "-0.05" below is to prevent it from printing 60.0 sec
|
|
|
|
// when the real value is e.g. 59.91
|
|
|
|
//
|
|
|
|
sprintf(buf, "%s%s%s%s%s", year_buf, day_buf, hour_buf, min_buf, sec_buf);
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-06 21:23:14 +00:00
|
|
|
// Convert nbytes into a string. If total_bytes is non-zero,
|
|
|
|
// convert the two into a fractional display (i.e. 4/16 KB)
|
|
|
|
//
|
2003-10-08 18:15:14 +00:00
|
|
|
void nbytes_to_string(double nbytes, double total_bytes, char* str, int len) {
|
2003-06-06 21:23:14 +00:00
|
|
|
char buf[256];
|
|
|
|
double xTera = (1024.0*1024.0*1024.0*1024.0);
|
|
|
|
double xGiga = (1024.0*1024.0*1024.0);
|
|
|
|
double xMega = (1024.0*1024.0);
|
|
|
|
double xKilo = (1024.0);
|
|
|
|
|
|
|
|
if (total_bytes != 0) {
|
|
|
|
if (total_bytes >= xTera) {
|
|
|
|
sprintf(buf, "%0.2f/%0.2f TB", nbytes/xTera, total_bytes/xTera);
|
|
|
|
} else if (total_bytes >= xGiga) {
|
|
|
|
sprintf(buf, "%0.2f/%0.2f GB", nbytes/xGiga, total_bytes/xGiga);
|
|
|
|
} else if (total_bytes >= xMega) {
|
|
|
|
sprintf(buf, "%0.2f/%0.2f MB", nbytes/xMega, total_bytes/xMega);
|
|
|
|
} else if (total_bytes >= xKilo) {
|
|
|
|
sprintf(buf, "%0.2f/%0.2f KB", nbytes/xKilo, total_bytes/xKilo);
|
|
|
|
} else {
|
|
|
|
sprintf(buf, "%0.0f/%0.0f bytes", nbytes, total_bytes);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (nbytes >= xTera) {
|
|
|
|
sprintf(buf, "%0.2f TB", nbytes/xTera);
|
|
|
|
} else if (nbytes >= xGiga) {
|
|
|
|
sprintf(buf, "%0.2f GB", nbytes/xGiga);
|
|
|
|
} else if (nbytes >= xMega) {
|
|
|
|
sprintf(buf, "%0.2f MB", nbytes/xMega);
|
|
|
|
} else if (nbytes >= xKilo) {
|
|
|
|
sprintf(buf, "%0.2f KB", nbytes/xKilo);
|
|
|
|
} else {
|
|
|
|
sprintf(buf, "%0.0f bytes", nbytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
safe_strncpy(str, buf, len);
|
|
|
|
}
|
2002-09-16 20:11:52 +00:00
|
|
|
|
2004-12-01 20:56:20 +00:00
|
|
|
#define EPOCHFILETIME_SEC (11644473600.)
|
|
|
|
#define TEN_MILLION 10000000.
|
2004-12-10 19:17:32 +00:00
|
|
|
|
|
|
|
// return time of day (seconds since 1970) as a double
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
|
|
|
double dtime() {
|
2002-08-15 21:52:04 +00:00
|
|
|
#ifdef _WIN32
|
2003-06-06 17:43:45 +00:00
|
|
|
LARGE_INTEGER time;
|
|
|
|
FILETIME sysTime;
|
2004-12-01 20:56:20 +00:00
|
|
|
double t;
|
2003-06-06 17:43:45 +00:00
|
|
|
GetSystemTimeAsFileTime(&sysTime);
|
2003-02-28 23:30:05 +00:00
|
|
|
time.LowPart = sysTime.dwLowDateTime;
|
|
|
|
time.HighPart = sysTime.dwHighDateTime; // Time is in 100 ns units
|
2004-12-01 20:56:20 +00:00
|
|
|
t = (double)time.QuadPart; // Convert to 1 s units
|
|
|
|
t /= TEN_MILLION; /* In seconds */
|
|
|
|
t -= EPOCHFILETIME_SEC; /* Offset to the Epoch time */
|
|
|
|
return t;
|
2002-08-15 21:52:04 +00:00
|
|
|
#else
|
2002-04-30 22:22:54 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
return tv.tv_sec + (tv.tv_usec/1.e6);
|
2002-08-15 21:52:04 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2002-06-06 18:50:12 +00:00
|
|
|
|
2005-04-04 06:02:36 +00:00
|
|
|
// return time today 0:00 in seconds since 1970 as a double
|
|
|
|
//
|
|
|
|
double dday() {
|
|
|
|
const double seconds_per_day=24*60*60;
|
|
|
|
const double now=dtime();
|
|
|
|
|
|
|
|
return (now-fmod(now,seconds_per_day));
|
|
|
|
}
|
|
|
|
|
2004-12-01 20:56:20 +00:00
|
|
|
// do sanity check on a time, replace with now if bad
|
|
|
|
// We switched to using all UNIX times on 12/1/04.
|
|
|
|
// During the transition, times in client_state.xml may be
|
2004-12-01 21:02:49 +00:00
|
|
|
// in Windows (1601-based) format. Fix them here.
|
2004-12-01 20:56:20 +00:00
|
|
|
//
|
|
|
|
void validate_time(double& t) {
|
|
|
|
if (t==0) return;
|
|
|
|
double now = dtime();
|
|
|
|
if (t > now+86400*1000) {
|
|
|
|
t -= EPOCHFILETIME_SEC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-15 21:52:04 +00:00
|
|
|
// sleep for a specified number of seconds
|
|
|
|
//
|
2003-03-11 22:18:01 +00:00
|
|
|
void boinc_sleep(double seconds) {
|
2002-06-06 18:50:12 +00:00
|
|
|
#ifdef _WIN32
|
2003-03-11 22:18:01 +00:00
|
|
|
::Sleep((int)(1000*seconds));
|
2002-06-06 18:50:12 +00:00
|
|
|
#else
|
2004-06-09 02:34:13 +00:00
|
|
|
unsigned int rem = (int) seconds;
|
|
|
|
while (1) {
|
|
|
|
rem = sleep(rem);
|
2004-11-02 23:12:29 +00:00
|
|
|
if (rem == 0) break;
|
|
|
|
if (rem > seconds) break; // paranoia
|
2004-06-09 02:34:13 +00:00
|
|
|
}
|
2003-09-07 03:11:03 +00:00
|
|
|
int x = (int)fmod(seconds*1000000,1000000);
|
|
|
|
if (x) usleep(x);
|
2002-06-06 18:50:12 +00:00
|
|
|
#endif
|
2002-08-15 21:52:04 +00:00
|
|
|
}
|
2002-10-22 22:44:52 +00:00
|
|
|
|
|
|
|
// take a string containing some space separated words.
|
|
|
|
// return an array of pointers to the null-terminated words.
|
|
|
|
// Modifies the string arg.
|
|
|
|
// Returns argc
|
|
|
|
// TODO: use strtok here
|
2003-02-28 23:42:25 +00:00
|
|
|
|
|
|
|
#define NOT_IN_TOKEN 0
|
|
|
|
#define IN_SINGLE_QUOTED_TOKEN 1
|
|
|
|
#define IN_DOUBLE_QUOTED_TOKEN 2
|
|
|
|
#define IN_UNQUOTED_TOKEN 3
|
|
|
|
|
2002-10-22 22:44:52 +00:00
|
|
|
int parse_command_line(char* p, char** argv) {
|
2003-02-28 23:42:25 +00:00
|
|
|
int state = NOT_IN_TOKEN;
|
2002-10-22 22:44:52 +00:00
|
|
|
int argc=0;
|
|
|
|
|
|
|
|
while (*p) {
|
2003-02-28 23:42:25 +00:00
|
|
|
switch(state) {
|
|
|
|
case NOT_IN_TOKEN:
|
|
|
|
if (isspace(*p)) {
|
|
|
|
} else if (*p == '\'') {
|
|
|
|
p++;
|
|
|
|
argv[argc++] = p;
|
|
|
|
state = IN_SINGLE_QUOTED_TOKEN;
|
|
|
|
break;
|
|
|
|
} else if (*p == '\"') {
|
|
|
|
p++;
|
|
|
|
argv[argc++] = p;
|
|
|
|
state = IN_DOUBLE_QUOTED_TOKEN;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
argv[argc++] = p;
|
|
|
|
state = IN_UNQUOTED_TOKEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IN_SINGLE_QUOTED_TOKEN:
|
|
|
|
if (*p == '\'') {
|
|
|
|
*p = 0;
|
|
|
|
state = NOT_IN_TOKEN;
|
2002-10-22 22:44:52 +00:00
|
|
|
}
|
2003-02-28 23:42:25 +00:00
|
|
|
break;
|
|
|
|
case IN_DOUBLE_QUOTED_TOKEN:
|
|
|
|
if (*p == '\"') {
|
|
|
|
*p = 0;
|
|
|
|
state = NOT_IN_TOKEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IN_UNQUOTED_TOKEN:
|
|
|
|
if (isspace(*p)) {
|
|
|
|
*p = 0;
|
|
|
|
state = NOT_IN_TOKEN;
|
|
|
|
}
|
|
|
|
break;
|
2002-10-22 22:44:52 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
2003-02-28 23:42:25 +00:00
|
|
|
argv[argc] = 0;
|
2002-10-22 22:44:52 +00:00
|
|
|
return argc;
|
|
|
|
}
|
|
|
|
|
2003-01-30 23:03:52 +00:00
|
|
|
static char x2c(char *what) {
|
|
|
|
register char digit;
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2003-01-30 23:03:52 +00:00
|
|
|
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
|
|
|
|
digit *= 16;
|
|
|
|
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
|
|
|
|
return(digit);
|
|
|
|
}
|
|
|
|
|
2003-03-08 23:49:35 +00:00
|
|
|
void c2x(char *what) {
|
2003-01-30 23:03:52 +00:00
|
|
|
char buf[3];
|
|
|
|
char num = atoi(what);
|
|
|
|
char d1 = num / 16;
|
|
|
|
char d2 = num % 16;
|
|
|
|
int abase1, abase2;
|
|
|
|
if (d1 < 10) abase1 = 48;
|
|
|
|
else abase1 = 55;
|
|
|
|
if (d2 < 10) abase2 = 48;
|
|
|
|
else abase2 = 55;
|
|
|
|
buf[0] = d1+abase1;
|
|
|
|
buf[1] = d2+abase2;
|
|
|
|
buf[2] = 0;
|
|
|
|
|
|
|
|
strcpy(what, buf);
|
|
|
|
}
|
|
|
|
|
2004-01-21 07:07:16 +00:00
|
|
|
// remove whitespace from start and end of a string
|
|
|
|
//
|
2003-05-13 23:10:06 +00:00
|
|
|
void strip_whitespace(char *str) {
|
2004-01-21 07:07:16 +00:00
|
|
|
int n;
|
2004-08-20 18:29:08 +00:00
|
|
|
while (1) {
|
|
|
|
if (!str[0]) break;
|
|
|
|
if (!isascii(str[0])) break;
|
|
|
|
if (!isspace(str[0])) break;
|
2004-01-21 07:07:16 +00:00
|
|
|
strcpy(str, str+1);
|
|
|
|
}
|
|
|
|
while (1) {
|
2004-03-06 09:45:25 +00:00
|
|
|
n = (int)strlen(str);
|
2004-01-21 07:07:16 +00:00
|
|
|
if (n == 0) break;
|
|
|
|
if (!isascii(str[n-1])) break;
|
|
|
|
if (!isspace(str[n-1])) break;
|
|
|
|
str[n-1] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void strip_whitespace(string& str) {
|
|
|
|
int n;
|
2004-08-20 18:29:08 +00:00
|
|
|
while (1) {
|
|
|
|
if (str.length() == 0) break;
|
|
|
|
if (!isascii(str[0])) break;
|
|
|
|
if (!isspace(str[0])) break;
|
2004-01-21 07:07:16 +00:00
|
|
|
str.erase(0, 1);
|
|
|
|
}
|
|
|
|
while (1) {
|
2004-03-06 09:45:25 +00:00
|
|
|
n = (int)str.length();
|
2004-01-21 07:07:16 +00:00
|
|
|
if (n == 0) break;
|
|
|
|
if (!isascii(str[n-1])) break;
|
|
|
|
if (!isspace(str[n-1])) break;
|
|
|
|
str.erase(n-1, 1);
|
2003-05-13 23:10:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-06 19:18:01 +00:00
|
|
|
#if 0
|
2005-03-10 22:05:42 +00:00
|
|
|
bool starts_with(const char* str, const char* prefix) {
|
2005-03-09 00:16:38 +00:00
|
|
|
if (strstr(str, prefix) == str) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-03-10 22:05:42 +00:00
|
|
|
bool ends_with(const char* str, const char* suffix) {
|
2005-03-15 00:00:56 +00:00
|
|
|
size_t nsuff = strlen(suffix);
|
|
|
|
size_t nstr = strlen(str);
|
2005-03-09 00:16:38 +00:00
|
|
|
|
|
|
|
if (nsuff > nstr) return false;
|
|
|
|
if (strcmp(str+nstr-nsuff, suffix)) return false;
|
|
|
|
return true;
|
|
|
|
}
|
2005-06-06 19:18:01 +00:00
|
|
|
#endif
|
2005-03-09 00:16:38 +00:00
|
|
|
|
2003-01-30 23:03:52 +00:00
|
|
|
void unescape_url(char *url) {
|
2005-03-15 00:00:56 +00:00
|
|
|
int x,y;
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2004-08-12 10:13:01 +00:00
|
|
|
for (x=0,y=0;url[y];++x,++y) {
|
|
|
|
if ((url[x] = url[y]) == '%') {
|
2003-01-30 23:03:52 +00:00
|
|
|
url[x] = x2c(&url[y+1]);
|
|
|
|
y+=2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
url[x] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void escape_url(char *in, char*out) {
|
|
|
|
int x, y;
|
|
|
|
for (x=0, y=0; in[x]; ++x) {
|
|
|
|
if (isalnum(in[x])) {
|
|
|
|
out[y] = in[x];
|
|
|
|
++y;
|
|
|
|
} else {
|
|
|
|
out[y] = '%';
|
|
|
|
++y;
|
|
|
|
out[y] = 0;
|
|
|
|
char buf[256];
|
|
|
|
sprintf(buf, "%d", (char)in[x]);
|
|
|
|
c2x(buf);
|
|
|
|
strcat(out, buf);
|
|
|
|
y += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out[y] = 0;
|
|
|
|
}
|
|
|
|
|
2003-05-20 00:03:39 +00:00
|
|
|
// Escape a URL for the project directory, cutting off the "http://",
|
|
|
|
// converting '\' '/' and ' ' to '_',
|
|
|
|
// and converting the non alphanumeric characters to %XY
|
|
|
|
// where XY is their hexadecimal equivalent
|
|
|
|
//
|
|
|
|
void escape_url_readable(char *in, char* out) {
|
|
|
|
int x, y;
|
|
|
|
char *temp;
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2003-05-20 00:03:39 +00:00
|
|
|
temp = strstr(in,"://");
|
|
|
|
if (temp) {
|
|
|
|
in = temp + strlen("://");
|
|
|
|
}
|
|
|
|
for (x=0, y=0; in[x]; ++x) {
|
|
|
|
if (isalnum(in[x]) || in[x]=='.' || in[x]=='-' || in[x]=='_') {
|
|
|
|
out[y] = in[x];
|
|
|
|
++y;
|
|
|
|
} else {
|
2003-08-22 23:09:40 +00:00
|
|
|
out[y] = '_';
|
2003-05-20 00:03:39 +00:00
|
|
|
++y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out[y] = 0;
|
|
|
|
}
|
2004-06-13 23:02:45 +00:00
|
|
|
|
2003-06-24 00:17:36 +00:00
|
|
|
// Canonicalize a master url.
|
2005-02-27 20:06:51 +00:00
|
|
|
// - Convert the first part of a URL (before the "://") to http://,
|
2004-08-13 10:56:33 +00:00
|
|
|
// or prepend it
|
|
|
|
// - Remove double slashes in the rest
|
2003-06-24 00:17:36 +00:00
|
|
|
// - Add a trailing slash if necessary
|
2003-06-06 17:43:45 +00:00
|
|
|
//
|
2004-08-12 14:58:14 +00:00
|
|
|
void canonicalize_master_url(char* url) {
|
|
|
|
char buf[1024];
|
2004-10-25 20:16:30 +00:00
|
|
|
size_t n;
|
2004-08-12 14:58:14 +00:00
|
|
|
|
2005-02-27 20:06:51 +00:00
|
|
|
char *p = strstr(url, "://");
|
2004-08-12 14:58:14 +00:00
|
|
|
if (p) {
|
2005-02-27 20:06:51 +00:00
|
|
|
strcpy(buf, p+3);
|
2004-08-12 14:58:14 +00:00
|
|
|
} else {
|
|
|
|
strcpy(buf, url);
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
p = strstr(buf, "//");
|
|
|
|
if (!p) break;
|
|
|
|
strcpy(p, p+1);
|
|
|
|
}
|
|
|
|
n = strlen(buf);
|
|
|
|
if (buf[n-1] != '/') {
|
|
|
|
strcat(buf, "/");
|
|
|
|
}
|
|
|
|
sprintf(url, "http://%s", buf);
|
|
|
|
}
|
|
|
|
|
2005-02-27 20:06:51 +00:00
|
|
|
// is the string a valid master URL, in canonical form?
|
|
|
|
//
|
|
|
|
bool valid_master_url(char* buf) {
|
|
|
|
char* p, *q;
|
2005-03-15 00:00:56 +00:00
|
|
|
size_t n;
|
2003-06-06 17:43:45 +00:00
|
|
|
|
2005-02-27 20:06:51 +00:00
|
|
|
p = strstr(buf, "http://");
|
|
|
|
if (p != buf) return false;
|
|
|
|
q = p+strlen("http://");
|
|
|
|
p = strstr(q, ".");
|
|
|
|
if (!p) return false;
|
|
|
|
if (p == q) return false;
|
|
|
|
q = p+1;
|
|
|
|
p = strstr(q, "/");
|
|
|
|
if (!p) return false;
|
|
|
|
if (p == q) return false;
|
|
|
|
n = strlen(buf);
|
|
|
|
if (buf[n-1] != '/') return false;
|
2005-02-28 20:17:37 +00:00
|
|
|
return true;
|
2004-07-11 21:54:00 +00:00
|
|
|
}
|
|
|
|
|
2003-08-21 00:07:03 +00:00
|
|
|
void safe_strncpy(char* dst, const char* src, int len) {
|
2003-04-03 18:35:40 +00:00
|
|
|
strncpy(dst, src, len);
|
2003-03-06 00:42:18 +00:00
|
|
|
dst[len-1]=0;
|
|
|
|
}
|
2003-03-06 17:42:49 +00:00
|
|
|
|
2004-10-25 20:16:30 +00:00
|
|
|
char* time_to_string(double t) {
|
2003-07-01 00:56:18 +00:00
|
|
|
static char buf[100];
|
2004-10-25 20:16:30 +00:00
|
|
|
time_t x = (time_t)t;
|
2004-03-23 01:44:47 +00:00
|
|
|
struct tm* tm = localtime(&x);
|
2003-07-01 00:56:18 +00:00
|
|
|
strftime(buf, sizeof(buf)-1, "%Y-%m-%d %H:%M:%S", tm);
|
|
|
|
return buf;
|
2003-03-06 17:42:49 +00:00
|
|
|
}
|
2003-06-28 00:15:42 +00:00
|
|
|
|
|
|
|
// set by command line
|
|
|
|
bool debug_fake_exponential_backoff = false;
|
|
|
|
double debug_total_exponential_backoff = 0;
|
2003-07-09 23:54:45 +00:00
|
|
|
static int count_debug_fake_exponential_backoff = 0;
|
|
|
|
static const int max_debug_fake_exponential_backoff = 1000; // safety limit
|
2003-06-28 00:15:42 +00:00
|
|
|
|
2005-01-12 00:50:32 +00:00
|
|
|
// return a random double in the range [MIN,min(e^n,MAX))
|
2004-10-25 20:16:30 +00:00
|
|
|
double calculate_exponential_backoff(
|
2003-08-31 00:13:09 +00:00
|
|
|
const char* debug_descr, int n, double MIN, double MAX,
|
|
|
|
double factor /* = 1.0 */
|
|
|
|
) {
|
2003-06-28 00:15:42 +00:00
|
|
|
double rmax = min(MAX, factor*exp((double)n));
|
|
|
|
|
|
|
|
if (debug_fake_exponential_backoff) {
|
|
|
|
// For debugging/testing purposes, fake exponential back-off by
|
|
|
|
// returning 0 seconds; report arguments so we can tell what we would
|
|
|
|
// have done (this doesn't test the rand_range() functions but is
|
|
|
|
// very useful for testing backoff/retry policies).
|
2003-08-31 00:13:09 +00:00
|
|
|
//
|
2003-06-28 00:15:42 +00:00
|
|
|
double expected_backoff = (MIN > rmax) ? MIN : (rmax-MIN)/2.0;
|
|
|
|
|
|
|
|
debug_total_exponential_backoff += expected_backoff;
|
2003-07-09 23:54:45 +00:00
|
|
|
++count_debug_fake_exponential_backoff;
|
2003-08-31 00:13:09 +00:00
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"## calculate_exponential_backoff(): #%5d descr=\"%s\", n=%d, MIN=%.1f, MAX=%.1f, factor=%.1f; rand_range [%.1f,%.1f); total expected backoff=%.1f\n",
|
|
|
|
count_debug_fake_exponential_backoff,
|
|
|
|
debug_descr, n, MIN, MAX, factor,
|
|
|
|
MIN, rmax, debug_total_exponential_backoff
|
|
|
|
);
|
2003-07-09 23:54:45 +00:00
|
|
|
if (count_debug_fake_exponential_backoff >= max_debug_fake_exponential_backoff) {
|
2003-08-31 00:13:09 +00:00
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"## calculate_exponential_backoff(): reached max_debug_fake_exponential_backoff\n"
|
|
|
|
);
|
2003-07-09 23:54:45 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-28 00:15:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-25 20:16:30 +00:00
|
|
|
return rand_range(MIN, rmax);
|
2003-06-28 00:15:42 +00:00
|
|
|
}
|
2003-07-03 00:48:43 +00:00
|
|
|
|
2004-10-25 20:16:30 +00:00
|
|
|
string timediff_format(double diff) {
|
2003-07-03 00:48:43 +00:00
|
|
|
char buf[256];
|
2004-10-25 20:16:30 +00:00
|
|
|
int tdiff = (int)diff;
|
2003-07-03 00:48:43 +00:00
|
|
|
|
|
|
|
int sex = tdiff % 60;
|
|
|
|
tdiff /= 60;
|
|
|
|
if (!tdiff) {
|
|
|
|
sprintf(buf, "%d seconds", sex);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int min = tdiff % 60;
|
|
|
|
tdiff /= 60;
|
|
|
|
if (!tdiff) {
|
|
|
|
sprintf(buf, "%d minutes and %d seconds", min, sex);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hours = tdiff % 24;
|
|
|
|
tdiff /= 24;
|
|
|
|
if (!tdiff) {
|
|
|
|
sprintf(buf, "%d hours, %d minutes, and %d seconds", hours, min, sex);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int days = tdiff % 7;
|
|
|
|
tdiff /= 7;
|
|
|
|
if (!tdiff) {
|
|
|
|
sprintf(buf, "%d days, %d hours, %d minutes, and %d seconds", days, hours, min, sex);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2003-07-03 16:35:20 +00:00
|
|
|
sprintf(buf, "%d weeks, %d days, %d hours, %d minutes, and %d seconds", (int)tdiff, days, hours, min, sex);
|
2003-07-03 00:48:43 +00:00
|
|
|
return buf;
|
|
|
|
}
|
2003-08-14 00:02:15 +00:00
|
|
|
|
|
|
|
// read entire file into string
|
2003-08-31 00:13:09 +00:00
|
|
|
int read_file_string(const char* pathname, string& result) {
|
2003-08-14 00:02:15 +00:00
|
|
|
result.erase();
|
2004-04-30 23:18:56 +00:00
|
|
|
FILE* f;
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
f = fopen(pathname, "r");
|
2003-10-21 04:06:55 +00:00
|
|
|
if (!f) return ERR_FOPEN;
|
2003-08-14 00:02:15 +00:00
|
|
|
|
2004-04-30 23:18:56 +00:00
|
|
|
while (fgets(buf, 256, f)) result += buf;
|
|
|
|
fclose(f);
|
2003-08-14 00:02:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-01-19 19:14:59 +00:00
|
|
|
|
2004-02-04 19:14:39 +00:00
|
|
|
#ifdef WIN32
|
2004-02-07 06:21:22 +00:00
|
|
|
|
2004-01-19 19:14:59 +00:00
|
|
|
//
|
2004-02-04 19:14:39 +00:00
|
|
|
// FUNCTION: windows_error_string
|
|
|
|
//
|
|
|
|
// PURPOSE: copies error message text to string
|
|
|
|
//
|
|
|
|
// PARAMETERS:
|
|
|
|
// pszBuf - destination buffer
|
|
|
|
// iSize - size of buffer
|
|
|
|
//
|
|
|
|
// RETURN VALUE:
|
|
|
|
// destination buffer
|
|
|
|
//
|
|
|
|
// COMMENTS:
|
|
|
|
//
|
2004-04-30 23:18:56 +00:00
|
|
|
char* windows_error_string( char* pszBuf, int iSize ) {
|
2004-02-04 19:14:39 +00:00
|
|
|
DWORD dwRet;
|
|
|
|
LPTSTR lpszTemp = NULL;
|
|
|
|
|
2004-04-30 23:18:56 +00:00
|
|
|
dwRet = FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_ARGUMENT_ARRAY,
|
|
|
|
NULL,
|
|
|
|
GetLastError(),
|
|
|
|
LANG_NEUTRAL,
|
|
|
|
(LPTSTR)&lpszTemp,
|
|
|
|
0,
|
|
|
|
NULL
|
|
|
|
);
|
2004-02-04 19:14:39 +00:00
|
|
|
|
|
|
|
// supplied buffer is not long enough
|
2004-04-30 23:18:56 +00:00
|
|
|
if ( !dwRet || ( (long)iSize < (long)dwRet+14 ) ) {
|
2004-02-04 19:14:39 +00:00
|
|
|
pszBuf[0] = TEXT('\0');
|
2004-04-30 23:18:56 +00:00
|
|
|
} else {
|
2004-02-04 19:14:39 +00:00
|
|
|
lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); //remove cr and newline character
|
|
|
|
sprintf( pszBuf, TEXT("%s (0x%x)"), lpszTemp, GetLastError() );
|
|
|
|
}
|
|
|
|
|
2004-04-30 23:18:56 +00:00
|
|
|
if ( lpszTemp ) {
|
2004-02-04 19:14:39 +00:00
|
|
|
LocalFree((HLOCAL) lpszTemp );
|
2004-04-30 23:18:56 +00:00
|
|
|
}
|
2004-02-04 19:14:39 +00:00
|
|
|
|
|
|
|
return pszBuf;
|
2004-01-19 19:14:59 +00:00
|
|
|
}
|
2004-02-07 06:21:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// FUNCTION: windows_format_error_string
|
|
|
|
//
|
|
|
|
// PURPOSE: copies error message text to string
|
|
|
|
//
|
|
|
|
// PARAMETERS:
|
|
|
|
// dwError - the error value to look up
|
|
|
|
// pszBuf - destination buffer
|
|
|
|
// iSize - size of buffer
|
|
|
|
//
|
|
|
|
// RETURN VALUE:
|
|
|
|
// destination buffer
|
|
|
|
//
|
|
|
|
// COMMENTS:
|
|
|
|
//
|
|
|
|
char* windows_format_error_string( unsigned long dwError, char* pszBuf, int iSize )
|
|
|
|
{
|
|
|
|
DWORD dwRet;
|
|
|
|
LPTSTR lpszTemp = NULL;
|
|
|
|
|
2004-04-30 23:18:56 +00:00
|
|
|
dwRet = FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_ARGUMENT_ARRAY,
|
|
|
|
NULL,
|
|
|
|
dwError,
|
|
|
|
LANG_NEUTRAL,
|
|
|
|
(LPTSTR)&lpszTemp,
|
|
|
|
0,
|
|
|
|
NULL
|
|
|
|
);
|
2004-02-07 06:21:22 +00:00
|
|
|
|
|
|
|
// supplied buffer is not long enough
|
2004-04-30 23:18:56 +00:00
|
|
|
if ( !dwRet || ( (long)iSize < (long)dwRet+14 ) ) {
|
2004-02-07 06:21:22 +00:00
|
|
|
pszBuf[0] = TEXT('\0');
|
2004-04-30 23:18:56 +00:00
|
|
|
} else {
|
2004-02-07 06:21:22 +00:00
|
|
|
lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); //remove cr and newline character
|
2004-02-07 22:55:01 +00:00
|
|
|
sprintf( pszBuf, TEXT("%s (0x%x)"), lpszTemp, dwError );
|
2004-02-07 06:21:22 +00:00
|
|
|
}
|
|
|
|
|
2004-04-30 23:18:56 +00:00
|
|
|
if ( lpszTemp ) {
|
2004-02-07 06:21:22 +00:00
|
|
|
LocalFree((HLOCAL) lpszTemp );
|
2004-04-30 23:18:56 +00:00
|
|
|
}
|
2004-02-07 06:21:22 +00:00
|
|
|
|
|
|
|
return pszBuf;
|
|
|
|
}
|
|
|
|
|
2004-12-05 23:52:17 +00:00
|
|
|
int boinc_thread_cpu_time(HANDLE thread_handle, double& cpu) {
|
|
|
|
FILETIME creationTime, exitTime, kernelTime, userTime;
|
2004-05-24 17:51:08 +00:00
|
|
|
|
|
|
|
if (GetThreadTimes(
|
|
|
|
thread_handle, &creationTime, &exitTime, &kernelTime, &userTime)
|
|
|
|
) {
|
|
|
|
ULARGE_INTEGER tKernel, tUser;
|
|
|
|
LONGLONG totTime;
|
|
|
|
|
|
|
|
tKernel.LowPart = kernelTime.dwLowDateTime;
|
|
|
|
tKernel.HighPart = kernelTime.dwHighDateTime;
|
|
|
|
tUser.LowPart = userTime.dwLowDateTime;
|
|
|
|
tUser.HighPart = userTime.dwHighDateTime;
|
|
|
|
totTime = tKernel.QuadPart + tUser.QuadPart;
|
|
|
|
|
|
|
|
// Runtimes in 100-nanosecond units
|
|
|
|
cpu = totTime / 1.e7;
|
|
|
|
} else {
|
2004-12-05 23:52:17 +00:00
|
|
|
return -1;
|
2004-05-24 17:51:08 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-05 23:52:17 +00:00
|
|
|
static void get_elapsed_time(double& cpu) {
|
|
|
|
static bool first = true;
|
|
|
|
static DWORD first_count = 0;
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
first_count = GetTickCount();
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
// TODO: Handle timer wraparound
|
|
|
|
DWORD cur = GetTickCount();
|
|
|
|
cpu = ((cur - first_count)/1000.);
|
|
|
|
}
|
|
|
|
|
|
|
|
int boinc_calling_thread_cpu_time(double& cpu) {
|
|
|
|
if (boinc_thread_cpu_time(GetCurrentThread(), cpu)) {
|
|
|
|
get_elapsed_time(cpu);
|
|
|
|
}
|
|
|
|
return 0;
|
2004-05-24 17:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2005-03-29 23:13:28 +00:00
|
|
|
// Unix: pthreads doesn't seem to provide an API for getting
|
|
|
|
// per-thread CPU time. So just get the process's CPU time
|
|
|
|
//
|
2004-12-05 23:52:17 +00:00
|
|
|
int boinc_calling_thread_cpu_time(double &cpu_t) {
|
2004-05-24 17:51:08 +00:00
|
|
|
int retval;
|
|
|
|
struct rusage ru;
|
|
|
|
retval = getrusage(RUSAGE_SELF, &ru);
|
|
|
|
if (retval) {
|
2005-04-04 06:02:36 +00:00
|
|
|
return ERR_GETRUSAGE;
|
2004-05-24 17:51:08 +00:00
|
|
|
}
|
2005-03-29 23:13:28 +00:00
|
|
|
// Sum the user and system time
|
|
|
|
//
|
2004-05-24 17:51:08 +00:00
|
|
|
cpu_t = (double)ru.ru_utime.tv_sec + (((double)ru.ru_utime.tv_usec) / ((double)1000000.0));
|
|
|
|
cpu_t += (double)ru.ru_stime.tv_sec + (((double)ru.ru_stime.tv_usec) / ((double)1000000.0));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-11 23:51:50 +00:00
|
|
|
#endif
|
2004-04-07 06:51:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Update an estimate of "units per day" of something (credit or CPU time).
|
|
|
|
// The estimate is exponentially averaged with a given half-life
|
|
|
|
// (i.e. if no new work is done, the average will decline by 50% in this time).
|
|
|
|
// This function can be called either with new work,
|
|
|
|
// or with zero work to decay an existing average.
|
|
|
|
//
|
2004-11-01 23:10:02 +00:00
|
|
|
// NOTE: if you change this, also change update_average in
|
|
|
|
// html/inc/credit.inc
|
|
|
|
//
|
2005-04-29 16:10:45 +00:00
|
|
|
|
|
|
|
// David, the quick fix I have done is minimalist. Consider the limit
|
|
|
|
// as diff->0, using the first-order Taylor expansion of
|
|
|
|
// exp(x)=1+x+O(x^2).
|
|
|
|
// So to the lowest order in diff:
|
|
|
|
// weight = 1 - diff ln(2) / half_life
|
|
|
|
// so one has
|
|
|
|
// avg += (1-weight)*(work/diff_days)
|
|
|
|
// avg += [diff*ln(2)/half_life] * (work*SECONDS_PER_DAY/diff)
|
|
|
|
// notice that diff cancels out, leaving
|
|
|
|
// avg += [ln(2)/half_life] * work*SECONDS_PER_DAY
|
|
|
|
|
2004-04-07 06:51:42 +00:00
|
|
|
void update_average(
|
|
|
|
double work_start_time, // when new work was started
|
|
|
|
// (or zero if no new work)
|
|
|
|
double work, // amount of new work
|
|
|
|
double half_life,
|
|
|
|
double& avg, // average work per day (in and out)
|
|
|
|
double& avg_time // when average was last computed
|
|
|
|
) {
|
|
|
|
double now = dtime();
|
|
|
|
|
|
|
|
if (avg_time) {
|
2005-04-29 16:10:45 +00:00
|
|
|
double diff, diff_days, weight;
|
|
|
|
|
|
|
|
diff = now - avg_time;
|
|
|
|
if (diff<0) diff=0;
|
|
|
|
|
|
|
|
diff_days = diff/SECONDS_PER_DAY;
|
|
|
|
weight = exp(-diff*M_LN2/half_life);
|
|
|
|
|
2004-04-07 06:51:42 +00:00
|
|
|
avg *= weight;
|
2005-04-29 16:10:45 +00:00
|
|
|
|
2005-04-29 17:44:08 +00:00
|
|
|
if ((1.0-weight) > 1.e-6) {
|
2005-04-29 16:10:45 +00:00
|
|
|
avg += (1-weight)*(work/diff_days);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
avg += M_LN2*work*SECONDS_PER_DAY/half_life;
|
|
|
|
}
|
2004-05-11 04:39:25 +00:00
|
|
|
} else if (work) {
|
2004-04-07 06:51:42 +00:00
|
|
|
double dd = (now - work_start_time)/SECONDS_PER_DAY;
|
|
|
|
avg = work/dd;
|
|
|
|
}
|
|
|
|
avg_time = now;
|
|
|
|
}
|
2004-08-05 11:35:09 +00:00
|
|
|
|
2004-12-27 21:14:18 +00:00
|
|
|
void mysql_timestamp(double dt, char* p) {
|
|
|
|
struct tm* tmp;
|
|
|
|
time_t t = (time_t)dt;
|
|
|
|
tmp = localtime(&t); // MySQL timestamps are in local time
|
|
|
|
sprintf(p, "%4d%02d%02d%02d%02d%02d",
|
|
|
|
tmp->tm_year+1900, tmp->tm_mon+1, tmp->tm_mday,
|
|
|
|
tmp->tm_hour, tmp->tm_min, tmp->tm_sec
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-05-01 13:03:18 +00:00
|
|
|
// Return a text-string description of a given error. Must be kept
|
|
|
|
// consistent with error_numbers.h
|
|
|
|
//
|
|
|
|
const char* boincerror(int which_error) {
|
|
|
|
switch (which_error) {
|
|
|
|
case BOINC_SUCCESS: return "Success";
|
|
|
|
case ERR_SELECT: return "system select";
|
|
|
|
case ERR_MALLOC: return "system malloc";
|
|
|
|
case ERR_READ: return "system read";
|
|
|
|
case ERR_WRITE: return "system write";
|
|
|
|
case ERR_FREAD: return "system fread";
|
|
|
|
case ERR_FWRITE: return "system fwrite";
|
|
|
|
case ERR_IO: return "system I/O";
|
|
|
|
case ERR_CONNECT: return "system connect";
|
|
|
|
case ERR_FOPEN: return "system fopen";
|
|
|
|
case ERR_RENAME: return "system rename";
|
|
|
|
case ERR_UNLINK: return "system unlink";
|
|
|
|
case ERR_OPENDIR: return "system opendir";
|
|
|
|
case ERR_XML_PARSE: return "unexpected XML tag or syntax";
|
|
|
|
case ERR_GETHOSTBYNAME: return "can't resolve hostname";
|
|
|
|
case ERR_GIVEUP_DOWNLOAD: return "timeout on download";
|
|
|
|
case ERR_GIVEUP_UPLOAD: return "timeout on upload";
|
|
|
|
case ERR_NULL: return "null pointer";
|
|
|
|
case ERR_NEG: return "unexpected negative value";
|
|
|
|
case ERR_BUFFER_OVERFLOW: return "buffer overflow";
|
|
|
|
case ERR_MD5_FAILED: return "md5 checksum failed for file";
|
|
|
|
case ERR_RSA_FAILED: return "RSA key check failed for file";
|
|
|
|
case ERR_OPEN: return "system open";
|
|
|
|
case ERR_DUP2: return "system dup";
|
|
|
|
case ERR_NO_SIGNATURE: return "no signature";
|
|
|
|
case ERR_THREAD: return "creating a thread";
|
|
|
|
case ERR_SIGNAL_CATCH: return "caught signal";
|
|
|
|
case ERR_UPLOAD_TRANSIENT: return "transient upload error";
|
|
|
|
case ERR_UPLOAD_PERMANENT: return "fatal upload error";
|
|
|
|
case ERR_IDLE_PERIOD: return "user preferences say can't start work";
|
|
|
|
case ERR_ALREADY_ATTACHED: return "already attached to project";
|
|
|
|
case ERR_FILE_TOO_BIG: return "file size too big";
|
|
|
|
case ERR_GETRUSAGE: return "system getrusage";
|
|
|
|
case ERR_BENCHMARK_FAILED: return "benchmark failed";
|
|
|
|
case ERR_BAD_HEX_FORMAT: return "hex format key data bad";
|
|
|
|
case ERR_USER_REJECTED: return "user rejected executable file";
|
|
|
|
case ERR_DB_NOT_FOUND: return "no database rows found in lookup/enumerate";
|
|
|
|
case ERR_DB_NOT_UNIQUE: return "database lookup not unique";
|
|
|
|
case ERR_DB_CANT_CONNECT: return "can't connect to datbase";
|
|
|
|
case ERR_GETS: return "system gets/fgets";
|
|
|
|
case ERR_SCANF: return "system scanf/fscanf";
|
|
|
|
case ERR_STRCHR: return "system strchr";
|
|
|
|
case ERR_STRSTR: return "system strstr";
|
|
|
|
case ERR_READDIR: return "system readdir";
|
|
|
|
case ERR_SHMGET: return "system shmget";
|
|
|
|
case ERR_SHMCTL: return "system shmctl";
|
|
|
|
case ERR_SHMAT: return "system shmat";
|
|
|
|
case ERR_FORK: return "system fork";
|
|
|
|
case ERR_EXEC: return "system exec";
|
|
|
|
case ERR_NOT_EXITED: return "process didn't exit";
|
|
|
|
case ERR_NOT_IMPLEMENTED: return "system call not implemented";
|
|
|
|
case ERR_GETHOSTNAME: return "system gethostname";
|
|
|
|
case ERR_NETOPEN: return "system netopen";
|
|
|
|
case ERR_SOCKET: return "system socket";
|
|
|
|
case ERR_FCNTL: return "system fcntl";
|
2005-05-31 21:59:29 +00:00
|
|
|
case ERR_AUTHENTICATOR: return "authentication error";
|
2005-05-01 13:03:18 +00:00
|
|
|
case ERR_SCHED_SHMEM: return "scheduler shared memory contents bad";
|
|
|
|
case ERR_ASYNCSELECT: return "system async select";
|
|
|
|
case ERR_BAD_RESULT_STATE: return "bad result state";
|
|
|
|
case ERR_DB_CANT_INIT: return "can't init database";
|
|
|
|
case ERR_NOT_UNIQUE: return "state files have redundant entries";
|
2005-05-31 21:59:29 +00:00
|
|
|
case ERR_NOT_FOUND: return "not found";
|
2005-05-01 13:03:18 +00:00
|
|
|
case ERR_NO_EXIT_STATUS: return "no exit status in scheduler request";
|
|
|
|
case ERR_FILE_MISSING: return "file missing";
|
|
|
|
case ERR_NESTED_UNHANDLED_EXCEPTION_DETECTED: return "nested unhandled exception";
|
|
|
|
case ERR_SEMGET: return "system get semaphore";
|
|
|
|
case ERR_SEMCTL: return "system control semaphore";
|
|
|
|
case ERR_SEMOP: return "system op semaphore";
|
|
|
|
case ERR_FTOK: return "system ftok";
|
|
|
|
case ERR_SOCKS_UNKNOWN_FAILURE: return "socket unknown";
|
|
|
|
case ERR_SOCKS_REQUEST_FAILED: return "socket request";
|
|
|
|
case ERR_SOCKS_BAD_USER_PASS: return "socket bad user password";
|
|
|
|
case ERR_SOCKS_UNKNOWN_SERVER_VERSION: return "socket unknown server version";
|
|
|
|
case ERR_SOCKS_UNSUPPORTED: return "socket unsupported";
|
|
|
|
case ERR_SOCKS_CANT_REACH_HOST: return "socket can't reach host";
|
|
|
|
case ERR_SOCKS_CONN_REFUSED: return "socket connection refused";
|
|
|
|
case ERR_TIMER_INIT: return "timer init";
|
|
|
|
case ERR_RSC_LIMIT_EXCEEDED: return "resource limit exceeded";
|
|
|
|
case ERR_INVALID_PARAM: return "invalid parameter";
|
|
|
|
case ERR_SIGNAL_OP: return "signal op";
|
|
|
|
case ERR_BIND: return "system bind";
|
|
|
|
case ERR_LISTEN: return "system listen";
|
|
|
|
case ERR_TIMEOUT: return "timeout";
|
|
|
|
case ERR_PROJECT_DOWN: return "project down";
|
|
|
|
case ERR_HTTP_ERROR: return "http error";
|
|
|
|
case ERR_RESULT_START: return "result start";
|
|
|
|
case ERR_RESULT_DOWNLOAD: return "result download";
|
|
|
|
case ERR_RESULT_UPLOAD: return "result upload";
|
|
|
|
case ERR_INVALID_URL: return "invalid URL";
|
|
|
|
case ERR_MAJOR_VERSION: return "major version";
|
|
|
|
case ERR_NO_OPTION: return "no option";
|
|
|
|
case ERR_MKDIR: return "mkdir";
|
|
|
|
case ERR_INVALID_EVENT: return "invalid event";
|
|
|
|
case ERR_ALREADY_RUNNING: return "already running";
|
|
|
|
case ERR_NO_APP_VERSION: return "no app version";
|
|
|
|
case ERR_WU_USER_RULE: return "user already did result for this workunit";
|
|
|
|
case ERR_ABORTED_VIA_GUI: return "result aborted via GUI";
|
|
|
|
case ERR_INSUFFICIENT_RESOURCE: return "insufficient resources";
|
|
|
|
case ERR_RETRY: return "retry";
|
|
|
|
case ERR_WRONG_SIZE: return "wrong size";
|
|
|
|
case ERR_USER_PERMISSION: return "user permission";
|
2005-06-24 08:12:11 +00:00
|
|
|
case ERR_SHMEM_NAME: return "can't get shared mem segment name";
|
2005-06-29 06:14:35 +00:00
|
|
|
case ERR_NO_NETWORK_CONNECTION: return "no available network connection";
|
2005-05-01 13:03:18 +00:00
|
|
|
}
|
2005-06-29 06:14:35 +00:00
|
|
|
static char buf[64];
|
|
|
|
sprintf(buf, "error %d", which_error);
|
|
|
|
return buf;
|
2005-05-01 13:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_ab65c90e1e = "$Id$";
|