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