Merge pull request #3962 from mkorkalo/fix_MonoBtlsContext_memory_leak
[mono.git] / support / map.c
index 6201c260c93369f1e37c4ceb340fe7c693b9a055..615584889dfa5c4e7b8659d43b9dcc638481648c 100644 (file)
@@ -367,6 +367,48 @@ int Mono_Posix_ToAtFlags (int x, int *r)
        return 0;
 }
 
+#ifdef HAVE_STRUCT_CMSGHDR
+int
+Mono_Posix_FromCmsghdr (struct Mono_Posix_Cmsghdr *from, struct cmsghdr *to)
+{
+       _cnm_return_val_if_overflow (gint64, from->cmsg_len, -1);
+
+       memset (to, 0, sizeof(*to));
+
+       to->cmsg_len   = from->cmsg_len;
+       if (Mono_Posix_FromUnixSocketProtocol (from->cmsg_level, &to->cmsg_level) != 0) {
+               return -1;
+       }
+       if (Mono_Posix_FromUnixSocketControlMessage (from->cmsg_type, &to->cmsg_type) != 0) {
+               return -1;
+       }
+
+       return 0;
+}
+#endif /* ndef HAVE_STRUCT_CMSGHDR */
+
+
+#ifdef HAVE_STRUCT_CMSGHDR
+int
+Mono_Posix_ToCmsghdr (struct cmsghdr *from, struct Mono_Posix_Cmsghdr *to)
+{
+       _cnm_return_val_if_overflow (gint64, from->cmsg_len, -1);
+
+       memset (to, 0, sizeof(*to));
+
+       to->cmsg_len   = from->cmsg_len;
+       if (Mono_Posix_ToUnixSocketProtocol (from->cmsg_level, &to->cmsg_level) != 0) {
+               return -1;
+       }
+       if (Mono_Posix_ToUnixSocketControlMessage (from->cmsg_type, &to->cmsg_type) != 0) {
+               return -1;
+       }
+
+       return 0;
+}
+#endif /* ndef HAVE_STRUCT_CMSGHDR */
+
+
 int Mono_Posix_FromConfstrName (int x, int *r)
 {
        *r = 0;
@@ -3880,12 +3922,17 @@ int Mono_Posix_ToMountFlags (guint64 x, guint64 *r)
 int Mono_Posix_FromMremapFlags (guint64 x, guint64 *r)
 {
        *r = 0;
+#ifndef __NetBSD__
        if ((x & Mono_Posix_MremapFlags_MREMAP_MAYMOVE) == Mono_Posix_MremapFlags_MREMAP_MAYMOVE)
 #ifdef MREMAP_MAYMOVE
                *r |= MREMAP_MAYMOVE;
 #else /* def MREMAP_MAYMOVE */
                {errno = EINVAL; return -1;}
 #endif /* ndef MREMAP_MAYMOVE */
+#else /* def __NetBSD__ */
+       if ((x & Mono_Posix_MremapFlags_MREMAP_MAYMOVE) != Mono_Posix_MremapFlags_MREMAP_MAYMOVE)
+               *r = MAP_FIXED;
+#endif /* def __NetBSD__ */
        if (x == 0)
                return 0;
        return 0;
@@ -3894,12 +3941,17 @@ int Mono_Posix_FromMremapFlags (guint64 x, guint64 *r)
 int Mono_Posix_ToMremapFlags (guint64 x, guint64 *r)
 {
        *r = 0;
+#ifndef __NetBSD__
        if (x == 0)
                return 0;
 #ifdef MREMAP_MAYMOVE
        if ((x & MREMAP_MAYMOVE) == MREMAP_MAYMOVE)
                *r |= Mono_Posix_MremapFlags_MREMAP_MAYMOVE;
 #endif /* ndef MREMAP_MAYMOVE */
+#else /* def __NetBSD__ */
+       if ((x & MAP_FIXED) != MAP_FIXED)
+               *r |= Mono_Posix_MremapFlags_MREMAP_MAYMOVE;
+#endif
        return 0;
 }
 
@@ -4664,52 +4716,36 @@ int Mono_Posix_ToPosixMadviseAdvice (int x, int *r)
 int Mono_Posix_FromSeekFlags (short x, short *r)
 {
        *r = 0;
-       if (x == Mono_Posix_SeekFlags_L_INCR)
 #ifdef L_INCR
+       if (x == Mono_Posix_SeekFlags_L_INCR)
                {*r = L_INCR; return 0;}
-#else /* def L_INCR */
-               {errno = EINVAL; return -1;}
-#endif /* ndef L_INCR */
-       if (x == Mono_Posix_SeekFlags_L_SET)
+#endif /* def L_INCR */
 #ifdef L_SET
+       if (x == Mono_Posix_SeekFlags_L_SET)
                {*r = L_SET; return 0;}
-#else /* def L_SET */
-               {errno = EINVAL; return -1;}
-#endif /* ndef L_SET */
-       if (x == Mono_Posix_SeekFlags_L_XTND)
+#endif /* def L_SET */
 #ifdef L_XTND
+       if (x == Mono_Posix_SeekFlags_L_XTND)
                {*r = L_XTND; return 0;}
-#else /* def L_XTND */
-               {errno = EINVAL; return -1;}
-#endif /* ndef L_XTND */
-       if (x == Mono_Posix_SeekFlags_SEEK_CUR)
+#endif /* def L_XTND */
 #ifdef SEEK_CUR
+       if (x == Mono_Posix_SeekFlags_SEEK_CUR)
                {*r = SEEK_CUR; return 0;}
-#else /* def SEEK_CUR */
-               {errno = EINVAL; return -1;}
-#endif /* ndef SEEK_CUR */
-       if (x == Mono_Posix_SeekFlags_SEEK_END)
+#endif /* def SEEK_CUR */
 #ifdef SEEK_END
+       if (x == Mono_Posix_SeekFlags_SEEK_END)
                {*r = SEEK_END; return 0;}
-#else /* def SEEK_END */
-               {errno = EINVAL; return -1;}
-#endif /* ndef SEEK_END */
-       if (x == Mono_Posix_SeekFlags_SEEK_SET)
+#endif /* def SEEK_END */
 #ifdef SEEK_SET
+       if (x == Mono_Posix_SeekFlags_SEEK_SET)
                {*r = SEEK_SET; return 0;}
-#else /* def SEEK_SET */
-               {errno = EINVAL; return -1;}
-#endif /* ndef SEEK_SET */
-       if (x == 0)
-               return 0;
+#endif /* def SEEK_SET */
        errno = EINVAL; return -1;
 }
 
 int Mono_Posix_ToSeekFlags (short x, short *r)
 {
        *r = 0;
-       if (x == 0)
-               return 0;
 #ifdef L_INCR
        if (x == L_INCR)
                {*r = Mono_Posix_SeekFlags_L_INCR; return 0;}
@@ -8247,6 +8283,42 @@ int Mono_Posix_ToUnixAddressFamily (int x, int *r)
        errno = EINVAL; return -1;
 }
 
+int Mono_Posix_FromUnixSocketControlMessage (int x, int *r)
+{
+       *r = 0;
+       if (x == Mono_Posix_UnixSocketControlMessage_SCM_CREDENTIALS)
+#ifdef SCM_CREDENTIALS
+               {*r = SCM_CREDENTIALS; return 0;}
+#else /* def SCM_CREDENTIALS */
+               {errno = EINVAL; return -1;}
+#endif /* ndef SCM_CREDENTIALS */
+       if (x == Mono_Posix_UnixSocketControlMessage_SCM_RIGHTS)
+#ifdef SCM_RIGHTS
+               {*r = SCM_RIGHTS; return 0;}
+#else /* def SCM_RIGHTS */
+               {errno = EINVAL; return -1;}
+#endif /* ndef SCM_RIGHTS */
+       if (x == 0)
+               return 0;
+       errno = EINVAL; return -1;
+}
+
+int Mono_Posix_ToUnixSocketControlMessage (int x, int *r)
+{
+       *r = 0;
+       if (x == 0)
+               return 0;
+#ifdef SCM_CREDENTIALS
+       if (x == SCM_CREDENTIALS)
+               {*r = Mono_Posix_UnixSocketControlMessage_SCM_CREDENTIALS; return 0;}
+#endif /* ndef SCM_CREDENTIALS */
+#ifdef SCM_RIGHTS
+       if (x == SCM_RIGHTS)
+               {*r = Mono_Posix_UnixSocketControlMessage_SCM_RIGHTS; return 0;}
+#endif /* ndef SCM_RIGHTS */
+       errno = EINVAL; return -1;
+}
+
 int Mono_Posix_FromUnixSocketFlags (int x, int *r)
 {
        *r = 0;
@@ -9236,4 +9308,3 @@ int Mono_Posix_ToXattrFlags (int x, int *r)
 #endif /* ndef XATTR_REPLACE */
        return 0;
 }
-