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

View as plain text