2012-07-29 03:05:53 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2012 Paul Gardner-Stephen, Serval Project.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-08-08 01:26:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include "win32/win32.h"
|
|
|
|
#endif
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "constants.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "str.h"
|
|
|
|
|
2012-07-29 03:05:53 +00:00
|
|
|
#include "monitor-client.h"
|
2012-08-08 01:26:05 +00:00
|
|
|
#include <ctype.h>
|
2012-07-29 03:05:53 +00:00
|
|
|
|
2012-08-08 01:26:05 +00:00
|
|
|
#define STATE_INIT 0
|
|
|
|
#define STATE_DATA 1
|
|
|
|
#define STATE_READY 2
|
|
|
|
|
|
|
|
#define MONITOR_CLIENT_BUFFER_SIZE 8192
|
|
|
|
#define MAX_ARGS 32
|
|
|
|
|
|
|
|
struct monitor_state {
|
|
|
|
char *cmd;
|
|
|
|
int argc;
|
|
|
|
char *argv[MAX_ARGS];
|
|
|
|
unsigned char *data;
|
|
|
|
int dataBytes;
|
|
|
|
int cmdBytes;
|
|
|
|
|
|
|
|
int state;
|
|
|
|
unsigned char buffer[MONITOR_CLIENT_BUFFER_SIZE];
|
|
|
|
int bufferBytes;
|
|
|
|
};
|
|
|
|
|
|
|
|
int monitor_socket_name(struct sockaddr_un *name){
|
|
|
|
int len;
|
|
|
|
#ifdef linux
|
|
|
|
/* Use abstract namespace as Android has no writable FS which supports sockets.
|
|
|
|
Abstract namespace is just plain better, anyway, as no dead files end up
|
|
|
|
hanging around. */
|
2012-08-09 04:15:57 +00:00
|
|
|
name->sun_path[0] = '\0';
|
2012-08-08 01:26:05 +00:00
|
|
|
/* XXX: 104 comes from OSX sys/un.h - no #define (note Linux has UNIX_PATH_MAX and it's 108(!)) */
|
2012-08-20 06:44:35 +00:00
|
|
|
snprintf(&name->sun_path[1],104-2,"%s",
|
2012-08-08 01:26:05 +00:00
|
|
|
confValueGet("monitor.socket",DEFAULT_MONITOR_SOCKET_NAME));
|
|
|
|
/* Doesn't include trailing nul */
|
|
|
|
len = 1+strlen(&name->sun_path[1]) + sizeof(name->sun_family);
|
|
|
|
#else
|
|
|
|
snprintf(name->sun_path,104-1,"%s/%s",
|
|
|
|
serval_instancepath(),
|
|
|
|
confValueGet("monitor.socket",DEFAULT_MONITOR_SOCKET_NAME));
|
|
|
|
/* Includes trailing nul */
|
|
|
|
len = 1+strlen(name->sun_path) + sizeof(name->sun_family);
|
|
|
|
#endif
|
|
|
|
return len;
|
|
|
|
}
|
2012-07-29 03:05:53 +00:00
|
|
|
|
|
|
|
/* Open monitor interface abstract domain named socket */
|
2012-08-08 01:26:05 +00:00
|
|
|
int monitor_client_open(struct monitor_state **res)
|
2012-07-29 03:05:53 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
|
|
|
if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
2012-10-19 05:39:20 +00:00
|
|
|
WHYF_perror("socket(AF_UNIX, SOCK_STREAM, 0)");
|
2012-08-08 01:26:05 +00:00
|
|
|
return -1;
|
2012-07-29 03:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sun_family = AF_UNIX;
|
2012-08-08 01:26:05 +00:00
|
|
|
int len = monitor_socket_name(&addr);
|
2012-10-19 05:39:20 +00:00
|
|
|
|
|
|
|
INFOF("Attempting to connect to %s %s",
|
|
|
|
addr.sun_path[0] ? "local" : "abstract",
|
|
|
|
alloca_str_toprint(addr.sun_path[0] ? &addr.sun_path[0] : &addr.sun_path[1])
|
|
|
|
);
|
2012-07-29 03:05:53 +00:00
|
|
|
|
|
|
|
if (connect(fd, (struct sockaddr*)&addr, len) == -1) {
|
2012-10-19 05:39:20 +00:00
|
|
|
WHYF_perror("connect(%d, %s)", fd, alloca_toprint(-1, &addr, len));
|
2012-08-13 04:32:13 +00:00
|
|
|
close(fd);
|
2012-08-08 01:26:05 +00:00
|
|
|
return -1;
|
2012-07-29 03:05:53 +00:00
|
|
|
}
|
2012-10-19 05:39:20 +00:00
|
|
|
|
2012-08-08 01:26:05 +00:00
|
|
|
*res = (struct monitor_state*)malloc(sizeof(struct monitor_state));
|
2012-08-20 06:41:12 +00:00
|
|
|
memset(*res,0,sizeof(struct monitor_state));
|
2012-07-29 03:05:53 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2012-08-08 01:26:05 +00:00
|
|
|
int monitor_client_close(int fd, struct monitor_state *res){
|
|
|
|
free(res);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int monitor_client_writeline(int fd,char *fmt, ...)
|
2012-07-29 03:05:53 +00:00
|
|
|
{
|
2012-08-08 01:26:05 +00:00
|
|
|
char msg[512];
|
|
|
|
int n;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (fd<0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
n=vsnprintf(msg, sizeof(msg), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return write(fd,msg,n);
|
2012-07-29 03:05:53 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 01:26:05 +00:00
|
|
|
int monitor_client_writeline_and_data(int fd,unsigned char *data,int bytes,char *fmt,...)
|
2012-07-29 03:05:53 +00:00
|
|
|
{
|
2012-08-08 01:26:05 +00:00
|
|
|
int maxlen=512+bytes;
|
2012-07-29 03:05:53 +00:00
|
|
|
char out[maxlen];
|
2012-08-08 01:26:05 +00:00
|
|
|
va_list ap;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (fd<0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
n=snprintf(out,maxlen-bytes,"*%d:",bytes);
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
n+=vsnprintf(out+n, maxlen-bytes-n, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
bcopy(data,out+n,bytes);
|
|
|
|
n+=bytes;
|
|
|
|
return write(fd,out,n);
|
2012-07-29 03:05:53 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 01:26:05 +00:00
|
|
|
int monitor_client_read(int fd, struct monitor_state *res, struct monitor_command_handler *handlers, int handler_count)
|
2012-07-29 03:05:53 +00:00
|
|
|
{
|
|
|
|
/* Read any available bytes */
|
2012-08-08 01:26:05 +00:00
|
|
|
int oldOffset = res->bufferBytes;
|
|
|
|
|
2012-09-27 06:57:37 +00:00
|
|
|
if (oldOffset+1>=MONITOR_CLIENT_BUFFER_SIZE)
|
|
|
|
return WHY("Buffer full without finding command");
|
|
|
|
|
2012-08-08 01:26:05 +00:00
|
|
|
if (res->bufferBytes==0)
|
|
|
|
res->cmd = (char *)res->buffer;
|
|
|
|
|
2012-10-19 09:08:32 +00:00
|
|
|
int bytesRead = read(fd, res->buffer + oldOffset, MONITOR_CLIENT_BUFFER_SIZE - oldOffset);
|
|
|
|
if (bytesRead == -1){
|
2012-08-08 01:26:05 +00:00
|
|
|
switch(errno) {
|
|
|
|
case ENOTRECOVERABLE:
|
|
|
|
/* transient errors */
|
2012-10-19 09:08:32 +00:00
|
|
|
break;
|
2012-08-20 06:41:12 +00:00
|
|
|
case EINTR:
|
2012-08-08 01:26:05 +00:00
|
|
|
case EAGAIN:
|
2012-08-20 06:41:12 +00:00
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
2012-08-08 01:26:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-10-19 09:08:32 +00:00
|
|
|
WHYF_perror("read(%d, %p, %ld)", fd, res->buffer + oldOffset, MONITOR_CLIENT_BUFFER_SIZE - oldOffset);
|
|
|
|
return -1;
|
|
|
|
} else if (bytesRead == 0) {
|
|
|
|
WHYF("read(%d, %p, %ld) returned %d", fd, res->buffer + oldOffset, MONITOR_CLIENT_BUFFER_SIZE - oldOffset, bytesRead);
|
2012-08-08 01:26:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res->bufferBytes+=bytesRead;
|
|
|
|
|
|
|
|
again:
|
|
|
|
// wait until we have the whole command line
|
|
|
|
if (res->state == STATE_INIT){
|
|
|
|
int i;
|
|
|
|
for(i=oldOffset;i<res->bufferBytes;i++){
|
|
|
|
if (res->buffer[i]=='\n'){
|
|
|
|
// skip any leading \n's
|
|
|
|
if ((char*)(res->buffer+i) == res->cmd){
|
|
|
|
res->cmd++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
res->buffer[i]=0;
|
|
|
|
res->dataBytes = 0;
|
|
|
|
res->cmdBytes = i + 1;
|
|
|
|
if (*res->cmd=='*'){
|
|
|
|
res->cmd++;
|
|
|
|
for (; isdigit(*res->cmd); ++res->cmd)
|
|
|
|
res->dataBytes = res->dataBytes * 10 + *res->cmd - '0';
|
2012-09-27 06:57:37 +00:00
|
|
|
if (res->dataBytes<0 || res->dataBytes > MONITOR_CLIENT_BUFFER_SIZE)
|
|
|
|
return WHYF("Invalid data length %d", res->dataBytes);
|
2012-08-08 01:26:05 +00:00
|
|
|
if (*res->cmd==':')
|
|
|
|
res->cmd++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find all arguments, initialise argc / argv && null terminate strings
|
2012-07-29 03:05:53 +00:00
|
|
|
{
|
2012-08-08 01:26:05 +00:00
|
|
|
char *p=res->cmd;
|
|
|
|
res->argc=0;
|
2012-09-27 06:57:37 +00:00
|
|
|
while (*p && res->argc<MAX_ARGS){
|
2012-08-08 01:26:05 +00:00
|
|
|
if (*p==':'){
|
|
|
|
*p=0;
|
|
|
|
res->argv[res->argc]=p+1;
|
|
|
|
res->argc++;
|
2012-07-29 03:05:53 +00:00
|
|
|
}
|
2012-08-08 01:26:05 +00:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res->dataBytes){
|
|
|
|
res->data=(unsigned char *)&res->buffer[i+1];
|
|
|
|
res->state = STATE_DATA;
|
|
|
|
}else{
|
|
|
|
res->data=NULL;
|
|
|
|
res->state = STATE_READY;
|
|
|
|
}
|
|
|
|
break;
|
2012-07-29 03:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-08 01:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure all the data has arrived
|
|
|
|
if (res->state == STATE_DATA){
|
|
|
|
if (res->bufferBytes >= res->dataBytes + res->cmdBytes){
|
|
|
|
res->state = STATE_READY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, now we can try to process the command
|
|
|
|
if (res->state == STATE_READY){
|
|
|
|
int handled=0;
|
|
|
|
int i;
|
|
|
|
// call all handlers that match (yes there might be more than one)
|
|
|
|
for (i=0;i<handler_count;i++){
|
|
|
|
/* since we know res->cmd is terminated with a '\n',
|
|
|
|
and there shouldn't be a '\n' in h->command,
|
|
|
|
this shouldn't run past the end of the buffer */
|
|
|
|
if (handlers[i].handler && (!handlers[i].command || strcase_startswith(res->cmd,handlers[i].command, NULL))){
|
|
|
|
if (handlers[i].handler(res->cmd, res->argc, res->argv, res->data, res->dataBytes, handlers[i].context)>0)
|
|
|
|
handled=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled){
|
|
|
|
INFOF("Event \"%s\" was not handled", res->cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// shuffle any unprocessed bytes
|
|
|
|
int remaining = res->bufferBytes - (res->dataBytes + res->cmdBytes);
|
|
|
|
if (remaining>0){
|
|
|
|
bcopy(res->buffer+res->dataBytes + res->cmdBytes,res->buffer,remaining);
|
|
|
|
}
|
|
|
|
res->bufferBytes=remaining;
|
|
|
|
res->cmdBytes=0;
|
|
|
|
res->dataBytes=0;
|
|
|
|
res->state = STATE_INIT;
|
|
|
|
res->cmd = (char *)res->buffer;
|
|
|
|
oldOffset = 0;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2012-08-16 04:26:50 +00:00
|
|
|
if (res->bufferBytes >= MONITOR_CLIENT_BUFFER_SIZE)
|
|
|
|
return WHY("Buffer full");
|
2012-08-08 01:26:05 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-07-29 03:05:53 +00:00
|
|
|
}
|