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); }
static int cmd_getattr (optparse_t *p, int ac, char *av[]) { flux_t h = NULL; const char *val; int n, flags; n = optparse_optind (p); if (n != ac - 1) optparse_fatal_usage (p, 1, NULL); h = builtin_get_flux_handle (p); if (!(val = flux_attr_get (h, av[n], &flags))) err_exit ("%s", av[n]); printf ("%s\n", val); flux_close (h); return (0); }
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 cmd_lsattr (optparse_t *p, int ac, char *av[]) { const char *name, *val; flux_t h; if (optparse_optind (p) != ac) optparse_fatal_usage (p, 1, NULL); h = builtin_get_flux_handle (p); name = flux_attr_first (h); while (name) { if (optparse_hasopt (p, "values")) { val = flux_attr_get (h, name, NULL); printf ("%-40s%s\n", name, val ? val : "-"); } else { printf ("%s\n", name); } name = flux_attr_next (h); } 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_hwloc_reload (optparse_t *p, int ac, char *av[]) { int n = optparse_optind (p); const char *default_nodeset = "all"; const char *nodeset = optparse_get_str (p, "rank", default_nodeset); char *dirpath = NULL; flux_t h; if (!(h = builtin_get_flux_handle (p))) err_exit ("flux_open"); if (av[n] && !(dirpath = realpath (av[n], NULL))) err_exit ("%s", av[n]); config_hwloc_paths (h, dirpath); request_hwloc_reload (h, nodeset); free (dirpath); flux_close (h); 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); }
static optparse_t * setup_optparse_parse_args (int argc, char *argv[]) { optparse_err_t e; struct optparse_option helpopt = { .name = "help", .key = 'h', .usage = "Display this message" }; optparse_t *p = optparse_create ("flux"); if (p == NULL) log_err_exit ("optparse_create"); optparse_set (p, OPTPARSE_USAGE, "[OPTIONS] COMMAND ARGS"); e = optparse_add_option_table (p, opts); if (e != OPTPARSE_SUCCESS) log_msg_exit ("optparse_add_option_table() failed"); // Remove automatic `--help' in favor of our own usage() from above e = optparse_remove_option (p, "help"); if (e != OPTPARSE_SUCCESS) log_msg_exit ("optparse_remove_option (\"help\")"); e = optparse_add_option (p, &helpopt); if (e != OPTPARSE_SUCCESS) log_msg_exit ("optparse_add_option (\"help\")"); // Don't print internal subcommands in --help (we print subcommands using // emit_command_help() above. e = optparse_set (p, OPTPARSE_PRINT_SUBCMDS, 0); if (e != OPTPARSE_SUCCESS) log_msg_exit ("optparse_set (OPTPARSE_PRINT_SUBCMDS"); register_builtin_subcommands (p); if (optparse_parse_args (p, argc, argv) < 0) exit (1); return (p); } int main (int argc, char *argv[]) { bool vopt = false; struct environment *env; optparse_t *p; const char *searchpath; const char *argv0 = argv[0]; int flags = 0; log_init ("flux"); p = setup_optparse_parse_args (argc, argv); if (!flux_is_installed ()) flags |= CONF_FLAG_INTREE; optparse_set_data (p, "conf_flags", &flags); if (optparse_hasopt (p, "help")) { usage (p); // N.B. accesses "conf_flags" exit (0); } optind = optparse_optind (p); if (argc - optind == 0) { usage (p); exit (1); } vopt = optparse_hasopt (p, "verbose"); /* prepare the environment 'env' that will be passed to subcommands. */ env = environment_create (); /* Add PATH to env and prepend path to this executable if necessary. */ setup_path (env, argv0); /* Prepend config values to env values. * Note special handling of lua ;; (default path). */ environment_from_env (env, "LUA_CPATH", "", ';'); environment_no_dedup_push_back (env, "LUA_CPATH", ";;"); environment_push (env, "LUA_CPATH", flux_conf_get ("lua_cpath_add", flags)); environment_push (env, "LUA_CPATH", getenv ("FLUX_LUA_CPATH_PREPEND")); environment_from_env (env, "LUA_PATH", "", ';'); environment_no_dedup_push_back (env, "LUA_PATH", ";;"); environment_push (env, "LUA_PATH", flux_conf_get ("lua_path_add", flags)); environment_push (env, "LUA_PATH", getenv ("FLUX_LUA_PATH_PREPEND")); environment_from_env (env, "PYTHONPATH", "", ':'); environment_push (env, "PYTHONPATH", flux_conf_get ("python_path", flags)); environment_push (env, "PYTHONPATH", getenv ("FLUX_PYTHONPATH_PREPEND")); environment_from_env (env, "MANPATH", "", ':'); environment_push (env, "MANPATH", flux_conf_get ("man_path", flags)); environment_from_env (env, "FLUX_EXEC_PATH", "", ':'); environment_push (env, "FLUX_EXEC_PATH", flux_conf_get ("exec_path", flags)); environment_push (env, "FLUX_EXEC_PATH", getenv ("FLUX_EXEC_PATH_PREPEND")); environment_from_env (env, "FLUX_CONNECTOR_PATH", "", ':'); environment_push (env, "FLUX_CONNECTOR_PATH", flux_conf_get ("connector_path", flags)); environment_push (env, "FLUX_CONNECTOR_PATH", getenv ("FLUX_CONNECTOR_PATH_PREPEND")); environment_from_env (env, "FLUX_MODULE_PATH", "", ':'); environment_push (env, "FLUX_MODULE_PATH", flux_conf_get ("module_path", flags)); environment_push (env, "FLUX_MODULE_PATH", getenv ("FLUX_MODULE_PATH_PREPEND")); /* Set FLUX_SEC_DIRECTORY, possibly to $HOME/.flux. */ setup_keydir (env, flags); if (getenv ("FLUX_URI")) environment_from_env (env, "FLUX_URI", "", 0); /* pass-thru */ environment_from_env (env, "FLUX_RC1_PATH", flux_conf_get ("rc1_path", flags), 0); environment_from_env (env, "FLUX_RC3_PATH", flux_conf_get ("rc3_path", flags), 0); environment_from_env (env, "FLUX_PMI_LIBRARY_PATH", flux_conf_get ("pmi_library_path", flags), 0); environment_from_env (env, "FLUX_WRECK_LUA_PATTERN", flux_conf_get ("wreck_lua_pattern", flags), 0); environment_from_env (env, "FLUX_WREXECD_PATH", flux_conf_get ("wrexecd_path", flags), 0); environment_apply(env); optparse_set_data (p, "env", env); if (vopt) print_environment (env); if (optparse_get_subcommand (p, argv [optind])) { if (optparse_run_subcommand (p, argc, argv) < 0) exit (1); } else { searchpath = environment_get (env, "FLUX_EXEC_PATH"); if (vopt) printf ("sub-command search path: %s\n", searchpath); exec_subcommand (searchpath, vopt, argv + optind); } environment_destroy (env); optparse_destroy (p); log_fini (); return 0; }