]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
locking: use a more portable mechanism
authorBaptiste Daroussin <bapt@FreeBSD.org>
Fri, 2 Dec 2022 12:55:17 +0000 (13:55 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Fri, 2 Dec 2022 12:55:17 +0000 (13:55 +0100)
include/utils.h
src/incindexfile.c
src/log_oper.c
src/mlmmj-maintd.c
src/mlmmj-send.c
src/mlmmj-sub.c
src/mlmmj.c
src/utils.c

index f94d59ca090f207680f4203c43f674ad5cca8e60..f2d6894265f0acf521d9a9724c1a6259e1c4cc5d 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <sys/file.h>
 #include <stdint.h>
+#include <stdbool.h>
 
 #include <time.h>
 #include <fcntl.h>
@@ -35,36 +36,4 @@ uintmax_t strtouim(const char *, uintmax_t, uintmax_t, const char **);
 void exec_or_die(const char *arg, ...);
 int exec_and_wait(const char *arg, ...);
 time_t strtotimet(const char *np, const char **errpp);
-
-static inline int open_locked(const char *path, int flags, mode_t mode) {
-#ifdef O_EXLOCK
-       flags |= O_EXLOCK;
-       return (open(path, flags, mode));
-#else
-       int fd = open(path, flags, mode);
-       if (fd != -1) {
-               if (flock(fd, LOCK_EX) == -1) {
-                       close(fd);
-                       fd = -1;
-               }
-       }
-       return (fd);
-#endif
-}
-
-static inline int openat_locked(int dfd, const char *path, int flags, mode_t mode) {
-#ifdef O_EXLOCK
-       flags |= O_EXLOCK;
-       return (openat(dfd, path, flags, mode));
-#else
-       int fd = openat(dfd, path, flags, mode);
-       if (fd != -1) {
-               if (flock(fd, LOCK_EX) == -1) {
-                       close(fd);
-                       fd = -1;
-               }
-       }
-       return (fd);
-#endif
-}
-
+bool lock(int fd, bool write);
index 96d3798f9a2038fcc29a5a622f2decc69689114f..ac522021c2903dc6ab5beeaaf7c52cdc3b68d980 100644 (file)
@@ -49,8 +49,8 @@ int incindexfile(const char *listdir)
        const char *errstr;
 
        xasprintf(&indexfilename, "%s/index", listdir);
-       fd = open_locked(indexfilename, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
-       if(fd == -1) {
+       fd = open(indexfilename, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
+       if(fd == -1 && !lock(fd, true)) {
                free(indexfilename);
                log_error(LOG_ARGS, "Error opening index file");
                return 0;
index 1e27f79de4d9eec4df23f96203085a9d19b95865..82ab11dc4125d3a61523ba40249d253aed16c90f 100644 (file)
@@ -65,8 +65,8 @@ int log_oper(const char *prefix, const char *basename, const char *fmt, ...)
                free(tmp);
        }
        
-       fd = open_locked(logfilename, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
-       if(fd < 0) {
+       fd = open(logfilename, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
+       if(fd < 0 && !lock(fd, true)) {
                log_error(LOG_ARGS, "Could not open %s", logfilename);
                free(logfilename);
                return -1;
index 74481e22d7e310c81679d4376b33d24aaac6abdf..1ab7ad2bc832ed42023153bedf59b5d6aa1c7098 100644 (file)
@@ -602,8 +602,8 @@ run_digests(int dfd, const char *mlmmjsend, const char *listdir, int logfd)
        digestinterval = ctrltimet(dfd, "digestinterval", DIGESTINTERVAL);
        digestmaxmails = ctrllong(listdir, "digestmaxmail", DIGESTMAXMAILS);
 
-       fd = openat_locked(dfd, "lastdigest", O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
-       if (fd < 0) {
+       fd = openat(dfd, "lastdigest", O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
+       if (fd < 0 && !lock(fd, true)) {
                log(" - Could not open or create 'lastdigest': %s\n",
                    strerror(errno));
                return (false);
index 93488c7f298e9fc15a765a63ab77e4ad1538aaca..dd8d0af44e39ed7d5bc87f59ad677ad430d372fd 100644 (file)
@@ -919,7 +919,8 @@ int main(int argc, char **argv)
        
        /* initialize file with mail to send */
 
-       if((mailfd = open_locked(mailfilename, O_RDWR, 0644)) < 0) {
+       if((mailfd = open(mailfilename, O_RDWR, 0644)) < 0 &&
+           !lock(mailfd, true)) {
                log_error(LOG_ARGS, "Could not open '%s'", mailfilename);
                exit(EXIT_FAILURE);
        }
index 4c24c379bca1793278732748c9efbe0463f7662e..b72bb5e90513c2fc0dc7b467d16d0ec47f564568 100644 (file)
@@ -569,9 +569,9 @@ static void subscribe_type(char *listdir, char *listaddr, char *listdelim,
        
        subfilename = concatstr(3, listdir, subdir, chstr);
 
-       subfilefd = open_locked(subfilename, O_RDWR|O_CREAT|O_APPEND,
+       subfilefd = open(subfilename, O_RDWR|O_CREAT|O_APPEND,
                                S_IRUSR|S_IWUSR|groupwritable);
-       if(subfilefd == -1) {
+       if(subfilefd == -1 && !lock(subfilefd, true)) {
                log_error(LOG_ARGS, "Could not open '%s'", subfilename);
                exit(EXIT_FAILURE);
        }
index 2100f26bbd0f1858633412fd23e550401947e537..c9d7697412b3afd4a3a3d22aba304887249f496e 100644 (file)
@@ -187,9 +187,11 @@ unsubscribe(int listfd, const char *address, enum subtype typesub)
                    strcmp(dp->d_name, "..") == 0)
                        continue;
 
-               rfd = openat_locked(fd, dp->d_name, O_RDONLY, 0644);
+               rfd = openat(fd, dp->d_name, O_RDONLY, 0644);
                if (rfd == -1)
                        continue;
+               if (!lock(rfd, false))
+                       continue;
 
                if (fstat(rfd, &st) == -1) {
                        log_err("Impossible to determine the size of %s/%s: %s",
@@ -255,9 +257,9 @@ unsubscribe(int listfd, const char *address, enum subtype typesub)
                }
 
                xasprintf(&wrname, "%s.new", dp->d_name);
-               wfd = openat_locked(fd, wrname, O_RDWR|O_CREAT|O_TRUNC,
+               wfd = openat(fd, wrname, O_RDWR|O_CREAT|O_TRUNC,
                    S_IRUSR|S_IWUSR|groupwritable);
-               if (wfd == -1) {
+               if (wfd == -1 && !lock(wfd, true)) {
                        log_err("Could not open '%s/%s': %s", subdir, wrname,
                            strerror(errno));
                        munmap(start, st.st_size);
index cab6f7bb5cfc87b88487ce20f8b7e84f8c35757a..6b0677a461fb8339f6752814bfc44c1117f09d4a 100644 (file)
@@ -20,6 +20,7 @@
  * IN THE SOFTWARE.
  */
 
+#include <fcntl.h>
 #include <sys/wait.h>
 #include <sys/param.h>
 
@@ -28,6 +29,7 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <spawn.h>
+#include <stdbool.h>
 #include <unistd.h>
 
 #include "xmalloc.h"
@@ -179,3 +181,25 @@ exec_and_wait(const char *arg, ...)
 
        return (WEXITSTATUS(pstat));
 }
+
+bool
+lock(int fd, bool wr)
+{
+       struct flock lck;
+       int r;
+
+       if (fd == -1)
+               return (false);
+       lck.l_type = wr ? F_WRLCK : F_RDLCK;
+       lck.l_whence = SEEK_SET;
+       lck.l_start = 0;
+       lck.l_len = 0;
+       do {
+               r = fcntl(fd, F_SETLKW, &lck);
+       } while (r < 0 && errno != EINTR);
+       if (r == -1) {
+               close(fd);
+               return (false);
+       }
+       return (true);
+}