From cc68426929697e79450998b856729e641ea0141d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A5=D1=80=D0=BE=D0=BC=D0=B5=D0=BD=D0=BE=D0=BA=20=D0=A0?= =?UTF-8?q?=D0=BE=D0=BC=D0=B0=D0=BD=20=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Thu, 23 May 2024 10:40:21 +0200 Subject: [PATCH] Added a feature that allows you to run python scripts from the cli without bash --- apps/cli/cli_common.c | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/apps/cli/cli_common.c b/apps/cli/cli_common.c index 9382e5a9..e6578f1a 100644 --- a/apps/cli/cli_common.c +++ b/apps/cli/cli_common.c @@ -774,6 +774,67 @@ 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 + * + * 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 pid = 0; + int status = 0; + char *script_path = NULL; + char *runner = "python3"; + struct passwd *pw = NULL; + + if (cvec_len(argv) > 1){ + fprintf(stderr,"A lot of arguments"); + return -1; + } + + if (cvec_len(argv) == 1){ + script_path = cv_string_get(cvec_i(argv, 0)); + } + + if (cvec_len(cvv) == 2){ + script_path = cv_string_get(cvec_i(cvv, 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"); + return -1; + } + + if ((pw = getpwuid(getuid())) == NULL){ + fprintf(stderr,"getpwuid -> %s", strerror(errno)); + return -1; + } + if (chdir(pw->pw_dir) < 0){ + fprintf(stderr,"chdir -> %s", strerror(errno)); + return -1; + } + + if ((pid = fork()) == 0) { + execlp(runner, runner, script_path, NULL); + perror("Error run script"); + exit(0); + } + + if (waitpid(pid, &status, 0) == pid) + return WEXITSTATUS(status); + else + return -1; +} + /*! Start bash from cli callback * * Typical usage: shell("System Bash") , cli_start_shell();