本文介绍了对于下面的C程序,execl的返回-1,标志着一个权限被拒绝的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于下面的C程序,execl的返回-1,标志着一个权限被拒绝的错误。我查了读,写。执行我的可执行文件的权限和他们都RWX。可能有人请让我知道为什么execl的返回一个权限被拒绝的错误?非常感谢你。

 的#include<&stdio.h中GT;
#包括LT&;&STDDEF.H GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / wait.h>
#包括LT&; SYS / stat.h>
#包括LT&;&fcntl.h GT;
#包括LT&; SYS / shm.h>
#包括LT&;&errno.h中GT;
#包括LT&;&semaphore.h GT;#定义MAXLINE 256
#定义MAXARGS 128
#定义SHELL/ H / fchang03/// bin / sh的的key_t shmkey;
INT的shmid;
sem_t * sem的;
INT * P;结构statStuff {
    INT PID; //%d个
    焦炭通讯[256]; //%S
    焦状态; // %C
    INT PPID; //%d个
    INT PGRP; //%d个
    INT会议; //%d个
    INT tty_nr; //%d个
    INT tpgid; //%d个
    无符号长标志; //%禄
    无符号长minflt; //%禄
    无符号长cminflt; //%禄
    无符号长majflt; //%禄
    无符号长cmajflt; //%禄
    无符号长UTIME; //%禄
    无符号长STIME; //%禄
    长cutime; //%LD
    长cstime; //%LD
    长期优先; //%LD
    长美观大方; //%LD
    长NUM_THREADS; //%LD
    长itrealvalue; //%LD
    无符号长开始时间; //%禄
    无符号长VSIZE; //%禄
    长RSS; //%LD
    无符号长RLIM; //%禄
    无符号长启动code; //%禄
    无符号长端code; //%禄
    无符号长startstack; //%禄
    无符号长kstkesp; //%禄
    无符号长kstkei​​p; //%禄
    无符号长信号; //%禄
    无符号长堵塞; //%禄
    无符号长sigignore; //%禄
    无符号长sigcatch; //%禄
    无符号长WCHAN; //%禄
    无符号长nswap; //%禄
    无符号长cnswap; //%禄
    INT exit_signal; //%d个
    INT处理器; //%d个
    无符号长rt_priority; //%禄
    unsigned long类型的政策; //%禄
    无符号长长delayacct_blkio_ticks; //%LLU
};静态INT readStat(INT PID,结构statStuff * S){    为const char *格式=%d个%S%C%D%D%D%禄%禄%禄%禄%禄%禄%禄%LD%LD%LD%LD%LD%LD%禄%lu个%ld的%lu个%lu个%lu个%lu个%lu个%lu个%lu个%lu个%lu个%lu个%lu个%lu个%lu个%D%禄禄%LLU%;    焦炭BUF [256];
    FILE * PROC;
    sprintf的(BUF的/ proc /%D / STAT,PID);
    PROC = FOPEN(BUF,R);
    如果(PROC){
        如果(42 ==的fscanf(PROC,格式
                       &安培; S-> PID,
                       S-GT&;通讯,
                       &安培; S->的状态,
                       &安培; S-> PPID,
                       &安培; S-> PGRP,
                       &安培; S->会议,
                       &安培; S-> tty_nr,
                       &安培; S-> tpgid,
                       &安培; S->的标志,
                       &安培; S-> minflt,
                       &安培; S-> cminflt,
                       &安培; S-> majflt,
                       &安培; S-> cmajflt,
                       &安培; S-> UTIME,
                       &安培; S-> STIME,
                       &安培; S-> cutime,
                       &安培; S-> cstime,
                       &安培; S->的优先级,
                       &安培; S->不错,
                       &安培; S-> NUM_THREADS,
                       &安培; S-> itrealvalue,
                       &安培; S->开始时间,
                       &安培; S-> VSIZE,
                       &安培; S-> RSS,
                       &安培; S-> RLIM,
                       &安培; S->启动code,
                       &安培; S->结束code,
                       &安培; S-> startstack,
                       &安培; S-> kstkesp,
                       &安培; S-> kstkei​​p,
                       &放大器; S->信号,
                       &安培; S->受阻,
                       &安培; S-> sigignore,
                       &安培; S-> sigcatch,
                       &安培; S-> WCHAN,
                       &安培; S-> nswap,
                       &安培; S-> cnswap,
                       &安培; S-> exit_signal,
                       &安培; S->处理器,
                       &安培; S-> rt_priority,
                       &安培; S->的政策,
                       &安培; S-> delayacct_blkio_ticks
                       )){
            的printf(堆栈开始=%X \\ n,S> startstack);
            的printf(堆栈结束=%X \\ n,S> kstkesp);
            FCLOSE(PROC);
            返回1;
        }其他{
            FCLOSE(PROC);
            返回0;
        }
    }其他{
        返回0;
    }
}INT的eval(为const char * CMDLINE)
{
    的char * ARGV [MAXARGS]
    INT BG;
    将为pid_t PID;
    INT状态;
    结构statStuff试验;
    int值;    PID =叉();
    如果(PID大于0){
        / *这是父进程。 * /
        字符* P;
        INT n_bytes = 256;
        INT bytes_read缓存;
        字符缓冲区[256];        的printf(父进程1 \\ n);
        //而(!(*值)){
        //
        //}
        睡眠(2);
        的printf(父进程2 \\ n);        sem_getvalue(SEM,&安培;值);
        sprintf的(缓冲的/ proc /%D /图,价值);
        的printf(缓冲=%S \\ n,缓冲区);
        INT FD =开放(缓冲,O_RDONLY);
        的printf(FD =%d个\\ N,FD);
        P =(字符*)malloc的(256);
        而(bytes_read缓存=读(FD,P,n_bytes)){
            //输出(STRING =%S \\ n,p)的;
            //打破;
            如果(的strstr(P[堆])!= NULL){
                的printf(%S \\ n,p)的;
            }
            自由(对);
            P =(字符*)malloc的(256);
            //了lseek(FD,256,SEEK_CUR);
        }
        waitpid函数(PID,和放大器;状态,0);
        // EXECL(外壳,外壳 - C,CMDLINE,NULL);
        / *
         memset的(安培;测试,0的sizeof(结构statStuff));
         readStat(PID,和放大器;测试);
         // _exit(EXIT_FAILURE);
         * /
    }
    否则如果(PID℃,){
        / *叉失败。报告故障。 * /
        状态= -1;
    }
    其他{
        的printf(BOO \\ n!);
        SEM = sem_open(统筹局常任秘书长,O_CREAT | O_EXCL,PID);
        的printf(FOO \\ n!);
        // sem_unlink(PSEM);
        的printf(你好\\ n);
        //的printf(ERROR =%d个\\ N,
        execl的(/小时/ fchang03,/小时/ fchang03 /二,0);
        PERROR(execl的);
    }
}INT主要(无效)
{
    烧焦CMDLINE [MAXLINE]
    焦温度[MAXLINE]    shmkey = ftok(的/ dev / null的,5);
    的shmid = shmget的(shmkey,sizeof的(INT),0644 | IPC_CREAT);
    如果(的shmid℃,){
        PERROR(shmget的\\ n);
        出口(1);
    }    p值=(INT *)的shmat(的shmid,NULL,0);
    * p值= 0;    而(1){
        的printf(>中);
        与fgets(温度,MAXLINE,标准输入);
        sprintf的(CMDLINE./%的,温度);
        如果(的feof(标准输入)){
            出口(0);
        }
        的printf(CMDLINE =%S \\ n,CMDLINE);
        的eval(CMDLINE);
    }    shmdt(P);
    了shmctl(的shmid,IPC_RMID,0);    sem_destroy(SEM);
    出口(0);
}


解决方案

这行

  EXECL(/ H / fchang03,/ H / fchang03 /二,0);

建议你试图执行一个目录。

For the following C program, execl returns -1, signifying a Permission Denied error. I checked the read,write. execute permissions of my executables and they were all rwx. Could someone please let me know why execl is returning a Permission Denied error? Thank you very much

#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);
}
解决方案

This line

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

suggests that you are trying to execute a directory.

这篇关于对于下面的C程序,execl的返回-1,标志着一个权限被拒绝的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 16:28