github.com/inspektor-gadget/inspektor-gadget@v0.28.1/cmd/ig/daemon.go (about)

     1  // Copyright 2023 The Inspektor Gadget authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"os/user"
    21  	"path/filepath"
    22  	"strconv"
    23  
    24  	log "github.com/sirupsen/logrus"
    25  	"github.com/spf13/cobra"
    26  
    27  	gadgetservice "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-service"
    28  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-service/api"
    29  	"github.com/inspektor-gadget/inspektor-gadget/pkg/runtime"
    30  )
    31  
    32  func newDaemonCommand(runtime runtime.Runtime) *cobra.Command {
    33  	daemonCmd := &cobra.Command{
    34  		Use:          "daemon",
    35  		Short:        "Run Inspektor Gadget as a daemon",
    36  		Args:         cobra.NoArgs,
    37  		SilenceUsage: true,
    38  	}
    39  
    40  	var socket string
    41  	var group string
    42  	var eventBufferLength uint64
    43  
    44  	daemonCmd.PersistentFlags().StringVarP(
    45  		&group,
    46  		"group",
    47  		"G",
    48  		"0",
    49  		"Group name or id that the unix socket should use (if daemon-socket is set to e.g. unix:///path/to.socket)")
    50  
    51  	daemonCmd.PersistentFlags().StringVarP(
    52  		&socket,
    53  		"host",
    54  		"H",
    55  		api.DefaultDaemonPath,
    56  		"The socket to listen on for new requests. Can be a unix socket"+
    57  			" (unix:///path/to.socket) or a tcp socket (tcp://127.0.0.1:1234)")
    58  
    59  	daemonCmd.PersistentFlags().Uint64VarP(
    60  		&eventBufferLength,
    61  		"events-buffer-length",
    62  		"",
    63  		16384,
    64  		"The events buffer length. A low value could impact horizontal scaling.")
    65  
    66  	daemonCmd.RunE = func(cmd *cobra.Command, args []string) error {
    67  		if os.Geteuid() != 0 {
    68  			return fmt.Errorf("%s must be run as root to be able to run eBPF programs", filepath.Base(os.Args[0]))
    69  		}
    70  
    71  		socketType, socketPath, err := api.ParseSocketAddress(socket)
    72  		if err != nil {
    73  			return fmt.Errorf("invalid daemon-socket address: %w", err)
    74  		}
    75  
    76  		gid := 0
    77  		if tmpGroup, err := user.LookupGroup(group); err == nil {
    78  			gid, err = strconv.Atoi(tmpGroup.Gid)
    79  			if err != nil {
    80  				return fmt.Errorf("unexpected non-numeric group id %q for group %q", tmpGroup.Gid, group)
    81  			}
    82  		} else if tmpGid, err := strconv.Atoi(group); err == nil {
    83  			gid = tmpGid
    84  		} else {
    85  			return fmt.Errorf("group %q not found", group)
    86  		}
    87  
    88  		log.Infof("starting Inspektor Gadget daemon at %q", socket)
    89  		service := gadgetservice.NewService(log.StandardLogger(), eventBufferLength)
    90  		return service.Run(gadgetservice.RunConfig{
    91  			SocketType: socketType,
    92  			SocketPath: socketPath,
    93  			SocketGID:  gid,
    94  		})
    95  	}
    96  
    97  	return daemonCmd
    98  }