mkerrors.sh

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