static gboolean initable_init (GInitable *initable, GCancellable *cancellable, GError **error) { GDBusServer *server = G_DBUS_SERVER (initable); gboolean ret; guint n; gchar **addr_array; GError *last_error; ret = FALSE; addr_array = NULL; last_error = NULL; if (!g_dbus_is_guid (server->guid)) { g_set_error (&last_error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, _("The string '%s' is not a valid D-Bus GUID"), server->guid); goto out; } server->listener = G_SOCKET_LISTENER (g_threaded_socket_service_new (-1)); addr_array = g_strsplit (server->address, ";", 0); last_error = NULL; for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++) { const gchar *address_entry = addr_array[n]; GHashTable *key_value_pairs; gchar *transport_name; GError *this_error; this_error = NULL; if (g_dbus_is_supported_address (address_entry, &this_error) && _g_dbus_address_parse_entry (address_entry, &transport_name, &key_value_pairs, &this_error)) { if (FALSE) { } #ifdef G_OS_UNIX else if (g_strcmp0 (transport_name, "unix") == 0) ret = try_unix (server, address_entry, key_value_pairs, &this_error); #endif else if (g_strcmp0 (transport_name, "tcp") == 0) ret = try_tcp (server, address_entry, key_value_pairs, FALSE, &this_error); else if (g_strcmp0 (transport_name, "nonce-tcp") == 0) ret = try_tcp (server, address_entry, key_value_pairs, TRUE, &this_error); else g_set_error (&this_error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, _("Cannot listen on unsupported transport '%s'"), transport_name); g_free (transport_name); if (key_value_pairs != NULL) g_hash_table_unref (key_value_pairs); if (ret) { g_assert (this_error == NULL); goto out; } } if (this_error != NULL) { if (last_error != NULL) g_error_free (last_error); last_error = this_error; } } out: g_strfreev (addr_array); if (ret) { if (last_error != NULL) g_error_free (last_error); /* Right now we don't have any transport not using the listener... */ g_assert (server->is_using_listener); server->run_signal_handler_id = g_signal_connect (G_SOCKET_SERVICE (server->listener), "run", G_CALLBACK (on_run), server); } else { g_assert (last_error != NULL); g_propagate_error (error, last_error); } return ret; }
int get_xsockets (int *number, struct x_socket **sockets, int tcp_socket) { int dpy; struct x_socket *s; int n; int i; s = malloc (sizeof(*s) * 5); if (s == NULL) errx (1, "malloc: out of memory"); try_mkdir (X_UNIX_PATH); try_mkdir (X_PIPE_PATH); for(dpy = 4; dpy < 256; ++dpy) { char **path; int tmp = 0; n = 0; for (path = x_sockets; *path; ++path) { tmp = try_socket (&s[n], dpy, *path); if (tmp == -1) { if (errno != ENOTDIR && errno != ENOENT) err(1, "failed to open '%s'", *path); } else if (tmp == 1) { while(--n >= 0) { close (s[n].fd); free (s[n].pathname); } break; } else if (tmp == 0) ++n; } if (tmp == 1) continue; #ifdef MAY_HAVE_X11_PIPES for (path = x_pipes; *path; ++path) { tmp = try_pipe (&s[n], dpy, *path); if (tmp == -1) { if (errno != ENOTDIR && errno != ENOENT && errno != ENOSYS) err(1, "failed to open '%s'", *path); } else if (tmp == 1) { while (--n >= 0) { close (s[n].fd); free (s[n].pathname); } break; } else if (tmp == 0) ++n; } if (tmp == 1) continue; #endif if (tcp_socket) { tmp = try_tcp (&s[n], dpy); if (tmp == -1) err(1, "failed to open tcp stocket"); else if (tmp == 1) { while (--n >= 0) { close (s[n].fd); free (s[n].pathname); } break; } else if (tmp == 0) ++n; } break; } if (dpy == 256) errx (1, "no free x-servers"); for (i = 0; i < n; ++i) if (s[i].flags & LISTENP && listen (s[i].fd, SOMAXCONN) < 0) err (1, "listen %s", s[i].pathname ? s[i].pathname : "tcp"); *number = n; *sockets = s; return dpy; }