prevent zombie processes on cmd_extscript
[NeonServV5.git] / src / cmd_neonserv_extscript.c
index fc9dc5ab5efb7bd47dacf8bab5f325eee3c7aaab..37d6f9109805a5b0e95b0009440864d061b142c6 100644 (file)
@@ -86,7 +86,7 @@ CMD_BIND(neonserv_cmd_extscript) {
                 partpos = sprintf(part, "%s", ((user->flags & USERFLAG_ISAUTHED) ? user->auth : ""));
             } else if(!strcmp(argv[i], "access")) {
                 if(chan)
-                    partpos = sprintf(part, "%d", getChannelAccess(user, chan, 0));
+                    partpos = sprintf(part, "%d", getChannelAccess(user, chan));
             }
         } else {
             partpos = sprintf(part, "%s", argv[i]);
@@ -114,16 +114,30 @@ CMD_BIND(neonserv_cmd_extscript) {
     FILE *fp;
     fp = popen(command, "r");
     if (fp) {
-        char *a;
-        while (fgets(command, 1024, fp) != NULL) {
-            if((a = strchr(command, '\n'))) 
-                *a = '\0';
-            if(answere_channel) {
-                putsock(client, "PRIVMSG %s :%s", chan->name, command);
-            } else
-                reply(getTextBot(), user, "%s", command);
+        struct ClientSocket *textbot = getTextBot();
+        pid_t pID = fork();
+        if (pID == 0) { //We're the child process :D
+            pID = fork();
+            if(pID < 0) exit(EXIT_FAILURE);
+            if(pID != 0) exit(EXIT_SUCCESS);
+            char *a;
+            while (fgets(command, 1024, fp) != NULL) {
+                if((a = strchr(command, '\n'))) 
+                    *a = '\0';
+                if(answere_channel)
+                    putsock(client, "PRIVMSG %s :%s", chan->name, command);
+                else
+                    reply(textbot, user, "%s", command);
+            }
+            pclose(fp);
+            exit(EXIT_FAILURE);
+        } else if (pID < 0) {
+            reply(getTextBot(), user, "internal bot error - please contact an administrator!");
+            pclose(fp);
+        } else {
+            //parent bot - continue program
+            wait(NULL);
         }
-        pclose(fp);
     } else {
         //error
         reply(getTextBot(), user, "internal bot error - please contact an administrator!");