/**
 * Use this function iterator-style to go through the entries
 * in the table of managed interfaces. You should call 
 * policy_table_iterator_reset before iterating over entries with 
 * this function.
 * 
 *
 * @return
 *      Pointer to policy table entry if successful, otherwise NULL.
 */
policy_table_entry_t *
policy_table_next(void)
{
    policy_table_entry_t *v;

    INSIST(if_table != NULL);
    if(hashtable_count(if_table) < 1)
        return NULL;

    if(itr_broken) {
        if(itr)
            free(itr);
        itr = hashtable_iterator(if_table); // mallocs
        INSIST(itr != NULL);
        itr_broken = FALSE;
        // now it's at the first element
    } else if (!hashtable_iterator_advance(itr)){
        // no more
        free(itr);
        itr = NULL;
        itr_broken = TRUE; // will reset next time around

        return NULL;
    }

    v = hashtable_iterator_value(itr);

    return v;
}
Ejemplo n.º 2
0
void print_queue_stats(char *temp, int len_limit) {
    char *pos = temp;
    int remains = len_limit - 3;
    int res;
    int64_t set_hits,get_hits;
    pthread_rwlock_rdlock(&qlist_ht_lock);
    char *k;
    queue_t *q;
    struct hashtable_itr *itr = NULL;
    itr = hashtable_iterator(qlist_htp);
    assert(itr != NULL);
    if (hashtable_count(qlist_htp) > 0)
    {
        do {
            k = hashtable_iterator_key(itr);
            q = hashtable_iterator_value(itr);
            pthread_mutex_lock(&(q->lock));
            set_hits = q->set_hits;
            get_hits = q->get_hits;
            pthread_mutex_unlock(&(q->lock));
            if (remains > strlen(k) + 50) {
                res = sprintf(pos, "STAT %s %lld/%lld\r\n", k, set_hits, get_hits);
                remains -= res;
                pos += res;                
            } else {
                break;
            }
        } while (hashtable_iterator_advance(itr));
    }
    free(itr);
    pthread_rwlock_unlock(&qlist_ht_lock);
    sprintf(pos, "END");
    return;
}
Ejemplo n.º 3
0
int
hashtable_iterator_remove(struct hashtable_itr *itr)
{
    struct entry *remember_e, *remember_parent;
    int ret;

    /* Do the removal */
    if (NULL == (itr->parent))
    {
        /* element is head of a chain */
        itr->h->table[itr->index] = itr->e->next;
    } else {
        /* element is mid-chain */
        itr->parent->next = itr->e->next;
    }
    /* itr->e is now outside the hashtable */
    remember_e = itr->e;
    itr->h->entrycount--;
    freekey(remember_e->k);

    /* Advance the iterator, correcting the parent */
    remember_parent = itr->parent;
    ret = hashtable_iterator_advance(itr);
    if (itr->parent == remember_e) { itr->parent = remember_parent; }
    free(remember_e);
    return ret;
}
Ejemplo n.º 4
0
void destroy_shp_hash(void) {
    struct hashtable_itr *iterator=NULL;
    shpinfo *si;
    int ret;

    if (shp_hash) {
        // walk through the hashtable, free any pointers in the values
        // that aren't null, or we'll leak like a sieve.

        // the hashtable functions always attempt to dereference iterator,
        // and don't check if you give it a null, but will return null if
        // there's nothing in the table.  Grrrr.
        iterator=hashtable_iterator(shp_hash);
        do {
            ret=0;
            if (iterator) {
                si = hashtable_iterator_value(iterator);
                if (si) 
                    empty_shpinfo(si);
                ret=hashtable_iterator_advance(iterator);
            }
        } while (ret);
        hashtable_destroy(shp_hash, 1);  // destroy the hashtable, freeing
                                         // what's left of the entries
        shp_hash=NULL;
        if (iterator) free(iterator);
    }
}
Ejemplo n.º 5
0
static void
add_file_info_indexes(uint32_t *indexes, uint32_t size,
                      struct manifest *mf, struct hashtable *included_files)
{
	if (size == 0) {
		return;
	}

	// path --> index
	struct hashtable *mf_files =
		create_string_index_map(mf->files, mf->n_files);
	// struct file_info --> index
	struct hashtable *mf_file_infos =
		create_file_info_index_map(mf->file_infos, mf->n_file_infos);
	struct hashtable_itr *iter = hashtable_iterator(included_files);
	uint32_t i = 0;
	do {
		char *path = hashtable_iterator_key(iter);
		struct file_hash *file_hash = hashtable_iterator_value(iter);
		indexes[i] = get_file_hash_index(mf, path, file_hash, mf_files,
		                                 mf_file_infos);
		i++;
	} while (hashtable_iterator_advance(iter));
	assert(i == size);

	hashtable_destroy(mf_file_infos, 1);
	hashtable_destroy(mf_files, 1);
}
/**
 * first_name_htbl_element - return next element of the name hash table.
 * @ph_elt: the path hash table the name hash table belongs to
 * @itr: double pointer to a 'struct hashtable_itr' object where the
 *       information about further iterations is stored
 *
 * This function implements name hash table iteration together with
 * 'first_name_htbl_element()'. Returns the next name hash table element or
 * %NULL if there are no more elements.
 */
struct name_htbl_element *
next_name_htbl_element(struct path_htbl_element *ph_elt,
		       struct hashtable_itr **itr)
{
	if (!path_htbl || !ph_elt || !hashtable_iterator_advance(*itr))
		return NULL;

	return hashtable_iterator_value(*itr);
}
Ejemplo n.º 7
0
/*
 * matWrite - write signals from measurement structure to MAT file
 */
int matWrite(measurement_t *measurement, const char *outFileName)
{
  size_t dims[2];
  int err = 0;
  mat_t *mat;
  matvar_t *matvar;

  mat = Mat_Create(outFileName, NULL);
  if (mat != NULL) {

    /* loop over all time series */
    struct hashtable *timeSeriesHash = measurement->timeSeriesHash;

    /* Iterator constructor only returns a valid iterator if
     * the hashtable is not empty */
    if (hashtable_count(timeSeriesHash) > 0) {

      struct hashtable_itr *itr = hashtable_iterator(timeSeriesHash);
      do {
        char         *signalName = hashtable_iterator_key(itr);
        timeSeries_t *timeSeries = hashtable_iterator_value(itr);
        double *timeValue = (double *)malloc(sizeof(double)*2*timeSeries->n);
        unsigned int i;

        /*
         * build up a 1x2n array with time stamps in [0..n-1] and
         * values in [n..2n-1].
         */
        for(i=0;i<timeSeries->n;i++) {
          timeValue[i] = timeSeries->time[i];
        }
        for(i=0;i<timeSeries->n;i++) {
          timeValue[timeSeries->n + i] = timeSeries->value[i];
        }
        dims[0] = timeSeries->n;
        dims[1] = 2;

        /* output signal to mat structure and free up temp array. */
        matvar = Mat_VarCreate(signalName, MAT_C_DOUBLE, MAT_T_DOUBLE,
                               2, dims, timeValue, 0);
        Mat_VarWrite(mat, matvar, 0);
        Mat_VarFree(matvar);

        free(timeValue);
      } while (hashtable_iterator_advance(itr));
      free(itr);
    }

    Mat_Close(mat);
  } else {
    fprintf(stderr, "error: could not create MAT file %s\n", outFileName);
    err = 1;
  }

  return err;
}
Ejemplo n.º 8
0
void purge_shp_hash(time_t secs_now) {
    struct hashtable_itr *iterator=NULL;
    shpinfo *si;
    int ret;


    if (secs_now > purge_time) {  // Time to purge
        //time_now = localtime(&secs_now);
        //(void)strftime(timestring,100,"%a %b %d %H:%M:%S %Z %Y",time_now);
        //fprintf(stderr,"Purging...%s\n",timestring);

        purge_time += PURGE_PERIOD;

        if (shp_hash) {
            // walk through the hash table and kill entries that are old

            iterator=hashtable_iterator(shp_hash);
            do {
                ret=0;
                if (iterator) {  // must check this, because could be null
                                 // if the iterator malloc failed
                    si=hashtable_iterator_value(iterator);
                    
                    if (si) {
                        if (secs_now > si->last_access+PURGE_PERIOD) {
                            // this is stale, hasn't been accessed in a while
                            //fprintf(stderr,
                            // "found stale entry for %s, deleting it.\n",
                            // si->filename);
                            //fprintf(stderr,"    Destroying si=%lx\n",
                            //            (unsigned long int) si);
                            ret=hashtable_iterator_remove(iterator);
                            
                            // Important that we NOT do the
                            // destroy first, because we've used
                            // the filename pointer field of the
                            // structure as the key, and the
                            // remove function will free that.  If
                            // we clobber the struct first, we
                            // invite segfaults
                            destroy_shpinfo(si);
                            
                            //fprintf(stderr,"    removing from hashtable\n");
                        } else {
                            ret=hashtable_iterator_advance(iterator);
                        }
                    }
                }
            } while (ret);
            // we're now done with the iterator.  Free it to stop us from
            // leaking!
            if (iterator) free(iterator);
        }
        //fprintf(stderr,"   done Purging...\n");
    }
}
Ejemplo n.º 9
0
void provide_symbols(TCCState *tccs) {
    if(hashtable_count(symbol_table)) {
        struct hashtable_itr *itr = hashtable_iterator(symbol_table);
        do {
            struct symbol *s = hashtable_iterator_value(itr);
            tcc_add_symbol(tccs, s->name, s->addr);
        } while(hashtable_iterator_advance(itr));
        free(itr);
    }
}
Ejemplo n.º 10
0
char *symbol_declarations(void) {
    char *decls = strdup("");
    if(hashtable_count(symbol_table)) {
        struct hashtable_itr *itr = hashtable_iterator(symbol_table);
        do {
            struct symbol *s = hashtable_iterator_value(itr);
            asprintfa(&decls, "extern %s;\n", s->decl);
        } while(hashtable_iterator_advance(itr));
        free(itr);
    }
    return decls;
}
Ejemplo n.º 11
0
static void copy_table(zk_hashtable *from, watcher_object_list_t *to) {
    struct hashtable_itr *it;
    int hasMore;
    if(hashtable_count(from->ht)==0)
        return;
    it=hashtable_iterator(from->ht);
    do {
        watcher_object_list_t *w = hashtable_iterator_value(it);
        copy_watchers(w, to, 1);
        hasMore=hashtable_iterator_advance(it);
    } while(hasMore);
    free(it);
}
Ejemplo n.º 12
0
/** Enable all the instrumentation points DSUI knows about for
  * a particular datastream */
void dsui_enable_all_ips(dsui_stream_t ds)
{
	int ctr = 0;

	hashtable_itr_t itr;
	init_iterator(&itr, ip_names);

	do {
		struct datastream_ip *ip = hashtable_iterator_value(&itr);
		ctr++;
		__dsui_enable_ip(ds, ip, NULL);
	} while (hashtable_iterator_advance(&itr));
	dprintf("Enabled %d entities for datastream [%d]\n",
			ctr, ds);
}
Ejemplo n.º 13
0
/* Write the DSUI header to an output file. This includes a timekeeping
 * event (to establish tsc-to-nanosecond correspondence) and then a
 * namespace event for each instrumentation point we know about */
static void write_header(struct logging_thread *log)
{
	hashtable_itr_t itr;

	log_time_state(log);
	if (!hashtable_count(ip_names))
		return;

	init_iterator(&itr, ip_names);

	do {
		struct datastream_ip *ip = hashtable_iterator_value(&itr);
		write_namespace_event(ip->ip, log);
	} while (hashtable_iterator_advance(&itr));
}
/**
 * Mark all policies in the table UNVERIFIED. Since we can't delete routes or
 * filters, all that are left unverified (i.e. they don't get 
 * added again) when calling policy_table_clean() get removed.
 * 
 * @return
 *      TRUE upon success;
 *      FALSE if aborted because there's still changes pending 
 *            in which case all route statuses stay the same
 */
boolean
policy_table_unverify_all(void)
{
    policy_table_entry_t * pol = NULL;
    ped_policy_route_t * route = NULL;
    struct hashtable_itr * iterator = NULL;
    
    if(hashtable_count(if_table) < 1)
        return TRUE; // nothing to do
        
    if(changes_pending > 0)
        return FALSE; // don't do anything if there's still changes pending

    junos_trace(PED_TRACEFLAG_HT, "%s", __func__);

    iterator = hashtable_iterator(if_table); // mallocs
    INSIST(iterator != NULL);

    // go through all policies
    do {
        pol = hashtable_iterator_value(iterator);
        
        // for all filters and routes in route lists set status to UNVERIFIED
        
        if(!pol->broken) {
            if(pol->filter) {
                pol->filter->status = FILTER_UNVERIFIED;
            }
            if((route = pol->route)) {
                while(route) {
                    // changes_pending is wrong if this fails
                    INSIST(route->status != ROUTE_PENDING);
                    route->status = ROUTE_UNVERIFIED;
                    route = route->next;
                }
            }
        }
        
    } while (hashtable_iterator_advance(iterator));

    free(iterator);
    
    clean_table = TRUE;
    
    return TRUE;
}
Ejemplo n.º 15
0
/** Register an instrumentation point with DSUI. This accomplishes
 * several things:
 * 1) The IP is inserted into the global instrumentation point
 *    hash table so it can be looked up by name
 * 2) The numerical ID of the instrumentation point is assigned.
 *    Entities are logged by ID, with the name and other metadata
 *    stored in the previously logged namespace information.
 *
 * If DSUI dicovers that an instrumentation point is already registered
 * under the same name, then it assumes that this IP is another instance
 * of the same point, and modifies the IP's pointers so that it refers
 * to the same namespace/state information as the previous IP.
 *
 * If there are any open log files, a namespace event for this IP
 * will be written to each of them.
 *
 * You must hold the DSUI write-lock when calling this function */
static void __dsui_register_ip(struct datastream_ip *ip)
{
	struct datastream_ip_data *ipdata = ip->ip;
	char *ipname;
	struct datastream_ip *v;
	hashtable_itr_t itr;

	if (ipdata->id) {
		bprintf("Already Registered %s/%s/%d\n", ipdata->group,
			ipdata->name, ipdata->id);
		return;
	}

	ipname = malloc(strlen(ipdata->group) + strlen(ipdata->name) + 2);
	sprintf(ipname, "%s/%s", ipdata->group, ipdata->name);
	v = hashtable_search(ip_names, ipname);
	if (v) {
		//dprintf("%s encountered more than once mapping %p->%p\n",
		//		ipname, ip, v);
		ip->ip = v->ip;
		ip->next = &v->ip->next;
		ip->id = &v->ip->id;
		v->ip->line = -1;
		free(ipname);
		return;
	}

	ipdata->id = starting_id++;
	hashtable_insert(ip_names, ipname, ip);
	//dprintf("Registered %s/%s/%d \n", ipdata->group,
	//		ipdata->name, ipdata->id);

	/* if this ip was registered when there are already active
	 * logging threads, we need to write namespace information
	 * for this ip */

	if (!hashtable_count(logging_threads)) {
		return;
	}
	init_iterator(&itr, logging_threads);
	do {
		struct logging_thread *log;
		log = hashtable_iterator_value(&itr);
		write_namespace_event(ipdata, log);
	} while (hashtable_iterator_advance(&itr));
}
Ejemplo n.º 16
0
/* this function obtains all sender stats. hlper to getAllStatsLines()
 * We need to keep this looked to avoid resizing of the hash table
 * (what could otherwise cause a segfault).
 */
static void
getSenderStats(rsRetVal(*cb)(void*, const char*),
	void *usrptr,
	statsFmtType_t fmt,
	const int8_t bResetCtrs)
{
	struct hashtable_itr *itr = NULL;
	struct sender_stats *stat;
	char fmtbuf[2048];

	pthread_mutex_lock(&mutSenders);

	/* Iterator constructor only returns a valid iterator if
	 * the hashtable is not empty
	 */
	if(hashtable_count(stats_senders) > 0) {
		itr = hashtable_iterator(stats_senders);
		do {
			stat = (struct sender_stats*)hashtable_iterator_value(itr);
			if(fmt == statsFmt_Legacy) {
				snprintf(fmtbuf, sizeof(fmtbuf),
					"_sender_stat: sender=%s messages=%"
					PRIu64,
					stat->sender, stat->nMsgs);
			} else {
				snprintf(fmtbuf, sizeof(fmtbuf),
					"{ \"name\":\"_sender_stat\", "
					"\"sender\":\"%s\", \"messages\":\"%"
					PRIu64 "\"}",
					stat->sender, stat->nMsgs);
			}
			fmtbuf[sizeof(fmtbuf)-1] = '\0';
			cb(usrptr, fmtbuf);
			if(bResetCtrs)
				stat->nMsgs = 0;
		} while (hashtable_iterator_advance(itr));
	}

	free(itr);
	pthread_mutex_unlock(&mutSenders);
}
Ejemplo n.º 17
0
/**
 * Free a value_t pointer, as well as any data stored within it. Container types
 * will be recursively freed.
 * @param value	value_t pointer to free
 */
void free_value(value_t *value) {
	list_t *listval, *cur, *temp;
	hashtable_t *hashval;
	hashtable_itr_t *itr;
	value_t *tempv;

	switch(value->type) {
	case INTTYPE:
	case BOOLTYPE:
	case LONGTYPE:
	case DOUBLETYPE:
		break;
	case STRINGTYPE:
	case REFTYPE:
		free(value->value.s);
		break;
	case INVOTYPE:
		free(value->value.v->name);
		tempv = encap_hash(value->value.v->params);
		free_value(tempv);
		break;
	case LISTTYPE:
		listval = value->value.l;
		list_for_each_safe(cur, temp, listval) {
			free_value((value_t*)cur->item);
			free(cur);
		}
		free(listval);
		break;
	case DICTTYPE:
		hashval = value->value.h;
		if (hashtable_count(hashval) > 0) {
			itr = hashtable_iterator(hashval);
			do {
				free_value((value_t*)hashtable_iterator_value(itr));
			} while (hashtable_iterator_advance(itr));
			free(itr);
		}
		hashtable_destroy(hashval, 0);
		break;
	}
Ejemplo n.º 18
0
void
cache_indexes_empty(void)
{
	size_t *cached;
	struct hashtable_itr *itr;

	cached = indexcachescached;
	itr = hashtable_iterator(indexcachehash);
	if (hashtable_count(indexcachehash) > 0) {
		do {
			indexcache_t *ic;

			ic = hashtable_iterator_value(itr);
			if (ic == NULL)
				continue;
			munmap(ic->ptr, ic->size);
		} while (hashtable_iterator_advance(itr));
	}

	hashtable_destroy(indexcachehash, 1);
}
Ejemplo n.º 19
0
void bdb_qlist_db_close(void){
    dump_qstats_to_db();
    
    struct hashtable_itr *itr = NULL;
    queue_t *q;
    itr = hashtable_iterator(qlist_htp);
    assert(itr != NULL);
    if (hashtable_count(qlist_htp) > 0)
    {
        do {
            q = hashtable_iterator_value(itr);
            q->dbp->close(q->dbp, 0);
            pthread_mutex_destroy(&(q->lock));
        } while (hashtable_iterator_advance(itr));
    }
    free(itr);
    if (qlist_dbp != NULL) {
        qlist_dbp->close(qlist_dbp, 0);        
    }
    fprintf(stderr, "qlist_dbp->close: OK\n");
}
Ejemplo n.º 20
0
/** returns a configfile linked list of active logging thread filenames.
 * These are the keys in the logging_threads hashtable. */
list_t *get_dsui_output_filenames()
{
	list_t *f = create_list();
	struct logging_thread *log;
	hashtable_itr_t itr;

	km_rdwr_rlock(&dsui_rwlock);

	if (!hashtable_count(logging_threads)) {
		goto out;
	}

	init_iterator(&itr, logging_threads);
	do {
		log = hashtable_iterator_value(&itr);
		list_append(f, encap_string(log->filename));
	} while (hashtable_iterator_advance(&itr));
out:
	km_rdwr_runlock(&dsui_rwlock);
	return f;
}
Ejemplo n.º 21
0
void reseedEncoderFLY(LTencoderFLY *encoder, unsigned long newSeed) {

    // Reinit the totGenSymbols and the refRandomSeed
    encoder->totGenSymbols = 0;
    unsigned long oldSeed = encoder->refRandomSeed;
    encoder->refRandomSeed = newSeed;
    unsigned long oldConfSeed;
    struct hashtable *h = encoder->DistributionsTable;

#ifdef DEBUGencoderPrintBucketRandomSeed
    printf("Encoder[reseed()] - Seed: %lu\n", encoder->refRandomSeed);
#endif

    struct hashtable_itr *itr;
    struct key *k;
    struct value *v;

    // Update all the configurations, if any
    itr = hashtable_iterator(h);
    if (hashtable_count(h) > 0) {
        do {
            k = hashtable_iterator_key(itr);
            v = hashtable_iterator_value(itr);

            // Update the confSeed
            oldConfSeed = v->confSeed;
            v->confSeed = newSeed + (oldConfSeed-oldSeed);

            // Reinit the random generator
            initRandom(&v->randomGenerator, v->confSeed);

            // Erase the number of generated symbols
            v->genSymbols = 0;
        } while (hashtable_iterator_advance(itr));
    }
    free(itr);
    itr = NULL;
    return;
}
Ejemplo n.º 22
0
void ht_to_perl_ht(HV *perl_ht, struct hashtable *params) {
    if (!hashtable_count(params)) return;

    struct hashtable_itr *itr;
    itr = hashtable_iterator(params);


    do {
        char *param = hashtable_iterator_key(itr);
        char *value = hashtable_iterator_value(itr);
        
        // check if key already exists
        if (hv_exists(perl_ht, param, strlen(param))) {
            fprintf(stderr, "Parameter '%s' is already defined. Ignoring.\n", param);
            continue;
        }

        hv_store(perl_ht, param, strlen(param),
            sv_2mortal(newSVpv(value, 0)), 0);
        
    } while (hashtable_iterator_advance(itr));
    free(itr);
}
/**
 * free_devtable_info - free device table information.
 *
 * This function frees the path hash table and the name hash tables.
 */
void free_devtable_info(void)
{
	struct hashtable_itr *ph_itr;
	struct path_htbl_element *ph_elt;

	if (!path_htbl)
		return;

	if (hashtable_count(path_htbl) > 0) {
		ph_itr = hashtable_iterator(path_htbl);
		do {
			ph_elt = hashtable_iterator_value(ph_itr);
			/*
			 * Note, since we use the same string for the key and
			 * @name in the name hash table elements, we do not
			 * have to iterate name hash table because @name memory
			 * will be freed when freeing the key.
			 */
			hashtable_destroy(ph_elt->name_htbl, 1);
		} while (hashtable_iterator_advance(ph_itr));
	}
	hashtable_destroy(path_htbl, 1);
}
Ejemplo n.º 24
0
/* check if a sender has not sent info to us for an extended period
 * of time.
 */
void
checkGoneAwaySenders(const time_t tCurr)
{
	struct hashtable_itr *itr = NULL;
	struct sender_stats *stat;
	const time_t rqdLast = tCurr - glblSenderStatsTimeout;
	struct tm tm;

	pthread_mutex_lock(&mutSenders);

	/* Iterator constructor only returns a valid iterator if
	 * the hashtable is not empty
	 */
	if(hashtable_count(stats_senders) > 0) {
		itr = hashtable_iterator(stats_senders);
		do {
			stat = (struct sender_stats*)hashtable_iterator_value(itr);
			if(stat->lastSeen < rqdLast) {
				if(glblReportGoneAwaySenders) {
					localtime_r(&stat->lastSeen, &tm);
					LogMsg(0, RS_RET_SENDER_GONE_AWAY,
						LOG_WARNING,
						"removing sender '%s' from connection "
						"table, last seen at "
						"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
						stat->sender,
						tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
						tm.tm_hour, tm.tm_min, tm.tm_sec);
				}
				hashtable_remove(stats_senders, (void*)stat->sender);
			}
		} while (hashtable_iterator_advance(itr));
	}

	pthread_mutex_unlock(&mutSenders);
	free(itr);
}
Ejemplo n.º 25
0
void *
cache_free(cache_t *c)
{

    	cache_value_t *v;

    struct hashtable_itr *itr;
    if (hashtable_count(c->c_data) > 0)
    {
        itr = hashtable_iterator(c->c_data);
        do {
            v = hashtable_iterator_value(itr);

	    c->c_freevalue(v->p);
	    free(v);
        } while (hashtable_iterator_advance(itr));

     	free(itr);

    }

    hashtable_destroy(c->c_data,0); /* second arg indicates "free(value)" */

}
/**
 * Clean policy table, remove all UNVERIFIED filters and routes.
 * Anything left in the UNVERIFIED state (status) will be deleted.
 */
void
policy_table_clean(void)
{
    policy_table_entry_t * policy = NULL;
    ped_policy_route_t * route_tmp = NULL, * route = NULL;
    struct hashtable_itr * iterator = NULL;
    
    if(hashtable_count(if_table) < 1 || !clean_table ||
       !get_ssd_ready() || !get_ssd_idle()) {
        return; // nothing to do or don't clean yet
    }
    
    junos_trace(PED_TRACEFLAG_HT, "%s", __func__);
    
    iterator = hashtable_iterator(if_table); // mallocs
    INSIST(iterator != NULL);

    // go through all policies
    do {
        policy = hashtable_iterator_value(iterator);
        
        // if we have a broken policy, then remove it
        if(policy->broken) {
            
            // remove and delete filters
            if(policy->filter) {
                remove_filters_from_interface(policy->ifname);
                free(policy->filter);
            }
            
            if(policy->pfd_filter) {
                remove_pfd_filter_from_interface(policy->ifname);
                policy->pfd_filter = FALSE;
            }
            
            // remove and delete routes
            
            route = policy->route;
            while(route) {
                route_tmp = route;      // Save pointer to current route.
                route = route->next;    // Move to the next route.
                if(route_tmp->status != ROUTE_FAILED) {
                    remove_route(route_tmp);
                    if(route->status == ROUTE_PENDING) {
                        --changes_pending;
                    }
                }
                free(route_tmp);        // Free the current route data.
            }

            free(policy);
            
            if(hashtable_iterator_remove(iterator))
                continue;
            else
                break;
        }
        
        // else: policy is not broken 
        // but we will check for routes or filters that don't belong
        
        
        // Check filter:
        if(policy->filter && policy->filter->status == FILTER_UNVERIFIED) {
            free(policy->filter);
            policy->filter = NULL;
            remove_filters_from_interface(policy->ifname);
        }
        
        if(!policy->pfd_filter) {
            policy->pfd_filter = apply_pfd_filter_to_interface(policy->ifname);
        }

        // Check route(s):        
        route = policy->route;
        route_tmp = NULL;
        policy->route = NULL;  // Detach route list temporarily
        
        while(route) {
            
            // check we don't get FAILED routes (in non-broken policies)
            if(route->status == ROUTE_FAILED) {
                // log and crash because this shouldn't happen
                ERRMSG(PED, TRACE_LOG_EMERG, 
                    "%s: Found a route with status=FAILED in a non-broken "
                    "policy. This should not happen.", __func__);
                // abort() is called in the above ERRMSG
            }
            
            // its status should probably be ADDED...
            // unless it's not part of the PSD policy anymore it will be:
            // UNVERIFIED
            if(route->status == ROUTE_UNVERIFIED) {
                // we've received all routes from the PSD, so it is  
                // no longer part of the policy, and we need to delete it
                
                // copy route data into new mem to pass to callback
                // we choose to do this only when policy is not broken
                
                if(route_tmp) { // route_tmp points to last kept route

                    route_tmp->next = route->next;
                    
                    remove_route(route);
                    free(route);
                    
                    // make route point to the next route (in tmp->next) and
                    route = route_tmp->next;
                    
                } else {  // No previous routes that we've kept yet
                    
                    // use tmp ptr to save next
                    route_tmp = route->next;
                    
                    remove_route(route);
                    free(route);
                    
                    // restore route to next route (in tmp) and tmp to NULL
                    route = route_tmp;
                    route_tmp = NULL;
                }
            } else { // a route we are keeping

                if(policy->route == NULL) {
                    policy->route = route;  // First route in the new list
                }
                
                route_tmp = route; // move route_tmp to end
                route = route->next;
            }
        }
        
        // Done checking route(s)
        
    } while (hashtable_iterator_advance(iterator));

    free(iterator);
    
    clean_table = FALSE;
}
Ejemplo n.º 27
0
int transaction_serialize(transaction* t, tr_submit_msg* msg, int max_size) {
    int size = 0;
    int hsize = 0;
	key* k;
    flat_key_val* kv;
    flat_key_hash* kh;
	struct hashtable_itr* itr;
	
	msg->type = TRANSACTION_SUBMIT;
    msg->id.client_id = t->id.client_id;
    msg->id.seqnumber = t->id.seqnumber;
    msg->id.node_id = NodeID;
    msg->start = t->st;
    msg->readset_count = 0;
    msg->writeset_count = 0;
    
    // copy readset hashes
	if (hashtable_count(t->rs) > 0) {
		itr = hashtable_iterator(t->rs);
		do {
			if ((size + (int)sizeof(flat_key_hash)) > max_size) {
				return -1;
			}
				
			k = hashtable_iterator_key(itr);
			kh = (flat_key_hash*)&msg->data[size];
			
			kh->hash[0] = hashtable_iterator_hash(itr);
			kh->hash[1] = djb2_hash(k->data, k->size);
			
			size += sizeof(flat_key_hash);
			msg->readset_count++;
		} while (hashtable_iterator_advance(itr));
		free(itr);
	}

    
    // copy writeset hashes
	if (hashtable_count(t->ws) > 0) {
		itr = hashtable_iterator(t->ws);
		do {
			if ((size + (int)sizeof(flat_key_hash)) > max_size) {
				return -1;
			}
			
			k = hashtable_iterator_key(itr);
			kh = (flat_key_hash*)&msg->data[size];
			
			kh->hash[0] = hashtable_iterator_hash(itr);
			kh->hash[1] = djb2_hash(k->data, k->size);
			
			size += sizeof(flat_key_hash);
			msg->writeset_count++;
		} while (hashtable_iterator_advance(itr));
		free(itr);
		hsize = size;
		
		itr = hashtable_iterator(t->ws);
		do {
			kv = hashtable_iterator_value(itr);
			if ((size + (int)FLAT_KEY_VAL_SIZE(kv)) > max_size) {
				return -1;
			}
			
			memcpy(&msg->data[size], kv, FLAT_KEY_VAL_SIZE(kv));
			size += (int)FLAT_KEY_VAL_SIZE(kv);
		} while (hashtable_iterator_advance(itr));
		free(itr);
	}
    msg->writeset_size = (size - hsize);
    return TR_SUBMIT_MSG_SIZE(msg);
}
Ejemplo n.º 28
0
static void dump_qstats_to_db(void) {
    /* qstats hashtable */
    char *kk;
    qstats_t *s;
    struct hashtable *qstats_htp = NULL;    
    qstats_htp = create_hashtable(64, hashfromkey, equalkeys);
    assert(qstats_htp != NULL);
    
    /* cp hashtable to stats table, this is very fast in-memory */
    pthread_rwlock_rdlock(&qlist_ht_lock);
    char *k;
    queue_t *q;
    int result;
    struct hashtable_itr *itr = NULL;
    itr = hashtable_iterator(qlist_htp);
    assert(itr != NULL);
    if (hashtable_count(qlist_htp) > 0)
    {
        do {
            k = hashtable_iterator_key(itr);
            q = hashtable_iterator_value(itr);
            pthread_mutex_lock(&(q->lock));
            if (q->old_set_hits == q->set_hits &&
                q->old_get_hits == q->get_hits) {
                pthread_mutex_unlock(&(q->lock));
                continue;
            }
            q->old_set_hits = q->set_hits;
            q->old_get_hits = q->get_hits;
            pthread_mutex_unlock(&(q->lock));
            kk = strdup(k);
            assert(kk);
            s = calloc(1, sizeof(qstats_t));
            assert(s);
            s->set_hits = q->old_set_hits;
            s->get_hits = q->old_get_hits;
            result = hashtable_insert(qstats_htp, (void *)kk, (void *)s);
            assert(result != 0);
        } while (hashtable_iterator_advance(itr));
    }
    free(itr);
    itr = NULL;
    pthread_rwlock_unlock(&qlist_ht_lock);
    
    /* dump stats hashtable to db */
    DBT dbkey, dbdata;
    int ret;
    DB_TXN *txnp = NULL;
    ret = envp->txn_begin(envp, NULL, &txnp, 0);
    CHECK_DB_RET(ret);
    itr = hashtable_iterator(qstats_htp);
    assert(itr != NULL);
    if (hashtable_count(qstats_htp) > 0)
    {
        do {
            kk = hashtable_iterator_key(itr);
            s = hashtable_iterator_value(itr);
            BDB_CLEANUP_DBT();
            dbkey.data = (void *)kk;
            dbkey.size = strlen(kk) + 1;
            dbdata.data = (void *)s;
            dbdata.size = sizeof(qstats_t);
            ret = qlist_dbp->put(qlist_dbp, txnp, &dbkey, &dbdata, 0);
            CHECK_DB_RET(ret);
            fprintf(stderr, "dump stats[%s], set_hits: %lld, get_hits: %lld \n",
                    kk, s->set_hits, s->get_hits);
        } while (hashtable_iterator_advance(itr));
    }
    free(itr);
    itr = NULL;
    ret = txnp->commit(txnp, 0);
    CHECK_DB_RET(ret);

    hashtable_destroy(qstats_htp, 1);
    qstats_htp = NULL;
    return;
dberr:
    if(itr != NULL) {
        free(itr);
    }
    if (qstats_htp != NULL) {
        hashtable_destroy(qstats_htp, 1);
    }
    if (txnp != NULL){
        txnp->abort(txnp);
    }
    if (settings.verbose > 1) {
        fprintf(stderr, "dump_qstats_to_db: %s\n", db_strerror(ret));
    }
}
Ejemplo n.º 29
0
int bbdocument_add(char subname[],char documenturi[],char documenttype[],char document[],const int dokument_size,unsigned int lastmodified,char *acl_allow, char *acl_denied,const char title[], char doctype[], char *attributes, container *attrkeys) {

	struct ReposetoryHeaderFormat ReposetoryHeader;

	int htmlbuffersize = 0;//((dokument_size *2) +512);	//+512 da vi skal ha med div meta data, som html kode
	char *htmlbuffer = NULL;// = malloc(htmlbuffersize);
	char *imagebuffer;
	char *documenttype_real;
	size_t imageSize;
	unsigned int DocIDForExistTest;
	unsigned int lastmodifiedForExistTest;
	struct hashtable *metahash = NULL;
	buffer *documentbuffer;

	memset(&ReposetoryHeader,0,sizeof(ReposetoryHeader));

	printf("bbdocument_add: \"%s\"\n",documenturi);

	printf("bbdocument_add: Attributes = \"%s\" (", attributes);
	if (attrkeys!=NULL) printf("+)\n");
	else printf(" )\n");


	//tester at det ikke finnes først. Hvis det finnes hånterer vi det. Eventuelt også lagre  den nye versjonen hvis det er forandret.
	if (uriindex_get(documenturi,&DocIDForExistTest,&lastmodifiedForExistTest,subname)) {

		if (lastmodifiedForExistTest == lastmodified) {
			printf("bbdocument_add: Uri \"%s\" all redy exist with DocID \"%u\" and time \"%u\"\n",documenturi,DocIDForExistTest,lastmodifiedForExistTest);
			return 1;
		}
		//hvis url er kjent, men oppdater rebruker vi den.
		ReposetoryHeader.DocID = DocIDForExistTest;
	}
	else {
		//hvis vi ikke har DocID har vi et system som trenger å få opprettet det. For eks bb eller bdisk.
		ReposetoryHeader.DocID = rGeneraeADocID(subname);

		#ifdef DEBUG
			printf("Dident have a known DocID for document. Did generet on. DOcID is now %u\n",ReposetoryHeader.DocID);
		#endif

	}

	// hvis vi har en "text applicatino" enkoding, som ikke er en vanlig tekst fil, men tekst som kommer fra 
	// en apllikasjon behandler vi det som text.
	//if (strcmp(documenttype,"tapp") == 0) {
	//	documenttype_real = strdup("text");
	//}
	//else 
	if (documenttype[0] == '\0') {
		if ((documenttype_real = sfindductype(documenturi)) == NULL) {
			printf("Will use .none as documenttype because I can't decide format. File name isent dos type (8.3): %s\n", documenturi);
			documenttype_real = strdup("none");
			
		}
	}
	else {
		documenttype_real = malloc(strlen(documenttype)+1);
		strcpy(documenttype_real,documenttype);
	}



	//hvis vi ikke her med noen egen doctype så bruker vi den vi har fått via documenttype
	if (doctype[0] == '\0') {
		//vi trenger ikke å ha \0 på slutten her
		strncpy(ReposetoryHeader.doctype,documenttype_real,sizeof(ReposetoryHeader.doctype));
	}
	else {
		//vi trenger ikke å ha \0 på slutten her
		strncpy(ReposetoryHeader.doctype,doctype,sizeof(ReposetoryHeader.doctype));
	}

	documentbuffer = buffer_init(0);
	if (!bbdocument_convert(documenttype_real,document,dokument_size,documentbuffer,title,subname,documenturi, lastmodified,acl_allow, acl_denied, &metahash)) {

		printf("can't run bbdocument_convert\n");
		//lager en tom html buffer
		//Setter titelen som subjekt. Hva hvis vi ikke har title?
		htmlbuffersize = strlen(html_text_tempelate) + strlen(title) + 1;
		htmlbuffer = malloc(htmlbuffersize);
		snprintf(htmlbuffer, htmlbuffersize, html_text_tempelate,title,"");
		htmlbuffersize = strlen(htmlbuffer);
		printf("useing title \"%s\" as title\n",title);
		printf("htmlbuffersize %i\n",htmlbuffersize);
		free(buffer_abort(documentbuffer));
	} else {
		htmlbuffersize = buffer_length(documentbuffer);
		htmlbuffer = buffer_exit(documentbuffer);
	}

	buffer *attrbuffer = buffer_init(-1);
	bprintf(attrbuffer, "%s,", attributes);

	if (metahash) {
		struct hashtable_itr *itr;

		if (hashtable_count(metahash) > 0) {
			itr = hashtable_iterator(metahash);
			do {
				char *key, *value;

				key = hashtable_iterator_key(itr);
				value = hashtable_iterator_value(itr);

				printf("%s: Key: %s Value: %s\n", documenttype_real, key, value);

				if (strcmp(key, "lastmodified") == 0) {
					lastmodified = atol(value);
					continue;
				}

				bprintf(attrbuffer, "%s=%s,", key, value);
			} while (hashtable_iterator_advance(itr));

			free(itr);
		}
		hashtable_destroy(metahash, 1);
	}
	char *all_attributes = buffer_exit(attrbuffer);



	//prøver å lag et bilde
	//if ( (imagebuffer = generate_thumbnail( document, dokument_size, &imageSize )) == NULL ) {
	if (!bbdocument_makethumb(documenttype_real,document,dokument_size,&imagebuffer,&imageSize)) {
		printf("can't generate image\n");
		ReposetoryHeader.imageSize = 0;
		imagebuffer = NULL;
	}
	else {
		debug("generated image\n");
		ReposetoryHeader.imageSize = imageSize;
	}


	ReposetoryHeader.clientVersion = 2.14;

	//runarb:  8 juli 2008: tar bort bruken av ReposetoryHeader's url
	//runarb: 11 juli 2008: kan ikke gjøre dette, da vi kopierer den inn i DocumentIndex fra ReposetoryHeader 
	strncpy(ReposetoryHeader.url,documenturi,sizeof(ReposetoryHeader.url));
		

	ReposetoryHeader.response = 200;
	strcpy(ReposetoryHeader.content_type,"htm");

	ReposetoryHeader.acl_allowSize = strlen(acl_allow);
#ifdef IIACL
	ReposetoryHeader.acl_deniedSize = strlen(acl_denied);
#endif
	ReposetoryHeader.time = lastmodified;

	ReposetoryHeader.storageTime = 0; //setes automatisk av rApendPostcompress

#ifdef DEBUG
	printf("ACL was allow \"%s\", %i bytes, denied \"%s\", %i bytes\nsubname %s\n",acl_allow,ReposetoryHeader.acl_allowSize,acl_allow,ReposetoryHeader.acl_allowSize,subname);
#endif

	ReposetoryHeader.urllen = strlen(documenturi);
	ReposetoryHeader.attributeslen = strlen(all_attributes);
	rApendPostcompress(&ReposetoryHeader, htmlbuffer, imagebuffer, subname, acl_allow, acl_denied, NULL, documenturi, all_attributes, attrkeys, htmlbuffersize);

#ifdef DEBUG	
	printf("legger til DocID \"%u\", time \"%u\"\n",ReposetoryHeader.DocID,lastmodified);
	printf("htmlSize %u, imageSize %ho\n",ReposetoryHeader.htmlSize2,ReposetoryHeader.imageSize);
	printf("html: -%s-\n",htmlbuffer);
#endif

	uriindex_add(documenturi,ReposetoryHeader.DocID,lastmodified,subname);

	free(all_attributes);
	free(htmlbuffer);
	free(documenttype_real);

	if (imagebuffer != NULL) {
		free(imagebuffer);
	}

	return 1;
}	
Ejemplo n.º 30
0
TPM_RESULT VTPM_SaveManagerData(void) {
  TPM_RESULT status=TPM_SUCCESS;
  int fh, dmis=-1;

  BYTE *flat_boot_key=NULL, *flat_dmis=NULL, *flat_enc=NULL;
  buffer_t clear_flat_global=NULL_BUF, enc_flat_global=NULL_BUF;
  UINT32 storageKeySize = buffer_len(&vtpm_globals->storageKeyWrap);
  UINT32 bootKeySize = buffer_len(&vtpm_globals->bootKeyWrap);
  struct pack_buf_t storage_key_pack = {storageKeySize, vtpm_globals->storageKeyWrap.bytes};
  struct pack_buf_t boot_key_pack = {bootKeySize, vtpm_globals->bootKeyWrap.bytes};
  BYTE vtpm_manager_gen = VTPM_MANAGER_GEN;

  struct hashtable_itr *dmi_itr;
  VTPM_DMI_RESOURCE *dmi_res;

  UINT32 boot_key_size = 0, flat_dmis_size = 0;

  // Initially fill these with buffer sizes for each data type. Later fill
  // in actual size, once flattened.
  boot_key_size =  sizeof(UINT32) +       // bootkeysize
                   bootKeySize;           // boot key

  TPMTRYRETURN(buffer_init(&clear_flat_global,sizeof(BYTE) + // manager version
                                              3*sizeof(TPM_DIGEST) + // Auths
                                              sizeof(UINT32) +// storagekeysize
                                              storageKeySize, NULL) ); // storage key


  flat_boot_key = (BYTE *) malloc( boot_key_size );
  flat_enc = (BYTE *) malloc( sizeof(UINT32) );

  boot_key_size = BSG_PackList(flat_boot_key, 1,
                               BSG_TPM_SIZE32_DATA, &boot_key_pack);

  BSG_PackList(clear_flat_global.bytes, 4,
                BSG_TYPE_BYTE,    &vtpm_manager_gen,
                BSG_TPM_AUTHDATA, &vtpm_globals->owner_usage_auth,
                BSG_TPM_SECRET,   &vtpm_globals->storage_key_usage_auth,
                BSG_TPM_SIZE32_DATA, &storage_key_pack);

  TPMTRYRETURN(envelope_encrypt(&clear_flat_global,
                                &vtpm_globals->bootKey,
                                &enc_flat_global) );

  BSG_PackConst(buffer_len(&enc_flat_global), 4, flat_enc);

  // Per DMI values to be saved (if any exit)
  if (hashtable_count(vtpm_globals->dmi_map) > 1) {

    flat_dmis = (BYTE *) malloc( 
                     (hashtable_count(vtpm_globals->dmi_map) - 1) * // num DMIS (-1 for Dom0)
                     (sizeof(UINT32) +sizeof(BYTE) + 2*sizeof(TPM_DIGEST)) ); // Per DMI info

    dmi_itr = hashtable_iterator(vtpm_globals->dmi_map);
    do {
      dmi_res = (VTPM_DMI_RESOURCE *) hashtable_iterator_value(dmi_itr);
      dmis++;

      // No need to save dmi0.
      if (dmi_res->dmi_id == 0)
        continue;


      flat_dmis_size += BSG_PackList( flat_dmis + flat_dmis_size, 4,
                                        BSG_TYPE_UINT32, &dmi_res->dmi_id,
                                        BSG_TYPE_BYTE, &dmi_res->dmi_type,
                                        BSG_TPM_DIGEST, &dmi_res->NVM_measurement,
                                        BSG_TPM_DIGEST, &dmi_res->DMI_measurement);

    } while (hashtable_iterator_advance(dmi_itr));
  }

  fh = open(STATE_FILE, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
  if (fh == -1) {
    vtpmlogerror(VTPM_LOG_VTPM, "Unable to open %s file for write.\n", STATE_FILE);
    status = TPM_IOERROR;
    goto abort_egress;
  }

  if ( ( write(fh, flat_boot_key, boot_key_size) != boot_key_size ) ||
       ( write(fh, flat_enc, sizeof(UINT32)) != sizeof(UINT32) ) ||
       ( write(fh, enc_flat_global.bytes, buffer_len(&enc_flat_global)) != buffer_len(&enc_flat_global) ) ||
       ( write(fh, flat_dmis, flat_dmis_size) != flat_dmis_size ) ) {
    vtpmlogerror(VTPM_LOG_VTPM, "Failed to completely write service data.\n");
    status = TPM_IOERROR;
    goto abort_egress;
 }

  goto egress;

 abort_egress:
 egress:

  free(flat_boot_key);
  free(flat_enc);
  buffer_free(&enc_flat_global);
  free(flat_dmis);
  close(fh);

  vtpmloginfo(VTPM_LOG_VTPM, "Saved VTPM Manager state (status = %d, dmis = %d)\n", (int) status, dmis);
  return status;
}