github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/utils/netns/netns.go (about)

     1  // Copyright 2024 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 netns is a small wrapper around github.com/vishvananda/netns that provides
    16  // GetFromPidWithAltProcfs() and GetFromThreadWithAltProcfs().
    17  // TODO: Remove once https://github.com/vishvananda/netns/pull/76 is merged
    18  package netns
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/vishvananda/netns"
    24  )
    25  
    26  // GetFromPidWithAltProcfs gets a handle to the network namespace of a given
    27  // pid using the specified procfs path.
    28  func GetFromPidWithAltProcfs(pid int, procfs string) (netns.NsHandle, error) {
    29  	return netns.GetFromPath(fmt.Sprintf("%s/%d/ns/net", procfs, pid))
    30  }
    31  
    32  // GetFromThreadWithAltProcfs gets a handle to the network namespace of a given
    33  // pid and tid using the specified procfs path.
    34  func GetFromThreadWithAltProcfs(pid, tid int, procfs string) (netns.NsHandle, error) {
    35  	return netns.GetFromPath(fmt.Sprintf("%s/%d/task/%d/ns/net", procfs, pid, tid))
    36  }