在C语言中使用伪终端与bash交互

  1. 了解伪终端概念
    • 伪终端(PTY)由一对设备组成:主设备(master)和从设备(slave)。数据写入主设备会出现在从设备,反之亦然。这允许一个进程通过主设备与另一个进程(如bash)通过从设备进行通信。
  2. 相关函数
    • openpty :用于创建伪终端对。其原型为int openpty(int *amaster, int *aslave, char *name, const struct termios *termp, const struct winsize *winp)amasteraslave是指向文件描述符的指针,分别用于主设备和从设备;name可用于获取从设备的路径名;termp可用于设置终端属性;winp可用于设置窗口大小。
    • fork :创建一个新进程,子进程通常用于执行bash,父进程用于与伪终端主设备交互。
    • dup2:在子进程中用于将标准输入、输出和错误重定向到伪终端从设备。
    • writeread :用于在父进程中向伪终端主设备写入命令,并读取bash的输出。
  3. 示例代码
c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>
#include <string.h>

#define BUFFER_SIZE 1024

// 设置文件描述符为非阻塞模式
void set_nonblocking(int fd) {
    int flags = fcntl(fd, F_GETFL, 0);
    fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}

int main() {
    int master, slave;
    pid_t pid;
    char buffer[BUFFER_SIZE];
    struct termios oldtty, newtty;

    // 创建伪终端对
    if (openpty(&master, &slave, NULL, NULL, NULL) == -1) {
        perror("openpty");
        return 1;
    }

    // 保存当前终端设置
    tcgetattr(STDIN_FILENO, &oldtty);
    newtty = oldtty;
    // 设置终端为非规范模式
    newtty.c_lflag &= ~(ICANON | ECHO);
    // 设置终端属性
    tcsetattr(STDIN_FILENO, TCSANOW, &newtty);

    // 设置伪终端主设备为非阻塞模式
    set_nonblocking(master);

    // 创建子进程
    pid = fork();
    if (pid == -1) {
        perror("fork");
        close(master);
        close(slave);
        return 1;
    } else if (pid == 0) {
        // 子进程
        close(master);
        // 将标准输入、输出和错误重定向到伪终端从设备
        if (dup2(slave, STDIN_FILENO) == -1) {
            perror("dup2 stdin");
            return 1;
        }
        if (dup2(slave, STDOUT_FILENO) == -1) {
            perror("dup2 stdout");
            return 1;
        }
        if (dup2(slave, STDERR_FILENO) == -1) {
            perror("dup2 stderr");
            return 1;
        }
        close(slave);

        // 启动bash
        execl("/bin/bash", "bash", (char *)NULL);
        perror("execl");
        return 1;
    } else {
        // 父进程
        close(slave);

        // 向bash发送命令
        const char *command = "ls\n";
        if (write(master, command, strlen(command))!= strlen(command)) {
            perror("write");
        }

        // 读取bash的输出
        ssize_t bytes_read;
        while ((bytes_read = read(master, buffer, sizeof(buffer) - 1)) > 0) {
            buffer[bytes_read] = '\0';
            printf("%s", buffer);
        }

        // 等待子进程结束
        waitpid(pid, NULL, 0);

        // 恢复终端设置
        tcsetattr(STDIN_FILENO, TCSANOW, &oldtty);

        close(master);
    }

    return 0;
}
  1. 代码解释
    • 创建伪终端对 :使用openpty创建伪终端主设备master和从设备slave
    • 终端设置 :保存当前终端设置oldtty,并修改为非规范模式newtty,这样可以实时读取输入而无需等待换行符。
    • 设置非阻塞模式:将伪终端主设备设置为非阻塞模式,以便在读取输出时不会阻塞。
    • 进程创建 :通过fork创建子进程,子进程执行bash,父进程与伪终端主设备交互。
    • 子进程操作 :关闭master,将标准输入、输出和错误重定向到slave,然后执行bash
    • 父进程操作 :关闭slave,向master写入命令(如ls\n),循环读取并打印bash的输出,等待子进程结束,最后恢复终端设置。

这样,通过上述代码,你可以在C语言程序中使用伪终端与bash进行交互。

相关推荐
.Ashy.6 小时前
2026.4.11 蓝桥杯软件类C/C++ G组山东省赛 小记
c语言·c++·蓝桥杯
菜菜艾6 小时前
基于llama.cpp部署私有大模型
linux·运维·服务器·人工智能·ai·云计算·ai编程
重生的黑客6 小时前
Linux开发工具:条件编译、动静态库与 make/makefile 入门
linux·运维·服务器
2401_892070986 小时前
链栈(链式栈) 超详细实现(C 语言 + 逐行精讲)
c语言·数据结构·链栈
minji...7 小时前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
w6100104667 小时前
cka-2026-ConfigMap
java·linux·cka·configmap
cc_yy_zh7 小时前
Win10 家庭版找不到Device Guard; 无法处理 VMware Workstation与Device Guard不兼容问题
linux·vmware
嵌入式吴彦祖7 小时前
Luckfox Pico Ultra W WIFI
linux·嵌入式硬件
SPC的存折7 小时前
MySQL 8组复制完全指南
linux·运维·服务器·数据库·mysql
Linux技术芯9 小时前
Refault Distance算法详解
linux