blob: a47b035f9af37935cd35c8e7fb280d33a229b25a [file] [log] [blame]
kesavand2cde6582020-06-22 04:56:23 -04001#!/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>
Andrea Campanella764f1ed2022-03-24 11:46:38 +010047#include <sys/select.h>
kesavand2cde6582020-06-22 04:56:23 -040048#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
kesavandc71914f2022-03-25 11:19:03 +053057#define KERNEL 1
kesavand2cde6582020-06-22 04:56:23 -040058#define _DARWIN_USE_64_BIT_INODE
Andrea Campanella764f1ed2022-03-24 11:46:38 +010059#define __APPLE_USE_RFC_3542
kesavand2cde6582020-06-22 04:56:23 -040060#include <stdint.h>
61#include <sys/attr.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +010062#include <sys/clonefile.h>
63#include <sys/kern_control.h>
kesavand2cde6582020-06-22 04:56:23 -040064#include <sys/types.h>
65#include <sys/event.h>
66#include <sys/ptrace.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +010067#include <sys/select.h>
kesavand2cde6582020-06-22 04:56:23 -040068#include <sys/socket.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +010069#include <sys/un.h>
kesavand2cde6582020-06-22 04:56:23 -040070#include <sys/sockio.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +010071#include <sys/sys_domain.h>
kesavand2cde6582020-06-22 04:56:23 -040072#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>
kesavandc71914f2022-03-25 11:19:03 +053078#include <sys/vsock.h>
kesavand2cde6582020-06-22 04:56:23 -040079#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>
kesavandc71914f2022-03-25 11:19:03 +053086
87// for backwards compatibility because moved TIOCREMOTE to Kernel.framework after MacOSX12.0.sdk.
88#define TIOCREMOTE 0x80047469
kesavand2cde6582020-06-22 04:56:23 -040089'
90
91includes_DragonFly='
92#include <sys/types.h>
93#include <sys/event.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +010094#include <sys/select.h>
kesavand2cde6582020-06-22 04:56:23 -040095#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>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100105#include <net/if_clone.h>
kesavand2cde6582020-06-22 04:56:23 -0400106#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
114includes_FreeBSD='
115#include <sys/capsicum.h>
116#include <sys/param.h>
117#include <sys/types.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100118#include <sys/disk.h>
kesavand2cde6582020-06-22 04:56:23 -0400119#include <sys/event.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100120#include <sys/sched.h>
121#include <sys/select.h>
kesavand2cde6582020-06-22 04:56:23 -0400122#include <sys/socket.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100123#include <sys/un.h>
kesavand2cde6582020-06-22 04:56:23 -0400124#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
150includes_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__)
161struct sgttyb {
162 char sg_ispeed;
163 char sg_ospeed;
164 char sg_erase;
165 char sg_kill;
166 short sg_flags;
167};
168
169struct 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
178struct 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>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100199#include <sys/select.h>
kesavand2cde6582020-06-22 04:56:23 -0400200#include <sys/signalfd.h>
201#include <sys/socket.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100202#include <sys/timerfd.h>
203#include <sys/uio.h>
kesavand2cde6582020-06-22 04:56:23 -0400204#include <sys/xattr.h>
205#include <linux/bpf.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100206#include <linux/can.h>
207#include <linux/can/error.h>
208#include <linux/can/raw.h>
kesavand2cde6582020-06-22 04:56:23 -0400209#include <linux/capability.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100210#include <linux/cryptouser.h>
211#include <linux/devlink.h>
212#include <linux/dm-ioctl.h>
kesavand2cde6582020-06-22 04:56:23 -0400213#include <linux/errqueue.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100214#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>
kesavand2cde6582020-06-22 04:56:23 -0400224#include <linux/if.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100225#include <linux/if_addr.h>
kesavand2cde6582020-06-22 04:56:23 -0400226#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>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100232#include <linux/if_xdp.h>
233#include <linux/input.h>
kesavand2cde6582020-06-22 04:56:23 -0400234#include <linux/kexec.h>
235#include <linux/keyctl.h>
kesavandc71914f2022-03-25 11:19:03 +0530236#include <linux/landlock.h>
kesavand2cde6582020-06-22 04:56:23 -0400237#include <linux/loop.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100238#include <linux/lwtunnel.h>
kesavand2cde6582020-06-22 04:56:23 -0400239#include <linux/magic.h>
240#include <linux/memfd.h>
241#include <linux/module.h>
kesavandc71914f2022-03-25 11:19:03 +0530242#include <linux/mount.h>
kesavand2cde6582020-06-22 04:56:23 -0400243#include <linux/netfilter/nfnetlink.h>
244#include <linux/netlink.h>
245#include <linux/net_namespace.h>
kesavandc71914f2022-03-25 11:19:03 +0530246#include <linux/nfc.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100247#include <linux/nsfs.h>
kesavand2cde6582020-06-22 04:56:23 -0400248#include <linux/perf_event.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100249#include <linux/pps.h>
250#include <linux/ptrace.h>
kesavand2cde6582020-06-22 04:56:23 -0400251#include <linux/random.h>
252#include <linux/reboot.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100253#include <linux/rtc.h>
kesavand2cde6582020-06-22 04:56:23 -0400254#include <linux/rtnetlink.h>
kesavand2cde6582020-06-22 04:56:23 -0400255#include <linux/sched.h>
256#include <linux/seccomp.h>
kesavand2cde6582020-06-22 04:56:23 -0400257#include <linux/serial.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100258#include <linux/sockios.h>
kesavand2cde6582020-06-22 04:56:23 -0400259#include <linux/taskstats.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100260#include <linux/tipc.h>
261#include <linux/vm_sockets.h>
262#include <linux/wait.h>
kesavand2cde6582020-06-22 04:56:23 -0400263#include <linux/watchdog.h>
kesavandc71914f2022-03-25 11:19:03 +0530264#include <linux/wireguard.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100265
kesavand2cde6582020-06-22 04:56:23 -0400266#include <mtd/ubi-user.h>
kesavandc71914f2022-03-25 11:19:03 +0530267#include <mtd/mtd-user.h>
kesavand2cde6582020-06-22 04:56:23 -0400268#include <net/route.h>
269
270#if defined(__sparc__)
271// On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
272// definition in glibc. As only the error constants are needed here, include the
273// generic termibits.h (which is included by termbits.h on sparc).
274#include <asm-generic/termbits.h>
275#else
276#include <asm/termbits.h>
277#endif
278
279#ifndef MSG_FASTOPEN
280#define MSG_FASTOPEN 0x20000000
281#endif
282
283#ifndef PTRACE_GETREGS
284#define PTRACE_GETREGS 0xc
285#endif
286
287#ifndef PTRACE_SETREGS
288#define PTRACE_SETREGS 0xd
289#endif
290
291#ifndef SOL_NETLINK
292#define SOL_NETLINK 270
293#endif
294
295#ifdef SOL_BLUETOOTH
296// SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
297// but it is already in bluetooth_linux.go
298#undef SOL_BLUETOOTH
299#endif
300
301// Certain constants are missing from the fs/crypto UAPI
302#define FS_KEY_DESC_PREFIX "fscrypt:"
303#define FS_KEY_DESC_PREFIX_SIZE 8
304#define FS_MAX_KEY_SIZE 64
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100305
306// The code generator produces -0x1 for (~0), but an unsigned value is necessary
307// for the tipc_subscr timeout __u32 field.
308#undef TIPC_WAIT_FOREVER
309#define TIPC_WAIT_FOREVER 0xffffffff
310
311// Copied from linux/l2tp.h
312// Including linux/l2tp.h here causes conflicts between linux/in.h
313// and netinet/in.h included via net/route.h above.
314#define IPPROTO_L2TP 115
315
316// Copied from linux/hid.h.
317// Keep in sync with the size of the referenced fields.
318#define _HIDIOCGRAWNAME_LEN 128 // sizeof_field(struct hid_device, name)
319#define _HIDIOCGRAWPHYS_LEN 64 // sizeof_field(struct hid_device, phys)
320#define _HIDIOCGRAWUNIQ_LEN 64 // sizeof_field(struct hid_device, uniq)
321
322#define _HIDIOCGRAWNAME HIDIOCGRAWNAME(_HIDIOCGRAWNAME_LEN)
323#define _HIDIOCGRAWPHYS HIDIOCGRAWPHYS(_HIDIOCGRAWPHYS_LEN)
324#define _HIDIOCGRAWUNIQ HIDIOCGRAWUNIQ(_HIDIOCGRAWUNIQ_LEN)
325
kesavand2cde6582020-06-22 04:56:23 -0400326'
327
328includes_NetBSD='
329#include <sys/types.h>
330#include <sys/param.h>
331#include <sys/event.h>
332#include <sys/extattr.h>
333#include <sys/mman.h>
334#include <sys/mount.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100335#include <sys/sched.h>
336#include <sys/select.h>
kesavand2cde6582020-06-22 04:56:23 -0400337#include <sys/socket.h>
338#include <sys/sockio.h>
339#include <sys/sysctl.h>
340#include <sys/termios.h>
341#include <sys/ttycom.h>
342#include <sys/wait.h>
343#include <net/bpf.h>
344#include <net/if.h>
345#include <net/if_types.h>
346#include <net/route.h>
347#include <netinet/in.h>
348#include <netinet/in_systm.h>
349#include <netinet/ip.h>
350#include <netinet/ip_mroute.h>
351#include <netinet/if_ether.h>
352
353// Needed since <sys/param.h> refers to it...
354#define schedppq 1
355'
356
357includes_OpenBSD='
358#include <sys/types.h>
359#include <sys/param.h>
360#include <sys/event.h>
361#include <sys/mman.h>
362#include <sys/mount.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100363#include <sys/select.h>
364#include <sys/sched.h>
kesavand2cde6582020-06-22 04:56:23 -0400365#include <sys/socket.h>
366#include <sys/sockio.h>
367#include <sys/stat.h>
368#include <sys/sysctl.h>
369#include <sys/termios.h>
370#include <sys/ttycom.h>
371#include <sys/unistd.h>
372#include <sys/wait.h>
373#include <net/bpf.h>
374#include <net/if.h>
375#include <net/if_types.h>
376#include <net/if_var.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#include <net/if_bridge.h>
384
385// We keep some constants not supported in OpenBSD 5.5 and beyond for
386// the promise of compatibility.
387#define EMUL_ENABLED 0x1
388#define EMUL_NATIVE 0x2
389#define IPV6_FAITH 0x1d
390#define IPV6_OPTIONS 0x1
391#define IPV6_RTHDR_STRICT 0x1
392#define IPV6_SOCKOPT_RESERVED1 0x3
393#define SIOCGIFGENERIC 0xc020693a
394#define SIOCSIFGENERIC 0x80206939
395#define WALTSIG 0x4
396'
397
398includes_SunOS='
399#include <limits.h>
400#include <sys/types.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100401#include <sys/select.h>
kesavand2cde6582020-06-22 04:56:23 -0400402#include <sys/socket.h>
403#include <sys/sockio.h>
404#include <sys/stat.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100405#include <sys/stream.h>
kesavand2cde6582020-06-22 04:56:23 -0400406#include <sys/mman.h>
407#include <sys/wait.h>
408#include <sys/ioctl.h>
409#include <sys/mkdev.h>
410#include <net/bpf.h>
411#include <net/if.h>
412#include <net/if_arp.h>
413#include <net/if_types.h>
414#include <net/route.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100415#include <netinet/icmp6.h>
kesavand2cde6582020-06-22 04:56:23 -0400416#include <netinet/in.h>
kesavand2cde6582020-06-22 04:56:23 -0400417#include <netinet/ip.h>
418#include <netinet/ip_mroute.h>
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100419#include <termios.h>
kesavand2cde6582020-06-22 04:56:23 -0400420'
421
422
423includes='
424#include <sys/types.h>
425#include <sys/file.h>
426#include <fcntl.h>
427#include <dirent.h>
428#include <sys/socket.h>
429#include <netinet/in.h>
430#include <netinet/ip.h>
431#include <netinet/ip6.h>
432#include <netinet/tcp.h>
433#include <errno.h>
434#include <sys/signal.h>
435#include <signal.h>
436#include <sys/resource.h>
437#include <time.h>
438'
439ccflags="$@"
440
441# Write go tool cgo -godefs input.
442(
443 echo package unix
444 echo
445 echo '/*'
446 indirect="includes_$(uname)"
447 echo "${!indirect} $includes"
448 echo '*/'
449 echo 'import "C"'
450 echo 'import "syscall"'
451 echo
452 echo 'const ('
453
454 # The gcc command line prints all the #defines
455 # it encounters while processing the input
456 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
457 awk '
458 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
459
460 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
461 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
462 $2 ~ /^(SCM_SRCRT)$/ {next}
463 $2 ~ /^(MAP_FAILED)$/ {next}
464 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
465
466 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
467 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
468
469 $2 !~ /^ECCAPBITS/ &&
470 $2 !~ /^ETH_/ &&
471 $2 !~ /^EPROC_/ &&
472 $2 !~ /^EQUIV_/ &&
473 $2 !~ /^EXPR_/ &&
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100474 $2 !~ /^EVIOC/ &&
kesavand2cde6582020-06-22 04:56:23 -0400475 $2 ~ /^E[A-Z0-9_]+$/ ||
476 $2 ~ /^B[0-9_]+$/ ||
477 $2 ~ /^(OLD|NEW)DEV$/ ||
478 $2 == "BOTHER" ||
479 $2 ~ /^CI?BAUD(EX)?$/ ||
480 $2 == "IBSHIFT" ||
481 $2 ~ /^V[A-Z0-9]+$/ ||
482 $2 ~ /^CS[A-Z0-9]/ ||
483 $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
484 $2 ~ /^IGN/ ||
485 $2 ~ /^IX(ON|ANY|OFF)$/ ||
486 $2 ~ /^IN(LCR|PCK)$/ ||
487 $2 !~ "X86_CR3_PCID_NOFLUSH" &&
488 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
489 $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
490 $2 == "BRKINT" ||
491 $2 == "HUPCL" ||
492 $2 == "PENDIN" ||
493 $2 == "TOSTOP" ||
494 $2 == "XCASE" ||
495 $2 == "ALTWERASE" ||
496 $2 == "NOKERNINFO" ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100497 $2 == "NFDBITS" ||
kesavand2cde6582020-06-22 04:56:23 -0400498 $2 ~ /^PAR/ ||
499 $2 ~ /^SIG[^_]/ ||
500 $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
501 $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
502 $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
503 $2 ~ /^O?XTABS$/ ||
504 $2 ~ /^TC[IO](ON|OFF)$/ ||
505 $2 ~ /^IN_/ ||
kesavandc71914f2022-03-25 11:19:03 +0530506 $2 ~ /^LANDLOCK_/ ||
kesavand2cde6582020-06-22 04:56:23 -0400507 $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
508 $2 ~ /^LO_(KEY|NAME)_SIZE$/ ||
509 $2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ ||
kesavandc71914f2022-03-25 11:19:03 +0530510 $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)_/ ||
511 $2 ~ /^NFC_(GENL|PROTO|COMM|RF|SE|DIRECTION|LLCP|SOCKPROTO)_/ ||
512 $2 ~ /^NFC_.*_(MAX)?SIZE$/ ||
513 $2 ~ /^RAW_PAYLOAD_/ ||
kesavand2cde6582020-06-22 04:56:23 -0400514 $2 ~ /^TP_STATUS_/ ||
515 $2 ~ /^FALLOC_/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100516 $2 ~ /^ICMPV?6?_(FILTER|SEC)/ ||
kesavand2cde6582020-06-22 04:56:23 -0400517 $2 == "SOMAXCONN" ||
518 $2 == "NAME_MAX" ||
519 $2 == "IFNAMSIZ" ||
520 $2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
521 $2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
522 $2 ~ /^HW_MACHINE$/ ||
523 $2 ~ /^SYSCTL_VERS/ ||
524 $2 !~ "MNT_BITS" &&
kesavandc71914f2022-03-25 11:19:03 +0530525 $2 ~ /^(MS|MNT|MOUNT|UMOUNT)_/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100526 $2 ~ /^NS_GET_/ ||
kesavand2cde6582020-06-22 04:56:23 -0400527 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100528 $2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT|TFD)_/ ||
kesavand2cde6582020-06-22 04:56:23 -0400529 $2 ~ /^KEXEC_/ ||
530 $2 ~ /^LINUX_REBOOT_CMD_/ ||
531 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
532 $2 ~ /^MODULE_INIT_/ ||
533 $2 !~ "NLA_TYPE_MASK" &&
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100534 $2 !~ /^RTC_VL_(ACCURACY|BACKUP|DATA)/ &&
kesavand2cde6582020-06-22 04:56:23 -0400535 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100536 $2 ~ /^FIORDCHK$/ ||
kesavand2cde6582020-06-22 04:56:23 -0400537 $2 ~ /^SIOC/ ||
538 $2 ~ /^TIOC/ ||
539 $2 ~ /^TCGET/ ||
540 $2 ~ /^TCSET/ ||
541 $2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
542 $2 !~ "RTF_BITS" &&
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100543 $2 ~ /^(IFF|IFT|NET_RT|RTM(GRP)?|RTF|RTV|RTA|RTAX)_/ ||
kesavand2cde6582020-06-22 04:56:23 -0400544 $2 ~ /^BIOC/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100545 $2 ~ /^DIOC/ ||
kesavand2cde6582020-06-22 04:56:23 -0400546 $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
547 $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
548 $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
549 $2 ~ /^CLONE_[A-Z_]+/ ||
550 $2 !~ /^(BPF_TIMEVAL|BPF_FIB_LOOKUP_[A-Z]+)$/ &&
551 $2 ~ /^(BPF|DLT)_/ ||
552 $2 ~ /^(CLOCK|TIMER)_/ ||
553 $2 ~ /^CAN_/ ||
554 $2 ~ /^CAP_/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100555 $2 ~ /^CP_/ ||
556 $2 ~ /^CPUSTATES$/ ||
557 $2 ~ /^CTLIOCGINFO$/ ||
kesavand2cde6582020-06-22 04:56:23 -0400558 $2 ~ /^ALG_/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100559 $2 ~ /^FI(CLONE|DEDUPERANGE)/ ||
560 $2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE)/ ||
561 $2 ~ /^FS_IOC_.*(ENCRYPTION|VERITY|[GS]ETFLAGS)/ ||
562 $2 ~ /^FS_VERITY_/ ||
563 $2 ~ /^FSCRYPT_/ ||
564 $2 ~ /^DM_/ ||
kesavand2cde6582020-06-22 04:56:23 -0400565 $2 ~ /^GRND_/ ||
566 $2 ~ /^RND/ ||
567 $2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
568 $2 ~ /^KEYCTL_/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100569 $2 ~ /^PERF_/ ||
kesavand2cde6582020-06-22 04:56:23 -0400570 $2 ~ /^SECCOMP_MODE_/ ||
kesavandc71914f2022-03-25 11:19:03 +0530571 $2 ~ /^SEEK_/ ||
kesavand2cde6582020-06-22 04:56:23 -0400572 $2 ~ /^SPLICE_/ ||
573 $2 ~ /^SYNC_FILE_RANGE_/ ||
574 $2 !~ /^AUDIT_RECORD_MAGIC/ &&
575 $2 !~ /IOC_MAGIC/ &&
576 $2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
577 $2 ~ /^(VM|VMADDR)_/ ||
578 $2 ~ /^IOCTL_VM_SOCKETS_/ ||
579 $2 ~ /^(TASKSTATS|TS)_/ ||
580 $2 ~ /^CGROUPSTATS_/ ||
581 $2 ~ /^GENL_/ ||
582 $2 ~ /^STATX_/ ||
583 $2 ~ /^RENAME/ ||
584 $2 ~ /^UBI_IOC[A-Z]/ ||
585 $2 ~ /^UTIME_/ ||
586 $2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
587 $2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
588 $2 ~ /^FSOPT_/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100589 $2 ~ /^WDIO[CFS]_/ ||
kesavand2cde6582020-06-22 04:56:23 -0400590 $2 ~ /^NFN/ ||
591 $2 ~ /^XDP_/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100592 $2 ~ /^RWF_/ ||
kesavand2cde6582020-06-22 04:56:23 -0400593 $2 ~ /^(HDIO|WIN|SMART)_/ ||
594 $2 ~ /^CRYPTO_/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100595 $2 ~ /^TIPC_/ ||
596 $2 !~ "DEVLINK_RELOAD_LIMITS_VALID_MASK" &&
597 $2 ~ /^DEVLINK_/ ||
598 $2 ~ /^ETHTOOL_/ ||
599 $2 ~ /^LWTUNNEL_IP/ ||
kesavand2cde6582020-06-22 04:56:23 -0400600 $2 !~ "WMESGLEN" &&
601 $2 ~ /^W[A-Z0-9]+$/ ||
602 $2 ~/^PPPIOC/ ||
603 $2 ~ /^FAN_|FANOTIFY_/ ||
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100604 $2 == "HID_MAX_DESCRIPTOR_SIZE" ||
605 $2 ~ /^_?HIDIOC/ ||
606 $2 ~ /^BUS_(USB|HIL|BLUETOOTH|VIRTUAL)$/ ||
kesavandc71914f2022-03-25 11:19:03 +0530607 $2 ~ /^MTD/ ||
608 $2 ~ /^OTP/ ||
609 $2 ~ /^MEM/ ||
610 $2 ~ /^WG/ ||
kesavand2cde6582020-06-22 04:56:23 -0400611 $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
612 $2 ~ /^__WCOREFLAG$/ {next}
613 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
614
615 {next}
616 ' | sort
617
618 echo ')'
619) >_const.go
620
621# Pull out the error names for later.
622errors=$(
623 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
624 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
625 sort
626)
627
628# Pull out the signal names for later.
629signals=$(
630 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
631 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
632 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
633 sort
634)
635
636# Again, writing regexps to a file.
637echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
638 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
639 sort >_error.grep
640echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
641 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
642 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
643 sort >_signal.grep
644
645echo '// mkerrors.sh' "$@"
646echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
647echo
Andrea Campanella764f1ed2022-03-24 11:46:38 +0100648echo "//go:build ${GOARCH} && ${GOOS}"
kesavand2cde6582020-06-22 04:56:23 -0400649echo "// +build ${GOARCH},${GOOS}"
650echo
651go tool cgo -godefs -- "$@" _const.go >_error.out
652cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
653echo
654echo '// Errors'
655echo 'const ('
656cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
657echo ')'
658
659echo
660echo '// Signals'
661echo 'const ('
662cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
663echo ')'
664
665# Run C program to print error and syscall strings.
666(
667 echo -E "
668#include <stdio.h>
669#include <stdlib.h>
670#include <errno.h>
671#include <ctype.h>
672#include <string.h>
673#include <signal.h>
674
675#define nelem(x) (sizeof(x)/sizeof((x)[0]))
676
677enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
678
679struct tuple {
680 int num;
681 const char *name;
682};
683
684struct tuple errors[] = {
685"
686 for i in $errors
687 do
688 echo -E ' {'$i', "'$i'" },'
689 done
690
691 echo -E "
692};
693
694struct tuple signals[] = {
695"
696 for i in $signals
697 do
698 echo -E ' {'$i', "'$i'" },'
699 done
700
701 # Use -E because on some systems bash builtin interprets \n itself.
702 echo -E '
703};
704
705static int
706tuplecmp(const void *a, const void *b)
707{
708 return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
709}
710
711int
712main(void)
713{
714 int i, e;
715 char buf[1024], *p;
716
717 printf("\n\n// Error table\n");
718 printf("var errorList = [...]struct {\n");
719 printf("\tnum syscall.Errno\n");
720 printf("\tname string\n");
721 printf("\tdesc string\n");
722 printf("} {\n");
723 qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
724 for(i=0; i<nelem(errors); i++) {
725 e = errors[i].num;
726 if(i > 0 && errors[i-1].num == e)
727 continue;
728 strcpy(buf, strerror(e));
729 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
730 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
731 buf[0] += a - A;
732 printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
733 }
734 printf("}\n\n");
735
736 printf("\n\n// Signal table\n");
737 printf("var signalList = [...]struct {\n");
738 printf("\tnum syscall.Signal\n");
739 printf("\tname string\n");
740 printf("\tdesc string\n");
741 printf("} {\n");
742 qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
743 for(i=0; i<nelem(signals); i++) {
744 e = signals[i].num;
745 if(i > 0 && signals[i-1].num == e)
746 continue;
747 strcpy(buf, strsignal(e));
748 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
749 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
750 buf[0] += a - A;
751 // cut trailing : number.
752 p = strrchr(buf, ":"[0]);
753 if(p)
754 *p = '\0';
755 printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
756 }
757 printf("}\n\n");
758
759 return 0;
760}
761
762'
763) >_errors.c
764
765$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out