11#define _DEFAULT_SOURCE
19#include <sys/socket.h>
29#define UNIX_PATH_MAX 108
32#define LOCK_SUFFIX ".lock"
33#define LOCK_SUFFIXLEN 5
43static struct wl_socket *wl_socket_alloc(
void)
47 s = malloc(
sizeof *s);
57static int wl_socket_lock(
struct wl_socket *socket)
59 struct stat socket_stat;
63 socket->
fd_lock = open(socket->
lock_addr, O_CREAT | O_CLOEXEC | O_RDWR, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
66 printf(
"unable to open lockfile %s check permissions\n", socket->
lock_addr);
70 if (flock(socket->
fd_lock, LOCK_EX | LOCK_NB) < 0) {
71 printf(
"unable to lock lockfile %s, maybe another compositor is running\n", socket->
lock_addr);
75 if (lstat(socket->
addr.sun_path, &socket_stat) < 0) {
76 if (errno != ENOENT) {
77 printf(
"did not manage to stat file %s\n", socket->
addr.sun_path);
80 }
else if (socket_stat.st_mode & S_IWUSR || socket_stat.st_mode & S_IWGRP) {
81 unlink(socket->
addr.sun_path);
94 *socket->
addr.sun_path = 0;
101 if (s->
addr.sun_path[0])
102 unlink(s->
addr.sun_path);
131 const int MAX_DISPLAYNO = 32;
132 const char *runtime_dir = getenv(
"XDG_RUNTIME_DIR");
134 printf(
"XDG_RUNTIME_DIR not set");
138 s = wl_socket_alloc();
144 s->
addr.sun_family = AF_LOCAL;
145 name_size = snprintf(s->
addr.sun_path,
sizeof s->
addr.sun_path,
"%s/%s", runtime_dir, s->
display_name) + 1;
146 assert(name_size > 0);
148 if (name_size > (
int)
sizeof s->
addr.sun_path) {
152 if (wl_socket_lock(s) < 0)
155 s->
fd = socket(PF_LOCAL, SOCK_STREAM, 0);
157 int size = SUN_LEN(&s->
addr);
158 int ret = bind(s->
fd, (
struct sockaddr*)&s->
addr, size);
162 ret = listen(s->
fd, 128);
167 }
while (displayno++ < MAX_DISPLAYNO);
char lock_addr[UNIX_PATH_MAX+LOCK_SUFFIXLEN]
struct wl_socket * wl_socket_create()
void wl_socket_destroy(struct wl_socket *s)
int wl_socket_get_fd(struct wl_socket *s)
const char * wl_socket_get_display_name(struct wl_socket *s)