mkerrors.sh 19 KB

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