/* * The MIT License (MIT) * * Copyright (c) 2016 Matias Linares. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include void spawn_child(char *progname, char **args, int fd); void usage(char *progname); void usage(char *progname) { printf("%s [-o outfile] program arg1 arg2...\n" "\n" "Profile the program passed by parameter with it's arguments.\n" "If the [-o outfile] option is provided, the output of the program\n" "will be stored on output file. Otherwise it won't print any output\n", progname); } void spawn_child(char *progname, char **args, int fd) { /* close the files descriptors. */ if(fd > 0) { close(STDOUT_FILENO); dup(fd); close(STDERR_FILENO); dup(fd); close(fd); } else { close(STDERR_FILENO); close(STDOUT_FILENO); } close(STDIN_FILENO); if((execvp(progname, args)) == -1) { fprintf(stderr, "%d - %s\n", errno, strerror(errno)); exit(1); } } int main(int argc, char **argv) { pid_t curpid; int status = -1; char *prog_name = NULL; char **args = NULL; char *out_file = NULL; int offset = 1; if(strcmp(argv[1], "-o") == 0) { out_file = (char *) calloc(strlen(argv[2]), sizeof(char)); strcpy(out_file, argv[2]); offset += 2; } if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { 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*)); for(int i = offset; i < argc; i++) args[i - offset] = argv[i]; if((curpid = fork()) < 0) { perror("unable to fork()"); exit(1); } if(curpid == 0) { int fd_out = -1; if(out_file) { /* Open the outfile with 664 permissions. */ fd_out = open(out_file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); } process_child(prog_name, args, fd_out); } else { struct rusage usage; int who = RUSAGE_CHILDREN; waitpid(curpid, &status, 0); getrusage(who, &usage); printf("user CPU time used:\t%lu %lu\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec); printf("system CPU time used:\t%lu %lu\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec); printf("residet set size used: %ld\n", usage.ru_maxrss); } return status; }