github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadgets/trace/open/tracer/utils.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 tracer
    16  
    17  var flagNames = []string{
    18  	"O_CREAT",
    19  	"O_EXCL",
    20  	"O_NOCTTY",
    21  	"O_TRUNC",
    22  	"O_APPEND",
    23  	"O_NONBLOCK",
    24  	"O_DSYNC",
    25  	"O_FASYNC",
    26  	"O_DIRECT",
    27  	"O_LARGEFILE",
    28  	"O_DIRECTORY",
    29  	"O_NOFOLLOW",
    30  	"O_NOATIME",
    31  	"O_CLOEXEC",
    32  }
    33  
    34  func DecodeFlags(flags int32) []string {
    35  	flagsStr := []string{}
    36  
    37  	// We first need to deal with the first 3 bits which indicates access mode.
    38  	switch flags & 0b11 {
    39  	case 0:
    40  		flagsStr = append(flagsStr, "O_RDONLY")
    41  	case 1:
    42  		flagsStr = append(flagsStr, "O_WRONLY")
    43  	case 2:
    44  		flagsStr = append(flagsStr, "O_RDWR")
    45  	}
    46  
    47  	// Then, we need to remove the last 6 bits and we can deal with the other
    48  	// flags.
    49  	// Indeed, O_CREAT is defined as 00000100, see:
    50  	// https://github.com/torvalds/linux/blob/9d646009f65d/include/uapi/asm-generic/fcntl.h#L24
    51  	flags >>= 6
    52  	for i, val := range flagNames {
    53  		if (1<<i)&flags == 0 {
    54  			continue
    55  		}
    56  		flagsStr = append(flagsStr, val)
    57  	}
    58  
    59  	return flagsStr
    60  }