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