|
|
|
@ -1,4 +1,4 @@ |
|
|
|
|
package crypticnet |
|
|
|
|
package main |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"errors" |
|
|
|
@ -12,33 +12,13 @@ import ( |
|
|
|
|
|
|
|
|
|
var errDaemonNotRunning = errors.New("no cryptic-net daemon process running") |
|
|
|
|
|
|
|
|
|
// ProcLock is used to lock a process.
|
|
|
|
|
type ProcLock interface { |
|
|
|
|
|
|
|
|
|
// WriteLock creates a new lock, or errors if the lock is alread held.
|
|
|
|
|
WriteLock() error |
|
|
|
|
|
|
|
|
|
// AssertLock returns an error if the lock already exists.
|
|
|
|
|
AssertLock() error |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type procLock struct { |
|
|
|
|
dir string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewProcLock returns a ProcLock which will use a file in the given directory
|
|
|
|
|
// to lock the process.
|
|
|
|
|
func NewProcLock(dir string) ProcLock { |
|
|
|
|
return &procLock{dir: dir} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (pl *procLock) path() string { |
|
|
|
|
return filepath.Join(pl.dir, "lock") |
|
|
|
|
func lockFilePath() string { |
|
|
|
|
return filepath.Join(envRuntimeDirPath, "lock") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (pl *procLock) WriteLock() error { |
|
|
|
|
func writeLock() error { |
|
|
|
|
|
|
|
|
|
lockFilePath := pl.path() |
|
|
|
|
lockFilePath := lockFilePath() |
|
|
|
|
|
|
|
|
|
lockFile, err := os.OpenFile( |
|
|
|
|
lockFilePath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0400, |
|
|
|
@ -63,11 +43,31 @@ func (pl *procLock) WriteLock() error { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// returns a cleanup function which will clean up the created runtime directory.
|
|
|
|
|
func setupAndLockRuntimeDir() (func(), error) { |
|
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "will use runtime directory %q for temporary state\n", envRuntimeDirPath) |
|
|
|
|
|
|
|
|
|
if err := os.MkdirAll(envRuntimeDirPath, 0700); err != nil { |
|
|
|
|
return nil, fmt.Errorf("creating directory %q: %w", envRuntimeDirPath, err) |
|
|
|
|
|
|
|
|
|
} else if err := writeLock(); err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return func() { |
|
|
|
|
fmt.Fprintf(os.Stderr, "cleaning up runtime directory %q\n", envRuntimeDirPath) |
|
|
|
|
if err := os.RemoveAll(envRuntimeDirPath); err != nil { |
|
|
|
|
fmt.Fprintf(os.Stderr, "error removing temporary directory %q: %v", envRuntimeDirPath, err) |
|
|
|
|
} |
|
|
|
|
}, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// checks that the lock file exists and that the process which created it also
|
|
|
|
|
// still exists.
|
|
|
|
|
func (pl *procLock) AssertLock() error { |
|
|
|
|
func assertLock() error { |
|
|
|
|
|
|
|
|
|
lockFilePath := pl.path() |
|
|
|
|
lockFilePath := lockFilePath() |
|
|
|
|
|
|
|
|
|
lockFile, err := os.Open(lockFilePath) |
|
|
|
|
|