mkerrors.sh 20 KB

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