Example #1
0
int kvs_conf_save (flux_t h, flux_conf_t cf)
{
    flux_conf_itr_t itr = flux_conf_itr_create (cf);
    const char *key;
    int rc = -1;

    if (kvs_unlink (h, kvs_conf_root) < 0)
        goto done;
    if (kvs_commit (h) < 0)
        goto done;
    while ((key = flux_conf_next (itr))) {
        char *nkey = xasprintf ("%s.%s", kvs_conf_root, key);
        const char *val = flux_conf_get (cf, key);
        int n = kvs_put_string (h, nkey, val);
        free (nkey);
        if (n < 0)
            goto done;
    }       
    if (kvs_commit (h) < 0)
        goto done;
    rc = 0;
done:
    flux_conf_itr_destroy (itr);
    return rc;
}
Example #2
0
int main (int argc, char *argv[])
{
    flux_t *h;

    plan (NO_PLAN);

    (void)setenv ("FLUX_CONNECTOR_PATH",
                  flux_conf_get ("connector_path", CONF_FLAG_INTREE), 0);
    ok ((h = flux_open ("loop://", 0)) != NULL,
        "opened loop connector");
    if (!h)
        BAIL_OUT ("can't continue without loop handle");

    flux_attr_set_cacheonly (h, "rank", "0");
    flux_attr_set_cacheonly (h, "tbon.level", "0");
    flux_attr_set_cacheonly (h, "tbon.maxlevel", "0");

    test_nopolicy (h); // 6
    test_hwm (h); // 37
    test_timed(h); // 18

    flux_close (h);
    done_testing();
    return (0);
}
Example #3
0
void config_get (flux_conf_t cf, int ac, char **av)
{
    const char *val;
    if (ac != 1)
        msg_exit ("get accepts one argument");
    val = flux_conf_get (cf, av[0]);
    if (!val)
        err_exit ("%s", av[0]);
    printf ("%s\n", val);
}
Example #4
0
void config_dump (flux_conf_t cf, int ac, char **av)
{
    flux_conf_itr_t itr;
    const char *key, *val;
    if (ac > 0)
        msg_exit ("dump accepts no arguments");
    itr = flux_conf_itr_create (cf);
    while ((key = flux_conf_next (itr))) {
        if (!(val = flux_conf_get (cf, key)))
            err_exit ("%s", key);
        printf("%s=%s\n", key, val);
    }
    flux_conf_itr_destroy (itr);
}
Example #5
0
void test_helpers (void)
{
    char *name, *path;
    const char *modpath = flux_conf_get ("module_path", CONF_FLAG_INTREE);

    path = xasprintf ("%s/kvs/.libs/kvs.so", modpath);
    ok (access (path, F_OK) == 0,
        "built kvs module is located");
    name = flux_modname (path);
    ok ((name != NULL),
        "flux_modname on kvs should find a name");
    skip (name == NULL, 1,
        "skip next test because kvs.so name is NULL");
    like (name, "^kvs$",
        "flux_modname says kvs module is named kvs");
    end_skip;
    if (name)
        free (name);
    free (path);

    ok (!flux_modfind ("nowhere", "foo"),
        "flux_modfind fails with nonexistent directory");
    ok (!flux_modfind (".", "foo"),
        "flux_modfind fails in current directory");
    ok (!flux_modfind (modpath, "foo"),
        "flux_modfind fails to find unknown module in moduledir");

    path = xasprintf ("%s/kvs/.libs", modpath);
    name = flux_modfind (path, "kvs");
    ok ((name != NULL),
        "flux_modfind finds kvs in flat directory");
    if (name)
        free (name);
    free (path);

    name = flux_modfind (modpath, "kvs");
    ok ((name != NULL),
        "flux_modfind also finds kvs in moduledir");
    if (name)
        free (name);

    path = xasprintf ("foo:bar:xyz:%s:zzz", modpath);
    name = flux_modfind (path, "kvs");
    ok ((name != NULL),
        "flux_modfind also finds kvs in search path");
    if (name)
        free (name);
    free (path);
}
Example #6
0
void setup_keydir (struct environment *env, int flags)
{
    const char *dir = getenv ("FLUX_SEC_DIRECTORY");
    char *new_dir = NULL;

    if (!dir)
        dir = flux_conf_get ("keydir", flags);
    if (!dir) {
        struct passwd *pw = getpwuid (getuid ());
        if (!pw)
            log_msg_exit ("Who are you!?!");
        dir = new_dir = xasprintf ("%s/.flux", pw->pw_dir);
    }
    if (!dir)
        log_msg_exit ("Could not determine keydir");
    environment_set (env, "FLUX_SEC_DIRECTORY", dir, 0);
    if (new_dir)
        free (new_dir);
}
Example #7
0
int main (int argc, char *argv[])
{
    flux_msg_t *msg;
    flux_t *h;
    flux_reactor_t *reactor;

    plan (NO_PLAN);

    (void)setenv ("FLUX_CONNECTOR_PATH",
                  flux_conf_get ("connector_path", CONF_FLAG_INTREE), 0);
    ok ((h = flux_open ("loop://", FLUX_O_COPROC)) != NULL,
        "opened loop connector");
    if (!h)
        BAIL_OUT ("can't continue without loop handle");
    flux_fatal_set (h, fatal_err, NULL);
    ok ((reactor = flux_get_reactor(h)) != NULL,
       "obtained reactor");
    if (!h)
        BAIL_OUT ("can't continue without reactor");

    ok (flux_msg_handler_addvec (h, htab, NULL) == 0,
        "registered message handlers");
    /* test continues in rpctest_begin_cb() so that rpc calls
     * can sleep while we answer them
     */
    ok ((msg = flux_request_encode ("rpctest.begin", NULL)) != NULL,
        "encoded rpctest.begin request OK");
    ok (flux_send (h, msg, 0) == 0,
        "sent rpctest.begin request");
    ok (flux_reactor_run (reactor, 0) == 0,
        "reactor completed normally");
    flux_msg_destroy (msg);

    /* test _then:  Slightly tricky.
     * Send request.  We're not in a coproc ctx here in main(), so there
     * will be no response, therefore, check will be false.  Register
     * continuation, start reactor.  Response will be received, continuation
     * will be invoked. Continuation stops the reactor.
    */
    flux_rpc_t *r;
    ok ((r = flux_rpc (h, "rpctest.echo", "{}", FLUX_NODEID_ANY, 0)) != NULL,
        "flux_rpc with payload when payload is expected works");
    ok (flux_rpc_check (r) == false,
        "flux_rpc_check says get would block");
    /* reg/unreg _then a couple times for fun */
    ok (flux_rpc_then (r, NULL, 0) == 0,
        "flux_rpc_then with NULL cb works");
    ok (flux_rpc_then (r, then_cb, h) == 0,
        "flux_rpc_then works after NULL");
    ok (flux_rpc_then (r, NULL, 0) == 0,
        "flux_rpc_then with NULL cb after non-NULL works");
    ok (flux_rpc_then (r, then_cb, h) == 0,
        "flux_rpc_then works");
    /* enough of that */
    ok (flux_reactor_run (reactor, 0) == 0,
        "reactor completed normally");
    flux_rpc_destroy (r);

    /* Test a _then corner case:
     * If _check() is called before _then(), a message may have been cached
     * in the flux_rpc_t.  rpctest_thenbug_cb creates this condition.
     * Next, _then continuation is installed, but will reactor call it?
     * This will hang if rpc implementation doesn't return a cached message
     * back to the handle in _then().  Else, continuation will stop reactor.
     */
    ok ((thenbug_r = flux_rpc (h, "rpctest.echo", "{}",
        FLUX_NODEID_ANY, 0)) != NULL,
        "thenbug: sent echo request");
    do {
        if (!(msg = flux_request_encode ("rpctest.thenbug", NULL))
                  || flux_send (h, msg, 0) < 0
                  || flux_reactor_run (reactor, 0) < 0) {
            flux_msg_destroy (msg);
            break;
        }
        flux_msg_destroy (msg);
    } while (!flux_rpc_check (thenbug_r));
    ok (true,
        "thenbug: check says message ready");
    ok (flux_rpc_then (thenbug_r, then_cb, h) == 0,
        "thenbug: registered then - hangs on failure");
    ok (flux_reactor_run (reactor, 0) == 0,
        "reactor completed normally");
    flux_rpc_destroy (thenbug_r);

    flux_msg_handler_delvec (htab);
    flux_close (h);
    done_testing();
    return (0);
}
Example #8
0
int main (int argc, char **argv)
{
    flux_t h;
    heartbeat_t *hb;
    flux_msg_handler_t *w;

    plan (18);

    check_codec ();

    (void)setenv ("FLUX_CONNECTOR_PATH",
                  flux_conf_get ("connector_path", CONF_FLAG_INTREE), 0);
    ok ((h = flux_open ("loop://", 0)) != NULL,
        "opened loop connector");
    if (!h)
        BAIL_OUT ("can't continue without loop handle");
    flux_fatal_set (h, fatal_err, NULL);

    ok ((hb = heartbeat_create ()) != NULL,
        "heartbeat_create works");

    heartbeat_set_flux (hb, h);

    ok (heartbeat_get_rate (hb) == 2.,
        "heartbeat_get_rate returns default of 2s");
    errno = 0;
    ok (heartbeat_set_rate (hb, -1) < 1 && errno == EINVAL,
        "heartbeat_set_rate -1 fails with EINVAL");
    errno = 0;
    ok (heartbeat_set_rate (hb, 1000000) < 1 && errno == EINVAL,
        "heartbeat_set_rate 1000000 fails with EINVAL");
    ok (heartbeat_set_ratestr (hb, "250ms") == 0,
        "heartbeat_set_ratestr 250ms works");
    ok (heartbeat_get_rate (hb) == 0.250,
        "heartbeat_get_rate returns what was set");
    ok (heartbeat_set_rate (hb, 0.1) == 0,
        "heartbeat_set_rate 0.1 works");
    ok (heartbeat_get_rate (hb) == 0.1,
        "heartbeat_get_rate returns what was set");

    ok (heartbeat_get_epoch (hb) == 0,
        "heartbeat_get_epoch works, default is zero");

    w = flux_msg_handler_create (h, FLUX_MATCH_EVENT, heartbeat_event_cb, hb);
    ok (w != NULL,
        "created event watcher");
    flux_msg_handler_start (w);

    ok (heartbeat_start (hb) == 0,
        "heartbeat_start works");

    ok (flux_reactor_run (flux_get_reactor (h), 0) == 0,
        "flux reactor exited normally");

    heartbeat_destroy (hb);
    flux_msg_handler_destroy (w);
    flux_close (h);

    done_testing ();
    return 0;
}
Example #9
0
flux_t *flux_open (const char *uri, int flags)
{
    char *default_uri = NULL;
    char *path = NULL;
    char *scheme = NULL;
    void *dso = NULL;
    connector_init_f *connector_init = NULL;
    const char *s;
    flux_t *h = NULL;

    if (!uri)
        uri = getenv ("FLUX_URI");
    if (!uri) {
        if (asprintf (&default_uri, "local://%s",
                      flux_conf_get ("rundir", 0)) < 0)
            goto done;
        uri = default_uri;
    }
    if (!(scheme = strdup (uri))) {
        errno = ENOMEM;
        goto done;
    }
    path = strstr (scheme, "://");
    if (path) {
        *path = '\0';
        path = strtrim (path + 3, " \t");
    }
    if (!(connector_init = find_connector (scheme, &dso)))
        goto done;
    if (getenv ("FLUX_HANDLE_TRACE"))
        flags |= FLUX_O_TRACE;
    if (getenv ("FLUX_HANDLE_MATCHDEBUG"))
        flags |= FLUX_O_MATCHDEBUG;
    if (!(h = connector_init (path, flags))) {
        dlclose (dso);
        goto done;
    }
    h->dso = dso;
#if HAVE_CALIPER
    profiling_context_init(&h->prof);
#endif
    if ((s = getenv ("FLUX_HANDLE_USERID"))) {
        uint32_t userid = strtoul (s, NULL, 10);
        if (flux_opt_set (h, FLUX_OPT_TESTING_USERID, &userid,
                                                      sizeof (userid)) < 0) {
            flux_handle_destroy (h);
            h = NULL;
            goto done;
        }
    }
    if ((s = getenv ("FLUX_HANDLE_ROLEMASK"))) {
        uint32_t rolemask = strtoul (s, NULL, 0);
        if (flux_opt_set (h, FLUX_OPT_TESTING_ROLEMASK, &rolemask,
                                                    sizeof (rolemask)) < 0) {
            flux_handle_destroy (h);
            h = NULL;
            goto done;
        }
    }
done:
    free (scheme);
    free (default_uri);
    return h;
}
Example #10
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;
}
Example #11
0
static const char *default_cmdhelp_pattern (optparse_t *p)
{
    int *flags = optparse_get_data (p, "conf_flags");
    return flux_conf_get ("cmdhelp_pattern", *flags);
}