linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Havoc Pennington <hp@redhat.com>
To: linux-hotplug@vger.kernel.org
Subject: Re: communicating with user login sessions
Date: Fri, 18 Apr 2003 05:51:04 +0000	[thread overview]
Message-ID: <marc-linux-hotplug-105064514720848@msgid-missing> (raw)
In-Reply-To: <marc-linux-hotplug-104977671310586@msgid-missing>

[-- Attachment #1: Type: text/plain, Size: 1317 bytes --]

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



[-- Attachment #2: cups-dbus.patch --]
[-- Type: text/plain, Size: 3100 bytes --]

--- 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 <syslog.h>
 #include <grp.h>
 
+#include <dbus/dbus.h>
+
 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
 #  include <malloc.h>
 #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 ();
 }
 
 

[-- Attachment #3: eggcups-0.0.2-dbus.patch --]
[-- Type: text/plain, Size: 4552 bytes --]

--- 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 <rhult@codefactory.se>
- * Copyright (C) 2002 Tim Waugh <twaugh@redhat.com>
+ * Copyright (C) 2002, 2003 Tim Waugh <twaugh@redhat.com>
  *
  * 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 <rhult@codefactory.se>
- * Copyright (C) 2002 Tim Waugh <twaugh@redhat.com>
+ * Copyright (C) 2002, 2003 Tim Waugh <twaugh@redhat.com>
  *
  * 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 <gdk/gdkkeysyms.h>
 #include <gtk/gtk.h>
 #include <libgnomeui/gnome-stock-icons.h>
+#include <dbus/dbus-glib.h>
 #include "eggtrayicon.h"
 #include "lp-intl.h"
 #include "lpcups.h"
 #include "lpegg.h"
 
+#include <stdio.h>
+
 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;
 }

  parent reply	other threads:[~2003-04-18  5:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-08  4:37 communicating with user login sessions Havoc Pennington
2003-04-08  5:20 ` Greg KH
2003-04-17 23:50 ` David Brownell
2003-04-18  0:13 ` David Brownell
2003-04-18  5:39 ` Havoc Pennington
2003-04-18  5:51 ` Havoc Pennington [this message]
2003-04-18 22:40 ` Greg KH
2003-04-18 23:45 ` David Brownell
2003-04-19  0:49 ` David Brownell
2003-04-19  1:24 ` Havoc Pennington
2003-04-19  1:30 ` Havoc Pennington
2003-04-19  1:43 ` Havoc Pennington

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=marc-linux-hotplug-105064514720848@msgid-missing \
    --to=hp@redhat.com \
    --cc=linux-hotplug@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).