Created
March 6, 2026 22:38
-
-
Save jhoblitt/4a0b62ec16b7cb33fc49c3eff4fbda5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define _GNU_SOURCE | |
| #include <errno.h> | |
| #include <fcntl.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/mman.h> | |
| #include <sys/stat.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| int main(void) { | |
| const char *src = "config"; | |
| const char *dst = "config.lock"; | |
| const char *src_content = | |
| "[remote \"origin\"]\n" | |
| "\turl = https://example.invalid/repo.git\n"; | |
| int fd; | |
| ssize_t n; | |
| struct stat st; | |
| void *addr; | |
| /* Create a small source file like .git/config */ | |
| fd = open(src, O_WRONLY | O_CREAT | O_TRUNC, 0644); | |
| if (fd < 0) { | |
| perror("open src for write"); | |
| return 1; | |
| } | |
| n = write(fd, src_content, strlen(src_content)); | |
| if (n < 0 || (size_t)n != strlen(src_content)) { | |
| perror("write src"); | |
| close(fd); | |
| return 1; | |
| } | |
| if (close(fd) != 0) { | |
| perror("close src after create"); | |
| return 1; | |
| } | |
| /* Open lock file in a Git-like way */ | |
| int lockfd = open(dst, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666); | |
| if (lockfd < 0) { | |
| perror("open lock"); | |
| return 1; | |
| } | |
| /* Redundant after O_CLOEXEC, but matches the Git trace pattern */ | |
| if (fcntl(lockfd, F_SETFD, FD_CLOEXEC) < 0) { | |
| perror("fcntl(F_SETFD)"); | |
| close(lockfd); | |
| return 1; | |
| } | |
| /* Open source file read-only and mmap it */ | |
| int srcfd = open(src, O_RDONLY); | |
| if (srcfd < 0) { | |
| perror("open src for read"); | |
| close(lockfd); | |
| return 1; | |
| } | |
| if (fstat(srcfd, &st) != 0) { | |
| perror("fstat"); | |
| close(srcfd); | |
| close(lockfd); | |
| return 1; | |
| } | |
| if (st.st_size == 0) { | |
| fprintf(stderr, "source file is empty\n"); | |
| close(srcfd); | |
| close(lockfd); | |
| return 1; | |
| } | |
| addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, srcfd, 0); | |
| if (addr == MAP_FAILED) { | |
| perror("mmap"); | |
| close(srcfd); | |
| close(lockfd); | |
| return 1; | |
| } | |
| printf("mmap returned %p\n", addr); | |
| /* Close the source fd after mmap: mapping remains valid */ | |
| if (close(srcfd) != 0) { | |
| perror("close srcfd"); | |
| munmap(addr, st.st_size); | |
| close(lockfd); | |
| return 1; | |
| } | |
| if (chmod(dst, 0644) != 0) { | |
| perror("chmod lock"); | |
| munmap(addr, st.st_size); | |
| close(lockfd); | |
| return 1; | |
| } | |
| n = write(lockfd, "[core]\n", 7); | |
| if (n < 0) { | |
| perror("write prefix 1"); | |
| munmap(addr, st.st_size); | |
| close(lockfd); | |
| return 1; | |
| } | |
| printf("wrote prefix 1: %zd bytes\n", n); | |
| n = write(lockfd, "\tbare = false\n", 14); | |
| if (n < 0) { | |
| perror("write prefix 2"); | |
| munmap(addr, st.st_size); | |
| close(lockfd); | |
| return 1; | |
| } | |
| printf("wrote prefix 2: %zd bytes\n", n); | |
| /* Write directly from the mmap'd address */ | |
| n = write(lockfd, addr, st.st_size); | |
| if (n < 0) { | |
| perror("write from mmap"); | |
| munmap(addr, st.st_size); | |
| close(lockfd); | |
| return 1; | |
| } | |
| printf("wrote mapped content: %zd bytes\n", n); | |
| if (close(lockfd) != 0) { | |
| perror("close lockfd"); | |
| munmap(addr, st.st_size); | |
| return 1; | |
| } | |
| if (munmap(addr, st.st_size) != 0) { | |
| perror("munmap"); | |
| return 1; | |
| } | |
| /* Show result */ | |
| fd = open(dst, O_RDONLY); | |
| if (fd < 0) { | |
| perror("open dst for readback"); | |
| return 1; | |
| } | |
| printf("--- %s contents ---\n", dst); | |
| char buf[4096]; | |
| while ((n = read(fd, buf, sizeof(buf))) > 0) { | |
| ssize_t off = 0; | |
| while (off < n) { | |
| ssize_t wn = write(STDOUT_FILENO, buf + off, (size_t)(n - off)); | |
| if (wn < 0) { | |
| perror("write stdout"); | |
| close(fd); | |
| return 1; | |
| } | |
| off += wn; | |
| } | |
| } | |
| if (n < 0) { | |
| perror("read dst"); | |
| close(fd); | |
| return 1; | |
| } | |
| if (close(fd) != 0) { | |
| perror("close dst readback"); | |
| return 1; | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rook-ceph-demo-nfs-auxtel-jhoblitt-test1:/data/auxtel/gittest$ strace ./a.out | |
| execve("./a.out", ["./a.out"], 0x7ffc1e9c2a50 /* 16 vars */) = 0 | |
| arch_prctl(ARCH_SET_FS, 0x7f54b8fc47b8) = 0 | |
| set_tid_address(0x7f54b8fc48f0) = 563 | |
| brk(NULL) = 0xbc6000 | |
| brk(0xbc7000) = 0xbc7000 | |
| readlink("/proc/self/exe", "/data/auxtel/gittest/a.out", 4096) = 26 | |
| execve("/lib/ld-musl-x86_64.so.1", ["ld-linux-x86-64.so.2", "--argv0", "./a.out", "--preload", "/lib/libgcompat.so.0 ", "--", "/data/auxtel/gittest/a.out"], 0x7ffe3e83dc78 /* 16 vars */) = 0 | |
| arch_prctl(ARCH_SET_FS, 0x7fd1b4ddfb28) = 0 | |
| set_tid_address(0x7fd1b4ddff90) = 563 | |
| open("/data/auxtel/gittest/a.out", O_RDONLY|O_LARGEFILE) = 3 | |
| read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\2\0>\0\1\0\0\0@\4@\0\0\0\0\0"..., 960) = 960 | |
| mmap(0x400000, 16384, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x400000 | |
| mmap(0x401000, 4096, PROT_READ, MAP_PRIVATE|MAP_FIXED, 3, 0x1000) = 0x401000 | |
| mmap(0x402000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0x1000) = 0x402000 | |
| close(3) = 0 | |
| brk(NULL) = 0x555556877000 | |
| brk(0x555556879000) = 0x555556879000 | |
| mmap(0x555556877000, 4096, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x555556877000 | |
| open("/lib/libgcompat.so.0", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3 | |
| fcntl(3, F_SETFD, FD_CLOEXEC) = 0 | |
| fstat(3, {st_mode=S_IFREG|0755, st_size=67184, ...}) = 0 | |
| read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\0\0\0\0\0\0\0"..., 960) = 960 | |
| mmap(NULL, 73728, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd1b4d28000 | |
| mmap(0x7fd1b4d2f000, 20480, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 0x7000) = 0x7fd1b4d2f000 | |
| mmap(0x7fd1b4d34000, 12288, PROT_READ, MAP_PRIVATE|MAP_FIXED, 3, 0xc000) = 0x7fd1b4d34000 | |
| mmap(0x7fd1b4d37000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0xf000) = 0x7fd1b4d37000 | |
| mmap(0x7fd1b4d39000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fd1b4d39000 | |
| close(3) = 0 | |
| open("/etc/ld-musl-x86_64.path", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory) | |
| open("/lib/libucontext.so.1", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory) | |
| open("/usr/local/lib/libucontext.so.1", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory) | |
| open("/usr/lib/libucontext.so.1", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3 | |
| fcntl(3, F_SETFD, FD_CLOEXEC) = 0 | |
| fstat(3, {st_mode=S_IFREG|0755, st_size=13936, ...}) = 0 | |
| read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\0\0\0\0\0\0\0"..., 960) = 960 | |
| mmap(NULL, 20480, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd1b4d23000 | |
| mmap(0x7fd1b4d24000, 4096, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 0x1000) = 0x7fd1b4d24000 | |
| mmap(0x7fd1b4d25000, 4096, PROT_READ, MAP_PRIVATE|MAP_FIXED, 3, 0x2000) = 0x7fd1b4d25000 | |
| mmap(0x7fd1b4d26000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0x2000) = 0x7fd1b4d26000 | |
| close(3) = 0 | |
| open("/lib/libobstack.so.1", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory) | |
| open("/usr/local/lib/libobstack.so.1", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory) | |
| open("/usr/lib/libobstack.so.1", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3 | |
| fcntl(3, F_SETFD, FD_CLOEXEC) = 0 | |
| fstat(3, {st_mode=S_IFREG|0755, st_size=13944, ...}) = 0 | |
| read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\0\0\0\0\0\0\0"..., 960) = 960 | |
| mmap(NULL, 20480, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd1b4d1e000 | |
| mmap(0x7fd1b4d1f000, 4096, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 0x1000) = 0x7fd1b4d1f000 | |
| mmap(0x7fd1b4d20000, 4096, PROT_READ, MAP_PRIVATE|MAP_FIXED, 3, 0x2000) = 0x7fd1b4d20000 | |
| mmap(0x7fd1b4d21000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0x2000) = 0x7fd1b4d21000 | |
| close(3) = 0 | |
| mprotect(0x7fd1b4d37000, 4096, PROT_READ) = 0 | |
| mprotect(0x7fd1b4ddc000, 4096, PROT_READ) = 0 | |
| mprotect(0x7fd1b4d26000, 4096, PROT_READ) = 0 | |
| mprotect(0x7fd1b4d21000, 4096, PROT_READ) = 0 | |
| mprotect(0x402000, 4096, PROT_READ) = 0 | |
| open("config", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0644) = 3 | |
| write(3, "[remote \"origin\"]\n\turl = https:/"..., 58) = 58 | |
| close(3) = 0 | |
| open("config.lock", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE|O_CLOEXEC, 0666) = 3 | |
| fcntl(3, F_SETFD, FD_CLOEXEC) = 0 | |
| fcntl(3, F_SETFD, FD_CLOEXEC) = 0 | |
| open("config", O_RDONLY|O_LARGEFILE) = 4 | |
| fstat(4, {st_mode=S_IFREG|0644, st_size=58, ...}) = 0 | |
| mmap(NULL, 58, PROT_READ, MAP_PRIVATE, 4, 0) = 0x7fd1b4d1d000 | |
| ioctl(1, TIOCGWINSZ, {ws_row=48, ws_col=190, ws_xpixel=0, ws_ypixel=0}) = 0 | |
| writev(1, [{iov_base="mmap returned 0x7fd1b4d1d000", iov_len=28}, {iov_base="\n", iov_len=1}], 2mmap returned 0x7fd1b4d1d000 | |
| ) = 29 | |
| close(4) = 0 | |
| chmod("config.lock", 0644) = 0 | |
| write(3, "[core]\n", 7) = 7 | |
| writev(1, [{iov_base="wrote prefix 1: 7", iov_len=17}, {iov_base=" bytes\n", iov_len=7}], 2wrote prefix 1: 7 bytes | |
| ) = 24 | |
| write(3, "\tbare = false\n", 14) = 14 | |
| writev(1, [{iov_base="wrote prefix 2: 14", iov_len=18}, {iov_base=" bytes\n", iov_len=7}], 2wrote prefix 2: 14 bytes | |
| ) = 25 | |
| write(3, "[remote \"origin\"]\n\turl = https:/"..., 58) = 58 | |
| writev(1, [{iov_base="wrote mapped content: 58", iov_len=24}, {iov_base=" bytes\n", iov_len=7}], 2wrote mapped content: 58 bytes | |
| ) = 31 | |
| close(3) = 0 | |
| munmap(0x7fd1b4d1d000, 58) = 0 | |
| open("config.lock", O_RDONLY|O_LARGEFILE) = 3 | |
| writev(1, [{iov_base="--- config.lock", iov_len=15}, {iov_base=" contents ---\n", iov_len=14}], 2--- config.lock contents --- | |
| ) = 29 | |
| read(3, "[core]\n\tbare = false\n[remote \"or"..., 4096) = 79 | |
| write(1, "[core]\n\tbare = false\n[remote \"or"..., 79[core] | |
| bare = false | |
| [remote "origin"] | |
| url = https://example.invalid/repo.git | |
| ) = 79 | |
| read(3, "", 4096) = 0 | |
| close(3) = 0 | |
| exit_group(0) = ? | |
| +++ exited with 0 +++ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment