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;
}
static gboolean
xfce_posix_signal_handler_pipe_io(GIOChannel *source,
                                  GIOCondition condition,
                                  gpointer data)
{
    gint signal_id = 0;
    GError *error = NULL;
    gsize bin = 0;
    XfcePosixSignalHandlerData *hdata;

    if(G_IO_STATUS_NORMAL == g_io_channel_read_chars(source, (gchar *)&signal_id,
                                                     sizeof(signal_id), &bin,
                                                     &error)
       && bin == sizeof(signal_id))
    {
        hdata = g_hash_table_lookup(__handlers, GINT_TO_POINTER(signal_id));
        if(hdata)
            hdata->handler(signal_id, hdata->user_data);
    } else {
        if(error) {
            g_critical("Signal pipe read failed: %s\n", error->message);
            g_error_free(error);
        } else {
            g_critical("Short read from signal pipe (expected %d, got %d)\n",
                       (int)sizeof(signal_id), (int)bin);
        }
    }

    return TRUE;
}
Esempio n. 3
0
/* The event loop callback that handles the unix signals. Must be a GIOFunc. */
static gboolean 
deliver_signal (GIOChannel *source, GIOCondition cond, gpointer d)
{
    GError *err = NULL;
    GIOStatus st;
    gsize read;
    gchar sig;

    while ((st = g_io_channel_read_chars (source, &sig, sizeof (gchar), 
                                          &read, &err)) == G_IO_STATUS_NORMAL) {

        g_assert (err == NULL);
        if (read != 1)
            break;
        if (sig < MAX_SIGNAL) {
            if (signal_handlers[(int)sig])
                (signal_handlers[(int)sig]) (sig);
        }
    }
  
    if (err != NULL)
        g_warning ("reading signal pipe failed: %s", err->message);
    if (st == G_IO_STATUS_EOF) 
        g_critical ("signal pipe has been closed");

    return TRUE;
}
Esempio n. 4
0
static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
							gpointer user_data)
{
	struct connman_inotify *inotify = user_data;
	char buffer[256];
	char *next_event;
	gsize bytes_read;
	GIOStatus status;
	GSList *list;

	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
		inotify->watch = 0;
		return FALSE;
	}

	status = g_io_channel_read_chars(channel, buffer,
					sizeof(buffer) -1, &bytes_read, NULL);

	switch (status) {
	case G_IO_STATUS_NORMAL:
		break;
	case G_IO_STATUS_AGAIN:
		return TRUE;
	default:
		connman_error("Reading from inotify channel failed");
		inotify->watch = 0;
		return FALSE;
	}

	next_event = buffer;

	while (bytes_read > 0) {
		struct inotify_event *event;
		gchar *ident;
		gsize len;

		event = (struct inotify_event *) next_event;
		if (event->len)
			ident = next_event + sizeof(struct inotify_event);
		else
			ident = NULL;

		len = sizeof(struct inotify_event) + event->len;

		/* check if inotify_event block fit */
		if (len > bytes_read)
			break;

		next_event += len;
		bytes_read -= len;

		for (list = inotify->list; list != NULL; list = list->next) {
			inotify_event_cb callback = list->data;

			(*callback)(event, ident);
		}
	}

	return TRUE;
}
Esempio n. 5
0
static void
ik_read_events (gsize  *buffer_size_out, 
                gchar **buffer_out)
{
  static gchar *buffer = NULL;
  static gsize buffer_size;
  
  /* Initialize the buffer on our first call */
  if (buffer == NULL)
    {
      buffer_size = AVERAGE_EVENT_SIZE;
      buffer_size *= MAX_QUEUED_EVENTS;
      buffer = g_malloc (buffer_size);
    }

  *buffer_size_out = 0;
  *buffer_out = NULL;
  
  memset (buffer, 0, buffer_size);

  if (g_io_channel_read_chars (inotify_read_ioc, (char *)buffer, buffer_size, buffer_size_out, NULL) != G_IO_STATUS_NORMAL) {
    /* error reading */
  }
  *buffer_out = buffer;
}
Esempio n. 6
0
/* Signal pipe handler */
static gboolean handle_signal_cb(GIOChannel *gio, GIOCondition condition, gpointer ptr_unused)
{
    uint8_t signo;
    gsize len = 0;
    g_io_channel_read_chars(gio, (void*) &signo, 1, &len, NULL);
    if (len == 1)
    {
        /* we did receive a signal */
        log_debug("Got signal %d through signal pipe", signo);
        if (signo != SIGCHLD)
            g_main_loop_quit(s_main_loop);
        else
        {
            pid_t cpid;
            int status;
            while ((cpid = safe_waitpid(-1, &status, WNOHANG)) > 0)
            {
                if (WIFSIGNALED(status))
                    log_debug("abrt-server(%d) signaled with %d", cpid, WTERMSIG(status));
                else if (WIFEXITED(status))
                    log_debug("abrt-server(%d) exited with %d", cpid, WEXITSTATUS(status));
                else
                {
                    log_debug("abrt-server(%d) is being debugged", cpid);
                    continue;
                }

                remove_abrt_server_proc(cpid, status);
            }
        }
    }
    start_idle_timeout();
    return TRUE; /* "please don't remove this event" */
}
Esempio n. 7
0
static gboolean
gam_dnotify_pipe_handler(gpointer user_data)
{
    char buf[5000];
    DNotifyData *data;
    gpointer fd;
    int i;

    GAM_DEBUG(DEBUG_INFO, "gam_dnotify_pipe_handler()\n");
    g_io_channel_read_chars(pipe_read_ioc, buf, sizeof(buf), NULL, NULL);

    i = 0;
    while ((fd = g_queue_pop_tail(changes)) != NULL) {

        G_LOCK(dnotify);
        data = g_hash_table_lookup(fd_hash, fd);
        G_UNLOCK(dnotify);

        if (data == NULL)
            continue;

        GAM_DEBUG(DEBUG_INFO, "handling signal\n");

        gam_poll_generic_scan_directory(data->path);
        i++;
    }

    GAM_DEBUG(DEBUG_INFO, "gam_dnotify_pipe_handler() done\n");
    return TRUE;
}
Esempio n. 8
0
/* Read count_bytes from the channel and write them to the loader.
 * Returns number of bytes written.
 * count_bytes = G_MAXSIZE means read as many bytes as possible. */
static gsize
loader_write_from_channel (GdkPixbufLoader *loader,
                           GIOChannel      *channel,
                           gsize            count_bytes)
{
    guchar* buffer;
    gsize bytes_read;
    GIOStatus read_status;

    if(count_bytes < G_MAXSIZE) {
        /* read no more than 'count_bytes' bytes */
        buffer = g_malloc(count_bytes);
        read_status = g_io_channel_read_chars (channel, (gchar*) buffer, count_bytes, &bytes_read, NULL);
    } else {
        /*read up to end */
        read_status = g_io_channel_read_to_end (channel, (gchar**) &buffer, &bytes_read, NULL);
    }

    if ((read_status != G_IO_STATUS_NORMAL) && (read_status != G_IO_STATUS_EOF))
        g_assert_not_reached ();

    if (!gdk_pixbuf_loader_write(loader, buffer, bytes_read, NULL))
        g_assert_not_reached ();

    g_free (buffer);
    return bytes_read;
}
Esempio n. 9
0
type8 ms_load_file (type8s *name, type8 *ptr, type16 size)
{
    gchar *filename;
    GIOChannel *file = NULL;
    gsize bytes_read;
    GError *error = NULL;

    const gchar *filters[] =
	{
	    "Saved game files (*.sav)", "*.sav",
	    NULL
	};

    if (!name)
    {
	filename = file_selector (FALSE, NULL, filters, "Restore game");
	if (!filename)
	    return -1;
    } else
	filename = g_strdup ((gchar *) name);

    if (g_file_test (filename, G_FILE_TEST_EXISTS))
    {
	file = g_io_channel_new_file (filename, "r", &error);
	g_io_channel_set_encoding (file, NULL, &error);
	g_io_channel_read_chars (file, (gchar *) ptr, size, &bytes_read, &error);
	g_io_channel_unref (file);
    }
    g_free (filename);
    return file ? 0 : -1;
}
Esempio n. 10
0
static gboolean _channel_in(Progress * p, GIOChannel * source)
{
	GIOStatus status;
	gsize read;
	GError * error = NULL;

#ifdef DEBUG
	fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
	p->in_id = 0;
	status = g_io_channel_read_chars(source, &p->buf[p->buf_cnt],
			p->bufsiz - p->buf_cnt, &read, &error);
	if(status == G_IO_STATUS_ERROR)
	{
		_progress_gerror(p, p->prefs->filename, error, 1);
		g_io_channel_shutdown(source, TRUE, NULL);
		gtk_main_quit();
		return FALSE;
	}
	else if(status == G_IO_STATUS_EOF)
	{
		p->eof = 1; /* reached end of input file */
		g_io_channel_shutdown(source, TRUE, NULL);
	}
	else if(p->buf_cnt + read != p->bufsiz)
		g_idle_add(_progress_idle_in, p); /* continue to read */
	if(p->buf_cnt == 0)
		g_idle_add(_progress_idle_out, p); /* begin to write */
	p->buf_cnt += read;
	return FALSE;
}
gboolean On_VolDown_ButtonPress(GIOChannel *source, GIOCondition condition, gpointer data)
{
	IoT_Error_t rc = NONE_ERROR;
	GError *error=0;
	char buf[10];
	char payload[MAX_PAYLOAD];
	struct input_event event;
	gsize bytes_read;

	//read and clear the event
	g_io_channel_seek_position(source, 0, G_SEEK_SET, 0);
	g_io_channel_read_chars(source, (gchar*) &event, sizeof(event), &bytes_read, NULL);

//	if(bytes_read >0)
//	    printf("Event0: keypress value=%x, type=%x, code=%x\n", event.value, event.type, event.code);

	if(event.code == 0x72 && event.value == 0x1)
	{
		INFO("Vol_Down Button pressed!");

		char* thingID = (char*) data;
		sprintf(payload, "{\n\"timestamp\": %lu, \"volume\": \"%s\" \n}\n", GetTimeSinceEpoch(), "decrease");
		printf("%s", payload);

		/**/
		char topic[512];
        	sprintf(topic, vol_button_topic, thingID);        
		rc = MQTT_Send_Message(topic, payload, strlen(payload));
		if (NONE_ERROR != rc)
			ERROR("Could not publish event: ");
	}
	return 1;	//indicate event handled
}
Esempio n. 12
0
static gboolean gio_in (GIOChannel *gio, GIOCondition condition, gpointer data) {
	GIOStatus ret;
	GError *err = NULL;
	gchar *msg;
	gsize len=0;
	char buf[32];

	
//	printf("Cond: %x\n",condition);
	if (condition & G_IO_IN) {
		ret = g_io_channel_read_chars (gio, buf, 4, &len, &err);

		if (ret == G_IO_STATUS_ERROR)
			g_error ("Error reading: %s\n", err->message);
		printf ("Read %u bytes: %s\n", len, msg);
	}

	if ((condition & G_IO_HUP) && (len==0)) {
		DEBUGPRINT("Ending pipe test\n");
		g_main_loop_quit(mLoop1);
	}
	
	g_free (msg);
	return TRUE;
}
Esempio n. 13
0
static gboolean bt_event_cb(GIOChannel *bt_io, GIOCondition condition,
							gpointer userdata)
{
	struct ofono_modem *modem = userdata;
	struct telit_data *data = ofono_modem_get_data(modem);

	if (condition & G_IO_IN) {
		GIOStatus status;
		gsize bytes_read, bytes_written;
		gchar buf[300];

		status = g_io_channel_read_chars(bt_io, buf, 300,
							&bytes_read, NULL);

		if (bytes_read > 0)
			g_io_channel_write_chars(data->hw_io, buf,
					bytes_read, &bytes_written, NULL);

		if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
			return FALSE;

		return TRUE;
	}

	return FALSE;
}
Esempio n. 14
0
GList *
_ige_conf_defaults_read_file (const gchar  *path,
                              GError      **error)
{
        DefaultData          data;
	GMarkupParser       *parser;
	GMarkupParseContext *context;
	GIOChannel          *io = NULL;
	gchar                buf[BYTES_PER_READ];

        io = g_io_channel_new_file (path, "r", error);
        if (!io) {
                return NULL;
        }

	parser = g_new0 (GMarkupParser, 1);

	parser->start_element = parser_start_cb;
	parser->end_element = parser_end_cb;
	parser->text = parser_text_cb;
	parser->error = parser_error_cb;

        memset (&data, 0, sizeof (DefaultData));

	context = g_markup_parse_context_new (parser,
                                              0,
                                              &data,
                                              NULL);

        while (TRUE) {
                GIOStatus io_status;
                gsize     bytes_read;

                io_status = g_io_channel_read_chars (io, buf, BYTES_PER_READ,
                                                     &bytes_read, error);
                if (io_status == G_IO_STATUS_ERROR) {
                        goto exit;
                }
                if (io_status != G_IO_STATUS_NORMAL) {
                        break;
                }

                g_markup_parse_context_parse (context, buf, bytes_read, error);
                if (error != NULL && *error != NULL) {
                        goto exit;
                }

                if (bytes_read < BYTES_PER_READ) {
                        break;
                }
        }

 exit:
        g_io_channel_unref (io);
	g_markup_parse_context_free (context);
	g_free (parser);

	return data.defaults;
}
Esempio n. 15
0
static gboolean
udev_read_cb (GIOChannel *source,
              GIOCondition condition,
              gpointer user_data)
{
  OdccmDeviceManagerPrivate *priv = ODCCM_DEVICE_MANAGER_GET_PRIVATE (user_data);
#define UEVENT_BUFFER_SIZE 2048
  gchar buf[UEVENT_BUFFER_SIZE*2];
  GIOStatus status;
  GError *error = NULL;
  guint buflen = 0;
  size_t bufpos;
  gboolean is_net = FALSE, is_ppp = FALSE, added = FALSE;
  gchar iface[10];
  gchar udi[UEVENT_BUFFER_SIZE];
  
  status = g_io_channel_read_chars(priv->udev_chann, &buf, sizeof(buf), &buflen, &error);

  if (status != G_IO_STATUS_NORMAL) {
    g_warning("%s: error reading event: %s",G_STRFUNC,error->message);
    g_error_free(error);
    error = NULL;
  }  

  bufpos = strlen(buf) + 1;
  while (bufpos < (size_t)buflen) {
    int keylen;
    char *key;
    char *tmpptr;
    
    key = &buf[bufpos];
    keylen = strlen(key);
    if (keylen == 0) break;
    
    if (strncmp("SUBSYSTEM=net",key,13)==0) is_net = TRUE;
    if (strncmp("INTERFACE=ppp",key,13)==0){
      is_ppp = TRUE;
      tmpptr = strchr(key,'=');
      strncpy(iface,tmpptr+1,10);
    }
    if (strncmp("ACTION=remove",key,13)==0) added = FALSE;
    if (strncmp("ACTION=add",key,13)==0) added = TRUE;
    if (strncmp("DEVPATH",key,7)==0) {
      tmpptr = strchr(key,'=');
      strncpy(udi,tmpptr+1,UEVENT_BUFFER_SIZE);
    }
    bufpos += keylen + 1;
  }

  if (is_net == TRUE && is_ppp == TRUE) {
    if (added == TRUE) {
      udev_device_added(user_data,iface,udi);
    } else {
      udev_device_removed(user_data,iface,udi);
    }
  }

  return TRUE;
}
Esempio n. 16
0
static gboolean
io_callback (GIOChannel * io, GIOCondition condition, gpointer data)
{
    gchar in;
    g_io_channel_read_chars (io, &in, 1, NULL, NULL);
    printf("we got: %c\n", in);
    return TRUE;
}
Esempio n. 17
0
static gboolean
snra_server_client_io_cb (G_GNUC_UNUSED GIOChannel * source,
                          GIOCondition condition, SnraServerClient * client)
{
    GIOStatus status = G_IO_STATUS_NORMAL;
#if 0
    g_print ("Got IO callback for client %p w/ condition %u\n", client,
             (guint) (condition));
#endif

    if (condition & (G_IO_HUP | G_IO_ERR)) {
        snra_server_connection_lost (client);
        return FALSE;
    }

    if (condition & G_IO_IN) {
        gsize bread = 0;

        if (client->in_bufsize <= client->in_bufavail) {
            gsize cur_offs = client->in_bufptr - client->in_buf;

            client->in_bufsize *= 2;
            g_print ("Growing io_buf to %" G_GSIZE_FORMAT " bytes\n",
                     client->in_bufsize);

            client->in_buf = g_renew (gchar, client->in_buf, client->in_bufsize);
            client->in_bufptr = client->in_buf + cur_offs;
        }

        status =
            g_io_channel_read_chars (client->io,
                                     client->in_buf + client->in_bufavail,
                                     client->in_bufsize - client->in_bufavail, &bread, NULL);

        if (status == G_IO_STATUS_ERROR) {
            snra_server_connection_lost (client);
        } else {
            g_print ("Collected %" G_GSIZE_FORMAT " bytes to io buf\n", bread);
            client->in_bufavail += bread;
        }

        while (client->in_bufavail > 0 && try_parse_websocket_fragment (client)) {
        };

        if (client->in_buf != client->in_bufptr) {
            memmove (client->in_buf, client->in_bufptr, client->in_bufavail);
            client->in_bufptr = client->in_buf;
        }
    }

    if (status == G_IO_STATUS_EOF) {
        // no more data
        snra_server_connection_lost (client);
        return FALSE;
    }

    return TRUE;
}
Esempio n. 18
0
/*
 * Function: refresh_textview_from_fd
 *
 * This function is called when data are readable from our pipe.
 * It reads the data and adds it to the GTK textview.
 */
gboolean refresh_textview_from_fd(GIOChannel * channel,
				  GIOCondition condition, gpointer data)
{
    GtkTextView *view = GTK_TEXT_VIEW(data);
    GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));

    gchar buf[1024];
    gsize len;
    GError *error = NULL;
    gint status;


    while ((status =
	    g_io_channel_read_chars(channel, buf, 1024, &len, &error))
	   == G_IO_STATUS_AGAIN) {

	while (gtk_events_pending())
	    gtk_main_iteration();
    }

    if (status != G_IO_STATUS_NORMAL) {
	/* status = G_IO_STATUS_ERROR or G_IO_STATUS_EOF */
	if (error) {
	    fprintf(stderr, _("Error while reading sub-child output : %s"),
		    error->message);
	    g_error_free(error);
	    error = NULL;
	}
	return FALSE;
    }

    if (len > 0) {
	GtkTextIter end;
	GtkTextMark *mark;
	gchar *utftext;
	gsize localelen;
	gsize utflen;

	gtk_text_buffer_get_end_iter(buffer, &end);

	if (!g_utf8_validate(buf, len, NULL)) {
	    utftext =
		g_convert_with_fallback(buf, len, "UTF-8",
					"ISO-8859-1", NULL, &localelen,
					&utflen, NULL);
	    gtk_text_buffer_insert(buffer, &end, utftext, utflen);
	    g_free(utftext);
	} else {
	    gtk_text_buffer_insert(buffer, &end, buf, len);
	}

	/* Scroll down TextView */
	mark = gtk_text_buffer_get_insert(buffer);
	gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(view), mark, 0.0, FALSE,
				     0.0, 1.0);
    }
    return TRUE;
}
Esempio n. 19
0
static gboolean
theora_enc_read_multipass_cache (GstTheoraEnc * enc)
{
    GstBuffer *cache_buf;
    const guint8 *cache_data;
    gsize bytes_read = 0;
    gssize bytes_consumed = 0;
    GIOStatus stat = G_IO_STATUS_NORMAL;
    gboolean done = FALSE;

    while (!done) {
        if (gst_adapter_available (enc->multipass_cache_adapter) == 0) {
            GstMapInfo minfo;

            cache_buf = gst_buffer_new_allocate (NULL, 512, NULL);

            gst_buffer_map (cache_buf, &minfo, GST_MAP_WRITE);

            stat = g_io_channel_read_chars (enc->multipass_cache_fd,
                                            (gchar *) minfo.data, minfo.size, &bytes_read, NULL);

            if (bytes_read <= 0) {
                gst_buffer_unmap (cache_buf, &minfo);
                gst_buffer_unref (cache_buf);
                break;
            } else {
                gst_buffer_unmap (cache_buf, &minfo);
                gst_buffer_resize (cache_buf, 0, bytes_read);

                gst_adapter_push (enc->multipass_cache_adapter, cache_buf);
            }
        }
        if (gst_adapter_available (enc->multipass_cache_adapter) == 0)
            break;

        bytes_read =
            MIN (gst_adapter_available (enc->multipass_cache_adapter), 512);

        cache_data = gst_adapter_map (enc->multipass_cache_adapter, bytes_read);

        bytes_consumed =
            th_encode_ctl (enc->encoder, TH_ENCCTL_2PASS_IN, (guint8 *) cache_data,
                           bytes_read);
        gst_adapter_unmap (enc->multipass_cache_adapter);

        done = bytes_consumed <= 0;
        if (bytes_consumed > 0)
            gst_adapter_flush (enc->multipass_cache_adapter, bytes_consumed);
    }

    if (stat == G_IO_STATUS_ERROR || (stat == G_IO_STATUS_EOF && bytes_read == 0)
            || bytes_consumed < 0) {
        GST_ELEMENT_ERROR (enc, RESOURCE, READ, (NULL),
                           ("Failed to read multipass cache file"));
        return FALSE;
    }
    return TRUE;
}
Esempio n. 20
0
gboolean test_io_cb(GIOChannel *io, GIOCondition cond, gpointer user_data)
{
	struct test_data *d = user_data;
	GIOStatus status;
	gsize bytes_written, rbytes, send_buf_len, expect_len;
	char buf[65535];
	const char *send_buf, *expect;

	expect = d->recv[d->count].data;
	expect_len = d->recv[d->count].len;
	send_buf = d->send[d->count].data;
	send_buf_len = d->send[d->count].len;

	d->count++;

	status = g_io_channel_read_chars(io, buf, sizeof(buf), &rbytes, NULL);
	if (status != G_IO_STATUS_NORMAL) {
		g_print("io_cb count %u\n", d->count);
		g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
				"Reading data failed with status %d", status);
		goto failed;
	}

	if (rbytes < expect_len) {
		g_print("io_cb count %u\n", d->count);
		dump_bufs(expect, expect_len, buf, rbytes);
		g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
					"Not enough data from socket");
		goto failed;
	}

	if (memcmp(buf, expect, expect_len) != 0) {
		g_print("io_cb count %u\n", d->count);
		dump_bufs(expect, expect_len, buf, rbytes);
		g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
					"Received data is not correct");
		goto failed;
	}

	if (send_buf_len == 0)
		goto failed;

	g_io_channel_write_chars(io, send_buf, send_buf_len, &bytes_written,
									NULL);
	if (bytes_written != send_buf_len) {
		g_print("io_cb count %u\n", d->count);
		g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
						"Unable to write to socket");
		goto failed;
	}

	return TRUE;

failed:
	g_main_loop_quit(d->mainloop);
	return FALSE;
}
Esempio n. 21
0
void pipe_callback(GIOChannel *source,GIOCondition condition,gpointer data) {
	char buf[128];
	gsize size;
	GError *err;
	
	g_io_channel_read_chars(source, buf, 128, &size, &err);
	printf("Kalle");
	return FALSE;
}
static gboolean
event_cb (GIOChannel   *source,
	  GIOCondition  condition,
	  CcRfkillGlib   *rfkill)
{
	GList *events;

	events = NULL;

	if (condition & G_IO_IN) {
		GIOStatus status;
		struct rfkill_event event;
		gsize read;

		status = g_io_channel_read_chars (source,
						  (char *) &event,
						  sizeof(event),
						  &read,
						  NULL);

		while (status == G_IO_STATUS_NORMAL && read == sizeof(event)) {
			struct rfkill_event *event_ptr;

			print_event (&event);

			event_ptr = g_memdup (&event, sizeof(event));
			events = g_list_prepend (events, event_ptr);

			status = g_io_channel_read_chars (source,
							  (char *) &event,
							  sizeof(event),
							  &read,
							  NULL);
		}
		events = g_list_reverse (events);
	} else {
		g_debug ("something else happened");
		return FALSE;
	}

	emit_changed_signal_and_free (rfkill, events);

	return TRUE;
}
Esempio n. 23
0
static gboolean pass_handle_data(struct pending_client *cl)
{
	gchar header[2];
	gsize read;
	gboolean accepted;
	GIOStatus status;
	gchar uname[0x100], pass[0x100];

	status = g_io_channel_read_chars(cl->connection, header, 2, &read, NULL);
	if (status != G_IO_STATUS_NORMAL) {
		return FALSE;
	}

	if (header[0] != SOCKS_VERSION && header[0] != 0x1) {
		listener_log(LOG_WARNING, cl->listener,
					 "Client suddenly changed socks uname/pwd version to %x",
					 header[0]);
	 	return listener_socks_error(cl, REP_GENERAL_FAILURE);
	}

	status = g_io_channel_read_chars(cl->connection, uname, header[1], &read,
									 NULL);
	if (status != G_IO_STATUS_NORMAL) {
		return FALSE;
	}

	uname[(guint8)header[1]] = '\0';

	status = g_io_channel_read_chars(cl->connection, header, 1, &read, NULL);
	if (status != G_IO_STATUS_NORMAL) {
		return FALSE;
	}

	status = g_io_channel_read_chars(cl->connection, pass, header[0], &read, NULL);
	if (status != G_IO_STATUS_NORMAL) {
		return FALSE;
	}

	pass[(guint8)header[0]] = '\0';

	accepted = cl->listener->ops->socks_auth_simple(cl, uname, pass,
													on_socks_pass_accepted);
	return TRUE;
}
Esempio n. 24
0
static gboolean write_to_child(
    GIOChannel  *child_stdin,
    GIOCondition condition,
    gpointer     data)
{
    GIOStatus      ret;
    GError        *err    = NULL;
    gsize          bytes_read, bytes_written;
    GIOChannel    *infile =  ((fd_write_data_t *)data)->infile;
    gchar         *buf    =  ((fd_write_data_t *)data)->buf;
    /** number of attempts for temporarily busy resource */
    unsigned char stillRetry = 10;

    static int br         = 0;

    if (condition & G_IO_HUP)
        g_critical("Write end of pipe died!");
    if (condition & G_IO_ERR)
        g_critical("Error writing to child process");

    while(stillRetry)
    {
        ret = g_io_channel_read_chars(infile, buf, fd_bufsize,
                                      &bytes_read,
                                      &err);
        switch (ret) {
          case G_IO_STATUS_ERROR:
            g_critical("Error reading: %s", err->message);
            break;
          case G_IO_STATUS_EOF:
            g_debug("eof from infile");
            g_io_channel_shutdown(child_stdin, TRUE, &err);
            return FALSE;
          case G_IO_STATUS_AGAIN:
            g_debug("resource temporarily busy");
            stillRetry--;
            continue;
          default:
            break;
        }
        break;
    }

    br += bytes_read;

    /* g_debug("Read %u bytes from input file (total: %lu).", bytes_read, br);*/

    ret = g_io_channel_write_chars(child_stdin, buf, bytes_read, &bytes_written,
                                   &err);

    if (ret == G_IO_STATUS_ERROR)
        g_critical("Error writing: %s", err->message);

    /* g_debug("Wrote %u bytes to child.", bytes_written); */
    return TRUE;
}
Esempio n. 25
0
static gboolean prompt_read(GIOChannel *chan, GIOCondition cond,
                            gpointer user_data)
{
    gsize bytes_read;
    GIOStatus status;
    GError *err = NULL;
    int i;


    if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
        g_io_channel_unref(chan);
        return FALSE;
    }

    while (cond & G_IO_IN) {
        bytes_read = 0;
        status = g_io_channel_read_chars(chan, &inp[didx], INPUT_SIZE - didx, &bytes_read, &err);
        inp[didx + bytes_read] = '\0';
        if (bytes_read && (status == G_IO_STATUS_NORMAL)) {
            for (i = 0; (i < INPUT_SIZE) && (i < (didx + bytes_read)); i++) {
                if (inp[i] == '\r' || inp[i] == '\n')
                    inp[i] = '\0';
                if (!inp[i]) {
                    didx = i;
                    break;
                }
            }
            if (inp[i] == '\0') {
                if (didx) {
                    didx = 0;
                    parse_line(inp);
                }
                //printf("\r%s", get_prompt());
                //fflush(stdout);
                return TRUE;
            } else if (didx < INPUT_SIZE) {
                inp[didx] = '\0';
                if (didx == INPUT_SIZE)
                    didx--;
            } else {
                /* Should never get here */
                didx--;
            }
        } else if (err) {
            printf("Error: %s\n", err->message);
            break;
        } else {
            printf("IO Done\n");
            break;
        }
    }
    printf("Cond: %d\n", g_io_channel_get_buffer_condition(chan));


    return TRUE;
}
Esempio n. 26
0
static void
ipc_recv_and_dispatch_or_enqueue(ipc_endpoint_t *ipc)
{
    g_assert(ipc);

    ipc_recv_state_t *state = &ipc->recv_state;
    GIOChannel *channel = ipc->channel;

    gchar *buf = (state->hdr_done ? state->payload : &state->hdr) + state->bytes_read;
    gsize remaining = (state->hdr_done ? state->hdr.length : sizeof(state->hdr)) - state->bytes_read;
    gsize bytes_read;
    GError *error = NULL;

    switch (g_io_channel_read_chars(channel, buf, remaining, &bytes_read, &error)) {
        case G_IO_STATUS_NORMAL:
            break;
        case G_IO_STATUS_AGAIN:
            return;
        case G_IO_STATUS_EOF:
            return;
        case G_IO_STATUS_ERROR:
            if (!g_str_equal(ipc->name, "UI"))
            if (!g_str_equal(error->message, "Connection reset by peer"))
                error("g_io_channel_read_chars(): %s", error->message);
            g_error_free(error);
            return;
        default:
            g_assert_not_reached();
    }

    /* Update ipc_recv state */
    state->bytes_read += bytes_read;
    remaining -= bytes_read;

    if (remaining > 0)
        return;

    /* If we've just finished downloading the header... */
    if (!state->hdr_done) {
        /* ... update state, and try to download payload */
        state->hdr_done = TRUE;
        state->bytes_read = 0;
        state->payload = g_malloc(state->hdr.length);
        ipc_recv_and_dispatch_or_enqueue(ipc);
        return;
    }

    /* Otherwise, we finished downloading the message */
    ipc_dispatch(ipc, state->hdr, state->payload);
    g_free(state->payload);

    /* Reset state for the next message */
    state->payload = NULL;
    state->bytes_read = 0;
    state->hdr_done = FALSE;
}
Esempio n. 27
0
static gboolean maki_dcc_send_out_write (GIOChannel* source, GIOCondition condition, gpointer data)
{
	gchar buffer[1024];
	gsize bytes_read;
	GIOStatus status;
	makiDCCSend* dcc = data;

	if (condition & (G_IO_HUP | G_IO_ERR))
	{
		goto error;
	}

	if ((status = g_io_channel_read_chars(dcc->channel.file, buffer, 1024, &bytes_read, NULL)) != G_IO_STATUS_ERROR)
	{
		if (bytes_read > 0)
		{
			dcc->position += bytes_read;

			i_io_channel_write_chars(source, buffer, bytes_read, NULL, NULL);
			g_io_channel_flush(source, NULL);
		}

		if (dcc->position >= dcc->size || status == G_IO_STATUS_EOF)
		{
			if (dcc->d.out.ack.position < dcc->size)
			{
				dcc->d.out.wait = TRUE;
			}

			goto finish;
		}
	}

	if (status == G_IO_STATUS_ERROR)
	{
		goto error;
	}

	return TRUE;

error:
	dcc->status |= s_error;
finish:
	if (!dcc->d.out.wait)
	{
		dcc->status &= ~s_running;

		maki_dcc_send_close(dcc);

		maki_dcc_send_emit(dcc);
	}

	dcc->d.out.sources[s_out_write] = 0;

	return FALSE;
}
Esempio n. 28
0
static gboolean received_data(GIOChannel *channel, GIOCondition cond,
							gpointer data)
{
	GAtMux *mux = data;
	int i;
	GIOStatus status;
	gsize bytes_read;

	if (cond & G_IO_NVAL)
		return FALSE;

	debug(mux, "received data");

	bytes_read = 0;
	status = g_io_channel_read_chars(mux->channel, mux->buf + mux->buf_used,
					sizeof(mux->buf) - mux->buf_used,
					&bytes_read, NULL);

	mux->buf_used += bytes_read;

	if (bytes_read > 0 && mux->driver->feed_data) {
		int nread;

		memset(mux->newdata, 0, BITMAP_SIZE);

		nread = mux->driver->feed_data(mux, mux->buf, mux->buf_used);
		mux->buf_used -= nread;

		if (mux->buf_used > 0)
			memmove(mux->buf, mux->buf + nread, mux->buf_used);

		for (i = 1; i <= MAX_CHANNELS; i++) {
			int offset = i / 8;
			int bit = i % 8;

			if (!(mux->newdata[offset] & (1 << bit)))
				continue;

			debug(mux, "dispatching sources for channel: %p",
				mux->dlcs[i-1]);

			dispatch_sources(mux->dlcs[i-1], G_IO_IN);
		}
	}

	if (cond & (G_IO_HUP | G_IO_ERR))
		return FALSE;

	if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
		return FALSE;

	if (mux->buf_used == sizeof(mux->buf))
		return FALSE;

	return TRUE;
}
static gboolean
matedialog_text_handle_stdin (GIOChannel  *channel,
                          GIOCondition condition,
                          gpointer     data)
{
  static GtkTextBuffer *buffer;
  gchar buf[1024];

  gsize len;

  buffer = GTK_TEXT_BUFFER (data);

  if ((condition & G_IO_IN) || (condition & (G_IO_IN | G_IO_HUP))) {
    GError *error = NULL;
    gint status;

    while (channel->is_readable != TRUE)
      ;

    do {
      status = g_io_channel_read_chars (channel, buf, 1024, &len, &error);

      while (gtk_events_pending ())
        gtk_main_iteration ();

    } while (status == G_IO_STATUS_AGAIN);

    if (status != G_IO_STATUS_NORMAL) {
      if (error) {
        g_warning ("matedialog_text_handle_stdin () : %s", error->message);
        g_error_free (error);
        error = NULL;
      }
      return FALSE;
    }

    if (len > 0) {
      GtkTextIter end;
      gchar *utftext; 
      gsize localelen; 
      gsize utflen;

      gtk_text_buffer_get_end_iter (buffer, &end);

      if (!g_utf8_validate (buf, len, NULL)) {
        utftext = g_convert_with_fallback (buf, len, "UTF-8", "ISO-8859-1", NULL, &localelen, &utflen, NULL);
        gtk_text_buffer_insert (buffer, &end, utftext, utflen);
        g_free (utftext);
      } else {
        gtk_text_buffer_insert (buffer, &end, buf, len);
      }
    }
  }

  return TRUE;
}
Esempio n. 30
0
static gboolean received_data(GIOChannel *channel, GIOCondition cond,
				gpointer data)
{
	unsigned char *buf;
	GRilIO *io = data;
	GIOStatus status;
	gsize rbytes;
	gsize toread;
	gsize total_read = 0;
	guint read_count = 0;

	if (cond & G_IO_NVAL)
		return FALSE;

	/* Regardless of condition, try to read all the data available */
	do {
		toread = ring_buffer_avail_no_wrap(io->buf);

		if (toread == 0)
			break;

		rbytes = 0;
		buf = ring_buffer_write_ptr(io->buf, 0);

		status = g_io_channel_read_chars(channel, (char *) buf,
							toread, &rbytes, NULL);

		g_ril_util_debug_hexdump(TRUE, buf, rbytes,
						io->debugf, io->debug_data);

		read_count++;

		total_read += rbytes;

		if (rbytes > 0)
			ring_buffer_write_advance(io->buf, rbytes);

	} while (status == G_IO_STATUS_NORMAL && rbytes > 0 &&
					read_count < io->max_read_attempts);

	if (total_read > 0 && io->read_handler)
		io->read_handler(io->buf, io->read_data);

	if (cond & (G_IO_HUP | G_IO_ERR))
		return FALSE;

	if (read_count > 0 && rbytes == 0 && status != G_IO_STATUS_AGAIN)
		return FALSE;

	/* We're overflowing the buffer, shutdown the socket */
	if (ring_buffer_avail(io->buf) == 0)
		return FALSE;

	return TRUE;
}