mkerrors.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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/mount.h>
  224. #include <linux/netfilter/nfnetlink.h>
  225. #include <linux/netlink.h>
  226. #include <linux/net_namespace.h>
  227. #include <linux/nfc.h>
  228. #include <linux/nsfs.h>
  229. #include <linux/perf_event.h>
  230. #include <linux/pps.h>
  231. #include <linux/ptrace.h>
  232. #include <linux/random.h>
  233. #include <linux/reboot.h>
  234. #include <linux/rtc.h>
  235. #include <linux/rtnetlink.h>
  236. #include <linux/sched.h>
  237. #include <linux/seccomp.h>
  238. #include <linux/serial.h>
  239. #include <linux/sockios.h>
  240. #include <linux/taskstats.h>
  241. #include <linux/tipc.h>
  242. #include <linux/vm_sockets.h>
  243. #include <linux/wait.h>
  244. #include <linux/watchdog.h>
  245. #include <linux/wireguard.h>
  246. #include <mtd/ubi-user.h>
  247. #include <mtd/mtd-user.h>
  248. #include <net/route.h>
  249. #if defined(__sparc__)
  250. // On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
  251. // definition in glibc. As only the error constants are needed here, include the
  252. // generic termibits.h (which is included by termbits.h on sparc).
  253. #include <asm-generic/termbits.h>
  254. #else
  255. #include <asm/termbits.h>
  256. #endif
  257. #ifndef MSG_FASTOPEN
  258. #define MSG_FASTOPEN 0x20000000
  259. #endif
  260. #ifndef PTRACE_GETREGS
  261. #define PTRACE_GETREGS 0xc
  262. #endif
  263. #ifndef PTRACE_SETREGS
  264. #define PTRACE_SETREGS 0xd
  265. #endif
  266. #ifndef SOL_NETLINK
  267. #define SOL_NETLINK 270
  268. #endif
  269. #ifdef SOL_BLUETOOTH
  270. // SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
  271. // but it is already in bluetooth_linux.go
  272. #undef SOL_BLUETOOTH
  273. #endif
  274. // Certain constants are missing from the fs/crypto UAPI
  275. #define FS_KEY_DESC_PREFIX "fscrypt:"
  276. #define FS_KEY_DESC_PREFIX_SIZE 8
  277. #define FS_MAX_KEY_SIZE 64
  278. // The code generator produces -0x1 for (~0), but an unsigned value is necessary
  279. // for the tipc_subscr timeout __u32 field.
  280. #undef TIPC_WAIT_FOREVER
  281. #define TIPC_WAIT_FOREVER 0xffffffff
  282. // Copied from linux/l2tp.h
  283. // Including linux/l2tp.h here causes conflicts between linux/in.h
  284. // and netinet/in.h included via net/route.h above.
  285. #define IPPROTO_L2TP 115
  286. // Copied from linux/hid.h.
  287. // Keep in sync with the size of the referenced fields.
  288. #define _HIDIOCGRAWNAME_LEN 128 // sizeof_field(struct hid_device, name)
  289. #define _HIDIOCGRAWPHYS_LEN 64 // sizeof_field(struct hid_device, phys)
  290. #define _HIDIOCGRAWUNIQ_LEN 64 // sizeof_field(struct hid_device, uniq)
  291. #define _HIDIOCGRAWNAME HIDIOCGRAWNAME(_HIDIOCGRAWNAME_LEN)
  292. #define _HIDIOCGRAWPHYS HIDIOCGRAWPHYS(_HIDIOCGRAWPHYS_LEN)
  293. #define _HIDIOCGRAWUNIQ HIDIOCGRAWUNIQ(_HIDIOCGRAWUNIQ_LEN)
  294. '
  295. includes_NetBSD='
  296. #include <sys/types.h>
  297. #include <sys/param.h>
  298. #include <sys/event.h>
  299. #include <sys/extattr.h>
  300. #include <sys/mman.h>
  301. #include <sys/mount.h>
  302. #include <sys/sched.h>
  303. #include <sys/select.h>
  304. #include <sys/socket.h>
  305. #include <sys/sockio.h>
  306. #include <sys/sysctl.h>
  307. #include <sys/termios.h>
  308. #include <sys/ttycom.h>
  309. #include <sys/wait.h>
  310. #include <net/bpf.h>
  311. #include <net/if.h>
  312. #include <net/if_types.h>
  313. #include <net/route.h>
  314. #include <netinet/in.h>
  315. #include <netinet/in_systm.h>
  316. #include <netinet/ip.h>
  317. #include <netinet/ip_mroute.h>
  318. #include <netinet/if_ether.h>
  319. // Needed since <sys/param.h> refers to it...
  320. #define schedppq 1
  321. '
  322. includes_OpenBSD='
  323. #include <sys/types.h>
  324. #include <sys/param.h>
  325. #include <sys/event.h>
  326. #include <sys/mman.h>
  327. #include <sys/mount.h>
  328. #include <sys/select.h>
  329. #include <sys/sched.h>
  330. #include <sys/socket.h>
  331. #include <sys/sockio.h>
  332. #include <sys/stat.h>
  333. #include <sys/sysctl.h>
  334. #include <sys/termios.h>
  335. #include <sys/ttycom.h>
  336. #include <sys/unistd.h>
  337. #include <sys/wait.h>
  338. #include <net/bpf.h>
  339. #include <net/if.h>
  340. #include <net/if_types.h>
  341. #include <net/if_var.h>
  342. #include <net/route.h>
  343. #include <netinet/in.h>
  344. #include <netinet/in_systm.h>
  345. #include <netinet/ip.h>
  346. #include <netinet/ip_mroute.h>
  347. #include <netinet/if_ether.h>
  348. #include <net/if_bridge.h>
  349. // We keep some constants not supported in OpenBSD 5.5 and beyond for
  350. // the promise of compatibility.
  351. #define EMUL_ENABLED 0x1
  352. #define EMUL_NATIVE 0x2
  353. #define IPV6_FAITH 0x1d
  354. #define IPV6_OPTIONS 0x1
  355. #define IPV6_RTHDR_STRICT 0x1
  356. #define IPV6_SOCKOPT_RESERVED1 0x3
  357. #define SIOCGIFGENERIC 0xc020693a
  358. #define SIOCSIFGENERIC 0x80206939
  359. #define WALTSIG 0x4
  360. '
  361. includes_SunOS='
  362. #include <limits.h>
  363. #include <sys/types.h>
  364. #include <sys/select.h>
  365. #include <sys/socket.h>
  366. #include <sys/sockio.h>
  367. #include <sys/stat.h>
  368. #include <sys/stream.h>
  369. #include <sys/mman.h>
  370. #include <sys/wait.h>
  371. #include <sys/ioctl.h>
  372. #include <sys/mkdev.h>
  373. #include <net/bpf.h>
  374. #include <net/if.h>
  375. #include <net/if_arp.h>
  376. #include <net/if_types.h>
  377. #include <net/route.h>
  378. #include <netinet/icmp6.h>
  379. #include <netinet/in.h>
  380. #include <netinet/ip.h>
  381. #include <netinet/ip_mroute.h>
  382. #include <termios.h>
  383. '
  384. includes='
  385. #include <sys/types.h>
  386. #include <sys/file.h>
  387. #include <fcntl.h>
  388. #include <dirent.h>
  389. #include <sys/socket.h>
  390. #include <netinet/in.h>
  391. #include <netinet/ip.h>
  392. #include <netinet/ip6.h>
  393. #include <netinet/tcp.h>
  394. #include <errno.h>
  395. #include <sys/signal.h>
  396. #include <signal.h>
  397. #include <sys/resource.h>
  398. #include <time.h>
  399. '
  400. ccflags="$@"
  401. # Write go tool cgo -godefs input.
  402. (
  403. echo package unix
  404. echo
  405. echo '/*'
  406. indirect="includes_$(uname)"
  407. echo "${!indirect} $includes"
  408. echo '*/'
  409. echo 'import "C"'
  410. echo 'import "syscall"'
  411. echo
  412. echo 'const ('
  413. # The gcc command line prints all the #defines
  414. # it encounters while processing the input
  415. echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
  416. awk '
  417. $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
  418. $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
  419. $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
  420. $2 ~ /^(SCM_SRCRT)$/ {next}
  421. $2 ~ /^(MAP_FAILED)$/ {next}
  422. $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
  423. $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
  424. $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
  425. $2 !~ /^ECCAPBITS/ &&
  426. $2 !~ /^ETH_/ &&
  427. $2 !~ /^EPROC_/ &&
  428. $2 !~ /^EQUIV_/ &&
  429. $2 !~ /^EXPR_/ &&
  430. $2 !~ /^EVIOC/ &&
  431. $2 ~ /^E[A-Z0-9_]+$/ ||
  432. $2 ~ /^B[0-9_]+$/ ||
  433. $2 ~ /^(OLD|NEW)DEV$/ ||
  434. $2 == "BOTHER" ||
  435. $2 ~ /^CI?BAUD(EX)?$/ ||
  436. $2 == "IBSHIFT" ||
  437. $2 ~ /^V[A-Z0-9]+$/ ||
  438. $2 ~ /^CS[A-Z0-9]/ ||
  439. $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
  440. $2 ~ /^IGN/ ||
  441. $2 ~ /^IX(ON|ANY|OFF)$/ ||
  442. $2 ~ /^IN(LCR|PCK)$/ ||
  443. $2 !~ "X86_CR3_PCID_NOFLUSH" &&
  444. $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
  445. $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
  446. $2 == "BRKINT" ||
  447. $2 == "HUPCL" ||
  448. $2 == "PENDIN" ||
  449. $2 == "TOSTOP" ||
  450. $2 == "XCASE" ||
  451. $2 == "ALTWERASE" ||
  452. $2 == "NOKERNINFO" ||
  453. $2 == "NFDBITS" ||
  454. $2 ~ /^PAR/ ||
  455. $2 ~ /^SIG[^_]/ ||
  456. $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
  457. $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
  458. $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
  459. $2 ~ /^O?XTABS$/ ||
  460. $2 ~ /^TC[IO](ON|OFF)$/ ||
  461. $2 ~ /^IN_/ ||
  462. $2 ~ /^LANDLOCK_/ ||
  463. $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
  464. $2 ~ /^LO_(KEY|NAME)_SIZE$/ ||
  465. $2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ ||
  466. $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)_/ ||
  467. $2 ~ /^NFC_(GENL|PROTO|COMM|RF|SE|DIRECTION|LLCP|SOCKPROTO)_/ ||
  468. $2 ~ /^NFC_.*_(MAX)?SIZE$/ ||
  469. $2 ~ /^RAW_PAYLOAD_/ ||
  470. $2 ~ /^TP_STATUS_/ ||
  471. $2 ~ /^FALLOC_/ ||
  472. $2 ~ /^ICMPV?6?_(FILTER|SEC)/ ||
  473. $2 == "SOMAXCONN" ||
  474. $2 == "NAME_MAX" ||
  475. $2 == "IFNAMSIZ" ||
  476. $2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
  477. $2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
  478. $2 ~ /^HW_MACHINE$/ ||
  479. $2 ~ /^SYSCTL_VERS/ ||
  480. $2 !~ "MNT_BITS" &&
  481. $2 ~ /^(MS|MNT|MOUNT|UMOUNT)_/ ||
  482. $2 ~ /^NS_GET_/ ||
  483. $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
  484. $2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT|TFD)_/ ||
  485. $2 ~ /^KEXEC_/ ||
  486. $2 ~ /^LINUX_REBOOT_CMD_/ ||
  487. $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
  488. $2 ~ /^MODULE_INIT_/ ||
  489. $2 !~ "NLA_TYPE_MASK" &&
  490. $2 !~ /^RTC_VL_(ACCURACY|BACKUP|DATA)/ &&
  491. $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
  492. $2 ~ /^FIORDCHK$/ ||
  493. $2 ~ /^SIOC/ ||
  494. $2 ~ /^TIOC/ ||
  495. $2 ~ /^TCGET/ ||
  496. $2 ~ /^TCSET/ ||
  497. $2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
  498. $2 !~ "RTF_BITS" &&
  499. $2 ~ /^(IFF|IFT|NET_RT|RTM(GRP)?|RTF|RTV|RTA|RTAX)_/ ||
  500. $2 ~ /^BIOC/ ||
  501. $2 ~ /^DIOC/ ||
  502. $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
  503. $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
  504. $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
  505. $2 ~ /^CLONE_[A-Z_]+/ ||
  506. $2 !~ /^(BPF_TIMEVAL|BPF_FIB_LOOKUP_[A-Z]+)$/ &&
  507. $2 ~ /^(BPF|DLT)_/ ||
  508. $2 ~ /^(CLOCK|TIMER)_/ ||
  509. $2 ~ /^CAN_/ ||
  510. $2 ~ /^CAP_/ ||
  511. $2 ~ /^CP_/ ||
  512. $2 ~ /^CPUSTATES$/ ||
  513. $2 ~ /^CTLIOCGINFO$/ ||
  514. $2 ~ /^ALG_/ ||
  515. $2 ~ /^FI(CLONE|DEDUPERANGE)/ ||
  516. $2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE)/ ||
  517. $2 ~ /^FS_IOC_.*(ENCRYPTION|VERITY|[GS]ETFLAGS)/ ||
  518. $2 ~ /^FS_VERITY_/ ||
  519. $2 ~ /^FSCRYPT_/ ||
  520. $2 ~ /^DM_/ ||
  521. $2 ~ /^GRND_/ ||
  522. $2 ~ /^RND/ ||
  523. $2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
  524. $2 ~ /^KEYCTL_/ ||
  525. $2 ~ /^PERF_/ ||
  526. $2 ~ /^SECCOMP_MODE_/ ||
  527. $2 ~ /^SEEK_/ ||
  528. $2 ~ /^SPLICE_/ ||
  529. $2 ~ /^SYNC_FILE_RANGE_/ ||
  530. $2 !~ /^AUDIT_RECORD_MAGIC/ &&
  531. $2 !~ /IOC_MAGIC/ &&
  532. $2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
  533. $2 ~ /^(VM|VMADDR)_/ ||
  534. $2 ~ /^IOCTL_VM_SOCKETS_/ ||
  535. $2 ~ /^(TASKSTATS|TS)_/ ||
  536. $2 ~ /^CGROUPSTATS_/ ||
  537. $2 ~ /^GENL_/ ||
  538. $2 ~ /^STATX_/ ||
  539. $2 ~ /^RENAME/ ||
  540. $2 ~ /^UBI_IOC[A-Z]/ ||
  541. $2 ~ /^UTIME_/ ||
  542. $2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
  543. $2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
  544. $2 ~ /^FSOPT_/ ||
  545. $2 ~ /^WDIO[CFS]_/ ||
  546. $2 ~ /^NFN/ ||
  547. $2 ~ /^XDP_/ ||
  548. $2 ~ /^RWF_/ ||
  549. $2 ~ /^(HDIO|WIN|SMART)_/ ||
  550. $2 ~ /^CRYPTO_/ ||
  551. $2 ~ /^TIPC_/ ||
  552. $2 !~ "DEVLINK_RELOAD_LIMITS_VALID_MASK" &&
  553. $2 ~ /^DEVLINK_/ ||
  554. $2 ~ /^ETHTOOL_/ ||
  555. $2 ~ /^LWTUNNEL_IP/ ||
  556. $2 !~ "WMESGLEN" &&
  557. $2 ~ /^W[A-Z0-9]+$/ ||
  558. $2 ~/^PPPIOC/ ||
  559. $2 ~ /^FAN_|FANOTIFY_/ ||
  560. $2 == "HID_MAX_DESCRIPTOR_SIZE" ||
  561. $2 ~ /^_?HIDIOC/ ||
  562. $2 ~ /^BUS_(USB|HIL|BLUETOOTH|VIRTUAL)$/ ||
  563. $2 ~ /^MTD/ ||
  564. $2 ~ /^OTP/ ||
  565. $2 ~ /^MEM/ ||
  566. $2 ~ /^WG/ ||
  567. $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
  568. $2 ~ /^__WCOREFLAG$/ {next}
  569. $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
  570. {next}
  571. ' | sort
  572. echo ')'
  573. ) >_const.go
  574. # Pull out the error names for later.
  575. errors=$(
  576. echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
  577. awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
  578. sort
  579. )
  580. # Pull out the signal names for later.
  581. signals=$(
  582. echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
  583. awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
  584. egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
  585. sort
  586. )
  587. # Again, writing regexps to a file.
  588. echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
  589. awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
  590. sort >_error.grep
  591. echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
  592. awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
  593. egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
  594. sort >_signal.grep
  595. echo '// mkerrors.sh' "$@"
  596. echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
  597. echo
  598. echo "//go:build ${GOARCH} && ${GOOS}"
  599. echo "// +build ${GOARCH},${GOOS}"
  600. echo
  601. go tool cgo -godefs -- "$@" _const.go >_error.out
  602. cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
  603. echo
  604. echo '// Errors'
  605. echo 'const ('
  606. cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
  607. echo ')'
  608. echo
  609. echo '// Signals'
  610. echo 'const ('
  611. cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
  612. echo ')'
  613. # Run C program to print error and syscall strings.
  614. (
  615. echo -E "
  616. #include <stdio.h>
  617. #include <stdlib.h>
  618. #include <errno.h>
  619. #include <ctype.h>
  620. #include <string.h>
  621. #include <signal.h>
  622. #define nelem(x) (sizeof(x)/sizeof((x)[0]))
  623. enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
  624. struct tuple {
  625. int num;
  626. const char *name;
  627. };
  628. struct tuple errors[] = {
  629. "
  630. for i in $errors
  631. do
  632. echo -E ' {'$i', "'$i'" },'
  633. done
  634. echo -E "
  635. };
  636. struct tuple signals[] = {
  637. "
  638. for i in $signals
  639. do
  640. echo -E ' {'$i', "'$i'" },'
  641. done
  642. # Use -E because on some systems bash builtin interprets \n itself.
  643. echo -E '
  644. };
  645. static int
  646. tuplecmp(const void *a, const void *b)
  647. {
  648. return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
  649. }
  650. int
  651. main(void)
  652. {
  653. int i, e;
  654. char buf[1024], *p;
  655. printf("\n\n// Error table\n");
  656. printf("var errorList = [...]struct {\n");
  657. printf("\tnum syscall.Errno\n");
  658. printf("\tname string\n");
  659. printf("\tdesc string\n");
  660. printf("} {\n");
  661. qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
  662. for(i=0; i<nelem(errors); i++) {
  663. e = errors[i].num;
  664. if(i > 0 && errors[i-1].num == e)
  665. continue;
  666. strcpy(buf, strerror(e));
  667. // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  668. if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
  669. buf[0] += a - A;
  670. printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
  671. }
  672. printf("}\n\n");
  673. printf("\n\n// Signal table\n");
  674. printf("var signalList = [...]struct {\n");
  675. printf("\tnum syscall.Signal\n");
  676. printf("\tname string\n");
  677. printf("\tdesc string\n");
  678. printf("} {\n");
  679. qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
  680. for(i=0; i<nelem(signals); i++) {
  681. e = signals[i].num;
  682. if(i > 0 && signals[i-1].num == e)
  683. continue;
  684. strcpy(buf, strsignal(e));
  685. // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  686. if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
  687. buf[0] += a - A;
  688. // cut trailing : number.
  689. p = strrchr(buf, ":"[0]);
  690. if(p)
  691. *p = '\0';
  692. printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
  693. }
  694. printf("}\n\n");
  695. return 0;
  696. }
  697. '
  698. ) >_errors.c
  699. $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out