blob: 1bef7148d2a20eb3af8ad7977fc669cf22012ab1 [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
59#include <stdint.h>
60#include <sys/attr.h>
Andrea Campanella3614a922021-02-25 12:40:42 +010061#include <sys/clonefile.h>
khenaidooac637102019-01-14 15:44:34 -050062#include <sys/types.h>
63#include <sys/event.h>
64#include <sys/ptrace.h>
Scott Baker8461e152019-10-01 14:44:30 -070065#include <sys/select.h>
khenaidooac637102019-01-14 15:44:34 -050066#include <sys/socket.h>
67#include <sys/sockio.h>
68#include <sys/sysctl.h>
69#include <sys/mman.h>
70#include <sys/mount.h>
71#include <sys/utsname.h>
72#include <sys/wait.h>
73#include <sys/xattr.h>
74#include <net/bpf.h>
75#include <net/if.h>
76#include <net/if_types.h>
77#include <net/route.h>
78#include <netinet/in.h>
79#include <netinet/ip.h>
80#include <termios.h>
81'
82
83includes_DragonFly='
84#include <sys/types.h>
85#include <sys/event.h>
Scott Baker8461e152019-10-01 14:44:30 -070086#include <sys/select.h>
khenaidooac637102019-01-14 15:44:34 -050087#include <sys/socket.h>
88#include <sys/sockio.h>
89#include <sys/stat.h>
90#include <sys/sysctl.h>
91#include <sys/mman.h>
92#include <sys/mount.h>
93#include <sys/wait.h>
94#include <sys/ioctl.h>
95#include <net/bpf.h>
96#include <net/if.h>
97#include <net/if_types.h>
98#include <net/route.h>
99#include <netinet/in.h>
100#include <termios.h>
101#include <netinet/ip.h>
102#include <net/ip_mroute/ip_mroute.h>
103'
104
105includes_FreeBSD='
106#include <sys/capsicum.h>
107#include <sys/param.h>
108#include <sys/types.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100109#include <sys/disk.h>
khenaidooac637102019-01-14 15:44:34 -0500110#include <sys/event.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100111#include <sys/sched.h>
Scott Baker8461e152019-10-01 14:44:30 -0700112#include <sys/select.h>
khenaidooac637102019-01-14 15:44:34 -0500113#include <sys/socket.h>
114#include <sys/sockio.h>
115#include <sys/stat.h>
116#include <sys/sysctl.h>
117#include <sys/mman.h>
118#include <sys/mount.h>
119#include <sys/wait.h>
120#include <sys/ioctl.h>
121#include <net/bpf.h>
122#include <net/if.h>
123#include <net/if_types.h>
124#include <net/route.h>
125#include <netinet/in.h>
126#include <termios.h>
127#include <netinet/ip.h>
128#include <netinet/ip_mroute.h>
129#include <sys/extattr.h>
130
131#if __FreeBSD__ >= 10
132#define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
133#undef SIOCAIFADDR
134#define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
135#undef SIOCSIFPHYADDR
136#define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
137#endif
138'
139
140includes_Linux='
141#define _LARGEFILE_SOURCE
142#define _LARGEFILE64_SOURCE
143#ifndef __LP64__
144#define _FILE_OFFSET_BITS 64
145#endif
146#define _GNU_SOURCE
147
148// <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
149// these structures. We just include them copied from <bits/termios.h>.
150#if defined(__powerpc__)
151struct sgttyb {
152 char sg_ispeed;
153 char sg_ospeed;
154 char sg_erase;
155 char sg_kill;
156 short sg_flags;
157};
158
159struct tchars {
160 char t_intrc;
161 char t_quitc;
162 char t_startc;
163 char t_stopc;
164 char t_eofc;
165 char t_brkc;
166};
167
168struct ltchars {
169 char t_suspc;
170 char t_dsuspc;
171 char t_rprntc;
172 char t_flushc;
173 char t_werasc;
174 char t_lnextc;
175};
176#endif
177
178#include <bits/sockaddr.h>
179#include <sys/epoll.h>
180#include <sys/eventfd.h>
181#include <sys/inotify.h>
182#include <sys/ioctl.h>
183#include <sys/mman.h>
184#include <sys/mount.h>
185#include <sys/prctl.h>
186#include <sys/stat.h>
187#include <sys/types.h>
188#include <sys/time.h>
Scott Baker8461e152019-10-01 14:44:30 -0700189#include <sys/select.h>
Stephane Barbarie260a5632019-02-26 16:12:49 -0500190#include <sys/signalfd.h>
khenaidooac637102019-01-14 15:44:34 -0500191#include <sys/socket.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100192#include <sys/timerfd.h>
193#include <sys/uio.h>
khenaidooac637102019-01-14 15:44:34 -0500194#include <sys/xattr.h>
Scott Baker8461e152019-10-01 14:44:30 -0700195#include <linux/bpf.h>
196#include <linux/can.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100197#include <linux/can/error.h>
198#include <linux/can/raw.h>
Scott Baker8461e152019-10-01 14:44:30 -0700199#include <linux/capability.h>
200#include <linux/cryptouser.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100201#include <linux/devlink.h>
202#include <linux/dm-ioctl.h>
Stephane Barbarie260a5632019-02-26 16:12:49 -0500203#include <linux/errqueue.h>
Scott Baker8461e152019-10-01 14:44:30 -0700204#include <linux/falloc.h>
205#include <linux/fanotify.h>
206#include <linux/filter.h>
207#include <linux/fs.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100208#include <linux/fscrypt.h>
209#include <linux/fsverity.h>
Scott Baker8461e152019-10-01 14:44:30 -0700210#include <linux/genetlink.h>
211#include <linux/hdreg.h>
212#include <linux/icmpv6.h>
khenaidooac637102019-01-14 15:44:34 -0500213#include <linux/if.h>
Scott Baker8461e152019-10-01 14:44:30 -0700214#include <linux/if_addr.h>
khenaidooac637102019-01-14 15:44:34 -0500215#include <linux/if_alg.h>
216#include <linux/if_arp.h>
217#include <linux/if_ether.h>
218#include <linux/if_ppp.h>
219#include <linux/if_tun.h>
220#include <linux/if_packet.h>
Scott Baker8461e152019-10-01 14:44:30 -0700221#include <linux/if_xdp.h>
khenaidooac637102019-01-14 15:44:34 -0500222#include <linux/kexec.h>
223#include <linux/keyctl.h>
Scott Baker8461e152019-10-01 14:44:30 -0700224#include <linux/loop.h>
khenaidooac637102019-01-14 15:44:34 -0500225#include <linux/magic.h>
226#include <linux/memfd.h>
227#include <linux/module.h>
228#include <linux/netfilter/nfnetlink.h>
229#include <linux/netlink.h>
230#include <linux/net_namespace.h>
Scott Baker8461e152019-10-01 14:44:30 -0700231#include <linux/nsfs.h>
khenaidooac637102019-01-14 15:44:34 -0500232#include <linux/perf_event.h>
Scott Baker8461e152019-10-01 14:44:30 -0700233#include <linux/ptrace.h>
khenaidooac637102019-01-14 15:44:34 -0500234#include <linux/random.h>
235#include <linux/reboot.h>
Scott Baker8461e152019-10-01 14:44:30 -0700236#include <linux/rtc.h>
khenaidooac637102019-01-14 15:44:34 -0500237#include <linux/rtnetlink.h>
khenaidooac637102019-01-14 15:44:34 -0500238#include <linux/sched.h>
239#include <linux/seccomp.h>
khenaidooac637102019-01-14 15:44:34 -0500240#include <linux/serial.h>
Scott Baker8461e152019-10-01 14:44:30 -0700241#include <linux/sockios.h>
khenaidooac637102019-01-14 15:44:34 -0500242#include <linux/taskstats.h>
Scott Baker8461e152019-10-01 14:44:30 -0700243#include <linux/tipc.h>
244#include <linux/vm_sockets.h>
245#include <linux/wait.h>
khenaidooac637102019-01-14 15:44:34 -0500246#include <linux/watchdog.h>
Scott Baker8461e152019-10-01 14:44:30 -0700247
khenaidooac637102019-01-14 15:44:34 -0500248#include <mtd/ubi-user.h>
249#include <net/route.h>
250
251#if defined(__sparc__)
252// On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
253// definition in glibc. As only the error constants are needed here, include the
254// generic termibits.h (which is included by termbits.h on sparc).
255#include <asm-generic/termbits.h>
256#else
257#include <asm/termbits.h>
258#endif
259
260#ifndef MSG_FASTOPEN
261#define MSG_FASTOPEN 0x20000000
262#endif
263
264#ifndef PTRACE_GETREGS
265#define PTRACE_GETREGS 0xc
266#endif
267
268#ifndef PTRACE_SETREGS
269#define PTRACE_SETREGS 0xd
270#endif
271
272#ifndef SOL_NETLINK
273#define SOL_NETLINK 270
274#endif
275
276#ifdef SOL_BLUETOOTH
277// SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
278// but it is already in bluetooth_linux.go
279#undef SOL_BLUETOOTH
280#endif
281
282// Certain constants are missing from the fs/crypto UAPI
283#define FS_KEY_DESC_PREFIX "fscrypt:"
284#define FS_KEY_DESC_PREFIX_SIZE 8
285#define FS_MAX_KEY_SIZE 64
Scott Baker8461e152019-10-01 14:44:30 -0700286
287// The code generator produces -0x1 for (~0), but an unsigned value is necessary
288// for the tipc_subscr timeout __u32 field.
289#undef TIPC_WAIT_FOREVER
290#define TIPC_WAIT_FOREVER 0xffffffff
Andrea Campanella3614a922021-02-25 12:40:42 +0100291
292// Copied from linux/l2tp.h
293// Including linux/l2tp.h here causes conflicts between linux/in.h
294// and netinet/in.h included via net/route.h above.
295#define IPPROTO_L2TP 115
khenaidooac637102019-01-14 15:44:34 -0500296'
297
298includes_NetBSD='
299#include <sys/types.h>
300#include <sys/param.h>
301#include <sys/event.h>
302#include <sys/extattr.h>
303#include <sys/mman.h>
304#include <sys/mount.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100305#include <sys/sched.h>
Scott Baker8461e152019-10-01 14:44:30 -0700306#include <sys/select.h>
khenaidooac637102019-01-14 15:44:34 -0500307#include <sys/socket.h>
308#include <sys/sockio.h>
309#include <sys/sysctl.h>
310#include <sys/termios.h>
311#include <sys/ttycom.h>
312#include <sys/wait.h>
313#include <net/bpf.h>
314#include <net/if.h>
315#include <net/if_types.h>
316#include <net/route.h>
317#include <netinet/in.h>
318#include <netinet/in_systm.h>
319#include <netinet/ip.h>
320#include <netinet/ip_mroute.h>
321#include <netinet/if_ether.h>
322
323// Needed since <sys/param.h> refers to it...
324#define schedppq 1
325'
326
327includes_OpenBSD='
328#include <sys/types.h>
329#include <sys/param.h>
330#include <sys/event.h>
331#include <sys/mman.h>
332#include <sys/mount.h>
Scott Baker8461e152019-10-01 14:44:30 -0700333#include <sys/select.h>
Andrea Campanella3614a922021-02-25 12:40:42 +0100334#include <sys/sched.h>
khenaidooac637102019-01-14 15:44:34 -0500335#include <sys/socket.h>
336#include <sys/sockio.h>
337#include <sys/stat.h>
338#include <sys/sysctl.h>
339#include <sys/termios.h>
340#include <sys/ttycom.h>
341#include <sys/unistd.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/if_var.h>
347#include <net/route.h>
348#include <netinet/in.h>
349#include <netinet/in_systm.h>
350#include <netinet/ip.h>
351#include <netinet/ip_mroute.h>
352#include <netinet/if_ether.h>
353#include <net/if_bridge.h>
354
355// We keep some constants not supported in OpenBSD 5.5 and beyond for
356// the promise of compatibility.
357#define EMUL_ENABLED 0x1
358#define EMUL_NATIVE 0x2
359#define IPV6_FAITH 0x1d
360#define IPV6_OPTIONS 0x1
361#define IPV6_RTHDR_STRICT 0x1
362#define IPV6_SOCKOPT_RESERVED1 0x3
363#define SIOCGIFGENERIC 0xc020693a
364#define SIOCSIFGENERIC 0x80206939
365#define WALTSIG 0x4
366'
367
368includes_SunOS='
369#include <limits.h>
370#include <sys/types.h>
Scott Baker8461e152019-10-01 14:44:30 -0700371#include <sys/select.h>
khenaidooac637102019-01-14 15:44:34 -0500372#include <sys/socket.h>
373#include <sys/sockio.h>
374#include <sys/stat.h>
375#include <sys/mman.h>
376#include <sys/wait.h>
377#include <sys/ioctl.h>
378#include <sys/mkdev.h>
379#include <net/bpf.h>
380#include <net/if.h>
381#include <net/if_arp.h>
382#include <net/if_types.h>
383#include <net/route.h>
384#include <netinet/in.h>
385#include <termios.h>
386#include <netinet/ip.h>
387#include <netinet/ip_mroute.h>
388'
389
390
391includes='
392#include <sys/types.h>
393#include <sys/file.h>
394#include <fcntl.h>
395#include <dirent.h>
396#include <sys/socket.h>
397#include <netinet/in.h>
398#include <netinet/ip.h>
399#include <netinet/ip6.h>
400#include <netinet/tcp.h>
401#include <errno.h>
402#include <sys/signal.h>
403#include <signal.h>
404#include <sys/resource.h>
405#include <time.h>
406'
407ccflags="$@"
408
409# Write go tool cgo -godefs input.
410(
411 echo package unix
412 echo
413 echo '/*'
414 indirect="includes_$(uname)"
415 echo "${!indirect} $includes"
416 echo '*/'
417 echo 'import "C"'
418 echo 'import "syscall"'
419 echo
420 echo 'const ('
421
422 # The gcc command line prints all the #defines
423 # it encounters while processing the input
424 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
425 awk '
426 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
427
428 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
429 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
430 $2 ~ /^(SCM_SRCRT)$/ {next}
431 $2 ~ /^(MAP_FAILED)$/ {next}
432 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
433
434 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
435 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
436
437 $2 !~ /^ECCAPBITS/ &&
438 $2 !~ /^ETH_/ &&
439 $2 !~ /^EPROC_/ &&
440 $2 !~ /^EQUIV_/ &&
441 $2 !~ /^EXPR_/ &&
442 $2 ~ /^E[A-Z0-9_]+$/ ||
443 $2 ~ /^B[0-9_]+$/ ||
444 $2 ~ /^(OLD|NEW)DEV$/ ||
445 $2 == "BOTHER" ||
446 $2 ~ /^CI?BAUD(EX)?$/ ||
447 $2 == "IBSHIFT" ||
448 $2 ~ /^V[A-Z0-9]+$/ ||
449 $2 ~ /^CS[A-Z0-9]/ ||
450 $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
451 $2 ~ /^IGN/ ||
452 $2 ~ /^IX(ON|ANY|OFF)$/ ||
453 $2 ~ /^IN(LCR|PCK)$/ ||
454 $2 !~ "X86_CR3_PCID_NOFLUSH" &&
455 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
456 $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
457 $2 == "BRKINT" ||
458 $2 == "HUPCL" ||
459 $2 == "PENDIN" ||
460 $2 == "TOSTOP" ||
461 $2 == "XCASE" ||
462 $2 == "ALTWERASE" ||
463 $2 == "NOKERNINFO" ||
Scott Baker8461e152019-10-01 14:44:30 -0700464 $2 == "NFDBITS" ||
khenaidooac637102019-01-14 15:44:34 -0500465 $2 ~ /^PAR/ ||
466 $2 ~ /^SIG[^_]/ ||
467 $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
468 $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
469 $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
470 $2 ~ /^O?XTABS$/ ||
471 $2 ~ /^TC[IO](ON|OFF)$/ ||
472 $2 ~ /^IN_/ ||
473 $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
Scott Baker8461e152019-10-01 14:44:30 -0700474 $2 ~ /^LO_(KEY|NAME)_SIZE$/ ||
475 $2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ ||
476 $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|MCAST|EVFILT|NOTE|EV|SHUT|PROT|MAP|MFD|T?PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
khenaidooac637102019-01-14 15:44:34 -0500477 $2 ~ /^TP_STATUS_/ ||
478 $2 ~ /^FALLOC_/ ||
479 $2 == "ICMPV6_FILTER" ||
480 $2 == "SOMAXCONN" ||
481 $2 == "NAME_MAX" ||
482 $2 == "IFNAMSIZ" ||
483 $2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
484 $2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
485 $2 ~ /^HW_MACHINE$/ ||
486 $2 ~ /^SYSCTL_VERS/ ||
487 $2 !~ "MNT_BITS" &&
488 $2 ~ /^(MS|MNT|UMOUNT)_/ ||
Scott Baker8461e152019-10-01 14:44:30 -0700489 $2 ~ /^NS_GET_/ ||
khenaidooac637102019-01-14 15:44:34 -0500490 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
Andrea Campanella3614a922021-02-25 12:40:42 +0100491 $2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT|TFD)_/ ||
khenaidooac637102019-01-14 15:44:34 -0500492 $2 ~ /^KEXEC_/ ||
493 $2 ~ /^LINUX_REBOOT_CMD_/ ||
494 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
495 $2 ~ /^MODULE_INIT_/ ||
496 $2 !~ "NLA_TYPE_MASK" &&
Andrea Campanella3614a922021-02-25 12:40:42 +0100497 $2 !~ /^RTC_VL_(ACCURACY|BACKUP|DATA)/ &&
khenaidooac637102019-01-14 15:44:34 -0500498 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
499 $2 ~ /^SIOC/ ||
500 $2 ~ /^TIOC/ ||
501 $2 ~ /^TCGET/ ||
502 $2 ~ /^TCSET/ ||
503 $2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
504 $2 !~ "RTF_BITS" &&
Andrea Campanella3614a922021-02-25 12:40:42 +0100505 $2 ~ /^(IFF|IFT|NET_RT|RTM(GRP)?|RTF|RTV|RTA|RTAX)_/ ||
khenaidooac637102019-01-14 15:44:34 -0500506 $2 ~ /^BIOC/ ||
Andrea Campanella3614a922021-02-25 12:40:42 +0100507 $2 ~ /^DIOC/ ||
khenaidooac637102019-01-14 15:44:34 -0500508 $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
509 $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
510 $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
511 $2 ~ /^CLONE_[A-Z_]+/ ||
Scott Baker8461e152019-10-01 14:44:30 -0700512 $2 !~ /^(BPF_TIMEVAL|BPF_FIB_LOOKUP_[A-Z]+)$/ &&
khenaidooac637102019-01-14 15:44:34 -0500513 $2 ~ /^(BPF|DLT)_/ ||
514 $2 ~ /^(CLOCK|TIMER)_/ ||
515 $2 ~ /^CAN_/ ||
516 $2 ~ /^CAP_/ ||
Andrea Campanella3614a922021-02-25 12:40:42 +0100517 $2 ~ /^CP_/ ||
518 $2 ~ /^CPUSTATES$/ ||
khenaidooac637102019-01-14 15:44:34 -0500519 $2 ~ /^ALG_/ ||
Andrea Campanella3614a922021-02-25 12:40:42 +0100520 $2 ~ /^FI(CLONE|DEDUPERANGE)/ ||
521 $2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE)/ ||
522 $2 ~ /^FS_IOC_.*(ENCRYPTION|VERITY|[GS]ETFLAGS)/ ||
523 $2 ~ /^FS_VERITY_/ ||
524 $2 ~ /^FSCRYPT_/ ||
525 $2 ~ /^DM_/ ||
khenaidooac637102019-01-14 15:44:34 -0500526 $2 ~ /^GRND_/ ||
Stephane Barbarie260a5632019-02-26 16:12:49 -0500527 $2 ~ /^RND/ ||
khenaidooac637102019-01-14 15:44:34 -0500528 $2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
529 $2 ~ /^KEYCTL_/ ||
530 $2 ~ /^PERF_EVENT_IOC_/ ||
531 $2 ~ /^SECCOMP_MODE_/ ||
532 $2 ~ /^SPLICE_/ ||
533 $2 ~ /^SYNC_FILE_RANGE_/ ||
534 $2 !~ /^AUDIT_RECORD_MAGIC/ &&
535 $2 !~ /IOC_MAGIC/ &&
536 $2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
537 $2 ~ /^(VM|VMADDR)_/ ||
538 $2 ~ /^IOCTL_VM_SOCKETS_/ ||
539 $2 ~ /^(TASKSTATS|TS)_/ ||
540 $2 ~ /^CGROUPSTATS_/ ||
541 $2 ~ /^GENL_/ ||
542 $2 ~ /^STATX_/ ||
543 $2 ~ /^RENAME/ ||
544 $2 ~ /^UBI_IOC[A-Z]/ ||
545 $2 ~ /^UTIME_/ ||
546 $2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
547 $2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
548 $2 ~ /^FSOPT_/ ||
549 $2 ~ /^WDIOC_/ ||
550 $2 ~ /^NFN/ ||
551 $2 ~ /^XDP_/ ||
Andrea Campanella3614a922021-02-25 12:40:42 +0100552 $2 ~ /^RWF_/ ||
khenaidooac637102019-01-14 15:44:34 -0500553 $2 ~ /^(HDIO|WIN|SMART)_/ ||
Scott Baker8461e152019-10-01 14:44:30 -0700554 $2 ~ /^CRYPTO_/ ||
555 $2 ~ /^TIPC_/ ||
Andrea Campanella3614a922021-02-25 12:40:42 +0100556 $2 ~ /^DEVLINK_/ ||
khenaidooac637102019-01-14 15:44:34 -0500557 $2 !~ "WMESGLEN" &&
558 $2 ~ /^W[A-Z0-9]+$/ ||
559 $2 ~/^PPPIOC/ ||
William Kurkiandaa6bb22019-03-07 12:26:28 -0500560 $2 ~ /^FAN_|FANOTIFY_/ ||
khenaidooac637102019-01-14 15:44:34 -0500561 $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
562 $2 ~ /^__WCOREFLAG$/ {next}
563 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
564
565 {next}
566 ' | sort
567
568 echo ')'
569) >_const.go
570
571# Pull out the error names for later.
572errors=$(
573 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
574 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
575 sort
576)
577
578# Pull out the signal names for later.
579signals=$(
580 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
581 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
582 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
583 sort
584)
585
586# Again, writing regexps to a file.
587echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
588 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
589 sort >_error.grep
590echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
591 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
592 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
593 sort >_signal.grep
594
595echo '// mkerrors.sh' "$@"
596echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
597echo
598echo "// +build ${GOARCH},${GOOS}"
599echo
600go tool cgo -godefs -- "$@" _const.go >_error.out
601cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
602echo
603echo '// Errors'
604echo 'const ('
605cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
606echo ')'
607
608echo
609echo '// Signals'
610echo 'const ('
611cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
612echo ')'
613
614# Run C program to print error and syscall strings.
615(
616 echo -E "
617#include <stdio.h>
618#include <stdlib.h>
619#include <errno.h>
620#include <ctype.h>
621#include <string.h>
622#include <signal.h>
623
624#define nelem(x) (sizeof(x)/sizeof((x)[0]))
625
626enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
627
628struct tuple {
629 int num;
630 const char *name;
631};
632
633struct tuple errors[] = {
634"
635 for i in $errors
636 do
637 echo -E ' {'$i', "'$i'" },'
638 done
639
640 echo -E "
641};
642
643struct tuple signals[] = {
644"
645 for i in $signals
646 do
647 echo -E ' {'$i', "'$i'" },'
648 done
649
650 # Use -E because on some systems bash builtin interprets \n itself.
651 echo -E '
652};
653
654static int
655tuplecmp(const void *a, const void *b)
656{
657 return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
658}
659
660int
661main(void)
662{
663 int i, e;
664 char buf[1024], *p;
665
666 printf("\n\n// Error table\n");
667 printf("var errorList = [...]struct {\n");
668 printf("\tnum syscall.Errno\n");
669 printf("\tname string\n");
670 printf("\tdesc string\n");
671 printf("} {\n");
672 qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
673 for(i=0; i<nelem(errors); i++) {
674 e = errors[i].num;
675 if(i > 0 && errors[i-1].num == e)
676 continue;
677 strcpy(buf, strerror(e));
678 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
679 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
680 buf[0] += a - A;
681 printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
682 }
683 printf("}\n\n");
684
685 printf("\n\n// Signal table\n");
686 printf("var signalList = [...]struct {\n");
687 printf("\tnum syscall.Signal\n");
688 printf("\tname string\n");
689 printf("\tdesc string\n");
690 printf("} {\n");
691 qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
692 for(i=0; i<nelem(signals); i++) {
693 e = signals[i].num;
694 if(i > 0 && signals[i-1].num == e)
695 continue;
696 strcpy(buf, strsignal(e));
697 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
698 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
699 buf[0] += a - A;
700 // cut trailing : number.
701 p = strrchr(buf, ":"[0]);
702 if(p)
703 *p = '\0';
704 printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
705 }
706 printf("}\n\n");
707
708 return 0;
709}
710
711'
712) >_errors.c
713
714$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out