mkerrors.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. #include <time.h>
  204. '
  205. ccflags="$@"
  206. # Write go tool cgo -godefs input.
  207. (
  208. echo package unix
  209. echo
  210. echo '/*'
  211. indirect="includes_$(uname)"
  212. echo "${!indirect} $includes"
  213. echo '*/'
  214. echo 'import "C"'
  215. echo 'import "syscall"'
  216. echo
  217. echo 'const ('
  218. # The gcc command line prints all the #defines
  219. # it encounters while processing the input
  220. echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
  221. awk '
  222. $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
  223. $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
  224. $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
  225. $2 ~ /^(SCM_SRCRT)$/ {next}
  226. $2 ~ /^(MAP_FAILED)$/ {next}
  227. $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
  228. $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
  229. $2 !~ /^ETH_/ &&
  230. $2 !~ /^EPROC_/ &&
  231. $2 !~ /^EQUIV_/ &&
  232. $2 !~ /^EXPR_/ &&
  233. $2 ~ /^E[A-Z0-9_]+$/ ||
  234. $2 ~ /^B[0-9_]+$/ ||
  235. $2 ~ /^V[A-Z0-9]+$/ ||
  236. $2 ~ /^CS[A-Z0-9]/ ||
  237. $2 ~ /^I(SIG|CANON|CRNL|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
  238. $2 ~ /^IGN/ ||
  239. $2 ~ /^IX(ON|ANY|OFF)$/ ||
  240. $2 ~ /^IN(LCR|PCK)$/ ||
  241. $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
  242. $2 ~ /^C(LOCAL|READ)$/ ||
  243. $2 == "BRKINT" ||
  244. $2 == "HUPCL" ||
  245. $2 == "PENDIN" ||
  246. $2 == "TOSTOP" ||
  247. $2 ~ /^PAR/ ||
  248. $2 ~ /^SIG[^_]/ ||
  249. $2 ~ /^O[CNPFP][A-Z]+[^_][A-Z]+$/ ||
  250. $2 ~ /^IN_/ ||
  251. $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
  252. $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
  253. $2 == "ICMPV6_FILTER" ||
  254. $2 == "SOMAXCONN" ||
  255. $2 == "NAME_MAX" ||
  256. $2 == "IFNAMSIZ" ||
  257. $2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
  258. $2 ~ /^SYSCTL_VERS/ ||
  259. $2 ~ /^(MS|MNT)_/ ||
  260. $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
  261. $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
  262. $2 ~ /^LINUX_REBOOT_CMD_/ ||
  263. $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
  264. $2 !~ "NLA_TYPE_MASK" &&
  265. $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
  266. $2 ~ /^SIOC/ ||
  267. $2 ~ /^TIOC/ ||
  268. $2 !~ "RTF_BITS" &&
  269. $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
  270. $2 ~ /^BIOC/ ||
  271. $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
  272. $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|NOFILE|STACK)|RLIM_INFINITY/ ||
  273. $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
  274. $2 ~ /^CLONE_[A-Z_]+/ ||
  275. $2 !~ /^(BPF_TIMEVAL)$/ &&
  276. $2 ~ /^(BPF|DLT)_/ ||
  277. $2 ~ /^CLOCK_/ ||
  278. $2 !~ "WMESGLEN" &&
  279. $2 ~ /^W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", $2, $2)}
  280. $2 ~ /^__WCOREFLAG$/ {next}
  281. $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
  282. {next}
  283. ' | sort
  284. echo ')'
  285. ) >_const.go
  286. # Pull out the error names for later.
  287. errors=$(
  288. echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
  289. awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
  290. sort
  291. )
  292. # Pull out the signal names for later.
  293. signals=$(
  294. echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
  295. awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
  296. egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
  297. sort
  298. )
  299. # Again, writing regexps to a file.
  300. echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
  301. awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
  302. sort >_error.grep
  303. echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
  304. awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
  305. egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
  306. sort >_signal.grep
  307. echo '// mkerrors.sh' "$@"
  308. echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
  309. echo
  310. go tool cgo -godefs -- "$@" _const.go >_error.out
  311. cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
  312. echo
  313. echo '// Errors'
  314. echo 'const ('
  315. cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
  316. echo ')'
  317. echo
  318. echo '// Signals'
  319. echo 'const ('
  320. cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
  321. echo ')'
  322. # Run C program to print error and syscall strings.
  323. (
  324. echo -E "
  325. #include <stdio.h>
  326. #include <stdlib.h>
  327. #include <errno.h>
  328. #include <ctype.h>
  329. #include <string.h>
  330. #include <signal.h>
  331. #define nelem(x) (sizeof(x)/sizeof((x)[0]))
  332. enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
  333. int errors[] = {
  334. "
  335. for i in $errors
  336. do
  337. echo -E ' '$i,
  338. done
  339. echo -E "
  340. };
  341. int signals[] = {
  342. "
  343. for i in $signals
  344. do
  345. echo -E ' '$i,
  346. done
  347. # Use -E because on some systems bash builtin interprets \n itself.
  348. echo -E '
  349. };
  350. static int
  351. intcmp(const void *a, const void *b)
  352. {
  353. return *(int*)a - *(int*)b;
  354. }
  355. int
  356. main(void)
  357. {
  358. int i, j, e;
  359. char buf[1024], *p;
  360. printf("\n\n// Error table\n");
  361. printf("var errors = [...]string {\n");
  362. qsort(errors, nelem(errors), sizeof errors[0], intcmp);
  363. for(i=0; i<nelem(errors); i++) {
  364. e = errors[i];
  365. if(i > 0 && errors[i-1] == e)
  366. continue;
  367. strcpy(buf, strerror(e));
  368. // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  369. if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
  370. buf[0] += a - A;
  371. printf("\t%d: \"%s\",\n", e, buf);
  372. }
  373. printf("}\n\n");
  374. printf("\n\n// Signal table\n");
  375. printf("var signals = [...]string {\n");
  376. qsort(signals, nelem(signals), sizeof signals[0], intcmp);
  377. for(i=0; i<nelem(signals); i++) {
  378. e = signals[i];
  379. if(i > 0 && signals[i-1] == e)
  380. continue;
  381. strcpy(buf, strsignal(e));
  382. // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  383. if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
  384. buf[0] += a - A;
  385. // cut trailing : number.
  386. p = strrchr(buf, ":"[0]);
  387. if(p)
  388. *p = '\0';
  389. printf("\t%d: \"%s\",\n", e, buf);
  390. }
  391. printf("}\n\n");
  392. return 0;
  393. }
  394. '
  395. ) >_errors.c
  396. $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out