diff options
author | Matias Linares <matiaslina@openmailbox.org> | 2016-01-05 21:21:04 -0300 |
---|---|---|
committer | Matias Linares <matiaslina@openmailbox.org> | 2016-01-05 21:21:04 -0300 |
commit | f7e0cad06fbabec0ea1a83d8913b6c213dc62c68 (patch) | |
tree | 2a842a421c7555f5fef9be41db76cd5e5bdf084e | |
parent | ef135e7a0b99f25b8e36dbe112c950b97d2057d2 (diff) | |
download | profile-f7e0cad06fbabec0ea1a83d8913b6c213dc62c68.tar.gz |
Don't close STDIN for the child process.
Also this fixes a bug with the argv parameters. The first argument of
argv for execvp needs to be the name of the program that is running
-rw-r--r-- | profile.c | 24 |
1 files changed, 17 insertions, 7 deletions
@@ -63,8 +63,6 @@ void spawn_child(char *progname, char **args, int fd) close(STDOUT_FILENO); } - close(STDIN_FILENO); - if((execvp(progname, args)) == -1) { fprintf(stderr, "%d - %s\n", errno, strerror(errno)); @@ -93,16 +91,19 @@ int main(int argc, char **argv) usage(argv[0]); return 0; } - prog_name = (char *) calloc(strlen(argv[offset]), sizeof(char)); strcpy(prog_name, argv[offset]); - offset++; if(offset < argc) - args = calloc(argc - offset, sizeof(char*)); + args = calloc(argc - offset + 1, sizeof(char*)); + /* The last parameter of the argv needs to be NULL. */ for(int i = offset; i < argc; i++) - args[i - offset] = argv[i]; + { + args[i - offset] = (char *) calloc(strlen(argv[i]) + 1, sizeof(char)); + strncpy(args[i - offset], argv[i], strlen(argv[i])); + } + args[argc - offset] = NULL; if((curpid = fork()) < 0) { @@ -117,7 +118,7 @@ int main(int argc, char **argv) { /* Open the outfile with 664 permissions. */ fd_out = open(out_file, - O_WRONLY | O_CREAT, + O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); } spawn_child(prog_name, args, fd_out); @@ -152,5 +153,14 @@ int main(int argc, char **argv) #endif } + /* Clean up */ + if(prog_name) + free(prog_name); + for(int i = 0; i < argc - offset + 1; ++i) + if(args[i] != NULL) + free(args[i]); + if(args) + free(args); + return status; } |