X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=execute.c;fp=execute.c;h=9a3d1f75059b112c3e961da9680e81a2f0d4c5fc;hb=07d2b55d88742b0ca38507daae7101619f0b96d5;hp=0000000000000000000000000000000000000000;hpb=e0072d01d5353d23d2e344c5bbb40ba0c8cb4241;p=fips diff --git a/execute.c b/execute.c new file mode 100644 index 0000000..9a3d1f7 --- /dev/null +++ b/execute.c @@ -0,0 +1,80 @@ +/* Copyright © 2013, Intel Corporation + * + * 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 + +static int +fork_exec_and_wait (char * const argv[]) +{ + pid_t pid; + int i, status; + + pid = fork (); + + /* Child */ + if (pid == 0) { + execvp (argv[0], argv); + fprintf (stderr, "Failed to execute:"); + for (i = 0; argv[i]; i++) { + fprintf (stderr, " %s", argv[i]); + } + fprintf (stderr, "\n"); + exit (1); + } + + /* Parent */ + waitpid (pid, &status, 0); + if (WIFEXITED (status)) { + return (WEXITSTATUS (status)); + } + if (WIFSIGNALED (status)) { + fprintf (stderr, "Child terminated by signal %d\n", + WTERMSIG (status)); + } + return 1; +} + +int +execute (int argc, char * const argv[]) +{ + char **execvp_args; + int i; + + execvp_args = malloc((argc + 1) * sizeof(char *)); + if (execvp_args == NULL) { + fprintf (stderr, "Out of memory,\n"); + return 1; + } + + for (i = 0; i < argc; i++) { + execvp_args[i] = argv[i]; + } + + /* execvp needs final NULL */ + execvp_args[i] = NULL; + + return fork_exec_and_wait (execvp_args); +}