Example #1
0
static void
gspeechd_client_read_speak_data_cb (GInputStream *stream,
                       				GAsyncResult *res,
                       				gpointer user_data)
{
	GSpeechdClient *client = user_data;
	gchar *s, *response;
	gsize len;
	SsipStatus status;
	gboolean ret;
	GError *error = NULL;

	s = g_data_input_stream_read_line_finish (G_DATA_INPUT_STREAM(stream),
	                                          res,
                                              &len,
	                                          &error);
	g_printf ("--->%s\n",s);

#define END_SPEAK_DATA_STR ".\xD"
#define END_SPEAK_DATA_RESPONSE "225-1\r\n"
	if (g_strcmp0 (s, END_SPEAK_DATA_STR) != 0) {
		g_data_input_stream_read_line_async(client->input,
								G_PRIORITY_DEFAULT,
								client->cancellable,
								(GAsyncReadyCallback)gspeechd_client_read_speak_data_cb,
								gspeechd_client_ref (client));
		return;
	}

	ret = g_data_output_stream_put_string (client->output,
	                                       END_SPEAK_DATA_RESPONSE,
					                       client->cancellable,
					                       NULL);
	g_printf ("%d %s\n", ret, END_SPEAK_DATA_RESPONSE);

	status = OK_MESSAGE_QUEUED;
	response = ssip_response (status);
	ret = g_data_output_stream_put_string (client->output,
	                                       response,
					                       client->cancellable,
					                       NULL);
	g_printf ("%d %s\n", ret, response);

	/* parse commands again */
	g_data_input_stream_read_line_async(client->input,
							G_PRIORITY_DEFAULT,
							client->cancellable,
							(GAsyncReadyCallback)gspeechd_client_read_line_cb,
							gspeechd_client_ref (client));
}
Example #2
0
GSpeechdClient *
gspeechd_client_new (GSocketConnection *connection)
{
	GSpeechdClient *client;
	GOutputStream *output_stream;
	GInputStream *input_stream;

	client = g_slice_new0 (GSpeechdClient);
	client->ref_count = 1;
	client->cancellable = g_cancellable_new ();
	client->connection = g_object_ref (connection);
	client->failed = FALSE;

	/*
	 * Start listening for the message
	 */
	input_stream = g_io_stream_get_input_stream(G_IO_STREAM(connection));
	client->input = g_data_input_stream_new (input_stream);

	g_data_input_stream_read_line_async(client->input,
							G_PRIORITY_DEFAULT,
							client->cancellable,
							(GAsyncReadyCallback)gspeechd_client_read_line_cb,
							gspeechd_client_ref (client));

	output_stream = g_io_stream_get_output_stream (G_IO_STREAM(connection));
	client->output = g_data_output_stream_new (output_stream);

	return (client);
}
Example #3
0
static void
_j4status_io_stream_set_connection(J4statusIOStream *self, GSocketConnection *connection)
{
    self->connection = connection;
    self->out = g_data_output_stream_new(g_io_stream_get_output_stream(G_IO_STREAM(self->connection)));
    self->in = g_data_input_stream_new(g_io_stream_get_input_stream(G_IO_STREAM(self->connection)));
    if ( ! self->header_sent )
        _j4status_io_stream_put_header(self, self->io->header);
    g_data_input_stream_read_line_async(self->in, G_PRIORITY_DEFAULT, NULL, _j4status_io_stream_read_callback, self);
}
int main(void)
{
    pid_t parent_pid;
    GInputStream *stdin_unix_stream;

  /* Nuke the environment to get a well-known and sanitized
   * environment to avoid attacks via e.g. the DBUS_SYSTEM_BUS_ADDRESS
   * environment variable and similar.
   */
    if (clearenv () != 0) {
        FATAL_ERROR("Error clearing environment: %s\n", g_strerror (errno));
        return 1;
    }

#if !GLIB_CHECK_VERSION(2,36,0)
    g_type_init();
#endif

    loop = g_main_loop_new(NULL, FALSE);

    authority = polkit_authority_get_sync(NULL, NULL);
    parent_pid = getppid ();
    if (parent_pid == 1) {
            FATAL_ERROR("Parent process was reaped by init(1)\n");
            return 1;
    }
    /* Do what pkexec does */
    subject = polkit_unix_process_new_for_owner(parent_pid, 0, getuid ());

    stdin_unix_stream = g_unix_input_stream_new(STDIN_FILENO, 0);
    stdin_stream = g_data_input_stream_new(stdin_unix_stream);
    g_data_input_stream_set_newline_type(stdin_stream,
                                         G_DATA_STREAM_NEWLINE_TYPE_LF);
    g_clear_object(&stdin_unix_stream);
    g_data_input_stream_read_line_async(stdin_stream, G_PRIORITY_DEFAULT, NULL,
                                        stdin_read_complete, NULL);

    g_main_loop_run(loop);

    if (polkit_cancellable)
        g_clear_object(&polkit_cancellable);
    g_object_unref(stdin_stream);
    g_object_unref(authority);
    g_object_unref(subject);
    g_main_loop_unref(loop);

    return exit_status;
}
Example #5
0
void ControllerLIRC::line_read( GObject * stream , GAsyncResult * result )
{
    GError * error = 0;

    char * line = g_data_input_stream_read_line_finish( G_DATA_INPUT_STREAM( stream ) , result , NULL , & error );

    if ( error )
    {
        tplog( "READ ERROR : %s" , error->message );
        g_clear_error( & error );
        g_object_unref( connection );
        connection = 0;
        return;
    }

    if ( line )
    {
        // Split it into 4 pieces

        gchar * * parts = g_strsplit( line , " " , 4 );

        if ( g_strv_length( parts ) >= 3 )
        {
            KeyMap::const_iterator it = key_map.find( parts[ 2 ] );

            if ( it != key_map.end() )
            {
                if ( g_timer_elapsed( timer , NULL ) >= repeat )
                {
                    tp_controller_key_down( controller , it->second , 0 , TP_CONTROLLER_MODIFIER_NONE );
                    tp_controller_key_up( controller , it->second , 0 , TP_CONTROLLER_MODIFIER_NONE );

                    g_timer_start( timer );
                }
            }
        }

        g_strfreev(parts);

        g_free( line );
    }

    g_data_input_stream_read_line_async( G_DATA_INPUT_STREAM( stream ) , 0 , NULL , line_read , this );
}
Example #6
0
static gboolean
identd_incoming_cb (GSocketService *service, GSocketConnection *conn,
					GObject *source, gpointer userdata)
{
	GDataInputStream *data_stream;
	GInputStream *stream;
	ident_info *info;

	info = g_new0 (ident_info, 1);

	info->conn = conn;
	g_object_ref (conn);

	stream = g_io_stream_get_input_stream (G_IO_STREAM (conn));
	data_stream = g_data_input_stream_new (stream);
	g_data_input_stream_set_newline_type (data_stream, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
	g_data_input_stream_read_line_async (data_stream, G_PRIORITY_DEFAULT,
										NULL, (GAsyncReadyCallback)identd_read_ready, info);

	return TRUE;
}
Example #7
0
static void
_j4status_io_stream_read_callback(GObject *stream, GAsyncResult *res, gpointer user_data)
{
    J4statusIOStream *self = user_data;
    GError *error = NULL;

    gchar *line;
    line = g_data_input_stream_read_line_finish(self->in, res, NULL, &error);
    if ( line == NULL )
    {
        if ( error != NULL )
        {
            g_warning("Input error: %s", error->message);
            g_clear_error(&error);
        }
        _j4status_io_stream_reconnect_maybe(self);
        return;
    }

    j4status_core_action(self->io->core, line);

    g_free(line);
    g_data_input_stream_read_line_async(self->in, G_PRIORITY_DEFAULT, NULL, _j4status_io_stream_read_callback, self);
}
Example #8
0
static gboolean
test_client_do (TestClient *client,
                GError    **error,
                ...)
{
  GString *command = g_string_new (NULL);
  char *line = NULL;

  va_list vap;
  va_start (vap, error);

  while (TRUE)
    {
      char *word = va_arg (vap, char *);
      if (word == NULL)
        break;

      if (command->len > 0)
        g_string_append_c (command, ' ');

      char *quoted = g_shell_quote (word);
      g_string_append (command, quoted);
      g_free (quoted);
    }

  va_end (vap);

  g_string_append_c (command, '\n');

  if (!g_data_output_stream_put_string (client->in, command->str,
                                        client->cancellable, error))
    goto out;

  g_data_input_stream_read_line_async (client->out,
                                       G_PRIORITY_DEFAULT,
                                       client->cancellable,
                                       test_client_line_read,
                                       client);

  client->error = error;
  g_main_loop_run (client->loop);
  line = client->line;
  client->line = NULL;
  client->error = NULL;

  if (!line)
    {
      if (*error == NULL)
        g_set_error (error, TEST_RUNNER_ERROR, TEST_RUNNER_ERROR_RUNTIME_ERROR,
                     "test client exited");
      goto out;
    }

  if (strcmp (line, "OK") != 0)
    {
      g_set_error (error, TEST_RUNNER_ERROR, TEST_RUNNER_ERROR_RUNTIME_ERROR,
                   "%s", line);
      goto out;
    }

 out:
  g_string_free (command, TRUE);
  g_free (line);

  return *error == NULL;
}
static void stdin_read_complete(GObject *src, GAsyncResult *res, gpointer data)
{
    char *s, *ep;
    GError *err = NULL;
    gsize len;

    s = g_data_input_stream_read_line_finish(G_DATA_INPUT_STREAM(src), res,
                                             &len, &err);
    if (!s) {
        if (err) {
            FATAL_ERROR("Reading from stdin: %s\n", err->message);
            g_error_free(err);
            return;
        }

        switch (state) {
        case STATE_WAITING_FOR_BUS_N_DEV:
            FATAL_ERROR("EOF while waiting for bus and device num\n");
            break;
        case STATE_WAITING_FOR_POL_KIT:
            ERROR("Cancelled while waiting for authorization\n");
            break;
        case STATE_WAITING_FOR_STDIN_EOF:
            cleanup();
            break;
        }
        return;
    }

    switch (state) {
    case STATE_WAITING_FOR_BUS_N_DEV:
        busnum = strtol(s, &ep, 10);
        if (!isspace(*ep)) {
            FATAL_ERROR("Invalid busnum / devnum: %s\n", s);
            break;
        }
        devnum = strtol(ep, &ep, 10);
        if (*ep != '\0') {
            FATAL_ERROR("Invalid busnum / devnum: %s\n", s);
            break;
        }

        /*
         * The set_facl() call is a no-op for root, so no need to ask PolKit
         * and then if ok call set_facl(), when called by a root process.
         */
        if (getuid() != 0) {
            polkit_cancellable = g_cancellable_new();
            polkit_authority_check_authorization(
                authority, subject, "org.spice-space.lowlevelusbaccess", NULL,
                POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
                polkit_cancellable,
                (GAsyncReadyCallback)check_authorization_cb, NULL);
            state = STATE_WAITING_FOR_POL_KIT;
        } else {
            fprintf(stdout, "SUCCESS\n");
            fflush(stdout);
            state = STATE_WAITING_FOR_STDIN_EOF;
        }

        g_data_input_stream_read_line_async(stdin_stream, G_PRIORITY_DEFAULT,
                                            NULL, stdin_read_complete, NULL);
        break;
    default:
        FATAL_ERROR("Unexpected extra input in state %u: %s\n", state, s);
    }
    g_free(s);
}
Example #10
0
static void
_j4status_io_add_stream(J4statusIOContext *self, const gchar *stream_desc)
{
    J4statusIOStream *stream;
    if ( g_strcmp0(stream_desc, "std") == 0 )
    {
        GOutputStream *out;
        GInputStream *in;
#ifdef G_OS_UNIX
        out = g_unix_output_stream_new(1, FALSE);
        in = g_unix_input_stream_new(0, FALSE);
#else /* ! G_OS_UNIX */
        return;
#endif /* ! G_OS_UNIX */

        stream = _j4status_io_stream_new(self);

        stream->out = g_data_output_stream_new(out);
        stream->in = g_data_input_stream_new(in);
        g_object_unref(out);
        g_object_unref(in);

        _j4status_io_stream_put_header(stream, self->header);
        g_data_input_stream_read_line_async(stream->in, G_PRIORITY_DEFAULT, NULL, _j4status_io_stream_read_callback, stream);

        goto end;
    }

    GSocketAddress *address = NULL;

    if ( g_str_has_prefix(stream_desc, "tcp:") )
    {
        const gchar *uri = stream_desc + strlen("tcp:");
        gchar *port_str = g_utf8_strrchr(uri, -1, ':');
        if ( port_str == NULL )
            /* No port, illegal stream description */
            return;

        *port_str = '\0';
        ++port_str;

        guint64 port;
        port = g_ascii_strtoull(port_str, NULL, 10);
        if ( port > 65535 )
            return;

        GInetAddress *inet_address;
        inet_address = g_inet_address_new_from_string(uri);

        address = g_inet_socket_address_new(inet_address, port);
    }

#ifdef G_OS_UNIX
    if ( g_str_has_prefix(stream_desc, "unix:") )
    {
        const gchar *path = stream_desc + strlen("unix:");

        address = g_unix_socket_address_new(path);
    }
#endif /* G_OS_UNIX */

    if ( address == NULL )
        return;

    stream = _j4status_io_stream_new(self);
    stream->address = address;

    _j4status_io_stream_connect(stream);

end:
    self->streams = g_list_prepend(self->streams, stream);
}
Example #11
0
static void
gspeechd_client_read_line_cb (GInputStream *stream,
                       GAsyncResult *res,
                       gpointer user_data)
{
	GSpeechdClient *client = user_data;
	gchar *s, *response;
	gsize len;
	gboolean ret;
	GError *error = NULL;
	SsipCommand *cmd;
	SsipStatus status;

	/* read lines and removes newline */
	s = g_data_input_stream_read_line_finish (G_DATA_INPUT_STREAM(stream),
	                                          res,
                                              &len,
	                                          &error);
	if (error != NULL ) {
		g_printf ("-->%s\n", error->message);
		g_error_free (error);
		return;
	}

	if (s == NULL) {
		/* client has closed a connection */
		g_printf ("-->connection closed\n");
		return; 
	}

	g_printf ("-->%s\n", s);

	/* parse SSIP command */
	cmd = ssip_command_new (s);
	/* TODO remove history */
	client->history = g_list_append (client->history, s);

	/* process command */
	switch (ssip_cmd_get (cmd)) {
		case SSIP_CMD_SET:
			switch (ssip_set_param_get (cmd)) {
				case SSIP_SET_PARAM_CLIENT_NAME:
					/* TODO free previous name before */
					client->name = g_strdup (ssip_set_param_value_get (cmd));
					/* TODO emit event to the server */
					status = OK_CLIENT_NAME_SET;
					break;
				case SSIP_SET_PARAM_PRIORITY:
					status = OK_PRIORITY_SET;
					break;
				default:
					break;
			}
			break;
		case SSIP_CMD_SPEAK:
			status = OK_RECEIVE_DATA;
			response = ssip_response (status);
			ret = g_data_output_stream_put_string (client->output,
	        		                               response,
							                       client->cancellable,
					        		               NULL);
			g_printf ("%d %s\n", ret, response);
			g_data_input_stream_read_line_async(client->input,
									G_PRIORITY_DEFAULT,
									client->cancellable,
									(GAsyncReadyCallback)gspeechd_client_read_speak_data_cb,
									gspeechd_client_ref (client));
			return;
		case SSIP_CMD_HELP:
		default:
			break;
	}

	response = ssip_response (status);
	ret = g_data_output_stream_put_string (client->output,
	                                       response,
					                       client->cancellable,
					                       NULL);
	g_printf ("%d %s\n", ret, response);

	/* when QUIT received, send an event connection_closed */

	g_data_input_stream_read_line_async(client->input,
							G_PRIORITY_DEFAULT,
							client->cancellable,
							(GAsyncReadyCallback)gspeechd_client_read_line_cb,
							gspeechd_client_ref (client));
}
Example #12
0
ControllerLIRC::ControllerLIRC( TPContext * context , const char * uds , guint _repeat )
:
    connection( 0 ),
    controller( 0 ),
    timer( 0 ),
    repeat( gdouble( _repeat ) / 1000.0 )
{
    g_assert( context );
    g_assert( uds );

    //.........................................................................
    // Get the address of the Unix Domain Socket

    GSocketAddress * socket_address = g_unix_socket_address_new( uds );

    if ( ! socket_address )
    {
        tpwarn( "FAILED TO CREATE SOCKET ADDRESS WITH '%d'" , uds );
        return;
    }

    //.........................................................................
    // Create a socket client and attempt to connect to the address

    GSocketClient * client = g_socket_client_new();

    connection = g_socket_client_connect( client , G_SOCKET_CONNECTABLE( socket_address ) , NULL , NULL );

    g_object_unref( socket_address );

    g_object_unref( client );

    if ( ! connection )
    {
        tplog( "FAILED TO CONNECT TO LIRC SOCKET" );
        return;
    }

    //.........................................................................
    // Now, get the socket's input stream, create a data input stream from it
    // and start reading lines.

    GDataInputStream * input_stream = g_data_input_stream_new( g_io_stream_get_input_stream( G_IO_STREAM( connection ) ) );

    g_assert( input_stream );

    g_data_input_stream_read_line_async( input_stream , 0 , NULL , line_read , this );

    g_object_unref( input_stream );

    //.........................................................................
    // Add the controller

    TPControllerSpec controller_spec;

    memset( & controller_spec , 0 , sizeof( controller_spec ) );

    controller_spec.capabilities = TP_CONTROLLER_HAS_KEYS;

    controller = tp_context_add_controller( context , "Remote" , & controller_spec , 0 );

    g_assert( controller );

    //.........................................................................
    // Populate the key map

    key_map[ "0"         ] = TP_KEY_0;
    key_map[ "1"         ] = TP_KEY_1;
    key_map[ "2"         ] = TP_KEY_2;
    key_map[ "3"         ] = TP_KEY_3;
    key_map[ "4"         ] = TP_KEY_4;
    key_map[ "5"         ] = TP_KEY_5;
    key_map[ "6"         ] = TP_KEY_6;
    key_map[ "7"         ] = TP_KEY_7;
    key_map[ "8"         ] = TP_KEY_8;
    key_map[ "9"         ] = TP_KEY_9;
    key_map[ "MUTE"      ] = TP_KEY_MUTE;
    key_map[ "CH_UP"     ] = TP_KEY_CHAN_UP;
    key_map[ "VOL_UP"    ] = TP_KEY_VOL_UP;
    key_map[ "CH_DOWN"   ] = TP_KEY_CHAN_DOWN;
    key_map[ "VOL_DOWN"  ] = TP_KEY_VOL_DOWN;
    key_map[ "UP"        ] = TP_KEY_UP;
    key_map[ "LEFT"      ] = TP_KEY_LEFT;
    key_map[ "OK"        ] = TP_KEY_OK;
    key_map[ "RIGHT"     ] = TP_KEY_RIGHT;
    key_map[ "DOWN"      ] = TP_KEY_DOWN;
    key_map[ "MENU"      ] = TP_KEY_MENU;
    key_map[ "EXIT"      ] = TP_KEY_EXIT;
    key_map[ "PLAY"      ] = TP_KEY_PLAY;
    key_map[ "PAUSE"     ] = TP_KEY_PAUSE;
    key_map[ "STOP"      ] = TP_KEY_STOP;
    key_map[ "|<<"       ] = TP_KEY_PREV;
    key_map[ ">>|"       ] = TP_KEY_NEXT;
    key_map[ "RECORD"    ] = TP_KEY_REC;
    key_map[ "<<"        ] = TP_KEY_REW;
    key_map[ ">>"        ] = TP_KEY_FFWD;
    key_map[ "RED"       ] = TP_KEY_RED;
    key_map[ "GREEN"     ] = TP_KEY_GREEN;
    key_map[ "YELLOW"    ] = TP_KEY_YELLOW;
    key_map[ "BLUE"      ] = TP_KEY_BLUE;
    key_map[ "BACK"      ] = TP_KEY_BACK;

    timer = g_timer_new();

    tplog( "READY" );
}