Esempio n. 1
0
static int put_zfs_value (kstat_t *ksp, char const *k, value_t v)
{
	llentry_t *e;
	char *k_copy;
	value_t *v_copy;

	k_copy = strdup (k);
	if (k_copy == NULL)
		return ENOMEM;

	v_copy = malloc (sizeof (*v_copy));
	if (v_copy == NULL)
	{
		sfree (k_copy);
		return ENOMEM;
	}
	*v_copy = v;

	e = llentry_create (k_copy, v_copy);
	if (e == NULL)
	{
		sfree (v_copy);
		sfree (k_copy);
		return ENOMEM;
	}

	llist_append (ksp, e);
	return 0;
}
Esempio n. 2
0
void hmap_set(struct hash_map *map, void *key, void *data)
{
	size_t hashed_key = map->hash_fn(key);
	struct linked_list *list = &map->buckets_array[hashed_key % map->num_buckets];
	struct ll_node *node;

	node = llist_search(list, _find_key, (void*)&hashed_key);
	if(!node) {
		if(!(node = llist_append(list))) {
			return;
		}
	}

	/* set data */
	{
		struct keydata_pair *kdp;

		if(!node->data) {
			if(!(node->data = (struct keydata_pair*)malloc(sizeof(struct keydata_pair)))) {
				llist_remove(list, node, NULL, NULL);
				ERROR("hmap_set: out of memory!");
				return;
			}
		}

		kdp = (struct keydata_pair*)node->data;
		kdp->hashed_key = hashed_key;
		kdp->data = data;
	}
}
Esempio n. 3
0
static int register_callback (llist_t **list, /* {{{ */
		const char *name, callback_func_t *cf)
{
	llentry_t *le;
	char *key;

	if (*list == NULL)
	{
		*list = llist_create ();
		if (*list == NULL)
		{
			ERROR ("plugin: register_callback: "
					"llist_create failed.");
			destroy_callback (cf);
			return (-1);
		}
	}

	key = strdup (name);
	if (key == NULL)
	{
		ERROR ("plugin: register_callback: strdup failed.");
		destroy_callback (cf);
		return (-1);
	}

	le = llist_search (*list, name);
	if (le == NULL)
	{
		le = llentry_create (key, cf);
		if (le == NULL)
		{
			ERROR ("plugin: register_callback: "
					"llentry_create failed.");
			free (key);
			destroy_callback (cf);
			return (-1);
		}

		llist_append (*list, le);
	}
	else
	{
		callback_func_t *old_cf;

		old_cf = le->value;
		le->value = cf;

		WARNING ("plugin: register_callback: "
				"a callback named `%s' already exists - "
				"overwriting the old entry!", name);

		destroy_callback (old_cf);
		sfree (key);
	}

	return (0);
} /* }}} int register_callback */
Esempio n. 4
0
int sql_append_map (struct sql_expr *expr, char *alias)
{
	struct sql_map *map;
	map = type_alloc(struct sql_map);
	map->expr = expr;
	map->alias = alias;
	llist_append(&query.map, map);
	return 0;
}
Esempio n. 5
0
int sql_append_group (struct sql_source *src, int fld)
{
	struct sql_group *grp;
	grp = type_alloc(struct sql_group);
	grp->source = src;
	grp->field = fld;
	llist_append(&query.group, grp);
	return 0;
}
Esempio n. 6
0
int sql_append_aggre_resolved (aggre_t *func, struct sql_source *src, int fld)
{
	struct sql_aggre *aggre;
	aggre = type_alloc(struct sql_aggre);
	aggre->aggre = func;
	aggre->source = src;
	aggre->field = fld;
	llist_append(&(query.aggre), aggre);
	return 0;
}
Esempio n. 7
0
int sql_append_aggre (char *func, char *src, char *fld)
{
	struct sql_aggre *aggre;
	aggre = type_alloc(struct sql_aggre);
	aggre->naggre = func;
	aggre->nsource = src;
	aggre->nfield = fld;
	llist_append(&(query.aggre), aggre);
	return 0;
}
Esempio n. 8
0
void
string_hash_set (StringHash *hash, char *key, char *value)
{
	StringHashItem *item;

	item = (StringHashItem *) llist_append ((LList *) hash);
	item->key = key;
	item->hash = calc_hash (key);
	item->value = value;
}
Esempio n. 9
0
//doesn't check for key collisions
void hash_map_insert(hash_map *hm, void *key, void *value) {
    int hash = hm->hash(key);
    assert(hash < hm->hashLength);

    hash_item *item = malloc(sizeof(hash_item));
    item->key = key;
    item->value = value;

    llist_append(hm->boxes[hash], item);
}
int main()
{
	node entry = node_new(28);
	list head = NULL;

	int i = 0;
	while(i < 5)
	{
		llist_append(head,new_node(5*i++));
	}
	llist_append(head,entry);
	while(i < 8)
	{
		llist_append(head,new_node(5*i++));
	}

	llist_show(head);

	return 0;
}
Esempio n. 11
0
int sql_append_filter (func_t *func, struct sql_source *src1, int fld1, struct sql_source *src2, int fld2)
{
	struct sql_filter *filter;
	filter = type_alloc(struct sql_filter);
	filter->source = src1;
	filter->field = fld1;
	filter->source2 = src2;
	filter->field2 = fld2;
	filter->comp = func;
	llist_append(&(query.filter), filter);
	return 0;
}
Esempio n. 12
0
int sql_append_src_filter (func_t *comp, struct sql_source *src, int fld, void *data, type_t *type)
{
	struct sql_filter *filter;
	filter = type_alloc(struct sql_filter) ;
	filter->source = src;
	filter->field = fld;
	filter->data = data;
	filter->type = type;
	filter->comp = comp;
	llist_append(&(src->filter), filter);
	return 0;
}
Esempio n. 13
0
int cllist_append(cLinkedList *cllist, void *elem)
{
	int returnVal;

	pthread_mutex_lock(&(cllist->mutex));

	returnVal = llist_append(cllist->llist, elem);

	pthread_mutex_unlock(&(cllist->mutex));

	return returnVal;
}
Esempio n. 14
0
int llist_reorder( llist_t *list_in, llist_t *list_out )
{
	long i = list_in->begin;
	long j = 0;
	while( i != -1 && j < list_in->size )
	{
		if( llist_append( list_out, list_in->start[i].data ) < 0 )
			return -1;
		i = list_in->start[i].next;
		++j;
	}
	return 0;
}
Esempio n. 15
0
File: util.c Progetto: moxley/parse1
int list_push(struct list *list, void *item) {
  struct item *listitem;

  if (!list->first) {
    list->first = llist_newitem(item);
    list->last = list->first;
  }
  else {
    listitem = llist_newitem(item);
    llist_append(list->last, listitem);
    list->last = listitem;
  }
  return ++list->size;
}
Esempio n. 16
0
static int mr_config_add_meta_regex(llist_t **meta, /* {{{ */
                                    oconfig_item_t *ci) {
  char *meta_key;
  llentry_t *entry;
  mr_regex_t *re_head;
  int status;
  char buffer[1024];

  if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
      (ci->values[1].type != OCONFIG_TYPE_STRING)) {
    log_warn("`%s' needs exactly two string arguments.", ci->key);
    return -1;
  }

  if (*meta == NULL) {
    *meta = llist_create();
    if (*meta == NULL) {
      log_err("mr_config_add_meta_regex: llist_create failed.");
      return -1;
    }
  }

  meta_key = ci->values[0].value.string;
  entry = llist_search(*meta, meta_key);
  if (entry == NULL) {
    meta_key = strdup(meta_key);
    if (meta_key == NULL) {
      log_err("mr_config_add_meta_regex: strdup failed.");
      return -1;
    }
    entry = llentry_create(meta_key, NULL);
    if (entry == NULL) {
      log_err("mr_config_add_meta_regex: llentry_create failed.");
      sfree(meta_key);
      return -1;
    }
    /* meta_key and entry will now be freed by mr_free_match(). */
    llist_append(*meta, entry);
  }

  snprintf(buffer, sizeof(buffer), "%s `%s'", ci->key, meta_key);
  /* Can't pass &entry->value into mr_add_regex, so copy in/out. */
  re_head = entry->value;
  status = mr_add_regex(&re_head, ci->values[1].value.string, buffer);
  if (status == 0) {
    entry->value = re_head;
  }
  return status;
} /* }}} int mr_config_add_meta_regex */
Esempio n. 17
0
int sql_append_group_filter_pre (int cmp, struct sql_source *src, int fld, aggre_t *aggre, void *data, type_t *type)
{
	func_t *f;
	struct sql_group_filter *pre;
	pre = type_alloc(struct sql_group_filter);
	pre->aggre = aggre;
	pre->source = src;
	pre->field = fld;
	pre->type = type;
	pre->data = data;
	f = func_load(cmp2text(cmp), aggre->otype, type);
	if (f == NULL) sql_ok = 0;
	pre->comp = f;
	llist_append(&(query.aggre), pre);
	return 0;
}
Esempio n. 18
0
int streambuf_write(streambuf_t *p, struct streambuf_iov_st *iov)
{
	int ret=0;
	struct streambuf_st *buf=p;

	pthread_mutex_lock(&buf->mut);
	pthread_cleanup_push(mutex_unlock, &buf->mut);
	while (buf->nr_bytes >= buf->h_limit) {
		pthread_cond_wait(&buf->cond, &buf->mut);
	}
	llist_append(buf->ringbuf, iov);
	buf->nr_bytes += iov->len;
	pthread_cond_signal(&buf->cond);
	pthread_cleanup_pop(1);
	return ret;
}
Esempio n. 19
0
int main(){
    int D,N;
    scanf("%d%d",&D,&N);
    LList*peg_1=llist_new();
    LList*peg_2=llist_new();
    LList*peg_3=llist_new();
    int i=1;
    while(i<=D){
      peg_1=llist_append(peg_1,i);
      i++;
    }
    int num_of_steps = 0;

    num_of_steps = play_TOH(peg_1,peg_2,peg_3);
    printf(" No of steps= %d\n",num_of_steps);
    return 0;
}
Esempio n. 20
0
int streambuf_write_nb(streambuf_t *p, struct streambuf_iov_st *iov)
{
	int ret=0;
	struct streambuf_st *buf=p;

	pthread_mutex_lock(&buf->mut);
	pthread_cleanup_push(mutex_unlock, &buf->mut);
	if (buf->nr_bytes >= buf->h_limit) {
		ret = ENOMEM;
		goto quit;
	}
	llist_append(buf->ringbuf, iov);
	buf->nr_bytes += iov->len;
	pthread_cond_signal(&buf->cond);
quit:
	pthread_cleanup_pop(1);
	return ret;
}
Esempio n. 21
0
static void schan_members_join(struct llist_header *head, char *nick, char *uhost, char *user, char *chan)
{
	struct stats_member *m;
	char *host;
#ifndef NO_EGG
	struct chanset_t *eggchan;
#endif

	m = schan_members_create();
	m->nick = nmalloc(strlen(nick) + 1);
	strcpy(m->nick, nick);
	m->uhost = nmalloc(strlen(uhost) + 1);
	strcpy(m->uhost, uhost);
	m->joined = now;
	if (user) {
		m->user = findsuser_by_name(user);
		if (!m->user) {
			m->user = addsuser(user, now, now);
			debug1("Stats.Mod: Created suserrec for %s.", user);
		}
	} else {
		host = nmalloc(strlen(nick) + 1 + strlen(uhost) + 1);
		sprintf(host, "%s!%s", nick, uhost);
		m->user = findsuser(host);
		nfree(host);
	}
	if (m->user) {
		m->user->laston = now;
		m->stats = findlocstats(chan, m->user->user);
		if (!m->stats)
			m->stats = initstats(chan, m->user->user);
	}
#ifndef NO_EGG
	eggchan = findchan_by_dname(chan);
	if (chan)
		m->eggmember = ismember(eggchan, nick);
	if (!m->eggmember)
		debug2("Warning[stats.mod]: Couldn't find eggmember for %s in %s.", nick, chan);
#endif
	llist_append(head, (void *) m);
}
Esempio n. 22
0
llist *quadtree_retrieve_node(quadtree *q, quadtree_node *qn,
		quadtree_rect *r, llist *list) {
	if (!qn) {
		return list;
	}

	if (qn->points && !llist_is_empty(qn->points)) {
		quadtree_data *d = llist_first(qn->points);
		if (quadtree_rect_contains(r, d->x, d->y)) {
			llist_append(list, qn->points);
		}
	} else {
		for(int i = 0; i < 4; i++) {
			if (quadtree_rect_contains_node(r, qn->children[i])) {
				quadtree_retrieve_node(q, qn->children[i], r, list);
			}
		}
	}

	return list;
}
Esempio n. 23
0
int main() //@ : main
  //@ requires emp;
  //@ ensures emp;
{
  struct llist *l1 = create_llist();
  struct llist *l2 = create_llist();
  llist_add(l1, 10);
  llist_add(l1, 20);
  llist_add(l1, 30);
  llist_add(l2, 40);
  llist_add(l2, 50);
  llist_add(l2, 60);
  int x = llist_removeFirst(l2); assert(x == 40);
  llist_append(l1, l2);
  int n = llist_length(l1); assert(n == 5);
  int e0 = llist_lookup(l1, 0); assert(e0 == 10);
  int e1 = llist_lookup(l1, 1); assert(e1 == 20);
  int e2 = llist_lookup(l1, 2); assert(e2 == 30);
  int e3 = llist_lookup(l1, 3); assert(e3 == 50);
  int e4 = llist_lookup(l1, 4); assert(e4 == 60);
  llist_dispose(l1);
  return 0;
}
Esempio n. 24
0
static int cx_config_add_xpath (cx_t *db, oconfig_item_t *ci) /* {{{ */
{
  cx_xpath_t *xpath;
  char *name;
  llentry_t *le;
  int status;
  int i;

  xpath = malloc (sizeof (*xpath));
  if (xpath == NULL)
  {
    ERROR ("curl_xml plugin: malloc failed.");
    return (-1);
  }
  memset (xpath, 0, sizeof (*xpath));

  status = cf_util_get_string (ci, &xpath->path);
  if (status != 0)
  {
    cx_xpath_free (xpath);
    return (status);
  }

  /* error out if xpath->path is an empty string */
  if (strlen (xpath->path) == 0)
  {
    ERROR ("curl_xml plugin: invalid xpath. "
           "xpath value can't be an empty string");
    cx_xpath_free (xpath);
    return (-1);
  }

  status = 0;
  for (i = 0; i < ci->children_num; i++)
  {
    oconfig_item_t *child = ci->children + i;

    if (strcasecmp ("Type", child->key) == 0)
      status = cf_util_get_string (child, &xpath->type);
    else if (strcasecmp ("InstancePrefix", child->key) == 0)
      status = cf_util_get_string (child, &xpath->instance_prefix);
    else if (strcasecmp ("InstanceFrom", child->key) == 0)
      status = cf_util_get_string (child, &xpath->instance);
    else if (strcasecmp ("ValuesFrom", child->key) == 0)
      status = cx_config_add_values ("ValuesFrom", xpath, child);
    else
    {
      WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
      status = -1;
    }

    if (status != 0)
      break;
  } /* for (i = 0; i < ci->children_num; i++) */

  if (status != 0)
  {
    cx_xpath_free (xpath);
    return status;
  }

  if (xpath->type == NULL)
  {
    WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
    cx_xpath_free (xpath);
    return -1;
  }

  if (db->list == NULL)
  {
    db->list = llist_create();
    if (db->list == NULL)
    {
      ERROR ("curl_xml plugin: list creation failed.");
      cx_xpath_free (xpath);
      return (-1);
    }
  }

  name = strdup (xpath->path);
  if (name == NULL)
  {
    ERROR ("curl_xml plugin: strdup failed.");
    cx_xpath_free (xpath);
    return (-1);
  }

  le = llentry_create (name, xpath);
  if (le == NULL)
  {
    ERROR ("curl_xml plugin: llentry_create failed.");
    cx_xpath_free (xpath);
    sfree (name);
    return (-1);
  }

  llist_append (db->list, le);
  return (0);
} /* }}} int cx_config_add_xpath */
Esempio n. 25
0
int main(int argc, char **argv)
{
    llist_t *paths,*src,*todo;
    set_t *incl;
    map_t *deps;

    if (argc < 2) {
        fprintf(stderr,"FastDep v%s for LAMMPS\n"
                "Usage: %s [-I <path> ...] -- <src1> [<src2> ...]\n",
                version,argv[0]);
        fprintf(stderr,"Supported extensions: %s, %s, %s\n",
                extensions[0], extensions[1], extensions[2]);
        return 1;
    }

    /* hash tables for all known included files and dependencies
     * we guesstimate a little over 2x as many entries as sources. */
    incl = set_init(2*argc);
    deps = map_init(2*argc);

    /* list of include search paths. prefixed by "." and "..". */
    paths = llist_init();
    llist_append(paths,".");
    llist_append(paths,"..");

    while (++argv, --argc > 0) {
        if (strncmp(*argv, "-I", 2) == 0) {
            if ((*argv)[2] != '\0') {
                llist_append(paths,trim_path(*argv+2));
            } else {
                ++argv;
                --argc;
                if (argc > 0) {
                    if (strcmp(*argv,"--") == 0) {
                        break;
                    } else {
                        llist_append(paths,trim_path(*argv));
                    }
                }
            }
        } else if (strcmp(*argv,"--") == 0) {
            break;
        } /* ignore all unrecognized arguments before '--'. */
    }

    src = llist_init();
    while (++argv, --argc > 0) {
        llist_append(src,*argv);
    }

    /* process files to look for includes */
    todo = llist_init();
    find_includes(src->head,todo,paths,incl,deps);
    find_includes(todo->head,todo,paths,incl,deps);
    llist_free(todo);

    fprintf(stdout,"# FastDep v%s for LAMMPS\n",version);
    fputs("# Search path: ",stdout);
    llist_print(paths);
    fprintf(stdout,"# % 5d sources\n# % 5d includes\n# % 5d depfiles\n",
            llist_size(src),set_size(incl),map_size(deps));

    set_free(incl);
    do_depend(src->head,deps);

    llist_free(src);
    llist_free(paths);
    map_free(deps);
    return 0;
}
Esempio n. 26
0
static void find_includes(llnode_t *head, llist_t *todo, llist_t *paths,
                   set_t *incl, map_t *deps)
{
    FILE *fp;
    llnode_t *tmp;
    char *buffer,*full,*ptr,*end;
    const char *file;

    buffer = (char *)malloc(4096);
    full = (char *)malloc(4096);

    tmp = head;
    while (tmp->next != NULL) {
        file = tmp->key;
        fp = fopen(file,"r");
        if (fp == NULL) {
            perror("Cannot read source");
            fprintf(stderr,"For file: %s\n",file);
            exit(EXIT_FAILURE);
        }

        /* read file line by line and look for #include "..." */
        while (!feof(fp) && !ferror(fp)) {
            if (fgets(buffer,4096,fp) == NULL) continue;
            ptr = buffer;
            while (*ptr == ' ' || *ptr == '\t') ++ptr;
            if (*ptr != '#') continue;
            while (*ptr == ' ' || *ptr == '\t') ++ptr;
            if (*++ptr != 'i') continue;
            if (*++ptr != 'n') continue;
            if (*++ptr != 'c') continue;
            if (*++ptr != 'l') continue;
            if (*++ptr != 'u') continue;
            if (*++ptr != 'd') continue;
            if (*++ptr != 'e') continue;
            ++ptr;
            while (*ptr == ' ' || *ptr == '\t') ++ptr;
            if (*ptr != '"') continue;
            ++ptr;
            end = ptr;
            while (*end != '"') {
                if (*end == '\0') {
                    fprintf(stderr,"Unmatched '\"': %s\n",buffer);
                    exit(EXIT_FAILURE);
                }
                ++end;
            }
            *end = '\0';

            /* get full path to include file */
            make_path(ptr,paths,full);
            /* skip, if not found or unreadable. */
            if (full[0] == '\0') continue;

            /* if this is a yet unknown include, add to the
             * todo list, if append is enabled */
            if (set_find(incl,full) == 0) {
                set_add(incl,full);
                llist_append(todo,full);
            }

            map_add(deps,file,full);
        }
        fclose(fp);
        tmp = tmp->next;
    }
    free(buffer);
    free(full);
}
Esempio n. 27
0
static int cx_config_add_xpath(cx_t *db, oconfig_item_t *ci) /* {{{ */
{
  cx_xpath_t *xpath = calloc(1, sizeof(*xpath));
  if (xpath == NULL) {
    ERROR("curl_xml plugin: calloc failed.");
    return -1;
  }

  int status = cf_util_get_string(ci, &xpath->path);
  if (status != 0) {
    cx_xpath_free(xpath);
    return status;
  }

  /* error out if xpath->path is an empty string */
  if (strlen(xpath->path) == 0) {
    ERROR("curl_xml plugin: invalid xpath. "
          "xpath value can't be an empty string");
    cx_xpath_free(xpath);
    return -1;
  }

  status = 0;
  for (int i = 0; i < ci->children_num; i++) {
    oconfig_item_t *child = ci->children + i;

    if (strcasecmp("Type", child->key) == 0)
      status = cf_util_get_string(child, &xpath->type);
    else if (strcasecmp("InstancePrefix", child->key) == 0)
      status = cf_util_get_string(child, &xpath->instance_prefix);
    else if (strcasecmp("InstanceFrom", child->key) == 0)
      status = cf_util_get_string(child, &xpath->instance);
    else if (strcasecmp("PluginInstanceFrom", child->key) == 0)
      status = cf_util_get_string(child, &xpath->plugin_instance_from);
    else if (strcasecmp("ValuesFrom", child->key) == 0)
      status = cx_config_add_values("ValuesFrom", xpath, child);
    else {
      WARNING("curl_xml plugin: Option `%s' not allowed here.", child->key);
      status = -1;
    }

    if (status != 0)
      break;
  } /* for (i = 0; i < ci->children_num; i++) */

  if (status != 0) {
    cx_xpath_free(xpath);
    return status;
  }

  if (xpath->type == NULL) {
    WARNING("curl_xml plugin: `Type' missing in `xpath' block.");
    cx_xpath_free(xpath);
    return -1;
  }

  if (xpath->values_len == 0) {
    WARNING("curl_xml plugin: `ValuesFrom' missing in `xpath' block.");
    cx_xpath_free(xpath);
    return -1;
  }

  llentry_t *le = llentry_create(xpath->path, xpath);
  if (le == NULL) {
    ERROR("curl_xml plugin: llentry_create failed.");
    cx_xpath_free(xpath);
    return -1;
  }

  llist_append(db->xpath_list, le);
  return 0;
} /* }}} int cx_config_add_xpath */
Esempio n. 28
0
static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */
{
  char *socket_temp;

  list_item_t *item;
  int status;
  int i;

  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
  {
    WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
	ci->key);
    return (-1);
  }

  item = (list_item_t *) malloc (sizeof (list_item_t));
  if (item == NULL)
  {
    ERROR ("powerdns plugin: malloc failed.");
    return (-1);
  }
  memset (item, '\0', sizeof (list_item_t));

  item->instance = strdup (ci->values[0].value.string);
  if (item->instance == NULL)
  {
    ERROR ("powerdns plugin: strdup failed.");
    sfree (item);
    return (-1);
  }

  /*
   * Set default values for the members of list_item_t
   */
  if (strcasecmp ("Server", ci->key) == 0)
  {
    item->server_type = SRV_AUTHORITATIVE;
    item->func = powerdns_read_server;
    item->socktype = SOCK_STREAM;
    socket_temp = strdup (SERVER_SOCKET);
  }
  else if (strcasecmp ("Recursor", ci->key) == 0)
  {
    item->server_type = SRV_RECURSOR;
    item->func = powerdns_read_recursor;
    item->socktype = SOCK_DGRAM;
    socket_temp = strdup (RECURSOR_SOCKET);
  }
  else
  {
    /* We must never get here.. */
    assert (0);
    return (-1);
  }

  status = 0;
  for (i = 0; i < ci->children_num; i++)
  {
    oconfig_item_t *option = ci->children + i;

    if (strcasecmp ("Collect", option->key) == 0)
      status = powerdns_config_add_collect (item, option);
    else if (strcasecmp ("Socket", option->key) == 0)
      status = powerdns_config_add_string ("Socket", &socket_temp, option);
    else
    {
      ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
      status = -1;
    }

    if (status != 0)
      break;
  }

  while (status == 0)
  {
    llentry_t *e;

    if (socket_temp == NULL)
    {
      ERROR ("powerdns plugin: socket_temp == NULL.");
      status = -1;
      break;
    }

    item->sockaddr.sun_family = AF_UNIX;
    sstrncpy (item->sockaddr.sun_path, socket_temp,
      sizeof (item->sockaddr.sun_path));

    e = llentry_create (item->instance, item);
    if (e == NULL)
    {
      ERROR ("powerdns plugin: llentry_create failed.");
      status = -1;
      break;
    }
    llist_append (list, e);

    break;
  }

  if (status != 0)
  {
    sfree (item);
    return (-1);
  }

  DEBUG ("powerdns plugin: Add server: instance = %s;", item->instance);

  return (0);
} /* }}} int powerdns_config_add_server */
Esempio n. 29
0
int main() {
    running = 1;
    signal(SIGINT, interrupt_handler);
    printf("Starting server\n");

    if(!init_server_socket(&sock, PORT))
        return -1;

    //A linked list might not be the most efficient for this
    llist client_sessions;
    llist_init(&client_sessions);

    queue message_queue;
    queue_init(&message_queue);

    //TODO: initialise thread pool
    const int num_workers = 4;
    pthread_t workers[num_workers];
    for(int i=0; i<num_workers; i++) {
        pthread_create(&workers[i], NULL, worker_run, (void*)&message_queue);
    }
    
    //select stuff
    fd_set readsocketset;
    fd_set writesocketset;
    fd_set errorsocketset;
    struct timeval timeout;

    while(running) {
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;
        
        printf("1\n");
        build_socket_list(&client_sessions, &readsocketset);
        FD_ZERO(&readsocketset);
        FD_SET(sock, &readsocketset);

        int s = select(client_sessions.len, &readsocketset, 0, 0, &timeout);
        printf("2\n");
        if(s < 0) {
            printf("ERROR: Select error\n");
            exit(1);
        }
        //if we have a new connection create a new session
        if(FD_ISSET(sock, &readsocketset)) {
            int csock = check_for_connections(sock);
            session clientSession;
            session_init(&clientSession, csock);
            llist_append(&client_sessions, (void*)&clientSession);
        }

        printf("2\n");
        //check if each session exists in the read socket thingE
        llist_node *cur = client_sessions.head;
        while(cur != NULL) {
            int sock = ((session*)cur->data)->sock;
            //check readsocketset
            if(FD_ISSET(sock, &readsocketset)) 
                client_read_data((session*)cur->data);
            //check writesocketset
            //check errorset
            cur = cur->next;
        }

        //TODO:
        //parse the messages
        //add parsed message to the queue

        //send messages on the queue (Should this be done here?)
    }
    printf("Exiting..\n");
    
    //free memory
    llist_node *cur = client_sessions.head;
    while(cur != NULL) {
        session *sess = (session*)cur->data;
        session_end(sess);
        free(sess);
        cur = cur->next;
    }
    llist_free(&client_sessions);
    close(sock);

    pthread_exit(NULL);
}
Esempio n. 30
0
void func_test()
{
	char *s = "this is a test";
	char *s2 = "x";
	void *b;
	int i, j;
	llist_t *ll;

	/*
	 * test 1
	 */

	ll = llist_new(20);	
	for (i = 0; i < 4; i++) {
		if (i%3 == 0) {
			llist_append(ll, &s2[0]);
		}
		llist_append(ll, &s[i]);
	}
	
	llist_travel(ll, &show);
	printf("\n");
	for (i = 0; i < 4; i++) {
		llist_prepend(ll, &s[0]);
	}
	llist_travel(ll, &show);
	printf("\n");
	for (i = 0; i < 4; i++) {
		llist_prepend_nb(ll, &s[1]);
	}
	llist_travel(ll, &show);
	printf("\n");
	llist_fetch_head(ll, &b);
	llist_travel(ll, &show);
	printf("\n");
	llist_fetch_head(ll, &b);
	llist_travel(ll, &show);
	printf("\n");
	llist_delete(ll);
	
	printf("%stest1 successful%s\n", COR_BEGIN, COR_END);

	/*
	 * test 2
	 */

	ll = llist_new(1000);
	for (j = 0; j < 100; j++) {
		for (i = 0; i < 10; i++) {
			llist_append_nb(ll, &s[i]);
		}
	}
	printf("append done.\n");
	for (i = 0; i< 100; i++) {
		llist_fetch_head_nb(ll, &b);
		printf("%c", *(char *)b);
	}
	printf("\nthere are %d nodes\n", ll->nr_nodes);
	for (i = 0; i< 100; i++) {
		llist_get_head_nb(ll, &b);
		printf("%c", *(char *)b);
	}
	printf("\n");
	printf("%stest2 successful%s\n", COR_BEGIN, COR_END);
	printf("there are %d nodes\n", ll->nr_nodes);
	
	/*
	 * test 3
	 */
	void *ptr, *ptr2;
	llist_get_head_node_nb(ll, &ptr);
	for (i = 0; i< 100; i++) {
		printf("%c", *(char *)(((llist_node_t *)ptr)->ptr));
		ptr = llist_get_next_nb(ll, ptr);
	}
	printf("\nthere are %d nodes\n", ll->nr_nodes);
	llist_delete(ll);
	printf("%stest3 successful%s\n", COR_BEGIN, COR_END);
	return;
}