在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进行交互。

相关推荐
XM-54582 分钟前
2025微信小程序wxapkg解包全攻略
linux·运维·小程序
朗晴35 分钟前
文本编辑器VIM的使用方法!
linux·运维·服务器
奇文怪式39 分钟前
VSCode+arm-none-eabi-gcc交叉编译+CMake构建+OpenOCD(基于Raspberry Pico RP2040)
arm开发·ide·vscode·rp2040
Imagine Miracle41 分钟前
Ubuntu for ARM 更换为阿里云镜像源
arm开发·ubuntu·阿里云
wwwlyj12332143 分钟前
arm 精准总线错误与非精准总线错误
arm开发
星辰pid2 小时前
STM32实现四自由度机械臂(SG90舵机)多功能控制(软件篇freertos)
stm32·单片机·嵌入式硬件·机械臂
森焱森7 小时前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
2401_826097628 小时前
JavaEE-Linux环境部署
java·linux·java-ee
(:满天星:)10 小时前
第31篇:块设备与字符设备管理深度解析(基于OpenEuler 24.03)
linux·运维·服务器·网络·centos
爱莉希雅&&&10 小时前
shell编程之awk命令详解
linux·服务器·git