You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

577 lines
14 KiB

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