diff --git a/cmd/main.go b/cmd/main.go index b34185e46e..de1a1fdb99 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -440,16 +440,29 @@ func main() { err = mainHandler.Start(ctx) if err != nil { logger.L().Ctx(ctx).Error("error starting the container watcher", helpers.Error(err)) - switch { - case strings.Contains(err.Error(), utils.ErrKernelVersion): - os.Exit(utils.ExitCodeIncompatibleKernel) - case strings.Contains(err.Error(), utils.ErrMacOS): - os.Exit(utils.ExitCodeMacOS) - default: - os.Exit(utils.ExitCodeError) + + // Container watcher can fail only when FIM mode is enabled + // FIM (File Integrity Manager) can run standalone without container monitoring + if cfg.EnableFIM { + logger.L().Ctx(ctx).Warning("container watcher failed but continuing in FIM-only mode", helpers.Error(err)) + logger.L().Ctx(ctx).Warning("running in FIM-only mode - container monitoring is disabled") + } else { + // Container watcher is critical - fail startup + switch { + case strings.Contains(err.Error(), utils.ErrKernelVersion): + os.Exit(utils.ExitCodeIncompatibleKernel) + case strings.Contains(err.Error(), utils.ErrMacOS): + os.Exit(utils.ExitCodeMacOS) + default: + os.Exit(utils.ExitCodeError) + } } } - defer mainHandler.Stop() + + // Only defer Stop() if Start succeeded or we're in FIM-only mode + if err == nil || cfg.EnableFIM { + defer mainHandler.Stop() + } // start watching dWatcher.Start(ctx) diff --git a/pkg/containerwatcher/v2/container_watcher.go b/pkg/containerwatcher/v2/container_watcher.go index 7f0cdcfd52..841786152a 100644 --- a/pkg/containerwatcher/v2/container_watcher.go +++ b/pkg/containerwatcher/v2/container_watcher.go @@ -290,11 +290,16 @@ func (cw *ContainerWatcher) Start(ctx context.Context) error { cw.containerProfileManager.RegisterForContainerEndOfLife(cw.containerEolNotificationChannel) // Start container collection (similar to v1 startContainerCollection) + var containerCollectionErr error logger.L().TimedWrapper("StartContainerCollection", 5*time.Second, func() { if err := cw.StartContainerCollection(ctx); err != nil { + containerCollectionErr = err logger.L().Error("error starting container collection", helpers.Error(err)) } }) + if containerCollectionErr != nil { + return fmt.Errorf("starting container collection: %w", containerCollectionErr) + } // Start ordered event queue BEFORE tracers @@ -306,7 +311,7 @@ func (cw *ContainerWatcher) Start(ctx context.Context) error { cw.gadgetRuntime = local.New() if err := cw.gadgetRuntime.Init(nil); err != nil { - logger.L().Fatal("runtime init", helpers.Error(err)) + return fmt.Errorf("initializing gadget runtime: %w", err) } // Create tracer factory