-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
269 lines (248 loc) · 7.21 KB
/
main.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright ©2023 Dan Kortschak. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"errors"
"flag"
"fmt"
"io/fs"
"log/slog"
"maps"
"os"
"os/signal"
"path/filepath"
"sort"
"strings"
"syscall"
"github.com/gofrs/flock"
public "github.com/kortschak/dex/config"
"github.com/kortschak/dex/internal/config"
"github.com/kortschak/dex/internal/device"
"github.com/kortschak/dex/internal/slogext"
"github.com/kortschak/dex/internal/state"
"github.com/kortschak/dex/internal/sys"
"github.com/kortschak/dex/internal/version"
"github.com/kortschak/dex/internal/xdg"
"github.com/kortschak/dex/rpc"
)
// Exit status codes.
const (
success = 0
internalError = 1 << (iota - 1)
invocationError
)
func main() { os.Exit(Main()) }
func Main() int {
logging := flag.String("log", "info", "logging level (debug, info, warn or error)")
lines := flag.Bool("lines", false, "display source line details in logs")
redact := flag.Bool("redact_private", true, "redact private fields in system state requests")
v := flag.Bool("version", false, "print version and exit")
flag.Parse()
if *v {
err := version.Print()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return internalError
}
return success
}
var level slog.LevelVar
err := level.UnmarshalText([]byte(*logging))
if err != nil {
flag.Usage()
return invocationError
}
addSource := slogext.NewAtomicBool(*lines)
// log is the root logger.
log := slog.New(slogext.GoID{Handler: slogext.NewJSONHandler(os.Stderr, &slogext.HandlerOptions{
Level: &level,
AddSource: addSource,
})})
// mlog is the logger for main.
mlog := log.With(slog.String("component", "dex.main"))
runtimeDir, err := xdg.Runtime(rpc.RuntimeDir)
if err != nil {
if err != syscall.ENOENT {
fmt.Fprintln(os.Stderr, err)
return internalError
}
var ok bool
runtimeDir, ok = xdg.RuntimeDir()
if !ok {
fmt.Fprintln(os.Stderr, "no xdg runtime directory")
return internalError
}
runtimeDir = filepath.Join(runtimeDir, rpc.RuntimeDir)
err = os.MkdirAll(runtimeDir, 0o700)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return internalError
}
}
pidFile := filepath.Join(runtimeDir, "pid")
fl := flock.New(pidFile)
ok, err := fl.TryLock()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return internalError
}
if !ok {
fmt.Fprintln(os.Stderr, "dex is already running")
return internalError
}
defer func() {
fl.Unlock()
os.Remove(pidFile)
}()
pid := fmt.Sprintln(os.Getpid())
err = os.WriteFile(pidFile, []byte(pid), 0o600)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return internalError
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-c
log.LogAttrs(ctx, slog.LevelInfo, "terminating", slog.Any("signal", slogext.Stringer{Stringer: sig}))
cancel()
}()
cfgdir, err := xdg.Config("dex", true)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
fmt.Fprintln(os.Stderr, err)
return internalError
}
cfgdir, err = xdgMkdir(xdg.ConfigHome, "dex", 0o755)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return internalError
}
mlog.LogAttrs(ctx, slog.LevelInfo, "created config dir", slog.String("path", cfgdir))
}
mlog.LogAttrs(ctx, slog.LevelInfo, "config dir", slog.String("path", cfgdir))
datadir, err := xdg.State("dex")
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
fmt.Fprintln(os.Stderr, err)
return internalError
}
datadir, err = xdgMkdir(xdg.StateHome, "dex", 0o755)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return internalError
}
mlog.LogAttrs(ctx, slog.LevelInfo, "created data dir", slog.String("path", datadir))
}
mlog.LogAttrs(ctx, slog.LevelInfo, "data dir", slog.String("path", datadir))
datapath := filepath.Join(datadir, "state.sqlite3")
store, err := state.Open(datapath, log)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open data store: %v\n", err)
return internalError
}
defer store.Close()
sysman, err := sys.NewManager(
rpc.NewKernel, device.NewManager,
store, datadir, log, &level, addSource)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to start manager: %v\n", err)
return internalError
}
defer sysman.Close()
funcs, err := mergeFuncs(sysman, log,
sys.Funcs[*rpc.Kernel, *device.Manager](*redact),
device.Funcs,
device.GoFuncs[*rpc.Kernel, *device.Manager](ctx),
state.Funcs,
)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to configure kernel plugins: %v\n", err)
return internalError
}
sysman.SetFuncs(funcs)
changes := make(chan config.Change)
w, err := config.NewWatcher(ctx, cfgdir, changes, -1, log)
if err != nil {
mlog.LogAttrs(ctx, slog.LevelError, err.Error())
return internalError
}
go w.Watch(ctx)
cfgman := config.NewManager(log)
for cfg := range changes {
if cfg.Err != nil {
switch len(cfg.Event) {
case 0:
mlog.LogAttrs(ctx, slog.LevelWarn, "config stream error", slog.Any("error", cfg.Err))
case 1:
mlog.LogAttrs(ctx, slog.LevelWarn, "config stream error", slog.String("path", cfg.Event[0].Name), slog.Any("error", cfg.Err))
default:
path := make([]string, 0, len(cfg.Event))
for _, e := range cfg.Event {
path = append(path, e.Name)
}
mlog.LogAttrs(ctx, slog.LevelWarn, "config stream error", slog.Any("path", path), slog.Any("error", cfg.Err))
}
continue
}
mlog.LogAttrs(ctx, slog.LevelDebug, "config stream element", slog.Any("config", slogext.PrivateRedact{Val: cfg.Config, Tag: "json"}), slog.Any("events", cfg.Event))
err = cfgman.Apply(cfg)
if err != nil {
mlog.LogAttrs(ctx, slog.LevelWarn, "config manager apply error", slog.Any("error", err))
continue
}
unified, cue, included, remain, err := cfgman.Unify(public.Schema)
if err != nil {
mlog.LogAttrs(ctx, slog.LevelWarn, "config manager unify error", slog.Any("error", err), slog.Any("cue", cue))
continue
}
mlog.LogAttrs(ctx, slog.LevelDebug, "config manager files", slog.Any("included", included), slog.Any("remain", remain))
err = sysman.Configure(ctx, unified)
if err != nil {
mlog.LogAttrs(ctx, slog.LevelWarn, "manager configure error", slog.Any("error", err))
}
}
return success
}
func xdgMkdir(parent func() (dir string, ok bool), path string, perm fs.FileMode) (dir string, err error) {
dir, ok := parent()
if !ok {
return "", syscall.ENOENT
}
dir = filepath.Join(dir, path)
err = os.MkdirAll(dir, perm)
if err != nil {
return "", err
}
return dir, nil
}
func mergeFuncs[K sys.Kernel, D sys.Device[B], B sys.Button](
manager *sys.Manager[K, D, B], log *slog.Logger, list ...func(*sys.Manager[K, D, B], *slog.Logger) rpc.Funcs,
) (rpc.Funcs, error) {
switch len(list) {
case 0:
return nil, nil
case 1:
return list[0](manager, log), nil
}
var intersect []string
dst := maps.Clone(list[0](manager, log))
for _, mk := range list[1:] {
funcs := mk(manager, log)
for k := range funcs {
if _, ok := dst[k]; ok {
intersect = append(intersect, k)
}
}
if len(intersect) != 0 {
sort.Strings(intersect)
return nil, fmt.Errorf("kernel func name collisions: %s", strings.Join(intersect, " "))
}
maps.Copy(dst, funcs)
}
return dst, nil
}