update to new API

This commit is contained in:
lxsang 2020-09-13 01:46:11 +02:00
parent 63688c416f
commit a8979c48dc
7 changed files with 130 additions and 136 deletions

View File

@ -1,12 +1,13 @@
[Unit] [Unit]
Description=Antd Publisher Service Description=Antd Publisher Service
After=antd.target After=antd.service
[Service] [Service]
Type=simple Type=simple
User=root User=root
WorkingDirectory=/opt/www WorkingDirectory=/opt/www
ExecStart=/opt/www/bin/runnerd ExecStart=/opt/www/bin/runnerd
Restart=always
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

Binary file not shown.

View File

@ -8,6 +8,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <antd/list.h> #include <antd/list.h>
#include <antd/ini.h> #include <antd/ini.h>
#include <antd/utils.h>
#include "log.h" #include "log.h"
#define MODULE_NAME "runner" #define MODULE_NAME "runner"

Binary file not shown.

View File

@ -13,6 +13,7 @@
#include <antd/list.h> #include <antd/list.h>
#include <antd/bst.h> #include <antd/bst.h>
#include <antd/utils.h>
#include "../tunnel.h" #include "../tunnel.h"
@ -368,8 +369,6 @@ int main(int argc, char** argv)
// now read data // now read data
while(running) while(running)
{ {
timeout.tv_sec = 0;
timeout.tv_usec = 500;
FD_ZERO(&fd_in); FD_ZERO(&fd_in);
FD_SET(fd, &fd_in); FD_SET(fd, &fd_in);
maxfd = fd; maxfd = fd;
@ -388,7 +387,7 @@ int main(int argc, char** argv)
} }
list_free(&list); list_free(&list);
status = select(maxfd + 1, &fd_in, NULL, NULL, &timeout); status = select(maxfd + 1, &fd_in, NULL, NULL, NULL);
switch (status) switch (status)
{ {
@ -397,9 +396,6 @@ int main(int argc, char** argv)
running = 0; running = 0;
break; break;
case 0: case 0:
timeout.tv_sec = 0;
timeout.tv_usec = 10000; // 5 ms
select(0, NULL, NULL, NULL, &timeout);
break; break;
// we have data // we have data
default: default:

Binary file not shown.

View File

@ -8,59 +8,62 @@
#include <sys/select.h> #include <sys/select.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <antd/list.h> #include <antd/list.h>
#include <antd/bst.h> #include <antd/bst.h>
#include <antd/utils.h>
#include "../tunnel.h" #include "../tunnel.h"
#define MODULE_NAME "wfifo" #define MODULE_NAME "wfifo"
static bst_node_t* clients = NULL; static bst_node_t *clients = NULL;
static volatile int running = 1; static volatile int running = 1;
static void int_handler(int dummy) { static void int_handler(int dummy)
(void) dummy; {
(void)dummy;
running = 0; running = 0;
} }
static void send_data(bst_node_t* node, void** argv, int argc) static void send_data(bst_node_t *node, void **argv, int argc)
{ {
(void)argc; (void)argc;
tunnel_msg_t* msg = (tunnel_msg_t*) argv[0]; tunnel_msg_t *msg = (tunnel_msg_t *)argv[0];
int *fd = (int*) argv[1]; int *fd = (int *)argv[1];
msg->header.client_id = node->key; msg->header.client_id = node->key;
if(msg_write(*fd, msg) == -1) if (msg_write(*fd, msg) == -1)
{ {
M_ERROR(MODULE_NAME, "Unable to write data message to client %d", node->key); 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) static void unsubscribe(bst_node_t *node, void **args, int argc)
{ {
(void) argc; (void)argc;
tunnel_msg_t msg; tunnel_msg_t msg;
int* ufd = (int*) args[0]; int *ufd = (int *)args[0];
msg.header.type = CHANNEL_UNSUBSCRIBE; msg.header.type = CHANNEL_UNSUBSCRIBE;
msg.header.client_id = node->key; msg.header.client_id = node->key;
msg.header.size = 0; msg.header.size = 0;
if(msg_write(*ufd, &msg) == -1) if (msg_write(*ufd, &msg) == -1)
{ {
M_ERROR(MODULE_NAME, "Unable to request unsubscribe to client %d", node->key); M_ERROR(MODULE_NAME, "Unable to request unsubscribe to client %d", node->key);
} }
} }
int main(int argc, char** argv) int main(int argc, char **argv)
{ {
int fd, fifo_fd; int fd, fifo_fd;
tunnel_msg_t msg; tunnel_msg_t msg;
fd_set fd_in; fd_set fd_in;
int status, maxfd; int status, maxfd;
struct timeval timeout; char buff[BUFFLEN + 1];
char buff[BUFFLEN+1]; void *fargv[2];
void* fargv[2]; uint8_t *tmp;
uint8_t* tmp;
LOG_INIT(MODULE_NAME); LOG_INIT(MODULE_NAME);
if(argc != 5) if (argc != 5)
{ {
printf("Usage: %s path/to/hotline/socket channel_name input_file\n", argv[0]); printf("Usage: %s path/to/hotline/socket channel_name input_file\n", argv[0]);
return -1; return -1;
@ -69,14 +72,14 @@ int main(int argc, char** argv)
signal(SIGABRT, SIG_IGN); signal(SIGABRT, SIG_IGN);
signal(SIGINT, int_handler); signal(SIGINT, int_handler);
// create the fifo first // create the fifo first
(void) unlink(argv[3]); (void)unlink(argv[3]);
if(mkfifo(argv[3], 0666) == -1) if (mkfifo(argv[3], 0666) == -1)
{ {
M_ERROR(MODULE_NAME, "Unable to create FIFO %s: %s", argv[3], strerror(errno)); M_ERROR(MODULE_NAME, "Unable to create FIFO %s: %s", argv[3], strerror(errno));
return -1; return -1;
} }
fifo_fd = open(argv[3], O_RDWR); fifo_fd = open(argv[3], O_RDWR);
if(fifo_fd == -1) if (fifo_fd == -1)
{ {
M_ERROR(MODULE_NAME, "Unable to open FIFO %s: %s", argv[3], strerror(errno)); M_ERROR(MODULE_NAME, "Unable to open FIFO %s: %s", argv[3], strerror(errno));
return -1; return -1;
@ -86,61 +89,58 @@ int main(int argc, char** argv)
M_LOG(MODULE_NAME, "Hotline is: %s", argv[1]); M_LOG(MODULE_NAME, "Hotline is: %s", argv[1]);
// now try to request new channel from hotline // now try to request new channel from hotline
fd = open_unix_socket(argv[1]); fd = open_unix_socket(argv[1]);
if(fd == -1) if (fd == -1)
{ {
M_ERROR(MODULE_NAME, "Unable to open the hotline: %s", argv[1]); M_ERROR(MODULE_NAME, "Unable to open the hotline: %s", argv[1]);
(void) close(fifo_fd); (void)close(fifo_fd);
return -1; return -1;
} }
msg.header.type = CHANNEL_OPEN; msg.header.type = CHANNEL_OPEN;
msg.header.channel_id = 0; msg.header.channel_id = 0;
msg.header.client_id = 0; msg.header.client_id = 0;
M_LOG(MODULE_NAME, "Request to open the channel %s", argv[2]); M_LOG(MODULE_NAME, "Request to open the channel %s", argv[2]);
(void)strncpy(buff, argv[2],MAX_CHANNEL_NAME); (void)strncpy(buff, argv[2], MAX_CHANNEL_NAME);
msg.header.size = strlen(buff); msg.header.size = strlen(buff);
msg.data = (uint8_t*) buff; msg.data = (uint8_t *)buff;
if(msg_write(fd, &msg) == -1) if (msg_write(fd, &msg) == -1)
{ {
M_ERROR(MODULE_NAME, "Unable to write message to hotline"); M_ERROR(MODULE_NAME, "Unable to write message to hotline");
(void) close(fd); (void)close(fd);
(void) close(fifo_fd); (void)close(fifo_fd);
return -1; return -1;
} }
M_LOG(MODULE_NAME, "Wait for comfirm creation of %s", argv[2]); M_LOG(MODULE_NAME, "Wait for comfirm creation of %s", argv[2]);
// now wait for message // now wait for message
if(msg_read(fd, &msg) == -1) if (msg_read(fd, &msg) == -1)
{ {
M_ERROR(MODULE_NAME, "Unable to read message from hotline"); M_ERROR(MODULE_NAME, "Unable to read message from hotline");
(void) close(fd); (void)close(fd);
(void) close(fifo_fd); (void)close(fifo_fd);
return -1; return -1;
} }
if(msg.header.type == CHANNEL_OK) if (msg.header.type == CHANNEL_OK)
{ {
M_LOG(MODULE_NAME, "Channel created: %s", argv[2]); M_LOG(MODULE_NAME, "Channel created: %s", argv[2]);
if(msg.data) if (msg.data)
free(msg.data); free(msg.data);
} }
else else
{ {
M_ERROR(MODULE_NAME, "Channel is not created: %s. Tunnel service responds with msg of type %d", argv[2], msg.header.type); M_ERROR(MODULE_NAME, "Channel is not created: %s. Tunnel service responds with msg of type %d", argv[2], msg.header.type);
if(msg.data) if (msg.data)
free(msg.data); free(msg.data);
running = 0; running = 0;
} }
// now read data // now read data
while(running) while (running)
{ {
timeout.tv_sec = 0;
timeout.tv_usec = 500;
FD_ZERO(&fd_in); FD_ZERO(&fd_in);
FD_SET(fd, &fd_in); FD_SET(fd, &fd_in);
FD_SET(fifo_fd, &fd_in); FD_SET(fifo_fd, &fd_in);
maxfd = fd > fifo_fd? fd: fifo_fd; maxfd = fd > fifo_fd ? fd : fifo_fd;
status = select(maxfd + 1, &fd_in, NULL, NULL, &timeout); status = select(maxfd + 1, &fd_in, NULL, NULL, NULL);
switch (status) switch (status)
{ {
@ -149,15 +149,12 @@ int main(int argc, char** argv)
running = 0; running = 0;
break; break;
case 0: case 0:
timeout.tv_sec = 0;
timeout.tv_usec = 10000; // 5 ms
select(0, NULL, NULL, NULL, &timeout);
break; break;
// we have data // we have data
default: default:
if (FD_ISSET(fd, &fd_in)) if (FD_ISSET(fd, &fd_in))
{ {
if(msg_read(fd, &msg) == -1) if (msg_read(fd, &msg) == -1)
{ {
M_ERROR(MODULE_NAME, "Unable to read message from channel. quit"); M_ERROR(MODULE_NAME, "Unable to read message from channel. quit");
running = 0; running = 0;
@ -168,21 +165,21 @@ int main(int argc, char** argv)
{ {
case CHANNEL_SUBSCRIBE: case CHANNEL_SUBSCRIBE:
M_LOG(MODULE_NAME, "Client %d subscribes to the chanel", msg.header.client_id); M_LOG(MODULE_NAME, "Client %d subscribes to the chanel", msg.header.client_id);
clients = bst_insert(clients,msg.header.client_id, NULL); clients = bst_insert(clients, msg.header.client_id, NULL);
break; break;
case CHANNEL_UNSUBSCRIBE: case CHANNEL_UNSUBSCRIBE:
M_LOG(MODULE_NAME, "Client %d unsubscribes to the chanel", msg.header.client_id); M_LOG(MODULE_NAME, "Client %d unsubscribes to the chanel", msg.header.client_id);
clients = bst_delete(clients,msg.header.client_id); clients = bst_delete(clients, msg.header.client_id);
break; break;
case CHANNEL_DATA: case CHANNEL_DATA:
if(argv[4][0] == 'w') if (argv[4][0] == 'w')
{ {
// write data to the FIFO // write data to the FIFO
if(msg.header.size >0) if (msg.header.size > 0)
{ {
if( write(fifo_fd, msg.data, msg.header.size) == -1) 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)); 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; running = 0;
@ -195,8 +192,8 @@ int main(int argc, char** argv)
msg.header.type = CHANNEL_ERROR; msg.header.type = CHANNEL_ERROR;
msg.header.size = strlen(buff); msg.header.size = strlen(buff);
tmp = msg.data; tmp = msg.data;
msg.data = (uint8_t*)buff; msg.data = (uint8_t *)buff;
if(msg_write(fd, &msg) == -1) if (msg_write(fd, &msg) == -1)
{ {
M_ERROR(MODULE_NAME, "Unable to write message to hotline"); M_ERROR(MODULE_NAME, "Unable to write message to hotline");
running = 0; running = 0;
@ -211,16 +208,16 @@ int main(int argc, char** argv)
msg.header.client_id, msg.header.type); msg.header.client_id, msg.header.type);
break; break;
} }
if(msg.data) if (msg.data)
{ {
free(msg.data); free(msg.data);
} }
} }
} }
else if(FD_ISSET(fifo_fd, &fd_in) && argv[4][0] == 'r') else if (FD_ISSET(fifo_fd, &fd_in) && argv[4][0] == 'r')
{ {
// on the fifo side // on the fifo side
if((status = read(fifo_fd,buff, BUFFLEN)) == -1) 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)); M_ERROR(MODULE_NAME, "Unable to read data from the FIFO %s: %s", argv[3], strerror(errno));
running = 0; running = 0;
@ -229,17 +226,16 @@ int main(int argc, char** argv)
{ {
msg.header.type = CHANNEL_DATA; msg.header.type = CHANNEL_DATA;
msg.header.size = status; msg.header.size = status;
msg.data = (uint8_t*)buff; msg.data = (uint8_t *)buff;
fargv[0] = (void*) &msg; fargv[0] = (void *)&msg;
fargv[1] = (void*) &fd; fargv[1] = (void *)&fd;
bst_for_each(clients, send_data, fargv, 2); bst_for_each(clients, send_data, fargv, 2);
} }
} }
} }
} }
// unsubscribe all client // unsubscribe all client
fargv[0] = (void*) &fd; fargv[0] = (void *)&fd;
bst_for_each(clients, unsubscribe, fargv, 1); bst_for_each(clients, unsubscribe, fargv, 1);
// close the channel // close the channel
@ -247,14 +243,14 @@ int main(int argc, char** argv)
msg.header.type = CHANNEL_CLOSE; msg.header.type = CHANNEL_CLOSE;
msg.header.size = 0; msg.header.size = 0;
msg.data = NULL; msg.data = NULL;
if( msg_write(fd, &msg) == -1) if (msg_write(fd, &msg) == -1)
{ {
M_ERROR(MODULE_NAME, "Unable to request channel close"); M_ERROR(MODULE_NAME, "Unable to request channel close");
} }
// close all opened terminal // close all opened terminal
(void)msg_read(fd, &msg); (void)msg_read(fd, &msg);
(void) close(fd); (void)close(fd);
(void) close(fifo_fd); (void)close(fifo_fd);
return 0; return 0;
} }