mirror of
https://github.com/lxsang/antd-tunnel-publishers
synced 2024-12-26 01:48:21 +01:00
add generic purpose FIFO service to the tunnel
This commit is contained in:
parent
f0ec25e00a
commit
d2aa1f3557
@ -15,4 +15,4 @@ install-data-local:
|
||||
|
||||
EXTRA_DIST = runner.ini runnerd tunnel.h antd-tunnel-publisher.service log.h
|
||||
|
||||
SUBDIRS = . vterm
|
||||
SUBDIRS = . vterm wfifo
|
@ -68,7 +68,7 @@ AM_CONDITIONAL([OSX], [test "$build_mac" = "yes"])
|
||||
# find a file called Makefile.in, substitute placeholders
|
||||
# like @PACKAGE_VERSION@ with values like 0.1.0a,
|
||||
# and write the results to Makefile.
|
||||
AC_CONFIG_FILES([Makefile vterm/Makefile])
|
||||
AC_CONFIG_FILES([Makefile vterm/Makefile wfifo/Makefile])
|
||||
|
||||
# output the script:
|
||||
AC_OUTPUT
|
BIN
dist/antd-publishers-0.1.0a.tar.gz
vendored
BIN
dist/antd-publishers-0.1.0a.tar.gz
vendored
Binary file not shown.
28
runner.c
28
runner.c
@ -14,7 +14,7 @@
|
||||
|
||||
static volatile int running = 1;
|
||||
static list_t pids;
|
||||
static char socket_file[BUFFLEN];
|
||||
|
||||
static void int_handler(int dummy) {
|
||||
(void) dummy;
|
||||
running = 0;
|
||||
@ -24,17 +24,29 @@ static int ini_handle(void *user_data, const char *section, const char *name,
|
||||
const char *value)
|
||||
{
|
||||
pid_t pid;
|
||||
char* argv[3];
|
||||
char* argv[11];
|
||||
list_t list;
|
||||
item_t item;
|
||||
int i;
|
||||
UNUSED(user_data);
|
||||
if (EQU(section, "RUNNER"))
|
||||
{
|
||||
if(EQU(name, "socket"))
|
||||
if(EQU(name, "service"))
|
||||
{
|
||||
M_LOG(MODULE_NAME, "Configuration file is %s", value);
|
||||
(void)strncpy(socket_file,value,BUFFLEN);
|
||||
list = split(value, " ");
|
||||
i = list_size(list);
|
||||
if( i > 10)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Too many arguments %d, expected max 10", i);
|
||||
return 0;
|
||||
}
|
||||
else if(EQU(name, "service"))
|
||||
i = 0;
|
||||
list_for_each(item, list)
|
||||
{
|
||||
argv[i] = item->value.ptr;
|
||||
i++;
|
||||
}
|
||||
argv[i] = NULL;
|
||||
M_LOG(MODULE_NAME, "Running service %s...", value);
|
||||
pid = fork();
|
||||
if(pid == -1)
|
||||
@ -45,14 +57,12 @@ static int ini_handle(void *user_data, const char *section, const char *name,
|
||||
if(pid == 0)
|
||||
{
|
||||
// child
|
||||
argv[0] = (char*)value;
|
||||
argv[1] = (char*)socket_file;
|
||||
argv[2] = NULL;
|
||||
execve(argv[0], &argv[0], NULL);
|
||||
// Nothing below this line should be executed by child process. If so,
|
||||
// it means that the execl function wasn't successfull, so lets exit:
|
||||
_exit(1);
|
||||
}
|
||||
list_free(&list);
|
||||
// parent
|
||||
list_put_i(&pids, pid);
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
[RUNNER]
|
||||
socket=/opt/www/tmp/channels/antd_hotline.sock
|
||||
service=/opt/www/bin/vterm
|
||||
service=/opt/www/bin/vterm /opt/www/tmp/channels/antd_hotline.sock
|
||||
service=/opt/www/bin/wfifo /opt/www/tmp/channels/antd_hotline.sock notification /var/wfifo_notification r
|
||||
;service=/opt/www/bin/wfifo /opt/www/tmp/channels/antd_hotline.sock server_to_client /var/wfifo_s2c r
|
||||
;service=/opt/www/bin/wfifo /opt/www/tmp/channels/antd_hotline.sock client_to_server /var/wfifo_c2s w
|
BIN
vterm/vterm
BIN
vterm/vterm
Binary file not shown.
@ -401,7 +401,6 @@ int main(int argc, char** argv)
|
||||
if(msg_read(fd, &msg) == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to read message from channel. quit");
|
||||
(void) close(fd);
|
||||
running = 0;
|
||||
}
|
||||
else
|
||||
|
11
wfifo/Makefile.am
Normal file
11
wfifo/Makefile.am
Normal file
@ -0,0 +1,11 @@
|
||||
AUTOMAKE_OPTIONS = foreign
|
||||
|
||||
|
||||
|
||||
AM_CPPFLAGS = -W -Wall -g -std=c99
|
||||
|
||||
# bin
|
||||
bin_PROGRAMS = wfifo
|
||||
# source files
|
||||
wfifo_SOURCES = wfifo.c ../tunnel.c
|
||||
# antd_LDADD = libantd.la
|
BIN
wfifo/wfifo
Executable file
BIN
wfifo/wfifo
Executable file
Binary file not shown.
252
wfifo/wfifo.c
Normal file
252
wfifo/wfifo.c
Normal file
@ -0,0 +1,252 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <antd/list.h>
|
||||
#include <antd/bst.h>
|
||||
|
||||
#include "../tunnel.h"
|
||||
|
||||
#define MODULE_NAME "wfifo"
|
||||
|
||||
static bst_node_t* clients = NULL;
|
||||
|
||||
static volatile int running = 1;
|
||||
|
||||
static void int_handler(int dummy) {
|
||||
(void) dummy;
|
||||
running = 0;
|
||||
}
|
||||
static void send_data(bst_node_t* node, void** argv, int argc)
|
||||
{
|
||||
(void)argc;
|
||||
tunnel_msg_t* msg = (tunnel_msg_t*) argv[0];
|
||||
int *fd = (int*) argv[1];
|
||||
msg->header.client_id = node->key;
|
||||
if(msg_write(*fd, msg) == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to write data message to client %d", node->key);
|
||||
}
|
||||
}
|
||||
static void unsubscribe(bst_node_t* node, void** args, int argc)
|
||||
{
|
||||
(void) argc;
|
||||
tunnel_msg_t msg;
|
||||
int* ufd = (int*) args[0];
|
||||
msg.header.type = CHANNEL_UNSUBSCRIBE;
|
||||
msg.header.client_id = node->key;
|
||||
msg.header.size = 0;
|
||||
if(msg_write(*ufd, &msg) == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to request unsubscribe to client %d", node->key);
|
||||
}
|
||||
}
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int fd, fifo_fd;
|
||||
tunnel_msg_t msg;
|
||||
fd_set fd_in;
|
||||
int status, maxfd;
|
||||
struct timeval timeout;
|
||||
char buff[BUFFLEN+1];
|
||||
void* fargv[2];
|
||||
uint8_t* tmp;
|
||||
LOG_INIT(MODULE_NAME);
|
||||
|
||||
if(argc != 5)
|
||||
{
|
||||
printf("Usage: %s path/to/hotline/socket channel_name input_file\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
signal(SIGABRT, SIG_IGN);
|
||||
signal(SIGINT, int_handler);
|
||||
// create the fifo first
|
||||
(void) unlink(argv[3]);
|
||||
if(mkfifo(argv[3], 0666) == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to create FIFO %s: %s", argv[3], strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
fifo_fd = open(argv[3], O_RDWR);
|
||||
if(fifo_fd == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to open FIFO %s: %s", argv[3], strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
M_LOG(MODULE_NAME, "FIFO: %s created", argv[3]);
|
||||
|
||||
M_LOG(MODULE_NAME, "Hotline is: %s", argv[1]);
|
||||
// now try to request new channel from hotline
|
||||
fd = open_unix_socket(argv[1]);
|
||||
if(fd == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to open the hotline: %s", argv[1]);
|
||||
(void) close(fifo_fd);
|
||||
return -1;
|
||||
}
|
||||
msg.header.type = CHANNEL_OPEN;
|
||||
msg.header.channel_id = 0;
|
||||
msg.header.client_id = 0;
|
||||
M_LOG(MODULE_NAME, "Request to open the channel %s", argv[2]);
|
||||
(void)strncpy(buff, argv[2],MAX_CHANNEL_NAME);
|
||||
msg.header.size = strlen(buff);
|
||||
msg.data = (uint8_t*) buff;
|
||||
if(msg_write(fd, &msg) == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to write message to hotline");
|
||||
(void) close(fd);
|
||||
(void) close(fifo_fd);
|
||||
return -1;
|
||||
}
|
||||
M_LOG(MODULE_NAME, "Wait for comfirm creation of %s", argv[2]);
|
||||
// now wait for message
|
||||
if(msg_read(fd, &msg) == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to read message from hotline");
|
||||
(void) close(fd);
|
||||
(void) close(fifo_fd);
|
||||
return -1;
|
||||
}
|
||||
if(msg.header.type == CHANNEL_OK)
|
||||
{
|
||||
M_LOG(MODULE_NAME, "Channel created: %s", argv[2]);
|
||||
if(msg.data)
|
||||
free(msg.data);
|
||||
}
|
||||
|
||||
// now read data
|
||||
while(running)
|
||||
{
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 500;
|
||||
FD_ZERO(&fd_in);
|
||||
FD_SET(fd, &fd_in);
|
||||
FD_SET(fifo_fd, &fd_in);
|
||||
maxfd = fd > fifo_fd? fd: fifo_fd;
|
||||
|
||||
status = select(maxfd + 1, &fd_in, NULL, NULL, &timeout);
|
||||
|
||||
switch (status)
|
||||
{
|
||||
case -1:
|
||||
M_LOG(MODULE_NAME, "Error %d on select()\n", errno);
|
||||
running = 0;
|
||||
break;
|
||||
case 0:
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 10000; // 5 ms
|
||||
select(0, NULL, NULL, NULL, &timeout);
|
||||
break;
|
||||
// we have data
|
||||
default:
|
||||
if (FD_ISSET(fd, &fd_in))
|
||||
{
|
||||
if(msg_read(fd, &msg) == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to read message from channel. quit");
|
||||
running = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (msg.header.type)
|
||||
{
|
||||
case CHANNEL_SUBSCRIBE:
|
||||
M_LOG(MODULE_NAME, "Client %d subscribes to the chanel", msg.header.client_id);
|
||||
clients = bst_insert(clients,msg.header.client_id, NULL);
|
||||
break;
|
||||
|
||||
case CHANNEL_UNSUBSCRIBE:
|
||||
M_LOG(MODULE_NAME, "Client %d unsubscribes to the chanel", msg.header.client_id);
|
||||
clients = bst_delete(clients,msg.header.client_id);
|
||||
break;
|
||||
|
||||
case CHANNEL_DATA:
|
||||
if(argv[4][0] == 'w')
|
||||
{
|
||||
// write data to the FIFO
|
||||
if(msg.header.size >0)
|
||||
{
|
||||
if( write(fifo_fd, msg.data, msg.header.size) == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to write data to the FIFO %s from client %d: %s", argv[3], msg.header.client_id, strerror(errno));
|
||||
running = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
(void)snprintf(buff, BUFFLEN, "Channel is read only");
|
||||
msg.header.type = CHANNEL_ERROR;
|
||||
msg.header.size = strlen(buff);
|
||||
tmp = msg.data;
|
||||
msg.data = buff;
|
||||
if(msg_write(fd, &msg) == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to write message to hotline");
|
||||
running = 0;
|
||||
}
|
||||
msg.data = tmp;
|
||||
M_ERROR(MODULE_NAME, "Channel is read only %s(%d)", argv[3], msg.header.client_id);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
M_LOG(MODULE_NAME, "Client %d send message of type %d",
|
||||
msg.header.client_id, msg.header.type);
|
||||
break;
|
||||
}
|
||||
if(msg.data)
|
||||
{
|
||||
free(msg.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(FD_ISSET(fifo_fd, &fd_in) && argv[4][0] == 'r')
|
||||
{
|
||||
// on the fifo side
|
||||
if((status = read(fifo_fd,buff, BUFFLEN)) == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to read data from the FIFO %s: %s", argv[3], strerror(errno));
|
||||
running = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
msg.header.type = CHANNEL_DATA;
|
||||
msg.header.size = status;
|
||||
msg.data = buff;
|
||||
fargv[0] = (void*) &msg;
|
||||
fargv[1] = (void*) &fd;
|
||||
bst_for_each(clients, send_data, fargv, 2);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
// unsubscribe all client
|
||||
fargv[0] = (void*) &fd;
|
||||
bst_for_each(clients, unsubscribe, fargv, 1);
|
||||
|
||||
// close the channel
|
||||
M_LOG(MODULE_NAME, "Close the channel %s (%d)", argv[2], fd);
|
||||
msg.header.type = CHANNEL_CLOSE;
|
||||
msg.header.size = 0;
|
||||
msg.data = NULL;
|
||||
if( msg_write(fd, &msg) == -1)
|
||||
{
|
||||
M_ERROR(MODULE_NAME, "Unable to request channel close");
|
||||
}
|
||||
// close all opened terminal
|
||||
|
||||
(void)msg_read(fd, &msg);
|
||||
(void) close(fd);
|
||||
(void) close(fifo_fd);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user