Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #36

Merged
merged 4 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 1 addition & 45 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package daemon
import (
"errors"
"os"
"syscall"
)

var errNotSupported = errors.New("daemon: Non-POSIX OS is not supported")
Expand All @@ -17,49 +16,6 @@ const (
// Default file permissions for log and pid files.
const FILE_PERM = os.FileMode(0640)

// A Context describes daemon 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

// Struct contains only serializable public fields (!!!)
abspath string
pidFile *LockFile
logFile *os.File
nullFile *os.File

rpipe, wpipe *os.File
}

// WasReborn returns true in child process (daemon) and false in parent process.
func WasReborn() bool {
return os.Getenv(MARK_NAME) == MARK_VALUE
Expand All @@ -75,7 +31,7 @@ func (d *Context) Reborn() (child *os.Process, err error) {
return d.reborn()
}

// Search search daemons process by given in context pid file name.
// Search searches 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.
func (d *Context) Search() (daemon *os.Process, err error) {
Expand Down
35 changes: 34 additions & 1 deletion daemon_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,39 @@ import (
"os"
)

// A Context describes daemon 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

// If Umask is non-zero, the daemon-process call Umask() func with given value.
Umask int
}

func (d *Context) reborn() (child *os.Process, err error) {
return nil, errNotSupported
}
Expand All @@ -15,5 +48,5 @@ func (d *Context) search() (daemon *os.Process, err error) {
}

func (d *Context) release() (err error) {
return nil, errNotSupported
return errNotSupported
}
42 changes: 42 additions & 0 deletions daemon_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,48 @@ import (
"github.com/kardianos/osext"
)

// A Context describes daemon 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.
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

// Struct contains only serializable public fields (!!!)
abspath string
pidFile *LockFile
logFile *os.File
nullFile *os.File

rpipe, wpipe *os.File
}

func (d *Context) reborn() (child *os.Process, err error) {
if !WasReborn() {
child, err = d.parent()
Expand Down
7 changes: 4 additions & 3 deletions examples/cmd/gd-simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package main

import (
"fmt"
"github.com/sevlyar/go-daemon"
"html"
"log"
"net/http"

"github.com/sevlyar/go-daemon"
)

// To terminate the daemon use:
Expand Down Expand Up @@ -34,10 +35,10 @@ func main() {
log.Print("- - - - - - - - - - - - - - -")
log.Print("daemon started")

serveHttp()
serveHTTP()
}

func serveHttp() {
func serveHTTP() {
http.HandleFunc("/", httpHandler)
http.ListenAndServe("127.0.0.1:8080", nil)
}
Expand Down
2 changes: 1 addition & 1 deletion lock_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var (
// ErrWoldBlock indicates on locking pid-file by another process.
// ErrWouldBlock indicates on locking pid-file by another process.
ErrWouldBlock = errors.New("daemon: Resource temporarily unavailable")
)

Expand Down
88 changes: 0 additions & 88 deletions oldapi/daemon_posix.go

This file was deleted.

72 changes: 0 additions & 72 deletions oldapi/example_test.go

This file was deleted.

Loading