/* Retrieve keys from URL and write the result to the provided output stream OUTFP. */ gpg_error_t ks_action_fetch (ctrl_t ctrl, const char *url, estream_t outfp) { gpg_error_t err = 0; estream_t infp; parsed_uri_t parsed_uri; /* The broken down URI. */ if (!url) return gpg_error (GPG_ERR_INV_URI); err = http_parse_uri (&parsed_uri, url, 1); if (err) return err; if (parsed_uri->is_http) { err = ks_http_fetch (ctrl, url, &infp); if (!err) { err = copy_stream (infp, outfp); es_fclose (infp); } } else if (!parsed_uri->opaque) { err = gpg_error (GPG_ERR_INV_URI); } else if (!strcmp (parsed_uri->scheme, "finger")) { err = ks_finger_fetch (ctrl, parsed_uri, &infp); if (!err) { err = copy_stream (infp, outfp); es_fclose (infp); } } else if (!strcmp (parsed_uri->scheme, "kdns")) { err = ks_kdns_fetch (ctrl, parsed_uri, &infp); if (!err) { err = copy_stream (infp, outfp); es_fclose (infp); } } else err = gpg_error (GPG_ERR_INV_URI); http_release_parsed_uri (parsed_uri); return err; }
void file::move_to_file() { std::string tmp_dir; if(temporary_dir_.empty()) { char const *tmp=getenv("TEMP"); if(!tmp) tmp=getenv("TMP"); if(!tmp) tmp="/tmp"; tmp_dir=tmp; } else { tmp_dir = temporary_dir_; } tmp_file_name_ = tmp_dir + "/cppcms_uploads_"; urandom_device rnd; char buf[16]; char rand[33]={0}; rnd.generate(buf,16); impl::tohex(buf,sizeof(buf),rand); tmp_file_name_.append(rand); tmp_file_name_+=".tmp"; file_.open(tmp_file_name_.c_str(), std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc); if(!file_) throw cppcms_error("Failed to create temporary file"); file_data_.seekg(0); copy_stream(file_data_,file_); file_data_.str(""); saved_in_file_ = 1; }
/* Get the requested keys (matching PATTERNS) using all configured keyservers and write the result to the provided output stream. */ gpg_error_t ks_action_get (ctrl_t ctrl, strlist_t patterns, estream_t outfp) { gpg_error_t err = 0; gpg_error_t first_err = 0; int any_server = 0; int any_data = 0; strlist_t sl; uri_item_t uri; estream_t infp; if (!patterns) return gpg_error (GPG_ERR_NO_USER_ID); /* FIXME: We only take care of the first keyserver. To fully support multiple keyservers we need to track the result for each pattern and use the next keyserver if one key was not found. The keyservers might not all be fully synced thus it is not clear whether the first keyserver has the freshest copy of the key. Need to think about a better strategy. */ for (uri = ctrl->keyservers; !err && uri; uri = uri->next) { if (uri->parsed_uri->is_http) { any_server = 1; for (sl = patterns; !err && sl; sl = sl->next) { err = ks_hkp_get (ctrl, uri->parsed_uri, sl->d, &infp); if (err) { /* It is possible that a server does not carry a key, thus we only save the error and continue with the next pattern. FIXME: It is an open question how to return such an error condition to the caller. */ first_err = err; err = 0; } else { err = copy_stream (infp, outfp); /* Reading from the keyserver should never fail, thus return this error. */ if (!err) any_data = 1; es_fclose (infp); infp = NULL; } } } if (any_data) break; /* Stop loop after a keyserver returned something. */ } if (!any_server) err = gpg_error (GPG_ERR_NO_KEYSERVER); else if (!err && first_err && !any_data) err = first_err; return err; }
intrusive_ptr<ConfigImpl> load_from_file( Char const* fname, cfg_pair_t const* symbols) { FileStreamInput fin(fname); MemoryStreamOutput mout; copy_stream(fin, mout); return load_from_string_r(jag_reinterpret_cast<Char const*>(mout.data()), static_cast<size_t>(mout.tell()), symbols); }
void create_bbs_message(char *directory,char *copyfile,char *subject, int user,char *from, struct board_info *new_board, int num_msg, struct bbs_board_info *bbs_info) { int flag = 0; struct ffblk look_up; char s[27]; char str[60]; char temp[29]; FILE *file_write; FILE *file_read; time_t now; while (!flag) { sprintf(s,"%s\\m%04dx%02d",directory,user_lines[tswitch].number,(dans_counter % 100)); lock_dos(); flag = findfirst(s,&look_up,FA_NORMAL); unlock_dos(); next_task(); }; lock_dos(); if ((file_write=g_fopen(s,"wb","BBS#1"))==NULL) { #ifdef DEBUG log_error(s); #endif unlock_dos(); return; }; fprintf(file_write,"|*h1|*f4 User: |*f7(#%03d) %s|*r1%s",user,from,cr_lf); fprintf(file_write,"|*h1|*f4Subject: |*f7%s|*r1%s",subject,cr_lf); time(&now); str_time(temp,30,localtime(&now)); strftime(s,70,"%a %b %d %Y",localtime(&now)); sprintf(str,"%s %s ",s,temp); fprintf(file_write," |*f4|*h1Date:|*f7 %s%s%s|*r1",str,cr_lf,cr_lf); if ((file_read=g_fopen(copyfile,"rb","BBS#2"))==NULL) { log_error(copyfile); g_fclose(file_write); unlock_dos(); return; }; copy_stream(file_read,file_write); g_fclose(file_read); g_fclose(file_write); if (num_msg >= new_board->limit_messages) { sprintf(s,"%s\\%s",directory,bbs_info[0].filename); remove(s); }; unlock_dos(); };
void file::save_by_copy(std::string const &file_name,std::istream &in) { booster::nowide::ofstream f(file_name.c_str(),std::ios_base::binary | std::ios_base::out); if(!f) { throw cppcms_error("Failed to save open file:"+file_name); } copy_stream(in,f); f << std::flush; f.close(); }
/* Amiga Kickstart-like floppy for Amiga 1000 */ static int cmd_amiga_kickdisk(FILE* infile, const char* infilename, FILE* outfile, const char* outfilename) { int ret; /* boolean return value: 0 == error, 1 == OK */ size_t written; /* Number of bytes written this time */ size_t source_size; size_t bootblock_size = 512; size_t rom_size = 256 * 1024; size_t target_size = 880 * 1024; size_t pad_size; printf("# Padding %s to Amiga 1000 Kickstart disk into %s\n", infilename, outfilename); /* Get the input file size */ source_size = get_file_size(infile, infilename); if (source_size == SIZE_ERROR) return 0; /* Check if the input file size is not too big */ if (source_size != rom_size) { fprintf(stderr, "%s: %s has invalid size: %lu bytes (should be %lu bytes)\n", g_argv0, infilename, (unsigned long)source_size, (unsigned long)rom_size); return 0; } /* Write the Kickstart bootblock */ strncpy((char*)g_buffer, "KICK", bootblock_size); written = fwrite(g_buffer, 1, bootblock_size, outfile); if (written != bootblock_size) { fprintf(stderr, "%s: %s: %s\n", g_argv0, outfilename, strerror(errno)); return 0; } /* Copy the input file */ ret = copy_stream(infile, infilename, outfile, outfilename, source_size); if (!ret) return ret; /* Pad with zeroes */ pad_size = target_size - bootblock_size - source_size; ret = write_byte_block(outfile, outfilename, 0, pad_size); if (!ret) return ret; printf("# %s done\n", outfilename); return 1; }
void processReq_Raw(int fd) { int f1; if (busy == FALSE) { if ((f1 = open_printer()) >= 0) //modify by Lisa { if (copy_stream(fd, f1) < 0) { //syslog(0, "copy stream err\n"); } close(f1); } } close(fd); }
/* Steem Engine cartridge image */ static int cmd_stc(FILE* infile, const char* infilename, FILE* outfile, const char* outfilename) { size_t source_size; size_t target_size = 128 * 1024; size_t free_size; int ret; /* boolean return value: 0 == error, 1 == OK */ printf("# Padding %s to %ld kB Steem Engine cartridge image into %s\n", infilename, ((long)target_size) / 1024, outfilename); /* Get the input file size */ source_size = get_file_size(infile, infilename); if (source_size == SIZE_ERROR) return 0; /* Check if the input file size is not too big */ if (source_size > target_size) { fprintf(stderr, "%s: %s is too big: %lu extra bytes\n", g_argv0, infilename, (unsigned long)(source_size - target_size)); return 0; } /* Insert a long zero at the beginning */ ret = write_byte_block(outfile, outfilename, 0, 4); if (!ret) return ret; /* Copy the input file */ ret = copy_stream(infile, infilename, outfile, outfilename, source_size); if (!ret) return ret; /* Pad with zeroes */ free_size = target_size - source_size; ret = write_byte_block(outfile, outfilename, 0, free_size); if (!ret) return ret; printf("# %s done (%lu bytes free)\n", outfilename, (unsigned long)free_size); return 1; }
void processReq_Raw(int fd) { int f1; //PRINT("fd=%d \n",fd); //strcpy(clientaddr , inet_ntoa(client.sin_addr)); //PRINT("Connection from %s prot %d accepted \n",inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port)); //syslog(LOGOPTS, "Connection from %s port %hd accepted\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port)); /*write(fd, "Printing", 8);*/ if (lptstatus.busy == FALSE) { if ((f1 = open_printer()) >= 0) //modify by Lisa { if (copy_stream(fd, f1) < 0) syslog(LOGOPTS, "read: %m\n"); close(f1); } (void)close(fd); } else return 0; }
void one_job(int lpnumber) { int lp, open_sleep = 10; struct sockaddr_storage client; socklen_t clientlen = sizeof(client); if (getpeername(0, (struct sockaddr *)&client, &clientlen) >= 0) { char host[INET6_ADDRSTRLEN]; dolog(LOG_NOTICE, "Connection from %s port %hu\n", get_ip_str((struct sockaddr *)&client, host, sizeof(host)), get_port((struct sockaddr *)&client)); } if (get_lock(lpnumber) == 0) return; /* Make sure lp device is open... */ while ((lp = open_printer(lpnumber)) == -1) { sleep(open_sleep); if (open_sleep < 320) /* ~5 min interval to avoid spam in syslog */ open_sleep *= 2; } if (copy_stream(0, lp) < 0) dolog(LOGOPTS, "copy_stream: %m\n"); close(lp); free_lock(); }
/* Search all configured keyservers for keys matching PATTERNS and write the result to the provided output stream. */ gpg_error_t ks_action_search (ctrl_t ctrl, strlist_t patterns, estream_t outfp) { gpg_error_t err = 0; int any_server = 0; uri_item_t uri; estream_t infp; if (!patterns) return gpg_error (GPG_ERR_NO_USER_ID); /* FIXME: We only take care of the first pattern. To fully support multiple patterns we might either want to run several queries in parallel and merge them. We also need to decide what to do with errors - it might not be the best idea to ignore an error from one server and silently continue with another server. For now we stop at the first error. */ for (uri = ctrl->keyservers; !err && uri; uri = uri->next) { if (uri->parsed_uri->is_http) { any_server = 1; err = ks_hkp_search (ctrl, uri->parsed_uri, patterns->d, &infp); if (!err) { err = copy_stream (infp, outfp); es_fclose (infp); break; } } } if (!any_server) err = gpg_error (GPG_ERR_NO_KEYSERVER); return err; }
void server(int lpnumber) { struct rlimit resourcelimit; #ifdef USE_GETPROTOBYNAME struct protoent *proto; #endif int netfd = -1, fd, lp, one = 1; int open_sleep = 10; socklen_t clientlen; struct sockaddr_storage client; struct addrinfo hints, *res, *ressave; char pidfilename[sizeof(PIDFILE)]; char service[10]; // 9100 (65535 max) FILE *f; const int bufsiz = 65536; #ifndef TESTING if (!log_to_stdout) { switch (fork()) { case -1: dolog(LOGOPTS, "fork: %m\n"); exit(1); case 0: /* child */ break; default: /* parent */ exit(0); } /* Now in child process */ resourcelimit.rlim_max = 0; if (getrlimit(RLIMIT_NOFILE, &resourcelimit) < 0) { dolog(LOGOPTS, "getrlimit: %m\n"); exit(1); } for (fd = 0; fd < resourcelimit.rlim_max; ++fd) (void)close(fd); if (setsid() < 0) { dolog(LOGOPTS, "setsid: %m\n"); exit(1); } (void)chdir("/"); (void)umask(022); fd = open("/dev/null", O_RDWR); /* stdin */ (void)dup(fd); /* stdout */ (void)dup(fd); /* stderr */ (void)snprintf(pidfilename, sizeof(pidfilename), PIDFILE, lpnumber); if ((f = fopen(pidfilename, "w")) == NULL) { dolog(LOGOPTS, "%s: %m\n", pidfilename); exit(1); } (void)fprintf(f, "%d\n", getpid()); (void)fclose(f); } if (get_lock(lpnumber) == 0) exit(1); #endif memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_flags = AI_PASSIVE; hints.ai_socktype = SOCK_STREAM; (void)snprintf(service, sizeof(service), "%hu", (BASEPORT + lpnumber - '0')); if (getaddrinfo(bindaddr, service, &hints, &res) != 0) { dolog(LOGOPTS, "getaddr: %m\n"); exit(1); } ressave = res; while (res) { #ifdef USE_GETPROTOBYNAME if ((proto = getprotobyname("tcp6")) == NULL) { if ((proto = getprotobyname("tcp")) == NULL) { dolog(LOGOPTS, "Cannot find protocol for TCP!\n"); exit(1); } } if ((netfd = socket(res->ai_family, res->ai_socktype, proto->p_proto)) < 0) #else if ((netfd = socket(res->ai_family, res->ai_socktype, IPPROTO_IP)) < 0) #endif { dolog(LOGOPTS, "socket: %m\n"); close(netfd); res = res->ai_next; continue; } if (setsockopt(netfd, SOL_SOCKET, SO_RCVBUF, &bufsiz, sizeof(bufsiz)) < 0) { dolog(LOGOPTS, "setsocketopt: SO_RCVBUF: %m\n"); /* not fatal if it fails */ } if (setsockopt(netfd, SOL_SOCKET, SO_SNDBUF, &bufsiz, sizeof(bufsiz)) < 0) { dolog(LOGOPTS, "setsocketopt: SO_SNDBUF: %m\n"); /* not fatal if it fails */ } if (setsockopt(netfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { dolog(LOGOPTS, "setsocketopt: SO_REUSEADDR: %m\n"); close(netfd); res = res->ai_next; continue; } if (bind(netfd, res->ai_addr, res->ai_addrlen) < 0) { dolog(LOGOPTS, "bind: %m\n"); close(netfd); res = res->ai_next; continue; } if (listen(netfd, 5) < 0) { dolog(LOGOPTS, "listen: %m\n"); close(netfd); res = res->ai_next; continue; } break; } freeaddrinfo(ressave); clientlen = sizeof(client); memset(&client, 0, sizeof(client)); while ((fd = accept(netfd, (struct sockaddr *)&client, &clientlen)) >= 0) { char host[INET6_ADDRSTRLEN]; #ifdef USE_LIBWRAP if (hosts_ctl("p910nd", STRING_UNKNOWN, get_ip_str((struct sockaddr *)&client, host, sizeof(host)), STRING_UNKNOWN) == 0) { dolog(LOGOPTS, "Connection from %s port %hu rejected\n", get_ip_str((struct sockaddr *)&client, host, sizeof(host)), get_port((struct sockaddr *)&client)); close(fd); continue; } #endif dolog(LOG_NOTICE, "Connection from %s port %hu accepted\n", get_ip_str((struct sockaddr *)&client, host, sizeof(host)), get_port((struct sockaddr *)&client)); /*write(fd, "Printing", 8); */ /* Make sure lp device is open... */ while ((lp = open_printer(lpnumber)) == -1) { sleep(open_sleep); if (open_sleep < 320) /* ~5 min interval to avoid spam in syslog */ open_sleep *= 2; } open_sleep = 10; if (copy_stream(fd, lp) < 0) dolog(LOGOPTS, "copy_stream: %m\n"); (void)close(fd); (void)close(lp); } dolog(LOGOPTS, "accept: %m\n"); free_lock(); exit(1); }
int main(int argc, char** argv) { #ifndef _USING_FCGI_ FILE* fin, *fout; #else FCGI_FILE *fin, *fout; #endif int i, retval; char req_path[MAXPATHLEN], reply_path[MAXPATHLEN]; char log_path[MAXPATHLEN], path[MAXPATHLEN]; unsigned int counter=0; char* code_sign_key; int length = -1; log_messages.pid = getpid(); bool debug_log = false; for (i=1; i<argc; i++) { if (!strcmp(argv[i], "--batch")) { batch = true; continue; } else if (!strcmp(argv[i], "--mark_jobs_done")) { mark_jobs_done = true; } else if (!strcmp(argv[i], "--debug_log")) { debug_log = true; #ifdef GCL_SIMULATOR } else if (!strcmp(argv[i], "--simulator")) { if(!argv[++i]) { log_messages.printf(MSG_CRITICAL, "%s requires an argument\n\n", argv[--i]); usage(argv[0]); exit(1); } simtime = atof(argv[i]); #endif } else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { usage(argv[0]); exit(0); } else if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) { printf("%s\n", SVN_VERSION); exit(0); } else if (strlen(argv[i])){ log_messages.printf(MSG_CRITICAL, "unknown command line argument: %s\n\n", argv[i]); usage(argv[0]); exit(1); } } // install a signal handler that catches SIGTERMS sent by Apache if the CGI // times out. // signal(SIGTERM, sigterm_handler); if (debug_log) { if (!freopen("debug_log", "w", stderr)) { fprintf(stderr, "Can't redirect stderr\n"); exit(1); } } else { char *stderr_buffer; get_log_path(path, "scheduler.log"); #ifndef _USING_FCGI_ char buf[256]; if (!freopen(path, "a", stderr)) { fprintf(stderr, "Can't redirect stderr\n"); sprintf(buf, "Server can't open log file (%s)", path); send_message(buf, 3600); exit(1); } #else FCGI_FILE* f = FCGI::fopen(path, "a"); if (f) { log_messages.redirect(f); } else { char buf[256]; fprintf(stderr, "Can't redirect FCGI log messages\n"); sprintf(buf, "Server can't open log file for FCGI (%s)", path); send_message(buf, 3600); exit(1); } #endif // install a larger buffer for stderr. This ensures that // log information from different scheduler requests running // in parallel aren't intermingled in the log file. // if (config.scheduler_log_buffer) { stderr_buffer = (char*)malloc(config.scheduler_log_buffer); if (!stderr_buffer) { log_messages.printf(MSG_CRITICAL, "Unable to allocate stderr buffer\n" ); } else { #ifdef _USING_FCGI_ retval = setvbuf( f->stdio_stream, stderr_buffer, _IOFBF, config.scheduler_log_buffer ); #else retval = setvbuf( stderr, stderr_buffer, _IOFBF, config.scheduler_log_buffer ); #endif if (retval) { log_messages.printf(MSG_CRITICAL, "Unable to change stderr buffering\n" ); } } } } srand(time(0)+getpid()); log_messages.set_debug_level(DEBUG_LEVEL); #if DUMP_CORE_ON_SEGV set_core_dump_size_limit(); #endif retval = config.parse_file(); if (retval) { log_messages.printf(MSG_CRITICAL, "Can't parse config.xml: %s\n", boincerror(retval) ); send_message("Server can't parse configuration file", 3600); exit(0); } log_messages.set_debug_level(config.sched_debug_level); if (config.sched_debug_level == 4) g_print_queries = true; gui_urls.init(); project_files.init(); init_file_delete_regex(); sprintf(path, "%s/code_sign_public", config.key_dir); retval = read_file_malloc(path, code_sign_key); if (retval) { log_messages.printf(MSG_CRITICAL, "Can't read code sign key file (%s)\n", path ); send_message("Server can't find key file", 3600); exit(0); } strip_whitespace(code_sign_key); g_pid = getpid(); #ifdef _USING_FCGI_ //while(FCGI_Accept() >= 0 && counter < MAX_FCGI_COUNT) { while(FCGI_Accept() >= 0) { counter++; log_messages.set_indent_level(0); #endif if (config.debug_request_headers) { log_request_headers(length); } if (!debug_log && check_stop_sched()) { send_message("Project is temporarily shut down for maintenance", 3600); goto done; } if (!ssp) { attach_to_feeder_shmem(); } if (!ssp) { send_message("Server error: can't attach shared memory", 3600); goto done; } if (strlen(config.debug_req_reply_dir)) { struct stat statbuf; // 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). // NOTE: to use this, you must create group-writeable dirs // boinc_req and boinc_reply in the project dir // sprintf(req_path, "%s/%d_%u_sched_request.xml", config.debug_req_reply_dir, g_pid, counter); sprintf(reply_path, "%s/%d_%u_sched_reply.xml", config.debug_req_reply_dir, g_pid, counter); // keep an own 'log' per PID in case general logging fails // this allows to associate at leas the scheduler request with the client // IP address (as shown in httpd error log) in case of a crash sprintf(log_path, "%s/%d_%u_sched.log", config.debug_req_reply_dir, g_pid, counter); #ifndef _USING_FCGI_ fout = fopen(log_path, "a"); #else fout = FCGI::fopen(log_path,"a"); #endif fprintf(fout, "PID: %d Client IP: %s\n", g_pid, get_remote_addr()); fclose(fout); log_messages.printf(MSG_DEBUG, "keeping sched_request in %s, sched_reply in %s, custom log in %s\n", req_path, reply_path, log_path ); #ifndef _USING_FCGI_ fout = fopen(req_path, "w"); #else fout = FCGI::fopen(req_path,"w"); #endif if (!fout) { log_messages.printf(MSG_CRITICAL, "can't write request file\n" ); exit(1); } copy_stream(stdin, fout); fclose(fout); stat(req_path, &statbuf); if (length>=0 && (statbuf.st_size != length)) { log_messages.printf(MSG_CRITICAL, "Request length %d != CONTENT_LENGTH %d\n", (int)statbuf.st_size, length ); } #ifndef _USING_FCGI_ fin = fopen(req_path, "r"); #else fin = FCGI::fopen(req_path,"r"); #endif if (!fin) { log_messages.printf(MSG_CRITICAL, "can't read request file\n" ); exit(1); } #ifndef _USING_FCGI_ fout = fopen(reply_path, "w"); #else fout = FCGI::fopen(reply_path, "w"); #endif if (!fout) { log_messages.printf(MSG_CRITICAL, "can't write reply file\n" ); exit(1); } handle_request(fin, fout, code_sign_key); fclose(fin); fclose(fout); #ifndef _USING_FCGI_ fin = fopen(reply_path, "r"); #else fin = FCGI::fopen(reply_path, "r"); #endif if (!fin) { log_messages.printf(MSG_CRITICAL, "can't read reply file\n" ); exit(1); } copy_stream(fin, stdout); fclose(fin); // if not contacted from a client, don't keep the log files /* not sure what lead to the assumption of a client setting CONTENT_LENGTH, but it's wrong at least on our current project / Apache / Client configuration. Commented out. if (getenv("CONTENT_LENGTH")) { unlink(req_path); unlink(reply_path); } */ #ifndef _USING_FCGI_ } else if (batch) { while (!feof(stdin)) { handle_request(stdin, stdout, code_sign_key); fflush(stdout); } #endif } else { handle_request(stdin, stdout, code_sign_key); fflush(stderr); } done: #ifdef _USING_FCGI_ if (config.debug_fcgi) { log_messages.printf(MSG_NORMAL, "FCGI: counter: %d\n", counter ); log_messages.flush(); } } // do() if (counter == MAX_FCGI_COUNT) { fprintf(stderr, "FCGI: counter passed MAX_FCGI_COUNT - exiting..\n"); } else { fprintf(stderr, "FCGI: FCGI_Accept failed - exiting..\n"); } // when exiting, write headers back to apache so it won't complain // about "incomplete headers" fprintf(stdout,"Content-type: text/plain\n\n"); #endif if (db_opened) { boinc_db.close(); } }
/* Search all configured keyservers for keys matching PATTERNS and write the result to the provided output stream. */ gpg_error_t ks_action_search (ctrl_t ctrl, uri_item_t keyservers, strlist_t patterns, estream_t outfp) { gpg_error_t err = 0; int any_server = 0; int any_results = 0; uri_item_t uri; estream_t infp; if (!patterns) return gpg_error (GPG_ERR_NO_USER_ID); /* FIXME: We only take care of the first pattern. To fully support multiple patterns we might either want to run several queries in parallel and merge them. We also need to decide what to do with errors - it might not be the best idea to ignore an error from one server and silently continue with another server. For now we stop at the first error, unless the server responds with '404 Not Found', in which case we try the next server. */ for (uri = keyservers; !err && uri; uri = uri->next) { int is_http = uri->parsed_uri->is_http; int is_ldap = 0; unsigned int http_status = 0; #if USE_LDAP is_ldap = (strcmp (uri->parsed_uri->scheme, "ldap") == 0 || strcmp (uri->parsed_uri->scheme, "ldaps") == 0 || strcmp (uri->parsed_uri->scheme, "ldapi") == 0); #endif if (is_http || is_ldap) { any_server = 1; #if USE_LDAP if (is_ldap) err = ks_ldap_search (ctrl, uri->parsed_uri, patterns->d, &infp); else #endif { err = ks_hkp_search (ctrl, uri->parsed_uri, patterns->d, &infp, &http_status); } if (err == gpg_error (GPG_ERR_NO_DATA) && http_status == 404 /* not found */) { /* No record found. Clear error and try next server. */ err = 0; continue; } if (!err) { err = copy_stream (infp, outfp); es_fclose (infp); any_results = 1; break; } } } if (!any_server) err = gpg_error (GPG_ERR_NO_KEYSERVER); else if (err == 0 && !any_results) err = gpg_error (GPG_ERR_NO_DATA); return err; }
/** * @brief initialize segmenter with source context * @param context segmenter context * @param source input source context * @param filter stream filter * @return 0 on success, negative error code on failure */ int segmenter_init(SegmenterContext *context, AVFormatContext *source, char* file_base_name, char* media_base_name, double target_duration, int media_filter) { int i; int video_index = -1, audio_index = -1; for (i=0; i < source->nb_streams && (video_index < 0 || audio_index < 0); i++) { AVStream *_stream = source->streams[i]; switch (_stream->codec->codec_type) { case AVMEDIA_TYPE_VIDEO: if ((media_filter & MediaTypeVideo) && !load_decoder(_stream->codec)) { video_index = i; } break; case AVMEDIA_TYPE_AUDIO: if ((media_filter & MediaTypeAudio) && !load_decoder(_stream->codec)) { audio_index = i; } break; default: break; } } if (video_index < 0 && audio_index < 0) { return SGERROR(SGERROR_NO_STREAM); } context->source_video_index = video_index; context->source_audio_index = audio_index; AVOutputFormat *oformat; if (video_index < 0) { switch (source->streams[audio_index]->codec->codec_id) { case CODEC_ID_AAC: oformat = av_guess_format(kFormatADTS, NULL, NULL); context->extension = kExtensionAAC; break; case CODEC_ID_MP3: oformat = av_guess_format(kFormatMP3, NULL, NULL); context->extension = kExtensionMP3; default: oformat = av_guess_format(kFormatMPEGTS, NULL, NULL); context->extension = kExtensionMPEGTS; break; } } else { oformat = av_guess_format(kFormatMPEGTS, NULL, NULL); context->extension = kExtensionMPEGTS; } if (!oformat) { return SGERROR(SGERROR_UNSUPPORTED_FORMAT); } if (avformat_alloc_output_context2(&context->output, oformat, NULL, NULL)) { return SGERROR(SGERROR_MEM_ALLOC); } context->video = video_index >= 0 ? copy_stream(context->output, source->streams[video_index]) : NULL; context->audio = audio_index >= 0 ? copy_stream(context->output, source->streams[audio_index]) : NULL; context->file_base_name = file_base_name; context->media_base_name = media_base_name; context->target_duration = target_duration; context->buf_size = snprintf(NULL, 0, "%s/%s%u.%s", context->file_base_name, context->media_base_name, UINT_MAX, context->extension) + 1; if (!(context->buf = (char*)malloc(context->buf_size * sizeof(char)))) { return SGERROR(SGERROR_MEM_ALLOC); } return 0; }
int main(int argc, char** argv) { #ifndef _USING_FCGI_ FILE* fin, *fout; #else FCGI_FILE *fin, *fout; #endif int i, retval; char req_path[256], reply_path[256], path[256]; unsigned int counter=0; char* code_sign_key; int length=-1; log_messages.pid = getpid(); bool debug_log = false; for (i=1; i<argc; i++) { if (!strcmp(argv[i], "--batch")) { batch = true; continue; } else if (!strcmp(argv[i], "--mark_jobs_done")) { mark_jobs_done = true; } else if (!strcmp(argv[i], "--debug_log")) { debug_log = true; #ifdef GCL_SIMULATOR } else if (!strcmp(argv[i], "--simulator")) { simtime = atof(argv[++i]); #endif } else { usage(argv[0]); } } // install a signal handler that catches SIGTERMS sent by Apache if the cgi // times out. // signal(SIGTERM, sigterm_handler); if (debug_log) { freopen("debug_log", "w", stderr); } else { char *stderr_buffer, buf[256]; get_log_path(path, "scheduler.log"); #ifndef _USING_FCGI_ if (!freopen(path, "a", stderr)) { fprintf(stderr, "Can't redirect stderr\n"); sprintf(buf, "Server can't open log file (%s)", path); send_message(buf, 3600); exit(1); } // install a larger buffer for stderr. This ensures that // log information from different scheduler requests running // in parallel don't collide in the log file and appear intermingled. // if (!(stderr_buffer=(char *)malloc(32768)) || setvbuf(stderr, stderr_buffer, _IOFBF, 32768)) { log_messages.printf(MSG_CRITICAL, "Unable to change stderr buffering preferences\n" ); } #else FCGI_FILE* f = FCGI::fopen(path, "a"); if (f) { log_messages.redirect(f); } else { char buf[256]; fprintf(stderr, "Can't redirect FCGI log messages\n"); sprintf(buf, "Server can't open log file for FCGI (%s)", path); send_message(buf, 3600); exit(1); } // set buffer as above, note that f is really a struct from fcgi_stdio.h if (!(stderr_buffer=(char *)malloc(32768)) || setvbuf(f->stdio_stream, stderr_buffer, _IOFBF, 32768)) { log_messages.printf(MSG_CRITICAL, "Unable to change stderr FCGI buffering preferences\n" ); } #endif } srand(time(0)+getpid()); log_messages.set_debug_level(DEBUG_LEVEL); #if DUMP_CORE_ON_SEGV set_core_dump_size_limit(); #endif retval = config.parse_file(); if (retval) { log_messages.printf(MSG_CRITICAL, "Can't parse config.xml: %s\n", boincerror(retval) ); send_message("Server can't parse configuration file", 3600); exit(0); } log_messages.set_debug_level(config.sched_debug_level); gui_urls.init(); project_files.init(); sprintf(path, "%s/code_sign_public", config.key_dir); retval = read_file_malloc(path, code_sign_key); if (retval) { log_messages.printf(MSG_CRITICAL, "Can't read code sign key file (%s)\n", path ); send_message("Server can't find key file", 3600); exit(0); } g_pid = getpid(); #ifdef _USING_FCGI_ //while(FCGI_Accept() >= 0 && counter < MAX_FCGI_COUNT) { while(FCGI_Accept() >= 0) { counter++; log_messages.set_indent_level(0); #endif if (config.debug_request_headers) { log_request_headers(length); } if (check_stop_sched()) { send_message("Project is temporarily shut down for maintenance", 3600); goto done; } if (!ssp) { attach_to_feeder_shmem(); } if (!ssp) { send_message("Server error: can't attach shared memory", 3600); goto done; } if (use_files) { struct stat statbuf; // 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). // NOTE: to use this, you must create group-writeable dirs // boinc_req and boinc_reply in the project dir // sprintf(req_path, "%s%d_%u", config.project_path(REQ_FILE_PREFIX), g_pid, counter); sprintf(reply_path, "%s%d_%u", config.project_path(REPLY_FILE_PREFIX), g_pid, counter); #ifndef _USING_FCGI_ fout = fopen(req_path, "w"); #else fout = FCGI::fopen(req_path,"w"); #endif if (!fout) { log_messages.printf(MSG_CRITICAL, "can't write request file\n" ); exit(1); } copy_stream(stdin, fout); fclose(fout); stat(req_path, &statbuf); if (length>=0 && (statbuf.st_size != length)) { log_messages.printf(MSG_CRITICAL, "Request length %d != CONTENT_LENGTH %d\n", (int)statbuf.st_size, length ); } #ifndef _USING_FCGI_ fin = fopen(req_path, "r"); #else fin = FCGI::fopen(req_path,"r"); #endif if (!fin) { log_messages.printf(MSG_CRITICAL, "can't read request file\n" ); exit(1); } #ifndef _USING_FCGI_ fout = fopen(reply_path, "w"); #else fout = FCGI::fopen(reply_path, "w"); #endif if (!fout) { log_messages.printf(MSG_CRITICAL, "can't write reply file\n" ); exit(1); } handle_request(fin, fout, code_sign_key); fclose(fin); fclose(fout); #ifndef _USING_FCGI_ fin = fopen(reply_path, "r"); #else fin = FCGI::fopen(reply_path, "r"); #endif if (!fin) { log_messages.printf(MSG_CRITICAL, "can't read reply file\n" ); exit(1); } copy_stream(fin, stdout); fclose(fin); #ifdef EINSTEIN_AT_HOME if (getenv("CONTENT_LENGTH")) unlink(req_path); if (getenv("CONTENT_LENGTH")) unlink(reply_path); #else // unlink(req_path); // unlink(reply_path); #endif #ifndef _USING_FCGI_ } else if (batch) { while (!feof(stdin)) { handle_request(stdin, stdout, code_sign_key); fflush(stdout); } #endif } else { handle_request(stdin, stdout, code_sign_key); } done: #ifdef _USING_FCGI_ log_messages.printf(MSG_DEBUG, "FCGI: counter: %d\n", counter ); log_messages.flush(); } // do() if (counter == MAX_FCGI_COUNT) { fprintf(stderr, "FCGI: counter passed MAX_FCGI_COUNT - exiting..\n"); } else { fprintf(stderr, "FCGI: FCGI_Accept failed - exiting..\n"); } // when exiting, write headers back to apache so it won't complain // about "incomplete headers" fprintf(stdout,"Content-type: text/plain\n\n"); #endif if (db_opened) { boinc_db.close(); } }
// do an account manager RPC; // if URL is null, detach from current account manager // int ACCT_MGR_OP::do_rpc(ACCT_MGR_INFO& _ami, bool _via_gui) { int retval; unsigned int i; char buf[256]; ami = _ami; error_num = ERR_IN_PROGRESS; error_str = ""; via_gui = _via_gui; global_prefs_xml = ""; // if null URL, detach from current AMS // if (!strlen(ami.master_url) && strlen(gstate.acct_mgr_info.master_url)) { msg_printf(NULL, MSG_INFO, "Removing account manager info"); gstate.acct_mgr_info.clear(); boinc_delete_file(ACCT_MGR_URL_FILENAME); boinc_delete_file(ACCT_MGR_LOGIN_FILENAME); error_num = 0; for (i=0; i<gstate.projects.size(); i++) { gstate.projects[i]->detach_ams(); } ::rss_feeds.update_feed_list(); gstate.set_client_state_dirty("detach from AMS"); return 0; } canonicalize_master_url(ami.master_url, sizeof(ami.master_url)); if (!valid_master_url(ami.master_url)) { error_num = ERR_INVALID_URL; return 0; } FILE* f = boinc_fopen(ACCT_MGR_REQUEST_FILENAME, "w"); if (!f) return ERR_FOPEN; fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" "<acct_mgr_request>\n" ); if (strlen(ami.authenticator)) { fprintf(f, " <authenticator>%s</authenticator>\n", ami.authenticator ); } else { fprintf(f, " <name>%s</name>\n" " <password_hash>%s</password_hash>\n", ami.login_name, ami.password_hash ); } fprintf(f, " <host_cpid>%s</host_cpid>\n" " <domain_name>%s</domain_name>\n" " <client_version>%d.%d.%d</client_version>\n" " <run_mode>%s</run_mode>\n", gstate.host_info.host_cpid, gstate.host_info.domain_name, gstate.core_client_version.major, gstate.core_client_version.minor, gstate.core_client_version.release, run_mode_name[gstate.cpu_run_mode.get_perm()] ); gstate.write_platforms(NULL, f); if (strlen(gstate.acct_mgr_info.previous_host_cpid)) { fprintf(f, " <previous_host_cpid>%s</previous_host_cpid>\n", gstate.acct_mgr_info.previous_host_cpid ); } // If the AMS requested it, send GUI RPC port and password. // This is for the "farm" account manager so it // can know where to send GUI RPC requests to // without having to configure each host // if (gstate.acct_mgr_info.send_gui_rpc_info) { if (gstate.cmdline_gui_rpc_port) { fprintf(f," <gui_rpc_port>%d</gui_rpc_port>\n", gstate.cmdline_gui_rpc_port); } else { fprintf(f," <gui_rpc_port>%d</gui_rpc_port>\n", GUI_RPC_PORT); } if (boinc_file_exists(GUI_RPC_PASSWD_FILE)) { char gui_rpc_password[256]; safe_strcpy(gui_rpc_password, ""); FILE* pwdf = fopen(GUI_RPC_PASSWD_FILE, "r"); if (pwdf) { if (fgets(gui_rpc_password, 256, pwdf)) { strip_whitespace(gui_rpc_password); } fclose(pwdf); } fprintf(f, " <gui_rpc_password>%s</gui_rpc_password>\n", gui_rpc_password ); } } for (i=0; i<gstate.projects.size(); i++) { PROJECT* p = gstate.projects[i]; double not_started_dur, in_progress_dur; p->get_task_durs(not_started_dur, in_progress_dur); fprintf(f, " <project>\n" " <url>%s</url>\n" " <project_name>%s</project_name>\n" " <suspended_via_gui>%d</suspended_via_gui>\n" " <hostid>%d</hostid>\n" " <not_started_dur>%f</not_started_dur>\n" " <in_progress_dur>%f</in_progress_dur>\n" " <attached_via_acct_mgr>%d</attached_via_acct_mgr>\n" " <dont_request_more_work>%d</dont_request_more_work>\n" " <detach_when_done>%d</detach_when_done>\n" " <ended>%d</ended>\n" " <resource_share>%f</resource_share>\n" " <disk_usage>%f</disk_usage>\n" " <disk_share>%f</disk_share>\n", p->master_url, p->project_name, p->suspended_via_gui?1:0, p->hostid, not_started_dur, in_progress_dur, p->attached_via_acct_mgr?1:0, p->dont_request_more_work?1:0, p->detach_when_done?1:0, p->ended?1:0, p->resource_share, p->disk_usage, p->disk_share ); // send work and starvation-related info // if (ami.dynamic) { fprintf(f, " <nrpc_failures>%d</nrpc_failures>\n" " <cpu_ec>%f</cpu_ec>\n" " <cpu_time>%f</cpu_time>\n" " <gpu_ec>%f</gpu_ec>\n" " <gpu_time>%f</gpu_time>\n" " <njobs_success>%d</njobs_success>\n" " <njobs_error>%d</njobs_error>\n", p->nrpc_failures, p->cpu_ec, p->cpu_time, p->gpu_ec, p->gpu_time, p->njobs_success, p->njobs_error ); for (int j=0; j<coprocs.n_rsc; j++) { if (p->sched_req_no_work[j]) { fprintf(f, " <sched_req_no_work>%s</sched_req_no_work>\n", coprocs.coprocs[j].type ); } } } if (p->attached_via_acct_mgr) { fprintf(f, " <account_key>%s</account_key>\n", p->authenticator ); } fprintf(f, " </project>\n" ); } MIOFILE mf; mf.init_file(f); // send working prefs // fprintf(f, "<working_global_preferences>\n"); gstate.global_prefs.write(mf); fprintf(f, "</working_global_preferences>\n"); if (boinc_file_exists(GLOBAL_PREFS_FILE_NAME)) { FILE* fprefs = fopen(GLOBAL_PREFS_FILE_NAME, "r"); if (fprefs) { copy_stream(fprefs, f); fclose(fprefs); } } gstate.host_info.write(mf, !cc_config.suppress_net_info, true); if (strlen(gstate.acct_mgr_info.opaque)) { fprintf(f, " <opaque>\n%s\n" " </opaque>\n", gstate.acct_mgr_info.opaque ); } gstate.time_stats.write(mf, true); gstate.net_stats.write(mf); fprintf(f, "</acct_mgr_request>\n"); fclose(f); snprintf(buf, sizeof(buf), "%srpc.php", ami.master_url); retval = gui_http->do_rpc_post( this, buf, ACCT_MGR_REQUEST_FILENAME, ACCT_MGR_REPLY_FILENAME, true ); if (retval) { error_num = retval; return retval; } msg_printf(NULL, MSG_INFO, "Contacting account manager at %s", ami.master_url); return 0; }
// do an account manager RPC; // if URL is null, detach from current account manager // int ACCT_MGR_OP::do_rpc( std::string _url, std::string name, std::string password_hash, bool _via_gui ) { int retval; unsigned int i; char url[256], password[256], buf[256]; FILE *pwdf; strlcpy(url, _url.c_str(), sizeof(url)); error_num = ERR_IN_PROGRESS; via_gui = _via_gui; if (global_prefs_xml) { free(global_prefs_xml); global_prefs_xml = 0; } // if null URL, detach from current AMS // if (!strlen(url) && strlen(gstate.acct_mgr_info.acct_mgr_url)) { msg_printf(NULL, MSG_INFO, "Removing account manager info"); gstate.acct_mgr_info.clear(); boinc_delete_file(ACCT_MGR_URL_FILENAME); boinc_delete_file(ACCT_MGR_LOGIN_FILENAME); error_num = 0; for (i=0; i<gstate.projects.size(); i++) { PROJECT* p = gstate.projects[i]; p->attached_via_acct_mgr = false; p->ams_resource_share = -1; } return 0; } canonicalize_master_url(url); if (!valid_master_url(url)) { error_num = ERR_INVALID_URL; return 0; } strlcpy(ami.acct_mgr_url, url, sizeof(ami.acct_mgr_url)); strlcpy(ami.acct_mgr_name, "", sizeof(ami.acct_mgr_name)); strlcpy(ami.login_name, name.c_str(), sizeof(ami.login_name)); strlcpy(ami.password_hash, password_hash.c_str(), sizeof(ami.password_hash)); FILE* f = boinc_fopen(ACCT_MGR_REQUEST_FILENAME, "w"); if (!f) return ERR_FOPEN; fprintf(f, "<acct_mgr_request>\n" " <name>%s</name>\n" " <password_hash>%s</password_hash>\n" " <host_cpid>%s</host_cpid>\n" " <domain_name>%s</domain_name>\n" " <client_version>%d.%d.%d</client_version>\n" " <run_mode>%s</run_mode>\n", name.c_str(), password_hash.c_str(), gstate.host_info.host_cpid, gstate.host_info.domain_name, gstate.core_client_version.major, gstate.core_client_version.minor, gstate.core_client_version.release, run_mode_name[gstate.run_mode.get_perm()] ); if (strlen(gstate.acct_mgr_info.previous_host_cpid)) { fprintf(f, " <previous_host_cpid>%s</previous_host_cpid>\n", gstate.acct_mgr_info.previous_host_cpid ); } // If the AMS requested it, send GUI RPC port and password hash. // This is for the "farm" account manager so it // can know where to send GUI RPC requests to // without having to configure each host // if (gstate.acct_mgr_info.send_gui_rpc_info) { if (gstate.cmdline_gui_rpc_port) { fprintf(f," <gui_rpc_port>%d</gui_rpc_port>\n", gstate.cmdline_gui_rpc_port); } else { fprintf(f," <gui_rpc_port>%d</gui_rpc_port>\n", GUI_RPC_PORT); } if (boinc_file_exists(GUI_RPC_PASSWD_FILE)) { strcpy(password, ""); pwdf = fopen(GUI_RPC_PASSWD_FILE, "r"); if (pwdf) { if (fgets(password, 256, pwdf)) { strip_whitespace(password); } fclose(pwdf); } fprintf(f," <gui_rpc_password>%s</gui_rpc_password>\n", password); } } for (i=0; i<gstate.projects.size(); i++) { PROJECT* p = gstate.projects[i]; fprintf(f, " <project>\n" " <url>%s</url>\n" " <project_name>%s</project_name>\n" " <suspended_via_gui>%d</suspended_via_gui>\n" " <account_key>%s</account_key>\n" " <hostid>%d</hostid>\n" "%s" " </project>\n", p->master_url, p->project_name, p->suspended_via_gui, p->authenticator, p->hostid, p->attached_via_acct_mgr?" <attached_via_acct_mgr/>\n":"" ); } if (boinc_file_exists(GLOBAL_PREFS_FILE_NAME)) { FILE* fprefs = fopen(GLOBAL_PREFS_FILE_NAME, "r"); if (fprefs) { copy_stream(fprefs, f); fclose(fprefs); } } if (strlen(gstate.acct_mgr_info.opaque)) { fprintf(f, " <opaque>\n%s\n" " </opaque>\n", gstate.acct_mgr_info.opaque ); } fprintf(f, "</acct_mgr_request>\n"); fclose(f); sprintf(buf, "%srpc.php", url); retval = gstate.gui_http.do_rpc_post( this, buf, ACCT_MGR_REQUEST_FILENAME, ACCT_MGR_REPLY_FILENAME ); if (retval) { error_num = retval; return retval; } msg_printf(NULL, MSG_INFO, "Contacting account manager at %s", url); return 0; }
/* Amiga ROM image */ static int cmd_amiga(FILE* infile, const char* infilename, FILE* outfile, const char* outfilename) { int i; size_t nwrite; size_t source_size; size_t target_size = 256 * 1024; struct amiga_rom_footer footer; size_t max_size = target_size - sizeof footer; size_t free_size; int ret; /* boolean return value: 0 == error, 1 == OK */ int err; /* Seek error */ unsigned long checksum; printf("# Padding %s to %ld kB Amiga ROM image into %s\n", infilename, ((long)target_size) / 1024, outfilename); /* Get the input file size */ source_size = get_file_size(infile, infilename); if (source_size == SIZE_ERROR) return 0; /* Check if the input file size is not too big */ if (source_size > max_size) { fprintf(stderr, "%s: %s is too big: %lu extra bytes\n", g_argv0, infilename, (unsigned long)(source_size - max_size)); return 0; } /* Copy the input file */ ret = copy_stream(infile, infilename, outfile, outfilename, source_size); if (!ret) return ret; /* Pad with zeroes */ free_size = max_size - source_size; ret = write_byte_block(outfile, outfilename, 0, free_size); if (!ret) return ret; /* Set up ROM footer */ /* The checksum fixup will be overwritten in a second pass */ write_big_endian_long(&footer.checksum_fixup, 0); write_big_endian_long(&footer.romsize, target_size); for (i = 0; i < 8; i++) write_big_endian_short(&footer.vectors[i], 0x18 + i); /* Write the footer with temporary checksum_fixup */ nwrite = fwrite(&footer, 1, sizeof footer, outfile); if (nwrite != sizeof footer) { fprintf(stderr, "%s: %s: %s\n", g_argv0, outfilename, strerror(errno)); return 0; } /* Compute the adequate checksum_fixup */ ret = compute_amiga_checksum(outfile, outfilename, target_size, &checksum); if (!ret) return ret; #if DBG_MKROM printf("# checksum before fixup = 0x%08lx\n", checksum); #endif write_big_endian_long(&footer.checksum_fixup, 0xffffffff - checksum); /* Seek to the footer location again */ err = fseek(outfile, max_size, SEEK_SET); if (err != 0) { fprintf(stderr, "%s: %s: %s\n", g_argv0, outfilename, strerror(errno)); return 0; } /* Write the footer a second time with the fixed checksum */ nwrite = fwrite(&footer, 1, sizeof footer, outfile); if (nwrite != sizeof footer) { fprintf(stderr, "%s: %s: %s\n", g_argv0, outfilename, strerror(errno)); return 0; } #if DBG_MKROM ret = compute_amiga_checksum(outfile, outfilename, target_size, &checksum); if (!ret) return ret; printf("# checksum after fixup = 0x%08lx\n", checksum); #endif printf("# %s done (%lu bytes free)\n", outfilename, (unsigned long)free_size); return 1; }