/* Creates a new method */ duda_method_t *_duda_method_new(char *uid, char *cb_webservice, void (*cb_builtin)(duda_request_t *), int n_params) { duda_method_t *method; method = mk_api->mem_alloc(sizeof(duda_method_t)); method->uid = uid; method->uid_len = strlen(uid); method->num_params = n_params; if (cb_webservice) { method->callback = cb_webservice; method->cb_webservice = NULL; method->cb_builtin = NULL; } else { method->callback = NULL; method->cb_webservice = NULL; method->cb_builtin = cb_builtin; } mk_list_init(&method->params); return method; }
static struct mk_list *mk_dirhtml_create_list(DIR * dir, char *path, unsigned long *list_len) { char full_path[PATH_MAX]; struct mk_list *list; struct dirent *ent; struct mk_f_list *entry = 0; list = mk_api->mem_alloc(sizeof(struct mk_list)); mk_list_init(list); while ((ent = readdir(dir)) != NULL) { if ((ent->d_name[0] == '.') && (strcmp(ent->d_name, "..") != 0)) continue; /* Look just for files and dirs */ if (ent->d_type != DT_REG && ent->d_type != DT_DIR && ent->d_type != DT_LNK && ent->d_type != DT_UNKNOWN) { continue; } snprintf(full_path, PATH_MAX, "%s%s", path, ent->d_name); entry = mk_dirhtml_create_element(ent->d_name, ent->d_type, full_path, list_len); if (!entry) { continue; } mk_list_add(&entry->_head, list); } return list; }
int mk_epoll_state_init() { struct mk_list *state_list = mk_mem_malloc(sizeof(struct mk_list)); mk_list_init(state_list); return pthread_setspecific(mk_epoll_state_k, (void *) state_list); }
/* Creates a new interface */ duda_interface_t *duda_interface_new(char *uid) { duda_interface_t *iface; iface = mk_api->mem_alloc(sizeof(duda_interface_t)); iface->uid = uid; iface->uid_len = strlen(uid); mk_list_init(&iface->methods); return iface; }
/* invoked in thread context */ void mk_patas_conx_init() { struct mk_list *thread_conx_list; /* connection list */ thread_conx_list = mk_api->mem_alloc(sizeof(struct mk_list)); mk_list_init(thread_conx_list); /* set data to thread key */ mk_patas_conx_set(thread_conx_list); }
/* Create a new channel */ struct mk_channel *mk_channel_new(int type, int fd) { struct mk_channel *channel; channel = mk_mem_malloc(sizeof(struct mk_channel)); channel->type = type; channel->fd = fd; mk_list_init(&channel->streams); return channel; }
/* Creates a new method */ duda_method_t *duda_method_new(char *uid, char *callback, int n_params) { duda_method_t *method; method = mk_api->mem_alloc(sizeof(duda_method_t)); method->uid = uid; method->uid_len = strlen(uid); method->num_params = n_params; method->callback = callback; method->func_cb = NULL; mk_list_init(&method->params); return method; }
/* * @METHOD_NAME: chan_create * @METHOD_DESC: create a channel(pipe) for dthread communication. * @METHOD_PROTO: mk_thread_channel_t *chan_create(int size) * @METHOD_PARAM: size the buffered size of the channel. * @METHOD_RETURN: returns a new channel. */ struct mk_thread_channel *mk_thread_channel_create(int size) { struct mk_thread_channel *chan; chan = mk_mem_malloc(sizeof(*chan)); if (!chan) { return NULL; } chan->size = size + 1; chan->used = 0; mk_list_init(&chan->bufs); chan->sender = -1; chan->receiver = -1; chan->ended = 0; chan->done = 0; return chan; }
/* Creates a web service instance */ struct duda_service *duda_service_create(struct duda *d, char *root, char *log, char *data, char *html, char *service) { int ret; void *handle; struct duda_service *ds; struct duda_api_objects *api; if (!d) { return NULL; } /* Check access to root, log, data and html directories */ if (root) { ret = validate_dir(root); if (ret != 0) { return NULL; } } if (log) { ret = validate_dir(log); if (ret != 0) { return NULL; } } if (data) { ret = validate_dir(log); if (ret != 0) { return NULL; } } if (html) { ret = validate_dir(html); if (ret != 0) { return NULL; } } /* Validate the web service file */ handle = dlopen(service, RTLD_LAZY); if (!handle) { fprintf(stderr, "Error opening web service file '%s'\n", service); return NULL; } /* Create web service context */ ds = mk_mem_alloc_z(sizeof(struct duda_service)); if (!ds) { dlclose(handle); return NULL; } /* Root prefix path */ if (root) { ds->path_root = mk_string_dup(root); } /* * If root prefix path is set, for all incoming paths that are not * absolute, we prefix the path */ if (log) { ds->path_log = service_path(root, log); } if (data) { ds->path_data = service_path(root, data); } if (html) { ds->path_html = service_path(root, html); } if (service) { ds->path_service = service_path(root, service); } ds->dl_handle = handle; mk_list_add(&ds->_head, &d->services); /* Initialize references for API objects */ mk_list_init(&ds->router_list); api = duda_api_create(); map_internals(ds, api); return ds; }
/* Read configuration parameters */ int mk_patas_conf(char *confdir) { int res; int val_port; char *val_host; char *val_uri; unsigned long len; char *conf_path=NULL; struct mk_config_section *section; struct mk_config_entry *entry; struct mk_patas_node *node; /* Init nodes list */ mk_patas_nodes_list = mk_api->mem_alloc(sizeof(struct mk_list)); mk_list_init(mk_patas_nodes_list); /* Read configuration */ mk_api->str_build(&conf_path, &len, "%s/patas.conf", confdir); conf = mk_api->config_create(conf_path); section = mk_api->config_section_get(conf, "NODE"); while (section) { entry = section->entry; val_host = NULL; val_port = -1; val_uri = NULL; /* Get section values */ val_host = mk_api->config_section_getval(section, "IP", MK_CONFIG_VAL_STR); val_port = (int) mk_api->config_section_getval(section, "Port", MK_CONFIG_VAL_NUM); val_uri = mk_api->config_section_getval(section, "Uri", MK_CONFIG_VAL_LIST); if (val_host && val_uri && val_port > 0) { /* validate that node:ip is not pointing this server */ if (mk_patas_validate_node(val_host, val_port) < 0) { break; } /* alloc node */ node = mk_api->mem_alloc(sizeof(struct mk_patas_node)); node->host = val_host; node->port = val_port; /* pre-socket stuff */ node->sockaddr = mk_api->mem_alloc_z(sizeof(struct sockaddr_in)); node->sockaddr->sin_family = AF_INET; res = inet_pton(AF_INET, node->host, (void *) (&(node->sockaddr->sin_addr.s_addr))); if (res < 0) { mk_warn("Can't set remote->sin_addr.s_addr"); mk_api->mem_free(node->sockaddr); return -1; } else if (res == 0) { mk_err("Invalid IP address"); mk_api->mem_free(node->sockaddr); return -1; } node->sockaddr->sin_port = htons(node->port); /* add node to list */ PLUGIN_TRACE("Balance Node: %s:%i", val_host, val_port); mk_list_add(&node->_head, mk_patas_nodes_list); mk_patas_n_nodes++; } section = section->next; } mk_api->mem_free(conf_path); return 0; }
void cache_req_thread_init() { struct mk_list *pool = malloc(sizeof(struct mk_list)); mk_list_init(pool); pthread_setspecific(cache_req_pool, pool); }