gint main (gint argc, gchar *argv[]) { g_autoptr(GDBusConnection) connection = NULL; g_autoptr(IpcGitService) service = NULL; g_autoptr(GOutputStream) stdout_stream = NULL; g_autoptr(GInputStream) stdin_stream = NULL; g_autoptr(GIOStream) stream = NULL; g_autoptr(GMainLoop) main_loop = NULL; g_autoptr(GError) error = NULL; g_set_prgname ("gnome-builder-git"); g_set_application_name ("gnome-builder-git"); prctl (PR_SET_PDEATHSIG, SIGTERM); signal (SIGPIPE, SIG_IGN); ggit_init (); g_log_set_handler (NULL, G_LOG_LEVEL_MASK, log_func, NULL); if (!g_unix_set_fd_nonblocking (STDIN_FILENO, TRUE, &error) || !g_unix_set_fd_nonblocking (STDOUT_FILENO, TRUE, &error)) goto error; main_loop = g_main_loop_new (NULL, FALSE); stdin_stream = g_unix_input_stream_new (STDIN_FILENO, FALSE); stdout_stream = g_unix_output_stream_new (STDOUT_FILENO, FALSE); stream = g_simple_io_stream_new (stdin_stream, stdout_stream); if (!(connection = create_connection (stream, main_loop, &error))) goto error; service = ipc_git_service_impl_new (); if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (service), connection, "/org/gnome/Builder/Git", &error)) goto error; g_dbus_connection_start_message_processing (connection); g_main_loop_run (main_loop); return EXIT_SUCCESS; error: if (error != NULL) g_printerr ("%s\n", error->message); return EXIT_FAILURE; }
static gboolean emit_new_connection_in_idle (gpointer user_data) { EmitIdleData *data = user_data; gboolean claimed; claimed = FALSE; g_signal_emit (data->server, _signals[NEW_CONNECTION_SIGNAL], 0, data->connection, &claimed); if (claimed) g_dbus_connection_start_message_processing (data->connection); g_object_unref (data->connection); return FALSE; }
/* Called in new thread */ static gboolean on_run (GSocketService *service, GSocketConnection *socket_connection, GObject *source_object, gpointer user_data) { GDBusServer *server = G_DBUS_SERVER (user_data); GDBusConnection *connection; GDBusConnectionFlags connection_flags; if (server->nonce != NULL) { gchar buf[16]; gsize bytes_read; if (!g_input_stream_read_all (g_io_stream_get_input_stream (G_IO_STREAM (socket_connection)), buf, 16, &bytes_read, NULL, /* GCancellable */ NULL)) /* GError */ goto out; if (bytes_read != 16) goto out; if (memcmp (buf, server->nonce, 16) != 0) goto out; } connection_flags = G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER | G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING; if (server->flags & G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS) connection_flags |= G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS; connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection), server->guid, connection_flags, server->authentication_observer, NULL, /* GCancellable */ NULL); /* GError */ if (connection == NULL) goto out; if (server->flags & G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) { gboolean claimed; claimed = FALSE; g_signal_emit (server, _signals[NEW_CONNECTION_SIGNAL], 0, connection, &claimed); if (claimed) g_dbus_connection_start_message_processing (connection); g_object_unref (connection); } else { GSource *idle_source; EmitIdleData *data; data = g_new0 (EmitIdleData, 1); data->server = g_object_ref (server); data->connection = g_object_ref (connection); idle_source = g_idle_source_new (); g_source_set_priority (idle_source, G_PRIORITY_DEFAULT); g_source_set_callback (idle_source, emit_new_connection_in_idle, data, (GDestroyNotify) emit_idle_data_free); g_source_attach (idle_source, server->main_context_at_construction); g_source_unref (idle_source); } out: return TRUE; }