You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mkerrors.sh 19KB

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