コード例 #1
0
ファイル: proctl_service.c プロジェクト: bygreencn/acl
int proctl_service_wait(void)
{
	const char *myname = "proctl_service_wait";
	DWORD timeout = 1000 * 2, ret;
	char  ebuf[256];
	HANDLE handle_sem;

	if (__cur_handle == 0)
		return (0);

	/* Create the semaphore, with max value 32K */
	handle_sem = CreateSemaphore(NULL, 0, 32 * 1024, NULL);
	while (1) {
		ret = WaitForMultipleObjects(__cur_handle, __handles, FALSE, timeout);
		if (ret == WAIT_OBJECT_0) {
			proctl_msg_main();
		} else if (ret == WAIT_FAILED) {
			acl_msg_error("%s(%d): wait child object error(%s)",
				myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf)));
			return (-1);
		} else if (ret != WAIT_TIMEOUT)
			break;
	}

	acl_debug(ACL_DEBUG_PROCTL, 2) ("%s(%d): __cur_handle=%d",
		myname, __LINE__, __cur_handle);
	return (0);
}
コード例 #2
0
int acl_socket_init(void)
{
	const char *myname = "acl_socket_init";
	WORD version = 0;
	WSADATA data;
	char  ebuf[256];

	if (__socket_inited) {
		acl_msg_warn("%s(%d): has been inited", myname, __LINE__);
		return 0;
	}

	__socket_inited = 1;

	FillMemory(&data, sizeof(WSADATA), 0);

	version = MAKEWORD(2, 0);

	if (WSAStartup(version, &data) != 0) {
		acl_msg_error("%s(%d): WSAStartup error(%s)",
			myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf)));
		return -1;
	}
	if (LOBYTE(data.wVersion) != 2 || HIBYTE(data.wVersion) != 0) {
		WSACleanup();
		acl_msg_error("%s(%d): LOBYTE(data.wVersion) = %d"
			", HIBYTE(data.wVersion) = %d", myname, __LINE__,
			LOBYTE(data.wVersion), HIBYTE(data.wVersion));
		return -1;
	}

	__socket_ended = 0;
	return (0);
}
コード例 #3
0
ファイル: events_select_thr.c プロジェクト: aaronshang/acl
ACL_EVENT *event_new_select_thr(void)
{
	const char *myname = "event_new_select_thr";
	EVENT_SELECT_THR *event_thr;
	int   status;
	char  ebuf[256];

	event_thr = (EVENT_SELECT_THR*) event_alloc(sizeof(EVENT_SELECT_THR));

	snprintf(event_thr->event.event.name, sizeof(event_thr->event.event.name),
		 "thread events - select");
	event_thr->event.event.event_mode           = ACL_EVENT_SELECT;
	event_thr->event.event.use_thread           = 1;
	event_thr->event.event.loop_fn              = event_loop;
	event_thr->event.event.free_fn              = event_free;
	event_thr->event.event.add_dog_fn           = event_add_dog;
	event_thr->event.event.enable_read_fn       = event_enable_read;
	event_thr->event.event.enable_write_fn      = event_enable_write;
	event_thr->event.event.enable_listen_fn     = event_enable_listen;
	event_thr->event.event.disable_readwrite_fn = event_disable_readwrite;
	event_thr->event.event.isrset_fn            = event_isrset;
	event_thr->event.event.iswset_fn            = event_iswset;
	event_thr->event.event.isxset_fn            = event_isxset;
	event_thr->event.event.timer_request        = event_timer_request_thr;
	event_thr->event.event.timer_cancel         = event_timer_cancel_thr;
	event_thr->event.event.timer_keep           = event_timer_keep_thr;
	event_thr->event.event.timer_ifkeep         = event_timer_ifkeep_thr;

        FD_ZERO(&event_thr->rmask);
        FD_ZERO(&event_thr->wmask);
        FD_ZERO(&event_thr->xmask);

	status = acl_pthread_mutex_init(&event_thr->event.tm_mutex, NULL);
	if (status != 0) {
		acl_msg_fatal("%s(%d)->%s: pthread_mutex_init error=%s",
			__FILE__, __LINE__, myname, acl_last_strerror(ebuf, sizeof(ebuf)));
	}

	status = acl_pthread_mutex_init(&event_thr->event.tb_mutex, NULL);
	if (status != 0) {
		acl_msg_fatal("%s(%d)->%s: pthread_mutex_init error=%s",
			__FILE__, __LINE__, myname, acl_last_strerror(ebuf, sizeof(ebuf)));
	}

	return ((ACL_EVENT *) event_thr);
}
コード例 #4
0
ファイル: acl_timed_wait.c プロジェクト: 10jschen/acl
int acl_timed_waitpid(pid_t pid, ACL_WAIT_STATUS_T *statusp, int options,
	int time_limit)
{
	const char *myname = "timed_waitpid";
	struct sigaction action;
	struct sigaction old_action;
	int     time_left;
	int     wpid;
	char    tbuf[256];

	/*
	 * Sanity checks.
	 */
	if (time_limit <= 0)
		acl_msg_panic("%s: bad time limit: %d", myname, time_limit);

	/*
	 * Set up a timer.
	 */
	sigemptyset(&action.sa_mask);
	action.sa_flags = 0;
	action.sa_handler = timed_wait_alarm;
	if (sigaction(SIGALRM, &action, &old_action) < 0)
		acl_msg_fatal("%s: sigaction(SIGALRM): %s", myname,
			acl_last_strerror(tbuf, sizeof(tbuf)));
	timed_wait_expired = 0;
	time_left = alarm(time_limit);

	/*
	 * Wait for only a limited amount of time.
	 */
	if ((wpid = waitpid(pid, statusp, options)) < 0 && timed_wait_expired)
		acl_set_error(ETIMEDOUT);

	/*
	 * Cleanup.
	 */
	alarm(0);
	if (sigaction(SIGALRM, &old_action, (struct sigaction *) 0) < 0)
		acl_msg_fatal("%s: sigaction(SIGALRM): %s", myname,
			acl_last_strerror(tbuf, sizeof(tbuf)));
	if (time_left)
		alarm(time_left);

	return (wpid);
}
コード例 #5
0
ファイル: acl_loadcfg.c プロジェクト: LazyPlanet/acl
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
}
コード例 #6
0
ファイル: acl_scan_dir.c プロジェクト: kagajubo/acl
acl_int64 acl_scan_dir_rm(const char *pathname, int recursive, int *ndir, int *nfile)
{
	const char *myname = "acl_scan_dir_rmall";
	ACL_SCAN_DIR *scan;
	struct acl_stat sbuf;
	acl_int64 nsize;
	char tbuf[256];

	if (ndir)
		*ndir = 0;
	if (nfile)
		*nfile = 0;

	if (acl_stat(pathname, &sbuf) < 0) {
		acl_msg_error("%s(%d), %s: stat pathname(%s) error(%s)",
			__FILE__, __LINE__, myname, pathname,
			acl_last_strerror(tbuf, sizeof(tbuf)));
		return (-1);
	}
	if (S_ISDIR(sbuf.st_mode) == 0) {
		if (nfile)
			*nfile = 1;
		SANE_UNLINK(pathname);

		return (1);
	}

	scan = acl_scan_dir_open(pathname, recursive);
	if (scan == NULL) {
		acl_msg_error("%s(%d), %s: open path(%s) error(%s)",
			__FILE__, __LINE__, myname,
			pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		return (-1);
	}

	acl_scan_dir_rm2(scan);

	if (ndir)
		*ndir = scan->ndirs;
	if (nfile)
		*nfile = scan->nfiles;
	nsize = scan->nsize;
	acl_scan_dir_close(scan);
	return (nsize);
}
コード例 #7
0
ファイル: acl_unix_listen.c プロジェクト: 1514louluo/acl
ACL_SOCKET acl_unix_listen(const char *addr, int backlog, int block_mode)
{
#undef sun
	struct sockaddr_un sun;
	int     len = strlen(addr);
	ACL_SOCKET  sock;
	char  tbuf[256];

	/*
	 * Translate address information to internal form.
	 */
	if (len >= (int) sizeof(sun.sun_path))
		acl_msg_fatal("unix-domain name too long: %s", addr);
	memset((char *) &sun, 0, sizeof(sun));
	sun.sun_family = AF_UNIX;
#ifdef HAS_SUN_LEN
	sun.sun_len = len + 1;
#endif
	memcpy(sun.sun_path, addr, len + 1);

	/*
	 * Create a listener socket. Do whatever we can so we don't run into
	 * trouble when this process is restarted after crash.
	 */
	if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == ACL_SOCKET_INVALID)
		acl_msg_fatal("socket: %s", acl_last_strerror(tbuf, sizeof(tbuf)));
	(void) unlink(addr);

	if (bind(sock, (struct sockaddr *) & sun, sizeof(sun)) < 0)
		acl_msg_fatal("bind: %s: %s", addr, acl_last_strerror(tbuf, sizeof(tbuf)));
#ifdef FCHMOD_UNIX_SOCKETS
	if (fchmod(sock, 0666) < 0)
		acl_msg_fatal("fchmod socket %s: %s", addr,
			acl_last_strerror(tbuf, sizeof(tbuf)));
#else
	if (chmod(addr, 0666) < 0)
		acl_msg_fatal("chmod socket %s: %s", addr,
			acl_last_strerror(tbuf, sizeof(tbuf)));
#endif
	acl_non_blocking(sock, block_mode);
	if (listen(sock, backlog) < 0)
		acl_msg_fatal("listen: %s", acl_last_strerror(tbuf, sizeof(tbuf)));
	return (sock);
}
コード例 #8
0
ファイル: acl_scan_dir.c プロジェクト: kagajubo/acl
void acl_scan_dir_rm2(ACL_SCAN_DIR *scan)
{
	const char *myname = "acl_scan_dir_rm2";
	const char *name;
	char  path[256];
	struct acl_stat sbuf;

	for (;;) {
		if ((name = acl_scan_dir_next(scan)) == NULL) {
			if (scan->current != NULL)
				snprintf(path, sizeof(path), "%s", ACL_SCAN_DIR_PATH(scan));
			else
				path[0] = 0;

			/* ±ØÐëÍ˳ö¸Ã¿ÕĿ¼ºó²Å¿ÉÒÔɾ³ý¸ÃĿ¼ */

			if (acl_scan_dir_pop(scan) == 0) {
				/* ɾ³ý×²ã¿ÕĿ¼ */
				if (path[0] != 0 && SANE_RMDIR(path) == 0)
					scan->ndirs++;
				break;
			}

			/* ɾ³ý¿ÕĿ¼ */
			if (path[0] != 0 && SANE_RMDIR(path) == 0)
				scan->ndirs++;
			continue;
		}

		snprintf(path, sizeof(path), "%s/%s", ACL_SCAN_DIR_PATH(scan), name);

		if (acl_stat(path, &sbuf) < 0) {
			char tbuf[256];
			acl_msg_error("%s(%d), %s: stat file(%s) error(%s)",
				__FILE__, __LINE__, myname,
				path, acl_last_strerror(tbuf, sizeof(tbuf)));
			break;
		}
		if (S_ISDIR(sbuf.st_mode)) {
			scan->file_name[0] = 0;
			if (scan->recursive && acl_scan_dir_push(scan, name) < 0)
				break;
			if (scan->scan_fn && scan->scan_fn(scan, scan->scan_ctx) < 0)
				break;
			continue;
		} else
			ACL_SAFE_STRNCPY(scan->file_name, name, sizeof(scan->file_name));

		if (scan->scan_fn && scan->scan_fn(scan, scan->scan_ctx) < 0)
			break;
		scan->nfiles++;
		scan->nsize += sbuf.st_size;
		SANE_UNLINK(path);
	}
}
コード例 #9
0
ファイル: acl_loadcfg.c プロジェクト: LazyPlanet/acl
static int _cfg_line_dump(ACL_FILE_HANDLE filefd, const ACL_CFG_LINE *cfg_line, const char *delimiter)
{
	char  myname[] = "_cfg_line_dump";
	char *pbuf, *ptr;
	int   dlen = 0, i, j,  n;
	char  tbuf[256];

	n = cfg_line->ncount;
	if (delimiter != NULL)
		j = (int) strlen(delimiter);
	else
		j = 0;

	if (cfg_line->value != NULL) {
		for (i = 0; i < n; i++) {
			if (cfg_line->value[i] == NULL)
				break;
			dlen += (int) strlen(cfg_line->value[i]) + j;
		}
		dlen += 2;   /* for '\n' and '\0' */
		pbuf = (char *) acl_mycalloc(1, dlen);
		ptr = pbuf;
		for (i = 0; i < n; i++) {
			if (cfg_line->value[i] == NULL)
				break;
			if (i < n -1)
				sprintf(ptr, "%s%s", cfg_line->value[i], delimiter);
			else
				sprintf(ptr, "%s", cfg_line->value[i]);
			ptr = ptr + strlen(ptr);
		}
		ptr = ptr + strlen(ptr);
		strcat(ptr, "\n\0");
		i = acl_file_write(filefd, pbuf, strlen(pbuf), 0, NULL, NULL);
		if (i <= 0) {
			printf("%s: can't write pbuf, error=%s\n",
				myname, acl_last_strerror(tbuf, sizeof(tbuf)));
			acl_myfree(pbuf);
			return (-1);
		}
	} else if (cfg_line->pdata != NULL) {
		dlen = (int) strlen(cfg_line->pdata) + 2;
		pbuf = (char *) acl_mycalloc(1, dlen);
		if (pbuf == NULL)
			return (-1);
		sprintf(pbuf, "%s\n", cfg_line->pdata);
		i = acl_file_write(filefd, pbuf, strlen(pbuf), 0, NULL, NULL);
		if (i <= 0)
			return (-1);
	}

	return (0);
}
コード例 #10
0
int acl_change_uid(char *user_name)
{
	char where[] = "change_uid";
	struct passwd *pwd;
	uid_t  uid;
	gid_t  gid;
	char  tbuf[256];

	if ((pwd = getpwnam(user_name)) == NULL)
		acl_msg_fatal("%s: no such user=%s", where, user_name);
	uid = pwd->pw_uid;
	gid = pwd->pw_gid;
	if (setgid(gid) < 0)
		acl_msg_fatal("%s: setgid error(%s, %d): %s",
			where, user_name, uid, acl_last_strerror(tbuf, sizeof(tbuf)));
	if (setuid(uid) < 0)
		acl_msg_fatal("%s: setuid error(%s, %d): %s",
			where, user_name, uid, acl_last_strerror(tbuf, sizeof(tbuf)));

	return (0);
}
コード例 #11
0
void acl_set_ugid(uid_t uid, gid_t gid)
{
	int   saved_error = acl_last_error();
	char  tbuf[256];

	if (geteuid() != 0)
		if (seteuid(0) < 0)
			acl_msg_fatal("seteuid(0): %s", acl_last_strerror(tbuf, sizeof(tbuf)));
	if (setgid(gid) < 0)
		acl_msg_fatal("setgid(%ld): %s", (long) gid,
			acl_last_strerror(tbuf, sizeof(tbuf)));
	if (setgroups(1, &gid) < 0)
		acl_msg_fatal("setgroups(1, &%ld): %s", (long) gid,
			acl_last_strerror(tbuf, sizeof(tbuf)));
	if (setuid(uid) < 0)
		acl_msg_fatal("setuid(%ld): %s", (long) uid,
			acl_last_strerror(tbuf, sizeof(tbuf)));
	if (acl_msg_verbose > 1)
		acl_msg_info("setugid: uid %ld gid %ld", (long) uid, (long) gid);
	acl_set_error(saved_error);
}
コード例 #12
0
ファイル: acl_proctl.c プロジェクト: aaronshang/acl
/* 打开与控制进程的监听线程之间的数据连接 */
static ACL_VSTREAM *proctl_client_open(const char *progname)
{
	const char *myname = "proctl_client_open";
	char  ebuf[256], lock_file[MAX_PATH], addr[256];
	ACL_VSTREAM *client;

	proctl_init(progname);

	get_lock_file(lock_file, sizeof(lock_file));

	if (get_addr_from_file(lock_file, addr, sizeof(addr)) < 0)
		acl_msg_fatal("%s(%d): get addr from file(%s) error(%s)",
		myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));

	client = acl_vstream_connect(addr, ACL_BLOCKING, 10, 10, 1024);
	if (client == NULL)
		acl_msg_fatal("%s(%d): connect addr(%s) error(%s)",
		myname, __LINE__, addr, acl_last_strerror(ebuf, sizeof(ebuf)));

	return (client);
}
コード例 #13
0
ファイル: events_dog.c プロジェクト: aaronshang/acl
static void event_dog_open(EVENT_DOG *evdog)
{
	const char *myname = "event_dog_open";
	const char *addr = "127.0.0.1:0";
	char  ebuf[256];

	evdog->sstream = acl_vstream_listen(addr, 32);
	if (evdog->sstream == NULL)
		acl_msg_fatal("%s(%d): listen on addr(%s) error(%s)",
			myname, __LINE__, addr,
			acl_last_strerror(ebuf, sizeof(ebuf)));

	evdog->server = acl_vstream_connect(evdog->sstream->local_addr,
			ACL_BLOCKING, 0, 0, 1024);
	if (evdog->server == NULL)
		acl_msg_fatal("%s(%d): connect to addr(%s) error(%s)",
			myname, __LINE__, addr,
			acl_last_strerror(ebuf, sizeof(ebuf)));

	if (acl_vstream_writen(evdog->server, ebuf, 1) == ACL_VSTREAM_EOF)
		acl_msg_fatal("%s(%d): pre write error(%s)",
			myname, __LINE__,
			acl_last_strerror(ebuf, sizeof(ebuf)));

	evdog->client = acl_vstream_accept(evdog->sstream, ebuf, sizeof(ebuf));
	if (evdog->client == NULL)
		acl_msg_fatal("%s(%d): accept error(%s)",
			myname, __LINE__,
			acl_last_strerror(ebuf, sizeof(ebuf)));

	if (acl_vstream_readn(evdog->client, ebuf, 1) == ACL_VSTREAM_EOF)
		acl_msg_fatal("%s(%d): pre read error(%s)",
			myname, __LINE__,
			acl_last_strerror(ebuf, sizeof(ebuf)));

	acl_vstream_close(evdog->sstream);
	evdog->sstream = NULL;

	acl_event_enable_read(evdog->eventp, evdog->client, 0, read_fn, evdog);
}
コード例 #14
0
ファイル: acl_proctl.c プロジェクト: aaronshang/acl
void acl_proctl_start_one(const char *progname,
	const char *progchild, int argc, char *argv[])
{
	const char *myname = "acl_proctl_start_one";
	char  ebuf[256], buf[1024];
	ACL_VSTREAM *client;
	ACL_VSTRING *child_args = NULL;
	int   n;

	if (argc > 0) {
		int   i;

		child_args = acl_vstring_alloc(256);
		for (i = 0; i < argc; i++) {
			if (i > 0)
				acl_vstring_strcat(child_args, " ");
			acl_vstring_strcat(child_args, "\"");
			acl_vstring_strcat(child_args, argv[i]);
			acl_vstring_strcat(child_args, "\"");
		}

	}

	/* 打开与控制进程之间的连接 */
	client = proctl_client_open(progname);
	if (child_args) {
		/* 向控制进程发送消息,带有控制参数 */
		n = acl_vstream_fprintf(client, "%s|-d|START|-f|%s|-a|%s\r\n",
				progname, progchild, acl_vstring_str(child_args));
	} else {
		/* 向控制进程发送消息,不带控制参数 */
		n = acl_vstream_fprintf(client, "%s|-d|START|-f|%s\r\n",
				progname, progchild);
	}

	if (child_args != NULL)
		acl_vstring_free(child_args);

	if (n == ACL_VSTREAM_EOF)
		acl_msg_fatal("%s(%d): fprintf to acl_proctl error(%s)",
			myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf)));

	/* 接收所有来自于控制进程的消息响应结果 */
	while (1) {
		n = acl_vstream_gets_nonl(client, buf, sizeof(buf));
		if (n == ACL_VSTREAM_EOF)
			break;
		acl_msg_info("%s(%d): %s", myname, __LINE__, buf);
	}

	proctl_client_close(client);
}
コード例 #15
0
ファイル: proctl_utils.c プロジェクト: aaronshang/acl
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);
}
コード例 #16
0
ファイル: acl_set_eugid.c プロジェクト: aaronshang/acl
void acl_set_eugid(uid_t euid, gid_t egid)
{
	int   saved_error = acl_last_error();
	char  tbuf[256];

	if (geteuid() != 0)
		if (seteuid(0))
			acl_msg_fatal("set_eugid: seteuid(0): %s",
				acl_last_strerror(tbuf, sizeof(tbuf)));
	if (setegid(egid) < 0)
		acl_msg_fatal("set_eugid: setegid(%ld): %s", (long) egid,
			acl_last_strerror(tbuf, sizeof(tbuf)));
	if (setgroups(1, &egid) < 0)
		acl_msg_fatal("set_eugid: setgroups(%ld): %s", (long) egid,
			acl_last_strerror(tbuf, sizeof(tbuf)));
	if (euid != 0 && seteuid(euid) < 0)
		acl_msg_fatal("set_eugid: seteuid(%ld): %s", (long) euid,
			acl_last_strerror(tbuf, sizeof(tbuf)));
	if (acl_msg_verbose)
		acl_msg_info("set_eugid: euid %ld egid %ld",
				(long) euid, (long) egid);
	acl_set_error(saved_error);
}
コード例 #17
0
ファイル: proctl_utils.c プロジェクト: aaronshang/acl
ACL_VSTREAM *local_listen()
{
	const char *myname = "local_listen";
	char  lock_file[MAX_PATH], ebuf[256];
	ACL_VSTREAM *sstream, *fp;
	ACL_FILE_HANDLE handle;

	get_lock_file(lock_file, sizeof(lock_file));

	fp = acl_vstream_fopen(lock_file, O_RDWR | O_CREAT, 0600, 1024);
	if (fp == NULL)
		acl_msg_fatal("%s(%d): open file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));

	handle = ACL_VSTREAM_FILE(fp);
	if (acl_myflock(handle, 0, ACL_MYFLOCK_OP_EXCLUSIVE | ACL_MYFLOCK_OP_NOWAIT) == -1) {
		acl_msg_error("%s(%d): lock file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));
		return (NULL);
	}

	sstream = acl_vstream_listen_ex("127.0.0.1:0", 128, ACL_BLOCKING, 1024, 0);
	if (sstream == NULL)
		acl_msg_fatal("%s(%d): listen error(%s)",
			myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf)));

	if (acl_file_ftruncate(fp, 0) < 0)
		acl_msg_fatal("%s(%d): truncate file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));
	if (acl_vstream_fseek(fp, 0, SEEK_SET) < 0)
		acl_msg_fatal("%s(%d): fseek file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));

	if (acl_vstream_fprintf(fp, "%s\r\n", sstream->local_addr) == ACL_VSTREAM_EOF)
		acl_msg_fatal("%s(%d): fprintf to file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));

	/* XXX: 只能采用先解排它锁,再加共享锁,微软比较弱!!! */

	if (acl_myflock(handle, 0, ACL_MYFLOCK_OP_NONE) == -1)
		acl_msg_fatal("%s(%d): unlock file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));
	if (acl_myflock(handle, 0, ACL_MYFLOCK_OP_SHARED | ACL_MYFLOCK_OP_NOWAIT) == -1)
		acl_msg_fatal("%s(%d): lock file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));
	return (sstream);
}
コード例 #18
0
ファイル: acl_test_inner.c プロジェクト: aaronshang/acl
AUT_LINE *aut_add_inner_cmd(const ACL_CFG_LINE *line)
{
	char  myname[] = "aut_add_inner_cmd";
	AUT_LINE *test_line = NULL;
	__MATCH_CMD *pmatch_cmd;
	AUT_CMD_TOKEN *inner_token;
	int   i;

	if (line->ncount < 1) {
		aut_log_error("%s: ncount=%d", myname, line->ncount);
		return (NULL);
	}

	for (i = 0; __inner_cmd_tab[i].cmd_name != NULL; i++) {
		pmatch_cmd = &__inner_cmd_tab[i];
		if (strcasecmp(line->value[0], pmatch_cmd->cmd_name) == 0) {
			/* 由内部命令保留函数动态分配一个相对应的
			 * AUT_LINE 对象
			 */
			test_line = pmatch_cmd->match_fn(line);
			break;
		}
	}

	if (test_line == NULL)
		return (NULL);

	if (acl_array_append(var_aut_line_array, (void *) test_line) < 0) {
		char  tbuf[256];
		aut_log_fatal("%s: cmd_name=%s, "
			"acl_array_append error, err_msg=%s",
			myname, test_line->cmd_name, acl_last_strerror(tbuf, sizeof(tbuf)));
	}

	/* 设置有效行号 */
	inner_token = (AUT_CMD_TOKEN *) test_line->arg_inner;
	if (inner_token == NULL)
		return (test_line);

	test_line->valid_line_idx = var_aut_valid_line_idx++;

	/* 调整有效行号 */
	inner_token->valid_line_idx = test_line->valid_line_idx;

	/* 只对循环命令起作用, 设置相对命令位移 */
	inner_token->offset_valid_line_idx = inner_token->valid_line_idx;

	return (test_line);
}
コード例 #19
0
ファイル: http_chat_sync.c プロジェクト: FlowSea/acl
/*----------------------------------------------------------------------------*/
static HTTP_CHAT_CTX *new_ctx(void)
{
	const char *myname = "new_ctx";
	HTTP_CHAT_CTX *ctx;

	ctx = (HTTP_CHAT_CTX*) acl_mycalloc(1, sizeof(HTTP_CHAT_CTX));
	if (ctx == NULL) {
		char  ebuf[256];

		acl_msg_fatal("%s, %s(%d): calloc error(%s)",
			__FILE__, myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf)));
	}

	return ctx;
}
コード例 #20
0
ファイル: proctl_monitor.c プロジェクト: 1514louluo/acl
void *proctl_monitor_thread(void *arg)
{
	const char *myname = "proctl_monitor_thread";
	ACL_VSTREAM *sstream;
	char  ebuf[256];

	sstream = local_listen();
	if (sstream == NULL)
		acl_msg_fatal("%s(%d): local_listen return NULL, error(%s)",
			myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf)));
	proctl_monitor_loop(sstream);

	/* unreached */
	return (NULL);
}
コード例 #21
0
ファイル: acl_test_cfg.c プロジェクト: 10jschen/acl
/* 初始化 */
static void __init(void)
{
	const char *myname = "__init";

	if (var_aut_line_array != NULL)
		return;

	var_aut_line_array = acl_array_create(10);
	if (var_aut_line_array == NULL) {
		char tbuf[256];
		aut_log_fatal("%s: acl_array_create error(%s)",
			myname, acl_last_strerror(tbuf, sizeof(tbuf)));
	}
	var_aut_valid_line_idx = 0;
}
コード例 #22
0
ファイル: proctl_service.c プロジェクト: 1514louluo/acl
void proctl_service_init()
{
	const char *myname = "proctl_service_init";
	char  ebuf[256];

	__services = acl_array_create(10);
	__services_wait = acl_fifo_new();
	acl_pthread_mutex_init(&__mutex_running_service, NULL);
	acl_pthread_mutex_init(&__mutex_waiting_service, NULL);
	handles_init();

	__sem_handle = CreateSemaphore(NULL, 0, 1024, NULL);
	if (__sem_handle == NULL)
		acl_msg_fatal("%s(%d): CreateSemaphore error(%s)",
			myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf)));
	handles_add(__sem_handle);
}
コード例 #23
0
ファイル: acl_scan_dir.c プロジェクト: kagajubo/acl
acl_int64 acl_scan_dir_size2(ACL_SCAN_DIR *scan)
{
	const char *myname = "acl_scan_dir_size2";
	const char *name;
	char  pathbuf[256];
	struct acl_stat sbuf;

	if (scan == NULL)
		return (-1);

	while (1) {
		if ((name = acl_scan_dir_next(scan)) == NULL) {
			if (acl_scan_dir_pop(scan) == NULL)
				break;
			continue;
		}
		snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
			ACL_SCAN_DIR_PATH(scan), name);
		if (acl_stat(pathbuf, &sbuf) < 0) {
			char  tbuf[256];
			acl_msg_error("%s(%d), %s: stat file(%s) error(%s)",
				__FILE__, __LINE__, myname,
				pathbuf, acl_last_strerror(tbuf, sizeof(tbuf)));
			break;
		}

		memcpy(&scan->current->sbuf, &sbuf, sizeof(sbuf));

		if (S_ISDIR(sbuf.st_mode)) {
			scan->ndirs++;
			scan->file_name[0] = 0;
			if (scan->recursive && acl_scan_dir_push(scan, name) < 0)
				break;
		} else {
			ACL_SAFE_STRNCPY(scan->file_name, name, sizeof(scan->file_name));
			scan->nfiles++;
			scan->nsize += sbuf.st_size;
		}

		if (scan->scan_fn && scan->scan_fn(scan, scan->scan_ctx) < 0)
			break;
	}

	return (scan->nsize);
}
コード例 #24
0
ファイル: acl_test_runner.c プロジェクト: aaronshang/acl
static void __add_fn_item(ACL_ARRAY *fn_tab, const AUT_FN_ITEM *fn_item, int inner)
{
	char  myname[] = "__add_fn_item";
	AUT_FN_ITEM *item;

	item = (AUT_FN_ITEM *) acl_mycalloc(1, sizeof(AUT_FN_ITEM));
	item->cmd_name    = fn_item->cmd_name;
	item->fn_name     = fn_item->fn_name;
	item->fn_callback = fn_item->fn_callback;
	item->arg         = fn_item->arg;
	item->inner       = inner;

	if (acl_array_append(fn_tab, item) < 0) {
		char tbuf[256];
		aut_log_fatal("%s(%d): array_append error(%s)",
			myname, __LINE__, acl_last_strerror(tbuf, sizeof(tbuf)));
	}
}
コード例 #25
0
ファイル: acl_doze.c プロジェクト: DavadDi/acl
void acl_doze(unsigned delay)
{
#ifdef	ACL_UNIX
    struct timeval tv;

    tv.tv_sec = delay / 1000;
    tv.tv_usec = (delay - tv.tv_sec * 1000) * 1000;
    while (select(0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &tv) < 0)
        if (acl_last_error() != ACL_EINTR) {
            char tbuf[256];
            acl_msg_fatal("doze: select: %s",
                          acl_last_strerror(tbuf, sizeof(tbuf)));
        }
#elif	defined(WIN32)
    Sleep(delay);
#else
#error "unknown OS"
#endif
}
コード例 #26
0
ファイル: acl_myflock.c プロジェクト: 10jschen/acl
/* use LockFile/UnlockFile */
int acl_myflock(ACL_FILE_HANDLE fd, int lock_style acl_unused, int operation)
{
	const char *myname = "acl_myflock";
	DWORD size = 0xFFFFFFFF;
	char  ebuf[256];
	unsigned char lock_op;

	if ((operation & (ACL_FLOCK_OP_BITS)) != operation)
		acl_msg_panic("myflock: improper operation type: 0x%x", operation);

	lock_op = operation & ~ACL_FLOCK_OP_NOWAIT;

	if (lock_op == ACL_FLOCK_OP_NONE) {
		if(UnlockFile(fd, 0, 0, size, 0))
			return (0);

		acl_msg_error("%s(%d): unlock error(%s)",
			myname, __LINE__, acl_last_strerror(ebuf,sizeof(ebuf)));
		return (-1);
	} else if (lock_op == ACL_FLOCK_OP_SHARED) {
		while (1) {
			if(LockFile(fd, 0, 0, size, 0))
				return (0);

			if ((operation & ACL_FLOCK_OP_NOWAIT))
				return (-1);
			sleep(1);
		}
	} else if (lock_op == ACL_FLOCK_OP_EXCLUSIVE) {
		while (1) {
			if(LockFile(fd, 0, 0, size, 0))
				return (0);

			if ((operation & ACL_FLOCK_OP_NOWAIT))
				return (-1);
			sleep(1);
		}
	}

	acl_msg_error("%s(%d): invalid lock_op(%d)", myname, __LINE__, lock_op);
	return (-1);
}
コード例 #27
0
ファイル: acl_pthread_mutex.c プロジェクト: aaronshang/acl
int acl_pthread_mutex_unlock(acl_pthread_mutex_t *mutex)
{
	char  myname[] = "acl_pthread_mutex_unlock";
	char  buf[256];

	if (mutex == NULL) {
		acl_msg_error("%s, %s(%d): input invalid",
				__FILE__, myname, __LINE__);
		return (-1);
	}

	if (ReleaseMutex(mutex->id) == FALSE) {
		acl_msg_error("%s, %s(%d): ReleaseMutex error(%s)",
				__FILE__, myname, __LINE__,
				acl_last_strerror(buf, sizeof(buf)));
		return (-1);
	}

	return (0);
}
コード例 #28
0
ファイル: acl_pthread_mutex.c プロジェクト: aaronshang/acl
int acl_pthread_mutex_lock(acl_pthread_mutex_t *mutex)
{
	char  myname[] = "acl_pthread_mutex_lock";
	char  buf[256];

	if (mutex == NULL) {
		acl_msg_error("%s, %s(%d): input invalid",
				__FILE__, myname, __LINE__);
		return (-1);
	}

	if (WaitForSingleObject(mutex->id, INFINITE) == WAIT_FAILED) {
		acl_msg_error("%s, %s(%d): WaitForSingleObject error(%s)",
				__FILE__, myname, __LINE__,
				acl_last_strerror(buf, sizeof(buf)));
		return (-1);
	}

	return (0);
}
コード例 #29
0
ファイル: main.cpp プロジェクト: 1514louluo/acl
static int vstream_client(void)
{
	const char *myname = "vstream_client";
	ACL_VSTREAM *client;
	char  ebuf[256], buf[4096];
	int   n, dlen = 0;

	printf("addr: %s, timeout: %d\n", addr, timeout);
	client = acl_vstream_connect(addr, ACL_NON_BLOCKING, timeout, timeout, 1024);
	if (client == NULL) {
		printf("%s(%d): connect addr %s error(%s)\n",
			myname, __LINE__, addr, acl_last_strerror(ebuf, sizeof(ebuf)));
		return (-1);
	}

	printf("%s: connect %s ok\r\n", myname, addr);
	acl_non_blocking(ACL_VSTREAM_SOCK(client), ACL_BLOCKING);

	n = acl_write_wait(ACL_VSTREAM_SOCK(client), 10);
	if (n < 0) {
		printf("connect timeout: %s\n", acl_last_serror());
		goto END;
	}
	acl_vstream_fprintf(client, "hello world\n");
	while (1) {
		n = acl_vstream_read(client, buf, sizeof(buf));
		if (n == ACL_VSTREAM_EOF) {
			printf("read over: %s\n", acl_last_serror());
			break;
		}
		dlen += n;
		buf[n] = 0;
		printf("read reply: %s\n", buf);
	}

END:
	acl_vstream_close(client);
	printf("%s: read %d\n", myname, dlen);
	return (0);
}
コード例 #30
0
ファイル: acl_scan_dir.c プロジェクト: kagajubo/acl
ACL_SCAN_DIR *acl_scan_dir_pop(ACL_SCAN_DIR *scan)
{
	const char *myname = "acl_scan_dir_pop";
	ACL_SCAN_INFO *info = scan->current;
	ACL_SCAN_INFO *parent;

	if (info == NULL)
		return (NULL);
	parent = info->parent;
	if (closedir(info->dir_name)) {
		char tbuf[256];
		acl_msg_fatal("%s(%d), %s: close directory(%s) error(%s)",
			__FILE__, __LINE__, myname,
			info->path, acl_last_strerror(tbuf, sizeof(tbuf)));
	}
	if (acl_msg_verbose > 1)
		acl_msg_info("%s: close %s", myname, info->path);
	acl_myfree(info->path);
	acl_myfree(info);
	scan->current = parent;
	return (parent ? scan : NULL);
}