static void ocl_call_context_fin(SEXP context) { ocl_call_context_t *ctx = (ocl_call_context_t*) R_ExternalPtrAddr(context); if (ctx) { /* if this was an asynchronous call, we must wait for it to finish */ if (!ctx->finished) clFinish(ctx->commands); if (ctx->event) clReleaseEvent(ctx->event); if (ctx->output) clReleaseMemObject(ctx->output); if (ctx->float_args) arg_free(ctx->float_args, 0); if (ctx->float_out) free(ctx->float_out); if (ctx->mem_objects) arg_free(ctx->mem_objects, (afin_t) clReleaseMemObject); if (ctx->commands) clReleaseCommandQueue(ctx->commands); free(ctx); CAR(context) = 0; /* this allows us to call the finalizer manually */ } }
/** * Release the argtable data. */ /* private */void ArgTable::freeArgtable() { if (m_argtable != nullptr) { arg_free(m_argtable); m_argtable = nullptr; m_argEnd = nullptr; } } // ArgTable#freeArgtable
/*-----------------------------------------------------------------------------------*/ LOADER_INIT_FUNC(config_init, arg) { arg_free(arg); config_load(); config_apply(); LOADER_UNLOAD(); }
/*-----------------------------------------------------------------------------------*/ LOADER_INIT_FUNC(configedit_init, arg) { arg_free(arg); if(id == EK_ID_NONE) { id = ek_start(&p); } }
/*-----------------------------------------------------------------------------------*/ LOADER_INIT_FUNC(webserver_init, arg) { arg_free(arg); if(id == EK_ID_NONE) { id = ek_start(&p); } }
/*-----------------------------------------------------------------------------------*/ LOADER_INIT_FUNC(ssfire_init, arg) { arg_free(arg); if(id == EK_ID_NONE) { id = ek_start(&p); } }
static void arg_free(struct arg_chain *chain, afin_t fin) { int i, n = chain->args; if (chain->next) arg_free(chain->next, fin); for (i = 0; i < n; i++) if (fin) fin(chain->arg[i]); else free(chain->arg[i]); free(chain); }
/*-----------------------------------------------------------------------------------*/ LOADER_INIT_FUNC(webserver_init, arg) { arg_free(arg); if(id == EK_ID_NONE) { id = ek_start(&p); } else { ctk_window_open(&mainwindow); } }
/*-----------------------------------------------------------------------------------*/ LOADER_INIT_FUNC(themeloader_init, arg) { arg_free(arg); memcpy(ctk_hires_theme_ptr, &ctk_hires_theme, sizeof(struct ctk_hires_theme)); ctk_draw_init(); LOADER_UNLOAD(); }
/*-----------------------------------------------------------------------------------*/ LOADER_INIT_FUNC(about_init, arg) { arg_free(arg); if(id == EK_ID_NONE) { /* id = dispatcher_start(&p); */ id = ek_start(&p); } /* ctk_desktop_redraw(aboutdialog.desktop);*/ }
/*-----------------------------------------------------------------------------------*/ LOADER_INIT_FUNC(ctk_termserial_init, arg) { arg_free(arg); SIO_INIT; termstate = ctk_term_alloc_state(); if (termstate == NULL) return; if(id == EK_ID_NONE) { id = ek_start(&p); } }
/*-----------------------------------------------------------------------------------*/ LOADER_INIT_FUNC(directory_init, arg) { arg_free(arg); if(id == EK_ID_NONE) { id = ek_start(&p); width = ctk_draw_width() - 2; height = ctk_draw_height() - 3; } }
void free_crack_4test(ophcrack_t *crack) { for (list_nd_t *nd = crack->hashes->head; nd != NULL; nd = nd->next) { hash_t *hash = nd->data; hash_free(hash); } for (list_nd_t *nd = crack->tables->head; nd != NULL; nd = nd->next) { table_t *tbl = nd->data; table_free(tbl); } arg_free(crack->arg); ophcrack_free(crack); }
LOADER_INIT_FUNC(warmboot_init, arg) { arg_free(arg); asm("jsr $fda3"); asm("jsr $fd15"); asm("jsr $ff5b"); asm("ldx #$f8"); asm("txs"); asm("cli"); asm("jmp 2061"); }
static void main_loop () { log_write ("openvassd %s started\n", OPENVASSD_VERSION); proctitle_set ("openvassd: Waiting for incoming connections"); for (;;) { int soc; int family; unsigned int lg_address; struct sockaddr_in6 address6; struct sockaddr_in6 *p_addr; struct arglist *globals; struct addrinfo *ai; check_and_reload (); wait_for_children1 (); ai = arg_get_value (g_options, "addr"); lg_address = sizeof (struct sockaddr_in6); soc = accept (global_iana_socket, (struct sockaddr *) (&address6), &lg_address); if (soc == -1) continue; /* * MA: you cannot share an open SSL connection through fork/multithread * The SSL connection shall be open _after_ the fork */ globals = emalloc (sizeof (struct arglist)); arg_add_value (globals, "global_socket", ARG_INT, -1, GSIZE_TO_POINTER (soc)); arg_add_value (globals, "plugins", ARG_ARGLIST, -1, global_plugins); arg_add_value (globals, "preferences", ARG_ARGLIST, -1, global_preferences); p_addr = emalloc (sizeof (struct sockaddr_in6)); family = ai->ai_family; memcpy (p_addr, &address6, sizeof (address6)); arg_add_value (globals, "client_address", ARG_PTR, -1, p_addr); arg_add_value (globals, "family", ARG_INT, -1, GSIZE_TO_POINTER (family)); /* we do not want to create an io thread, yet so the last argument is -1 */ if (create_process ((process_func_t) scanner_thread, globals) < 0) { log_write ("Could not fork - client won't be served"); sleep (2); } close (soc); arg_free (globals); } }
void traditional_reset (void) { int i; #ifdef CFUNCTIONS_DEBUG if (trad_debug) printf ("traditional reset\n"); #endif for (i = 0; i <= n_arg; i++) { if (args[i]) arg_free (args[i]); args[i] = 0; } n_arg = -1; }
static void set_globals_from_preferences (struct arglist *prefs) { char *str; if ((str = arg_get_value (prefs, "max_hosts")) != NULL) { global_max_hosts = atoi (str); if (global_max_hosts <= 0) global_max_hosts = 15; } if ((str = arg_get_value (prefs, "max_checks")) != NULL) { global_max_checks = atoi (str); if (global_max_checks <= 0) global_max_checks = 10; } arg_free (global_preferences); global_preferences = prefs; }
/*---------------------------------------------------------------------------*/ EK_PROCESS_INIT(cfs_init_init, arg) { arg_free(arg); ek_service_start(CFS_SERVICE_NAME, &proc); }
/*---------------------------------------------------------------------------*/ LOADER_INIT_FUNC(tfe_dump_drv_init, arg) { arg_free(arg); ek_service_start(PACKET_SERVICE_NAME, &proc); }
/*---------------------------------------------------------------------------*/ LOADER_INIT_FUNC(tapdev_service_init, arg) { arg_free(arg); ek_service_start(PACKET_SERVICE_NAME, &proc); }
/** * @brief Attack one host. */ static void attack_host (struct arglist *globals, struct arglist *hostinfos, char *hostname, plugins_scheduler_t sched) { /* Used for the status */ int num_plugs = 0; int cur_plug = 1; kb_t kb; gboolean new_kb = FALSE; int forks_retry = 0; struct arglist *plugins = arg_get_value (globals, "plugins"); struct arglist *tmp; proctitle_set ("openvassd: testing %s", arg_get_value (hostinfos, "NAME")); kb = init_host_kb (globals, hostname, hostinfos, &new_kb); num_plugs = get_active_plugins_number (plugins); tmp = emalloc (sizeof (struct arglist)); arg_add_value (tmp, "HOSTNAME", ARG_ARGLIST, -1, hostinfos); /* launch the plugins */ pluginlaunch_init (globals); for (;;) { struct scheduler_plugin *plugin; pid_t parent; /* Check that our father is still alive */ parent = getppid (); if (parent <= 1 || process_alive (parent) == 0) { pluginlaunch_stop (); return; } /* Idle if the scan has been paused. */ if (pause_whole_test) { /* Let the running NVTs complete. */ pluginlaunch_wait (); /* Send the PAUSE status to the client. */ if (comm_send_status (globals, hostname, "pause", cur_plug, num_plugs) < 0) { pluginlaunch_stop (); goto host_died; } /* Wait for resume. */ while (pause_whole_test) sleep (1); /* Send the RESUME status to the client. */ if (comm_send_status (globals, hostname, "resume", cur_plug, num_plugs) < 0) { pluginlaunch_stop (); goto host_died; } } plugin = plugins_scheduler_next (sched); if (plugin != NULL && plugin != PLUG_RUNNING) { int e; again: e = launch_plugin (globals, sched, plugin, hostname, &cur_plug, num_plugs, hostinfos, kb, new_kb); if (e < 0) { /* * Remote host died */ if (e == ERR_HOST_DEAD) goto host_died; else if (e == ERR_CANT_FORK) { if (forks_retry < MAX_FORK_RETRIES) { forks_retry++; log_write ("fork() failed - sleeping %d seconds (%s)", forks_retry, strerror (errno)); fork_sleep (forks_retry); goto again; } else { log_write ("fork() failed too many times - aborting"); goto host_died; } } } } else if (plugin == NULL) break; else pluginlaunch_wait_for_free_process (); } pluginlaunch_wait (); host_died: comm_send_status (globals, hostname, "attack", num_plugs, num_plugs); arg_free (tmp); pluginlaunch_stop (); plugins_scheduler_free (sched); gchar *network_scan_status = arg_get_value (globals, "network_scan_status"); if (network_scan_status != NULL) { if (g_ascii_strcasecmp (network_scan_status, "busy") == 0) { save_kb_close (globals, "network"); } } else if (new_kb == TRUE) save_kb_close (globals, hostname); }
/*--------------------------------------------------------------------------*/ LOADER_INIT_FUNC(mtest_init, arg) { arg_free(arg); mtp_start(&t, test, NULL); }
/*--- generic blockwise argument list that can be freed in one go ---*/ typedef void (*afin_t)(void*); struct arg_chain { struct arg_chain *next; afin_t fin; int args, size; void *arg[1]; }; static struct arg_chain *arg_alloc(struct arg_chain *parent, int size) { struct arg_chain *c = (struct arg_chain*) malloc(sizeof(*c) + sizeof(void*) * size); if (!c) Rf_error("unable to allocate argument chain"); c->next = 0; c->size = size; c->args = 0; c->fin = 0; if (parent) parent->next = c; return c; } static struct arg_chain *arg_add(struct arg_chain *where, void *arg) { if (!where) where = arg_alloc(0, 32); if (where->args >= where->size) { while (where->next) where = where->next; where = where->next = arg_alloc(where, 32); } where->arg[where->args++] = arg; return where; } static void arg_free(struct arg_chain *chain, afin_t fin) { int i, n = chain->args; if (chain->next) arg_free(chain->next, fin); for (i = 0; i < n; i++) if (fin) fin(chain->arg[i]); else free(chain->arg[i]); free(chain); } #if 0 /* unused - we use it as part of the call context instead */ static void free_protected_args(SEXP o) { arg_free((struct arg_chain*)R_ExternalPtrAddr(o), 0); }
/* .External */ SEXP ocl_call(SEXP args) { struct arg_chain *float_args = 0; ocl_call_context_t *occ; int on, an = 0, ftype = FT_DOUBLE, ftsize, ftres, async; SEXP ker = CADR(args), olen, arg, res, octx, dimVec; cl_kernel kernel = getKernel(ker); cl_context context; cl_command_queue commands; cl_device_id device_id = getDeviceID(getAttrib(ker, Rf_install("device"))); cl_mem output; cl_int err; size_t wdims[3] = {0, 0, 0}; int wdim = 1; if (clGetKernelInfo(kernel, CL_KERNEL_CONTEXT, sizeof(context), &context, NULL) != CL_SUCCESS || !context) Rf_error("cannot obtain kernel context via clGetKernelInfo"); args = CDDR(args); res = Rf_getAttrib(ker, install("precision")); if (TYPEOF(res) == STRSXP && LENGTH(res) == 1 && CHAR(STRING_ELT(res, 0))[0] != 'd') ftype = FT_SINGLE; ftsize = (ftype == FT_DOUBLE) ? sizeof(double) : sizeof(float); olen = CAR(args); /* size */ args = CDR(args); on = Rf_asInteger(olen); if (on < 0) Rf_error("invalid output length"); ftres = (Rf_asInteger(CAR(args)) == 1) ? 1 : 0; /* native.result */ if (ftype != FT_SINGLE) ftres = 0; args = CDR(args); async = (Rf_asInteger(CAR(args)) == 1) ? 0 : 1; /* wait */ args = CDR(args); dimVec = coerceVector(CAR(args), INTSXP); /* dim */ wdim = LENGTH(dimVec); if (wdim > 3) Rf_error("OpenCL standard only supports up to three work item dimensions - use index vectors for higher dimensions"); if (wdim) { int i; /* we don't use memcpy in case int and size_t are different */ for (i = 0; i < wdim; i++) wdims[i] = INTEGER(dimVec)[i]; } if (wdim < 1 || wdims[0] < 1 || (wdim > 1 && wdims[1] < 1) || (wdim > 2 && wdims[2] < 1)) Rf_error("invalid dimensions - muse be a numeric vector with positive values"); args = CDR(args); occ = (ocl_call_context_t*) calloc(1, sizeof(ocl_call_context_t)); if (!occ) Rf_error("unable to allocate ocl_call context"); octx = PROTECT(R_MakeExternalPtr(occ, R_NilValue, R_NilValue)); R_RegisterCFinalizerEx(octx, ocl_call_context_fin, TRUE); occ->output = output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, ftsize * on, NULL, &err); if (!output) Rf_error("failed to create output buffer of %d elements via clCreateBuffer (%d)", on, err); if (clSetKernelArg(kernel, an++, sizeof(cl_mem), &output) != CL_SUCCESS) Rf_error("failed to set first kernel argument as output in clSetKernelArg"); if (clSetKernelArg(kernel, an++, sizeof(on), &on) != CL_SUCCESS) Rf_error("failed to set second kernel argument as output length in clSetKernelArg"); occ->commands = commands = clCreateCommandQueue(context, device_id, 0, &err); if (!commands) ocl_err("clCreateCommandQueue"); if (ftype == FT_SINGLE) /* need conversions, create floats buffer */ occ->float_args = float_args = arg_alloc(0, 32); while ((arg = CAR(args)) != R_NilValue) { int n, ndiv = 1; void *ptr; size_t al; switch (TYPEOF(arg)) { case REALSXP: if (ftype == FT_SINGLE) { int i; float *f; double *d = REAL(arg); n = LENGTH(arg); f = (float*) malloc(sizeof(float) * n); if (!f) Rf_error("unable to allocate temporary single-precision memory for conversion from a double-precision argument vector of length %d", n); for (i = 0; i < n; i++) f[i] = d[i]; ptr = f; al = sizeof(float); arg_add(float_args, ptr); } else { ptr = REAL(arg); al = sizeof(double); } break; case INTSXP: ptr = INTEGER(arg); al = sizeof(int); break; case LGLSXP: ptr = LOGICAL(arg); al = sizeof(int); break; case RAWSXP: if (inherits(arg, "clFloat")) { ptr = RAW(arg); ndiv = al = sizeof(float); break; } default: Rf_error("only numeric or logical kernel arguments are supported"); /* no-ops but needed to make the compiler happy */ ptr = 0; al = 0; } n = LENGTH(arg); if (ndiv != 1) n /= ndiv; if (n == 1) {/* scalar */ if (clSetKernelArg(kernel, an++, al, ptr) != CL_SUCCESS) Rf_error("Failed to set scalar kernel argument %d (size=%d)", an, al); } else { cl_mem input = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, al * n, ptr, &err); if (!input) Rf_error("Unable to create buffer (%d elements, %d bytes each) for vector argument %d (oclError %d)", n, al, an, err); if (!occ->mem_objects) occ->mem_objects = arg_alloc(0, 32); arg_add(occ->mem_objects, input); #if 0 /* we used this before CL_MEM_USE_HOST_PTR */ if (clEnqueueWriteBuffer(commands, input, CL_TRUE, 0, al * n, ptr, 0, NULL, NULL) != CL_SUCCESS) Rf_error("Failed to transfer data (%d elements) for vector argument %d", n, an); #endif if (clSetKernelArg(kernel, an++, sizeof(cl_mem), &input) != CL_SUCCESS) Rf_error("Failed to set vector kernel argument %d (size=%d, length=%d)", an, al, n); /* clReleaseMemObject(input); */ } args = CDR(args); } if (clEnqueueNDRangeKernel(commands, kernel, wdim, NULL, wdims, NULL, 0, NULL, async ? &occ->event : NULL) != CL_SUCCESS) Rf_error("Error during kernel execution"); if (async) { /* asynchronous call -> get out and return the context */ #if USE_OCL_COMPLETE_CALLBACK clSetEventCallback(occ->event, CL_COMPLETE, ocl_complete_callback, occ); #endif clFlush(commands); /* the specs don't guarantee execution unless clFlush is called */ occ->ftres = ftres; occ->ftype = ftype; occ->on = on; Rf_setAttrib(octx, R_ClassSymbol, mkString("clCallContext")); UNPROTECT(1); return octx; } clFinish(commands); occ->finished = 1; /* we can release input memory objects now */ if (occ->mem_objects) { arg_free(occ->mem_objects, (afin_t) clReleaseMemObject); occ->mem_objects = 0; } if (float_args) { arg_free(float_args, 0); float_args = occ->float_args = 0; } res = ftres ? Rf_allocVector(RAWSXP, on * sizeof(float)) : Rf_allocVector(REALSXP, on); if (ftype == FT_SINGLE) { if (ftres) { if ((err = clEnqueueReadBuffer( commands, output, CL_TRUE, 0, sizeof(float) * on, RAW(res), 0, NULL, NULL )) != CL_SUCCESS) Rf_error("Unable to transfer result vector (%d float elements, oclError %d)", on, err); PROTECT(res); Rf_setAttrib(res, R_ClassSymbol, mkString("clFloat")); UNPROTECT(1); } else { /* float - need a temporary buffer */ float *fr = (float*) malloc(sizeof(float) * on); double *r = REAL(res); int i; if (!fr) Rf_error("unable to allocate memory for temporary single-precision output buffer"); occ->float_out = fr; if ((err = clEnqueueReadBuffer( commands, output, CL_TRUE, 0, sizeof(float) * on, fr, 0, NULL, NULL )) != CL_SUCCESS) Rf_error("Unable to transfer result vector (%d float elements, oclError %d)", on, err); for (i = 0; i < on; i++) r[i] = fr[i]; } } else if ((err = clEnqueueReadBuffer( commands, output, CL_TRUE, 0, sizeof(double) * on, REAL(res), 0, NULL, NULL )) != CL_SUCCESS) Rf_error("Unable to transfer result vector (%d double elements, oclError %d)", on, err); ocl_call_context_fin(octx); UNPROTECT(1); return res; }
SEXP ocl_collect_call(SEXP octx, SEXP wait) { SEXP res = R_NilValue; ocl_call_context_t *occ; int on; cl_int err; if (!Rf_inherits(octx, "clCallContext")) Rf_error("Invalid call context"); occ = (ocl_call_context_t*) R_ExternalPtrAddr(octx); if (!occ || occ->finished) Rf_error("The call results have already been collected, they cannot be retrieved twice"); if (Rf_asInteger(wait) == 0 && occ->event) { cl_int status; if ((err = clGetEventInfo(occ->event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(status), &status, NULL)) != CL_SUCCESS) Rf_error("OpenCL error 0x%x while querying event object for the supplied context", (int) err); if (status < 0) Rf_error("Asynchronous call failed with error code 0x%x", (int) -status); if (status != CL_COMPLETE) return R_NilValue; } clFinish(occ->commands); occ->finished = 1; /* we can release input memory objects now */ if (occ->mem_objects) { arg_free(occ->mem_objects, (afin_t) clReleaseMemObject); occ->mem_objects = 0; } if (occ->float_args) { arg_free(occ->float_args, 0); occ->float_args = 0; } on = occ->on; res = occ->ftres ? Rf_allocVector(RAWSXP, on * sizeof(float)) : Rf_allocVector(REALSXP, on); if (occ->ftype == FT_SINGLE) { if (occ->ftres) { if ((err = clEnqueueReadBuffer( occ->commands, occ->output, CL_TRUE, 0, sizeof(float) * on, RAW(res), 0, NULL, NULL )) != CL_SUCCESS) Rf_error("Unable to transfer result vector (%d float elements, oclError %d)", on, err); PROTECT(res); Rf_setAttrib(res, R_ClassSymbol, mkString("clFloat")); UNPROTECT(1); } else { /* float - need a temporary buffer */ float *fr = (float*) malloc(sizeof(float) * on); double *r = REAL(res); int i; if (!fr) Rf_error("unable to allocate memory for temporary single-precision output buffer"); occ->float_out = fr; if ((err = clEnqueueReadBuffer( occ->commands, occ->output, CL_TRUE, 0, sizeof(float) * on, fr, 0, NULL, NULL )) != CL_SUCCESS) Rf_error("Unable to transfer result vector (%d float elements, oclError %d)", on, err); for (i = 0; i < on; i++) r[i] = fr[i]; } } else if ((err = clEnqueueReadBuffer( occ->commands, occ->output, CL_TRUE, 0, sizeof(double) * on, REAL(res), 0, NULL, NULL )) != CL_SUCCESS) Rf_error("Unable to transfer result vector (%d double elements, oclError %d)", on, err); ocl_call_context_fin(octx); return res; }