Skip to content

Commit

Permalink
Port Native libraries to SunOS (#34867)
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 committed Apr 14, 2020
1 parent 7ac8db8 commit b88be15
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal enum UnixFileSystemTypes : long
bdevfs = 0x62646576,
bfs = 0x1BADFACE,
binfmt_misc = 0x42494E4D,
bootfs = 0xA56D3FF9,
btrfs = 0x9123683E,
ceph = 0x00C36400,
cgroupfs = 0x0027E0EB,
Expand All @@ -65,6 +66,7 @@ internal enum UnixFileSystemTypes : long
ext3 = 0xEF53,
ext4 = 0xEF53,
fat = 0x4006,
fd = 0xF00D1E,
fhgfs = 0x19830326,
fuse = 0x65735546,
fuseblk = 0x65735546,
Expand All @@ -86,6 +88,7 @@ internal enum UnixFileSystemTypes : long
jffs2 = 0x72B6,
jfs = 0x3153464A,
kafs = 0x6B414653,
lofs = 0xEF53, /* loopback filesystem, magic same as ext2 */
logfs = 0xC97E8168,
lustre = 0x0BD00BD0,
minix_old = 0x137F, /* orig. minix */
Expand Down Expand Up @@ -227,6 +230,7 @@ private static DriveType GetDriveType(string fileSystemName)
case "bdevfs":
case "befs":
case "bfs":
case "bootfs":
case "bpf_fs":
case "btrfs":
case "btrfs_test":
Expand Down Expand Up @@ -261,6 +265,7 @@ private static DriveType GetDriveType(string fileSystemName)
case "jffs":
case "jffs2":
case "jfs":
case "lofs":
case "logfs":
case "lxfs":
case "minix (30 char.)":
Expand Down Expand Up @@ -384,25 +389,30 @@ private static DriveType GetDriveType(string fileSystemName)
case "cgroupfs":
case "cgroup2fs":
case "configfs":
case "cpuset":
case "cramfs":
case "cramfs-wend":
case "cryptkeeper":
case "cpuset":
case "ctfs":
case "debugfs":
case "dev":
case "devfs":
case "devpts":
case "devtmpfs":
case "encfs":
case "fd":
case "fdesc":
case "fuse.gvfsd-fuse":
case "fusectl":
case "futexfs":
case "hugetlbfs":
case "libpam-encfs":
case "ibpam-mount":
case "mntfs":
case "mqueue":
case "mtpfs":
case "mythtvfs":
case "mqueue":
case "objfs":
case "openprom":
case "openpromfs":
case "pipefs":
Expand All @@ -417,6 +427,7 @@ private static DriveType GetDriveType(string fileSystemName)
case "securityfs":
case "selinux":
case "selinuxfs":
case "sharefs":
case "sockfs":
case "sysfs":
case "tmpfs":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,6 @@ internal enum SysLogPriority : int
LOG_NOTICE = 5, /* normal but significant condition */
LOG_INFO = 6, /* informational */
LOG_DEBUG = 7, /* debug-level messages */
// Facilities
LOG_KERN = (0<<3), /* kernel messages */
LOG_USER = (1<<3), /* random user-level messages */
LOG_MAIL = (2<<3), /* mail system */
LOG_DAEMON = (3<<3), /* system daemons */
LOG_AUTH = (4<<3), /* authorization messages */
LOG_SYSLOG = (5<<3), /* messages generated internally by syslogd */
LOG_LPR = (6<<3), /* line printer subsystem */
LOG_NEWS = (7<<3), /* network news subsystem */
LOG_UUCP = (8<<3), /* UUCP subsystem */
LOG_CRON = (9<<3), /* clock daemon */
LOG_AUTHPRIV = (10<<3), /* authorization messages (private) */
LOG_FTP = (11<<3), /* ftp daemon */
// Between FTP and Local is reserved for system use
LOG_LOCAL0 = (16<<3), /* reserved for local use */
LOG_LOCAL1 = (17<<3), /* reserved for local use */
LOG_LOCAL2 = (18<<3), /* reserved for local use */
LOG_LOCAL3 = (19<<3), /* reserved for local use */
LOG_LOCAL4 = (20<<3), /* reserved for local use */
LOG_LOCAL5 = (21<<3), /* reserved for local use */
LOG_LOCAL6 = (22<<3), /* reserved for local use */
LOG_LOCAL7 = (23<<3), /* reserved for local use */
}

/// <summary>
Expand Down
8 changes: 7 additions & 1 deletion src/libraries/Native/Unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,13 @@ if(CLR_CMAKE_TARGET_UNIX)
add_link_options(-Wl,-bind_at_load)
else()
add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-Wa,--noexecstack>)
add_link_options(-Wl,--build-id=sha1 -Wl,-z,relro,-z,now)
if(CLR_CMAKE_TARGET_SUNOS)
add_definitions(-D__EXTENSIONS__ -D_XPG4_2 -D_POSIX_PTHREAD_SEMANTICS)
else()
# -z,now is required for full relro.
# see https://www.redhat.com/en/blog/hardening-elf-binaries-using-relocation-read-only-relro
add_link_options(-Wl,--build-id=sha1 -Wl,-z,relro,-z,now)
endif()
endif()
endif()

Expand Down
9 changes: 9 additions & 0 deletions src/libraries/Native/Unix/Common/pal_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#cmakedefine01 HAVE_MNTINFO
#cmakedefine01 HAVE_STATFS_FSTYPENAME
#cmakedefine01 HAVE_STATVFS_FSTYPENAME
#cmakedefine01 HAVE_NON_LEGACY_STATFS
#cmakedefine01 HAVE_STRCPY_S
#cmakedefine01 HAVE_STRLCPY
#cmakedefine01 HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP
Expand Down Expand Up @@ -72,6 +73,11 @@
#cmakedefine01 HAVE_RT_MSGHDR2
#cmakedefine01 HAVE_IF_MSGHDR2
#cmakedefine01 HAVE_SYS_SYSCTL_H
#cmakedefine01 HAVE_SYS_IOCTL_H
#cmakedefine01 HAVE_SYS_FILIO_H
#cmakedefine01 HAVE_NETPACKET_PACKET_H
#cmakedefine01 HAVE_NET_IF_ARP_H
#cmakedefine01 HAVE_SYS_MNTENT_H
#cmakedefine01 HAVE_NET_IFMEDIA_H
#cmakedefine01 HAVE_LINUX_RTNETLINK_H
#cmakedefine01 HAVE_LINUX_CAN_H
Expand Down Expand Up @@ -102,6 +108,9 @@
#cmakedefine01 HAVE_TCP_H_TCP_KEEPALIVE
#cmakedefine01 HAVE_BUILTIN_MUL_OVERFLOW
#cmakedefine01 HAVE_DISCONNECTX
#cmakedefine01 HAVE_CFSETSPEED
#cmakedefine01 HAVE_CFMAKERAW
#cmakedefine01 HAVE_GETGROUPLIST

// Mac OS X has stat64, but it is deprecated since plain stat now
// provides the same 64-bit aware struct when targeting OS X > 10.5
Expand Down
20 changes: 20 additions & 0 deletions src/libraries/Native/Unix/System.IO.Ports.Native/pal_termios.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <unistd.h>
#include <errno.h>
#include <pal_termios.h>
#if HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif

/* This is dup of System/IO/Ports/NativeMethods.cs */
enum
Expand Down Expand Up @@ -359,7 +362,12 @@ int32_t SystemIoPortsNative_TermiosSetSpeed(intptr_t handle, int32_t speed)
return -1;
}

#if HAVE_CFSETSPEED
cfsetspeed(&term, brate);
#else // on SunOS, set input and output speeds individually
cfsetispeed(&term, brate);
cfsetospeed(&term, brate);
#endif

if (tcsetattr(fd, TCSANOW, &term) < 0)
{
Expand Down Expand Up @@ -418,7 +426,15 @@ int32_t SystemIoPortsNative_TermiosReset(intptr_t handle, int32_t speed, int32_t
return -1;
}

#if HAVE_CFMAKERAW
cfmakeraw(&term);
#else
term.c_iflag &= ~(IMAXBEL|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
term.c_oflag &= ~OPOST;
term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
term.c_cflag &= ~(CSIZE|PARENB);
term.c_cflag |= CS8;
#endif
term.c_cflag |= (CLOCAL | CREAD);
term.c_lflag &= ~((tcflag_t)(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN ));
term.c_oflag &= ~((tcflag_t)(OPOST));
Expand Down Expand Up @@ -495,7 +511,11 @@ int32_t SystemIoPortsNative_TermiosReset(intptr_t handle, int32_t speed, int32_t
return -1;
}

#if HAVE_CFSETSPEED
ret = cfsetspeed(&term, brate);
#else
ret = cfsetispeed(&term, brate) & cfsetospeed(&term, brate);
#endif
}

if ((ret != 0) || (tcsetattr(fd, TCSANOW, &term) < 0))
Expand Down
8 changes: 4 additions & 4 deletions src/libraries/Native/Unix/System.Native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ if (GEN_SHARED_LIB)

if (CLR_CMAKE_TARGET_FREEBSD)
target_link_libraries(System.Native pthread)
if (HAVE_INOTIFY)
find_library(INOTIFY_LIBRARY inotify HINTS /usr/local/lib)
target_link_libraries(System.Native ${INOTIFY_LIBRARY})
endif ()
if (HAVE_INOTIFY)
find_library(INOTIFY_LIBRARY inotify HINTS /usr/local/lib)
target_link_libraries(System.Native ${INOTIFY_LIBRARY})
endif ()
endif ()
install_with_stripped_symbols (System.Native PROGRAMS .)
endif ()
Expand Down
11 changes: 7 additions & 4 deletions src/libraries/Native/Unix/System.Native/pal_interfaceaddresses.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#if HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#if HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#if HAVE_ETHTOOL_H
#include <linux/ethtool.h>
#include <linux/sockios.h>
Expand All @@ -28,11 +31,11 @@

#if defined(AF_PACKET)
#include <sys/ioctl.h>
#if defined(_WASM_)
#if HAVE_NETPACKET_PACKET_H
#include <netpacket/packet.h>
#else // _WASM_
#else
#include <linux/if_packet.h>
#endif // _WASM_
#endif
#elif defined(AF_LINK)
#include <net/if_dl.h>
#include <net/if_types.h>
Expand Down Expand Up @@ -431,7 +434,7 @@ int32_t SystemNative_GetNetworkInterfaces(int32_t * interfaceCount, NetworkInter
#endif
}

#if HAVE_RT_MSGHDR
#if HAVE_RT_MSGHDR && defined(CTL_NET)
int32_t SystemNative_EnumerateGatewayAddressesForInterface(uint32_t interfaceIndex, GatewayAddressFound onGatewayFound)
{
static struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
Expand Down
13 changes: 9 additions & 4 deletions src/libraries/Native/Unix/System.Native/pal_maphardwaretype.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
#include <sys/types.h>

#if defined(AF_PACKET)
#if defined(_WASM_)
#if HAVE_NETPACKET_PACKET_H
#include <netpacket/packet.h>
#include <net/if_arp.h>
#else // _WASM_
#else
#include <linux/if_packet.h>
#endif
#if HAVE_NET_IF_ARP_H
#include <net/if_arp.h>
#else
#include <linux/if_arp.h>
#endif // _WASM_
#endif
#elif defined(AF_LINK)
#include <net/if_dl.h>
#include <net/if_types.h>
Expand Down Expand Up @@ -43,7 +46,9 @@ uint16_t MapHardwareType(uint16_t nativeType)
case ARPHRD_PPP:
return NetworkInterfaceType_Ppp;
case ARPHRD_TUNNEL:
#ifdef ARPHRD_TUNNEL6
case ARPHRD_TUNNEL6:
#endif
return NetworkInterfaceType_Tunnel;
case ARPHRD_LOOPBACK:
return NetworkInterfaceType_Loopback;
Expand Down
36 changes: 31 additions & 5 deletions src/libraries/Native/Unix/System.Native/pal_mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
#include <sys/mount.h>
#else
#include <sys/statfs.h>
#if HAVE_SYS_MNTENT_H
#include <sys/mntent.h>
#include <sys/mnttab.h>
#include <sys/statvfs.h>
#else
#include <mntent.h>
#endif
#define STRING_BUFFER_SIZE 8192

// Android does not define MNTOPT_RO
Expand All @@ -42,8 +48,28 @@ static int32_t GetMountInfo(MountPointFound onFound)
return 0;
}

#else
#elif HAVE_SYS_MNTENT_H
int result = -1;
FILE* fp = fopen("/proc/mounts", MNTOPT_RO);
if (fp != NULL)
{
char buffer[STRING_BUFFER_SIZE] = {0};
struct mnttab entry;
while(getmntent(fp, &entry) == 0)
{
onFound(entry.mnt_mountp);
}

result = fclose(fp);
assert(result == 1); // documented to always return 1
result =
0; // We need to standardize a success return code between our implementations, so settle on 0 for success
}

return result;
}

#else
int result = -1;
FILE* fp = setmntent("/proc/mounts", MNTOPT_RO);
if (fp != NULL)
Expand Down Expand Up @@ -78,7 +104,7 @@ int32_t SystemNative_GetSpaceInfoForMountPoint(const char* name, MountPointInfor
assert(name != NULL);
assert(mpi != NULL);

#if defined(HAVE_STATFS)
#if HAVE_NON_LEGACY_STATFS
struct statfs stats;
memset(&stats, 0, sizeof(struct statfs));

Expand Down Expand Up @@ -117,7 +143,7 @@ SystemNative_GetFormatInfoForMountPoint(const char* name, char* formatNameBuffer
assert((formatNameBuffer != NULL) && (formatType != NULL));
assert(bufferLength > 0);

#if defined(HAVE_STATFS)
#if HAVE_NON_LEGACY_STATFS
struct statfs stats;
int result = statfs(name, &stats);
#else
Expand All @@ -142,12 +168,12 @@ SystemNative_GetFormatInfoForMountPoint(const char* name, char* formatNameBuffer
SafeStringCopy(formatNameBuffer, Int32ToSizeT(bufferLength), stats.f_fstypename);
*formatType = -1;
}
#elif defined(HAVE_STATFS)
#elif HAVE_NON_LEGACY_STATFS
assert(formatType != NULL);
*formatType = (int64_t)(stats.f_type);
SafeStringCopy(formatNameBuffer, Int32ToSizeT(bufferLength), "");
#else
*formatType = 0;
*formatType = 0;
#endif
}
else
Expand Down
6 changes: 5 additions & 1 deletion src/libraries/Native/Unix/System.Native/pal_networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
#if HAVE_LINUX_CAN_H
#include <linux/can.h>
#endif
#if HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif

#if HAVE_KQUEUE
#if KEVENT_HAS_VOID_UDATA
static void* GetKeventUdata(uintptr_t udata)
Expand Down Expand Up @@ -2310,7 +2314,7 @@ static bool TryConvertProtocolTypePlatformToPal(int32_t palAddressFamily, int pl
return true;

default:
*palProtocolType = (int)palProtocolType;
*palProtocolType = (int32_t)(intptr_t)palProtocolType;
return false;
}

Expand Down
Loading

0 comments on commit b88be15

Please sign in to comment.