flux_modlist_t module_get_modlist (modhash_t *mh) { flux_modlist_t mods; zlist_t *uuids; char *uuid; module_t *p; if (!(mods = flux_modlist_create ())) goto done; if (!(uuids = zhash_keys (mh->zh_byuuid))) oom (); uuid = zlist_first (uuids); while (uuid) { p = zhash_lookup (mh->zh_byuuid, uuid); assert (p != NULL); if (flux_modlist_append (mods, module_get_name (p), p->size, p->digest, module_get_idle (p)) < 0) { flux_modlist_destroy (mods); mods = NULL; goto done; } uuid = zlist_next (uuids); } done: zlist_destroy (&uuids); return mods; }
void test_lsmod_codec (void) { flux_modlist_t *mods; int idle, size; const char *name, *digest; char *json_str; int status; mods = flux_modlist_create (); ok (mods != NULL, "flux_modlist_create works"); ok (flux_modlist_append (mods, "foo", 42, "aa", 3, 0) == 0, "first flux_modlist_append works"); ok (flux_modlist_append (mods, "bar", 43, "bb", 2, 1) == 0, "second flux_modlist_append works"); ok (flux_modlist_count (mods) == 2, "flux_modlist_count works"); ok (flux_modlist_get (mods, 0, &name, &size, &digest, &idle, &status) == 0 && name && size == 42 && digest && idle == 3 && status == 0 && !strcmp (name, "foo") && !strcmp (digest, "aa"), "flux_modlist_get(0) works"); ok (flux_modlist_get (mods, 1, &name, &size, &digest, &idle, &status) == 0 && name && size == 43 && digest && idle == 2 && status == 1 && !strcmp (name, "bar") && !strcmp (digest, "bb"), "flux_modlist_get(1) works"); /* again after encode/decode */ ok ((json_str = flux_lsmod_json_encode (mods)) != NULL, "flux_lsmod_json_encode works"); flux_modlist_destroy (mods); ok ((mods = flux_lsmod_json_decode (json_str)) != NULL, "flux_lsmod_json_decode works"); ok (flux_modlist_count (mods) == 2, "flux_modlist_count still works"); ok (flux_modlist_get (mods, 0, &name, &size, &digest, &idle, &status) == 0 && name && size == 42 && digest && idle == 3 && status == 0 && !strcmp (name, "foo") && !strcmp (digest, "aa"), "flux_modlist_get(0) still works"); ok (flux_modlist_get (mods, 1, &name, &size, &digest, &idle, &status) == 0 && name && size == 43 && digest && idle == 2 && status == 1 && !strcmp (name, "bar") && !strcmp (digest, "bb"), "flux_modlist_get(1) still works"); flux_modlist_destroy (mods); free (json_str); }
static void lsmod_cb (flux_t *h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { struct sched_plugin_loader *sploader = arg; struct sched_plugin *plugin = sched_plugin_get (sploader); flux_modlist_t *mods = NULL; zfile_t *zf = NULL; char *json_str = NULL; struct stat sb; int rc = -1; if (flux_request_decode (msg, NULL, NULL) < 0) goto done; if (!(mods = flux_modlist_create ())) goto done; if (plugin) { if (stat (plugin->path, &sb) < 0) goto done; if (!(zf = zfile_new (NULL, plugin->path))) goto done; if (flux_modlist_append (mods, plugin->name, sb.st_size, zfile_digest (zf), 0, FLUX_MODSTATE_RUNNING) < 0) goto done; } if (!(json_str = flux_lsmod_json_encode (mods))) goto done; rc = 0; done: if (flux_respond (h, msg, rc < 0 ? errno : 0, rc < 0 ? NULL : json_str) < 0) flux_log_error (h, "%s: flux_respond", __FUNCTION__); if (mods) flux_modlist_destroy (mods); zfile_destroy (&zf); if (json_str) free (json_str); }