I'm a beginner to systems programming and I am currently trying to play around with basic server/client communication using sockets, pipes, etc
More specifically I want to be able to connect as a client and input something like '/bin/echo hello'. The server will then split the string into the command and its arguments, and run the command with a call to a function that calls execl(). For now I'm just testing exec call before trying to pass user input in. Why does the following fail with errno set to EFAULT: bad address?
int do_command(char *command) {
if(strcmp(command, "some_string") == 0) {
pid_t pid = fork();
if(pid == -1) {
perror("fork");
exit(1);
}
if(pid == 0) {
if (execl("/bin/echo", "/bin/echo/", "hello", (char*)NULL) == -1) {
perror("execl");
exit(1);
}
} else {
printf("parent\n");
}
}
}
But running the same code in main() runs just fine with an output of 'hello'
int main() {
pid_t pid = fork();
if(pid == -1) {
perror("fork");
exit(1);
}
if(pid == 0) {
if (execl("/bin/echo", "/bin/echo/", "hello", (char*)NULL) == -1) {
perror("execl");
exit(1);
}
} else {
printf("parent\n");
}
}
Is it not possible to run exec() system calls within functions? Thanks in advance.