static void on_name_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) { already_acquired = TRUE; #ifdef HAVE_FUSE gboolean no_fuse = GPOINTER_TO_UINT (user_data); if (!no_fuse) { char *fuse_path; char *argv2[6]; /* Use the old .gvfs location as fallback, not .cache/gvfs */ if (g_get_user_runtime_dir() == g_get_user_cache_dir ()) fuse_path = g_build_filename (g_get_home_dir(), ".gvfs", NULL); else fuse_path = g_build_filename (g_get_user_runtime_dir (), "gvfs", NULL); if (!g_file_test (fuse_path, G_FILE_TEST_EXISTS)) g_mkdir (fuse_path, 0700); /* The -f (foreground) option prevent libfuse to call daemon(). */ /* First, this is not required as g_spawn_async() already */ /* detach the process. Secondly, calling daemon() and then */ /* pthread_create() produce an undefined result accoring to */ /* Opengroup. On system with the uClibc library this will badly */ /* hang the process. */ argv2[0] = LIBEXEC_DIR "/gvfsd-fuse"; argv2[1] = fuse_path; argv2[2] = "-f"; argv2[3] = "-o"; argv2[4] = "big_writes"; argv2[5] = NULL; g_spawn_async (NULL, argv2, NULL, G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, NULL, NULL); g_free (fuse_path); } #endif }
void socket_listen(char *suffix) { GSocketService *sock; GSocketAddress *sa; GError *err = NULL; char *name, *path; name = g_strdup_printf("%s-%s", __NAME__, suffix); path = g_build_filename(g_get_user_runtime_dir(), name, NULL); g_free(name); unlink(path); sa = g_unix_socket_address_new(path); g_free(path); sock = g_threaded_socket_service_new(-1); if (!g_socket_listener_add_address(G_SOCKET_LISTENER(sock), sa, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL, NULL, &err)) { fprintf(stderr, "Failed to set up socket: '%s'\n", err->message); exit(EXIT_FAILURE); } g_signal_connect(G_OBJECT(sock), "run", G_CALLBACK(sock_incoming), NULL); g_socket_service_start(sock); }
void cooperation_setup(void) { GIOChannel *towatch; gchar *fifofilename, *fifopath; fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix); fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL); g_free(fifofilename); if (!g_file_test(fifopath, G_FILE_TEST_EXISTS)) mkfifo(fifopath, 0600); cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK); if (!cooperative_pipe_fp) { fprintf(stderr, __NAME__": Can't open FIFO at all.\n"); } else { if (write(cooperative_pipe_fp, "", 0) == -1) { /* Could not do an empty write to the FIFO which means there's * no one listening. */ close(cooperative_pipe_fp); towatch = g_io_channel_new_file(fifopath, "r+", NULL); g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL); } else cooperative_alone = FALSE; } g_free(fifopath); }
int main(int argc, char *argv[]) { int r = 99; eventd_tests_env_setup(argv, "ws-connection"); EventdTestsEnv *env = eventd_tests_env_new(NULL, NULL, FALSE); if ( ! eventd_tests_env_start_eventd(env) ) goto end; gchar *file; gchar *port; gsize length; file = g_build_filename(g_get_user_runtime_dir(), PACKAGE_NAME, EVP_UNIX_SOCKET, NULL); if ( g_file_test(file, G_FILE_TEST_IS_REGULAR) && g_file_get_contents(file, &port, &length, NULL) && ( length < 6 ) ) { gchar uri[21]; g_snprintf(uri, sizeof(uri), "ws://localhost:%s", port); r = eventd_tests_run_libeventc(uri); } g_free(file); if ( ! eventd_tests_env_stop_eventd(env) ) r = 99; end: eventd_tests_env_free(env); return r; }
GumdDbusServerP2P * gumd_dbus_server_p2p_new () { /* In case of session bus, privileges are dropped in the daemon start phase * as it is not needed. Besides in order for session bus to work, effective * uid/gid should be same as real uid/gid as 'set-user bit on execution(s)' * is set on the daemon binary. * * In case of system bus, privileges are dropped when bus name is acquired * rather than at the daemon start phase as dbus does not allow daemon to * connect if the privileges are dropped earlier. * * In case of p2p, privileges are dropped in the daemon start phase as it is * not needed. * * In all cases once connected, privileges are gained and dropped on * need basis * */ gum_utils_drop_privileges (); gchar *address = g_strdup_printf (GUM_DBUS_ADDRESS, g_get_user_runtime_dir()); GumdDbusServerP2P *server = gumd_dbus_server_p2p_new_with_address ( address); g_free (address); return server; }
gboolean gspeechd_options_parse (int argc, char * argv[]) { GOptionContext *context; GError *error = NULL; gboolean parsed; server_parameters = g_array_new (FALSE, TRUE, sizeof (GParameter)); context = g_option_context_new (" - common interface for speech synthesis"); // g_option_context_set_summary (context, "gspeechd is a device independent layer for speech synthesis that provides a common easy to use interface for both client applications (programs that want to speak) and for software synthesizers (programs actually able to convert text to speech)."); g_option_context_add_main_entries (context, options, argv[0]); parsed = g_option_context_parse (context, &argc, &argv, &error); g_option_context_free (context); /* check consistency now */ if (parsed) { if (cmd_options.log_level < 1 || cmd_options.log_level > 5 ) { cmd_options.log_level = cmd_options.log_level; } if (g_strcmp0 (GSPEECHD_DEFAULT_OPTION_PID_FILE, cmd_options.pid_file) == 0) { cmd_options.pid_file = g_build_filename (g_get_user_runtime_dir(), "speech-dispatcher", "pid", "speechd.pid", NULL); } } /* TRUE if the parsing was successful */ return parsed; }
static gchar * dconf_shm_get_shmdir (void) { static gchar *shmdir; if (g_once_init_enter (&shmdir)) g_once_init_leave (&shmdir, g_build_filename (g_get_user_runtime_dir (), "dconf", NULL)); return shmdir; }
static gboolean init(LXTermWindow* lxtermwin, gint argc, gchar** argv) { /* Normally, LXTerminal uses one process to control all of its windows. * The first process to start will create a Unix domain socket in * g_get_user_runtime_dir(). It will then bind and listen on this socket. * The subsequent processes will connect to the controller that owns the * Unix domain socket. They will pass their command line over the socket * and exit. * * If for any reason both the connect and bind fail, we will fall back to * having that process be standalone; it will not be either the controller * or a user of the controller. * * This function returns TRUE if this process should keep running and FALSE * if it should exit. */ /* Formulate the path for the Unix domain socket. */ #if GLIB_CHECK_VERSION (2, 28, 0) gchar * socket_path = g_strdup_printf("%s/.lxterminal-socket-%s", g_get_user_runtime_dir(), gdk_display_get_name(gdk_display_get_default())); #else gchar * socket_path = g_strdup_printf("%s/.lxterminal-socket-%s", g_get_user_cache_dir(), gdk_display_get_name(gdk_display_get_default())); #endif /* Create socket. */ int fd = socket(PF_UNIX, SOCK_STREAM, 0); if (fd < 0) { g_warning("Socket create failed: %s\n", g_strerror(errno)); goto err_socket; } /* Initialize socket address for Unix domain socket. */ struct sockaddr_un sock_addr; memset(&sock_addr, 0, sizeof(sock_addr)); sock_addr.sun_family = AF_UNIX; snprintf(sock_addr.sun_path, sizeof(sock_addr.sun_path), "%s", socket_path); /* Try to connect to an existing LXTerminal process. */ if (connect(fd, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) == 0) { g_free(socket_path); send_msg_to_controller(fd, argc, argv); return FALSE; } unlink(socket_path); g_free(socket_path); start_controller(&sock_addr, lxtermwin, fd); return TRUE; err_socket: g_free(socket_path); return TRUE; }
static GPid start_ssh_agent (void) { GError *error = NULL; GSpawnFlags flags; GPid pid = 0; gint fd = -1; gchar *bind_address = g_strdup_printf ("%s/ssh-agent.XXXXXX", g_get_user_runtime_dir ()); gchar *agent_argv[] = { "ssh-agent", "-a", bind_address, NULL }; fd = g_mkstemp (bind_address); if (fd < 0) { g_warning ("couldn't create temporary socket file: %s", g_strerror (errno)); goto out; } if (g_unlink (bind_address) < 0) { g_warning ("couldn't remove temporary socket file: %s", g_strerror (errno)); goto out; } flags = G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL; g_spawn_async (NULL, agent_argv, NULL, flags, setup_daemon, GINT_TO_POINTER (-1), &pid, &error); if (error != NULL) { if (g_error_matches (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT)) g_debug ("couldn't start %s: %s", agent_argv[0], error->message); else g_warning ("couldn't start %s: %s", agent_argv[0], error->message); pid = 0; goto out; } g_debug ("launched %s", agent_argv[0]); g_setenv ("SSH_AUTH_SOCK", bind_address, TRUE); out: g_clear_error (&error); if (fd >= 0) close (fd); g_free (bind_address); return pid; }
static GstMemory * gst_wl_shm_allocator_alloc (GstAllocator * allocator, gsize size, GstAllocationParams * params) { GstWlShmAllocator *self = GST_WL_SHM_ALLOCATOR (allocator); char filename[1024]; static int init = 0; int fd; GstMemory *mem; GstMapInfo info; /* TODO: make use of the allocation params, if necessary */ /* allocate shm pool */ snprintf (filename, 1024, "%s/%s-%d-%s", g_get_user_runtime_dir (), "wayland-shm", init++, "XXXXXX"); fd = g_mkstemp (filename); if (fd < 0) { GST_ERROR_OBJECT (self, "opening temp file %s failed: %s", filename, strerror (errno)); return NULL; } if (ftruncate (fd, size) < 0) { GST_ERROR_OBJECT (self, "ftruncate failed: %s", strerror (errno)); close (fd); return NULL; } mem = gst_fd_allocator_alloc (allocator, fd, size, GST_FD_MEMORY_FLAG_KEEP_MAPPED); if (G_UNLIKELY (!mem)) { GST_ERROR_OBJECT (self, "GstFdMemory allocation failed"); close (fd); return NULL; } /* we need to map the memory in order to unlink the file without losing it */ if (!gst_memory_map (mem, &info, GST_MAP_READWRITE)) { GST_ERROR_OBJECT (self, "GstFdMemory map failed"); close (fd); return NULL; } /* unmap will not really munmap(), we just * need it to release the miniobject lock */ gst_memory_unmap (mem, &info); unlink (filename); return mem; }
gboolean io_init_socket(const char *name) { char *dir, *path; int sock; struct sockaddr_un local; /* create socket in runtime directory */ dir = g_build_filename(g_get_user_runtime_dir(), PROJECT, "socket", NULL); util_create_dir_if_not_exists(dir); path = g_build_filename(dir, name, NULL); g_free(dir); /* if the file already exists - remove this first */ if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) { unlink(path); } sock = socket(AF_UNIX, SOCK_STREAM, 0); if (sock < 0) { g_warning("Can't create socket %s", path); } /* prepare socket address */ local.sun_family = AF_UNIX; strcpy(local.sun_path, path); if (bind(sock, (struct sockaddr*)&local, sizeof(local)) != -1 && listen(sock, 5) >= 0 ) { GIOChannel *chan = g_io_channel_unix_new(sock); if (chan) { g_io_channel_set_encoding(chan, NULL, NULL); g_io_channel_set_buffered(chan, false); g_io_add_watch(chan, G_IO_IN|G_IO_HUP, (GIOFunc)socket_accept, chan); /* don't free path - because we want to keep the value in * vb.state.socket_path still accessible */ vb.state.socket_path = path; g_setenv("VIMB_SOCKET", path, true); return true; } } else { g_warning("no bind"); } g_warning("Could not listen on %s: %s", path, strerror(errno)); g_free(path); return false; }
static gboolean gst_wayland_buffer_pool_start (GstBufferPool * pool) { GstWaylandBufferPool *self = GST_WAYLAND_BUFFER_POOL (pool); guint size = 0; int fd; char filename[1024]; static int init = 0; GST_DEBUG_OBJECT (self, "Initializing wayland buffer pool"); /* configure */ size = GST_VIDEO_INFO_SIZE (&self->info) * 15; /* allocate shm pool */ snprintf (filename, 1024, "%s/%s-%d-%s", g_get_user_runtime_dir (), "wayland-shm", init++, "XXXXXX"); fd = mkstemp (filename); if (fd < 0) { GST_ERROR_OBJECT (pool, "opening temp file %s failed: %s", filename, strerror (errno)); return FALSE; } if (ftruncate (fd, size) < 0) { GST_ERROR_OBJECT (pool, "ftruncate failed: %s", strerror (errno)); close (fd); return FALSE; } self->data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (self->data == MAP_FAILED) { GST_ERROR_OBJECT (pool, "mmap failed: %s", strerror (errno)); close (fd); return FALSE; } self->wl_pool = wl_shm_create_pool (self->display->shm, fd, size); unlink (filename); close (fd); self->size = size; self->used = 0; return GST_BUFFER_POOL_CLASS (parent_class)->start (pool); }
static GstMemory * gst_wl_shm_allocator_alloc (GstAllocator * allocator, gsize size, GstAllocationParams * params) { GstWlShmAllocator *self = GST_WL_SHM_ALLOCATOR (allocator); char filename[1024]; static int init = 0; int fd; gpointer data; GstWlShmMemory *mem; /* TODO: make use of the allocation params, if necessary */ /* allocate shm pool */ snprintf (filename, 1024, "%s/%s-%d-%s", g_get_user_runtime_dir (), "wayland-shm", init++, "XXXXXX"); fd = g_mkstemp (filename); if (fd < 0) { GST_ERROR_OBJECT (self, "opening temp file %s failed: %s", filename, strerror (errno)); return NULL; } if (ftruncate (fd, size) < 0) { GST_ERROR_OBJECT (self, "ftruncate failed: %s", strerror (errno)); close (fd); return NULL; } data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (data == MAP_FAILED) { GST_ERROR_OBJECT (self, "mmap failed: %s", strerror (errno)); close (fd); return NULL; } unlink (filename); mem = g_slice_new0 (GstWlShmMemory); gst_memory_init ((GstMemory *) mem, GST_MEMORY_FLAG_NO_SHARE, allocator, NULL, size, 0, 0, size); mem->data = data; mem->fd = fd; return (GstMemory *) mem; }
/** * ide_is_flatpak: * * This function checks to see if the application is running within * a flatpak. This might be useful for cases where you need to perform * a different command when you are in the bundled flatpak version. */ gboolean ide_is_flatpak (void) { static gboolean checked; static gboolean is_flatpak; if (!checked) { g_autofree gchar *path = NULL; path = g_build_filename (g_get_user_runtime_dir (), "flatpak-info", NULL); is_flatpak = g_file_test (path, G_FILE_TEST_EXISTS); checked = TRUE; } return is_flatpak; }
static FILE * create_auth_file (char **filename) { char *auth_dir = NULL; char *auth_file = NULL; int fd; FILE *fp = NULL; auth_dir = g_build_filename (g_get_user_runtime_dir (), "gdm", NULL); g_mkdir_with_parents (auth_dir, 0711); auth_file = g_build_filename (auth_dir, "Xauthority", NULL); g_clear_pointer (&auth_dir, g_free); fd = g_open (auth_file, O_RDWR | O_CREAT | O_TRUNC, 0700); if (fd < 0) { g_debug ("could not open %s to store auth cookie: %m", auth_file); g_clear_pointer (&auth_file, g_free); goto out; } fp = fdopen (fd, "w+"); if (fp == NULL) { g_debug ("could not set up stream for auth cookie file: %m"); g_clear_pointer (&auth_file, g_free); close (fd); goto out; } *filename = auth_file; out: return fp; }
static void got_permission_handle (GObject *object, GAsyncResult *result, gpointer user_data) { GDBusConnection *connection = G_DBUS_CONNECTION (object); g_autofree ContentChooserData *data = user_data; g_autoptr(GVariant) res = NULL; g_autoptr(GError) error = NULL; g_autofree char *basename = data->basename; g_autofree char *path = g_strdup_printf ("%s/doc/%x/%s", g_get_user_runtime_dir(), data->handle, basename); res = g_dbus_connection_call_finish (connection, result, &error); if (res == NULL) g_dbus_method_invocation_return_error (data->invocation, XDP_ERROR, XDP_ERROR_FAILED, "Granting permissions failed: %s", error->message); else g_dbus_method_invocation_return_value (data->invocation, g_variant_new ("(^ay)", path)); }
/* * Return the address of XDG_RUNTIME_DIR/bus if it exists, belongs to * us, and is a socket, and we are on Unix. */ static gchar * get_session_address_xdg (void) { #ifdef G_OS_UNIX gchar *ret = NULL; gchar *bus; gchar *tmp; GStatBuf buf; bus = g_build_filename (g_get_user_runtime_dir (), "bus", NULL); /* if ENOENT, EPERM, etc., quietly don't use it */ if (g_stat (bus, &buf) < 0) goto out; /* if it isn't ours, we have incorrectly inherited someone else's * XDG_RUNTIME_DIR; silently don't use it */ if (buf.st_uid != geteuid ()) goto out; /* if it isn't a socket, silently don't use it */ if ((buf.st_mode & S_IFMT) != S_IFSOCK) goto out; tmp = g_dbus_address_escape_value (bus); ret = g_strconcat ("unix:path=", tmp, NULL); g_free (tmp); out: g_free (bus); return ret; #else return NULL; #endif }
int main(int argc, char *argv[]) { gchar *filename; gboolean default_used; GtkWidget *notebook; GtkActionGroup *action_group; GtkUIManager *ui_manager; GtkWidget *vbox; GtkWidget *menubar; GtkAccelGroup *accel_group; GError *error = NULL; gchar *icon_file; GOptionContext *context; default_game = g_build_filename(get_pioneers_dir(), "default.game", NULL); /* Gtk+ handles the locale, we must bind the translations */ setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); bind_textdomain_codeset(PACKAGE, "UTF-8"); GtkOSXApplication *theApp = g_object_new(GTK_TYPE_OSX_APPLICATION, NULL); context = /* Long description in the command line: --help */ g_option_context_new(_("- Editor for games of Pioneers")); g_option_context_add_main_entries(context, commandline_entries, GETTEXT_PACKAGE); g_option_context_add_group(context, gtk_get_option_group(TRUE)); g_option_context_parse(context, &argc, &argv, &error); if (error != NULL) { g_print("%s\n", error->message); g_error_free(error); return 1; } if (show_version) { g_print(_("Pioneers version:")); g_print(" "); g_print(FULL_VERSION); g_print("\n"); return 0; } if (filenames != NULL) filename = g_strdup(filenames[0]); else filename = NULL; toplevel = gtk_window_new(GTK_WINDOW_TOPLEVEL); g_signal_connect(G_OBJECT(toplevel), "delete_event", G_CALLBACK(exit_cb), NULL); action_group = gtk_action_group_new("MenuActions"); gtk_action_group_set_translation_domain(action_group, PACKAGE); gtk_action_group_add_actions(action_group, entries, G_N_ELEMENTS(entries), toplevel); ui_manager = gtk_ui_manager_new(); gtk_ui_manager_insert_action_group(ui_manager, action_group, 0); accel_group = gtk_ui_manager_get_accel_group(ui_manager); gtk_window_add_accel_group(GTK_WINDOW(toplevel), accel_group); error = NULL; if (!gtk_ui_manager_add_ui_from_string(ui_manager, ui_description, -1, &error)) { g_message(_("Building menus failed: %s"), error->message); g_error_free(error); return 1; } config_init("pioneers-editor"); icon_file = g_build_filename(g_get_user_runtime_dir(), "pixmaps", MAINICON_FILE, NULL); if (g_file_test(icon_file, G_FILE_TEST_EXISTS)) { gtk_window_set_default_icon_from_file(icon_file, NULL); } else { /* Missing pixmap, main icon file */ g_warning("Pixmap not found: %s", icon_file); } g_free(icon_file); themes_init(); colors_init(); notebook = gtk_notebook_new(); gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP); gtk_notebook_append_page(GTK_NOTEBOOK(notebook), build_map(), /* Tab page name */ gtk_label_new(_("Map"))); gtk_notebook_append_page(GTK_NOTEBOOK(notebook), build_settings(GTK_WINDOW(toplevel)), /* Tab page name */ gtk_label_new(_("Settings"))); terrain_menu = build_terrain_menu(); roll_menu = build_roll_menu(); port_menu = build_port_menu(); vbox = gtk_vbox_new(FALSE, 0); gtk_container_add(GTK_CONTAINER(toplevel), vbox); menubar = gtk_ui_manager_get_widget(ui_manager, "/MainMenu"); gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0); if (filename == NULL) { filename = config_get_string("editor/last-game", &default_used); if (default_used || !g_file_test(filename, G_FILE_TEST_EXISTS)) { g_free(filename); filename = NULL; } } load_game(filename, FALSE); g_free(filename); if (params == NULL) return 1; gtk_widget_show_all(toplevel); gtk_accel_map_load(g_getenv("ACCEL_MAP")); gtk_widget_hide (menubar); gtk_osxapplication_set_menu_bar(theApp, GTK_MENU_SHELL(menubar)); gtk_osxapplication_ready(theApp); gtk_main(); config_finish(); guimap_delete(gmap); g_free(default_game); g_option_context_free(context); return 0; }
int main (int argc, char *argv[]) { GError *error = NULL; GOptionContext *context; GMainLoop *loop; GSocketAddress *address; GSocketService *listener; char *path, *base; char *http_address = NULL; int http_port = 0; int display = 1; const GOptionEntry entries[] = { { "port", 'p', 0, G_OPTION_ARG_INT, &http_port, "Httpd port", "PORT" }, { "address", 'a', 0, G_OPTION_ARG_STRING, &http_address, "Ip address to bind to ", "ADDRESS" }, { NULL } }; context = g_option_context_new ("[:DISPLAY] - broadway display daemon"); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_printerr ("option parsing failed: %s\n", error->message); exit (1); } if (argc > 1) { if (*argv[1] != ':') { g_printerr ("Usage broadwayd [:DISPLAY]\n"); exit (1); } display = strtol(argv[1]+1, NULL, 10); if (display == 0) { g_printerr ("Failed to parse display num %s\n", argv[1]); exit (1); } } if (http_port == 0) http_port = 8080 + (display - 1); server = broadway_server_new (http_address, http_port, &error); if (server == NULL) { g_printerr ("%s\n", error->message); return 1; } base = g_strdup_printf ("broadway%d.socket", display); path = g_build_filename (g_get_user_runtime_dir (), base, NULL); g_free (base); g_print ("Listening on %s\n", path); address = g_unix_socket_address_new_with_type (path, -1, G_UNIX_SOCKET_ADDRESS_ABSTRACT); g_free (path); listener = g_socket_service_new (); if (!g_socket_listener_add_address (G_SOCKET_LISTENER (listener), address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, G_OBJECT (server), NULL, &error)) { g_printerr ("Can't listen: %s\n", error->message); return 1; } g_object_unref (address); g_signal_connect (listener, "incoming", G_CALLBACK (incoming_client), NULL); g_socket_service_start (G_SOCKET_SERVICE (listener)); loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (loop); return 0; }
int main (int argc, char *argv[]) { GError *error = NULL; GOptionContext *context; GMainLoop *loop; GInetAddress *inet; GSocketAddress *address; GSocketService *listener; char *path, *basename; char *http_address = NULL; char *unixsocket_address = NULL; int http_port = 0; char *ssl_cert = NULL; char *ssl_key = NULL; char *display; int port = 0; const GOptionEntry entries[] = { { "port", 'p', 0, G_OPTION_ARG_INT, &http_port, "Httpd port", "PORT" }, { "address", 'a', 0, G_OPTION_ARG_STRING, &http_address, "Ip address to bind to ", "ADDRESS" }, #ifdef G_OS_UNIX { "unixsocket", 'u', 0, G_OPTION_ARG_STRING, &unixsocket_address, "Unix domain socket address", "ADDRESS" }, #endif { "cert", 'c', 0, G_OPTION_ARG_STRING, &ssl_cert, "SSL certificate path", "PATH" }, { "key", 'k', 0, G_OPTION_ARG_STRING, &ssl_key, "SSL key path", "PATH" }, { NULL } }; setlocale (LC_ALL, ""); context = g_option_context_new ("[:DISPLAY] - broadway display daemon"); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_printerr ("option parsing failed: %s\n", error->message); exit (1); } display = NULL; if (argc > 1) { if (*argv[1] != ':') { g_printerr ("Usage broadwayd [:DISPLAY]\n"); exit (1); } display = argv[1]; } if (display == NULL) { #ifdef G_OS_UNIX if (g_unix_socket_address_abstract_names_supported ()) display = ":0"; else #endif display = ":tcp"; } if (g_str_has_prefix (display, ":tcp")) { port = strtol (display + strlen (":tcp"), NULL, 10); inet = g_inet_address_new_from_string ("127.0.0.1"); g_print ("Listening on 127.0.0.1:%d\n", port + 9090); address = g_inet_socket_address_new (inet, port + 9090); g_object_unref (inet); } #ifdef G_OS_UNIX else if (display[0] == ':' && g_ascii_isdigit(display[1])) { port = strtol (display + strlen (":"), NULL, 10); basename = g_strdup_printf ("broadway%d.socket", port + 1); path = g_build_filename (g_get_user_runtime_dir (), basename, NULL); g_free (basename); g_print ("Listening on %s\n", path); address = g_unix_socket_address_new_with_type (path, -1, G_UNIX_SOCKET_ADDRESS_ABSTRACT); g_free (path); } #endif else { g_printerr ("Failed to parse display %s\n", display); exit (1); } if (http_port == 0) http_port = 8080 + port; if (unixsocket_address != NULL) server = broadway_server_on_unix_socket_new (unixsocket_address, &error); else server = broadway_server_new (http_address, http_port, ssl_cert, ssl_key, &error); if (server == NULL) { g_printerr ("%s\n", error->message); return 1; } listener = g_socket_service_new (); if (!g_socket_listener_add_address (G_SOCKET_LISTENER (listener), address, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, G_OBJECT (server), NULL, &error)) { g_printerr ("Can't listen: %s\n", error->message); return 1; } g_object_unref (address); g_signal_connect (listener, "incoming", G_CALLBACK (incoming_client), NULL); g_socket_service_start (G_SOCKET_SERVICE (listener)); loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (loop); return 0; }
int main ( int argc, char *argv[] ) { TIMINGS_START (); cmd_set_arguments ( argc, argv ); // Version if ( find_arg ( "-v" ) >= 0 || find_arg ( "-version" ) >= 0 ) { #ifdef GIT_VERSION fprintf ( stdout, "Version: "GIT_VERSION "\n" ); #else fprintf ( stdout, "Version: "VERSION "\n" ); #endif exit ( EXIT_SUCCESS ); } // Detect if we are in dmenu mode. // This has two possible causes. // 1 the user specifies it on the command-line. if ( find_arg ( "-dmenu" ) >= 0 ) { dmenu_mode = TRUE; } // 2 the binary that executed is called dmenu (e.g. symlink to rofi) else{ // Get the base name of the executable called. char *base_name = g_path_get_basename ( argv[0] ); const char * const dmenu_str = "dmenu"; dmenu_mode = ( strcmp ( base_name, dmenu_str ) == 0 ); // Free the basename for dmenu detection. g_free ( base_name ); } TICK (); // Get the path to the cache dir. cache_dir = g_get_user_cache_dir (); // Create pid file path. const char *path = g_get_user_runtime_dir (); if ( path ) { pidfile = g_build_filename ( path, "rofi.pid", NULL ); } config_parser_add_option ( xrm_String, "pid", (void * *) &pidfile, "Pidfile location" ); if ( find_arg ( "-config" ) < 0 ) { const char *cpath = g_get_user_config_dir (); if ( cpath ) { config_path = g_build_filename ( cpath, "rofi", "config", NULL ); } } else { char *c = NULL; find_arg_str ( "-config", &c ); config_path = rofi_expand_path ( c ); } TICK (); // Register cleanup function. atexit ( cleanup ); TICK (); // Get DISPLAY, first env, then argument. char *display_str = getenv ( "DISPLAY" ); find_arg_str ( "-display", &display_str ); if ( setlocale ( LC_ALL, "" ) == NULL ) { fprintf ( stderr, "Failed to set locale.\n" ); return EXIT_FAILURE; } xcb->connection = xcb_connect ( display_str, &xcb->screen_nbr ); if ( xcb_connection_has_error ( xcb->connection ) ) { fprintf ( stderr, "Failed to open display: %s", display_str ); return EXIT_FAILURE; } TICK_N ( "Open Display" ); xcb->screen = xcb_aux_get_screen ( xcb->connection, xcb->screen_nbr ); xcb_intern_atom_cookie_t *ac = xcb_ewmh_init_atoms ( xcb->connection, &xcb->ewmh ); xcb_generic_error_t *errors = NULL; xcb_ewmh_init_atoms_replies ( &xcb->ewmh, ac, &errors ); if ( errors ) { fprintf ( stderr, "Failed to create EWMH atoms\n" ); free ( errors ); } if ( xkb_x11_setup_xkb_extension ( xcb->connection, XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION, XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS, NULL, NULL, &xkb.first_event, NULL ) < 0 ) { fprintf ( stderr, "cannot setup XKB extension!\n" ); return EXIT_FAILURE; } xkb.context = xkb_context_new ( XKB_CONTEXT_NO_FLAGS ); if ( xkb.context == NULL ) { fprintf ( stderr, "cannot create XKB context!\n" ); return EXIT_FAILURE; } xkb.xcb_connection = xcb->connection; xkb.device_id = xkb_x11_get_core_keyboard_device_id ( xcb->connection ); enum { required_events = ( XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY | XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_STATE_NOTIFY ), required_nkn_details = ( XCB_XKB_NKN_DETAIL_KEYCODES ), required_map_parts = ( XCB_XKB_MAP_PART_KEY_TYPES | XCB_XKB_MAP_PART_KEY_SYMS | XCB_XKB_MAP_PART_MODIFIER_MAP | XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS | XCB_XKB_MAP_PART_KEY_ACTIONS | XCB_XKB_MAP_PART_VIRTUAL_MODS | XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP ), required_state_details = ( XCB_XKB_STATE_PART_MODIFIER_BASE | XCB_XKB_STATE_PART_MODIFIER_LATCH | XCB_XKB_STATE_PART_MODIFIER_LOCK | XCB_XKB_STATE_PART_GROUP_BASE | XCB_XKB_STATE_PART_GROUP_LATCH | XCB_XKB_STATE_PART_GROUP_LOCK ), }; static const xcb_xkb_select_events_details_t details = { .affectNewKeyboard = required_nkn_details, .newKeyboardDetails = required_nkn_details, .affectState = required_state_details, .stateDetails = required_state_details, }; xcb_xkb_select_events ( xcb->connection, xkb.device_id, required_events, /* affectWhich */ 0, /* clear */ required_events, /* selectAll */ required_map_parts, /* affectMap */ required_map_parts, /* map */ &details ); xkb.keymap = xkb_x11_keymap_new_from_device ( xkb.context, xcb->connection, xkb.device_id, XKB_KEYMAP_COMPILE_NO_FLAGS ); if ( xkb.keymap == NULL ) { fprintf ( stderr, "Failed to get Keymap for current keyboard device.\n" ); return EXIT_FAILURE; } xkb.state = xkb_x11_state_new_from_device ( xkb.keymap, xcb->connection, xkb.device_id ); if ( xkb.state == NULL ) { fprintf ( stderr, "Failed to get state object for current keyboard device.\n" ); return EXIT_FAILURE; } xkb.compose.table = xkb_compose_table_new_from_locale ( xkb.context, setlocale ( LC_CTYPE, NULL ), 0 ); if ( xkb.compose.table != NULL ) { xkb.compose.state = xkb_compose_state_new ( xkb.compose.table, 0 ); } else { fprintf ( stderr, "Failed to get keyboard compose table. Trying to limp on.\n" ); } if ( xcb_connection_has_error ( xcb->connection ) ) { fprintf ( stderr, "Connection has error\n" ); exit ( EXIT_FAILURE ); } x11_setup ( &xkb ); if ( xcb_connection_has_error ( xcb->connection ) ) { fprintf ( stderr, "Connection has error\n" ); exit ( EXIT_FAILURE ); } const xcb_query_extension_reply_t *er = xcb_get_extension_data ( xcb->connection, &xcb_xinerama_id ); if ( er ) { if ( er->present ) { xcb_xinerama_is_active_cookie_t is_active_req = xcb_xinerama_is_active ( xcb->connection ); xcb_xinerama_is_active_reply_t *is_active = xcb_xinerama_is_active_reply ( xcb->connection, is_active_req, NULL ); xcb->has_xinerama = is_active->state; free ( is_active ); } } main_loop = g_main_loop_new ( NULL, FALSE ); TICK_N ( "Setup mainloop" ); // startup not. xcb->sndisplay = sn_xcb_display_new ( xcb->connection, error_trap_push, error_trap_pop ); if ( xcb_connection_has_error ( xcb->connection ) ) { fprintf ( stderr, "Connection has error\n" ); exit ( EXIT_FAILURE ); } if ( xcb->sndisplay != NULL ) { xcb->sncontext = sn_launchee_context_new_from_environment ( xcb->sndisplay, xcb->screen_nbr ); } if ( xcb_connection_has_error ( xcb->connection ) ) { fprintf ( stderr, "Connection has error\n" ); exit ( EXIT_FAILURE ); } TICK_N ( "Startup Notification" ); // Initialize Xresources subsystem. config_parse_xresource_init (); TICK_N ( "Initialize Xresources system" ); // Setup keybinding setup_abe (); TICK_N ( "Setup abe" ); if ( find_arg ( "-no-config" ) < 0 ) { load_configuration ( ); } if ( !dmenu_mode ) { // setup_modi setup_modi (); } if ( find_arg ( "-no-config" ) < 0 ) { // Reload for dynamic part. load_configuration_dynamic ( ); } // Dump. // catch help request if ( find_arg ( "-h" ) >= 0 || find_arg ( "-help" ) >= 0 || find_arg ( "--help" ) >= 0 ) { help ( argc, argv ); exit ( EXIT_SUCCESS ); } if ( find_arg ( "-dump-xresources" ) >= 0 ) { config_parse_xresource_dump (); exit ( EXIT_SUCCESS ); } if ( find_arg ( "-dump-xresources-theme" ) >= 0 ) { config_parse_xresources_theme_dump (); exit ( EXIT_SUCCESS ); } main_loop_source = g_water_xcb_source_new_for_connection ( NULL, xcb->connection, main_loop_x11_event_handler, NULL, NULL ); TICK_N ( "X11 Setup " ); rofi_view_workers_initialize (); // Setup signal handling sources. // SIGINT g_unix_signal_add ( SIGINT, main_loop_signal_handler_int, NULL ); g_idle_add ( startup, NULL ); // Start mainloop. g_main_loop_run ( main_loop ); return return_code; }
static int run_bridge (const gchar *interactive, gboolean privileged_slave) { CockpitTransport *transport; gboolean terminated = FALSE; gboolean interupted = FALSE; gboolean closed = FALSE; CockpitPortal *super = NULL; CockpitPortal *pcp = NULL; gpointer polkit_agent = NULL; const gchar *directory; struct passwd *pwd; GPid daemon_pid = 0; GPid agent_pid = 0; guint sig_term; guint sig_int; int outfd; uid_t uid; cockpit_set_journal_logging (G_LOG_DOMAIN, !isatty (2)); /* * The bridge always runs from within $XDG_RUNTIME_DIR * This makes it easy to create user sockets and/or files. */ if (!privileged_slave) { directory = g_get_user_runtime_dir (); if (g_mkdir_with_parents (directory, 0700) < 0) g_warning ("couldn't create runtime dir: %s: %s", directory, g_strerror (errno)); else if (g_chdir (directory) < 0) g_warning ("couldn't change to runtime dir: %s: %s", directory, g_strerror (errno)); } /* Always set environment variables early */ uid = geteuid(); pwd = getpwuid_a (uid); if (pwd == NULL) { g_message ("couldn't get user info: %s", g_strerror (errno)); } else { g_setenv ("USER", pwd->pw_name, TRUE); g_setenv ("HOME", pwd->pw_dir, TRUE); g_setenv ("SHELL", pwd->pw_shell, TRUE); } /* Reset the umask, typically this is done in .bashrc for a login shell */ umask (022); /* * This process talks on stdin/stdout. However lots of stuff wants to write * to stdout, such as g_debug, and uses fd 1 to do that. Reroute fd 1 so that * it goes to stderr, and use another fd for stdout. */ outfd = dup (1); if (outfd < 0 || dup2 (2, 1) < 1) { g_warning ("bridge couldn't redirect stdout to stderr"); outfd = 1; } sig_term = g_unix_signal_add (SIGTERM, on_signal_done, &terminated); sig_int = g_unix_signal_add (SIGINT, on_signal_done, &interupted); g_type_init (); /* Start daemons if necessary */ if (!interactive && !privileged_slave) { if (!have_env ("DBUS_SESSION_BUS_ADDRESS")) daemon_pid = start_dbus_daemon (); if (!have_env ("SSH_AUTH_SOCK")) agent_pid = start_ssh_agent (); } packages = cockpit_packages_new (); cockpit_dbus_internal_startup (interactive != NULL); if (interactive) { /* Allow skipping the init message when interactive */ init_received = TRUE; transport = cockpit_interact_transport_new (0, outfd, interactive); } else { transport = cockpit_pipe_transport_new_fds ("stdio", 0, outfd); } if (uid != 0) { if (!interactive) polkit_agent = cockpit_polkit_agent_register (transport, NULL); super = cockpit_portal_new_superuser (transport); } g_resources_register (cockpitassets_get_resource ()); cockpit_web_failure_resource = "/org/cockpit-project/Cockpit/fail.html"; pcp = cockpit_portal_new_pcp (transport); cockpit_dbus_time_startup (); cockpit_dbus_user_startup (pwd); cockpit_dbus_setup_startup (); g_free (pwd); pwd = NULL; g_signal_connect (transport, "control", G_CALLBACK (on_transport_control), NULL); g_signal_connect (transport, "closed", G_CALLBACK (on_closed_set_flag), &closed); send_init_command (transport); /* Owns the channels */ channels = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); while (!terminated && !closed && !interupted) g_main_context_iteration (NULL, TRUE); if (polkit_agent) cockpit_polkit_agent_unregister (polkit_agent); if (super) g_object_unref (super); g_object_unref (pcp); g_object_unref (transport); g_hash_table_destroy (channels); cockpit_dbus_internal_cleanup (); cockpit_packages_free (packages); packages = NULL; if (daemon_pid) kill (daemon_pid, SIGTERM); if (agent_pid) kill (agent_pid, SIGTERM); g_source_remove (sig_term); g_source_remove (sig_int); /* So the caller gets the right signal */ if (terminated) raise (SIGTERM); return 0; }
static const gchar * parse_cert_option_as_database (JsonObject *options, const gchar *option, GTlsDatabase **database) { gboolean temporary = FALSE; GError *error = NULL; const gchar *problem; const gchar *file; const gchar *data; gchar *path; gint fd; problem = parse_option_file_or_data (options, option, &file, &data); if (problem) return problem; if (file) { path = expand_filename (file); problem = NULL; } else if (data) { temporary = TRUE; path = g_build_filename (g_get_user_runtime_dir (), "cockpit-bridge-cert-authority.XXXXXX", NULL); fd = g_mkstemp (path); if (fd < 0) { g_warning ("couldn't create temporary directory: %s: %s", path, g_strerror (errno)); problem = "internal-error"; } else { close (fd); if (!g_file_set_contents (path, data, -1, &error)) { g_warning ("couldn't write temporary data to: %s: %s", path, error->message); problem = "internal-error"; g_clear_error (&error); } } } else { /* Not specified */ *database = NULL; return NULL; } if (problem == NULL) { *database = g_tls_file_database_new (path, &error); if (error) { g_warning ("couldn't load certificate data: %s: %s", path, error->message); problem = "internal-error"; g_clear_error (&error); } } /* Leave around when problem, for debugging */ if (temporary && problem == NULL) g_unlink (path); g_free (path); return problem; }
int main (int argc, char **argv) { GError *error = NULL; static char **override_autostart_dirs = NULL; static char *opt_session_name = NULL; const char *debug_string = NULL; gboolean gl_failed = FALSE; guint name_owner_id; GOptionContext *options; static GOptionEntry entries[] = { { "autostart", 'a', 0, G_OPTION_ARG_STRING_ARRAY, &override_autostart_dirs, N_("Override standard autostart directories"), N_("AUTOSTART_DIR") }, { "session", 0, 0, G_OPTION_ARG_STRING, &opt_session_name, N_("Session to use"), N_("SESSION_NAME") }, { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, N_("Enable debugging code"), NULL }, { "failsafe", 'f', 0, G_OPTION_ARG_NONE, &failsafe, N_("Do not load user-specified applications"), NULL }, { "version", 0, 0, G_OPTION_ARG_NONE, &show_version, N_("Version of this application"), NULL }, /* Translators: the 'fail whale' is the black dialog we show when something goes seriously wrong */ { "whale", 0, 0, G_OPTION_ARG_NONE, &please_fail, N_("Show the fail whale dialog for testing"), NULL }, { "disable-acceleration-check", 0, 0, G_OPTION_ARG_NONE, &disable_acceleration_check, N_("Disable hardware acceleration check"), NULL }, { NULL, 0, 0, 0, NULL, NULL, NULL } }; /* Make sure that we have a session bus */ if (!require_dbus_session (argc, argv, &error)) { gsm_util_init_error (TRUE, "%s", error->message); } /* From 3.14 GDM sets XDG_CURRENT_DESKTOP. For compatibility with * older versions of GDM, other display managers, and startx, * set a fallback value if we don't find it set. */ if (g_getenv ("XDG_CURRENT_DESKTOP") == NULL) { g_setenv("XDG_CURRENT_DESKTOP", "GNOME", TRUE); gsm_util_setenv ("XDG_CURRENT_DESKTOP", "GNOME"); } /* Make sure we initialize gio in a way that does not autostart any daemon */ initialize_gio (); setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); debug_string = g_getenv ("GNOME_SESSION_DEBUG"); if (debug_string != NULL) { debug = rpmatch (debug_string) == TRUE || atoi (debug_string) == 1; } error = NULL; options = g_option_context_new (_(" - the GNOME session manager")); g_option_context_add_main_entries (options, entries, GETTEXT_PACKAGE); g_option_context_parse (options, &argc, &argv, &error); if (error != NULL) { g_warning ("%s", error->message); exit (1); } g_option_context_free (options); /* Rebind stdout/stderr to the journal explicitly, so that * journald picks ups the nicer "gnome-session" as the program * name instead of whatever shell script GDM happened to use. */ #ifdef HAVE_SYSTEMD if (!debug) { int journalfd; journalfd = sd_journal_stream_fd (PACKAGE, LOG_INFO, 0); if (journalfd >= 0) { dup2(journalfd, 1); dup2(journalfd, 2); } } #endif gdm_log_init (); gdm_log_set_debug (debug); if (disable_acceleration_check) { g_debug ("hardware acceleration check is disabled"); } else { /* Check GL, if it doesn't work out then force software fallback */ if (!check_gl (&error)) { gl_failed = TRUE; g_debug ("hardware acceleration check failed: %s", error? error->message : ""); g_clear_error (&error); if (g_getenv ("LIBGL_ALWAYS_SOFTWARE") == NULL) { g_setenv ("LIBGL_ALWAYS_SOFTWARE", "1", TRUE); if (!check_gl (&error)) { g_warning ("software acceleration check failed: %s", error? error->message : ""); g_clear_error (&error); } else { gl_failed = FALSE; } } } } if (show_version) { g_print ("%s %s\n", argv [0], VERSION); exit (0); } if (gl_failed) { gsm_fail_whale_dialog_we_failed (FALSE, TRUE, NULL); gsm_main (); exit (1); } if (please_fail) { gsm_fail_whale_dialog_we_failed (TRUE, TRUE, NULL); gsm_main (); exit (1); } { gchar *ibus_path; ibus_path = g_find_program_in_path("ibus-daemon"); if (ibus_path) { const gchar *p; p = g_getenv ("QT_IM_MODULE"); if (!p || !*p) p = "ibus"; gsm_util_setenv ("QT_IM_MODULE", p); p = g_getenv ("XMODIFIERS"); if (!p || !*p) p = "@im=ibus"; gsm_util_setenv ("XMODIFIERS", p); } g_free (ibus_path); } /* Some third-party programs rely on GNOME_DESKTOP_SESSION_ID to * detect if GNOME is running. We keep this for compatibility reasons. */ gsm_util_setenv ("GNOME_DESKTOP_SESSION_ID", "this-is-deprecated"); /* We want to use the GNOME menus which has the designed categories. */ gsm_util_setenv ("XDG_MENU_PREFIX", "gnome-"); /* hack to fix keyring until we can reorder things in 3.20 * https://bugzilla.gnome.org/show_bug.cgi?id=738205 */ if (g_strcmp0 (g_getenv ("XDG_SESSION_TYPE"), "wayland") == 0 && g_getenv ("GSM_SKIP_SSH_AGENT_WORKAROUND") == NULL) { char *ssh_socket; ssh_socket = g_build_filename (g_get_user_runtime_dir (), "keyring", "ssh", NULL); gsm_util_setenv ("SSH_AUTH_SOCK", ssh_socket); g_free (ssh_socket); } gsm_util_set_autostart_dirs (override_autostart_dirs); session_name = opt_session_name; /* Talk to logind before acquiring a name, since it does synchronous * calls at initialization time that invoke a main loop and if we * already owned a name, then we would service too early during * that main loop. */ g_object_unref (gsm_get_system ()); name_owner_id = acquire_name (); gsm_main (); g_clear_object (&manager); g_free (gl_renderer); g_bus_unown_name (name_owner_id); gdm_log_shutdown (); return 0; }
/** * eventc_light_connection_connect: * @connection: an #EventcLightConnection * * Initializes the connection to the stored name. * * This call does bloking I/O. */ EVENTD_EXPORT gint eventc_light_connection_connect(EventcLightConnection *self) { g_return_val_if_fail(self != NULL, -EFAULT); gint error = 0; if ( ! _eventc_light_connection_expect_disconnected(self, &error) ) return error; const gchar *name = self->name; gchar *name_ = NULL; if ( ( name == NULL ) || ( *name == '\0' ) ) name = g_getenv("EVENTC_HOST"); if ( ( name == NULL ) || ( *name == '\0' ) || ( g_strcmp0(name, "localhost") == 0 ) ) { const gchar *runtime_dir = g_get_user_runtime_dir(); #ifdef G_OS_UNIX /* System mode */ if ( g_getenv("XDG_RUNTIME_DIR") == NULL ) runtime_dir = "/run"; #endif /* G_OS_UNIX */ name = name_ = g_build_filename(runtime_dir, PACKAGE_NAME, EVP_UNIX_SOCKET, NULL); } if ( ! g_path_is_absolute(name) ) { error = -EINVAL; goto ret; } #ifdef G_OS_UNIX gsize length; length = strlen(name); sa_family_t family = AF_UNIX; struct sockaddr_un addr = { .sun_family = family }; if ( ( length + 1 ) > sizeof(addr.sun_path) ) { error = -E2BIG; goto ret; } strncpy(addr.sun_path, name, length+1); if ( g_str_has_prefix(name, "@") ) addr.sun_path[0] = '\0'; gint fd; fd = socket(family, SOCK_STREAM, 0); if ( fd < 0 ) { error = -errno; goto ret; } if ( connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0 ) { error = -errno; close(fd); goto ret; } if ( fcntl(fd, F_SETFL, O_NONBLOCK) < 0 ) { error = -errno; close(fd); goto ret; } self->socket = fd; #else /* ! G_OS_UNIX */ if ( ! g_file_test(name, G_FILE_TEST_IS_REGULAR) ) { error = -ENOENT; goto ret; } GError *_inner_error_ = NULL; gchar *str; guint64 port; g_file_get_contents(name, &str, NULL, &_inner_error_); if ( str == NULL ) { error = -WSAEINVAL; goto ret; } port = g_ascii_strtoull(str, NULL, 10); //g_free(str); if ( ( port == 0 ) || ( port > G_MAXUINT16 ) ) { error = -ERANGE; goto ret; } struct sockaddr_in addr = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK), .sin_port = htons(port) }; SOCKET sock; sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if ( sock == INVALID_SOCKET ) { error = -WSAGetLastError(); goto ret; } g_debug("Socket created, connecting to [%s]:%llu", inet_ntoa(addr.sin_addr), port); if ( connect(sock, (SOCKADDR *) &addr, sizeof(addr)) == SOCKET_ERROR ) { error = -WSAGetLastError(); closesocket(sock); goto ret; } g_debug("Socket connected"); gulong set = 1; if ( ioctlsocket(sock, FIONBIO, &set) == SOCKET_ERROR ) { error = -WSAGetLastError(); closesocket(sock); goto ret; } g_debug("Socket non blocking"); self->socket = sock; #endif /* ! G_OS_UNIX */ if ( self->subscribe ) error = _eventc_light_connection_send_message(self, eventd_protocol_generate_subscribe(self->protocol, self->subscriptions)); if ( error != 0 ) { close(self->socket); self->socket = 0; } g_debug("ok"); ret: g_free(name_); return error; }
static gboolean _eventd_nd_wl_create_buffer(EventdNdSurface *self) { struct wl_shm_pool *pool; struct wl_buffer *buffer; gint fd; gpointer data; gint width, height, stride; gsize size; width = self->width * self->context->scale; height = self->height * self->context->scale; stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); size = stride * height; gchar *filename; filename = g_build_filename(g_get_user_runtime_dir(), PACKAGE_NAME G_DIR_SEPARATOR_S "wayland-surface", NULL); fd = g_open(filename, O_CREAT | O_RDWR | O_CLOEXEC, 0); g_unlink(filename); g_free(filename); if ( fd < 0 ) { g_warning("creating a buffer file for %zu B failed: %s\n", size, g_strerror(errno)); return FALSE; } if ( ftruncate(fd, size) < 0 ) { close(fd); return FALSE; } data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if ( data == MAP_FAILED ) { g_warning("mmap failed: %s\n", g_strerror(errno)); close(fd); return FALSE; } cairo_surface_t *cairo_surface; cairo_surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, 4 * width); cairo_surface_set_device_scale(cairo_surface, self->context->scale, self->context->scale); self->context->nd->notification_draw(self->notification, cairo_surface, TRUE); cairo_surface_destroy(cairo_surface); munmap(data, size); pool = wl_shm_create_pool(self->context->shm, fd, size); buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888); wl_shm_pool_destroy(pool); close(fd); if ( self->buffer != NULL ) _eventd_nd_wl_buffer_release(self->buffer, self->buffer->buffer); self->buffer = g_new0(EventdNdWlBuffer, 1); self->buffer->buffer = buffer; self->buffer->data = data; self->buffer->size = size; wl_buffer_add_listener(buffer, &_eventd_nd_wl_buffer_listener, self->buffer); wl_surface_damage(self->surface, 0, 0, self->width, self->height); wl_surface_attach(self->surface, self->buffer->buffer, 0, 0); if ( wl_surface_get_version(self->surface) >= WL_SURFACE_SET_BUFFER_SCALE_SINCE_VERSION ) wl_surface_set_buffer_scale(self->surface, self->context->scale); wl_surface_commit(self->surface); return TRUE; }
static void kinit_thread_func (GTask *task, gpointer source_object, gpointer task_data, GCancellable *cancellable) { LoginClosure *login = task_data; krb5_context k5 = NULL; krb5_error_code code; GError *error = NULL; gchar *filename = NULL; gchar *contents; gsize length; gint temp_fd; filename = g_build_filename (g_get_user_runtime_dir (), "um-krb5-creds.XXXXXX", NULL); temp_fd = g_mkstemp_full (filename, O_RDWR, S_IRUSR | S_IWUSR); if (temp_fd == -1) { g_warning ("Couldn't create credential cache file: %s: %s", filename, g_strerror (errno)); g_free (filename); filename = NULL; } else { close (temp_fd); } code = krb5_init_context (&k5); if (code == 0) { code = login_perform_kinit (k5, login->realm, login->user, login->password, filename); } switch (code) { case 0: if (filename != NULL) { g_file_get_contents (filename, &contents, &length, &error); if (error == NULL) { login->credentials = g_bytes_new_take (contents, length); g_debug ("Read in credential cache: %s", filename); } else { g_warning ("Couldn't read credential cache: %s: %s", filename, error->message); g_error_free (error); } } g_task_return_boolean (task, TRUE); break; case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN: case KRB5KDC_ERR_POLICY: g_task_return_new_error (task, UM_REALM_ERROR, UM_REALM_ERROR_BAD_LOGIN, _("Cannot log in as %s at the %s domain"), login->user, login->domain); break; case KRB5KDC_ERR_PREAUTH_FAILED: case KRB5KRB_AP_ERR_BAD_INTEGRITY: g_task_return_new_error (task, UM_REALM_ERROR, UM_REALM_ERROR_BAD_PASSWORD, _("Invalid password, please try again")); break; case KRB5_PREAUTH_FAILED: case KRB5KDC_ERR_KEY_EXP: case KRB5KDC_ERR_CLIENT_REVOKED: case KRB5KDC_ERR_ETYPE_NOSUPP: case KRB5_PROG_ETYPE_NOSUPP: g_task_return_new_error (task, UM_REALM_ERROR, UM_REALM_ERROR_CANNOT_AUTH, _("Cannot log in as %s at the %s domain"), login->user, login->domain); break; default: g_task_return_new_error (task, UM_REALM_ERROR, UM_REALM_ERROR_GENERIC, _("Couldn't connect to the %s domain: %s"), login->domain, krb5_get_error_message (k5, code)); break; } if (filename) { g_unlink (filename); g_debug ("Deleted credential cache: %s", filename); g_free (filename); } if (k5) krb5_free_context (k5); }