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
 10unset LANG
 11export LC_ALL=C
 12export LC_CTYPE=C
 13
 14if test -z "$GOARCH" -o -z "$GOOS"; then
 15	echo 1>&2 "GOARCH or GOOS not defined in environment"
 16	exit 1
 17fi
 18
 19# Check that we are using the new build system if we should
 20if [[ "$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
 24fi
 25
 26if [[ "$GOOS" = "aix" ]]; then
 27	CC=${CC:-gcc}
 28else
 29	CC=${CC:-cc}
 30fi
 31
 32if [[ "$GOOS" = "solaris" ]]; then
 33	# Assumes GNU versions of utilities in PATH.
 34	export PATH=/usr/gnu/bin:$PATH
 35fi
 36
 37uname=$(uname)
 38
 39includes_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
 55includes_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
 93includes_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
116includes_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
153includes_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__)
174struct sgttyb {
175        char    sg_ispeed;
176        char    sg_ospeed;
177        char    sg_erase;
178        char    sg_kill;
179        short   sg_flags;
180};
181
182struct 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
191struct 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/elf.h>
230#include <linux/errqueue.h>
231#include <linux/ethtool_netlink.h>
232#include <linux/falloc.h>
233#include <linux/fanotify.h>
234#include <linux/fib_rules.h>
235#include <linux/filter.h>
236#include <linux/fs.h>
237#include <linux/fscrypt.h>
238#include <linux/fsverity.h>
239#include <linux/genetlink.h>
240#include <linux/hdreg.h>
241#include <linux/hidraw.h>
242#include <linux/if.h>
243#include <linux/if_addr.h>
244#include <linux/if_alg.h>
245#include <linux/if_arp.h>
246#include <linux/if_ether.h>
247#include <linux/if_ppp.h>
248#include <linux/if_tun.h>
249#include <linux/if_packet.h>
250#include <linux/if_xdp.h>
251#include <linux/input.h>
252#include <linux/kcm.h>
253#include <linux/kexec.h>
254#include <linux/keyctl.h>
255#include <linux/landlock.h>
256#include <linux/loop.h>
257#include <linux/lwtunnel.h>
258#include <linux/magic.h>
259#include <linux/mei.h>
260#include <linux/memfd.h>
261#include <linux/module.h>
262#include <linux/mount.h>
263#include <linux/netfilter/nfnetlink.h>
264#include <linux/netfilter/nf_tables.h>
265#include <linux/netlink.h>
266#include <linux/net_namespace.h>
267#include <linux/nfc.h>
268#include <linux/nsfs.h>
269#include <linux/perf_event.h>
270#include <linux/pps.h>
271#include <linux/ptp_clock.h>
272#include <linux/ptrace.h>
273#include <linux/random.h>
274#include <linux/reboot.h>
275#include <linux/rtc.h>
276#include <linux/rtnetlink.h>
277#include <linux/sched.h>
278#include <linux/seccomp.h>
279#include <linux/serial.h>
280#include <linux/sock_diag.h>
281#include <linux/sockios.h>
282#include <linux/taskstats.h>
283#include <linux/tipc.h>
284#include <linux/vm_sockets.h>
285#include <linux/wait.h>
286#include <linux/watchdog.h>
287#include <linux/wireguard.h>
288
289#include <mtd/ubi-user.h>
290#include <mtd/mtd-user.h>
291#include <net/route.h>
292
293#if defined(__sparc__)
294// On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
295// definition in glibc. As only the error constants are needed here, include the
296// generic termibits.h (which is included by termbits.h on sparc).
297#include <asm-generic/termbits.h>
298#else
299#include <asm/termbits.h>
300#endif
301
302#ifndef PTRACE_GETREGS
303#define PTRACE_GETREGS	0xc
304#endif
305
306#ifndef PTRACE_SETREGS
307#define PTRACE_SETREGS	0xd
308#endif
309
310#ifdef SOL_BLUETOOTH
311// SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
312// but it is already in bluetooth_linux.go
313#undef SOL_BLUETOOTH
314#endif
315
316// Certain constants are missing from the fs/crypto UAPI
317#define FS_KEY_DESC_PREFIX              "fscrypt:"
318#define FS_KEY_DESC_PREFIX_SIZE         8
319#define FS_MAX_KEY_SIZE                 64
320
321// The code generator produces -0x1 for (~0), but an unsigned value is necessary
322// for the tipc_subscr timeout __u32 field.
323#undef TIPC_WAIT_FOREVER
324#define TIPC_WAIT_FOREVER 0xffffffff
325
326// Copied from linux/netfilter/nf_nat.h
327// Including linux/netfilter/nf_nat.h here causes conflicts between linux/in.h
328// and netinet/in.h.
329#define NF_NAT_RANGE_MAP_IPS			(1 << 0)
330#define NF_NAT_RANGE_PROTO_SPECIFIED		(1 << 1)
331#define NF_NAT_RANGE_PROTO_RANDOM		(1 << 2)
332#define NF_NAT_RANGE_PERSISTENT			(1 << 3)
333#define NF_NAT_RANGE_PROTO_RANDOM_FULLY		(1 << 4)
334#define NF_NAT_RANGE_PROTO_OFFSET		(1 << 5)
335#define NF_NAT_RANGE_NETMAP			(1 << 6)
336#define NF_NAT_RANGE_PROTO_RANDOM_ALL		\
337	(NF_NAT_RANGE_PROTO_RANDOM | NF_NAT_RANGE_PROTO_RANDOM_FULLY)
338#define NF_NAT_RANGE_MASK					\
339	(NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED |	\
340	 NF_NAT_RANGE_PROTO_RANDOM | NF_NAT_RANGE_PERSISTENT |	\
341	 NF_NAT_RANGE_PROTO_RANDOM_FULLY | NF_NAT_RANGE_PROTO_OFFSET | \
342	 NF_NAT_RANGE_NETMAP)
343
344// Copied from linux/hid.h.
345// Keep in sync with the size of the referenced fields.
346#define _HIDIOCGRAWNAME_LEN	128 // sizeof_field(struct hid_device, name)
347#define _HIDIOCGRAWPHYS_LEN	64  // sizeof_field(struct hid_device, phys)
348#define _HIDIOCGRAWUNIQ_LEN	64  // sizeof_field(struct hid_device, uniq)
349
350#define _HIDIOCGRAWNAME		HIDIOCGRAWNAME(_HIDIOCGRAWNAME_LEN)
351#define _HIDIOCGRAWPHYS		HIDIOCGRAWPHYS(_HIDIOCGRAWPHYS_LEN)
352#define _HIDIOCGRAWUNIQ		HIDIOCGRAWUNIQ(_HIDIOCGRAWUNIQ_LEN)
353
354// Renamed in v6.16, commit c6d732c38f93 ("net: ethtool: remove duplicate defines for family info")
355#define ETHTOOL_FAMILY_NAME	ETHTOOL_GENL_NAME
356#define ETHTOOL_FAMILY_VERSION	ETHTOOL_GENL_VERSION
357'
358
359includes_NetBSD='
360#include <sys/types.h>
361#include <sys/param.h>
362#include <sys/event.h>
363#include <sys/extattr.h>
364#include <sys/mman.h>
365#include <sys/mount.h>
366#include <sys/sched.h>
367#include <sys/select.h>
368#include <sys/socket.h>
369#include <sys/sockio.h>
370#include <sys/sysctl.h>
371#include <sys/termios.h>
372#include <sys/ttycom.h>
373#include <sys/wait.h>
374#include <net/bpf.h>
375#include <net/if.h>
376#include <net/if_types.h>
377#include <net/route.h>
378#include <netinet/in.h>
379#include <netinet/in_systm.h>
380#include <netinet/ip.h>
381#include <netinet/ip_mroute.h>
382#include <netinet/if_ether.h>
383
384// Needed since <sys/param.h> refers to it...
385#define schedppq 1
386'
387
388includes_OpenBSD='
389#include <sys/types.h>
390#include <sys/param.h>
391#include <sys/event.h>
392#include <sys/mman.h>
393#include <sys/mount.h>
394#include <sys/select.h>
395#include <sys/sched.h>
396#include <sys/socket.h>
397#include <sys/sockio.h>
398#include <sys/stat.h>
399#include <sys/sysctl.h>
400#include <sys/termios.h>
401#include <sys/ttycom.h>
402#include <sys/unistd.h>
403#include <sys/wait.h>
404#include <net/bpf.h>
405#include <net/if.h>
406#include <net/if_types.h>
407#include <net/if_var.h>
408#include <net/route.h>
409#include <netinet/in.h>
410#include <netinet/in_systm.h>
411#include <netinet/ip.h>
412#include <netinet/ip_mroute.h>
413#include <netinet/if_ether.h>
414#include <net/if_bridge.h>
415
416// We keep some constants not supported in OpenBSD 5.5 and beyond for
417// the promise of compatibility.
418#define EMUL_ENABLED		0x1
419#define EMUL_NATIVE		0x2
420#define IPV6_FAITH		0x1d
421#define IPV6_OPTIONS		0x1
422#define IPV6_RTHDR_STRICT	0x1
423#define IPV6_SOCKOPT_RESERVED1	0x3
424#define SIOCGIFGENERIC		0xc020693a
425#define SIOCSIFGENERIC		0x80206939
426#define WALTSIG			0x4
427'
428
429includes_SunOS='
430#include <limits.h>
431#include <sys/types.h>
432#include <sys/select.h>
433#include <sys/socket.h>
434#include <sys/sockio.h>
435#include <sys/stat.h>
436#include <sys/stream.h>
437#include <sys/mman.h>
438#include <sys/wait.h>
439#include <sys/ioctl.h>
440#include <sys/mkdev.h>
441#include <net/bpf.h>
442#include <net/if.h>
443#include <net/if_arp.h>
444#include <net/if_types.h>
445#include <net/route.h>
446#include <netinet/icmp6.h>
447#include <netinet/in.h>
448#include <netinet/ip.h>
449#include <netinet/ip_mroute.h>
450#include <termios.h>
451'
452
453
454includes='
455#include <sys/types.h>
456#include <sys/file.h>
457#include <fcntl.h>
458#include <dirent.h>
459#include <sys/socket.h>
460#include <netinet/in.h>
461#include <netinet/ip.h>
462#include <netinet/ip6.h>
463#include <netinet/tcp.h>
464#include <errno.h>
465#include <sys/signal.h>
466#include <signal.h>
467#include <sys/resource.h>
468#include <time.h>
469'
470ccflags="$@"
471
472# Write go tool cgo -godefs input.
473(
474	echo package unix
475	echo
476	echo '/*'
477	indirect="includes_$(uname)"
478	echo "${!indirect} $includes"
479	echo '*/'
480	echo 'import "C"'
481	echo 'import "syscall"'
482	echo
483	echo 'const ('
484
485	# The gcc command line prints all the #defines
486	# it encounters while processing the input
487	echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
488	awk '
489		$1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
490
491		$2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next}  # 386 registers
492		$2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
493		$2 ~ /^(SCM_SRCRT)$/ {next}
494		$2 ~ /^(MAP_FAILED)$/ {next}
495		$2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
496
497		$2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
498		$2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
499
500		$2 !~ /^ECCAPBITS/ &&
501		$2 !~ /^ETH_/ &&
502		$2 !~ /^EPROC_/ &&
503		$2 !~ /^EQUIV_/ &&
504		$2 !~ /^EXPR_/ &&
505		$2 !~ /^EVIOC/ &&
506		$2 ~ /^E[A-Z0-9_]+$/ ||
507		$2 ~ /^B[0-9_]+$/ ||
508		$2 ~ /^(OLD|NEW)DEV$/ ||
509		$2 == "BOTHER" ||
510		$2 ~ /^CI?BAUD(EX)?$/ ||
511		$2 == "IBSHIFT" ||
512		$2 ~ /^V[A-Z0-9]+$/ ||
513		$2 ~ /^CS[A-Z0-9]/ ||
514		$2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
515		$2 ~ /^IGN/ ||
516		$2 ~ /^IX(ON|ANY|OFF)$/ ||
517		$2 ~ /^IN(LCR|PCK)$/ ||
518		$2 !~ "X86_CR3_PCID_NOFLUSH" &&
519		$2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
520		$2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
521		$2 == "BRKINT" ||
522		$2 == "HUPCL" ||
523		$2 == "PENDIN" ||
524		$2 == "TOSTOP" ||
525		$2 == "XCASE" ||
526		$2 == "ALTWERASE" ||
527		$2 == "NOKERNINFO" ||
528		$2 == "NFDBITS" ||
529		$2 ~ /^PAR/ ||
530		$2 ~ /^SIG[^_]/ ||
531		$2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
532		$2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
533		$2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
534		$2 ~ /^(DT|EI|ELF|EV|NN|NT|PF|SHF|SHN|SHT|STB|STT|VER)_/ ||
535		$2 ~ /^O?XTABS$/ ||
536		$2 ~ /^TC[IO](ON|OFF)$/ ||
537		$2 ~ /^IN_/ ||
538		$2 ~ /^KCM/ ||
539		$2 ~ /^LANDLOCK_/ ||
540		$2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
541		$2 ~ /^LO_(KEY|NAME)_SIZE$/ ||
542		$2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ ||
543		$2 == "LOOP_CONFIGURE" ||
544		$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)_/ ||
545		$2 ~ /^NFC_(GENL|PROTO|COMM|RF|SE|DIRECTION|LLCP|SOCKPROTO)_/ ||
546		$2 ~ /^NFC_.*_(MAX)?SIZE$/ ||
547		$2 ~ /^PTP_/ ||
548		$2 ~ /^RAW_PAYLOAD_/ ||
549		$2 ~ /^[US]F_/ ||
550		$2 ~ /^TP_STATUS_/ ||
551		$2 ~ /^FALLOC_/ ||
552		$2 ~ /^ICMPV?6?_(FILTER|SEC)/ ||
553		$2 == "SOMAXCONN" ||
554		$2 == "NAME_MAX" ||
555		$2 == "IFNAMSIZ" ||
556		$2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
557		$2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
558		$2 ~ /^HW_MACHINE$/ ||
559		$2 ~ /^SYSCTL_VERS/ ||
560		$2 !~ "MNT_BITS" &&
561		$2 ~ /^(MS|MNT|MOUNT|UMOUNT)_/ ||
562		$2 ~ /^NS_GET_/ ||
563		$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
564		$2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT|PIOD|TFD)_/ ||
565		$2 ~ /^KEXEC_/ ||
566		$2 ~ /^LINUX_REBOOT_CMD_/ ||
567		$2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
568		$2 ~ /^MODULE_INIT_/ ||
569		$2 !~ "NLA_TYPE_MASK" &&
570		$2 !~ /^RTC_VL_(ACCURACY|BACKUP|DATA)/ &&
571		$2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
572		$2 ~ /^SOCK_|SK_DIAG_|SKNLGRP_$/ ||
573		$2 ~ /^(CONNECT|SAE)_/ ||
574		$2 ~ /^FIORDCHK$/ ||
575		$2 ~ /^SIOC/ ||
576		$2 ~ /^TIOC/ ||
577		$2 ~ /^TCGET/ ||
578		$2 ~ /^TCSET/ ||
579		$2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
580		$2 !~ "RTF_BITS" &&
581		$2 ~ /^(IFF|IFT|NET_RT|RTM(GRP)?|RTF|RTV|RTA|RTAX)_/ ||
582		$2 ~ /^BIOC/ ||
583		$2 ~ /^DIOC/ ||
584		$2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
585		$2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
586		$2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
587		$2 ~ /^CLONE_[A-Z_]+/ ||
588		$2 !~ /^(BPF_TIMEVAL|BPF_FIB_LOOKUP_[A-Z]+|BPF_F_LINK)$/ &&
589		$2 ~ /^(BPF|DLT)_/ ||
590		$2 ~ /^AUDIT_/ ||
591		$2 ~ /^(CLOCK|TIMER)_/ ||
592		$2 ~ /^CAN_/ ||
593		$2 ~ /^CAP_/ ||
594		$2 ~ /^CP_/ ||
595		$2 ~ /^CPUSTATES$/ ||
596		$2 ~ /^CTLIOCGINFO$/ ||
597		$2 ~ /^ALG_/ ||
598		$2 ~ /^FI(CLONE|DEDUPERANGE)/ ||
599		$2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE)/ ||
600		$2 ~ /^FS_IOC_.*(ENCRYPTION|VERITY|[GS]ETFLAGS)/ ||
601		$2 ~ /^FS_VERITY_/ ||
602		$2 ~ /^FSCRYPT_/ ||
603		$2 ~ /^DM_/ ||
604		$2 ~ /^GRND_/ ||
605		$2 ~ /^RND/ ||
606		$2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
607		$2 ~ /^KEYCTL_/ ||
608		$2 ~ /^PERF_/ ||
609		$2 ~ /^SECCOMP_/ ||
610		$2 ~ /^SEEK_/ ||
611		$2 ~ /^SCHED_/ ||
612		$2 ~ /^SPLICE_/ ||
613		$2 ~ /^SYNC_FILE_RANGE_/ ||
614		$2 !~ /IOC_MAGIC/ &&
615		$2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
616		$2 ~ /^(VM|VMADDR)_/ ||
617		$2 ~ /^(IOCTL_VM_SOCKETS_|IOCTL_MEI_)/ ||
618		$2 ~ /^(TASKSTATS|TS)_/ ||
619		$2 ~ /^CGROUPSTATS_/ ||
620		$2 ~ /^GENL_/ ||
621		$2 ~ /^STATX_/ ||
622		$2 ~ /^RENAME/ ||
623		$2 ~ /^UBI_IOC[A-Z]/ ||
624		$2 ~ /^UTIME_/ ||
625		$2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
626		$2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
627		$2 ~ /^FSOPT_/ ||
628		$2 ~ /^WDIO[CFS]_/ ||
629		$2 ~ /^NFN/ ||
630		$2 !~ /^NFT_META_IIFTYPE/ &&
631		$2 ~ /^NFT_/ ||
632		$2 ~ /^NF_NAT_/ ||
633		$2 ~ /^XDP_/ ||
634		$2 ~ /^RWF_/ ||
635		$2 ~ /^(HDIO|WIN|SMART)_/ ||
636		$2 ~ /^CRYPTO_/ ||
637		$2 ~ /^TIPC_/ ||
638		$2 !~  "DEVLINK_RELOAD_LIMITS_VALID_MASK" &&
639		$2 ~ /^DEVLINK_/ ||
640		$2 ~ /^ETHTOOL_/ ||
641		$2 ~ /^LWTUNNEL_IP/ ||
642		$2 ~ /^ITIMER_/ ||
643		$2 !~ "WMESGLEN" &&
644		$2 ~ /^W[A-Z0-9]+$/ ||
645		$2 ~ /^P_/ ||
646		$2 ~/^PPPIOC/ ||
647		$2 ~ /^FAN_|FANOTIFY_/ ||
648		$2 == "HID_MAX_DESCRIPTOR_SIZE" ||
649		$2 ~ /^_?HIDIOC/ ||
650		$2 ~ /^BUS_(USB|HIL|BLUETOOTH|VIRTUAL)$/ ||
651		$2 ~ /^MTD/ ||
652		$2 ~ /^OTP/ ||
653		$2 ~ /^MEM/ ||
654		$2 ~ /^WG/ ||
655		$2 ~ /^FIB_RULE_/ ||
656		$2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE|IOMIN$|IOOPT$|ALIGNOFF$|DISCARD|ROTATIONAL$|ZEROOUT$|GETDISKSEQ$)/ {printf("\t%s = C.%s\n", $2, $2)}
657		$2 ~ /^__WCOREFLAG$/ {next}
658		$2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
659
660		{next}
661	' | sort
662
663	echo ')'
664) >_const.go
665
666# Pull out the error names for later.
667errors=$(
668	echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
669	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
670	sort
671)
672
673# Pull out the signal names for later.
674signals=$(
675	echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
676	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
677	grep -E -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
678	sort
679)
680
681# Again, writing regexps to a file.
682echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
683	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
684	sort >_error.grep
685echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
686	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
687	grep -E -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
688	sort >_signal.grep
689
690echo '// mkerrors.sh' "$@"
691echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
692echo
693echo "//go:build ${GOARCH} && ${GOOS}"
694echo
695go tool cgo -godefs -- "$@" _const.go >_error.out
696cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
697echo
698echo '// Errors'
699echo 'const ('
700cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
701echo ')'
702
703echo
704echo '// Signals'
705echo 'const ('
706cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
707echo ')'
708
709# Run C program to print error and syscall strings.
710(
711	echo -E "
712#include <stdio.h>
713#include <stdlib.h>
714#include <errno.h>
715#include <ctype.h>
716#include <string.h>
717#include <signal.h>
718
719#define nelem(x) (sizeof(x)/sizeof((x)[0]))
720
721enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
722
723struct tuple {
724	int num;
725	const char *name;
726};
727
728struct tuple errors[] = {
729"
730	for i in $errors
731	do
732		echo -E '	{'$i', "'$i'" },'
733	done
734
735	echo -E "
736};
737
738struct tuple signals[] = {
739"
740	for i in $signals
741	do
742		echo -E '	{'$i', "'$i'" },'
743	done
744
745	# Use -E because on some systems bash builtin interprets \n itself.
746	echo -E '
747};
748
749static int
750tuplecmp(const void *a, const void *b)
751{
752	return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
753}
754
755int
756main(void)
757{
758	int i, e;
759	char buf[1024], *p;
760
761	printf("\n\n// Error table\n");
762	printf("var errorList = [...]struct {\n");
763	printf("\tnum  syscall.Errno\n");
764	printf("\tname string\n");
765	printf("\tdesc string\n");
766	printf("} {\n");
767	qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
768	for(i=0; i<nelem(errors); i++) {
769		e = errors[i].num;
770		if(i > 0 && errors[i-1].num == e)
771			continue;
772		strncpy(buf, strerror(e), sizeof(buf) - 1);
773		buf[sizeof(buf) - 1] = '\0';
774		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
775		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
776			buf[0] += a - A;
777		printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
778	}
779	printf("}\n\n");
780
781	printf("\n\n// Signal table\n");
782	printf("var signalList = [...]struct {\n");
783	printf("\tnum  syscall.Signal\n");
784	printf("\tname string\n");
785	printf("\tdesc string\n");
786	printf("} {\n");
787	qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
788	for(i=0; i<nelem(signals); i++) {
789		e = signals[i].num;
790		if(i > 0 && signals[i-1].num == e)
791			continue;
792		strncpy(buf, strsignal(e), sizeof(buf) - 1);
793		buf[sizeof(buf) - 1] = '\0';
794		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
795		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
796			buf[0] += a - A;
797		// cut trailing : number.
798		p = strrchr(buf, ":"[0]);
799		if(p)
800			*p = '\0';
801		printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
802	}
803	printf("}\n\n");
804
805	return 0;
806}
807
808'
809) >_errors.c
810
811$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out