2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-04-30 22:22:54 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-08-11 04:28:23 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2003-08-11 04:28:23 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-08-11 04:28:23 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2002-07-11 01:09:53 +00:00
|
|
|
#include <assert.h>
|
2003-05-20 22:57:28 +00:00
|
|
|
#include <unistd.h>
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-04-07 19:06:00 +00:00
|
|
|
#include "boinc_db.h"
|
2002-07-05 05:33:40 +00:00
|
|
|
#include "crypt.h"
|
2004-04-13 23:55:05 +00:00
|
|
|
#include "error_numbers.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "md5_file.h"
|
2002-12-12 01:07:24 +00:00
|
|
|
#include "parse.h"
|
2004-04-13 23:55:05 +00:00
|
|
|
#include "util.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#include "backend_lib.h"
|
|
|
|
|
2004-04-30 23:18:56 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
#include "fcgi_stdio.h"
|
|
|
|
#endif
|
|
|
|
|
2004-05-13 18:18:22 +00:00
|
|
|
int read_file(FILE* f, char* buf, int len) {
|
|
|
|
int n = fread(buf, 1, len, f);
|
2002-04-30 22:22:54 +00:00
|
|
|
buf[n] = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-13 18:18:22 +00:00
|
|
|
int read_filename(const char* path, char* buf, int len) {
|
2002-07-05 05:33:40 +00:00
|
|
|
int retval;
|
|
|
|
FILE* f = fopen(path, "r");
|
|
|
|
if (!f) return -1;
|
2004-05-13 18:18:22 +00:00
|
|
|
retval = read_file(f, buf, len);
|
2002-07-05 05:33:40 +00:00
|
|
|
fclose(f);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2002-12-12 01:07:24 +00:00
|
|
|
|
|
|
|
// process WU template
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
|
|
|
static int process_wu_template(
|
2004-07-02 17:53:31 +00:00
|
|
|
WORKUNIT& wu,
|
|
|
|
char* tmplate,
|
|
|
|
const char* dirpath,
|
|
|
|
const char** infiles,
|
|
|
|
int n,
|
|
|
|
const char* upload_url,
|
|
|
|
const char* download_url
|
2002-04-30 22:22:54 +00:00
|
|
|
) {
|
|
|
|
char* p;
|
2004-05-13 18:18:22 +00:00
|
|
|
char buf[LARGE_BLOB_SIZE], md5[33], path[256];
|
2004-04-13 23:55:05 +00:00
|
|
|
char out[LARGE_BLOB_SIZE];
|
2002-12-12 01:07:24 +00:00
|
|
|
int retval, file_number;
|
2002-04-30 22:22:54 +00:00
|
|
|
double nbytes;
|
2002-12-12 01:07:24 +00:00
|
|
|
char open_name[256];
|
2003-01-15 23:54:24 +00:00
|
|
|
bool found=false;
|
2002-12-05 19:13:06 +00:00
|
|
|
|
2002-12-12 01:07:24 +00:00
|
|
|
strcpy(out, "");
|
2004-04-14 18:46:33 +00:00
|
|
|
for (p=strtok(tmplate, "\n"); p; p=strtok(0, "\n")) {
|
2002-12-12 01:07:24 +00:00
|
|
|
if (match_tag(p, "<file_info>")) {
|
2004-04-13 23:55:05 +00:00
|
|
|
file_number = -1;
|
|
|
|
strcat(out, "<file_info>\n");
|
|
|
|
while (1) {
|
|
|
|
p = strtok(0, "\n");
|
|
|
|
if (!p) break;
|
|
|
|
if (parse_int(p, "<number>", file_number)) {
|
|
|
|
continue;
|
|
|
|
} else if (match_tag(p, "</file_info>")) {
|
|
|
|
if (file_number < 0) {
|
|
|
|
fprintf(stderr, "No file number found\n");
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
|
|
|
sprintf(path, "%s/%s", dirpath, infiles[file_number]);
|
|
|
|
retval = md5_file(path, md5, nbytes);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "process_wu_template: md5_file %d\n", retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
sprintf(buf,
|
|
|
|
" <name>%s</name>\n"
|
|
|
|
" <url>%s/%s</url>\n"
|
|
|
|
" <md5_cksum>%s</md5_cksum>\n"
|
|
|
|
" <nbytes>%.0f</nbytes>\n"
|
|
|
|
"</file_info>\n",
|
|
|
|
infiles[file_number],
|
|
|
|
download_url, infiles[file_number],
|
|
|
|
md5,
|
|
|
|
nbytes
|
|
|
|
);
|
|
|
|
strcat(out, buf);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
strcat(out, p);
|
|
|
|
strcat(out, "\n");
|
|
|
|
}
|
2002-10-03 18:33:46 +00:00
|
|
|
}
|
2002-12-12 01:07:24 +00:00
|
|
|
} else if (match_tag(p, "<workunit>")) {
|
2003-01-15 23:54:24 +00:00
|
|
|
found = true;
|
2002-12-12 01:07:24 +00:00
|
|
|
strcat(out, "<workunit>\n");
|
|
|
|
} else if (match_tag(p, "</workunit>")) {
|
|
|
|
strcat(out, "</workunit>\n");
|
|
|
|
} else if (match_tag(p, "<file_ref>")) {
|
2004-04-13 23:55:05 +00:00
|
|
|
file_number = -1;
|
|
|
|
while (1) {
|
|
|
|
p = strtok(0, "\n");
|
|
|
|
if (!p) break;
|
|
|
|
if (parse_int(p, "<file_number>", file_number)) {
|
|
|
|
continue;
|
|
|
|
} else if (parse_str(p, "<open_name>", open_name, sizeof(open_name))) {
|
|
|
|
continue;
|
|
|
|
} else if (match_tag(p, "</file_ref>")) {
|
|
|
|
sprintf(buf,
|
|
|
|
"<file_ref>\n"
|
|
|
|
" <file_name>%s</file_name>\n"
|
|
|
|
" <open_name>%s</open_name>\n"
|
|
|
|
"</file_ref>\n",
|
|
|
|
infiles[file_number],
|
|
|
|
open_name
|
|
|
|
);
|
|
|
|
strcat(out, buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (parse_double(p, "<rsc_fpops_est>", wu.rsc_fpops_est)) {
|
|
|
|
continue;
|
|
|
|
} else if (parse_double(p, "<rsc_fpops_bound>", wu.rsc_fpops_bound)) {
|
|
|
|
continue;
|
|
|
|
} else if (parse_double(p, "<rsc_memory_bound>", wu.rsc_memory_bound)) {
|
|
|
|
continue;
|
|
|
|
} else if (parse_double(p, "<rsc_disk_bound>", wu.rsc_disk_bound)) {
|
|
|
|
continue;
|
|
|
|
} else if (parse_int(p, "<delay_bound>", wu.delay_bound)) {
|
|
|
|
continue;
|
|
|
|
} else if (parse_int(p, "<min_quorum>", wu.min_quorum)) {
|
|
|
|
continue;
|
|
|
|
} else if (parse_int(p, "<target_nresults>", wu.target_nresults)) {
|
|
|
|
continue;
|
|
|
|
} else if (parse_int(p, "<max_error_results>", wu.max_error_results)) {
|
|
|
|
continue;
|
|
|
|
} else if (parse_int(p, "<max_total_results>", wu.max_total_results)) {
|
|
|
|
continue;
|
|
|
|
} else if (parse_int(p, "<max_success_results>", wu.max_success_results)) {
|
|
|
|
continue;
|
2002-12-12 01:07:24 +00:00
|
|
|
} else {
|
|
|
|
strcat(out, p);
|
|
|
|
strcat(out, "\n");
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-15 23:54:24 +00:00
|
|
|
if (!found) {
|
|
|
|
fprintf(stderr, "create_work: bad WU template - no <workunit>\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-05-13 18:18:22 +00:00
|
|
|
if (strlen(out) > sizeof(wu.xml_doc)-1) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"create_work: WU XML field is too long (%d bytes; max is %d)\n",
|
|
|
|
strlen(out), sizeof(wu.xml_doc)-1
|
|
|
|
);
|
|
|
|
return ERR_BUFFER_OVERFLOW;
|
|
|
|
}
|
2004-04-13 23:55:05 +00:00
|
|
|
safe_strncpy(wu.xml_doc, out, sizeof(wu.xml_doc));
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-07 19:31:34 +00:00
|
|
|
// Set the time-varying fields of a result to their initial state.
|
|
|
|
// This is used to create clones of existing results,
|
|
|
|
// so set only the time-varying fields
|
|
|
|
//
|
2004-07-02 17:53:31 +00:00
|
|
|
void initialize_result(DB_RESULT& result, int workunit_id, int workunit_appid) {
|
2002-11-07 19:31:34 +00:00
|
|
|
result.id = 0;
|
|
|
|
result.create_time = time(0);
|
2004-07-02 17:53:31 +00:00
|
|
|
result.workunitid = workunit_id;
|
2002-12-07 00:56:51 +00:00
|
|
|
result.server_state = RESULT_SERVER_STATE_UNSENT;
|
2002-11-07 19:31:34 +00:00
|
|
|
result.hostid = 0;
|
2003-01-23 08:07:48 +00:00
|
|
|
result.report_deadline = 0;
|
2002-11-07 19:31:34 +00:00
|
|
|
result.sent_time = 0;
|
|
|
|
result.received_time = 0;
|
2002-12-09 23:06:17 +00:00
|
|
|
result.client_state = 0;
|
2002-11-07 19:31:34 +00:00
|
|
|
result.cpu_time = 0;
|
|
|
|
strcpy(result.xml_doc_out, "");
|
|
|
|
strcpy(result.stderr_out, "");
|
2003-01-23 08:07:48 +00:00
|
|
|
result.outcome = RESULT_OUTCOME_INIT;
|
2002-12-17 19:00:43 +00:00
|
|
|
result.file_delete_state = ASSIMILATE_INIT;
|
2003-01-23 08:07:48 +00:00
|
|
|
result.validate_state = VALIDATE_STATE_INIT;
|
2002-11-07 19:31:34 +00:00
|
|
|
result.claimed_credit = 0;
|
|
|
|
result.granted_credit = 0;
|
2004-07-02 17:53:31 +00:00
|
|
|
result.appid = workunit_appid;
|
2002-11-07 19:31:34 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 01:21:00 +00:00
|
|
|
// Create a new result for the given WU.
|
2004-04-13 23:55:05 +00:00
|
|
|
// This is called ONLY from the transitioner
|
2002-10-16 01:21:00 +00:00
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
int create_result(
|
2004-07-02 19:45:33 +00:00
|
|
|
int workunit_id,
|
|
|
|
int workunit_appid,
|
|
|
|
char* wu_name,
|
2004-07-02 17:53:31 +00:00
|
|
|
char* result_template_filename,
|
|
|
|
char* result_name_suffix,
|
|
|
|
R_RSA_PRIVATE_KEY& key,
|
2003-08-15 23:44:28 +00:00
|
|
|
char* upload_url
|
2002-07-05 05:33:40 +00:00
|
|
|
) {
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_RESULT result;
|
2002-04-30 22:22:54 +00:00
|
|
|
char base_outfile_name[256];
|
2004-07-02 17:53:31 +00:00
|
|
|
char result_template[LARGE_BLOB_SIZE];
|
2002-04-30 22:22:54 +00:00
|
|
|
int retval;
|
2002-09-26 05:57:10 +00:00
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
result.clear();
|
2004-07-02 17:53:31 +00:00
|
|
|
initialize_result(result, workunit_id, workunit_appid);
|
2004-07-02 19:45:33 +00:00
|
|
|
sprintf(result.name, "%s_%s", wu_name, result_name_suffix);
|
2003-06-04 17:21:26 +00:00
|
|
|
sprintf(base_outfile_name, "%s_", result.name);
|
2002-07-05 05:33:40 +00:00
|
|
|
|
2004-07-02 17:53:31 +00:00
|
|
|
retval = read_filename(result_template_filename, result_template, sizeof(result_template));
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "Failed to read result template file '%s': %d\n",
|
|
|
|
result_template_filename, retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
retval = process_result_template(
|
2004-07-02 17:53:31 +00:00
|
|
|
result_template,
|
2002-07-05 05:33:40 +00:00
|
|
|
key,
|
2002-10-09 04:56:41 +00:00
|
|
|
base_outfile_name,
|
2003-08-15 23:44:28 +00:00
|
|
|
upload_url
|
2002-04-30 22:22:54 +00:00
|
|
|
);
|
2004-07-02 17:53:31 +00:00
|
|
|
if (strlen(result_template) > sizeof(result.xml_doc_in)-1) {
|
2004-05-13 18:18:22 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"result XML doc is too long: %d bytes, max is %d\n",
|
2004-07-02 17:53:31 +00:00
|
|
|
strlen(result_template), sizeof(result.xml_doc_in)-1
|
2004-05-13 18:18:22 +00:00
|
|
|
);
|
|
|
|
return ERR_BUFFER_OVERFLOW;
|
|
|
|
}
|
2004-07-02 17:53:31 +00:00
|
|
|
safe_strncpy(result.xml_doc_in, result_template, sizeof(result.xml_doc_in));
|
2003-08-11 04:28:23 +00:00
|
|
|
|
|
|
|
// NOTE: result::insert() sets random
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
retval = result.insert();
|
2002-07-05 05:33:40 +00:00
|
|
|
if (retval) {
|
2003-06-04 17:21:26 +00:00
|
|
|
fprintf(stderr, "result.insert(): %d\n", retval);
|
2004-05-13 18:18:22 +00:00
|
|
|
return retval;
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
2004-05-13 18:18:22 +00:00
|
|
|
return 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2003-09-30 18:09:58 +00:00
|
|
|
// make sure a WU's input files are actually there
|
|
|
|
//
|
|
|
|
int check_files(char** infiles, int ninfiles, char* download_dir) {
|
|
|
|
int i;
|
|
|
|
char path[256];
|
|
|
|
FILE* f;
|
|
|
|
|
|
|
|
for (i=0; i<ninfiles; i++) {
|
|
|
|
sprintf(path, "%s/%s", download_dir, infiles[i]);
|
|
|
|
f = fopen(path, "r");
|
|
|
|
if (f) {
|
|
|
|
fclose(f);
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
int create_work(
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_WORKUNIT& wu,
|
2003-09-16 21:50:03 +00:00
|
|
|
const char* _wu_template,
|
|
|
|
const char* result_template_filename,
|
|
|
|
const char* infile_dir,
|
|
|
|
const char** infiles,
|
2002-07-05 05:33:40 +00:00
|
|
|
int ninfiles,
|
2002-10-03 18:33:46 +00:00
|
|
|
R_RSA_PRIVATE_KEY& key,
|
2003-09-16 21:50:03 +00:00
|
|
|
const char* upload_url, const char* download_url
|
2002-04-30 22:22:54 +00:00
|
|
|
) {
|
2003-08-15 23:44:28 +00:00
|
|
|
int retval;
|
2004-05-13 18:18:22 +00:00
|
|
|
char _result_template[LARGE_BLOB_SIZE];
|
|
|
|
char wu_template[LARGE_BLOB_SIZE];
|
2002-09-26 05:57:10 +00:00
|
|
|
|
2003-09-30 18:09:58 +00:00
|
|
|
#if 0
|
|
|
|
retval = check_files(infiles, ninfiles, download_dir);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "Missing input file: %s\n", infiles[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-01-15 23:54:24 +00:00
|
|
|
strcpy(wu_template, _wu_template);
|
2002-04-30 22:22:54 +00:00
|
|
|
wu.create_time = time(0);
|
|
|
|
retval = process_wu_template(
|
2004-04-13 23:55:05 +00:00
|
|
|
wu, wu_template, infile_dir, infiles, ninfiles,
|
2002-10-03 18:33:46 +00:00
|
|
|
upload_url, download_url
|
2002-04-30 22:22:54 +00:00
|
|
|
);
|
2002-10-03 18:33:46 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "process_wu_template: %d\n", retval);
|
|
|
|
return retval;
|
|
|
|
}
|
2003-08-15 23:44:28 +00:00
|
|
|
|
2004-05-13 18:18:22 +00:00
|
|
|
retval = read_filename(
|
|
|
|
result_template_filename, _result_template, sizeof(_result_template)
|
|
|
|
);
|
2002-10-03 18:33:46 +00:00
|
|
|
if (retval) {
|
2003-08-15 23:44:28 +00:00
|
|
|
fprintf(stderr, "create_work: can't read result template\n");
|
2002-10-03 18:33:46 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-07-02 19:17:53 +00:00
|
|
|
if (strlen(result_template_filename) > sizeof(wu.result_template_file)-1) {
|
|
|
|
fprintf(stderr, "result template filename is too big: %d bytes, max is %d\n",
|
|
|
|
strlen(result_template_filename), sizeof(wu.result_template_file)-1
|
2004-05-13 18:18:22 +00:00
|
|
|
);
|
|
|
|
return ERR_BUFFER_OVERFLOW;
|
|
|
|
}
|
2004-07-02 19:17:53 +00:00
|
|
|
safe_strncpy(wu.result_template_file, result_template_filename, sizeof(wu.result_template_file));
|
2003-08-15 23:44:28 +00:00
|
|
|
|
2003-08-18 23:19:01 +00:00
|
|
|
wu.transition_time = time(0);
|
2003-08-15 23:44:28 +00:00
|
|
|
retval = wu.insert();
|
2002-11-06 09:03:55 +00:00
|
|
|
if (retval) {
|
2003-08-15 23:44:28 +00:00
|
|
|
fprintf(stderr, "create_work: workunit.insert() %d\n", retval);
|
2002-11-06 09:03:55 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2003-09-05 21:26:21 +00:00
|
|
|
wu.id = boinc_db.insert_id();
|
2003-08-15 23:44:28 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|