Fixed forking, to prevent LCR to run multiple times
authorAndreas Eversberg <jolly@eversberg.eu>
Wed, 10 Aug 2011 19:50:45 +0000 (21:50 +0200)
committerAndreas Eversberg <jolly@eversberg.eu>
Wed, 10 Aug 2011 19:50:45 +0000 (21:50 +0200)
Executing a script caused very weird behaviour, due to forking of LCR.

action.cpp
route.c

index 352efb4..478406d 100644 (file)
@@ -2003,10 +2003,8 @@ void EndpointAppPBX::action_execute(void)
                        if ((pid2 = fork()) == 0) {
                                execve(command, argv, environ);
                        }
-                       else {
-                               /* Exit immediately and release the waiting parent. The subprocess falls to init because the parent died */
-                               exit(0);
-                       }
+                       /* Exit immediately and release the waiting parent. The subprocess falls to init because the parent died */
+                       exit(0);
                        break;
                default:
                        trace_header("ACTION execute", DIRECTION_NONE);
diff --git a/route.c b/route.c
index 8217ff1..c1bf079 100644 (file)
--- a/route.c
+++ b/route.c
@@ -1902,7 +1902,7 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
        struct admin_list       *admin;
        time_t                  now;
        struct tm               *now_tm;
-       int                     pid2;
+       int                     pid, status;
 
        /* reset timeout action */
        e_match_to_action = NULL;
@@ -2086,10 +2086,17 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
                                argv[j++] = isdn_port;
                                argv[j++] = e_callerinfo.imsi;
                                argv[j++] = NULL; /* check also number of args above */
-                               if (fork() == 0) {
-                                       if ((pid2 = fork()) == 0) {
-                                               execve(cond->string_value, argv, environ);
-                                       }
+                               switch ((pid = fork())) {
+                               case 0:
+                                       execve(cond->string_value, argv, environ);
+                                       perror("execve");
+                                       exit(1);
+                               case -1:
+                                       break;
+                               default:
+                                       waitpid(pid, &status, 0);
+                                       if (0 == WEXITSTATUS(status))
+                                               istrue = 1;
                                }
                                break;