mkerrors.sh 11 KB

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