@@ -113,31 +113,23 @@ func Main(clientBuildFS fs.FS) {
113
113
signals := make (chan os.Signal , 1 )
114
114
signal .Notify (signals , syscall .SIGINT , syscall .SIGTERM , syscall .SIGHUP , syscall .SIGQUIT )
115
115
116
- go func () {
117
- ctx := context .Background ()
118
- for {
119
- sig := <- signals
120
- log .Info ("Received signal %q" , sig )
121
- switch sig {
122
- case syscall .SIGHUP :
123
- globalContext .clients .storage .ReloadARP (ctx )
124
- globalContext .tls .reload ()
125
- default :
126
- cleanup (ctx )
127
- cleanupAlways ()
128
- close (done )
129
- }
130
- }
131
- }()
116
+ ctx := context .Background ()
117
+ sigHdlr := newSignalHandler (signals , func (ctx context.Context ) {
118
+ cleanup (ctx )
119
+ cleanupAlways ()
120
+ close (done )
121
+ })
122
+
123
+ go sigHdlr .handle (ctx )
132
124
133
125
if opts .serviceControlAction != "" {
134
- handleServiceControlAction (opts , clientBuildFS , signals , done )
126
+ handleServiceControlAction (opts , clientBuildFS , signals , done , sigHdlr )
135
127
136
128
return
137
129
}
138
130
139
131
// run the protection
140
- run (opts , clientBuildFS , done )
132
+ run (opts , clientBuildFS , done , sigHdlr )
141
133
}
142
134
143
135
// setupContext initializes [globalContext] fields. It also reads and upgrades
@@ -278,7 +270,11 @@ func setupOpts(opts options) (err error) {
278
270
}
279
271
280
272
// initContextClients initializes Context clients and related fields.
281
- func initContextClients (ctx context.Context , logger * slog.Logger ) (err error ) {
273
+ func initContextClients (
274
+ ctx context.Context ,
275
+ logger * slog.Logger ,
276
+ sigHdlr * signalHandler ,
277
+ ) (err error ) {
282
278
err = setupDNSFilteringConf (ctx , logger , config .Filtering )
283
279
if err != nil {
284
280
// Don't wrap the error, because it's informative enough as is.
@@ -313,6 +309,7 @@ func initContextClients(ctx context.Context, logger *slog.Logger) (err error) {
313
309
globalContext .etcHosts ,
314
310
arpDB ,
315
311
config .Filtering ,
312
+ sigHdlr ,
316
313
)
317
314
}
318
315
@@ -583,7 +580,7 @@ func fatalOnError(err error) {
583
580
// run configures and starts AdGuard Home.
584
581
//
585
582
// TODO(e.burkov): Make opts a pointer.
586
- func run (opts options , clientBuildFS fs.FS , done chan struct {}) {
583
+ func run (opts options , clientBuildFS fs.FS , done chan struct {}, sigHdlr * signalHandler ) {
587
584
// Configure working dir.
588
585
err := initWorkingDir (opts )
589
586
fatalOnError (err )
@@ -599,6 +596,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
599
596
600
597
// TODO(a.garipov): Use slog everywhere.
601
598
slogLogger := newSlogLogger (ls )
599
+ sigHdlr .swapLogger (slogLogger )
602
600
603
601
// Print the first message after logger is configured.
604
602
log .Info (version .Full ())
@@ -621,7 +619,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
621
619
// TODO(s.chzhen): Use it for the entire initialization process.
622
620
ctx := context .Background ()
623
621
624
- err = initContextClients (ctx , slogLogger )
622
+ err = initContextClients (ctx , slogLogger , sigHdlr )
625
623
fatalOnError (err )
626
624
627
625
err = setupOpts (opts )
@@ -664,6 +662,8 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
664
662
onConfigModified ()
665
663
}
666
664
665
+ sigHdlr .addTLSManager (globalContext .tls )
666
+
667
667
globalContext .web , err = initWeb (ctx , opts , clientBuildFS , upd , slogLogger , customURL )
668
668
fatalOnError (err )
669
669
0 commit comments