void http_hdr_build_request(const HTTP_HDR_REQ *hdr_req, ACL_VSTRING *strbuf) { ACL_ARRAY *entries; HTTP_HDR_ENTRY *entry; int i, n; entries = hdr_req->hdr.entry_lnk; n = acl_array_size(entries); entry = (HTTP_HDR_ENTRY *) acl_array_index(entries, 0); #if 0 acl_vstring_sprintf(strbuf, "%s %s\r\n", entry->name, entry->value); #else acl_vstring_sprintf(strbuf, "%s %s HTTP/%d.%d\r\n", entry->name, acl_vstring_str(hdr_req->url_part), hdr_req->hdr.version.major, hdr_req->hdr.version.minor); #endif for (i = 1; i < n; i++) { entry = (HTTP_HDR_ENTRY *) acl_array_index(entries, i); if (entry == NULL) break; if (entry->off) continue; acl_vstring_sprintf_append(strbuf, "%s: %s\r\n", entry->name, entry->value); } acl_vstring_strcat(strbuf, "\r\n"); }
void http_hdr_req_free(HTTP_HDR_REQ *hh) { ACL_ARRAY *pool; if (hh == NULL) return; if (var_http_tls_cache <= 0 || cache_pool == NULL) { __hdr_free_member(hh); http_hdr_free((HTTP_HDR *) hh); return; } #ifdef USE_TLS_EX pool = (ACL_ARRAY*) acl_pthread_tls_get(&cache_key); if (pool != NULL) { pool->push_back(pool, hh); return; } #else pool = (ACL_ARRAY*) acl_pthread_getspecific(cache_key); if (pool != NULL && acl_array_size(pool) < var_http_tls_cache) { pool->push_back(pool, hh); return; } #endif __hdr_free_member(hh); http_hdr_free((HTTP_HDR *) hh); }
ACL_DNS_DB *acl_netdb_clone(const ACL_DNS_DB *db) { ACL_DNS_DB *dbp; ACL_HOSTNAME *phost, *h_host; int i, n; if (db == NULL || db->h_db == NULL) return NULL; n = acl_array_size(db->h_db); if (n <= 0) { acl_msg_error("%s, %s(%d): h_db's size(%d) <= 0", __FILE__, __FUNCTION__, __LINE__, n); return NULL; } dbp = acl_netdb_new(db->name); for (i = 0; i < n; i++) { phost = (ACL_HOSTNAME *) acl_array_index(db->h_db, i); acl_assert(phost); h_host = acl_mycalloc(1, sizeof(ACL_HOSTNAME)); memcpy(&h_host->saddr, &phost->saddr, sizeof(h_host->saddr)); ACL_SAFE_STRNCPY(h_host->ip, phost->ip, sizeof(h_host->ip)); h_host->hport = phost->hport; (void) acl_array_append(dbp->h_db, h_host); dbp->size++; } return dbp; }
int acl_cfg_parser_dump(const ACL_CFG_PARSER *parser, const char *pathname, const char *delimiter) { char myname[] = "acl_cfg_parser_dump"; ACL_CFG_LINE *cfg_line; ACL_FILE_HANDLE filefd = ACL_FILE_INVALID; int i, n, ret; char tbuf[256]; #undef RETURN #define RETURN(x) do { \ if (filefd != ACL_FILE_INVALID) \ acl_file_close(filefd); \ return (x); \ } while (0); if (parser == NULL || pathname == NULL || *pathname == 0) return (-1); #ifdef ACL_UNIX # ifdef ACL_ANDROID filefd = acl_file_open(pathname, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0644); # else filefd = acl_file_open(pathname, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, S_IREAD | S_IWRITE | S_IRGRP); # endif #elif defined(ACL_WINDOWS) filefd = acl_file_open(pathname, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, S_IREAD | S_IWRITE); #else # error "unknown OS" #endif if (filefd == ACL_FILE_INVALID) { printf("%s: can't open, pathname=%s, errmsg=%s\n", myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf))); RETURN(-1); } n = acl_array_size(parser->_cfg_array); for (i = 0; i < n; i++) { cfg_line = (ACL_CFG_LINE *) acl_array_index(parser->_cfg_array, i); if (cfg_line == NULL) break; ret = _cfg_line_dump(filefd, cfg_line, delimiter); if (ret < 0) { RETURN (-1); } } RETURN (0); #ifdef ACL_BCB_COMPILER return (0); #endif }
int acl_cfg_parser_delete(ACL_CFG_PARSER *parser, const char *name) { char myname[] = "acl_cfg_parser_delete"; ACL_CFG_LINE *cfg_line = NULL; int i, n, ok = 0, j; if (parser == NULL || name == NULL || *name == 0) { printf("%s: input error\n", myname); return (-1); } if (parser->_cfg_array == NULL) { return (0); } n = acl_array_size(parser->_cfg_array); for (i = 0; i < n; i++) { cfg_line = (ACL_CFG_LINE *) acl_array_index(parser->_cfg_array, i); if (cfg_line == NULL) return (0); if (cfg_line->ncount < 1) continue; if (strcmp(cfg_line->value[0], name) == 0) { ok = 1; break; } } if (ok) { if (cfg_line->pdata == NULL && cfg_line->value != NULL) parser->valid_line--; parser->total_line--; acl_array_delete_idx(parser->_cfg_array, i, _cfg_line_free); n = acl_array_size(parser->_cfg_array); for (j = i; j < n; j++) { cfg_line = (ACL_CFG_LINE *) acl_array_index(parser->_cfg_array, j); if (cfg_line == NULL) break; cfg_line->line_number--; } } return (0); }
ACL_DNS_DB *acl_netdb_clone(const ACL_DNS_DB *h_dns_db) { const char *myname = "acl_netdb_clone"; char buf[256]; ACL_DNS_DB *dns_db; ACL_HOSTNAME *phost, *h_host; int i, n; if (h_dns_db == NULL || h_dns_db->h_db == NULL) return (NULL); n = acl_array_size(h_dns_db->h_db); if (n <= 0) { acl_msg_error("%s, %s(%d): h_db's size(%d) <= 0", __FILE__, myname, __LINE__, n); return (NULL); } dns_db = acl_netdb_new(h_dns_db->name); if (dns_db == NULL) { acl_msg_error("%s, %s(%d): acl_netdb_new error(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); return (NULL); } for (i = 0; i < n; i++) { phost = (ACL_HOSTNAME *) acl_array_index(h_dns_db->h_db, i); acl_assert(phost); h_host = acl_mycalloc(1, sizeof(ACL_HOSTNAME)); if (h_host == NULL) { acl_msg_error("%s, %s(%d): calloc error(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); acl_netdb_free(dns_db); return (NULL); } h_host->saddr.sin_addr.s_addr = phost->saddr.sin_addr.s_addr; ACL_SAFE_STRNCPY(h_host->ip, phost->ip, sizeof(h_host->ip)); h_host->hport = phost->hport; if (acl_array_append(dns_db->h_db, h_host) < 0) { acl_msg_error("%s, %s(%d): array append error(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); acl_netdb_free(dns_db); return (NULL); } dns_db->size++; } return (dns_db); }
static void parse_json(const char *data) { ACL_JSON *json = acl_json_alloc(); ACL_VSTRING *buf1 = acl_vstring_alloc(128); ACL_VSTRING *buf2 = acl_vstring_alloc(128); ACL_ARRAY *nodes; const char *tag = "header"; printf("buf src: %s\r\n", data); printf("------------------------------------------------\r\n"); acl_json_update(json, data); acl_json_build(json, buf1); printf("result src: %s\r\n", STR(buf1)); printf("------------------------------------------------\r\n"); nodes = acl_json_getElementsByTagName(json, tag); if (nodes == NULL) { printf("not found tag: %s\r\n", tag); acl_vstring_free(buf1); acl_vstring_free(buf2); acl_json_free(json); return; } printf(">>>tag: %s, len: %d\r\n", tag, acl_array_size(nodes)); ACL_ITER iter; #define STR acl_vstring_str #define LEN ACL_VSTRING_LEN acl_foreach(iter, nodes) { ACL_JSON_NODE *node = (ACL_JSON_NODE*) iter.data; ACL_JSON_NODE *tag_node = node->tag_node; if (tag_node == NULL) continue; printf(">>>tag: %s\r\n", STR(node->ltag)); ACL_ITER iter2; acl_foreach(iter2, tag_node) { ACL_JSON_NODE *node1 = (ACL_JSON_NODE*) iter2.data; if (node1->ltag == NULL || LEN(node1->ltag) == 0) continue; printf(">>>child tag: %s, txt: %s\r\n", STR(node1->ltag), node1->text ? STR(node1->text) : "null"); }
/* 打印输出配置文件中的有效配置行 */ int aut_cfg_print(void) { const char *myname = "aut_cfg_print"; AUT_LINE *line = NULL; AUT_ARG_ITEM *arg; int i, j, n, m, first_line_arg; if (var_aut_line_array == NULL) { printf("%s: var_aut_line_array=NULL\n", myname); return (-1); } n = acl_array_size(var_aut_line_array); for (i = 0; i < n; i++) { line = (AUT_LINE *) acl_array_index(var_aut_line_array, i); if (line == NULL) break; printf("%s|%d|%d|", line->cmd_name, line->result, line->argc); m = acl_array_size(line->argv); first_line_arg = 1; for (j = 0; j < m; j++) { arg = (AUT_ARG_ITEM *) acl_array_index(line->argv, j); if (arg == NULL) break; /* 判断是否是第一个参数项 */ if (first_line_arg) { printf("%s=%s", arg->name, arg->value); first_line_arg = 0; } else { printf(",%s=%s", arg->name, arg->value); } } printf("\n"); } return (0); }
int http_hdr_res_parse(HTTP_HDR_RES *hdr_res) { const char *myname = "http_hdr_res_parse"; HTTP_HDR *hdr = (HTTP_HDR *) hdr_res; HTTP_HDR_ENTRY *entry; char *ptr; char buf[32]; /* 2xx, 3xx, 4xx, 5xx */ int n; if (hdr == NULL) acl_msg_fatal("%s: hdr_res null", myname); if (hdr->entry_lnk == NULL) acl_msg_fatal("%s: entry_lnk null", myname); n = acl_array_size(hdr->entry_lnk); if (n <= 0) { acl_msg_error("%s: entry_lnk's size %d invalid", myname, n); return (-1); } /* data format: xxx info */ entry = (HTTP_HDR_ENTRY *) acl_array_index(hdr->entry_lnk, 0); ptr = entry->value; while (*ptr == ' ' || *ptr == '\t') ptr++; if (*ptr == 0) { acl_msg_error("%s: status empty", myname); return (-1); } snprintf(buf, sizeof(buf), "%s", ptr); ptr = buf; while (*ptr) { if (*ptr == ' ' || *ptr == '\t') { *ptr = 0; break; } ptr++; } hdr_res->reply_status = atoi(buf); if (hdr_res->reply_status < 100 || hdr_res->reply_status >= 600) { acl_msg_error("%s: status(%s) invalid", myname, buf); return (-1); } return (http_hdr_parse(hdr)); }
void acl_cfg_parser_walk(ACL_CFG_PARSER *parser, ACL_CFG_WALK_FN walk_fn) { int i, n; ACL_CFG_LINE *cfg_line; if (parser) { n = acl_array_size(parser->_cfg_array); for (i = 0; i < n; i++) { cfg_line = (ACL_CFG_LINE *) acl_array_index(parser->_cfg_array, i); if (cfg_line == NULL) break; walk_fn((void *) cfg_line); } } }
static AUT_LINE *__mkcmd_endif(const ACL_CFG_LINE *line) { char myname[] = "__mkcmd_endif"; AUT_LINE *test_line, *test_line_peer; AUT_CMD_TOKEN *if_token = NULL, *if_token_peer; int n, i; __if_nested_count--; if_token = (AUT_CMD_TOKEN *) acl_mycalloc(1, sizeof(*if_token)); if_token->flag = AUT_FLAG_ENDIF; if_token->match_number = ++__endif_count; if_token->peer = NULL; test_line = aut_line_new(line); test_line->arg_inner = (void *) if_token; test_line->free_arg_inner = acl_myfree_fn; n = acl_array_size(var_aut_line_array); for (i = 0; i < n; i++) { test_line_peer = (AUT_LINE *) acl_array_index(var_aut_line_array, i); if (test_line_peer->arg_inner == NULL) continue; if_token_peer = (AUT_CMD_TOKEN *) test_line_peer->arg_inner; if (if_token_peer->flag != AUT_FLAG_IF) continue; if (if_token_peer->match_number != if_token->match_number) continue; /* 找到匹配的循环开始对等结点 */ if_token_peer->peer = test_line; if_token->peer = test_line_peer; } if (if_token->peer == NULL) { aut_log_fatal("%s: line_number=%d, cmd=%s, " "if_nested=%d, if_count=%d, " "endif_count=%d, please check configure, " "err_msg=not found peer loop begin", myname, test_line->line_number, test_line->cmd_name, __if_nested_count, __if_count, __endif_count); } return (test_line); }
AUT_LINE *aut_loop_make_break(const ACL_CFG_LINE *cfg_line) { const char *myname = "aut_loop_make_break"; AUT_LINE *break_line, *tmp_line, *begin_line; AUT_CMD_TOKEN *break_token, *tmp_token, *begin_token; int i, n; break_line = aut_line_new(cfg_line); break_line->obj_type = AUT_OBJ_INNER; break_token = (AUT_CMD_TOKEN *) acl_mycalloc(1, sizeof(AUT_CMD_TOKEN)); break_token->flag = AUT_FLAG_LOOP_BREAK; /* 将 breaktoken 作为内部参数存储在 break_line->arg_inner 中 */ break_line->arg_inner = (void *) break_token; break_line->free_arg_inner = acl_myfree_fn; n = acl_array_size(var_aut_line_array); begin_line = NULL; begin_token = NULL; for (i = n - 1; i >= 0; i--) { tmp_line = (AUT_LINE *) acl_array_index(var_aut_line_array, i); if (tmp_line == NULL) aut_log_fatal("%s(%d): loop_begin null", myname, __LINE__); if (tmp_line->obj_type != AUT_OBJ_INNER) continue; if (tmp_line->arg_inner == NULL) continue; tmp_token = (AUT_CMD_TOKEN *) tmp_line->arg_inner; if (tmp_token->flag != AUT_FLAG_LOOP_BEGIN) continue; begin_line = tmp_line; begin_token = tmp_token; break; } if (begin_line == NULL || begin_token == NULL) aut_log_fatal("%s(%d): no LOOP_BEGIN before LOOP_BREAK", myname, __LINE__); break_token->peer = begin_line; return (break_line); }
int proctl_service_exist(const char *filepath) { PROCTL_SERVICE *service; int i, n; LOCK_RUNNING_SERVICE; n = acl_array_size(__services); for (i = 0; i < n; i++) { service = (PROCTL_SERVICE*) acl_array_index(__services, i); if (strcasecmp(service->filepath, filepath) == 0) break; } UNLOCK_RUNNING_SERVICE; if (n <= 0 || i == n) return (0); return (1); }
static AUT_FN_ITEM *__lookup_fn_item(const AUT_LINE *test_line) { char myname[] = "__lookup_fn_item"; int i, n; AUT_FN_ITEM *item = NULL, *tmp; n = acl_array_size(__all_callback_fn); for (i = 0; i < n; i++) { tmp = (AUT_FN_ITEM *) acl_array_index(__all_callback_fn, i); if (tmp == NULL) aut_log_fatal("%s(%d): idx=%d, null rebuild from array", myname, __LINE__, i); if (aut_line_cmdcmp(test_line, tmp->cmd_name) == 0) { item = tmp; break; } } return (item); }
static PROCTL_SERVICE *proctl_service_find(HANDLE handle) { const char *myname = "proctl_service_find"; PROCTL_SERVICE *service; int i, n; LOCK_RUNNING_SERVICE; n = acl_array_size(__services); for (i = 0; i < n; i++) { service = (PROCTL_SERVICE*) acl_array_index(__services, i); if (service->hProcess == handle) { UNLOCK_RUNNING_SERVICE; return (service); } } UNLOCK_RUNNING_SERVICE; return (NULL); }
ACL_ARGV *proctl_serivce_get_all() { ACL_ARGV *argv = acl_argv_alloc(10); PROCTL_SERVICE *service; int i, n; LOCK_RUNNING_SERVICE; n = acl_array_size(__services); for (i = 0; i < n; i++) { service = (PROCTL_SERVICE*) acl_array_index(__services, i); acl_argv_add(argv, service->filepath, NULL); } UNLOCK_RUNNING_SERVICE; if (argv->argc == 0) { acl_argv_free(argv); return (NULL); } return (argv); }
/*----------------------------------------------------------------------------*/ static void __dbpool_mysql_destroy(ACL_DB_POOL *db_pool) { ACL_DB_POOL_MYSQL *mysql_pool; ACL_DB_HANDLE_MYSQL *mysql_handle; int i, n; mysql_pool = (ACL_DB_POOL_MYSQL *) db_pool; n = acl_array_size(mysql_pool->handles); for (i = 0; i < n; i++) { mysql_handle = (ACL_DB_HANDLE_MYSQL *) acl_array_index(mysql_pool->handles, i); if (mysql_handle == NULL) continue; __close_mysql_handle(mysql_handle); acl_myfree(mysql_handle); } acl_pthread_mutex_destroy(&mysql_pool->mutex); acl_array_destroy(mysql_pool->handles, NULL); acl_myfree(mysql_pool); mysql_library_end(); }
int acl_iplink_list(const ACL_IPLINK *plink) { int i, n; ACL_IPITEM *item; char buf[64]; unsigned ip_begin, ip_end; n = acl_array_size(plink->parray); for (i = 0; i < n; i++) { item = (ACL_IPITEM *) acl_array_index(plink->parray, i); if (item == NULL) break; ip_begin = (unsigned) item->begin; ip_end = (unsigned) item->end; __sane_inet_ntoa(ip_begin, buf, sizeof(buf)); printf("ipbegin=%s", buf); __sane_inet_ntoa(ip_end, buf, sizeof(buf)); printf(", ipend=%s\n", buf); } return n; }
/*----------------------------------------------------------------------------*/ static void __dbpool_mysql_check(ACL_DB_POOL *db_pool) { ACL_DB_POOL_MYSQL *mysql_pool = (ACL_DB_POOL_MYSQL*) db_pool; ACL_DB_HANDLE_MYSQL *mysql_handle; int i, n, ping_inter; time_t now = time(NULL); ping_inter = mysql_pool->db_pool.db_info.ping_inter; n = acl_array_size(mysql_pool->handles); for (i = 0; i < n; i++) { mysql_handle = (ACL_DB_HANDLE_MYSQL *) acl_array_index(mysql_pool->handles, i); if (mysql_handle == NULL) continue; if (mysql_handle->handle.status != ACL_DBH_STATUS_READY) continue; /* if the connecion is idle timeout ? */ if (now > mysql_handle->handle.timeout) { __close_mysql_handle(mysql_handle); mysql_pool->db_pool.db_ready--; continue; } /* has the ping time reached ? */ if (now <= mysql_handle->handle.ping) continue; if (mysql_ping(mysql_handle->connection) == 0) { mysql_handle->handle.ping = time(NULL) + ping_inter; } else { __close_mysql_handle(mysql_handle); mysql_pool->db_pool.db_ready--; } } }
int acl_xml_node_delete(ACL_XML_NODE *node) { ACL_RING *next; ACL_XML_NODE *node_next; int n = 1; while ((next = acl_ring_pop_head(&node->children)) != NULL) { node_next = acl_ring_to_appl(next, ACL_XML_NODE, node); n += acl_xml_node_delete(node_next); } node->xml->node_cnt--; if (node->id != NULL) acl_htable_delete(node->xml->id_table, STR(node->id), NULL); if (node->xml->node_cache && acl_array_size(node->xml->node_cache) < node->xml->max_cache) { node->xml->node_cache->push_back(node->xml->node_cache, node); acl_ring_detach(&node->node); } else acl_xml_node_free(node); return (n); }
void http_hdr_build(const HTTP_HDR *hdr, ACL_VSTRING *strbuf) { ACL_ARRAY *entries; HTTP_HDR_ENTRY *entry; int i, n; entries = hdr->entry_lnk; n = acl_array_size(entries); entry = (HTTP_HDR_ENTRY *) acl_array_index(entries, 0); acl_vstring_sprintf(strbuf, "%s %s\r\n", entry->name, entry->value); for (i = 1; i < n; i++) { entry = (HTTP_HDR_ENTRY *) acl_array_index(entries, i); if (entry == NULL) break; if (entry->off) continue; acl_vstring_sprintf_append(strbuf, "%s: %s\r\n", entry->name, entry->value); } acl_vstring_strcat(strbuf, "\r\n"); }
static ACL_CFG_LINE *_create_cfg_line(char *data, const char *delimiter) { ACL_CFG_LINE *cfg_line = NULL; ACL_ARRAY *a = NULL; int i, n; char *ptr, *pdata, *pitem; #undef ERETURN #define ERETURN(x) do { \ if (a) \ acl_array_destroy(a, acl_myfree_fn); \ if (cfg_line) { \ if (cfg_line->value) \ acl_myfree(cfg_line); \ acl_myfree(cfg_line); \ } \ return (x); \ } while (0); if (data == NULL) return (NULL); pdata = data; cfg_line = (ACL_CFG_LINE *) acl_mycalloc(1, sizeof(ACL_CFG_LINE)); if (cfg_line == NULL) return (NULL); a = acl_array_create(10); while (1) { ptr = acl_mystrtok(&pdata, delimiter); if (ptr == NULL) break; pitem = acl_mystrdup(ptr); if (pitem == NULL) { ERETURN (NULL); } if (acl_array_append(a, (void *) pitem) < 0) { ERETURN (NULL); } } cfg_line->ncount = 0; cfg_line->pdata = NULL; n = acl_array_size(a); if (n > 0) { cfg_line->value = (char **) acl_mycalloc(1 + n, sizeof(char *)); if (cfg_line->value == NULL) { ERETURN (NULL); } for (i = 0; i < n; i++) { pitem = (char *) acl_array_index(a, i); if (pitem == NULL) break; cfg_line->value[i] = pitem; if (cfg_line->value[i] == NULL) ERETURN (NULL); cfg_line->ncount++; } } /* NOTICE: in acl_array_destroy, please don't input acl_myfree, but * NULL as the second parameter, because the mystrup's result * set are stored in cfg_line->value now:) */ acl_array_destroy(a, NULL); return (cfg_line); }
/*----------------------------------------------------------------------------*/ static ACL_DB_HANDLE *__dbpool_mysql_peek(ACL_DB_POOL *db_pool) { char myname[] = "__dbpool_mysql_peek"; ACL_DB_POOL_MYSQL *mysql_pool = (ACL_DB_POOL_MYSQL *) db_pool; ACL_DB_HANDLE_MYSQL *mysql_handle, *mysql_handle_slot = NULL; int i, n; time_t now; static time_t last_time; /* 因为在调用此函数时已经上锁, * 所以此处声明一静态变量是线程安全的. */ #undef RETURN #define RETURN(_x_) do { \ now = time(NULL); \ if (acl_msg_verbose && now - last_time > 5) { \ acl_msg_info("Database status: max = %d, idle = %d, busy = %d", \ db_pool->db_max, db_pool->db_ready, db_pool->db_inuse); \ last_time = now; \ } \ DB_UNLOCK(mysql_pool->mutex); \ return (_x_); \ } while (0) DB_LOCK(mysql_pool->mutex); if (time(NULL) >= mysql_pool->when_check) { int inter = db_pool->db_info.ping_inter > db_pool->db_info.timeout_inter ? db_pool->db_info.timeout_inter : db_pool->db_info.ping_inter; db_pool->dbh_check(db_pool); mysql_pool->when_check = time(NULL) + inter; } if (db_pool->db_inuse >= db_pool->db_max) { acl_msg_warn("%s, %s(%d): all connections be used, reached db_max(%d)", __FILE__, myname, __LINE__, db_pool->db_max); RETURN (NULL); } n = acl_array_size(mysql_pool->handles); /* lookup mysql connection from pool */ for (i = 0; i < n; i++) { mysql_handle = (ACL_DB_HANDLE_MYSQL *) acl_array_index(mysql_pool->handles, i); if (mysql_handle == NULL) continue; if (mysql_handle->handle.status == ACL_DBH_STATUS_READY) { mysql_handle->handle.status = ACL_DBH_STATUS_INUSE; db_pool->db_inuse++; db_pool->db_ready--; RETURN ((ACL_DB_HANDLE *) mysql_handle); } else if (mysql_handle->handle.status == ACL_DBH_STATUS_NULL && mysql_handle_slot == NULL) mysql_handle_slot = mysql_handle; } /* create new mysql connection */ mysql_handle = __open_mysql_handle(mysql_pool, mysql_handle_slot, &db_pool->db_info); if (mysql_handle == NULL) RETURN (NULL); mysql_handle->handle.status = ACL_DBH_STATUS_INUSE; db_pool->db_inuse++; RETURN ((ACL_DB_HANDLE *) mysql_handle); }
ACL_CFG_PARSER *acl_cfg_parser_load(const char *pathname, const char *delimiter) { char myname[] = "acl_cfg_parse_load"; ACL_CFG_PARSER *parser = NULL; ACL_CFG_LINE *cfg_line; struct stat stat_buf; int buf_size; char *content_buf = NULL, *ptr; char *pline_begin; ACL_FILE_HANDLE filefd = ACL_FILE_INVALID; char tbuf[256]; #undef ERETURN #define ERETURN(x) do { \ if (content_buf != NULL) \ acl_myfree(content_buf); \ if (filefd != ACL_FILE_INVALID) \ acl_file_close(filefd); \ if (parser != NULL) { \ acl_array_destroy(parser->_cfg_array, NULL); \ acl_myfree(parser); \ } \ return (x); \ } while (0); #undef RETURN #define RETURN(x) do { \ if (content_buf != NULL) \ acl_myfree(content_buf); \ if (filefd != ACL_FILE_INVALID) \ acl_file_close(filefd); \ return (x); \ } while (0); if (pathname == NULL || *pathname == 0) { printf("%s: invalid pathname\n", myname); return (NULL); } if (stat(pathname, &stat_buf) < 0) { printf("%s: can't stat, pathname=%s, errmsg=%s\n", myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf))); ERETURN (NULL); } parser = (ACL_CFG_PARSER *) acl_mycalloc(1, sizeof(*parser)); if (parser == NULL) { printf("%s: can't calloc ACL_CFG_PARSER, pathname=%s, errmsg=%s", myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf))); ERETURN (NULL); } parser->_cfg_array = acl_array_create(10); if (parser->_cfg_array == NULL) { printf("%s: can't create array, pathname=%s, errmsg=%s", myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf))); ERETURN (NULL); } parser->total_line = 0; parser->valid_line = 0; buf_size = (int) stat_buf.st_size + 256; content_buf = (char *) acl_mycalloc(1, buf_size); if (content_buf == NULL) { printf("%s: can't calloc, pathname=%s, errmsg=%s\n", myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf))); ERETURN (NULL); } #ifdef ACL_UNIX # ifdef ACL_ANDROID filefd = acl_file_open(pathname, O_RDWR, 0644); # else filefd = acl_file_open(pathname, O_RDWR, S_IREAD | S_IWRITE | S_IRGRP); # endif #elif defined(ACL_WINDOWS) filefd = acl_file_open(pathname, O_RDWR, S_IREAD | S_IWRITE); #else # error "unknown OS" #endif if (filefd == ACL_FILE_INVALID) { printf("%s: can't open, pathname=%s, errmsg=%s\n", myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf))); ERETURN (NULL); } if (_cfg_file_load(filefd, content_buf, buf_size) < 0) { printf("%s: can't read, pathname=%s, errmsg=%s\n", myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf))); ERETURN (NULL); } #undef SKIP #define SKIP(var, cond) \ for (; *var && (cond); var++) {} ptr = content_buf; while (*ptr) { pline_begin = ptr; /* keep the line header */ /* first, skip all ' ' and '\t' */ SKIP(ptr, (*ptr == ' ' || *ptr == '\t')); if (*ptr == '#') { /* the comment line */ SKIP(ptr, *ptr != '\n'); /* find the line's end */ if (*ptr) { /* this must be '\n' */ *ptr++ = 0; /* set '\0' and skip one byte */ } cfg_line = _backup_junk_line(pline_begin); if (cfg_line == NULL) ERETURN (NULL); if (acl_array_append(parser->_cfg_array, (void *) cfg_line) < 0) { printf("%s: can't add ACL_CFG_LINE to array, " "errmsg=%s", myname, acl_last_strerror(tbuf, sizeof(tbuf))); ERETURN (NULL); } parser->total_line++; cfg_line->line_number = parser->total_line; continue; } else if (*ptr == '\r' || *ptr == '\n') { /* SKIP(ptr, (*ptr == '\r' || *ptr == '\n')); */ if (*ptr == '\r' && *(ptr + 1) == '\n') { *ptr = 0; /* set '\0' first and go on */ ptr += 2; } else if (*ptr == '\n') { *ptr = 0; /* set '\0' first and go on */ ptr++; } cfg_line = _backup_junk_line(pline_begin); if (cfg_line == NULL) ERETURN (NULL); if (acl_array_append(parser->_cfg_array, (void *) cfg_line) < 0) { printf("%s: can't add ACL_CFG_LINE to array, " "errmsg=%s", myname, acl_last_strerror(tbuf, sizeof(tbuf))); ERETURN (NULL); } parser->total_line++; cfg_line->line_number = parser->total_line; continue; } pline_begin = ptr; /* reset the line header */ /* find the line's end */ SKIP(ptr, (*ptr != '\n' && *ptr != '\r')); if (*ptr) { /* this must be '\r' or '\n' */ if (*ptr == '\r' && *(ptr + 1) == '\n') { *ptr = 0; /* set '\0' first and go on */ ptr += 2; } else if (*ptr == '\n') { *ptr = 0; /* set '\0' first and go on */ ptr++; } } /* make ptr to the next line's beginning */ /* SKIP(ptr, (*ptr == '\r' || *ptr == '\n')); */ cfg_line = _create_cfg_line(pline_begin, delimiter); if (cfg_line == NULL) ERETURN (NULL); if (acl_array_append(parser->_cfg_array, (void *) cfg_line) < 0) { printf("%s: can't add ACL_CFG_LINE to array, errmsg=%s", myname, acl_last_strerror(tbuf, sizeof(tbuf))); ERETURN (NULL); } parser->total_line++; parser->valid_line++; cfg_line->line_number = parser->total_line; } if (parser->total_line != acl_array_size(parser->_cfg_array)) { printf("%s: total_line=%d, acl_array_size=%d, errmsg=not equal\n", myname, parser->total_line, acl_array_size(parser->_cfg_array)); } RETURN (parser); #ifdef ACL_BCB_COMPILER return (NULL); #endif }
AUT_LINE *aut_loop_make_end(const ACL_CFG_LINE *cfg_line) { const char *myname = "aut_loop_make_end"; AUT_LINE *test_line, *test_line_peer; AUT_CMD_TOKEN *loop_token = NULL, *loop_token_peer; int n, i; /* 记数器, 对 loop_end 标记记数减 1 */ __loop_end_count++; loop_token = (AUT_CMD_TOKEN *) acl_mycalloc(1, sizeof(*loop_token)); loop_token->flag = AUT_FLAG_LOOP_END; loop_token->status = AUT_STAT_BUSY; /* 与前面的 loop_begin 相匹配 */ loop_token->match_number = __loop_nested_count; loop_token->peer = NULL; /* 循环对嵌套减 1 */ __loop_nested_count--; test_line = aut_line_new(cfg_line); test_line->obj_type = AUT_OBJ_INNER; /* 将 loop_token 作为内部参数存储在 test_line->arg_inner 中 */ test_line->arg_inner = (void *) loop_token; test_line->free_arg_inner = acl_myfree_fn; n = acl_array_size(var_aut_line_array); /* 查找与循环结束标志相配对的循环开始对象 */ for (i = 0; i < n; i++) { test_line_peer = (AUT_LINE *) acl_array_index(var_aut_line_array, i); /* xxx: 不应该发生此种情况, 除非动态数组出了故障 */ if (test_line_peer == NULL) break; /* 先判断是否是内部对象 */ if (test_line_peer->obj_type != AUT_OBJ_INNER) continue; /* 先查看是否有内部数据参数存储在 test_line_peer 中 */ if (test_line_peer->arg_inner == NULL) continue; loop_token_peer = (AUT_CMD_TOKEN *) test_line_peer->arg_inner; /* 看该 loopbegin 对象是否已经被 一个 loopend 对象给匹配了 */ if (loop_token_peer->status == AUT_STAT_BUSY) continue; /* 是否是 循环开始标志, 以判断是否是一个循环的开始 */ if (loop_token_peer->flag != AUT_FLAG_LOOP_BEGIN) continue; /* 比较匹配行号是否相等 */ if (loop_token_peer->match_number != loop_token->match_number) continue; /* 找到匹配的循环开始对等结点 */ loop_token_peer->peer = test_line; loop_token_peer->status = AUT_STAT_BUSY; loop_token->peer = test_line_peer; } if (loop_token->peer == NULL) { aut_log_fatal("%s(%d)->%s: line_number=%d, cmd=%s, " "loop_nested=%d, loop_begin=%d, " "loop_end=%d, please check configure, " "err_msg=not found peer loop begin", __FILE__, __LINE__, myname, test_line->line_number, test_line->cmd_name, __loop_nested_count, __loop_begin_count, __loop_end_count); } return (test_line); }
int acl_iplink_count_item(ACL_IPLINK *plink) { return acl_array_size(plink->parray); }
static ZDB_DISK *zdb_disks_load(const char *dbname, const char *dbpath) { const char *myname = "zdb_disks_load"; ACL_VSTRING *buf = acl_vstring_alloc(256); ACL_FILE *fp = NULL; char disk_info[INFO_LEN + 1]; ZDB_DISK *disk, *disks; ACL_ARRAY *a = NULL; ACL_ITER iter; int n, i; #undef RETURN #define RETURN(x) do { \ if (fp) \ acl_fclose(fp); \ acl_vstring_free(buf); \ if (a) \ acl_array_destroy(a, free_disk); \ return (x); \ } while (0) acl_vstring_sprintf(buf, "%s/.%s.disk", dbpath, dbname); fp = acl_fopen(STR(buf), "r"); if (fp == NULL) { acl_msg_error("%s(%d): fopen(%s) error(%s)", myname, __LINE__, STR(buf), acl_last_serror()); RETURN (NULL); } a = acl_array_create(10); while (1) { ACL_ARGV *argv; if (acl_fgets_nonl(disk_info, sizeof(disk_info), fp) == NULL) break; argv = acl_argv_split(disk_info, "|"); if (argv->argc != ITEM_CNT) { acl_msg_error("%s(%d): invalid line(%s)", myname, __LINE__, disk_info); acl_argv_free(argv); continue; } disk = (ZDB_DISK*) acl_mycalloc(1, sizeof(ZDB_DISK)); disk->path = acl_mystrdup(argv->argv[0]); disk->idisk = atoi(argv->argv[1]); disk->priority = atoi(argv->argv[2]); disk->limit = acl_atoui64(argv->argv[3]); disk->count = acl_atoui64(argv->argv[4]); if (acl_array_append(a, disk) < 0) acl_msg_fatal("%s(%d): add disk error(%s)", myname, __LINE__, acl_last_serror()); acl_argv_free(argv); } n = acl_array_size(a); if (n <= 0) { acl_msg_error("%s(%d): empty array of ZDB_DISK", myname, __LINE__); RETURN (NULL); } disks = (ZDB_DISK*) acl_mycalloc(n + 1, sizeof(ZDB_DISK)); i = 0; acl_foreach(iter, a) { disk = (ZDB_DISK*) iter.data; disks[i].limit = disk->limit; disks[i].count = disk->count; disks[i].path = acl_mystrdup(disk->path); disks[i].idisk = disk->idisk; disks[i].priority = disk->priority; disks[i].dat_ifiles = NULL; disks[i].dat_ifiles_size = 0; if (disks[i].idisk != i) { acl_msg_error("%s(%d): idisk(%d) != %d invalid for %s", myname, __LINE__, disks[i].idisk, i, disks[i].path); acl_myfree(disks); RETURN (NULL); } i++; }