C - exec()位于另一个目录中的文件

时间:2013-04-15 23:03:10

标签: c linux unix exec

如何使用exec()函数之一执行位于另一个目录中的二进制文件(从c源编译)? 我正在使用inotify API,我想执行位于另一个目录中的文件。 这是家庭作业:每当创建文件时通知;如果此文件是可执行文件,请执行它。

以下是代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/inotify.h>
#include <sys/wait.h>

#define EVENT_SIZE      (sizeof(struct inotify_event))
#define EVENT_BUF_LEN   (1024 * (EVENT_SIZE + 16))

int main(int argc, char *argv[]) {

int fd, wd, length = 0;
char buffer[EVENT_BUF_LEN];
struct stat sb;

if(argc != 2) {
    printf("Usage: ./spy dirpath\n");
    exit(EXIT_FAILURE);
}

if( (fd = inotify_init()) == -1 )
    perror("inotify_init()");
if( (wd = inotify_add_watch(fd, argv[1], IN_CREATE)) == -1 )
    perror("inotify_add_watch");
while(1) {  
    if( (length = read(fd, buffer, EVENT_BUF_LEN)) < 0 )
        perror("read()");
    struct inotify_event *event = (struct inotify_event *)&buffer;
    if(event->len) {
        if(event->mask & IN_CREATE) {
            if(event->mask & IN_ISDIR)
                continue;
            else {
                if(access(event->name, X_OK)) {
                    printf("New executable file created\n");                
                    pid_t child;
                        int cstatus;
                        child = fork();
                        if(child > 0) { /* father */
                wait(&cstatus);
                }
    else { /* child */
        chdir(argv[1]);
        /* This time, I try to tell it directly the filename*/
        char *args[2] = { "./helloworld" ,NULL };
        execvp(args[0], args);
        printf("execvp failed\n");
                    exit(EXIT_FAILURE);
    }

            }
        }
    }
}

inotify_rm_watch(fd, wd);
close(fd);

return(0);
}

1 个答案:

答案 0 :(得分:1)

你的一个问题是:

    char *args[0]; args[1] = NULL;

你正在践踏阵列的界限。实际上,在标准C中,您根本不能拥有维度为0的数组。对(不存在的)args[1]的赋值有可能损坏或覆盖指针event(尽管您可能期望核心转储而不仅仅是'execvp()失败'消息)。你需要:

    char *args[2] = { event->name, NULL };

exit()失败后不要忘记execvp(),否则你最终会有两个进程读取数据,这会让人非常困惑。你还应该报告'stderr'的错误;它是报告错误的标准流。