Compare commits

...

5 commits

Author SHA1 Message Date
Stephan I. Böttcher
b8cbb8a7a0 send_file(): explicitly base64, := 2023-11-16 00:49:09 +01:00
Stephan I. Böttcher
5c6aae5fdb verbosity work 2023-11-15 14:56:45 +01:00
Stephan I. Böttcher
15a8c657f2 acmd socket and mainloop fixes 2023-11-14 23:12:02 +01:00
Stephan I. Böttcher
ce9718a550 impelemt print_asocket() 2023-11-14 22:59:04 +01:00
Stephan I. Böttcher
7f728c698f add maintainance mode 2023-11-14 09:10:49 +01:00

97
irena.c
View file

@ -26,26 +26,15 @@ const char version[] = "$Id$";
#include <sys/un.h>
#include <netinet/in.h>
#define DEBUG
FILE *mout;
int verbosity = 1;
int next_verbosity = 1;
static inline void silent(int v) {
if (verbosity < v)
next_verbosity = 0;
}
static inline void loud(int v) {
if (v > verbosity)
next_verbosity = v;
}
static inline int verbose(int v)
{
v = next_verbosity >= v;
next_verbosity = verbosity;
return v;
}
int maintainance = 0;
int verbosity[2] = {1,1};
static inline void silent(int v) { if (verbosity[0] < v) verbosity[1] = 0; }
static inline void unsilent() { verbosity[1] = verbosity[0]; }
static inline void loud(int v) { if (v > verbosity[0]) verbosity[1] = v; }
static inline int verbose(int v) { v = verbosity[1] >= v; unsilent(); return v; }
// replacement for perror()
void merror(const char *s)
@ -55,8 +44,9 @@ void merror(const char *s)
fprintf(mout, "%s: %s\n", s, strerror(e));
}
#define DEBUG 3
#ifdef DEBUG
# define debug(fmt, ...) fprintf(mout, fmt, __VA_ARGS__)
# define debug(fmt, ...) if (verbose(DEBUG)) fprintf(mout, fmt, __VA_ARGS__)
#else
# define debug(fmt, ...)
#endif
@ -1961,8 +1951,11 @@ int send_irena_command(const char *c)
}
no_uart=0;
time_t t = time(0);
if (last_irena_command_sent+1 >= t)
if (last_irena_command_sent+1 >= t) {
if (verbose(3))
fprintf(mout, "IRENA CMD postponed: %s\n", c);
return -2;
}
size_t sl = strlen(c);
int n = write(i_buf.fd, c, sl);
last_irena_command_sent = t;
@ -2003,6 +1996,7 @@ void send_fct(time_t t)
fct_ack = -1;
fct_status = fct_size;
}
unsilent();
return;
}
snprintf(fct_cmd, sizeof(fct_cmd), "u/f=%d", fct_size);
@ -2020,6 +2014,7 @@ void send_fct(time_t t)
fct_got_data = 0;
fct_sent++;
}
unsilent();
}
}
@ -2100,7 +2095,7 @@ int send_file(const char *fn)
for (int i=0; i < n; ) {
char cmd[64];
int retry = 0;
int c = snprintf(cmd, 64, "v f[%d]=:", i>>2);
int c = snprintf(cmd, 64, "v f[%d]=:=", i>>2);
while (c<58) {
int nn = -i & 3;
if (c < 54 || nn > n-i)
@ -2227,6 +2222,20 @@ int close_cmd_socket()
return 0;
}
void print_asocket(FILE *mout)
{
char *mode = cmd_socket_flags & SOCK_SHALL_BE_MOUT ? "interactive " : "";
if (acmd_socket_addr.g.sa_family == AF_INET) {
unsigned int ip = ntohl(acmd_socket_addr.i.sin_addr.s_addr);
fprintf(mout, "%ssocket connection from %d.%d.%d.%d:%d\n",
mode,
(ip>>24) & 0xff, (ip>>16) & 0xff, (ip>>8) & 0xff, ip & 0xff,
ntohs(acmd_socket_addr.i.sin_port) );
}
else if (acmd_socket_addr.g.sa_family == AF_UNIX)
fprintf(mout, "%sunix socket connection\n", mode);
}
int open_cmd_socket(int na, char **av)
{
if (!strcmp(av[na-1], "close")) {
@ -2263,6 +2272,7 @@ int open_cmd_socket(int na, char **av)
else if (na==1) {
is_open:
fprintf(mout, "command socket is %s«%s»\n", mode, cmd_socket_name());
print_asocket(mout);
return 0;
}
else {
@ -2282,6 +2292,7 @@ int open_cmd_socket(int na, char **av)
setsockopt(cmd_socket_fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
if (bind(cmd_socket_fd, &cmd_socket_addr.g, sizeof(cmd_socket_addr.u))<0) {
merror("bind");
goto error;
}
@ -2327,10 +2338,7 @@ int accept_cmd_socket(struct line_buffer *b, int fd)
buffer_reset(b);
b->fd = sfd;
char *mode = "";
if (cmd_socket_flags & SOCK_SHALL_BE_MOUT) {
mode = "interactive ";
int fd = dup(sfd);
if (fd<0) {
merror("dup socket");
@ -2347,15 +2355,9 @@ int accept_cmd_socket(struct line_buffer *b, int fd)
fprintf(mout, "irena %s\n", version);
}
if (acmd_socket_addr.g.sa_family == AF_INET) {
unsigned int ip = ntohl(acmd_socket_addr.i.sin_addr.s_addr);
fprintf(stderr, "accepting %ssocket connection from %d.%d.%d.%d:%d\n",
mode,
(ip>>24) & 0xff, (ip>>16) & 0xff, (ip>>8) & 0xff, ip & 0xff,
ntohs(acmd_socket_addr.i.sin_port) );
}
else
fprintf(stderr, "accepting %sunix socket connection\n", mode);
if (verbose(0))
print_asocket(stderr);
return sfd;
}
@ -2368,12 +2370,17 @@ int close_acmd_socket(struct line_buffer *b)
if (r)
merror("fclose socket");
}
if (b->fd >= 0) {
close(b->fd);
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, b->fd, 0);
if (verbose(0)) {
print_asocket(stderr);
fprintf(stderr, "closing socket connection\n");
}
if (b->fd >= 0) {
close(b->fd);
if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, b->fd, 0))
merror("EPOLL_CTL_DEL acmd socket");
}
b->fd = -1;
cmd_socket_addr.g.sa_family = AF_UNSPEC;
return 0;
}
@ -2464,7 +2471,8 @@ struct numbers {
{.name="time_resolution", .doc="seconds", .i=&time_resolution, .flags=NU_INT},
{.name="highgain", .doc="ch#", .i=high_gain_ch, .flags=NU_INT, .size=18},
{.name="lowgain", .doc="ch#", .i=low_gain_ch, .flags=NU_INT, .size=18},
{.name="verbosity", .doc="int", .i=&verbosity, .flags=NU_INT},
{.name="verbosity", .doc="int", .i=verbosity, .flags=NU_INT, .size=2},
{.name="maintainance", .doc="int", .i=&maintainance, .flags=NU_INT},
{.name=0}
};
@ -3003,7 +3011,7 @@ int main(int argc, char **argv)
close_acmd_socket(&s_buf);
while (l) {
process_cmd(l);
l = get_line(&c_buf, 0);
l = get_line(&s_buf, 0);
}
}
else if (eev[i].data.fd) {
@ -3013,14 +3021,23 @@ int main(int argc, char **argv)
char *l = get_line(&c_buf, eev[i].events);
while (l) {
process_cmd(l);
if (prompt)
if (prompt) {
if (maintainance)
fprintf(stderr, "M:%s", prompt);
else
fprintf(stderr, prompt);
}
l = get_line(&c_buf, 0);
}
if (eev[i].events & EPOLLHUP)
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, 0, 0);
if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, 0, 0))
merror("EPOLL_CTL_DEL <stdin>");
}
}
if (maintainance)
continue;
int tick = t/time_resolution;
if (tick != last_tick) {
sync_clocks(t);