wayland-client: Fix indentation
server: Clean up socket destruction

The code here is wrong, leaky, and inconsistent. We don't free,
unlink or clean up things when we should in every error path.

Centralize the data destruction so it's easier to keep track of
and easier to bug fix.
server: Create the socket FD after taking the lock

We're going to split out the lock-taking to another function so we
can repetitively try locks.
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.
server: Make get_socket_lock operate directly on the socket's lock_fd
server: Save the display name in the wl_socket

This allows us to return the display name to the client in a new API.
server: Add a simple API to find a good default display

This allows compositors to easily select a good display to listen on.
server: move memset after check

If the malloc fails, memset would touch invalid memory.

Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
tests: add tests for bug in adding socket

Last set of commits introduced a bug. When adding of socket with
a particular name fails, then the socket and its lockfile are deleted
regardless who created the socket.

/* OK */
wl_display_add_socket(display, "wayland-0");

/* this call fails and will delete the original socket */
wl_display_add_socket(display, "wayland-0");

Signed-off-by: Marek Chalupa <mchqwerty@gmail.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
server: fix error handling when adding socket

When some function during adding socket fails, it must clean
everything it set or we can get funky errors.

This patch fixes:
http://lists.freedesktop.org/archives/wayland-devel/2014-August/016331.html

Signed-off-by: Marek Chalupa <mchqwerty@gmail.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
server: fix conditions for fds in wl_socket_destroy

0 is also a valid fd, and needs to be closed.

On error we set fd to -1. We need to also initialize fds to -1, so we do
not accidentally close stdout on error.

While fixing this, also remove one use-before-NULL-check.

Based on the patch by Marek.

Cc: Marek Chalupa <mchqwerty@gmail.com>
Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Reviewed-by: Jasper St. Pierre <jstpierre@mecheye.net>