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.

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