blob: 2c434f7ae756d68e235823980e786047ad1cf225 [file] [log] [blame]
David K. Bainbridge215e0242017-09-05 23:18:24 -07001#!/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" -eq "linux" ]] && [[ "$GOARCH" != "sparc64" ]]; then
21 if [[ "$GOLANG_SYS_BUILD" -ne "docker" ]]; then
22 echo 1>&2 "In the new build system, mkerrors should not be called directly."
23 echo 1>&2 "See README.md"
24 exit 1
25 fi
26fi
27
28CC=${CC:-cc}
29
30if [[ "$GOOS" -eq "solaris" ]]; then
31 # Assumes GNU versions of utilities in PATH.
32 export PATH=/usr/gnu/bin:$PATH
33fi
34
35uname=$(uname)
36
37includes_Darwin='
38#define _DARWIN_C_SOURCE
39#define KERNEL
40#define _DARWIN_USE_64_BIT_INODE
41#include <sys/types.h>
42#include <sys/event.h>
43#include <sys/ptrace.h>
44#include <sys/socket.h>
45#include <sys/sockio.h>
46#include <sys/sysctl.h>
47#include <sys/mman.h>
48#include <sys/mount.h>
49#include <sys/wait.h>
50#include <net/bpf.h>
51#include <net/if.h>
52#include <net/if_types.h>
53#include <net/route.h>
54#include <netinet/in.h>
55#include <netinet/ip.h>
56#include <termios.h>
57'
58
59includes_DragonFly='
60#include <sys/types.h>
61#include <sys/event.h>
62#include <sys/socket.h>
63#include <sys/sockio.h>
64#include <sys/sysctl.h>
65#include <sys/mman.h>
66#include <sys/wait.h>
67#include <sys/ioctl.h>
68#include <net/bpf.h>
69#include <net/if.h>
70#include <net/if_types.h>
71#include <net/route.h>
72#include <netinet/in.h>
73#include <termios.h>
74#include <netinet/ip.h>
75#include <net/ip_mroute/ip_mroute.h>
76'
77
78includes_FreeBSD='
79#include <sys/capability.h>
80#include <sys/param.h>
81#include <sys/types.h>
82#include <sys/event.h>
83#include <sys/socket.h>
84#include <sys/sockio.h>
85#include <sys/sysctl.h>
86#include <sys/mman.h>
87#include <sys/wait.h>
88#include <sys/ioctl.h>
89#include <net/bpf.h>
90#include <net/if.h>
91#include <net/if_types.h>
92#include <net/route.h>
93#include <netinet/in.h>
94#include <termios.h>
95#include <netinet/ip.h>
96#include <netinet/ip_mroute.h>
97#include <sys/extattr.h>
98
99#if __FreeBSD__ >= 10
100#define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
101#undef SIOCAIFADDR
102#define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
103#undef SIOCSIFPHYADDR
104#define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
105#endif
106'
107
108includes_Linux='
109#define _LARGEFILE_SOURCE
110#define _LARGEFILE64_SOURCE
111#ifndef __LP64__
112#define _FILE_OFFSET_BITS 64
113#endif
114#define _GNU_SOURCE
115
116// <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
117// these structures. We just include them copied from <bits/termios.h>.
118#if defined(__powerpc__)
119struct sgttyb {
120 char sg_ispeed;
121 char sg_ospeed;
122 char sg_erase;
123 char sg_kill;
124 short sg_flags;
125};
126
127struct tchars {
128 char t_intrc;
129 char t_quitc;
130 char t_startc;
131 char t_stopc;
132 char t_eofc;
133 char t_brkc;
134};
135
136struct ltchars {
137 char t_suspc;
138 char t_dsuspc;
139 char t_rprntc;
140 char t_flushc;
141 char t_werasc;
142 char t_lnextc;
143};
144#endif
145
146#include <bits/sockaddr.h>
147#include <sys/epoll.h>
148#include <sys/eventfd.h>
149#include <sys/inotify.h>
150#include <sys/ioctl.h>
151#include <sys/mman.h>
152#include <sys/mount.h>
153#include <sys/prctl.h>
154#include <sys/stat.h>
155#include <sys/types.h>
156#include <sys/time.h>
157#include <sys/socket.h>
158#include <sys/xattr.h>
159#include <linux/if.h>
160#include <linux/if_alg.h>
161#include <linux/if_arp.h>
162#include <linux/if_ether.h>
163#include <linux/if_tun.h>
164#include <linux/if_packet.h>
165#include <linux/if_addr.h>
166#include <linux/falloc.h>
167#include <linux/filter.h>
168#include <linux/fs.h>
169#include <linux/keyctl.h>
170#include <linux/netlink.h>
171#include <linux/perf_event.h>
172#include <linux/random.h>
173#include <linux/reboot.h>
174#include <linux/rtnetlink.h>
175#include <linux/ptrace.h>
176#include <linux/sched.h>
177#include <linux/seccomp.h>
178#include <linux/sockios.h>
179#include <linux/wait.h>
180#include <linux/icmpv6.h>
181#include <linux/serial.h>
182#include <linux/can.h>
183#include <linux/vm_sockets.h>
184#include <linux/taskstats.h>
185#include <linux/genetlink.h>
186#include <net/route.h>
187#include <asm/termbits.h>
188
189#ifndef MSG_FASTOPEN
190#define MSG_FASTOPEN 0x20000000
191#endif
192
193#ifndef PTRACE_GETREGS
194#define PTRACE_GETREGS 0xc
195#endif
196
197#ifndef PTRACE_SETREGS
198#define PTRACE_SETREGS 0xd
199#endif
200
201#ifndef SOL_NETLINK
202#define SOL_NETLINK 270
203#endif
204
205#ifdef SOL_BLUETOOTH
206// SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
207// but it is already in bluetooth_linux.go
208#undef SOL_BLUETOOTH
209#endif
210
211// Certain constants are missing from the fs/crypto UAPI
212#define FS_KEY_DESC_PREFIX "fscrypt:"
213#define FS_KEY_DESC_PREFIX_SIZE 8
214#define FS_MAX_KEY_SIZE 64
215'
216
217includes_NetBSD='
218#include <sys/types.h>
219#include <sys/param.h>
220#include <sys/event.h>
221#include <sys/mman.h>
222#include <sys/socket.h>
223#include <sys/sockio.h>
224#include <sys/sysctl.h>
225#include <sys/termios.h>
226#include <sys/ttycom.h>
227#include <sys/wait.h>
228#include <net/bpf.h>
229#include <net/if.h>
230#include <net/if_types.h>
231#include <net/route.h>
232#include <netinet/in.h>
233#include <netinet/in_systm.h>
234#include <netinet/ip.h>
235#include <netinet/ip_mroute.h>
236#include <netinet/if_ether.h>
237
238// Needed since <sys/param.h> refers to it...
239#define schedppq 1
240'
241
242includes_OpenBSD='
243#include <sys/types.h>
244#include <sys/param.h>
245#include <sys/event.h>
246#include <sys/mman.h>
247#include <sys/socket.h>
248#include <sys/sockio.h>
249#include <sys/sysctl.h>
250#include <sys/termios.h>
251#include <sys/ttycom.h>
252#include <sys/wait.h>
253#include <net/bpf.h>
254#include <net/if.h>
255#include <net/if_types.h>
256#include <net/if_var.h>
257#include <net/route.h>
258#include <netinet/in.h>
259#include <netinet/in_systm.h>
260#include <netinet/ip.h>
261#include <netinet/ip_mroute.h>
262#include <netinet/if_ether.h>
263#include <net/if_bridge.h>
264
265// We keep some constants not supported in OpenBSD 5.5 and beyond for
266// the promise of compatibility.
267#define EMUL_ENABLED 0x1
268#define EMUL_NATIVE 0x2
269#define IPV6_FAITH 0x1d
270#define IPV6_OPTIONS 0x1
271#define IPV6_RTHDR_STRICT 0x1
272#define IPV6_SOCKOPT_RESERVED1 0x3
273#define SIOCGIFGENERIC 0xc020693a
274#define SIOCSIFGENERIC 0x80206939
275#define WALTSIG 0x4
276'
277
278includes_SunOS='
279#include <limits.h>
280#include <sys/types.h>
281#include <sys/socket.h>
282#include <sys/sockio.h>
283#include <sys/mman.h>
284#include <sys/wait.h>
285#include <sys/ioctl.h>
286#include <net/bpf.h>
287#include <net/if.h>
288#include <net/if_arp.h>
289#include <net/if_types.h>
290#include <net/route.h>
291#include <netinet/in.h>
292#include <termios.h>
293#include <netinet/ip.h>
294#include <netinet/ip_mroute.h>
295'
296
297
298includes='
299#include <sys/types.h>
300#include <sys/file.h>
301#include <fcntl.h>
302#include <dirent.h>
303#include <sys/socket.h>
304#include <netinet/in.h>
305#include <netinet/ip.h>
306#include <netinet/ip6.h>
307#include <netinet/tcp.h>
308#include <errno.h>
309#include <sys/signal.h>
310#include <signal.h>
311#include <sys/resource.h>
312#include <time.h>
313'
314ccflags="$@"
315
316# Write go tool cgo -godefs input.
317(
318 echo package unix
319 echo
320 echo '/*'
321 indirect="includes_$(uname)"
322 echo "${!indirect} $includes"
323 echo '*/'
324 echo 'import "C"'
325 echo 'import "syscall"'
326 echo
327 echo 'const ('
328
329 # The gcc command line prints all the #defines
330 # it encounters while processing the input
331 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
332 awk '
333 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
334
335 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
336 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
337 $2 ~ /^(SCM_SRCRT)$/ {next}
338 $2 ~ /^(MAP_FAILED)$/ {next}
339 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
340
341 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
342 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
343
344 $2 !~ /^ETH_/ &&
345 $2 !~ /^EPROC_/ &&
346 $2 !~ /^EQUIV_/ &&
347 $2 !~ /^EXPR_/ &&
348 $2 ~ /^E[A-Z0-9_]+$/ ||
349 $2 ~ /^B[0-9_]+$/ ||
350 $2 == "BOTHER" ||
351 $2 ~ /^CI?BAUD(EX)?$/ ||
352 $2 == "IBSHIFT" ||
353 $2 ~ /^V[A-Z0-9]+$/ ||
354 $2 ~ /^CS[A-Z0-9]/ ||
355 $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
356 $2 ~ /^IGN/ ||
357 $2 ~ /^IX(ON|ANY|OFF)$/ ||
358 $2 ~ /^IN(LCR|PCK)$/ ||
359 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
360 $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
361 $2 == "BRKINT" ||
362 $2 == "HUPCL" ||
363 $2 == "PENDIN" ||
364 $2 == "TOSTOP" ||
365 $2 == "XCASE" ||
366 $2 == "ALTWERASE" ||
367 $2 == "NOKERNINFO" ||
368 $2 ~ /^PAR/ ||
369 $2 ~ /^SIG[^_]/ ||
370 $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
371 $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
372 $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
373 $2 ~ /^O?XTABS$/ ||
374 $2 ~ /^TC[IO](ON|OFF)$/ ||
375 $2 ~ /^IN_/ ||
376 $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
377 $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
378 $2 ~ /^FALLOC_/ ||
379 $2 == "ICMPV6_FILTER" ||
380 $2 == "SOMAXCONN" ||
381 $2 == "NAME_MAX" ||
382 $2 == "IFNAMSIZ" ||
383 $2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
384 $2 ~ /^SYSCTL_VERS/ ||
385 $2 ~ /^(MS|MNT|UMOUNT)_/ ||
386 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
387 $2 ~ /^(O|F|E?FD|NAME|S|PTRACE|PT)_/ ||
388 $2 ~ /^LINUX_REBOOT_CMD_/ ||
389 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
390 $2 !~ "NLA_TYPE_MASK" &&
391 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
392 $2 ~ /^SIOC/ ||
393 $2 ~ /^TIOC/ ||
394 $2 ~ /^TCGET/ ||
395 $2 ~ /^TCSET/ ||
396 $2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
397 $2 !~ "RTF_BITS" &&
398 $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
399 $2 ~ /^BIOC/ ||
400 $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
401 $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
402 $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
403 $2 ~ /^CLONE_[A-Z_]+/ ||
404 $2 !~ /^(BPF_TIMEVAL)$/ &&
405 $2 ~ /^(BPF|DLT)_/ ||
406 $2 ~ /^CLOCK_/ ||
407 $2 ~ /^CAN_/ ||
408 $2 ~ /^CAP_/ ||
409 $2 ~ /^ALG_/ ||
410 $2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE|IOC_(GET|SET)_ENCRYPTION)/ ||
411 $2 ~ /^GRND_/ ||
412 $2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
413 $2 ~ /^KEYCTL_/ ||
414 $2 ~ /^PERF_EVENT_IOC_/ ||
415 $2 ~ /^SECCOMP_MODE_/ ||
416 $2 ~ /^SPLICE_/ ||
417 $2 ~ /^(VM|VMADDR)_/ ||
418 $2 ~ /^(TASKSTATS|TS)_/ ||
419 $2 ~ /^GENL_/ ||
420 $2 ~ /^XATTR_(CREATE|REPLACE)/ ||
421 $2 !~ "WMESGLEN" &&
422 $2 ~ /^W[A-Z0-9]+$/ ||
423 $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
424 $2 ~ /^__WCOREFLAG$/ {next}
425 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
426
427 {next}
428 ' | sort
429
430 echo ')'
431) >_const.go
432
433# Pull out the error names for later.
434errors=$(
435 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
436 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
437 sort
438)
439
440# Pull out the signal names for later.
441signals=$(
442 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
443 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
444 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
445 sort
446)
447
448# Again, writing regexps to a file.
449echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
450 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
451 sort >_error.grep
452echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
453 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
454 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
455 sort >_signal.grep
456
457echo '// mkerrors.sh' "$@"
458echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
459echo
460echo "// +build ${GOARCH},${GOOS}"
461echo
462go tool cgo -godefs -- "$@" _const.go >_error.out
463cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
464echo
465echo '// Errors'
466echo 'const ('
467cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
468echo ')'
469
470echo
471echo '// Signals'
472echo 'const ('
473cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
474echo ')'
475
476# Run C program to print error and syscall strings.
477(
478 echo -E "
479#include <stdio.h>
480#include <stdlib.h>
481#include <errno.h>
482#include <ctype.h>
483#include <string.h>
484#include <signal.h>
485
486#define nelem(x) (sizeof(x)/sizeof((x)[0]))
487
488enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
489
490int errors[] = {
491"
492 for i in $errors
493 do
494 echo -E ' '$i,
495 done
496
497 echo -E "
498};
499
500int signals[] = {
501"
502 for i in $signals
503 do
504 echo -E ' '$i,
505 done
506
507 # Use -E because on some systems bash builtin interprets \n itself.
508 echo -E '
509};
510
511static int
512intcmp(const void *a, const void *b)
513{
514 return *(int*)a - *(int*)b;
515}
516
517int
518main(void)
519{
520 int i, e;
521 char buf[1024], *p;
522
523 printf("\n\n// Error table\n");
524 printf("var errors = [...]string {\n");
525 qsort(errors, nelem(errors), sizeof errors[0], intcmp);
526 for(i=0; i<nelem(errors); i++) {
527 e = errors[i];
528 if(i > 0 && errors[i-1] == e)
529 continue;
530 strcpy(buf, strerror(e));
531 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
532 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
533 buf[0] += a - A;
534 printf("\t%d: \"%s\",\n", e, buf);
535 }
536 printf("}\n\n");
537
538 printf("\n\n// Signal table\n");
539 printf("var signals = [...]string {\n");
540 qsort(signals, nelem(signals), sizeof signals[0], intcmp);
541 for(i=0; i<nelem(signals); i++) {
542 e = signals[i];
543 if(i > 0 && signals[i-1] == e)
544 continue;
545 strcpy(buf, strsignal(e));
546 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
547 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
548 buf[0] += a - A;
549 // cut trailing : number.
550 p = strrchr(buf, ":"[0]);
551 if(p)
552 *p = '\0';
553 printf("\t%d: \"%s\",\n", e, buf);
554 }
555 printf("}\n\n");
556
557 return 0;
558}
559
560'
561) >_errors.c
562
563$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out