为什么我不能在busybox中用'\ t'模拟自动完成

时间:2021-05-14 09:06:05

I want to use "yp\t\n\0" to run “ypdomainname” command by exploiting auto-completion in busybox, but it failed. my code and result are below:

我想使用“yp \ t \ n \ 0”通过在busybox中利用自动完成来运行“ypdomainname”命令,但它失败了。我的代码和结果如下:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>

#define DEFAULT_BUSYBOX_PATH "/bin/busybox"
#define MAX_BUF 1000

int main()
{
    int fd[2];
    pid_t pid;
    FILE *file;
    int status;

    if(pipe(fd) < 0){
        fprintf(stderr, "pipe error!\n");
        return -1;
    }

    if((pid = fork()) < 0){
        fprintf(stderr, "pipe error!\n");
    }else if(pid == 0){     //child
        close(fd[1]);

        int fd_output;
        fd_output = open("result", O_CREAT | O_RDWR, 777);

        if(fd_output != STDOUT_FILENO){
            if(dup2(fd_output, STDOUT_FILENO) != STDOUT_FILENO)
                fprintf(stderr, "dup2 error to stdout\n");
        }

        if(fd[0] != STDIN_FILENO){
            if(dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
                fprintf(stderr, "dup2 error to stdin\n");
        }
        execl(DEFAULT_BUSYBOX_PATH, DEFAULT_BUSYBOX_PATH, "ash", NULL);
        close(fd[0]);
        close(fd_output);
        return 0;
    }else{                  //parent
        close(fd[0]);

        char buf[MAX_BUF] = "yp";
        buf[2] = '\t';
        buf[3] = '\n';
        buf[4] = '\0';

        write(fd[1], buf, strlen(buf));

        close(fd[1]);
        return 0;
    }

}

result of my code

我的代码的结果

What makes me confused is that the character is not changed in function lineedit_read_key() in file lineedit.c and it will run the function input_tab() when the character is '\t'. input_tab will be executed when character is '\t'

令我困惑的是,文件lineedit.c中的lineedit_read_key()函数中的字符没有更改,当字符为'\ t'时,它将运行函数input_tab()。当字符为'\ t'时,将执行input_tab

1 个解决方案

#1


0  

Your code fails because a pipe is not a terminal. Many programs will use isatty(3) and alike to detect if the standard input is connected to a terminal and adjust their behaviour depending on the result.

您的代码失败,因为管道不是终端。许多程序将使用isatty(3)等来检测标准输入是否连接到终端并根据结果调整其行为。

What you can do is to open a pty pair using openpty(3) and run the command with the slave duplicated to its standard input, output and error descriptors, and using the master to communicate with it. Unfortunately I have no time right now writing a full solution as it is rather intricate; I've done it ever in Python and it was tricky even there.

你可以做的是使用openpty(3)打开一个pty对,然后运行命令,将slave复制到其标准输入,输出和错误描述符,并使用master与之通信。不幸的是,我现在没有时间写一个完整的解决方案,因为它相当错综复杂;我用Python做过它,即使在那里它也很棘手。

#1


0  

Your code fails because a pipe is not a terminal. Many programs will use isatty(3) and alike to detect if the standard input is connected to a terminal and adjust their behaviour depending on the result.

您的代码失败,因为管道不是终端。许多程序将使用isatty(3)等来检测标准输入是否连接到终端并根据结果调整其行为。

What you can do is to open a pty pair using openpty(3) and run the command with the slave duplicated to its standard input, output and error descriptors, and using the master to communicate with it. Unfortunately I have no time right now writing a full solution as it is rather intricate; I've done it ever in Python and it was tricky even there.

你可以做的是使用openpty(3)打开一个pty对,然后运行命令,将slave复制到其标准输入,输出和错误描述符,并使用master与之通信。不幸的是,我现在没有时间写一个完整的解决方案,因为它相当错综复杂;我用Python做过它,即使在那里它也很棘手。