static errno_t ipa_sudo_conv_store(hash_table_t *table, const char *key, void *value) { hash_key_t hkey; hash_value_t hvalue; int hret; if (table == NULL || key == NULL) { return EINVAL; } hkey.type = HASH_KEY_STRING; hkey.str = discard_const(key); /* If value is NULL we don't want to override existing entry. */ if (value == NULL && hash_has_key(table, &hkey)) { return EEXIST; } hvalue.type = HASH_VALUE_PTR; hvalue.ptr = value; hret = hash_enter(table, &hkey, &hvalue); if (hret != HASH_SUCCESS) { return EIO; } if (value != NULL) { talloc_steal(table, value); } return EOK; }
s3wid_t dict_add_word (dict_t *d, char *word, s3cipid_t *p, int32 np) { int32 w, len; dictword_t *wordp; s3wid_t newwid; if (d->n_word >= d->max_words) { E_ERROR("Dictionary full; add(%s) failed\n", word); return (BAD_WID); } wordp = d->word + d->n_word; wordp->word = (char *) ckd_salloc (word); /* Associate word string with d->n_word in hash table */ if (hash_enter (d->ht, wordp->word, d->n_word) != d->n_word) { ckd_free (wordp->word); return (BAD_WID); } /* Fill in word entry, and set defaults */ if (p && (np > 0)) { wordp->ciphone = (s3cipid_t *) ckd_malloc (np * sizeof(s3cipid_t)); memcpy (wordp->ciphone, p, np*sizeof(s3cipid_t)); wordp->pronlen = np; } else { wordp->ciphone = NULL; wordp->pronlen = 0; } wordp->alt = BAD_WID; wordp->basewid = d->n_word; wordp->n_comp = 0; wordp->comp = NULL; /* Determine base/alt wids */ if ((len = word2basestr (word)) > 0) { /* Truncated to a baseword string; find its ID */ if (hash_lookup (d->ht, word, &w) < 0) { word[len] = '('; /* Get back the original word */ E_FATAL("Missing base word for: %s\n", word); } else word[len] = '('; /* Get back the original word */ /* Link into alt list */ wordp->basewid = w; wordp->alt = d->word[w].alt; d->word[w].alt = d->n_word; } newwid = d->n_word++; return (newwid); }
static void ciphone_add (mdef_t *m, char *ci, s3pid_t p) { if (mdef->n_ciphone >= max_ciphone) E_FATAL("Max CI phones (%d) exceeded\n", max_ciphone); /* Make sure pid being assigned is the next free ciphone */ assert (p == mdef->n_ciphone); /* Note ciphone name */ mdef->ciphone[p].name = (char *) ckd_salloc (ci); if (hash_enter (mdef->ciphone_ht, mdef->ciphone[p].name, p) < 0) E_FATAL("hash_enter(%s) failed\n", mdef->ciphone[p].name); mdef->n_ciphone++; }
static int32 word2id(char *w) { int32 wid; if (hash_lookup(dict_ht, w, &wid) < 0) { if (n_word >= n_word_alloc) E_FATAL("Increase dictionary size\n"); word[n_word] = ckd_salloc(w); hash_enter(dict_ht, word[n_word], n_word); wid = n_word++; } return wid; }
static s3cipid_t dict_ciphone_id (dict_t *d, char *str) { int32 id; if (d->mdef) return mdef_ciphone_id (d->mdef, str); else { if (hash_lookup (d->pht, str, &id) < 0) { id = (d->n_ciphone)++; if (id >= MAX_CIPID) E_FATAL("Too many CIphones in dictionary; increase MAX_CIPID\n"); d->ciphone_str[id] = (char *) ckd_salloc(str); if (hash_enter (d->pht, d->ciphone_str[id], id) != id) E_FATAL("hash_enter(local-phonetable, %s) failed\n", str); } return id; } }
int create_env_hash_table(char ** env, hash_table_t ** table_out) { hash_table_t *local_table = NULL; hash_key_t key; hash_value_t value; char * tmp; char **ui; int err_h; err_h = hash_create((unsigned long)INIT_ENV_TABLE_SIZE, &local_table, delete_callback, NULL); if (err_h != HASH_SUCCESS) { fprintf(stderr, "couldn't create hash table (%s)\n", hash_error_string(err_h)); return err_h; } for(ui = (char **) msg.user_env; *ui!=NULL; ui++) { tmp = strchr(*ui,'='); *tmp = '\0'; key.type = HASH_KEY_STRING; key.str = strdup(*ui); value.type = HASH_VALUE_PTR; value.ptr = tmp+1; if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "couldn't add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } *tmp = '=' ; } *table_out = local_table; return HASH_SUCCESS; }
static errno_t add_sids_from_rid_array_to_hash_table(struct dom_sid *dom_sid, struct samr_RidWithAttributeArray *groups, struct sss_idmap_ctx *idmap_ctx, hash_table_t *sid_table) { enum idmap_error_code err; char *dom_sid_str = NULL; size_t dom_sid_str_len; char *sid_str = NULL; char *rid_start; hash_key_t key; hash_value_t value; int ret; size_t c; TALLOC_CTX *tmp_ctx = NULL; tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n"); return ENOMEM; } key.type = HASH_KEY_STRING; value.type = HASH_VALUE_ULONG; err = sss_idmap_smb_sid_to_sid(idmap_ctx, dom_sid, &dom_sid_str); if (err != IDMAP_SUCCESS) { DEBUG(SSSDBG_OP_FAILURE, "sss_idmap_smb_sid_to_sid failed.\n"); ret = EFAULT; goto done; } dom_sid_str_len = strlen(dom_sid_str); sid_str = talloc_zero_size(tmp_ctx, dom_sid_str_len + 12); if (sid_str == NULL) { DEBUG(SSSDBG_OP_FAILURE, "talloc_zero_size failed.\n"); ret = ENOMEM; goto done; } rid_start = sid_str + dom_sid_str_len; memcpy(sid_str, dom_sid_str, dom_sid_str_len); for (c = 0; c < groups->count; c++) { memset(rid_start, '\0', 12); ret = snprintf(rid_start, 12, "-%lu", (unsigned long) groups->rids[c].rid); if (ret < 0 || ret > 12) { DEBUG(SSSDBG_OP_FAILURE, "snprintf failed.\n"); ret = EIO; goto done; } key.str = sid_str; value.ul = 0; ret = hash_enter(sid_table, &key, &value); if (ret != HASH_SUCCESS) { DEBUG(SSSDBG_OP_FAILURE, "hash_enter failed [%d][%s].\n", ret, hash_error_string(ret)); ret = EIO; goto done; } } ret = EOK; done: sss_idmap_free_sid(idmap_ctx, dom_sid_str); talloc_free(tmp_ctx); return ret; }
int find_path( int in_room_vnum, int out_room_vnum, CHAR_DATA *ch, int depth, int in_zone ) { struct room_q *tmp_q, *q_head, *q_tail; struct hash_header x_room; int i, tmp_room, count=0, thru_doors; ROOM_INDEX_DATA *herep; ROOM_INDEX_DATA *startp; EXIT_DATA *exitp; if ( depth <0 ) { thru_doors = TRUE; depth = -depth; } else { thru_doors = FALSE; } startp = get_room_index( in_room_vnum ); init_hash_table( &x_room, sizeof(int), 2048 ); hash_enter( &x_room, in_room_vnum, (void *) - 1 ); /* initialize queue */ q_head = (struct room_q *) malloc(sizeof(struct room_q)); q_tail = q_head; q_tail->room_nr = in_room_vnum; q_tail->next_q = 0; while(q_head) { herep = get_room_index( q_head->room_nr ); /* for each room test all directions */ if( herep->area == startp->area || !in_zone ) { /* only look in this zone... saves cpu time and makes world safer for players */ for( i = 0; i <= 5; i++ ) { exitp = herep->exit[i]; if( exit_ok(exitp) && ( thru_doors ? GO_OK_SMARTER : GO_OK ) ) { /* next room */ tmp_room = herep->exit[i]->to_room->vnum; if( tmp_room != out_room_vnum ) { /* shall we add room to queue ? count determines total breadth and depth */ if( !hash_find( &x_room, tmp_room ) && ( count < depth ) ) /* && !IS_SET( RM_FLAGS(tmp_room), DEATH ) ) */ { count++; /* mark room as visted and put on queue */ tmp_q = (struct room_q *) malloc(sizeof(struct room_q)); tmp_q->room_nr = tmp_room; tmp_q->next_q = 0; q_tail->next_q = tmp_q; q_tail = tmp_q; /* ancestor for first layer is the direction */ hash_enter( &x_room, tmp_room, ((int)hash_find(&x_room,q_head->room_nr) == -1) ? (void*)(i+1) : hash_find(&x_room,q_head->room_nr)); } } else { /* have reached our goal so free queue */ tmp_room = q_head->room_nr; for(;q_head;q_head = tmp_q) { tmp_q = q_head->next_q; free(q_head); } /* return direction if first layer */ if ((int)hash_find(&x_room,tmp_room)==-1) { if (x_room.buckets) { /* junk left over from a previous track */ destroy_hash_table(&x_room, donothing); } return(i); } else { /* else return the ancestor */ int i; i = (int)hash_find(&x_room,tmp_room); if (x_room.buckets) { /* junk left over from a previous track */ destroy_hash_table(&x_room, donothing); } return( -1+i); } } } } } /* free queue head and point to next entry */ tmp_q = q_head->next_q; free(q_head); q_head = tmp_q; } /* couldn't find path */ if( x_room.buckets ) { /* junk left over from a previous track */ destroy_hash_table( &x_room, donothing ); } return -1; }
int create_settings_hash_table(hash_table_t ** table_out) { hash_table_t *local_table = NULL; hash_key_t key; hash_value_t value; const char * truth = strdup("TRUE"); const char * fallacy = strdup("FALSE"); int err_h; err_h = hash_create((unsigned long)INIT_SETTINGS_TABLE_SIZE, &local_table, delete_callback, NULL); if (err_h != HASH_SUCCESS) { fprintf(stderr, "couldn't create hash table (%s)\n", hash_error_string(err_h)); return err_h; } key.type = HASH_KEY_STRING; value.type = HASH_VALUE_PTR; if(msg.runas_user && *msg.runas_user ){ key.str = strdup(SSS_SUDO_ITEM_RUSER); value.ptr = msg.runas_user; if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); } if(msg.runas_group && *msg.runas_group ){ key.str = strdup(SSS_SUDO_ITEM_RGROUP); value.ptr = msg.runas_group; if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); } if(msg.prompt && *msg.prompt ){ key.str = strdup(SSS_SUDO_ITEM_PROMPT); value.ptr = msg.prompt; if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); } if(msg.network_addrs && *msg.network_addrs ){ key.str = strdup(SSS_SUDO_ITEM_NETADDR); value.ptr = msg.network_addrs; if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); } key.str = strdup(SSS_SUDO_ITEM_USE_SUDOEDIT); value.ptr = GET_BOOL_STRING(msg.use_sudoedit); if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); key.str = strdup(SSS_SUDO_ITEM_USE_SETHOME); value.ptr = GET_BOOL_STRING(msg.use_set_home); if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); key.str = strdup(SSS_SUDO_ITEM_USE_PRESERV_ENV); value.ptr = GET_BOOL_STRING(msg.use_preserve_environment); if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); key.str = strdup(SSS_SUDO_ITEM_USE_IMPLIED_SHELL); value.ptr = GET_BOOL_STRING(msg.use_implied_shell); if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); key.str = strdup(SSS_SUDO_ITEM_USE_LOGIN_SHELL); value.ptr = GET_BOOL_STRING(msg.use_login_shell); if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); key.str = strdup(SSS_SUDO_ITEM_USE_RUN_SHELL); value.ptr = GET_BOOL_STRING(msg.use_run_shell); if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); key.str = strdup(SSS_SUDO_ITEM_USE_PRE_GROUPS); value.i = GET_BOOL_STRING(msg.use_preserve_groups); if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); key.str = strdup(SSS_SUDO_ITEM_USE_IGNORE_TICKET); value.ptr = GET_BOOL_STRING(msg.use_ignore_ticket); if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); key.str = strdup(SSS_SUDO_ITEM_USE_NON_INTERACTIVE); value.ptr =GET_BOOL_STRING(msg.use_noninteractive); if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); key.str = strdup(SSS_SUDO_ITEM_DEBUG_LEVEL); value.ptr = GET_BOOL_STRING(msg.debug_level); if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); key.str = strdup(SSS_SUDO_ITEM_CLI_PID); asprintf(&value.ptr,"%u",msg.cli_pid); if ((err_h = hash_enter(local_table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(err_h)); return err_h; } free(key.str); *table_out = local_table; return HASH_SUCCESS; }
static errno_t process_ext_groups(TALLOC_CTX *mem_ctx, size_t reply_count, struct sysdb_attrs **reply, hash_table_t **_ext_group_hash) { int ret; hash_table_t *ext_group_hash = NULL; hash_key_t key; hash_value_t value; hash_table_t *m_hash = NULL; hash_key_t m_key; hash_value_t m_value; size_t g; size_t s; size_t m; TALLOC_CTX *tmp_ctx = NULL; const char **ext_sids; const char **mof; tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n"); ret = ENOMEM; goto done; } ret = sss_hash_create(mem_ctx, reply_count, &ext_group_hash); if (ret != HASH_SUCCESS) { DEBUG(SSSDBG_OP_FAILURE, "sss_hash_create failed.\n"); goto done; } key.type = HASH_KEY_STRING; m_key.type = HASH_KEY_STRING; m_value.type = HASH_VALUE_PTR; m_value.ptr = NULL; for (g = 0; g < reply_count; g++) { ret = sysdb_attrs_get_string_array(reply[g], "ipaExternalMember", tmp_ctx, &ext_sids); if (ret == ENOENT) { /* no external members, try next external group. */ continue; } if (ret != EOK) { DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_get_string_array failed.\n"); goto done; } ret = sysdb_attrs_get_string_array(reply[g], "memberOf", tmp_ctx, &mof); if (ret == ENOENT) { /* no IPA groups, try next external group. */ continue; } if (ret != EOK) { DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_get_string_array failed.\n"); goto done; } for (s = 0; ext_sids[s] != NULL; s++) { /* hash_lookup does not modify key.str. */ key.str = discard_const(ext_sids[s]); ret = hash_lookup(ext_group_hash, &key, &value); if (ret == HASH_SUCCESS) { if (value.type != HASH_VALUE_PTR) { DEBUG(SSSDBG_OP_FAILURE, "Unexpected value type.\n"); ret = EINVAL; goto done; } for (m = 0; mof[m] != NULL; m++) { /* hash_enter does not modify m_key.str. */ m_key.str = discard_const(mof[m]); DEBUG(SSSDBG_TRACE_ALL, "Adding group [%s] to SID [%s].\n", m_key.str, key.str); ret = hash_enter(value.ptr, &m_key, &m_value); if (ret != HASH_SUCCESS) { DEBUG(SSSDBG_OP_FAILURE, "hash_enter failed.\n"); goto done; } } } else if (ret == HASH_ERROR_KEY_NOT_FOUND) { ret = sss_hash_create(ext_group_hash, 5, &m_hash); if (ret != HASH_SUCCESS) { DEBUG(SSSDBG_OP_FAILURE, "sss_hash_create failed.\n"); goto done; } value.type = HASH_VALUE_PTR; value.ptr = m_hash; DEBUG(SSSDBG_TRACE_ALL, "Adding SID [%s] to external group hash.\n", key.str); ret = hash_enter(ext_group_hash, &key, &value); if (ret != HASH_SUCCESS) { DEBUG(SSSDBG_OP_FAILURE, "hash_enter failed.\n"); goto done; } for (m = 0; mof[m] != NULL; m++) { /* hash_enter does not modify m_key.str. */ m_key.str = discard_const(mof[m]); DEBUG(SSSDBG_TRACE_ALL, "Adding group [%s] to SID [%s].\n", m_key.str, key.str); ret = hash_enter(m_hash, &m_key, &m_value); if (ret != HASH_SUCCESS) { DEBUG(SSSDBG_OP_FAILURE, "hash_enter failed.\n"); goto done; } } } else { DEBUG(SSSDBG_OP_FAILURE, "hash_lookup failed.\n"); goto done; } } } ret = EOK; done: if (ret != EOK) { talloc_free(ext_group_hash); } else { *_ext_group_hash = ext_group_hash; } talloc_free(tmp_ctx); return ret; }
static errno_t find_ipa_ext_memberships(TALLOC_CTX *mem_ctx, const char *user_name, struct sss_domain_info *user_dom, hash_table_t *ext_group_hash, struct ldb_dn **_user_dn, char ***_groups) { int ret; TALLOC_CTX *tmp_ctx = NULL; struct ldb_result *result; char **groups = NULL; size_t c; const char *sid; hash_key_t key; hash_value_t value; hash_entry_t *entry; struct hash_iter_context_t *iter; hash_table_t *group_hash; size_t g_count; struct ldb_dn *user_dn = NULL; tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { return ENOMEM; } ret = sysdb_initgroups(tmp_ctx, user_dom, user_name, &result); if (ret != EOK) { DEBUG(SSSDBG_OP_FAILURE, "sysdb_initgroups failed.\n"); goto done; } if (result->count == 0) { DEBUG(SSSDBG_MINOR_FAILURE, "User [%s] not found in cache.\n", user_name); ret = EOK; goto done; } ret = sss_hash_create(tmp_ctx, 10, &group_hash); if (ret != HASH_SUCCESS) { DEBUG(SSSDBG_OP_FAILURE, "sss_hash_create failed.\n"); goto done; } key.type = HASH_KEY_STRING; /* The IPA external domains can have references to group and user SIDs. * This means that we not only want to look up the group SIDs but the SID * of the user (first element of result) as well. */ for (c = 0; c < result->count; c++) { sid = ldb_msg_find_attr_as_string(result->msgs[c], SYSDB_SID_STR, NULL); if (sid == NULL) { DEBUG(SSSDBG_MINOR_FAILURE, "Group [%s] does not have a SID.\n", ldb_dn_get_linearized(result->msgs[c]->dn)); continue; } key.str = discard_const(sid); ret = hash_lookup(ext_group_hash, &key, &value); if (ret == HASH_ERROR_KEY_NOT_FOUND) { DEBUG(SSSDBG_TRACE_ALL, "SID [%s] not found in ext group hash.\n", sid); } else if (ret == HASH_SUCCESS) { iter = new_hash_iter_context(value.ptr); if (iter == NULL) { DEBUG(SSSDBG_OP_FAILURE, "new_hash_iter_context failed.\n"); ret = EINVAL; goto done; } while ((entry = iter->next(iter)) != NULL) { ret = hash_enter(group_hash, &entry->key, &entry->value); if (ret != HASH_SUCCESS) { DEBUG(SSSDBG_OP_FAILURE, "Failed to add group [%s].\n", entry->key.str); } } talloc_free(iter); } else { DEBUG(SSSDBG_OP_FAILURE, "hash_lookup failed for SID [%s].\n", sid); } } g_count = hash_count(group_hash); if (g_count == 0) { DEBUG(SSSDBG_TRACE_FUNC, "No external groupmemberships found.\n"); ret = EOK; goto done; } groups = talloc_zero_array(mem_ctx, char *, g_count + 1); if (groups == NULL) { DEBUG(SSSDBG_OP_FAILURE, "talloc_array failed.\n"); ret = ENOMEM; goto done; } iter = new_hash_iter_context(group_hash); if (iter == NULL) { DEBUG(SSSDBG_OP_FAILURE, "new_hash_iter_context failed.\n"); ret = EINVAL; goto done; } c = 0; while ((entry = iter->next(iter)) != NULL) { groups[c] = talloc_strdup(groups, entry->key.str); if (groups[c] == NULL) { DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n"); ret = ENOMEM; goto done; } c++; } user_dn = ldb_dn_copy(mem_ctx, result->msgs[0]->dn); if (user_dn == NULL) { DEBUG(SSSDBG_OP_FAILURE, "ldb_dn_copy failed.\n"); ret = ENOMEM; goto done; } ret = EOK; done: *_user_dn = user_dn; *_groups = groups; talloc_free(tmp_ctx); return ret; }
static errno_t get_active_uid_linux(hash_table_t *table, uid_t search_uid) { DIR *proc_dir = NULL; struct dirent *dirent; int ret, err; pid_t pid = -1; uid_t uid; hash_key_t key; hash_value_t value; proc_dir = opendir("/proc"); if (proc_dir == NULL) { ret = errno; DEBUG(SSSDBG_CRIT_FAILURE, "Cannot open proc dir.\n"); goto done; }; errno = 0; while ((dirent = readdir(proc_dir)) != NULL) { if (only_numbers(dirent->d_name) != 0) continue; ret = name_to_pid(dirent->d_name, &pid); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "name_to_pid failed.\n"); goto done; } ret = get_uid_from_pid(pid, &uid); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "get_uid_from_pid failed.\n"); goto done; } if (table != NULL) { key.type = HASH_KEY_ULONG; key.ul = (unsigned long) uid; value.type = HASH_VALUE_ULONG; value.ul = (unsigned long) uid; ret = hash_enter(table, &key, &value); if (ret != HASH_SUCCESS) { DEBUG(SSSDBG_CRIT_FAILURE, "cannot add to table [%s]\n", hash_error_string(ret)); ret = ENOMEM; goto done; } } else { if (uid == search_uid) { ret = EOK; goto done; } } errno = 0; } if (errno != 0 && dirent == NULL) { ret = errno; DEBUG(SSSDBG_CRIT_FAILURE, "readdir failed.\n"); goto done; } ret = closedir(proc_dir); proc_dir = NULL; if (ret == -1) { DEBUG(SSSDBG_CRIT_FAILURE, "closedir failed, watch out.\n"); } if (table != NULL) { ret = EOK; } else { ret = ENOENT; } done: if (proc_dir != NULL) { err = closedir(proc_dir); if (err) { DEBUG(SSSDBG_CRIT_FAILURE, "closedir failed, bad dirp?\n"); } } return ret; }
int main(int argc, char **argv) { static hash_table_t *table = NULL; hash_key_t key, *keys; hash_value_t value; struct hash_iter_context_t *iter; hash_entry_t *entry; unsigned long i, n_entries; int error; struct my_data_t *my_data = new_data(1024, "Hello World!"); unsigned long count; /* Create a hash table */ error = hash_create(10, &table, delete_callback, NULL); if (error != HASH_SUCCESS) { fprintf(stderr, "cannot create hash table (%s)\n", hash_error_string(error)); return error; } /* Enter a key named "My Data" and specify it's value as a pointer to my_data */ key.type = HASH_KEY_STRING; key.str = strdup("My Data"); value.type = HASH_VALUE_PTR; value.ptr = my_data; if ((error = hash_enter(table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(error)); return error; } free(key.str); /* Get a list of keys and print them out, free the list when we're done */ if ((error = hash_keys(table, &count, &keys)) != HASH_SUCCESS) { fprintf(stderr, "cannot get key list (%s)\n", hash_error_string(error)); return error; } for (i = 0; i < count; i++) printf("key: %s\n", keys[i].str); free(keys); /* Lookup the key named "My Data" */ key.type = HASH_KEY_STRING; key.str = strdup("My Data"); if ((error = hash_lookup(table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot find key \"%s\" (%s)\n", key.str, hash_error_string(error)); } free(key.str); /* Visit each entry in the table, callback will increment count on each visit */ printf("Iterate using callback\n"); count = 0; hash_iterate(table, visit_callback, &count); /* Assure number of visits equal the table size */ assert(count == hash_count(table)); /* Visit each entry using iterator object */ printf("Iterate using iterator\n"); n_entries = 0; iter = new_hash_iter_context(table); while ((entry = iter->next(iter)) != NULL) { struct my_data_t *data = (struct my_data_t *) entry->value.ptr; printf("%s = [foo=%d bar=%s]\n", entry->key.str, data->foo, data->bar); n_entries++; } free(iter); /* Assure number of visits equal the table size */ assert(n_entries == hash_count(table)); /* Remove the entry, deletion callback will be invoked */ key.type = HASH_KEY_STRING; key.str = strdup("My Data"); if ((error = hash_delete(table, &key)) != HASH_SUCCESS) { fprintf(stderr, "cannot delete from table (%s)\n", hash_error_string(error)); } /* Assure key is no longer in table */ assert (!hash_has_key(table, &key)); free(key.str); /* Free the table */ hash_destroy(table); return 0; }