|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# An --append or --inplace transfer that is aborted while the receiver is |
| 3 | +# writing out its buffer must leave the destination a clean prefix of the |
| 4 | +# source. |
| 5 | +# |
| 6 | +# When a transfer aborts, the generator sends the receiver SIGUSR1, and the |
| 7 | +# handler runs exit_cleanup(). For these modes that cleanup used to flush the |
| 8 | +# receiver's write buffer. If the signal was handled after a write() of the |
| 9 | +# buffer had returned, but before flush_write_file() had counted those bytes as |
| 10 | +# written, the cleanup wrote them a second time. With --append the duplicate |
| 11 | +# then stayed in the file for good, since later runs only add data past the |
| 12 | +# end. |
| 13 | +# |
| 14 | +# A signal that arrives during a slow write (FUSE, NFS) is handled exactly |
| 15 | +# there, as the write returns. To reach that point deterministically, this |
| 16 | +# test runs rsync under a scratch-built LD_PRELOAD shim: right after the |
| 17 | +# receiver's first large write() to the destination file, the shim keeps every |
| 18 | +# signal pending and reports the receiver's pid, and the test sends SIGUSR1 |
| 19 | +# before letting it continue. |
| 20 | + |
| 21 | +import os |
| 22 | +import shlex |
| 23 | +import shutil |
| 24 | +import signal |
| 25 | +import subprocess |
| 26 | +import sys |
| 27 | +import time |
| 28 | + |
| 29 | +from rsyncfns import ( |
| 30 | + RSYNC, SCRATCHDIR, assert_same, make_data_file, makepath, rmtree, |
| 31 | + rsync_argv, run_rsync, split_rsync_cmd, test_fail, test_skipped, |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +SHIM_C = r''' |
| 36 | +#define _GNU_SOURCE 1 |
| 37 | +#include <dlfcn.h> |
| 38 | +#include <errno.h> |
| 39 | +#include <fcntl.h> |
| 40 | +#include <limits.h> |
| 41 | +#include <signal.h> |
| 42 | +#include <stdio.h> |
| 43 | +#include <stdlib.h> |
| 44 | +#include <string.h> |
| 45 | +#include <sys/stat.h> |
| 46 | +#include <time.h> |
| 47 | +#include <unistd.h> |
| 48 | +
|
| 49 | +static ssize_t (*real_write)(int, const void *, size_t); |
| 50 | +static char target_path[PATH_MAX]; |
| 51 | +static int held; |
| 52 | +
|
| 53 | +static void note(const char *env, const char *text) |
| 54 | +{ |
| 55 | + const char *path = getenv(env); |
| 56 | + int fd; |
| 57 | +
|
| 58 | + if (!path || !*path) |
| 59 | + return; |
| 60 | + fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 61 | + if (fd >= 0) { |
| 62 | + (void)real_write(fd, text, strlen(text)); |
| 63 | + (void)close(fd); |
| 64 | + } |
| 65 | +} |
| 66 | +
|
| 67 | +__attribute__((constructor)) |
| 68 | +static void init(void) |
| 69 | +{ |
| 70 | + const char *target = getenv("RSYNC_MIDWRITE_TARGET"); |
| 71 | +
|
| 72 | + real_write = (ssize_t (*)(int, const void *, size_t)) |
| 73 | + dlsym(RTLD_NEXT, "write"); |
| 74 | + if (target && *target && !realpath(target, target_path)) { |
| 75 | + strncpy(target_path, target, sizeof target_path - 1); |
| 76 | + target_path[sizeof target_path - 1] = '\0'; |
| 77 | + } |
| 78 | + note("RSYNC_MIDWRITE_LOADED", "loaded\n"); |
| 79 | +} |
| 80 | +
|
| 81 | +static int fd_is_target(int fd) |
| 82 | +{ |
| 83 | + char procpath[64]; |
| 84 | + char actual[PATH_MAX]; |
| 85 | + ssize_t n; |
| 86 | +
|
| 87 | + if (!target_path[0]) |
| 88 | + return 0; |
| 89 | + snprintf(procpath, sizeof procpath, "/proc/self/fd/%d", fd); |
| 90 | + n = readlink(procpath, actual, sizeof actual - 1); |
| 91 | + if (n < 0) |
| 92 | + return 0; |
| 93 | + actual[n] = '\0'; |
| 94 | + return strcmp(actual, target_path) == 0; |
| 95 | +} |
| 96 | +
|
| 97 | +/* Report our pid, then wait (at most a minute) for the test to release us. */ |
| 98 | +static void hold(void) |
| 99 | +{ |
| 100 | + const char *release = getenv("RSYNC_MIDWRITE_RELEASE"); |
| 101 | + struct timespec tick = { 0, 10 * 1000 * 1000 }; |
| 102 | + char pid[32]; |
| 103 | + int i; |
| 104 | +
|
| 105 | + snprintf(pid, sizeof pid, "%d\n", (int)getpid()); |
| 106 | + note("RSYNC_MIDWRITE_HELD", pid); |
| 107 | + for (i = 0; release && i < 6000 && access(release, F_OK) != 0; i++) |
| 108 | + nanosleep(&tick, NULL); |
| 109 | +} |
| 110 | +
|
| 111 | +ssize_t write(int fd, const void *buf, size_t count) |
| 112 | +{ |
| 113 | + sigset_t all, old; |
| 114 | + ssize_t ret; |
| 115 | + int saved_errno; |
| 116 | +
|
| 117 | + if (!real_write) { |
| 118 | + errno = EIO; |
| 119 | + return -1; |
| 120 | + } |
| 121 | + if (held || count < 65536 || !fd_is_target(fd)) |
| 122 | + return real_write(fd, buf, count); |
| 123 | +
|
| 124 | + held = 1; |
| 125 | + sigfillset(&all); |
| 126 | + sigprocmask(SIG_BLOCK, &all, &old); |
| 127 | + ret = real_write(fd, buf, count); |
| 128 | + saved_errno = errno; |
| 129 | + hold(); |
| 130 | + sigprocmask(SIG_SETMASK, &old, NULL); |
| 131 | + errno = saved_errno; |
| 132 | + return ret; |
| 133 | +} |
| 134 | +''' |
| 135 | + |
| 136 | + |
| 137 | +def compiler(): |
| 138 | + cc = os.environ.get('CC') |
| 139 | + if cc: |
| 140 | + return shlex.split(cc) |
| 141 | + for name in ('cc', 'gcc', 'clang'): |
| 142 | + path = shutil.which(name) |
| 143 | + if path: |
| 144 | + return [path] |
| 145 | + return None |
| 146 | + |
| 147 | + |
| 148 | +def build_shim(): |
| 149 | + if not sys.platform.startswith('linux'): |
| 150 | + test_skipped('the mid-write hook is an LD_PRELOAD shim that matches ' |
| 151 | + 'the destination fd through /proc/self/fd') |
| 152 | + cc = compiler() |
| 153 | + if not cc: |
| 154 | + test_skipped('no C compiler available to build the mid-write shim') |
| 155 | + |
| 156 | + cfile = SCRATCHDIR / 'midwrite.c' |
| 157 | + sofile = SCRATCHDIR / 'midwrite.so' |
| 158 | + cfile.write_text(SHIM_C) |
| 159 | + proc = subprocess.run( |
| 160 | + cc + ['-shared', '-fPIC', '-O2', '-Wall', '-Wextra', |
| 161 | + '-o', str(sofile), str(cfile), '-ldl'], |
| 162 | + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 163 | + if proc.returncode != 0 or not sofile.is_file(): |
| 164 | + test_skipped('could not build the mid-write shim:\n' |
| 165 | + + (proc.stdout or '').strip()) |
| 166 | + return sofile |
| 167 | + |
| 168 | + |
| 169 | +def gone(pid): |
| 170 | + """True once `pid` has exited (a zombie counts: it can no longer write).""" |
| 171 | + try: |
| 172 | + with open(f'/proc/{pid}/stat') as f: |
| 173 | + return f.read().rsplit(')', 1)[1].split()[0] == 'Z' |
| 174 | + except FileNotFoundError: |
| 175 | + return True |
| 176 | + |
| 177 | + |
| 178 | +def first_difference(a, b): |
| 179 | + for i in range(0, min(len(a), len(b)), 4096): |
| 180 | + if a[i:i + 4096] != b[i:i + 4096]: |
| 181 | + return next(j for j in range(i, i + 4096) if a[j] != b[j]) |
| 182 | + return min(len(a), len(b)) |
| 183 | + |
| 184 | + |
| 185 | +def abort_mid_write(mode): |
| 186 | + """Abort an rsync `mode` transfer while the receiver is inside its first |
| 187 | + write() to the destination, then check what that left behind.""" |
| 188 | + base = SCRATCHDIR / 'append-abort' |
| 189 | + src = base / 'src' |
| 190 | + dest = base / 'dest' |
| 191 | + rmtree(base) |
| 192 | + makepath(src, dest) |
| 193 | + |
| 194 | + # A growing file whose first MiB was synced by an earlier run. |
| 195 | + make_data_file(src / 'grow.bin', 4 * 1024 * 1024) |
| 196 | + data = (src / 'grow.bin').read_bytes() |
| 197 | + prefix = 1024 * 1024 |
| 198 | + (dest / 'grow.bin').write_bytes(data[:prefix]) |
| 199 | + |
| 200 | + loaded = SCRATCHDIR / 'midwrite.loaded' |
| 201 | + held = SCRATCHDIR / 'midwrite.held' |
| 202 | + release = SCRATCHDIR / 'midwrite.release' |
| 203 | + for p in (loaded, held, release): |
| 204 | + p.unlink(missing_ok=True) |
| 205 | + env = os.environ.copy() |
| 206 | + env['LD_PRELOAD'] = str(shim) |
| 207 | + env['RSYNC_MIDWRITE_TARGET'] = str((dest / 'grow.bin').resolve()) |
| 208 | + env['RSYNC_MIDWRITE_LOADED'] = str(loaded) |
| 209 | + env['RSYNC_MIDWRITE_HELD'] = str(held) |
| 210 | + env['RSYNC_MIDWRITE_RELEASE'] = str(release) |
| 211 | + |
| 212 | + argv = rsync_argv('-a', mode, f'{src}/', f'{dest}/') |
| 213 | + print('Running:', ' '.join(argv)) |
| 214 | + proc = subprocess.Popen(argv, env=env) |
| 215 | + try: |
| 216 | + deadline = time.monotonic() + 60 |
| 217 | + while not held.is_file() or not held.read_text().endswith('\n'): |
| 218 | + if proc.poll() is not None or time.monotonic() > deadline: |
| 219 | + break |
| 220 | + time.sleep(0.01) |
| 221 | + if not loaded.exists(): |
| 222 | + test_skipped('mid-write shim was not loaded; rsync may be static ' |
| 223 | + 'or the platform ignored LD_PRELOAD') |
| 224 | + if not held.is_file(): |
| 225 | + test_fail(f'{mode}: the receiver never wrote to the destination ' |
| 226 | + 'file, so the shim could not hold it') |
| 227 | + receiver = int(held.read_text()) |
| 228 | + # What the generator's exit cleanup does when a transfer aborts. |
| 229 | + os.kill(receiver, signal.SIGUSR1) |
| 230 | + finally: |
| 231 | + release.touch() |
| 232 | + try: |
| 233 | + proc.wait(timeout=60) |
| 234 | + except subprocess.TimeoutExpired: |
| 235 | + proc.kill() |
| 236 | + test_fail(f'{mode}: rsync did not exit after its receiver was aborted') |
| 237 | + deadline = time.monotonic() + 30 |
| 238 | + while not gone(receiver): |
| 239 | + if time.monotonic() > deadline: |
| 240 | + test_fail(f'{mode}: the receiver did not exit after SIGUSR1') |
| 241 | + time.sleep(0.01) |
| 242 | + if proc.returncode == 0: |
| 243 | + test_fail(f'{mode}: rsync exited 0 although its receiver was aborted') |
| 244 | + |
| 245 | + got = (dest / 'grow.bin').read_bytes() |
| 246 | + if len(got) > len(data) or got != data[:len(got)]: |
| 247 | + test_fail(f'{mode}: after the abort the destination ({len(got)} ' |
| 248 | + f'bytes) is not a prefix of the source; it differs at byte ' |
| 249 | + f'{first_difference(got, data)}, so the receiver wrote data ' |
| 250 | + 'it had already written') |
| 251 | + |
| 252 | + run_rsync('-a', mode, f'{src}/', f'{dest}/') |
| 253 | + assert_same(src / 'grow.bin', dest / 'grow.bin', |
| 254 | + label=f'{mode} rerun after the abort') |
| 255 | + |
| 256 | + |
| 257 | +if os.path.basename(split_rsync_cmd(RSYNC)[0]) == 'valgrind': |
| 258 | + test_skipped('the LD_PRELOAD write hook is not meaningful under valgrind') |
| 259 | + |
| 260 | +shim = build_shim() |
| 261 | +for mode in ('--append', '--inplace'): |
| 262 | + abort_mid_write(mode) |
| 263 | + |
| 264 | +print('append-abort-mid-write: an abort in the middle of a write left the ' |
| 265 | + 'destination a clean prefix for --append and --inplace') |
0 commit comments