cloudflare / cloudflare/pingora

Daemonize should have a configurable working path

Open
#331 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
27.4k
Forks
1.7k
Avg merge
6h 22m
Merged PRs (30d)
3

Description

## What is the problem your feature solves, or the need it fulfills?

Currently, when `pingora` is configured to daemonize, it uses `Daemonize::new()` as the base configuration:

https://github.com/cloudflare/pingora/blob/42a847d372f1b21334b43c3be03655b12a8cc21a/pingora-core/src/server/daemon.rs#L56-L114

However, this selects the system root, e.g. `/`, as the new working path for the daemonized process:

https://github.com/knsd/daemonize/blob/c270c91354524b6d750dc3cecfbf3d21dcec508c/daemonize/src/lib.rs#L259-L275

This means that if the pidfile is specifed as a relative path, e.g. `./river.pidfile`, it will try to create the pidfile in the system root, e.g. `/river.pidfile`. This will often fail, and was confusing to me until I used `strace` to figure out what it was actually trying to do:

```
56466 statx(AT_FDCWD, "./testdir/river.pidfile", AT_STATX_SYNC_AS_STAT, STATX_ALL,
56467 clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=10000000},
56466 <... statx resumed>0xffffd704c260) = -1 ENOENT (No such file or directory)
56466 clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xffffa66810f0) = 56468
56468 set_robust_list(0xffffa6681100, 24
56466 wait4(56468,
56468 <... set_robust_list resumed>) = 0
56468 chdir("/") = 0
56468 setsid() = 56468
56468 umask(007) = 022
56468 clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD
56469 set_robust_list(0xffffa6681100, 24
56468 <... clone resumed>, child_tidptr=0xffffa66810f0) = 56469
56469 <... set_robust_list resumed>) = 0
56468 sigaltstack({ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=16384},
56469 openat(AT_FDCWD, "./testdir/river.pidfile", O_WRONLY|O_CREAT, 0666
56468 <... sigaltstack resumed>NULL) = 0
56469 <... openat resumed>) = -1 ENOENT (No such file or directory)
```

The relevant lines are:

```
# the parent process changes to the root directory
56468 chdir("/") = 0
# the child process attempts to open the pidfile at that root path
56469 openat(AT_FDCWD, "./testdir/river.pidfile", O_WRONLY|O_CREAT, 0666
```

We can see the current working directory is `/`, and the path to open is `./testdir/river.pidfile`.

This might also affect the upgrade socket path in addition to the pidfile.

## Describe the solution you'd like

We should likely do one or both of two things:

1. Allow the configuration of the working directory of the daemonized process, having a configuration that perhaps defaults to the host process' current working directory if not otherwise set
2. Canonicalize the paths prior to forking, turning them into absolute paths relative to the working directory of the host process

## Describe alternatives you've considered

We could also refuse to accept relative paths at all for both of these configuration values, but this may impact other configuration paths in the future.

## Additional Context

CC https://github.com/memorysafety/river/issues/50

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.