static char * lxl_dns_core_listen(lxl_conf_t *cf, lxl_command_t *cmd, void *conf) { size_t off; lxl_uint_t i, nelts; lxl_url_t u; lxl_str_t *value; in_port_t port; struct sockaddr *sa; struct sockaddr_in *sin; lxl_dns_listen_t *ls; lxl_dns_core_main_conf_t *cmcf; value = lxl_array_elts(cf->args); u.url = value[1]; u.listen = 1; if (lxl_parse_url(cf->pool, &u) != 0) { if (u.err) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "%s in \"%s\" of the \"listen\" directive", u.err, u.url.data); } return LXL_CONF_ERROR; } cmcf = lxl_dns_conf_get_module_main_conf(cf, lxl_dns_core_module); ls = lxl_array_elts(&cmcf->listens); nelts = lxl_array_nelts(&cmcf->listens); for (i = 0; i < nelts; ++i) { sa = (struct sockaddr *) ls[i].sockaddr; off = offsetof(struct sockaddr_in, sin_addr); sin = (struct sockaddr_in *) sa; port = sin->sin_port; if (memcmp(ls[i].sockaddr + off, u.sockaddr + off, 4) != 0) { continue; } if (port != u.port) { continue; } lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "duplicate \"%s\" address and port again", &u.url); return LXL_CONF_ERROR; } ls = lxl_array_push(&cmcf->listens); if (ls == NULL) { return LXL_CONF_ERROR; } memset(ls, 0x00, sizeof(lxl_dns_listen_t)); memcpy(ls->sockaddr, u.sockaddr, u.socklen); ls->socklen = u.socklen; ls->wildcard = u.wildcard; ls->ctx = cf->ctx; return LXL_CONF_OK; }
static char * lxl_event_use(lxl_conf_t *cf, lxl_command_t *cmd, void *conf) { lxl_event_conf_t *ecf = (lxl_event_conf_t *) conf; lxl_int_t i; lxl_str_t *value; lxl_event_module_t *module; if (ecf->use != LXL_CONF_UNSET_UINT) { return "is duplicate"; } value = lxl_array_elts(cf->args); for (i = 0; lxl_modules[i]; ++i) { if (lxl_modules[i]->type != LXL_EVENT_MODULE) { continue; } module = (lxl_event_module_t *) lxl_modules[i]->ctx; if (module->name->len == value[1].len && memcmp(module->name->data, value[1].data, value[1].len) == 0) { ecf->use = lxl_modules[i]->ctx_index; ecf->name = module->name->data; return LXL_CONF_OK; } } lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "invalid event type \"%s\"", value[1].data); return LXL_CONF_ERROR; }
char * lxl_conf_set_flag_slot(lxl_conf_t *cf, lxl_command_t *cmd, void *conf) { char *p = conf; lxl_int_t *fp; lxl_str_t *value; fp = (lxl_int_t *) (p + cmd->offset); if (*fp != LXL_CONF_UNSET) { return "is duplicate"; } value = lxl_array_elts(cf->args); if (strcmp(value[1].data, "on") == 0) { *fp = 1; } else if (strcmp(value[1].data, "off") == 0) { *fp = 0; } else { // lxl_conf_log_error(LXL_LOG_EMERG, cf); return LXL_CONF_ERROR; } return LXL_CONF_OK; }
char * lxl_conf_set_num_slot(lxl_conf_t *cf, lxl_command_t *cmd, void *conf) { char *p = conf; lxl_int_t *np; lxl_str_t *value; np = (lxl_int_t *) (p + cmd->offset); if (*np != LXL_CONF_UNSET) { return "is duplicate"; } value = lxl_array_elts(cf->args); *np = atoi(value[1].data); if (*np <= 0) { return "invalid number"; } /*if (cmd->post) { post = cmd->post; return post->post_handle(cf, post, field); }*/ return LXL_CONF_OK; }
char * lxl_conf_set_sec_slot(lxl_conf_t *cf, lxl_command_t *cmd, void *conf) { char *p = conf; time_t *sp; lxl_str_t *value; sp = (time_t *) (p + cmd->offset); if (*sp != LXL_CONF_UNSET) { return "is duplicate"; } value = lxl_array_elts(cf->args); *sp = lxl_conf_parse_time(&value[1], 1); if (*sp == (time_t) -1) { return "is invalid"; } return LXL_CONF_OK; }
char * lxl_conf_set_msec_slot(lxl_conf_t *cf, lxl_command_t *cmd, void *conf) { char *p = conf; lxl_msec_t *msp; lxl_str_t *value; msp = (lxl_msec_t *) (p + cmd->offset); if (*msp != LXL_CONF_UNSET_MSEC) { return "is duplicate"; } value = lxl_array_elts(cf->args); *msp = lxl_conf_parse_time(&value[1], 0); if (*msp == (lxl_msec_t) -1) { return "invalid value"; } return LXL_CONF_OK; }
char * lxl_conf_set_str_slot(lxl_conf_t *cf, lxl_command_t *cmd, void *conf) { char *p = conf; lxl_str_t *field, *value; field = (lxl_str_t *) (p + cmd->offset); if (field->data) { return "is duplicate"; } value = lxl_array_elts(cf->args); *field = value[1]; /*if (cmd->post) { post = cmd->post; return post->post_handle(cf, post, field); }*/ return LXL_CONF_OK; }
static char * lxl_dfst_storage_pass(lxl_conf_t *cf, lxl_command_t *cmd, void *conf) { lxl_dfst_storage_srv_conf_t *sscf = conf; lxl_str_t *value; lxl_url_t u; if (sscf->upstream.upstream) { return "is duplicate"; } memset(&u, 0x00, sizeof(lxl_url_t)); value = lxl_array_elts(cf->args); u.host = value[1]; sscf->upstream.upstream = lxl_dfst_upstream_add(cf, &u, LXL_DFST_UPSTREAM_STORAGE); if (sscf->upstream.upstream == NULL) { return LXL_CONF_ERROR; } return LXL_CONF_OK; }
static char * lxl_event_connections(lxl_conf_t *cf, lxl_command_t *cmd, void *conf) { lxl_event_conf_t *ecf = (lxl_event_conf_t *) conf; lxl_str_t *value; if (ecf->connections != LXL_CONF_UNSET_UINT) { return "is duplicate"; } value = lxl_array_elts(cf->args); ecf->connections = atoi(value[1].data); if (ecf->connections <= 0) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "invalid number \"%s\"", value[1].data); return LXL_CONF_ERROR; } cf->cycle->connection_n = ecf->connections; return LXL_CONF_OK; }
static char * lxl_set_worker_process(lxl_conf_t *cf, lxl_command_t *cmd, void *conf) { lxl_str_t *value; lxl_core_conf_t *ccf; ccf = (lxl_core_conf_t *) conf; if (ccf->worker_process != LXL_CONF_UNSET) { return "is duplicate"; } value = (lxl_str_t *) lxl_array_elts(cf->args); if (value[1].len == 4 && memcmp(value[1].data, "auto", 4) == 0) { //ccf->worker_porcess = lxl_ncpu; ccf->worker_process = 4; } ccf->worker_process = atoi(value[1].data); if (ccf->worker_process <= 0) { return "invalid value"; } return LXL_CONF_OK; }
int lxl_event_process_init(lxl_cycle_t *cycle) { lxl_uint_t i, nelts; lxl_connection_t *c, *next; lxl_listening_t *ls; lxl_core_conf_t *ccf; lxl_event_conf_t *ecf; lxl_event_module_t *m; ccf = (lxl_core_conf_t *) lxl_get_conf(cycle->conf_ctx, lxl_core_module); ecf = (*(lxl_get_conf(cycle->conf_ctx, lxl_events_module))) [lxl_event_core_module.ctx_index]; if (ccf->worker_process > 1 && ecf->accept_mutex) { lxl_use_accept_mutex = 1; } else { lxl_use_accept_mutex = 0; } lxl_accept_disabled = 1; //lxl_list_init(&lxl_posted_event_list); lxl_event_timer_init(); for (i = 0; lxl_modules[i]; ++i) { if (lxl_modules[i]->type != LXL_EVENT_MODULE) { continue; } if (lxl_modules[i]->ctx_index != ecf->use) { continue; } m = lxl_modules[i]->ctx; if (m->actions.init(cycle) != 0) { /* fatal */ exit(2); } break; } cycle->connections = lxl_alloc(cycle->connection_n * sizeof(lxl_connection_t)); if (cycle->connections == NULL) { return -1; } cycle->read_events = lxl_alloc(cycle->connection_n * sizeof(lxl_event_t)); if (cycle->read_events == NULL) { return -1; } for (i = 0; i < cycle->connection_n; ++i) { cycle->read_events[i].closed = 1; } cycle->write_events = lxl_alloc(cycle->connection_n * sizeof(lxl_event_t)); if (cycle->write_events == NULL) { return -1; } for (i = 0; i < cycle->connection_n; ++i) { cycle->write_events[i].closed = 1; } next = NULL; i = cycle->connection_n; c = cycle->connections; do { --i; c[i].data = next; c[i].fd = -1; c[i].read = &cycle->read_events[i]; c[i].write = &cycle->write_events[i]; next = &c[i]; } while (i); cycle->free_connection_n = cycle->connection_n; cycle->free_connections = next; ls = lxl_array_elts(&cycle->listening); nelts = lxl_array_nelts(&cycle->listening); for (i = 0; i < nelts; ++i) { c = lxl_get_connection(ls[i].fd); if (c == NULL) { return -1; } c->listening = &ls[i]; ls[i].connection = c; //rev = c->read; c->read->accept = 1; if (ls[i].type == SOCK_STREAM) { c->udp = 0; //c->closefd = 1; c->read->handler = lxl_event_accept; /* tcp or udp */ } else { c->udp = 1; //c->closefd = 1; c->read->handler = lxl_event_accept_udp; /*c->recv = lxl_recvfrom; c->send = lxl_sendto;*/ } if (lxl_use_accept_mutex) { continue; } if (lxl_add_event(c->read, LXL_READ_EVENT, LXL_LEVEL_EVENT) == -1) { return -1; } } return 0; }
static char * lxl_log_set_log(lxl_conf_t *cf, lxl_command_t *cmd, void *conf) { lxl_log_conf_t *lcf = (lxl_log_conf_t *) conf; int n, use_flush, set_error_level, set_flush_flag; lxl_uint_t i, nelts, error_level, debug_level; lxl_str_t *value, name; lxl_log_t *log; lxl_cycle_t *cycle; cycle = cf->cycle; log = cycle->log; if (lcf->use_flush != LXL_CONF_UNSET) { return "is duplicate"; } nelts = lxl_array_nelts(cf->args); value = lxl_array_elts(cf->args); if (value[1].len == 6 && memcmp(value[1].data, "stderr", 6) == 0) { lcf->use_stderr = 1; } else { name = value[1]; if (lxl_get_full_name(cycle->pool, &cycle->prefix, &name) == -1) { return LXL_CONF_ERROR; } if (name.len > LXL_MAX_CONF_PATH) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "error_log path %s is to long", name.data); return LXL_CONF_ERROR; } lcf->file.len = name.len; lcf->file.data = name.data; lcf->use_stderr = 0; } if (nelts == 2) { log->use_flush = 0; log->error_level = LXL_LOG_ERROR; log->debug_level = 0; return LXL_CONF_OK; } set_error_level = 0; set_flush_flag = 0; use_flush = 0; error_level = LXL_LOG_ERROR; debug_level = 0; for (i = 2; i < nelts; ++i) { n = lxl_log_get_error_level(value[i].data); if (n != -1) { if (set_error_level) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "duplicat log level \"%s\"", value[i].data); return LXL_CONF_ERROR; } set_error_level = 1; error_level = (lxl_uint_t) n; continue; } n = lxl_log_get_debug_level(value[i].data); if (n != -1) { debug_level |= (lxl_uint_t) n; continue; } n = lxl_log_get_flush_flag(value[i].data); if (n != -1) { if (set_flush_flag) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "duplicat log flush flag \"%s\"", value[i].data); return LXL_CONF_ERROR; } set_flush_flag = 1; use_flush = n; continue; } lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "invalid log arguments \"%s\"", value[i].data); return LXL_CONF_ERROR; } lcf->use_flush = use_flush; lcf->error_level = error_level; lcf->debug_level = debug_level; return LXL_CONF_OK; }
char * lxl_conf_parse(lxl_conf_t *cf, lxl_str_t *filename) { int fd; lxl_int_t rc; lxl_buf_t buf; lxl_conf_file_t conf_file; enum { parse_file = 0, parse_block } type; if (filename) { fd = open(filename->data, O_RDONLY); if (fd == -1) { lxl_conf_log_error(LXL_LOG_EMERG, cf, errno, "open() \"%s\" failed", filename->data); return LXL_CONF_ERROR; } cf->conf_file = &conf_file; if (lxl_fd_info(fd, &cf->conf_file->file.info) == -1) { lxl_log_error(LXL_LOG_EMERG, errno, lxl_fd_info_n " \"%s\" failed", filename->data); } cf->conf_file->buffer = &buf; buf.start = lxl_alloc(LXL_CONF_BUFFER); if (buf.start == NULL) { goto failed; } buf.pos = buf.start; buf.last = buf.start; buf.end = buf.last + LXL_CONF_BUFFER; cf->conf_file->line = 1; cf->conf_file->file.fd = fd; cf->conf_file->file.name.len = filename->len; cf->conf_file->file.name.data = filename->data; cf->conf_file->file.offset = 0; cf->conf_file->file.sys_offset = 0; type = parse_file; } else { type = parse_block; } for (; ;) { rc = lxl_conf_read_token(cf); #if 1 lxl_uint_t i, nelts; lxl_str_t *value; nelts = lxl_array_nelts(cf->args); value = lxl_array_elts(cf->args); for (i = 0; i < nelts; ++i) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "args: index:%lu value:%s", i, value[i].data); } #endif if (rc == LXL_ERROR) { goto done; } if (rc == LXL_CONF_BLOCK_DONE) { if (type != parse_block) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "unexpected end of file, unexpected \"}\""); } goto done; } if (rc == LXL_CONF_FILE_DONE) { if (type == parse_block) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "unexpected end of file, expecting \"}\""); goto failed; } goto done; } /*if (cf->handler) { }*/ rc = lxl_conf_handler(cf, rc); if (rc == -1) { goto failed; } } failed: rc = LXL_ERROR; done: if (filename) { if (cf->conf_file->buffer->start) { lxl_free(cf->conf_file->buffer->start); } if (close(fd) == -1) { lxl_log_error(LXL_LOG_ALERT, errno, "close() %s failed", filename->data); return LXL_CONF_ERROR; } } if (rc == LXL_ERROR) { return LXL_CONF_ERROR; } return LXL_CONF_OK; }
static int lxl_conf_handler(lxl_conf_t *cf, lxl_int_t last) { char *rv; void *conf, **confp; lxl_uint_t i, nelts, found; lxl_str_t *name; lxl_command_t *cmd; found = 0; name = (lxl_str_t *) lxl_array_elts(cf->args); for (i = 0; lxl_modules[i]; ++i) { cmd = lxl_modules[i]->commands; if (cmd == NULL) { continue; } for ( /* void */; cmd->name.len; ++cmd) { if (name->len == cmd->name.len && memcmp(name->data, cmd->name.data, name->len) == 0) { found = 1; if (lxl_modules[i]->type != cf->module_type) { continue; } /* is directive's location right ? */ if (!(cmd->type & cf->cmd_type)) { continue; } if (!(cmd->type & LXL_CONF_BLOCK) && last != LXL_OK) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "directive \"%s\" is not terminated by \";\"", name->data); return -1; } if ((cmd->type & LXL_CONF_BLOCK) && last != LXL_CONF_BLOCK_START) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "directive \"%s\" has not opening \"{\"", name->data); return -1; } nelts = lxl_array_nelts(cf->args); if (cmd->type & LXL_CONF_FLAG) { if (nelts != 2) { goto invalid; } } else if (cmd->type & LXL_CONF_1MORE) { if (nelts < 2) { goto invalid; } } else if (cmd->type & LXL_CONF_2MORE) { if (nelts < 3) { goto invalid; } } else if (nelts > LXL_CONF_MAX_ARGS) { goto invalid; } else if (!(cmd->type & argument_number[nelts - 1])) { goto invalid; } conf = NULL; if (cmd->type & LXL_DIRECT_CONF) { conf = ((void **) cf->ctx)[lxl_modules[i]->index]; } else if (cmd->type & LXL_MAIN_CONF) { conf = &(((void **) cf->ctx)[lxl_modules[i]->index]); } else if (cf->ctx) { confp = *(void **) ((char *) cf->ctx + cmd->conf); if (confp) { conf = confp[lxl_modules[i]->ctx_index]; } } rv = cmd->set(cf, cmd, conf); if (rv == LXL_CONF_OK) { return 0; } if (rv == LXL_CONF_ERROR) { return -1; } lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "\"%s\" directive %s", name->data, rv); return -1; } } } if (found) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "\"%s\" directive not allowed here", name->data); return -1; } lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "unknow directive \"%s\"", name->data); return -1; invalid: lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "invalid number of arguments in \"%s\"", name->data); return -1; }