// The contents of this file are subject to the BOINC 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://boinc.berkeley.edu/license_1.0.txt // // 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): // #ifdef _WIN32 #include "stdafx.h" #endif #ifndef _WIN32 #include #endif #include "boinc_api.h" #include "parse.h" #include "error_numbers.h" #include "util.h" #include "filesys.h" #include "app_ipc.h" char* xml_graphics_modes[NGRAPHICS_MODES] = { "", "", "", "", "" }; #define REREAD_PREFS_MSG "" int write_init_data_file(FILE* f, APP_INIT_DATA& ai) { string str1, str2; if (strlen(ai.app_name)) { fprintf(f, "%s\n", ai.app_name); } if (strlen(ai.project_preferences)) { fprintf(f, "\n%s\n", ai.project_preferences); } if (strlen(ai.team_name)) { str1 = ai.team_name; xml_escape(str1, str2); fprintf(f, "%s\n", str2.c_str()); } if (strlen(ai.user_name)) { str1 = ai.user_name; xml_escape(str1, str2); fprintf(f, "%s\n", str2.c_str()); } if (strlen(ai.project_dir)) { fprintf(f, "%s\n", ai.project_dir); } if (strlen(ai.boinc_dir)) { fprintf(f, "%s\n", ai.boinc_dir); } if (strlen(ai.authenticator)) { fprintf(f, "%s\n", ai.authenticator); } if (strlen(ai.wu_name)) { fprintf(f, "%s\n", ai.wu_name); } #ifdef _WIN32 if (strlen(ai.comm_obj_name)) { fprintf(f, "%s\n", ai.comm_obj_name); } #else fprintf(f, "%d\n", ai.shm_key); #endif fprintf(f, "%d\n" "%f\n" "%f\n" "%f\n" "%f\n" "%f\n" "%f\n" "%f\n" "%f" "%f", ai.slot, ai.wu_cpu_time, ai.user_total_credit, ai.user_expavg_credit, ai.host_total_credit, ai.host_expavg_credit, ai.checkpoint_period, ai.fraction_done_update_period, ai.fraction_done_start, ai.fraction_done_end ); return 0; } int parse_init_data_file(FILE* f, APP_INIT_DATA& ai) { char buf[256]; memset(&ai, 0, sizeof(ai)); ai.fraction_done_start = 0; ai.fraction_done_end = 1; while (fgets(buf, 256, f)) { if (match_tag(buf, "")) { safe_strncpy(ai.project_preferences, "", sizeof(ai.project_preferences)); while (fgets(buf, 256, f)) { if (match_tag(buf, "")) break; safe_strcat(ai.project_preferences, buf); } continue; } else if (parse_str(buf, "", ai.app_name, sizeof(ai.app_name))) continue; else if (parse_str(buf, "", ai.user_name, sizeof(ai.user_name))) continue; else if (parse_str(buf, "", ai.team_name, sizeof(ai.team_name))) continue; else if (parse_str(buf, "", ai.project_dir, sizeof(ai.project_dir))) continue; else if (parse_str(buf, "", ai.boinc_dir, sizeof(ai.boinc_dir))) continue; else if (parse_str(buf, "", ai.authenticator, sizeof(ai.authenticator))) continue; else if (parse_str(buf, "", ai.wu_name, sizeof(ai.wu_name))) continue; #ifdef _WIN32 else if (parse_str(buf, "", ai.comm_obj_name, sizeof(ai.comm_obj_name))) continue; #else else if (parse_int(buf, "", ai.shm_key)) continue; #endif else if (parse_int(buf, "", ai.slot)) continue; else if (parse_double(buf, "", ai.user_total_credit)) continue; else if (parse_double(buf, "", ai.user_expavg_credit)) continue; else if (parse_double(buf, "", ai.host_total_credit)) continue; else if (parse_double(buf, "", ai.host_expavg_credit)) continue; else if (parse_double(buf, "", ai.wu_cpu_time)) continue; else if (parse_double(buf, "", ai.checkpoint_period)) continue; else if (parse_double(buf, "", ai.fraction_done_update_period)) continue; else if (parse_double(buf, "", ai.fraction_done_start)) continue; else if (parse_double(buf, "", ai.fraction_done_end)) continue; //else fprintf(stderr, "parse_init_data_file: unrecognized %s", buf); } return 0; } // TODO: this should handle arbitrarily many fd/filename pairs. // Also, give the tags better names // int write_fd_init_file(FILE* f, char *file_name, int fdesc, bool input_file) { if (input_file) { fprintf(f, "%s\n", file_name); fprintf(f, "%d\n", fdesc); } else { fprintf(f, "%s\n", file_name); fprintf(f, "%d\n", fdesc); } return 0; } // TODO: this should handle arbitrarily many fd/filename pairs. // Also, this shouldn't be doing the actual duping! // int parse_fd_init_file(FILE* f) { char buf[256],filename[256]; int filedesc; while (fgets(buf, 256, f)) { if (parse_str(buf, "", filename, sizeof(filename))) { if (fgets(buf, 256, f)) { if (parse_int(buf, "", filedesc)) { freopen(filename, "r", stdin); fprintf(stderr, "opened input file %s\n", filename); } } } else if (parse_str(buf, "", filename, sizeof(filename))) { if (fgets(buf, 256, f)) { if (parse_int(buf, "", filedesc)) { freopen(filename, "w", stdout); fprintf(stderr, "opened output file %s\n", filename); } } } else fprintf(stderr, "parse_fd_init_file: unrecognized %s", buf); } return 0; } APP_CLIENT_SHM::APP_CLIENT_SHM() { shm = 0; } bool APP_CLIENT_SHM::pending_msg(int seg_num) { if (seg_num < 0 || seg_num >= NUM_SEGS || shm == NULL) return false; return (shm[seg_num*SHM_SEG_SIZE]?true:false); } bool APP_CLIENT_SHM::get_msg(char *msg, int seg_num) { if (seg_num < 0 || seg_num >= NUM_SEGS || shm == NULL) return false; // Check if there's an available message // if (!shm[seg_num*SHM_SEG_SIZE]) return false; // Copy the message from shared memory // safe_strncpy(msg, &shm[(seg_num*SHM_SEG_SIZE)+1], SHM_SEG_SIZE-1); // Reset the message status flag // shm[seg_num*SHM_SEG_SIZE] = 0; return true; } bool APP_CLIENT_SHM::send_msg(char *msg,int seg_num) { if (seg_num < 0 || seg_num >= NUM_SEGS || shm == NULL) return false; // Check if there's already a message // //if (shm[seg_num*SHM_SEG_SIZE]) return false; // Copy the message into shared memory // safe_strncpy(&shm[(seg_num*SHM_SEG_SIZE)+1], msg, SHM_SEG_SIZE-1); // Set the message status flag // shm[seg_num*SHM_SEG_SIZE] = 1; return true; } void APP_CLIENT_SHM::reset_msgs(void) { if (shm == NULL) return; memset(shm, 0, sizeof(char)*NUM_SEGS*SHM_SEG_SIZE); } void APP_CLIENT_SHM::reset_msg(int seg_num) { if (seg_num < 0 || seg_num >= NUM_SEGS || shm == NULL) return; memset(&shm[seg_num*SHM_SEG_SIZE], 0, sizeof(char)*SHM_SEG_SIZE); } bool APP_CLIENT_SHM::send_graphics_msg(int seg, int msg, int mode) { switch (msg) { case GRAPHICS_MSG_SET_MODE: return send_msg(xml_graphics_modes[mode], seg); break; case GRAPHICS_MSG_REREAD_PREFS: return send_msg(REREAD_PREFS_MSG, seg); break; } return false; } bool APP_CLIENT_SHM::get_graphics_msg(int seg, int& msg, int& mode) { char buf[SHM_SEG_SIZE]; int i; if (!get_msg(buf, seg)) return false; for (i=0; i\n" " %d\n" " %d\n" " %f\n" "\n", gi->xsize, gi->ysize, gi->refresh_period ); return 0; } int parse_graphics_file(FILE* f, GRAPHICS_INFO* gi) { char buf[256]; while (fgets(buf, 256, f)) { if (match_tag(buf, "")) continue; if (match_tag(buf, "")) return 0; else if (parse_int(buf, "", gi->xsize)) continue; else if (parse_int(buf, "", gi->ysize)) continue; else if (parse_double(buf, "", gi->refresh_period)) continue; else fprintf(stderr, "parse_graphics_file: unrecognized %s", buf); } return ERR_XML_PARSE; } // create a file (new_link) which contains an XML // reference to existing file. // int boinc_link(const char *existing, const char *new_link) { FILE *fp; fp = fopen(new_link, "w"); if (!fp) return ERR_FOPEN; fprintf(fp, "%s\n", existing); fclose(fp); return 0; } // resolve XML soft link // int boinc_resolve_filename(const char *virtual_name, char *physical_name, int len) { FILE *fp; char buf[512]; safe_strncpy(physical_name, virtual_name, len); // Open the file and load the first line // fp = boinc_fopen(virtual_name, "r"); if (!fp) return ERR_FOPEN; fgets(buf, 512, fp); fclose(fp); // If it's the XML tag, return its value, // otherwise, return the original file name // parse_str(buf, "", physical_name, len); return 0; } // resolve XML soft link // int boinc_resolve_filename_s(const char *virtual_name, string& physical_name) { physical_name = virtual_name; // Open the file and load the first line FILE *fp = fopen(virtual_name, "r"); if (!fp) return ERR_FOPEN; char buf[512]; fgets(buf, 512, fp); fclose(fp); // If it's the XML tag, return its value, // otherwise, return the original file name // parse_str(buf, "", physical_name); return 0; }