Browse Source

Use a vector of string to pass environment variables

Baptiste Jonglez 7 years ago
parent
commit
ce51e65f65
4 changed files with 31 additions and 9 deletions
  1. 5 1
      src/lease_select.cc
  2. 6 1
      src/pkt_receive.cc
  3. 14 6
      src/runscript.cc
  4. 6 1
      src/runscript.h

+ 5 - 1
src/lease_select.cc

@@ -4,6 +4,9 @@
 #include <dhcp/pkt6.h>
 #include <dhcpsrv/lease.h>
 
+#include <string>
+#include <vector>
+
 #include "runscript.h"
 
 using namespace isc::dhcp;
@@ -13,7 +16,8 @@ extern "C" {
 
 int lease4_select(CalloutHandle& handle) {
     int ret;
-    char *env[] = { "FOO=bar", (char *)NULL };
+    std::vector<std::string> env;
+    env.push_back("FOO=bar");
     ret = run_script("lease4_select", env);
     fprintf(stderr, "ret = %d\n", ret);
     return 0;

+ 6 - 1
src/pkt_receive.cc

@@ -3,6 +3,9 @@
 #include <dhcp/dhcp6.h>
 #include <dhcp/pkt6.h>
 
+#include <string>
+#include <vector>
+
 #include "runscript.h"
 
 using namespace isc::dhcp;
@@ -12,7 +15,9 @@ extern "C" {
 
 int pkt4_receive(CalloutHandle& handle) {
     int ret;
-    char *env[] = { "FOO=bar", "BAZ=bang", (char *)NULL };
+    std::vector<std::string> env;
+    env.push_back("DHCP4_RELAYED=1");
+    env.push_back("HWADDR_TYPE=foo");
     ret = run_script("pkt4_receive", env);
     fprintf(stderr, "ret = %d\n", ret);
     return 0;

+ 14 - 6
src/runscript.cc

@@ -2,19 +2,27 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include <string.h>
 // TODO: Not nescessary
 #include <stdio.h>
 
+#include <string>
+#include <vector>
+
 #include "common.h"
 
 extern "C" {
 
-/* Runs the configured script with the given (single) argument and
- * environment variables.  Returns -1 upon failure, or the exit code of
- * the script upon success. */
-int run_script(const char *arg0, char *const *envp)
+int run_script(std::string arg0, std::vector<std::string> env)
 {
+    /* Convert the vector containing environment variables to the format
+     * expected by execle(). */
+    char const* envp[env.size() + 1];
+    for (int i = 0; i < env.size(); ++i) {
+        envp[i] = env[i].data();
+    }
+    envp[env.size()] = (char const*) NULL;
+
+    /* fork() & execle() */
     int ret, wstatus, exitcode;
     pid_t pid;
     pid = fork();
@@ -25,7 +33,7 @@ int run_script(const char *arg0, char *const *envp)
     }
     if (pid == 0) {
         /* Child process */
-        ret = execle(script_path.data(), script_name.data(), arg0, (char *)NULL, envp);
+        ret = execle(script_path.data(), script_name.data(), arg0.data(), (char *)NULL, envp);
         // TODO: logging, errno is usable
         fprintf(stderr, "Error during execle() in child\n");
         exit(EXIT_FAILURE);

+ 6 - 1
src/runscript.h

@@ -1,6 +1,11 @@
+#include <string>
+#include <vector>
 
 extern "C" {
 
-int run_script(const char *arg0, char *const *envp);
+/* Runs the configured script with the given (single) argument and
+ * environment variables.  Returns -1 upon failure, or the exit code of
+ * the script upon success. */
+int run_script(std::string arg0, std::vector<std::string> env);
 
 }