You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.7 KiB

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