int process_test_str(struct config *config, char *value, int value_sz, char *keyprefix) { int r; char c_file[1024]; char elf_file[1024]; snprintf(c_file, sizeof(c_file), "%s/plugin-%s.c", config->tmpdir, keyprefix); snprintf(elf_file, sizeof(elf_file), "%s/plugin-%s.elf", config->tmpdir, keyprefix); if(0 != write_file(c_file, value, value_sz)) { log_perror("write()"); return(-1); } char warn_buf[1024]; int warn_buf_sz = 0; if(0 != process_compile(config, c_file, elf_file, warn_buf, sizeof(warn_buf), &warn_buf_sz)) { warn_buf[MAX(1, warn_buf_sz)-1] = '\0'; log_error("compilation failed:\n%s", warn_buf); return(-1); } struct process *process = process_new(NULL, keyprefix, strlen(keyprefix)); log_warn("#%p: loaded code from str", process); r = process_load(process, elf_file); if(0 != r) { process_free(process); log_error("unable to load process:\n%s", warn_buf); return(-1); } r = process_run(NULL, process); process_free(process); if(r <= 0) { log_error("error while running process"); return(-1); } // ok! return(r); }
/* Request: MUST NOT have extras. MUST have key. MUST have value. */ ST_RES *cmd_code_load(CONN *conn, ST_REQ *req, ST_RES *res) { if(req->extras_sz || !req->key_sz || !req->value_sz) return(set_error_code(res, MEMCACHE_STATUS_INVALID_ARGUMENTS, NULL)); if(CONFIG(conn)->vx32_disabled) { return(set_error_code(res, MEMCACHE_STATUS_UNKNOWN_COMMAND, "Command disabled by configuration")); } if(NULL != find_process(req->key, req->key_sz)) return(set_error_code(res, MEMCACHE_STATUS_KEY_EXISTS, NULL)); int r; char c_file[1024]; char elf_file[1024]; char keyprefix[32]; key_escape(keyprefix, sizeof(keyprefix), req->key, req->key_sz); keyprefix[MIN(req->key_sz, sizeof(keyprefix)-1)] = '\0'; // make it reasonably short snprintf(c_file, sizeof(c_file), "%s/plugin-%s.c", CONFIG(conn)->tmpdir, keyprefix); snprintf(elf_file, sizeof(elf_file), "%s/plugin-%s.elf", CONFIG(conn)->tmpdir, keyprefix); res->value = res->buf; r = write_file(c_file, req->value, req->value_sz); if(0 != r) { // never res->status = MEMCACHE_STATUS_ITEM_NOT_STORED; res->value_sz += snprintf(&res->value[res->value_sz], res->buf_sz - res->value_sz, "Error while saving file: %s", strerror(errno)); return(res); } if(0 != process_compile(CONFIG(conn), c_file, elf_file, res->value, res->buf_sz, (int*)&res->value_sz)) { res->status = MEMCACHE_STATUS_ITEM_NOT_STORED; return(res); } res->value_sz = MIN(res->value_sz, 4096); // no more than 4k logs res->value[res->value_sz++] = '\n'; res->value[res->value_sz++] = '\n'; struct process *process = process_new(conn, req->key, req->key_sz); log_warn("#%p: loaded code", process); r = process_load(process, elf_file); if(0 != r) { // never process_free(process); res->value_sz += snprintf(&res->value[res->value_sz], res->buf_sz - res->value_sz, "Error while loading the binary"); res->status = MEMCACHE_STATUS_ITEM_NOT_STORED; return(res); } if(0 != process_run(conn, process)) { process_free(process); res->value_sz += snprintf(&res->value[res->value_sz], res->buf_sz - res->value_sz, "Error while running the binary"); res->status = MEMCACHE_STATUS_ITEM_NOT_STORED; return(res); } res->status = MEMCACHE_STATUS_OK; return res; }
int quick_load() { int ok = 0; quick_fp = fopen(quick_file, "rb"); if (quick_fp != NULL) { // check quicksave version is compatible process_load(quick_control, COUNT(quick_control)); if (strcmp(quick_control, quick_version) != 0) { fclose(quick_fp); quick_fp = NULL; return 0; } stop_sounds(); start_timer(timer_0, 5); // briefly display a black screen as a visual cue draw_rect(&screen_rect, 0); screen_updates_suspended = 0; request_screen_update(); screen_updates_suspended = 1; word old_rem_min = rem_min; word old_rem_tick = rem_tick; ok = quick_process(process_load); fclose(quick_fp); quick_fp = NULL; restore_room_after_quick_load(); do_wait(timer_0); screen_updates_suspended = 0; request_screen_update(); #ifdef USE_QUICKLOAD_PENALTY // Subtract one minute from the remaining time (if it is above 5 minutes) if (options.enable_quicksave_penalty) { int ticks_elapsed = 720 * (rem_min - old_rem_min) + (rem_tick - old_rem_tick); // don't restore time at all if the elapsed time is between 0 and 1 minutes if (ticks_elapsed > 0 && ticks_elapsed < 720) { rem_min = old_rem_min; rem_tick = old_rem_tick; } else { if (rem_min == 6) rem_tick = 719; // crop to "5 minutes" exactly, if hitting the threshold in <1 minute if (rem_min > 5) --rem_min; } } #endif } return ok; }
static void initqueue() { long i,repeats; for (i=0; i<QUEUESIZE; i++) queuetype[i]=i%PROGRAMS; // for (i=0; i<QUEUESIZE; i++) queuetype[i]=lrand48()%PROGRAMS; for (repeats=0; repeats<10; repeats++) for (i=0; i<QUEUESIZE; i++) { int j=lrand48()%QUEUESIZE; long temp=queuetype[i]; queuetype[i]=queuetype[j]; queuetype[j]=temp; } for (i=0; i<QUEUESIZE; i++) { process_clear(queue+i); process_load(queue+i,programs+queuetype[i], i, queuetype[i]); } queueend=0; }
/* * This is the entry point for loading (registering) a process with omnius. */ int omnius_load(blob_t *blob) { int ret = EXIT_FAILURE; if (!g_pid_lookup[blob->head.pid]) { /* create a proc based on pid */ secmem_process_t *proc = (secmem_process_t *) calloc(1, sizeof(secmem_process_t)); if (proc) { if ((ret = process_load(blob, proc)) == EXIT_SUCCESS) { /* If we are successful, add the process object to a global lookup table for future reference, otherwise free mem */ g_pid_lookup[blob->head.pid] = proc; } else { free(proc); } } } return ret; }