improvements to alias file locking code
authorEric Allman <eric@ucbvax.Berkeley.EDU>
Fri, 3 Sep 1993 08:19:16 +0000 (00:19 -0800)
committerEric Allman <eric@ucbvax.Berkeley.EDU>
Fri, 3 Sep 1993 08:19:16 +0000 (00:19 -0800)
SCCS-vsn: usr.sbin/sendmail/src/alias.c 8.14
SCCS-vsn: usr.sbin/sendmail/src/map.c 8.12

usr/src/usr.sbin/sendmail/src/alias.c
usr/src/usr.sbin/sendmail/src/map.c

index f25b704..30d9547 100644 (file)
@@ -10,7 +10,7 @@
 # include <pwd.h>
 
 #ifndef lint
 # include <pwd.h>
 
 #ifndef lint
-static char sccsid[] = "@(#)alias.c    8.13 (Berkeley) %G%";
+static char sccsid[] = "@(#)alias.c    8.14 (Berkeley) %G%";
 #endif /* not lint */
 
 
 #endif /* not lint */
 
 
@@ -347,6 +347,7 @@ rebuildaliases(map, automatic)
        bool automatic;
 {
        FILE *af;
        bool automatic;
 {
        FILE *af;
+       bool nolock = FALSE;
        void (*oldsigint)();
 
        if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
        void (*oldsigint)();
 
        if (!bitset(MCF_REBUILDABLE, map->map_class->map_cflags))
@@ -355,20 +356,28 @@ rebuildaliases(map, automatic)
        /* try to lock the source file */
        if ((af = fopen(map->map_file, "r+")) == NULL)
        {
        /* try to lock the source file */
        if ((af = fopen(map->map_file, "r+")) == NULL)
        {
-               int saveerr = errno;
+               if (errno != EACCES || automatic ||
+                   (af = fopen(map->map_file, "r")) == NULL)
+               {
+                       int saveerr = errno;
 
 
-               if (tTd(27, 1))
-                       printf("Can't open %s: %s\n",
-                               map->map_file, errstring(saveerr));
-               if (!automatic)
-                       message("newaliases: cannot open %s: %s",
-                               map->map_file, errstring(saveerr));
-               errno = 0;
-               return;
+                       if (tTd(27, 1))
+                               printf("Can't open %s: %s\n",
+                                       map->map_file, errstring(saveerr));
+                       if (!automatic)
+                               message("newaliases: cannot open %s: %s",
+                                       map->map_file, errstring(saveerr));
+                       errno = 0;
+                       return;
+               }
+               nolock = TRUE;
+               message("warning: cannot lock %s: %s",
+                       map->map_file, errstring(errno));
        }
 
        /* see if someone else is rebuilding the alias file */
        }
 
        /* see if someone else is rebuilding the alias file */
-       if (!lockfile(fileno(af), map->map_file, NULL, LOCK_EX|LOCK_NB))
+       if (!nolock &&
+           !lockfile(fileno(af), map->map_file, NULL, LOCK_EX|LOCK_NB))
        {
                /* yes, they are -- wait until done */
                message("Alias file %s is already being rebuilt",
        {
                /* yes, they are -- wait until done */
                message("Alias file %s is already being rebuilt",
index d785ad8..5b2fc95 100644 (file)
@@ -7,7 +7,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)map.c      8.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)map.c      8.12 (Berkeley) %G%";
 #endif /* not lint */
 
 #include "sendmail.h"
 #endif /* not lint */
 
 #include "sendmail.h"
@@ -427,7 +427,7 @@ ndbm_map_lookup(map, name, av, statp)
                key.dptr = keybuf;
        }
        fd = dbm_dirfno((DBM *) map->map_db1);
                key.dptr = keybuf;
        }
        fd = dbm_dirfno((DBM *) map->map_db1);
-       if (fd >= 0)
+       if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags))
                (void) lockfile(fd, map->map_file, ".dir", LOCK_SH);
        val.dptr = NULL;
        if (bitset(MF_TRY0NULL, map->map_mflags))
                (void) lockfile(fd, map->map_file, ".dir", LOCK_SH);
        val.dptr = NULL;
        if (bitset(MF_TRY0NULL, map->map_mflags))
@@ -443,7 +443,7 @@ ndbm_map_lookup(map, name, av, statp)
                if (val.dptr != NULL)
                        map->map_mflags &= ~MF_TRY0NULL;
        }
                if (val.dptr != NULL)
                        map->map_mflags &= ~MF_TRY0NULL;
        }
-       if (fd >= 0)
+       if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags))
                (void) lockfile(fd, map->map_file, ".dir", LOCK_UN);
        if (val.dptr == NULL)
                return NULL;
                (void) lockfile(fd, map->map_file, ".dir", LOCK_UN);
        if (val.dptr == NULL)
                return NULL;
@@ -591,10 +591,15 @@ bt_map_open(map, mode)
        fd = db->fd(db);
 # if !defined(O_EXLOCK)
        if (mode == O_RDWR && fd >= 0)
        fd = db->fd(db);
 # if !defined(O_EXLOCK)
        if (mode == O_RDWR && fd >= 0)
-               (void) lockfile(fd, map->map_file, ".db", LOCK_EX);
+       {
+               if (lockfile(fd, map->map_file, ".db", LOCK_EX))
+                       map->map_mflags |= MF_LOCKED;
+       }
 # else
        if (mode == O_RDONLY && fd >= 0)
                (void) lockfile(fd, map->map_file, ".db", LOCK_UN);
 # else
        if (mode == O_RDONLY && fd >= 0)
                (void) lockfile(fd, map->map_file, ".db", LOCK_UN);
+       else
+               map->map_mflags |= MF_LOCKED;
 # endif
 #endif
 
 # endif
 #endif
 
@@ -665,10 +670,15 @@ hash_map_open(map, mode)
        fd = db->fd(db);
 # if !defined(O_EXLOCK)
        if (mode == O_RDWR && fd >= 0)
        fd = db->fd(db);
 # if !defined(O_EXLOCK)
        if (mode == O_RDWR && fd >= 0)
-               (void) lockfile(fd, map->map_file, ".db", LOCK_EX);
+       {
+               if (lockfile(fd, map->map_file, ".db", LOCK_EX))
+                       map->map_mflags |= MF_LOCKED;
+       }
 # else
        if (mode == O_RDONLY && fd >= 0)
                (void) lockfile(fd, map->map_file, ".db", LOCK_UN);
 # else
        if (mode == O_RDONLY && fd >= 0)
                (void) lockfile(fd, map->map_file, ".db", LOCK_UN);
+       else
+               map->map_mflags |= MF_LOCKED;
 # endif
 #endif
 
 # endif
 #endif
 
@@ -719,7 +729,9 @@ db_map_lookup(map, name, av, statp)
        if (!bitset(MF_NOFOLDCASE, map->map_mflags))
                makelower(keybuf);
 #ifndef OLD_NEWDB
        if (!bitset(MF_NOFOLDCASE, map->map_mflags))
                makelower(keybuf);
 #ifndef OLD_NEWDB
-       (void) lockfile(db->fd(db), map->map_file, ".db", LOCK_SH);
+       fd = db->fd(db);
+       if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags))
+               (void) lockfile(db->fd(db), map->map_file, ".db", LOCK_SH);
 #endif
        st = 1;
        if (bitset(MF_TRY0NULL, map->map_mflags))
 #endif
        st = 1;
        if (bitset(MF_TRY0NULL, map->map_mflags))
@@ -737,8 +749,7 @@ db_map_lookup(map, name, av, statp)
        }
        saveerrno = errno;
 #ifndef OLD_NEWDB
        }
        saveerrno = errno;
 #ifndef OLD_NEWDB
-       fd = db->fd(db);
-       if (fd >= 0)
+       if (fd >= 0 && !bitset(MF_LOCKED, map->map_mflags))
                (void) lockfile(fd, map->map_file, ".db", LOCK_UN);
 #endif
        if (st != 0)
                (void) lockfile(fd, map->map_file, ".db", LOCK_UN);
 #endif
        if (st != 0)