{"modulePath":"github.com/infinitbyte/framework","version":"v1.1.5","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package daemon\n\n## Constants\n\n```go\nconst (\n\tMarkName  = \"_GO_DAEMON\"\n\tMarkValue = \"1\"\n)\n```\nMark of daemon process - system environment variable \\_GO\\_DAEMON=1\n\n```go\nconst FilePerm = os.FileMode(0640)\n```\nFilePerm set default file permissions for log and pid files.\n\n## Variables\n\n```go\nvar ErrStop = errors.New(\"stop serve signals\")\n```\nErrStop should be returned signal handler function for termination of handling signals.\n\n```go\nvar (\n\t// ErrWouldBlock indicates on locking pid-file by another process.\n\tErrWouldBlock = syscall.EWOULDBLOCK\n)\n```\n\n## Functions\n\n```go\nfunc GetFdName(fd uintptr) (name string, err error)\n```\nGetFdName returns file name for given descriptor.\n\n```go\nfunc ReadPidFile(name string) (pid int, err error)\n```\nReadPidFile reads process id from file with give name and returns pid. If unable read from a file, returns error.\n\n```go\nfunc ServeSignals() (err error)\n```\nServeSignals calls handlers for system signals.\n\n```go\nfunc SetSigHandler(handler SignalHandlerFunc, signals ...os.Signal)\n```\nSetSigHandler sets handler for the given signals. SIGTERM has the default handler, he returns ErrStop.\n\n```go\nfunc WasReborn() bool\n```\nWasReborn returns true in child process (daemon) and false in parent process.\n\n## Types\n\n```go\ntype Context struct {\n\t// If PidFileName is non-empty, parent process will try to create and lock\n\t// pid file with given name. Child process writes process id to file.\n\tPidFileName string\n\t// Permissions for new pid file.\n\tPidFilePerm os.FileMode\n\n\t// If LogFileName is non-empty, parent process will create file with given name\n\t// and will link to fd 2 (stderr) for child process.\n\tLogFileName string\n\t// Permissions for new log file.\n\tLogFilePerm os.FileMode\n\n\t// If WorkDir is non-empty, the child changes into the directory before\n\t// creating the process.\n\tWorkDir string\n\t// If Chroot is non-empty, the child changes root directory\n\tChroot string\n\n\t// If Env is non-nil, it gives the environment variables for the\n\t// daemon-process in the form returned by os.Environ.\n\t// If it is nil, the result of os.Environ will be used.\n\tEnv []string\n\t// If Args is non-nil, it gives the command-line args for the\n\t// daemon-process. If it is nil, the result of os.Args will be used\n\t// (without program name).\n\tArgs []string\n\n\t// Credential holds user and group identities to be assumed by a daemon-process.\n\tCredential *syscall.Credential\n\t// If Umask is non-zero, the daemon-process call Umask() func with given value.\n\tUmask int\n\t// contains filtered or unexported fields\n}\n```\nA Context describes daemon context.\n\n```go\nfunc (d *Context) Reborn() (child *os.Process, err error)\n```\nReborn 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.\n\n```go\nfunc (d *Context) Release() (err error)\n```\nRelease provides correct pid-file release in daemon.\n\n```go\nfunc (d *Context) Search() (daemon *os.Process, err error)\n```\nSearch 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.\n\n```go\ntype LockFile struct {\n\t*os.File\n}\n```\nLockFile wraps \\*os.File and provide functions for locking of files.\n\n```go\nfunc CreatePidFile(name string, perm os.FileMode) (lock *LockFile, err error)\n```\nCreatePidFile opens the named file, applies exclusive lock and writes current process id to file.\n\n```go\nfunc NewLockFile(file *os.File) *LockFile\n```\nNewLockFile returns a new LockFile with the given File.\n\n```go\nfunc OpenLockFile(name string, perm os.FileMode) (lock *LockFile, err error)\n```\nOpenLockFile opens the named file with flags os.O\\_RDWR|os.O\\_CREATE and specified perm. If successful, function returns LockFile for opened file.\n\n```go\nfunc (file *LockFile) Lock() error\n```\nLock apply exclusive lock on an open file. If file already locked, returns error.\n\n```go\nfunc (file *LockFile) ReadPid() (pid int, err error)\n```\nReadPid reads process id from file and returns pid. If unable read from a file, returns error.\n\n```go\nfunc (file *LockFile) Remove() error\n```\nRemove removes lock, closes and removes an open file.\n\n```go\nfunc (file *LockFile) Unlock() error\n```\nUnlock remove exclusive lock on an open file.\n\n```go\nfunc (file *LockFile) WritePid() (err error)\n```\nWritePid writes current process id to an open file.\n\n```go\ntype SignalHandlerFunc func(sig os.Signal) (err error)\n```\nSignalHandlerFunc is the interface for signal handler functions.\n\n","path":"github.com/infinitbyte/framework/core/daemon","name":"daemon","synopsis":"","isRedistributable":true}
