[Openvas-commits] r6049 - in trunk/openvas-administrator: . src

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Dec 3 09:50:27 CET 2009


Author: mwiegand
Date: 2009-12-03 09:50:26 +0100 (Thu, 03 Dec 2009)
New Revision: 6049

Modified:
   trunk/openvas-administrator/ChangeLog
   trunk/openvas-administrator/src/admin.c
   trunk/openvas-administrator/src/admin.h
   trunk/openvas-administrator/src/oap.c
   trunk/openvas-administrator/src/openvasad.c
Log:
Improved locking during feed synchronization.

* src/oap.c: Defined status for a busy resource.
(oap_xml_handle_end_element): Add a currently_syncing element to the
feed description if the feed is currently being synchronized. Respond
with a "busy" message when feed is already being synchronized. Do proper
locking before feed synchronization and remove the lock when done. Send
empty name and description instead of error if we could not identify the
synchronization script.

* src/admin.c: Added new functions for lockfile management.
(openvasad_create_lockfile, openvasad_remove_lockfile,
openvasad_currently_syncing): New.

* src/admin.h: Updated header.

* src/openvasad.c (main): Use new functions for lockfile management.


Modified: trunk/openvas-administrator/ChangeLog
===================================================================
--- trunk/openvas-administrator/ChangeLog	2009-12-03 00:27:53 UTC (rev 6048)
+++ trunk/openvas-administrator/ChangeLog	2009-12-03 08:50:26 UTC (rev 6049)
@@ -1,3 +1,23 @@
+2009-12-03  Michael Wiegand <michael.wiegand at intevation.de>
+
+	Improved locking during feed synchronization.
+
+	* src/oap.c: Defined status for a busy resource.
+	(oap_xml_handle_end_element): Add a currently_syncing element to the
+	feed description if the feed is currently being synchronized. Respond
+	with a "busy" message when feed is already being synchronized. Do proper
+	locking before feed synchronization and remove the lock when done. Send
+	empty name and description instead of error if we could not identify the
+	synchronization script.
+
+	* src/admin.c: Added new functions for lockfile management.
+	(openvasad_create_lockfile, openvasad_remove_lockfile,
+	openvasad_currently_syncing): New.
+
+	* src/admin.h: Updated header.
+
+	* src/openvasad.c (main): Use new functions for lockfile management.
+
 2009-12-01  Michael Wiegand <michael.wiegand at intevation.de>
 
 	Improving feed synchronization support.

Modified: trunk/openvas-administrator/src/admin.c
===================================================================
--- trunk/openvas-administrator/src/admin.c	2009-12-03 00:27:53 UTC (rev 6048)
+++ trunk/openvas-administrator/src/admin.c	2009-12-03 08:50:26 UTC (rev 6049)
@@ -961,4 +961,88 @@
   return TRUE;
 }
 
+/**
+ * @brief Create a lockfile for synchronization and store the name of the
+ * synchronization script.
+ *
+ * @param sync_script The file name of the synchronization script.
+ *
+ * @return TRUE if the lockfile was written successfully, FALSE if
+ * not.
+ */
+gboolean
+openvasad_create_lockfile (const gchar * sync_script)
+{
+  g_assert (sync_script);
 
+  if (openvasad_currently_syncing ())
+    {
+      return FALSE;
+    }
+
+  gchar *lockfile_name = g_build_filename (g_get_tmp_dir (),
+                                           "openvasad-feed-sync.lock",
+                                           NULL);
+
+  if (!g_file_set_contents (lockfile_name, sync_script, -1, NULL))
+    {
+      g_warning ("Failed to write lockfile!");
+      g_free (lockfile_name);
+      return FALSE;
+    }
+
+  g_free (lockfile_name);
+  return TRUE;
+}
+
+/**
+ * @brief Remove a lockfile for synchronization.
+ *
+ * @return TRUE if the lockfile was removed successfully, FALSE if
+ * not.
+ */
+gboolean
+openvasad_remove_lockfile (void)
+{
+  gchar *lockfile_name = g_build_filename (g_get_tmp_dir (),
+                                           "openvasad-feed-sync.lock",
+                                           NULL);
+  if (!g_file_test (lockfile_name, G_FILE_TEST_EXISTS))
+    {
+      g_free (lockfile_name);
+      return TRUE;
+    }
+
+  if (g_unlink (lockfile_name) == 0)
+    {
+      g_free (lockfile_name);
+      return TRUE;
+    }
+
+  g_warning ("Failed to remove lockfile!");
+  g_free (lockfile_name);
+  return FALSE;
+}
+
+/**
+ * @brief Determine if the administrator is currently synchronizing with a
+ * feed, i.e. if a lockfile exists.
+ *
+ * @return TRUE if the administrator is currently synchronizing with a feed,
+ * FALSE if not.
+ */
+gboolean
+openvasad_currently_syncing (void)
+{
+  gchar *lockfile_name = g_build_filename (g_get_tmp_dir (),
+                                           "openvasad-feed-sync.lock",
+                                           NULL);
+  if (g_file_test (lockfile_name, G_FILE_TEST_EXISTS))
+    {
+      g_free (lockfile_name);
+      return TRUE;
+    }
+
+  g_free (lockfile_name);
+  return FALSE;
+}

Modified: trunk/openvas-administrator/src/admin.h
===================================================================
--- trunk/openvas-administrator/src/admin.h	2009-12-03 00:27:53 UTC (rev 6048)
+++ trunk/openvas-administrator/src/admin.h	2009-12-03 08:50:26 UTC (rev 6049)
@@ -40,6 +40,10 @@
 gboolean openvas_get_sync_script_identification (const gchar *, gchar **);
 gboolean openvas_get_sync_script_description (const gchar *, gchar **);
 
+gboolean openvasad_create_lockfile (const gchar *);
+gboolean openvasad_remove_lockfile (void);
+gboolean openvasad_currently_syncing (void);
+
 GString *print_preferences_xml (GHashTable *);
 GString *print_users_xml (GSList *);
 GString *print_users_text (GSList *);

Modified: trunk/openvas-administrator/src/oap.c
===================================================================
--- trunk/openvas-administrator/src/oap.c	2009-12-03 00:27:53 UTC (rev 6048)
+++ trunk/openvas-administrator/src/oap.c	2009-12-03 08:50:26 UTC (rev 6049)
@@ -198,6 +198,7 @@
  *     400 Bad request
  *     401 Must auth
  *     404 Missing
+ *     409 Busy/Conflict
  */
 
 /**
@@ -226,6 +227,16 @@
 #define STATUS_ERROR_MISSING_TEXT      "Resource missing"
 
 /**
+ * @brief Response code for a busy resource.
+ */
+#define STATUS_ERROR_BUSY              "409"
+
+/**
+ * @brief Response code text for a busy resource.
+ */
+#define STATUS_ERROR_BUSY_TEXT         "Resource busy"
+
+/**
  * @brief Response code when authorisation failed.
  */
 #define STATUS_ERROR_AUTH_FAILED       "400"
@@ -1017,6 +1028,10 @@
                 SEND_TO_CLIENT_OR_FAIL ("<feed>");
                 SENDF_TO_CLIENT_OR_FAIL ("<name>%s</name>", ident[3]);
                 SENDF_TO_CLIENT_OR_FAIL ("<description>%s</description>", feed_description);
+                if (openvasad_currently_syncing ())
+                  {
+                    SEND_TO_CLIENT_OR_FAIL ("<currently_syncing />");
+                  }
                 SEND_TO_CLIENT_OR_FAIL ("</feed>");
                 SEND_TO_CLIENT_OR_FAIL ("</describe_feed_response>");
                 g_strfreev (ident);
@@ -1025,7 +1040,14 @@
               }
             else
               {
-                SEND_TO_CLIENT_OR_FAIL (XML_INTERNAL_ERROR ("describe_feed"));
+                SEND_TO_CLIENT_OR_FAIL ("<describe_feed_response"
+                                        " status=\"" STATUS_OK "\""
+                                        " status_text=\"" STATUS_OK_TEXT "\">");
+                SEND_TO_CLIENT_OR_FAIL ("<feed>");
+                SEND_TO_CLIENT_OR_FAIL ("<name></name>");
+                SEND_TO_CLIENT_OR_FAIL ("<description></description>");
+                SEND_TO_CLIENT_OR_FAIL ("</feed>");
+                SEND_TO_CLIENT_OR_FAIL ("</describe_feed_response>");
               }
             set_client_state (CLIENT_AUTHENTIC);
             break;
@@ -1033,6 +1055,14 @@
 
       case CLIENT_SYNC_FEED:
           {
+            if (openvasad_currently_syncing ())
+              {
+                SEND_TO_CLIENT_OR_FAIL ("<sync_feed_response"
+                                        " status=\"" STATUS_ERROR_BUSY "\""
+                                        " status_text=\"" STATUS_ERROR_BUSY_TEXT "\">");
+                SEND_TO_CLIENT_OR_FAIL ("</sync_feed_response>");
+              }
+            openvasad_create_lockfile (sync_script);
             if (openvas_sync_feed (sync_script))
               {
                 SEND_TO_CLIENT_OR_FAIL ("<sync_feed_response"
@@ -1045,6 +1075,7 @@
               {
                 SEND_TO_CLIENT_OR_FAIL (XML_INTERNAL_ERROR ("sync_feed"));
               }
+            openvasad_remove_lockfile ();
             set_client_state (CLIENT_AUTHENTIC);
             break;
           }

Modified: trunk/openvas-administrator/src/openvasad.c
===================================================================
--- trunk/openvas-administrator/src/openvasad.c	2009-12-03 00:27:53 UTC (rev 6048)
+++ trunk/openvas-administrator/src/openvasad.c	2009-12-03 08:50:26 UTC (rev 6049)
@@ -670,33 +670,22 @@
                          " synchronization.");
               exit (EXIT_FAILURE);
             }
-          gchar *lockfile_name = g_build_filename (g_get_tmp_dir (),
-                                                   "openvasad-feed-sync.lock",
-                                                   NULL);
-          if (g_file_test (lockfile_name, G_FILE_TEST_EXISTS))
+          if (openvasad_currently_syncing ())
             {
               g_warning ("Feed is already being synchronized, aborting");
               exit (EXIT_FAILURE);
             }
-
-          if (!g_file_set_contents (lockfile_name, sync_script, -1, NULL))
-            {
-              g_warning ("Failed to write lockfile, aborting");
-              exit (EXIT_FAILURE);
-            }
-
+          openvasad_create_lockfile (sync_script);
           if (!openvas_sync_feed (sync_script))
             {
               g_warning ("Failed to execute synchronization script (%s)!", sync_script);
-              g_unlink (lockfile_name);
-              g_free (lockfile_name);
+              openvasad_remove_lockfile ();
               exit (EXIT_FAILURE);
             }
           else
             {
               g_message ("The feed has been successfully synchronized.");
-              g_unlink (lockfile_name);
-              g_free (lockfile_name);
+              openvasad_remove_lockfile ();
               exit (EXIT_SUCCESS);
             }
         }



More information about the Openvas-commits mailing list