Browse Source

Convert the remaining printf to logging commands

Baptiste Jonglez 7 years ago
parent
commit
81d2f97381
3 changed files with 22 additions and 11 deletions
  1. 0 2
      src/callouts.cc
  2. 15 0
      src/messages.mes
  3. 7 9
      src/runscript.cc

+ 0 - 2
src/callouts.cc

@@ -39,7 +39,6 @@ int pkt4_receive(CalloutHandle& handle) {
     /* Run script */
     int ret;
     ret = run_script("pkt4_receive", env);
-    fprintf(stderr, "ret = %d\n", ret);
     return 0;
 }
 
@@ -57,7 +56,6 @@ int lease4_select(CalloutHandle& handle) {
     /* Run script */
     int ret;
     ret = run_script("lease4_select", env);
-    fprintf(stderr, "ret = %d\n", ret);
     return 0;
 }
 

+ 15 - 0
src/messages.mes

@@ -7,3 +7,18 @@ A parameter of this library hook is defined in the configuration but has the
 wrong type.  For instance, a string may have been passed as parameter while
 an integer was expected.
 
+% RUNSCRIPT_FORK_FAILED fork() failed with error: %1
+Running the user-defined script is done with fork + exec, and there was an error
+during the fork, possibly due to a lack of resources.
+
+% RUNSCRIPT_EXEC_FAILED exec() failed, please check that the script exists and is executable. Error: %1
+Attempting to execute the user-defined script failed.
+
+% RUNSCRIPT_WAITPID_FAILED waitpid() failed with error: %1
+The main hook process failed to wait for the child process to exit.
+The hook runs the user-defined script in a child process, and normally waits for it to exit.
+
+% RUNSCRIPT_WAITING_SCRIPT the user-defined script is running, and the main process is currently waiting
+The user-defined script has just been launched, and Kea is waiting for it to exit.
+This is useful to know, because if the user-defined script blocks, Kea will stay stuck
+at this point.

+ 7 - 9
src/runscript.cc

@@ -2,12 +2,12 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-// TODO: Not nescessary
-#include <stdio.h>
 
 #include <string>
 #include <vector>
+#include <cerrno>
 
+#include "logger.h"
 #include "common.h"
 
 extern "C" {
@@ -27,23 +27,21 @@ int run_script(std::string arg0, std::vector<std::string> env)
     pid_t pid;
     pid = fork();
     if (pid == -1) {
-        // TODO: logging, errno is usable
-        fprintf(stderr, "Error during fork()\n");
+        LOG_ERROR(runscript_logger, RUNSCRIPT_FORK_FAILED).arg(strerror(errno));
         return -1;
     }
     if (pid == 0) {
         /* Child process */
         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");
+        LOG_ERROR(runscript_logger, RUNSCRIPT_EXEC_FAILED).arg(strerror(errno));
+        /* This only exists the child, not Kea itself. */
         exit(EXIT_FAILURE);
     } else {
         /* Parent process */
-        fprintf(stderr, "Waiting for script to return...\n");
+        LOG_DEBUG(runscript_logger, 50, RUNSCRIPT_WAITING_SCRIPT);
         ret = wait(&wstatus);
         if (ret == -1) {
-            // TODO: logging, errno is usable
-            fprintf(stderr, "waitpid() failure\n");
+            LOG_ERROR(runscript_logger, RUNSCRIPT_WAITPID_FAILED).arg(strerror(errno));
             return -1;
         }
         /* Get exit code */