opkg: Consolidate error reporting from opkg_conf_init and ensure return value is
authorticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Mon, 15 Dec 2008 05:22:06 +0000 (05:22 +0000)
committerticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Mon, 15 Dec 2008 05:22:06 +0000 (05:22 +0000)
      checked in the appropriate places.

opkg: Add a locking mechanism to prevent two instances of opkg being run at the
      same time.

git-svn-id: http://opkg.googlecode.com/svn/trunk@130 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358

libopkg/libopkg.c
libopkg/opkg.c
libopkg/opkg_cmd.c
libopkg/opkg_cmd.h
libopkg/opkg_conf.c
libopkg/opkg_conf.h
libopkg/opkg_error.h

index dba19838f47862e552c6337ac3a15aa7374bbdb8..68da96d355a61184c05f590f71dcb10c7954de16 100644 (file)
@@ -271,6 +271,7 @@ opkg_packages_download (args_t * args, const char *name)
        err = opkg_conf_init (&opkg_conf, args);
        if (err)
        {
        err = opkg_conf_init (&opkg_conf, args);
        if (err)
        {
+               opkg_print_error_list (&opkg_conf);
                return err;
        }
 
                return err;
        }
 
@@ -485,6 +486,7 @@ opkg_op (int argc, char *argv[])
        err = opkg_conf_init (&opkg_conf, &args);
        if (err)
        {
        err = opkg_conf_init (&opkg_conf, &args);
        if (err)
        {
+               opkg_print_error_list (&opkg_conf);
                return err;
        }
 
                return err;
        }
 
index 038469e8f42309de256f1b9fd074af32b1b2cae4..9f081cf3937ad5b953d0989d98fd96e9e4a286c7 100644 (file)
@@ -185,13 +185,28 @@ opkg_t *
 opkg_new ()
 {
   opkg_t *opkg;
 opkg_new ()
 {
   opkg_t *opkg;
+  int err;
+
   opkg = malloc (sizeof (opkg_t));
 
   opkg->args = malloc (sizeof (args_t));
   opkg = malloc (sizeof (opkg_t));
 
   opkg->args = malloc (sizeof (args_t));
-  args_init (opkg->args);
+  err = args_init (opkg->args);
+  if (err)
+  {
+    free (opkg->args);
+    free (opkg);
+    return NULL;
+  }
 
   opkg->conf = malloc (sizeof (opkg_conf_t));
 
   opkg->conf = malloc (sizeof (opkg_conf_t));
-  opkg_conf_init (opkg->conf, opkg->args);
+  err = opkg_conf_init (opkg->conf, opkg->args);
+  if (err)
+  {
+    free (opkg->conf);
+    free (opkg->args);
+    free (opkg);
+    return NULL;
+  }
 
   opkg_init_options_array (opkg->conf, &opkg->options);
   return opkg;
 
   opkg_init_options_array (opkg->conf, &opkg->options);
   return opkg;
index 94cfb9987c4a52567163c0879a01900294054a10..e91a24c7357947b50f0884ad5d1f25dae3dc8323 100644 (file)
@@ -138,6 +138,23 @@ opkg_cmd_t *opkg_cmd_find(const char *name)
      return NULL;
 }
 
      return NULL;
 }
 
+void opkg_print_error_list (opkg_conf_t *conf)
+{
+  if ( error_list ) {
+     reverse_error_list(&error_list);
+
+     printf ("Collected errors:\n");
+     /* Here we print the errors collected and free the list */
+     while (error_list != NULL) {
+           printf (" * %s", error_list->errmsg);
+           error_list = error_list->next;
+
+     }
+     free_error_list(&error_list);
+  }
+
+}
+
 int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **argv, void *userdata)
 {
        int result;
 int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **argv, void *userdata)
 {
        int result;
@@ -150,20 +167,8 @@ int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **arg
            opkg_message(conf, OPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
         }
 
            opkg_message(conf, OPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
         }
 
-        if ( error_list ) {
-           reverse_error_list(&error_list);
-
-           opkg_message(conf, OPKG_NOTICE, "Collected errors:\n");
-           /* Here we print the errors collected and free the list */
-           while (error_list != NULL) {
-                 opkg_message(conf, OPKG_NOTICE, " * %s", error_list->errmsg);
-                 error_list = error_list->next;
+        opkg_print_error_list (conf);
 
 
-           }
-           free_error_list(&error_list);
-
-        }
-   
        p_userdata = NULL;
        return result;
 }
        p_userdata = NULL;
        return result;
 }
index b6496c86dc1fdac3acf82551095ea803260d37ef..43fc42887a8a874a1c8ca5b7273cce867e528c03 100644 (file)
@@ -38,5 +38,6 @@ int opkg_install_wanted_packages(opkg_conf_t *conf);
 int opkg_configure_packages(opkg_conf_t *conf, char *pkg_name);
 
 int pkg_mark_provides(pkg_t *pkg);
 int opkg_configure_packages(opkg_conf_t *conf, char *pkg_name);
 
 int pkg_mark_provides(pkg_t *pkg);
+void opkg_print_error_list (opkg_conf_t *conf);
 
 #endif
 
 #endif
index ba0daf5de1c58dfb4c43835c860edcd06a2a916f..965618f37a681b9e88193c4a9e57307db18dc750 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "includes.h"
 #include "opkg_conf.h"
 
 #include "includes.h"
 #include "opkg_conf.h"
+#include "opkg_error.h"
 
 #include "xregex.h"
 #include "sprintf_alloc.h"
 
 #include "xregex.h"
 #include "sprintf_alloc.h"
@@ -28,6 +29,9 @@
 #include <glob.h>
 #include "opkg_defines.h"
 
 #include <glob.h>
 #include "opkg_defines.h"
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
                                pkg_src_list_t *pkg_src_list,
 
 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
                                pkg_src_list_t *pkg_src_list,
@@ -100,10 +104,10 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
      int err;
      char *tmp_dir_base;
      nv_pair_list_t tmp_dest_nv_pair_list;
      int err;
      char *tmp_dir_base;
      nv_pair_list_t tmp_dest_nv_pair_list;
-     char * lists_dir =NULL;
+     char *lists_dir = NULL, *lock_file = NULL;
      glob_t globbuf;
      char *etc_opkg_conf_pattern = "/etc/opkg/*.conf";
      glob_t globbuf;
      char *etc_opkg_conf_pattern = "/etc/opkg/*.conf";
-     char *pending_dir  =NULL;
+     char *pending_dir NULL;
 
      memset(conf, 0, sizeof(opkg_conf_t));
 
 
      memset(conf, 0, sizeof(opkg_conf_t));
 
@@ -117,6 +121,23 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
      conf->restrict_to_default_dest = 0;
      conf->default_dest = NULL;
 
      conf->restrict_to_default_dest = 0;
      conf->default_dest = NULL;
 
+     /* check for lock file */
+     if (args->offline_root)
+       sprintf_alloc (&lock_file, "%s/%s/lock", args->offline_root, OPKG_STATE_DIR_PREFIX);
+     else
+       sprintf_alloc (&lock_file, "%s/lock", OPKG_STATE_DIR_PREFIX);
+
+     conf->lock_fd = creat (lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
+     err = lockf (conf->lock_fd, F_TLOCK, 0);
+
+     free (lock_file);
+
+     if (err)
+     {
+       opkg_message (conf, OPKG_ERROR, "Could not obtain administrative lock\n");
+       return OPKG_CONF_ERR_LOCK;
+     }
+
 
      if (args->tmp_dir)
          tmp_dir_base = args->tmp_dir;
 
      if (args->tmp_dir)
          tmp_dir_base = args->tmp_dir;
@@ -129,7 +150,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
      if (conf->tmp_dir == NULL) {
          fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
                  __FUNCTION__, conf->tmp_dir, strerror(errno));
      if (conf->tmp_dir == NULL) {
          fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
                  __FUNCTION__, conf->tmp_dir, strerror(errno));
-         return errno;
+         return OPKG_CONF_ERR_TMP_DIR;
      }
 
      conf->force_depends = 0;
      }
 
      conf->force_depends = 0;
@@ -165,19 +186,17 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
               if (opkg_conf_parse_file(conf, args->conf_file,
                                    &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
                    /* Memory leakage from opkg_conf_parse-file */
               if (opkg_conf_parse_file(conf, args->conf_file,
                                    &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
                    /* Memory leakage from opkg_conf_parse-file */
-                   return -1;
+                   return OPKG_CONF_ERR_PARSE;
                }
                }
-                   
      }
 
      }
 
-     /* if (!lists_dir ){*/
      if (strlen(lists_dir)<=1 ){
         lists_dir = realloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2);
         sprintf (lists_dir,"%s",OPKG_CONF_LISTS_DIR);
      }
 
      if (args->offline_root) {
      if (strlen(lists_dir)<=1 ){
         lists_dir = realloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2);
         sprintf (lists_dir,"%s",OPKG_CONF_LISTS_DIR);
      }
 
      if (args->offline_root) {
-            char *tmp;// = malloc(strlen(lists_dir) + strlen(args->offline_root) + 1);
+            char *tmp;
             sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
             free(lists_dir);
             lists_dir = tmp;
             sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
             free(lists_dir);
             lists_dir = tmp;
@@ -202,7 +221,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
                    if ( opkg_conf_parse_file(conf, globbuf.gl_pathv[i], 
                                         &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
                         /* Memory leakage from opkg_conf_parse-file */
                    if ( opkg_conf_parse_file(conf, globbuf.gl_pathv[i], 
                                         &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
                         /* Memory leakage from opkg_conf_parse-file */
-                        return -1;
+                        return OPKG_CONF_ERR_PARSE;
                    }
          }
      }
                    }
          }
      }
@@ -289,7 +308,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
         if (args->dest) {
             err = opkg_conf_set_default_dest(conf, args->dest);
             if (err) {
         if (args->dest) {
             err = opkg_conf_set_default_dest(conf, args->dest);
             if (err) {
-                 return err;
+                 return OPKG_CONF_ERR_DEFAULT_DEST;
             }
         }
      }
             }
         }
      }
index 6e202d46b0a73b8770614c5112b098bd26a9e5df..3c5dfe4f1bdbc18d526745f6ae07a3baac94f25e 100644 (file)
@@ -41,6 +41,7 @@ typedef struct opkg_conf opkg_conf_t;
 
 struct opkg_conf
 {
 
 struct opkg_conf
 {
+     int lock_fd; /* file descriptor for the lock file */
      pkg_src_list_t pkg_src_list;
      pkg_dest_list_t pkg_dest_list;
      nv_pair_list_t arch_list;
      pkg_src_list_t pkg_src_list;
      pkg_dest_list_t pkg_dest_list;
      nv_pair_list_t arch_list;
index b04bd0bf97c712051c8febe3bc19204b0dad7a79..94dbdeaa99edb00ea70ad39a263ac7d398542919 100644 (file)
@@ -20,6 +20,11 @@ enum opkg_error {
   OPKG_ERR_UNKNOWN = -1,
   OPKG_ERR_NONE = 0,
 
   OPKG_ERR_UNKNOWN = -1,
   OPKG_ERR_NONE = 0,
 
+  OPKG_CONF_ERR_DEFAULT_DEST,  /* could not set default dest */
+  OPKG_CONF_ERR_PARSE,         /* error parsing config file */
+  OPKG_CONF_ERR_TMP_DIR,       /* could not create temporary directory */
+  OPKG_CONF_ERR_LOCK,          /* could not get opkg lock */
+
   OPKG_PKG_DEPS_UNSATISFIED,
   OPKG_PKG_IS_ESSENTIAL,
   OPKG_PKG_HAS_DEPENDENTS,
   OPKG_PKG_DEPS_UNSATISFIED,
   OPKG_PKG_IS_ESSENTIAL,
   OPKG_PKG_HAS_DEPENDENTS,