File 95_sock_cloexec.patch of Package uim

Description: plug leak to unclosed socket after exec() in client by setting SOCK_CLOEXEC
Author: "Yuriy M. Kaminskiy" <yumkam@gmail.com>
Bug-Debian: https://bugs.debian.org/787208
Forwarded: https://github.com/uim/uim/pull/56
Last-Update: 2015-05-29

Index: uim-1.8.6/uim/uim-helper-client.c
===================================================================
diff -u uim-1.8.6.orig/uim/uim-helper-client.c uim-1.8.6/uim/uim-helper-client.c
--- uim-1.8.6.orig/uim/uim-helper-client.c
+++ uim-1.8.6/uim/uim-helper-client.c
@@ -89,6 +89,12 @@
   server.sun_family = PF_UNIX;
   strlcpy(server.sun_path, path, sizeof(server.sun_path));
 
+#ifdef SOCK_CLOEXEC
+  /* linux-2.6.27+ variant that prevents racing on concurrent fork & exec in other thread */
+  fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+  if (fd == -1 && errno == EINVAL)
+    /* fallback to plain SOCK_TYPE on older kernel */
+#endif
   fd = socket(PF_UNIX, SOCK_STREAM, 0);
   if (fd < 0) {
     perror("fail to create socket");
Index: uim-1.8.6/uim/socket.c
===================================================================
diff -u uim-1.8.6.orig/uim/socket.c uim-1.8.6/uim/socket.c
--- uim-1.8.6.orig/uim/socket.c
+++ uim-1.8.6/uim/socket.c
@@ -278,7 +278,15 @@
 static uim_lisp
 c_socket(uim_lisp domain_, uim_lisp type_, uim_lisp protocol_)
 {
-  int fd = socket(C_INT(domain_), C_INT(type_), C_INT(protocol_));
+  int type_i = C_INT(type_);
+  int fd;
+#ifdef SOCK_CLOEXEC
+  /* linux-2.6.27+ variant that prevents racing on concurrent fork & exec in other thread */
+  fd = socket(C_INT(domain_), type_i | SOCK_CLOEXEC, C_INT(protocol_));
+  if (fd == -1 && errno == EINVAL)
+    /* fallback to plain SOCK_TYPE on older kernel */
+#endif
+    fd = socket(C_INT(domain_), type_i, C_INT(protocol_));
   if (fd != -1)
     fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
   return MAKE_INT(fd);