A lightweight mechanism to provide an *instant kickstart* to a Go web server instance upon changing any Go source files under the project directory (and its subdirectories).

mkerrors.sh 13KB

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