Compare commits

..

No commits in common. "b8cbb8a7a013c547187c4263ef4187c60eb25061" and "1e247d1c89bd3b3266550a85e7c9db7c743a56da" have entirely different histories.

95
irena.c
View file

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