chiark / gitweb /
prefork: Break out flock_file
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Aug 2022 11:30:24 +0000 (12:30 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Aug 2022 20:21:10 +0000 (21:21 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
cprogs/prefork.c
cprogs/prefork.h

index 21c4b7af4e950ea90bcf4a7db00284817f3cdddd..12d60218c5adad56b45138d7329cc67172dbe907 100644 (file)
@@ -184,14 +184,12 @@ void find_socket_path(void) {
 }  
 
 // Returns fd
-int acquire_lock(void) {
+int flock_file(const char *lock_path) {
   int r;
   int lockfd = -1;
   struct stat stab_fd;
   struct stat stab_path;
 
-  lock_path = m_asprintf("%s/l%s",run_base,ident);
-
   for (;;) {
     if (lockfd >= 0) { close(lockfd); lockfd = -1; }
 
@@ -216,6 +214,12 @@ int acquire_lock(void) {
   return lockfd;
 }
 
+// Returns fd
+int acquire_lock(void) {
+  lock_path = m_asprintf("%s/l%s",run_base,ident);
+  return flock_file(lock_path);
+}
+
 static void shbang_opts(const char *const **argv_io,
                        const struct cmdinfo *cmdinfos) {
   myopt(argv_io, cmdinfos);
index a046835179a37bd029e5ab85375880fa761301cd..11329cef1f59dec4fcccadc8e36a68bcc4beb3af 100644 (file)
@@ -44,6 +44,7 @@ bool find_run_base_var_run(void);
 void find_socket_path(void);
 
 int acquire_lock(void);
+int flock_file(const char *lock_path);
 
 extern const struct cmdinfo cmdinfos[];
 #define PREFORK_CMDINFOS \