From mboxrd@z Thu Jan 1 00:00:00 1970 From: Havoc Pennington Date: Fri, 18 Apr 2003 05:51:04 +0000 Subject: Re: communicating with user login sessions MIME-Version: 1 Content-Type: multipart/mixed; boundary="YiEDa0DAkWCtVeE4" Message-Id: List-Id: References: In-Reply-To: To: linux-hotplug@vger.kernel.org --YiEDa0DAkWCtVeE4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Apr 17, 2003 at 04:50:14PM -0700, David Brownell wrote: > How would you speculate that a hotplug event should be delivered > to the "system message bus"? I'm not quite sure how the kernel > would be expected to authenticate itself, or whether it should. > If there's going to be a daemon collecting kernel events, I tend > to think it should be able to read them directly ... Hmm, that long mail I just sent answered the question "what events go over D-BUS" not "who sends the events to D-BUS" I guess. ;-) How the events get to D-BUS is something I'd defer to kernel hackers on - I can't say I have an informed opinion. Basically I gather there's some minimal hook to get the event to a userspace process, and that process connects to D-BUS and sends the event. Easiest way to send an event is with Philip Blundell's dbus-send command line tool from a shell script. Downside of that is that it creates a new connection to the bus and redoes some authentication/handshake work for each event. If that turned out to be a performance issue, you'd probably want some sort of daemon that maintained a permanent connection. I'll attach two patches Tim Waugh did for the CUPS print daemon and the "eggcups" print queue monitor icon - these are in the rawhide packages. They show what's involved. Havoc --YiEDa0DAkWCtVeE4 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="cups-dbus.patch" --- cups-1.1.18/scheduler/main.c.dbus 2003-04-12 19:32:53.000000000 -0400 +++ cups-1.1.18/scheduler/main.c 2003-04-12 19:41:35.000000000 -0400 @@ -42,6 +42,8 @@ #include #include +#include + #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO) # include #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */ @@ -62,6 +64,50 @@ static void usage(void); +static DBusConnection *dbus_connection = NULL; + +static int +init_dbus (void) +{ + DBusConnection *connection; + DBusMessage *message, *reply; + DBusError error; + + if (dbus_connection && + !dbus_connection_get_is_connected (dbus_connection)) { + dbus_connection_unref (dbus_connection); + dbus_connection = NULL; + } + + dbus_error_init (&error); + connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error); + if (connection == NULL) { + dbus_error_free (&error); + return -1; + } + + dbus_connection = connection; + return 0; +} + +int dbus_broadcast (void) +{ + DBusMessage *message; + + if (!dbus_connection || !dbus_connection_get_is_connected (dbus_connection)) { + if (init_dbus () || !dbus_connection) + return -1; + } + + message = dbus_message_new (DBUS_SERVICE_BROADCAST, + "com.redhat.PrinterSpooler.QueueChanged"); + dbus_connection_send (dbus_connection, message, NULL); + dbus_connection_flush (dbus_connection); + dbus_message_unref (message); + + return 0; +} + /* * 'main()' - Main entry for the CUPS scheduler. */ @@ -329,6 +375,8 @@ if (only_ppds) return 0; + init_dbus (); + #ifdef __sgi /* * Try to create a fake lpsched lock file if one is not already there. --- cups-1.1.18/scheduler/cupsd.h.dbus 2002-12-17 14:00:15.000000000 -0500 +++ cups-1.1.18/scheduler/cupsd.h 2003-04-12 19:32:53.000000000 -0400 @@ -171,6 +171,7 @@ extern void StartServer(void); extern void StopServer(void); +extern int dbus_broadcast (void); /* * End of "$Id: cupsd.h,v 1.36 2002/12/17 19:00:15 swdev Exp $". --- cups-1.1.18/scheduler/Makefile.dbus 2002-12-17 14:00:12.000000000 -0500 +++ cups-1.1.18/scheduler/Makefile 2003-04-12 19:32:53.000000000 -0400 @@ -24,6 +24,9 @@ include ../Makedefs +CFLAGS += `pkg-config --cflags dbus-1.0` +DBUS_LDFLAGS += `pkg-config --libs dbus-1.0` + CUPSDOBJS = auth.o banners.o cert.o classes.o client.o conf.o devices.o \ dirsvc.o main.o ipp.o listen.o job.o log.o network.o \ ppds.o printers.o quotas.o server.o @@ -80,7 +83,8 @@ cupsd: $(CUPSDOBJS) libmime.a ../cups/$(LIBCUPS) echo Linking $@... $(CC) $(LDFLAGS) -o cupsd $(CUPSDOBJS) libmime.a \ - $(LIBZ) $(SSLLIBS) $(LIBSLP) $(PAMLIBS) $(LIBS) $(LIBMALLOC) + $(LIBZ) $(SSLLIBS) $(LIBSLP) $(PAMLIBS) $(LIBS) $(LIBMALLOC) \ + $(DBUS_LDFLAGS) # --- cups-1.1.18/scheduler/job.c.dbus 2002-12-17 14:00:16.000000000 -0500 +++ cups-1.1.18/scheduler/job.c 2003-04-12 19:32:53.000000000 -0400 @@ -108,6 +108,7 @@ else Jobs = job; + dbus_broadcast (); return (job); } @@ -201,8 +202,11 @@ NumJobs --; } + dbus_broadcast (); return; } + + dbus_broadcast (); } --YiEDa0DAkWCtVeE4 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="eggcups-0.0.2-dbus.patch" --- desktop-printing-0.1.10/eggcups-0.0.2/configure.in.dbus 2003-04-13 00:45:16.000000000 -0400 +++ desktop-printing-0.1.10/eggcups-0.0.2/configure.in 2003-04-13 00:45:16.000000000 -0400 @@ -48,6 +48,7 @@ gobject-2.0 gtk+-2.0 >= $GTK_REQUIRED libgnomeui-2.0 >= $LIBGNOMEUI_REQUIRED + dbus-glib-1.0 ]) AC_SUBST(EGGCUPS_CFLAGS) --- desktop-printing-0.1.10/eggcups-0.0.2/main.c.dbus 2002-12-08 05:04:44.000000000 -0500 +++ desktop-printing-0.1.10/eggcups-0.0.2/main.c 2003-04-13 00:45:16.000000000 -0400 @@ -2,7 +2,7 @@ /* * Copyright (C) 2002 CodeFactory AB * Copyright (C) 2002 Richard Hult - * Copyright (C) 2002 Tim Waugh + * Copyright (C) 2002, 2003 Tim Waugh * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -31,6 +31,8 @@ #include "lp-intl.h" #include "lpegg.h" +gboolean debug = 0; + gboolean session_die (GnomeClient *client, gpointer client_data) { @@ -74,7 +76,8 @@ if (strcmp (arg, "-?") == 0) { g_printerr ("Usage: %s [--debug]\n", argv[0]); return 0; - } + } else if (strcmp (arg, "--debug") == 0) + debug = 1; ++i; } --- desktop-printing-0.1.10/eggcups-0.0.2/lpegg.c.dbus 2002-12-08 06:55:27.000000000 -0500 +++ desktop-printing-0.1.10/eggcups-0.0.2/lpegg.c 2003-04-13 01:08:20.000000000 -0400 @@ -2,7 +2,7 @@ /* * Copyright (C) 2002 CodeFactory AB * Copyright (C) 2002 Richard Hult - * Copyright (C) 2002 Tim Waugh + * Copyright (C) 2002, 2003 Tim Waugh * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -33,12 +33,18 @@ #include #include #include +#include #include "eggtrayicon.h" #include "lp-intl.h" #include "lpcups.h" #include "lpegg.h" +#include + struct _LpEgg { + /* D-BUS */ + DBusConnection *dbus; + /* Widgets. */ EggTrayIcon *icon; GtkWidget *icon_image; @@ -51,6 +57,8 @@ GtkItemFactory *popup_factory; GtkTooltips *tips; gchar *tip; + + unsigned int timer; }; static void @@ -144,6 +152,61 @@ extern gboolean debug; +static gboolean maybe_change_state (LpEgg *lp); + +static DBusHandlerResult +handle_dbus_message (DBusMessageHandler *handler, + DBusConnection *connection, + DBusMessage *message, + void *data) +{ + LpEgg *lp = (LpEgg *) data; + if (debug) printf ("eggcups: Received message: %s\n", + dbus_message_get_name (message)); + if (dbus_message_name_is (message, DBUS_MESSAGE_LOCAL_DISCONNECT)) { + /* Fall back to polling. */ + if (debug) puts ("eggcups: Falling back to polling.."); + lp->dbus = NULL; + dbus_connection_unref (connection); + lp->timer = g_timeout_add (5000, + (GSourceFunc) maybe_change_state, + lp); + } else if (dbus_message_name_is (message, "com.redhat.PrinterSpooler.QueueChanged")) + maybe_change_state (lp); + + return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS; +} + +static void leak (void *nothing) { } + +static int +init_dbus (LpEgg *lp) +{ + DBusMessageHandler *handler; + DBusConnection *dbus; + DBusError error; + DBusMessage *message, *reply; + + lp->dbus = NULL; + dbus_error_init (&error); + dbus = dbus_bus_get (DBUS_BUS_SYSTEM, &error); + if (dbus == NULL) { + if (debug) printf ("eggcups: %s\n", error.message); + dbus_error_free (&error); + return -1; + } + + /* FIXME "handler" is leaked if we later unref the connection. + * (the way this works may change in libdbus, it's kind + * of annoying) - hp + */ + handler = dbus_message_handler_new (handle_dbus_message, lp, leak); + dbus_connection_add_filter (dbus, handler); + dbus_connection_setup_with_g_main (dbus); + lp->dbus = dbus; + return 0; +} + static gboolean maybe_change_state (LpEgg *lp) { @@ -176,6 +239,12 @@ } else g_free (tip); + /* Try to connect to the system D-BUS */ + if (lp->timer != -1 && !lp->dbus) { + if (!init_dbus (lp)) + g_source_remove (lp->timer); + } + return TRUE; } @@ -335,11 +404,14 @@ G_CALLBACK (icon_destroy_cb), lp); + lp->timer = -1; maybe_change_state (lp); - g_timeout_add (5000, - (GSourceFunc) maybe_change_state, - lp); + if (init_dbus (lp)) + /* Fall back to polling. */ + lp->timer = g_timeout_add (5000, + (GSourceFunc) maybe_change_state, + lp); return lp; } --YiEDa0DAkWCtVeE4-- ------------------------------------------------------- This sf.net email is sponsored by:ThinkGeek Welcome to geek heaven. http://thinkgeek.com/sf _______________________________________________ Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net Linux-hotplug-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel