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.
 
 

782 lines
19 KiB

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