예제 #1
0
파일: nbd.c 프로젝트: 16aug/nvmeqemu
int unix_socket_incoming(const char *path)
{
    char *ostr = NULL;
    int olen = 0;

    return unix_listen(path, ostr, olen);
}
예제 #2
0
파일: t2env.c 프로젝트: romildo/jgmenu
void tint2env_init_socket(void)
{
	socketfile_unlink();
	sfd = unix_listen(TINT2_SOCKET_PATH, 5);
	if (sfd == -1)
		die("unix_listen");
	make_sfd_nonblocking();
	atexit(socketfile_unlink);
}
예제 #3
0
파일: qemu-nbd.c 프로젝트: LingxiaoJIA/qemu
static int unix_socket_incoming(const char *path)
{
    Error *local_err = NULL;
    int fd = unix_listen(path, NULL, 0, &local_err);

    if (local_err != NULL) {
        error_report_err(local_err);
    }
    return fd;
}
예제 #4
0
파일: qemu-nbd.c 프로젝트: vicamo/qemu
static int unix_socket_incoming(const char *path)
{
    Error *local_err = NULL;
    int fd = unix_listen(path, NULL, 0, &local_err);

    if (local_err != NULL) {
        error_report("%s", error_get_pretty(local_err));
        error_free(local_err);
    }
    return fd;
}
예제 #5
0
void unix_start_incoming_migration(const char *path, Error **errp)
{
    int s;

    s = unix_listen(path, NULL, 0, errp);
    if (s < 0) {
        return;
    }

    qemu_set_fd_handler2(s, NULL, unix_accept_incoming_migration, NULL,
                         (void *)(intptr_t)s);
}
예제 #6
0
int main(int argc, char *argv[])
{
	GOptionContext *context;
	GError *err = NULL;
	struct sigaction sa;
	guint server_id;

	context = g_option_context_new(NULL);
	g_option_context_add_main_entries(context, options, NULL);

	g_option_context_parse(context, &argc, &argv, &err);
	if (err != NULL) {
		g_printerr("%s\n", err->message);
		g_error_free(err);
		exit(EXIT_FAILURE);
	}

	if (option_root && chdir(option_root) < 0) {
		perror("chdir:");
		exit(EXIT_FAILURE);
	}

	if (option_bluetooth)
		server_id = bluetooth_listen();
	else
		server_id = unix_listen();

	if (server_id == 0)
		exit(EXIT_FAILURE);

	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = sig_term;
	sigaction(SIGINT, &sa, NULL);
	sigaction(SIGTERM, &sa, NULL);

	main_loop = g_main_loop_new(NULL, FALSE);

	g_main_loop_run(main_loop);

	g_source_remove(server_id);
	g_slist_free_full(clients, (GDestroyNotify) g_obex_unref);
	g_option_context_free(context);
	g_main_loop_unref(main_loop);

	exit(EXIT_SUCCESS);
}
예제 #7
0
파일: ircd.c 프로젝트: Ykstort/IRCdaemon
static void
ircd ()
{
    struct ev_loop *loop;

    /* reopn stderr to log to ircd.err */
    logfile = freopen("ircd.err", "w", stderr);
    if (logfile == NULL)
    {
        fprintf(stderr, "freopen: ircd.err: %s\n", strerror(errno));
        fprintf(stderr, "logging to stderr instead\n");
    }

    /* bind/listen server socket */
    server_fd = unix_listen(IRCD_HOST, IRCD_PORT);
    if (server_fd == -1)
    {
        fprintf(stderr, "Could not register socket, exiting.");
        exit(1);
    }

    /* init libev default loop */
    loop = ev_default_loop(0);

    /* set up libev callback for incoming connections */
    ev_io_init(&server_w, &server_cb, server_fd, EV_READ);
    ev_io_start(EV_A_ &server_w);
    
    /* ignore some signals, catch TERM with our own handler, consider using
     * libev for the SIGTERM callback for consistency's sake */
    signal(SIGPIPE, SIG_IGN);
    signal(SIGHUP, SIG_IGN);
    ev_signal_init(&sigterm_w, &sigterm_cb, SIGTERM);
    ev_signal_start(EV_A_ &sigterm_w);

    /* enter main loop */
    ev_run(EV_A_ 0);

    /* clean server state here, in case later on I decide I want to handle
     * SIGHUP to reboot the server without exiting the process or something */
    ev_io_stop(EV_A_ &server_w);
    
    /* close socket */
    close(server_fd);
}
예제 #8
0
void searchdaemon::go() {

	/* let search engine get at logger */
	engine.setlogger(&_log);

	/* fork off listeners */
	_log.lprintf( logger::DEBUG, "Looks good: %d\n", getpid() );
	childunix=0;
	childinet=0;

	/* fork off a unix listener .. */
	if( _options.get_bool( "ListenUnix" ) ) {
		_log.lprintf( logger::DEBUG, "Forking UNIX domain listener\n" );
		if( (childunix=fork())==0 ) {
			listener unix_listen( _log, _options.get_string( "UnixSocket" ) );
			listenloop( unix_listen );
		}
	}

	/* .. and an inet listeneer */
	if( _options.get_bool( "ListenInet" ) ) {
		_log.lprintf( logger::DEBUG, "Forking inet domain listener\n" );
		if( (childinet=fork())==0 ) {
			listener inet_listen( _log, _options.get_string( "BindAddress" ), _options.get_int( "ListenPort" ) );
			listenloop( inet_listen );
		}
	}

	signal( SIGTERM, handler_term );

	/* now just chill for a bit */
   while( childunix != 0 || childinet != 0 ) {
     pid_t child = wait( NULL );
     if( child==childunix ) childunix = 0;
     if( child==childinet ) childinet = 0;
   }
	exit( 0 );
}
예제 #9
0
static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod method)
{
    int ret;
    c->method = method;

    switch (c->method) {
    case GA_CHANNEL_VIRTIO_SERIAL: {
        int fd = qemu_open(path, O_RDWR | O_NONBLOCK
#ifndef CONFIG_SOLARIS
                           | O_ASYNC
#endif
                           );
        if (fd == -1) {
            g_critical("error opening channel: %s", strerror(errno));
            return false;
        }
#ifdef CONFIG_SOLARIS
        ret = ioctl(fd, I_SETSIG, S_OUTPUT | S_INPUT | S_HIPRI);
        if (ret == -1) {
            g_critical("error setting event mask for channel: %s",
                       strerror(errno));
            close(fd);
            return false;
        }
#endif
        ret = ga_channel_client_add(c, fd);
        if (ret) {
            g_critical("error adding channel to main loop");
            close(fd);
            return false;
        }
        break;
    }
    case GA_CHANNEL_ISA_SERIAL: {
        struct termios tio;
        int fd = qemu_open(path, O_RDWR | O_NOCTTY | O_NONBLOCK);
        if (fd == -1) {
            g_critical("error opening channel: %s", strerror(errno));
            return false;
        }
        tcgetattr(fd, &tio);
        /* set up serial port for non-canonical, dumb byte streaming */
        tio.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP |
                         INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY |
                         IMAXBEL);
        tio.c_oflag = 0;
        tio.c_lflag = 0;
        tio.c_cflag |= GA_CHANNEL_BAUDRATE_DEFAULT;
        /* 1 available byte min or reads will block (we'll set non-blocking
         * elsewhere, else we have to deal with read()=0 instead)
         */
        tio.c_cc[VMIN] = 1;
        tio.c_cc[VTIME] = 0;
        /* flush everything waiting for read/xmit, it's garbage at this point */
        tcflush(fd, TCIFLUSH);
        tcsetattr(fd, TCSANOW, &tio);
        ret = ga_channel_client_add(c, fd);
        if (ret) {
            g_critical("error adding channel to main loop");
            close(fd);
            return false;
        }
        break;
    }
    case GA_CHANNEL_UNIX_LISTEN: {
        Error *local_err = NULL;
        int fd = unix_listen(path, NULL, strlen(path), &local_err);
        if (local_err != NULL) {
            g_critical("%s", error_get_pretty(local_err));
            error_free(local_err);
            return false;
        }
        ga_channel_listen_add(c, fd, true);
        break;
    }
    default:
        g_critical("error binding/listening to specified socket");
        return false;
    }

    return true;
}
예제 #10
0
파일: bossan_ext.c 프로젝트: henteko/bossan
static VALUE
bossan_run_loop(int argc, VALUE *argv, VALUE self)
{
  int ret;
  VALUE args1, args2, args3;

  rb_scan_args(argc, argv, "21", &args1, &args2, &args3);

  if(listen_sock > 0){
    rb_raise(rb_eException, "already set listen socket");
  }

  if (argc == 3){
    server_name = StringValuePtr(args1);
    server_port = NUM2INT(args2);

    long _port = NUM2INT(args2);

    if (_port <= 0 || _port >= 65536) {
      // out of range
      rb_raise(rb_eArgError, "port number outside valid range");
    }

    server_port = (short)_port;

    ret = inet_listen();
    rack_app = args3;
  } else {
    Check_Type(args1, T_STRING);
    ret = unix_listen(StringValuePtr(args1));
    rack_app = args2;
  }

  if(ret < 0){
    //error
    listen_sock = -1;
  }

  if(listen_sock <= 0){
    rb_raise(rb_eTypeError, "not found listen socket");
  }
    
  /* init picoev */
  picoev_init(MAX_FDS);
  /* create loop */
  main_loop = picoev_create_loop(60);
  loop_done = 1;
  
  setsig(SIGPIPE, sigpipe_cb);
  setsig(SIGINT, sigint_cb);
  setsig(SIGTERM, sigint_cb);
    
  picoev_add(main_loop, listen_sock, PICOEV_READ, ACCEPT_TIMEOUT_SECS, accept_callback, NULL);
    
  /* loop */
  while (loop_done) {
    picoev_loop_once(main_loop, 10);
  }
  picoev_destroy_loop(main_loop);
  picoev_deinit();

  printf("Bye.\n");
  return Qnil;
}
예제 #11
0
int     main(int argc, char **argv)
{
    int     sock;
    int     backlog;
    int     ch;
    int     ttl;
    const char *protocols = INET_PROTO_NAME_ALL;

    /*
     * Fingerprint executables and core dumps.
     */
    MAIL_VERSION_STAMP_ALLOCATE;

    /*
     * Fix 20051207.
     */
    signal(SIGPIPE, SIG_IGN);

    /*
     * Initialize diagnostics.
     */
    msg_vstream_init(argv[0], VSTREAM_ERR);

    /*
     * Parse JCL.
     */
    while ((ch = GETOPT(argc, argv, "46cvx:")) > 0) {
	switch (ch) {
	case '4':
	    protocols = INET_PROTO_NAME_IPV4;
	    break;
	case '6':
	    protocols = INET_PROTO_NAME_IPV6;
	    break;
	case 'c':
	    count_deliveries++;
	    break;
	case 'v':
	    msg_verbose++;
	    break;
	case 'x':
	    if ((ttl = atoi(optarg)) <= 0)
		usage(argv[0]);
	    event_request_timer(terminate, (void *) 0, ttl);
	    break;
	default:
	    usage(argv[0]);
	}
    }
    if (argc - optind != 2)
	usage(argv[0]);
    if ((backlog = atoi(argv[optind + 1])) <= 0)
	usage(argv[0]);

    /*
     * Initialize.
     */
    (void) inet_proto_init("protocols", protocols);
    buffer = vstring_alloc(1024);
    if (strncmp(argv[optind], "unix:", 5) == 0) {
	sock = unix_listen(argv[optind] + 5, backlog, BLOCKING);
    } else {
	if (strncmp(argv[optind], "inet:", 5) == 0)
	    argv[optind] += 5;
	sock = inet_listen(argv[optind], backlog, BLOCKING);
    }

    /*
     * Start the event handler.
     */
    event_enable_read(sock, connect_event, CAST_INT_TO_VOID_PTR(sock));
    for (;;)
	event_loop(-1);
}
예제 #12
0
파일: scm_rights_recv.c 프로젝트: xirc/tlpi
int
main(int argc, char *argv[])
{
    struct msghdr msgh;
    struct iovec iov;
    int data, lfd, sfd, fd, opt;
    ssize_t nr;
    int use_datagram_socket;
    union {
        struct cmsghdr cmh;
        char   control[CMSG_SPACE(sizeof(int))];
                        /* Space large enough to hold an 'int' */
    } control_un;
    struct cmsghdr *cmhp;

    /* Parse command-line arguments */
    use_datagram_socket = 0;
    while ((opt = getopt(argc, argv, "d")) != -1) {
        switch (opt) {
        case 'd':
            use_datagram_socket = 1;
            break;
        default:
            usage(stderr, argv[0]);
            exit(EXIT_FAILURE);
        }
    }

    /* Create socket bound to well-known address */

    if (remove(SOCK_PATH) == -1 && errno != ENOENT) {
        fprintf(stderr, "remove-%s\n", SOCK_PATH);
        exit(EXIT_FAILURE);
    }

    if (use_datagram_socket) {
        fprintf(stderr, "Receiving via datagram socket\n");
        sfd = unix_bind(SOCK_PATH, SOCK_DGRAM);
        if (sfd == -1) {
            perror("unix_bind");
            exit(EXIT_FAILURE);
        }
    } else {
        fprintf(stderr, "Receiving via stream socket\n");
        lfd = unix_listen(SOCK_PATH, 5);
        if (lfd == -1) {
            perror("unix_listen");
            exit(EXIT_FAILURE);
        }
        sfd = accept(lfd, NULL, 0);
        if (sfd == -1) {
            perror("accept");
            exit(EXIT_FAILURE);
        }
    }

    /* Set 'control_un' to describe ancillary data that we want to receive */

    control_un.cmh.cmsg_len = CMSG_LEN(sizeof(int));
    control_un.cmh.cmsg_level = SOL_SOCKET;
    control_un.cmh.cmsg_type = SCM_RIGHTS;

    /* Set 'msgh' fields to describe 'control_un' */

    msgh.msg_control = control_un.control;
    msgh.msg_controllen = sizeof(control_un.control);

    /* Set fields of 'msgh' to point to buffer used to receive (real)
       data read by recvmsg() */

    msgh.msg_iov = &iov;
    msgh.msg_iovlen = 1;
    iov.iov_base = &data;
    iov.iov_len = sizeof(int);

    msgh.msg_name = NULL;               /* We don't need address of peer */
    msgh.msg_namelen = 0;

    /* Receive real plus ancillary data */

    nr = recvmsg(sfd, &msgh, 0);
    if (nr == -1) {
        perror("recvmsg");
        exit(EXIT_FAILURE);
    }
    fprintf(stderr, "recvmsg() returned %ld\n", (long) nr);

    if (nr > 0) {
        fprintf(stderr, "Received data = %d\n", data);
    }

    /* Get the received file descriptor (which is typically a different
       file descriptor number than was used in the sending process) */

    cmhp = CMSG_FIRSTHDR(&msgh);
    if (cmhp == NULL || cmhp->cmsg_len != CMSG_LEN(sizeof(int))) {
        fprintf(stderr, "bad cmsg header / message length\n");
        exit(EXIT_FAILURE);
    }
    if (cmhp->cmsg_level != SOL_SOCKET) {
        fprintf(stderr, "cmsg_level != SOL_SOCKET\n");
        exit(EXIT_FAILURE);
    }
    if (cmhp->cmsg_type != SCM_RIGHTS) {
        fprintf(stderr, "cmsg_type != SCM_RIGHTS\n");
        exit(EXIT_FAILURE);
    }

    fd = *((int *) CMSG_DATA(cmhp));
    fprintf(stderr, "Received fd=%d\n", fd);

    /* Having obtained the file descriptor, read the file's contents and
       print them on standard output */

    while (1) {
        char buf[BUF_SIZE];
        ssize_t numRead;

        numRead = read(fd, buf, BUF_SIZE);
        if (numRead == -1) {
            perror("read");
            exit(EXIT_FAILURE);
        }
        if (numRead == 0) {
            break;
        }
        write(STDOUT_FILENO, buf, numRead);
    }

    exit(EXIT_SUCCESS);
}