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;
}