gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/strace/internal/abi/abi_unix.go (about)

     1  // Copyright 2018 Google LLC.
     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 abi
    16  
    17  import (
    18  	"fmt"
    19  	"syscall"
    20  
    21  	"golang.org/x/sys/unix"
    22  )
    23  
    24  // OpenMode represents the mode to open(2) a file.
    25  var OpenMode = FlagSet{
    26  	&Value{
    27  		Value: syscall.O_RDWR,
    28  		Name:  "O_RDWR",
    29  	},
    30  	&Value{
    31  		Value: syscall.O_WRONLY,
    32  		Name:  "O_WRONLY",
    33  	},
    34  	&Value{
    35  		Value: syscall.O_RDONLY,
    36  		Name:  "O_RDONLY",
    37  	},
    38  }
    39  
    40  // OpenFlagSet is the set of open(2) flags.
    41  var OpenFlagSet = FlagSet{
    42  	&BitFlag{
    43  		Value: syscall.O_APPEND,
    44  		Name:  "O_APPEND",
    45  	},
    46  	&BitFlag{
    47  		Value: syscall.O_ASYNC,
    48  		Name:  "O_ASYNC",
    49  	},
    50  	&BitFlag{
    51  		Value: syscall.O_CLOEXEC,
    52  		Name:  "O_CLOEXEC",
    53  	},
    54  	&BitFlag{
    55  		Value: syscall.O_CREAT,
    56  		Name:  "O_CREAT",
    57  	},
    58  	&BitFlag{
    59  		Value: syscall.O_DIRECT,
    60  		Name:  "O_DIRECT",
    61  	},
    62  	&BitFlag{
    63  		Value: syscall.O_DIRECTORY,
    64  		Name:  "O_DIRECTORY",
    65  	},
    66  	&BitFlag{
    67  		Value: syscall.O_EXCL,
    68  		Name:  "O_EXCL",
    69  	},
    70  	&BitFlag{
    71  		Value: syscall.O_NOATIME,
    72  		Name:  "O_NOATIME",
    73  	},
    74  	&BitFlag{
    75  		Value: syscall.O_NOCTTY,
    76  		Name:  "O_NOCTTY",
    77  	},
    78  	&BitFlag{
    79  		Value: syscall.O_NOFOLLOW,
    80  		Name:  "O_NOFOLLOW",
    81  	},
    82  	&BitFlag{
    83  		Value: syscall.O_NONBLOCK,
    84  		Name:  "O_NONBLOCK",
    85  	},
    86  	&BitFlag{
    87  		Value: 0x200000, // O_PATH
    88  		Name:  "O_PATH",
    89  	},
    90  	&BitFlag{
    91  		Value: syscall.O_SYNC,
    92  		Name:  "O_SYNC",
    93  	},
    94  	&BitFlag{
    95  		Value: syscall.O_TRUNC,
    96  		Name:  "O_TRUNC",
    97  	},
    98  }
    99  
   100  func Open(val uint64) string {
   101  	s := OpenMode.Parse(val & syscall.O_ACCMODE)
   102  	if flags := OpenFlagSet.Parse(val &^ syscall.O_ACCMODE); flags != "" {
   103  		s += "|" + flags
   104  	}
   105  	return s
   106  }
   107  
   108  // socket
   109  
   110  // SocketFamily are the possible socket(2) families.
   111  var SocketFamily = FlagSet{
   112  	&Value{
   113  		Value: unix.AF_UNSPEC,
   114  		Name:  "AF_UNSPEC",
   115  	},
   116  	&Value{
   117  		Value: unix.AF_UNIX,
   118  		Name:  "AF_UNIX",
   119  	},
   120  	&Value{
   121  		Value: unix.AF_INET,
   122  		Name:  "AF_INET",
   123  	},
   124  	&Value{
   125  		Value: unix.AF_AX25,
   126  		Name:  "AF_AX25",
   127  	},
   128  	&Value{
   129  		Value: unix.AF_IPX,
   130  		Name:  "AF_IPX",
   131  	},
   132  	&Value{
   133  		Value: unix.AF_APPLETALK,
   134  		Name:  "AF_APPLETALK",
   135  	},
   136  	&Value{
   137  		Value: unix.AF_NETROM,
   138  		Name:  "AF_NETROM",
   139  	},
   140  	&Value{
   141  		Value: unix.AF_BRIDGE,
   142  		Name:  "AF_BRIDGE",
   143  	},
   144  	&Value{
   145  		Value: unix.AF_ATMPVC,
   146  		Name:  "AF_ATMPVC",
   147  	},
   148  	&Value{
   149  		Value: unix.AF_X25,
   150  		Name:  "AF_X25",
   151  	},
   152  	&Value{
   153  		Value: unix.AF_INET6,
   154  		Name:  "AF_INET6",
   155  	},
   156  	&Value{
   157  		Value: unix.AF_ROSE,
   158  		Name:  "AF_ROSE",
   159  	},
   160  	&Value{
   161  		Value: unix.AF_DECnet,
   162  		Name:  "AF_DECnet",
   163  	},
   164  	&Value{
   165  		Value: unix.AF_NETBEUI,
   166  		Name:  "AF_NETBEUI",
   167  	},
   168  	&Value{
   169  		Value: unix.AF_SECURITY,
   170  		Name:  "AF_SECURITY",
   171  	},
   172  	&Value{
   173  		Value: unix.AF_KEY,
   174  		Name:  "AF_KEY",
   175  	},
   176  	&Value{
   177  		Value: unix.AF_NETLINK,
   178  		Name:  "AF_NETLINK",
   179  	},
   180  	&Value{
   181  		Value: unix.AF_PACKET,
   182  		Name:  "AF_PACKET",
   183  	},
   184  	&Value{
   185  		Value: unix.AF_ASH,
   186  		Name:  "AF_ASH",
   187  	},
   188  	&Value{
   189  		Value: unix.AF_ECONET,
   190  		Name:  "AF_ECONET",
   191  	},
   192  	&Value{
   193  		Value: unix.AF_ATMSVC,
   194  		Name:  "AF_ATMSVC",
   195  	},
   196  	&Value{
   197  		Value: unix.AF_RDS,
   198  		Name:  "AF_RDS",
   199  	},
   200  	&Value{
   201  		Value: unix.AF_SNA,
   202  		Name:  "AF_SNA",
   203  	},
   204  	&Value{
   205  		Value: unix.AF_IRDA,
   206  		Name:  "AF_IRDA",
   207  	},
   208  	&Value{
   209  		Value: unix.AF_PPPOX,
   210  		Name:  "AF_PPPOX",
   211  	},
   212  	&Value{
   213  		Value: unix.AF_WANPIPE,
   214  		Name:  "AF_WANPIPE",
   215  	},
   216  	&Value{
   217  		Value: unix.AF_LLC,
   218  		Name:  "AF_LLC",
   219  	},
   220  	&Value{
   221  		Value: unix.AF_IB,
   222  		Name:  "AF_IB",
   223  	},
   224  	&Value{
   225  		Value: unix.AF_MPLS,
   226  		Name:  "AF_MPLS",
   227  	},
   228  	&Value{
   229  		Value: unix.AF_CAN,
   230  		Name:  "AF_CAN",
   231  	},
   232  	&Value{
   233  		Value: unix.AF_TIPC,
   234  		Name:  "AF_TIPC",
   235  	},
   236  	&Value{
   237  		Value: unix.AF_BLUETOOTH,
   238  		Name:  "AF_BLUETOOTH",
   239  	},
   240  	&Value{
   241  		Value: unix.AF_IUCV,
   242  		Name:  "AF_IUCV",
   243  	},
   244  	&Value{
   245  		Value: unix.AF_RXRPC,
   246  		Name:  "AF_RXRPC",
   247  	},
   248  	&Value{
   249  		Value: unix.AF_ISDN,
   250  		Name:  "AF_ISDN",
   251  	},
   252  	&Value{
   253  		Value: unix.AF_PHONET,
   254  		Name:  "AF_PHONET",
   255  	},
   256  	&Value{
   257  		Value: unix.AF_IEEE802154,
   258  		Name:  "AF_IEEE802154",
   259  	},
   260  	&Value{
   261  		Value: unix.AF_CAIF,
   262  		Name:  "AF_CAIF",
   263  	},
   264  	&Value{
   265  		Value: unix.AF_ALG,
   266  		Name:  "AF_ALG",
   267  	},
   268  	&Value{
   269  		Value: unix.AF_NFC,
   270  		Name:  "AF_NFC",
   271  	},
   272  	&Value{
   273  		Value: unix.AF_VSOCK,
   274  		Name:  "AF_VSOCK",
   275  	},
   276  }
   277  
   278  // SocketType are the possible socket(2) types.
   279  var SocketType = FlagSet{
   280  	&Value{
   281  		Value: unix.SOCK_STREAM,
   282  		Name:  "SOCK_STREAM",
   283  	},
   284  	&Value{
   285  		Value: unix.SOCK_DGRAM,
   286  		Name:  "SOCK_DGRAM",
   287  	},
   288  	&Value{
   289  		Value: unix.SOCK_RAW,
   290  		Name:  "SOCK_RAW",
   291  	},
   292  	&Value{
   293  		Value: unix.SOCK_RDM,
   294  		Name:  "SOCK_RDM",
   295  	},
   296  	&Value{
   297  		Value: unix.SOCK_SEQPACKET,
   298  		Name:  "SOCK_SEQPACKET",
   299  	},
   300  	&Value{
   301  		Value: unix.SOCK_DCCP,
   302  		Name:  "SOCK_DCCP",
   303  	},
   304  	&Value{
   305  		Value: unix.SOCK_PACKET,
   306  		Name:  "SOCK_PACKET",
   307  	},
   308  }
   309  
   310  // SocketFlagSet are the possible socket(2) flags.
   311  var SocketFlagSet = FlagSet{
   312  	&BitFlag{
   313  		Value: unix.SOCK_CLOEXEC,
   314  		Name:  "SOCK_CLOEXEC",
   315  	},
   316  	&BitFlag{
   317  		Value: unix.SOCK_NONBLOCK,
   318  		Name:  "SOCK_NONBLOCK",
   319  	},
   320  }
   321  
   322  // ipProtocol are the possible socket(2) types for INET and INET6 sockets.
   323  var ipProtocol = FlagSet{
   324  	&Value{
   325  		Value: unix.IPPROTO_IP,
   326  		Name:  "IPPROTO_IP",
   327  	},
   328  	&Value{
   329  		Value: unix.IPPROTO_ICMP,
   330  		Name:  "IPPROTO_ICMP",
   331  	},
   332  	&Value{
   333  		Value: unix.IPPROTO_IGMP,
   334  		Name:  "IPPROTO_IGMP",
   335  	},
   336  	&Value{
   337  		Value: unix.IPPROTO_IPIP,
   338  		Name:  "IPPROTO_IPIP",
   339  	},
   340  	&Value{
   341  		Value: unix.IPPROTO_TCP,
   342  		Name:  "IPPROTO_TCP",
   343  	},
   344  	&Value{
   345  		Value: unix.IPPROTO_EGP,
   346  		Name:  "IPPROTO_EGP",
   347  	},
   348  	&Value{
   349  		Value: unix.IPPROTO_PUP,
   350  		Name:  "IPPROTO_PUP",
   351  	},
   352  	&Value{
   353  		Value: unix.IPPROTO_UDP,
   354  		Name:  "IPPROTO_UDP",
   355  	},
   356  	&Value{
   357  		Value: unix.IPPROTO_IDP,
   358  		Name:  "IPPROTO_IDP",
   359  	},
   360  	&Value{
   361  		Value: unix.IPPROTO_TP,
   362  		Name:  "IPPROTO_TP",
   363  	},
   364  	&Value{
   365  		Value: unix.IPPROTO_DCCP,
   366  		Name:  "IPPROTO_DCCP",
   367  	},
   368  	&Value{
   369  		Value: unix.IPPROTO_IPV6,
   370  		Name:  "IPPROTO_IPV6",
   371  	},
   372  	&Value{
   373  		Value: unix.IPPROTO_RSVP,
   374  		Name:  "IPPROTO_RSVP",
   375  	},
   376  	&Value{
   377  		Value: unix.IPPROTO_GRE,
   378  		Name:  "IPPROTO_GRE",
   379  	},
   380  	&Value{
   381  		Value: unix.IPPROTO_ESP,
   382  		Name:  "IPPROTO_ESP",
   383  	},
   384  	&Value{
   385  		Value: unix.IPPROTO_AH,
   386  		Name:  "IPPROTO_AH",
   387  	},
   388  	&Value{
   389  		Value: unix.IPPROTO_MTP,
   390  		Name:  "IPPROTO_MTP",
   391  	},
   392  	&Value{
   393  		Value: unix.IPPROTO_BEETPH,
   394  		Name:  "IPPROTO_BEETPH",
   395  	},
   396  	&Value{
   397  		Value: unix.IPPROTO_ENCAP,
   398  		Name:  "IPPROTO_ENCAP",
   399  	},
   400  	&Value{
   401  		Value: unix.IPPROTO_PIM,
   402  		Name:  "IPPROTO_PIM",
   403  	},
   404  	&Value{
   405  		Value: unix.IPPROTO_COMP,
   406  		Name:  "IPPROTO_COMP",
   407  	},
   408  	&Value{
   409  		Value: unix.IPPROTO_SCTP,
   410  		Name:  "IPPROTO_SCTP",
   411  	},
   412  	&Value{
   413  		Value: unix.IPPROTO_UDPLITE,
   414  		Name:  "IPPROTO_UDPLITE",
   415  	},
   416  	&Value{
   417  		Value: unix.IPPROTO_MPLS,
   418  		Name:  "IPPROTO_MPLS",
   419  	},
   420  	&Value{
   421  		Value: unix.IPPROTO_RAW,
   422  		Name:  "IPPROTO_RAW",
   423  	},
   424  }
   425  
   426  // SocketProtocol are the possible socket(2) protocols for each protocol family.
   427  var SocketProtocol = map[int32]FlagSet{
   428  	unix.AF_INET:  ipProtocol,
   429  	unix.AF_INET6: ipProtocol,
   430  	unix.AF_NETLINK: {
   431  		&Value{
   432  			Value: unix.NETLINK_ROUTE,
   433  			Name:  "NETLINK_ROUTE",
   434  		},
   435  		&Value{
   436  			Value: unix.NETLINK_UNUSED,
   437  			Name:  "NETLINK_UNUSED",
   438  		},
   439  		&Value{
   440  			Value: unix.NETLINK_USERSOCK,
   441  			Name:  "NETLINK_USERSOCK",
   442  		},
   443  		&Value{
   444  			Value: unix.NETLINK_FIREWALL,
   445  			Name:  "NETLINK_FIREWALL",
   446  		},
   447  		&Value{
   448  			Value: unix.NETLINK_SOCK_DIAG,
   449  			Name:  "NETLINK_SOCK_DIAG",
   450  		},
   451  		&Value{
   452  			Value: unix.NETLINK_NFLOG,
   453  			Name:  "NETLINK_NFLOG",
   454  		},
   455  		&Value{
   456  			Value: unix.NETLINK_XFRM,
   457  			Name:  "NETLINK_XFRM",
   458  		},
   459  		&Value{
   460  			Value: unix.NETLINK_SELINUX,
   461  			Name:  "NETLINK_SELINUX",
   462  		},
   463  		&Value{
   464  			Value: unix.NETLINK_ISCSI,
   465  			Name:  "NETLINK_ISCSI",
   466  		},
   467  		&Value{
   468  			Value: unix.NETLINK_AUDIT,
   469  			Name:  "NETLINK_AUDIT",
   470  		},
   471  		&Value{
   472  			Value: unix.NETLINK_FIB_LOOKUP,
   473  			Name:  "NETLINK_FIB_LOOKUP",
   474  		},
   475  		&Value{
   476  			Value: unix.NETLINK_CONNECTOR,
   477  			Name:  "NETLINK_CONNECTOR",
   478  		},
   479  		&Value{
   480  			Value: unix.NETLINK_NETFILTER,
   481  			Name:  "NETLINK_NETFILTER",
   482  		},
   483  		&Value{
   484  			Value: unix.NETLINK_IP6_FW,
   485  			Name:  "NETLINK_IP6_FW",
   486  		},
   487  		&Value{
   488  			Value: unix.NETLINK_DNRTMSG,
   489  			Name:  "NETLINK_DNRTMSG",
   490  		},
   491  		&Value{
   492  			Value: unix.NETLINK_KOBJECT_UEVENT,
   493  			Name:  "NETLINK_KOBJECT_UEVENT",
   494  		},
   495  		&Value{
   496  			Value: unix.NETLINK_GENERIC,
   497  			Name:  "NETLINK_GENERIC",
   498  		},
   499  		&Value{
   500  			Value: unix.NETLINK_SCSITRANSPORT,
   501  			Name:  "NETLINK_SCSITRANSPORT",
   502  		},
   503  		&Value{
   504  			Value: unix.NETLINK_ECRYPTFS,
   505  			Name:  "NETLINK_ECRYPTFS",
   506  		},
   507  		&Value{
   508  			Value: unix.NETLINK_RDMA,
   509  			Name:  "NETLINK_RDMA",
   510  		},
   511  		&Value{
   512  			Value: unix.NETLINK_CRYPTO,
   513  			Name:  "NETLINK_CRYPTO",
   514  		},
   515  	},
   516  }
   517  
   518  var ControlMessageType = map[int32]string{
   519  	unix.SCM_RIGHTS:      "SCM_RIGHTS",
   520  	unix.SCM_CREDENTIALS: "SCM_CREDENTIALS",
   521  	unix.SO_TIMESTAMP:    "SO_TIMESTAMP",
   522  }
   523  
   524  func SockType(stype int32) string {
   525  	s := SocketType.Parse(uint64(stype & SOCK_TYPE_MASK))
   526  	if flags := SocketFlagSet.Parse(uint64(stype &^ SOCK_TYPE_MASK)); flags != "" {
   527  		s += "|" + flags
   528  	}
   529  	return s
   530  }
   531  
   532  func SockProtocol(family, protocol int32) string {
   533  	protocols, ok := SocketProtocol[family]
   534  	if !ok {
   535  		return fmt.Sprintf("%#x", protocol)
   536  	}
   537  	return protocols.Parse(uint64(protocol))
   538  }
   539  
   540  func SockFlags(flags int32) string {
   541  	if flags == 0 {
   542  		return "0"
   543  	}
   544  	return SocketFlagSet.Parse(uint64(flags))
   545  }
   546  
   547  // MessageHeader64 is the 64-bit representation of the msghdr struct used in
   548  // the recvmsg and sendmsg syscalls.
   549  type MessageHeader64 struct {
   550  	// Name is the optional pointer to a network address buffer.
   551  	Name uint64
   552  
   553  	// NameLen is the length of the buffer pointed to by Name.
   554  	NameLen uint32
   555  	_       uint32
   556  
   557  	// Iov is a pointer to an array of io vectors that describe the memory
   558  	// locations involved in the io operation.
   559  	Iov uint64
   560  
   561  	// IovLen is the length of the array pointed to by Iov.
   562  	IovLen uint64
   563  
   564  	// Control is the optional pointer to ancillary control data.
   565  	Control uint64
   566  
   567  	// ControlLen is the length of the data pointed to by Control.
   568  	ControlLen uint64
   569  
   570  	// Flags on the sent/received message.
   571  	Flags int32
   572  	_     int32
   573  }