Skip to content

Commit

Permalink
Fix: change ipc_exec_as_process to use copy instead of pointer
Browse files Browse the repository at this point in the history
To make it more obvious on usage that a stack struct is preferred and
that ipc_exec_as_process will not clean the given pointer on call the
given ipc_exec_context is defined as a value instead of a pointer.
  • Loading branch information
nichtsfrei committed Sep 7, 2022
1 parent e371651 commit 1db7508
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
18 changes: 8 additions & 10 deletions misc/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ ipc_init (enum ipc_protocol type, enum ipc_relation relation)
}

struct ipc_context *
ipc_exec_as_process (enum ipc_protocol type, struct ipc_exec_context *exec_ctx)
ipc_exec_as_process (enum ipc_protocol type, struct ipc_exec_context exec_ctx)
{
struct ipc_context *pctx = NULL, *cctx = NULL;
pid_t pid;
if (exec_ctx == NULL || exec_ctx->func == NULL)
if (exec_ctx.func == NULL)
return NULL;
switch (type)
{
Expand Down Expand Up @@ -147,19 +147,17 @@ ipc_exec_as_process (enum ipc_protocol type, struct ipc_exec_context *exec_ctx)
exit (1);
}

if (exec_ctx->pre_func != NULL)
(*exec_ctx->pre_func) (cctx, exec_ctx->pre_arg);
(*exec_ctx->func) (cctx, exec_ctx->func_arg);
if (exec_ctx->post_func != NULL)
(*exec_ctx->post_func) (cctx, exec_ctx->pre_arg);
if (exec_ctx.pre_func != NULL)
(*exec_ctx.pre_func) (cctx, exec_ctx.pre_arg);
(*exec_ctx.func) (cctx, exec_ctx.func_arg);
if (exec_ctx.post_func != NULL)
(*exec_ctx.post_func) (cctx, exec_ctx.pre_arg);
switch (type)
{
case IPC_PIPE:
ipc_destroy (pctx);
break;
}
free (exec_ctx);
// we want to cleanup file handle
exit (0);
}

Expand All @@ -171,7 +169,7 @@ ipc_exec_as_process (enum ipc_protocol type, struct ipc_exec_context *exec_ctx)
}
pctx->relation = IPC_MAIN;
pctx->type = type;
pctx->context = exec_ctx->shared_context;
pctx->context = exec_ctx.shared_context;
}
// we are the parent process and return the id of the child process for
// observation
Expand Down
2 changes: 1 addition & 1 deletion misc/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ ipc_close (struct ipc_context *context);

struct ipc_context *
ipc_exec_as_process (enum ipc_protocol type,
struct ipc_exec_context *exec_context);
struct ipc_exec_context exec_context);

struct ipc_context *
ipc_init (enum ipc_protocol protocol, enum ipc_relation relation);
Expand Down
2 changes: 1 addition & 1 deletion src/processes.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ create_ipc_process (ipc_process_func func, void *args)
ec.post_func = (ipc_process_func) &post_fork_fun_call;
ec.func = (ipc_process_func) func;
ec.func_arg = args;
if ((pctx = ipc_exec_as_process (IPC_PIPE, &ec)) == NULL)
if ((pctx = ipc_exec_as_process (IPC_PIPE, ec)) == NULL)
{
g_warning ("Error : could not fork ! Error : %s", strerror (errno));
return FORKFAILED;
Expand Down
2 changes: 0 additions & 2 deletions src/processes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

#include "../misc/ipc.h"

#include "../misc/ipc.h"

#include <sys/types.h> /* for pid_t */

#define FORKFAILED -1
Expand Down

0 comments on commit 1db7508

Please sign in to comment.