-1

对于以下 C 程序,execl 返回 -1,表示 Permission Denied 错误。我检查了读,写。我的可执行文件的执行权限,它们都是 rwx。有人可以让我知道为什么 execl 返回 Permission Denied 错误吗?非常感谢

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <errno.h>
#include <semaphore.h>

#define MAXLINE 256
#define MAXARGS 128
#define SHELL   "/h/fchang03" // "/bin/sh"

key_t shmkey;
int shmid;
sem_t *sem;
int* p;

struct statStuff {
    int pid;                    // %d
    char comm[256];             // %s
    char state;                 // %c
    int ppid;                   // %d
    int pgrp;                   // %d
    int session;                // %d
    int tty_nr;                 // %d
    int tpgid;                  // %d
    unsigned long flags;        // %lu
    unsigned long minflt;       // %lu
    unsigned long cminflt;      // %lu
    unsigned long majflt;       // %lu
    unsigned long cmajflt;      // %lu
    unsigned long utime;        // %lu
    unsigned long stime;        // %lu
    long cutime;                // %ld
    long cstime;                // %ld
    long priority;              // %ld
    long nice;                  // %ld
    long num_threads;           // %ld
    long itrealvalue;           // %ld
    unsigned long starttime;    // %lu
    unsigned long vsize;        // %lu
    long rss;                   // %ld
    unsigned long rlim;         // %lu
    unsigned long startcode;    // %lu
    unsigned long endcode;      // %lu
    unsigned long startstack;   // %lu
    unsigned long kstkesp;      // %lu
    unsigned long kstkeip;      // %lu
    unsigned long signal;       // %lu
    unsigned long blocked;      // %lu
    unsigned long sigignore;    // %lu
    unsigned long sigcatch;     // %lu
    unsigned long wchan;        // %lu
    unsigned long nswap;        // %lu
    unsigned long cnswap;       // %lu
    int exit_signal;            // %d
    int processor;              // %d
    unsigned long rt_priority;  // %lu
    unsigned long policy;       // %lu
    unsigned long long delayacct_blkio_ticks;   // %llu
} ;

static int readStat(int pid, struct statStuff *s) {

    const char *format = "%d %s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu";

    char buf[256];
    FILE *proc;
    sprintf(buf,"/proc/%d/stat",pid);
    proc = fopen(buf,"r");
    if (proc) {
        if (42==fscanf(proc, format,
                       &s->pid,
                       s->comm,
                       &s->state,
                       &s->ppid,
                       &s->pgrp,
                       &s->session,
                       &s->tty_nr,
                       &s->tpgid,
                       &s->flags,
                       &s->minflt,
                       &s->cminflt,
                       &s->majflt,
                       &s->cmajflt,
                       &s->utime,
                       &s->stime,
                       &s->cutime,
                       &s->cstime,
                       &s->priority,
                       &s->nice,
                       &s->num_threads,
                       &s->itrealvalue,
                       &s->starttime,
                       &s->vsize,
                       &s->rss,
                       &s->rlim,
                       &s->startcode,
                       &s->endcode,
                       &s->startstack,
                       &s->kstkesp,
                       &s->kstkeip,
                       &s->signal,
                       &s->blocked,
                       &s->sigignore,
                       &s->sigcatch,
                       &s->wchan,
                       &s->nswap,
                       &s->cnswap,
                       &s->exit_signal,
                       &s->processor,
                       &s->rt_priority,
                       &s->policy,
                       &s->delayacct_blkio_ticks
                       )) {
            printf("Stack start = %x\n",s->startstack);
            printf("Stack end = %x\n",s->kstkesp);
            fclose(proc);
            return 1;
        } else {
            fclose(proc);
            return 0;
        }
    } else {
        return 0;
    }
}

int eval(const char *cmdline)
{
    char *argv[MAXARGS];
    int bg;
    pid_t pid;
    int status;
    struct statStuff test;
    int value;

    pid = fork();
    if (pid > 0){
        /* This is the parent process. */
        char* p;
        int n_bytes = 256;
        int bytes_read;
        char buffer[256];

        printf("PARENT PROCESS 1\n");
        //   while (!(*value)){
        //
        //  }
        sleep(2);
        printf("PARENT PROCESS 2\n");

        sem_getvalue(sem, &value);
        sprintf(buffer,"/proc/%d/maps",value);
        printf("buffer = %s\n",buffer);
        int fd  = open(buffer,O_RDONLY);
        printf("fd = %d\n",fd);
        p = (char *)malloc(256);
        while (bytes_read = read(fd,p,n_bytes)){
            //printf("STRING = %s\n",p);
            //break;
            if (strstr(p,"[heap]") !=  NULL){
                printf("%s\n",p);
            }
            free(p);
            p = (char *)malloc(256);
            // lseek(fd,256,SEEK_CUR);
        }
        waitpid(pid, &status, 0);
        //      execl(SHELL,SHELL,"-c",cmdline,NULL);
        /*
         memset(&test, 0, sizeof(struct statStuff));
         readStat(pid, &test);
         //         _exit(EXIT_FAILURE);
         */
    }
    else if (pid < 0){
        /* The fork failed. Report failure. */
        status = -1;
    }
    else{
        printf("BOO!\n");
        sem = sem_open("pSem", O_CREAT | O_EXCL, pid);
        printf("FOO!\n");
        //      sem_unlink("pSem");
        printf("HOWDY\n");
        //    printf("ERROR = %d\n",
        execl("/h/fchang03","/h/fchang03/two",0);
        perror("execl");
    }
}

int main(void)
{
    char cmdline[MAXLINE];
    char temp[MAXLINE];

    shmkey = ftok("/dev/null",5);
    shmid = shmget(shmkey, sizeof (int), 0644 | IPC_CREAT);
    if (shmid < 0){
        perror("shmget\n");
        exit(1);
    }

    p = (int *)shmat(shmid, NULL, 0);
    *p = 0;

    while (1){
        printf("> ");
        fgets(temp, MAXLINE, stdin);
        sprintf(cmdline,"./%s",temp);
        if (feof(stdin)){
            exit(0);
        }
        printf("cmdline = %s\n",cmdline);
        eval(cmdline);
    }

    shmdt(p);
    shmctl(shmid, IPC_RMID, 0);

    sem_destroy(sem);
    exit(0);
}
4

2 回答 2

5

这条线

 execl("/h/fchang03","/h/fchang03/two",0);

建议您正在尝试执行目录。

于 2013-10-16T21:46:24.913 回答
0

“这些函数的初始参数是要执行的文件的名称。”

看起来您提供了目录的名称,而不是文件的名称。


此外:

“参数列表必须由 NULL 指针终止,并且由于这些是可变参数函数,因此该指针必须强制转换 (char *) NULL。”

你在这条线上的演员在哪里(char*)

execl("/h/fchang03","/h/fchang03/two",0);
于 2013-10-16T21:52:30.883 回答