]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/apmd/apmd-3.2.2/select-instead-of-sleep.patch
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / apmd / apmd-3.2.2 / select-instead-of-sleep.patch
1 --- apmd/apmd.c~        2006-04-10 20:10:42.787178752 +0100
2 +++ apmd/apmd.c 2006-04-17 20:16:25.984067744 +0100
3 @@ -345,6 +345,7 @@
4                 int status, retval;
5                 ssize_t len;
6                 time_t countdown;
7 +               struct timeval timeout;
8  
9                 if (pid < 0) {
10                         /* Couldn't fork */
11 @@ -357,56 +358,50 @@
12                 /* Capture the child's output, if any, but only until it terminates */
13                 close(fds[1]);
14                 fcntl(fds[0], F_SETFL, O_RDONLY|O_NONBLOCK);
15 -               countdown = proxy_timeout;
16 -               do {
17 -                       countdown -= 1;
18 -                       while ((len = read(fds[0], line, sizeof(line)-1)) > 0) {
19 -                               line[len] = 0;
20 -                               APMD_SYSLOG(LOG_INFO, "+ %s", line);
21 -                       }
22 -                               
23 -                       retval = waitpid(pid, &status, WNOHANG);
24 -                       if (retval == pid)
25 -                               goto proxy_done;
26 -                       if (retval < 0) {
27 -                               APMD_TRACE1(LOG_ERR, "waitpid() failed: %s", strerror(errno));
28 -                               status = __W_EXITCODE(0, SIGTERM) | __WCOREFLAG;
29 -                               goto proxy_done;
30 -                       }
31 -                               
32 -                       while (sleep(1) > 0) ;
33 -               } while (
34 -                       (countdown >= 0)
35 -                       || (proxy_timeout < 0)
36 -               );
37 -
38 -               APMD_SYSLOG(LOG_NOTICE, "Proxy has been running more than %d seconds; killing it", proxy_timeout);
39 -
40 -               kill(pid, SIGTERM);
41 -               countdown = 5;
42 -               do {
43 -                       retval = waitpid(pid, &status, WNOHANG);
44 -                       if (retval == pid)
45 -                               goto proxy_done;
46 -                       if (retval < 0) {
47 -                               APMD_TRACE1(LOG_ERR, "waitpid() failed: %s", strerror(errno));
48 -                               status = __W_EXITCODE(0, SIGTERM) | __WCOREFLAG;
49 -                               goto proxy_done;
50 +               timeout.tv_sec = proxy_timeout;
51 +               timeout.tv_usec = 0;
52 +               for (;;) {
53 +                       int r;
54 +                       fd_set rfds;
55 +                       FD_ZERO (&rfds);
56 +                       FD_SET (fds[0], &rfds);
57 +                       r = select (fds[0] + 1, &rfds, NULL, NULL, &timeout);
58 +                       if (r == 0) {
59 +                               APMD_SYSLOG(LOG_NOTICE, "Proxy has been running more than %d seconds; killing it", proxy_timeout);
60 +
61 +                               kill(pid, SIGTERM);
62 +                               countdown = 5;
63 +                               do {
64 +                                       retval = waitpid(pid, &status, WNOHANG);
65 +                                       if (retval == pid)
66 +                                               goto proxy_done;
67 +                                       if (retval < 0) {
68 +                                               APMD_TRACE1(LOG_ERR, "waitpid() failed: %s", strerror(errno));
69 +                                               status = __W_EXITCODE(0, SIGTERM) | __WCOREFLAG;
70 +                                               goto proxy_done;
71 +                                       }
72 +                                       while (sleep(1) > 0) ;
73 +                               } while (countdown-- >= 0);
74 +
75 +                               kill(pid, SIGKILL);
76 +
77 +                               break;
78 +                       } else if (r < 0) {
79 +                               perror ("select");
80 +                               break;
81 +                       } else {
82 +                               len = read(fds[0], line, sizeof(line)-1);
83 +                               if (len > 0) {
84 +                                       line[len] = 0;
85 +                                       APMD_SYSLOG(LOG_INFO, "+ %s", line);
86 +                               } else
87 +                                       break;
88                         }
89 +               }
90  
91 -                       while (sleep(1) > 0) ;
92 -
93 -               } while (countdown >= 0);
94 -
95 -               kill(pid, SIGKILL);
96 -               status = __W_EXITCODE(0, SIGKILL);
97 +               retval = waitpid(pid, &status, 0);
98  
99  proxy_done:
100 -               /* Flush any remaining data */
101 -               while ((len = read(fds[0], line, sizeof(line)-1)) > 0) {
102 -                       line[len] = 0;
103 -                       APMD_SYSLOG(LOG_INFO, "+ %s", line);
104 -               }
105                 close(fds[0]);
106                                 
107                 /* Collect the exit code */