Beispiel #1
0
// This callback is called when data is readable on socket.
static void server_cb(EV_P_ ev_io *w, int revents) 
{
    puts("Socket has become readable");

    int client_fd;
    struct sock_ev_client* client;

    // since ev_io is the first member,
    // watcher 'w' has the address of the 
    // start of the sock_ev_serv struct
    struct sock_ev_serv* server = (struct sock_ev_serv*) w;

    while (1) {
        client_fd = accept(server->fd, NULL, NULL);

        if( client_fd == -1 ) {
            if( errno != EAGAIN && errno != EWOULDBLOCK ) {
                printf("accept() failed errno=%i (%s)",  errno, strerror(errno));
                exit(EXIT_FAILURE);
            }
            break;
        }

        puts("accepted a client");

        client = client_new(client_fd);
        client->server = server;
        client->index = array_push(&server->clients, client);
        ev_io_start(EV_A_ &client->io);
    }
}
Beispiel #2
0
static bool read_input() {
  char* line = read_line(stdin);
  userlog(LOG_DEBUG, "input: %s", (line ? line : "<null>"));

  if (line == NULL || strcmp(line, "EXIT") == 0) {
    return false;
  }

  if (strcmp(line, "ROOTS") == 0) {
    array* new_roots = array_create(20);
    CHECK_NULL(new_roots, false);

    while (1) {
      line = read_line(stdin);
      userlog(LOG_DEBUG, "input: %s", (line ? line : "<null>"));
      if (line == NULL || strlen(line) == 0) {
        return false;
      }
      else if (strcmp(line, "#") == 0) {
        break;
      }
      else {
        int l = strlen(line);
        if (l > 1 && line[l-1] == '/')  line[l-1] = '\0';
        CHECK_NULL(array_push(new_roots, strdup(line)), false);
      }
    }

    return update_roots(new_roots);
  }

  return true;
}
Beispiel #3
0
static rstatus_t
memcache_append_key(struct msg *r, uint8_t *key, uint32_t keylen)
{
    struct mbuf *mbuf;
    struct keypos *kpos;

    mbuf = msg_ensure_mbuf(r, keylen + 2);
    if (mbuf == NULL) {
        return NC_ENOMEM;
    }

    kpos = array_push(r->keys);
    if (kpos == NULL) {
        return NC_ENOMEM;
    }

    kpos->start = mbuf->last;
    kpos->end = mbuf->last + keylen;
    mbuf_copy(mbuf, key, keylen);
    r->mlen += keylen;

    mbuf_copy(mbuf, (uint8_t *)" ", 1);
    r->mlen += 1;
    return NC_OK;
}
Beispiel #4
0
rstatus_t
conf_server_each_transform(void *elem, void *data)
{
    struct conf_server *cs = elem;
    struct array *server = data;
    struct server *s;

    ASSERT(cs->valid);

    s = array_push(server);
    ASSERT(s != NULL);

    s->idx = array_idx(server, s);
    s->owner = NULL;

    s->pname = cs->pname;
    s->name = cs->name;
    s->port = (uint16_t)cs->port;
    s->weight = (uint32_t)cs->weight;

    s->family = cs->info.family;
    s->addrlen = cs->info.addrlen;
    s->addr = (struct sockaddr *)&cs->info.addr;

    s->ns_conn_q = 0;
    TAILQ_INIT(&s->s_conn_q);

    s->next_retry = 0LL;
    s->failure_count = 0;

    log_debug(LOG_VERB, "transform to server %"PRIu32" '%.*s'",
              s->idx, s->pname.len, s->pname.data);

    return NC_OK;
}
Beispiel #5
0
void _ShaderNode_add_input_link(const char* _file, euint32 _line, ShaderNode _sn, ShaderObject _so, euint32 _array_index)
{
    ShaderObject so = ShaderObject_clone(_so);
    ShaderObject_set_index(so, _array_index);
    EAssert (_ShaderNode_test_input_link(_file, _line, _sn, so), "%s", "test input link fail");
    _sn->input_links = array_push(_sn->input_links, so);
}
Beispiel #6
0
void
cmd_list_create(struct response *rsp, struct request *req, struct command *cmd)
{
    struct item *it;
    struct bstring *key = _get_key(req);
    struct element *reply = (struct element *)array_push(rsp->token);

    INCR(process_metrics, list_create);

    it = _add_key(rsp, key);
    if (it == NULL) {
        log_debug("command '%.*s' '%.*s' failed: cannot store", cmd->bstr.len,
                cmd->bstr.data, key->len, key->data);
        return;
    }

    /* initialize data structure */
    ziplist_reset((ziplist_p)item_data(it));
    it->vlen = ZIPLIST_HEADER_SIZE;

    /* link into index */
    item_insert(it, key);

    rsp->type = reply->type = ELEM_STR;
    reply->bstr = str2bstr(RSP_OK);

    log_verb("command '%.*s' '%.*s' succeeded", cmd->bstr.len, cmd->bstr.data,
            key->len, key->data);
}
Beispiel #7
0
static rstatus_t
conf_push_scalar(struct conf *cf)
{
    rstatus_t status;
    struct string *value;
    uint8_t *scalar;
    uint32_t scalar_len;

    scalar = cf->event.data.scalar.value;
    scalar_len = (uint32_t)cf->event.data.scalar.length;

    log_debug(LOG_VVERB, "push '%.*s'", scalar_len, scalar);

    value = array_push(&cf->arg);
    if (value == NULL) {
        return NC_ENOMEM;
    }
    string_init(value);

    status = string_copy(value, scalar, scalar_len);
    if (status != NC_OK) {
        array_pop(&cf->arg);
        return status;
    }

    return NC_OK;
}
Beispiel #8
0
static rstatus_t
stats_pool_map(struct array *stats_pool, struct array *server_pool)
{
    rstatus_t status;
    uint32_t i, npool;

    npool = array_n(server_pool);
    ASSERT(npool != 0);

    status = array_init(stats_pool, npool, sizeof(struct stats_pool));
    if (status != NC_OK) {
        return status;
    }

    for (i = 0; i < npool; i++) {
        struct server_pool *sp = array_get(server_pool, i);
        struct stats_pool *stp = array_push(stats_pool);

        status = stats_pool_init(stp, sp);
        if (status != NC_OK) {
            return status;
        }
    }

    log_debug(LOG_VVVERB, "map %"PRIu32" stats pools", npool);

    return NC_OK;
}
Beispiel #9
0
char *
conf_add_group(struct conf *cf, struct command *cmd, void *conf)
{
    rstatus_t status;
    uint8_t *p;
    struct array *a;
    struct string *field, *value;

    p = conf;
    a = (struct array *)(p + cmd->offset);

    field = array_push(a);
    if (field == NULL) {
        return CONF_ERROR;
    }

    string_init(field);

    value = array_top(&cf->arg);

    status = string_duplicate(field, value);
    if (status != NC_OK) {
        return CONF_ERROR;
    }

    return CONF_OK;
}
Beispiel #10
0
static struct node *
gossip_add_node_to_rack(struct server_pool *sp, struct string *dc, struct gossip_rack *g_rack,
		struct string *address, struct string *ip, struct string *port, struct dyn_token *token)
{
	rstatus_t status;
	log_debug(LOG_VERB, "gossip_add_node_to_rack : dc[%.*s] rack[%.*s] address[%.*s] ip[%.*s] port[%.*s]",
			dc->len, dc->data, g_rack->name, address->len, address->data, ip->len, ip->data, port->len, port->data);


	int port_i = dn_atoi(port->data, port->len);
	if (port_i == 0) {
		return NULL; //bad data
	}

	struct node *gnode = (struct node *) array_push(&g_rack->nodes);
	node_init(gnode);
	status = string_copy(&gnode->dc, dc->data, dc->len);
	status = string_copy(&gnode->rack, g_rack->name.data, g_rack->name.len);
	status = string_copy(&gnode->name, ip->data, ip->len);
	status = string_copy(&gnode->pname, address->data, address->len); //ignore the port for now
	gnode->port = port_i;

	struct dyn_token * gtoken = &gnode->token;
	copy_dyn_token(token, gtoken);

	g_rack->nnodes++;

	//add into dicts
	dictAdd(g_rack->dict_name_nodes, &gnode->name, gnode);
	dictAdd(g_rack->dict_token_nodes, token_to_string(token), gnode);

	return gnode;
}
Beispiel #11
0
rstatus_t
conf_server_each_transform(void *elem, void *data)
{
    struct string *group = elem;
    struct array *server = data;
    struct server *s;

    s = array_push(server);
    ASSERT(s != NULL);

    s->idx = array_idx(server, s);
    s->owner = NULL;

    string_init(&s->pname);
    s->name = *group;/* ref */
    string_init(&s->addrstr);
    s->port = 0;
    s->weight = 1;
    memset(&s->info, 0, sizeof(s->info));
    s->ns_conn_q = 0;
    TAILQ_INIT(&s->s_conn_q);
    s->timer = NULL;
    s->status = 0; 

    log_debug(LOG_VERB, "transform to server %"PRIu32" '%.*s'",
              s->idx, group->len, group->data);

    return NC_OK;
}
Beispiel #12
0
iridium_method(File, each_line) {
  object self = local(self);
  object filename = local(filename); // From self
  object fn = local(fn);
  object str = NULL;
  FILE * f = get_file(context, self);
  size_t file_size = file_length(context, f, filename);
  char * buffer = GC_MALLOC((file_size+1)*sizeof(char));
  assert(buffer);
  int nchars;
  char * line = NULL;

  while ((nchars = getline(&buffer, &file_size, f)) != -1) {
    // Remove the newline, if present
    if (buffer[nchars-1] == '\n') {
      buffer[nchars-1] = 0;
    }
    line = GC_MALLOC((nchars + 1) * sizeof(char));
    assert(line);
    strncpy(line, buffer, nchars);
    str = IR_STRING(line);
    calls(context, fn, array_push(array_new(), str));
  }
  return NIL;
}
Beispiel #13
0
int engine_register(struct engine *engine, const char *name, const char *precedence, const char* ref)
{
	if (engine == NULL || name == NULL) {
		return kr_error(EINVAL);
	}
	/* Make sure module is unloaded */
	(void) engine_unregister(engine, name);
	/* Find the index of referenced module. */
	module_array_t *mod_list = &engine->modules;
	size_t ref_pos = mod_list->len;
	if (precedence && ref) {
		ref_pos = module_find(mod_list, ref);
		if (ref_pos >= mod_list->len) {
			return kr_error(EIDRM);
		}
	}
	/* Attempt to load binary module */
	struct kr_module *module = malloc(sizeof(*module));
	if (!module) {
		return kr_error(ENOMEM);
	}
	module->data = engine;
	int ret = kr_module_load(module, name, NULL);
	/* Load Lua module if not a binary */
	if (ret == kr_error(ENOENT)) {
		ret = ffimodule_register_lua(engine, module, name);
	}
	if (ret != 0) {
		free(module);
		return ret;
	}
	if (array_push(engine->modules, module) < 0) {
		engine_unload(engine, module);
		return kr_error(ENOMEM);
	}
	/* Evaluate precedence operator */
	if (precedence) {
		struct kr_module **arr = mod_list->at;
		size_t emplacement = mod_list->len;
		if (strcasecmp(precedence, ">") == 0) {
			if (ref_pos + 1 < mod_list->len)
				emplacement = ref_pos + 1; /* Insert after target */
		}
		if (strcasecmp(precedence, "<") == 0) {
			emplacement = ref_pos; /* Insert at target */
		}
		/* Move the tail if it has some elements. */
		if (emplacement + 1 < mod_list->len) {
			memmove(&arr[emplacement + 1], &arr[emplacement], sizeof(*arr) * (mod_list->len - (emplacement + 1)));
			arr[emplacement] = module;
		}
	}

	/* Register properties */
	if (module->props || module->config) {
		return register_properties(engine, module);
	}

	return kr_ok();
}
Beispiel #14
0
static bool unwatchable_mounts(array* mounts) {
  FILE* mtab = fopen("/etc/mtab", "r");
  if (mtab == NULL) {
    mtab = fopen("/proc/mounts", "r");
  }
  if (mtab == NULL) {
    userlog(LOG_ERR, "neither /etc/mtab nor /proc/mounts can be read");
    return false;
  }

  char* line;
  while ((line = read_line(mtab)) != NULL) {
    userlog(LOG_DEBUG, "mtab: %s", line);
    char* dev = strtok(line, MTAB_DELIMS);
    char* point = strtok(NULL, MTAB_DELIMS);
    char* fs = strtok(NULL, MTAB_DELIMS);

    if (dev == NULL || point == NULL || fs == NULL) {
      userlog(LOG_ERR, "can't parse mount line");
      return false;
    }

    if (!is_watchable(dev, point, fs)) {
      CHECK_NULL(array_push(mounts, strdup(point)), false);
    }
  }

  fclose(mtab);
  return true;
}
Beispiel #15
0
		VALUE array_push(const CallFrame* here, VALUE self, VALUE it) {
			ObjectPtr<Array> array = self;
			if (array == NULL) return NULL;
		// TODO: Check for recursion?
			array_push(array, it);
			return self;
		}
Beispiel #16
0
static rstatus_t
stats_server_map(struct array *stats_server, struct array *server)
{
    rstatus_t status;
    uint32_t i, nserver;

    nserver = array_n(server);
    ASSERT(nserver != 0);

    status = array_init(stats_server, nserver, sizeof(struct stats_server));
    if (status != NC_OK) {
        return status;
    }

    for (i = 0; i < nserver; i++) {
        struct server *s = array_get(server, i);
        struct stats_server *sts = array_push(stats_server);

        status = stats_server_init(sts, s);
        if (status != NC_OK) {
            return status;
        }
    }

    log_debug(LOG_VVVERB, "map %"PRIu32" stats servers", nserver);

    return NC_OK;
}
Beispiel #17
0
struct datacenter *
server_get_dc(struct server_pool *pool, struct string *dcname)
{
	struct datacenter *dc;
	uint32_t i, len;

	if (log_loggable(LOG_DEBUG)) {
		log_debug(LOG_DEBUG, "server_get_dc pool  '%.*s'",
				dcname->len, dcname->data);
	}

	for (i = 0, len = array_n(&pool->datacenters); i < len; i++) {
		dc = (struct datacenter *) array_get(&pool->datacenters, i);
		ASSERT(dc != NULL);
		ASSERT(dc->name != NULL);

		if (string_compare(dc->name, dcname) == 0) {
			return dc;
		}
	}

	dc = array_push(&pool->datacenters);
	dc_init(dc);
	string_copy(dc->name, dcname->data, dcname->len);

	if (log_loggable(LOG_DEBUG)) {
		log_debug(LOG_DEBUG, "server_get_dc pool about to exit  '%.*s'",
				dc->name->len, dc->name->data);
	}

	return dc;
}
Beispiel #18
0
file_socket * new_file_socket(char *filename) {
	struct stat st;
	if (stat(filename, &st) == -1) {
		printf("Error accessing %s: %s\n", filename, strerror(errno));
		return NULL;
	}

	file_socket * s = malloc(sizeof(file_socket));
	s->type = SOCKTYPE_FILE;

	s->fd = open(filename, O_RDONLY | O_NOCTTY);
	if (s->fd == -1) {
		printf("Error opening %s: %s\n", filename, strerror(errno));
		free(s);
		return NULL;
	}

	s->filesize = st.st_size;

	ringbuffer_init(&s->rxbuffer);

	memset(&s->starttime, 0, sizeof(struct timeval));
	memset(&s->lastpausetime, 0, sizeof(struct timeval));
	memset(&s->pausedtime, 0, sizeof(struct timeval));

	s->paused = 1;
	s->eof = 0;

	errorsockets = array_push(errorsockets, s);

	return s;
}
Beispiel #19
0
void set_argv(static_context_t *this_context, int argc, char **argv)
{
	array_t *ARGV = array_new();
	int i;
	for (i = 0; i < argc; i++)
		array_push(this_context, ARGV, string_new_cstr(argv[i]));
	send2(Object, s_const_set, intern("ARGV"), ARGV);
}
Beispiel #20
0
int edb_array_push(obj_handle *h, void* data) {
  LOG_HERE;
  int err = 0;
  u32 root;
  CHECK(obj_write(h, &root));
  CHECK(array_push(h->db, root, data));
  err: return err;
}
Beispiel #21
0
void test_push_grow_array(void)
{
  array_t *a;
  
  a = array_create(2, sizeof(char));
  
  array_push(a);
  array_push(a);
  
  assert_equal(2, a->nalloc);
  
  array_push(a);
  
  assert_equal(4, a->nalloc);
  
  array_destroy(a);
}
Beispiel #22
0
static unsigned int* _random_allocate_buffer( void )
{
	unsigned int* buffer = memory_allocate( sizeof( unsigned int ) * ( RANDOM_STATE_SIZE + 1 ), 0, MEMORY_PERSISTENT );
	_random_seed_buffer( buffer );
	buffer[RANDOM_STATE_SIZE] = 0;
	array_push( _random_state, buffer );
	return buffer;
}
Beispiel #23
0
listening_t * conn_listening_add(array_t *listening, pool_t *pool, 
    log_t *log, in_addr_t addr, in_port_t port, event_handler_pt handler,
    int rbuff_len, int sbuff_len)
{
    uchar_t             *address = NULL;
    listening_t        *ls = NULL;
    struct sockaddr_in *sin = NULL;

    if (!listening || !pool || !log ||  (port <= 0)) {
        return NULL;
    }
    //alloc sin addr
    sin = pool_alloc(pool, sizeof(struct sockaddr_in));
    if (!sin) {
        fast_log_error(log, FAST_LOG_ALERT, 0,
            "conn_listening_add: pooll alloc sockaddr failed");
        return NULL;
    }
    sin->sin_family = AF_INET;
    sin->sin_addr.s_addr = addr;
    sin->sin_port = htons(port);
    address = (uchar_t *)inet_ntoa(sin->sin_addr);
    //push listening socket
    ls = array_push(listening);
    if (!ls) {
        fast_log_error(log, FAST_LOG_ALERT, 0,
            "conn_listening_add: push listening socket failed!");
        return NULL;
    }
    memory_zero(ls, sizeof(listening_t));
    ls->addr_text.data = pool_calloc(pool,
        INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1);
    if (!ls->addr_text.data) {
        fast_log_error(log, FAST_LOG_ALERT, 0,
            "conn_listening_add: pool alloc ls->addr text failed");
        return NULL;
    }
    ls->addr_text.len = string_xxsprintf(ls->addr_text.data,
        "%s:%d", address, port) - ls->addr_text.data;
    ls->fd = FAST_INVALID_FILE;
    ls->family = AF_INET;
    ls->type = SOCK_STREAM;
    ls->sockaddr = (struct sockaddr *) sin;
    ls->socklen = sizeof(struct sockaddr_in);
    //config from config file
    ls->backlog = CONN_DEFAULT_BACKLOG;
   	ls->rcvbuf = rbuff_len > CONN_DEFAULT_RCVBUF? rbuff_len: CONN_DEFAULT_RCVBUF;
    ls->sndbuf = sbuff_len > CONN_DEFAULT_SNDBUF? sbuff_len: CONN_DEFAULT_SNDBUF;
	
    //connection pool size
    ls->conn_psize = CONN_DEFAULT_POOL_SIZE;
    ls->log = log;
    ls->handler = handler;
    ls->open = 0;
    ls->linger = 1;

    return ls;
}
Beispiel #24
0
/**********************************************************************
 * array_insert
 *
 * Insert a data element into a particular spot in the array.  Move all
 * the elements in the array (past that spot) down one to make room for
 * the new element.
 **********************************************************************/
ARRAY array_insert(ARRAY array, int index, void *value) {
    int x;

    array = array_push (array, NULL);
    for (x = array_count (array) - 1; x > index; x--)
        array_value (array, x) = array_value (array, x - 1);
    array_value (array, index) = value;
    return (array);
}
Beispiel #25
0
void process_set_arguments( process_t* proc, const char** args, unsigned int num )
{
	unsigned int ia;
	if( !proc )
		return;
	string_array_deallocate( proc->args );
	for( ia = 0; ia < num; ++ia )
		array_push( proc->args, string_clone( args[ia] ) );
}
Beispiel #26
0
void array_append(array_t* input,array_t* peer)
{
	arrayelement_t* element=peer->first;
	while(element)
	{
		array_push(input,element->data);
		element=element->next;
	}
}
Beispiel #27
0
static void run_self_test() {
  array* test_roots = array_create(1);
  char* cwd = malloc(PATH_MAX);
  if (getcwd(cwd, PATH_MAX) == NULL) {
    strncpy(cwd, ".", PATH_MAX);
  }
  array_push(test_roots, cwd);
  update_roots(test_roots);
}
Beispiel #28
0
void
ffi_pool_add_server(struct server_pool *pool, struct server *server) {
    struct server **s;

    s = array_push(&pool->ffi_server);
    *s = server;

    log_debug(LOG_NOTICE, "prepare to add server %s", server->name.data);
}
Beispiel #29
0
static int add_watch(const char* path, watch_node* parent) {
  int wd = inotify_add_watch(inotify_fd, path, IN_MODIFY | IN_ATTRIB | IN_CREATE | IN_DELETE | IN_MOVE | IN_DELETE_SELF);
  if (wd < 0) {
    if (errno == ENOSPC) {
      limit_reached = true;
    }
    userlog(LOG_ERR, "inotify_add_watch(%s): %s", path, strerror(errno));
    return ERR_CONTINUE;
  }
  else {
    userlog(LOG_DEBUG, "watching %s: %d", path, wd);
  }

  watch_node* node = table_get(watches, wd);
  if (node != NULL) {
    if (node->wd != wd || strcmp(node->name, path) != 0) {
      char buf1[PATH_MAX], buf2[PATH_MAX];
      const char* normalized1 = realpath(node->name, buf1);
      const char* normalized2 = realpath(path, buf2);
      if (normalized1 == NULL || normalized2 == NULL || strcmp(normalized1, normalized2) != 0) {
        userlog(LOG_ERR, "table error: collision at %d (new %s, existing %s)", wd, path, node->name);
        return ERR_ABORT;
      }
      else {
        userlog(LOG_WARNING, "intersection at %d: (new %s, existing %s, real %s)", wd, path, node->name, normalized1);
        return ERR_IGNORE;
      }
    }

    return wd;
  }

  node = malloc(sizeof(watch_node));

  CHECK_NULL(node);
  node->name = strdup(path);
  CHECK_NULL(node->name);
  node->wd = wd;
  node->parent = parent;
  node->kids = NULL;

  if (parent != NULL) {
    if (parent->kids == NULL) {
      parent->kids = array_create(DEFAULT_SUBDIR_COUNT);
      CHECK_NULL(parent->kids);
    }
    CHECK_NULL(array_push(parent->kids, node));
  }

  if (table_put(watches, wd, node) == NULL) {
    userlog(LOG_ERR, "table error: unable to put (%d:%s)", wd, path);
    return ERR_ABORT;
  }

  return wd;
}
Beispiel #30
0
array_t* array_from_vector(vector_t* vector)
{
	size_t i;
	array_t* output=array_new();
	for(i=0;i<vector->length;i++)
	{
		array_push(output,vector_get(vector,i));
	}
	return output;
}