const char *acl_getcwd() { const char *myname = "acl_getcwd"; char *buf_ptr = get_tls_buf(); char *ptr; ptr = getcwd(buf_ptr, BUF_SIZE); if (ptr == NULL) { acl_msg_error("%s(%d): getcwd error(%s)", myname, __LINE__, acl_last_serror()); } return (ptr); }
const char *acl_netdb_index_ip(const ACL_DNS_DB *db, int n) { ACL_HOSTNAME *h_hostname; if (db == NULL || n < 0) { acl_msg_error("%s, %s(%d): input error", __FILE__, __FUNCTION__, __LINE__); return NULL; } if (db->size == 0) { acl_msg_error("%s, %s(%d): dns db size is 0", __FILE__, __FUNCTION__, __LINE__); return NULL; } if (n >= db->size) { acl_msg_error("%s, %s(%d): index(%d) > size(%d)", __FILE__, __FUNCTION__, __LINE__, n, db->size); return NULL; } h_hostname = (ACL_HOSTNAME *) acl_array_index(db->h_db, n); return h_hostname->ip; }
const char *acl_process_path() { const char *myname = "acl_process_path"; const char *ptr; ptr = getexecname(); if (ptr == NULL) { acl_msg_error("%s(%d): readlink error(%s)", myname, __LINE__, acl_last_serror()); return (NULL); } return (ptr); }
static int epoll_event_add(EVENT *ev, int fd, int mask) { EVENT_EPOLL *ep = (EVENT_EPOLL *) ev; struct epoll_event ee; int op; if ((ev->events[fd].mask & mask) == mask) return 0; /* If the fd was already monitored for some event, we need a MOD * operation. Otherwise we need an ADD operation. */ op = ev->events[fd].mask == EVENT_NONE ? EPOLL_CTL_ADD : EPOLL_CTL_MOD; ee.events = 0; ee.data.u64 = 0; ee.data.ptr = NULL; ee.data.fd = fd; #if 1 mask |= ev->events[fd].mask; /* Merge old events */ #endif if (mask & EVENT_READABLE) ee.events |= EPOLLIN; if (mask & EVENT_WRITABLE) ee.events |= EPOLLOUT; #if 0 #ifdef EPOLLRDHUP ee.events |= EPOLLERR | EPOLLRDHUP | EPOLLHUP; #else ee.events |= EPOLLERR | EPOLLHUP; #endif #endif if (__sys_epoll_ctl == NULL) hook_epoll(); if (__sys_epoll_ctl(ep->epfd, op, fd, &ee) == -1) { fiber_save_errno(); #if 0 acl_msg_error("%s, %s(%d): epoll_ctl error %s, epfd=%d, fd=%d", __FILE__, __FUNCTION__, __LINE__, acl_last_serror(), ep->epfd, fd); #endif return -1; } return 0; }
const char *acl_process_path() { const char *myname = "acl_process_path"; char *buf_ptr = get_tls_buf(); int ret; ret = readlink("/proc/curproc/file", buf_ptr, BUF_SIZE); if (ret < 0) { acl_msg_error("%s(%d): readlink error(%s)", myname, __LINE__, acl_last_serror()); return (NULL); } return (buf_ptr); }
void acl_clean_env(char **preserve_list) { #ifdef ACL_UNIX extern char **environ; char **env = environ; #elif defined(ACL_WINDOWS) extern char **_environ; char **env = _environ; #endif ACL_ARGV *save_list; char *value; char **cpp; char *eq; /* * Preserve or specify selected environment variables. */ #define STRING_AND_LENGTH(x, y) (x), (ssize_t) (y) save_list = acl_argv_alloc(10); for (cpp = preserve_list; *cpp; cpp++) if ((eq = strchr(*cpp, '=')) != 0) acl_argv_addn(save_list, STRING_AND_LENGTH(*cpp, eq - *cpp), STRING_AND_LENGTH(eq + 1, strlen(eq + 1)), (char *) 0); else if ((value = acl_safe_getenv(*cpp)) != 0) acl_argv_add(save_list, *cpp, value, (char *) 0); /* * Truncate the process environment, if available. On some systems * (Ultrix!), environ can be a null pointer. */ if (env) env[0] = 0; /* * Restore preserved environment variables. */ for (cpp = save_list->argv; *cpp; cpp += 2) #ifdef ACL_UNIX if (setenv(cpp[0], cpp[1], 1)) #elif defined(ACL_WINDOWS) if (!SetEnvironmentVariable(cpp[0], cpp[1])) #endif acl_msg_error("setenv(%s, %s): %s", cpp[0], cpp[1], acl_last_serror()); /* * Cleanup. */ acl_argv_free(save_list); }
const char *acl_netdb_index_ip(const ACL_DNS_DB *h_dns_db, int n) { const char *myname = "acl_netdb_index_ip"; ACL_HOSTNAME *h_hostname; if (h_dns_db == NULL || n < 0) { acl_msg_error("%s, %s(%d): input error", __FILE__, myname, __LINE__); return (NULL); } if (h_dns_db->size == 0) { acl_msg_error("%s, %s(%d): dns db size is 0", __FILE__, myname, __LINE__); return (NULL); } if (n >= h_dns_db->size) { acl_msg_error("%s, %s(%d): index(%d) > size(%d)", __FILE__, myname, __LINE__, n, h_dns_db->size); return (NULL); } h_hostname = (ACL_HOSTNAME *) acl_array_index(h_dns_db->h_db, n); return (h_hostname->ip); }
static DWORD WINAPI RunThreadWrap(LPVOID data) #endif { acl_pthread_t *thread = (acl_pthread_t *) data; void *return_arg; ACL_FIFO *tls_value_list_ptr = tls_value_list_get(); unsigned long *tid = 0; /* 只是为了避免与主线程的 h_thread->handle = handle 产生冲突 */ if (__thread_inited) acl_pthread_mutex_lock(&__thread_lock); if (__thread_inited) acl_pthread_mutex_unlock(&__thread_lock); thread->id = acl_pthread_self(); return_arg = (void*) thread->start_routine(thread->routine_arg); /* 释放由 acl_pthread_setspecific 添加的线程局部变量 */ while (1) { TLS_VALUE *tls_value = private_fifo_pop(tls_value_list_ptr); if (tls_value == NULL) break; if (tls_value->tls_key == NULL || tls_value->tls_key->destructor == NULL || tls_value->tls_key->key < 0 || tls_value->tls_key->key >= ACL_PTHREAD_KEYS_MAX) { acl_default_free(__FILE__, __LINE__, tls_value); continue; } tls_value->tls_key->destructor(tls_value->value); acl_default_free(__FILE__, __LINE__, tls_value); } private_fifo_free(tls_value_list_ptr, NULL); /* 如果线程创建时为分离方式则需要关闭线程句柄 */ if (thread->detached) { if (!CloseHandle(thread->handle)) { acl_msg_error("close handle error(%s)", acl_last_serror()); } } acl_default_free(__FILE__, __LINE__, thread); return (DWORD) return_arg; }
int smtp_send_file(SMTP_CLIENT *client, const char* filepath) { int ret; ACL_VSTREAM *in = acl_vstream_fopen(filepath, O_RDONLY, 0600, 4096); if (in == NULL) { acl_msg_error("%s(%d): open %s error(%s)", __FUNCTION__, __LINE__, filepath, acl_last_serror()); return -1; } ret = smtp_send_stream(client, in); acl_vstream_close(in); return ret; }
void acl_netdb_refer_oper(ACL_DNS_DB *db, int idx, int value) { ACL_HOSTNAME *h_hostname; if (db == NULL || idx < 0) { acl_msg_error("%s, %s(%d): input error", __FILE__, __FUNCTION__, __LINE__); return; } if (db->size == 0) { acl_msg_error("%s, %s(%d): dns db size is 0", __FILE__, __FUNCTION__, __LINE__); return; } if (idx >= db->size) { acl_msg_error("%s, %s(%d): index(%d) > size(%d)", __FILE__, __FUNCTION__, __LINE__, idx, db->size); return; } h_hostname = (ACL_HOSTNAME *) acl_array_index(db->h_db, idx); h_hostname->nrefer += value; }
int acl_btree_destroy(ACL_BTREE *tree) { const char *myname = "acl_btree_destroy"; btree_validate(tree); if (tree->root != NULL) { acl_msg_error("%s(%d): Tree not empty", myname, __LINE__); return (-1); } acl_slice_destroy(tree->slice); acl_myfree(tree); return (0); }
inline ssize_t fiber_recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) { ssize_t ret; EVENT *ev; ACL_FIBER *me; if (sockfd < 0) { acl_msg_error("%s: invalid sockfd: %d", __FUNCTION__, sockfd); return -1; } if (!acl_var_hook_sys_api) { if (__sys_recvfrom == NULL) hook_io(); return __sys_recvfrom(sockfd, buf, len, flags, src_addr, addrlen); } ev = fiber_io_event(); if (ev && event_readable(ev, sockfd)) { event_clear_readable(ev, sockfd); ret = __sys_recvfrom(sockfd, buf, len, flags, src_addr, addrlen); if (ret < 0) fiber_save_errno(); return ret; } fiber_wait_read(sockfd); if (ev) event_clear_readable(ev, sockfd); ret = __sys_recvfrom(sockfd, buf, len, flags, src_addr, addrlen); if (ret >= 0) return ret; fiber_save_errno(); me = acl_fiber_running(); if (acl_fiber_killed(me)) { acl_msg_info("%s(%d), %s: fiber-%u is existing", __FILE__, __LINE__, __FUNCTION__, acl_fiber_id(me)); return -1; } return ret; }
const char *acl_process_path() { const char *myname = "acl_process_path"; char *buf_ptr = get_tls_buf(); int ret; ret = GetModuleFileName(NULL, buf_ptr, BUF_SIZE); if (ret == 0) { acl_msg_error("%s(%d): readlink error(%s)", myname, __LINE__, acl_last_serror()); return (NULL); } return (buf_ptr); }
int acl_get_tcp_nodelay(ACL_SOCKET fd) { const char *myname = "acl_get_tcp_nodelay"; int on = 0; socklen_t len = (socklen_t) sizeof(on); if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*) &on, &len) < 0) { acl_msg_error("%s(%d): getsockopt error: %s, fd: %d", myname, __LINE__, acl_last_serror(), fd); return -1; } return on; }
int get_addr_from_file(const char *filepath, char *buf, size_t size) { const char *myname = "get_addr_from_file"; ACL_VSTREAM *fp; char ebuf[256]; int n; fp = acl_vstream_fopen(filepath, O_RDONLY, 0600, 1024); if (fp == NULL) { acl_msg_error("%s(%d): fopen file(%s) error(%s)", myname, __LINE__, filepath, acl_last_strerror(ebuf, sizeof(ebuf))); return (-1); } n = acl_vstream_gets_nonl(fp, buf, size); acl_vstream_close(fp); if (n == ACL_VSTREAM_EOF) acl_msg_error("%s(%d): gets from file(%s) error(%s)", myname, __LINE__, filepath, acl_last_strerror(ebuf, sizeof(ebuf))); return (0); }
int http_util_req_open(HTTP_UTIL *http_util) { const char *myname = "http_util_req_open"; int ret; /* 连接远程 http 服务器 */ http_util->stream = acl_vstream_connect(http_util->server_addr, ACL_BLOCKING /* 采用阻塞方式 */, http_util->conn_timeout /* 连接超时时间 */, http_util->rw_timeout /* 网络 IO 操作超时时间 */, 4096 /* stream 流缓冲区大小为 4096 字节 */); if (http_util->stream == NULL) { acl_msg_error("%s(%d): connect %s error(%s)", myname, __LINE__, http_util->server_addr, acl_last_serror()); return (-1); } /* 构建 HTTP 请求头数据 */ http_hdr_build_request(http_util->hdr_req, http_util->req_buf); /* 向 HTTP 服务器发送请求 */ ret = acl_vstream_writen(http_util->stream, acl_vstring_str(http_util->req_buf), ACL_VSTRING_LEN(http_util->req_buf)); if (ret == ACL_VSTREAM_EOF) { acl_msg_error("%s(%d): send request to server(%s) error(%s)", myname, __LINE__, http_util->server_addr, acl_last_serror()); return (-1); } return (0); }
int smtp_mail(SMTP_CLIENT *client, const char *from) { int ret; ACL_ARGV *tokens; client->smtp_code = 0; client->buf[0] = 0; /* 发送 mail from 信息 */ ret = acl_vstream_fprintf(client->conn, "MAIL FROM: <%s>\r\n", from); if (ret == ACL_VSTREAM_EOF) { acl_msg_error("%s(%d): send mail from error(%s)", __FUNCTION__, __LINE__, acl_last_serror()); return -1; } /* 读取响应信息 */ ret = acl_vstream_gets_nonl(client->conn, client->buf, client->size); if (ret == ACL_VSTREAM_EOF) { acl_msg_error("%s(%d): gets mail from's reply error(%s)", __FUNCTION__, __LINE__, acl_last_serror()); return (-1); } tokens = acl_argv_split(client->buf, "\t "); client->smtp_code = atoi(tokens->argv[0]); if (client->smtp_code != 250) { acl_msg_error("%s(%d): mail from error(%d), line(%s)", __FUNCTION__, __LINE__, client->smtp_code, client->buf); acl_argv_free(tokens); return -1; } acl_argv_free(tokens); return 0; }
static void notify_thread(void *arg) { const char *myname = "notify_thread"; WARN_INFO *info = (WARN_INFO*) arg; ACL_VSTREAM *client; ACL_VSTRING *buf; int ret; buf = acl_vstring_alloc(256); acl_vstring_sprintf(buf, "PROC=%s|PID=%d|RCPT=%s|info=%s\r\n", info->path, info->pid, info->notify_recipients, info->desc); client = acl_vstream_connect(info->notify_addr, ACL_BLOCKING, 60, 60, 1024); if (client == NULL) { acl_msg_error("%s(%d): connect %s error, info(%s)", myname, __LINE__, info->notify_addr, acl_vstring_str(buf)); acl_vstring_free(buf); warn_info_free(info); return; } /* 禁止将该句柄传递给子进程 */ acl_close_on_exec(ACL_VSTREAM_SOCK(client), ACL_CLOSE_ON_EXEC); ret = acl_vstream_writen(client, acl_vstring_str(buf), ACL_VSTRING_LEN(buf)); if (ret == ACL_VSTREAM_EOF) acl_msg_error("%s(%d): write to notify server error, info(%s)", myname, __LINE__, acl_vstring_str(buf)); else acl_msg_info("%s(%d): notify ok!", myname, __LINE__); acl_vstream_close(client); acl_vstring_free(buf); warn_info_free(info); }
static int chunked_sep_gets(HTTP_CHAT_CTX *ctx) { const char *myname = "chunked_sep_gets2"; char buf[HTTP_BSIZE]; int n; n = acl_vstream_gets(ctx->stream, buf, sizeof(buf)); if (n == ACL_VSTREAM_EOF) { acl_msg_error("%s(%d): gets sep line error", myname, __LINE__); return -1; } ctx->body_len += n; return 0; }
int acl_get_tcp_solinger(ACL_SOCKET fd) { const char *myname = "acl_get_tcp_solinger"; struct linger l; socklen_t len = (socklen_t) sizeof(l); memset(&l, 0, sizeof(l)); if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (char*) &l, &len) < 0) { acl_msg_error("%s(%d): getsockopt error: %s, fd: %d", myname, __LINE__, acl_last_serror(), fd); return -1; } return l.l_linger == 0 ? -1 : l.l_linger; }
void acl_tcp_so_linger(ACL_SOCKET fd, int onoff, int timeout) { const char *myname = "acl_tcp_so_linger"; struct linger l; l.l_onoff = onoff ? 1 : 0; l.l_linger = timeout >= 0 ? timeout : 0; if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &l, sizeof(l)) < 0) { acl_msg_error("%s(%d): setsockopt(SO_LINGER) error(%s)," " onoff(%d), timeout(%d)", myname, __LINE__, acl_last_serror(), onoff, timeout); } }
int acl_pthread_mutex_init(acl_pthread_mutex_t *mutex, const acl_pthread_mutexattr_t *mattr) { const char *myname = "acl_pthread_mutex_init"; if (mutex == NULL) { acl_msg_error("%s, %s(%d): input invalid", __FILE__, myname, __LINE__); return -1; } mutex->dynamic = 0; /* Create the mutex, with initial value signaled */ mutex->id = CreateMutex((SECURITY_ATTRIBUTES *) mattr, FALSE, NULL); if (!mutex->id) { acl_msg_error("%s, %s(%d): CreateMutex error(%s)", __FILE__, myname, __LINE__, acl_last_serror()); acl_myfree(mutex); return -1; } return 0; }
int http_util_set_dump_file(HTTP_UTIL *http_util, const char *filename) { const char *myname = "http_util_set_dump_file"; if (filename == NULL || *filename == 0) { acl_msg_error("%s(%d): filename invalid", myname, __LINE__); return (-1); } if ((http_util->flag & HTTP_UTIL_FLAG_SET_DUMP_STREAM)) { acl_msg_error("%s(%d): You've called http_util_set_dump_stream before!", myname, __LINE__); return (-1); } http_util->dump_stream = acl_vstream_fopen(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600, 4096); if (http_util->dump_stream == NULL) { acl_msg_error("%s(%d): open dump file(%s) error(%s)", myname, __LINE__, filename, acl_last_serror()); return (-1); } http_util->flag |= HTTP_UTIL_FLAG_SET_DUMP_FILE; return (0); }
int acl_pthread_mutex_init(acl_pthread_mutex_t *mutex, const acl_pthread_mutexattr_t *mattr) { char myname[] = "acl_pthread_mutex_init"; char buf[256]; if (mutex == NULL) { acl_msg_error("%s, %s(%d): input invalid", __FILE__, myname, __LINE__); return (-1); } mutex->dynamic = 0; /* Create the mutex, with initial value signaled */ mutex->id = CreateMutex((SECURITY_ATTRIBUTES *) mattr, FALSE, NULL); if (!mutex->id) { acl_msg_error("%s, %s(%d): CreateMutex error(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); acl_myfree(mutex); return (-1); } return (0); }
/* 通知主线程,启动一个服务 */ static int proctl_monitor_cmd_start(ACL_VSTREAM *client, const char *filepath, const char *args) { const char *myname = "proctl_monitor_cmd_start"; ACL_VSTRING *cmdline; PROCTL_SERVICE *service; PROCTL_MSG *msg; if (filepath[0] == 0) { acl_vstream_fprintf(client, "-ERR|filepath null\r\n"); acl_msg_error("%s(%d): no filepath", myname, __LINE__); return (-1); } if (proctl_service_exist(filepath)) { acl_msg_error("%s(%d): child(%s) maybe be running!", myname, __LINE__, filepath); acl_vstream_fprintf(client, "-ERR|child(%s) maybe be running!\r\n", filepath); return (-1); } cmdline = acl_vstring_alloc(256); acl_vstring_strcpy(cmdline, "\""); acl_vstring_strcat(cmdline, filepath); acl_vstring_strcat(cmdline, "\""); if (args && *args) { acl_vstring_strcat(cmdline, " "); acl_vstring_strcat(cmdline, args); } service = proctl_service_alloc(filepath, cmdline); msg = proctl_msg_new(PROCTL_MSG_START); msg->service = service; proctl_msg_push(msg); return (0); }
/* Create a semaphore */ ACL_SEM *acl_sem_create2(const char *pathname, unsigned int initial_value) { const char *myname = "acl_sem_create2"; ACL_SEM *sem; /* Allocate sem memory */ sem = (ACL_SEM *) acl_mymalloc(sizeof(*sem)); if (sem == NULL) { acl_msg_error("%s, %s(%d): malloc error(%s)", __FILE__, myname, __LINE__, acl_last_serror()); return NULL; } /* Create the semaphore, with max value 32K */ sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, pathname); sem->count = initial_value; if (!sem->id) { acl_msg_error("%s, %s(%d): Couldn't create semaphore(%s)", __FILE__, myname, __LINE__, acl_last_serror()); acl_myfree(sem); return NULL; } return sem; }
int acl_pipe(ACL_FILE_HANDLE fds[2]) { const char *myname = "acl_pipe"; DWORD dwPipeMode; DWORD dwOpenMode; char name[250]; SECURITY_ATTRIBUTES sa; static unsigned long id = 0; InterlockedIncrement(&id); sa.nLength = sizeof(sa); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; /* Create the read/write end of the pipe */ dwOpenMode = PIPE_ACCESS_DUPLEX; dwPipeMode = PIPE_WAIT; /* PIPE_NOWAIT; */ snprintf(name, sizeof(name), "\\\\.\\pipe\\acl-pipe-%u-%lu-%lu", _getpid(), (unsigned long) acl_pthread_self(), id); fds[0] = CreateNamedPipe(name, dwOpenMode, dwPipeMode, 1, /* nMaxInstances */ 65536, /* nOutBufferSize */ 65536, /* nInBufferSize */ 1, /* nDefaultTimeOut */ &sa); /* Create the read/write end of the pipe */ dwOpenMode = FILE_ATTRIBUTE_NORMAL; fds[1] = CreateFile(name, GENERIC_WRITE | GENERIC_READ, /* access mode */ 0, /* share mode */ NULL, /* Security attributes */ OPEN_EXISTING, /* dwCreationDisposition */ dwOpenMode, /* Pipe attributes */ NULL); /* handle to template file */ if (fds[1] == ACL_FILE_INVALID) { acl_msg_error("%s(%d): CreateFile(%s) error(%s)", myname, __LINE__, name, acl_last_serror()); acl_file_close(fds[0]); return (-1); } return (0); }
bool CDelBOM::DeleteBOM(CString& filePath) { char* sBuf; sBuf = acl_vstream_loadfile(filePath); if (sBuf == NULL) return false; size_t len = strlen(sBuf); if (len < 3) { acl_myfree(sBuf); return false; } // 先判断文件内容前缀是否是BOM格式 if (sBuf[0] != (char) 0xEF || sBuf[1] != (char) 0xBB || sBuf[2] != (char) 0xBF) { acl_myfree(sBuf); return false; } // 将内容指针偏移 3 个字节,即去掉BOM格式 len -= 3; char* ptr = sBuf + 3; ACL_VSTREAM* fp = acl_vstream_fopen(filePath, O_WRONLY | O_APPEND | O_TRUNC, 0600, 4096); if (fp == NULL) { acl_myfree(sBuf); return false; } else if (len == 0) { acl_vstream_fclose(fp); acl_myfree(sBuf); return true; } else if (acl_vstream_writen(fp, ptr, len) == ACL_VSTREAM_EOF) { acl_msg_error("write to file: %s error: %s", filePath.GetString(), acl_last_serror()); } acl_vstream_fclose(fp); acl_myfree(sBuf); return true; }
static int io_close_fn(ACL_ASTREAM *astream, void *context) { const char *myname = "io_close_fn"; CLIENT_ENTRY *entry = (CLIENT_ENTRY *) context; ACL_VSTREAM *stream; stream = acl_aio_vstream(astream); if (stream) acl_debug(22, 2) ("%s: error to stream", myname); else acl_msg_error("not connected"); forward_complete(entry); return (-1); }
void http_util_set_dump_stream(HTTP_UTIL *http_util, ACL_VSTREAM *stream) { const char *myname = "http_util_set_dump_stream"; if (stream == NULL) return; if ((http_util->flag & HTTP_UTIL_FLAG_SET_DUMP_FILE)) { acl_msg_error("%s(%d): You've called http_util_set_dump_file before!", myname, __LINE__); return; } http_util->dump_stream = stream; http_util->flag |= HTTP_UTIL_FLAG_SET_DUMP_STREAM; }