blob: 007358af8fc18789e1384d52c1657a10eba5c089 [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -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>
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
58#define _DARWIN_USE_64_BIT_INODE
59#define __APPLE_USE_RFC_3542
60#include <stdint.h>
61#include <sys/attr.h>
62#include <sys/clonefile.h>
63#include <sys/kern_control.h>
64#include <sys/types.h>
65#include <sys/event.h>
66#include <sys/ptrace.h>
67#include <sys/select.h>
68#include <sys/socket.h>
69#include <sys/un.h>
70#include <sys/sockio.h>
71#include <sys/sys_domain.h>
72#include <sys/sysctl.h>
73#include <sys/mman.h>
74#include <sys/mount.h>
75#include <sys/utsname.h>
76#include <sys/wait.h>
77#include <sys/xattr.h>
78#include <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>
90#include <sys/select.h>
91#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>
101#include <net/if_clone.h>
102#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>
114#include <sys/disk.h>
115#include <sys/event.h>
116#include <sys/sched.h>
117#include <sys/select.h>
118#include <sys/socket.h>
119#include <sys/un.h>
120#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>
195#include <sys/select.h>
196#include <sys/signalfd.h>
197#include <sys/socket.h>
198#include <sys/timerfd.h>
199#include <sys/uio.h>
200#include <sys/xattr.h>
201#include <linux/bpf.h>
202#include <linux/can.h>
203#include <linux/can/error.h>
204#include <linux/can/raw.h>
205#include <linux/capability.h>
206#include <linux/cryptouser.h>
207#include <linux/devlink.h>
208#include <linux/dm-ioctl.h>
209#include <linux/errqueue.h>
210#include <linux/ethtool_netlink.h>
211#include <linux/falloc.h>
212#include <linux/fanotify.h>
213#include <linux/filter.h>
214#include <linux/fs.h>
215#include <linux/fscrypt.h>
216#include <linux/fsverity.h>
217#include <linux/genetlink.h>
218#include <linux/hdreg.h>
219#include <linux/hidraw.h>
220#include <linux/icmp.h>
221#include <linux/icmpv6.h>
222#include <linux/if.h>
223#include <linux/if_addr.h>
224#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>
230#include <linux/if_xdp.h>
231#include <linux/input.h>
232#include <linux/kexec.h>
233#include <linux/keyctl.h>
234#include <linux/loop.h>
235#include <linux/lwtunnel.h>
236#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>
242#include <linux/nsfs.h>
243#include <linux/perf_event.h>
244#include <linux/pps.h>
245#include <linux/ptrace.h>
246#include <linux/random.h>
247#include <linux/reboot.h>
248#include <linux/rtc.h>
249#include <linux/rtnetlink.h>
250#include <linux/sched.h>
251#include <linux/seccomp.h>
252#include <linux/serial.h>
253#include <linux/sockios.h>
254#include <linux/taskstats.h>
255#include <linux/tipc.h>
256#include <linux/vm_sockets.h>
257#include <linux/wait.h>
258#include <linux/watchdog.h>
259
260#include <mtd/ubi-user.h>
261#include <net/route.h>
262
263#if defined(__sparc__)
264// On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
265// definition in glibc. As only the error constants are needed here, include the
266// generic termibits.h (which is included by termbits.h on sparc).
267#include <asm-generic/termbits.h>
268#else
269#include <asm/termbits.h>
270#endif
271
272#ifndef MSG_FASTOPEN
273#define MSG_FASTOPEN 0x20000000
274#endif
275
276#ifndef PTRACE_GETREGS
277#define PTRACE_GETREGS 0xc
278#endif
279
280#ifndef PTRACE_SETREGS
281#define PTRACE_SETREGS 0xd
282#endif
283
284#ifndef SOL_NETLINK
285#define SOL_NETLINK 270
286#endif
287
288#ifdef SOL_BLUETOOTH
289// SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
290// but it is already in bluetooth_linux.go
291#undef SOL_BLUETOOTH
292#endif
293
294// Certain constants are missing from the fs/crypto UAPI
295#define FS_KEY_DESC_PREFIX "fscrypt:"
296#define FS_KEY_DESC_PREFIX_SIZE 8
297#define FS_MAX_KEY_SIZE 64
298
299// The code generator produces -0x1 for (~0), but an unsigned value is necessary
300// for the tipc_subscr timeout __u32 field.
301#undef TIPC_WAIT_FOREVER
302#define TIPC_WAIT_FOREVER 0xffffffff
303
304// Copied from linux/l2tp.h
305// Including linux/l2tp.h here causes conflicts between linux/in.h
306// and netinet/in.h included via net/route.h above.
307#define IPPROTO_L2TP 115
308
309// Copied from linux/hid.h.
310// Keep in sync with the size of the referenced fields.
311#define _HIDIOCGRAWNAME_LEN 128 // sizeof_field(struct hid_device, name)
312#define _HIDIOCGRAWPHYS_LEN 64 // sizeof_field(struct hid_device, phys)
313#define _HIDIOCGRAWUNIQ_LEN 64 // sizeof_field(struct hid_device, uniq)
314
315#define _HIDIOCGRAWNAME HIDIOCGRAWNAME(_HIDIOCGRAWNAME_LEN)
316#define _HIDIOCGRAWPHYS HIDIOCGRAWPHYS(_HIDIOCGRAWPHYS_LEN)
317#define _HIDIOCGRAWUNIQ HIDIOCGRAWUNIQ(_HIDIOCGRAWUNIQ_LEN)
318
319'
320
321includes_NetBSD='
322#include <sys/types.h>
323#include <sys/param.h>
324#include <sys/event.h>
325#include <sys/extattr.h>
326#include <sys/mman.h>
327#include <sys/mount.h>
328#include <sys/sched.h>
329#include <sys/select.h>
330#include <sys/socket.h>
331#include <sys/sockio.h>
332#include <sys/sysctl.h>
333#include <sys/termios.h>
334#include <sys/ttycom.h>
335#include <sys/wait.h>
336#include <net/bpf.h>
337#include <net/if.h>
338#include <net/if_types.h>
339#include <net/route.h>
340#include <netinet/in.h>
341#include <netinet/in_systm.h>
342#include <netinet/ip.h>
343#include <netinet/ip_mroute.h>
344#include <netinet/if_ether.h>
345
346// Needed since <sys/param.h> refers to it...
347#define schedppq 1
348'
349
350includes_OpenBSD='
351#include <sys/types.h>
352#include <sys/param.h>
353#include <sys/event.h>
354#include <sys/mman.h>
355#include <sys/mount.h>
356#include <sys/select.h>
357#include <sys/sched.h>
358#include <sys/socket.h>
359#include <sys/sockio.h>
360#include <sys/stat.h>
361#include <sys/sysctl.h>
362#include <sys/termios.h>
363#include <sys/ttycom.h>
364#include <sys/unistd.h>
365#include <sys/wait.h>
366#include <net/bpf.h>
367#include <net/if.h>
368#include <net/if_types.h>
369#include <net/if_var.h>
370#include <net/route.h>
371#include <netinet/in.h>
372#include <netinet/in_systm.h>
373#include <netinet/ip.h>
374#include <netinet/ip_mroute.h>
375#include <netinet/if_ether.h>
376#include <net/if_bridge.h>
377
378// We keep some constants not supported in OpenBSD 5.5 and beyond for
379// the promise of compatibility.
380#define EMUL_ENABLED 0x1
381#define EMUL_NATIVE 0x2
382#define IPV6_FAITH 0x1d
383#define IPV6_OPTIONS 0x1
384#define IPV6_RTHDR_STRICT 0x1
385#define IPV6_SOCKOPT_RESERVED1 0x3
386#define SIOCGIFGENERIC 0xc020693a
387#define SIOCSIFGENERIC 0x80206939
388#define WALTSIG 0x4
389'
390
391includes_SunOS='
392#include <limits.h>
393#include <sys/types.h>
394#include <sys/select.h>
395#include <sys/socket.h>
396#include <sys/sockio.h>
397#include <sys/stat.h>
398#include <sys/stream.h>
399#include <sys/mman.h>
400#include <sys/wait.h>
401#include <sys/ioctl.h>
402#include <sys/mkdev.h>
403#include <net/bpf.h>
404#include <net/if.h>
405#include <net/if_arp.h>
406#include <net/if_types.h>
407#include <net/route.h>
408#include <netinet/icmp6.h>
409#include <netinet/in.h>
410#include <netinet/ip.h>
411#include <netinet/ip_mroute.h>
412#include <termios.h>
413'
414
415
416includes='
417#include <sys/types.h>
418#include <sys/file.h>
419#include <fcntl.h>
420#include <dirent.h>
421#include <sys/socket.h>
422#include <netinet/in.h>
423#include <netinet/ip.h>
424#include <netinet/ip6.h>
425#include <netinet/tcp.h>
426#include <errno.h>
427#include <sys/signal.h>
428#include <signal.h>
429#include <sys/resource.h>
430#include <time.h>
431'
432ccflags="$@"
433
434# Write go tool cgo -godefs input.
435(
436 echo package unix
437 echo
438 echo '/*'
439 indirect="includes_$(uname)"
440 echo "${!indirect} $includes"
441 echo '*/'
442 echo 'import "C"'
443 echo 'import "syscall"'
444 echo
445 echo 'const ('
446
447 # The gcc command line prints all the #defines
448 # it encounters while processing the input
449 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
450 awk '
451 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
452
453 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
454 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
455 $2 ~ /^(SCM_SRCRT)$/ {next}
456 $2 ~ /^(MAP_FAILED)$/ {next}
457 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
458
459 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
460 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
461
462 $2 !~ /^ECCAPBITS/ &&
463 $2 !~ /^ETH_/ &&
464 $2 !~ /^EPROC_/ &&
465 $2 !~ /^EQUIV_/ &&
466 $2 !~ /^EXPR_/ &&
467 $2 !~ /^EVIOC/ &&
468 $2 !~ /^EV_/ &&
469 $2 ~ /^E[A-Z0-9_]+$/ ||
470 $2 ~ /^B[0-9_]+$/ ||
471 $2 ~ /^(OLD|NEW)DEV$/ ||
472 $2 == "BOTHER" ||
473 $2 ~ /^CI?BAUD(EX)?$/ ||
474 $2 == "IBSHIFT" ||
475 $2 ~ /^V[A-Z0-9]+$/ ||
476 $2 ~ /^CS[A-Z0-9]/ ||
477 $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
478 $2 ~ /^IGN/ ||
479 $2 ~ /^IX(ON|ANY|OFF)$/ ||
480 $2 ~ /^IN(LCR|PCK)$/ ||
481 $2 !~ "X86_CR3_PCID_NOFLUSH" &&
482 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
483 $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
484 $2 == "BRKINT" ||
485 $2 == "HUPCL" ||
486 $2 == "PENDIN" ||
487 $2 == "TOSTOP" ||
488 $2 == "XCASE" ||
489 $2 == "ALTWERASE" ||
490 $2 == "NOKERNINFO" ||
491 $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ANOTIFY_/ ||
593 $2 == "HID_MAX_DESCRIPTOR_SIZE" ||
594 $2 ~ /^_?HIDIOC/ ||
595 $2 ~ /^BUS_(USB|HIL|BLUETOOTH|VIRTUAL)$/ ||
596 $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
597 $2 ~ /^__WCOREFLAG$/ {next}
598 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
599
600 {next}
601 ' | sort
602
603 echo ')'
604) >_const.go
605
606# Pull out the error names for later.
607errors=$(
608 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
609 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
610 sort
611)
612
613# Pull out the signal names for later.
614signals=$(
615 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
616 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
617 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
618 sort
619)
620
621# Again, writing regexps to a file.
622echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
623 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
624 sort >_error.grep
625echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
626 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
627 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
628 sort >_signal.grep
629
630echo '// mkerrors.sh' "$@"
631echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
632echo
633echo "//go:build ${GOARCH} && ${GOOS}"
634echo "// +build ${GOARCH},${GOOS}"
635echo
636go tool cgo -godefs -- "$@" _const.go >_error.out
637cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
638echo
639echo '// Errors'
640echo 'const ('
641cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
642echo ')'
643
644echo
645echo '// Signals'
646echo 'const ('
647cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
648echo ')'
649
650# Run C program to print error and syscall strings.
651(
652 echo -E "
653#include <stdio.h>
654#include <stdlib.h>
655#include <errno.h>
656#include <ctype.h>
657#include <string.h>
658#include <signal.h>
659
660#define nelem(x) (sizeof(x)/sizeof((x)[0]))
661
662enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
663
664struct tuple {
665 int num;
666 const char *name;
667};
668
669struct tuple errors[] = {
670"
671 for i in $errors
672 do
673 echo -E ' {'$i', "'$i'" },'
674 done
675
676 echo -E "
677};
678
679struct tuple signals[] = {
680"
681 for i in $signals
682 do
683 echo -E ' {'$i', "'$i'" },'
684 done
685
686 # Use -E because on some systems bash builtin interprets \n itself.
687 echo -E '
688};
689
690static int
691tuplecmp(const void *a, const void *b)
692{
693 return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
694}
695
696int
697main(void)
698{
699 int i, e;
700 char buf[1024], *p;
701
702 printf("\n\n// Error table\n");
703 printf("var errorList = [...]struct {\n");
704 printf("\tnum syscall.Errno\n");
705 printf("\tname string\n");
706 printf("\tdesc string\n");
707 printf("} {\n");
708 qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
709 for(i=0; i<nelem(errors); i++) {
710 e = errors[i].num;
711 if(i > 0 && errors[i-1].num == e)
712 continue;
713 strcpy(buf, strerror(e));
714 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
715 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
716 buf[0] += a - A;
717 printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
718 }
719 printf("}\n\n");
720
721 printf("\n\n// Signal table\n");
722 printf("var signalList = [...]struct {\n");
723 printf("\tnum syscall.Signal\n");
724 printf("\tname string\n");
725 printf("\tdesc string\n");
726 printf("} {\n");
727 qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
728 for(i=0; i<nelem(signals); i++) {
729 e = signals[i].num;
730 if(i > 0 && signals[i-1].num == e)
731 continue;
732 strcpy(buf, strsignal(e));
733 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
734 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
735 buf[0] += a - A;
736 // cut trailing : number.
737 p = strrchr(buf, ":"[0]);
738 if(p)
739 *p = '\0';
740 printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
741 }
742 printf("}\n\n");
743
744 return 0;
745}
746
747'
748) >_errors.c
749
750$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out