Skip to content

Commit 69fa922

Browse files
committed
Adapt to new eloop.
1 parent 95f91f5 commit 69fa922

File tree

4 files changed

+40
-64
lines changed

4 files changed

+40
-64
lines changed

src/dhcpcd.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ dhcpcd_renew(struct dhcpcd_ctx *ctx)
14891489

14901490
#ifdef USE_SIGNALS
14911491
#define sigmsg "received %s, %s"
1492-
void
1492+
static void
14931493
dhcpcd_signal_cb(int sig, void *arg)
14941494
{
14951495
struct dhcpcd_ctx *ctx = arg;
@@ -1576,11 +1576,10 @@ dhcpcd_signal_cb(int sig, void *arg)
15761576
stop_all_interfaces(ctx, opts))
15771577
{
15781578
/* We stopped something, we will exit once that is done. */
1579-
eloop_exitallinners(exit_code);
15801579
return;
15811580
}
15821581

1583-
eloop_exitall(exit_code);
1582+
eloop_exit(ctx->eloop, exit_code);
15841583
}
15851584
#endif
15861585

@@ -2797,7 +2796,6 @@ main(int argc, char **argv, char **envp)
27972796
#ifdef PRIVSEP
27982797
if (ps_root_stop(&ctx) == -1)
27992798
i = EXIT_FAILURE;
2800-
eloop_free(ctx.ps_eloop);
28012799
#endif
28022800

28032801
#ifdef USE_SIGNALS

src/dhcpcd.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ struct dhcpcd_ctx {
196196
int ps_data_fd; /* data returned from processes */
197197
int ps_log_fd; /* chroot logging */
198198
int ps_log_root_fd; /* outside chroot log reader */
199-
struct eloop *ps_eloop; /* eloop for polling root data */
200199
struct fd_list *ps_control; /* Queue for the above */
201200
struct fd_list *ps_control_client; /* Queue for the above */
202201
#endif
@@ -259,8 +258,6 @@ int dhcpcd_afwaiting(const struct dhcpcd_ctx *);
259258
void dhcpcd_daemonised(struct dhcpcd_ctx *);
260259
void dhcpcd_daemonise(struct dhcpcd_ctx *);
261260

262-
void dhcpcd_signal_cb(int, void *);
263-
264261
void dhcpcd_linkoverflow(struct dhcpcd_ctx *);
265262
int dhcpcd_handleargs(struct dhcpcd_ctx *, struct fd_list *, int, char **);
266263
void dhcpcd_handlecarrier(struct interface *, int, unsigned int);

src/privsep-root.c

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,29 @@ struct psr_ctx {
7676
bool psr_usemdata;
7777
};
7878

79-
static void
80-
ps_root_readerrorcb(void *arg, unsigned short events)
79+
static ssize_t
80+
ps_root_readerrorcb(struct psr_ctx *psr_ctx)
8181
{
82-
struct psr_ctx *psr_ctx = arg;
8382
struct dhcpcd_ctx *ctx = psr_ctx->psr_ctx;
83+
int fd = PS_ROOT_FD(ctx);
8484
struct psr_error *psr_error = &psr_ctx->psr_error;
8585
struct iovec iov[] = {
8686
{ .iov_base = psr_error, .iov_len = sizeof(*psr_error) },
8787
{ .iov_base = NULL, .iov_len = 0 },
8888
};
8989
ssize_t len;
90-
int exit_code = EXIT_FAILURE;
91-
92-
if (events & ELE_HANGUP)
93-
goto out;
94-
95-
if (events != ELE_READ)
96-
logerrx("%s: unexpected event 0x%04x", __func__, events);
9790

9891
#define PSR_ERROR(e) \
9992
do { \
10093
psr_error->psr_result = -1; \
10194
psr_error->psr_errno = (e); \
102-
goto out; \
95+
return -1; \
10396
} while (0 /* CONSTCOND */)
10497

105-
len = recv(PS_ROOT_FD(ctx), psr_error, sizeof(*psr_error), MSG_PEEK);
98+
if (eloop_waitfd(fd) == -1)
99+
PSR_ERROR(errno);
100+
101+
len = recv(fd, psr_error, sizeof(*psr_error), MSG_PEEK);
106102
if (len == -1)
107103
PSR_ERROR(errno);
108104
else if ((size_t)len < sizeof(*psr_error))
@@ -130,29 +126,23 @@ ps_root_readerrorcb(void *arg, unsigned short events)
130126
iov[1].iov_len = psr_error->psr_datalen;
131127
}
132128

133-
len = readv(PS_ROOT_FD(ctx), iov, __arraycount(iov));
129+
len = readv(fd, iov, __arraycount(iov));
134130
if (len == -1)
135131
PSR_ERROR(errno);
136132
else if ((size_t)len != sizeof(*psr_error) + psr_error->psr_datalen)
137133
PSR_ERROR(EINVAL);
138-
exit_code = EXIT_SUCCESS;
139-
140-
out:
141-
eloop_exit(ctx->ps_eloop, exit_code);
134+
return len;
142135
}
143136

144137
ssize_t
145138
ps_root_readerror(struct dhcpcd_ctx *ctx, void *data, size_t len)
146139
{
147140
struct psr_ctx *pc = ctx->ps_root->psp_data;
148-
int err;
149141

150142
pc->psr_data = data;
151143
pc->psr_datalen = len;
152144
pc->psr_usemdata = false;
153-
err = eloop_start(ctx->ps_eloop);
154-
if (err < 0)
155-
return err;
145+
ps_root_readerrorcb(pc);
156146

157147
errno = pc->psr_error.psr_errno;
158148
return pc->psr_error.psr_result;
@@ -162,13 +152,10 @@ ssize_t
162152
ps_root_mreaderror(struct dhcpcd_ctx *ctx, void **data, size_t *len)
163153
{
164154
struct psr_ctx *pc = ctx->ps_root->psp_data;
165-
int err;
166155
void *d;
167156

168157
pc->psr_usemdata = true;
169-
err = eloop_start(ctx->ps_eloop);
170-
if (err < 0)
171-
return err;
158+
ps_root_readerrorcb(pc);
172159

173160
if (pc->psr_error.psr_datalen != 0) {
174161
if (pc->psr_error.psr_datalen > pc->psr_mdatalen) {
@@ -212,7 +199,7 @@ ps_root_writeerror(struct dhcpcd_ctx *ctx, ssize_t result,
212199
err = sendmsg(fd, &msg, MSG_EOR);
213200

214201
/* Error sending the message? Try sending the error of sending. */
215-
if (err == -1) {
202+
if (err == -1 && errno != EPIPE) {
216203
logerr("%s: result=%zd, data=%p, len=%zu",
217204
__func__, result, data, len);
218205
psr.psr_result = err;
@@ -776,7 +763,7 @@ ps_root_signalcb(int sig, void *arg)
776763
if (!(ctx->options & DHCPCD_EXITING))
777764
return;
778765
if (!(ps_waitforprocs(ctx)))
779-
eloop_exit(ctx->ps_eloop, EXIT_SUCCESS);
766+
eloop_exit(ctx->eloop, EXIT_SUCCESS);
780767
}
781768

782769
int (*handle_interface)(void *, int, const char *);
@@ -888,6 +875,7 @@ ps_root_start(struct dhcpcd_ctx *ctx)
888875

889876
if (xsocketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CXNB, 0, datafd) == -1)
890877
return -1;
878+
891879
if (ps_setbuf_fdpair(datafd) == -1)
892880
return -1;
893881
#ifdef PRIVSEP_RIGHTS
@@ -916,9 +904,6 @@ ps_root_start(struct dhcpcd_ctx *ctx)
916904
}
917905

918906
psp->psp_data = pc;
919-
if (eloop_event_add(ctx->ps_eloop, psp->psp_fd, ELE_READ,
920-
ps_root_readerrorcb, pc) == -1)
921-
return -1;
922907

923908
if (pid == 0) {
924909
ctx->ps_log_fd = logfd[0]; /* Keep open to pass to processes */
@@ -976,11 +961,9 @@ ps_root_stop(struct dhcpcd_ctx *ctx)
976961
struct ps_process *psp = ctx->ps_root;
977962
int err;
978963

979-
if (!(ctx->options & DHCPCD_PRIVSEP) ||
980-
ctx->eloop == NULL)
964+
if (!(ctx->options & DHCPCD_PRIVSEP))
981965
return 0;
982966

983-
984967
/* If we are the root process then remove the pidfile */
985968
if (ctx->options & DHCPCD_PRIVSEPROOT) {
986969
if (!(ctx->options & DHCPCD_TEST) && unlink(ctx->pidfile) == -1)
@@ -990,12 +973,25 @@ ps_root_stop(struct dhcpcd_ctx *ctx)
990973
if (ctx->ps_log_root_fd != -1) {
991974
ssize_t loglen;
992975

976+
#ifdef __linux__
977+
/* Seems to help to get the last parts,
978+
* sched_yield(2) does not. */
979+
sleep(0);
980+
#endif
993981
do {
994982
loglen = logreadfd(ctx->ps_log_root_fd);
995983
} while (loglen != 0 && loglen != -1);
984+
close(ctx->ps_log_root_fd);
985+
ctx->ps_log_root_fd = -1;
996986
}
997987
}
998988

989+
if (ctx->ps_data_fd != -1) {
990+
eloop_event_delete(ctx->eloop, ctx->ps_data_fd);
991+
close(ctx->ps_data_fd);
992+
ctx->ps_data_fd = -1;
993+
}
994+
999995
/* Only the manager process gets past this point. */
1000996
if (ctx->options & DHCPCD_FORKED)
1001997
return 0;
@@ -1015,7 +1011,8 @@ ps_root_stop(struct dhcpcd_ctx *ctx)
10151011
} /* else the root process has already exited :( */
10161012

10171013
err = ps_stopwait(ctx);
1018-
ps_freeprocess(ctx->ps_root);
1014+
if (ctx->ps_root != NULL)
1015+
ps_freeprocess(ctx->ps_root);
10191016
return err;
10201017
}
10211018

src/privsep.c

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ ps_processhangup(void *arg, unsigned short events)
342342
if (!(ctx->options & DHCPCD_EXITING))
343343
return;
344344
if (!(ps_waitforprocs(ctx)))
345-
eloop_exit(ctx->ps_eloop, EXIT_SUCCESS);
345+
eloop_exit(ctx->eloop, EXIT_SUCCESS);
346346
}
347347
#endif
348348

@@ -442,16 +442,6 @@ ps_startprocess(struct ps_process *psp,
442442
logerr("%s: eloop_forked", __func__);
443443
goto errexit;
444444
}
445-
if (ctx->ps_eloop != NULL) {
446-
if (eloop_forked(ctx->ps_eloop, ELF_KEEP_SIGNALS) == -1) {
447-
logerr("%s: eloop_forked", __func__);
448-
goto errexit;
449-
}
450-
if (!(flags & PSF_ELOOP)) {
451-
eloop_free(ctx->ps_eloop);
452-
ctx->ps_eloop = NULL;
453-
}
454-
}
455445

456446
pidfile_clean();
457447
ps_freeprocesses(ctx, psp);
@@ -552,10 +542,6 @@ ps_start(struct dhcpcd_ctx *ctx)
552542

553543
TAILQ_INIT(&ctx->ps_processes);
554544

555-
/* We need an inner eloop to block with. */
556-
if ((ctx->ps_eloop = eloop_new_with_signals(ctx->eloop)) == NULL)
557-
return -1;
558-
559545
switch (pid = ps_root_start(ctx)) {
560546
case -1:
561547
logerr("ps_root_start");
@@ -738,11 +724,11 @@ ps_stopwait(struct dhcpcd_ctx *ctx)
738724
{
739725
int error = EXIT_SUCCESS;
740726

741-
if (ctx->ps_eloop == NULL || !ps_waitforprocs(ctx))
727+
if (!ps_waitforprocs(ctx))
742728
return 0;
743729

744730
ctx->options |= DHCPCD_EXITING;
745-
if (eloop_timeout_add_sec(ctx->ps_eloop, PS_PROCESS_TIMEOUT,
731+
if (eloop_timeout_add_sec(ctx->eloop, PS_PROCESS_TIMEOUT,
746732
ps_process_timeout, ctx) == -1)
747733
logerr("%s: eloop_timeout_add_sec", __func__);
748734

@@ -752,18 +738,18 @@ ps_stopwait(struct dhcpcd_ctx *ctx)
752738
TAILQ_FOREACH(psp, &ctx->ps_processes, next) {
753739
if (psp->psp_pfd == -1)
754740
continue;
755-
if (eloop_event_add(ctx->ps_eloop, psp->psp_pfd,
741+
if (eloop_event_add(ctx->eloop, psp->psp_pfd,
756742
ELE_HANGUP, ps_processhangup, psp) == -1)
757743
logerr("%s: eloop_event_add pfd %d",
758744
__func__, psp->psp_pfd);
759745
}
760746
#endif
761747

762-
error = eloop_start(ctx->ps_eloop);
748+
error = eloop_start(ctx->eloop);
763749
if (error < 0)
764750
logerr("%s: eloop_start", __func__);
765751

766-
eloop_timeout_delete(ctx->ps_eloop, ps_process_timeout, ctx);
752+
eloop_timeout_delete(ctx->eloop, ps_process_timeout, ctx);
767753

768754
return error;
769755
}
@@ -791,8 +777,6 @@ ps_freeprocess(struct ps_process *psp)
791777
#ifdef HAVE_CAPSICUM
792778
if (psp->psp_pfd != -1) {
793779
eloop_event_delete(ctx->eloop, psp->psp_pfd);
794-
if (ctx->ps_eloop != NULL)
795-
eloop_event_delete(ctx->ps_eloop, psp->psp_pfd);
796780
close(psp->psp_pfd);
797781
}
798782
#endif
@@ -1173,7 +1157,7 @@ ps_recvpsmsg(struct dhcpcd_ctx *ctx, int fd, unsigned short events,
11731157
logdebugx("process %d stopping", getpid());
11741158
#endif
11751159
ps_free(ctx);
1176-
eloop_exitall(len != -1 ? EXIT_SUCCESS : EXIT_FAILURE);
1160+
eloop_exit(ctx->eloop, len != -1 ? EXIT_SUCCESS : EXIT_FAILURE);
11771161
return len;
11781162
}
11791163
dlen -= sizeof(psm.psm_hdr);

0 commit comments

Comments
 (0)