static int connect_to_socket(const char *name) { struct sockaddr_un addr; socklen_t size; const char *runtime_dir; int name_size, fd; runtime_dir = getenv("XDG_RUNTIME_DIR"); if (!runtime_dir) { fprintf(stderr, "error: XDG_RUNTIME_DIR not set in the environment.\n"); /* to prevent programs reporting * "failed to create display: Success" */ errno = ENOENT; return -1; } if (name == NULL) name = getenv("WAYLAND_DISPLAY"); if (name == NULL) name = "wayland-0"; fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0); if (fd < 0) return -1; memset(&addr, 0, sizeof addr); addr.sun_family = AF_LOCAL; name_size = snprintf(addr.sun_path, sizeof addr.sun_path, "%s/%s", runtime_dir, name) + 1; assert(name_size > 0); if (name_size > (int)sizeof addr.sun_path) { fprintf(stderr, "error: socket path \"%s/%s\" plus null terminator" " exceeds 108 bytes\n", runtime_dir, name); close(fd); /* to prevent programs reporting * "failed to add socket: Success" */ errno = ENAMETOOLONG; return -1; }; size = offsetof (struct sockaddr_un, sun_path) + name_size; if (connect(fd, (struct sockaddr *) &addr, size) < 0) { close(fd); return -1; } return fd; }
static void do_os_wrappers_socket_cloexec(int n) { int fd; int nr_fds; nr_fds = count_open_fds(); /* simply create a socket that closes on exec */ fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0); assert(fd >= 0); /* * Must have 2 calls if falling back, but must also allow * falling back without a forced fallback. */ assert(wrapped_calls_socket > n); exec_fd_leak_check(nr_fds); }