$OpenBSD: patch-server_gam_channel_c,v 1.4 2010/07/13 14:10:35 ajacoutot Exp $

From FreeBSD:
Restore the stale socket cleanup code from gamin 0.1.5 to ensure that even
if gamin crashes or is killed with SIGKILL, a leftover socket does not
prevent gam_server from starting.

--- server/gam_channel.c.orig	Wed Jul  4 15:36:49 2007
+++ server/gam_channel.c	Tue Jul 13 15:53:14 2010
@@ -1,4 +1,5 @@
 #include "server_config.h"
+#include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
@@ -153,7 +154,11 @@ gam_client_conn_check_cred(GIOChannel * source, int fd
 
     {
 #ifdef SO_PEERCRED
+#ifndef __OpenBSD__
         struct ucred cr;
+#else
+	struct sockpeercred cr;
+#endif
         socklen_t cr_len = sizeof(cr);
 
         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) ==
@@ -620,6 +625,7 @@ gam_listen_unix_socket(const char *path)
 {
     int fd;
     struct sockaddr_un addr;
+    struct stat st;
 
     fd = socket(PF_UNIX, SOCK_STREAM, 0);
     if (fd < 0) {
@@ -640,8 +646,18 @@ gam_listen_unix_socket(const char *path)
      * some extra protection checks. Also make sure the socket is created
      * with restricted mode
      */
-    if (!gam_check_secure_path(path)) {
+    if (!gam_check_secure_dir()) {
+        close(fd);
 	return (-1);
+    }
+
+    if (stat(path, &st) == 0) {
+        /* bind() will fail if the socket already exists */
+        if (unlink(path) < 0) {
+            GAM_DEBUG(DEBUG_INFO, "Failed to remove %s\n", path);
+            close(fd);
+            return (-1);
+        }
     }
     strncpy(&addr.sun_path[0], path, (sizeof(addr) - 4) - 1);
     umask(0077);
