c - 如何处理 execvp 中的错误?

标签 c linux exec pipeline

我已经编写了一个小程序(使用来自 SO 的代码)来促进 printenv |排序 |少。现在我想实现错误处理,我从 execvp 开始。是否只是检查返回值等等? AFAIK 我只是检查此函数中的返回值是否为 0 return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);。对吗?

#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct command
{
    const char **argv;
};
/* Helper function that spawns processes */
int spawn_proc (int in, int out, struct command *cmd) {
    pid_t pid;
    if ((pid = fork ()) == 0) {
        if (in != 0) {
            dup2 (in, 0);
            close (in);
        }
        if (out != 1) {
            dup2 (out, 1);
            close (out);
        }
        return execvp (cmd->argv [0], (char * const *)cmd->argv);
    }
    return pid;
}
/* Helper function that forks pipes */
int fork_pipes (int n, struct command *cmd) {
    int i;
    int in, fd [2];
    for (i = 0; i < n - 1; ++i) {
        pipe (fd);
        spawn_proc (in, fd [1], cmd + i);
        close (fd [1]);
        in = fd [0];
    }
    dup2 (in, 0);
    return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}

int main (int argc, char ** argv) {
    int i;
    if (argc == 1) { /* There were no arguments */
        const char *printenv[] = { "printenv", 0};
        const char *sort[] = { "sort", 0 };
        const char *less[] = { "less", 0 };
        struct command cmd [] = { {printenv}, {sort}, {less} };
        return fork_pipes (3, cmd);
    }
    if (argc > 1) { /* I'd like an argument */

        if (strncmp(argv[1], "cd", 2) && strncmp(argv[1], "exit", 2)) {
            char *tmp;
            int len = 1;
            for( i=1; i<argc; i++)
            {
                len += strlen(argv[i]) + 2;
            }
            tmp = (char*) malloc(len);
            tmp[0] = '\0';
            int pos = 0;
            for( i=1; i<argc; i++)
            {
                pos += sprintf(tmp+pos, "%s%s", (i==1?"":"|"), argv[i]);
            }
            const char *printenv[] = { "printenv", 0};
            const char *grep[] = { "grep", "-E", tmp, NULL};
            const char *sort[] = { "sort", 0 };
            const char *less[] = { "less", 0 };
            struct command cmd [] = { {printenv}, {grep}, {sort}, {less} };
            return fork_pipes (4, cmd);
            free(tmp);
        } else if (! strncmp(argv[1], "cd", 2)) { /* change directory */
            printf("change directory to %s\n" , argv[2]);
            chdir(argv[2]);
        } else if (! strncmp(argv[1], "exit", 2)) { /* change directory */
            printf("exit\n");
            exit(0);
        }
    }
    exit(0);
}

最佳答案

查看manual : execvp 将您进程的程序镜像更改为另一个。所以如果有返回值,显然是 -1,因为它显然失败了。

至于任何系统调用或调用系统调用的函数,execvp 设置 errno 以获得适当的错误代码,您可以将其与 perror 一起使用知道为什么你的功能失败。 (顺便说一下,您应该对任何系统调用执行相同的操作:至少 fork、pipe 和 dup2。)

然后,如果通过“错误处理”,您的意思是处理您启动的程序的错误,您可以使用 wait or waitpid 来实现.它会让你知道程序是如何结束的,它返回值,如果它以信号结束,如果是,那么等等。

关于c - 如何处理 execvp 中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29535576/

相关文章:

c - 指针和整数之间的比较是怎样的

c - GCC 使用的函数 rem_pio2f() 是什么?

linux - 使用 Bash 启动和停止 openconnect

c - 在 linux 上找不到 pathfind 命令

c - GTK3 和多线程,替换已弃用的函数

mysql - mysql 5.6.14补丁如何更新到mysql 5.6.28以上?

python - tcpkill : write: Operation not permitted with subprocess. 弹出

python - exec 不从闭包中获取变量

php - 有没有什么方法可以将图像作为变量从 php 传递到 python 脚本

c - 如果不使用 sudo 运行,为什么这个带有 execl 的程序会中断?