blob: 3f670faba31570661dbdaa554e095d9d4ed2251d [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001#!/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 Campanella3614a922021-02-25 12:40:42 +010047#include <sys/select.h>
khenaidooac637102019-01-14 15:44:34 -050048#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
58#define _DARWIN_USE_64_BIT_INODE
khenaidood948f772021-08-11 17:49:24 -040059#define __APPLE_USE_RFC_3542
khenaidooac637102019-01-14 15:44:34 -050060#include <stdint.h>
61#include <sys/attr.h>
Andrea Campanella3614a922021-02-25 12:40:42 +010062#include <sys/clonefile.h>
khenaidood948f772021-08-11 17:49:24 -040063#include <sys/kern_control.h>
khenaidooac637102019-01-14 15:44:34 -050064#include <sys/types.h>
65#include <sys/event.h>
66#include <sys/ptrace.h>
Scott Baker8461e152019-10-01 14:44:30 -070067#include <sys/select.h>
khenaidooac637102019-01-14 15:44:34 -050068#include <sys/socket.h>
khenaidood948f772021-08-11 17:49:24 -040069#include <sys/un.h>
khenaidooac637102019-01-14 15:44:34 -050070#include <sys/sockio.h>
khenaidood948f772021-08-11 17:49:24 -040071#include <sys/sys_domain.h>
khenaidooac637102019-01-14 15:44:34 -050072#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 <net/bpf.h>
79#include <net/if.h>
80#include <net/if_types.h>
81#include <net/route.h>
82#include <netinet/in.h>
83#include <netinet/ip.h>
84#include <termios.h>
85'
86
87includes_DragonFly='
88#include <sys/types.h>
89#include <sys/event.h>
Scott Baker8461e152019-10-01 14:44:30 -070090#include <sys/select.h>
khenaidooac637102019-01-14 15:44:34 -050091#include <sys/socket.h>
92#include <sys/sockio.h>
93#include <sys/stat.h>
94#include <sys/sysctl.h>
95#include <sys/mman.h>
96#include <sys/mount.h>
97#include <sys/wait.h>
98#include <sys/ioctl.h>
99#include <net/bpf.h>
100#include <net/if.h>
khenaidood948f772021-08-11 17:49:24 -0400101#include <net/if_clone.h>
khenaidooac637102019-01-14 15:44:34 -0500102#include <net/if_types.h>
103#include <net/route.h>
104#include <netinet/in.h>
105#include <termios.h>
106#include <netinet/ip.h>
107#include <net/ip_mroute/ip_mroute.h>
108'
109
110includes_FreeBSD='
111#include <sys/capsicum.h>
112#include <sys/param.h>
113#include <sys/types.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100114#include <sys/disk.h>
khenaidooac637102019-01-14 15:44:34 -0500115#include <sys/event.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100116#include <sys/sched.h>
Scott Baker8461e152019-10-01 14:44:30 -0700117#include <sys/select.h>
khenaidooac637102019-01-14 15:44:34 -0500118#include <sys/socket.h>
khenaidood948f772021-08-11 17:49:24 -0400119#include <sys/un.h>
khenaidooac637102019-01-14 15:44:34 -0500120#include <sys/sockio.h>
121#include <sys/stat.h>
122#include <sys/sysctl.h>
123#include <sys/mman.h>
124#include <sys/mount.h>
125#include <sys/wait.h>
126#include <sys/ioctl.h>
127#include <net/bpf.h>
128#include <net/if.h>
129#include <net/if_types.h>
130#include <net/route.h>
131#include <netinet/in.h>
132#include <termios.h>
133#include <netinet/ip.h>
134#include <netinet/ip_mroute.h>
135#include <sys/extattr.h>
136
137#if __FreeBSD__ >= 10
138#define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
139#undef SIOCAIFADDR
140#define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
141#undef SIOCSIFPHYADDR
142#define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
143#endif
144'
145
146includes_Linux='
147#define _LARGEFILE_SOURCE
148#define _LARGEFILE64_SOURCE
149#ifndef __LP64__
150#define _FILE_OFFSET_BITS 64
151#endif
152#define _GNU_SOURCE
153
154// <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
155// these structures. We just include them copied from <bits/termios.h>.
156#if defined(__powerpc__)
157struct sgttyb {
158 char sg_ispeed;
159 char sg_ospeed;
160 char sg_erase;
161 char sg_kill;
162 short sg_flags;
163};
164
165struct tchars {
166 char t_intrc;
167 char t_quitc;
168 char t_startc;
169 char t_stopc;
170 char t_eofc;
171 char t_brkc;
172};
173
174struct ltchars {
175 char t_suspc;
176 char t_dsuspc;
177 char t_rprntc;
178 char t_flushc;
179 char t_werasc;
180 char t_lnextc;
181};
182#endif
183
184#include <bits/sockaddr.h>
185#include <sys/epoll.h>
186#include <sys/eventfd.h>
187#include <sys/inotify.h>
188#include <sys/ioctl.h>
189#include <sys/mman.h>
190#include <sys/mount.h>
191#include <sys/prctl.h>
192#include <sys/stat.h>
193#include <sys/types.h>
194#include <sys/time.h>
Scott Baker8461e152019-10-01 14:44:30 -0700195#include <sys/select.h>
Stephane Barbarie260a5632019-02-26 16:12:49 -0500196#include <sys/signalfd.h>
khenaidooac637102019-01-14 15:44:34 -0500197#include <sys/socket.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100198#include <sys/timerfd.h>
199#include <sys/uio.h>
khenaidooac637102019-01-14 15:44:34 -0500200#include <sys/xattr.h>
Scott Baker8461e152019-10-01 14:44:30 -0700201#include <linux/bpf.h>
202#include <linux/can.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100203#include <linux/can/error.h>
204#include <linux/can/raw.h>
Scott Baker8461e152019-10-01 14:44:30 -0700205#include <linux/capability.h>
206#include <linux/cryptouser.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100207#include <linux/devlink.h>
208#include <linux/dm-ioctl.h>
Stephane Barbarie260a5632019-02-26 16:12:49 -0500209#include <linux/errqueue.h>
khenaidood948f772021-08-11 17:49:24 -0400210#include <linux/ethtool_netlink.h>
Scott Baker8461e152019-10-01 14:44:30 -0700211#include <linux/falloc.h>
212#include <linux/fanotify.h>
213#include <linux/filter.h>
214#include <linux/fs.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100215#include <linux/fscrypt.h>
216#include <linux/fsverity.h>
Scott Baker8461e152019-10-01 14:44:30 -0700217#include <linux/genetlink.h>
218#include <linux/hdreg.h>
khenaidood948f772021-08-11 17:49:24 -0400219#include <linux/hidraw.h>
220#include <linux/icmp.h>
Scott Baker8461e152019-10-01 14:44:30 -0700221#include <linux/icmpv6.h>
khenaidooac637102019-01-14 15:44:34 -0500222#include <linux/if.h>
Scott Baker8461e152019-10-01 14:44:30 -0700223#include <linux/if_addr.h>
khenaidooac637102019-01-14 15:44:34 -0500224#include <linux/if_alg.h>
225#include <linux/if_arp.h>
226#include <linux/if_ether.h>
227#include <linux/if_ppp.h>
228#include <linux/if_tun.h>
229#include <linux/if_packet.h>
Scott Baker8461e152019-10-01 14:44:30 -0700230#include <linux/if_xdp.h>
khenaidood948f772021-08-11 17:49:24 -0400231#include <linux/input.h>
khenaidooac637102019-01-14 15:44:34 -0500232#include <linux/kexec.h>
233#include <linux/keyctl.h>
Scott Baker8461e152019-10-01 14:44:30 -0700234#include <linux/loop.h>
khenaidood948f772021-08-11 17:49:24 -0400235#include <linux/lwtunnel.h>
khenaidooac637102019-01-14 15:44:34 -0500236#include <linux/magic.h>
237#include <linux/memfd.h>
238#include <linux/module.h>
239#include <linux/netfilter/nfnetlink.h>
240#include <linux/netlink.h>
241#include <linux/net_namespace.h>
khenaidood948f772021-08-11 17:49:24 -0400242#include <linux/nfc.h>
Scott Baker8461e152019-10-01 14:44:30 -0700243#include <linux/nsfs.h>
khenaidooac637102019-01-14 15:44:34 -0500244#include <linux/perf_event.h>
khenaidood948f772021-08-11 17:49:24 -0400245#include <linux/pps.h>
Scott Baker8461e152019-10-01 14:44:30 -0700246#include <linux/ptrace.h>
khenaidooac637102019-01-14 15:44:34 -0500247#include <linux/random.h>
248#include <linux/reboot.h>
Scott Baker8461e152019-10-01 14:44:30 -0700249#include <linux/rtc.h>
khenaidooac637102019-01-14 15:44:34 -0500250#include <linux/rtnetlink.h>
khenaidooac637102019-01-14 15:44:34 -0500251#include <linux/sched.h>
252#include <linux/seccomp.h>
khenaidooac637102019-01-14 15:44:34 -0500253#include <linux/serial.h>
Scott Baker8461e152019-10-01 14:44:30 -0700254#include <linux/sockios.h>
khenaidooac637102019-01-14 15:44:34 -0500255#include <linux/taskstats.h>
Scott Baker8461e152019-10-01 14:44:30 -0700256#include <linux/tipc.h>
257#include <linux/vm_sockets.h>
258#include <linux/wait.h>
khenaidooac637102019-01-14 15:44:34 -0500259#include <linux/watchdog.h>
Scott Baker8461e152019-10-01 14:44:30 -0700260
khenaidooac637102019-01-14 15:44:34 -0500261#include <mtd/ubi-user.h>
khenaidood948f772021-08-11 17:49:24 -0400262#include <mtd/mtd-user.h>
khenaidooac637102019-01-14 15:44:34 -0500263#include <net/route.h>
264
265#if defined(__sparc__)
266// On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
267// definition in glibc. As only the error constants are needed here, include the
268// generic termibits.h (which is included by termbits.h on sparc).
269#include <asm-generic/termbits.h>
270#else
271#include <asm/termbits.h>
272#endif
273
274#ifndef MSG_FASTOPEN
275#define MSG_FASTOPEN 0x20000000
276#endif
277
278#ifndef PTRACE_GETREGS
279#define PTRACE_GETREGS 0xc
280#endif
281
282#ifndef PTRACE_SETREGS
283#define PTRACE_SETREGS 0xd
284#endif
285
286#ifndef SOL_NETLINK
287#define SOL_NETLINK 270
288#endif
289
290#ifdef SOL_BLUETOOTH
291// SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
292// but it is already in bluetooth_linux.go
293#undef SOL_BLUETOOTH
294#endif
295
296// Certain constants are missing from the fs/crypto UAPI
297#define FS_KEY_DESC_PREFIX "fscrypt:"
298#define FS_KEY_DESC_PREFIX_SIZE 8
299#define FS_MAX_KEY_SIZE 64
Scott Baker8461e152019-10-01 14:44:30 -0700300
301// The code generator produces -0x1 for (~0), but an unsigned value is necessary
302// for the tipc_subscr timeout __u32 field.
303#undef TIPC_WAIT_FOREVER
304#define TIPC_WAIT_FOREVER 0xffffffff
Andrea Campanella3614a922021-02-25 12:40:42 +0100305
306// Copied from linux/l2tp.h
307// Including linux/l2tp.h here causes conflicts between linux/in.h
308// and netinet/in.h included via net/route.h above.
309#define IPPROTO_L2TP 115
khenaidood948f772021-08-11 17:49:24 -0400310
311// Copied from linux/hid.h.
312// Keep in sync with the size of the referenced fields.
313#define _HIDIOCGRAWNAME_LEN 128 // sizeof_field(struct hid_device, name)
314#define _HIDIOCGRAWPHYS_LEN 64 // sizeof_field(struct hid_device, phys)
315#define _HIDIOCGRAWUNIQ_LEN 64 // sizeof_field(struct hid_device, uniq)
316
317#define _HIDIOCGRAWNAME HIDIOCGRAWNAME(_HIDIOCGRAWNAME_LEN)
318#define _HIDIOCGRAWPHYS HIDIOCGRAWPHYS(_HIDIOCGRAWPHYS_LEN)
319#define _HIDIOCGRAWUNIQ HIDIOCGRAWUNIQ(_HIDIOCGRAWUNIQ_LEN)
320
khenaidooac637102019-01-14 15:44:34 -0500321'
322
323includes_NetBSD='
324#include <sys/types.h>
325#include <sys/param.h>
326#include <sys/event.h>
327#include <sys/extattr.h>
328#include <sys/mman.h>
329#include <sys/mount.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100330#include <sys/sched.h>
Scott Baker8461e152019-10-01 14:44:30 -0700331#include <sys/select.h>
khenaidooac637102019-01-14 15:44:34 -0500332#include <sys/socket.h>
333#include <sys/sockio.h>
334#include <sys/sysctl.h>
335#include <sys/termios.h>
336#include <sys/ttycom.h>
337#include <sys/wait.h>
338#include <net/bpf.h>
339#include <net/if.h>
340#include <net/if_types.h>
341#include <net/route.h>
342#include <netinet/in.h>
343#include <netinet/in_systm.h>
344#include <netinet/ip.h>
345#include <netinet/ip_mroute.h>
346#include <netinet/if_ether.h>
347
348// Needed since <sys/param.h> refers to it...
349#define schedppq 1
350'
351
352includes_OpenBSD='
353#include <sys/types.h>
354#include <sys/param.h>
355#include <sys/event.h>
356#include <sys/mman.h>
357#include <sys/mount.h>
Scott Baker8461e152019-10-01 14:44:30 -0700358#include <sys/select.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100359#include <sys/sched.h>
khenaidooac637102019-01-14 15:44:34 -0500360#include <sys/socket.h>
361#include <sys/sockio.h>
362#include <sys/stat.h>
363#include <sys/sysctl.h>
364#include <sys/termios.h>
365#include <sys/ttycom.h>
366#include <sys/unistd.h>
367#include <sys/wait.h>
368#include <net/bpf.h>
369#include <net/if.h>
370#include <net/if_types.h>
371#include <net/if_var.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#include <net/if_bridge.h>
379
380// We keep some constants not supported in OpenBSD 5.5 and beyond for
381// the promise of compatibility.
382#define EMUL_ENABLED 0x1
383#define EMUL_NATIVE 0x2
384#define IPV6_FAITH 0x1d
385#define IPV6_OPTIONS 0x1
386#define IPV6_RTHDR_STRICT 0x1
387#define IPV6_SOCKOPT_RESERVED1 0x3
388#define SIOCGIFGENERIC 0xc020693a
389#define SIOCSIFGENERIC 0x80206939
390#define WALTSIG 0x4
391'
392
393includes_SunOS='
394#include <limits.h>
395#include <sys/types.h>
Scott Baker8461e152019-10-01 14:44:30 -0700396#include <sys/select.h>
khenaidooac637102019-01-14 15:44:34 -0500397#include <sys/socket.h>
398#include <sys/sockio.h>
399#include <sys/stat.h>
khenaidood948f772021-08-11 17:49:24 -0400400#include <sys/stream.h>
khenaidooac637102019-01-14 15:44:34 -0500401#include <sys/mman.h>
402#include <sys/wait.h>
403#include <sys/ioctl.h>
404#include <sys/mkdev.h>
405#include <net/bpf.h>
406#include <net/if.h>
407#include <net/if_arp.h>
408#include <net/if_types.h>
409#include <net/route.h>
khenaidood948f772021-08-11 17:49:24 -0400410#include <netinet/icmp6.h>
khenaidooac637102019-01-14 15:44:34 -0500411#include <netinet/in.h>
khenaidooac637102019-01-14 15:44:34 -0500412#include <netinet/ip.h>
413#include <netinet/ip_mroute.h>
khenaidood948f772021-08-11 17:49:24 -0400414#include <termios.h>
khenaidooac637102019-01-14 15:44:34 -0500415'
416
417
418includes='
419#include <sys/types.h>
420#include <sys/file.h>
421#include <fcntl.h>
422#include <dirent.h>
423#include <sys/socket.h>
424#include <netinet/in.h>
425#include <netinet/ip.h>
426#include <netinet/ip6.h>
427#include <netinet/tcp.h>
428#include <errno.h>
429#include <sys/signal.h>
430#include <signal.h>
431#include <sys/resource.h>
432#include <time.h>
433'
434ccflags="$@"
435
436# Write go tool cgo -godefs input.
437(
438 echo package unix
439 echo
440 echo '/*'
441 indirect="includes_$(uname)"
442 echo "${!indirect} $includes"
443 echo '*/'
444 echo 'import "C"'
445 echo 'import "syscall"'
446 echo
447 echo 'const ('
448
449 # The gcc command line prints all the #defines
450 # it encounters while processing the input
451 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
452 awk '
453 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
454
455 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
456 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
457 $2 ~ /^(SCM_SRCRT)$/ {next}
458 $2 ~ /^(MAP_FAILED)$/ {next}
459 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
460
461 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
462 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
463
464 $2 !~ /^ECCAPBITS/ &&
465 $2 !~ /^ETH_/ &&
466 $2 !~ /^EPROC_/ &&
467 $2 !~ /^EQUIV_/ &&
468 $2 !~ /^EXPR_/ &&
khenaidood948f772021-08-11 17:49:24 -0400469 $2 !~ /^EVIOC/ &&
470 $2 !~ /^EV_/ &&
khenaidooac637102019-01-14 15:44:34 -0500471 $2 ~ /^E[A-Z0-9_]+$/ ||
472 $2 ~ /^B[0-9_]+$/ ||
473 $2 ~ /^(OLD|NEW)DEV$/ ||
474 $2 == "BOTHER" ||
475 $2 ~ /^CI?BAUD(EX)?$/ ||
476 $2 == "IBSHIFT" ||
477 $2 ~ /^V[A-Z0-9]+$/ ||
478 $2 ~ /^CS[A-Z0-9]/ ||
479 $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
480 $2 ~ /^IGN/ ||
481 $2 ~ /^IX(ON|ANY|OFF)$/ ||
482 $2 ~ /^IN(LCR|PCK)$/ ||
483 $2 !~ "X86_CR3_PCID_NOFLUSH" &&
484 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
485 $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
486 $2 == "BRKINT" ||
487 $2 == "HUPCL" ||
488 $2 == "PENDIN" ||
489 $2 == "TOSTOP" ||
490 $2 == "XCASE" ||
491 $2 == "ALTWERASE" ||
492 $2 == "NOKERNINFO" ||
Scott Baker8461e152019-10-01 14:44:30 -0700493 $2 == "NFDBITS" ||
khenaidooac637102019-01-14 15:44:34 -0500494 $2 ~ /^PAR/ ||
495 $2 ~ /^SIG[^_]/ ||
496 $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
497 $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
498 $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
499 $2 ~ /^O?XTABS$/ ||
500 $2 ~ /^TC[IO](ON|OFF)$/ ||
501 $2 ~ /^IN_/ ||
502 $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
Scott Baker8461e152019-10-01 14:44:30 -0700503 $2 ~ /^LO_(KEY|NAME)_SIZE$/ ||
504 $2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ ||
khenaidood948f772021-08-11 17:49:24 -0400505 $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)_/ ||
506 $2 ~ /^NFC_(GENL|PROTO|COMM|RF|SE|DIRECTION|LLCP|SOCKPROTO)_/ ||
507 $2 ~ /^NFC_.*_(MAX)?SIZE$/ ||
508 $2 ~ /^RAW_PAYLOAD_/ ||
khenaidooac637102019-01-14 15:44:34 -0500509 $2 ~ /^TP_STATUS_/ ||
510 $2 ~ /^FALLOC_/ ||
khenaidood948f772021-08-11 17:49:24 -0400511 $2 ~ /^ICMPV?6?_(FILTER|SEC)/ ||
khenaidooac637102019-01-14 15:44:34 -0500512 $2 == "SOMAXCONN" ||
513 $2 == "NAME_MAX" ||
514 $2 == "IFNAMSIZ" ||
515 $2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
516 $2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
517 $2 ~ /^HW_MACHINE$/ ||
518 $2 ~ /^SYSCTL_VERS/ ||
519 $2 !~ "MNT_BITS" &&
520 $2 ~ /^(MS|MNT|UMOUNT)_/ ||
Scott Baker8461e152019-10-01 14:44:30 -0700521 $2 ~ /^NS_GET_/ ||
khenaidooac637102019-01-14 15:44:34 -0500522 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
Andrea Campanella3614a922021-02-25 12:40:42 +0100523 $2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT|TFD)_/ ||
khenaidooac637102019-01-14 15:44:34 -0500524 $2 ~ /^KEXEC_/ ||
525 $2 ~ /^LINUX_REBOOT_CMD_/ ||
526 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
527 $2 ~ /^MODULE_INIT_/ ||
528 $2 !~ "NLA_TYPE_MASK" &&
Andrea Campanella3614a922021-02-25 12:40:42 +0100529 $2 !~ /^RTC_VL_(ACCURACY|BACKUP|DATA)/ &&
khenaidooac637102019-01-14 15:44:34 -0500530 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
khenaidood948f772021-08-11 17:49:24 -0400531 $2 ~ /^FIORDCHK$/ ||
khenaidooac637102019-01-14 15:44:34 -0500532 $2 ~ /^SIOC/ ||
533 $2 ~ /^TIOC/ ||
534 $2 ~ /^TCGET/ ||
535 $2 ~ /^TCSET/ ||
536 $2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
537 $2 !~ "RTF_BITS" &&
Andrea Campanella3614a922021-02-25 12:40:42 +0100538 $2 ~ /^(IFF|IFT|NET_RT|RTM(GRP)?|RTF|RTV|RTA|RTAX)_/ ||
khenaidooac637102019-01-14 15:44:34 -0500539 $2 ~ /^BIOC/ ||
Andrea Campanella3614a922021-02-25 12:40:42 +0100540 $2 ~ /^DIOC/ ||
khenaidooac637102019-01-14 15:44:34 -0500541 $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
542 $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
543 $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
544 $2 ~ /^CLONE_[A-Z_]+/ ||
Scott Baker8461e152019-10-01 14:44:30 -0700545 $2 !~ /^(BPF_TIMEVAL|BPF_FIB_LOOKUP_[A-Z]+)$/ &&
khenaidooac637102019-01-14 15:44:34 -0500546 $2 ~ /^(BPF|DLT)_/ ||
547 $2 ~ /^(CLOCK|TIMER)_/ ||
548 $2 ~ /^CAN_/ ||
549 $2 ~ /^CAP_/ ||
Andrea Campanella3614a922021-02-25 12:40:42 +0100550 $2 ~ /^CP_/ ||
551 $2 ~ /^CPUSTATES$/ ||
khenaidood948f772021-08-11 17:49:24 -0400552 $2 ~ /^CTLIOCGINFO$/ ||
khenaidooac637102019-01-14 15:44:34 -0500553 $2 ~ /^ALG_/ ||
Andrea Campanella3614a922021-02-25 12:40:42 +0100554 $2 ~ /^FI(CLONE|DEDUPERANGE)/ ||
555 $2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE)/ ||
556 $2 ~ /^FS_IOC_.*(ENCRYPTION|VERITY|[GS]ETFLAGS)/ ||
557 $2 ~ /^FS_VERITY_/ ||
558 $2 ~ /^FSCRYPT_/ ||
559 $2 ~ /^DM_/ ||
khenaidooac637102019-01-14 15:44:34 -0500560 $2 ~ /^GRND_/ ||
Stephane Barbarie260a5632019-02-26 16:12:49 -0500561 $2 ~ /^RND/ ||
khenaidooac637102019-01-14 15:44:34 -0500562 $2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
563 $2 ~ /^KEYCTL_/ ||
khenaidood948f772021-08-11 17:49:24 -0400564 $2 ~ /^PERF_/ ||
khenaidooac637102019-01-14 15:44:34 -0500565 $2 ~ /^SECCOMP_MODE_/ ||
566 $2 ~ /^SPLICE_/ ||
567 $2 ~ /^SYNC_FILE_RANGE_/ ||
568 $2 !~ /^AUDIT_RECORD_MAGIC/ &&
569 $2 !~ /IOC_MAGIC/ &&
570 $2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
571 $2 ~ /^(VM|VMADDR)_/ ||
572 $2 ~ /^IOCTL_VM_SOCKETS_/ ||
573 $2 ~ /^(TASKSTATS|TS)_/ ||
574 $2 ~ /^CGROUPSTATS_/ ||
575 $2 ~ /^GENL_/ ||
576 $2 ~ /^STATX_/ ||
577 $2 ~ /^RENAME/ ||
578 $2 ~ /^UBI_IOC[A-Z]/ ||
579 $2 ~ /^UTIME_/ ||
580 $2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
581 $2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
582 $2 ~ /^FSOPT_/ ||
khenaidood948f772021-08-11 17:49:24 -0400583 $2 ~ /^WDIO[CFS]_/ ||
khenaidooac637102019-01-14 15:44:34 -0500584 $2 ~ /^NFN/ ||
585 $2 ~ /^XDP_/ ||
Andrea Campanella3614a922021-02-25 12:40:42 +0100586 $2 ~ /^RWF_/ ||
khenaidooac637102019-01-14 15:44:34 -0500587 $2 ~ /^(HDIO|WIN|SMART)_/ ||
Scott Baker8461e152019-10-01 14:44:30 -0700588 $2 ~ /^CRYPTO_/ ||
589 $2 ~ /^TIPC_/ ||
khenaidood948f772021-08-11 17:49:24 -0400590 $2 !~ "DEVLINK_RELOAD_LIMITS_VALID_MASK" &&
Andrea Campanella3614a922021-02-25 12:40:42 +0100591 $2 ~ /^DEVLINK_/ ||
khenaidood948f772021-08-11 17:49:24 -0400592 $2 ~ /^ETHTOOL_/ ||
593 $2 ~ /^LWTUNNEL_IP/ ||
khenaidooac637102019-01-14 15:44:34 -0500594 $2 !~ "WMESGLEN" &&
595 $2 ~ /^W[A-Z0-9]+$/ ||
596 $2 ~/^PPPIOC/ ||
William Kurkiandaa6bb22019-03-07 12:26:28 -0500597 $2 ~ /^FAN_|FANOTIFY_/ ||
khenaidood948f772021-08-11 17:49:24 -0400598 $2 == "HID_MAX_DESCRIPTOR_SIZE" ||
599 $2 ~ /^_?HIDIOC/ ||
600 $2 ~ /^BUS_(USB|HIL|BLUETOOTH|VIRTUAL)$/ ||
601 $2 ~ /^MTD/ ||
602 $2 ~ /^OTP/ ||
603 $2 ~ /^MEM/ ||
khenaidooac637102019-01-14 15:44:34 -0500604 $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
605 $2 ~ /^__WCOREFLAG$/ {next}
606 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
607
608 {next}
609 ' | sort
610
611 echo ')'
612) >_const.go
613
614# Pull out the error names for later.
615errors=$(
616 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
617 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
618 sort
619)
620
621# Pull out the signal names for later.
622signals=$(
623 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
624 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
625 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
626 sort
627)
628
629# Again, writing regexps to a file.
630echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
631 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
632 sort >_error.grep
633echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
634 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
635 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
636 sort >_signal.grep
637
638echo '// mkerrors.sh' "$@"
639echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
640echo
khenaidood948f772021-08-11 17:49:24 -0400641echo "//go:build ${GOARCH} && ${GOOS}"
khenaidooac637102019-01-14 15:44:34 -0500642echo "// +build ${GOARCH},${GOOS}"
643echo
644go tool cgo -godefs -- "$@" _const.go >_error.out
645cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
646echo
647echo '// Errors'
648echo 'const ('
649cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
650echo ')'
651
652echo
653echo '// Signals'
654echo 'const ('
655cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
656echo ')'
657
658# Run C program to print error and syscall strings.
659(
660 echo -E "
661#include <stdio.h>
662#include <stdlib.h>
663#include <errno.h>
664#include <ctype.h>
665#include <string.h>
666#include <signal.h>
667
668#define nelem(x) (sizeof(x)/sizeof((x)[0]))
669
670enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
671
672struct tuple {
673 int num;
674 const char *name;
675};
676
677struct tuple errors[] = {
678"
679 for i in $errors
680 do
681 echo -E ' {'$i', "'$i'" },'
682 done
683
684 echo -E "
685};
686
687struct tuple signals[] = {
688"
689 for i in $signals
690 do
691 echo -E ' {'$i', "'$i'" },'
692 done
693
694 # Use -E because on some systems bash builtin interprets \n itself.
695 echo -E '
696};
697
698static int
699tuplecmp(const void *a, const void *b)
700{
701 return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
702}
703
704int
705main(void)
706{
707 int i, e;
708 char buf[1024], *p;
709
710 printf("\n\n// Error table\n");
711 printf("var errorList = [...]struct {\n");
712 printf("\tnum syscall.Errno\n");
713 printf("\tname string\n");
714 printf("\tdesc string\n");
715 printf("} {\n");
716 qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
717 for(i=0; i<nelem(errors); i++) {
718 e = errors[i].num;
719 if(i > 0 && errors[i-1].num == e)
720 continue;
721 strcpy(buf, strerror(e));
722 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
723 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
724 buf[0] += a - A;
725 printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
726 }
727 printf("}\n\n");
728
729 printf("\n\n// Signal table\n");
730 printf("var signalList = [...]struct {\n");
731 printf("\tnum syscall.Signal\n");
732 printf("\tname string\n");
733 printf("\tdesc string\n");
734 printf("} {\n");
735 qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
736 for(i=0; i<nelem(signals); i++) {
737 e = signals[i].num;
738 if(i > 0 && signals[i-1].num == e)
739 continue;
740 strcpy(buf, strsignal(e));
741 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
742 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
743 buf[0] += a - A;
744 // cut trailing : number.
745 p = strrchr(buf, ":"[0]);
746 if(p)
747 *p = '\0';
748 printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
749 }
750 printf("}\n\n");
751
752 return 0;
753}
754
755'
756) >_errors.c
757
758$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out