[PATCH 01/10] wayland-server: reduce use of magic numbers

Kristian Høgsberg hoegsberg at gmail.com
Sun Jul 1 20:14:49 PDT 2012


On Sun, Jul 01, 2012 at 05:51:19PM +0000, nobled wrote:
> Make it clear what the significance of '5' and '113' actually is.
> Also drop an unneeded function argument.
> ---
> Patch series available here:
> https://github.com/nobled/wayland.git 'sockets' branch

That is a good series.  Very nice catch and fix for the socket lock
race.  Happy to see that our fd-leak test part of the test suite
caught something :)  All merged, though Pekka had a couple of good
points in his review.  We can fix that up later.

Kristian


>  src/wayland-server.c |   21 +++++++++++++++------
>  1 file changed, 15 insertions(+), 6 deletions(-)
> 
> diff --git a/src/wayland-server.c b/src/wayland-server.c
> index a9c04f9..3bd427b 100644
> --- a/src/wayland-server.c
> +++ b/src/wayland-server.c
> @@ -46,11 +46,21 @@
>  #include "wayland-server-protocol.h"
>  #include "wayland-os.h"
> 
> +/* This is the size of the char array in struct sock_addr_un.
> +   No Wayland socket can be created with a path longer than this,
> +   including the null terminator. */
> +#ifndef UNIX_PATH_MAX
> +#define UNIX_PATH_MAX	108
> +#endif
> +
> +#define LOCK_SUFFIX	".lock"
> +#define LOCK_SUFFIXLEN	5
> +
>  struct wl_socket {
>  	int fd;
>  	int fd_lock;
>  	struct sockaddr_un addr;
> -	char lock_addr[113];
> +	char lock_addr[UNIX_PATH_MAX + LOCK_SUFFIXLEN];
>  	struct wl_list link;
>  	struct wl_event_source *source;
>  };
> @@ -1100,13 +1110,12 @@ socket_data(int fd, uint32_t mask, void *data)
>  }
> 
>  static int
> -get_socket_lock(struct wl_socket *socket, socklen_t name_size)
> +get_socket_lock(struct wl_socket *socket)
>  {
>  	struct stat socket_stat;
> -	int lock_size = name_size + 5;
> 
> -	snprintf(socket->lock_addr, lock_size,
> -		 "%s.lock", socket->addr.sun_path);
> +	snprintf(socket->lock_addr, sizeof socket->lock_addr,
> +		 "%s%s", socket->addr.sun_path, LOCK_SUFFIX);
> 
>  	socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
>  			       (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
> @@ -1177,7 +1186,7 @@ wl_display_add_socket(struct wl_display
> *display, const char *name)
>  			     "%s/%s", runtime_dir, name) + 1;
>  	wl_log("using socket %s\n", s->addr.sun_path);
> 
> -	if (get_socket_lock(s,name_size) < 0) {
> +	if (get_socket_lock(s) < 0) {
>  		close(s->fd);
>  		free(s);
>  		return -1;
> -- 
> 1.7.9.5
> _______________________________________________
> wayland-devel mailing list
> wayland-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/wayland-devel


More information about the wayland-devel mailing list