mkerrors.sh 11 KB

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