Skip to content

Commit

Permalink
libgo: export NetBSD-specific types in mksysinfo.sh
Browse files Browse the repository at this point in the history
The syscall package depends on many NetBSD-specific types on NetBSD.
Teach mksysinfo.sh to export these types.

This alone is not sufficient to get the syscall package to compile on
NetBSD, but it's a start.

Note that the IfMsgHdr type is recapitalized to IfMsghdr, which requires
changes in the AIX port. The new capitalization is what's used by
upstream in existing NetBSD-specific code and is more consistent with
the capitalization of other C structs with the "hdr" suffix.

Updates golang/go#38538.

Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/261739
  • Loading branch information
benesch authored and ianlancetaylor committed Oct 14, 2020
1 parent ca56d57 commit dc57070
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gcc/go/gofrontend/MERGE
@@ -1,4 +1,4 @@
c5505c4e626fa4217911443b4db8b065855a0206
cc1f7d613f9b0666bbf8aac3dd208d5adfe88546

The first line of this file holds the git revision number of the last
merge done from the gofrontend repository.
6 changes: 6 additions & 0 deletions libgo/config.h.in
Expand Up @@ -174,6 +174,9 @@
/* Define to 1 if you have the <netpacket/packet.h> header file. */
#undef HAVE_NETPACKET_PACKET_H

/* Define to 1 if you have the <net/bpf.h> header file. */
#undef HAVE_NET_BPF_H

/* Define to 1 if you have the <net/if_arp.h> header file. */
#undef HAVE_NET_IF_ARP_H

Expand Down Expand Up @@ -294,6 +297,9 @@
/* Define to 1 if you have the <sys/syscall.h> header file. */
#undef HAVE_SYS_SYSCALL_H

/* Define to 1 if you have the <sys/sysctl.h> header file. */
#undef HAVE_SYS_SYSCTL_H

/* Define to 1 if you have the <sys/sysinfo.h> header file. */
#undef HAVE_SYS_SYSINFO_H

Expand Down
2 changes: 1 addition & 1 deletion libgo/configure
Expand Up @@ -15202,7 +15202,7 @@ $as_echo "#define HAVE_GETIPINFO 1" >>confdefs.h
fi


for ac_header in port.h sched.h semaphore.h sys/file.h sys/mman.h syscall.h sys/epoll.h sys/event.h sys/inotify.h sys/ptrace.h sys/syscall.h sys/user.h sys/utsname.h sys/select.h sys/socket.h net/if.h net/if_arp.h net/route.h netpacket/packet.h sys/prctl.h sys/mount.h sys/vfs.h sys/statfs.h sys/timex.h sys/sysinfo.h utime.h linux/ether.h linux/fs.h linux/ptrace.h linux/reboot.h netinet/in_syst.h netinet/ip.h netinet/ip_mroute.h netinet/if_ether.h
for ac_header in port.h sched.h semaphore.h sys/file.h sys/mman.h syscall.h sys/epoll.h sys/event.h sys/inotify.h sys/ptrace.h sys/syscall.h sys/sysctl.h sys/user.h sys/utsname.h sys/select.h sys/socket.h net/bpf.h net/if.h net/if_arp.h net/route.h netpacket/packet.h sys/prctl.h sys/mount.h sys/vfs.h sys/statfs.h sys/timex.h sys/sysinfo.h utime.h linux/ether.h linux/fs.h linux/ptrace.h linux/reboot.h netinet/in_syst.h netinet/ip.h netinet/ip_mroute.h netinet/if_ether.h
do :
as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"
Expand Down
2 changes: 1 addition & 1 deletion libgo/configure.ac
Expand Up @@ -580,7 +580,7 @@ AC_C_BIGENDIAN

GCC_CHECK_UNWIND_GETIPINFO

AC_CHECK_HEADERS(port.h sched.h semaphore.h sys/file.h sys/mman.h syscall.h sys/epoll.h sys/event.h sys/inotify.h sys/ptrace.h sys/syscall.h sys/user.h sys/utsname.h sys/select.h sys/socket.h net/if.h net/if_arp.h net/route.h netpacket/packet.h sys/prctl.h sys/mount.h sys/vfs.h sys/statfs.h sys/timex.h sys/sysinfo.h utime.h linux/ether.h linux/fs.h linux/ptrace.h linux/reboot.h netinet/in_syst.h netinet/ip.h netinet/ip_mroute.h netinet/if_ether.h)
AC_CHECK_HEADERS(port.h sched.h semaphore.h sys/file.h sys/mman.h syscall.h sys/epoll.h sys/event.h sys/inotify.h sys/ptrace.h sys/syscall.h sys/sysctl.h sys/user.h sys/utsname.h sys/select.h sys/socket.h net/bpf.h net/if.h net/if_arp.h net/route.h netpacket/packet.h sys/prctl.h sys/mount.h sys/vfs.h sys/statfs.h sys/timex.h sys/sysinfo.h utime.h linux/ether.h linux/fs.h linux/ptrace.h linux/reboot.h netinet/in_syst.h netinet/ip.h netinet/ip_mroute.h netinet/if_ether.h)

AC_CHECK_HEADERS([netinet/icmp6.h], [], [],
[#include <netinet/in.h>
Expand Down
7 changes: 0 additions & 7 deletions libgo/go/runtime/os_netbsd.go
Expand Up @@ -33,13 +33,6 @@ func lwp_unpark(lwp int32, hint unsafe.Pointer) int32
//extern sysctl
func sysctl(*uint32, uint32, *byte, *uintptr, *byte, uintptr) int32

// From NetBSD's <sys/sysctl.h>
const (
_CTL_HW = 6
_HW_NCPU = 3
_HW_PAGESIZE = 7
)

func getncpu() int32 {
mib := [2]uint32{_CTL_HW, _HW_NCPU}
out := uint32(0)
Expand Down
139 changes: 133 additions & 6 deletions libgo/mksysinfo.sh
Expand Up @@ -225,6 +225,22 @@ if ! grep '^const _AT_FDCWD = ' ${OUT} >/dev/null 2>&1; then
echo "const _AT_FDCWD = -100" >> ${OUT}
fi

# sysctl constants.
grep '^const _CTL' gen-sysinfo.go |
sed -e 's/^\(const \)_\(CTL[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
grep '^const _SYSCTL' gen-sysinfo.go |
sed -e 's/^\(const \)_\(SYSCTL[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
grep '^const _NET_RT' gen-sysinfo.go |
sed -e 's/^\(const \)_\(NET_RT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}

# The sysctlnode struct.
grep '^type _sysctlnode ' gen-sysinfo.go | \
sed -e 's/_sysctlnode/Sysctlnode/' \
-e 's/sysctl_flags/Flags/' \
-e 's/sysctl_name/Name/' \
-e 's/sysctl_num/Num/' \
>> ${OUT}

# sysconf constants.
grep '^const __SC' gen-sysinfo.go |
sed -e 's/^\(const \)__\(SC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
Expand Down Expand Up @@ -533,6 +549,7 @@ fi | sed -e 's/type _dirent64/type Dirent/' \
-e 's/d_name \[0+1\]/d_name [0+256]/' \
-e 's/d_name/Name/' \
-e 's/]int8/]byte/' \
-e 's/d_fileno/Fileno/' \
-e 's/d_ino/Ino/' \
-e 's/d_namlen/Namlen/' \
-e 's/d_off/Off/' \
Expand Down Expand Up @@ -994,6 +1011,39 @@ grep '^type _rtgenmsg ' gen-sysinfo.go | \
-e 's/rtgen_family/Family/' \
>> ${OUT}

# The rt_msghdr struct.
grep '^type _rt_msghdr ' gen-sysinfo.go | \
sed -e 's/_rt_msghdr/RtMsghdr/g' \
-e 's/rtm_msglen/Msglen/' \
-e 's/rtm_version/Version/' \
-e 's/rtm_type/Type/' \
-e 's/rtm_index/Index/' \
-e 's/rtm_flags/Flags/' \
-e 's/rtm_addrs/Addrs/' \
-e 's/rtm_pid/Pid/' \
-e 's/rtm_seq/Seq/' \
-e 's/rtm_errno/Errno/' \
-e 's/rtm_use/Use/' \
-e 's/rtm_inits/Inits/' \
-e 's/rtm_rmx/Rmx/' \
-e 's/_rt_metrics/RtMetrics/' \
>> ${OUT}

# The rt_metrics struct.
grep '^type _rt_metrics ' gen-sysinfo.go | \
sed -e 's/_rt_metrics/RtMetrics/g' \
-e 's/rmx_locks/Locks/' \
-e 's/rmx_mtu/Mtu/' \
-e 's/rmx_hopcount/Hopcount/' \
-e 's/rmx_recvpipe/Recvpipe/' \
-e 's/rmx_sendpipe/Sendpipe/' \
-e 's/rmx_ssthresh/Ssthresh/' \
-e 's/rmx_rtt/Rtt/' \
-e 's/rmx_rttvar/Rttvar/' \
-e 's/rmx_expire/Expire/' \
-e 's/rmx_pksent/Pksent/' \
>> ${OUT}

# The routing message flags.
grep '^const _RT_' gen-sysinfo.go | \
sed -e 's/^\(const \)_\(RT_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
Expand All @@ -1020,9 +1070,14 @@ grep '^type _ifinfomsg ' gen-sysinfo.go | \
-e 's/ifi_change/Change/' \
>> ${OUT}

# The if_msghdr struct.
# The if_msghdr struct. Upstream uses inconsistent capitalization for this type
# on AIX, so we do too.
ifmsghdr_name=IfMsghdr
if test "${GOOS}" = "aix"; then
ifmsghdr_name=IfMsgHdr
fi
grep '^type _if_msghdr ' gen-sysinfo.go | \
sed -e 's/_if_msghdr/IfMsgHdr/' \
sed -e "s/_if_msghdr/${ifmsghdr_name}/" \
-e 's/ifm_msglen/Msglen/' \
-e 's/ifm_version/Version/' \
-e 's/ifm_type/Type/' \
Expand All @@ -1032,6 +1087,17 @@ grep '^type _if_msghdr ' gen-sysinfo.go | \
-e 's/ifm_addrlen/Addrlen/' \
>> ${OUT}

# The if_announcemsghdr struct.
grep '^type _if_announcemsghdr ' gen-sysinfo.go | \
sed -e 's/_if_announcemsghdr/IfAnnounceMsghdr/g' \
-e 's/ifan_msglen/Msglen/' \
-e 's/ifan_version/Version/' \
-e 's/ifan_type/Type/' \
-e 's/ifan_index/Index/' \
-e 's/ifan_name/Name/' \
-e 's/ifan_what/What/' \
>> ${OUT}

# The interface information types and flags.
grep '^const _IFA' gen-sysinfo.go | \
sed -e 's/^\(const \)_\(IFA[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
Expand Down Expand Up @@ -1061,13 +1127,73 @@ grep '^type _ifaddrmsg ' gen-sysinfo.go | \
-e 's/ifa_index/Index/' \
>> ${OUT}

# The ifa_msghdr struct.
grep '^type _ifa_msghdr ' gen-sysinfo.go | \
sed -e 's/_ifa_msghdr/IfaMsghdr/g' \
-e 's/ifam_msglen/Msglen/' \
-e 's/ifam_version/Version/' \
-e 's/ifam_type/Type/' \
-e 's/ifam_addrs/Addrs/' \
-e 's/ifam_flags/Flags/' \
-e 's/ifam_metric/Metric/' \
-e 's/ifam_index/Index/' \
>> ${OUT}

# The rtattr struct.
grep '^type _rtattr ' gen-sysinfo.go | \
sed -e 's/_rtattr/RtAttr/' \
-e 's/rta_len/Len/' \
-e 's/rta_type/Type/' \
>> ${OUT}

# The bpf_version struct.
grep '^type _bpf_version ' gen-sysinfo.go | \
sed -e 's/_bpf_version/BpfVersion/g' \
-e 's/bv_major/Major/' \
-e 's/bv_minor/Minor/' \
>> ${OUT}

# The bpf_stat struct.
grep '^type _bpf_stat ' gen-sysinfo.go | \
sed -e 's/_bpf_stat/BpfStat/g' \
-e 's/bs_recv/Recv/' \
-e 's/bs_drop/Drop/' \
-e 's/bs_capt/Capt/' \
-e 's/bs_padding/Padding/' \
>> ${OUT}

# The bpf_insn struct.
grep '^type _bpf_insn ' gen-sysinfo.go | \
sed -e 's/_bpf_insn/BpfInsn/g' \
-e 's/code/Code/' \
-e 's/jt/Jt/' \
-e 's/jf/Jf/' \
-e 's/k/K/' \
>> ${OUT}

# The bpf_program struct.
grep '^type _bpf_program ' gen-sysinfo.go | \
sed -e 's/_bpf_program/BpfProgram/g' \
-e 's/bf_len/Len/' \
-e 's/bf_insns/Insns/' \
-e 's/_bpf_insn/BpfInsn/' \
>> ${OUT}

# The BPF ioctl constants.
grep '^const _BIOC' gen-sysinfo.go | \
grep -v '_val =' | \
sed -e 's/^\(const \)_\(BIOC[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
for c in BIOCFLUSH BIOCGBLEN BIOCGDLT BIOCGETIF BIOCGHDRCMPLT BIOCGRTIMEOUT \
BIOCGSTATS BIOCIMMEDIATE BIOCPROMISC BIOCSBLEN BIOCSDLT BIOCSETF \
BIOCSETIF BIOCSHDRCMPLT BIOCSRTIMEOUT BIOCVERSION
do
if ! grep "^const ${c}" ${OUT} >/dev/null 2>&1; then
if grep "^const _${c}_val" ${OUT} >/dev/null 2>&1; then
echo "const ${c} = _${c}_val" >> ${OUT}
fi
fi
done

# The in_pktinfo struct.
grep '^type _in_pktinfo ' gen-sysinfo.go | \
sed -e 's/_in_pktinfo/Inet4Pktinfo/' \
Expand Down Expand Up @@ -1344,10 +1470,11 @@ fi

# Struct sizes.
set cmsghdr Cmsghdr ip_mreq IPMreq ip_mreqn IPMreqn ipv6_mreq IPv6Mreq \
ifaddrmsg IfAddrmsg ifinfomsg IfInfomsg in_pktinfo Inet4Pktinfo \
in6_pktinfo Inet6Pktinfo inotify_event InotifyEvent linger Linger \
msghdr Msghdr nlattr NlAttr nlmsgerr NlMsgerr nlmsghdr NlMsghdr \
rtattr RtAttr rtgenmsg RtGenmsg rtmsg RtMsg rtnexthop RtNexthop \
ifaddrmsg IfAddrmsg ifa_msghdr IfaMsghdr ifinfomsg IfInfomsg \
if_msghdr IfMsghdr in_pktinfo Inet4Pktinfo in6_pktinfo Inet6Pktinfo \
inotify_event InotifyEvent linger Linger msghdr Msghdr nlattr NlAttr \
nlmsgerr NlMsgerr nlmsghdr NlMsghdr rtattr RtAttr rt_msghdr RtMsghdr \
rtgenmsg RtGenmsg rtmsg RtMsg rtnexthop RtNexthop \
sock_filter SockFilter sock_fprog SockFprog ucred Ucred \
icmp6_filter ICMPv6Filter ip6_mtuinfo IPv6MTUInfo
while test $# != 0; do
Expand Down
54 changes: 54 additions & 0 deletions libgo/sysinfo.c
Expand Up @@ -47,6 +47,9 @@
#if defined(HAVE_SYS_SYSCALL_H)
#include <sys/syscall.h>
#endif
#if defined(HAVE_SYS_SYSCTL_H)
#include <sys/sysctl.h>
#endif
#if defined(HAVE_SYS_EPOLL_H)
#include <sys/epoll.h>
#endif
Expand Down Expand Up @@ -117,6 +120,9 @@
#if defined(HAVE_LINUX_RTNETLINK_H)
#include <linux/rtnetlink.h>
#endif
#if defined(HAVE_NET_BPF_H)
#include <net/bpf.h>
#endif
#if defined(HAVE_NET_IF_H)
#include <net/if.h>
#endif
Expand Down Expand Up @@ -279,6 +285,54 @@ enum {
#ifdef NLA_HDRLEN
NLA_HDRLEN_val = NLA_HDRLEN,
#endif
#ifdef BIOCFLUSH
BIOCFLUSH_val = BIOCFLUSH,
#endif
#ifdef BIOCGBLEN
BIOCGBLEN_val = BIOCGBLEN,
#endif
#ifdef BIOCGDLT
BIOCGDLT_val = BIOCGDLT,
#endif
#ifdef BIOCGETIF
BIOCGETIF_val = BIOCGETIF,
#endif
#ifdef BIOCGHDRCMPLT
BIOCGHDRCMPLT_val = BIOCGHDRCMPLT,
#endif
#ifdef BIOCGRTIMEOUT
BIOCGRTIMEOUT_val = BIOCGRTIMEOUT,
#endif
#ifdef BIOCGSTATS
BIOCGSTATS_val = BIOCGSTATS,
#endif
#ifdef BIOCIMMEDIATE
BIOCIMMEDIATE_val = BIOCIMMEDIATE,
#endif
#ifdef BIOCPROMISC
BIOCPROMISC_val = BIOCPROMISC,
#endif
#ifdef BIOCSBLEN
BIOCSBLEN_val = BIOCSBLEN,
#endif
#ifdef BIOCSDLT
BIOCSDLT_val = BIOCSDLT,
#endif
#ifdef BIOCSETF
BIOCSETF_val = BIOCSETF,
#endif
#ifdef BIOCSETIF
BIOCSETIF_val = BIOCSETIF,
#endif
#ifdef BIOCSHDRCMPLT
BIOCSHDRCMPLT_val = BIOCSHDRCMPLT,
#endif
#ifdef BIOCSRTIMEOUT
BIOCSRTIMEOUT_val = BIOCSRTIMEOUT,
#endif
#ifdef BIOCVERSION
BIOCVERSION_val = BIOCVERSION,
#endif
};

// SIOCGIFMTU can't be added in the above enum as it might
Expand Down

0 comments on commit dc57070

Please sign in to comment.