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-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 *
|
2007-10-29 17:29:58 +01:00
|
|
|
get_client_addr(const char *display)
|
2007-10-29 16:14:50 +01:00
|
|
|
{
|
2007-12-17 12:26:09 +01:00
|
|
|
char *homedir, *tmp;
|
|
|
|
const char *real_display = NULL;
|
2007-10-29 16:14:50 +01:00
|
|
|
ssize_t path_len;
|
|
|
|
struct sockaddr_un *addr;
|
|
|
|
|
|
|
|
addr = p_new(struct sockaddr_un, 1);
|
|
|
|
homedir = getenv("HOME");
|
2007-12-16 21:01:38 +01:00
|
|
|
if(a_strlen(display))
|
|
|
|
{
|
|
|
|
if((tmp = strchr(display, ':')))
|
|
|
|
real_display = tmp + 1;
|
|
|
|
else
|
2007-12-17 12:26:09 +01:00
|
|
|
real_display = display;
|
2007-12-16 21:01:38 +01:00
|
|
|
if((tmp = strrchr(display, '.')))
|
|
|
|
*tmp = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* a_strlen(display) because we strcat on display and
|
|
|
|
* + 2 for / and \0 */
|
2007-12-04 10:17:02 +01:00
|
|
|
path_len = a_strlen(homedir) + a_strlen(CONTROL_UNIX_SOCKET_PATH)
|
2007-12-16 21:01:38 +01:00
|
|
|
+ (a_strlen(display) ? (a_strlen(real_display)) : 1) + 2;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
a_strcpy(addr->sun_path, path_len, homedir);
|
|
|
|
a_strcat(addr->sun_path, path_len, "/");
|
|
|
|
a_strcat(addr->sun_path, path_len, CONTROL_UNIX_SOCKET_PATH);
|
2007-12-16 21:01:38 +01:00
|
|
|
if(a_strlen(display))
|
|
|
|
a_strcat(addr->sun_path, path_len, real_display);
|
2007-10-29 17:29:58 +01:00
|
|
|
else
|
|
|
|
a_strcat(addr->sun_path, path_len, "0");
|
2007-10-29 16:14:50 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
get_client_socket(void)
|
|
|
|
{
|
|
|
|
int csfd;
|
|
|
|
|
|
|
|
csfd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
if(csfd < 0)
|
2008-03-19 13:11:13 +01:00
|
|
|
warn("error opening UNIX domain socket: %s\n", 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
|