]> git.cworth.org Git - fips/commitdiff
Add the ability to execute a program.
authorCarl Worth <cworth@cworth.org>
Mon, 22 Apr 2013 23:03:07 +0000 (16:03 -0700)
committerCarl Worth <cworth@cworth.org>
Tue, 23 Apr 2013 21:01:29 +0000 (14:01 -0700)
Simply fork and exec the command-line arguments given.

(Nothing is actually changed yet about the environment in which the
program is executed.)

Makefile.local
configure
execute.c [new file with mode: 0644]
execute.h [new file with mode: 0644]
fips.c

index 2b09150f9d8222f3c64b37d4cbd2fbe705a1c418..52a02ff0b5ee131e3cd008ad6b92a545900c3f34 100644 (file)
@@ -60,6 +60,7 @@ distclean: clean
        rm -rf $(DISTCLEAN)
 
 fips_srcs = \
+       execute.c \
        fips.c
 
 fips_modules = $(fips_srcs:.c=.o)
index f01633c99ed4d5fb0dfb100fe5302a87618e5783..91797ce86eedce8895de98550ef3139793ec08f6 100755 (executable)
--- 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 (file)
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 <stdio.h>
+#include <stdlib.h>
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+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 (file)
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 ce76e31421a586032012ae52dbaad8aabede699f..dbb8158b6ea0eee2b79ffed8b1dbb31f46802acc 100644 (file)
--- a/fips.c
+++ b/fips.c
@@ -24,6 +24,8 @@
 #include <limits.h>
 #include <getopt.h>
 
+#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;
 }