Author: sandervanderburg
Date: Tue Oct 12 15:15:26 2010
New Revision: 24238
URL: https://svn.nixos.org/websvn/nix/?rev=24238&sc=1

Log:
Refactored some fork/exec stuff into a seperate library, since some of them are 
shared and it also removes some boilerplate code

Added:
   disnix/disnix/trunk/src/libinterface/
   disnix/disnix/trunk/src/libinterface/Makefile.am
   disnix/disnix/trunk/src/libinterface/client-interface.c
   disnix/disnix/trunk/src/libinterface/client-interface.h
Modified:
   disnix/disnix/trunk/configure.ac
   disnix/disnix/trunk/src/Doxyfile
   disnix/disnix/trunk/src/Makefile.am
   disnix/disnix/trunk/src/activate/Makefile.am
   disnix/disnix/trunk/src/activate/main.c
   disnix/disnix/trunk/src/collect-garbage/Makefile.am
   disnix/disnix/trunk/src/collect-garbage/main.c
   disnix/disnix/trunk/src/query/Makefile.am
   disnix/disnix/trunk/src/query/main.c

Modified: disnix/disnix/trunk/configure.ac
==============================================================================
--- disnix/disnix/trunk/configure.ac    Tue Oct 12 08:09:57 2010        (r24237)
+++ disnix/disnix/trunk/configure.ac    Tue Oct 12 15:15:26 2010        (r24238)
@@ -84,6 +84,7 @@
 src/libmain/Makefile
 src/libmanifest/Makefile
 src/libinfrastructure/Makefile
+src/libinterface/Makefile
 src/distribute/Makefile
 src/activate/Makefile
 src/visualize/Makefile

Modified: disnix/disnix/trunk/src/Doxyfile
==============================================================================
--- disnix/disnix/trunk/src/Doxyfile    Tue Oct 12 08:09:57 2010        (r24237)
+++ disnix/disnix/trunk/src/Doxyfile    Tue Oct 12 15:15:26 2010        (r24238)
@@ -574,7 +574,7 @@
 # directories like "/usr/src/myproject". Separate the files or directories
 # with spaces.
 
-INPUT                  = activate build collect-garbage dbus-service 
distribute libdistderivation libinfrastructure libmanifest libxmlutil query 
visualize
+INPUT                  = activate build collect-garbage dbus-service 
distribute libdistderivation libinfrastructure libinterface libmain libmanifest 
libxmlutil query visualize
 
 # This tag can be used to specify the character encoding of the source files
 # that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is

Modified: disnix/disnix/trunk/src/Makefile.am
==============================================================================
--- disnix/disnix/trunk/src/Makefile.am Tue Oct 12 08:09:57 2010        (r24237)
+++ disnix/disnix/trunk/src/Makefile.am Tue Oct 12 15:15:26 2010        (r24238)
@@ -1 +1 @@
-SUBDIRS = libmain libxmlutil libinfrastructure collect-garbage query 
dbus-service libdistderivation libmanifest build distribute activate visualize
+SUBDIRS = libmain libxmlutil libinfrastructure libinterface collect-garbage 
query dbus-service libdistderivation libmanifest build distribute activate 
visualize

Modified: disnix/disnix/trunk/src/activate/Makefile.am
==============================================================================
--- disnix/disnix/trunk/src/activate/Makefile.am        Tue Oct 12 08:09:57 
2010        (r24237)
+++ disnix/disnix/trunk/src/activate/Makefile.am        Tue Oct 12 15:15:26 
2010        (r24238)
@@ -3,5 +3,5 @@
 bin_PROGRAMS = disnix-activate
 
 disnix_activate_SOURCES = main.c
-disnix_activate_LDADD = ../libmanifest/libmanifest.la ../libmain/libmain.la
-disnix_activate_CFLAGS = -I../libmanifest -I../libmain
+disnix_activate_LDADD = ../libmanifest/libmanifest.la ../libmain/libmain.la 
../libinterface/libinterface.la
+disnix_activate_CFLAGS = -I../libmanifest -I../libmain -I../libinterface

Modified: disnix/disnix/trunk/src/activate/main.c
==============================================================================
--- disnix/disnix/trunk/src/activate/main.c     Tue Oct 12 08:09:57 2010        
(r24237)
+++ disnix/disnix/trunk/src/activate/main.c     Tue Oct 12 15:15:26 2010        
(r24238)
@@ -30,6 +30,7 @@
 #include <activationmapping.h>
 #include <distributionmapping.h>
 #include <defaultoptions.h>
+#include <client-interface.h>
 
 static void print_usage()
 {
@@ -77,46 +78,14 @@
        
        printf("\n");
 
-       status = fork();
-       
-       if(status == 0)
-       {
-           unsigned int i;
-           char **args = (char**)g_malloc((8 + 2 * arguments_size) * 
sizeof(gchar*));
-           
-           args[0] = interface;
-           args[1] = "--activate";
-           args[2] = "--type";
-           args[3] = actual_mapping->type;
-           args[4] = "--target";
-           args[5] = target_interface;
-           
-           for(i = 0; i < arguments_size * 2; i += 2)
-           {
-               args[i + 6] = "--arguments";
-               args[i + 7] = arguments[i / 2];
-           }
-           
-           args[i + 6] = actual_mapping->service;
-           args[i + 7] = NULL;
-           
-           execvp(interface, args);
-           _exit(1);       
-       }
+       status = wait_to_finish(exec_activate(interface, target_interface, 
actual_mapping->type, arguments, arguments_size, actual_mapping->service));
        
        g_strfreev(arguments);
        
-       if(status == -1)
+       if(status != 0)
            return FALSE;
-       else
-       {               
-           wait(&status);
-               
-           if(WEXITSTATUS(status) != 0)
-               return FALSE;
-       }
-       
-       actual_mapping->activated = TRUE;
+       else            
+           actual_mapping->activated = TRUE;
     }
     
     return TRUE;
@@ -155,46 +124,14 @@
        
        printf("\n");
        
-       status = fork();
-       
-       if(status == 0)
-       {
-           unsigned int i;
-           char **args = (char**)g_malloc((8 + 2 * arguments_size) * 
sizeof(gchar*));
-           
-           args[0] = interface;
-           args[1] = "--deactivate";
-           args[2] = "--type";
-           args[3] = actual_mapping->type;
-           args[4] = "--target";
-           args[5] = target_interface;
-           
-           for(i = 0; i < arguments_size * 2; i += 2)
-           {
-               args[i + 6] = "--arguments";
-               args[i + 7] = arguments[i / 2];
-           }
-           
-           args[i + 6] = actual_mapping->service;
-           args[i + 7] = NULL;
-           
-           execvp(interface, args);
-           _exit(1);
-       }
+       status = wait_to_finish(exec_deactivate(interface, target_interface, 
actual_mapping->type, arguments, arguments_size, actual_mapping->service));
                
        g_free(arguments);
 
-       if(status == -1)
+       if(status != 0)
            return FALSE;
-       else
-       {
-           wait(&status);
-       
-           if(WEXITSTATUS(status) != 0)
-               return FALSE;
-       }
-       
-       actual_mapping->activated = FALSE;
+       else    
+           actual_mapping->activated = FALSE;
     }
     
     return TRUE;
@@ -405,19 +342,14 @@
     for(i = 0; i < distribution_array->len; i++)
     {
        DistributionItem *item = g_array_index(distribution_array, 
DistributionItem*, i);
-       status = fork();
+       
+       status = exec_unlock(interface, item->target, profile);
        
        if(status == -1)
        {
            fprintf(stderr, "Error with forking unlock process!\n");
            exit_status = FALSE;
        }
-       else if(status == 0)
-       {
-           char *args[] = {interface, "--unlock", "--target", item->target, 
"--profile", profile, NULL};
-           execvp(interface, args);
-           _exit(1);
-       }
        else
            running_processes++;
     }
@@ -450,19 +382,14 @@
     for(i = 0; i < distribution_array->len; i++)
     {
        DistributionItem *item = g_array_index(distribution_array, 
DistributionItem*, i);
-       status = fork();
-
+       
+       status = exec_lock(interface, item->target, profile);
+       
        if(status == -1)
        {
            fprintf(stderr, "Error with forking lock process!\n");
            exit_status = FALSE;
        }       
-       else if(status == 0)
-       {
-           char *args[] = {interface, "--lock", "--target", item->target, 
"--profile", profile, NULL};
-           execvp(interface, args);
-           _exit(1);
-       }
        else
            g_array_append_val(try_array, item);
     }    

Modified: disnix/disnix/trunk/src/collect-garbage/Makefile.am
==============================================================================
--- disnix/disnix/trunk/src/collect-garbage/Makefile.am Tue Oct 12 08:09:57 
2010        (r24237)
+++ disnix/disnix/trunk/src/collect-garbage/Makefile.am Tue Oct 12 15:15:26 
2010        (r24238)
@@ -3,5 +3,5 @@
 bin_PROGRAMS = disnix-collect-garbage
 
 disnix_collect_garbage_SOURCES = main.c
-disnix_collect_garbage_LDADD = ../libinfrastructure/libinfrastructure.la 
../libmain/libmain.la
-disnix_collect_garbage_CFLAGS = $(LIBXML2_CFLAGS) $(LIBXSLT_CFLAGS) 
-I../libinfrastructure -I../libmain
+disnix_collect_garbage_LDADD = ../libinfrastructure/libinfrastructure.la 
../libmain/libmain.la ../libinterface/libinterface.la
+disnix_collect_garbage_CFLAGS = $(LIBXML2_CFLAGS) $(LIBXSLT_CFLAGS) 
-I../libinfrastructure -I../libmain -I../libinterface

Modified: disnix/disnix/trunk/src/collect-garbage/main.c
==============================================================================
--- disnix/disnix/trunk/src/collect-garbage/main.c      Tue Oct 12 08:09:57 
2010        (r24237)
+++ disnix/disnix/trunk/src/collect-garbage/main.c      Tue Oct 12 15:15:26 
2010        (r24238)
@@ -26,6 +26,7 @@
 #include <glib.h>
 #include <infrastructure.h>
 #include <defaultoptions.h>
+#include <client-interface.h>
 
 static void print_usage()
 {
@@ -47,7 +48,7 @@
        {0, 0, 0, 0}
     };
     char *interface = NULL;
-    char *delete_old_arg = "";
+    gboolean delete_old = FALSE;
     char *target_property = NULL;
     
     /* Parse command-line options */
@@ -62,7 +63,7 @@
                target_property = optarg;
                break;
            case 'd':
-               delete_old_arg = "-d";
+               delete_old = TRUE;
                break;
            case 'h':
                print_usage();
@@ -94,24 +95,15 @@
            for(i = 0; i < target_array->len; i++)
            {
                gchar *target = g_array_index(target_array, gchar*, i);
-               char *args[] = {interface, "--target", target, 
"--collect-garbage", delete_old_arg, NULL};
-               int status;
-               
+                               
                printf("Collecting garbage on: %s\n", target);
-               
-               status = fork();
+               status = exec_collect_garbage(interface, target, delete_old);   
        
                
                if(status == -1)
                {
                    fprintf(stderr, "Error forking garbage collection 
process!\n");
                    exit_status = -1;
                }
-               else if(status == 0)
-               {
-                   execvp(interface, args);
-                   fprintf(stderr, "Error starting garbage collection process 
on: %s\n", target);
-                   _exit(1);
-               }
                else
                    running_processes++;
            }

Added: disnix/disnix/trunk/src/libinterface/Makefile.am
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ disnix/disnix/trunk/src/libinterface/Makefile.am    Tue Oct 12 15:15:26 
2010        (r24238)
@@ -0,0 +1,6 @@
+AM_CPPFLAGS=-ggdb
+
+pkglib_LTLIBRARIES = libinterface.la
+noinst_HEADERS = client-interface.h
+
+libinterface_la_SOURCES = client-interface.c

Added: disnix/disnix/trunk/src/libinterface/client-interface.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ disnix/disnix/trunk/src/libinterface/client-interface.c     Tue Oct 12 
15:15:26 2010        (r24238)
@@ -0,0 +1,142 @@
+/*
+ * Disnix - A distributed application layer for Nix
+ * Copyright (C) 2008-2010  Sander van der Burg
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA
+ */
+
+#include <sys/wait.h>
+#include <sys/types.h>
+#include "client-interface.h"
+
+int wait_to_finish(pid_t pid)
+{
+    if(pid == -1)
+    {
+       g_printerr("Error with forking process!\n");
+       return -1;
+    }
+    else
+    {
+       int status;
+       wait(&status);
+       return WEXITSTATUS(status);
+    }    
+}
+
+static pid_t exec_activate_or_deactivate(gchar *operation, gchar *interface, 
gchar *target, gchar *type, gchar **arguments, unsigned int arguments_size, 
gchar *service)
+{
+    int pid = fork();
+       
+    if(pid == 0)
+    {
+       unsigned int i;
+       char **args = (char**)g_malloc((8 + 2 * arguments_size) * 
sizeof(gchar*));
+           
+       args[0] = interface;
+       args[1] = operation;
+       args[2] = "--target";
+       args[3] = target;
+       args[4] = "--type";
+       args[5] = type;
+           
+       for(i = 0; i < arguments_size * 2; i += 2)
+       {
+           args[i + 6] = "--arguments";
+           args[i + 7] = arguments[i / 2];
+        }
+           
+        args[i + 6] = service;
+        args[i + 7] = NULL;
+           
+        execvp(interface, args);
+        _exit(1);          
+    }
+    else
+       return pid;
+}
+
+pid_t exec_activate(gchar *interface, gchar *target, gchar *type, gchar 
**arguments, unsigned int arguments_size, gchar *service)
+{
+    return exec_activate_or_deactivate("--activate", interface, target, type, 
arguments, arguments_size, service);
+}
+
+pid_t exec_deactivate(gchar *interface, gchar *target, gchar *type, gchar 
**arguments, unsigned int arguments_size, gchar *service)
+{
+    return exec_activate_or_deactivate("--deactivate", interface, target, 
type, arguments, arguments_size, service);
+}
+
+static pid_t exec_lock_or_unlock(gchar *operation, gchar *interface, gchar 
*target, gchar *profile)
+{
+    int pid = fork();
+    
+    if(pid == 0)
+    {
+       char *args[] = {interface, operation, "--target", target, "--profile", 
profile, NULL};
+       execvp(interface, args);
+       _exit(1);
+    }
+    else
+       return pid;
+}
+
+pid_t exec_lock(gchar *interface, gchar *target, gchar *profile)
+{
+    return exec_lock_or_unlock("--lock", interface, target, profile);
+}
+
+pid_t exec_unlock(gchar *interface, gchar *target, gchar *profile)
+{
+    return exec_lock_or_unlock("--unlock", interface, target, profile);
+}
+
+pid_t exec_collect_garbage(gchar *interface, gchar *target, gboolean 
delete_old)
+{
+    /* Declarations */
+    int pid;
+    char *delete_old_arg;
+    
+    /* Determine whether to use the delete old option */
+    if(delete_old)
+       delete_old_arg = "-d";
+    else
+       delete_old_arg = NULL;
+
+    /* Fork and execute the collect garbage process */
+    pid = fork();
+    
+    if(pid == 0)
+    {  
+       char *args[] = {interface, "--target", target, "--collect-garbage", 
delete_old_arg, NULL};
+       execvp(interface, args);
+       _exit(1);
+    }
+    else
+       return pid;
+}
+
+pid_t exec_query_installed(gchar *interface, gchar *target, gchar *profile)
+{
+    int pid = fork();
+    
+    if(pid == 0)
+    {
+       char *args[] = {interface, "--target", target, "--profile", profile, 
"--query-installed", NULL};
+       execvp(interface, args);
+       _exit(1);
+    }
+    else
+       return pid;
+}

Added: disnix/disnix/trunk/src/libinterface/client-interface.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ disnix/disnix/trunk/src/libinterface/client-interface.h     Tue Oct 12 
15:15:26 2010        (r24238)
@@ -0,0 +1,100 @@
+/*
+ * Disnix - A distributed application layer for Nix
+ * Copyright (C) 2008-2010  Sander van der Burg
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA
+ */
+
+#ifndef __CLIENT_INTERFACE_H
+#define __CLIENT_INTERFACE_H
+
+#include <unistd.h>
+#include <glib.h>
+
+/**
+ * Waits until the given PID is finished and then returns the exit status
+ *
+ * @param pid PID of a process
+ * @return Non-zero value in case of a failure
+ */
+int wait_to_finish(pid_t pid);
+
+/**
+ * Invokes the activate operation through a Disnix client interface
+ *
+ * @param interface Path to the interface executable
+ * @param target Target Address of the remote interface
+ * @param type Type Type of the service
+ * @param arguments String vector with activation arguments in the form 
key=value
+ * @param arguments_size Size of the arguments string vector
+ * @param service Service to activate
+ * @return PID of the client interface process performing the operation, or -1 
in case of a failure
+ */
+pid_t exec_activate(gchar *interface, gchar *target, gchar *type, gchar 
**arguments, unsigned int arguments_size, gchar *service);
+
+/**
+ * Invokes the deactivate operation through a Disnix client interface
+ *
+ * @param interface Path to the interface executable
+ * @param target Target Address of the remote interface
+ * @param type Type Type of the service
+ * @param arguments String vector with activation arguments in the form 
key=value
+ * @param arguments_size Size of the arguments string vector
+ * @param service Service to deactivate
+ * @return PID of the client interface process performing the operation, or -1 
in case of a failure
+ */
+pid_t exec_deactivate(gchar *interface, gchar *target, gchar *type, gchar 
**arguments, unsigned int arguments_size, gchar *service);
+
+/**
+ * Invokes the lock operation through a Disnix client interface
+ *
+ * @param interface Path to the interface executable
+ * @param target Target Address of the remote interface
+ * @param profile Identifier of the distributed profile
+ * @return PID of the client interface process performing the operation, or -1 
in case of a failure
+ */
+pid_t exec_lock(gchar *interface, gchar *target, gchar *profile);
+
+/**
+ * Invokes the unlock operation through a Disnix client interface
+ *
+ * @param interface Path to the interface executable
+ * @param target Target Address of the remote interface
+ * @param profile Identifier of the distributed profile
+ * @return PID of the client interface process performing the operation, or -1 
in case of a failure
+ */
+pid_t exec_unlock(gchar *interface, gchar *target, gchar *profile);
+
+/**
+ * Invokes the collect garbage operation through a Disnix client interface
+ *
+ * @param interface Path to the interface executable
+ * @param target Target Address of the remote interface
+ * @param delete_old Indicates whether old profile generations must be removed
+ * @return PID of the client interface process performing the operation, or -1 
in case of a failure
+ */
+pid_t exec_collect_garbage(gchar *interface, gchar *target, gboolean 
delete_old);
+
+/**
+ * Queries the installed services operation through a Disnix client interface
+ *
+ * @param interface Path to the interface executable
+ * @param target Target Address of the remote interface
+ * @param profile Identifier of the distributed profile
+ * @return PID of the client interface process performing the operation, or -1 
in case of a failure
+ */
+pid_t exec_query_installed(gchar *interface, gchar *target, gchar *profile);
+
+#endif

Modified: disnix/disnix/trunk/src/query/Makefile.am
==============================================================================
--- disnix/disnix/trunk/src/query/Makefile.am   Tue Oct 12 08:09:57 2010        
(r24237)
+++ disnix/disnix/trunk/src/query/Makefile.am   Tue Oct 12 15:15:26 2010        
(r24238)
@@ -3,5 +3,5 @@
 bin_PROGRAMS = disnix-query
 
 disnix_query_SOURCES = main.c
-disnix_query_LDADD = ../libinfrastructure/libinfrastructure.la 
../libmain/libmain.la
-disnix_query_CFLAGS = -I../libinfrastructure -I../libmain
+disnix_query_LDADD = ../libinfrastructure/libinfrastructure.la 
../libmain/libmain.la ../libinterface/libinterface.la
+disnix_query_CFLAGS = -I../libinfrastructure -I../libmain -I../libinterface

Modified: disnix/disnix/trunk/src/query/main.c
==============================================================================
--- disnix/disnix/trunk/src/query/main.c        Tue Oct 12 08:09:57 2010        
(r24237)
+++ disnix/disnix/trunk/src/query/main.c        Tue Oct 12 15:15:26 2010        
(r24238)
@@ -27,6 +27,7 @@
 #include <glib.h>
 #include <infrastructure.h>
 #include <defaultoptions.h>
+#include <client-interface.h>
 
 static void print_usage()
 {
@@ -50,6 +51,7 @@
     char *interface = NULL;
     char *target_property = NULL;
     char *profile = NULL;
+    int exit_status = 0;
     
     /* Parse command-line options */
     while((c = getopt_long(argc, argv, "p:h", long_options, &option_index)) != 
-1)
@@ -97,37 +99,15 @@
                
                printf("\nServices on: %s\n\n", target);
                
-               status = fork();
-               
-               if(status == 0)
-               {
-                   char *args[] = {interface, "--target", target, "--profile", 
profile, "--query-installed", NULL};
-                   execvp(interface, args);
-                   fprintf(stderr, "Error with executing query process!\n");
-                   _exit(1);
-               }
-               
-               if(status == -1)
-               {
-                   fprintf(stderr, "Error with forking query process!\n");
-                   delete_target_array(target_array);
-                   return -1;
-               }
-               else
-               {               
-                   wait(&status);
-                               
-                   if(WEXITSTATUS(status) != 0)
-                   {
-                       delete_target_array(target_array);
-                       return WEXITSTATUS(status);
-                   }
-               }
+               status = wait_to_finish(exec_query_installed(interface, target, 
profile));
+                                               
+               if(status != 0)
+                   exit_status = status;
            }
            
            delete_target_array(target_array);
        }
 
-       return 0;
+       return exit_status;
     }
 }
_______________________________________________
nix-commits mailing list
[email protected]
http://mail.cs.uu.nl/mailman/listinfo/nix-commits

Reply via email to