Skip to content

Commit

Permalink
Fix PID-file path with Chroot option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sevlyar committed May 2, 2018
1 parent c48f50c commit 45a2ba1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 46 deletions.
1 change: 0 additions & 1 deletion daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var errNotSupported = errors.New("daemon: Non-POSIX OS is not supported")
const (
MARK_NAME = "_GO_DAEMON"
MARK_VALUE = "1"
MARK_PID = "_GO_DAEMON_PID"
)

// Default file permissions for log and pid files.
Expand Down
45 changes: 24 additions & 21 deletions daemon_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type Context struct {

// Struct contains only serializable public fields (!!!)
abspath string
pidpath string
pidFile *LockFile
logFile *os.File
nullFile *os.File
Expand Down Expand Up @@ -105,8 +104,10 @@ func (d *Context) parent() (child *os.Process, err error) {

d.rpipe.Close()
encoder := json.NewEncoder(d.wpipe)
err = encoder.Encode(d)

if err = encoder.Encode(d); err != nil {
return
}
_, err = fmt.Fprint(d.wpipe, "\n\n")
return
}

Expand All @@ -122,13 +123,23 @@ func (d *Context) openFiles() (err error) {
return
}

if len(d.pidpath) > 0 {
if d.pidFile, err = OpenLockFile(d.pidpath, d.PidFilePerm); err != nil {
if len(d.PidFileName) > 0 {
if d.PidFileName, err = filepath.Abs(d.PidFileName); err != nil {
return err
}
if d.pidFile, err = OpenLockFile(d.PidFileName, d.PidFilePerm); err != nil {
return
}
if err = d.pidFile.Lock(); err != nil {
return
}
if len(d.Chroot) > 0 {
// Calculate PID-file absolute path in child's environment
if d.PidFileName, err = filepath.Rel(d.Chroot, d.PidFileName); err != nil {
return err
}
d.PidFileName = "/" + d.PidFileName
}
}

if len(d.LogFileName) > 0 {
Expand Down Expand Up @@ -175,14 +186,6 @@ func (d *Context) prepareEnv() (err error) {
}
d.Env = append(d.Env, mark)

if len(d.PidFileName) != 0 {
if d.pidpath, err = filepath.Abs(d.PidFileName); err != nil {
return err
}
mark = fmt.Sprintf("%s=%s", MARK_PID, d.pidpath)
d.Env = append(d.Env, mark)
}

return
}

Expand Down Expand Up @@ -213,20 +216,20 @@ func (d *Context) child() (err error) {
}
initialized = true

d.pidpath = os.Getenv(MARK_PID)
if len(d.pidpath) > 0 {
d.pidFile = NewLockFile(os.NewFile(4, d.pidpath))
if err = d.pidFile.WritePid(); err != nil {
return
}
}

decoder := json.NewDecoder(os.Stdin)
if err = decoder.Decode(d); err != nil {
d.pidFile.Remove()
return
}

// create PID file after context decoding to know PID file full path.
if len(d.PidFileName) > 0 {
d.pidFile = NewLockFile(os.NewFile(4, d.PidFileName))
if err = d.pidFile.WritePid(); err != nil {
return
}
}

if err = syscall.Close(0); err != nil {
d.pidFile.Remove()
return
Expand Down
23 changes: 0 additions & 23 deletions lock_file_darwin.go

This file was deleted.

2 changes: 1 addition & 1 deletion lock_file_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build dragonfly freebsd linux netbsd openbsd plan9 solaris
// +build darwin dragonfly freebsd linux netbsd openbsd plan9 solaris

package daemon

Expand Down

0 comments on commit 45a2ba1

Please sign in to comment.