@@ -119,6 +119,9 @@ func stopProfile() {
119
119
}
120
120
121
121
func tetragonExecute () error {
122
+ ctx , cancel := context .WithCancel (context .Background ())
123
+ defer cancel ()
124
+
122
125
sigs := make (chan os.Signal , 1 )
123
126
signal .Notify (sigs , syscall .SIGINT , syscall .SIGTERM )
124
127
@@ -143,6 +146,12 @@ func tetragonExecute() error {
143
146
defaults .NetnsDir = viper .GetString (keyNetnsDir )
144
147
}
145
148
149
+ // Setup file system mounts
150
+ bpf .CheckOrMountFS ("" )
151
+ bpf .CheckOrMountDebugFS ()
152
+ bpf .CheckOrMountCgroup2 ()
153
+
154
+ // Start profilers first as we have to capture them in signal handling
146
155
if memProfile != "" {
147
156
log .WithField ("file" , memProfile ).Info ("Starting mem profiling" )
148
157
}
@@ -160,25 +169,35 @@ func tetragonExecute() error {
160
169
log .WithField ("file" , cpuProfile ).Info ("Starting cpu profiling" )
161
170
}
162
171
163
- bpf .CheckOrMountFS ("" )
164
- bpf .CheckOrMountDebugFS ()
165
- bpf .CheckOrMountCgroup2 ()
166
-
167
- sensors .LogRegisteredSensorsAndProbes ()
172
+ defer stopProfile ()
168
173
174
+ // Raise memory resource
169
175
bpf .ConfigureResourceLimits ()
176
+
177
+ // Get observer bpf maps and programs directory
170
178
observerDir := getObserverDir ()
171
179
option .Config .BpfDir = observerDir
172
180
option .Config .MapDir = observerDir
181
+
182
+ // Get observer from configFile
173
183
obs := observer .NewObserver (configFile )
184
+ defer func () {
185
+ obs .PrintStats ()
186
+ obs .RemovePrograms ()
187
+ }()
188
+
189
+ go func () {
190
+ s := <- sigs
191
+ log .Infof ("Received signal %s, shutting down..." , s )
192
+ cancel ()
193
+ }()
194
+
195
+ sensors .LogRegisteredSensorsAndProbes ()
196
+
174
197
if err := obs .InitSensorManager (); err != nil {
175
198
return err
176
199
}
177
200
178
- ctx , cancel := context .WithCancel (context .Background ())
179
- defer cancel ()
180
- var cancelWg sync.WaitGroup
181
-
182
201
/* Remove any stale programs, otherwise feature set change can cause
183
202
* old programs to linger resulting in undefined behavior. And because
184
203
* we recapture current running state from proc and/or have cache of
@@ -209,6 +228,9 @@ func tetragonExecute() error {
209
228
return err
210
229
}
211
230
231
+ var cancelWg sync.WaitGroup
232
+ defer cancelWg .Wait ()
233
+
212
234
pm , err := tetragonGrpc .NewProcessManager (
213
235
ctx ,
214
236
& cancelWg ,
@@ -226,16 +248,6 @@ func tetragonExecute() error {
226
248
}
227
249
}
228
250
229
- go func () {
230
- <- sigs
231
- obs .PrintStats ()
232
- obs .RemovePrograms ()
233
- stopProfile ()
234
- cancel ()
235
- cancelWg .Wait ()
236
- os .Exit (1 )
237
- }()
238
-
239
251
log .WithField ("enabled" , exportFilename != "" ).WithField ("fileName" , exportFilename ).Info ("Exporter configuration" )
240
252
obs .AddListener (pm )
241
253
saveInitInfo ()
0 commit comments