diff --git a/apps/cli/cli_common.c b/apps/cli/cli_common.c index e6578f1a..0f4201e7 100644 --- a/apps/cli/cli_common.c +++ b/apps/cli/cli_common.c @@ -774,31 +774,34 @@ cli_set_mode(clixon_handle h, return retval; } -/** +/*! * @brief Runs a Python script in a new process * - * @param h Clicon handle - * @param cvv CLIgen variable vector - * @param argv function arguments - * + * @param[in] h Clicon handle + * @param[in] cvv CLIgen variable vector + * @param[in] argv function arguments + * @retval 0 OK + * @retval -1 Error * The function creates a new child process in which the Python script is launched. * In the child process, before launching the script, environment variables are set * taken from cvv. * * The function returns the exit code of the Python script, or a value of (-1) on error. - * - * @return Returns 0 on success, -1 in case of error */ -int cli_start_python3(clixon_handle h, cvec *cvv, cvec *argv) { +int +cli_start_python3(clixon_handle h, + cvec *cvv, + cvec *argv) +{ int pid = 0; - int status = 0; + int retval = -1; char *script_path = NULL; char *runner = "python3"; struct passwd *pw = NULL; if (cvec_len(argv) > 1){ - fprintf(stderr,"A lot of arguments"); - return -1; + clixon_err(OE_PLUGIN, EINVAL, "A lot of arguments"); + goto done; } if (cvec_len(argv) == 1){ @@ -810,29 +813,32 @@ int cli_start_python3(clixon_handle h, cvec *cvv, cvec *argv) { } if ((cvec_len(cvv) == 2) && (cvec_len(argv) == 1)){ - fprintf(stderr,"Both the arguments of the function and the vector of values are specified\n"); - return -1; + clixon_err(OE_PLUGIN, EINVAL, "Both the arguments of the function and the vector of values are specified\n"); + goto done; } if ((pw = getpwuid(getuid())) == NULL){ - fprintf(stderr,"getpwuid -> %s", strerror(errno)); - return -1; + clixon_err(OE_PLUGIN, errno, "getpwuid"); + goto done; } if (chdir(pw->pw_dir) < 0){ - fprintf(stderr,"chdir -> %s", strerror(errno)); - return -1; + clixon_err(OE_PLUGIN, errno, "chdir"); + goto done; } if ((pid = fork()) == 0) { execlp(runner, runner, script_path, NULL); - perror("Error run script"); + clixon_err(OE_PLUGIN, errno, "Error run script"); exit(0); } - if (waitpid(pid, &status, 0) == pid) - return WEXITSTATUS(status); + if (waitpid(pid, &retval, 0) == pid) + goto done; else - return -1; + goto done; + + done: + return retval; } /*! Start bash from cli callback