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 void lxl_enable_accept_events(lxl_cycle_t *cycle) { lxl_uint_t i, nelts; lxl_listening_t *ls; lxl_connection_t *c; nelts = lxl_array_nelts(&cycle->listening); for (i = 0; i < nelts; ++i) { ls = lxl_array_data(&cycle->listening, lxl_listening_t, i); c = ls->connection; if (c->read->active) { continue; } lxl_add_event(c->read, LXL_READ_EVENT, 0); } }
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_read_token(lxl_conf_t *cf) { char ch, *dst, *src, *start; off_t file_size; size_t len; ssize_t n, size; lxl_str_t *word; lxl_buf_t *b; lxl_uint_t found, start_line, last_space, sharp_comment; found = 0; last_space = 1; sharp_comment = 0; lxl_array_clear(cf->args); b = cf->conf_file->buffer; start = b->pos; start_line = cf->conf_file->line; file_size = lxl_file_size(&cf->conf_file->file.info); for (; ;) { if (b->pos >= b->last) { if (cf->conf_file->file.offset >= file_size) { if (cf->args->nelts > 0 || !last_space) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "unexpected end file, \";\" or \"}\""); return LXL_ERROR; } return LXL_CONF_FILE_DONE; } len = b->pos - start; if (len == LXL_CONF_BUFFER) { cf->conf_file->line = start_line; lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "too long paraments \"%*s...\" started", start); return LXL_ERROR; } if (len) { memmove(b->start, start, len); } size = (ssize_t) (file_size - cf->conf_file->file.offset); if (size > b->end - (b->start + len)) { size = b->end - (b->start + len); } n = lxl_read_file(&cf->conf_file->file, b->start + len, size, cf->conf_file->file.offset); if (n == -1) { return -1; } if (n != size) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, lxl_read_file_n " returned only %ld bytes instead of %ld", n, size); return -1; } b->pos = b->start + len; b->last = b->pos + n; start = b->start; } ch = *b->pos++; if (ch == '\n') { cf->conf_file->line++; if (sharp_comment) { sharp_comment = 0; } } if (sharp_comment) { continue; } if (last_space) { if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) { continue; } start = b->pos - 1; start_line = cf->conf_file->line; switch (ch) { case ';': if (lxl_array_nelts(cf->args) == 0) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "unexpected \";\""); return LXL_ERROR; } return LXL_OK; case '{': if (lxl_array_nelts(cf->args) == 0) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "unexpected \"{\""); return LXL_ERROR; } return LXL_CONF_BLOCK_START; case '}': if (lxl_array_nelts(cf->args) != 0) { lxl_conf_log_error(LXL_LOG_EMERG, cf, 0, "unexpected \"}\""); return LXL_ERROR; } return LXL_CONF_BLOCK_DONE; case '#': sharp_comment = 1; continue; default: last_space = 0; } } else { if (ch == ' ' || ch == '\t' || ch == CR || ch == LF || ch == ';' || ch == '{') { last_space = 1; found = 1; } if (found) { word = lxl_array_push(cf->args); if (word == NULL) { return LXL_ERROR; } word->data = lxl_pnalloc(cf->pool, b->pos - start + 1); if (word == NULL) { return LXL_ERROR; } for (dst = word->data, src = start, len = 0; src < b->pos - 1; ++len) { *dst++ = *src++; } *dst = '\0'; word->len = len; if (ch == ';') { return LXL_OK; } if (ch == '{') { return LXL_CONF_BLOCK_START; } found = 0; } } } }
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; }
static int lxl_dfss_core_init(lxl_cycle_t *cycle) { int s; unsigned i, n; char *base_path = LXL_DFSS_DATA_PATH; char dir[256]; struct ifreq ifr; lxl_uint_t nelts; lxl_listening_t *ls; lxl_connection_t *c; lxl_log_debug(LXL_LOG_DEBUG_DFS, 0, "dfss core init"); s = socket(AF_INET, SOCK_DGRAM, 0); if (s == -1) { lxl_log_error(LXL_LOG_ERROR, errno, "socket() failed"); return -1; } strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name)); if (ioctl(s, SIOCGIFHWADDR, &ifr) != 0) { lxl_log_error(LXL_LOG_ERROR, errno, "ioctl() failed"); return -1; } snprintf(lxl_dfss_uid, sizeof(lxl_dfss_uid), "%02x%02x%02x%02x%02x%02x%04x", (unsigned char) ifr.ifr_hwaddr.sa_data[0], (unsigned char) ifr.ifr_hwaddr.sa_data[1], (unsigned char) ifr.ifr_hwaddr.sa_data[2], (unsigned char) ifr.ifr_hwaddr.sa_data[3], (unsigned char) ifr.ifr_hwaddr.sa_data[4], (unsigned char) ifr.ifr_hwaddr.sa_data[5], lxl_dfss_ip_port.port); lxl_log_error(LXL_LOG_INFO, 0, "dfss uid %s", lxl_dfss_uid); if (lxl_hash1_init(&lxl_dfss_fid_hash, 1000000, lxl_hash_key) == -1) { return -1; } lxl_list_init(&lxl_dfss_sync_pull_list); lxl_list_init(&lxl_dfss_sync_push_list); lxl_dfss_temp_pool = lxl_create_pool(LXL_DEFAULT_POOL_SIZE); if (lxl_dfss_temp_pool == NULL) { return -1; } if (lxl_hash_init(&lxl_dfss_fid_phash, lxl_dfss_temp_pool, 10000000, lxl_hash_key) == -1) { goto failed; } if (access(base_path, R_OK|W_OK|X_OK) != 0) { //lxl_log_error(LXL_LOG_INFO, errno, "access(R_OK|W_OK|X_OK) %s failed", base_path); if (errno == ENOENT) { if (mkdir(base_path, 0744) != 0) { //if (mkdir(base_path, 0774) != 0) { lxl_log_error(LXL_LOG_EMERG, errno, "mkdir(%s) failed", base_path); goto failed; } } else { lxl_log_error(LXL_LOG_EMERG, errno, "access(R_OK|W_OK|X_OK) %s failed", base_path); goto failed; } } n = LXL_DFSS_DIR_COUNT; for (i = 0; i < n; ++i) { snprintf(dir, 256, "%s%02X", base_path, i); /* access qu diao panduan cuowu */ if (access(dir, R_OK|W_OK|X_OK) != 0 && errno == ENOENT) { if (errno == ENOENT) { if (mkdir(dir, 0777) != 0) { lxl_log_error(LXL_LOG_EMERG, errno, "mkdir(%s) failed", dir); goto failed; } } else { lxl_log_error(LXL_LOG_EMERG, errno, "access(R_OK|W_OK|X_OK) %s failed", dir); goto failed; } } } lxl_dfss_collect_fid(LXL_DFSS_DATA_PATH); lxl_dfss_collect_sync_push_fid(LXL_DFSS_SYSNLOG_PATH); lxl_dfss_dir_seed = 0; lxl_dfss_file_count = 0; //lxl_dfss_ip_port.ip = 1024; //lxl_dfss_ip_port.port = 1024; nelts = lxl_array_nelts(&cycle->listening); if (nelts == 0) { lxl_log_error(LXL_LOG_ERROR, 0, "listening nelts is 0"); return -1; } ls = lxl_array_data(&cycle->listening, lxl_listening_t, 0); c = lxl_get_connection(-1); if (c == NULL) { return -1; } c->listening = ls; memset(&lxl_dfss_report_state_event, 0x00, sizeof(lxl_event_t)); lxl_dfss_report_state_event.data = c; lxl_dfss_report_state_event.handler = lxl_dfss_report_state_handler; //lxl_add_timer(&lxl_dfss_report_state_event, 100); c = lxl_get_connection(-1); if (c == NULL) { return -1; } c->listening = ls; memset(&lxl_dfss_sync_push_event, 0x00, sizeof(lxl_event_t)); lxl_dfss_sync_push_event.data = c; lxl_dfss_sync_push_event.handler = lxl_dfss_sync_push_handler; //lxl_add_timer(&lxl_dfss_report_fid_event, 60000); c = lxl_get_connection(-1); if (c == NULL) { return -1; } c->listening = ls; memset(&lxl_dfss_sync_pull_event, 0x00, sizeof(lxl_event_t)); lxl_dfss_sync_pull_event.data = c; lxl_dfss_sync_pull_event.handler = lxl_dfss_sync_pull_handler; c = lxl_get_connection(-1); if (c == NULL) { return -1; } c->listening = ls; memset(&lxl_dfss_sync_fid_event, 0x00, sizeof(lxl_event_t)); lxl_dfss_sync_fid_event.data = c; lxl_dfss_sync_fid_event.handler = lxl_dfss_sync_fid_handler; //lxl_dfss_sync_fid_handler(&lxl_dfss_sync_fid_event); lxl_dfss_sync_fid(); return 0; failed: lxl_destroy_pool(lxl_dfss_temp_pool); return -1; }