]> git.hungrycats.org Git - bees/commitdiff
hash: fix FLAGS_CREATE_FILE O_WRONLY preventing pread on new hash table
authorZygo Blaxell <bees@furryterror.org>
Fri, 13 Mar 2026 23:06:32 +0000 (19:06 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 2 May 2026 03:48:57 +0000 (23:48 -0400)
FLAGS_CREATE_FILE used O_WRONLY|O_CREAT|O_EXCL.  On a fresh beeshome,
open_file() creates beeshash.dat through this flag and assigns the
write-only fd to m_fd.  The prefetch thread then calls pread_or_die(m_fd)
and gets EBADF because pread() requires O_RDONLY or O_RDWR.

In production, beesd and OpenRC pre-create beeshash.dat with truncate(1)
before exec'ing bees, so the creation branch in open_file() was never
exercised.  Integration tests exposed this by starting bees on a fresh
$BEESHOME.

Change FLAGS_CREATE_FILE from O_WRONLY to O_RDWR.  The name still
describes the intent (O_CREAT|O_EXCL ensure exclusive creation); O_RDWR
simply allows reading the file back after writing it.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
src/bees.h

index 1fd86b4d545325c3884dd4b42c3ddbc00eff4a28..7c0cfca5bd5ee9f8d57f581385598fd31133f41a 100644 (file)
@@ -118,14 +118,11 @@ const size_t BEES_READAHEAD_SIZE = 1024 * 1024;
 
 // Flags
 const int FLAGS_OPEN_COMMON   = O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC | O_NOATIME | O_LARGEFILE | O_NOCTTY;
-const int FLAGS_OPEN_DIR      = FLAGS_OPEN_COMMON | O_RDONLY | O_DIRECTORY;
-const int FLAGS_OPEN_FILE     = FLAGS_OPEN_COMMON | O_RDONLY;
-const int FLAGS_OPEN_FILE_RW  = FLAGS_OPEN_COMMON | O_RDWR;
-const int FLAGS_OPEN_TMPFILE  = FLAGS_OPEN_FILE_RW | O_TMPFILE | O_TRUNC | O_EXCL;
-const int FLAGS_CREATE_FILE   = FLAGS_OPEN_COMMON | O_WRONLY | O_CREAT | O_EXCL;
-
-// Fanotify allows O_APPEND, O_DSYNC, O_NOATIME, O_NONBLOCK, O_CLOEXEC, O_LARGEFILE
-const int FLAGS_OPEN_FANOTIFY = O_RDWR | O_NOATIME | O_CLOEXEC | O_LARGEFILE;
+const int FLAGS_OPEN_DIR      = FLAGS_OPEN_COMMON | O_RDONLY | O_DIRECTORY;  ///< Open a directory read-only.
+const int FLAGS_OPEN_FILE     = FLAGS_OPEN_COMMON | O_RDONLY;                ///< Open a regular file read-only.
+const int FLAGS_OPEN_FILE_RW  = FLAGS_OPEN_COMMON | O_RDWR;                  ///< Open a regular file read-write.
+const int FLAGS_OPEN_TMPFILE  = FLAGS_OPEN_FILE_RW | O_TMPFILE | O_TRUNC | O_EXCL; ///< Create an anonymous temp file.
+const int FLAGS_CREATE_FILE   = FLAGS_OPEN_COMMON | O_RDWR   | O_CREAT | O_EXCL;   ///< Create a new file exclusively.
 
 // macros ----------------------------------------