mkerrors.sh 10 KB

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