From 9b14c97533a276e797732ad11dd0b653a9015bcd Mon Sep 17 00:00:00 2001
From: sfanxiang <sfanxiang@gmail.com>
Date: Tue, 2 Jan 2018 12:51:02 +0000
Subject: port to Haiku


diff --git a/common/unix-peer.c b/common/unix-peer.c
index 7fe2eea..f4c6789 100644
--- a/common/unix-peer.c
+++ b/common/unix-peer.c
@@ -41,7 +41,6 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <sys/uio.h>
-#include <sys/errno.h>
 
 #ifdef HAVE_UCRED_H
 #  include <ucred.h>
diff --git a/p11-kit/server.c b/p11-kit/server.c
index 75b93d6..0e0d806 100644
--- a/p11-kit/server.c
+++ b/p11-kit/server.c
@@ -56,6 +56,7 @@
 #include <grp.h>
 #include <pwd.h>
 #include <signal.h>
+#include <sys/select.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/un.h>
@@ -279,7 +280,7 @@ create_socket (const char *address,
 	}
 
 	umask (066);
-	rc = bind (sd, (struct sockaddr *)&sa, SUN_LEN (&sa));
+	rc = bind (sd, (struct sockaddr *)&sa, sizeof(sa));
 	if (rc == -1) {
 		close (sd);
 		p11_message_err (errno, "could not bind socket %s", socket_file);
-- 
2.24.0


From 8ac008f97dc467957b5a10f62a8f0465df355fcd Mon Sep 17 00:00:00 2001
From: sfanxiang <sfanxiang@gmail.com>
Date: Tue, 2 Jan 2018 14:38:35 +0000
Subject: fix frob_setuid dependency


diff --git a/Makefile.in b/Makefile.in
index d70782f..ca9f404 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -621,7 +621,7 @@ am_p11_kit_frob_setuid_OBJECTS = p11-kit/frob-setuid.$(OBJEXT)
 p11_kit_frob_setuid_OBJECTS = $(am_p11_kit_frob_setuid_OBJECTS)
 am__DEPENDENCIES_6 = libp11-kit-testable.la libp11-test.la \
 	libp11-common.la $(am__DEPENDENCIES_1)
-p11_kit_frob_setuid_DEPENDENCIES = $(am__DEPENDENCIES_6)
+p11_kit_frob_setuid_DEPENDENCIES = $(am__DEPENDENCIES_6) libp11-library.la
 am_p11_kit_p11_kit_OBJECTS = p11-kit/lists.$(OBJEXT) \
 	p11-kit/p11-kit.$(OBJEXT) $(am__objects_1)
 p11_kit_p11_kit_OBJECTS = $(am_p11_kit_p11_kit_OBJECTS)
-- 
2.24.0


From c47bcefb32515b71b151b3c354d17ebe7aa5c85f Mon Sep 17 00:00:00 2001
From: sfanxiang <sfanxiang@gmail.com>
Date: Wed, 3 Jan 2018 13:57:49 +0000
Subject: avoid segfault caused by ffi closures


diff --git a/p11-kit/virtual.c b/p11-kit/virtual.c
index a7bca48..c4e92f9 100644
--- a/p11-kit/virtual.c
+++ b/p11-kit/virtual.c
@@ -65,6 +65,10 @@
 #include "ffi.h"
 #endif
 
+#ifdef FFI_CLOSURES
+#undef FFI_CLOSURES	/* ffi closures segfault in Haiku */
+#endif
+
 /* There are 66 functions in PKCS#11, with a maximum of 8 args */
 #define MAX_FUNCTIONS 66
 #define MAX_ARGS 10
-- 
2.24.0


From 8b646b6c3ca9b07d685b4874459b8c876a19bbe2 Mon Sep 17 00:00:00 2001
From: sfanxiang <sfanxiang@gmail.com>
Date: Thu, 4 Jan 2018 12:42:37 +0000
Subject: use __thread instead of pthread_getspecific()

Using pthread_getspecific() and pthread_setspecific() leads to segfault
in this program, but doing the same with __thread doesn't.

This is just a workaround.

diff --git a/common/library.c b/common/library.c
index c1275f3..5b69965 100644
--- a/common/library.c
+++ b/common/library.c
@@ -58,8 +58,6 @@ typedef struct {
 	char message[P11_MESSAGE_MAX];
 } p11_local;
 
-static p11_local * _p11_library_get_thread_local (void);
-
 #ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
 p11_mutex_t p11_library_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
 
@@ -85,9 +83,8 @@ extern locale_t p11_message_locale;
 static char *
 thread_local_message (void)
 {
-	p11_local *local;
-	local = _p11_library_get_thread_local ();
-	return local ? local->message : NULL;
+	static __thread p11_local local = {};
+	return local.message;
 }
 
 static char *
@@ -119,23 +116,7 @@ _p11_library_get_thread_local (void)
 	return &local;
 }
 #else
-static pthread_key_t thread_local = 0;
 
-static p11_local *
-_p11_library_get_thread_local (void)
-{
-	p11_local *local;
-
-	p11_library_init_once ();
-
-	local = pthread_getspecific (thread_local);
-	if (local == NULL) {
-		local = calloc (1, sizeof (p11_local));
-		pthread_setspecific (thread_local, local);
-	}
-
-	return local;
-}
 #endif
 
 static void
@@ -153,7 +134,6 @@ p11_library_init_impl (void)
 	P11_RECURSIVE_MUTEX_INIT (p11_library_mutex);
 	P11_RECURSIVE_MUTEX_INIT (p11_virtual_mutex);
 #ifndef P11_TLS_KEYWORD
-	pthread_key_create (&thread_local, free);
 #endif
 	p11_message_storage = thread_local_message;
 #ifdef HAVE_STRERROR_L
@@ -175,9 +155,6 @@ p11_library_uninit (void)
 	uninit_common ();
 
 #ifndef P11_TLS_KEYWORD
-	/* Some cleanup to pacify valgrind */
-	free (pthread_getspecific (thread_local));
-	pthread_setspecific (thread_local, NULL);
 #endif
 
 #ifdef HAVE_STRERROR_L
@@ -185,7 +162,6 @@ p11_library_uninit (void)
 #endif
 	p11_message_storage = dont_store_message;
 #ifndef P11_TLS_KEYWORD
-	pthread_key_delete (thread_local);
 #endif
 	p11_mutex_uninit (&p11_virtual_mutex);
 	p11_mutex_uninit (&p11_library_mutex);
-- 
2.24.0


From 2b19ec4b17ced537506f7f417149949aac72259c Mon Sep 17 00:00:00 2001
From: sfanxiang <sfanxiang@gmail.com>
Date: Thu, 4 Jan 2018 13:40:33 +0000
Subject: trust/save: don't use hard links on Haiku


diff --git a/trust/save.c b/trust/save.c
index bb77348..2946a42 100644
--- a/trust/save.c
+++ b/trust/save.c
@@ -184,7 +184,7 @@ dir_free (p11_save_dir *dir) {
 	free (dir);
 }
 
-#ifdef OS_UNIX
+#if defined(OS_UNIX) && !defined(__HAIKU__)
 
 static int
 on_unique_try_link (void *data,
@@ -202,7 +202,7 @@ on_unique_try_link (void *data,
 	return 1; /* All done */
 }
 
-#else /* OS_WIN32 */
+#else /* defined(OS_UNIX) && !defined(__HAIKU__) */
 
 static int
 on_unique_try_rename (void *data,
@@ -222,7 +222,7 @@ on_unique_try_rename (void *data,
 	return 1; /* All done */
 }
 
-#endif /* OS_WIN32 */
+#endif /* defined(OS_UNIX) && !defined(__HAIKU__) */
 
 bool
 p11_save_finish_file (p11_save_file *file,
@@ -249,7 +249,7 @@ p11_save_finish_file (p11_save_file *file,
 		p11_message_err (errno, "couldn't write file: %s", file->temp);
 		ret = false;
 
-#ifdef OS_UNIX
+#if defined(OS_UNIX) && !defined(__HAIKU__)
 	/* Set the mode of the file, readable by everyone, but not writable */
 	} else if (chmod (file->temp, S_IRUSR | S_IRGRP | S_IROTH) < 0) {
 		p11_message_err (errno, "couldn't set file permissions: %s", file->temp);
@@ -281,7 +281,7 @@ p11_save_finish_file (p11_save_file *file,
 		}
 		unlink (file->temp);
 
-#else /* OS_WIN32 */
+#else /* defined(OS_UNIX) && !defined(__HAIKU__) */
 
 	/* Windows does not do atomic renames, so delete original file first */
 	} else {
@@ -308,7 +308,7 @@ p11_save_finish_file (p11_save_file *file,
 			unlink (file->temp);
 		}
 
-#endif /* OS_WIN32 */
+#endif /* defined(OS_UNIX) && !defined(__HAIKU__) */
 	}
 
 	if (ret && path_out) {
-- 
2.24.0


From f8463ea9d066263cb4ef08e3598c1ad899124415 Mon Sep 17 00:00:00 2001
From: fbrosson <fbrosson@localhost>
Date: Tue, 29 May 2018 16:08:23 +0000
Subject: fix incorrect printf format for pid_t.


diff --git a/common/debug.c b/common/debug.c
index bcf6e21..cf18090 100644
--- a/common/debug.c
+++ b/common/debug.c
@@ -140,7 +140,7 @@ p11_debug_message (int flag,
 	va_list args;
 
 	if (flag & p11_debug_current_flags) {
-		fprintf (stderr, "(p11-kit:%d) ", getpid());
+		fprintf (stderr, "(p11-kit:%ld) ", getpid());
 		va_start (args, format);
 		vfprintf (stderr, format, args);
 		va_end (args);
@@ -157,7 +157,7 @@ p11_debug_message_err (int flag,
 	char strerr[P11_DEBUG_MESSAGE_MAX];
 
 	if (flag & p11_debug_current_flags) {
-		fprintf (stderr, "(p11-kit:%d) ", getpid());
+		fprintf (stderr, "(p11-kit:%ld) ", getpid());
 		va_start (args, format);
 		vfprintf (stderr, format, args);
 		va_end (args);
-- 
2.24.0

