Commit 3a3c71e1 authored by Alessandro Rubini's avatar Alessandro Rubini

unix sockets: better error management

set to -1 all unused sockets, so FS_SET won't crash if, for example,
we request UDP on a port with no IPV4 address, after the message:

   unix_open_ch: ioctl(SIOCGIFADDR): Cannot assign requested address

While at it, I factorized some common code.
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 4184f7ea
...@@ -61,6 +61,9 @@ int main(int argc, char **argv) ...@@ -61,6 +61,9 @@ int main(int argc, char **argv)
struct pp_link *lnk = &ppg->links[i]; struct pp_link *lnk = &ppg->links[i];
ppi = &ppg->pp_instances[i]; ppi = &ppg->pp_instances[i];
NP(ppi)->ch[PP_NP_EVT].fd = -1;
NP(ppi)->ch[PP_NP_GEN].fd = -1;
ppi->glbs = ppg; ppi->glbs = ppg;
ppi->iface_name = lnk->iface_name; ppi->iface_name = lnk->iface_name;
ppi->ethernet_mode = (lnk->proto == 0) ? 1 : 0; ppi->ethernet_mode = (lnk->proto == 0) ? 1 : 0;
......
...@@ -324,6 +324,7 @@ err_out: ...@@ -324,6 +324,7 @@ err_out:
pp_printf("%s: %s: %s\n", __func__, context, strerror(errno)); pp_printf("%s: %s: %s\n", __func__, context, strerror(errno));
if (sock >= 0) if (sock >= 0)
close(sock); close(sock);
NP(ppi)->ch[chtype].fd = -1;
return -1; return -1;
} }
...@@ -413,7 +414,7 @@ struct pp_network_operations unix_net_ops = { ...@@ -413,7 +414,7 @@ struct pp_network_operations unix_net_ops = {
int unix_net_check_pkt(struct pp_globals *ppg, int delay_ms) int unix_net_check_pkt(struct pp_globals *ppg, int delay_ms)
{ {
fd_set set; fd_set set;
int i, j; int i, j, k;
int ret = 0; int ret = 0;
int maxfd = -1; int maxfd = -1;
struct unix_arch_data *arch_data = POSIX_ARCH(ppg); struct unix_arch_data *arch_data = POSIX_ARCH(ppg);
...@@ -439,36 +440,17 @@ int unix_net_check_pkt(struct pp_globals *ppg, int delay_ms) ...@@ -439,36 +440,17 @@ int unix_net_check_pkt(struct pp_globals *ppg, int delay_ms)
struct pp_instance *ppi = &ppg->pp_instances[j]; struct pp_instance *ppi = &ppg->pp_instances[j];
int fd_to_set; int fd_to_set;
NP(ppi)->ch[PP_NP_GEN].pkt_present = 0; /* Use either fd that is valid, irrespective of ether/udp */
NP(ppi)->ch[PP_NP_EVT].pkt_present = 0; for (k = 0; k < 2; k++) {
NP(ppi)->ch[k].pkt_present = 0;
if (ppi->ethernet_mode) { fd_to_set = NP(ppi)->ch[k].fd;
fd_to_set = NP(ppi)->ch[PP_NP_GEN].fd;
if (fd_to_set < 0) if (fd_to_set < 0)
continue; continue;
FD_SET(fd_to_set, &set); FD_SET(fd_to_set, &set);
maxfd = fd_to_set > maxfd ? fd_to_set : maxfd; maxfd = fd_to_set > maxfd ? fd_to_set : maxfd;
continue;
} }
/* else: UDP */
fd_to_set = NP(ppi)->ch[PP_NP_GEN].fd;
FD_SET(fd_to_set, &set);
maxfd = fd_to_set > maxfd ? fd_to_set : maxfd;
fd_to_set = NP(ppi)->ch[PP_NP_EVT].fd;
FD_SET(fd_to_set, &set);
maxfd = fd_to_set > maxfd ? fd_to_set : maxfd;
} }
if (maxfd < 0) {
usleep(1000 * delay_ms);
return 0;
}
i = select(maxfd + 1, &set, NULL, NULL, &arch_data->tv); i = select(maxfd + 1, &set, NULL, NULL, &arch_data->tv);
if (i < 0 && errno != EINTR) if (i < 0 && errno != EINTR)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment