static int internal_content_load (optparse_t *p, int ac, char *av[]) { int n; const char *blobref; uint8_t *data; int size; flux_t *h; flux_rpc_t *rpc; const char *topic; n = optparse_optind (p); if (n != ac - 1) { optparse_print_usage (p); exit (1); } blobref = av[n]; if (!(h = builtin_get_flux_handle (p))) log_err_exit ("flux_open"); if (optparse_hasopt (p, "bypass-cache")) topic = "content-backing.load"; else topic = "content.load"; if (!(rpc = flux_rpc_raw (h, topic, blobref, strlen (blobref) + 1, 0, 0))) log_err_exit ("%s", topic); if (flux_rpc_get_raw (rpc, &data, &size) < 0) log_err_exit ("%s", topic); if (write_all (STDOUT_FILENO, data, size) < 0) log_err_exit ("write"); flux_rpc_destroy (rpc); flux_close (h); return (0); }
static int cmd_setattr (optparse_t *p, int ac, char *av[]) { int n; const char *name = NULL, *val = NULL; flux_t h; log_init ("flux-setattr"); n = optparse_optind (p); if (optparse_hasopt (p, "expunge") && n == ac - 1) { name = av[n]; } else if (!optparse_hasopt (p, "expunge") && n == ac - 2) { name = av[n]; val = av[n + 1]; } else { optparse_print_usage (p); exit (1); } h = builtin_get_flux_handle (p); if (flux_attr_set (h, name, val) < 0) log_err_exit ("%s", av[1]); flux_close (h); return (0); }
static int internal_content_store (optparse_t *p, int ac, char *av[]) { const uint32_t blob_size_limit = 1048576; /* RFC 10 */ uint8_t *data; int size; flux_t *h; flux_rpc_t *rpc; const char *topic; if (optparse_optind (p) != ac) { optparse_print_usage (p); exit (1); } if ((size = read_all (STDIN_FILENO, &data)) < 0) log_err_exit ("read"); if (!(h = builtin_get_flux_handle (p))) log_err_exit ("flux_open"); if (optparse_hasopt (p, "dry-run")) { int flags; const char *hashfun; if (size > blob_size_limit) log_errn_exit (EFBIG, "content-store"); if (!(hashfun = flux_attr_get (h, "content-hash", &flags))) log_err_exit ("flux_attr_get content-hash"); if (!strcmp (hashfun, "sha1")) { uint8_t hash[SHA1_DIGEST_SIZE]; char hashstr[SHA1_STRING_SIZE]; SHA1_CTX sha1_ctx; SHA1_Init (&sha1_ctx); SHA1_Update (&sha1_ctx, (uint8_t *)data, size); SHA1_Final (&sha1_ctx, hash); sha1_hashtostr (hash, hashstr); printf ("%s\n", hashstr); } else log_msg_exit ("content-store: unsupported hash function: %s", hashfun); } else { const char *blobref; int blobref_size; if (optparse_hasopt (p, "bypass-cache")) topic = "content-backing.store"; else topic = "content.store"; if (!(rpc = flux_rpc_raw (h, topic, data, size, 0, 0))) log_err_exit ("%s", topic); if (flux_rpc_get_raw (rpc, &blobref, &blobref_size) < 0) log_err_exit ("%s", topic); if (!blobref || blobref[blobref_size - 1] != '\0') log_msg_exit ("%s: protocol error", topic); printf ("%s\n", blobref); flux_rpc_destroy (rpc); } flux_close (h); free (data); return (0); }
void usage (optparse_t *p) { char *help_pattern; const char *val = getenv ("FLUX_CMDHELP_PATTERN"); const char *def = default_cmdhelp_pattern (p); if (asprintf (&help_pattern, "%s%s%s", def ? def : "", val ? ":" : "", val ? val : "") < 0) log_err_exit ("faled to get command help list!"); optparse_print_usage (p); fprintf (stderr, "\n"); emit_command_help (help_pattern, stderr); }
static int internal_heaptrace_stop (optparse_t *p, int ac, char *av[]) { flux_t *h; flux_rpc_t *rpc; if (optparse_optind (p) != ac) { optparse_print_usage (p); exit (1); } if (!(h = builtin_get_flux_handle (p))) log_err_exit ("flux_open"); if (!(rpc = flux_rpc (h, "cmb.heaptrace.stop", NULL, FLUX_NODEID_ANY, 0)) || flux_rpc_get (rpc, NULL) < 0) log_err_exit ("cmb.heaptrace.stop"); flux_rpc_destroy (rpc); flux_close (h); return (0); }
static int internal_content_dropcache (optparse_t *p, int ac, char *av[]) { flux_t *h; flux_rpc_t *rpc = NULL; const char *topic = "content.dropcache"; if (optparse_optind (p) != ac) { optparse_print_usage (p); exit (1); } if (!(h = builtin_get_flux_handle (p))) log_err_exit ("flux_open"); if (!(rpc = flux_rpc (h, topic, NULL, FLUX_NODEID_ANY, 0))) log_err_exit ("%s", topic); if (flux_rpc_get (rpc, NULL) < 0) log_err_exit ("%s", topic); flux_rpc_destroy (rpc); flux_close (h); return (0); }
static int internal_content_spam (optparse_t *p, int ac, char *av[]) { int i, count; flux_rpc_t *rpc; flux_t *h; flux_reactor_t *r; char data[256]; int size = 256; if (ac != 2 && ac != 3) { optparse_print_usage (p); exit (1); } count = strtoul (av[1], NULL, 10); if (ac == 3) spam_max_inflight = strtoul (av[2], NULL, 10); else spam_max_inflight = 1; if (!(h = builtin_get_flux_handle (p))) log_err_exit ("flux_open"); if (!(r = flux_get_reactor (h))) log_err_exit ("flux_get_reactor"); spam_cur_inflight = 0; i = 0; while (i < count || spam_cur_inflight > 0) { while (i < count && spam_cur_inflight < spam_max_inflight) { snprintf (data, size, "spam-o-matic pid=%d seq=%d", getpid(), i); if (!(rpc = flux_rpc_raw (h, "content.store", data, size, 0, 0))) log_err_exit ("content.store(%d)", i); if (flux_rpc_then (rpc, store_completion, r) < 0) log_err_exit ("flux_rpc_then(%d)", i); spam_cur_inflight++; i++; } if (flux_reactor_run (r, 0) < 0) log_err ("flux_reactor_run"); } return (0); }
static int internal_heaptrace_dump (optparse_t *p, int ac, char *av[]) { flux_t *h; flux_rpc_t *rpc; json_object *in = Jnew (); if (optparse_optind (p) != ac - 1) { optparse_print_usage (p); exit (1); } Jadd_str (in, "reason", av[ac - 1]); if (!(h = builtin_get_flux_handle (p))) log_err_exit ("flux_open"); if (!(rpc = flux_rpc (h, "cmb.heaptrace.dump", Jtostr (in), FLUX_NODEID_ANY, 0)) || flux_rpc_get (rpc, NULL) < 0) log_err_exit ("cmb.heaptrace.dump"); flux_rpc_destroy (rpc); flux_close (h); return (0); }
int mod_main (flux_t h, int argc, char **argv) { optparse_t *p = optparse_create ("pymod"); if (optparse_add_option_table (p, opts) != OPTPARSE_SUCCESS) msg_exit ("optparse_add_option_table"); if (optparse_set (p, OPTPARSE_USAGE, usage_msg) != OPTPARSE_SUCCESS) msg_exit ("optparse_set usage"); int option_index = optparse_parse_args (p, argc, argv); if (option_index <= 0 || optparse_hasopt(p, "help") || option_index >= argc){ optparse_print_usage(p); return (option_index < 0); } const char * module_name = argv[option_index]; Py_SetProgramName("pymod"); Py_Initialize(); PyObject *search_path = PySys_GetObject("path"); // Add installation search paths add_if_not_present(search_path, optparse_get_str(p, "path", "")); add_if_not_present(search_path, FLUX_PYTHON_PATH); PySys_SetObject("path", search_path); if(optparse_hasopt(p, "verbose")){ PyObject_Print(search_path, stderr, 0); } flux_log(h, LOG_INFO, "loading python module named: %s", module_name); PyObject *module = PyImport_ImportModule("flux.core"); if(!module){ PyErr_Print(); return EINVAL; } PyObject *mod_main = PyObject_GetAttrString(module, "mod_main_trampoline"); if(mod_main && PyCallable_Check(mod_main)){ //maybe unpack args directly? probably easier to use a dict PyObject *py_args = PyTuple_New(3); PyTuple_SetItem(py_args, 0, PyString_FromString(module_name)); PyTuple_SetItem(py_args, 1, PyLong_FromVoidPtr(h)); //Convert zhash to native python dict, should preserve mods //through switch to argc-style arguments PyObject *arg_list = PyList_New(0); char ** it = argv + option_index; int i; for (i=0; *it; i++, it++){ PyList_Append(arg_list, PyString_FromString(*it)); } PyTuple_SetItem(py_args, 2, arg_list); // Call into trampoline PyObject_CallObject(mod_main, py_args); if(PyErr_Occurred()){ PyErr_Print(); } Py_DECREF(py_args); Py_DECREF(arg_list); } Py_Finalize(); return 0; }