sysinfo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* sysinfo.c -- input for mksysinfo.sh
  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. /* This file is passed to GCC with the -fdump-go-spec option to
  6. generate a Go version of the system information. */
  7. #include "config.h"
  8. #include <stddef.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <dirent.h>
  13. #include <errno.h>
  14. #include <fcntl.h>
  15. #include <ucontext.h>
  16. #include <netinet/in.h>
  17. /* <netinet/tcp.h> needs u_char/u_short, but <sys/bsd_types> is only
  18. included by <netinet/in.h> if _SGIAPI (i.e. _SGI_SOURCE
  19. && !_XOPEN_SOURCE.
  20. <sys/termios.h> only defines TIOCNOTTY if !_XOPEN_SOURCE, while
  21. <sys/ttold.h> does so unconditionally. */
  22. #ifdef __sgi__
  23. #include <sys/bsd_types.h>
  24. #include <sys/ttold.h>
  25. #endif
  26. #include <netinet/tcp.h>
  27. #if defined(HAVE_NETINET_IN_SYSTM_H)
  28. #include <netinet/in_systm.h>
  29. #endif
  30. #if defined(HAVE_NETINET_IP_H)
  31. #include <netinet/ip.h>
  32. #endif
  33. #if defined(HAVE_NETINET_IP_MROUTE_H)
  34. #include <netinet/ip_mroute.h>
  35. #endif
  36. #if defined(HAVE_NETINET_IF_ETHER_H)
  37. #include <netinet/if_ether.h>
  38. #endif
  39. #include <signal.h>
  40. #include <sys/ioctl.h>
  41. #include <termios.h>
  42. #if defined(HAVE_SYSCALL_H)
  43. #include <syscall.h>
  44. #endif
  45. #if defined(HAVE_SYS_SYSCALL_H)
  46. #include <sys/syscall.h>
  47. #endif
  48. #if defined(HAVE_SYS_SYSCTL_H)
  49. #include <sys/sysctl.h>
  50. #endif
  51. #if defined(HAVE_SYS_EPOLL_H)
  52. #include <sys/epoll.h>
  53. #endif
  54. #if defined(HAVE_SYS_EVENT_H)
  55. #include <sys/event.h>
  56. #endif
  57. #if defined(HAVE_SYS_FILE_H)
  58. #include <sys/file.h>
  59. #endif
  60. #if defined(HAVE_SYS_MMAN_H)
  61. #include <sys/mman.h>
  62. #endif
  63. #if defined(HAVE_SYS_PRCTL_H)
  64. #include <sys/prctl.h>
  65. #endif
  66. #if defined(HAVE_SYS_PTRACE_H)
  67. #include <sys/ptrace.h>
  68. #endif
  69. #include <sys/resource.h>
  70. #include <sys/uio.h>
  71. #include <sys/socket.h>
  72. #include <sys/stat.h>
  73. #include <sys/time.h>
  74. #include <sys/times.h>
  75. #include <sys/wait.h>
  76. #include <sys/un.h>
  77. #if defined(HAVE_SYS_USER_H)
  78. #include <sys/user.h>
  79. #endif
  80. #if defined(HAVE_SYS_UTSNAME_H)
  81. #include <sys/utsname.h>
  82. #endif
  83. #if defined(HAVE_SYS_SELECT_H)
  84. #include <sys/select.h>
  85. #endif
  86. #include <time.h>
  87. #include <unistd.h>
  88. #include <netdb.h>
  89. #include <pwd.h>
  90. #include <grp.h>
  91. #if defined(HAVE_LINUX_FILTER_H)
  92. #include <linux/filter.h>
  93. #endif
  94. #if defined(HAVE_LINUX_IF_ADDR_H)
  95. #include <linux/if_addr.h>
  96. #endif
  97. #if defined(HAVE_LINUX_IF_ETHER_H)
  98. #include <linux/if_ether.h>
  99. #endif
  100. #if defined(HAVE_LINUX_IF_TUN_H)
  101. #include <linux/if_tun.h>
  102. #endif
  103. #if defined(HAVE_LINUX_NETLINK_H)
  104. #include <linux/netlink.h>
  105. #endif
  106. #if defined(HAVE_LINUX_PTRACE_H)
  107. /* Avoid https://sourceware.org/bugzilla/show_bug.cgi?id=762 . */
  108. #define ia64_fpreg pt_ia64_fpreg
  109. #define pt_all_user_regs pt_ia64_all_user_regs
  110. /* Avoid redefinition of ptrace_peeksiginfo from <sys/ptrace.h>.
  111. https://gcc.gnu.org/PR81324 . */
  112. #define ptrace_peeksiginfo_args ignore_ptrace_peeksiginfo_args
  113. #include <linux/ptrace.h>
  114. #undef ia64_fpreg
  115. #undef pt_all_user_regs
  116. #undef ptrace_peeksiginfo_args
  117. #endif
  118. #if defined(HAVE_LINUX_RTNETLINK_H)
  119. #include <linux/rtnetlink.h>
  120. #endif
  121. #if defined(HAVE_NET_BPF_H)
  122. #include <net/bpf.h>
  123. #endif
  124. #if defined(HAVE_NET_IF_H)
  125. #include <net/if.h>
  126. #endif
  127. #if defined(HAVE_NET_IF_ARP_H)
  128. #include <net/if_arp.h>
  129. #endif
  130. #if defined(HAVE_NET_ROUTE_H)
  131. #include <net/route.h>
  132. #endif
  133. #if defined (HAVE_NETPACKET_PACKET_H)
  134. #include <netpacket/packet.h>
  135. #endif
  136. #if defined(HAVE_SYS_MOUNT_H)
  137. #include <sys/mount.h>
  138. #endif
  139. #if defined(HAVE_SYS_VFS_H)
  140. #include <sys/vfs.h>
  141. #endif
  142. #if defined(HAVE_STATFS_H)
  143. #include <sys/statfs.h>
  144. #endif
  145. #if defined(HAVE_SYS_TIMEX_H)
  146. #include <sys/timex.h>
  147. #endif
  148. #if defined(HAVE_SYS_SYSINFO_H)
  149. #include <sys/sysinfo.h>
  150. #endif
  151. #if defined(HAVE_UTIME_H)
  152. #include <utime.h>
  153. #endif
  154. #if defined(HAVE_LINUX_ETHER_H)
  155. #include <linux/ether.h>
  156. #endif
  157. #if defined(HAVE_LINUX_FS_H)
  158. #include <linux/fs.h>
  159. #endif
  160. #if defined(HAVE_LINUX_REBOOT_H)
  161. #include <linux/reboot.h>
  162. #endif
  163. #if defined(HAVE_SYS_INOTIFY_H)
  164. #include <sys/inotify.h>
  165. #endif
  166. #if defined(HAVE_NETINET_ICMP6_H)
  167. #include <netinet/icmp6.h>
  168. #endif
  169. #if defined(HAVE_SCHED_H)
  170. #include <sched.h>
  171. #endif
  172. #if defined(HAVE_SEMAPHORE_H)
  173. #include <semaphore.h>
  174. #endif
  175. #if defined(HAVE_PORT_H)
  176. #include <port.h>
  177. #endif
  178. #if defined(HAVE_LWP_H)
  179. #include <lwp.h>
  180. #endif
  181. #ifdef USE_LIBFFI
  182. #include "ffi.h"
  183. #endif
  184. /* Constants that may only be defined as expressions on some systems,
  185. expressions too complex for -fdump-go-spec to handle. These are
  186. handled specially below. */
  187. enum {
  188. #ifdef TIOCGWINSZ
  189. TIOCGWINSZ_val = TIOCGWINSZ,
  190. #endif
  191. #ifdef TIOCSWINSZ
  192. TIOCSWINSZ_val = TIOCSWINSZ,
  193. #endif
  194. #ifdef TIOCNOTTY
  195. TIOCNOTTY_val = TIOCNOTTY,
  196. #endif
  197. #ifdef TIOCSCTTY
  198. TIOCSCTTY_val = TIOCSCTTY,
  199. #endif
  200. #ifdef TIOCGPGRP
  201. TIOCGPGRP_val = TIOCGPGRP,
  202. #endif
  203. #ifdef TIOCSPGRP
  204. TIOCSPGRP_val = TIOCSPGRP,
  205. #endif
  206. #ifdef TIOCGPTN
  207. TIOCGPTN_val = TIOCGPTN,
  208. #endif
  209. #ifdef TIOCSPTLCK
  210. TIOCSPTLCK_val = TIOCSPTLCK,
  211. #endif
  212. #ifdef TIOCGDEV
  213. TIOCGDEV_val = TIOCGDEV,
  214. #endif
  215. #ifdef TIOCSIG
  216. TIOCSIG_val = TIOCSIG,
  217. #endif
  218. #ifdef TCGETS
  219. TCGETS_val = TCGETS,
  220. #endif
  221. #ifdef TCSETS
  222. TCSETS_val = TCSETS,
  223. #endif
  224. #ifdef TUNSETIFF
  225. TUNSETIFF_val = TUNSETIFF,
  226. #endif
  227. #ifdef TUNSETNOCSUM
  228. TUNSETNOCSUM_val = TUNSETNOCSUM,
  229. #endif
  230. #ifdef TUNSETDEBUG
  231. TUNSETDEBUG_val = TUNSETDEBUG,
  232. #endif
  233. #ifdef TUNSETPERSIST
  234. TUNSETPERSIST_val = TUNSETPERSIST,
  235. #endif
  236. #ifdef TUNSETOWNER
  237. TUNSETOWNER_val = TUNSETOWNER,
  238. #endif
  239. #ifdef TUNSETLINK
  240. TUNSETLINK_val = TUNSETLINK,
  241. #endif
  242. #ifdef TUNSETGROUP
  243. TUNSETGROUP_val = TUNSETGROUP,
  244. #endif
  245. #ifdef TUNGETFEATURES
  246. TUNGETFEATURES_val = TUNGETFEATURES,
  247. #endif
  248. #ifdef TUNSETOFFLOAD
  249. TUNSETOFFLOAD_val = TUNSETOFFLOAD,
  250. #endif
  251. #ifdef TUNSETTXFILTER
  252. TUNSETTXFILTER_val = TUNSETTXFILTER,
  253. #endif
  254. #ifdef TUNGETIFF
  255. TUNGETIFF_val = TUNGETIFF,
  256. #endif
  257. #ifdef TUNGETSNDBUF
  258. TUNGETSNDBUF_val = TUNGETSNDBUF,
  259. #endif
  260. #ifdef TUNSETSNDBUF
  261. TUNSETSNDBUF_val = TUNSETSNDBUF,
  262. #endif
  263. #ifdef TUNATTACHFILTER
  264. TUNATTACHFILTER_val = TUNATTACHFILTER,
  265. #endif
  266. #ifdef TUNDETACHFILTER
  267. TUNDETACHFILTER_val = TUNDETACHFILTER,
  268. #endif
  269. #ifdef TUNGETVNETHDRSZ
  270. TUNGETVNETHDRSZ_val = TUNGETVNETHDRSZ,
  271. #endif
  272. #ifdef TUNSETVNETHDRSZ
  273. TUNSETVNETHDRSZ_val = TUNSETVNETHDRSZ,
  274. #endif
  275. #ifdef TUNSETQUEUE
  276. TUNSETQUEUE_val = TUNSETQUEUE,
  277. #endif
  278. #ifdef TUNSETIFINDEX
  279. TUNSETIFINDEX_val = TUNSETIFINDEX,
  280. #endif
  281. #ifdef TUNGETFILTER
  282. TUNGETFILTER_val = TUNGETFILTER,
  283. #endif
  284. #ifdef NLA_HDRLEN
  285. NLA_HDRLEN_val = NLA_HDRLEN,
  286. #endif
  287. #ifdef BIOCFLUSH
  288. BIOCFLUSH_val = BIOCFLUSH,
  289. #endif
  290. #ifdef BIOCGBLEN
  291. BIOCGBLEN_val = BIOCGBLEN,
  292. #endif
  293. #ifdef BIOCGDLT
  294. BIOCGDLT_val = BIOCGDLT,
  295. #endif
  296. #ifdef BIOCGETIF
  297. BIOCGETIF_val = BIOCGETIF,
  298. #endif
  299. #ifdef BIOCGHDRCMPLT
  300. BIOCGHDRCMPLT_val = BIOCGHDRCMPLT,
  301. #endif
  302. #ifdef BIOCGRTIMEOUT
  303. BIOCGRTIMEOUT_val = BIOCGRTIMEOUT,
  304. #endif
  305. #ifdef BIOCGSTATS
  306. BIOCGSTATS_val = BIOCGSTATS,
  307. #endif
  308. #ifdef BIOCIMMEDIATE
  309. BIOCIMMEDIATE_val = BIOCIMMEDIATE,
  310. #endif
  311. #ifdef BIOCPROMISC
  312. BIOCPROMISC_val = BIOCPROMISC,
  313. #endif
  314. #ifdef BIOCSBLEN
  315. BIOCSBLEN_val = BIOCSBLEN,
  316. #endif
  317. #ifdef BIOCSDLT
  318. BIOCSDLT_val = BIOCSDLT,
  319. #endif
  320. #ifdef BIOCSETF
  321. BIOCSETF_val = BIOCSETF,
  322. #endif
  323. #ifdef BIOCSETIF
  324. BIOCSETIF_val = BIOCSETIF,
  325. #endif
  326. #ifdef BIOCSHDRCMPLT
  327. BIOCSHDRCMPLT_val = BIOCSHDRCMPLT,
  328. #endif
  329. #ifdef BIOCSRTIMEOUT
  330. BIOCSRTIMEOUT_val = BIOCSRTIMEOUT,
  331. #endif
  332. #ifdef BIOCVERSION
  333. BIOCVERSION_val = BIOCVERSION,
  334. #endif
  335. #ifdef SO_RCVTIMEO
  336. SO_RCVTIMEO_val = SO_RCVTIMEO,
  337. #endif
  338. };
  339. // SIOCGIFMTU can't be added in the above enum as it might
  340. // be signed in some OSes.
  341. #ifdef SIOCGIFMTU
  342. enum {
  343. SIOCGIFMTU_val = SIOCGIFMTU,
  344. };
  345. #endif
  346. #if defined(HAVE_SYS_EPOLL_H)
  347. enum {
  348. epoll_data_offset = offsetof(struct epoll_event, data)
  349. };
  350. #endif
  351. // The following section introduces explicit references to types and
  352. // constants of interest to support bootstrapping libgo using a
  353. // compiler that doesn't support -fdump-go-spec (e.g., clang), via
  354. // DWARF-based tools. This process is made more difficult due to the
  355. // fact that clang tries hard to omit types/constants from DWARF if it
  356. // can't find explicit references to them, so here we make sure that
  357. // key items are mentioned in ways that will force them into the
  358. // generated DWARF.
  359. #if defined(__clang__)
  360. // Make a reference to a type
  361. #define TREF(typ) typ typ ## ref
  362. // Make a reference to an opaque type
  363. #define OTREF(typ) typ *typ ## ref
  364. // Make a reference to a struct tag
  365. #define SREF(stag) struct stag stag ## ref
  366. // Make a reference to an enum literal
  367. #define EREF(elit) unsigned elit ## fn(unsigned x) { return x == elit ? 1 : 0; }
  368. //......................................................................
  369. // From dirent.h
  370. SREF(dirent);
  371. SREF(dirent64);
  372. OTREF(DIR);
  373. EREF(DT_UNKNOWN);
  374. // From fcntl.h
  375. SREF(flock);
  376. SREF(flock64);
  377. // From ffi headers
  378. SREF(_ffi_type);
  379. TREF(ffi_cif);
  380. TREF(ffi_abi);
  381. TREF(ffi_status);
  382. EREF(FFI_OK);
  383. // From grp.h
  384. SREF(group);
  385. #if defined(HAVE_LINUX_FILTER_H)
  386. // From linux/filter.h
  387. SREF(sock_filter);
  388. SREF(sock_fprog);
  389. #endif
  390. // From linux/if.h
  391. EREF(IFF_UP);
  392. #if defined(HAVE_LINUX_IF_ADDR_H)
  393. // From linux/if_addr.h
  394. SREF(ifaddrmsg);
  395. EREF(IFA_ADDRESS);
  396. #endif
  397. #if defined(HAVE_LINUX_RTNETLINK_H)
  398. // From linux/if_link.h
  399. EREF(IFLA_ADDRESS);
  400. #endif
  401. // From in.h, in6.h, icmp6.h
  402. SREF(ip_mreq);
  403. SREF(ip_mreqn);
  404. SREF(ipv6_mreq);
  405. SREF(ip6_mtuinfo);
  406. SREF(icmp6_filter);
  407. SREF(in_pktinfo);
  408. EREF(IPPROTO_TCP);
  409. #if defined(HAVE_LINUX_RTNETLINK_H)
  410. // From linux/rtnetlink.h
  411. SREF(rtgenmsg);
  412. SREF(rtmsg);
  413. SREF(ifinfomsg);
  414. SREF(rtattr);
  415. SREF(rtnexthop);
  416. EREF(RTM_BASE);
  417. EREF(RTN_UNSPEC);
  418. #endif
  419. // From netdb.h
  420. SREF(addrinfo);
  421. // From netlink.h
  422. SREF(nlattr);
  423. SREF(nlmsgerr);
  424. // From pthread.h and related
  425. TREF(pthread_attr_t);
  426. TREF(pthread_t);
  427. TREF(pthread_mutex_t);
  428. TREF(pthread_mutexattr_t);
  429. // From pwd.h
  430. SREF(passwd);
  431. // From signal.h and related
  432. TREF(sigset_t);
  433. TREF(siginfo_t);
  434. TREF(stack_t);
  435. SREF(sigaction);
  436. SREF(sigstack);
  437. EREF(SI_USER);
  438. EREF(FPE_INTOVF);
  439. EREF(BUS_ADRALN);
  440. EREF(SS_ONSTACK);
  441. EREF(SEGV_MAPERR);
  442. // From stat.h
  443. SREF(stat64);
  444. // From statfs.h
  445. SREF(statfs);
  446. SREF(statfs64);
  447. // From sysinfo.h
  448. SREF(sysinfo);
  449. // From <sys/epoll.h>
  450. #if defined(HAVE_SYS_EPOLL_H)
  451. SREF(epoll_event);
  452. EREF(EPOLLIN);
  453. EREF(epoll_data_offset);
  454. #endif
  455. #if defined(HAVE_SYS_MOUNT_H)
  456. // From sys/mount.h
  457. EREF(MS_PRIVATE);
  458. EREF(MNT_FORCE);
  459. #endif
  460. #if defined(HAVE_SYS_PTRACE_H)
  461. // From <sys/ptrace.h>
  462. #if defined (__aarch64__)
  463. SREF(user_pt_regs);
  464. #else
  465. SREF(pt_regs);
  466. #endif
  467. EREF(PTRACE_PEEKTEXT);
  468. #endif
  469. // From sys/resource.h
  470. SREF(rusage);
  471. SREF(rlimit64);
  472. EREF(RLIMIT_NOFILE);
  473. EREF(PRIO_USER);
  474. EREF(RUSAGE_SELF);
  475. // From sys/select.h
  476. TREF(fd_set);
  477. // From sys/socket.h
  478. SREF(msghdr);
  479. SREF(cmsghdr);
  480. SREF(ucred);
  481. EREF(MSG_OOB);
  482. EREF(SCM_RIGHTS);
  483. EREF(SOCK_RAW);
  484. EREF(SHUT_RD);
  485. // From sys/time.h and sys/times.h
  486. SREF(timespec);
  487. SREF(timeval);
  488. SREF(itimerval);
  489. SREF(tms);
  490. EREF(ITIMER_PROF);
  491. #if defined(HAVE_SYS_TIMEX_H)
  492. // From sys/timex.h
  493. SREF(timex);
  494. #endif
  495. // From sys/types.h
  496. TREF(pid_t);
  497. TREF(off_t);
  498. TREF(loff_t);
  499. TREF(size_t);
  500. TREF(ssize_t);
  501. TREF(mode_t);
  502. TREF(dev_t);
  503. TREF(time_t);
  504. // From sys/ucontext.h
  505. TREF(ucontext_t);
  506. #if defined(HAVE_SYS_USER_H)
  507. // From sys/user.h
  508. SREF(user_regs_struct);
  509. #endif
  510. #if defined(HAVE_SYS_UTSNAME_H)
  511. // From sys/utsname.h
  512. SREF(utsname);
  513. #endif
  514. // From termios.h
  515. SREF(termios);
  516. // From uio.h
  517. SREF(iovec);
  518. // From utime.h
  519. SREF(utimbuf);
  520. // From unistd.h
  521. EREF(_PC_NAME_MAX);
  522. EREF(_SC_GETPW_R_SIZE_MAX);
  523. #endif // clang