From ed9901d842ed4810cf4e503829541ec4407c9619 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 30 Mar 2010 09:50:32 +0800 Subject: osmocon: calcluate unix domain socket length in a portable way Let's hope this concept (as seen in Xorg sources) is portable enough to calculate the length of the sockaddr_un properly on all architectures. --- src/host/osmocon/osmocon.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/host/osmocon/osmocon.c') diff --git a/src/host/osmocon/osmocon.c b/src/host/osmocon/osmocon.c index f934dd77..8ed3a6d2 100644 --- a/src/host/osmocon/osmocon.c +++ b/src/host/osmocon/osmocon.c @@ -615,6 +615,7 @@ static int register_tool_server(struct tool_server *ts, { struct bsc_fd *bfd = &ts->bfd; struct sockaddr_un local; + unsigned int namelen; int rc; bfd->fd = socket(AF_UNIX, SOCK_STREAM, 0); @@ -628,8 +629,20 @@ static int register_tool_server(struct tool_server *ts, strncpy(local.sun_path, path, sizeof(local.sun_path)); local.sun_path[sizeof(local.sun_path) - 1] = '\0'; unlink(local.sun_path); - rc = bind(bfd->fd, (struct sockaddr *) &local, - sizeof(local.sun_family) + strlen(local.sun_path)); + + /* we use the same magic that X11 uses in Xtranssock.c for + * calculating the proper length of the sockaddr */ +#if defined(BSD44SOCKETS) || defined(__UNIXWARE__) + local.sun_len = strlen(local.sun_path); +#endif +#if defined(BSD44SOCKETS) || defined(SUN_LEN) + namelen = SUN_LEN(&local); +#else + namelen = strlen(local.sun_path) + + offsetof(struct sockaddr_un, sun_path); +#endif + + rc = bind(bfd->fd, (struct sockaddr *) &local, namelen); if (rc != 0) { fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n", local.sun_path); -- cgit v1.2.3