Merge pull request #4954 from kissg1988/django
[feed/packages.git] / net / xl2tpd / patches / 200-xl2tpd-control-fix-xl2tpd-hanged-up-in-fopen.patch
1 From 97adf987cf230e47a4800c2f0a0940a1d0d98109 Mon Sep 17 00:00:00 2001
2 From: wendy2001011 <wendy2001011@163.com>
3 Date: Thu, 9 Feb 2017 17:32:14 +0800
4 Subject: [PATCH] xl2tpd-control: fix xl2tpd hanged up in "fopen"
5
6 This is a fix for xl2tpd hanged up in "fopen" result fifo while working
7 on xl2tpd with OpenWrt.
8
9 Root cause is as followings,
10 1. xl2tpd-control open result fifo ##fifo readers=1
11 2. xl2tpd-control read result fifo
12 3. xl2tpd-control close result fifo ##fifo readers=0
13 4. xl2tpd fopen result fifo ##xl2tpd is hanged up here to wait readers
14 5. xl2tpd-control unlink result fifo
15
16 The fix replaces the order of "unlink" and "close" when cleaning up to
17 avoid hang up issue in fopen, and add the retry waiting when reading
18 result fifo.
19
20 [Yousong Zhou: 2s as the timeout and 10ms as the check interval]
21 ---
22 xl2tpd-control.c | 23 +++++++++++++++++++++--
23 1 file changed, 21 insertions(+), 2 deletions(-)
24
25 diff --git a/xl2tpd-control.c b/xl2tpd-control.c
26 index 9fcab76..b8bf822 100644
27 --- a/xl2tpd-control.c
28 +++ b/xl2tpd-control.c
29 @@ -35,6 +35,7 @@
30
31 #define TUNNEL_REQUIRED 1
32 #define TUNNEL_NOT_REQUIRED 0
33 +#define TIMEOUT 2000000 //timeout is 2s
34
35 char result_filename[128];
36 int result_fd = -1;
37 @@ -149,9 +150,9 @@ void help()
38 void cleanup(void)
39 {
40 /* cleaning up */
41 - if (result_fd >= 0)
42 - close (result_fd);
43 unlink (result_filename);
44 + if (result_fd >= 0)
45 + close (result_fd);
46 }
47
48 int main (int argc, char *argv[])
49 @@ -340,6 +341,7 @@ void print_error (int level, const char *fmt, ...)
50 va_end (args);
51 }
52
53 +
54 int read_result(int result_fd, char* buf, ssize_t size)
55 {
56 /* read result from result_fd */
57 @@ -348,6 +350,11 @@ int read_result(int result_fd, char* buf, ssize_t size)
58 */
59 ssize_t readed = 0;
60 ssize_t len;
61 + int write_pipe = 0;
62 + struct timeval tvs;
63 + struct timeval tve;
64 + unsigned long diff;
65 + gettimeofday(&tvs, NULL);
66
67 do
68 {
69 @@ -360,8 +367,20 @@ int read_result(int result_fd, char* buf, ssize_t size)
70 "error: can't read command result: %s\n", strerror (errno));
71 break;
72 } else if (len == 0) {
73 + if(!write_pipe) {
74 + gettimeofday(&tve, NULL);
75 + diff = (tve.tv_sec - tvs.tv_sec) * 1000000 + (tve.tv_usec - tvs.tv_usec);
76 + if (diff >= TIMEOUT) {
77 + print_error (DEBUG_LEVEL, "error: read timout\n");
78 + break;
79 + } else {
80 + usleep(10000);
81 + continue;
82 + }
83 + }
84 break;
85 } else {
86 + write_pipe = 1;
87 readed += len;
88 if ((size - readed) <= 0)
89 break;
90 --
91 2.6.4
92