mkerrors.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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" = "linux" ]] && [[ "$GOLANG_SYS_BUILD" != "docker" ]]; then
  17. echo 1>&2 "In the Docker based build system, mkerrors should not be called directly."
  18. echo 1>&2 "See README.md"
  19. exit 1
  20. fi
  21. if [[ "$GOOS" = "aix" ]]; then
  22. CC=${CC:-gcc}
  23. else
  24. CC=${CC:-cc}
  25. fi
  26. if [[ "$GOOS" = "solaris" ]]; then
  27. # Assumes GNU versions of utilities in PATH.
  28. export PATH=/usr/gnu/bin:$PATH
  29. fi
  30. uname=$(uname)
  31. includes_AIX='
  32. #include <net/if.h>
  33. #include <net/netopt.h>
  34. #include <netinet/ip_mroute.h>
  35. #include <sys/protosw.h>
  36. #include <sys/stropts.h>
  37. #include <sys/mman.h>
  38. #include <sys/poll.h>
  39. #include <sys/select.h>
  40. #include <sys/termio.h>
  41. #include <termios.h>
  42. #include <fcntl.h>
  43. #define AF_LOCAL AF_UNIX
  44. '
  45. includes_Darwin='
  46. #define _DARWIN_C_SOURCE
  47. #define KERNEL
  48. #define _DARWIN_USE_64_BIT_INODE
  49. #include <stdint.h>
  50. #include <sys/attr.h>
  51. #include <sys/clonefile.h>
  52. #include <sys/kern_control.h>
  53. #include <sys/types.h>
  54. #include <sys/event.h>
  55. #include <sys/ptrace.h>
  56. #include <sys/select.h>
  57. #include <sys/socket.h>
  58. #include <sys/sockio.h>
  59. #include <sys/sys_domain.h>
  60. #include <sys/sysctl.h>
  61. #include <sys/mman.h>
  62. #include <sys/mount.h>
  63. #include <sys/utsname.h>
  64. #include <sys/wait.h>
  65. #include <sys/xattr.h>
  66. #include <net/bpf.h>
  67. #include <net/if.h>
  68. #include <net/if_types.h>
  69. #include <net/route.h>
  70. #include <netinet/in.h>
  71. #include <netinet/ip.h>
  72. #include <termios.h>
  73. '
  74. includes_DragonFly='
  75. #include <sys/types.h>
  76. #include <sys/event.h>
  77. #include <sys/select.h>
  78. #include <sys/socket.h>
  79. #include <sys/sockio.h>
  80. #include <sys/stat.h>
  81. #include <sys/sysctl.h>
  82. #include <sys/mman.h>
  83. #include <sys/mount.h>
  84. #include <sys/wait.h>
  85. #include <sys/ioctl.h>
  86. #include <net/bpf.h>
  87. #include <net/if.h>
  88. #include <net/if_clone.h>
  89. #include <net/if_types.h>
  90. #include <net/route.h>
  91. #include <netinet/in.h>
  92. #include <termios.h>
  93. #include <netinet/ip.h>
  94. #include <net/ip_mroute/ip_mroute.h>
  95. '
  96. includes_FreeBSD='
  97. #include <sys/capsicum.h>
  98. #include <sys/param.h>
  99. #include <sys/types.h>
  100. #include <sys/disk.h>
  101. #include <sys/event.h>
  102. #include <sys/sched.h>
  103. #include <sys/select.h>
  104. #include <sys/socket.h>
  105. #include <sys/sockio.h>
  106. #include <sys/stat.h>
  107. #include <sys/sysctl.h>
  108. #include <sys/mman.h>
  109. #include <sys/mount.h>
  110. #include <sys/wait.h>
  111. #include <sys/ioctl.h>
  112. #include <net/bpf.h>
  113. #include <net/if.h>
  114. #include <net/if_types.h>
  115. #include <net/route.h>
  116. #include <netinet/in.h>
  117. #include <termios.h>
  118. #include <netinet/ip.h>
  119. #include <netinet/ip_mroute.h>
  120. #include <sys/extattr.h>
  121. #if __FreeBSD__ >= 10
  122. #define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
  123. #undef SIOCAIFADDR
  124. #define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
  125. #undef SIOCSIFPHYADDR
  126. #define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
  127. #endif
  128. '
  129. includes_Linux='
  130. #define _LARGEFILE_SOURCE
  131. #define _LARGEFILE64_SOURCE
  132. #ifndef __LP64__
  133. #define _FILE_OFFSET_BITS 64
  134. #endif
  135. #define _GNU_SOURCE
  136. // <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
  137. // these structures. We just include them copied from <bits/termios.h>.
  138. #if defined(__powerpc__)
  139. struct sgttyb {
  140. char sg_ispeed;
  141. char sg_ospeed;
  142. char sg_erase;
  143. char sg_kill;
  144. short sg_flags;
  145. };
  146. struct tchars {
  147. char t_intrc;
  148. char t_quitc;
  149. char t_startc;
  150. char t_stopc;
  151. char t_eofc;
  152. char t_brkc;
  153. };
  154. struct ltchars {
  155. char t_suspc;
  156. char t_dsuspc;
  157. char t_rprntc;
  158. char t_flushc;
  159. char t_werasc;
  160. char t_lnextc;
  161. };
  162. #endif
  163. #include <bits/sockaddr.h>
  164. #include <sys/epoll.h>
  165. #include <sys/eventfd.h>
  166. #include <sys/inotify.h>
  167. #include <sys/ioctl.h>
  168. #include <sys/mman.h>
  169. #include <sys/mount.h>
  170. #include <sys/prctl.h>
  171. #include <sys/stat.h>
  172. #include <sys/types.h>
  173. #include <sys/time.h>
  174. #include <sys/select.h>
  175. #include <sys/signalfd.h>
  176. #include <sys/socket.h>
  177. #include <sys/timerfd.h>
  178. #include <sys/uio.h>
  179. #include <sys/xattr.h>
  180. #include <linux/bpf.h>
  181. #include <linux/can.h>
  182. #include <linux/can/error.h>
  183. #include <linux/can/raw.h>
  184. #include <linux/capability.h>
  185. #include <linux/cryptouser.h>
  186. #include <linux/devlink.h>
  187. #include <linux/dm-ioctl.h>
  188. #include <linux/errqueue.h>
  189. #include <linux/falloc.h>
  190. #include <linux/fanotify.h>
  191. #include <linux/filter.h>
  192. #include <linux/fs.h>
  193. #include <linux/fscrypt.h>
  194. #include <linux/fsverity.h>
  195. #include <linux/genetlink.h>
  196. #include <linux/hdreg.h>
  197. #include <linux/icmpv6.h>
  198. #include <linux/if.h>
  199. #include <linux/if_addr.h>
  200. #include <linux/if_alg.h>
  201. #include <linux/if_arp.h>
  202. #include <linux/if_ether.h>
  203. #include <linux/if_ppp.h>
  204. #include <linux/if_tun.h>
  205. #include <linux/if_packet.h>
  206. #include <linux/if_xdp.h>
  207. #include <linux/kexec.h>
  208. #include <linux/keyctl.h>
  209. #include <linux/loop.h>
  210. #include <linux/lwtunnel.h>
  211. #include <linux/magic.h>
  212. #include <linux/memfd.h>
  213. #include <linux/module.h>
  214. #include <linux/netfilter/nfnetlink.h>
  215. #include <linux/netlink.h>
  216. #include <linux/net_namespace.h>
  217. #include <linux/nsfs.h>
  218. #include <linux/perf_event.h>
  219. #include <linux/pps.h>
  220. #include <linux/ptrace.h>
  221. #include <linux/random.h>
  222. #include <linux/reboot.h>
  223. #include <linux/rtc.h>
  224. #include <linux/rtnetlink.h>
  225. #include <linux/sched.h>
  226. #include <linux/seccomp.h>
  227. #include <linux/serial.h>
  228. #include <linux/sockios.h>
  229. #include <linux/taskstats.h>
  230. #include <linux/tipc.h>
  231. #include <linux/vm_sockets.h>
  232. #include <linux/wait.h>
  233. #include <linux/watchdog.h>
  234. #include <mtd/ubi-user.h>
  235. #include <net/route.h>
  236. #if defined(__sparc__)
  237. // On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
  238. // definition in glibc. As only the error constants are needed here, include the
  239. // generic termibits.h (which is included by termbits.h on sparc).
  240. #include <asm-generic/termbits.h>
  241. #else
  242. #include <asm/termbits.h>
  243. #endif
  244. #ifndef MSG_FASTOPEN
  245. #define MSG_FASTOPEN 0x20000000
  246. #endif
  247. #ifndef PTRACE_GETREGS
  248. #define PTRACE_GETREGS 0xc
  249. #endif
  250. #ifndef PTRACE_SETREGS
  251. #define PTRACE_SETREGS 0xd
  252. #endif
  253. #ifndef SOL_NETLINK
  254. #define SOL_NETLINK 270
  255. #endif
  256. #ifdef SOL_BLUETOOTH
  257. // SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
  258. // but it is already in bluetooth_linux.go
  259. #undef SOL_BLUETOOTH
  260. #endif
  261. // Certain constants are missing from the fs/crypto UAPI
  262. #define FS_KEY_DESC_PREFIX "fscrypt:"
  263. #define FS_KEY_DESC_PREFIX_SIZE 8
  264. #define FS_MAX_KEY_SIZE 64
  265. // The code generator produces -0x1 for (~0), but an unsigned value is necessary
  266. // for the tipc_subscr timeout __u32 field.
  267. #undef TIPC_WAIT_FOREVER
  268. #define TIPC_WAIT_FOREVER 0xffffffff
  269. // Copied from linux/l2tp.h
  270. // Including linux/l2tp.h here causes conflicts between linux/in.h
  271. // and netinet/in.h included via net/route.h above.
  272. #define IPPROTO_L2TP 115
  273. '
  274. includes_NetBSD='
  275. #include <sys/types.h>
  276. #include <sys/param.h>
  277. #include <sys/event.h>
  278. #include <sys/extattr.h>
  279. #include <sys/mman.h>
  280. #include <sys/mount.h>
  281. #include <sys/sched.h>
  282. #include <sys/select.h>
  283. #include <sys/socket.h>
  284. #include <sys/sockio.h>
  285. #include <sys/sysctl.h>
  286. #include <sys/termios.h>
  287. #include <sys/ttycom.h>
  288. #include <sys/wait.h>
  289. #include <net/bpf.h>
  290. #include <net/if.h>
  291. #include <net/if_types.h>
  292. #include <net/route.h>
  293. #include <netinet/in.h>
  294. #include <netinet/in_systm.h>
  295. #include <netinet/ip.h>
  296. #include <netinet/ip_mroute.h>
  297. #include <netinet/if_ether.h>
  298. // Needed since <sys/param.h> refers to it...
  299. #define schedppq 1
  300. '
  301. includes_OpenBSD='
  302. #include <sys/types.h>
  303. #include <sys/param.h>
  304. #include <sys/event.h>
  305. #include <sys/mman.h>
  306. #include <sys/mount.h>
  307. #include <sys/select.h>
  308. #include <sys/sched.h>
  309. #include <sys/socket.h>
  310. #include <sys/sockio.h>
  311. #include <sys/stat.h>
  312. #include <sys/sysctl.h>
  313. #include <sys/termios.h>
  314. #include <sys/ttycom.h>
  315. #include <sys/unistd.h>
  316. #include <sys/wait.h>
  317. #include <net/bpf.h>
  318. #include <net/if.h>
  319. #include <net/if_types.h>
  320. #include <net/if_var.h>
  321. #include <net/route.h>
  322. #include <netinet/in.h>
  323. #include <netinet/in_systm.h>
  324. #include <netinet/ip.h>
  325. #include <netinet/ip_mroute.h>
  326. #include <netinet/if_ether.h>
  327. #include <net/if_bridge.h>
  328. // We keep some constants not supported in OpenBSD 5.5 and beyond for
  329. // the promise of compatibility.
  330. #define EMUL_ENABLED 0x1
  331. #define EMUL_NATIVE 0x2
  332. #define IPV6_FAITH 0x1d
  333. #define IPV6_OPTIONS 0x1
  334. #define IPV6_RTHDR_STRICT 0x1
  335. #define IPV6_SOCKOPT_RESERVED1 0x3
  336. #define SIOCGIFGENERIC 0xc020693a
  337. #define SIOCSIFGENERIC 0x80206939
  338. #define WALTSIG 0x4
  339. '
  340. includes_SunOS='
  341. #include <limits.h>
  342. #include <sys/types.h>
  343. #include <sys/select.h>
  344. #include <sys/socket.h>
  345. #include <sys/sockio.h>
  346. #include <sys/stat.h>
  347. #include <sys/stream.h>
  348. #include <sys/mman.h>
  349. #include <sys/wait.h>
  350. #include <sys/ioctl.h>
  351. #include <sys/mkdev.h>
  352. #include <net/bpf.h>
  353. #include <net/if.h>
  354. #include <net/if_arp.h>
  355. #include <net/if_types.h>
  356. #include <net/route.h>
  357. #include <netinet/in.h>
  358. #include <termios.h>
  359. #include <netinet/ip.h>
  360. #include <netinet/ip_mroute.h>
  361. '
  362. includes='
  363. #include <sys/types.h>
  364. #include <sys/file.h>
  365. #include <fcntl.h>
  366. #include <dirent.h>
  367. #include <sys/socket.h>
  368. #include <netinet/in.h>
  369. #include <netinet/ip.h>
  370. #include <netinet/ip6.h>
  371. #include <netinet/tcp.h>
  372. #include <errno.h>
  373. #include <sys/signal.h>
  374. #include <signal.h>
  375. #include <sys/resource.h>
  376. #include <time.h>
  377. '
  378. ccflags="$@"
  379. # Write go tool cgo -godefs input.
  380. (
  381. echo package unix
  382. echo
  383. echo '/*'
  384. indirect="includes_$(uname)"
  385. echo "${!indirect} $includes"
  386. echo '*/'
  387. echo 'import "C"'
  388. echo 'import "syscall"'
  389. echo
  390. echo 'const ('
  391. # The gcc command line prints all the #defines
  392. # it encounters while processing the input
  393. echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
  394. awk '
  395. $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
  396. $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
  397. $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
  398. $2 ~ /^(SCM_SRCRT)$/ {next}
  399. $2 ~ /^(MAP_FAILED)$/ {next}
  400. $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
  401. $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
  402. $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
  403. $2 !~ /^ECCAPBITS/ &&
  404. $2 !~ /^ETH_/ &&
  405. $2 !~ /^EPROC_/ &&
  406. $2 !~ /^EQUIV_/ &&
  407. $2 !~ /^EXPR_/ &&
  408. $2 ~ /^E[A-Z0-9_]+$/ ||
  409. $2 ~ /^B[0-9_]+$/ ||
  410. $2 ~ /^(OLD|NEW)DEV$/ ||
  411. $2 == "BOTHER" ||
  412. $2 ~ /^CI?BAUD(EX)?$/ ||
  413. $2 == "IBSHIFT" ||
  414. $2 ~ /^V[A-Z0-9]+$/ ||
  415. $2 ~ /^CS[A-Z0-9]/ ||
  416. $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
  417. $2 ~ /^IGN/ ||
  418. $2 ~ /^IX(ON|ANY|OFF)$/ ||
  419. $2 ~ /^IN(LCR|PCK)$/ ||
  420. $2 !~ "X86_CR3_PCID_NOFLUSH" &&
  421. $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
  422. $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
  423. $2 == "BRKINT" ||
  424. $2 == "HUPCL" ||
  425. $2 == "PENDIN" ||
  426. $2 == "TOSTOP" ||
  427. $2 == "XCASE" ||
  428. $2 == "ALTWERASE" ||
  429. $2 == "NOKERNINFO" ||
  430. $2 == "NFDBITS" ||
  431. $2 ~ /^PAR/ ||
  432. $2 ~ /^SIG[^_]/ ||
  433. $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
  434. $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
  435. $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
  436. $2 ~ /^O?XTABS$/ ||
  437. $2 ~ /^TC[IO](ON|OFF)$/ ||
  438. $2 ~ /^IN_/ ||
  439. $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
  440. $2 ~ /^LO_(KEY|NAME)_SIZE$/ ||
  441. $2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ ||
  442. $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|MCAST|EVFILT|NOTE|EV|SHUT|PROT|MAP|MFD|T?PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
  443. $2 ~ /^TP_STATUS_/ ||
  444. $2 ~ /^FALLOC_/ ||
  445. $2 == "ICMPV6_FILTER" ||
  446. $2 == "SOMAXCONN" ||
  447. $2 == "NAME_MAX" ||
  448. $2 == "IFNAMSIZ" ||
  449. $2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
  450. $2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
  451. $2 ~ /^HW_MACHINE$/ ||
  452. $2 ~ /^SYSCTL_VERS/ ||
  453. $2 !~ "MNT_BITS" &&
  454. $2 ~ /^(MS|MNT|UMOUNT)_/ ||
  455. $2 ~ /^NS_GET_/ ||
  456. $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
  457. $2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT|TFD)_/ ||
  458. $2 ~ /^KEXEC_/ ||
  459. $2 ~ /^LINUX_REBOOT_CMD_/ ||
  460. $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
  461. $2 ~ /^MODULE_INIT_/ ||
  462. $2 !~ "NLA_TYPE_MASK" &&
  463. $2 !~ /^RTC_VL_(ACCURACY|BACKUP|DATA)/ &&
  464. $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
  465. $2 ~ /^FIORDCHK$/ ||
  466. $2 ~ /^SIOC/ ||
  467. $2 ~ /^TIOC/ ||
  468. $2 ~ /^TCGET/ ||
  469. $2 ~ /^TCSET/ ||
  470. $2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
  471. $2 !~ "RTF_BITS" &&
  472. $2 ~ /^(IFF|IFT|NET_RT|RTM(GRP)?|RTF|RTV|RTA|RTAX)_/ ||
  473. $2 ~ /^BIOC/ ||
  474. $2 ~ /^DIOC/ ||
  475. $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
  476. $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
  477. $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
  478. $2 ~ /^CLONE_[A-Z_]+/ ||
  479. $2 !~ /^(BPF_TIMEVAL|BPF_FIB_LOOKUP_[A-Z]+)$/ &&
  480. $2 ~ /^(BPF|DLT)_/ ||
  481. $2 ~ /^(CLOCK|TIMER)_/ ||
  482. $2 ~ /^CAN_/ ||
  483. $2 ~ /^CAP_/ ||
  484. $2 ~ /^CP_/ ||
  485. $2 ~ /^CPUSTATES$/ ||
  486. $2 ~ /^CTLIOCGINFO$/ ||
  487. $2 ~ /^ALG_/ ||
  488. $2 ~ /^FI(CLONE|DEDUPERANGE)/ ||
  489. $2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE)/ ||
  490. $2 ~ /^FS_IOC_.*(ENCRYPTION|VERITY|[GS]ETFLAGS)/ ||
  491. $2 ~ /^FS_VERITY_/ ||
  492. $2 ~ /^FSCRYPT_/ ||
  493. $2 ~ /^DM_/ ||
  494. $2 ~ /^GRND_/ ||
  495. $2 ~ /^RND/ ||
  496. $2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
  497. $2 ~ /^KEYCTL_/ ||
  498. $2 ~ /^PERF_/ ||
  499. $2 ~ /^SECCOMP_MODE_/ ||
  500. $2 ~ /^SPLICE_/ ||
  501. $2 ~ /^SYNC_FILE_RANGE_/ ||
  502. $2 !~ /^AUDIT_RECORD_MAGIC/ &&
  503. $2 !~ /IOC_MAGIC/ &&
  504. $2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
  505. $2 ~ /^(VM|VMADDR)_/ ||
  506. $2 ~ /^IOCTL_VM_SOCKETS_/ ||
  507. $2 ~ /^(TASKSTATS|TS)_/ ||
  508. $2 ~ /^CGROUPSTATS_/ ||
  509. $2 ~ /^GENL_/ ||
  510. $2 ~ /^STATX_/ ||
  511. $2 ~ /^RENAME/ ||
  512. $2 ~ /^UBI_IOC[A-Z]/ ||
  513. $2 ~ /^UTIME_/ ||
  514. $2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
  515. $2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
  516. $2 ~ /^FSOPT_/ ||
  517. $2 ~ /^WDIO[CFS]_/ ||
  518. $2 ~ /^NFN/ ||
  519. $2 ~ /^XDP_/ ||
  520. $2 ~ /^RWF_/ ||
  521. $2 ~ /^(HDIO|WIN|SMART)_/ ||
  522. $2 ~ /^CRYPTO_/ ||
  523. $2 ~ /^TIPC_/ ||
  524. $2 ~ /^DEVLINK_/ ||
  525. $2 ~ /^LWTUNNEL_IP/ ||
  526. $2 !~ "WMESGLEN" &&
  527. $2 ~ /^W[A-Z0-9]+$/ ||
  528. $2 ~/^PPPIOC/ ||
  529. $2 ~ /^FAN_|FANOTIFY_/ ||
  530. $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
  531. $2 ~ /^__WCOREFLAG$/ {next}
  532. $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
  533. {next}
  534. ' | sort
  535. echo ')'
  536. ) >_const.go
  537. # Pull out the error names for later.
  538. errors=$(
  539. echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
  540. awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
  541. sort
  542. )
  543. # Pull out the signal names for later.
  544. signals=$(
  545. echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
  546. awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
  547. egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
  548. sort
  549. )
  550. # Again, writing regexps to a file.
  551. echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
  552. awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
  553. sort >_error.grep
  554. echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
  555. awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
  556. egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
  557. sort >_signal.grep
  558. echo '// mkerrors.sh' "$@"
  559. echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
  560. echo
  561. echo "// +build ${GOARCH},${GOOS}"
  562. echo
  563. go tool cgo -godefs -- "$@" _const.go >_error.out
  564. cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
  565. echo
  566. echo '// Errors'
  567. echo 'const ('
  568. cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
  569. echo ')'
  570. echo
  571. echo '// Signals'
  572. echo 'const ('
  573. cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
  574. echo ')'
  575. # Run C program to print error and syscall strings.
  576. (
  577. echo -E "
  578. #include <stdio.h>
  579. #include <stdlib.h>
  580. #include <errno.h>
  581. #include <ctype.h>
  582. #include <string.h>
  583. #include <signal.h>
  584. #define nelem(x) (sizeof(x)/sizeof((x)[0]))
  585. enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
  586. struct tuple {
  587. int num;
  588. const char *name;
  589. };
  590. struct tuple errors[] = {
  591. "
  592. for i in $errors
  593. do
  594. echo -E ' {'$i', "'$i'" },'
  595. done
  596. echo -E "
  597. };
  598. struct tuple signals[] = {
  599. "
  600. for i in $signals
  601. do
  602. echo -E ' {'$i', "'$i'" },'
  603. done
  604. # Use -E because on some systems bash builtin interprets \n itself.
  605. echo -E '
  606. };
  607. static int
  608. tuplecmp(const void *a, const void *b)
  609. {
  610. return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
  611. }
  612. int
  613. main(void)
  614. {
  615. int i, e;
  616. char buf[1024], *p;
  617. printf("\n\n// Error table\n");
  618. printf("var errorList = [...]struct {\n");
  619. printf("\tnum syscall.Errno\n");
  620. printf("\tname string\n");
  621. printf("\tdesc string\n");
  622. printf("} {\n");
  623. qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
  624. for(i=0; i<nelem(errors); i++) {
  625. e = errors[i].num;
  626. if(i > 0 && errors[i-1].num == e)
  627. continue;
  628. strcpy(buf, strerror(e));
  629. // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  630. if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
  631. buf[0] += a - A;
  632. printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
  633. }
  634. printf("}\n\n");
  635. printf("\n\n// Signal table\n");
  636. printf("var signalList = [...]struct {\n");
  637. printf("\tnum syscall.Signal\n");
  638. printf("\tname string\n");
  639. printf("\tdesc string\n");
  640. printf("} {\n");
  641. qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
  642. for(i=0; i<nelem(signals); i++) {
  643. e = signals[i].num;
  644. if(i > 0 && signals[i-1].num == e)
  645. continue;
  646. strcpy(buf, strsignal(e));
  647. // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  648. if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
  649. buf[0] += a - A;
  650. // cut trailing : number.
  651. p = strrchr(buf, ":"[0]);
  652. if(p)
  653. *p = '\0';
  654. printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
  655. }
  656. printf("}\n\n");
  657. return 0;
  658. }
  659. '
  660. ) >_errors.c
  661. $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out