libreoffice: update to 24.8.4.2
[oi-userland.git] / components / shell / shmux / patches / 02-loop.c.patch
blob9910fb3f29de80e018cf87413609e54df7b1a519
1 --- src/loop.c.orig 2019-01-02 14:28:47.000000000 +0000
2 +++ src/loop.c 2020-11-07 20:49:06.982768448 +0000
3 @@ -16,6 +16,9 @@
4 #include <sys/resource.h>
5 #include <poll.h>
6 #include <time.h>
7 +#include <sys/types.h>
8 +#include <sys/socket.h>
9 +#include <netdb.h>
11 #if !defined(WCOREDUMP)
12 # define WCOREDUMP(x) 0
13 @@ -795,7 +798,9 @@
14 u_int ctimeout, utest;
16 struct child *children;
17 - struct pollfd *pfd;
18 + struct pollfd *pfd, *sfd;
19 + struct sockaddr_in *addr;
20 + int ntarget = target_getmax();
21 struct sigaction sa, saved_sa;
22 int idx;
23 char *cargv[10];
24 @@ -844,6 +849,30 @@
26 memset((void *) children, 0, (max+1)*sizeof(struct child));
28 + sfd = (struct pollfd *) malloc(ntarget * sizeof(struct pollfd));
29 + if (sfd == NULL)
30 + {
31 + perror("malloc failed");
32 + free(pfd);
33 + free(children);
34 + return RC_ERROR;
35 + }
36 + memset((void *) sfd, 0, ntarget * sizeof(struct pollfd));
37 + idx = 0;
38 + while (idx < ntarget)
39 + sfd[idx++].fd = -1;
41 + addr = (struct sockaddr_in *)malloc(ntarget * sizeof(struct sockaddr_in));
42 + if (addr == NULL)
43 + {
44 + perror("malloc failed");
45 + free(pfd);
46 + free(children);
47 + free(sfd);
48 + return RC_ERROR;
49 + }
50 + memset((void *) addr, 0, ntarget * sizeof(struct sockaddr_in));
52 /* Setup SIGINT handler */
53 sigemptyset(&sa.sa_mask);
54 sa.sa_flags = 0;
55 @@ -857,42 +886,97 @@
56 /* Run fping if requested */
57 if (ping != NULL)
59 - u_int count = 0;
61 - pfd[2].fd = -1;
62 - cargv[0] = "fping"; cargv[1] = "-t"; cargv[2] = ping; cargv[3] = NULL;
63 - children[0].pid = exec(&(pfd[0].fd), &(pfd[1].fd), &(pfd[2].fd),
64 - NULL, cargv, 0);
65 - if (children[0].pid == -1)
66 - /* Error message was given by exec() */
67 - spawn_mode = SPAWN_FATAL;
68 - else
69 - {
70 - init_child(&(children[0]));
71 + int ret, flags, setfl, ndone = 0;
72 + struct hostent *hp;
73 + struct in_addr *haddr;
74 + idx = 0;
75 + while (target_next(1) == 0) {
76 + target_start();
77 + if ((sfd[idx].fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
78 + eprint("Ping(create socket) failed for %s: %s", target_getname(), strerror(errno));
79 + idx++;
80 + continue;
81 + }
82 + sfd[idx].events = POLLIN;
84 - pfd[1].events = POLLIN;
85 - pfd[2].events = POLLIN;
86 + if ((flags = fcntl(sfd[idx].fd, F_GETFL, 0)) == -1) {
87 + eprint("Ping(get status flag) failed for %s: %s", target_getname(), strerror(errno));
88 + close(sfd[idx].fd);
89 + sfd[idx].fd = -1;
90 + idx++;
91 + continue;
92 + }
93 + if ((setfl = fcntl(sfd[idx].fd, F_SETFL, flags | O_NONBLOCK)) == -1) {
94 + eprint("Ping(set status flag) failed for %s: %s", target_getname(), strerror(errno));
95 + close(sfd[idx].fd);
96 + sfd[idx].fd = -1;
97 + idx++;
98 + continue;
99 + }
101 - while (target_next(1) == 0)
103 - char *tname;
104 + if ((hp = gethostbyname(target_getname())) == NULL) {
105 + eprint("Ping(get host entry) failed for %s: %s", target_getname(), strerror(errno));
106 + close(sfd[idx].fd);
107 + sfd[idx].fd = -1;
108 + idx++;
109 + continue;
111 + haddr = (struct in_addr *)*hp->h_addr_list;
112 + addr[idx].sin_family = AF_INET;
113 + addr[idx].sin_addr = *haddr;
114 + addr[idx].sin_port = htons((short)80);
115 + bzero(&(addr[idx].sin_zero), 8);
117 - target_start();
118 - count += 1;
119 - tname = strchr(target_getname(), '@');
120 - if (tname == NULL)
121 - tname = target_getname();
122 - else
123 - tname += 1;
124 - write(pfd[0].fd, tname, strlen(tname));
125 - write(pfd[0].fd, "\n", 1);
127 - close(pfd[0].fd); pfd[0].fd = -1;
128 - iprint("Pinging %u targets...", count);
129 - dprint("fping pid = %d (idx=0) %d/%d/%d",
130 - children[0].pid, pfd[0].fd, pfd[1].fd, pfd[2].fd);
131 - ping = NULL;
133 + ret = connect(sfd[idx].fd, (struct sockaddr *)&addr[idx], sizeof(struct sockaddr));
134 + if (ret == 0) { //this case may happen on localhost
135 + target_result(1);
136 + close(sfd[idx].fd);
137 + sfd[idx].fd = -1;
138 + ndone++;
140 + idx++;
141 + } // end of while
143 + time_t t1 = time(NULL);
144 + while (time(NULL) - t1 < atoi(ping) && ndone != ntarget) {
145 + ret = poll(sfd, ntarget, 1000);
146 + if (ret == -1 && errno != EINTR) {
147 + eprint("poll failed: %s", strerror(errno));
148 + break;
149 + } else if (ret > 0) {
150 + for (idx = 0; idx < ntarget; idx++) {
151 + if (sfd[idx].revents > 0) {
152 + target_setbynum(idx);
153 + target_result(1);
154 + close(sfd[idx].fd);
155 + sfd[idx].fd = -1;
156 + ndone++;
158 + if (ndone == ntarget) {
159 + break;
161 + } // end of for
163 + } // end of while
165 + if (ndone < ntarget) {
166 + dprint("ping is done or timed out");
167 + while (target_pong(NULL) == 0) {
168 + idx = target_getnum();
169 + if (sfd[idx].fd > 0) {
170 + eprint("Ping timed out for %s", target_getname());
171 + target_result(-1);
172 + close(sfd[idx].fd);
173 + sfd[idx].fd = -1;
174 + } else {
175 + eprint("%s assumed to be alive (missing from ping results)", target_getname());
176 + target_result(1);
178 + } // end of while
180 + free(addr);
181 + free(sfd);
182 + ping = NULL;
184 else
185 /* No fping, let's move on to the next phase then */