]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
pop3d: support fcntl locks on NetBSD and OpenBSD
authorEric Wong <e@80x24.org>
Sat, 9 Sep 2023 01:48:38 +0000 (01:48 +0000)
committerEric Wong <e@80x24.org>
Sat, 9 Sep 2023 11:54:31 +0000 (11:54 +0000)
MboxLock already supported it since it locked the whole file,
but POP3D requires more fine-grained locking at file offsets.

I wonder if "struct flock" is old enough for it to be the same
across all the BSDs, it certainly seems so.

I originally considered using C11 `_Generic' support for the
struct offset/type dumping as I have in other projects, but
I am not ready to depend on C11 for this project, yet.

While we're modifying devel/sysdefs-list, add some Linux-only
structs to verify our `pack' templates are correct and remain
so when we encounter new architectures.

devel/sysdefs-list
lib/PublicInbox/POP3D.pm
t/pop3d.t

index 9764cc29c01de681eacc4636867a673404e034b8..aa7806ae1d1e9791dac1f225eb2fa77cb32d95e7 100755 (executable)
@@ -27,25 +27,57 @@ open my $fh, '>', $f or die "open $f $!";
 print $fh $str or die "print $f $!";
 close $fh or die "close $f $!";
 system($cc, '-o', $x, $f, @cflags) == 0 or die "$cc failed \$?=$?";
+say '%Config', map { " $_=$Config{$_}" } qw(ptrsize sizesize lseeksize);
 exec($x);
 __DATA__
 #ifndef _GNU_SOURCE
 #  define _GNU_SOURCE
 #endif
+#include <assert.h>
 #include <signal.h>
+#include <stddef.h>
 #include <sys/syscall.h>
 #include <sys/ioctl.h>
 #ifdef __linux__
-#include <linux/fs.h>
+#      include <linux/fs.h>
+#      include <sys/epoll.h>
+#      include <sys/inotify.h>
+#      include <sys/socket.h> // we don't care about this on *BSD
+#      include <sys/vfs.h>
 #endif
 #include <sys/types.h>
+#include <fcntl.h>
 #include <unistd.h>
 #include <stdio.h>
 
+#define STRUCT_BEGIN(t) do { t x; printf(#t" => %zu bytes\n", sizeof(x))
+#define STRUCT_END } while (0)
+
+// prints the struct field name, @offset, and signed/unsigned bit size
+#define PR_NUM(f) do { \
+       x.f = ~0; \
+       printf("\t.%s @%zu %c%zu\n", #f, \
+               offsetof(typeof(x),f), \
+               x.f > 0 ? 'u' : 's', \
+               sizeof(x.f) * 8); \
+} while (0)
+
+#define PR_PTR(f) do { \
+       assert(sizeof(x.f) == sizeof(void *)); \
+       printf("\t.%s @%zu ptr\n", #f, offsetof(typeof(x),f)); \
+} while (0)
+
+#define PR_OFF(f) do { \
+       printf("\t.%s @%zu\n", #f, offsetof(typeof(x),f)); \
+} while (0)
+
 #define D(x) printf("$" #x " = %ld;\n", (long)x)
 
 int main(void)
 {
+       // verify Config{(ptr|size|lseek)size} entries match:
+       printf("sizeof ptr=%zu size_t=%zu off_t=%zu\n",
+               sizeof(void *), sizeof(size_t), sizeof(off_t));
 #ifdef __linux__
        D(SYS_epoll_create1);
        D(SYS_epoll_ctl);
@@ -64,11 +96,54 @@ int main(void)
                (unsigned long)FS_IOC_GETFLAGS, (unsigned long)FS_IOC_SETFLAGS);
 #endif
        MAYBE SYS_renameat2
+
+       STRUCT_BEGIN(struct epoll_event);
+               PR_NUM(events);
+               PR_NUM(data.u64);
+       STRUCT_END;
+
+       STRUCT_BEGIN(struct inotify_event);
+               PR_NUM(wd);
+               PR_NUM(mask);
+               PR_NUM(cookie);
+               PR_NUM(len);
+               PR_OFF(name);
+       STRUCT_END;
+
+       /*
+        * msghdr and cmsghdr are portable, but we only care about its layout
+        * on OSes like Linux with stable syscall numbers
+        */
+       STRUCT_BEGIN(struct msghdr);
+               PR_PTR(msg_name);
+               PR_NUM(msg_namelen);
+               PR_PTR(msg_iov);
+               PR_NUM(msg_iovlen);
+               PR_PTR(msg_control);
+               PR_NUM(msg_controllen);
+               PR_NUM(msg_flags);
+       STRUCT_END;
+
+       STRUCT_BEGIN(struct cmsghdr);
+               PR_NUM(cmsg_len);
+               PR_NUM(cmsg_level);
+               PR_NUM(cmsg_type);
+       STRUCT_END;
+
+       STRUCT_BEGIN(struct statfs);
+               PR_NUM(f_type);
+       STRUCT_END;
 #endif /* Linux, any other OSes with stable syscalls? */
-       printf("size_t=%zu off_t=%zu pid_t=%zu\n",
-                sizeof(size_t), sizeof(off_t), sizeof(pid_t));
        D(SIGWINCH);
        MAYBE _SC_NPROCESSORS_ONLN
 
+       STRUCT_BEGIN(struct flock);
+               PR_NUM(l_start);
+               PR_NUM(l_len);
+               PR_NUM(l_pid);
+               PR_NUM(l_type);
+               PR_NUM(l_whence);
+       STRUCT_END;
+
        return 0;
 }
index 3fc85efc904f896d42ce341fcca7b729804191a6..580a26d322cebfad2790625d00c09df3f40b500d 100644 (file)
@@ -14,6 +14,7 @@ use PublicInbox::Syscall;
 use File::Temp 0.19 (); # 0.19 for ->newdir
 use Fcntl qw(F_SETLK F_UNLCK F_WRLCK SEEK_SET);
 my @FLOCK;
+# are all BSDs the same "struct flock"? tested Free+Net+Open...
 if ($^O eq 'linux' || $^O =~ /bsd/) {
        require Config;
        my $off_t;
@@ -27,7 +28,7 @@ if ($^O eq 'linux' || $^O =~ /bsd/) {
                if ($^O eq 'linux') {
                        @FLOCK = ("ss\@8$off_t$off_t\@32",
                                qw(l_type l_whence l_start l_len));
-               } elsif ($^O =~ /bsd/) {
+               } elsif ($^O =~ /bsd/) { # @32 may be enough
                        @FLOCK = ("${off_t}${off_t}lss\@256",
                                qw(l_start l_len l_pid l_type l_whence));
                }
index 001ceda889985e0fd843d15673f936d59aaae7a4..2a7a7c3fdaf04097f72526e12d427bd34122f320 100644 (file)
--- a/t/pop3d.t
+++ b/t/pop3d.t
@@ -14,7 +14,8 @@ unless (-r $key && -r $cert) {
 # Net::POP3 is part of the standard library, but distros may split it off...
 require_mods(qw(DBD::SQLite Net::POP3 IO::Socket::SSL));
 require_git('2.6'); # for v2
-require_mods(qw(File::FcntlLock)) if $^O !~ /\A(?:linux|freebsd)\z/;
+$^O =~ /\A(?:linux|(?:free|net|open)bsd)\z/ or
+       require_mods(qw(File::FcntlLock));
 use_ok 'IO::Socket::SSL';
 use_ok 'PublicInbox::TLS';
 my ($tmpdir, $for_destroy) = tmpdir();