Start New Discussion within our Software Development Community

Hi people. I'm trying to run whoami using execv but I'm getting a problem:

--- CHILD PROCESS ---
child PID: 3146
parent PID: 3145
execv: Permission denied
child exit code: 1

I already have certified myself that I have run permission for whoami for all users. I don't know what I can do to solve this problem.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    pid_t cpid; 
    int status, err;
    char* cmd[] = {"whoami", NULL};

    cpid = fork();

    if(cpid == 0) 
    { 
       printf("--- CHILD PROCESS ---\n");
       printf("child PID: %ld\n", (long)getpid());
       printf("parent PID: %ld\n", (long)getppid());


       if((err = execv("/usr/bin/", cmd)) == -1)
       {
          perror("execv");
          exit(EXIT_FAILURE);  
       }     
    }    

    else if(cpid < 0)
    {
       perror("fork");
       exit(EXIT_FAILURE);
    }    

    else { /* parent process */
        wait(&status);
        printf("child exit code: %d\n", WEXITSTATUS(status));
    }
    return EXIT_SUCCESS;
}

Your problem is that your first argument should be then entire command path, and the second an array of argument to pass to the command. IE,

const char* arglist[5] = {"arg1", "arg2", "arg3", "arg4", 0};
if ((err = execv("/usr/bin/cmd", arglist)) == -1)
.
.
.
This question has already been answered. Start a new discussion instead.