Example #1
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);
}
Example #2
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);
}
Example #3
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 sqlite_ctx_t *getctx (flux_t *h)
{
    sqlite_ctx_t *ctx = (sqlite_ctx_t *)flux_aux_get (h, "flux::content-sqlite");
    const char *dir;
    const char *tmp;
    bool cleanup = false;

    if (!ctx) {
        if (!(ctx = calloc (1, sizeof (*ctx))))
            goto error;
        if (!(ctx->lzo_buf = calloc (1, lzo_buf_chunksize)))
            goto error;
        ctx->lzo_bufsize = lzo_buf_chunksize;
        ctx->h = h;
        if (!(ctx->hashfun = flux_attr_get (h, "content.hash"))) {
            flux_log_error (h, "content.hash");
            goto error;
        }
        if (!(tmp = flux_attr_get (h, "content.blob-size-limit"))) {
            flux_log_error (h, "content.blob-size-limit");
            goto error;
        }
        ctx->blob_size_limit = strtoul (tmp, NULL, 10);

        if (!(dir = flux_attr_get (h, "persist-directory"))) {
            if (!(dir = flux_attr_get (h, "broker.rundir"))) {
                flux_log_error (h, "broker.rundir");
                goto error;
            }
            cleanup = true;
        }
        if (asprintf (&ctx->dbdir, "%s/content", dir) < 0)
            goto error;
        if (mkdir (ctx->dbdir, 0755) < 0 && errno != EEXIST) {
            flux_log_error (h, "mkdir %s", ctx->dbdir);
            goto error;
        }
        if (cleanup)
            cleanup_push_string (cleanup_directory_recursive, ctx->dbdir);
        if (asprintf (&ctx->dbfile, "%s/sqlite", ctx->dbdir) < 0)
            goto error;

        if (sqlite3_open (ctx->dbfile, &ctx->db) != SQLITE_OK) {
            flux_log_error (h, "sqlite3_open %s", ctx->dbfile);
            goto error;
        }
        if (sqlite3_exec (ctx->db, "PRAGMA journal_mode=OFF",
                                            NULL, NULL, NULL) != SQLITE_OK
                || sqlite3_exec (ctx->db, "PRAGMA synchronous=OFF",
                                            NULL, NULL, NULL) != SQLITE_OK
                || sqlite3_exec (ctx->db, "PRAGMA locking_mode=EXCLUSIVE",
                                            NULL, NULL, NULL) != SQLITE_OK) {
            log_sqlite_error (ctx, "setting sqlite pragmas");
            goto error_sqlite;
        }
        if (sqlite3_exec (ctx->db, sql_create_table,
                                            NULL, NULL, NULL) != SQLITE_OK) {
            log_sqlite_error (ctx, "creating table");
            goto error_sqlite;
        }
        if (sqlite3_prepare_v2 (ctx->db, sql_load, -1, &ctx->load_stmt,
                                            NULL) != SQLITE_OK) {
            log_sqlite_error (ctx, "preparing load stmt");
            goto error_sqlite;
        }
        if (sqlite3_prepare_v2 (ctx->db, sql_store, -1, &ctx->store_stmt,
                                            NULL) != SQLITE_OK) {
            log_sqlite_error (ctx, "preparing store stmt");
            goto error_sqlite;
        }
        if (sqlite3_prepare_v2 (ctx->db, sql_dump, -1, &ctx->dump_stmt,
                                            NULL) != SQLITE_OK) {
            log_sqlite_error (ctx, "preparing dump stmt");
            goto error_sqlite;
        }
        if (flux_aux_set (h, "flux::content-sqlite", ctx, freectx) < 0)
            goto error;
    }
    return ctx;
error_sqlite:
    set_errno_from_sqlite_error (ctx);
error:
    freectx (ctx);
    return NULL;
}
Example #5
0
int main (int argc, char *argv[])
{
    flux_t h;
    int ch;
    uint32_t rank = FLUX_NODEID_ANY; /* local */
    char *cmd;
    int e;

    log_init ("flux-comms");

    while ((ch = getopt_long (argc, argv, OPTIONS, longopts, NULL)) != -1) {
        switch (ch) {
            case 'h': /* --help */
                usage ();
                break;
            case 'r': /* --rank N */
                rank = strtoul (optarg, NULL, 10);
                break;
            default:
                usage ();
                break;
        }
    }
    if (optind == argc)
        usage ();
    cmd = argv[optind++];
    if (rank != FLUX_NODEID_ANY
            && (!strcmp (cmd, "recover-all") || !strcmp (cmd, "info")))
        usage ();

    if (!(h = flux_open (NULL, 0)))
        log_err_exit ("flux_open");

    if (!strcmp (cmd, "reparent")) {
        if (optind != argc - 1)
            usage ();
        if (flux_reparent (h, rank, argv[optind]) < 0)
            log_err_exit ("flux_reparent");
    } else if (!strcmp (cmd, "idle")) {
        if (optind != argc)
            usage ();
        char *peers;
        if (!(peers = flux_lspeer (h, rank)))
            log_err_exit ("flux_peer");
        printf ("%s\n", peers);
        free (peers);
    } else if (!strcmp (cmd, "panic")) {
        char *msg = NULL;
        size_t len = 0;
        if (optind < argc) {
            if ((e = argz_create (argv + optind, &msg, &len)) != 0)
                log_errn_exit (e, "argz_create");
            argz_stringify (msg, len, ' ');
        }
        flux_panic (h, rank, msg);
        if (msg)
            free (msg);
    } else if (!strcmp (cmd, "failover")) {
        if (optind != argc)
            usage ();
        if (flux_failover (h, rank) < 0)
            log_err_exit ("flux_failover");
    } else if (!strcmp (cmd, "recover")) {
        if (optind != argc)
            usage ();
        if (flux_recover (h, rank) < 0)
            log_err_exit ("flux_recover");
    } else if (!strcmp (cmd, "recover-all")) {
        if (optind != argc)
            usage ();
        if (flux_recover_all (h) < 0)
            log_err_exit ("flux_recover_all");
    } else if (!strcmp (cmd, "info")) {
        int arity;
        uint32_t rank, size;
        const char *s;
        if (flux_get_rank (h, &rank) < 0 || flux_get_size (h, &size) < 0)
            log_err_exit ("flux_get_rank/size");
        if (!(s = flux_attr_get (h, "tbon.arity", NULL)))
            log_err_exit ("flux_attr_get tbon.arity");
        arity = strtoul (s, NULL, 10);
        printf ("rank=%d\n", rank);
        printf ("size=%d\n", size);
        printf ("arity=%d\n", arity);
    } else
        usage ();

    flux_close (h);
    log_fini ();
    return 0;
}