static gboolean
check_connection(GIOChannel *chan) {
  gchar fake_buf[4096];
  gsize bytes_read;
  GError *tmp_error = NULL;
  GIOFlags flags;
  GIOStatus ret, iostat;

  flags = g_io_channel_get_flags(chan);
  
  /* set non-blocking */
  ret = g_io_channel_set_flags(chan, flags | G_IO_FLAG_NONBLOCK, NULL);
  if (ret == G_IO_STATUS_ERROR) {
    return FALSE;
  }
  
  iostat = g_io_channel_read_chars(chan, fake_buf,
				   sizeof(fake_buf), 
				   &bytes_read, &tmp_error);

  ret = g_io_channel_set_flags(chan, flags, NULL);
  if (ret == G_IO_STATUS_ERROR) {
    return FALSE;
  }

  /* this makes us disconnect from bad servers
     (those that send us information without us asking for it) */
  return iostat == G_IO_STATUS_AGAIN;
}
Пример #2
0
/**
 * Initializes the polling system.  This must be called before
 * any other functions in this module.
 *
 * @returns TRUE if initialization succeeded, FALSE otherwise
 */
gboolean
gam_dnotify_init(void)
{
    struct sigaction act;
    int fds[2];
    GSource *source;

    g_return_val_if_fail(gam_poll_dnotify_init (), FALSE);

    if (pipe(fds) < 0) {
        g_warning("Could not create pipe.\n");
        return FALSE;
    }

    pipe_read_ioc = g_io_channel_unix_new(fds[0]);
    pipe_write_ioc = g_io_channel_unix_new(fds[1]);

    g_io_channel_set_flags(pipe_read_ioc, G_IO_FLAG_NONBLOCK, NULL);
    g_io_channel_set_flags(pipe_write_ioc, G_IO_FLAG_NONBLOCK, NULL);


    source = g_io_create_watch(pipe_read_ioc,
                               G_IO_IN | G_IO_HUP | G_IO_ERR);
    g_source_set_callback(source, gam_dnotify_pipe_handler, NULL, NULL);

    g_source_attach(source, NULL);
    g_source_unref(source);

    /* setup some signal stuff */
    act.sa_sigaction = dnotify_signal_handler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = SA_SIGINFO;
    sigaction(SIGRTMIN, &act, NULL);

    /* catch SIGIO as well (happens when the realtime queue fills up) */
    act.sa_sigaction = overflow_signal_handler;
    sigemptyset(&act.sa_mask);
    sigaction(SIGIO, &act, NULL);

    changes = g_queue_new();

    path_hash = g_hash_table_new(g_str_hash, g_str_equal);
    fd_hash = g_hash_table_new(g_direct_hash, g_direct_equal);

    GAM_DEBUG(DEBUG_INFO, "dnotify initialized\n");

	gam_server_install_kernel_hooks (GAMIN_K_DNOTIFY, 
					 gam_dnotify_add_subscription,
					 gam_dnotify_remove_subscription,
					 gam_dnotify_remove_all_for,
					 gam_dnotify_directory_handler,
					 gam_dnotify_file_handler);

    return TRUE;
}
Пример #3
0
/*---------------------------------------------------------------------*/
static void
init_io_channels() {
   GError *err = NULL;
   
   hop_stdout = g_io_channel_unix_new( hop_pipe_stdout[ 0 ] );
   hop_stderr = g_io_channel_unix_new( hop_pipe_stderr[ 0 ] );

   g_io_add_watch( hop_stdout, G_IO_IN | G_IO_HUP, watch_output, 0 );
   g_io_add_watch( hop_stderr, G_IO_IN | G_IO_HUP, watch_output, 0 );
   
   g_io_channel_set_flags( hop_stdout, G_IO_FLAG_NONBLOCK, &err );
   g_io_channel_set_flags( hop_stderr, G_IO_FLAG_NONBLOCK, &err );
}
Пример #4
0
/**
 * Spawn a new process
 *
 * @param archive Should contain AM_Archive type of data
 * @param command Command, which we want to execute
 * @param input TRUE If we want to use our standard input, oterwise will be used /dev/null
 *
 */
void SpawnAsyncProcess(AM_Archive *archive, gchar *command, gboolean input, gboolean output_flag)
{
	GIOChannel *ioc , *err_ioc, *out_ioc;
	GError *error = NULL;
	gchar **argv;
	gint argcp, response;

	g_shell_parse_argv ( command , &argcp , &argv , NULL);
	if ( ! g_spawn_async_with_pipes (
		NULL,
		argv,
		NULL,
		G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
		NULL,
		NULL,
		&archive->child_pid,
		input ? &input_fd : NULL,
		&output_fd,
		&error_fd,
		&error) )
	{
		response = ShowGtkMessageDialog (NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK, "Can't run the archiver executable:",error->message);
		g_error_free (error);
		g_strfreev ( argv );
		archive->child_pid = 0;
		return;
	}
	g_strfreev ( argv );

	if ( archive->parse_output )
	{
		ioc = g_io_channel_unix_new ( output_fd );
		g_io_channel_set_encoding (ioc, locale , NULL);
		g_io_channel_set_flags ( ioc , G_IO_FLAG_NONBLOCK , NULL );
		g_io_add_watch (ioc, G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL, archive->parse_output, archive);
		g_child_watch_add ( archive->child_pid, (GChildWatchFunc)am_a_watch_child, archive);
	}
	if (output_flag)
	{
		out_ioc = g_io_channel_unix_new ( output_fd );
		g_io_channel_set_encoding (out_ioc, locale , NULL);
		g_io_channel_set_flags ( out_ioc , G_IO_FLAG_NONBLOCK , NULL );
		g_io_add_watch (out_ioc, G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL, am_a_report_child_stderr, NULL);
	}

	err_ioc = g_io_channel_unix_new ( error_fd );
	g_io_channel_set_encoding (err_ioc, locale , NULL);
	g_io_channel_set_flags ( err_ioc , G_IO_FLAG_NONBLOCK , NULL );
	g_io_add_watch (err_ioc, G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL, am_a_report_child_stderr, NULL);
}
Пример #5
0
static void add_abrt_server_proc(const pid_t pid, int fdout)
{
    struct abrt_server_proc *proc = xmalloc(sizeof(*proc));
    proc->pid = pid;
    proc->fdout = fdout;
    proc->dirname = NULL;
    proc->type = AS_UKNOWN;
    proc->channel = abrt_gio_channel_unix_new(proc->fdout);
    proc->watch_id = g_io_add_watch(proc->channel,
                                    G_IO_IN | G_IO_HUP,
                                    abrt_server_output_cb,
                                    proc);

    GError *error = NULL;
    g_io_channel_set_flags(proc->channel, G_IO_FLAG_NONBLOCK, &error);
    if (error != NULL)
        error_msg_and_die("g_io_channel_set_flags failed: '%s'", error->message);

    g_io_channel_set_buffered(proc->channel, TRUE);

    s_processes = g_list_append(s_processes, proc);
    if (g_list_length(s_processes) >= MAX_CLIENT_COUNT)
    {
        error_msg("Too many clients, refusing connections to '%s'", SOCKET_FILE);
        /* To avoid infinite loop caused by the descriptor in "ready" state,
         * the callback must be disabled.
         */
        g_source_remove(channel_id_socket);
        channel_id_socket = 0;
    }
}
Пример #6
0
static void mux_setup_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
	struct mux_setup_data *msd = user_data;
	GIOFlags flags;
	GIOChannel *channel;
	GAtMux *mux = NULL;

	if (!ok)
		goto error;

	channel = g_at_chat_get_channel(msd->chat);
	channel = g_io_channel_ref(channel);

	g_at_chat_unref(msd->chat);
	msd->chat = NULL;

	flags = g_io_channel_get_flags(channel) | G_IO_FLAG_NONBLOCK;
	g_io_channel_set_flags(channel, flags, NULL);

	g_io_channel_set_encoding(channel, NULL, NULL);
	g_io_channel_set_buffered(channel, FALSE);

	if (msd->mode == 0)
		mux = g_at_mux_new_gsm0710_basic(channel, msd->frame_size);
	else
		mux = g_at_mux_new_gsm0710_advanced(channel, msd->frame_size);

	g_io_channel_unref(channel);

error:
	msd->func(mux, msd->user);

	if (msd->destroy)
		msd->destroy(msd->user);
}
Пример #7
0
void HttpClient::on_connected(gpointer data, bool succeeded)
{
	HttpClient *oHttpClient = (HttpClient *)data;
	if (!succeeded) {
		gchar *mes = g_strdup_printf("Can not connect to %s: %s\n",
			oHttpClient->host_.c_str(), Socket::get_error_msg().c_str());
		on_error_.emit(oHttpClient, mes);
		g_free(mes);
		return;
	}
#ifdef _WIN32
	oHttpClient->channel_ = g_io_channel_win32_new_socket(oHttpClient->sd_);
#else
	oHttpClient->channel_ = g_io_channel_unix_new(oHttpClient->sd_);
#endif
	g_io_channel_set_encoding(oHttpClient->channel_, NULL, NULL);
	/* make sure that the channel is non-blocking */
	int flags = g_io_channel_get_flags(oHttpClient->channel_);
	flags |= G_IO_FLAG_NONBLOCK;
	GError *err = NULL;
	g_io_channel_set_flags(oHttpClient->channel_, GIOFlags(flags), &err);
	if (err) {
		gchar *str = g_strdup_printf("Unable to set the channel as non-blocking: %s", err->message);
		on_error_.emit(oHttpClient, str);
		g_free(str);
		g_error_free(err);
		return;
	}
	if (oHttpClient->SendGetRequest())
		return;
	oHttpClient->out_source_id_ = g_io_add_watch(oHttpClient->channel_, GIOCondition(G_IO_OUT), on_io_out_event, oHttpClient);
	oHttpClient->in_source_id_ = g_io_add_watch(oHttpClient->channel_, GIOCondition(G_IO_IN | G_IO_ERR), on_io_in_event, oHttpClient);
}
Пример #8
0
static GIOChannel *ipc_connect(const char *path, size_t size,
					GIOFunc connect_cb, void *user_data)
{
	struct sockaddr_un addr;
	GIOCondition cond;
	GIOChannel *io;
	int sk;

	sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
	if (sk < 0) {
		error("IPC: failed to create socket: %d (%s)", errno,
							strerror(errno));
		return NULL;
	}

	io = g_io_channel_unix_new(sk);

	g_io_channel_set_close_on_unref(io, TRUE);
	g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;

	memcpy(addr.sun_path, path, size);

	connect(sk, (struct sockaddr *) &addr, sizeof(addr));

	cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;

	g_io_add_watch(io, cond, connect_cb, user_data);

	return io;
}
static gboolean
accept_server (MilterTestClient *client)
{
    MilterTestClientPrivate *priv;
    GError *error = NULL;

    priv = MILTER_TEST_CLIENT_GET_PRIVATE(client);

    priv->server_fd = accept(priv->fd, NULL, NULL);

    priv->server_channel = g_io_channel_unix_new(priv->server_fd);
    g_io_channel_set_encoding(priv->server_channel, NULL, &error);
    gcut_assert_error(error);

    g_io_channel_set_flags(priv->server_channel,
                           G_IO_FLAG_NONBLOCK |
                           G_IO_FLAG_IS_READABLE |
                           G_IO_FLAG_IS_WRITEABLE,
                           &error);
    gcut_assert_error(error);

    g_io_channel_set_close_on_unref(priv->server_channel, TRUE);

    priv->server_watch_id = milter_test_io_add_decode_watch(priv->loop,
                                                            priv->server_channel,
                                                            priv->decoder);

    return TRUE;
}
Пример #10
0
static GIOChannel *
brasero_process_setup_channel (BraseroProcess *process,
			       int pipe,
			       gint *watch,
			       GIOFunc function)
{
	GIOChannel *channel;

	fcntl (pipe, F_SETFL, O_NONBLOCK);
	channel = g_io_channel_unix_new (pipe);

	/* It'd be good if we were allowed to add some to the default line
	 * separator */
//	g_io_channel_set_line_term (channel, "\b", -1);

	g_io_channel_set_flags (channel,
				g_io_channel_get_flags (channel) | G_IO_FLAG_NONBLOCK,
				NULL);
	g_io_channel_set_encoding (channel, NULL, NULL);
	*watch = g_io_add_watch (channel,
				(G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL),
				 function,
				 process);

	g_io_channel_set_close_on_unref (channel, TRUE);
	return channel;
}
Пример #11
0
static void
setup_io()
{
	int result;
	channel = g_io_channel_unix_new(STDIN_FILENO);
	g_io_channel_set_close_on_unref(channel, TRUE);

#if 0
	g_io_channel_set_encoding(channel, NULL, NULL);
	g_io_channel_set_buffered(channel, FALSE);
	g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL );
#endif

	channel_read_callback = result = g_io_add_watch_full(channel,  G_PRIORITY_HIGH,
					(G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI),
					io_invoke, NULL, NULL);

	channel_error_callback = g_io_add_watch_full(channel,  G_PRIORITY_HIGH,
					(G_IO_NVAL),
					io_invoke_error, GINT_TO_POINTER(result), NULL);

	g_io_channel_unref(channel);  /* Apparently this caused crashes for some people.
	                                 But irssi does this, so I am going to assume the
	                                 crashes were caused by some other stuff. */

	gnt_warning("setting up IO (%d)", channel_read_callback);
}
Пример #12
0
int
_gpgme_io_set_nonblocking (int fd)
{
  GIOChannel *chan;
  GIOStatus status;
 
  TRACE_BEG (DEBUG_SYSIO, "_gpgme_io_set_nonblocking", fd);

  chan = find_channel (fd);
  if (!chan)
    {
      errno = EIO;
      return TRACE_SYSRES (-1);
    }

  status = g_io_channel_set_flags (chan,
				   g_io_channel_get_flags (chan) |
				   G_IO_FLAG_NONBLOCK, NULL);

  if (status != G_IO_STATUS_NORMAL)
    {
#if 0
      /* glib 1.9.2 does not implement set_flags and returns an
	 error.  */
      errno = EIO;
      return TRACE_SYSRES (-1);
#else
      TRACE_LOG1 ("g_io_channel_set_flags failed: status=%d (ignored)",
		  status);
#endif
    }

  return TRACE_SYSRES (0);
}
Пример #13
0
void ForkExecParent::setupPipe(GIOChannel *&channel, guint &sourceID, int fd)
{
    if (fd == -1) {
        // nop
        return;
    }

    channel = g_io_channel_unix_new(fd);
    if (!channel) {
        // failure
        SE_LOG_DEBUG(NULL, NULL, "g_io_channel_unix_new() returned NULL");
        close(fd);
        return;
    }
    // Close fd when freeing the channel (done by caller).
    g_io_channel_set_close_on_unref(channel, true);
    // Don't block in outputReady().
    GErrorCXX error;
    g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, error);
    // We assume that the helper is writing data in the same encoding
    // and thus avoid any kind of conversion. Necessary to avoid
    // buffering.
    error.clear();
    g_io_channel_set_encoding(channel, NULL, error);
    g_io_channel_set_buffered(channel, true);
    sourceID = g_io_add_watch(channel, (GIOCondition)(G_IO_IN|G_IO_ERR|G_IO_HUP), outputReady, this);
}
Пример #14
0
static gboolean
init_inotify (NMInotifyHelper *self)
{
	NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self);
	GIOChannel *channel;
	guint source_id;

	priv->ifd = inotify_init ();
	if (priv->ifd == -1) {
		nm_log_warn (LOGD_SETTINGS, "couldn't initialize inotify");
		return FALSE;
	}

	/* Watch the inotify descriptor for file/directory change events */
	channel = g_io_channel_unix_new (priv->ifd);
	if (!channel) {
		nm_log_warn (LOGD_SETTINGS, "couldn't create new GIOChannel");
		close (priv->ifd);
		priv->ifd = -1;
		return FALSE;
	}

	g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
	g_io_channel_set_encoding (channel, NULL, NULL); 

	source_id = g_io_add_watch (channel,
	                            G_IO_IN | G_IO_ERR,
	                            (GIOFunc) inotify_event_handler,
	                            (gpointer) self);
	g_io_channel_unref (channel);
	return TRUE;
}
Пример #15
0
static guint bluetooth_listen(void)
{
	GIOChannel *io;
	guint id;
	GError *err = NULL;

	if (option_channel == -1) {
		g_printerr("Bluetooth channel not set\n");
		return 0;
	}

	if (option_packet || option_channel > 31)
		io = l2cap_listen(&err);
	else
		io = rfcomm_listen(&err);

	if (io == NULL) {
		g_printerr("%s\n", err->message);
		g_error_free(err);
		return 0;
	}

	g_print("Bluetooth socket created\n");

	id = g_io_add_watch(io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
							bluetooth_watch, NULL);

	g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
	g_io_channel_set_close_on_unref(io, TRUE);
	g_io_channel_unref(io);

	return id;
}
Пример #16
0
void 
seahorse_unix_signal_register (int sig, signal_handler handler)
{
    g_return_if_fail (sig < MAX_SIGNAL);
    g_return_if_fail (handler != NULL);

    /* Setup the signal channel */
    if (signal_channel == NULL) {

        memset (&signal_handlers, 0, sizeof (signal_handlers));
        
        if (pipe (signal_pipe)) {
            g_critical ("can't create signal pipe: %s", strerror (errno));
            return;
        }

        /* Non blocking to prevent deadlock */
        fcntl (signal_pipe[1], F_SETFL, fcntl (signal_pipe[1], F_GETFL) | O_NONBLOCK);

        /* convert the reading end of the pipe into a GIOChannel */
        signal_channel = g_io_channel_unix_new (signal_pipe[0]);
        g_io_channel_set_encoding (signal_channel, NULL, NULL);
        g_io_channel_set_flags (signal_channel, g_io_channel_get_flags (signal_channel) | G_IO_FLAG_NONBLOCK, NULL);

        /* register the reading end with the event loop */
        signal_watch_id = g_io_add_watch (signal_channel, G_IO_IN | G_IO_PRI | G_IO_HUP, deliver_signal, NULL);

        g_atexit (cleanup_signals);
    }

    /* Handle some signals */
    signal (sig, pipe_signals);

    signal_handlers[sig] = handler;
}
Пример #17
0
int __connman_rfkill_init(void)
{
	GIOFlags flags;
	int fd;

	DBG("");

	fd = open("/dev/rfkill", O_RDWR | O_CLOEXEC);
	if (fd < 0) {
		connman_error("Failed to open RFKILL control device");
		return -EIO;
	}

	channel = g_io_channel_unix_new(fd);
	g_io_channel_set_close_on_unref(channel, TRUE);

	g_io_channel_set_encoding(channel, NULL, NULL);
	g_io_channel_set_buffered(channel, FALSE);

	flags = g_io_channel_get_flags(channel);
	flags |= G_IO_FLAG_NONBLOCK;
	g_io_channel_set_flags(channel, flags, NULL);

	/* Process current RFKILL events sent on device open */
	while (rfkill_process(channel) == G_IO_STATUS_NORMAL);

	g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
							rfkill_event, NULL);

	return 0;
}
Пример #18
0
static gboolean obtain_send_channel_cb(GIOChannel *channel, GIOCondition condition,
	G_GNUC_UNUSED gpointer gdata)
{
#if HAS_SPAWN_LEAVE_STDIN_OPEN
	if (condition & G_IO_FAILURE)
		g_io_channel_shutdown(channel, FALSE, NULL);
	else
	{
		g_io_channel_ref(channel);
		send_channel = channel;
		create_send_source();  /* for the initialization commands */
	}
#else
	if (!(condition & G_IO_FAILURE))
	{
		gint stdin_fd = dup(g_io_channel_unix_get_fd(channel));

	#ifdef G_OS_UNIX
		send_channel = g_io_channel_unix_new(stdin_fd);
		g_io_channel_set_flags(send_channel, G_IO_FLAG_NONBLOCK, NULL);
	#else
		send_channel = g_io_channel_win32_new_fd(stdin_fd);
	#endif
		g_io_channel_set_encoding(send_channel, NULL, NULL);
		g_io_channel_set_buffered(send_channel, FALSE);
		create_send_source();  /* for the initialization commands */
	}
#endif

	return FALSE;
}
Пример #19
0
/**
 * msgpack_conn_new - Set up read/write buffering.
 */
struct msgpack_conn *
msgpack_conn_new(GIOChannel *channel, msgpack_conn_recv_t recv,
    msgpack_conn_drop_t drop)
{
  struct msgpack_conn *conn;

  if (channel == NULL) {
    return NULL;
  }

  conn = g_malloc0(sizeof(*conn));
  conn->mc_channel = channel;
  conn->mc_recv = recv;
  conn->mc_drop = drop;

  // TODO: This needs to be done when the socket is first opened.
  // Put the conn's channel into nonblocking mode.
  g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);

  // Set the channel encoding to binary.
  g_io_channel_set_encoding(channel, NULL, NULL);

  // Set up the read listener.
  msgpack_unpacker_init(&conn->mc_unpacker, BUFSIZE);
  g_io_add_watch(channel, G_IO_IN, msgpack_conn_read, conn);

  // Set up the write buffer.
  conn->mc_write_buffer = g_string_sized_new(BUFSIZE);

  return conn;
}
Пример #20
0
/*
 * Function: add_textview_refresher
 *
 * Adds a watch in order to display (when available) data from the
 * given file descriptor in the given GTK textview.
 */
void add_textview_refresher(GtkTextView * text_view, int fd)
{
    GIOChannel *channel = g_io_channel_unix_new(fd);
    g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
    g_io_add_watch(channel, G_IO_IN | G_IO_HUP, refresh_textview_from_fd,
		   text_view);
}
Пример #21
0
static gboolean attach_fifo(const char *name)
{
  GSource *source;
  int fd = open (name, O_RDONLY|O_NONBLOCK);
  if (fd == -1)
    return FALSE;

  if (fifo_channel)
    g_io_channel_unref(fifo_channel);

  fifo_channel = g_io_channel_unix_new(fd);

  g_io_channel_set_flags(fifo_channel, G_IO_FLAG_NONBLOCK, NULL);
  g_io_channel_set_encoding(fifo_channel, NULL, NULL);
  g_io_channel_set_close_on_unref(fifo_channel, TRUE);

  source = g_io_create_watch(fifo_channel,
                             G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL);
  g_source_set_callback(source, (GSourceFunc)fifo_callback,
                        (gpointer)fifo_channel,
                        (GDestroyNotify)fifo_destroy_callback);
  g_source_attach(source, main_context);

  return TRUE;
}
Пример #22
0
/**
 * paxos_peer_init - Set up peer read/write buffering.
 */
struct paxos_peer *
paxos_peer_init(GIOChannel *channel)
{
  struct paxos_peer *peer;

  if (channel == NULL) {
    return NULL;
  }

  peer = g_malloc0(sizeof(*peer));
  peer->pp_channel = channel;

  // TODO: this needs to be done when the socket is first opened
  // Put the peer's channel into nonblocking mode.
  g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);

  // Set the channel encoding to binary.
  g_io_channel_set_encoding(channel, NULL, NULL);

  // Set up the read listener.
  msgpack_unpacker_init(&peer->pp_unpacker, PIO_BUFSIZE);
  g_io_add_watch(channel, G_IO_IN, paxos_peer_read, peer);

  // Set up the write buffer.
  peer->pp_write_buffer = g_string_sized_new(PIO_BUFSIZE);

  return peer;
}
static gboolean
event_connection_setup (NMNetlinkMonitor *self, GError **error)
{
	NMNetlinkMonitorPrivate *priv = NM_NETLINK_MONITOR_GET_PRIVATE (self);
	GError *channel_error = NULL;
	GIOFlags channel_flags;
	int fd;

	g_return_val_if_fail (priv->io_channel == NULL, FALSE);

	/* Set up the event listener connection */
	priv->nlh_event = nl_socket_alloc ();
	if (!priv->nlh_event) {
		g_set_error (error, NM_NETLINK_MONITOR_ERROR,
		             NM_NETLINK_MONITOR_ERROR_NETLINK_ALLOC_HANDLE,
		             _("unable to allocate netlink handle for monitoring link status: %s"),
		             nl_geterror (ENOMEM));
		goto error;
	}

	if (!nlh_setup (priv->nlh_event, event_msg_ready, self, error))
		goto error;

	nl_socket_disable_seq_check (priv->nlh_event);

	/* Subscribe to the LINK group for internal carrier signals */
	if (!nm_netlink_monitor_subscribe (self, RTNLGRP_LINK, error))
		goto error;

	fd = nl_socket_get_fd (priv->nlh_event);
	priv->io_channel = g_io_channel_unix_new (fd);

	g_io_channel_set_encoding (priv->io_channel, NULL, &channel_error);
	/* Encoding is NULL, so no conversion error can possibly occur */
	g_assert (channel_error == NULL);

	g_io_channel_set_close_on_unref (priv->io_channel, TRUE);
	channel_flags = g_io_channel_get_flags (priv->io_channel);
	channel_error = NULL;
	g_io_channel_set_flags (priv->io_channel,
	                        channel_flags | G_IO_FLAG_NONBLOCK,
	                        &channel_error);
	if (channel_error != NULL) {
		g_propagate_error (error, channel_error);
		goto error;
	}

	return TRUE;

error:
	if (priv->io_channel)
		nm_netlink_monitor_close_connection (self);

	if (priv->nlh_event) {
		nl_socket_free (priv->nlh_event);
		priv->nlh_event = NULL;
	}

	return FALSE;
}
static void
watch_for_smartcards (GdmSmartcardExtension *extension)
{
        GError *error;
        GIOChannel *io_channel;
        char *args[] = { GDM_SMARTCARD_WORKER_COMMAND, NULL };
        GPid pid;
        int stdout_fd;

        error = NULL;

        if (!g_spawn_async_with_pipes (NULL, args, NULL, 0,
                                       NULL, NULL, &pid, NULL,
                                       &stdout_fd, NULL, &error)) {
                g_debug ("could not start smart card manager: %s", error->message);
                g_error_free (error);
                return;
        }
        fcntl (stdout_fd, F_SETFD, FD_CLOEXEC);

        io_channel = g_io_channel_unix_new (stdout_fd);
        g_io_channel_set_flags (io_channel, G_IO_FLAG_NONBLOCK, NULL);
        g_io_channel_set_encoding (io_channel, NULL, NULL);
        g_io_channel_set_buffered (io_channel, FALSE);
        g_io_add_watch (io_channel, G_IO_IN, on_smartcard_event, extension);
        g_io_channel_set_close_on_unref (io_channel, TRUE);
        g_io_channel_unref (io_channel);

        extension->priv->worker_pid = pid;
}
Пример #25
0
static VALUE
rg_set_flags(VALUE self, VALUE flags)
{
    GError* err = NULL;
    GIOStatus status = g_io_channel_set_flags(_SELF(self), 
                                              NUM2INT(flags), &err);
    ioc_error(status, err);
    return self;
}
Пример #26
0
gint
yad_notification_run ()
{
  GIOChannel *channel = NULL;

  status_icon = gtk_status_icon_new ();
  g_signal_connect (status_icon, "size-changed", G_CALLBACK (icon_size_changed_cb), NULL);

  if (options.data.dialog_text)
    {
      if (!options.data.no_markup)
        gtk_status_icon_set_tooltip_markup (status_icon, options.data.dialog_text);
      else
        gtk_status_icon_set_tooltip_text (status_icon, options.data.dialog_text);
    }
  else
    gtk_status_icon_set_tooltip_text (status_icon, _("Yad notification"));

  if (options.data.dialog_image)
    icon = g_strdup (options.data.dialog_image);
  if (options.common_data.command)
    action = g_strdup (options.common_data.command);

  set_icon ();

  g_signal_connect (status_icon, "activate", G_CALLBACK (activate_cb), NULL);
  g_signal_connect (status_icon, "popup_menu", G_CALLBACK (popup_menu_cb), NULL);

  if (options.notification_data.menu)
    parse_menu_str (options.notification_data.menu);

  /* quit on middle click (like press Esc) */
  if (options.notification_data.middle)
    g_signal_connect (status_icon, "button-press-event", G_CALLBACK (middle_quit_cb), NULL);

  if (options.common_data.listen)
    {
      channel = g_io_channel_unix_new (0);
      if (channel)
        {
          g_io_channel_set_encoding (channel, NULL, NULL);
          g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
          g_io_add_watch (channel, G_IO_IN | G_IO_HUP, handle_stdin, NULL);
        }
    }

  /* Show icon and wait */
  gtk_status_icon_set_visible (status_icon, !options.notification_data.hidden);

  if (options.data.timeout > 0)
    g_timeout_add_seconds (options.data.timeout, (GSourceFunc) timeout_cb, NULL);

  gtk_main ();

  return exit_code;
}
Пример #27
0
static void watchInput(GMainLoop* loop) {
  GIOChannel* in = g_io_channel_unix_new(STDIN_FILENO);
  GError* error = NULL;
  g_io_channel_set_flags(in,G_IO_FLAG_NONBLOCK,&error);
  if(error) {
    g_error("Can't nonblock %s",error->message);
    exit(23);
  }
  g_io_add_watch(in,G_IO_IN,(void*)on_input,loop);
}
Пример #28
0
static void
fill_buffer_from_stdin ()
{
  GIOChannel *channel;

  channel = g_io_channel_unix_new (0);
  g_io_channel_set_encoding (channel, NULL, NULL);
  g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
  g_io_add_watch (channel, G_IO_IN | G_IO_HUP, handle_stdin, NULL);
}
Пример #29
0
static void
matedialog_text_fill_entries_from_stdin (GtkTextBuffer *text_buffer)
{
  GIOChannel *channel; 

  channel = g_io_channel_unix_new (0);
  g_io_channel_set_encoding (channel, NULL, NULL);
  g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
  g_io_add_watch (channel, G_IO_IN | G_IO_HUP, matedialog_text_handle_stdin, text_buffer);
}
Пример #30
0
GIOChannel *serial_connect(Configuration *cfg, int *serial_fd)
{
    GIOChannel *io;
    int fd;
    struct termios config;

    /* O_RDWR - Opens the port for reading and writing
     * O_NOCTTY - The port never becomes the controlling terminal of the process.
     * O_NDELAY - Use non-blocking I/O. On some systems this also means the RS232 DCD signal line is ignored.
     */
    fd = open(cfg->port, O_RDWR | O_NOCTTY);// | O_NDELAY);


    tcgetattr(fd, &config);

    if (fd < -1)
    {
        g_message("Unable to connect to %s: %s(%d)!", cfg->port, strerror(errno), errno);
        return NULL;
    }

    config.c_cflag = get_cflag(cfg);
    config.c_iflag = IGNPAR | IGNBRK;
    if (cfg->flow == GUART_FLOW_XONXOFF)
    {
        config.c_iflag |= IXON | IXOFF;
    }

    config.c_oflag = 0;
    config.c_lflag = 0;

    config.c_cc[VTIME] = 0;
    config.c_cc[VMIN] = 1;


    if (tcsetattr(fd, TCSANOW, &config) < 0) {
        g_message("Can't change serial settings: %s(%d)", strerror(errno), errno);
        close(fd);
        return NULL;
    }

    tcflush(fd, TCOFLUSH);
    tcflush(fd, TCIFLUSH);

    io = g_io_channel_unix_new(fd);
    g_io_channel_set_close_on_unref(io, TRUE);

    if (g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL) != G_IO_STATUS_NORMAL) {
        g_io_channel_unref(io);
        return NULL;
    }

    *serial_fd = fd;
    return io;
}