Refactoring has been performed

Goto added
Added margins
Added comments
This commit is contained in:
Хроменок Роман Владимирович 2024-05-23 19:37:44 +02:00 committed by Olof Hagsand
parent de820417d1
commit b81e18141d

View file

@ -774,31 +774,34 @@ cli_set_mode(clixon_handle h,
return retval; return retval;
} }
/** /*!
* @brief Runs a Python script in a new process * @brief Runs a Python script in a new process
* *
* @param h Clicon handle * @param[in] h Clicon handle
* @param cvv CLIgen variable vector * @param[in] cvv CLIgen variable vector
* @param argv function arguments * @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. * 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 * In the child process, before launching the script, environment variables are set
* taken from cvv. * taken from cvv.
* *
* The function returns the exit code of the Python script, or a value of (-1) on error. * 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 pid = 0;
int status = 0; int retval = -1;
char *script_path = NULL; char *script_path = NULL;
char *runner = "python3"; char *runner = "python3";
struct passwd *pw = NULL; struct passwd *pw = NULL;
if (cvec_len(argv) > 1){ if (cvec_len(argv) > 1){
fprintf(stderr,"A lot of arguments"); clixon_err(OE_PLUGIN, EINVAL, "A lot of arguments");
return -1; goto done;
} }
if (cvec_len(argv) == 1){ 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)){ 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"); clixon_err(OE_PLUGIN, EINVAL, "Both the arguments of the function and the vector of values are specified\n");
return -1; goto done;
} }
if ((pw = getpwuid(getuid())) == NULL){ if ((pw = getpwuid(getuid())) == NULL){
fprintf(stderr,"getpwuid -> %s", strerror(errno)); clixon_err(OE_PLUGIN, errno, "getpwuid");
return -1; goto done;
} }
if (chdir(pw->pw_dir) < 0){ if (chdir(pw->pw_dir) < 0){
fprintf(stderr,"chdir -> %s", strerror(errno)); clixon_err(OE_PLUGIN, errno, "chdir");
return -1; goto done;
} }
if ((pid = fork()) == 0) { if ((pid = fork()) == 0) {
execlp(runner, runner, script_path, NULL); execlp(runner, runner, script_path, NULL);
perror("Error run script"); clixon_err(OE_PLUGIN, errno, "Error run script");
exit(0); exit(0);
} }
if (waitpid(pid, &status, 0) == pid) if (waitpid(pid, &retval, 0) == pid)
return WEXITSTATUS(status); goto done;
else else
return -1; goto done;
done:
return retval;
} }
/*! Start bash from cli callback /*! Start bash from cli callback