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>
|
|
|
|
|
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
|
|
|
|
2008-02-27 09:00:42 +01:00
|
|
|
/** Get a sockaddr_un struct with information feeded for opening a
|
|
|
|
* communication to the awesome socket for given display
|
|
|
|
* \param display the display number
|
|
|
|
* \return sockaddr_un struct ready to be used or NULL if a problem occured
|
|
|
|
*/
|
2007-10-29 16:14:50 +01:00
|
|
|
struct sockaddr_un *
|
2008-03-21 11:26:56 +01:00
|
|
|
socket_getaddr(const char *display)
|
2007-10-29 16:14:50 +01:00
|
|
|
{
|
2008-08-27 17:14:47 +02:00
|
|
|
char *homedir, *host = NULL;
|
2008-08-28 19:50:04 +02:00
|
|
|
int screenp = 0, displayp = 0;
|
|
|
|
ssize_t path_len, len;
|
2007-10-29 16:14:50 +01:00
|
|
|
struct sockaddr_un *addr;
|
|
|
|
|
|
|
|
addr = p_new(struct sockaddr_un, 1);
|
|
|
|
homedir = getenv("HOME");
|
2008-08-27 17:14:47 +02:00
|
|
|
|
|
|
|
xcb_parse_display(NULL, &host, &displayp, &screenp);
|
2007-12-16 21:01:38 +01:00
|
|
|
|
2008-08-28 19:50:04 +02:00
|
|
|
len = a_strlen(host);
|
|
|
|
|
2008-08-26 18:14:39 +02:00
|
|
|
/* + 2 for / and . and \0 */
|
2008-08-27 17:14:47 +02:00
|
|
|
path_len = snprintf(addr->sun_path, sizeof(addr->sun_path),
|
|
|
|
"%s/" CONTROL_UNIX_SOCKET_PATH "%s%s%d",
|
2008-08-28 19:50:04 +02:00
|
|
|
homedir, len ? host : "", len ? "." : "",
|
2008-08-27 17:14:47 +02:00
|
|
|
displayp);
|
2007-12-16 21:01:38 +01:00
|
|
|
|
2008-08-28 19:20:11 +02:00
|
|
|
p_delete(&host);
|
|
|
|
|
2007-10-29 16:14:50 +01:00
|
|
|
if(path_len >= ssizeof(addr->sun_path))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "error: path of control UNIX domain socket is too long");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr->sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|