change getpwuid on getpwuid_r

remove exit()
edit chdir in chold process
This commit is contained in:
Хроменок Роман Владимирович 2024-05-29 19:33:42 +02:00 committed by Olof Hagsand
parent c831300a14
commit be7c5eb7ca

View file

@ -809,7 +809,10 @@ cli_start_program(clixon_handle h,
int retval = -1; int retval = -1;
char *script_path = NULL; char *script_path = NULL;
char *runner = NULL; char *runner = NULL;
struct passwd *pw = NULL; char *buf = NULL;
struct passwd pw, *pwresult = NULL;
size_t bufsize;
int s;
/* Check parameters */ /* Check parameters */
if (cvec_len(argv) == 0){ if (cvec_len(argv) == 0){
@ -837,28 +840,50 @@ cli_start_program(clixon_handle h,
script_path = cv_string_get(cvec_i(cvv, 1)); script_path = cv_string_get(cvec_i(cvv, 1));
} }
if ((pw = getpwuid(getuid())) == NULL){ bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
clixon_err(OE_PLUGIN, errno, "getpwuid"); if (bufsize == -1){
bufsize = 16384;
}
buf = malloc(bufsize);
if (buf == NULL) {
perror("malloc");
goto done; goto done;
} }
if (chdir(pw->pw_dir) < 0){
clixon_err(OE_PLUGIN, errno, "chdir"); s = getpwuid_r(getuid(), &pw, buf, bufsize, &pwresult);
if (pwresult == NULL) {
if (s == 0)
clixon_err(OE_PLUGIN, errno, "getpwuid_r");
else
perror("getpwuid_r");
goto done; goto done;
} }
/* main run */ /* main run */
if ((pid = fork()) == 0) { if ((pid = fork()) == 0) {
/* child process */
if (chdir(pw.pw_dir) < 0){
clixon_err(OE_PLUGIN, errno, "chdir");
}
execlp(runner, runner, script_path, NULL); execlp(runner, runner, script_path, NULL);
clixon_err(OE_PLUGIN, errno, "Error run script"); clixon_err(OE_PLUGIN, errno, "Error run script");
exit(0); return -1;
}else if(pid == -1){
clixon_err(OE_PLUGIN, errno, "fork");
}else{
/* parent process */
int status;
if (waitpid(pid, &status, 0) != pid ){
clixon_err(OE_PLUGIN, errno, "waitpid error");
goto done;
} else {
return WEXITSTATUS(status);
} }
}
if (waitpid(pid, &retval, 0) == pid) retval = 0;
goto done;
else
goto done;
done: done:
if(buf)
free(buf);
return retval; return retval;
} }