socket: fix proto not supported under FreeBSD (FS#349)
[awesome.git] / common / socket.c
blobba89831cdefdf356a08eaeec0e3b8d83812d447b
1 /*
2 * socket.c - awesome client, communicate with socket, common functions
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2007 daniel@brinkers.de
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdio.h>
24 #include <errno.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
28 #include <xcb/xcb.h>
30 #include "common/socket.h"
31 #include "common/util.h"
33 #define CONTROL_UNIX_SOCKET_PATH ".awesome_ctl."
35 /** Get a sockaddr_un struct with information feeded for opening a
36 * communication to the awesome socket for given display
37 * \param display the display number
38 * \return sockaddr_un struct ready to be used or NULL if a problem occured
40 struct sockaddr_un *
41 socket_getaddr(const char *display)
43 char *homedir, *host = NULL;
44 int screenp = 0, displayp = 0;
45 ssize_t path_len, len;
46 struct sockaddr_un *addr;
48 addr = p_new(struct sockaddr_un, 1);
49 homedir = getenv("HOME");
51 xcb_parse_display(NULL, &host, &displayp, &screenp);
53 len = a_strlen(host);
55 /* + 2 for / and . and \0 */
56 path_len = snprintf(addr->sun_path, sizeof(addr->sun_path),
57 "%s/" CONTROL_UNIX_SOCKET_PATH "%s%s%d",
58 homedir, len ? host : "", len ? "." : "",
59 displayp);
61 p_delete(&host);
63 if(path_len >= ssizeof(addr->sun_path))
65 fprintf(stderr, "error: path of control UNIX domain socket is too long");
66 return NULL;
69 addr->sun_family = AF_UNIX;
71 return addr;
74 /** Get a AF_UNIX socket for communicating with awesome
75 * \return the socket file descriptor
77 int
78 socket_getclient(void)
80 int csfd;
81 #ifndef __FreeBSD__
82 csfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
83 #else
84 csfd = socket(PF_UNIX, SOCK_STREAM, 0);
85 #endif
87 if(csfd < 0)
88 warn("error opening UNIX domain socket: %s", strerror(errno));
90 return csfd;
92 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80