Example #1
0
int fpm_sockets_init_main() /* {{{ */
{
	unsigned i, lq_len;
	struct fpm_worker_pool_s *wp;
	char *inherited = getenv("FPM_SOCKETS");
	struct listening_socket_s *ls;

	if (0 == fpm_array_init(&sockets_list, sizeof(struct listening_socket_s), 10)) {
		return -1;
	}

	/* import inherited sockets */
	while (inherited && *inherited) {
		char *comma = strchr(inherited, ',');
		int type, fd_no;
		char *eq;

		if (comma) {
			*comma = '\0';
		}

		eq = strchr(inherited, '=');
		if (eq) {
			*eq = '\0';
			fd_no = atoi(eq + 1);
			type = fpm_sockets_domain_from_address(inherited);
			zlog(ZLOG_NOTICE, "using inherited socket fd=%d, \"%s\"", fd_no, inherited);
			fpm_sockets_hash_op(fd_no, 0, inherited, type, FPM_STORE_SOCKET);
		}

		if (comma) {
			inherited = comma + 1;
		} else {
			inherited = 0;
		}
	}

	/* create all required sockets */
	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
		switch (wp->listen_address_domain) {
			case FPM_AF_INET :
				wp->listening_socket = fpm_socket_af_inet_listening_socket(wp);
				break;

			case FPM_AF_UNIX :
				if (0 > fpm_unix_resolve_socket_premissions(wp)) {
					return -1;
				}
				wp->listening_socket = fpm_socket_af_unix_listening_socket(wp);
				break;
		}

		if (wp->listening_socket == -1) {
			return -1;
		}

	if (wp->listen_address_domain == FPM_AF_INET && fpm_socket_get_listening_queue(wp->listening_socket, NULL, &lq_len) >= 0) {
			fpm_scoreboard_update(-1, -1, -1, (int)lq_len, -1, -1, 0, FPM_SCOREBOARD_ACTION_SET, wp->scoreboard);
		}
	}

	/* close unused sockets that was inherited */
	ls = sockets_list.data;

	for (i = 0; i < sockets_list.used; ) {
		if (ls->refcount == 0) {
			close(ls->sock);
			if (ls->type == FPM_AF_UNIX) {
				unlink(ls->key);
			}
			free(ls->key);
			fpm_array_item_remove(&sockets_list, i);
		} else {
			++i;
			++ls;
		}
	}

	if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_sockets_cleanup, 0)) {
		return -1;
	}
	return 0;
}
Example #2
0
int fpm_sockets_init_main()
{
	int i;
	struct fpm_worker_pool_s *wp;
	char *inherited = getenv("FPM_SOCKETS");
	struct listening_socket_s *ls;

	if (0 == fpm_array_init(&sockets_list, sizeof(struct listening_socket_s), 10)) {
		return -1;
	}

	/* import inherited sockets */
	while (inherited && *inherited) {
		char *comma = strchr(inherited, ',');
		int type, fd_no;
		char *eq;

		if (comma) *comma = '\0';

		eq = strchr(inherited, '=');

		if (eq) {
			*eq = '\0';

			fd_no = atoi(eq + 1);

			type = fpm_sockets_domain_from_address(inherited);

			zlog(ZLOG_STUFF, ZLOG_NOTICE, "using inherited socket fd=%d, \"%s\"", fd_no, inherited);

			fpm_sockets_hash_op(fd_no, 0, inherited, type, FPM_STORE_SOCKET);
		}

		if (comma) inherited = comma + 1;
		else inherited = 0;
	}

	/* create all required sockets */
	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {

		if (!wp->is_template) {

			switch (wp->listen_address_domain) {

				case FPM_AF_INET :

					wp->listening_socket = fpm_socket_af_inet_listening_socket(wp);
					zlog(ZLOG_STUFF, ZLOG_NOTICE, "master process %d is listening sockfd %d ", getpid(), wp->listening_socket );
					break;

				case FPM_AF_UNIX :

					if (0 > fpm_unix_resolve_socket_premissions(wp)) {
						return -1;
					}

					wp->listening_socket = fpm_socket_af_unix_listening_socket(wp);
					break;

			}

			if (wp->listening_socket == -1) {
				return -1;
			}
		}

	}


	/* close unused sockets that was inherited */
	ls = sockets_list.data;

	for (i = 0; i < sockets_list.used; ) {

		if (ls->refcount == 0) {
			close(ls->sock);
			if (ls->type == FPM_AF_UNIX) {
				unlink(ls->key);
			}
			free(ls->key);
			fpm_array_item_remove(&sockets_list, i);
		}
		else {
			++i;
			++ls;
		}
	}

	fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_sockets_cleanup, 0);

	return 0;
}