Skip to content

Commit

Permalink
Re-assert lock on pidfile during pidfile_write
Browse files Browse the repository at this point in the history
The documented usage of pidfile_open and pidfile_write did not actually work
because the flock on the pidfile would not survive the daemonize call to fork
to a background process.  Now we re-assert the lock on the pidfile when calling
pidfile_write (aborting if the lock cannot be obtained) so that the pidfile can
be reliably used as a lockfile as documented.
  • Loading branch information
nugget committed Jul 15, 2014
1 parent c731071 commit 3380f35
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions tcllauncher-support.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ proc pidfile_open {{path ""} {mode 0600}} {
# failed to lock the file, read it for the pid of the owner
set pid [read -nonewline $fp]

# if we can get an integer out of it, return that
if {[scan $pid %d pid] > 0} {
close $fp
return $pid
}
# if we can get an integer out of it, return that
if {[scan $pid %d pid] > 0} {
close $fp
return $pid
}
}

# i got the lock
Expand All @@ -196,7 +196,7 @@ proc pidfile_open {{path ""} {mode 0600}} {
}

#
# pidfile_mtime - return the mtime of the pidfile, returns -1 if
# pidfile_mtime - return the mtime of the pidfile, returns -1 if
# "file mtime" failed.
#
proc pidfile_mtime {} {
Expand All @@ -217,10 +217,15 @@ proc pidfile_mtime {} {
proc pidfile_write {} {
variable pfh

pidfile_verify
pidfile_verify

set fp $pfh(fp)

if {![flock -write -nowait $fp]} {
puts stderr "Unable to obtain lock on pidfile for pidfile_write"
exit 252
}

ftruncate -fileid $fp 0

puts $fp [pid]
Expand Down

0 comments on commit 3380f35

Please sign in to comment.