From: Carl Worth Date: Mon, 22 Apr 2013 23:03:07 +0000 (-0700) Subject: Add the ability to execute a program. X-Git-Url: https://git.cworth.org/git?p=fips;a=commitdiff_plain;h=07d2b55d88742b0ca38507daae7101619f0b96d5 Add the ability to execute a program. Simply fork and exec the command-line arguments given. (Nothing is actually changed yet about the environment in which the program is executed.) --- diff --git a/Makefile.local b/Makefile.local index 2b09150..52a02ff 100644 --- a/Makefile.local +++ b/Makefile.local @@ -60,6 +60,7 @@ distclean: clean rm -rf $(DISTCLEAN) fips_srcs = \ + execute.c \ fips.c fips_modules = $(fips_srcs:.c=.o) diff --git a/configure b/configure index f01633c..91797ce 100755 --- a/configure +++ b/configure @@ -369,7 +369,7 @@ CC = ${CC} CFLAGS = ${CFLAGS} # Default FLAGS for the linker (can be overridden by user such as "make LDFLAGS=-znow") -LDFLAGS = ${LDFLAGS} +LDFLAGS = ${LDFLAGS} -ldl # Flags to enable warnings when using the C compiler WARN_CFLAGS=${WARN_CFLAGS} 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); +} diff --git a/execute.h b/execute.h new file mode 100644 index 0000000..8b9192f --- /dev/null +++ b/execute.h @@ -0,0 +1,30 @@ +/* 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. + */ + +#ifndef EXECUTE_H +#define EXECUTE_H + +/* Execute the program with arguments as specified. + */ +int +execute (int argc, char * const argv[]); + +#endif diff --git a/fips.c b/fips.c index ce76e31..dbb8158 100644 --- a/fips.c +++ b/fips.c @@ -24,6 +24,8 @@ #include #include +#include "execute.h" + static void usage (void) { @@ -39,7 +41,8 @@ usage (void) int main (int argc, char *argv[]) { - int opt; + int opt, ret; + const char *short_options = "h"; const struct option long_options[] = { {"help", no_argument, 0, 'h'}, @@ -71,7 +74,9 @@ main (int argc, char *argv[]) exit (1); } - return 0; + ret = execute (argc - optind, &argv[optind]); + + return ret; }