2002-12-03 18:57:40 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// 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
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
|
2002-10-18 16:52:28 +00:00
|
|
|
// make_work
|
|
|
|
// -wu_name name
|
|
|
|
// -result_template filename
|
|
|
|
// [ -redundancy n ]
|
|
|
|
// [ -cushion n ]
|
2002-10-14 23:10:12 +00:00
|
|
|
//
|
2002-11-05 18:36:09 +00:00
|
|
|
// Create WU and result records as needed to maintain a pool of work
|
|
|
|
// (for testing purposes).
|
2002-10-18 16:52:28 +00:00
|
|
|
// Makes a new WU for every "redundancy" results.
|
|
|
|
// Clones the WU of the given name.
|
2002-10-09 04:56:41 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "db.h"
|
2002-10-14 23:10:12 +00:00
|
|
|
#include "crypt.h"
|
|
|
|
#include "backend_lib.h"
|
2002-10-09 04:56:41 +00:00
|
|
|
#include "config.h"
|
2002-12-20 02:12:27 +00:00
|
|
|
#include "parse.h"
|
2002-10-09 04:56:41 +00:00
|
|
|
|
|
|
|
#define TRIGGER_FILENAME "stop_server"
|
|
|
|
|
2002-10-14 23:10:12 +00:00
|
|
|
int cushion = 10;
|
2002-10-18 16:52:28 +00:00
|
|
|
int redundancy = 10;
|
2002-10-14 23:10:12 +00:00
|
|
|
char wu_name[256], result_template_file[256];
|
|
|
|
|
2003-01-07 01:02:08 +00:00
|
|
|
void write_log(char* p) {
|
|
|
|
time_t now = time(0);
|
|
|
|
char* timestr = ctime(&now);
|
|
|
|
*(strchr(timestr, '\n')) = 0;
|
|
|
|
fprintf(stderr, "%s: %s", timestr, p);
|
|
|
|
}
|
|
|
|
|
2003-01-02 23:12:05 +00:00
|
|
|
// edit a WU XML doc, replacing one filename by another
|
|
|
|
// (should appear twice, within <file_info> and <file_ref>)
|
|
|
|
// Also patch the download URL (redundant)
|
|
|
|
//
|
|
|
|
void replace_file_name(
|
|
|
|
char* xml_doc, char* filename, char* new_filename, char* download_url
|
|
|
|
) {
|
|
|
|
char buf[MAX_BLOB_SIZE], temp[256], download_path[256],
|
|
|
|
new_download_path[256];
|
|
|
|
char * p;
|
2002-12-21 00:38:26 +00:00
|
|
|
|
2003-01-02 23:12:05 +00:00
|
|
|
sprintf(download_path,"%s/%s", download_url, filename);
|
|
|
|
sprintf(new_download_path,"%s/%s", download_url, new_filename);
|
|
|
|
strcpy(buf, xml_doc);
|
|
|
|
p = strtok(buf,"\n");
|
|
|
|
while (p) {
|
|
|
|
if (parse_str(p, "<name>", temp, sizeof(temp))) {
|
|
|
|
if(!strcmp(filename, temp)) {
|
|
|
|
replace_element(xml_doc + (p - buf),"<name>","</name>",new_filename);
|
|
|
|
}
|
|
|
|
} else if (parse_str(p, "<file_name>", temp, sizeof(temp))) {
|
|
|
|
if(!strcmp(filename, temp)) {
|
|
|
|
replace_element(xml_doc + (p - buf),"<file_name>","</file_name>",new_filename);
|
|
|
|
}
|
|
|
|
} else if (parse_str(p, "<url>", temp, sizeof(temp))) {
|
|
|
|
if(!strcmp(temp, download_path)) {
|
|
|
|
replace_element(xml_doc + (p - buf),"<url>","</url>",new_download_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = strtok(0, "\n");
|
2002-12-20 02:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-09 04:56:41 +00:00
|
|
|
void check_trigger() {
|
|
|
|
FILE* f = fopen(TRIGGER_FILENAME, "r");
|
|
|
|
if (!f) return;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_work() {
|
|
|
|
CONFIG config;
|
2002-12-20 02:12:27 +00:00
|
|
|
char * p;
|
2003-01-02 23:12:05 +00:00
|
|
|
int retval, start_time=time(0), n, nresults_left;
|
|
|
|
char keypath[256], suffix[256], result_template[MAX_BLOB_SIZE];
|
|
|
|
char file_name[256], buf[MAX_BLOB_SIZE], pathname[256];
|
|
|
|
char new_file_name[256], new_pathname[256], command[256];
|
|
|
|
char starting_xml[MAX_BLOB_SIZE], new_buf[MAX_BLOB_SIZE];
|
2002-10-14 23:10:12 +00:00
|
|
|
R_RSA_PRIVATE_KEY key;
|
|
|
|
WORKUNIT wu;
|
2003-01-02 23:12:05 +00:00
|
|
|
int seqno = 0;
|
2002-12-20 02:40:26 +00:00
|
|
|
|
2002-10-09 04:56:41 +00:00
|
|
|
retval = config.parse_file();
|
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("can't read config file\n");
|
2002-10-09 04:56:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
retval = boinc_db_open(config.db_name, config.db_passwd);
|
2002-10-09 04:56:41 +00:00
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("can't open db\n");
|
2002-10-09 04:56:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-10-14 23:10:12 +00:00
|
|
|
strcpy(wu.name, wu_name);
|
|
|
|
retval = db_workunit_lookup_name(wu);
|
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf, "can't find wu %s\n", wu_name);
|
|
|
|
write_log(buf);
|
2002-10-14 23:10:12 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-12-20 02:40:26 +00:00
|
|
|
strcpy(starting_xml,wu.xml_doc);
|
2002-12-21 00:38:26 +00:00
|
|
|
|
2002-10-14 23:10:12 +00:00
|
|
|
sprintf(keypath, "%s/upload_private", config.key_dir);
|
|
|
|
retval = read_key_file(keypath, key);
|
2002-10-09 04:56:41 +00:00
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("can't read key\n");
|
2002-10-09 04:56:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2002-12-20 02:12:27 +00:00
|
|
|
|
2002-11-21 00:48:50 +00:00
|
|
|
retval = read_filename(result_template_file, result_template);
|
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("can't open result template\n");
|
2002-11-21 00:48:50 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2002-10-18 16:52:28 +00:00
|
|
|
nresults_left = 0;
|
2003-01-07 01:02:08 +00:00
|
|
|
while (1) {
|
2002-12-07 00:56:51 +00:00
|
|
|
retval = db_result_count_server_state(RESULT_SERVER_STATE_UNSENT, n);
|
2002-10-09 04:56:41 +00:00
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("can't count results\n");
|
2002-10-09 04:56:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2002-10-14 23:10:12 +00:00
|
|
|
if (n > cushion) {
|
2002-10-09 04:56:41 +00:00
|
|
|
sleep(1);
|
|
|
|
continue;
|
|
|
|
}
|
2002-10-14 23:10:12 +00:00
|
|
|
|
2003-01-02 23:12:05 +00:00
|
|
|
// make a new workunit every "redundancy" results
|
|
|
|
//
|
2002-10-18 16:52:28 +00:00
|
|
|
if (nresults_left == 0) {
|
2003-01-02 23:12:05 +00:00
|
|
|
strcpy(buf, starting_xml);
|
|
|
|
p = strtok(buf, "\n");
|
|
|
|
strcpy(file_name, "");
|
|
|
|
|
|
|
|
// make new copies of all the WU's input files
|
|
|
|
//
|
|
|
|
while (p) {
|
|
|
|
if (parse_str(p, "<name>", file_name, sizeof(file_name))) {
|
|
|
|
sprintf(
|
2003-01-09 21:48:33 +00:00
|
|
|
new_file_name, "%s__%d_%d", file_name, start_time, seqno++
|
2003-01-02 23:12:05 +00:00
|
|
|
);
|
|
|
|
sprintf(pathname, "%s/%s", config.download_dir, file_name);
|
|
|
|
sprintf(
|
|
|
|
new_pathname, "%s/%s",config.download_dir, new_file_name
|
|
|
|
);
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(command,"cp %s %s", pathname, new_pathname);
|
2003-01-02 23:12:05 +00:00
|
|
|
if (system(command)) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("system() error\n");
|
2003-01-02 23:12:05 +00:00
|
|
|
perror(command);
|
2003-01-03 22:17:40 +00:00
|
|
|
exit(1);
|
2003-01-02 23:12:05 +00:00
|
|
|
}
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf, "%s\n", command);
|
|
|
|
write_log(buf);
|
2003-01-02 23:12:05 +00:00
|
|
|
strcpy(new_buf, starting_xml);
|
|
|
|
replace_file_name(
|
|
|
|
new_buf, file_name, new_file_name, config.download_url
|
|
|
|
);
|
|
|
|
strcpy(wu.xml_doc, new_buf);
|
|
|
|
}
|
|
|
|
p = strtok(0, "\n");
|
|
|
|
}
|
|
|
|
nresults_left = redundancy;
|
2003-01-03 22:17:40 +00:00
|
|
|
sprintf(wu.name, "wu_%d_%d", start_time, seqno++);
|
2003-01-02 23:12:05 +00:00
|
|
|
wu.id = 0;
|
|
|
|
wu.create_time = time(0);
|
|
|
|
retval = db_workunit_new(wu);
|
2003-01-09 07:24:27 +00:00
|
|
|
wu.id = boinc_db_insert_id();
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf, "Created new WU: %s\n", wu.name);
|
|
|
|
write_log(buf);
|
2002-10-18 16:52:28 +00:00
|
|
|
}
|
2003-01-02 23:12:05 +00:00
|
|
|
sprintf(suffix, "%d_%d", start_time, seqno++);
|
2002-10-14 23:10:12 +00:00
|
|
|
create_result(
|
2002-11-21 00:48:50 +00:00
|
|
|
wu, result_template, suffix, key,
|
2002-10-14 23:10:12 +00:00
|
|
|
config.upload_url, config.download_url
|
|
|
|
);
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf, "added result: %s_%s\n", wu.name, suffix);
|
|
|
|
write_log(buf);
|
2002-10-18 16:52:28 +00:00
|
|
|
nresults_left--;
|
2002-12-12 23:01:26 +00:00
|
|
|
check_trigger();
|
2002-10-09 04:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
bool asynch = false;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
unlink(TRIGGER_FILENAME);
|
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-asynch")) {
|
|
|
|
asynch = true;
|
2002-10-14 23:10:12 +00:00
|
|
|
} else if (!strcmp(argv[i], "-cushion")) {
|
|
|
|
cushion = atoi(argv[++i]);
|
|
|
|
} else if (!strcmp(argv[i], "-result_template")) {
|
|
|
|
strcpy(result_template_file, argv[++i]);
|
|
|
|
} else if (!strcmp(argv[i], "-wu_name")) {
|
|
|
|
strcpy(wu_name, argv[++i]);
|
2002-10-09 04:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-14 23:10:12 +00:00
|
|
|
if (!strlen(result_template_file)) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("missing -result_template\n");
|
2002-10-14 23:10:12 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!strlen(wu_name)) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("missing -wu_name\n");
|
2002-10-14 23:10:12 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-10-09 04:56:41 +00:00
|
|
|
if (asynch) {
|
|
|
|
if (!fork()) {
|
|
|
|
make_work();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
make_work();
|
|
|
|
}
|
|
|
|
}
|