void http_req_body_get_async(HTTP_REQ *request, ACL_ASTREAM *astream, HTTP_BODY_NOTIFY notify, void *arg, int timeout) { const char *myname = "http_req_body_get_async"; HTTP_CHAT_CTX *ctx; HTTP_HDR *hdr; if (request == NULL || astream == NULL || notify == NULL) acl_msg_fatal("%s: input invalid", myname); if (request->hdr_req == NULL) acl_msg_fatal("%s: hdr null", myname); hdr = &(request->hdr_req->hdr); if (hdr->content_length <= 0) { acl_msg_warn("%s: content_length(%lld)", myname, hdr->content_length); } ctx = request->hdr_req->hdr.chat_ctx; ctx->hdr = hdr; ctx->stream = astream; ctx->timeout = timeout; ctx->chunked = 0; ctx->chunk_len = hdr->content_length; ctx->read_cnt = 0; ctx->body_len = 0; ctx->notify.body_notify = notify; ctx->arg = arg; body_get(astream, ctx); }
TLS_SCACHE *tls_scache_open(const char *dbname, const char *cache_label, int verbose, int timeout) { const char *myname = "tls_scache_open"; TLS_SCACHE *cp; DICT *dict; /* * Logging. */ if (verbose) acl_msg_info("open %s TLS cache %s", cache_label, dbname); /* * Open the dictionary with O_TRUNC, so that we never have to worry about * opening a damaged file after some process terminated abnormally. */ #ifdef SINGLE_UPDATER #define DICT_FLAGS (DICT_FLAG_DUP_REPLACE) #elif defined(ACL_UNIX) #define DICT_FLAGS \ (DICT_FLAG_DUP_REPLACE | DICT_FLAG_LOCK | DICT_FLAG_SYNC_UPDATE) #elif defined(WIN32) #define DICT_FLAGS \ (DICT_FLAG_DUP_REPLACE | DICT_FLAG_SYNC_UPDATE) #endif dict = dict_open(dbname, O_RDWR | O_CREAT | O_TRUNC, DICT_FLAGS); /* * Sanity checks. */ if (dict->lock_fd < 0) acl_msg_fatal("%s: dictionary %s is not a regular file", myname, dbname); #ifdef SINGLE_UPDATER if (acl_myflock(dict->lock_fd, INTERNAL_LOCK, MYFLOCK_OP_EXCLUSIVE | MYFLOCK_OP_NOWAIT) < 0) acl_msg_fatal("%s: cannot lock dictionary %s for exclusive use: %s", myname, dbname, acl_last_serror()); #endif if (dict->update == 0) acl_msg_fatal("%s: dictionary %s does not support update operations", myname, dbname); if (dict->delete_it == 0) acl_msg_fatal("%s: dictionary %s does not support delete operations", myname, dbname); if (dict->sequence == 0) acl_msg_fatal("%s: dictionary %s does not support sequence operations", myname, dbname); /* * Create the TLS_SCACHE object. */ cp = (TLS_SCACHE *) acl_mymalloc(sizeof(*cp)); cp->flags = 0; cp->db = dict; cp->cache_label = acl_mystrdup(cache_label); cp->verbose = verbose; cp->timeout = timeout; cp->saved_cursor = 0; return (cp); }
static void msg_error(PROBE_SERVER *server, const char *fmt, ...) { const char *myname = "msg_error"; va_list ap; static char buf[4096]; if (server == NULL) acl_msg_fatal("%s: server null", myname); if (fmt == NULL) acl_msg_fatal("%s: fmt null", myname); va_start(ap, fmt); acl_vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); server->time_end = time(NULL); server->time_total_cost = server->time_end - server->time_begin; if (server->logfp != NULL) { char fmtstr[128]; acl_logtime_fmt(fmtstr, sizeof(fmtstr)); acl_vstream_fprintf(server->logfp, "%s, %s: <%s> addr(%s), url(%s), time(%ld), %s\n", myname, fmtstr, (server->warn_time > 0 && server->time_total_cost >= server->warn_time) ? "WARN" : "INFO", server->addr, server->url, server->time_total_cost, buf); } else acl_msg_error("%s <%s> addr(%s), url(%s), time(%ld), %s", myname, (server->warn_time > 0 && server->time_total_cost >= server->warn_time) ? "WARN" : "INFO", server->addr, server->url, server->time_total_cost, buf); }
static void parse_addr(char *addr, unsigned short *port, char **local_ip, char **remote) { const char *myname = "parse_addr"; char *ptr; ptr = strchr(addr, ':'); if (ptr == NULL) acl_msg_fatal("%s, %s(%d): invalid addr(%s)", __FILE__, myname, __LINE__, addr); *ptr++ = 0; *port = atoi(ptr); if (*port <= 0) acl_msg_fatal("%s, %s(%d): invalid port(%d)", __FILE__, myname, __LINE__, *port); ptr = strchr(addr, '@'); if (ptr != NULL) { *ptr++ = 0; *local_ip = addr; *remote = ptr; } else { *local_ip = NULL; *remote = addr; } if (strlen(*remote) == 0) acl_msg_fatal("%s, %s(%d): ip buf's length is 0", __FILE__, myname, __LINE__); }
void acl_master_start_service(ACL_MASTER_SERV *serv) { const char *myname = "acl_master_start_service"; if (serv == NULL) acl_msg_fatal("%s(%d): serv null", myname, __LINE__); if (acl_var_master_global_event == NULL) acl_var_master_global_event = acl_event_new_select( acl_var_master_delay_sec, acl_var_master_delay_usec); if (acl_var_master_global_event == NULL) acl_msg_fatal("%s(%d)->%s: acl_event_new null, serr=%s", __FILE__, __LINE__, myname, strerror(errno)); /* * Enable connection requests, wakeup timers, and status updates from * child processes. */ acl_msg_info("%s: starting service %s ...", myname, serv->name); acl_master_listen_init(serv); acl_msg_info("%s: service %s listen init ok ...", myname, serv->name); acl_master_status_init(serv); acl_msg_info("%s: service %s status init ok ...", myname, serv->name); acl_master_avail_listen(serv); acl_msg_info("%s: service %s avail listen ok ...", myname, serv->name); acl_master_wakeup_init(serv); acl_msg_info("%s: service %s wakeup init ok ...", myname, serv->name); acl_msg_info("%s: service started!", myname); }
int http_hdr_req_cookies_parse(HTTP_HDR_REQ *hh) { /* data format: "name1=value1; name2=value2; name3=value3" */ const char *myname = "http_hdr_req_cookies_parse"; const HTTP_HDR_ENTRY *entry; ACL_ARGV *argv; const char *ptr; ACL_ITER iter; if (hh == NULL) acl_msg_fatal("%s, %s(%d): input invalid", __FILE__, myname, __LINE__); if ((hh->flag & HTTP_HDR_REQ_FLAG_PARSE_COOKIE) == 0) return 0; entry = http_hdr_entry((HTTP_HDR *) hh, "Cookie"); if (entry == NULL) return 0; if (hh->cookies_table == NULL) hh->cookies_table = acl_htable_create(__http_hdr_max_cookies, ACL_HTABLE_FLAG_KEY_REUSE); if (hh->cookies_table == NULL) acl_msg_fatal("%s, %s(%d): htable create error(%s)", __FILE__, myname, __LINE__, acl_last_serror()); /* 分隔数据段 */ argv = acl_argv_split(entry->value, ";"); acl_foreach(iter, argv) { ptr = (const char*) iter.data; __add_cookie_item(hh->cookies_table, ptr); }
char *acl_url_encode(const char *str) { const char *myname = "acl_url_encode"; register int i, j, len, tmp_len; unsigned char *tmp; len = strlen(str); tmp_len = len; tmp = (unsigned char*) acl_mymalloc(len+1); if (tmp == NULL) acl_msg_fatal("%s(%d): malloc error", myname, __LINE__); for (i = 0, j = 0; i < len; i++, j++) { tmp[j] = (unsigned char)str[i]; if (tmp[j] == ' ') tmp[j] = '+'; else if (!isalnum(tmp[j]) && strchr("_-.", tmp[j]) == NULL) { tmp_len += 3; tmp = acl_myrealloc(tmp, tmp_len); if (!tmp) acl_msg_fatal("%s(%d): realloc error", myname, __LINE__); tmp[j++] = '%'; tmp[j++] = hex_enc_table[(unsigned char)str[i] >> 4]; tmp[j] = hex_enc_table[(unsigned char)str[i] & 0x0F]; } }
static void acl_pthread_init_once(void) { const char *myname = "acl_pthread_init_once"; int i; acl_pthread_mutex_init(&__thread_lock, NULL); __thread_inited = 1; for (i = 0; i < ACL_PTHREAD_KEYS_MAX; i++) { __tls_key_list[i].destructor = NULL; __tls_key_list[i].key = ACL_TLS_OUT_OF_INDEXES; } __tls_value_list_key = TlsAlloc(); if (__tls_value_list_key == ACL_TLS_OUT_OF_INDEXES) acl_msg_fatal("%s(%d): TlsAlloc error(%s)", myname, __LINE__, acl_last_serror()); if (__tls_value_list_key < 0 || __tls_value_list_key >= ACL_PTHREAD_KEYS_MAX) { acl_msg_fatal("%s(%d): TlsAlloc error(%s), not in(%d, %d)", myname, __LINE__, acl_last_serror(), 0, ACL_PTHREAD_KEYS_MAX); } __tls_key_list[__tls_value_list_key].destructor = NULL; __tls_key_list[__tls_value_list_key].key = __tls_value_list_key; }
void acl_fiber_rwlock_wunlock(ACL_FIBER_RWLOCK *lk) { ACL_FIBER *fiber; if (lk->writer == NULL) acl_msg_fatal("%s(%d), %s: wunlock: not locked", __FILE__, __LINE__, __FUNCTION__); lk->writer = NULL; if (lk->readers != 0) acl_msg_fatal("%s(%d), %s: wunlock: readers", __FILE__, __LINE__, __FUNCTION__); while ((fiber = FIRST_FIBER(&lk->rwaiting)) != NULL) { acl_ring_detach(&lk->rwaiting); lk->readers++; acl_fiber_ready(fiber); } if (lk->readers == 0 && (fiber = FIRST_FIBER(&lk->wwaiting)) != NULL) { acl_ring_detach(&lk->wwaiting); lk->writer = fiber; acl_fiber_ready(fiber); } }
void http_res_body_get_async(HTTP_RES *respond, ACL_ASTREAM *astream, HTTP_BODY_NOTIFY notify, void *arg, int timeout) { const char *myname = "http_res_body_get_async"; HTTP_CHAT_CTX *ctx; HTTP_HDR *hdr; if (respond == NULL || astream == NULL || notify == NULL) acl_msg_fatal("%s, %s(%d): input invalid", myname, __FILE__, __LINE__); if (respond->hdr_res == NULL) acl_msg_fatal("%s: hdr null", myname); hdr = &(respond->hdr_res->hdr); if (hdr->debug && hdr->content_length <= 0 && !hdr->chunked) { acl_msg_warn("%s, %s(%d): content_length(%lld), status(%d)", __FILE__, myname, __LINE__, hdr->content_length, respond->hdr_res->reply_status); } ctx = respond->hdr_res->hdr.chat_ctx; ctx->hdr = hdr; ctx->stream = astream; ctx->timeout = timeout; ctx->chunked = hdr->chunked; ctx->chunk_len = hdr->content_length; ctx->read_cnt = 0; ctx->body_len = 0; ctx->notify.body_notify = notify; ctx->arg = arg; body_get(astream, ctx); }
static void event_disable_readwrite(ACL_EVENT *eventp, ACL_VSTREAM *stream) { const char *myname = "event_disable_readwrite"; EVENT_SELECT_THR *event_thr = (EVENT_SELECT_THR *) eventp; ACL_EVENT_FDTABLE *fdp; ACL_SOCKET sockfd; sockfd = ACL_VSTREAM_SOCK(stream); THREAD_LOCK(&event_thr->event.tb_mutex); if (!FD_ISSET(sockfd, &event_thr->xmask)) { acl_msg_error("%s(%d): sockfd(%d) not be set", myname, __LINE__, sockfd); THREAD_UNLOCK(&event_thr->event.tb_mutex); return; } fdp = (ACL_EVENT_FDTABLE *) stream->fdp; if (fdp == NULL) { acl_msg_error("%s(%d): fdp null", myname, __LINE__); THREAD_UNLOCK(&event_thr->event.tb_mutex); return; } if (fdp->fdidx == -1) acl_msg_fatal("%s(%d): fdidx(%d) invalid", myname, __LINE__, fdp->fdidx); FD_CLR(sockfd, &event_thr->xmask); FD_CLR(sockfd, &event_thr->rmask); FD_CLR(sockfd, &event_thr->wmask); fdp->flag = 0; if (eventp->maxfd == sockfd) eventp->maxfd = ACL_SOCKET_INVALID; if (eventp->fdtabs[fdp->fdidx] == fdp) { if (fdp->fdidx < --eventp->fdcnt) { eventp->fdtabs[fdp->fdidx] = eventp->fdtabs[eventp->fdcnt]; eventp->fdtabs[fdp->fdidx]->fdidx = fdp->fdidx; } } else acl_msg_fatal("%s(%d): fdidx(%d)'s fdp invalid", myname, __LINE__, fdp->fdidx); if (fdp->fdidx_ready > 0 && fdp->fdidx_ready < eventp->fdcnt_ready && eventp->fdtabs_ready[fdp->fdidx_ready] == fdp) { eventp->fdtabs_ready[fdp->fdidx_ready] = NULL; } event_fdtable_free(fdp); stream->fdp = NULL; stream->nrefer--; THREAD_UNLOCK(&event_thr->event.tb_mutex); }
void *acl_default_realloc(const char *filename, int line, void *ptr, size_t len) { const char *myname = "acl_default_realloc"; MBLOCK *real_ptr; size_t old_len, new_len; const char *pname = NULL; if (filename && *filename) SET_FILE(pname, filename); else pname = __FILENAME_UNKNOWN; #ifndef NO_SHARED_EMPTY_STRINGS if (ptr == empty_string) return acl_default_malloc(pname, line, len); #endif if (len < 1) { acl_msg_warn("%s(%d)->%s: realloc: requested length %ld", pname, line, myname, (long) len); len = 128; } if (ptr == NULL) return acl_default_malloc(pname, line, len); CHECK_IN_PTR(ptr, real_ptr, old_len, pname, line); new_len = SPACE_FOR(len); if (new_len <= 0) acl_msg_fatal("%s(%d): new_len(%d) <= 0", myname, __LINE__, (int) new_len); else if (new_len >= __malloc_limit) { acl_msg_warn("%s(%d): new_len(%d) too large", myname, __LINE__, (int) new_len); } #ifdef _USE_GLIB if ((real_ptr = (MBLOCK *) g_realloc((char *) real_ptr, new_len)) == 0) acl_msg_fatal("%s(%d)->%s: realloc: insufficient memory: %s", pname, line, myname, strerror(errno)); #else if ((real_ptr = (MBLOCK *) realloc((char *) real_ptr, new_len)) == 0) acl_msg_fatal("%s(%d)->%s: realloc: insufficient memory: %s", pname, line, myname, strerror(errno)); #endif CHECK_OUT_PTR(ptr, real_ptr, len); #if 0 if (len > old_len) memset((char *) ptr + old_len, FILLER, len - old_len); #endif return ptr; }
/* add by zsx for rw timeout, 2005.9.25*/ void acl_multi_server_cancel_rw_timer(ACL_VSTREAM *stream) { const char *myname = "acl_multi_server_cancel_rw_timer"; if (stream == NULL) acl_msg_fatal("%s(%d), %s: input error", __FILE__, __LINE__, myname); if (__eventp == NULL) acl_msg_fatal("%s(%d), %s: event has not been inited", __FILE__, __LINE__, myname); }
ACL_SQL_RES *acl_dbsql_select(ACL_DB_HANDLE *handle, const char *sql, int *error) { const char *myname = "acl_dbsql_select"; if (handle == NULL || sql == NULL || *sql == 0) acl_msg_fatal("%s(%d): input invalid", myname, __LINE__); if (handle->sql_select == NULL) acl_msg_fatal("%s(%d): sql_select null", myname, __LINE__); return (handle->sql_select(handle, sql, error)); }
ACL_MEM_SLICE *acl_mem_slice_init(int base, int nslice, int nalloc_gc, unsigned int slice_flag) { const char *myname = "acl_mem_slice_init"; ACL_MEM_SLICE *mem_slice; if (__mem_slice_key != (acl_pthread_key_t) -1) { acl_msg_error("%s(%d): has been init", myname, __LINE__); return NULL; } __mem_base = base; __mem_nslice = nslice; __mem_nalloc_gc = nalloc_gc < 10 ? 10 : nalloc_gc; __mem_slice_flag = slice_flag; __mem_list_init_size = nalloc_gc / 10; if (__mem_list_init_size < 1000) __mem_list_init_size = 1000; else if (__mem_list_init_size > 1000000) __mem_list_init_size = 1000000; /* 主线程获得自己的线程局部存储内存池 */ mem_slice = mem_slice_create(); if (mem_slice == NULL) acl_msg_fatal("%s(%d): mem_slice null", myname, __LINE__); /* 创建进程空间内全局的内存池集合对象, 其存储所有线程的存储内存池句柄 */ __mem_slice_list = private_array_create(10); __mem_slice_list_lock = thread_mutex_create(); private_array_push(__mem_slice_list, mem_slice); mem_slice->slice_list = __mem_slice_list; mem_slice->slice_list_lock = __mem_slice_list_lock; if (__mem_slice_list == NULL) acl_msg_fatal("%s(%d): __mem_slice_list null", myname, __LINE__); if (__mem_slice_list_lock == NULL) acl_msg_fatal("%s(%d): __mem_slice_list_lock null", myname, __LINE__); atexit(free_global_ctx); mem_slice->tls_key = __mem_slice_key; acl_mem_hook(tls_mem_alloc, tls_mem_calloc, tls_mem_realloc, tls_mem_strdup, tls_mem_strndup, tls_mem_memdup, tls_mem_free); acl_msg_info("%s(%d): use ACL_MEM_SLICE, with tls", myname, __LINE__); return mem_slice; }
int acl_stream_connect(const char *path, int block_mode, int unused_timeout) { const char *myname = "acl_stream_connect"; #ifdef ACL_FREEBSD path = path; block_mode = block_mode; unused_timeout = unused_timeout; acl_msg_fatal("%s(%d): not support!", myname, __LINE__); return -1; #else int pair[2]; int fifo; unused_timeout = unused_timeout; /* * The requested file system object must exist, otherwise we can't reach * the server. */ if ((fifo = open(path, O_WRONLY | O_NONBLOCK, 0)) < 0) return -1; /* * Create a pipe, and send one pipe end to the server. */ if (pipe(pair) < 0) acl_msg_fatal("%s: pipe: %s", myname, acl_last_serror()); if (ioctl(fifo, I_SENDFD, pair[1]) < 0) acl_msg_fatal("%s: send file descriptor: %s", myname, acl_last_serror()); close(pair[1]); /* * This is for {unix,inet}_connect() compatibility. */ if (block_mode == ACL_NON_BLOCKING) acl_non_blocking(pair[0], ACL_NON_BLOCKING); /* * Cleanup. */ close(fifo); /* * Keep the other end of the pipe. */ return pair[0]; #endif /* ACL_FREEBSD */ }
/* * rfc1035HeaderUnpack() * * Unpacks a RFC1035 message header buffer into the header fields * of the rfc1035_message structure. * * Updates the buffer offset, which is the same as number of * octects unpacked since the header starts at offset 0. * * Returns 0 (success) or 1 (error) */ static int rfc1035HeaderUnpack(const char *buf, size_t sz, int *off, rfc1035_message * h) { const char *myname = "rfc1035HeaderUnpack"; unsigned short s; unsigned short t; if (*off != 0) acl_msg_fatal("%s: *off(%d) != 0", myname, *off); /* * The header is 12 octets. This is a bogus message if the size * is less than that. */ if (sz < 12) return 1; memcpy(&s, buf + (*off), sizeof(s)); (*off) += sizeof(s); h->id = ntohs(s); memcpy(&s, buf + (*off), sizeof(s)); (*off) += sizeof(s); t = ntohs(s); h->qr = (t >> 15) & 0x01; h->opcode = (t >> 11) & 0x0F; h->aa = (t >> 10) & 0x01; h->tc = (t >> 9) & 0x01; h->rd = (t >> 8) & 0x01; h->ra = (t >> 7) & 0x01; /* * We might want to check that the reserved 'Z' bits (6-4) are * all zero as per RFC 1035. If not the message should be * rejected. */ h->rcode = t & 0x0F; memcpy(&s, buf + (*off), sizeof(s)); (*off) += sizeof(s); h->qdcount = ntohs(s); memcpy(&s, buf + (*off), sizeof(s)); (*off) += sizeof(s); h->ancount = ntohs(s); memcpy(&s, buf + (*off), sizeof(s)); (*off) += sizeof(s); h->nscount = ntohs(s); memcpy(&s, buf + (*off), sizeof(s)); (*off) += sizeof(s); h->arcount = ntohs(s); if (*off != 12) acl_msg_fatal("%s: *off(%d) != 12", myname, *off); return 0; }
static void event_disable_readwrite(ACL_EVENT *eventp, ACL_VSTREAM *stream) { const char *myname = "event_disable_readwrite"; EVENT_POLL_THR *event_thr = (EVENT_POLL_THR *) eventp; ACL_EVENT_FDTABLE *fdp; ACL_SOCKET sockfd; sockfd = ACL_VSTREAM_SOCK(stream); fdp = (ACL_EVENT_FDTABLE *) stream->fdp; if (fdp == NULL) { acl_msg_error("%s(%d): fdp null", myname, __LINE__); return; } if ((fdp->flag & (EVENT_FDTABLE_FLAG_READ | EVENT_FDTABLE_FLAG_WRITE)) == 0) { acl_msg_error("%s(%d): sockfd(%d) not be set", myname, __LINE__, sockfd); return; } if (fdp->fdidx == -1) acl_msg_fatal("%s(%d): fdidx(%d) invalid", myname, __LINE__, fdp->fdidx); THREAD_LOCK(&event_thr->event.tb_mutex); if (eventp->maxfd == sockfd) eventp->maxfd = ACL_SOCKET_INVALID; if (eventp->fdtabs[fdp->fdidx] != fdp) acl_msg_fatal("%s(%d): fdidx(%d)'s fdp invalid", myname, __LINE__, fdp->fdidx); if (fdp->fdidx < --eventp->fdcnt) { eventp->fdtabs[fdp->fdidx] = eventp->fdtabs[eventp->fdcnt]; eventp->fdtabs[fdp->fdidx]->fdidx = fdp->fdidx; event_thr->fds[fdp->fdidx] = event_thr->fds[eventp->fdcnt]; } acl_fdmap_del(event_thr->fdmap, sockfd); THREAD_UNLOCK(&event_thr->event.tb_mutex); if (fdp->flag & EVENT_FDTABLE_FLAG_READ) stream->nrefer--; if (fdp->flag & EVENT_FDTABLE_FLAG_WRITE) stream->nrefer--; event_fdtable_reset(fdp); }
void *acl_default_malloc(const char *filename, int line, size_t len) { const char *myname = "acl_default_malloc"; size_t new_len; char *ptr; MBLOCK *real_ptr; const char *pname = NULL; if (filename && *filename) SET_FILE(pname, filename); else pname = __FILENAME_UNKNOWN; if (len < 1) acl_msg_fatal("%s(%d), %s: malloc: length %ld invalid", pname, line, myname, (long) len); new_len = SPACE_FOR(len); if (new_len <= 0) acl_msg_fatal("%s(%d): new_len(%d) <= 0", myname, __LINE__, (int) new_len); else if (new_len >= __malloc_limit) { acl_log_strace(); acl_msg_warn("%s(%d): new_len(%d) too large", myname, __LINE__, (int) new_len); } #ifdef _USE_GLIB if ((real_ptr = (MBLOCK *) g_malloc(new_len)) == 0) { acl_log_strace(); acl_msg_error("%s(%d)->%s: new_len: %d, g_malloc error(%s)", pname, line, myname, (int) new_len, strerror(errno)); return 0; } #else if ((real_ptr = (MBLOCK *) malloc(new_len)) == 0) { acl_log_strace(); acl_msg_error("%s(%d)->%s: malloc: insufficient memory: %s, " "new_len: %d", pname, line, myname, strerror(errno), (int) new_len); return 0; } #endif CHECK_OUT_PTR(ptr, real_ptr, len); #if 0 memset(ptr, FILLER, len); #endif return ptr; }
static ACL_DB_HANDLE *dbconn_get(void) { const char *myname = "dbconn_get"; ACL_DB_HANDLE *db_handle; if (__db_pool == NULL) acl_msg_fatal("%s(%d): db pool null", myname, __LINE__); db_handle = acl_dbpool_peek(__db_pool); if (db_handle == NULL) acl_msg_fatal("%s(%d): get db conn error", myname, __LINE__); return (db_handle); }
int proctl_service_join(void) { const char *myname = "proctl_service_join"; PROCTL_SERVICE *service; HANDLE hProcess; DWORD status; int i; char ebuf[256]; for (i = 0; i < __cur_handle; i++) { hProcess = __handles[i]; if (hProcess == INVALID_HANDLE_VALUE) acl_msg_fatal("%s(%d): invalid handle in array, i(%d)", myname, __LINE__, i); service = proctl_service_find(hProcess); if (service == NULL) { if (hProcess == __sem_handle) continue; acl_msg_fatal("%s(%d): not found hProcess", myname, __LINE__); } status = 0; if (!GetExitCodeProcess(hProcess, &status)) { acl_msg_error("%s(%d): get child exit error(%s)", myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf))); if (proctl_service_restart(service) < 0) proctl_service_stopped(service); } else if (status == 0) { acl_msg_error("%s(%d): child exit status 0, error(%s)", myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf))); /* 因为进程是正常退出,所以不需要重启动 */ proctl_service_stopped(service); } else if (status != STILL_ACTIVE) { /* child has exited abnormaly */ acl_msg_error("%s(%d): child exit status %d, child exit(%s)", myname, __LINE__, (int) status, acl_last_strerror(ebuf, sizeof(ebuf))); /* 因为进程是异常退出,所以需要重启动 */ if (proctl_service_restart(service) < 0) proctl_service_stopped(service); } /* else: STILL_ACTIVE */ } return (0); }
static ACL_FILE_HANDLE __mylock_try_open(const char *filename) { char myname[] = "__mylock_try_open"; if (__file_fd < 0) { if (filename == NULL || *filename == 0) acl_msg_fatal("%s(%d): filename invalid", myname, __LINE__); __file_fd = __mylock_open(filename); } if (__file_fd < 0) acl_msg_fatal("%s(%d): open file(%s) error(%s)", myname, __LINE__, filename, strerror(errno)); return (__file_fd); }
ACL_EVENT *acl_event_new(int event_mode, int use_thr, int delay_sec, int delay_usec) { const char *myname = "acl_event_new"; ACL_EVENT *eventp = NULL; if (use_thr) { switch (event_mode) { case ACL_EVENT_SELECT: eventp = acl_event_new_select_thr(delay_sec, delay_usec); break; case ACL_EVENT_KERNEL: eventp = acl_event_new_kernel_thr(delay_sec, delay_usec); break; case ACL_EVENT_POLL: eventp = acl_event_new_poll_thr(delay_sec, delay_usec); break; default: acl_msg_fatal("%s(%d): unsupport %d event", myname, __LINE__, event_mode); break; } } else { switch (event_mode) { case ACL_EVENT_SELECT: eventp = acl_event_new_select(delay_sec, delay_usec); break; case ACL_EVENT_KERNEL: eventp = acl_event_new_kernel(delay_sec, delay_usec); break; case ACL_EVENT_POLL: eventp = acl_event_new_poll(delay_sec, delay_usec); break; case ACL_EVENT_WMSG: /* 使用该值作为消息号 */ eventp = acl_event_new_wmsg((unsigned int) delay_sec); break; default: acl_msg_fatal("%s(%d): unsupport %d event", myname, __LINE__, event_mode); break; } } return eventp; }
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)); }
char *acl_default_strndup(const char *filename, int line, const char *str, size_t len) { const char *myname = "acl_default_strndup"; char *result; char *cp; const char *pname = NULL; if (filename && *filename) SET_FILE(pname, filename); else pname = __FILENAME_UNKNOWN; if (str == 0) acl_msg_fatal("%s(%d)->%s: null pointer argument", pname, line, myname); #ifndef NO_SHARED_EMPTY_STRINGS if (*str == 0) return ((char *) empty_string); #endif if ((cp = memchr(str, 0, len)) != 0) len = cp - str; result = memcpy(acl_default_malloc(pname, line, len + 1), str, len); result[len] = 0; return (result); }
ACL_VSTREAM *acl_vstream_connect_ex(const char *addr, int block_mode, int connect_timeout, int rw_timeout, int rw_bufsize, int *he_errorp) { const char *myname = "acl_vstream_connect_ex"; ACL_VSTREAM *client; ACL_SOCKET connfd; char *ptr, buf[256]; if (addr == NULL || *addr == 0) acl_msg_fatal("%s: addr null", myname); ptr = strchr(addr, ':'); if (ptr != NULL) { connfd = acl_inet_connect_ex(addr, block_mode, connect_timeout, he_errorp); } #ifdef WIN32 else { acl_msg_error("%s(%d): addr(%s) invalid", myname, __LINE__, addr); return NULL; } #elif defined(ACL_UNIX) else {
static int disable_write(EVENT_KERNEL *ev, ACL_EVENT_FDTABLE *fdp) { const char *myname = "disable_write"; ACL_VSTREAM *stream = fdp->stream; ACL_SOCKET sockfd = ACL_VSTREAM_SOCK(stream); int err, ret = 0; fdp->flag &= ~EVENT_FDTABLE_FLAG_DEL_WRITE; fdp->flag &= ~EVENT_FDTABLE_FLAG_WRITE; fdp->event_type &= ~(ACL_EVENT_WRITE | ACL_EVENT_CONNECT); if ((fdp->flag & EVENT_FDTABLE_FLAG_READ)) { #if (ACL_EVENTS_KERNEL_STYLE == ACL_EVENTS_STYLE_KQUEUE) EVENT_REG_DEL_WRITE(err, ev->event_fd, sockfd); #else EVENT_REG_MOD_READ(err, ev->event_fd, sockfd, fdp); #endif } else { #ifdef USE_FDMAP acl_fdmap_del(ev->fdmap, sockfd); #endif #ifdef EVENT_REG_DEL_BOTH EVENT_REG_DEL_BOTH(err, ev->event_fd, sockfd); #else EVENT_REG_DEL_WRITE(err, ev->event_fd, sockfd); #endif ret = 1; } if (err < 0) { acl_msg_fatal("%s: %s: %s, err(%d), fd(%d), ret(%d)", myname, EVENT_REG_DEL_TEXT, acl_last_serror(), err, sockfd, ret); } return (ret); }
static void enable_write(EVENT_KERNEL *ev, ACL_EVENT_FDTABLE *fdp) { const char *myname = "enable_write"; ACL_SOCKET sockfd = ACL_VSTREAM_SOCK(fdp->stream); int err; fdp->flag &= ~EVENT_FDTABLE_FLAG_ADD_WRITE; fdp->flag |= EVENT_FDTABLE_FLAG_WRITE; if ((fdp->flag & EVENT_FDTABLE_FLAG_READ)) { #if (ACL_EVENTS_KERNEL_STYLE == ACL_EVENTS_STYLE_KQUEUE) EVENT_REG_ADD_WRITE(err, ev->event_fd, sockfd, fdp); #else EVENT_REG_MOD_RDWR(err, ev->event_fd, sockfd, fdp); #endif } else { EVENT_REG_ADD_WRITE(err, ev->event_fd, sockfd, fdp); } if (err < 0) { acl_msg_fatal("%s: %s: %s, err(%d), fd(%d)", myname, EVENT_REG_ADD_TEXT, acl_last_serror(), err, sockfd); } }
/*----------------------------------------------------------------------------*/ static void __read_notify_callback(int event_type, ACL_SPOOL *h_spool, ACL_VSTREAM *cstream, void *context) { char myname[] = "__read_notify_callback"; SPOOL *spool; spool = (SPOOL *) context; switch (event_type) { case ACL_EVENT_READ: if (protocol(spool, cstream) < 0) { acl_vstream_close(cstream); } else { acl_spool_enable_read(h_spool, cstream, var_cfg_client_idle_limit, __read_notify_callback, (void *) spool); } break; case ACL_EVENT_RW_TIMEOUT: case ACL_EVENT_XCPT: acl_vstream_close(cstream); break; default: acl_msg_fatal("%s, %s(%d): unknown event type(%d)", __FILE__, myname, __LINE__, event_type); /* not reached */ break; } }
static void fiber_check(void) { if (__thread_fiber != NULL) return; acl_assert(acl_pthread_once(&__once_control, thread_init) == 0); __thread_fiber = (FIBER_TLS *) acl_mycalloc(1, sizeof(FIBER_TLS)); #ifdef USE_JMP /* set context NULL when using setjmp that setcontext will not be * called in fiber_swap. */ __thread_fiber->original.context = NULL; #else __thread_fiber->original.context = (ucontext_t *) acl_mycalloc(1, sizeof(ucontext_t)); #endif __thread_fiber->fibers = NULL; __thread_fiber->size = 0; __thread_fiber->slot = 0; __thread_fiber->idgen = 0; __thread_fiber->count = 0; acl_ring_init(&__thread_fiber->ready); acl_ring_init(&__thread_fiber->dead); if ((unsigned long) acl_pthread_self() == acl_main_thread_self()) { __main_fiber = __thread_fiber; atexit(fiber_schedule_main_free); } else if (acl_pthread_setspecific(__fiber_key, __thread_fiber) != 0) acl_msg_fatal("acl_pthread_setspecific error!"); }