Sfoglia il codice sorgente

Make modstatdb locking functions not take an admindir argument

Initialize the lockfile on modstatdb_init() via the fnis array. Make
sure we call modstatdb_init() and modstatdb_done() in case we are not
calling modstatdb_open() and modstatdb_shutdown().
Guillem Jover 15 anni fa
parent
commit
6c9093777f
4 ha cambiato i file con 15 aggiunte e 20 eliminazioni
  1. 7 16
      lib/dpkg/dbmodify.c
  2. 2 2
      lib/dpkg/dpkg-db.h
  3. 1 1
      src/enquiry.c
  4. 5 1
      src/update.c

+ 7 - 16
lib/dpkg/dbmodify.c

@@ -49,6 +49,7 @@
 static bool db_initialized;
 static bool db_initialized;
 
 
 static enum modstatdb_rw cstatus=-1, cflags=0;
 static enum modstatdb_rw cstatus=-1, cflags=0;
+static char *lockfile;
 static char *statusfile, *availablefile;
 static char *statusfile, *availablefile;
 static char *importanttmpfile=NULL;
 static char *importanttmpfile=NULL;
 static FILE *importanttmp;
 static FILE *importanttmp;
@@ -136,6 +137,7 @@ static const struct fni {
   const char *suffix;
   const char *suffix;
   char **store;
   char **store;
 } fnis[] = {
 } fnis[] = {
+  {   LOCKFILE,                   &lockfile           },
   {   STATUSFILE,                 &statusfile         },
   {   STATUSFILE,                 &statusfile         },
   {   AVAILFILE,                  &availablefile      },
   {   AVAILFILE,                  &availablefile      },
   {   UPDATESDIR,                 &updatesdir         },
   {   UPDATESDIR,                 &updatesdir         },
@@ -184,14 +186,11 @@ modstatdb_done(void)
 static int dblockfd = -1;
 static int dblockfd = -1;
 
 
 bool
 bool
-modstatdb_is_locked(const char *admindir)
+modstatdb_is_locked(void)
 {
 {
-  char *lockfile;
   int lockfd;
   int lockfd;
   bool locked;
   bool locked;
 
 
-  m_asprintf(&lockfile, "%s/%s", admindir, LOCKFILE);
-
   if (dblockfd == -1) {
   if (dblockfd == -1) {
     lockfd = open(lockfile, O_RDONLY);
     lockfd = open(lockfile, O_RDONLY);
     if (lockfd == -1)
     if (lockfd == -1)
@@ -207,20 +206,14 @@ modstatdb_is_locked(const char *admindir)
   if (dblockfd == -1)
   if (dblockfd == -1)
     close(lockfd);
     close(lockfd);
 
 
-  free(lockfile);
-
   return locked;
   return locked;
 }
 }
 
 
 void
 void
-modstatdb_lock(const char *admindir)
+modstatdb_lock(void)
 {
 {
-  char *dblockfile = NULL;
-
-  m_asprintf(&dblockfile, "%s/%s", admindir, LOCKFILE);
-
   if (dblockfd == -1) {
   if (dblockfd == -1) {
-    dblockfd = open(dblockfile, O_RDWR | O_CREAT | O_TRUNC, 0660);
+    dblockfd = open(lockfile, O_RDWR | O_CREAT | O_TRUNC, 0660);
     if (dblockfd == -1) {
     if (dblockfd == -1) {
       if (errno == EPERM)
       if (errno == EPERM)
         ohshit(_("you do not have permission to lock the dpkg status database"));
         ohshit(_("you do not have permission to lock the dpkg status database"));
@@ -228,9 +221,7 @@ modstatdb_lock(const char *admindir)
     }
     }
   }
   }
 
 
-  file_lock(&dblockfd, FILE_LOCK_NOWAIT, dblockfile, _("dpkg status database"));
-
-  free(dblockfile);
+  file_lock(&dblockfd, FILE_LOCK_NOWAIT, lockfile, _("dpkg status database"));
 }
 }
 
 
 void
 void
@@ -262,7 +253,7 @@ modstatdb_open(const char *admindir, enum modstatdb_rw readwritereq)
         ohshit(_("operation requires read/write access to dpkg status area"));
         ohshit(_("operation requires read/write access to dpkg status area"));
       cstatus= msdbrw_readonly;
       cstatus= msdbrw_readonly;
     } else {
     } else {
-      modstatdb_lock(admindir);
+      modstatdb_lock();
       cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
       cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
                 msdbrw_needsuperuserlockonly :
                 msdbrw_needsuperuserlockonly :
                 msdbrw_write);
                 msdbrw_write);

+ 2 - 2
lib/dpkg/dpkg-db.h

@@ -224,8 +224,8 @@ enum modstatdb_rw {
 
 
 void modstatdb_init(const char *admindir);
 void modstatdb_init(const char *admindir);
 void modstatdb_done(void);
 void modstatdb_done(void);
-bool modstatdb_is_locked(const char *admindir);
-void modstatdb_lock(const char *admindir);
+bool modstatdb_is_locked(void);
+void modstatdb_lock(void);
 void modstatdb_unlock(void);
 void modstatdb_unlock(void);
 enum modstatdb_rw modstatdb_open(const char *admindir, enum modstatdb_rw reqrwflags);
 enum modstatdb_rw modstatdb_open(const char *admindir, enum modstatdb_rw reqrwflags);
 void modstatdb_note(struct pkginfo *pkg);
 void modstatdb_note(struct pkginfo *pkg);

+ 1 - 1
src/enquiry.c

@@ -145,7 +145,7 @@ void audit(const char *const *argv) {
     while ((pkg = pkg_db_iter_next(it))) {
     while ((pkg = pkg_db_iter_next(it))) {
       if (!bsi->yesno(pkg,bsi)) continue;
       if (!bsi->yesno(pkg,bsi)) continue;
       if (!head_running) {
       if (!head_running) {
-        if (modstatdb_is_locked(admindir))
+        if (modstatdb_is_locked())
           puts(_(
           puts(_(
 "Another process has locked the database for writing, and might currently be\n"
 "Another process has locked the database for writing, and might currently be\n"
 "modifying it, some of the following problems might just be due to that.\n"));
 "modifying it, some of the following problems might just be due to that.\n"));

+ 5 - 1
src/update.c

@@ -39,6 +39,8 @@ void updateavailable(const char *const *argv) {
   int count= 0;
   int count= 0;
   static struct varbuf vb;
   static struct varbuf vb;
 
 
+  modstatdb_init(admindir);
+
   switch (cipaction->arg_int) {
   switch (cipaction->arg_int) {
   case act_avclear:
   case act_avclear:
     if (sourcefile) badusage(_("--%s takes no arguments"),cipaction->olong);
     if (sourcefile) badusage(_("--%s takes no arguments"),cipaction->olong);
@@ -58,7 +60,7 @@ void updateavailable(const char *const *argv) {
       else
       else
         ohshit(_("bulk available update requires write access to dpkg status area"));
         ohshit(_("bulk available update requires write access to dpkg status area"));
     }
     }
-    modstatdb_lock(admindir);
+    modstatdb_lock();
   }
   }
 
 
   switch (cipaction->arg_int) {
   switch (cipaction->arg_int) {
@@ -95,6 +97,8 @@ void updateavailable(const char *const *argv) {
   if (cipaction->arg_int != act_avclear)
   if (cipaction->arg_int != act_avclear)
     printf(P_("Information about %d package was updated.\n",
     printf(P_("Information about %d package was updated.\n",
               "Information about %d packages was updated.\n", count), count);
               "Information about %d packages was updated.\n", count), count);
+
+  modstatdb_done();
 }
 }
 
 
 void forgetold(const char *const *argv) {
 void forgetold(const char *const *argv) {