コード例 #1
0
ファイル: lwresd.c プロジェクト: gosudream/netbsd-src
static isc_result_t
configure_listener(isc_sockaddr_t *address, ns_lwresd_t *lwresd,
		   isc_mem_t *mctx, ns_lwreslistenerlist_t *newlisteners)
{
	ns_lwreslistener_t *listener, *oldlistener = NULL;
	char socktext[ISC_SOCKADDR_FORMATSIZE];
	isc_result_t result;

	(void)find_listener(address, &oldlistener);
	listener = NULL;
	result = listener_create(mctx, lwresd, &listener);
	if (result != ISC_R_SUCCESS) {
		isc_sockaddr_format(address, socktext, sizeof(socktext));
		isc_log_write(ns_g_lctx, ISC_LOGCATEGORY_GENERAL,
			      NS_LOGMODULE_LWRESD, ISC_LOG_WARNING,
			      "lwres failed to configure %s: %s",
			      socktext, isc_result_totext(result));
		return (result);
	}

	/*
	 * If there's already a listener, don't rebind the socket.
	 */
	if (oldlistener == NULL) {
		result = listener_bind(listener, address);
		if (result != ISC_R_SUCCESS) {
			ns_lwreslistener_detach(&listener);
			return (ISC_R_SUCCESS);
		}
	} else
		listener_copysock(oldlistener, listener);

	result = listener_startclients(listener);
	if (result != ISC_R_SUCCESS) {
		isc_sockaddr_format(address, socktext, sizeof(socktext));
		isc_log_write(ns_g_lctx, ISC_LOGCATEGORY_GENERAL,
			      NS_LOGMODULE_LWRESD, ISC_LOG_WARNING,
			      "lwres: failed to start %s: %s", socktext,
			      isc_result_totext(result));
		ns_lwreslistener_detach(&listener);
		return (ISC_R_SUCCESS);
	}

	if (oldlistener != NULL) {
		/*
		 * Remove the old listener from the old list and shut it down.
		 */
		ISC_LIST_UNLINK(listeners, oldlistener, link);
		listener_shutdown(oldlistener);
		ns_lwreslistener_detach(&oldlistener);
	} else {
		isc_sockaddr_format(address, socktext, sizeof(socktext));
		isc_log_write(ns_g_lctx, ISC_LOGCATEGORY_GENERAL,
			      NS_LOGMODULE_LWRESD, ISC_LOG_NOTICE,
			      "lwres listening on %s", socktext);
	}

	ISC_LIST_APPEND(*newlisteners, listener, link);
	return (result);
}
コード例 #2
0
ファイル: start.c プロジェクト: arthurphilippe/zappy
/*
** Prepares the selector for the game
*/
static int bstrap_stor(selector_t **stor, parser_t *parser)
{
	*stor = selector_create();
	if (!(*stor))
		return (84);
	(*stor)->s_data = game_create(parser->width, parser->height,
		parser->freq, parser->client_nb);
	if (!(*stor)->s_data) {
		selector_delete((*stor));
		return (84);
	}
	(*stor)->s_delete = game_delete;
	(*stor)->s_on_cycle = game_on_cycle;
	add_teams((*stor)->s_data, parser->team_name);
	if (listener_create((*stor), parser->port)) {
		perror("listener");
		selector_delete((*stor));
		return (84);
	}
	return (0);
}
コード例 #3
0
ファイル: network_thread.c プロジェクト: jyelloz/swupdate
void *network_thread (void *data)
{
    struct installer *instp = (struct installer *)data;
    int ctrllisten, ctrlconnfd;
    socklen_t clilen;
    struct sockaddr_un cliaddr;
    ipc_message msg;
    int nread;
    struct msg_elem *notification;
    int ret;

    if (!instp) {
        TRACE("Fatal error: Network thread aborting...");
        return (void *)0;
    }

    SIMPLEQ_INIT(&notifymsgs);
    register_notifier(network_notifier);

    /* Initialize and bind to UDS */
    ctrllisten = listener_create(SOCKET_CTRL_PATH, SOCK_STREAM);
    if (ctrllisten < 0 ) {
        TRACE("Error creating IPC sockets");
        exit(2);
    }

    do {
        clilen = sizeof(cliaddr);
        if ( (ctrlconnfd = accept(ctrllisten, (struct sockaddr *) &cliaddr, &clilen)) < 0) {
            if (errno == EINTR)
                continue;
            else {
                TRACE("Accept returns: %s", strerror(errno));
                continue;
            }
        }
        nread = read(ctrlconnfd, (void *)&msg, sizeof(msg));

        if (nread != sizeof(msg)) {
            TRACE("IPC message too short: fragmentation not supported");
            close(ctrlconnfd);
            continue;
        }
#ifdef DEBUG_IPC
        TRACE("request header: magic[0x%08X] type[0x%08X]", msg.magic, msg.type);
#endif

        pthread_mutex_lock(&stream_mutex);
        if (msg.magic == IPC_MAGIC)  {
            switch (msg.type) {
            case REQ_INSTALL:
                TRACE("Incoming network request: processing...");
                if (instp->status == IDLE) {
                    instp->fd = ctrlconnfd;
                    msg.type = ACK;

                    /* Drop all old notification from last run */
                    cleanum_msg_list();

                    /* Wake-up the installer */
                    pthread_cond_signal(&stream_wkup);
                } else {
                    msg.type = NACK;
                    sprintf(msg.data.msg, "Installation in progress");
                }
                break;
            case GET_STATUS:
                msg.type = GET_STATUS;
                memset(msg.data.msg, 0, sizeof(msg.data.msg));
                msg.data.status.current = instp->status;
                msg.data.status.last_result = instp->last_install;
                msg.data.status.error = instp->last_error;

                /* Get first notification from the queue */
                pthread_mutex_lock(&msglock);
                notification = SIMPLEQ_FIRST(&notifymsgs);
                if (notification) {
                    SIMPLEQ_REMOVE_HEAD(&notifymsgs, next);
                    nrmsgs--;
                    strncpy(msg.data.status.desc, notification->msg,
                            sizeof(msg.data.status.desc) - 1);
#ifdef DEBUG_IPC
                    printf("GET STATUS: %s\n", msg.data.status.desc);
#endif
                    msg.data.status.current = notification->status;
                    msg.data.status.error = notification->error;
                }
                pthread_mutex_unlock(&msglock);

                break;
            default:
                msg.type = NACK;
            }
        } else {
            /* Wrong request */
            msg.type = NACK;
            sprintf(msg.data.msg, "Wrong request: aborting");
        }
        ret = write(ctrlconnfd, &msg, sizeof(msg));
        if (ret < 0)
            printf("Error write on socket ctrl");

        if (msg.type != ACK)
            close(ctrlconnfd);
        pthread_mutex_unlock(&stream_mutex);
    } while (1);
    return (void *)0;
}