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