Home | History | Annotate | Download | only in syscall
      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 
     10 unset LANG
     11 export LC_ALL=C
     12 export LC_CTYPE=C
     13 
     14 CC=${CC:-gcc}
     15 
     16 uname=$(uname)
     17 
     18 includes_Darwin='
     19 #define _DARWIN_C_SOURCE
     20 #define KERNEL
     21 #define _DARWIN_USE_64_BIT_INODE
     22 #include <sys/types.h>
     23 #include <sys/event.h>
     24 #include <sys/ptrace.h>
     25 #include <sys/socket.h>
     26 #include <sys/sockio.h>
     27 #include <sys/sysctl.h>
     28 #include <sys/mman.h>
     29 #include <sys/wait.h>
     30 #include <net/bpf.h>
     31 #include <net/if.h>
     32 #include <net/if_types.h>
     33 #include <net/route.h>
     34 #include <netinet/in.h>
     35 #include <netinet/ip.h>
     36 #include <netinet/ip_mroute.h>
     37 #include <termios.h>
     38 '
     39 
     40 includes_DragonFly='
     41 #include <sys/types.h>
     42 #include <sys/event.h>
     43 #include <sys/socket.h>
     44 #include <sys/sockio.h>
     45 #include <sys/sysctl.h>
     46 #include <sys/mman.h>
     47 #include <sys/wait.h>
     48 #include <sys/ioctl.h>
     49 #include <net/bpf.h>
     50 #include <net/if.h>
     51 #include <net/if_types.h>
     52 #include <net/route.h>
     53 #include <netinet/in.h>
     54 #include <termios.h>
     55 #include <netinet/ip.h>
     56 #include <net/ip_mroute/ip_mroute.h>
     57 '
     58 
     59 includes_FreeBSD='
     60 #include <sys/param.h>
     61 #include <sys/types.h>
     62 #include <sys/event.h>
     63 #include <sys/socket.h>
     64 #include <sys/sockio.h>
     65 #include <sys/sysctl.h>
     66 #include <sys/mman.h>
     67 #include <sys/wait.h>
     68 #include <sys/ioctl.h>
     69 #include <net/bpf.h>
     70 #include <net/if.h>
     71 #include <net/if_types.h>
     72 #include <net/route.h>
     73 #include <netinet/in.h>
     74 #include <termios.h>
     75 #include <netinet/ip.h>
     76 #include <netinet/ip_mroute.h>
     77 
     78 #if __FreeBSD__ >= 10
     79 #define IFT_CARP	0xf8	// IFT_CARP is deprecated in FreeBSD 10
     80 #undef SIOCAIFADDR
     81 #define SIOCAIFADDR	_IOW(105, 26, struct oifaliasreq)	// ifaliasreq contains if_data
     82 #undef SIOCSIFPHYADDR
     83 #define SIOCSIFPHYADDR	_IOW(105, 70, struct oifaliasreq)	// ifaliasreq contains if_data
     84 #endif
     85 '
     86 
     87 includes_Linux='
     88 #define _LARGEFILE_SOURCE
     89 #define _LARGEFILE64_SOURCE
     90 #ifndef __LP64__
     91 #define _FILE_OFFSET_BITS 64
     92 #endif
     93 #define _GNU_SOURCE
     94 
     95 #include <bits/sockaddr.h>
     96 #include <sys/epoll.h>
     97 #include <sys/inotify.h>
     98 #include <sys/ioctl.h>
     99 #include <sys/mman.h>
    100 #include <sys/mount.h>
    101 #include <sys/prctl.h>
    102 #include <sys/stat.h>
    103 #include <sys/types.h>
    104 #include <sys/time.h>
    105 #include <sys/socket.h>
    106 #include <linux/if.h>
    107 #include <linux/if_arp.h>
    108 #include <linux/if_ether.h>
    109 #include <linux/if_tun.h>
    110 #include <linux/if_packet.h>
    111 #include <linux/if_addr.h>
    112 #include <linux/filter.h>
    113 #include <linux/netlink.h>
    114 #include <linux/reboot.h>
    115 #include <linux/rtnetlink.h>
    116 #include <linux/ptrace.h>
    117 #include <linux/sched.h>
    118 #include <linux/wait.h>
    119 #include <linux/icmpv6.h>
    120 #include <net/route.h>
    121 #include <termios.h>
    122 
    123 #ifndef MSG_FASTOPEN
    124 #define MSG_FASTOPEN    0x20000000
    125 #endif
    126 
    127 #ifndef PTRACE_GETREGS
    128 #define PTRACE_GETREGS	0xc
    129 #endif
    130 
    131 #ifndef PTRACE_SETREGS
    132 #define PTRACE_SETREGS	0xd
    133 #endif
    134 '
    135 
    136 includes_NetBSD='
    137 #include <sys/types.h>
    138 #include <sys/param.h>
    139 #include <sys/event.h>
    140 #include <sys/mman.h>
    141 #include <sys/socket.h>
    142 #include <sys/sockio.h>
    143 #include <sys/sysctl.h>
    144 #include <sys/termios.h>
    145 #include <sys/ttycom.h>
    146 #include <sys/wait.h>
    147 #include <net/bpf.h>
    148 #include <net/if.h>
    149 #include <net/if_types.h>
    150 #include <net/route.h>
    151 #include <netinet/in.h>
    152 #include <netinet/in_systm.h>
    153 #include <netinet/ip.h>
    154 #include <netinet/ip_mroute.h>
    155 #include <netinet/if_ether.h>
    156 
    157 // Needed since <sys/param.h> refers to it...
    158 #define schedppq 1
    159 '
    160 
    161 includes_OpenBSD='
    162 #include <sys/types.h>
    163 #include <sys/param.h>
    164 #include <sys/event.h>
    165 #include <sys/mman.h>
    166 #include <sys/socket.h>
    167 #include <sys/sockio.h>
    168 #include <sys/sysctl.h>
    169 #include <sys/termios.h>
    170 #include <sys/ttycom.h>
    171 #include <sys/wait.h>
    172 #include <net/bpf.h>
    173 #include <net/if.h>
    174 #include <net/if_types.h>
    175 #include <net/if_var.h>
    176 #include <net/route.h>
    177 #include <netinet/in.h>
    178 #include <netinet/in_systm.h>
    179 #include <netinet/ip.h>
    180 #include <netinet/ip_mroute.h>
    181 #include <netinet/if_ether.h>
    182 #include <net/if_bridge.h>
    183 
    184 // We keep some constants not supported in OpenBSD 5.5 and beyond for
    185 // the promise of compatibility.
    186 #define EMUL_ENABLED		0x1
    187 #define EMUL_NATIVE		0x2
    188 #define IPV6_FAITH		0x1d
    189 #define IPV6_OPTIONS		0x1
    190 #define IPV6_RTHDR_STRICT	0x1
    191 #define IPV6_SOCKOPT_RESERVED1	0x3
    192 #define SIOCGIFGENERIC		0xc020693a
    193 #define SIOCSIFGENERIC		0x80206939
    194 #define WALTSIG			0x4
    195 '
    196 
    197 includes_SunOS='
    198 #include <sys/types.h>
    199 #include <sys/socket.h>
    200 #include <sys/sockio.h>
    201 #include <sys/mman.h>
    202 #include <sys/wait.h>
    203 #include <sys/ioctl.h>
    204 #include <net/bpf.h>
    205 #include <net/if.h>
    206 #include <net/if_arp.h>
    207 #include <net/if_types.h>
    208 #include <net/route.h>
    209 #include <netinet/in.h>
    210 #include <termios.h>
    211 #include <netinet/ip.h>
    212 #include <netinet/ip_mroute.h>
    213 '
    214 
    215 includes='
    216 #include <sys/types.h>
    217 #include <sys/file.h>
    218 #include <fcntl.h>
    219 #include <dirent.h>
    220 #include <sys/socket.h>
    221 #include <netinet/in.h>
    222 #include <netinet/ip.h>
    223 #include <netinet/ip6.h>
    224 #include <netinet/tcp.h>
    225 #include <errno.h>
    226 #include <sys/signal.h>
    227 #include <signal.h>
    228 #include <sys/resource.h>
    229 '
    230 
    231 ccflags="$@"
    232 
    233 # Write go tool cgo -godefs input.
    234 (
    235 	echo package syscall
    236 	echo
    237 	echo '/*'
    238 	indirect="includes_$(uname)"
    239 	echo "${!indirect} $includes"
    240 	echo '*/'
    241 	echo 'import "C"'
    242 	echo
    243 	echo 'const ('
    244 
    245 	# The gcc command line prints all the #defines
    246 	# it encounters while processing the input
    247 	echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
    248 	awk '
    249 		$1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
    250 
    251 		$2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next}  # 386 registers
    252 		$2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
    253 		$2 ~ /^(SCM_SRCRT)$/ {next}
    254 		$2 ~ /^(MAP_FAILED)$/ {next}
    255 		$2 ~ /^ELF_.*$/ {next}	# <asm/elf.h> contains ELF_ARCH, etc.
    256 
    257 		$2 !~ /^ETH_/ &&
    258 		$2 !~ /^EPROC_/ &&
    259 		$2 !~ /^EQUIV_/ &&
    260 		$2 !~ /^EXPR_/ &&
    261 		$2 ~ /^E[A-Z0-9_]+$/ ||
    262 		$2 ~ /^B[0-9_]+$/ ||
    263 		$2 ~ /^V[A-Z0-9]+$/ ||
    264 		$2 ~ /^CS[A-Z0-9]/ ||
    265 		$2 ~ /^I(SIG|CANON|CRNL|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
    266 		$2 ~ /^IGN/ ||
    267 		$2 ~ /^IX(ON|ANY|OFF)$/ ||
    268 		$2 ~ /^IN(LCR|PCK)$/ ||
    269 		$2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
    270 		$2 ~ /^C(LOCAL|READ)$/ ||
    271 		$2 == "BRKINT" ||
    272 		$2 == "HUPCL" ||
    273 		$2 == "PENDIN" ||
    274 		$2 == "TOSTOP" ||
    275 		$2 ~ /^PAR/ ||
    276 		$2 ~ /^SIG[^_]/ ||
    277 		$2 ~ /^O[CNPFP][A-Z]+[^_][A-Z]+$/ ||
    278 		$2 ~ /^IN_/ ||
    279 		$2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
    280 		$2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
    281 		$2 == "ICMPV6_FILTER" ||
    282 		$2 == "SOMAXCONN" ||
    283 		$2 == "NAME_MAX" ||
    284 		$2 == "IFNAMSIZ" ||
    285 		$2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
    286 		$2 ~ /^SYSCTL_VERS/ ||
    287 		$2 ~ /^(MS|MNT)_/ ||
    288 		$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
    289 		$2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
    290 		$2 ~ /^LINUX_REBOOT_CMD_/ ||
    291 		$2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
    292 		$2 !~ "NLA_TYPE_MASK" &&
    293 		$2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
    294 		$2 ~ /^SIOC/ ||
    295 		$2 ~ /^TIOC/ ||
    296 		$2 !~ "RTF_BITS" &&
    297 		$2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
    298 		$2 ~ /^BIOC/ ||
    299 		$2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
    300 		$2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|NOFILE|STACK)|RLIM_INFINITY/ ||
    301 		$2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
    302 		$2 ~ /^CLONE_[A-Z_]+/ ||
    303 		$2 !~ /^(BPF_TIMEVAL)$/ &&
    304 		$2 ~ /^(BPF|DLT)_/ ||
    305 		$2 !~ "WMESGLEN" &&
    306 		$2 ~ /^W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", $2, $2)}
    307 		$2 ~ /^__WCOREFLAG$/ {next}
    308 		$2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
    309 
    310 		{next}
    311 	' | sort
    312 
    313 	echo ')'
    314 ) >_const.go
    315 
    316 # Pull out the error names for later.
    317 errors=$(
    318 	echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
    319 	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
    320 	sort
    321 )
    322 
    323 # Pull out the signal names for later.
    324 signals=$(
    325 	echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
    326 	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
    327 	egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
    328 	sort
    329 )
    330 
    331 # Again, writing regexps to a file.
    332 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
    333 	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
    334 	sort >_error.grep
    335 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
    336 	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
    337 	egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
    338 	sort >_signal.grep
    339 
    340 echo '// mkerrors.sh' "$@"
    341 echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
    342 echo
    343 go tool cgo -godefs -- "$@" _const.go >_error.out
    344 cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
    345 echo
    346 echo '// Errors'
    347 echo 'const ('
    348 cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= Errno(\1)/'
    349 echo ')'
    350 
    351 echo
    352 echo '// Signals'
    353 echo 'const ('
    354 cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= Signal(\1)/'
    355 echo ')'
    356 
    357 # Run C program to print error and syscall strings.
    358 (
    359 	echo -E "
    360 #include <stdio.h>
    361 #include <stdlib.h>
    362 #include <errno.h>
    363 #include <ctype.h>
    364 #include <string.h>
    365 #include <signal.h>
    366 
    367 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
    368 
    369 enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
    370 
    371 int errors[] = {
    372 "
    373 	for i in $errors
    374 	do
    375 		echo -E '	'$i,
    376 	done
    377 
    378 	echo -E "
    379 };
    380 
    381 int signals[] = {
    382 "
    383 	for i in $signals
    384 	do
    385 		echo -E '	'$i,
    386 	done
    387 
    388 	# Use -E because on some systems bash builtin interprets \n itself.
    389 	echo -E '
    390 };
    391 
    392 static int
    393 intcmp(const void *a, const void *b)
    394 {
    395 	return *(int*)a - *(int*)b;
    396 }
    397 
    398 int
    399 main(void)
    400 {
    401 	int i, j, e;
    402 	char buf[1024], *p;
    403 
    404 	printf("\n\n// Error table\n");
    405 	printf("var errors = [...]string {\n");
    406 	qsort(errors, nelem(errors), sizeof errors[0], intcmp);
    407 	for(i=0; i<nelem(errors); i++) {
    408 		e = errors[i];
    409 		if(i > 0 && errors[i-1] == e)
    410 			continue;
    411 		strcpy(buf, strerror(e));
    412 		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
    413 		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
    414 			buf[0] += a - A;
    415 		printf("\t%d: \"%s\",\n", e, buf);
    416 	}
    417 	printf("}\n\n");
    418 	
    419 	printf("\n\n// Signal table\n");
    420 	printf("var signals = [...]string {\n");
    421 	qsort(signals, nelem(signals), sizeof signals[0], intcmp);
    422 	for(i=0; i<nelem(signals); i++) {
    423 		e = signals[i];
    424 		if(i > 0 && signals[i-1] == e)
    425 			continue;
    426 		strcpy(buf, strsignal(e));
    427 		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
    428 		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
    429 			buf[0] += a - A;
    430 		// cut trailing : number.
    431 		p = strrchr(buf, ":"[0]);
    432 		if(p)
    433 			*p = '\0';
    434 		printf("\t%d: \"%s\",\n", e, buf);
    435 	}
    436 	printf("}\n\n");
    437 
    438 	return 0;
    439 }
    440 
    441 '
    442 ) >_errors.c
    443 
    444 $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out
    445