daemon

package
v0.0.0-...-678c32f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2020 License: Apache-2.0, MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MarkName  = "_GO_DAEMON"
	MarkValue = "1"
)

Mark of daemon process - system environment variable _GO_DAEMON=1

View Source
const FilePerm = os.FileMode(0640)

FilePerm set default file permissions for log and pid files.

Variables

View Source
var ErrStop = errors.New("stop serve signals")

ErrStop should be returned signal handler function for termination of handling signals.

View Source
var (
	// ErrWouldBlock indicates on locking pid-file by another process.
	ErrWouldBlock = syscall.EWOULDBLOCK
)

Functions

func GetFdName

func GetFdName(fd uintptr) (name string, err error)

GetFdName returns file name for given descriptor.

func ReadPidFile

func ReadPidFile(name string) (pid int, err error)

ReadPidFile reads process id from file with give name and returns pid. If unable read from a file, returns error.

func ServeSignals

func ServeSignals() (err error)

ServeSignals calls handlers for system signals.

func SetSigHandler

func SetSigHandler(handler SignalHandlerFunc, signals ...os.Signal)

SetSigHandler sets handler for the given signals. SIGTERM has the default handler, he returns ErrStop.

func WasReborn

func WasReborn() bool

WasReborn returns true in child process (daemon) and false in parent process.

Types

type Context

type Context struct {
	// If PidFileName is non-empty, parent process will try to create and lock
	// pid file with given name. Child process writes process id to file.
	PidFileName string
	// Permissions for new pid file.
	PidFilePerm os.FileMode

	// If LogFileName is non-empty, parent process will create file with given name
	// and will link to fd 2 (stderr) for child process.
	LogFileName string
	// Permissions for new log file.
	LogFilePerm os.FileMode

	// If WorkDir is non-empty, the child changes into the directory before
	// creating the process.
	WorkDir string
	// If Chroot is non-empty, the child changes root directory
	Chroot string

	// If Env is non-nil, it gives the environment variables for the
	// daemon-process in the form returned by os.Environ.
	// If it is nil, the result of os.Environ will be used.
	Env []string
	// If Args is non-nil, it gives the command-line args for the
	// daemon-process. If it is nil, the result of os.Args will be used
	// (without program name).
	Args []string

	// Credential holds user and group identities to be assumed by a daemon-process.
	Credential *syscall.Credential
	// If Umask is non-zero, the daemon-process call Umask() func with given value.
	Umask int
	// contains filtered or unexported fields
}

A Context describes daemon context.

func (*Context) Reborn

func (d *Context) Reborn() (child *os.Process, err error)

Reborn runs second copy of current process in the given context. function executes separate parts of code in child process and parent process and provides demonization of child process. It look similar as the fork-daemonization, but goroutine-safe. In success returns *os.Process in parent process and nil in child process. Otherwise returns error.

func (*Context) Release

func (d *Context) Release() (err error)

Release provides correct pid-file release in daemon.

func (*Context) Search

func (d *Context) Search() (daemon *os.Process, err error)

Search search daemons process by given in context pid file name. If success returns pointer on daemons os.Process structure, else returns error. Returns nil if filename is empty.

type LockFile

type LockFile struct {
	*os.File
}

LockFile wraps *os.File and provide functions for locking of files.

func CreatePidFile

func CreatePidFile(name string, perm os.FileMode) (lock *LockFile, err error)

CreatePidFile opens the named file, applies exclusive lock and writes current process id to file.

func NewLockFile

func NewLockFile(file *os.File) *LockFile

NewLockFile returns a new LockFile with the given File.

func OpenLockFile

func OpenLockFile(name string, perm os.FileMode) (lock *LockFile, err error)

OpenLockFile opens the named file with flags os.O_RDWR|os.O_CREATE and specified perm. If successful, function returns LockFile for opened file.

func (*LockFile) Lock

func (file *LockFile) Lock() error

Lock apply exclusive lock on an open file. If file already locked, returns error.

func (*LockFile) ReadPid

func (file *LockFile) ReadPid() (pid int, err error)

ReadPid reads process id from file and returns pid. If unable read from a file, returns error.

func (*LockFile) Remove

func (file *LockFile) Remove() error

Remove removes lock, closes and removes an open file.

func (*LockFile) Unlock

func (file *LockFile) Unlock() error

Unlock remove exclusive lock on an open file.

func (*LockFile) WritePid

func (file *LockFile) WritePid() (err error)

WritePid writes current process id to an open file.

type SignalHandlerFunc

type SignalHandlerFunc func(sig os.Signal) (err error)

SignalHandlerFunc is the interface for signal handler functions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL