Browse Source

Simplify parsing of script name

Baptiste Jonglez 7 years ago
parent
commit
405d040514
7 changed files with 32 additions and 21 deletions
  1. 2 0
      Makefile
  2. 12 0
      src/common.h
  3. 1 4
      src/lease_select.cc
  4. 8 3
      src/load.cc
  5. 1 4
      src/pkt_receive.cc
  6. 7 9
      src/runscript.cc
  7. 1 1
      src/runscript.h

+ 2 - 0
Makefile

@@ -11,6 +11,8 @@ kea-hook-runscript.so: $(OBJECTS)
 
 src/pkt_receive.o: src/runscript.h
 src/lease_select.o: src/runscript.h
+src/load.o: src/common.h
+src/runscript.o: src/common.h
 
 clean:
 	rm -f src/*.o

+ 12 - 0
src/common.h

@@ -0,0 +1,12 @@
+#include <string>
+
+extern "C" {
+
+/* From load.cc */
+
+/* Path of the script to be run in hooks. */
+extern std::string script_path;
+/* Name of the script (without the leading directory). */
+extern std::string script_name;
+
+}

+ 1 - 4
src/lease_select.cc

@@ -9,15 +9,12 @@
 using namespace isc::dhcp;
 using namespace isc::hooks;
 
-/* From load.cc */
-extern std::string script_path;
-
 extern "C" {
 
 int lease4_select(CalloutHandle& handle) {
     int ret;
     char *env[] = { "FOO=bar", (char *)NULL };
-    ret = run_script(script_path.data(), "lease4_select", env);
+    ret = run_script("lease4_select", env);
     fprintf(stderr, "ret = %d\n", ret);
     return 0;
 }

+ 8 - 3
src/load.cc

@@ -1,11 +1,15 @@
 #include <hooks/hooks.h>
 
+#include "common.h"
+
 using namespace isc::hooks;
 using namespace isc::data;
 
-/* Path of the script to run in hooks, accessed by the other files of this
- * library. */
+/* Path of the script to be run in hooks, accessed by the other files of
+ * this library. */
 std::string script_path;
+/* Name of the script (without the leading directory). */
+std::string script_name;
 
 extern "C" {
 
@@ -18,7 +22,8 @@ int load(LibraryHandle& handle) {
         return 1;
     }
     script_path = script->stringValue();
-    
+    script_name = script_path.substr(script_path.find_last_of('/') + 1);
+
     return 0;
 }
 

+ 1 - 4
src/pkt_receive.cc

@@ -8,15 +8,12 @@
 using namespace isc::dhcp;
 using namespace isc::hooks;
 
-/* From load.cc */
-extern std::string script_path;
-
 extern "C" {
 
 int pkt4_receive(CalloutHandle& handle) {
     int ret;
     char *env[] = { "FOO=bar", "BAZ=bang", (char *)NULL };
-    ret = run_script(script_path.data(), "pkt4_receive", env);
+    ret = run_script("pkt4_receive", env);
     fprintf(stderr, "ret = %d\n", ret);
     return 0;
 }

+ 7 - 9
src/runscript.cc

@@ -6,12 +6,14 @@
 // TODO: Not nescessary
 #include <stdio.h>
 
+#include "common.h"
+
 extern "C" {
 
-/* Runs the given script with the given argument and environment
- * variables.  Returns -1 upon failure, or the exit code of the script
- * upon success. */
-int run_script(const char *scriptpath, 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(const char *arg0, char *const *envp)
 {
     int ret, wstatus, exitcode;
     pid_t pid;
@@ -23,11 +25,7 @@ int run_script(const char *scriptpath, const char *arg0, char *const *envp)
     }
     if (pid == 0) {
         /* Child process */
-        const char *scriptname = strrchr(scriptpath, '/');
-        if (scriptname == NULL) {
-            scriptname = scriptpath + strlen(scriptpath);
-        }
-        ret = execle(scriptpath, scriptname, arg0, (char *)NULL, envp);
+        ret = execle(script_path.data(), script_name.data(), arg0, (char *)NULL, envp);
         // TODO: logging, errno is usable
         fprintf(stderr, "Error during execle() in child\n");
         exit(EXIT_FAILURE);

+ 1 - 1
src/runscript.h

@@ -1,6 +1,6 @@
 
 extern "C" {
 
-int run_script(const char *scriptpath, const char *arg0, char *const *envp);
+int run_script(const char *arg0, char *const *envp);
 
 }