Skip to content

Commit

Permalink
pidfile option to supply basedir
Browse files Browse the repository at this point in the history
Signed-off-by: Avi Deitcher <[email protected]>
  • Loading branch information
deitch committed May 10, 2024
1 parent fe74fbb commit 555ceec
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions pkg/pillar/pidfile/pidfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,37 @@ package pidfile
import (
"fmt"
"os"
"path"
"strconv"
"syscall"

"github.com/lf-edge/eve/pkg/pillar/base"
)

const (
rundir = "/run"
defaultRundir = "/run"
)

func writeMyPid(filename string) error {
pid := os.Getpid()
pidStr := fmt.Sprintf("%d", pid)
b := []byte(pidStr)
// if the directory does not exist, try to create it
if err := os.MkdirAll(path.Dir(filename), 0755); err != nil {
return err
}
return os.WriteFile(filename, b, 0644)
}

// CheckProcessExists returns true if agent process is running
// returns string with description of check result
func CheckProcessExists(log *base.LogObject, agentName string) (bool, string) {
filename := fmt.Sprintf("%s/%s.pid", rundir, agentName)
func CheckProcessExists(log *base.LogObject, agentName string, options ...Option) (bool, string) {
opt := processOpts(options)
return checkProcessExists(log, agentName, opt)
}

func checkProcessExists(log *base.LogObject, agentName string, opt opt) (bool, string) {
filename := path.Join(opt.baseDir, agentName+".pid")
if _, err := os.Stat(filename); err != nil && os.IsNotExist(err) {
return false, err.Error()
}
Expand All @@ -54,13 +64,43 @@ func CheckProcessExists(log *base.LogObject, agentName string) (bool, string) {
}

// CheckAndCreatePidfile check if old process is not running and create new pid file
func CheckAndCreatePidfile(log *base.LogObject, agentName string) error {
if exists, description := CheckProcessExists(log, agentName); exists {
func CheckAndCreatePidfile(log *base.LogObject, agentName string, options ...Option) error {
opt := processOpts(options)
if exists, description := checkProcessExists(log, agentName, opt); exists {
return fmt.Errorf("checkAndCreatePidfile: %s", description)
}
filename := fmt.Sprintf("%s/%s.pid", rundir, agentName)
rundir := defaultRundir
if opt.baseDir != "" {
rundir = opt.baseDir
}
filename := path.Join(rundir, agentName+".pid")
if err := writeMyPid(filename); err != nil {
log.Fatalf("checkAndCreatePidfile: %s", err)
}
return nil
}

func processOpts(options []Option) opt {
opt := opt{}
for _, o := range options {
o(&opt)
}
if opt.baseDir == "" {
opt.baseDir = defaultRundir
}
return opt
}

type opt struct {
baseDir string
}

// Option option function to pass to pidfile functions
type Option func(o *opt)

// WithBaseDir set the base directory for pidfiles. Default is /run.
func WithBaseDir(baseDir string) Option {
return func(o *opt) {
o.baseDir = baseDir
}
}

0 comments on commit 555ceec

Please sign in to comment.