summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
367882f)
This saves the X11 connection from leaking into children processes.
On Linux, this is fully thread-safe using SOCK_CLOEXEC. On other
systems, there is a small race condition.
Signed-off-by: Julien Danjou <julien@danjou.info>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <string.h>
#include "xcb.h"
#include <string.h>
#include "xcb.h"
return _xcb_open_unix(protocol, file);
}
return _xcb_open_unix(protocol, file);
}
+static int _xcb_socket(int family, int type, int proto)
+{
+ int fd;
+
+#ifdef SOCK_CLOEXEC
+ fd = socket(family, type | SOCK_CLOEXEC, proto);
+ if (fd == -1 && errno == EINVAL)
+#endif
+ {
+ fd = socket(family, type, proto);
+ if (fd >= 0)
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
+ }
+ return fd;
+}
+
#ifdef DNETCONN
static int _xcb_open_decnet(const char *host, const char *protocol, const unsigned short port)
{
#ifdef DNETCONN
static int _xcb_open_decnet(const char *host, const char *protocol, const unsigned short port)
{
return -1;
addr.sdn_objnum = 0;
return -1;
addr.sdn_objnum = 0;
- fd = socket(PF_DECnet, SOCK_STREAM, 0);
+ fd = _xcb_socket(PF_DECnet, SOCK_STREAM, 0);
for(addr = results; addr; addr = addr->ai_next)
{
for(addr = results; addr; addr = addr->ai_next)
{
- fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
+ fd = _xcb_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if(fd >= 0) {
int on = 1;
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
if(fd >= 0) {
int on = 1;
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
#ifdef HAVE_SOCKADDR_SUN_LEN
addr.sun_len = SUN_LEN(&addr);
#endif
#ifdef HAVE_SOCKADDR_SUN_LEN
addr.sun_len = SUN_LEN(&addr);
#endif
- fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
if(fd == -1)
return -1;
if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
if(fd == -1)
return -1;
if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
#ifdef HAVE_SOCKADDR_SUN_LEN
addr.sun_len = 1 + filelen;
#endif
#ifdef HAVE_SOCKADDR_SUN_LEN
addr.sun_len = 1 + filelen;
#endif
- fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ fd = _xcb_socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1)
return -1;
if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
if (fd == -1)
return -1;
if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {