Skip to content

Commit 397ed06

Browse files
committed
Pull request 2623: AGDNS-3868-imp-openbsd-in-ossvc
Squashed commit of the following: commit 4135405 Merge: 2b57619 f8a7275 Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Fri Apr 10 12:01:09 2026 +0300 Merge branch 'master' into AGDNS-3868-imp-openbsd-in-ossvc commit 2b57619 Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Wed Apr 8 14:55:28 2026 +0300 ossvc: add todo; commit 2bf37dd Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Wed Apr 8 14:06:02 2026 +0300 ossvc: rollback redundant changes; imp docs; imp code; commit d029056 Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Wed Apr 8 13:46:01 2026 +0300 ossvc: imp code; commit 3096404 Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Wed Apr 8 12:19:00 2026 +0300 ossvc: imp code in service_openbsd.go;
1 parent f8a7275 commit 397ed06

1 file changed

Lines changed: 83 additions & 90 deletions

File tree

internal/ossvc/service_openbsd.go

Lines changed: 83 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
1919
"github.com/AdguardTeam/golibs/errors"
2020
"github.com/AdguardTeam/golibs/ioutil"
21-
"github.com/AdguardTeam/golibs/log"
2221
"github.com/AdguardTeam/golibs/osutil/executil"
2322
"github.com/kardianos/service"
2423
)
@@ -36,90 +35,80 @@ import (
3635
const sysVersion = "openbsd-runcom"
3736

3837
// chooseSystem checks the current system detected and substitutes it with local
39-
// implementation if needed. cmdCons must not be nil.
40-
func chooseSystem(_ context.Context, _ *slog.Logger, cmdCons executil.CommandConstructor) {
38+
// implementation if needed. cmdCons and l must not be nil.
39+
func chooseSystem(_ context.Context, l *slog.Logger, cmdCons executil.CommandConstructor) {
4140
service.ChooseSystem(&openbsdSystem{
4241
cmdCons: cmdCons,
42+
logger: l,
4343
})
4444
}
4545

46-
// openbsdSystem is the service.System to be used on the OpenBSD.
46+
// openbsdSystem is an implementation of the [service.System] interface to be
47+
// used on the OpenBSD operating system.
4748
type openbsdSystem struct {
4849
cmdCons executil.CommandConstructor
50+
logger *slog.Logger
4951
}
5052

51-
// String implements service.System interface for openbsdSystem.
52-
func (sys *openbsdSystem) String() string {
53+
// type check
54+
var _ service.System = (*openbsdSystem)(nil)
55+
56+
// String implements the [service.System] interface for *openbsdSystem.
57+
func (sys *openbsdSystem) String() (s string) {
5358
return sysVersion
5459
}
5560

56-
// Detect implements service.System interface for openbsdSystem.
61+
// Detect implements the [service.System] interface for *openbsdSystem.
5762
func (sys *openbsdSystem) Detect() (ok bool) {
5863
return true
5964
}
6065

61-
// Interactive implements service.System interface for openbsdSystem.
66+
// Interactive implements the [service.System] interface for *openbsdSystem.
6267
func (sys *openbsdSystem) Interactive() (ok bool) {
6368
return os.Getppid() != 1
6469
}
6570

66-
// New implements service.System interface for openbsdSystem.
67-
func (sys *openbsdSystem) New(i service.Interface, c *service.Config) (s service.Service, err error) {
71+
// New implements the [service.System] interface for *openbsdSystem.
72+
func (sys *openbsdSystem) New(
73+
i service.Interface,
74+
c *service.Config,
75+
) (s service.Service, err error) {
6876
return &openbsdRunComService{
6977
cmdCons: sys.cmdCons,
78+
logger: sys.logger,
7079
i: i,
7180
cfg: c,
7281
}, nil
7382
}
7483

75-
// openbsdRunComService is the RunCom-based service.Service to be used on the
76-
// OpenBSD.
84+
// openbsdRunComService is the RunCom-based [service.Service] interface
85+
// implementation to be used on the OpenBSD operating system.
7786
type openbsdRunComService struct {
7887
cmdCons executil.CommandConstructor
7988
i service.Interface
8089
cfg *service.Config
90+
logger *slog.Logger
8191
}
8292

83-
// Platform implements service.Service interface for *openbsdRunComService.
93+
// type check
94+
var _ service.Service = (*openbsdRunComService)(nil)
95+
96+
// Platform implements the [service.Service] interface for *openbsdRunComService.
8497
func (*openbsdRunComService) Platform() (p string) {
8598
return "openbsd"
8699
}
87100

88-
// String implements service.Service interface for *openbsdRunComService.
89-
func (s *openbsdRunComService) String() string {
101+
// String implements the [service.Service] interface for *openbsdRunComService.
102+
func (s *openbsdRunComService) String() (str string) {
90103
return cmp.Or(s.cfg.DisplayName, s.cfg.Name)
91104
}
92105

93-
// getBool returns the value of the given name from kv, assuming the value is a
94-
// boolean. If the value isn't found or is not of the type, the defaultValue is
95-
// returned.
96-
func getBool(kv service.KeyValue, name string, defaultValue bool) (val bool) {
97-
var ok bool
98-
if val, ok = kv[name].(bool); ok {
99-
return val
100-
}
101-
102-
return defaultValue
103-
}
104-
105-
// getString returns the value of the given name from kv, assuming the value is
106-
// a string. If the value isn't found or is not of the type, the defaultValue
107-
// is returned.
108-
func getString(kv service.KeyValue, name, defaultValue string) (val string) {
109-
var ok bool
110-
if val, ok = kv[name].(string); ok {
111-
return val
112-
}
113-
114-
return defaultValue
115-
}
116-
117-
// getFuncNiladic returns the value of the given name from kv, assuming the
118-
// value is a func(). If the value isn't found or is not of the type, the
106+
// stringFromKV returns the value of the given name from kv, assuming the value
107+
// is a string. If the value isn't found or is not of the type, the
119108
// defaultValue is returned.
120-
func getFuncNiladic(kv service.KeyValue, name string, defaultValue func()) (val func()) {
109+
func stringFromKV(kv service.KeyValue, name, defaultValue string) (val string) {
121110
var ok bool
122-
if val, ok = kv[name].(func()); ok {
111+
if val, ok = kv[name].(string); ok {
123112
return val
124113
}
125114

@@ -130,8 +119,8 @@ const (
130119
// optionUserService is the UserService option name.
131120
optionUserService = "UserService"
132121

133-
// optionUserServiceDefault is the UserService option default value.
134-
optionUserServiceDefault = false
122+
// optionSvcInfo is the name of the option associated with service info.
123+
optionSvcInfo = "SvcInfo"
135124

136125
// errNoUserServiceRunCom is returned when the service uses some custom
137126
// path to script.
@@ -141,7 +130,7 @@ const (
141130
// scriptPath returns the absolute path to the script. It's commonly used to
142131
// send commands to the service.
143132
func (s *openbsdRunComService) scriptPath() (cp string, err error) {
144-
if getBool(s.cfg.Option, optionUserService, optionUserServiceDefault) {
133+
if usesCustomPath, ok := s.cfg.Option[optionUserService].(bool); ok && usesCustomPath {
145134
return "", errNoUserServiceRunCom
146135
}
147136

@@ -178,11 +167,9 @@ func (s *openbsdRunComService) template() (t *template.Template) {
178167
},
179168
}
180169

181-
return template.Must(template.New("").Funcs(tf).Parse(getString(
182-
s.cfg.Option,
183-
optionRunComScript,
184-
runComScript,
185-
)))
170+
script := stringFromKV(s.cfg.Option, optionRunComScript, runComScript)
171+
172+
return template.Must(template.New("").Funcs(tf).Parse(script))
186173
}
187174

188175
// execPath returns the absolute path to the executable to be run as a service.
@@ -203,7 +190,7 @@ func (s *openbsdRunComService) annotate(action string, err error) (annotated err
203190
return errors.Annotate(err, "%s %s %s service: %w", action, sysVersion, s.cfg.Name)
204191
}
205192

206-
// Install implements service.Service interface for *openbsdRunComService.
193+
// Install implements the [service.Service] interface for *openbsdRunComService.
207194
func (s *openbsdRunComService) Install() (err error) {
208195
defer func() { err = s.annotate("installing", err) }()
209196

@@ -262,7 +249,7 @@ func (s *openbsdRunComService) writeScript() (err error) {
262249
}{
263250
Config: s.cfg,
264251
Path: execPath,
265-
SvcInfo: getString(s.cfg.Option, "SvcInfo", s.String()),
252+
SvcInfo: stringFromKV(s.cfg.Option, optionSvcInfo, s.String()),
266253
})
267254
if err != nil {
268255
return err
@@ -274,7 +261,7 @@ func (s *openbsdRunComService) writeScript() (err error) {
274261
)
275262
}
276263

277-
// Uninstall implements service.Service interface for *openbsdRunComService.
264+
// Uninstall implements the [service.Service] interface for *openbsdRunComService.
278265
func (s *openbsdRunComService) Uninstall() (err error) {
279266
defer func() { err = s.annotate("uninstalling", err) }()
280267

@@ -294,24 +281,22 @@ func (s *openbsdRunComService) Uninstall() (err error) {
294281
return errors.Annotate(err, "removing rc.d script: %w")
295282
}
296283

297-
// optionRunWait is the name of the option associated with function which waits
298-
// for the service to be stopped.
299-
const optionRunWait = "RunWait"
300-
301284
// runWait is the default function to wait for service to be stopped.
302285
func runWait() {
303286
sigChan := make(chan os.Signal, 3)
287+
288+
// TODO(m.kazantsev): Replace with osutil interface.
304289
signal.Notify(sigChan, syscall.SIGTERM, os.Interrupt)
305290
<-sigChan
306291
}
307292

308-
// Run implements service.Service interface for *openbsdRunComService.
293+
// Run implements the [service.Service] interface for *openbsdRunComService.
309294
func (s *openbsdRunComService) Run() (err error) {
310295
if err = s.i.Start(s); err != nil {
311296
return err
312297
}
313298

314-
getFuncNiladic(s.cfg.Option, optionRunWait, runWait)()
299+
runWait()
315300

316301
return s.i.Stop(s)
317302
}
@@ -320,6 +305,7 @@ func (s *openbsdRunComService) Run() (err error) {
320305
func (s *openbsdRunComService) runCom(cmd string) (out string, err error) {
321306
var scriptPath string
322307
if scriptPath, err = s.scriptPath(); err != nil {
308+
// Don't wrap the error because it is informative as is.
323309
return "", err
324310
}
325311

@@ -344,7 +330,7 @@ func (s *openbsdRunComService) runCom(cmd string) (out string, err error) {
344330
return stdoutBuf.String(), nil
345331
}
346332

347-
// Status implements service.Service interface for *openbsdRunComService.
333+
// Status implements the [service.Service] interface for *openbsdRunComService.
348334
func (s *openbsdRunComService) Status() (status service.Status, err error) {
349335
defer func() { err = s.annotate("getting status of", err) }()
350336

@@ -364,30 +350,31 @@ func (s *openbsdRunComService) Status() (status service.Status, err error) {
364350
}
365351
}
366352

367-
// Start implements service.Service interface for *openbsdRunComService.
353+
// Start implements the [service.Service] interface for *openbsdRunComService.
368354
func (s *openbsdRunComService) Start() (err error) {
369355
_, err = s.runCom("start")
370356

371357
return s.annotate("starting", err)
372358
}
373359

374-
// Stop implements service.Service interface for *openbsdRunComService.
360+
// Stop implements the [service.Service] interface for *openbsdRunComService.
375361
func (s *openbsdRunComService) Stop() (err error) {
376362
_, err = s.runCom("stop")
377363

378364
return s.annotate("stopping", err)
379365
}
380366

381-
// Restart implements service.Service interface for *openbsdRunComService.
367+
// Restart implements the [service.Service] interface for *openbsdRunComService.
382368
func (s *openbsdRunComService) Restart() (err error) {
383369
if err = s.Stop(); err != nil {
370+
// Don't wrap the error because it is informative as is.
384371
return err
385372
}
386373

387374
return s.Start()
388375
}
389376

390-
// Logger implements service.Service interface for *openbsdRunComService.
377+
// Logger implements the [service.Service] interface for *openbsdRunComService.
391378
func (s *openbsdRunComService) Logger(errs chan<- error) (l service.Logger, err error) {
392379
if service.ChosenSystem().Interactive() {
393380
return service.ConsoleLogger, nil
@@ -396,58 +383,64 @@ func (s *openbsdRunComService) Logger(errs chan<- error) (l service.Logger, err
396383
return s.SystemLogger(errs)
397384
}
398385

399-
// SystemLogger implements service.Service interface for *openbsdRunComService.
386+
// SystemLogger implements the [service.Service] interface for *openbsdRunComService.
400387
func (s *openbsdRunComService) SystemLogger(errs chan<- error) (l service.Logger, err error) {
401-
return newSysLogger(s.cfg.Name, errs)
388+
return newSysLogger(s.logger, s.cfg.Name, errs)
402389
}
403390

404-
// newSysLogger returns a stub service.Logger implementation.
405-
func newSysLogger(_ string, _ chan<- error) (service.Logger, error) {
406-
return sysLogger{}, nil
391+
// newSysLogger returns a the [service.Logger] interface implementation.
392+
func newSysLogger(l *slog.Logger, _ string, _ chan<- error) (sl service.Logger, err error) {
393+
return &sysLogger{l: l}, nil
407394
}
408395

409-
// sysLogger wraps calls of the logging functions understandable for service
396+
// sysLogger is an implementation of [service.Logger] interface that wraps calls
397+
// of the logging functions in a way that is understandable for service
410398
// interfaces.
411-
type sysLogger struct{}
399+
type sysLogger struct {
400+
l *slog.Logger
401+
}
402+
403+
// type check
404+
var _ service.Logger = (*sysLogger)(nil)
412405

413-
// Error implements service.Logger interface for sysLogger.
414-
func (sysLogger) Error(v ...any) error {
415-
log.Error("%s", fmt.Sprint(v...))
406+
// Error implements the [service.Logger] interface for sysLogger.
407+
func (s *sysLogger) Error(v ...any) (err error) {
408+
s.l.Error(fmt.Sprint(v...))
416409

417410
return nil
418411
}
419412

420-
// Warning implements service.Logger interface for sysLogger.
421-
func (sysLogger) Warning(v ...any) error {
422-
log.Info("warning: %s", fmt.Sprint(v...))
413+
// Warning implements the [service.Logger] interface for sysLogger.
414+
func (s *sysLogger) Warning(v ...any) (err error) {
415+
s.l.Warn(fmt.Sprint(v...))
423416

424417
return nil
425418
}
426419

427-
// Info implements service.Logger interface for sysLogger.
428-
func (sysLogger) Info(v ...any) error {
429-
log.Info("%s", fmt.Sprint(v...))
420+
// Info implements the [service.Logger] interface for sysLogger.
421+
func (s *sysLogger) Info(v ...any) (err error) {
422+
s.l.Info(fmt.Sprint(v...))
430423

431424
return nil
432425
}
433426

434-
// Errorf implements service.Logger interface for sysLogger.
435-
func (sysLogger) Errorf(format string, a ...any) error {
436-
log.Error(format, a...)
427+
// Errorf implements the [service.Logger] interface for sysLogger.
428+
func (s *sysLogger) Errorf(format string, a ...any) (err error) {
429+
s.l.Error(fmt.Sprintf(format, a...))
437430

438431
return nil
439432
}
440433

441-
// Warningf implements service.Logger interface for sysLogger.
442-
func (sysLogger) Warningf(format string, a ...any) error {
443-
log.Info("warning: %s", fmt.Sprintf(format, a...))
434+
// Warningf implements the [service.Logger] interface for sysLogger.
435+
func (s *sysLogger) Warningf(format string, a ...any) (err error) {
436+
s.l.Warn(fmt.Sprintf(format, a...))
444437

445438
return nil
446439
}
447440

448-
// Infof implements service.Logger interface for sysLogger.
449-
func (sysLogger) Infof(format string, a ...any) error {
450-
log.Info(format, a...)
441+
// Infof implements the [service.Logger] interface for sysLogger.
442+
func (s *sysLogger) Infof(format string, a ...any) (err error) {
443+
s.l.Info(fmt.Sprintf(format, a...))
451444

452445
return nil
453446
}

0 commit comments

Comments
 (0)