This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-daemon.git

commit a29f65cdce4e689ab74e50020b4bf0baea18dfbb
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Jan 7 09:09:42 2026 +0000

    Fix broken unlock.
    
    Calling fclose closed the fd which caused unlock to fail
    Reported/fixed by Alex Dupre and Michael Osipov
---
 src/changes/changes.xml            | 2 +-
 src/native/unix/native/jsvc-unix.c | 9 ++++-----
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 47e2e93..09fc933 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -42,7 +42,7 @@
   <body>
     <release version="1.5.2" date="YYYY-MM-DD" description="Bug fix release">
       <!-- FIX -->
-      <action type="fix" dev="markt" due-to="Alex Dupre and Michael 
Osipov">jsvc. Fix a regression in 1.5.1 that exposed a long standing bug around 
the use of lockf(3) and pid files.</action>
+      <action type="fix" dev="markt" due-to="Alex Dupre and Michael 
Osipov">jsvc. Fix a regression in 1.5.1 that exposed long standing bugs around 
the locking and unlocking of pid files. Also fix the locking/unlocking 
bugs.</action>
       <!-- ADD -->
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump 
org.apache.commons:commons-parent from 93 to 94.</action>
diff --git a/src/native/unix/native/jsvc-unix.c 
b/src/native/unix/native/jsvc-unix.c
index cdf014c..4f49f85 100644
--- a/src/native/unix/native/jsvc-unix.c
+++ b/src/native/unix/native/jsvc-unix.c
@@ -566,7 +566,6 @@ static int mkdir2(const char *name, int perms)
 static int check_pid(arg_data *args)
 {
     int fd;
-    FILE *pidf;
     char buff[80];
     pid_t pidn = getpid();
     int i, pid;
@@ -609,10 +608,10 @@ retry:
             }
         }
         lseek(fd, 0, SEEK_SET);
-        pidf = fdopen(fd, "r+");
-        fprintf(pidf, "%d\n", (int)getpid());
-        fflush(pidf);
-        fclose(pidf);
+        ftruncate(fd, 0);
+        i = snprintf(buff, sizeof(buff), "%d\n", (int)getpid());
+        write(fd, buff, i);
+        fsync(fd);
         if (lockf(fd, F_ULOCK, 0)) {
             log_error("check_pid: Failed to unlock PID file [%s] with file 
descriptor [%d] after reading due to [%d]",
                     args->pidf, fd, errno);

Reply via email to