-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemonizer.go
More file actions
168 lines (141 loc) · 3.44 KB
/
Copy pathdaemonizer.go
File metadata and controls
168 lines (141 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package daemonizer
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"syscall"
)
const daemonFlag = "--__daemon__"
var (
ErrAlreadyDaemon = errors.New("already running as daemon")
ErrDaemonFailed = errors.New("daemon process failed to start")
)
type Config struct {
Dir string
Env []string
Stdin *os.File
Stdout *os.File
Stderr *os.File
}
type Daemon struct {
args []string
isDaemon bool
}
func New() *Daemon {
d := &Daemon{}
for _, arg := range os.Args {
if arg == daemonFlag {
d.isDaemon = true
} else {
d.args = append(d.args, arg)
}
}
if d.isDaemon {
os.Args = d.args
}
return d
}
func (d *Daemon) IsDaemon() bool {
return d.isDaemon
}
func (d *Daemon) Args() []string {
return d.args
}
// Daemonize launches the daemon process and waits for it to report readiness.
// params must be JSON-serializable (e.g., a struct with json tags).
// Called by the parent process.
func (d *Daemon) Daemonize(ctx context.Context, params any, cfg *Config) error {
if d.isDaemon {
return ErrAlreadyDaemon
}
paramR, paramW, err := os.Pipe()
if err != nil {
return fmt.Errorf("create param pipe: %w", err)
}
statusR, statusW, err := os.Pipe()
if err != nil {
paramR.Close()
paramW.Close()
return fmt.Errorf("create status pipe: %w", err)
}
cmd := exec.CommandContext(ctx, d.args[0], append([]string{daemonFlag}, d.args[1:]...)...)
cmd.ExtraFiles = []*os.File{paramR, statusW}
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
if cfg != nil {
cmd.Dir = cfg.Dir
cmd.Env = cfg.Env
cmd.Stdin = cfg.Stdin
cmd.Stdout = cfg.Stdout
cmd.Stderr = cfg.Stderr
}
if err := cmd.Start(); err != nil {
paramR.Close()
paramW.Close()
statusR.Close()
statusW.Close()
return fmt.Errorf("start daemon: %w", err)
}
// close child-side ends now that the child has inherited them
paramR.Close()
statusW.Close()
// send params
if err := json.NewEncoder(paramW).Encode(params); err != nil {
paramW.Close()
statusR.Close()
cmd.Process.Release()
return fmt.Errorf("send params: %w", err)
}
paramW.Close()
// wait for daemon to report status
var status struct {
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
}
if err := json.NewDecoder(statusR).Decode(&status); err != nil {
statusR.Close()
cmd.Process.Release()
return fmt.Errorf("read daemon status: %w", err)
}
statusR.Close()
cmd.Process.Release()
if !status.OK {
return fmt.Errorf("%w: %s", ErrDaemonFailed, status.Error)
}
return nil
}
// WaitForParent receives params from the parent process and deserializes into dest.
// dest must be a pointer to the type that was passed to Start.
// The returned function should be called to signal readiness (nil) or failure (error).
func (d *Daemon) WaitForParent(dest any) (ready func(error), err error) {
if !d.isDaemon {
return nil, errors.New("not a daemon process")
}
paramR := os.NewFile(3, "param_pipe")
statusW := os.NewFile(4, "status_pipe")
if err := json.NewDecoder(paramR).Decode(dest); err != nil {
paramR.Close()
statusW.Close()
return nil, fmt.Errorf("read params: %w", err)
}
paramR.Close()
called := false
ready = func(initErr error) {
if called {
return
}
called = true
defer statusW.Close()
status := struct {
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
}{OK: initErr == nil}
if initErr != nil {
status.Error = initErr.Error()
}
json.NewEncoder(statusW).Encode(status)
}
return ready, nil
}