| 1 |
|
| 2 |
"""Daemon coverage: standalone detaching listener. |
| 3 |
|
| 4 |
Every other daemon test spawns rsyncd with `--no-detach --address=127.0.0.1 |
| 5 |
--port=N`, so daemon_main()'s detach path and the rsyncd.conf port/address |
| 6 |
parameters are never reached: |
| 7 |
|
| 8 |
clientserver.c become_daemon() -- only when --no-detach is NOT given |
| 9 |
daemon-parm.h lp_rsync_port() -- only when --port is NOT given |
| 10 |
daemon-parm.h lp_bind_address() -- only when --address is NOT given |
| 11 |
|
| 12 |
This test spawns `rsync --daemon --config=<conf>` with NONE of those flags, |
| 13 |
reads port/address from the conf file, waits for the detached child to write |
| 14 |
its pid file and start listening, does one transfer, then kills it via the |
| 15 |
pid file. require_tcp-gated (it opens a real loopback listener). |
| 16 |
""" |
| 17 |
|
| 18 |
import os |
| 19 |
import platform |
| 20 |
import shlex |
| 21 |
import signal |
| 22 |
import socket |
| 23 |
import subprocess |
| 24 |
import time |
| 25 |
|
| 26 |
from rsyncfns import ( |
| 27 |
FROMDIR, RSYNC_PEER, SCRATCHDIR, |
| 28 |
claim_ports, make_tree, makepath, require_tcp, rmtree, rsync_argv, |
| 29 |
test_fail, test_skipped, write_daemon_conf, split_rsync_cmd, |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
if platform.system().startswith('CYGWIN'): |
| 36 |
test_skipped("a detached daemon orphans on cygwin's Windows process model") |
| 37 |
|
| 38 |
PORT = 19877 |
| 39 |
require_tcp("standalone detaching daemon opens a real loopback listener; " |
| 40 |
"run with --use-tcp") |
| 41 |
claim_ports(PORT) |
| 42 |
|
| 43 |
src = FROMDIR |
| 44 |
rmtree(src) |
| 45 |
make_tree(src, depth=2) |
| 46 |
dest = SCRATCHDIR / 'dest-detach' |
| 47 |
makepath(dest) |
| 48 |
|
| 49 |
pidfile = SCRATCHDIR / 'detach.pid' |
| 50 |
logfile = SCRATCHDIR / 'detach.log' |
| 51 |
for p in (pidfile, logfile): |
| 52 |
if p.exists(): |
| 53 |
p.unlink() |
| 54 |
|
| 55 |
conf = write_daemon_conf( |
| 56 |
[('mod', {'path': str(dest), 'read only': 'no', 'use chroot': 'no'})], |
| 57 |
globals={ |
| 58 |
|
| 59 |
'port': str(PORT), |
| 60 |
'address': '127.0.0.1', |
| 61 |
'pid file': str(pidfile), |
| 62 |
'log file': str(logfile), |
| 63 |
}, |
| 64 |
name='detach.conf', |
| 65 |
) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
launcher = subprocess.run( |
| 71 |
split_rsync_cmd(RSYNC_PEER) + ['--daemon', f'--config={conf}'], |
| 72 |
capture_output=True, text=True, timeout=15, |
| 73 |
) |
| 74 |
if launcher.returncode != 0: |
| 75 |
test_fail(f"daemon launcher exited {launcher.returncode}:\n{launcher.stderr}") |
| 76 |
|
| 77 |
|
| 78 |
def kill_detached(): |
| 79 |
try: |
| 80 |
pid = int(pidfile.read_text().strip()) |
| 81 |
except (FileNotFoundError, ValueError): |
| 82 |
return |
| 83 |
try: |
| 84 |
os.kill(pid, signal.SIGTERM) |
| 85 |
for _ in range(50): |
| 86 |
try: |
| 87 |
os.kill(pid, 0) |
| 88 |
except ProcessLookupError: |
| 89 |
return |
| 90 |
time.sleep(0.05) |
| 91 |
os.kill(pid, signal.SIGKILL) |
| 92 |
except ProcessLookupError: |
| 93 |
pass |
| 94 |
|
| 95 |
|
| 96 |
import atexit |
| 97 |
atexit.register(kill_detached) |
| 98 |
|
| 99 |
|
| 100 |
deadline = time.monotonic() + 10 |
| 101 |
while time.monotonic() < deadline: |
| 102 |
if pidfile.is_file(): |
| 103 |
try: |
| 104 |
with socket.create_connection(('127.0.0.1', PORT), timeout=0.5): |
| 105 |
break |
| 106 |
except OSError: |
| 107 |
pass |
| 108 |
time.sleep(0.05) |
| 109 |
else: |
| 110 |
test_fail(f"detached rsyncd never listened on 127.0.0.1:{PORT} " |
| 111 |
f"(pidfile={'present' if pidfile.is_file() else 'absent'}; " |
| 112 |
f"log:\n{logfile.read_text() if logfile.is_file() else '(none)'})") |
| 113 |
|
| 114 |
|
| 115 |
log = logfile.read_text() |
| 116 |
if f'listening on port {PORT}' not in log: |
| 117 |
test_fail(f"expected 'listening on port {PORT}' in detach.log:\n{log}") |
| 118 |
|
| 119 |
|
| 120 |
r = subprocess.run( |
| 121 |
rsync_argv('-r', f'{src}/', f'rsync://127.0.0.1:{PORT}/mod/'), |
| 122 |
capture_output=True, text=True, |
| 123 |
) |
| 124 |
if r.returncode != 0: |
| 125 |
test_fail(f"push to detached daemon failed (rc={r.returncode}):\n{r.stderr}") |
| 126 |
if not any(dest.iterdir()): |
| 127 |
test_fail("push to detached daemon wrote nothing") |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
detached_pid = pidfile.read_text().strip() |
| 133 |
kill_detached() |
| 134 |
atexit.unregister(kill_detached) |
| 135 |
|
| 136 |
print(f"daemon-standalone-detach: become_daemon + conf port/address + " |
| 137 |
f"accept-loop transfer ok (pid {detached_pid})") |