对于以下C程序,execl返回-1,表示权限被拒绝错误

时间:2013-10-16 21:42:14

标签: c linux

对于以下C程序,execl返回-1,表示权限被拒绝错误。我检查了读,写。执行我的可执行文件的权限,他们都是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);
}

2 个答案:

答案 0 :(得分:5)

这一行

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

表示您正在尝试执行目录。

答案 1 :(得分:0)

"The initial argument for these functions is the name of a file that is to be executed."

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


<强>此外:

"The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL."

你在这一行中对(char*)的演员在哪里?

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