bu_process_wait needs the process by reference

small qol, but makes quite the difference. Since we handle the free
inside 'wait', we need to be able to null out the pointer to protect
against future (albeit incorrect) calls after the function.
This commit is contained in:
f4alt committed 2024-04-10 00:07:12 -04:00
1 parent 5cf286398d
commit fa786a7f66
8 files changed
+32 -32

No files matched your search

+1 -1
View File
@@ -149,7 +149,7 @@ BU_EXPORT extern void bu_process_exec(struct bu_process **info, const char *cmd,
* undocumented.
* FIXME: wtime is no longer used on any platforms.
*/
BU_EXPORT extern int bu_process_wait(int *aborted, struct bu_process *pinfo, int wtime);
BU_EXPORT extern int bu_process_wait(int *aborted, struct bu_process **pinfo, int wtime);
/**
* @brief detect whether or not a program is being run in interactive mode
+14 -14
View File
@@ -228,12 +228,12 @@ bu_process_exec(struct bu_process **p, const char *cmd, int argc, const char **a
}
int
bu_process_wait(int *aborted, struct bu_process *pinfo, int UNUSED(wtime))
bu_process_wait(int *aborted, struct bu_process **pinfo, int UNUSED(wtime))
{
int rc = 0;
if (subprocess_join(pinfo->subprocess_p, &rc)) {
if (subprocess_join((*pinfo)->subprocess_p, &rc)) {
/* unsuccessful join, forcefully destroy */
(void)subprocess_destroy(pinfo->subprocess_p);
(void)subprocess_destroy((*pinfo)->subprocess_p);
}
// NOTE: if we want to maintain an 'aborted' var, we have to do some platform specific gunk
@@ -246,29 +246,29 @@ bu_process_wait(int *aborted, struct bu_process *pinfo, int UNUSED(wtime))
}
#else
if (GetLastError() == ERROR_PROCESS_ABORTED || rc == BU_MSVC_ABORT_EXIT) {
pinfo->aborted = 1;
(*pinfo)->aborted = 1;
}
#endif
if (aborted) {
(*aborted) = pinfo->aborted;
(*aborted) = (*pinfo)->aborted;
}
/* Clean up */
bu_process_close(pinfo, BU_PROCESS_STDOUT);
bu_process_close(pinfo, BU_PROCESS_STDERR);
bu_process_close(*pinfo, BU_PROCESS_STDOUT);
bu_process_close(*pinfo, BU_PROCESS_STDERR);
/* Free copy of exec args and struct allocs */
bu_free((void *)pinfo->cmd, "pinfo cmd copy");
bu_free((void *)(*pinfo)->cmd, "pinfo cmd copy");
if (pinfo->argv) {
for (int i = 0; i < pinfo->argc; i++) {
bu_free((void *)pinfo->argv[i], "pinfo argv member");
if ((*pinfo)->argv) {
for (int i = 0; i < (*pinfo)->argc; i++) {
bu_free((void *)(*pinfo)->argv[i], "pinfo argv member");
}
bu_free((void *)pinfo->argv, "pinfo argv array");
bu_free((void *)(*pinfo)->argv, "pinfo argv array");
}
BU_PUT(pinfo->subprocess_p, struct subprocess_t);
BU_PUT(pinfo, struct bu_process);
BU_PUT((*pinfo)->subprocess_p, struct subprocess_t);
BU_PUT(*pinfo, struct bu_process);
return rc;
}
+11 -11
View File
@@ -60,7 +60,7 @@ int _test_read(const char* cmd) {
return PROCESS_FAIL;
}
if (bu_process_wait(&aborted, p, 0)) {
if (bu_process_wait(&aborted, &p, 0)) {
fprintf(stderr, "bu_process_test[\"read\"] - wait failed\n");
return PROCESS_FAIL;
}
@@ -93,7 +93,7 @@ int _test_read_stderr(const char* cmd) {
return PROCESS_FAIL;
}
if (bu_process_wait(&aborted, p, 0)) {
if (bu_process_wait(&aborted, &p, 0)) {
fprintf(stderr, "bu_process_test[\"read\"] - wait failed\n");
return PROCESS_FAIL;
}
@@ -124,7 +124,7 @@ int _test_read_flood(const char* cmd) {
stderr_done = 1;
}
if (bu_process_wait(NULL, p, 0)) {
if (bu_process_wait(NULL, &p, 0)) {
fprintf(stderr, "bu_process_test[\"read\"] - wait failed\n");
return PROCESS_FAIL;
}
@@ -143,7 +143,7 @@ int _test_exec_wait(const char* cmd) {
// TODO: add tests to check options flags
bu_process_exec(&p, cmd, 2, (const char**)run_av, 0, 0);
if (bu_process_wait(NULL, p, 0)) {
if (bu_process_wait(NULL, &p, 0)) {
fprintf(stderr, "bu_process_test[\"exec_wait\"] - wait failed\n");
return PROCESS_FAIL;
}
@@ -180,7 +180,7 @@ int _test_ids(const char* cmd) {
return PROCESS_FAIL;
}
if (bu_process_wait(NULL, p, 0)) {
if (bu_process_wait(NULL, &p, 0)) {
fprintf(stderr, "bu_process_test[\"ids\"] - wait failed\n");
return PROCESS_FAIL;
}
@@ -251,7 +251,7 @@ int _test_streams(const char* cmd) {
bu_process_close(p, BU_PROCESS_STDOUT);
bu_process_close(p, BU_PROCESS_STDERR);
if (bu_process_wait(NULL, p, 0) == -1) {
if (bu_process_wait(NULL, &p, 0) == -1) {
fprintf(stderr, "bu_process_test[\"streams\"] - wait failed\n");
return PROCESS_FAIL;
}
@@ -278,7 +278,7 @@ int _test_abort(const char* cmd) {
return PROCESS_FAIL;
}
if (!bu_process_wait(&aborted, p, 0)) {
if (!bu_process_wait(&aborted, &p, 0)) {
fprintf(stderr, "bu_process_test[\"terminate\"] - wait should have reported abort code\n");
return PROCESS_FAIL;
}
@@ -326,7 +326,7 @@ int _test_args(const char* cmd) {
}
}
if (bu_process_wait(NULL, p, 0)) {
if (bu_process_wait(NULL, &p, 0)) {
fprintf(stderr, "bu_process_test[\"args\"] - wait failed\n");
return PROCESS_FAIL;
}
@@ -352,7 +352,7 @@ int _test_alive(const char* cmd) {
return PROCESS_FAIL;
}
if (bu_process_wait(NULL, p, 0)) {
if (bu_process_wait(NULL, &p, 0)) {
fprintf(stderr, "bu_process_test[\"alive\"] - wait failed\n");
return PROCESS_FAIL;
}
@@ -422,7 +422,7 @@ int _test_async_balanced(const char* cmd) {
return PROCESS_FAIL;
}
if (bu_process_wait(NULL, p, 0)) {
if (bu_process_wait(NULL, &p, 0)) {
fprintf(stderr, "bu_process_test[\"async_bal\"] - wait failed\n");
return PROCESS_FAIL;
}
@@ -465,7 +465,7 @@ int _test_async_unbalanced(const char* cmd) {
return PROCESS_FAIL;
}
if (bu_process_wait(NULL, p, 0)) {
if (bu_process_wait(NULL, &p, 0)) {
fprintf(stderr, "bu_process_test[\"async_unbal\"] - wait failed\n");
return PROCESS_FAIL;
}
+2 -2
View File
@@ -1616,7 +1616,7 @@ _ged_rt_output_handler2(void *clientData, int type)
/* Either EOF has been sent or there was a read error.
* there is no need to block indefinitely */
retcode = bu_process_wait(&aborted, rrtp->p, 120);
retcode = bu_process_wait(&aborted, &rrtp->p, 120);
if (aborted)
bu_log("Raytrace aborted.\n");
@@ -1712,7 +1712,7 @@ _ged_rt_output_handler(void *clientData, int mask)
/* Either EOF has been sent or there was a read error.
* there is no need to block indefinitely */
retcode = bu_process_wait(&aborted, rrtp->p, 120);
retcode = bu_process_wait(&aborted, &rrtp->p, 120);
if (aborted)
bu_log("Raytrace aborted.\n");
+1 -1
View File
@@ -63,7 +63,7 @@ rtcheck_handler_cleanup(struct ged_rtcheck *rtcp)
bu_process_close(rrtp->p, BU_PROCESS_STDOUT);
/* wait for the forked process */
retcode = bu_process_wait(NULL, rrtp->p, 0);
retcode = bu_process_wait(NULL, &rrtp->p, 0);
if (retcode != 0) {
_ged_wait_status(gedp->ged_result_str, retcode);
}
+1 -1
View File
@@ -85,7 +85,7 @@ rtcheck_handler_cleanup(struct ged_rtcheck *rtcp, int type)
bu_process_close(p->p, BU_PROCESS_STDOUT);
/* wait for the forked process */
int retcode = bu_process_wait(NULL, p->p, 0);
int retcode = bu_process_wait(NULL, &p->p, 0);
if (retcode != 0) {
_ged_wait_status(gedp->ged_result_str, retcode);
}
+1 -1
View File
@@ -355,7 +355,7 @@ subprocess_launcher(int id, void *data)
memset(line, 0, MAXPATHLEN+501);
}
if (bu_process_wait(&aborted, p, 120)) {
if (bu_process_wait(&aborted, &p, 120)) {
bu_vls_printf(sd->sd_result, "Test %ld(process %ld): rt_cache_subprocess failed\n", sd->test_num, sd->process_num);
sd->result = 1;
}
+1 -1
View File
@@ -2463,7 +2463,7 @@ mged_finish(int exitcode)
for (size_t i = 0; i < BU_PTBL_LEN(&GEDP->ged_subp); i++) {
int aborted = 0;
struct ged_subprocess *rrp = (struct ged_subprocess *)BU_PTBL_GET(&GEDP->ged_subp, i);
if (bu_process_wait(&aborted, rrp->p, 1) != -1) {
if (bu_process_wait(&aborted, &rrp->p, 1) != -1) {
bu_ptbl_ins(&rmp, (long *)rrp);
}
}