mirror of
https://gitlab.freedesktop.org/wayland/wayland.git
synced 2025-11-04 13:29:51 -05:00
server: Split out code to initialize the socket address for a display name
We'll use this to autodetect a good socket to open on.
This commit is contained in:
parent
6e8a662403
commit
79b1d2039a
1 changed files with 28 additions and 17 deletions
|
|
@ -1060,11 +1060,9 @@ get_socket_lock(struct wl_socket *socket)
|
||||||
return fd_lock;
|
return fd_lock;
|
||||||
}
|
}
|
||||||
|
|
||||||
WL_EXPORT int
|
static int
|
||||||
wl_display_add_socket(struct wl_display *display, const char *name)
|
wl_socket_init_for_display_name(struct wl_socket *s, const char *name)
|
||||||
{
|
{
|
||||||
struct wl_socket *s;
|
|
||||||
socklen_t size;
|
|
||||||
int name_size;
|
int name_size;
|
||||||
const char *runtime_dir;
|
const char *runtime_dir;
|
||||||
|
|
||||||
|
|
@ -1078,6 +1076,29 @@ wl_display_add_socket(struct wl_display *display, const char *name)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s->addr.sun_family = AF_LOCAL;
|
||||||
|
name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
|
||||||
|
"%s/%s", runtime_dir, name) + 1;
|
||||||
|
|
||||||
|
assert(name_size > 0);
|
||||||
|
if (name_size > (int)sizeof s->addr.sun_path) {
|
||||||
|
wl_log("error: socket path \"%s/%s\" plus null terminator"
|
||||||
|
" exceeds 108 bytes\n", runtime_dir, name);
|
||||||
|
/* to prevent programs reporting
|
||||||
|
* "failed to add socket: Success" */
|
||||||
|
errno = ENAMETOOLONG;
|
||||||
|
return -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
WL_EXPORT int
|
||||||
|
wl_display_add_socket(struct wl_display *display, const char *name)
|
||||||
|
{
|
||||||
|
struct wl_socket *s;
|
||||||
|
socklen_t size;
|
||||||
|
|
||||||
s = malloc(sizeof *s);
|
s = malloc(sizeof *s);
|
||||||
memset(s, 0, sizeof *s);
|
memset(s, 0, sizeof *s);
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
|
|
@ -1088,20 +1109,10 @@ wl_display_add_socket(struct wl_display *display, const char *name)
|
||||||
if (name == NULL)
|
if (name == NULL)
|
||||||
name = "wayland-0";
|
name = "wayland-0";
|
||||||
|
|
||||||
s->addr.sun_family = AF_LOCAL;
|
if (wl_socket_init_for_display_name(s, name) < 0) {
|
||||||
name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
|
|
||||||
"%s/%s", runtime_dir, name) + 1;
|
|
||||||
|
|
||||||
assert(name_size > 0);
|
|
||||||
if (name_size > (int)sizeof s->addr.sun_path) {
|
|
||||||
wl_log("error: socket path \"%s/%s\" plus null terminator"
|
|
||||||
" exceeds 108 bytes\n", runtime_dir, name);
|
|
||||||
wl_socket_destroy(s);
|
wl_socket_destroy(s);
|
||||||
/* to prevent programs reporting
|
|
||||||
* "failed to add socket: Success" */
|
|
||||||
errno = ENAMETOOLONG;
|
|
||||||
return -1;
|
return -1;
|
||||||
};
|
}
|
||||||
|
|
||||||
s->fd_lock = get_socket_lock(s);
|
s->fd_lock = get_socket_lock(s);
|
||||||
if (s->fd_lock < 0) {
|
if (s->fd_lock < 0) {
|
||||||
|
|
@ -1115,7 +1126,7 @@ wl_display_add_socket(struct wl_display *display, const char *name)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size = offsetof (struct sockaddr_un, sun_path) + name_size;
|
size = offsetof (struct sockaddr_un, sun_path) + strlen(s->addr.sun_path);
|
||||||
if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
|
if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
|
||||||
wl_log("bind() failed with error: %m\n");
|
wl_log("bind() failed with error: %m\n");
|
||||||
wl_socket_destroy(s);
|
wl_socket_destroy(s);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue