int attr_add_active (attr_t *attrs, const char *name, int flags, attr_get_f get, attr_set_f set, void *arg) { struct entry *e; int rc = -1; if ((e = zhash_lookup (attrs->hash, name))) { if (!set) { errno = EEXIST; goto done; } if (set (name, e->val, arg) < 0) goto done; } e = entry_create (name, NULL, flags); e->set = set; e->get = get; e->arg = arg; e->flags |= FLUX_ATTRFLAG_ACTIVE; zhash_update (attrs->hash, name, e); zhash_freefn (attrs->hash, name, entry_destroy); rc = 0; done: return rc; }
static void s_zdir_watch_subscribe (zdir_watch_t *watch, const char *path) { if (watch->verbose) zsys_info ("zdir_watch: Subscribing to directory path: %s", path); zdir_watch_sub_t *sub = (zdir_watch_sub_t *) zmalloc (sizeof (zdir_watch_sub_t)); sub->dir = zdir_new (path, NULL); if (!sub->dir) { if (watch->verbose) zsys_error ("zdir_watch: Unable to create zdir for path: %s", path); zsock_signal (watch->pipe, 1); return; } int rc = zhash_insert (watch->subs, path, sub); if (rc) { if (watch->verbose) zsys_error ("zdir_watch: Unable to insert path '%s' into subscription list", path); zsock_signal (watch->pipe, 1); return; } void *item = zhash_freefn (watch->subs, path, s_sub_free); if (item != sub) { if (watch->verbose) zsys_error ("zdir_watch: Unable to set free fn for path %s", path); zsock_signal (watch->pipe, 1); return; } if (watch->verbose) zsys_info ("zdir_watch: Successfully subscribed to %s", path); zsock_signal (watch->pipe, 0); }
static int attr_set_rpc (attr_ctx_t *ctx, const char *name, const char *val) { flux_future_t *f; attr_t *attr; int rc = -1; #if JANSSON_VERSION_HEX >= 0x020800 /* $? format specifier was introduced in jansson 2.8 */ f = flux_rpc_pack (ctx->h, "attr.set", FLUX_NODEID_ANY, 0, "{s:s, s:s?}", "name", name, "value", val); #else f = flux_rpc_pack (ctx->h, "attr.set", FLUX_NODEID_ANY, 0, val ? "{s:s, s:s}" : "{s:s, s:n}", "name", name, "value", val); #endif if (!f) goto done; if (flux_future_get (f, NULL) < 0) goto done; if (val) { if (!(attr = attr_create (val, 0))) goto done; zhash_update (ctx->hash, name, attr); zhash_freefn (ctx->hash, name, attr_destroy); } else zhash_delete (ctx->hash, name); rc = 0; done: flux_future_destroy (f); return rc; }
void agent_control_message (agent_t *self) { zmsg_t *msg = zmsg_recv (self->control); char *command = zmsg_pop (msg); if (strcmp (command, "CONNECT") == 0) { char *endpoint = zmsg_pop (msg); printf ("I: connecting to %s...\n", endpoint); int rc = zmq_connect (self->router, endpoint); assert (rc == 0); server_t *server = server_new (endpoint); zhash_insert (self->servers, endpoint, server); zhash_freefn (self->servers, endpoint, s_server_free); zlist_append (self->actives, server); server->ping_at = s_clock () + PING_INTERVAL; server->expires = s_clock () + SERVER_TTL; free (endpoint); } else if (strcmp (command, "REQUEST") == 0) { assert (!self->request); // Strict request-reply cycle // Prefix request with sequence number and empty envelope char sequence_text [10]; sprintf (sequence_text, "%u", ++self->sequence); zmsg_push (msg, sequence_text); // Take ownership of request message self->request = msg; msg = NULL; // Request expires after global timeout self->expires = s_clock () + GLOBAL_TIMEOUT; } free (command); zmsg_destroy (&msg); }
static halutils_err_e _disp_table_insert (disp_table_t *self, uint32_t key, disp_table_func_fp func_fp) { if (func_fp == NULL) { return HALUTILS_ERR_NULL_POINTER; } DBE_DEBUG (DBG_HAL_UTILS | DBG_LVL_TRACE, "[halutils:disp_table] Registering function (%p) opcode (%u) into dispatch table\n", func_fp, key); func_fp_wrapper_t *func_fp_wrapper = zmalloc (sizeof *func_fp_wrapper); ASSERT_ALLOC (func_fp_wrapper, err_func_wrapper_alloc); /* Initialize func_p_wrapper struct */ func_fp_wrapper->func_fp = func_fp; char *key_c = halutils_stringify_key (key); ASSERT_ALLOC (key_c, err_key_c_alloc); int zerr = zhash_insert (self->table_h, key_c, func_fp_wrapper); ASSERT_TEST(zerr == 0, "Could not insert item into dispatch table", err_insert_hash); /* Setup free function */ zhash_freefn (self->table_h, key_c, _disp_table_free_item); free (key_c); return HALUTILS_SUCCESS; err_insert_hash: free (key_c); err_key_c_alloc: free (func_fp_wrapper); err_func_wrapper_alloc: return HALUTILS_ERR_ALLOC; }
static int get_rlist_result (zhash_t *mods, flux_mrpc_t *mrpc, uint32_t nodeid, int *ep) { JSON o = NULL; int rc = -1; const char *name, *digest; int i, len, errnum, size, idle; module_t *m; if (flux_mrpc_get_outarg_obj (mrpc, nodeid, &o) < 0) goto done; if (modctl_rlist_dec (o, &errnum, &len) < 0) goto done; for (i = 0; i < len; i++) { if (modctl_rlist_dec_nth (o, i, &name, &size, &digest, &idle) < 0) goto done; if (!(m = zhash_lookup (mods, digest))) { if (!(m = module_create (name, size, digest, idle, nodeid))) goto done; zhash_update (mods, digest, m); zhash_freefn (mods, digest, (zhash_free_fn *)module_destroy); } else if (module_update (m, idle, nodeid) < 0) goto done; } *ep = errnum; rc = 0; done: Jput (o); return rc; }
static const char* processor_setup_module(processor_state_t *self, const char *page) { int max_mod_len = strlen(page); char module_str[max_mod_len+1]; char *mod_ptr = strchr(page, ':'); strcpy(module_str, "::"); if (mod_ptr != NULL){ if (mod_ptr != page) { int mod_len = mod_ptr - page; memcpy(module_str+2, page, mod_len); module_str[mod_len+2] = '\0'; } } else { char *action_ptr = strchr(page, '#'); if (action_ptr != NULL) { int mod_len = action_ptr - page; memcpy(module_str+2, page, mod_len); module_str[mod_len+2] = '\0'; } } char *module = zhash_lookup(self->modules, module_str); if (module == NULL) { module = strdup(module_str); int rc = zhash_insert(self->modules, module, module); assert(rc == 0); zhash_freefn(self->modules, module, free); } // printf("[D] page: %s\n", page); // printf("[D] module: %s\n", module); return module; }
int lsmod_hash_cb (uint32_t nodeid, const char *json_str, zhash_t *mods) { flux_modlist_t modlist; mod_t *m; int i, len; const char *name, *digest; int size, idle; int rc = -1; if (!(modlist = flux_lsmod_json_decode (json_str))) goto done; if ((len = flux_modlist_count (modlist)) == -1) goto done; for (i = 0; i < len; i++) { if (flux_modlist_get (modlist, i, &name, &size, &digest, &idle) < 0) goto done; if ((m = zhash_lookup (mods, digest))) { if (idle < m->idle) m->idle = idle; if (!nodeset_add_rank (m->nodeset, nodeid)) oom (); } else { m = mod_create (name, size, digest, idle, nodeid); zhash_update (mods, digest, m); zhash_freefn (mods, digest, (zhash_free_fn *)mod_destroy); } } rc = 0; done: if (modlist) flux_modlist_destroy (modlist); return rc; }
static void server_accept (server_t *self, const char *key, const char *value) { tuple_t *tuple = (tuple_t *) zhash_lookup (self->tuples, key); if (tuple && streq (tuple->value, value)) return; // Duplicate tuple, do nothing // Create new tuple tuple = (tuple_t *) zmalloc (sizeof (tuple_t)); assert (tuple); tuple->container = self->tuples; tuple->key = strdup (key); tuple->value = strdup (value); // Store new tuple zhash_update (tuple->container, key, tuple); zhash_freefn (tuple->container, key, tuple_free); // Deliver to calling application zstr_sendx (self->pipe, "DELIVER", key, value, NULL); // Hold in server context so we can broadcast to all clients self->cur_tuple = tuple; engine_broadcast_event (self, NULL, forward_event); // Copy new tuple announcement to all remotes zsock_t *remote = (zsock_t *) zlist_first (self->remotes); while (remote) { int rc = zgossip_msg_send_publish (remote, key, value, 0); assert (rc == 0); remote = (zsock_t *) zlist_next (self->remotes); } }
static zhash_t *determine_all_min_bandwidth (struct rdl *rdl, zlist_t *running_jobs) { double root_bw; double *curr_value = NULL; struct resource *root = NULL; job_t *curr_job = NULL; char job_id_str[100]; zhash_t *job_hash = zhash_new (); root = rdl_resource_get (rdl, "default"); root_bw = get_max_bandwidth (root); curr_job = zlist_first (running_jobs); while (curr_job != NULL) { curr_value = (double *)malloc (sizeof (double)); *curr_value = root_bw; sprintf (job_id_str, "%d", curr_job->id); zhash_insert (job_hash, job_id_str, curr_value); zhash_freefn (job_hash, job_id_str, free); curr_job = zlist_next (running_jobs); } determine_all_min_bandwidth_helper (root, root_bw, job_hash); return job_hash; }
static service_t * s_service_require (broker_t *self, zframe_t *service_frame) { assert (service_frame); char *name = zframe_strdup (service_frame); service_t *service = (service_t *) zhash_lookup (self->services, name); if (service == NULL) { service = (service_t *) zmalloc (sizeof (service_t)); service->broker = self; service->name = name; service->requests = zlist_new (); service->waiting = zlist_new (); service->blacklist = zlist_new (); zhash_insert (self->services, name, service); zhash_freefn (self->services, name, s_service_destroy); if (self->verbose) zclock_log ("I: added service: %s", name); } else free (name); return service; }
static int barrier_add_client (barrier_t *b, char *sender, zmsg_t **zmsg) { if (zhash_insert (b->clients, sender, *zmsg) < 0) return -1; zhash_freefn (b->clients, sender, (zhash_free_fn *)free_zmsg); *zmsg = NULL; /* list owns it now */ return 0; }
static int barrier_add_client (barrier_t *b, char *sender, const flux_msg_t *msg) { flux_msg_t *cpy = flux_msg_copy (msg, true); if (!cpy || zhash_insert (b->clients, sender, cpy) < 0) return -1; zhash_freefn (b->clients, sender, (zhash_free_fn *)flux_msg_destroy); return 0; }
static pipe_t * pipe_new (server_t *server, const char *name) { pipe_t *self = (pipe_t *) zmalloc (sizeof (pipe_t)); self->name = strdup (name); self->server = server; zhash_insert (server->pipes, name, self); zhash_freefn (server->pipes, name, s_delete_pipe); return self; }
maltcp_ctx_connection_t *maltcp_ctx_connection_register_outgoing(maltcp_ctx_t *self, int socket, char *peer_uri) { maltcp_ctx_connection_t *cnx_ptr = maltcp_ctx_connection_create(socket); if (cnx_ptr != NULL) { char *uri = (char *) malloc(strlen(peer_uri) +1); strcpy(uri, peer_uri); zhash_update(self->cnx_table, uri, cnx_ptr); zhash_freefn(self->cnx_table, uri, free); } return cnx_ptr; }
static int s_kvs_put (void *arg, const char *kvsname, const char *key, const char *val) { diag ("%s: %s::%s", __FUNCTION__, kvsname, key); struct context *ctx = arg; int rc = 0; zhash_update (ctx->kvs, key, xstrdup (val)); zhash_freefn (ctx->kvs, key, (zhash_free_fn *)free); return rc; }
int flux_attr_fake (flux_t *h, const char *name, const char *val, int flags) { attr_ctx_t *ctx = getctx (h); attr_t *attr = attr_create (val, flags); if (!ctx || !attr) return -1; zhash_update (ctx->hash, name, attr); zhash_freefn (ctx->hash, name, attr_destroy); return 0; }
/* Proxy ping. */ void xping_request_cb (flux_t h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { ctx_t *ctx = arg; const char *json_str; int saved_errno; int rank, seq = ctx->ping_seq++; const char *service; char *hashkey = NULL; JSON in = Jnew (); JSON o = NULL; flux_msg_t *cpy; if (flux_request_decode (msg, NULL, &json_str) < 0) { saved_errno = errno; goto error; } if (!(o = Jfromstr (json_str)) || !Jget_int (o, "rank", &rank) || !Jget_str (o, "service", &service)) { saved_errno = errno = EPROTO; goto error; } flux_log (h, LOG_DEBUG, "Rxping rank=%d service=%s", rank, service); Jadd_int (in, "seq", seq); flux_log (h, LOG_DEBUG, "Tping seq=%d %d!%s", seq, rank, service); flux_rpc_t *rpc; if (!(rpc = flux_rpc (h, service, Jtostr (in), rank, FLUX_RPC_NORESPONSE))) { saved_errno = errno; goto error; } flux_rpc_destroy (rpc); if (!(cpy = flux_msg_copy (msg, true))) { saved_errno = errno; goto error; } hashkey = xasprintf ("%d", seq); zhash_update (ctx->ping_requests, hashkey, cpy); zhash_freefn (ctx->ping_requests, hashkey, (zhash_free_fn *)flux_msg_destroy); Jput (o); Jput (in); if (hashkey) free (hashkey); return; error: if (flux_respond (h, msg, saved_errno, NULL) < 0) flux_log_error (h, "%s: flux_respond", __FUNCTION__); Jput (o); Jput (in); }
void CSetAdd(cset_t *self, const uint8_t *srcip, const uint8_t *srcport, const uint8_t *dstip, const uint8_t *dstport, void *value) { assert(self); assert(srcip); assert(srcport); assert(dstip); assert(dstport); const char *key = CSetCreateKey(srcip, srcport, dstip, dstport); zhash_insert(self->hash, key, value); zhash_freefn (self->hash, key, _cset_free_fn); }
zyre_group_t * zyre_group_new (const char *name, zhash_t *container) { zyre_group_t *self = (zyre_group_t *) zmalloc (sizeof (zyre_group_t)); self->name = strdup (name); self->peers = zhash_new (); // Insert into container if requested if (container) { zhash_insert (container, name, self); zhash_freefn (container, name, s_delete_group); } return self; }
static int64_t * seq_create (seqhash_t *s, const char *name) { int rc; int64_t *v; if (zhash_lookup (s->vhash, name)) { errno = EEXIST; return (NULL); } v = seq_new (); rc = zhash_insert (s->vhash, xstrdup (name), v); assert (rc >= 0); zhash_freefn (s->vhash, name, free); return (v); }
static barrier_t *barrier_create (ctx_t *ctx, const char *name, int nprocs) { barrier_t *b; b = xzmalloc (sizeof (barrier_t)); b->name = xstrdup (name); b->nprocs = nprocs; if (!(b->clients = zhash_new ())) oom (); b->ctx = ctx; zhash_insert (ctx->barriers, b->name, b); zhash_freefn (ctx->barriers, b->name, barrier_destroy); return b; }
static int command_list_read (zhash_t *h, const char *path) { int i; int rc = -1; int n = 0; json_t *o = NULL; if (!(o = command_list_file_read (path))) goto out; n = json_array_size (o); for (i = 0; i < n; i++) { const char *category; const char *command; const char *description; json_t *entry; zlist_t *zl; if (!(entry = json_array_get (o, i))) { log_msg ("%s: entry %d is not an object", path, i); goto out; } if (json_unpack (entry, "{s:s s:s s:s}", "category", &category, "command", &command, "description", &description) < 0) { log_msg ("%s: Missing element in JSON entry %d", path, i); goto out; } if (!(zl = zhash_lookup (h, category))) { char *s = strdup (category); if (s == NULL) goto out; zl = zlist_new (); //zlist_set_destructor (zl, (czmq_destructor *) cmdhelp_destroy); zhash_insert (h, s, (void *) zl); zhash_freefn (h, s, (zhash_free_fn *) cmd_list_destroy); free (s); } zlist_append (zl, cmdhelp_create (command, description)); } rc = 0; out: json_decref (o); return (rc); }
static void pipe_destroy (pipe_t **self_p) { assert (self_p); if (*self_p) { pipe_t *self = *self_p; // Remove pipe from hash of known pipes zhash_freefn (self->server->pipes, self->name, NULL); zhash_delete (self->server->pipes, self->name); free (self->remote); free (self->name); free (self); *self_p = NULL; } }
zre_peer_t * zre_peer_new (char *identity, zhash_t *container, zctx_t *ctx) { zre_peer_t *self = (zre_peer_t *) zmalloc (sizeof (zre_peer_t)); self->ctx = ctx; self->identity = strdup (identity); self->ready = false; self->sent_sequence = 0; self->want_sequence = 0; // Insert into container if requested if (container) { zhash_insert (container, identity, self); zhash_freefn (container, identity, s_delete_peer); } return self; }
int attr_add (attr_t *attrs, const char *name, const char *val, int flags) { struct entry *e; if (name == NULL || (flags & FLUX_ATTRFLAG_ACTIVE)) { errno = EINVAL; return -1; } if ((e = zhash_lookup (attrs->hash, name))) { errno = EEXIST; return -1; } e = entry_create (name, val, flags); zhash_update (attrs->hash, name, e); zhash_freefn (attrs->hash, name, entry_destroy); return 0; }
static service_t * s_service_require(server_t *self, const char *service_name) { char *name = strdup(service_name); service_t *service = (service_t *) zhash_lookup(self->services, name); if (service == NULL) { service = (service_t *) zmalloc(sizeof(service_t)); service->broker = self; service->name = name; service->requests = zlist_new(); service->waiting = zlist_new(); zhash_insert(self->services, name, service); zhash_freefn(self->services, name, s_service_destroy); } else zstr_free(&name); return service; }
static service_t * s_service_require (broker_t *self, char *name) { assert (name); service_t *service = (service_t *) zhash_lookup (self->services, name); if (service == NULL) { service = (service_t *) calloc (1, sizeof (service_t)); service->name = strdup (name); service->requests = zlist_new (); service->waiting = zlist_new (); zhash_insert (self->services, name, service); zhash_freefn (self->services, name, s_service_destroy); if (self->verbose) s_console ("I: received message:"); } return service; }
zyre_peer_t * zyre_peer_new (zhash_t *container, zuuid_t *uuid) { zyre_peer_t *self = (zyre_peer_t *) zmalloc (sizeof (zyre_peer_t)); self->uuid = zuuid_dup (uuid); self->ready = false; self->connected = false; self->sent_sequence = 0; self->want_sequence = 0; // Insert into container if requested if (container) { int rc = zhash_insert (container, zuuid_str (self->uuid), self); assert (rc == 0); zhash_freefn (container, zuuid_str (self->uuid), s_delete_peer); } return self; }
static worker_t * s_worker_require (broker_t *self, char *identity) { assert (identity); // self->workers is keyed off worker identity worker_t *worker = (worker_t *) zhash_lookup (self->workers, identity); if (worker == NULL) { worker = (worker_t *) calloc (1, sizeof (worker_t)); worker->identity = strdup (identity); zhash_insert (self->workers, identity, worker); zhash_freefn (self->workers, identity, s_worker_destroy); if (self->verbose) s_console ("I: registering new worker: %s", identity); } return worker; }