示例#1
0
static guint create_source_btdev(int fd, struct btdev *btdev)
{
	GIOChannel *channel;
	guint source;

	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);

	btdev_set_send_handler(btdev, writev_callback, channel);

	source = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT,
				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
				receive_btdev, btdev, NULL);

	g_io_channel_unref(channel);

	return source;
}
static void test_connect(const void *test_data)
{
	struct test_data *data = tester_get_data();
	const struct l2cap_data *l2data = data->test_data;
	GIOChannel *io;
	int sk;

	if (l2data->server_psm) {
		struct bthost *bthost = hciemu_client_get_host(data->hciemu);

		if (!l2data->data_len)
			bthost_add_l2cap_server(bthost, l2data->server_psm,
						NULL, NULL);
		else
			bthost_add_l2cap_server(bthost, l2data->server_psm,
						client_l2cap_connect_cb, data);
	}

	sk = create_l2cap_sock(data, 0, l2data->cid, l2data->sec_level);
	if (sk < 0) {
		tester_test_failed();
		return;
	}

	if (connect_l2cap_sock(data, sk, l2data->client_psm,
							l2data->cid) < 0) {
		close(sk);
		tester_test_failed();
		return;
	}

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

	data->io_id = g_io_add_watch(io, G_IO_OUT, l2cap_connect_cb, NULL);

	g_io_channel_unref(io);

	tester_print("Connect in progress");
}
示例#3
0
文件: avrcp.c 项目: mjbshaw/bluez
static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
{
	struct avrcp_device *dev = user_data;
	uint16_t imtu, omtu;
	char address[18];
	GError *gerr = NULL;
	int fd;

	if (err) {
		error("%s", err->message);
		return;
	}

	bt_io_get(chan, &gerr,
			BT_IO_OPT_DEST, address,
			BT_IO_OPT_IMTU, &imtu,
			BT_IO_OPT_OMTU, &omtu,
			BT_IO_OPT_INVALID);
	if (gerr) {
		error("%s", gerr->message);
		g_error_free(gerr);
		g_io_channel_shutdown(chan, TRUE, NULL);
		return;
	}

	fd = g_io_channel_unix_get_fd(chan);
	if (avrcp_device_add_session(dev, fd, imtu, omtu) < 0) {
		avrcp_device_free(dev);
		return;
	}

	g_io_channel_set_close_on_unref(chan, FALSE);

	if (dev->io) {
		g_io_channel_unref(dev->io);
		dev->io = NULL;
	}

	DBG("%s connected", address);
}
示例#4
0
static gboolean accept_watch(GIOChannel *io, GIOCondition cond,
						gpointer user_data)
{
	GIOChannel *cli_io;
	GIOCondition cli_cond = G_IO_IN | G_IO_ERR | G_IO_HUP;
	struct sockaddr_in client;
	int svr_sk, cli_sk, err;
	socklen_t sklen;

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

	svr_sk = g_io_channel_unix_get_fd(io);

	sklen = sizeof(client);
	memset(&client, 0, sklen);

	cli_sk = accept(svr_sk, (struct sockaddr *) &client, &sklen);
	if (cli_sk == -1) {
		err = errno;
		printf("accept(): %s(%d)\n", strerror(err), err);
		return TRUE;
	}

	printf("Peer's IP address is: %s\n", inet_ntoa(client.sin_addr));

	cli_io = g_io_channel_unix_new(cli_sk);
	g_io_channel_set_close_on_unref(cli_io, TRUE);

	/* Ending 'NULL' for binary data */
	g_io_channel_set_encoding(cli_io, NULL, NULL);
	g_io_channel_set_buffered(cli_io, FALSE);

	/* TCP client handler: incoming data from nrfd */
	g_io_add_watch(cli_io, cli_cond, data_watch, NULL);

	g_io_channel_unref(cli_io);

	return TRUE;
}
示例#5
0
int __connman_timezone_init(void)
{
	GIOChannel *channel;
	char *dirname;
	int fd, wd;

	DBG("");

	fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
	if (fd < 0)
		return -EIO;

	channel = g_io_channel_unix_new(fd);
	if (channel == NULL) {
		close(fd);
		return -EIO;
	}

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

	inotify_watch = g_io_add_watch(channel,
				G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
				inotify_data, NULL);

	g_io_channel_unref(channel);

	dirname = g_path_get_dirname(ETC_LOCALTIME);

	wd = inotify_add_watch(fd, dirname, IN_DONT_FOLLOW |
						IN_CLOSE_WRITE | IN_MOVED_TO);

	g_free(dirname);

	if (wd < 0)
		return -EIO;

	return 0;
}
示例#6
0
文件: client.c 项目: azuwis/mpd
static void client_init(struct client *client, int fd)
{
    static unsigned int next_client_num;

    assert(fd >= 0);

    client->cmd_list_size = 0;
    client->cmd_list_OK = -1;

#ifndef G_OS_WIN32
    client->channel = g_io_channel_unix_new(fd);
#else
    client->channel = g_io_channel_win32_new_socket(fd);
#endif
    /* GLib is responsible for closing the file descriptor */
    g_io_channel_set_close_on_unref(client->channel, true);
    /* NULL encoding means the stream is binary safe; the MPD
       protocol is UTF-8 only, but we are doing this call anyway
       to prevent GLib from messing around with the stream */
    g_io_channel_set_encoding(client->channel, NULL, NULL);
    /* we prefer to do buffering */
    g_io_channel_set_buffered(client->channel, false);

    client->source_id = g_io_add_watch(client->channel,
                                       G_IO_IN|G_IO_ERR|G_IO_HUP,
                                       client_in_event, client);

    client->input = fifo_buffer_new(4096);

    client->lastTime = time(NULL);
    client->cmd_list = NULL;
    client->deferred_send = g_queue_new();
    client->deferred_bytes = 0;
    client->num = next_client_num++;
    client->send_buf_used = 0;

    client->permission = getDefaultPermissions();

    (void)write(fd, GREETING, sizeof(GREETING) - 1);
}
示例#7
0
int interactive(const gchar *src, const gchar *dst, int psm)
{
    GIOChannel *pchan;
    gint events;

    opt_sec_level = g_strdup("low");

    inp = g_malloc0(INPUT_SIZE + 1);
    opt_src = g_strdup(src);
    opt_dst = g_strdup(dst);
    opt_psm = psm;

    prompt = g_string_new(NULL);

    event_loop = g_main_loop_new(NULL, FALSE);

    pchan = g_io_channel_unix_new(fileno(stdin));
    g_io_channel_set_close_on_unref(pchan, TRUE);
    g_io_channel_set_encoding(pchan, NULL, NULL);
    g_io_channel_set_buffered(pchan, FALSE);
    events = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
    g_io_add_watch(pchan, events, prompt_read, NULL);

    printf("\r%s", get_prompt());
    fflush(stdout);
    g_main_loop_run(event_loop);

    cmd_disconnect(0, NULL);
    g_io_channel_unref(pchan);
    g_main_loop_unref(event_loop);
    g_string_free(prompt, TRUE);

    g_free(opt_src);
    g_free(opt_dst);
    g_free(opt_sec_level);
    g_free(inp);

    return 0;
}
static void test_server(const void *test_data)
{
	struct test_data *data = tester_get_data();
	const struct rfcomm_server_data *server_data = data->test_data;
	const uint8_t *master_addr;
	struct bthost *bthost;
	GIOChannel *io;
	int sk;

	master_addr = hciemu_get_master_bdaddr(data->hciemu);

	sk = create_rfcomm_sock((bdaddr_t *) master_addr,
						server_data->server_channel);
	if (sk < 0) {
		tester_test_failed();
		return;
	}

	if (listen(sk, 5) < 0) {
		tester_warn("listening on socket failed: %s (%u)",
				strerror(errno), errno);
		tester_test_failed();
		close(sk);
		return;
	}

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

	data->io_id = g_io_add_watch(io, G_IO_IN, rfcomm_listen_cb, NULL);
	g_io_channel_unref(io);

	tester_print("Listening for connections");

	bthost = hciemu_client_get_host(data->hciemu);
	bthost_set_connect_cb(bthost, client_new_conn, data);

	bthost_hci_connect(bthost, master_addr, BDADDR_BREDR);
}
示例#9
0
static int create_watch(const char *path, struct connman_inotify *inotify)
{
	int fd;

	DBG("Add directory watch for %s", path);

	fd = inotify_init();
	if (fd < 0)
		return -EIO;

	inotify->wd = inotify_add_watch(fd, path,
					IN_MODIFY | IN_CREATE | IN_DELETE |
					IN_MOVED_TO | IN_MOVED_FROM);
	if (inotify->wd < 0) {
		connman_error("Creation of %s watch failed", path);
		close(fd);
		return -EIO;
	}

	inotify->channel = g_io_channel_unix_new(fd);
	if (inotify->channel == NULL) {
		connman_error("Creation of inotify channel failed");
		inotify_rm_watch(fd, inotify->wd);
		inotify->wd = 0;

		close(fd);
		return -EIO;
	}

	g_io_channel_set_close_on_unref(inotify->channel, TRUE);
	g_io_channel_set_encoding(inotify->channel, NULL, NULL);
	g_io_channel_set_buffered(inotify->channel, FALSE);

	inotify->watch = g_io_add_watch(inotify->channel,
				G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
				inotify_data, inotify);

	return 0;
}
示例#10
0
文件: gatmux.c 项目: Conjuror/ofono
GAtMux *g_at_mux_new(GIOChannel *channel, const GAtMuxDriver *driver)
{
	GAtMux *mux;

	if (channel == NULL)
		return NULL;

	mux = g_try_new0(GAtMux, 1);
	if (mux == NULL)
		return NULL;

	mux->ref_count = 1;
	mux->driver = driver;
	mux->shutdown = TRUE;

	mux->channel = channel;
	g_io_channel_ref(channel);

	g_io_channel_set_close_on_unref(channel, TRUE);

	return mux;
}
示例#11
0
int interactive(const gchar *src, const gchar *dst,
		const gchar *dst_type, int psm)
{
	GIOChannel *pchan;
	gint events;

	opt_sec_level = g_strdup("low");

	opt_src = g_strdup(src);
	opt_dst = g_strdup(dst);
	opt_dst_type = g_strdup(dst_type);
	opt_psm = psm;

	prompt = g_string_new(NULL);

	event_loop = g_main_loop_new(NULL, FALSE);

	pchan = g_io_channel_unix_new(fileno(stdin));
	g_io_channel_set_close_on_unref(pchan, TRUE);
	events = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
	g_io_add_watch(pchan, events, prompt_read, NULL);

	rl_attempted_completion_function = commands_completion;
	rl_callback_handler_install(get_prompt(), parse_line);

	g_main_loop_run(event_loop);

	rl_callback_handler_remove();
	cmd_disconnect(0, NULL);
	g_io_channel_unref(pchan);
	g_main_loop_unref(event_loop);
	g_string_free(prompt, TRUE);

	g_free(opt_src);
	g_free(opt_dst);
	g_free(opt_sec_level);

	return 0;
}
示例#12
0
文件: testsocket.c 项目: BYC/gtk
void
add_child (GtkWidget *window,
	   gboolean   active)
{
  Socket *socket;
  char *argv[3] = { "./testsocket_child", NULL, NULL };
  char buffer[20];
  int out_fd;
  GIOChannel *channel;
  GError *error = NULL;

  if (active)
    {
      socket = create_socket ();
      gtk_box_pack_start (GTK_BOX (box), socket->box, TRUE, TRUE, 0);
      gtk_widget_show (socket->box);
      sprintf(buffer, "%#lx", (gulong) gtk_socket_get_id (GTK_SOCKET (socket->socket)));
      argv[1] = buffer;
    }
  
  if (!g_spawn_async_with_pipes (NULL, argv, NULL, 0, NULL, NULL, NULL, NULL, &out_fd, NULL, &error))
    {
      fprintf (stderr, "Can't exec testsocket_child: %s\n", error->message);
      exit (1);
    }

  n_children++;
  channel = g_io_channel_unix_new (out_fd);
  g_io_channel_set_close_on_unref (channel, TRUE);
  g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, &error);
  if (error)
    {
      fprintf (stderr, "Error making channel non-blocking: %s\n", error->message);
      exit (1);
    }
  
  g_io_add_watch (channel, G_IO_IN | G_IO_HUP, child_read_watch, NULL);
  g_io_channel_unref (channel);
}
示例#13
0
文件: main.c 项目: hmallat/obexd
static guint setup_signalfd(void)
{
	GIOChannel *channel;
	guint source;
	sigset_t mask;
	int fd;

	sigemptyset(&mask);
	sigaddset(&mask, SIGINT);
	sigaddset(&mask, SIGTERM);
	sigaddset(&mask, SIGUSR2);
	sigaddset(&mask, SIGPIPE);

	if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
		perror("Failed to set signal mask");
		return 0;
	}

	fd = signalfd(-1, &mask, 0);
	if (fd < 0) {
		perror("Failed to create signal descriptor");
		return 0;
	}

	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);

	source = g_io_add_watch(channel,
				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
				signal_handler, NULL);

	g_io_channel_unref(channel);

	return source;
}
示例#14
0
static gboolean io_accept_event(GIOChannel *chan, GIOCondition cond, gpointer data)
{
	GIOChannel *io;
	int nsk;

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

	if (data == &l2cap_sock) {
		struct sockaddr_l2 addr;
		socklen_t len = sizeof(addr);

		nsk = accept(l2cap_sock, (struct sockaddr *) &addr, &len);
	} else if (data == &unix_sock) {
		struct sockaddr_un addr;
		socklen_t len = sizeof(addr);

		nsk = accept(unix_sock, (struct sockaddr *) &addr, &len);
	} else
		return FALSE;

	if (nsk < 0) {
		error("Can't accept connection: %s", strerror(errno));
		return TRUE;
	}

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

	g_io_add_watch(io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
					io_session_event, data);

	g_io_channel_unref(io);

	return TRUE;
}
示例#15
0
struct pending_client *listener_new_pending_client(struct irc_listener *listener, GIOChannel *c)
{
	struct pending_client *pc;

	if (listener->ssl) {
#ifdef HAVE_GNUTLS
		c = ssl_wrap_iochannel(c, SSL_TYPE_SERVER,
							 NULL, listener->ssl_credentials);
		g_assert(c != NULL);
#else
		listener_log(LOG_WARNING, listener, "SSL support not available, not listening for SSL connection");
#endif
	}

	g_io_channel_set_close_on_unref(c, TRUE);
	g_io_channel_set_encoding(c, NULL, NULL);
	g_io_channel_set_flags(c, G_IO_FLAG_NONBLOCK, NULL);
	pc = g_new0(struct pending_client, 1);
#ifdef HAVE_GSSAPI
	pc->authn_name = GSS_C_NO_NAME;
	pc->gss_ctx = GSS_C_NO_CONTEXT;
	pc->gss_service = GSS_C_NO_NAME;
	pc->service_cred = GSS_C_NO_CREDENTIAL;
#endif
	pc->connection = c;
	pc->watch_id = g_io_add_watch(c, G_IO_IN | G_IO_HUP, handle_client_receive, pc);
	pc->listener = listener;

	g_io_channel_unref(c);

	listener->pending = g_list_append(listener->pending, pc);

	if (listener->ops->new_client)
		listener->ops->new_client(pc);

	return pc;
}
示例#16
0
static void test_connect(gboolean use_socket)
{
	GIOChannel *io;
	GAtChat *chat;
	GAtSyntax *syntax;
	int fd;

	if (use_socket == TRUE)
		fd = do_connect();
	else
		fd = do_open();

	if (fd < 0)
		return;

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

	syntax = g_at_syntax_new_gsm_permissive();
	chat = g_at_chat_new_blocking(io, syntax);
	g_at_syntax_unref(syntax);

	g_io_channel_unref(io);

	if (chat == NULL) {
		g_printerr("Chat creation failed\n");
		return;
	}

	g_at_chat_set_debug(chat, caif_debug, NULL);
	g_at_chat_send(chat, "ATE0 +CMEE=1", NULL, caif_init, chat, NULL);

	mainloop = g_main_loop_new(NULL, FALSE);

	g_main_loop_run(mainloop);
	g_main_loop_unref(mainloop);
}
示例#17
0
static void rfcomm_callback(GIOChannel *io, GError *err, gpointer user_data)
{
	struct callback_data *callback = user_data;
	struct obc_session *session = callback->session;
	struct obc_driver *driver = session->driver;
	GwObex *obex;
	int fd;

	DBG("");

	if (err != NULL) {
		error("%s", err->message);
		goto done;
	}

	/* do not close when gw_obex is using the fd */
	g_io_channel_set_close_on_unref(session->io, FALSE);
	g_io_channel_unref(session->io);
	session->io = NULL;

	fd = g_io_channel_unix_get_fd(io);

	obex = gw_obex_setup_fd(fd, driver->target, driver->target_len,
								NULL, NULL);

	session->obex = obex;

	sessions = g_slist_prepend(sessions, session);

done:
	callback->func(callback->session, err, callback->data);

	obc_session_unref(callback->session);

	g_free(callback);
}
示例#18
0
void rfkill_init(void)
{
	int fd;

	if (!main_opts.remember_powered)
		return;

	fd = open("/dev/rfkill", O_RDWR);
	if (fd < 0) {
		error("Failed to open RFKILL control device. Try again");
		usleep(1000000);
		fd = open("/dev/rfkill", O_RDWR);
		if(fd < 0) {
			error("Failed to open RFKILL control device.");
			return;
		}
	}

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

	g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
							rfkill_event, NULL);
}
示例#19
0
文件: bnep.c 项目: mgumiero9/bluele4
struct bnep *bnep_new(int sk, uint16_t local_role, uint16_t remote_role,
								char *iface)
{
	struct bnep *session;
	int dup_fd;

	dup_fd = dup(sk);
	if (dup_fd < 0)
		return NULL;

	session = g_new0(struct bnep, 1);
	session->io = g_io_channel_unix_new(dup_fd);
	session->src = local_role;
	session->dst = remote_role;
	strncpy(session->iface, iface, 16);
	session->iface[15] = '\0';

	g_io_channel_set_close_on_unref(session->io, TRUE);
	session->watch = g_io_add_watch(session->io,
				G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
					(GIOFunc) bnep_setup_cb, session);

	return session;
}
示例#20
0
文件: gntdebug.c 项目: bf4/pidgin-mac
static void
handle_fprintf_stderr(gboolean stop)
{
	GIOChannel *stderrch;
	static int readhandle = -1;
	int pipes[2];

	if (stop) {
		if (readhandle >= 0) {
			g_source_remove(readhandle);
			readhandle = -1;
		}
		return;
	}
	pipe(pipes);
	dup2(pipes[1], STDERR_FILENO);

	stderrch = g_io_channel_unix_new(pipes[0]);
	g_io_channel_set_close_on_unref(stderrch, TRUE);
	readhandle = g_io_add_watch_full(stderrch, G_PRIORITY_HIGH,
			G_IO_IN | G_IO_ERR | G_IO_PRI,
			handle_fprintf_stderr_cb, NULL, NULL);
	g_io_channel_unref(stderrch);
}
示例#21
0
static void connect_socket(const uint8_t *client_bdaddr, int *sk_holder,
							GIOFunc connect_cb)
{
	struct test_data *data = tester_get_data();
	const struct l2cap_data *l2data = data->test_data;
	GIOChannel *io;
	int sk;

	sk = create_l2cap_sock(data, 0, l2data->cid, l2data->sec_level);
	if (sk < 0) {
		tester_print("Error in create_l2cap_sock");
		tester_test_failed();
		return;
	}

	*sk_holder = sk;

	if (connect_l2cap_impl(sk, client_bdaddr, BDADDR_LE_PUBLIC,
			l2data->client_psm, l2data->cid) < 0) {
		tester_print("Error in connect_l2cap_sock");
		close(sk);
		tester_test_failed();
		return;
	}

	if (connect_cb) {
		io = g_io_channel_unix_new(sk);
		g_io_channel_set_close_on_unref(io, TRUE);

		data->io_id = g_io_add_watch(io, G_IO_OUT, connect_cb, NULL);

		g_io_channel_unref(io);
	}

	tester_print("Connect in progress, sk = %d", sk);
}
示例#22
0
int main(int argc, char *argv[])
{
	GOptionContext *context;
	GError *gerr = NULL;
	GIOChannel *inotify_io;
	struct stat sb;
	int err, retval = 0;
	int inotifyFD, wd;
	guint watch_id;

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

	if (!g_option_context_parse(context, &argc, &argv, &gerr)) {
		g_printerr("Invalid arguments: %s\n", gerr->message);
		g_error_free(gerr);
		g_option_context_free(context);
		return EXIT_FAILURE;
	}

	g_option_context_free(context);

	if (stat(opt_cfg, &sb) == -1) {
		err = errno;
		g_printerr("%s: %s(%d)\n", opt_cfg, strerror(err), err);
		return EXIT_FAILURE;
	}

	if ((sb.st_mode & S_IFMT) != S_IFREG) {
		g_printerr("%s is not a regular file!\n", opt_cfg);
		return EXIT_FAILURE;
	}

	if (!opt_nodes) {
		g_printerr("Missing KNOT known nodes file!\n");
		return EXIT_FAILURE;
	}

	signal(SIGTERM, sig_term);
	signal(SIGINT, sig_term);
	signal(SIGPIPE, SIG_IGN);

	main_loop = g_main_loop_new(NULL, FALSE);

	hal_log_init("nrfd", opt_detach);
	hal_log_info("KNOT HAL nrfd");

	if (opt_host)
		hal_log_error("Development mode: %s:%u", opt_host, opt_port);

	err = manager_start(opt_cfg, opt_host, opt_port, opt_spi, opt_channel,
							opt_dbm, opt_nodes);
	if (err < 0) {
		hal_log_error("manager_start(): %s(%d)", strerror(-err), -err);
		g_main_loop_unref(main_loop);
		hal_log_close();
		return EXIT_FAILURE;
	}

	/* Set user id to nobody */
	if (setuid(65534) != 0) {
		err = errno;
		hal_log_error("Set uid to nobody failed. %s(%d). Exiting...",
							strerror(err), err);
		manager_stop();
		hal_log_close();
		return EXIT_FAILURE;
	}

	/* Starting inotify */
	inotifyFD = inotify_init();
	wd = inotify_add_watch(inotifyFD, opt_cfg, IN_MODIFY);
	if (wd == -1) {
		hal_log_error("Error adding watch on: %s", opt_cfg);
		close(inotifyFD);
		manager_stop();
		hal_log_close();
		return EXIT_FAILURE;
	}

	/* Setting gio channel to watch inotify fd*/
	inotify_io = g_io_channel_unix_new(inotifyFD);
	watch_id = g_io_add_watch(inotify_io, G_IO_IN, inotify_cb, NULL);
	g_io_channel_set_close_on_unref(inotify_io, TRUE);

	if (opt_detach) {
		if (daemon(0, 0)) {
			hal_log_error("Can't start daemon!");
			retval = EXIT_FAILURE;
			goto done;
		}
	}

	g_main_loop_run(main_loop);

done:
	g_source_remove(watch_id);
	inotify_rm_watch(inotifyFD, wd);
	g_io_channel_unref(inotify_io);

	manager_stop();

	hal_log_error("exiting ...");
	hal_log_close();

	g_main_loop_unref(main_loop);

	return retval;
}
示例#23
0
gboolean
gimp_plug_in_open (GimpPlugIn         *plug_in,
                   GimpPlugInCallMode  call_mode,
                   gboolean            synchronous)
{
  gint          my_read[2];
  gint          my_write[2];
  gchar       **envp;
  const gchar  *args[9];
  gchar       **argv;
  gint          argc;
  gchar        *interp, *interp_arg;
  gchar        *read_fd, *write_fd;
  const gchar  *mode;
  gchar        *stm;
  GError       *error = NULL;
  gboolean      debug;
  guint         debug_flag;
  guint         spawn_flags;

  g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), FALSE);
  g_return_val_if_fail (plug_in->call_mode == GIMP_PLUG_IN_CALL_NONE, FALSE);

  /* Open two pipes. (Bidirectional communication).
   */
  if ((pipe (my_read) == -1) || (pipe (my_write) == -1))
    {
      gimp_message (plug_in->manager->gimp, NULL, GIMP_MESSAGE_ERROR,
                    "Unable to run plug-in \"%s\"\n(%s)\n\npipe() failed: %s",
                    gimp_object_get_name (plug_in),
                    gimp_filename_to_utf8 (plug_in->prog),
                    g_strerror (errno));
      return FALSE;
    }

#if defined(G_WITH_CYGWIN)
  /* Set to binary mode */
  setmode (my_read[0], _O_BINARY);
  setmode (my_write[0], _O_BINARY);
  setmode (my_read[1], _O_BINARY);
  setmode (my_write[1], _O_BINARY);
#endif

#ifdef G_OS_WIN32
  /* Prevent the plug-in from inheriting our ends of the pipes */
  SetHandleInformation ((HANDLE) _get_osfhandle (my_read[0]), HANDLE_FLAG_INHERIT, 0);
  SetHandleInformation ((HANDLE) _get_osfhandle (my_write[1]), HANDLE_FLAG_INHERIT, 0);
#endif

  plug_in->my_read   = g_io_channel_unix_new (my_read[0]);
  plug_in->my_write  = g_io_channel_unix_new (my_write[1]);
  plug_in->his_read  = g_io_channel_unix_new (my_write[0]);
  plug_in->his_write = g_io_channel_unix_new (my_read[1]);

  g_io_channel_set_encoding (plug_in->my_read, NULL, NULL);
  g_io_channel_set_encoding (plug_in->my_write, NULL, NULL);
  g_io_channel_set_encoding (plug_in->his_read, NULL, NULL);
  g_io_channel_set_encoding (plug_in->his_write, NULL, NULL);

  g_io_channel_set_buffered (plug_in->my_read, FALSE);
  g_io_channel_set_buffered (plug_in->my_write, FALSE);
  g_io_channel_set_buffered (plug_in->his_read, FALSE);
  g_io_channel_set_buffered (plug_in->his_write, FALSE);

  g_io_channel_set_close_on_unref (plug_in->my_read, TRUE);
  g_io_channel_set_close_on_unref (plug_in->my_write, TRUE);
  g_io_channel_set_close_on_unref (plug_in->his_read, TRUE);
  g_io_channel_set_close_on_unref (plug_in->his_write, TRUE);

  /* Remember the file descriptors for the pipes.
   */
  read_fd  = g_strdup_printf ("%d",
                              g_io_channel_unix_get_fd (plug_in->his_read));
  write_fd = g_strdup_printf ("%d",
                              g_io_channel_unix_get_fd (plug_in->his_write));

  switch (call_mode)
    {
    case GIMP_PLUG_IN_CALL_QUERY:
      mode = "-query";
      debug_flag = GIMP_DEBUG_WRAP_QUERY;
      break;

    case GIMP_PLUG_IN_CALL_INIT:
      mode = "-init";
      debug_flag = GIMP_DEBUG_WRAP_INIT;
      break;

    case GIMP_PLUG_IN_CALL_RUN:
      mode = "-run";
      debug_flag = GIMP_DEBUG_WRAP_RUN;
      break;

    default:
      g_assert_not_reached ();
    }

  stm = g_strdup_printf ("%d", plug_in->manager->gimp->stack_trace_mode);

  interp = gimp_interpreter_db_resolve (plug_in->manager->interpreter_db,
                                        plug_in->prog, &interp_arg);

  argc = 0;

  if (interp)
    args[argc++] = interp;

  if (interp_arg)
    args[argc++] = interp_arg;

  args[argc++] = plug_in->prog;
  args[argc++] = "-gimp";
  args[argc++] = read_fd;
  args[argc++] = write_fd;
  args[argc++] = mode;
  args[argc++] = stm;
  args[argc++] = NULL;

  argv = (gchar **) args;
  envp = gimp_environ_table_get_envp (plug_in->manager->environ_table);
  spawn_flags = (G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
                 G_SPAWN_DO_NOT_REAP_CHILD      |
                 G_SPAWN_CHILD_INHERITS_STDIN);

  debug = FALSE;

  if (plug_in->manager->debug)
    {
      gchar **debug_argv = gimp_plug_in_debug_argv (plug_in->manager->debug,
                                                    plug_in->prog,
                                                    debug_flag, args);

      if (debug_argv)
        {
          debug = TRUE;
          argv = debug_argv;
          spawn_flags |= G_SPAWN_SEARCH_PATH;
        }
    }

  /* Fork another process. We'll remember the process id so that we
   * can later use it to kill the filter if necessary.
   */
  if (! g_spawn_async (NULL, argv, envp, spawn_flags,
                       gimp_plug_in_prep_for_exec, plug_in,
                       &plug_in->pid,
                       &error))
    {
      gimp_message (plug_in->manager->gimp, NULL, GIMP_MESSAGE_ERROR,
                    "Unable to run plug-in \"%s\"\n(%s)\n\n%s",
                    gimp_object_get_name (plug_in),
                    gimp_filename_to_utf8 (plug_in->prog),
                    error->message);
      g_error_free (error);
      goto cleanup;
    }

  g_io_channel_unref (plug_in->his_read);
  plug_in->his_read = NULL;

  g_io_channel_unref (plug_in->his_write);
  plug_in->his_write = NULL;

  if (! synchronous)
    {
      GSource *source;

      source = g_io_create_watch (plug_in->my_read,
                                  G_IO_IN  | G_IO_PRI | G_IO_ERR | G_IO_HUP);

      g_source_set_callback (source,
                             (GSourceFunc) gimp_plug_in_recv_message, plug_in,
                             NULL);

      g_source_set_can_recurse (source, TRUE);

      plug_in->input_id = g_source_attach (source, NULL);
      g_source_unref (source);
    }

  plug_in->open      = TRUE;
  plug_in->call_mode = call_mode;

  gimp_plug_in_manager_add_open_plug_in (plug_in->manager, plug_in);

 cleanup:

  if (debug)
    g_free (argv);

  g_free (read_fd);
  g_free (write_fd);
  g_free (stm);
  g_free (interp);
  g_free (interp_arg);

  return plug_in->open;
}
static gpointer
dropbox_command_client_thread(DropboxCommandClient *dcc) {
  struct sockaddr_un addr;
  socklen_t addr_len;
  int connection_attempts = 1;

  /* intialize address structure */
  addr.sun_family = AF_UNIX;
  g_snprintf(addr.sun_path,
	     sizeof(addr.sun_path),
	     "%s/.dropbox/command_socket",
	     g_get_home_dir());
  addr_len = sizeof(addr) - sizeof(addr.sun_path) + strlen(addr.sun_path);

  while (1) {
    GIOChannel *chan = NULL;
    GError *gerr = NULL;
    int sock;
    gboolean failflag = TRUE;

    do {
      int flags;

      if (0 > (sock = socket(PF_UNIX, SOCK_STREAM, 0))) {
	/* WTF */
	break;
      }

      /* set timeout on socket, to protect against
	 bad servers */
      {
	struct timeval tv = {3, 0};
	if (0 > setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
			   &tv, sizeof(struct timeval)) ||
	    0 > setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
			   &tv, sizeof(struct timeval))) {
	  /* debug("setsockopt failed"); */
	  break;
	}
      }

      /* set native non-blocking, for connect timeout */
      {
	if ((flags = fcntl(sock, F_GETFL, 0)) < 0 ||
	    fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
	  /* debug("fcntl failed"); */
	  break;
	}
      }

      /* if there was an error we have to try again later */
      if (connect(sock, (struct sockaddr *) &addr, addr_len) < 0) {
	if (errno == EINPROGRESS) {
	  fd_set writers;
	  struct timeval tv = {1, 0};

	  FD_ZERO(&writers);
	  FD_SET(sock, &writers);

	  /* if nothing was ready after 3 seconds, fail out homie */
	  if (select(sock+1, NULL, &writers, NULL, &tv) == 0) {
	    /* debug("connection timeout"); */
	    break;
	  }

	  if (connect(sock, (struct sockaddr *) &addr, addr_len) < 0) {
	    /*	    debug("couldn't connect to command server after 1 second"); */
	    break;
	  }
	}
	/* errno != EINPROGRESS */
	else {
	  /*	  debug("bad connection"); */
	  break;
	}
      }

      /* set back to blocking */
      if (fcntl(sock, F_SETFL, flags) < 0) {
	/* debug("fcntl2 failed"); */
	break;
      }

      failflag = FALSE;
    } while (0);

    if (failflag) {
      ConnectionAttempt *ca = g_new(ConnectionAttempt, 1);
      ca->dcc = dcc;
      ca->connect_attempt = connection_attempts;
      g_idle_add((GSourceFunc) on_connection_attempt, ca);
      if (sock >= 0) {
	close(sock);
      }
      g_usleep(G_USEC_PER_SEC);
      connection_attempts++;
      continue;
    }
    else {
      connection_attempts = 0;
    }

    /* connected */
    debug("command client connected");

    chan = g_io_channel_unix_new(sock);
    g_io_channel_set_close_on_unref(chan, TRUE);
    g_io_channel_set_line_term(chan, "\n", -1);

#define SET_CONNECTED_STATE(s)     {			\
      g_mutex_lock(dcc->command_connected_mutex);	\
      dcc->command_connected = s;			\
      g_mutex_unlock(dcc->command_connected_mutex);	\
    }

    SET_CONNECTED_STATE(TRUE);

    g_idle_add((GSourceFunc) on_connect, dcc);

    while (1) {
      DropboxCommand *dc;

      while (1) {
	GTimeVal gtv;

	g_get_current_time(&gtv);
	g_time_val_add(&gtv, G_USEC_PER_SEC / 10);
	/* get a request from caja */
	dc = g_async_queue_timed_pop(dcc->command_queue, &gtv);
	if (dc != NULL) {
	  break;
	}
	else {
	  if (check_connection(chan) == FALSE) {
	    goto BADCONNECTION;
	  }
	}
      }

      /* this pointer should be unique */
      if ((gpointer (*)(DropboxCommandClient *data)) dc == &dropbox_command_client_thread) {
	debug("got a reset request");
	goto BADCONNECTION;
      }

      switch (dc->request_type) {
      case GET_FILE_INFO: {
	debug("doing file info command");
	do_file_info_command(chan, (DropboxFileInfoCommand *) dc, &gerr);
      }
	break;
      case GENERAL_COMMAND: {
	debug("doing general command");
	do_general_command(chan, (DropboxGeneralCommand *) dc, &gerr);
      }
	break;
      default: 
	g_assert_not_reached();
	break;
      }
      
      debug("done.");

      if (gerr != NULL) {
	//	debug("COMMAND ERROR*****************************");
	/* mark this request as never to be completed */
	end_request(dc);

	debug("command error: %s", gerr->message);
	
	g_error_free(gerr);
      BADCONNECTION:
	/* grab all the rest of the data off the async queue and mark it
	   never to be completed, who knows how long we'll be disconnected */
	while ((dc = g_async_queue_try_pop(dcc->command_queue)) != NULL) {
	  end_request(dc);
	}

	g_io_channel_unref(chan);

	SET_CONNECTED_STATE(FALSE);

	/* call the disconnect handler */
	g_idle_add((GSourceFunc) on_disconnect, dcc);

	break;
      }
    }
    
#undef SET_CONNECTED_STATE
  }
  
  return NULL;
}
示例#25
0
文件: gsmdial.c 项目: AndriusA/ofono
int main(int argc, char **argv)
{
	GOptionContext *context;
	GError *err = NULL;
	sigset_t mask;
	int signal_fd;
	GIOChannel *signal_io;
	int signal_source;

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

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

		g_printerr("An unknown error occurred\n");
		return 1;
	}

	g_option_context_free(context);

	if (option_control) {
		int ret;

		g_print("Control: %s\n", option_control);
		if (option_modem)
			g_print("Modem: %s\n", option_modem);

		ret = open_serial();
		g_free(option_control);
		g_free(option_modem);

		if (ret < 0)
			goto out;
	} else {
		int ret;

		g_print("IP: %s\n", option_ip);
		g_print("Port: %d\n", option_port);
		ret = open_ip();
		g_free(option_ip);

		if (ret < 0)
			goto out;
	}

	g_print("APN: %s\n", option_apn);
	g_print("CID: %d\n", option_cid);

	sigemptyset(&mask);
	sigaddset(&mask, SIGTERM);
	sigaddset(&mask, SIGINT);
	sigaddset(&mask, SIGUSR1);
	sigaddset(&mask, SIGUSR2);
	sigaddset(&mask, SIGPIPE);

	if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
		perror("Can't set signal mask");
		return 1;
	}

	signal_fd = signalfd(-1, &mask, 0);
	if (signal_fd < 0) {
		perror("Can't create signal filedescriptor");
		return 1;
	}

	signal_io = g_io_channel_unix_new(signal_fd);
	g_io_channel_set_close_on_unref(signal_io, TRUE);
	signal_source = g_io_add_watch(signal_io,
			G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
			signal_cb, GINT_TO_POINTER(signal_fd));
	g_io_channel_unref(signal_io);

	event_loop = g_main_loop_new(NULL, FALSE);

	if (option_bluetooth) {
		g_at_chat_send(control, "ATD*99#", none_prefix, connect_cb,
				NULL, NULL);
	} else {
		g_at_chat_send(control, "ATE0Q0V1", NULL, NULL, NULL, NULL);
		g_at_chat_send(control, "AT+CFUN?", cfun_prefix,
							check_mode, NULL, NULL);
	}

	g_main_loop_run(event_loop);
	g_source_remove(signal_source);
	g_main_loop_unref(event_loop);

out:
	if (ppp == NULL) {
		g_at_chat_unref(control);
		g_at_chat_unref(modem);
	} else
		g_at_ppp_unref(ppp);

	g_free(option_apn);

	return 0;
}
示例#26
0
文件: main.c 项目: brownsr/muffin
/**
 * meta_init: (skip)
 *
 * Initialize muffin. Call this after meta_get_option_context() and
 * meta_plugin_manager_set_plugin_type(), and before meta_run().
 */
void
meta_init (void)
{
  struct sigaction act;
  sigset_t empty_mask;
  GIOChannel *channel;

  sigemptyset (&empty_mask);
  act.sa_handler = SIG_IGN;
  act.sa_mask    = empty_mask;
  act.sa_flags   = 0;
  if (sigaction (SIGPIPE,  &act, NULL) < 0)
    g_printerr ("Failed to register SIGPIPE handler: %s\n",
                g_strerror (errno));
#ifdef SIGXFSZ
  if (sigaction (SIGXFSZ,  &act, NULL) < 0)
    g_printerr ("Failed to register SIGXFSZ handler: %s\n",
                g_strerror (errno));
#endif

  if (pipe (sigterm_pipe_fds) != 0)
    g_printerr ("Failed to create SIGTERM pipe: %s\n",
                g_strerror (errno));

  channel = g_io_channel_unix_new (sigterm_pipe_fds[0]);
  g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
  g_io_add_watch (channel, G_IO_IN, (GIOFunc) on_sigterm, NULL);
  g_io_channel_set_close_on_unref (channel, TRUE);
  g_io_channel_unref (channel);

  act.sa_handler = &sigterm_handler;
  if (sigaction (SIGTERM, &act, NULL) < 0)
    g_printerr ("Failed to register SIGTERM handler: %s\n",
		g_strerror (errno));

  if (g_getenv ("MUFFIN_VERBOSE"))
    meta_set_verbose (TRUE);
  if (g_getenv ("MUFFIN_DEBUG"))
    meta_set_debugging (TRUE);

  if (g_get_home_dir ())
    if (chdir (g_get_home_dir ()) < 0)
      meta_warning ("Could not change to home directory %s.\n",
                    g_get_home_dir ());

  meta_print_self_identity ();
  
#ifdef HAVE_INTROSPECTION
  g_irepository_prepend_search_path (MUFFIN_PKGLIBDIR);
#endif

  meta_set_syncing (opt_sync || (g_getenv ("MUFFIN_SYNC") != NULL));

  meta_select_display (opt_display_name);
  
  if (opt_replace_wm)
    meta_set_replace_current_wm (TRUE);

  if (opt_save_file && opt_client_id)
    meta_fatal ("Can't specify both SM save file and SM client id\n");
  
  meta_main_loop = g_main_loop_new (NULL, FALSE);
  
  meta_ui_init ();

  /*
   * Clutter can only be initialized after the UI.
   */
  meta_clutter_init ();
  
  const char *renderer = (const char *) glGetString (GL_RENDERER);
  if (strstr (renderer, "llvmpipe") ||
      strstr (renderer, "Rasterizer") ||
      strstr (renderer, "softpipe"))
  {
	/* Clutter envs not set, since they won't work after Clutter init */
    g_setenv ("CINNAMON_SOFTWARE_RENDERING", "1", FALSE);
    g_setenv ("CINNAMON_SLOWDOWN_FACTOR", "0.0001", FALSE);
    g_setenv ("MUFFIN_NO_SHADOWS", "1", FALSE);
    meta_warning ("Software rendering detected: %s\n", renderer);
  }
}
static gboolean l2cap_listen_cb(GIOChannel *io, GIOCondition cond,
							gpointer user_data)
{
	struct test_data *data = tester_get_data();
	const struct l2cap_data *l2data = data->test_data;
	int sk, new_sk;

	data->io_id = 0;

	sk = g_io_channel_unix_get_fd(io);

	new_sk = accept(sk, NULL, NULL);
	if (new_sk < 0) {
		tester_warn("accept failed: %s (%u)", strerror(errno), errno);
		tester_test_failed();
		return FALSE;
	}

	if (!check_mtu(data, new_sk)) {
		tester_test_failed();
		return FALSE;
	}

	if (l2data->read_data) {
		struct bthost *bthost;
		GIOChannel *new_io;

		new_io = g_io_channel_unix_new(new_sk);
		g_io_channel_set_close_on_unref(new_io, TRUE);

		bthost = hciemu_client_get_host(data->hciemu);
		g_io_add_watch(new_io, G_IO_IN, server_received_data, NULL);
		bthost_send_cid(bthost, data->handle, data->dcid,
					l2data->read_data, l2data->data_len);

		g_io_channel_unref(new_io);

		return FALSE;
	} else if (l2data->write_data) {
		struct bthost *bthost;
		ssize_t ret;

		bthost = hciemu_client_get_host(data->hciemu);
		bthost_add_cid_hook(bthost, data->handle, data->scid,
					server_bthost_received_data, NULL);

		ret = write(new_sk, l2data->write_data, l2data->data_len);
		close(new_sk);

		if (ret != l2data->data_len) {
			tester_warn("Unable to write all data");
			tester_test_failed();
		}

		return FALSE;
	}

	tester_print("Successfully connected");

	close(new_sk);

	tester_test_passed();

	return FALSE;
}
示例#28
0
static int connect_session_transport(struct web_session *session)
{
	GIOFlags flags;
	int sk;

	sk = socket(session->addr->ai_family, SOCK_STREAM | SOCK_CLOEXEC,
			IPPROTO_TCP);
	if (sk < 0)
		return -EIO;

	if (session->web->index > 0) {
		if (bind_socket(sk, session->web->index,
					session->addr->ai_family) < 0) {
			debug(session->web, "bind() %s", strerror(errno));
			close(sk);
			return -EIO;
		}
	}

	if (session->flags & SESSION_FLAG_USE_TLS) {
		debug(session->web, "using TLS encryption");
		session->transport_channel = g_io_channel_gnutls_new(sk);
	} else {
		debug(session->web, "no encryption");
		session->transport_channel = g_io_channel_unix_new(sk);
	}

	if (session->transport_channel == NULL) {
		debug(session->web, "channel missing");
		close(sk);
		return -ENOMEM;
	}

	flags = g_io_channel_get_flags(session->transport_channel);
	g_io_channel_set_flags(session->transport_channel,
					flags | G_IO_FLAG_NONBLOCK, NULL);

	g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
	g_io_channel_set_buffered(session->transport_channel, FALSE);

	g_io_channel_set_close_on_unref(session->transport_channel, TRUE);

	if (connect(sk, session->addr->ai_addr,
			session->addr->ai_addrlen) < 0) {
		if (errno != EINPROGRESS) {
			debug(session->web, "connect() %s", strerror(errno));
			close(sk);
			return -EIO;
		}
	}

	session->transport_watch = g_io_add_watch(session->transport_channel,
				G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
						received_data, session);

	session->send_watch = g_io_add_watch(session->transport_channel,
				G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
						send_data, session);

	return 0;
}
示例#29
0
int main(int argc, char **argv){
  gtk_init (&argc, &argv);

  gtk_rc_parse_string
    ("style \"panel\" {"
     "  fg[NORMAL]=\"#ffffff\""
     "}"
     "style \"topframe\" {"
     "  font_name = \"sans 10 bold\""
     "  fg[NORMAL]=\"#cccccc\""
     "}"
     "style \"rowlabel\" {"
     "  font_name = \"sans 13 \""
     "  fg[NORMAL]=\"#cccccc\""
     "}"
     "class \"*\" style \"panel\""
     "widget \"*.topframe.GtkLabel\" style \"topframe\""
     "widget \"*.topframe*.rowlabel*\" style \"rowlabel\""
     );

  /* easiest way to inform gtk of changes and not deal with locking
     issues around the UI */
  if(pipe(eventpipe)){
    fprintf(stderr,"Unable to open event pipe:\n"
            "  %s\n",strerror(errno));
    return 1;
  }

  /* Allows event compression on the read side */
  if(fcntl(eventpipe[0], F_SETFL, O_NONBLOCK)){
    fprintf(stderr,"Unable to set O_NONBLOCK on event pipe:\n"
            "  %s\n",strerror(errno));
    return 1;
  }

  /* Tell glib to watch the notificaiton pipe in gtk_main() */
  GIOChannel *channel = g_io_channel_unix_new (eventpipe[0]);
  g_io_channel_set_encoding (channel, NULL, NULL);
  g_io_channel_set_buffered (channel, FALSE);
  g_io_channel_set_close_on_unref (channel, TRUE);
  g_io_add_watch (channel, G_IO_IN, async_event_handle, NULL);
  g_io_channel_unref (channel);

  /* go */
  make_panel();

  struct sched_param sched;
  int policy,s;
  sched.sched_priority = 99;
  pthread_create(&io_thread_id,NULL,&io_thread,NULL);

  if(pthread_setschedparam(io_thread_id, SCHED_FIFO, &sched)){
    fprintf(stderr,"Unable to set realtime scheduling on io thread\n");
  }

  if(pthread_getschedparam(io_thread_id, &policy, &sched)){
    fprintf(stderr,"Unable to check realtime scheduling on io thread\n");
  }

  printf("   io thread policy=%s, priority=%d\n",
         (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
         (policy == SCHED_RR)    ? "SCHED_RR" :
         (policy == SCHED_OTHER) ? "SCHED_OTHER" :
         "???",
         sched.sched_priority);


  sched.sched_priority = 90;
  if(pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched)){
    fprintf(stderr,"Unable to set realtime scheduling on main thread\n");
  }

  if(pthread_getschedparam(pthread_self(), &policy, &sched)){
    fprintf(stderr,"Unable to check realtime scheduling on main thread\n");
  }

  printf("   main thread policy=%s, priority=%d\n",
         (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
         (policy == SCHED_RR)    ? "SCHED_RR" :
         (policy == SCHED_OTHER) ? "SCHED_OTHER" :
         "???",
         sched.sched_priority);

  gtk_main();

  return 0;
}
示例#30
0
static void open_audio(struct modem_data *modem)
{
	GIOChannel *channel;
	struct termios ti;
	int fd;

	if (modem->is_huawei == FALSE)
		return;

	if (modem->audio_users > 0)
		return;

	g_print("enabling audio\n");

	modem->dsp_out = open("/dev/dsp", O_WRONLY, 0);
	if (modem->dsp_out < 0) {
		g_printerr("Failed to open DSP device\n");
		return;
	}

	if (ioctl(modem->dsp_out, SNDCTL_DSP_SETFMT, &modem->format) < 0)
		g_printerr("Failed to set DSP format\n");

	if (ioctl(modem->dsp_out, SNDCTL_DSP_CHANNELS, &modem->channels) < 0)
		g_printerr("Failed to set DSP channels\n");

	if (ioctl(modem->dsp_out, SNDCTL_DSP_SPEED, &modem->speed) < 0)
		g_printerr("Failed to set DSP speed\n");

	modem->dsp_in = open("/dev/dsp", O_RDONLY, 0);
	if (modem->dsp_in < 0) {
		g_printerr("Failed to open DSP device\n");
		close(modem->dsp_out);
		modem->dsp_out = -1;
		return;
	}

	if (ioctl(modem->dsp_in, SNDCTL_DSP_SETFMT, &modem->format) < 0)
		g_printerr("Failed to set DSP input format\n");

	if (ioctl(modem->dsp_in, SNDCTL_DSP_CHANNELS, &modem->channels) < 0)
		g_printerr("Failed to set DSP input channels\n");

	if (ioctl(modem->dsp_in, SNDCTL_DSP_SPEED, &modem->speed) < 0)
		g_printerr("Failed to set DSP input speed\n");

	fd = open(option_device, O_RDWR | O_NOCTTY);
	if (fd < 0) {
		g_printerr("Failed to open audio port\n");
		close(modem->dsp_out);
		modem->dsp_out = -1;
		close(modem->dsp_in);
		modem->dsp_in = -1;
		return;
	}

	/* Switch TTY to raw mode */
	memset(&ti, 0, sizeof(ti));
	cfmakeraw(&ti);

	tcflush(fd, TCIOFLUSH);
	tcsetattr(fd, TCSANOW, &ti);

	channel = g_io_channel_unix_new(fd);
	if (channel == NULL) {
		g_printerr("Failed to create IO channel\n");
		close(modem->dsp_out);
		modem->dsp_out = -1;
		close(modem->dsp_in);
		modem->dsp_in = -1;
		close(fd);
		return;
	}

	g_io_channel_set_close_on_unref(channel, TRUE);

	modem->audio_watch = g_io_add_watch(channel,
				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
				audio_receive, modem);

	g_io_channel_unref(channel);

	modem->audio_users++;
}