Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request #1

Open
rhakb opened this issue May 6, 2023 · 3 comments
Open

feature request #1

rhakb opened this issue May 6, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@rhakb
Copy link

rhakb commented May 6, 2023

hi, bro
Can you obtain command output through pipelines? This can avoid blocking long running commands

@souzomain
Copy link
Owner

souzomain commented May 7, 2023

yeah, of course, but pipelines still going block, an alternative is creating threads for commands.

If you have something as an example, I'll reproduce.

Or did I get it wrong? show me your idea bro

thanks for contributing!

@souzomain
Copy link
Owner

Let's implement this.

but for extented commands, demon of havoc uses job command, I can create this.

do you think best implement this for all commands executed or create "job" command to this?

@rhakb
Copy link
Author

rhakb commented May 8, 2023

Sorry, I have removed the fork function and improved the program. Here is the code

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define BUFFER_SIZE 1024

int main(int argc, char *argv[]) {
    char buffer[BUFFER_SIZE];
    FILE *nmap_pipe;
    int fd;
    fd_set set;
    struct timeval timeout;

    // Execute the nmap command and open the pipeline
    nmap_pipe = popen("ping 192.168.0.1", "r");
    if (!nmap_pipe) {
        fprintf(stderr, "Failed to open pipe: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }

    // Set the pipeline to non blocking mode
    fd = fileno(nmap_pipe);
    fcntl(fd, F_SETFL, O_NONBLOCK);

    //Background operation loop
    while (1) {
        //Use the select function to read the pipeline without blocking
        FD_ZERO(&set);
        FD_SET(fd, &set);
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;
        int result = select(fd + 1, &set, NULL, NULL, &timeout);
        if (result == -1) {
            fprintf(stderr, "Failed to select pipe: %s\n", strerror(errno));
            exit(EXIT_FAILURE);
        } else if (result == 0) {
            // Pipeline has no data, continue with background operation
            printf("No data in pipe, continue.\n");
            sleep(1);
            printf("Continue other work...\n");
        } else {
            // There is data in the pipeline, output and continue with background operations
            ssize_t len = fread(buffer, 1, BUFFER_SIZE - 1, nmap_pipe);
            if (len == -1) {
                fprintf(stderr, "Failed to read pipe: %s\n", strerror(errno));
                exit(EXIT_FAILURE);
            } else if (len == 0) {
                // The pipeline has been closed
                printf("Pipe closed.\n");
                break;
            } else {
                buffer[len] = '\0';
                printf("Data in pipe: %s\n", buffer);
                sleep(1);
                printf("Continue other work...\n");
            }
        }
    }

    // Close pipeline
    pclose(nmap_pipe);

    return 0;
}

@souzomain souzomain changed the title function feature request Jun 6, 2023
@souzomain souzomain added the enhancement New feature or request label Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants