$OpenBSD: patch-libgamin_gam_api_c,v 1.1.1.1 2009/12/04 08:07:20 ajacoutot Exp $

Adapt socket credentials code to OpenBSD.

--- libgamin/gam_api.c.orig	Mon Aug 27 12:21:03 2007
+++ libgamin/gam_api.c	Sat Nov 28 15:45:19 2009
@@ -7,6 +7,9 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>
+#if defined(__OpenBSD__)
+#include <string.h>
+#endif
 #include <sys/types.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -730,6 +733,21 @@ retry:
         c_pid = cmsg.cred.cmcred_pid;
         c_uid = cmsg.cred.cmcred_euid;
         c_gid = cmsg.cred.cmcred_groups[0];
+#elif defined(__OpenBSD__)
+        uid_t euid;
+        gid_t egid;
+        if (getpeereid (fd, &euid, &egid) == 0)
+            {
+                c_pid = getpid();
+                c_uid = euid;
+                c_gid = egid;
+            }
+        else
+            {
+                GAM_DEBUG(DEBUG_INFO,
+                    "Failed to getpeereid() credentials: %d\n", fd);
+                goto failed;
+            }
 #else /* !SO_PEERCRED && !HAVE_CMSGCRED */
         GAM_DEBUG(DEBUG_INFO,
                   "Socket credentials not supported on this OS\n");
@@ -1288,14 +1306,16 @@ FAMNextEvent(FAMConnection * fc, FAMEvent * fe)
 
     // FIXME: drop and reacquire lock while blocked?
     gamin_data_lock(conn);
-    if (!gamin_data_event_ready(conn)) {
+    while ((ret = gamin_data_event_ready(conn)) == 0) {
         if (gamin_read_data(conn, fc->fd, 1) < 0) {
 	    gamin_try_reconnect(conn, fc->fd);
 	    FAMErrno = FAM_CONNECT;
 	    return (-1);
 	}
     }
-    ret = gamin_data_read_event(conn, fe);
+    if (ret > 0)
+        ret = gamin_data_read_event(conn, fe);
+
     gamin_data_unlock(conn);
 
     if (ret < 0) {
