Text file src/cmd/vendor/golang.org/x/sys/unix/mkerrors.sh

     1  #!/usr/bin/env bash
     2  # Copyright 2009 The Go Authors. All rights reserved.
     3  # Use of this source code is governed by a BSD-style
     4  # license that can be found in the LICENSE file.
     5  
     6  # Generate Go code listing errors and other #defined constant
     7  # values (ENAMETOOLONG etc.), by asking the preprocessor
     8  # about the definitions.
     9  
    10  unset LANG
    11  export LC_ALL=C
    12  export LC_CTYPE=C
    13  
    14  if test -z "$GOARCH" -o -z "$GOOS"; then
    15  	echo 1>&2 "GOARCH or GOOS not defined in environment"
    16  	exit 1
    17  fi
    18  
    19  # Check that we are using the new build system if we should
    20  if [[ "$GOOS" = "linux" ]] && [[ "$GOLANG_SYS_BUILD" != "docker" ]]; then
    21  	echo 1>&2 "In the Docker based build system, mkerrors should not be called directly."
    22  	echo 1>&2 "See README.md"
    23  	exit 1
    24  fi
    25  
    26  if [[ "$GOOS" = "aix" ]]; then
    27  	CC=${CC:-gcc}
    28  else
    29  	CC=${CC:-cc}
    30  fi
    31  
    32  if [[ "$GOOS" = "solaris" ]]; then
    33  	# Assumes GNU versions of utilities in PATH.
    34  	export PATH=/usr/gnu/bin:$PATH
    35  fi
    36  
    37  uname=$(uname)
    38  
    39  includes_AIX='
    40  #include <net/if.h>
    41  #include <net/netopt.h>
    42  #include <netinet/ip_mroute.h>
    43  #include <sys/protosw.h>
    44  #include <sys/stropts.h>
    45  #include <sys/mman.h>
    46  #include <sys/poll.h>
    47  #include <sys/select.h>
    48  #include <sys/termio.h>
    49  #include <termios.h>
    50  #include <fcntl.h>
    51  
    52  #define AF_LOCAL AF_UNIX
    53  '
    54  
    55  includes_Darwin='
    56  #define _DARWIN_C_SOURCE
    57  #define KERNEL 1
    58  #define _DARWIN_USE_64_BIT_INODE
    59  #define __APPLE_USE_RFC_3542
    60  #include <stdint.h>
    61  #include <sys/stdio.h>
    62  #include <sys/attr.h>
    63  #include <sys/clonefile.h>
    64  #include <sys/kern_control.h>
    65  #include <sys/types.h>
    66  #include <sys/event.h>
    67  #include <sys/ptrace.h>
    68  #include <sys/select.h>
    69  #include <sys/socket.h>
    70  #include <sys/stat.h>
    71  #include <sys/un.h>
    72  #include <sys/sockio.h>
    73  #include <sys/sys_domain.h>
    74  #include <sys/sysctl.h>
    75  #include <sys/mman.h>
    76  #include <sys/mount.h>
    77  #include <sys/utsname.h>
    78  #include <sys/wait.h>
    79  #include <sys/xattr.h>
    80  #include <sys/vsock.h>
    81  #include <net/bpf.h>
    82  #include <net/if.h>
    83  #include <net/if_types.h>
    84  #include <net/route.h>
    85  #include <netinet/in.h>
    86  #include <netinet/ip.h>
    87  #include <termios.h>
    88  
    89  // for backwards compatibility because moved TIOCREMOTE to Kernel.framework after MacOSX12.0.sdk.
    90  #define TIOCREMOTE 0x80047469
    91  '
    92  
    93  includes_DragonFly='
    94  #include <sys/types.h>
    95  #include <sys/event.h>
    96  #include <sys/select.h>
    97  #include <sys/socket.h>
    98  #include <sys/sockio.h>
    99  #include <sys/stat.h>
   100  #include <sys/sysctl.h>
   101  #include <sys/mman.h>
   102  #include <sys/mount.h>
   103  #include <sys/wait.h>
   104  #include <sys/ioctl.h>
   105  #include <net/bpf.h>
   106  #include <net/if.h>
   107  #include <net/if_clone.h>
   108  #include <net/if_types.h>
   109  #include <net/route.h>
   110  #include <netinet/in.h>
   111  #include <termios.h>
   112  #include <netinet/ip.h>
   113  #include <net/ip_mroute/ip_mroute.h>
   114  '
   115  
   116  includes_FreeBSD='
   117  #include <sys/capsicum.h>
   118  #include <sys/param.h>
   119  #include <sys/types.h>
   120  #include <sys/disk.h>
   121  #include <sys/event.h>
   122  #include <sys/sched.h>
   123  #include <sys/select.h>
   124  #include <sys/socket.h>
   125  #include <sys/un.h>
   126  #include <sys/sockio.h>
   127  #include <sys/stat.h>
   128  #include <sys/sysctl.h>
   129  #include <sys/mman.h>
   130  #include <sys/mount.h>
   131  #include <sys/wait.h>
   132  #include <sys/ioctl.h>
   133  #include <sys/ptrace.h>
   134  #include <net/bpf.h>
   135  #include <net/if.h>
   136  #include <net/if_types.h>
   137  #include <net/route.h>
   138  #include <netinet/in.h>
   139  #include <termios.h>
   140  #include <netinet/ip.h>
   141  #include <netinet/ip_mroute.h>
   142  #include <sys/extattr.h>
   143  
   144  #if __FreeBSD__ >= 10
   145  #define IFT_CARP	0xf8	// IFT_CARP is deprecated in FreeBSD 10
   146  #undef SIOCAIFADDR
   147  #define SIOCAIFADDR	_IOW(105, 26, struct oifaliasreq)	// ifaliasreq contains if_data
   148  #undef SIOCSIFPHYADDR
   149  #define SIOCSIFPHYADDR	_IOW(105, 70, struct oifaliasreq)	// ifaliasreq contains if_data
   150  #endif
   151  '
   152  
   153  includes_Linux='
   154  #define _LARGEFILE_SOURCE
   155  #define _LARGEFILE64_SOURCE
   156  #ifndef __LP64__
   157  #define _FILE_OFFSET_BITS 64
   158  #endif
   159  #define _GNU_SOURCE
   160  
   161  // See the description in unix/linux/types.go
   162  #if defined(__ARM_EABI__) || \
   163  	(defined(__mips__) && (_MIPS_SIM == _ABIO32)) || \
   164  	(defined(__powerpc__) && (!defined(__powerpc64__)))
   165  # ifdef   _TIME_BITS
   166  #  undef  _TIME_BITS
   167  # endif
   168  # define  _TIME_BITS 32
   169  #endif
   170  
   171  // <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
   172  // these structures. We just include them copied from <bits/termios.h>.
   173  #if defined(__powerpc__)
   174  struct sgttyb {
   175          char    sg_ispeed;
   176          char    sg_ospeed;
   177          char    sg_erase;
   178          char    sg_kill;
   179          short   sg_flags;
   180  };
   181  
   182  struct tchars {
   183          char    t_intrc;
   184          char    t_quitc;
   185          char    t_startc;
   186          char    t_stopc;
   187          char    t_eofc;
   188          char    t_brkc;
   189  };
   190  
   191  struct ltchars {
   192          char    t_suspc;
   193          char    t_dsuspc;
   194          char    t_rprntc;
   195          char    t_flushc;
   196          char    t_werasc;
   197          char    t_lnextc;
   198  };
   199  #endif
   200  
   201  #include <bits/sockaddr.h>
   202  #include <sys/epoll.h>
   203  #include <sys/eventfd.h>
   204  #include <sys/inotify.h>
   205  #include <sys/ioctl.h>
   206  #include <sys/mman.h>
   207  #include <sys/mount.h>
   208  #include <sys/prctl.h>
   209  #include <sys/stat.h>
   210  #include <sys/types.h>
   211  #include <sys/time.h>
   212  #include <sys/select.h>
   213  #include <sys/signalfd.h>
   214  #include <sys/socket.h>
   215  #include <sys/timerfd.h>
   216  #include <sys/uio.h>
   217  #include <sys/xattr.h>
   218  #include <netinet/udp.h>
   219  #include <linux/audit.h>
   220  #include <linux/bpf.h>
   221  #include <linux/can.h>
   222  #include <linux/can/error.h>
   223  #include <linux/can/netlink.h>
   224  #include <linux/can/raw.h>
   225  #include <linux/capability.h>
   226  #include <linux/cryptouser.h>
   227  #include <linux/devlink.h>
   228  #include <linux/dm-ioctl.h>
   229  #include <linux/errqueue.h>
   230  #include <linux/ethtool_netlink.h>
   231  #include <linux/falloc.h>
   232  #include <linux/fanotify.h>
   233  #include <linux/fib_rules.h>
   234  #include <linux/filter.h>
   235  #include <linux/fs.h>
   236  #include <linux/fscrypt.h>
   237  #include <linux/fsverity.h>
   238  #include <linux/genetlink.h>
   239  #include <linux/hdreg.h>
   240  #include <linux/hidraw.h>
   241  #include <linux/if.h>
   242  #include <linux/if_addr.h>
   243  #include <linux/if_alg.h>
   244  #include <linux/if_arp.h>
   245  #include <linux/if_ether.h>
   246  #include <linux/if_ppp.h>
   247  #include <linux/if_tun.h>
   248  #include <linux/if_packet.h>
   249  #include <linux/if_xdp.h>
   250  #include <linux/input.h>
   251  #include <linux/kcm.h>
   252  #include <linux/kexec.h>
   253  #include <linux/keyctl.h>
   254  #include <linux/landlock.h>
   255  #include <linux/loop.h>
   256  #include <linux/lwtunnel.h>
   257  #include <linux/magic.h>
   258  #include <linux/memfd.h>
   259  #include <linux/module.h>
   260  #include <linux/mount.h>
   261  #include <linux/netfilter/nfnetlink.h>
   262  #include <linux/netfilter/nf_tables.h>
   263  #include <linux/netlink.h>
   264  #include <linux/net_namespace.h>
   265  #include <linux/nfc.h>
   266  #include <linux/nsfs.h>
   267  #include <linux/perf_event.h>
   268  #include <linux/pps.h>
   269  #include <linux/ptp_clock.h>
   270  #include <linux/ptrace.h>
   271  #include <linux/random.h>
   272  #include <linux/reboot.h>
   273  #include <linux/rtc.h>
   274  #include <linux/rtnetlink.h>
   275  #include <linux/sched.h>
   276  #include <linux/seccomp.h>
   277  #include <linux/serial.h>
   278  #include <linux/sock_diag.h>
   279  #include <linux/sockios.h>
   280  #include <linux/taskstats.h>
   281  #include <linux/tipc.h>
   282  #include <linux/vm_sockets.h>
   283  #include <linux/wait.h>
   284  #include <linux/watchdog.h>
   285  #include <linux/wireguard.h>
   286  
   287  #include <mtd/ubi-user.h>
   288  #include <mtd/mtd-user.h>
   289  #include <net/route.h>
   290  
   291  #if defined(__sparc__)
   292  // On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
   293  // definition in glibc. As only the error constants are needed here, include the
   294  // generic termibits.h (which is included by termbits.h on sparc).
   295  #include <asm-generic/termbits.h>
   296  #else
   297  #include <asm/termbits.h>
   298  #endif
   299  
   300  #ifndef PTRACE_GETREGS
   301  #define PTRACE_GETREGS	0xc
   302  #endif
   303  
   304  #ifndef PTRACE_SETREGS
   305  #define PTRACE_SETREGS	0xd
   306  #endif
   307  
   308  #ifdef SOL_BLUETOOTH
   309  // SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
   310  // but it is already in bluetooth_linux.go
   311  #undef SOL_BLUETOOTH
   312  #endif
   313  
   314  // Certain constants are missing from the fs/crypto UAPI
   315  #define FS_KEY_DESC_PREFIX              "fscrypt:"
   316  #define FS_KEY_DESC_PREFIX_SIZE         8
   317  #define FS_MAX_KEY_SIZE                 64
   318  
   319  // The code generator produces -0x1 for (~0), but an unsigned value is necessary
   320  // for the tipc_subscr timeout __u32 field.
   321  #undef TIPC_WAIT_FOREVER
   322  #define TIPC_WAIT_FOREVER 0xffffffff
   323  
   324  // Copied from linux/netfilter/nf_nat.h
   325  // Including linux/netfilter/nf_nat.h here causes conflicts between linux/in.h
   326  // and netinet/in.h.
   327  #define NF_NAT_RANGE_MAP_IPS			(1 << 0)
   328  #define NF_NAT_RANGE_PROTO_SPECIFIED		(1 << 1)
   329  #define NF_NAT_RANGE_PROTO_RANDOM		(1 << 2)
   330  #define NF_NAT_RANGE_PERSISTENT			(1 << 3)
   331  #define NF_NAT_RANGE_PROTO_RANDOM_FULLY		(1 << 4)
   332  #define NF_NAT_RANGE_PROTO_OFFSET		(1 << 5)
   333  #define NF_NAT_RANGE_NETMAP			(1 << 6)
   334  #define NF_NAT_RANGE_PROTO_RANDOM_ALL		\
   335  	(NF_NAT_RANGE_PROTO_RANDOM | NF_NAT_RANGE_PROTO_RANDOM_FULLY)
   336  #define NF_NAT_RANGE_MASK					\
   337  	(NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED |	\
   338  	 NF_NAT_RANGE_PROTO_RANDOM | NF_NAT_RANGE_PERSISTENT |	\
   339  	 NF_NAT_RANGE_PROTO_RANDOM_FULLY | NF_NAT_RANGE_PROTO_OFFSET | \
   340  	 NF_NAT_RANGE_NETMAP)
   341  
   342  // Copied from linux/hid.h.
   343  // Keep in sync with the size of the referenced fields.
   344  #define _HIDIOCGRAWNAME_LEN	128 // sizeof_field(struct hid_device, name)
   345  #define _HIDIOCGRAWPHYS_LEN	64  // sizeof_field(struct hid_device, phys)
   346  #define _HIDIOCGRAWUNIQ_LEN	64  // sizeof_field(struct hid_device, uniq)
   347  
   348  #define _HIDIOCGRAWNAME		HIDIOCGRAWNAME(_HIDIOCGRAWNAME_LEN)
   349  #define _HIDIOCGRAWPHYS		HIDIOCGRAWPHYS(_HIDIOCGRAWPHYS_LEN)
   350  #define _HIDIOCGRAWUNIQ		HIDIOCGRAWUNIQ(_HIDIOCGRAWUNIQ_LEN)
   351  
   352  '
   353  
   354  includes_NetBSD='
   355  #include <sys/types.h>
   356  #include <sys/param.h>
   357  #include <sys/event.h>
   358  #include <sys/extattr.h>
   359  #include <sys/mman.h>
   360  #include <sys/mount.h>
   361  #include <sys/sched.h>
   362  #include <sys/select.h>
   363  #include <sys/socket.h>
   364  #include <sys/sockio.h>
   365  #include <sys/sysctl.h>
   366  #include <sys/termios.h>
   367  #include <sys/ttycom.h>
   368  #include <sys/wait.h>
   369  #include <net/bpf.h>
   370  #include <net/if.h>
   371  #include <net/if_types.h>
   372  #include <net/route.h>
   373  #include <netinet/in.h>
   374  #include <netinet/in_systm.h>
   375  #include <netinet/ip.h>
   376  #include <netinet/ip_mroute.h>
   377  #include <netinet/if_ether.h>
   378  
   379  // Needed since <sys/param.h> refers to it...
   380  #define schedppq 1
   381  '
   382  
   383  includes_OpenBSD='
   384  #include <sys/types.h>
   385  #include <sys/param.h>
   386  #include <sys/event.h>
   387  #include <sys/mman.h>
   388  #include <sys/mount.h>
   389  #include <sys/select.h>
   390  #include <sys/sched.h>
   391  #include <sys/socket.h>
   392  #include <sys/sockio.h>
   393  #include <sys/stat.h>
   394  #include <sys/sysctl.h>
   395  #include <sys/termios.h>
   396  #include <sys/ttycom.h>
   397  #include <sys/unistd.h>
   398  #include <sys/wait.h>
   399  #include <net/bpf.h>
   400  #include <net/if.h>
   401  #include <net/if_types.h>
   402  #include <net/if_var.h>
   403  #include <net/route.h>
   404  #include <netinet/in.h>
   405  #include <netinet/in_systm.h>
   406  #include <netinet/ip.h>
   407  #include <netinet/ip_mroute.h>
   408  #include <netinet/if_ether.h>
   409  #include <net/if_bridge.h>
   410  
   411  // We keep some constants not supported in OpenBSD 5.5 and beyond for
   412  // the promise of compatibility.
   413  #define EMUL_ENABLED		0x1
   414  #define EMUL_NATIVE		0x2
   415  #define IPV6_FAITH		0x1d
   416  #define IPV6_OPTIONS		0x1
   417  #define IPV6_RTHDR_STRICT	0x1
   418  #define IPV6_SOCKOPT_RESERVED1	0x3
   419  #define SIOCGIFGENERIC		0xc020693a
   420  #define SIOCSIFGENERIC		0x80206939
   421  #define WALTSIG			0x4
   422  '
   423  
   424  includes_SunOS='
   425  #include <limits.h>
   426  #include <sys/types.h>
   427  #include <sys/select.h>
   428  #include <sys/socket.h>
   429  #include <sys/sockio.h>
   430  #include <sys/stat.h>
   431  #include <sys/stream.h>
   432  #include <sys/mman.h>
   433  #include <sys/wait.h>
   434  #include <sys/ioctl.h>
   435  #include <sys/mkdev.h>
   436  #include <net/bpf.h>
   437  #include <net/if.h>
   438  #include <net/if_arp.h>
   439  #include <net/if_types.h>
   440  #include <net/route.h>
   441  #include <netinet/icmp6.h>
   442  #include <netinet/in.h>
   443  #include <netinet/ip.h>
   444  #include <netinet/ip_mroute.h>
   445  #include <termios.h>
   446  '
   447  
   448  
   449  includes='
   450  #include <sys/types.h>
   451  #include <sys/file.h>
   452  #include <fcntl.h>
   453  #include <dirent.h>
   454  #include <sys/socket.h>
   455  #include <netinet/in.h>
   456  #include <netinet/ip.h>
   457  #include <netinet/ip6.h>
   458  #include <netinet/tcp.h>
   459  #include <errno.h>
   460  #include <sys/signal.h>
   461  #include <signal.h>
   462  #include <sys/resource.h>
   463  #include <time.h>
   464  '
   465  ccflags="$@"
   466  
   467  # Write go tool cgo -godefs input.
   468  (
   469  	echo package unix
   470  	echo
   471  	echo '/*'
   472  	indirect="includes_$(uname)"
   473  	echo "${!indirect} $includes"
   474  	echo '*/'
   475  	echo 'import "C"'
   476  	echo 'import "syscall"'
   477  	echo
   478  	echo 'const ('
   479  
   480  	# The gcc command line prints all the #defines
   481  	# it encounters while processing the input
   482  	echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
   483  	awk '
   484  		$1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
   485  
   486  		$2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next}  # 386 registers
   487  		$2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
   488  		$2 ~ /^(SCM_SRCRT)$/ {next}
   489  		$2 ~ /^(MAP_FAILED)$/ {next}
   490  		$2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
   491  
   492  		$2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
   493  		$2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
   494  
   495  		$2 !~ /^ECCAPBITS/ &&
   496  		$2 !~ /^ETH_/ &&
   497  		$2 !~ /^EPROC_/ &&
   498  		$2 !~ /^EQUIV_/ &&
   499  		$2 !~ /^EXPR_/ &&
   500  		$2 !~ /^EVIOC/ &&
   501  		$2 ~ /^E[A-Z0-9_]+$/ ||
   502  		$2 ~ /^B[0-9_]+$/ ||
   503  		$2 ~ /^(OLD|NEW)DEV$/ ||
   504  		$2 == "BOTHER" ||
   505  		$2 ~ /^CI?BAUD(EX)?$/ ||
   506  		$2 == "IBSHIFT" ||
   507  		$2 ~ /^V[A-Z0-9]+$/ ||
   508  		$2 ~ /^CS[A-Z0-9]/ ||
   509  		$2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
   510  		$2 ~ /^IGN/ ||
   511  		$2 ~ /^IX(ON|ANY|OFF)$/ ||
   512  		$2 ~ /^IN(LCR|PCK)$/ ||
   513  		$2 !~ "X86_CR3_PCID_NOFLUSH" &&
   514  		$2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
   515  		$2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
   516  		$2 == "BRKINT" ||
   517  		$2 == "HUPCL" ||
   518  		$2 == "PENDIN" ||
   519  		$2 == "TOSTOP" ||
   520  		$2 == "XCASE" ||
   521  		$2 == "ALTWERASE" ||
   522  		$2 == "NOKERNINFO" ||
   523  		$2 == "NFDBITS" ||
   524  		$2 ~ /^PAR/ ||
   525  		$2 ~ /^SIG[^_]/ ||
   526  		$2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
   527  		$2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
   528  		$2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
   529  		$2 ~ /^O?XTABS$/ ||
   530  		$2 ~ /^TC[IO](ON|OFF)$/ ||
   531  		$2 ~ /^IN_/ ||
   532  		$2 ~ /^KCM/ ||
   533  		$2 ~ /^LANDLOCK_/ ||
   534  		$2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
   535  		$2 ~ /^LO_(KEY|NAME)_SIZE$/ ||
   536  		$2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ ||
   537  		$2 == "LOOP_CONFIGURE" ||
   538  		$2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|TCP|MCAST|EVFILT|NOTE|SHUT|PROT|MAP|MREMAP|MFD|T?PACKET|MSG|SCM|MCL|DT|MADV|PR|LOCAL|TCPOPT|UDP)_/ ||
   539  		$2 ~ /^NFC_(GENL|PROTO|COMM|RF|SE|DIRECTION|LLCP|SOCKPROTO)_/ ||
   540  		$2 ~ /^NFC_.*_(MAX)?SIZE$/ ||
   541  		$2 ~ /^PTP_/ ||
   542  		$2 ~ /^RAW_PAYLOAD_/ ||
   543  		$2 ~ /^[US]F_/ ||
   544  		$2 ~ /^TP_STATUS_/ ||
   545  		$2 ~ /^FALLOC_/ ||
   546  		$2 ~ /^ICMPV?6?_(FILTER|SEC)/ ||
   547  		$2 == "SOMAXCONN" ||
   548  		$2 == "NAME_MAX" ||
   549  		$2 == "IFNAMSIZ" ||
   550  		$2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
   551  		$2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
   552  		$2 ~ /^HW_MACHINE$/ ||
   553  		$2 ~ /^SYSCTL_VERS/ ||
   554  		$2 !~ "MNT_BITS" &&
   555  		$2 ~ /^(MS|MNT|MOUNT|UMOUNT)_/ ||
   556  		$2 ~ /^NS_GET_/ ||
   557  		$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
   558  		$2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT|PIOD|TFD)_/ ||
   559  		$2 ~ /^KEXEC_/ ||
   560  		$2 ~ /^LINUX_REBOOT_CMD_/ ||
   561  		$2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
   562  		$2 ~ /^MODULE_INIT_/ ||
   563  		$2 !~ "NLA_TYPE_MASK" &&
   564  		$2 !~ /^RTC_VL_(ACCURACY|BACKUP|DATA)/ &&
   565  		$2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
   566  		$2 ~ /^SOCK_|SK_DIAG_|SKNLGRP_$/ ||
   567  		$2 ~ /^(CONNECT|SAE)_/ ||
   568  		$2 ~ /^FIORDCHK$/ ||
   569  		$2 ~ /^SIOC/ ||
   570  		$2 ~ /^TIOC/ ||
   571  		$2 ~ /^TCGET/ ||
   572  		$2 ~ /^TCSET/ ||
   573  		$2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
   574  		$2 !~ "RTF_BITS" &&
   575  		$2 ~ /^(IFF|IFT|NET_RT|RTM(GRP)?|RTF|RTV|RTA|RTAX)_/ ||
   576  		$2 ~ /^BIOC/ ||
   577  		$2 ~ /^DIOC/ ||
   578  		$2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
   579  		$2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
   580  		$2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
   581  		$2 ~ /^CLONE_[A-Z_]+/ ||
   582  		$2 !~ /^(BPF_TIMEVAL|BPF_FIB_LOOKUP_[A-Z]+|BPF_F_LINK)$/ &&
   583  		$2 ~ /^(BPF|DLT)_/ ||
   584  		$2 ~ /^AUDIT_/ ||
   585  		$2 ~ /^(CLOCK|TIMER)_/ ||
   586  		$2 ~ /^CAN_/ ||
   587  		$2 ~ /^CAP_/ ||
   588  		$2 ~ /^CP_/ ||
   589  		$2 ~ /^CPUSTATES$/ ||
   590  		$2 ~ /^CTLIOCGINFO$/ ||
   591  		$2 ~ /^ALG_/ ||
   592  		$2 ~ /^FI(CLONE|DEDUPERANGE)/ ||
   593  		$2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE)/ ||
   594  		$2 ~ /^FS_IOC_.*(ENCRYPTION|VERITY|[GS]ETFLAGS)/ ||
   595  		$2 ~ /^FS_VERITY_/ ||
   596  		$2 ~ /^FSCRYPT_/ ||
   597  		$2 ~ /^DM_/ ||
   598  		$2 ~ /^GRND_/ ||
   599  		$2 ~ /^RND/ ||
   600  		$2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
   601  		$2 ~ /^KEYCTL_/ ||
   602  		$2 ~ /^PERF_/ ||
   603  		$2 ~ /^SECCOMP_/ ||
   604  		$2 ~ /^SEEK_/ ||
   605  		$2 ~ /^SCHED_/ ||
   606  		$2 ~ /^SPLICE_/ ||
   607  		$2 ~ /^SYNC_FILE_RANGE_/ ||
   608  		$2 !~ /IOC_MAGIC/ &&
   609  		$2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
   610  		$2 ~ /^(VM|VMADDR)_/ ||
   611  		$2 ~ /^IOCTL_VM_SOCKETS_/ ||
   612  		$2 ~ /^(TASKSTATS|TS)_/ ||
   613  		$2 ~ /^CGROUPSTATS_/ ||
   614  		$2 ~ /^GENL_/ ||
   615  		$2 ~ /^STATX_/ ||
   616  		$2 ~ /^RENAME/ ||
   617  		$2 ~ /^UBI_IOC[A-Z]/ ||
   618  		$2 ~ /^UTIME_/ ||
   619  		$2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
   620  		$2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
   621  		$2 ~ /^FSOPT_/ ||
   622  		$2 ~ /^WDIO[CFS]_/ ||
   623  		$2 ~ /^NFN/ ||
   624  		$2 !~ /^NFT_META_IIFTYPE/ &&
   625  		$2 ~ /^NFT_/ ||
   626  		$2 ~ /^NF_NAT_/ ||
   627  		$2 ~ /^XDP_/ ||
   628  		$2 ~ /^RWF_/ ||
   629  		$2 ~ /^(HDIO|WIN|SMART)_/ ||
   630  		$2 ~ /^CRYPTO_/ ||
   631  		$2 ~ /^TIPC_/ ||
   632  		$2 !~  "DEVLINK_RELOAD_LIMITS_VALID_MASK" &&
   633  		$2 ~ /^DEVLINK_/ ||
   634  		$2 ~ /^ETHTOOL_/ ||
   635  		$2 ~ /^LWTUNNEL_IP/ ||
   636  		$2 ~ /^ITIMER_/ ||
   637  		$2 !~ "WMESGLEN" &&
   638  		$2 ~ /^W[A-Z0-9]+$/ ||
   639  		$2 ~ /^P_/ ||
   640  		$2 ~/^PPPIOC/ ||
   641  		$2 ~ /^FAN_|FANOTIFY_/ ||
   642  		$2 == "HID_MAX_DESCRIPTOR_SIZE" ||
   643  		$2 ~ /^_?HIDIOC/ ||
   644  		$2 ~ /^BUS_(USB|HIL|BLUETOOTH|VIRTUAL)$/ ||
   645  		$2 ~ /^MTD/ ||
   646  		$2 ~ /^OTP/ ||
   647  		$2 ~ /^MEM/ ||
   648  		$2 ~ /^WG/ ||
   649  		$2 ~ /^FIB_RULE_/ ||
   650  		$2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE|IOMIN$|IOOPT$|ALIGNOFF$|DISCARD|ROTATIONAL$|ZEROOUT$|GETDISKSEQ$)/ {printf("\t%s = C.%s\n", $2, $2)}
   651  		$2 ~ /^__WCOREFLAG$/ {next}
   652  		$2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
   653  
   654  		{next}
   655  	' | sort
   656  
   657  	echo ')'
   658  ) >_const.go
   659  
   660  # Pull out the error names for later.
   661  errors=$(
   662  	echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
   663  	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
   664  	sort
   665  )
   666  
   667  # Pull out the signal names for later.
   668  signals=$(
   669  	echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
   670  	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
   671  	grep -E -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
   672  	sort
   673  )
   674  
   675  # Again, writing regexps to a file.
   676  echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
   677  	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
   678  	sort >_error.grep
   679  echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
   680  	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
   681  	grep -E -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
   682  	sort >_signal.grep
   683  
   684  echo '// mkerrors.sh' "$@"
   685  echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
   686  echo
   687  echo "//go:build ${GOARCH} && ${GOOS}"
   688  echo
   689  go tool cgo -godefs -- "$@" _const.go >_error.out
   690  cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
   691  echo
   692  echo '// Errors'
   693  echo 'const ('
   694  cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
   695  echo ')'
   696  
   697  echo
   698  echo '// Signals'
   699  echo 'const ('
   700  cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
   701  echo ')'
   702  
   703  # Run C program to print error and syscall strings.
   704  (
   705  	echo -E "
   706  #include <stdio.h>
   707  #include <stdlib.h>
   708  #include <errno.h>
   709  #include <ctype.h>
   710  #include <string.h>
   711  #include <signal.h>
   712  
   713  #define nelem(x) (sizeof(x)/sizeof((x)[0]))
   714  
   715  enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
   716  
   717  struct tuple {
   718  	int num;
   719  	const char *name;
   720  };
   721  
   722  struct tuple errors[] = {
   723  "
   724  	for i in $errors
   725  	do
   726  		echo -E '	{'$i', "'$i'" },'
   727  	done
   728  
   729  	echo -E "
   730  };
   731  
   732  struct tuple signals[] = {
   733  "
   734  	for i in $signals
   735  	do
   736  		echo -E '	{'$i', "'$i'" },'
   737  	done
   738  
   739  	# Use -E because on some systems bash builtin interprets \n itself.
   740  	echo -E '
   741  };
   742  
   743  static int
   744  tuplecmp(const void *a, const void *b)
   745  {
   746  	return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
   747  }
   748  
   749  int
   750  main(void)
   751  {
   752  	int i, e;
   753  	char buf[1024], *p;
   754  
   755  	printf("\n\n// Error table\n");
   756  	printf("var errorList = [...]struct {\n");
   757  	printf("\tnum  syscall.Errno\n");
   758  	printf("\tname string\n");
   759  	printf("\tdesc string\n");
   760  	printf("} {\n");
   761  	qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
   762  	for(i=0; i<nelem(errors); i++) {
   763  		e = errors[i].num;
   764  		if(i > 0 && errors[i-1].num == e)
   765  			continue;
   766  		strncpy(buf, strerror(e), sizeof(buf) - 1);
   767  		buf[sizeof(buf) - 1] = '\0';
   768  		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
   769  		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
   770  			buf[0] += a - A;
   771  		printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
   772  	}
   773  	printf("}\n\n");
   774  
   775  	printf("\n\n// Signal table\n");
   776  	printf("var signalList = [...]struct {\n");
   777  	printf("\tnum  syscall.Signal\n");
   778  	printf("\tname string\n");
   779  	printf("\tdesc string\n");
   780  	printf("} {\n");
   781  	qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
   782  	for(i=0; i<nelem(signals); i++) {
   783  		e = signals[i].num;
   784  		if(i > 0 && signals[i-1].num == e)
   785  			continue;
   786  		strncpy(buf, strsignal(e), sizeof(buf) - 1);
   787  		buf[sizeof(buf) - 1] = '\0';
   788  		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
   789  		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
   790  			buf[0] += a - A;
   791  		// cut trailing : number.
   792  		p = strrchr(buf, ":"[0]);
   793  		if(p)
   794  			*p = '\0';
   795  		printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
   796  	}
   797  	printf("}\n\n");
   798  
   799  	return 0;
   800  }
   801  
   802  '
   803  ) >_errors.c
   804  
   805  $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out
   806  

View as plain text