1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
* Copyright (C) 2002 Dominic Sacr <bugcreator@gmx.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.
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <iostream>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include "ControlSocket.h"
#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108
#endif
using std::cerr;
using std::string;
using std::endl;
// ============================================================================
// ControlSocket
// ============================================================================
ControlSocket & ControlSocket::operator<< (unsigned int v)
{
send (m_sd, &v, sizeof(unsigned int), 0);
return *this;
}
ControlSocket & ControlSocket::operator<< (bool v)
{
send (m_sd, &v, sizeof(bool), 0);
return *this;
}
ControlSocket & ControlSocket::operator<< (const string & v)
{
size_t s = v.size() + 1;
send (m_sd, &s, sizeof(size_t), 0);
send (m_sd, v.c_str(), s, 0);
return *this;
}
ControlSocket & ControlSocket::operator>> (unsigned int & v)
{
recv (m_sd, &v, sizeof(unsigned int), 0);
return *this;
}
ControlSocket & ControlSocket::operator>> (bool & v)
{
recv (m_sd, &v, sizeof(bool), 0);
return *this;
}
ControlSocket & ControlSocket::operator>> (string & v)
{
size_t s;
recv (m_sd, &s, sizeof(size_t), 0);
char *p = new char[s];
if (p != NULL) {
recv (m_sd, p, s, 0);
v = string (p);
delete [] p;
}
return *this;
}
ControlSocket::ReadStatus ControlSocket::readStatus ()
{
char foo;
fcntl (m_sd, F_SETFL, O_NONBLOCK);
int r = recv (m_sd, &foo, sizeof(foo), MSG_PEEK);
fcntl (m_sd, F_SETFL, 0);
if (r > 0) return READ;
else if (r == 0) return CLOSED;
return WAITING;
}
// ============================================================================
// ControlSocketServer
// ============================================================================
bool ControlSocketServer::init (const string & socket_path)
{
m_saddr.sun_family = AF_UNIX;
snprintf (m_saddr.sun_path, UNIX_PATH_MAX, "%s/ctrlsocket", socket_path.c_str());
// try to create the directory where the socket should be created
if ((mkdir (socket_path.c_str(), 0700) == -1) && errno != EEXIST) {
cerr << "Failed to create directory `" << socket_path.c_str() << "' (Error: " << strerror(errno) << ")" << endl;
return false;
}
// try to delete the socket if it already exists
if ((unlink (m_saddr.sun_path) == -1) && errno != ENOENT) {
cerr << "Failed to unlink `" << m_saddr.sun_path << "' (Error: " << strerror(errno) << ")" << endl;
return false;
}
// create the socket
if ((m_sd = socket (AF_UNIX, SOCK_STREAM, 0)) == -1) {
cerr << "Failed to open socket" << endl;
return false;
}
if (bind (m_sd, (struct sockaddr *) &m_saddr, sizeof(m_saddr)) == -1) {
cerr << "Failed to assign `" << m_saddr.sun_path << "' to a socket (Error: " << strerror(errno) << ")" << endl;
close (m_sd);
return false;
}
chmod (m_saddr.sun_path, 0700);
listen (m_sd, 5);
return true;
}
void ControlSocketServer::quit ()
{
close (m_sd);
unlink (m_saddr.sun_path);
}
int ControlSocketServer::getConnection ()
{
fd_set set;
timeval tv;
FD_ZERO (&set);
FD_SET (m_sd, &set);
tv.tv_sec = 0;
tv.tv_usec = 0;
// see whether someone connected to our socket
if (select (m_sd + 1, &set, NULL, NULL, &tv) > 0) {
int sd = accept (m_sd, (sockaddr *) NULL, NULL);
return sd;
}
return -1;
}
void ControlSocketServer::closeConnection (int sd)
{
close (sd);
}
// ============================================================================
// ControlSocketClient
// ============================================================================
bool ControlSocketClient::init (const string & socket_path, bool quiet)
{
sockaddr_un saddr;
saddr.sun_family = AF_UNIX;
snprintf (saddr.sun_path, UNIX_PATH_MAX, "%s/ctrlsocket", socket_path.c_str());
if ((m_sd = socket (AF_UNIX, SOCK_STREAM, 0)) == -1) {
if (!quiet) cerr << "Failed to open socket" << endl;
return false;
}
if (connect (m_sd, (sockaddr *) &saddr, sizeof (saddr)) == -1) {
if (!quiet) cerr << "Couldn't connect to socket `" << saddr.sun_path << "'" << endl;
close (m_sd);
return false;
}
return true;
}
void ControlSocketClient::quit ()
{
close (m_sd);
}
|