Reputation: 4146
I am writing own shell-like program and I keep getting errors on exec*
function call.
Here is source code of core processes.c
:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/uio.h>
#define BUFSIZE 128
#define EXIT_STR "exit"
int main(int argc, char ** argv) {
const char *prompt = "> ";
char buffer[BUFSIZE];
int bytes_read;
int status;
pid_t child_p;
while(1) {
printf("%s", prompt);
fflush(stdout);
bytes_read = read(0, buffer, BUFSIZE);
buffer[bytes_read-1] = '\0';
if(strncmp(EXIT_STR, buffer, bytes_read) == 0)
exit(0);
if((child_p = fork()) == 0) {
printf("[*] %d executing: %s\n", getpid(), buffer);
execlp(buffer, buffer);
printf("[*] %d got error on execlp\n", getpid());
exit(1);
} else {
waitpid(child_p, &status, 0);
printf("[*] child returned: %d\n", status);
}
}
}
I have also simple other.c
program for testing:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv){
printf("Hello. I am %s with pid: %d\n", argv[0], getpid());
exit(0);
}
I use llvm
on MacOS High Sierra for compilation:
$ llvm-gcc processes.c -o processes -Wall
$ ./processes
> other
[*] 6040 executing: other
[*] 6040 got error on execl
[*] child returned: 256
> ls
[*] 6041 executing: ls
[*] 6041 got error on execl
[*] child returned: 256
> exit
What am I missing?
Upvotes: 2
Views: 578
Reputation: 181459
Together, the second argument argument to execlp()
and any subsequent arguments correspond to the strings provided to the new program's main()
function in its argument vector. They must all be pointers to null-terminated C strings, except that the end of the list must be marked by a null pointer of type char *
. For example:
execlp(buffer, buffer, (char *) NULL);
This is a documented requirement for the arguments to this function, and your program mail fail if you do not satisfy it. If you wish, you may rationalize it as providing a means for the system to count the elements of the argument vector, so as to pass that number to the new main()
. You may also consider that the argument vector itself is documented to be terminated by a null pointer.
Upvotes: 1