bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/7] init: use libhurdnotify for shutdown notifications


From: Justus Winter
Subject: [PATCH 2/7] init: use libhurdnotify for shutdown notifications
Date: Mon, 16 Sep 2013 16:09:00 +0200

XXX
---
 init/Makefile |    2 +-
 init/init.c   |   87 +++++++++++++++++++++------------------------------------
 2 files changed, 33 insertions(+), 56 deletions(-)

diff --git a/init/Makefile b/init/Makefile
index ffb82ff..cf4d492 100644
--- a/init/Makefile
+++ b/init/Makefile
@@ -23,7 +23,7 @@ OBJS = $(SRCS:.c=.o) \
        startupServer.o notifyServer.o startup_replyUser.o msgServer.o \
        startup_notifyUser.o
 target = init
-HURDLIBS = shouldbeinlibc
+HURDLIBS = shouldbeinlibc hurdnotify
 
 include ../Makeconf
 
diff --git a/init/init.c b/init/init.c
index 94f1a9b..0cd32c4 100644
--- a/init/init.c
+++ b/init/init.c
@@ -50,6 +50,7 @@
 #include <version.h>
 #include <argp.h>
 #include <pids.h>
+#include <hurd/notify.h>
 
 #include "startup_notify_U.h"
 #include "startup_reply_U.h"
@@ -82,14 +83,6 @@ static char doc[] = "Start and maintain hurd core servers 
and system run state";
 
 static int booted;             /* Set when the core servers are up.  */
 
-/* This structure keeps track of each notified task.  */
-struct ntfy_task
-  {
-    mach_port_t notify_port;
-    struct ntfy_task *next;
-    char *name;
-  };
-
 /* This structure keeps track of each registered essential task.  */
 struct ess_task
   {
@@ -100,7 +93,7 @@ struct ess_task
 
 /* These are linked lists of all of the registered items.  */
 static struct ess_task *ess_tasks;
-static struct ntfy_task *ntfy_tasks;
+static struct hurd_notify ntfy_tasks;
 
 
 /* Our receive right */
@@ -190,34 +183,35 @@ crash_mach (void)
   reboot_mach (crash_flags);
 }
 
-/* Notify all tasks that have requested shutdown notifications */
-void
-notify_shutdown (const char *msg)
+/* Notify a task that has requested shutdown notifications.  */
+error_t
+do_notify_shutdown (mach_port_t port,
+                    mach_port_t reply_port,
+                    void *data,
+                    void *common_data)
 {
-  struct ntfy_task *n;
+  error_t err;
+  printf ("%s: notifying %s of %s...",
+          program_invocation_short_name, data?: "(unknown)", common_data);
+  fflush (stdout);
 
-  for (n = ntfy_tasks; n != NULL; n = n->next)
-    {
-      error_t err;
-      printf ("%s: notifying %s of %s...",
-             program_invocation_short_name, n->name, msg);
-      fflush (stdout);
-      err = startup_dosync (n->notify_port, 60000); /* 1 minute to reply */
-      if (err == MACH_SEND_INVALID_DEST)
-       puts ("(no longer present)");
-      else if (err)
-       puts (strerror (err));
-      else
-       puts ("done");
-      fflush (stdout);
-    }
+  err = startup_dosync (port, 60000); /* 1 minute to reply */
+  if (err == MACH_SEND_INVALID_DEST)
+    puts ("(no longer present)");
+  else if (err)
+    puts (strerror (err));
+  else
+    puts ("done");
+  fflush (stdout);
+
+  return err;
 }
 
 /* Reboot the Hurd. */
 void
 reboot_system (int flags)
 {
-  notify_shutdown ("shutdown");
+  hurd_notify_do_notify (&ntfy_tasks, BOOT (flags));
 
   if (fakeboot)
     {
@@ -616,6 +610,8 @@ main (int argc, char **argv, char **envp)
   fflush (stdout);
   default_ports[INIT_PORT_BOOTSTRAP] = MACH_PORT_NULL;
 
+  hurd_notify_init (&ntfy_tasks, do_notify_shutdown, free);
+
   /* Wait for messages.  When both auth and proc have started, we
      run launch_system which does the rest of the boot.  */
   while (1)
@@ -1249,27 +1245,18 @@ S_startup_request_notification (mach_port_t server,
                                mach_port_t notify,
                                char *name)
 {
-  struct ntfy_task *nt;
-
   request_dead_name (notify);
 
-  /* Note that the ntfy_tasks list is kept in inverse order of the
-     calls; this is important.  We need later notification requests
-     to get executed first.  */
-  nt = malloc (sizeof (struct ntfy_task));
-  nt->notify_port = notify;
-  nt->next = ntfy_tasks;
-  ntfy_tasks = nt;
-  nt->name = malloc (strlen (name) + 1);
-  strcpy (nt->name, name);
-  return 0;
+  /* Note that the ntfy_tasks list is kept in inverse order of
+     the calls; this is important.  We need later notification
+     requests to get executed first.  */
+  return hurd_notify_prepend (&ntfy_tasks, notify, strdup (name));
 }
 
 kern_return_t
 do_mach_notify_dead_name (mach_port_t notify,
                          mach_port_t name)
 {
-  struct ntfy_task *nt, *pnt;
   struct ess_task *et;
 
   assert (notify == startup);
@@ -1285,19 +1272,9 @@ do_mach_notify_dead_name (mach_port_t notify,
        crash_system ();
       }
 
-  for (nt = ntfy_tasks, pnt = NULL; nt != NULL; pnt = nt, nt = nt->next)
-    if (nt->notify_port == name)
-      {
-       /* Someone who wanted to be notified is gone.  */
-       mach_port_deallocate (mach_task_self (), name);
-       if (pnt != NULL)
-         pnt->next = nt->next;
-       else
-         ntfy_tasks = nt->next;
-       free (nt);
-
-       return 0;
-      }
+  int found = hurd_notify_remove (&ntfy_tasks, name);
+  if (found)
+    return 0;
 
   if (! booted)
     {
-- 
1.7.10.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]