2007-10-29 16:14:50 +01:00
|
|
|
/*
|
2008-02-27 09:07:52 +01:00
|
|
|
* socket.c - awesome client, communicate with socket, common functions
|
2007-10-29 16:14:50 +01:00
|
|
|
*
|
2008-01-02 16:59:43 +01:00
|
|
|
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
|
2007-10-29 16:14:50 +01:00
|
|
|
* Copyright © 2007 daniel@brinkers.de
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-01-01 17:33:12 +01:00
|
|
|
#include <stdio.h>
|
2008-03-19 13:11:13 +01:00
|
|
|
#include <errno.h>
|
2007-10-29 16:14:50 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2009-03-20 01:59:40 +01:00
|
|
|
#include <unistd.h>
|
2007-10-29 16:14:50 +01:00
|
|
|
|
2008-08-27 17:14:47 +02:00
|
|
|
#include <xcb/xcb.h>
|
|
|
|
|
2008-02-27 09:07:52 +01:00
|
|
|
#include "common/socket.h"
|
2008-01-21 18:14:59 +01:00
|
|
|
#include "common/util.h"
|
2007-10-29 16:14:50 +01:00
|
|
|
|
2007-10-29 17:29:58 +01:00
|
|
|
#define CONTROL_UNIX_SOCKET_PATH ".awesome_ctl."
|
2007-10-29 16:14:50 +01:00
|
|
|
|
2009-03-26 19:12:24 +01:00
|
|
|
/** Open a communication socket with awesome.
|
2009-03-20 01:59:40 +01:00
|
|
|
* \param csfd The socket file descriptor.
|
|
|
|
* \param mode The open mode, either Bind or Connect.
|
|
|
|
* \return sockaddr_un Struct ready to be used or NULL if a problem ocurred.
|
2008-02-27 09:00:42 +01:00
|
|
|
*/
|
2007-10-29 16:14:50 +01:00
|
|
|
struct sockaddr_un *
|
2009-03-26 19:12:24 +01:00
|
|
|
socket_open(int csfd, const socket_mode_t mode)
|
2007-10-29 16:14:50 +01:00
|
|
|
{
|
2009-03-17 23:41:52 +01:00
|
|
|
char *host = NULL;
|
2008-08-28 19:50:04 +02:00
|
|
|
int screenp = 0, displayp = 0;
|
2009-03-20 01:59:40 +01:00
|
|
|
bool is_socket_opened = false;
|
2008-08-28 19:50:04 +02:00
|
|
|
ssize_t path_len, len;
|
2007-10-29 16:14:50 +01:00
|
|
|
struct sockaddr_un *addr;
|
2009-03-20 01:59:40 +01:00
|
|
|
const char *fallback[] = { ":HOME", ":TMPDIR", "/tmp", NULL }, *directory;
|
2007-10-29 16:14:50 +01:00
|
|
|
|
|
|
|
addr = p_new(struct sockaddr_un, 1);
|
2009-03-20 01:59:40 +01:00
|
|
|
addr->sun_family = AF_UNIX;
|
2009-03-17 23:41:52 +01:00
|
|
|
|
2008-08-27 17:14:47 +02:00
|
|
|
xcb_parse_display(NULL, &host, &displayp, &screenp);
|
2008-08-28 19:50:04 +02:00
|
|
|
len = a_strlen(host);
|
|
|
|
|
2009-03-20 01:59:40 +01:00
|
|
|
for(int i = 0; fallback[i] && !is_socket_opened; i++)
|
2007-10-29 16:14:50 +01:00
|
|
|
{
|
2009-03-20 01:59:40 +01:00
|
|
|
if(fallback[i][0] == ':')
|
|
|
|
{
|
|
|
|
if(!(directory = getenv(fallback[i] + 1)))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
directory = fallback[i];
|
|
|
|
|
|
|
|
/* + 2 for / and . and \0 */
|
|
|
|
path_len = snprintf(addr->sun_path, sizeof(addr->sun_path),
|
|
|
|
"%s/" CONTROL_UNIX_SOCKET_PATH "%s%s%d",
|
|
|
|
directory, len ? host : "", len ? "." : "",
|
|
|
|
displayp);
|
|
|
|
|
|
|
|
if(path_len < ssizeof(addr->sun_path))
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case SOCKET_MODE_BIND:
|
|
|
|
/* Needed for some OSes like Solaris */
|
|
|
|
#ifndef SUN_LEN
|
|
|
|
#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) + strlen ((ptr)->sun_path))
|
|
|
|
#endif
|
|
|
|
if(!bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
|
|
|
|
is_socket_opened = true;
|
|
|
|
else if(errno == EADDRINUSE)
|
|
|
|
{
|
|
|
|
if(unlink(addr->sun_path))
|
|
|
|
warn("error unlinking existing file: %s", strerror(errno));
|
|
|
|
if(!bind(csfd, (const struct sockaddr *)addr, SUN_LEN(addr)))
|
|
|
|
is_socket_opened = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SOCKET_MODE_CONNECT:
|
|
|
|
if(!connect(csfd, (const struct sockaddr *)addr, sizeof(struct sockaddr_un)))
|
|
|
|
is_socket_opened = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
warn("error: path (using %s) of control UNIX domain socket is too long", directory);
|
2007-10-29 16:14:50 +01:00
|
|
|
}
|
|
|
|
|
2009-03-20 01:59:40 +01:00
|
|
|
p_delete(&host);
|
|
|
|
|
|
|
|
if(!is_socket_opened)
|
|
|
|
p_delete(&addr);
|
2007-10-29 16:14:50 +01:00
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2008-02-27 09:00:42 +01:00
|
|
|
/** Get a AF_UNIX socket for communicating with awesome
|
|
|
|
* \return the socket file descriptor
|
|
|
|
*/
|
2007-10-29 16:14:50 +01:00
|
|
|
int
|
2008-03-21 11:26:56 +01:00
|
|
|
socket_getclient(void)
|
2007-10-29 16:14:50 +01:00
|
|
|
{
|
|
|
|
int csfd;
|
2008-11-03 14:53:23 +01:00
|
|
|
#ifndef __FreeBSD__
|
2008-09-19 06:48:55 +02:00
|
|
|
csfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
|
2008-11-03 14:53:23 +01:00
|
|
|
#else
|
|
|
|
csfd = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
#endif
|
2007-10-29 16:14:50 +01:00
|
|
|
|
|
|
|
if(csfd < 0)
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("error opening UNIX domain socket: %s", strerror(errno));
|
2007-10-29 16:14:50 +01:00
|
|
|
|
2009-04-02 13:33:11 +02:00
|
|
|
fd_set_close_on_exec(csfd);
|
|
|
|
|
2007-10-29 16:14:50 +01:00
|
|
|
return csfd;
|
|
|
|
}
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|