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.
6 # Generate Go code listing errors and other #defined constant
7 # values (ENAMETOOLONG etc.), by asking the preprocessor
8 # about the definitions.
14 if test -z "$GOARCH" -o -z "$GOOS"; then
15 echo 1>&2 "GOARCH or GOOS not defined in environment"
19 # Check that we are using the new build system if we should
20 if [[ "$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"
26 if [[ "$GOOS" = "aix" ]]; then
32 if [[ "$GOOS" = "solaris" ]]; then
33 # Assumes GNU versions of utilities in PATH.
34 export PATH=/usr/gnu/bin:$PATH
41 #include <net/netopt.h>
42 #include <netinet/ip_mroute.h>
43 #include <sys/protosw.h>
44 #include <sys/stropts.h>
47 #include <sys/termio.h>
51 #define AF_LOCAL AF_UNIX
55 #define _DARWIN_C_SOURCE
57 #define _DARWIN_USE_64_BIT_INODE
60 #include <sys/types.h>
61 #include <sys/event.h>
62 #include <sys/ptrace.h>
63 #include <sys/socket.h>
64 #include <sys/sockio.h>
65 #include <sys/sysctl.h>
67 #include <sys/mount.h>
68 #include <sys/utsname.h>
70 #include <sys/xattr.h>
73 #include <net/if_types.h>
74 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/ip.h>
81 #include <sys/types.h>
82 #include <sys/event.h>
83 #include <sys/socket.h>
84 #include <sys/sockio.h>
86 #include <sys/sysctl.h>
88 #include <sys/mount.h>
90 #include <sys/ioctl.h>
93 #include <net/if_types.h>
94 #include <net/route.h>
95 #include <netinet/in.h>
97 #include <netinet/ip.h>
98 #include <net/ip_mroute/ip_mroute.h>
102 #include <sys/capsicum.h>
103 #include <sys/param.h>
104 #include <sys/types.h>
105 #include <sys/event.h>
106 #include <sys/socket.h>
107 #include <sys/sockio.h>
108 #include <sys/stat.h>
109 #include <sys/sysctl.h>
110 #include <sys/mman.h>
111 #include <sys/mount.h>
112 #include <sys/wait.h>
113 #include <sys/ioctl.h>
116 #include <net/if_types.h>
117 #include <net/route.h>
118 #include <netinet/in.h>
120 #include <netinet/ip.h>
121 #include <netinet/ip_mroute.h>
122 #include <sys/extattr.h>
124 #if __FreeBSD__ >= 10
125 #define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
127 #define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
128 #undef SIOCSIFPHYADDR
129 #define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
134 #define _LARGEFILE_SOURCE
135 #define _LARGEFILE64_SOURCE
137 #define _FILE_OFFSET_BITS 64
141 // <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
142 // these structures. We just include them copied from <bits/termios.h>.
143 #if defined(__powerpc__)
171 #include <bits/sockaddr.h>
172 #include <sys/epoll.h>
173 #include <sys/eventfd.h>
174 #include <sys/inotify.h>
175 #include <sys/ioctl.h>
176 #include <sys/mman.h>
177 #include <sys/mount.h>
178 #include <sys/prctl.h>
179 #include <sys/stat.h>
180 #include <sys/types.h>
181 #include <sys/time.h>
182 #include <sys/signalfd.h>
183 #include <sys/socket.h>
184 #include <sys/xattr.h>
185 #include <linux/errqueue.h>
186 #include <linux/if.h>
187 #include <linux/if_alg.h>
188 #include <linux/if_arp.h>
189 #include <linux/if_ether.h>
190 #include <linux/if_ppp.h>
191 #include <linux/if_tun.h>
192 #include <linux/if_packet.h>
193 #include <linux/if_addr.h>
194 #include <linux/falloc.h>
195 #include <linux/fanotify.h>
196 #include <linux/filter.h>
197 #include <linux/fs.h>
198 #include <linux/kexec.h>
199 #include <linux/keyctl.h>
200 #include <linux/magic.h>
201 #include <linux/memfd.h>
202 #include <linux/module.h>
203 #include <linux/netfilter/nfnetlink.h>
204 #include <linux/netlink.h>
205 #include <linux/net_namespace.h>
206 #include <linux/perf_event.h>
207 #include <linux/random.h>
208 #include <linux/reboot.h>
209 #include <linux/rtnetlink.h>
210 #include <linux/ptrace.h>
211 #include <linux/sched.h>
212 #include <linux/seccomp.h>
213 #include <linux/sockios.h>
214 #include <linux/wait.h>
215 #include <linux/icmpv6.h>
216 #include <linux/serial.h>
217 #include <linux/can.h>
218 #include <linux/vm_sockets.h>
219 #include <linux/taskstats.h>
220 #include <linux/genetlink.h>
221 #include <linux/watchdog.h>
222 #include <linux/hdreg.h>
223 #include <linux/rtc.h>
224 #include <linux/if_xdp.h>
225 #include <mtd/ubi-user.h>
226 #include <net/route.h>
228 #if defined(__sparc__)
229 // On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
230 // definition in glibc. As only the error constants are needed here, include the
231 // generic termibits.h (which is included by termbits.h on sparc).
232 #include <asm-generic/termbits.h>
234 #include <asm/termbits.h>
238 #define MSG_FASTOPEN 0x20000000
241 #ifndef PTRACE_GETREGS
242 #define PTRACE_GETREGS 0xc
245 #ifndef PTRACE_SETREGS
246 #define PTRACE_SETREGS 0xd
250 #define SOL_NETLINK 270
254 // SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
255 // but it is already in bluetooth_linux.go
259 // Certain constants are missing from the fs/crypto UAPI
260 #define FS_KEY_DESC_PREFIX "fscrypt:"
261 #define FS_KEY_DESC_PREFIX_SIZE 8
262 #define FS_MAX_KEY_SIZE 64
266 #include <sys/types.h>
267 #include <sys/param.h>
268 #include <sys/event.h>
269 #include <sys/extattr.h>
270 #include <sys/mman.h>
271 #include <sys/mount.h>
272 #include <sys/socket.h>
273 #include <sys/sockio.h>
274 #include <sys/sysctl.h>
275 #include <sys/termios.h>
276 #include <sys/ttycom.h>
277 #include <sys/wait.h>
280 #include <net/if_types.h>
281 #include <net/route.h>
282 #include <netinet/in.h>
283 #include <netinet/in_systm.h>
284 #include <netinet/ip.h>
285 #include <netinet/ip_mroute.h>
286 #include <netinet/if_ether.h>
288 // Needed since <sys/param.h> refers to it...
293 #include <sys/types.h>
294 #include <sys/param.h>
295 #include <sys/event.h>
296 #include <sys/mman.h>
297 #include <sys/mount.h>
298 #include <sys/socket.h>
299 #include <sys/sockio.h>
300 #include <sys/stat.h>
301 #include <sys/sysctl.h>
302 #include <sys/termios.h>
303 #include <sys/ttycom.h>
304 #include <sys/unistd.h>
305 #include <sys/wait.h>
308 #include <net/if_types.h>
309 #include <net/if_var.h>
310 #include <net/route.h>
311 #include <netinet/in.h>
312 #include <netinet/in_systm.h>
313 #include <netinet/ip.h>
314 #include <netinet/ip_mroute.h>
315 #include <netinet/if_ether.h>
316 #include <net/if_bridge.h>
318 // We keep some constants not supported in OpenBSD 5.5 and beyond for
319 // the promise of compatibility.
320 #define EMUL_ENABLED 0x1
321 #define EMUL_NATIVE 0x2
322 #define IPV6_FAITH 0x1d
323 #define IPV6_OPTIONS 0x1
324 #define IPV6_RTHDR_STRICT 0x1
325 #define IPV6_SOCKOPT_RESERVED1 0x3
326 #define SIOCGIFGENERIC 0xc020693a
327 #define SIOCSIFGENERIC 0x80206939
333 #include <sys/types.h>
334 #include <sys/socket.h>
335 #include <sys/sockio.h>
336 #include <sys/stat.h>
337 #include <sys/mman.h>
338 #include <sys/wait.h>
339 #include <sys/ioctl.h>
340 #include <sys/mkdev.h>
343 #include <net/if_arp.h>
344 #include <net/if_types.h>
345 #include <net/route.h>
346 #include <netinet/in.h>
348 #include <netinet/ip.h>
349 #include <netinet/ip_mroute.h>
354 #include <sys/types.h>
355 #include <sys/file.h>
358 #include <sys/socket.h>
359 #include <netinet/in.h>
360 #include <netinet/ip.h>
361 #include <netinet/ip6.h>
362 #include <netinet/tcp.h>
364 #include <sys/signal.h>
366 #include <sys/resource.h>
371 # Write go tool cgo -godefs input.
376 indirect="includes_$(uname)"
377 echo "${!indirect} $includes"
380 echo 'import "syscall"'
384 # The gcc command line prints all the #defines
385 # it encounters while processing the input
386 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
388 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
390 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
391 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
392 $2 ~ /^(SCM_SRCRT)$/ {next}
393 $2 ~ /^(MAP_FAILED)$/ {next}
394 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
396 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
397 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
399 $2 !~ /^ECCAPBITS/ &&
404 $2 ~ /^E[A-Z0-9_]+$/ ||
406 $2 ~ /^(OLD|NEW)DEV$/ ||
408 $2 ~ /^CI?BAUD(EX)?$/ ||
410 $2 ~ /^V[A-Z0-9]+$/ ||
411 $2 ~ /^CS[A-Z0-9]/ ||
412 $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
414 $2 ~ /^IX(ON|ANY|OFF)$/ ||
415 $2 ~ /^IN(LCR|PCK)$/ ||
416 $2 !~ "X86_CR3_PCID_NOFLUSH" &&
417 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
418 $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
425 $2 == "NOKERNINFO" ||
428 $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
429 $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
430 $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
432 $2 ~ /^TC[IO](ON|OFF)$/ ||
434 $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
435 $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|MFD|T?PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
436 $2 ~ /^TP_STATUS_/ ||
438 $2 == "ICMPV6_FILTER" ||
442 $2 ~ /^CTL_(HW|KERN|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YNC_FILE_RANGE_/ ||
483 $2 !~ /^AUDIT_RECORD_MAGIC/ &&
485 $2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
486 $2 ~ /^(VM|VMADDR)_/ ||
487 $2 ~ /^IOCTL_VM_SOCKETS_/ ||
488 $2 ~ /^(TASKSTATS|TS)_/ ||
489 $2 ~ /^CGROUPSTATS_/ ||
493 $2 ~ /^UBI_IOC[A-Z]/ ||
495 $2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
496 $2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
501 $2 ~ /^(HDIO|WIN|SMART)_/ ||
503 $2 ~ /^W[A-Z0-9]+$/ ||
505 $2 ~ /^FAN_|FANOTIFY_/ ||
506 $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
507 $2 ~ /^__WCOREFLAG$/ {next}
508 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
516 # Pull out the error names for later.
518 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
519 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
523 # Pull out the signal names for later.
525 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
526 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
527 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
531 # Again, writing regexps to a file.
532 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
533 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
535 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
536 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
537 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
540 echo '// mkerrors.sh' "$@"
541 echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
543 echo "// +build ${GOARCH},${GOOS}"
545 go tool cgo -godefs -- "$@" _const.go >_error.out
546 cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
550 cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
556 cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
559 # Run C program to print error and syscall strings.
569 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
571 enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
578 struct tuple errors[] = {
582 echo -E ' {'$i', "'$i'" },'
588 struct tuple signals[] = {
592 echo -E ' {'$i', "'$i'" },'
595 # Use -E because on some systems bash builtin interprets \n itself.
600 tuplecmp(const void *a, const void *b)
602 return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
611 printf("\n\n// Error table\n");
612 printf("var errorList = [...]struct {\n");
613 printf("\tnum syscall.Errno\n");
614 printf("\tname string\n");
615 printf("\tdesc string\n");
617 qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
618 for(i=0; i<nelem(errors); i++) {
620 if(i > 0 && errors[i-1].num == e)
622 strcpy(buf, strerror(e));
623 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
624 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
626 printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
630 printf("\n\n// Signal table\n");
631 printf("var signalList = [...]struct {\n");
632 printf("\tnum syscall.Signal\n");
633 printf("\tname string\n");
634 printf("\tdesc string\n");
636 qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
637 for(i=0; i<nelem(signals); i++) {
639 if(i > 0 && signals[i-1].num == e)
641 strcpy(buf, strsignal(e));
642 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
643 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
645 // cut trailing : number.
646 p = strrchr(buf, ":"[0]);
649 printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
659 $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out