Private GIT

Skip to content
Snippets Groups Projects
Unverified Commit 5852574f authored by Gary Allan's avatar Gary Allan
Browse files

- Use stream_socket_pair() to send fping results from child to parent process

Avoids infinite loop if child process fails to create the pipe file
Avoids race condition resulting in fping not discovering IPs

```php
while (!empty($threads)) {
    foreach ($threads as $index => $thread) {
        $child_pipe = "/tmp/pipe_" . $thread->getPid();

        if (file_exists($child_pipe)) {    // child thread failed to create this file
            $file_descriptor = fopen($child_pipe, "r");
            $child_response = "";
            while (!feof($file_descriptor)) {
                $child_response .= fread($file_descriptor, 8192);
            }
            $child_response = unserialize($child_response);
            $scan_subnets[$index]->discovered = $child_response;
            unlink($child_pipe);
            unset($threads[$index]);    // This never runs = $threads[] can never be empty = inf loop
        }
    }
}
```

```php
while (!empty($threads)) {
    foreach ($threads as $index => $thread) {
        $child_pipe = "/tmp/pipe_" . $thread->getPid();

        if (file_exists($child_pipe)) {         // Child thread has created this file.
            $file_descriptor = fopen($child_pipe, "r");
            $child_response = "";
            while (!feof($file_descriptor)) {   // Child thread has not yet called fwrite() to send us the data.
                $child_response .= fread($file_descriptor, 8192);
            }
            $child_response = unserialize($child_response);  // The response is blank ""
            $scan_subnets[$index]->discovered = $child_response;
            unlink($child_pipe);
            unset($threads[$index]);
        }
    }
}
```

- Use join() to wait for child processes to exit instead of polling isAlive() in busy loop.

- Run $thread->start() in try-catch blocks to avoid terminating scripts on errors.

- Provide user feedback if we can't spawn a scanning thread.

- Misc code cleanups
parent 2c1ff67b
No related branches found
No related tags found
No related merge requests found
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment