Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

500 linhas
12 KiB

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