Skip to content

Commit 3513a92

Browse files
committed
pidfile: Adapt to new pidfile
While here, remove the hard coded limit on pidfile size.
1 parent 9d25437 commit 3513a92

8 files changed

Lines changed: 42 additions & 14 deletions

File tree

‎configure‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,12 +1039,12 @@ elif [ "$PRIVSEP" = yes ]; then
10391039
fi
10401040

10411041
if [ -z "$PIDFILE_LOCK" ]; then
1042-
printf "Testing for pidfile_lock ... "
1042+
printf "Testing for pidfile_unlock ... "
10431043
cat <<EOF >_pidfile.c
10441044
#include <stdlib.h>
10451045
#include <util.h>
10461046
int main(void) {
1047-
return (int)pidfile_lock(NULL);
1047+
return (int)pidfile_unlock();
10481048
}
10491049
EOF
10501050
# We only want to link to libutil if it exists in /lib
@@ -1065,8 +1065,9 @@ EOF
10651065
rm -rf _pidfile.* _pidfile
10661066
fi
10671067
if [ "$PIDFILE_LOCK" = no ]; then
1068-
echo "COMPAT_SRCS+= compat/pidfile.c" >>$CONFIG_MK
1068+
echo "CPPFLAGS+= -DPIDFILE_LOCAL" >>$CONFIG_MK
10691069
echo "#include \"compat/pidfile.h\"" >>$CONFIG_H
1070+
echo "COMPAT_SRCS+= compat/pidfile.c" >>$CONFIG_MK
10701071
else
10711072
echo "#define HAVE_UTIL_H" >>$CONFIG_H
10721073
if [ -n "$LIBUTIL" ]; then

‎src/bpf-bsd.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <stddef.h>
3737
#include <stdlib.h>
3838
#include <string.h>
39+
#include <unistd.h>
3940

4041
#include "bpf.h"
4142
#ifdef __sun

‎src/common.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <stdio.h>
3636
#include <stdlib.h>
3737
#include <string.h>
38+
#include <unistd.h>
3839

3940
#include "common.h"
4041
#include "dhcpcd.h"

‎src/dhcpcd.c‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,11 +2289,15 @@ main(int argc, char **argv, char **envp)
22892289
default:
22902290
per = "";
22912291
}
2292-
snprintf(ctx.pidfile, sizeof(ctx.pidfile), PIDFILE,
2293-
ifname, per, ".");
2292+
if (asprintf(&ctx.pidfile, PIDFILE, ifname, per, ".") == -1) {
2293+
logerr("%s: asprintf", __func__);
2294+
goto exit_failure;
2295+
}
22942296
} else {
2295-
snprintf(ctx.pidfile, sizeof(ctx.pidfile), PIDFILE, "",
2296-
"", "");
2297+
if (asprintf(&ctx.pidfile, PIDFILE, "", "", "") == -1) {
2298+
logerr("%s: asprintf", __func__);
2299+
goto exit_failure;
2300+
}
22972301
ctx.options |= DHCPCD_MANAGER;
22982302

22992303
/*
@@ -2582,6 +2586,18 @@ main(int argc, char **argv, char **envp)
25822586
#endif
25832587
goto exit_failure;
25842588
}
2589+
#ifdef PRIVSEP_RIGHTS
2590+
{
2591+
cap_rights_t rights;
2592+
2593+
cap_rights_init(&rights, CAP_READ, CAP_SEEK, CAP_FTRUNCATE);
2594+
if (cap_rights_limit(pidfile_fd(), &rights) == -1 &&
2595+
errno != ENOSYS) {
2596+
logerr("%s: cap_rights_limit", __func__);
2597+
goto exit_failure;
2598+
}
2599+
}
2600+
#endif
25852601
#endif
25862602

25872603
os_init();
@@ -2817,6 +2833,7 @@ main(int argc, char **argv, char **envp)
28172833
}
28182834
#endif
28192835

2836+
free(ctx.pidfile);
28202837
eloop_free(ctx.eloop);
28212838
logclose();
28222839
free(ctx.logfile);
@@ -2826,9 +2843,5 @@ main(int argc, char **argv, char **envp)
28262843
setproctitle_fini();
28272844
#endif
28282845

2829-
#ifdef USE_SIGNALS
2830-
if (ctx.options & (DHCPCD_FORKED | DHCPCD_PRIVSEP))
2831-
_exit(i); /* so atexit won't remove our pidfile */
2832-
#endif
28332846
return i;
28342847
}

‎src/dhcpcd.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ TAILQ_HEAD(if_head, interface);
116116
struct passwd;
117117

118118
struct dhcpcd_ctx {
119-
char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1];
119+
char *pidfile;
120120
char vendor[256];
121121
int fork_fd; /* FD for the fork init signal pipe */
122122
const char *cffile;

‎src/privsep-control.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <errno.h>
3030
#include <stdlib.h>
3131
#include <string.h>
32+
#include <unistd.h>
3233

3334
#include "control.h"
3435
#include "dhcpcd.h"

‎src/privsep-linux.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ static struct sock_filter ps_seccomp_filter[] = {
337337
#ifdef __NR_fstat64
338338
SECCOMP_ALLOW(__NR_fstat64),
339339
#endif
340+
#ifdef __NR_ftruncate
341+
SECCOMP_ALLOW(__NR_ftruncate),
342+
#endif
340343
#ifdef __NR_gettimeofday
341344
SECCOMP_ALLOW(__NR_gettimeofday),
342345
#endif
@@ -364,6 +367,9 @@ static struct sock_filter ps_seccomp_filter[] = {
364367
/* SECCOMP BPF is newer than nl80211 so we don't need SIOCGIWESSID
365368
* which lives in the impossible to include linux/wireless.h header */
366369
#endif
370+
#ifdef __NR_lseek
371+
SECCOMP_ALLOW(__NR_lseek),
372+
#endif
367373
#ifdef __NR_madvise /* needed for musl */
368374
SECCOMP_ALLOW(__NR_madvise),
369375
#endif

‎src/privsep.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,11 @@ ps_startprocess(struct ps_process *psp,
392392
return pid;
393393
}
394394

395-
/* If we are not the root process, close un-needed stuff. */
395+
/* Close things we no longer need */
396+
pidfile_unlock();
396397
eloop_closefdwaiter(ctx->eloop);
398+
399+
/* Close more if we are not root */
397400
if (ctx->ps_root != psp) {
398401
ps_root_close(ctx);
399402
#ifdef PLUGIN_DEV
@@ -420,7 +423,6 @@ ps_startprocess(struct ps_process *psp,
420423
goto errexit;
421424
}
422425

423-
pidfile_clean();
424426
ps_freeprocesses(ctx, psp);
425427

426428
if (ctx->ps_root != psp) {
@@ -622,6 +624,9 @@ ps_managersandbox(struct dhcpcd_ctx *ctx, const char *_pledge)
622624
return -1;
623625
}
624626

627+
/* We can no longer unlink the pidfile. */
628+
pidfile_unremoveable();
629+
625630
#ifdef PRIVSEP_RIGHTS
626631
if ((ctx->pf_inet_fd != -1 &&
627632
ps_rights_limit_ioctl(ctx->pf_inet_fd) == -1) ||

0 commit comments

Comments
 (0)