Skip to content

Commit 1032030

Browse files
committed
Split the code to fill an exec closure into two functions.
This lets us initialize the exec closure early and fill in the events later. It also makes things consistent with the exec_pty version.
1 parent a4a999b commit 1032030

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

src/exec_monitor.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,22 +407,34 @@ exec_cmnd_pty(struct command_details *details, sigset_t *mask,
407407
}
408408

409409
/*
410-
* Fill in the monitor closure and setup initial events.
411-
* Allocates read events for the signal pipe, error pipe and backchannel.
410+
* Fill in the non-event part of the monitor closure.
412411
*/
413412
static void
414-
fill_exec_closure_monitor(struct monitor_closure *mc,
413+
init_exec_closure_monitor(struct monitor_closure *mc,
415414
const struct command_details *details, struct command_status *cstat,
416-
int errfd, int backchannel)
415+
int backchannel)
417416
{
418-
debug_decl(fill_exec_closure_monitor, SUDO_DEBUG_EXEC);
417+
debug_decl(init_exec_closure_monitor, SUDO_DEBUG_EXEC);
419418

420419
/* Fill in the non-event part of the closure. */
420+
memset(mc, 0, sizeof(*mc));
421421
mc->details = details;
422422
mc->cstat = cstat;
423423
mc->backchannel = backchannel;
424424
mc->mon_pgrp = getpgrp();
425425

426+
debug_return;
427+
}
428+
429+
/*
430+
* Fill in the monitor closure and setup initial events.
431+
* Allocates read events for the signal pipe, error pipe and backchannel.
432+
*/
433+
static void
434+
init_exec_events_monitor(struct monitor_closure *mc, int errfd)
435+
{
436+
debug_decl(init_exec_events_monitor, SUDO_DEBUG_EXEC);
437+
426438
/* Setup event base and events. */
427439
mc->evbase = sudo_ev_base_alloc();
428440
if (mc->evbase == NULL)
@@ -437,7 +449,7 @@ fill_exec_closure_monitor(struct monitor_closure *mc,
437449
sudo_fatal("%s", U_("unable to add event to queue"));
438450

439451
/* Event for forwarded signals via backchannel. */
440-
mc->backchannel_event = sudo_ev_alloc(backchannel,
452+
mc->backchannel_event = sudo_ev_alloc(mc->backchannel,
441453
SUDO_EV_READ|SUDO_EV_PERSIST, mon_backchannel_cb, mc);
442454
if (mc->backchannel_event == NULL)
443455
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
@@ -542,7 +554,7 @@ int
542554
exec_monitor(struct command_details *details, sigset_t *oset,
543555
bool foreground, int backchannel, int intercept_fd)
544556
{
545-
struct monitor_closure mc = { 0 };
557+
struct monitor_closure mc;
546558
struct command_status cstat;
547559
struct sigaction sa;
548560
int errsock[2];
@@ -578,6 +590,9 @@ exec_monitor(struct command_details *details, sigset_t *oset,
578590
goto bad;
579591
}
580592

593+
/* Fill in exec closure after creating a new session. */
594+
init_exec_closure_monitor(&mc, details, &cstat, backchannel);
595+
581596
/*
582597
* The child waits on the other end of a socketpair for the
583598
* parent to set the controlling terminal. It also writes
@@ -648,7 +663,7 @@ exec_monitor(struct command_details *details, sigset_t *oset,
648663
* Create new event base and register read events for the
649664
* signal pipe, error pipe, and backchannel.
650665
*/
651-
fill_exec_closure_monitor(&mc, details, &cstat, errsock[0], backchannel);
666+
init_exec_events_monitor(&mc, errsock[0]);
652667

653668
/* Restore signal mask now that signal handlers are setup. */
654669
sigprocmask(SIG_SETMASK, oset, NULL);

src/exec_nopty.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,26 +201,36 @@ signal_cb_nopty(int signo, int what, void *v)
201201
debug_return;
202202
}
203203

204-
205204
/*
206-
* Fill in the exec closure and setup initial exec events.
207-
* Allocates events for the signal pipe and error pipe.
205+
* Fill in the non-event part of the exec closure.
208206
*/
209207
static void
210-
fill_exec_closure(struct exec_closure *ec, struct command_status *cstat,
211-
struct command_details *details, const struct user_details *user_details,
212-
struct sudo_event_base *evbase, int errfd)
208+
init_exec_closure(struct exec_closure *ec, struct command_status *cstat,
209+
struct command_details *details, const struct user_details *user_details)
213210
{
214-
debug_decl(fill_exec_closure, SUDO_DEBUG_EXEC);
211+
debug_decl(init_exec_closure, SUDO_DEBUG_EXEC);
215212

216213
/* Fill in the non-event part of the closure. */
214+
memset(ec, 0, sizeof(*ec));
217215
ec->sudo_pid = getpid();
218216
ec->ppgrp = getpgrp();
219217
ec->cstat = cstat;
220218
ec->details = details;
221219
ec->rows = user_details->ts_rows;
222220
ec->cols = user_details->ts_cols;
223221

222+
debug_return;
223+
}
224+
225+
/*
226+
* Allocate and set events for the signal pipe and error pipe.
227+
*/
228+
static void
229+
init_exec_events(struct exec_closure *ec, struct sudo_event_base *evbase,
230+
int errfd)
231+
{
232+
debug_decl(init_exec_events, SUDO_DEBUG_EXEC);
233+
224234
/* Setup event base and events. */
225235
ec->evbase = evbase;
226236

@@ -543,7 +553,7 @@ exec_nopty(struct command_details *details,
543553
{
544554
int io_pipe[3][2] = { { -1, -1 }, { -1, -1 }, { -1, -1 } };
545555
int errpipe[2], intercept_sv[2] = { -1, -1 };
546-
struct exec_closure ec = { 0 };
556+
struct exec_closure ec;
547557
sigset_t set, oset;
548558
debug_decl(exec_nopty, SUDO_DEBUG_EXEC);
549559

@@ -554,6 +564,9 @@ exec_nopty(struct command_details *details,
554564
if (policy_init_session(details) != true)
555565
sudo_fatalx("%s", U_("policy plugin failed session initialization"));
556566

567+
/* Fill in exec closure. */
568+
init_exec_closure(&ec, cstat, details, user_details);
569+
557570
/*
558571
* We use a pipe to get errno if execve(2) fails in the child.
559572
*/
@@ -659,11 +672,8 @@ exec_nopty(struct command_details *details,
659672
if (ISSET(details->flags, CD_SET_TIMEOUT))
660673
alarm(details->timeout);
661674

662-
/*
663-
* Fill in exec closure, allocate event base, signal events and
664-
* the error pipe event.
665-
*/
666-
fill_exec_closure(&ec, cstat, details, user_details, evbase, errpipe[0]);
675+
/* Allocate and set signal events and the error pipe event. */
676+
init_exec_events(&ec, evbase, errpipe[0]);
667677

668678
if (ISSET(details->flags, CD_INTERCEPT|CD_LOG_SUBCMDS)) {
669679
int rc = 1;

src/exec_pty.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ init_exec_closure(struct exec_closure *ec, struct command_status *cstat,
971971
debug_decl(init_exec_closure, SUDO_DEBUG_EXEC);
972972

973973
/* Fill in the non-event part of the closure. */
974+
memset(ec, 0, sizeof(*ec));
974975
ec->sudo_pid = sudo_pid;
975976
ec->ppgrp = ppgrp;
976977
ec->cmnd_pid = -1;

0 commit comments

Comments
 (0)