2002-04-30 22:22:54 +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-12-03 18:57:40 +00:00
|
|
|
// The BOINC scheduling server.
|
2003-01-07 01:02:08 +00:00
|
|
|
//
|
2002-12-03 18:57:40 +00:00
|
|
|
// command-line options:
|
|
|
|
|
2002-09-26 23:12:13 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
using namespace std;
|
2002-06-07 22:59:24 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2002-07-11 01:09:53 +00:00
|
|
|
#include <assert.h>
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-04-07 19:06:00 +00:00
|
|
|
#include "boinc_db.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "parse.h"
|
2002-05-24 04:29:10 +00:00
|
|
|
#include "shmem.h"
|
2003-03-06 17:42:49 +00:00
|
|
|
#include "util.h"
|
2002-10-03 18:33:46 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "server_types.h"
|
|
|
|
#include "handle_request.h"
|
2002-08-25 07:54:33 +00:00
|
|
|
#include "main.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-01-07 01:02:08 +00:00
|
|
|
#define STDERR_FILENAME "cgi_out"
|
2002-10-09 04:56:41 +00:00
|
|
|
#define REQ_FILE_PREFIX "boinc_req_"
|
|
|
|
#define REPLY_FILE_PREFIX "boinc_reply_"
|
2003-01-14 23:42:47 +00:00
|
|
|
bool use_files = false; // use disk files for req/reply msgs (for debugging)
|
2002-08-25 07:54:33 +00:00
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_PROJECT gproject;
|
2002-10-04 20:35:56 +00:00
|
|
|
CONFIG config;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-01-14 23:42:47 +00:00
|
|
|
int main() {
|
2002-04-30 22:22:54 +00:00
|
|
|
FILE* fin, *fout;
|
2002-06-01 20:26:21 +00:00
|
|
|
int i, retval, pid;
|
2003-01-07 01:02:08 +00:00
|
|
|
char buf[256], req_path[256], reply_path[256], path[256];
|
2002-05-24 04:29:10 +00:00
|
|
|
SCHED_SHMEM* ssp;
|
|
|
|
void* p;
|
2002-07-02 17:51:51 +00:00
|
|
|
unsigned int counter=0;
|
2002-07-07 20:39:24 +00:00
|
|
|
char* code_sign_key;
|
2003-01-14 23:42:47 +00:00
|
|
|
bool found;
|
2002-10-03 18:33:46 +00:00
|
|
|
|
2003-01-07 01:02:08 +00:00
|
|
|
if (!freopen(STDERR_FILENAME, "a", stderr)) {
|
|
|
|
fprintf(stderr, "Can't redirect stderr\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-10-03 18:33:46 +00:00
|
|
|
retval = config.parse_file();
|
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("Can't parse config file\n");
|
2002-10-03 18:33:46 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2002-07-07 20:39:24 +00:00
|
|
|
|
2002-10-03 18:33:46 +00:00
|
|
|
sprintf(path, "%s/code_sign_public", config.key_dir);
|
2002-07-07 20:39:24 +00:00
|
|
|
retval = read_file_malloc(path, code_sign_key);
|
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf, "Can't read code sign key file (%s)\n", path);
|
|
|
|
write_log(buf);
|
2002-07-07 20:39:24 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2002-07-01 22:41:09 +00:00
|
|
|
|
2002-10-03 18:33:46 +00:00
|
|
|
retval = attach_shmem(config.shmem_key, &p);
|
2002-05-24 04:29:10 +00:00
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("Can't attach shmem\n");
|
2002-05-24 04:29:10 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ssp = (SCHED_SHMEM*)p;
|
|
|
|
retval = ssp->verify();
|
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("shmem has wrong struct sizes - recompile\n");
|
2002-05-24 04:29:10 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2002-06-01 20:26:21 +00:00
|
|
|
|
|
|
|
for (i=0; i<10; i++) {
|
|
|
|
if (ssp->ready) break;
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("waiting for ready flag\n");
|
2002-06-01 20:26:21 +00:00
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
if (!ssp->ready) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("feeder doesn't seem to be running\n");
|
2002-06-01 20:26:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2002-12-03 18:57:40 +00:00
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
retval = boinc_db_open(config.db_name, config.db_passwd);
|
2002-07-02 17:51:51 +00:00
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("can't open database\n");
|
2002-08-25 07:54:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
found = false;
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!gproject.enumerate("")) {
|
2003-01-07 01:02:08 +00:00
|
|
|
found = true;
|
2002-07-02 17:51:51 +00:00
|
|
|
}
|
2002-08-25 07:54:33 +00:00
|
|
|
if (!found) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("can't find project\n");
|
|
|
|
exit(1);
|
2002-08-25 07:54:33 +00:00
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
pid = getpid();
|
2002-07-02 17:51:51 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
while(FCGI_Accept() >= 0) {
|
|
|
|
counter++;
|
|
|
|
#endif
|
2003-01-07 01:02:08 +00:00
|
|
|
printf("Content-type: text/plain\n\n");
|
2002-12-03 18:57:40 +00:00
|
|
|
if (use_files) {
|
2003-01-07 01:02:08 +00:00
|
|
|
// the code below is convoluted because,
|
|
|
|
// instead of going from stdin to stdout directly,
|
|
|
|
// we go via a pair of disk files
|
|
|
|
// (this makes it easy to save the input,
|
|
|
|
// and to know the length of the output).
|
|
|
|
//
|
2002-12-03 18:57:40 +00:00
|
|
|
sprintf(req_path, "%s%d_%u", REQ_FILE_PREFIX, pid, counter);
|
|
|
|
sprintf(reply_path, "%s%d_%u", REPLY_FILE_PREFIX, pid, counter);
|
|
|
|
fout = fopen(req_path, "w");
|
|
|
|
if (!fout) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("can't write request file\n");
|
2002-12-03 18:57:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
copy_stream(stdin, fout);
|
|
|
|
fclose(fout);
|
|
|
|
fin = fopen(req_path, "r");
|
|
|
|
if (!fin) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("can't read request file\n");
|
2002-12-03 18:57:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fout = fopen(reply_path, "w");
|
|
|
|
if (!fout) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("can't write reply file\n");
|
2002-12-03 18:57:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
handle_request(fin, fout, *ssp, code_sign_key);
|
|
|
|
fclose(fin);
|
|
|
|
fclose(fout);
|
|
|
|
fin = fopen(reply_path, "r");
|
|
|
|
if (!fin) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("can't read reply file\n");
|
2002-12-03 18:57:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
copy_stream(fin, stdout);
|
|
|
|
fclose(fin);
|
|
|
|
//unlink(req_path);
|
|
|
|
//unlink(reply_path);
|
|
|
|
} else {
|
|
|
|
handle_request(stdin, stdout, *ssp, code_sign_key);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2002-07-01 22:41:09 +00:00
|
|
|
#ifdef _USING_FCGI_
|
2002-06-07 21:21:55 +00:00
|
|
|
}
|
2002-07-01 22:41:09 +00:00
|
|
|
#endif
|
2003-01-09 07:24:27 +00:00
|
|
|
boinc_db_close();
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|