alfred: client: random fixes
cover alfred: client: random fixes
Similar to the batctl fixes, I have now compiled a list of potential fixes for alfred. Instead of posting everything at once, I will be posted in smaller chunks. They are just various (mostly minor) problems which should be tackled at some point.
---
Sven Eckelmann (4):
alfred: fix error code from unix_sock_req_data_reply
alfred: avoid endless-loop when reading data from stdin fails
alfred: close the listening socket on daemon setup errors
alfred: handle short reads when waiting for daemon answer
alfred.h | 1 + client.c | 77 +++++++++++++++++++++++++++++++++++++++---------------------- unix_sock.c | 39 +++++++++++++++++-------------- util.c | 25 ++++++++++++++++++++ 4 files changed, 97 insertions(+), 45 deletions(-)
---
base-commit: 462a0cf40615f62e0912c36d72a75accbb26f5ee
change-id: 20260731-bugfixes-client-0d5ed91e75db
Best regards,
Signature · 1 line
Sven Eckelmann <[email protected]>
PATCH 1/4 alfred: fix error code from unix_sock_req_data_reply
The error code of unix_sock_req_data_reply() was stored in an uint16_t before being returned from the int-returning function. A failed write to the client socket therefore returned 65535 instead of -1.
--- unix_sock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
unix_sock.c +1 -1
@@ -207,7 +207,7 @@ static int unix_sock_req_data_reply(struct globals *globals, int client_sock,struct hash_it_t *hashit =NULL ;uint8_t buf[MAX_PAYLOAD ];uint16_t seqno =0 ; -uint16_t ret =0 ; +int ret =0 ;int len;/* send some data back through the unix socket */
Signature · 1 line
2.47.3
PATCH 2/4 alfred: avoid endless-loop when reading data from stdin fails
In reply to Sven Eckelmannalfred_client_set_data() read the dataset from stdin in a loop guarded only by feof(). fread() returns 0 not only at end of file but also on a read error. A persistent read error therefore made 'alfred -s' spin forever without making progress.
Stop reading as soon as fread() cannot deliver any data, which covers both end of file and read errors.
--- client.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
client.c +11 -0
@@ -153,6 +153,17 @@ int alfred_client_set_data(struct globals *globals) len =sizeof (*push) +sizeof (*data);while (!feof (stdin)) { ret =fread (&buf[len],1 ,sizeof (buf) - len, stdin); +if (ret ==0 ) { +if (ferror (stdin)) { +fprintf (stderr,"%s: failed to read data from stdin\n" , + __func__); +unix_sock_close (globals); +return -1 ; + } + +break ; + } + len += ret;if (sizeof (buf) == len)
Signature · 1 line
2.47.3
PATCH 3/4 alfred: close the listening socket on daemon setup errors
In reply to Sven Eckelmannunix_sock_open_daemon() created the listening unix socket but returned -1 without closing it again when bind(), listen() or the epoll_ctl() registration failed, leaking the descriptor.
--- unix_sock.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
unix_sock.c +8 -3
@@ -51,12 +51,12 @@ int unix_sock_open_daemon(struct globals *globals)if (bind (globals->unix_sock, (struct sockaddr *)&addr,sizeof (addr)) <0 ) {perror ("can't bind unix socket" ); -return -1 ; + goto err; }if (listen (globals->unix_sock,10 ) <0 ) {perror ("can't listen on unix socket" ); -return -1 ; + goto err; } ev.events =EPOLLIN ;
@@ -66,10 +66,15 @@ int unix_sock_open_daemon(struct globals *globals)if (epoll_ctl (globals->epollfd,EPOLL_CTL_ADD , globals->unix_sock, &ev) ==-1 ) {perror ("Failed to add epoll for check_timer" ); -return -1 ; + goto err; }return 0 ; + +err: +close (globals->unix_sock); + globals->unix_sock =-1 ; +return -1 ; }int unix_sock_open_client (struct globals *globals)
Signature · 1 line
2.47.3
PATCH 4/4 alfred: handle short reads when waiting for daemon answer
In reply to Sven EckelmannThe answer of the alfred daemon is received over a SOCK_STREAM unix socket which doesn't preserve message boundaries. read() can therefore return less than the requested number of bytes. Such a short read was treated like an end of stream and the processing was stopped.
Retry the read until the requested amount of data, end of stream or a real error was received (ignoring temporary interruptions EINTR).
--- alfred.h | 1 + client.c | 66 +++++++++++++++++++++++++++++++++++-------------------------- unix_sock.c | 26 ++++++++++++------------ util.c | 25 +++++++++++++++++++++++ 4 files changed, 77 insertions(+), 41 deletions(-)
alfred.h +1 -0
@@ -224,6 +224,7 @@ void netsock_reopen(struct globals *globals);int netsock_own_address (const struct globals *globals,const alfred_addr *address);/* util.c */ +ssize_t read_full (int fd,void *buf,size_t count);int time_diff (struct timespec *tv1,struct timespec *tv2,struct timespec *tvdiff);void time_random_seed (void );
client.c +38 -28
135 lines not on this page · raw message
unix_sock.c +13 -13
@@ -542,26 +542,26 @@ static void unix_sock_read(struct globals *globals,return ; } -/* we assume that we can instantly read here. */ - length =read (client_sock, buf,sizeof (buf)); -if (length <=0 ) { + headsize =sizeof (*packet); + packet = (struct alfred_tlv *)buf; + +/* drop too small packets */ +if (read_full (client_sock, buf, headsize) < headsize) {perror ("read from unix socket failed" ); goto err; } -/* drop too small packets */ - headsize =sizeof (*packet); -if (length < headsize) - goto err; - - packet = (struct alfred_tlv *)buf; - -if ((length - headsize) < ((int )ntohs (packet->length))) - goto err; -if (packet->version !=ALFRED_VERSION ) goto err; + length =ntohs (packet->length); +if (length > (int )(sizeof (buf) - headsize)) + goto err; + +/* read the announced rest of the request */ +if (read_full (client_sock, buf + headsize, length) < length) + goto err; +switch (packet->type) {case ALFRED_PUSH_DATA : unix_sock_add_data(globals,
util.c +25 -0
@@ -6,6 +6,7 @@* License-Filename: LICENSES/preferred/GPL-2.0 */ +#include <errno.h> #include <netinet/ether.h> #include <stdbool.h> #include <stddef.h>
@@ -19,6 +20,30 @@#include <unistd.h> #include "alfred.h" +ssize_t read_full (int fd,void *buf,size_t count) +{ +size_t read_len =0 ; +uint8_t *pos = buf; +ssize_t ret; + +while (read_len < count) { + ret =read (fd, pos + read_len, count - read_len); +if (ret <0 ) { +if (errno ==EINTR ) +continue ; + +return ret; + } + +if (ret ==0 ) +break ; + + read_len += ret; + } + +return read_len; +} +int time_diff (struct timespec *tv1,struct timespec *tv2,struct timespec *tvdiff) { tvdiff->tv_sec = tv1->tv_sec - tv2->tv_sec;
Signature · 1 line
2.47.3