From 99b0423f2fc9076de1aaa07da5a1f5048999459c Mon Sep 17 00:00:00 2001 From: Josh Lehman Date: Tue, 16 Apr 2024 06:21:05 -0700 Subject: [PATCH 1/4] even better error messages for partial write --- pkg/noun/events.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/noun/events.c b/pkg/noun/events.c index 6e82a87146..a7e5074846 100644 --- a/pkg/noun/events.c +++ b/pkg/noun/events.c @@ -652,11 +652,12 @@ _ce_patch_write_page(u3_ce_patch* pat_u, (ret_zs = pwrite(pat_u->mem_i, mem_w, _ce_page, _ce_len(pgc_w))) ) { if ( 0 < ret_zs ) { - fprintf(stderr, "loom: patch partial write: %"PRIc3_zs", check disk space\r\n", ret_zs); + fprintf(stderr, "loom: patch partial write: %"PRIc3_zs"\r\n", ret_zs); } else { - fprintf(stderr, "loom: patch write: fail: %s, check disk space\r\n", strerror(errno)); + fprintf(stderr, "loom: patch write: fail: %s\r\n", strerror(errno)); } + fprintf(stderr, "info: you probably have insufficient disk space"); u3_assert(0); } } @@ -871,12 +872,13 @@ _ce_patch_apply(u3_ce_patch* pat_u) (ret_zs = pwrite(fid_i, buf_y, _ce_page, off_z)) ) { if ( 0 < ret_zs ) { - fprintf(stderr, "loom: patch apply partial write: %"PRIc3_zs", check disk space\r\n", + fprintf(stderr, "loom: patch apply partial write: %"PRIc3_zs"\r\n", ret_zs); } else { - fprintf(stderr, "loom: patch apply write: %s, check disk space\r\n", strerror(errno)); + fprintf(stderr, "loom: patch apply write: %s\r\n", strerror(errno)); } + fprintf(stderr, "info: you probably have insufficient disk space"); u3_assert(0); } } @@ -1323,13 +1325,14 @@ _ce_image_copy(u3e_image* fom_u, u3e_image* tou_u) } if ( _ce_page != (ret_i = write(tou_u->fid_i, buf_y, _ce_page)) ) { if ( 0 < ret_i ) { - fprintf(stderr, "loom: image (%s) copy partial write: %zu, check disk space\r\n", + fprintf(stderr, "loom: image (%s) copy partial write: %zu\r\n", tou_u->nam_c, (size_t)ret_i); } else { - fprintf(stderr, "loom: image (%s) copy write: %s, check disk space\r\n", + fprintf(stderr, "loom: image (%s) copy write: %s\r\n", tou_u->nam_c, strerror(errno)); } + fprintf(stderr, "info: you probably have insufficient disk space"); return c3n; } } From b22f4454d1554da56c230394be3f2e2c1c0db8fb Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 2 Aug 2024 11:29:09 -0400 Subject: [PATCH 2/4] docs: add `autoconf-archive` to installation instructions --- INSTALL.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index df92060dfb..a7a7946a4b 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -26,7 +26,7 @@ brew install automake libtool ### Linux -After installing `automake` and `libtool`, you need to install the [musl libc] toolchain. We use [musl libc][musl libc] instead of [glibc][glibc] on Linux, which enables us to generate statically linked binaries. As a prerequisite, you need to install the [musl libc][musl libc] toolchain appropriate for your target platform. +After installing `automake`, `autoconf-archive`, and `libtool`, you need to install the [musl libc] toolchain. We use [musl libc][musl libc] instead of [glibc][glibc] on Linux, which enables us to generate statically linked binaries. As a prerequisite, you need to install the [musl libc][musl libc] toolchain appropriate for your target platform. #### x86_64 @@ -119,9 +119,9 @@ bazel build --per_file_copt='pkg/.*@-DMACRO' bazel run //bazel:refresh_compile_commands ``` -Running this command will generate a `compile_commands.json` file in the root -of the repository, which `clangd` (or other language server processors) will -use automatically to provide modern editor features like syntax highlighting, +Running this command will generate a `compile_commands.json` file in the root +of the repository, which `clangd` (or other language server processors) will +use automatically to provide modern editor features like syntax highlighting, go-to definitions, call hierarchies, symbol manipulation, etc. ## Test Commands From 889679909b330410b7b4e40a86f82c9019089ac0 Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Fri, 2 Aug 2024 18:50:33 +0300 Subject: [PATCH 3/4] stun: fix stun --- pkg/vere/io/ames/stun.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/ames/stun.c b/pkg/vere/io/ames/stun.c index e1f314ef07..8bdad69742 100644 --- a/pkg/vere/io/ames/stun.c +++ b/pkg/vere/io/ames/stun.c @@ -34,11 +34,11 @@ _stun_has_fingerprint(c3_y* buf_y, c3_w len_w) fin_y = memmem(buf_y + i, len_w - i, ned_y, sizeof(ned_y)); if ( fin_y != 0 ) { - c3_w len_w = fin_y - buf_y; + c3_w lin_w = fin_y - buf_y; // Skip attribute type and length c3_w fingerprint = c3_sift_word(fin_y + sizeof(ned_y)); c3_w init = crc32(0L, Z_NULL, 0); - c3_w crc = htonl(crc32(init, buf_y, len_w) ^ 0x5354554e); + c3_w crc = htonl(crc32(init, buf_y, lin_w) ^ 0x5354554e); if ((fingerprint == crc) && (fin_y - buf_y + 8) == len_w) { return c3y; } @@ -125,6 +125,8 @@ u3_stun_make_response(const c3_y req_y[20], // XX hardcoded to match the requests we produce // memcpy(buf_y, req_y, cur_w); + buf_y[0] = 0x01; buf_y[1] = 0x01; // 0x0101 SUCCESS RESPONSE + buf_y[2] = 0x00; buf_y[3] = 0x14; // Length: 20 bytes memset(buf_y + cur_w, 0, cur_w); From e36ea43e13e51061c0aea8169af8f16345e799b0 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 5 Aug 2024 21:38:01 -0400 Subject: [PATCH 4/4] vere: use fcntl advisory locks on pidfile --- pkg/vere/disk.c | 161 +++++++++++++++++++++++++++++++----------------- pkg/vere/vere.h | 1 + 2 files changed, 106 insertions(+), 56 deletions(-) diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index 1a55abbef8..6cb3b62ca9 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -833,86 +833,134 @@ _disk_lock(c3_c* pax_c) return paf_c; } -/* u3_disk_acquire(): acquire a lockfile, killing anything that holds it. +/* _disk_acquire(): acquire a lockfile, killing anything that holds it. */ -static void -u3_disk_acquire(c3_c* pax_c) +static c3_i +_disk_acquire(c3_c* pax_c) { - c3_c* paf_c = _disk_lock(pax_c); - c3_w pid_w; - FILE* loq_u; - - if ( NULL != (loq_u = c3_fopen(paf_c, "r")) ) { - if ( 1 != fscanf(loq_u, "%" SCNu32, &pid_w) ) { - u3l_log("lockfile %s is corrupt!", paf_c); - kill(getpid(), SIGTERM); - sleep(1); u3_assert(0); - } - else if (pid_w != getpid()) { - c3_w i_w; + c3_c* paf_c = _disk_lock(pax_c); + c3_y dat_y[12] = {0}; + c3_w pid_w = 0; + c3_i fid_i, ret_i; + + if ( -1 == (fid_i = c3_open(paf_c, O_RDWR|O_CREAT, 0666)) ) { + fprintf(stderr, "disk: failed to open/create lock file\r\n"); + goto fail; + } - int ret = kill(pid_w, SIGTERM); + { + c3_y len_y = 0; + c3_y* buf_y = dat_y; + + do { + c3_zs ret_zs; + + if ( -1 == (ret_zs = read(fid_i, buf_y, 1)) ) { + if ( EINTR == errno ) continue; - if ( -1 == ret && errno == EPERM ) { - u3l_log("disk: permission denied when trying to kill process %d!", pid_w); - kill(getpid(), SIGTERM); - sleep(1); u3_assert(0); + fprintf(stderr, "disk: failed to read lockfile: %s\r\n", + strerror(errno)); + goto fail; } - if ( -1 != ret ) { - u3l_log("disk: stopping process %d, live in %s...", - pid_w, pax_c); + if ( !ret_zs ) break; + else if ( 1 != ret_zs ) { + fprintf(stderr, "disk: strange lockfile read %zd\r\n", ret_zs); + goto fail; + } - for ( i_w = 0; i_w < 16; i_w++ ) { - sleep(1); - if ( -1 == kill(pid_w, SIGTERM) ) { - break; - } - } - if ( 16 == i_w ) { - for ( i_w = 0; i_w < 16; i_w++ ) { - if ( -1 == kill(pid_w, SIGKILL) ) { - break; - } - sleep(1); - } - } - if ( 16 == i_w ) { - u3l_log("disk: process %d seems unkillable!", pid_w); - u3_assert(0); - } - u3l_log("disk: stopped old process %u", pid_w); + len_y++; + buf_y++; + } + while ( len_y < sizeof(dat_y) ); + + + if ( len_y ) { + if ( (1 != sscanf((c3_c*)dat_y, "%" SCNu32 "%n", &pid_w, &ret_i)) + || (0 >= ret_i) + || ('\n' != *(dat_y + ret_i)) ) + { + fprintf(stderr, "disk: lockfile is corrupt\r\n"); } } - fclose(loq_u); - c3_unlink(paf_c); } - if ( NULL == (loq_u = c3_fopen(paf_c, "w")) ) { - u3l_log("disk: unable to open %s: %s", paf_c, strerror(errno)); - u3_assert(0); + { + struct flock lok_u; + memset((void *)&lok_u, 0, sizeof(lok_u)); + lok_u.l_type = F_WRLCK; + lok_u.l_whence = SEEK_SET; + lok_u.l_start = 0; + lok_u.l_len = 1; + + while ( (ret_i = fcntl(fid_i, F_SETLK, &lok_u)) + && (EINTR == (ret_i = errno)) ); + + if ( ret_i ) { + if ( pid_w ) { + fprintf(stderr, "pier: locked by PID %u\r\n", pid_w); + } + else { + fprintf(stderr, "pier: strange: locked by empty lockfile\r\n"); + } + + goto fail; + } } - fprintf(loq_u, "%u\n", getpid()); + ret_i = snprintf((c3_c*)dat_y, sizeof(dat_y), "%u\n", getpid()); + + if ( 0 >= ret_i ) { + fprintf(stderr, "disk: failed to write lockfile\r\n"); + goto fail; + } { - c3_i fid_i = fileno(loq_u); - c3_sync(fid_i); + c3_y len_y = (c3_y)ret_i; + c3_y* buf_y = dat_y; + + do { + c3_zs ret_zs; + + if ( (-1 == (ret_zs = write(fid_i, buf_y, len_y))) + && (EINTR != errno) ) + { + fprintf(stderr, "disk: lockfile write failed %s\r\n", + strerror(errno)); + goto fail; + } + + if ( 0 < ret_zs ) { + len_y -= ret_zs; + buf_y += ret_zs; + } + } + while ( len_y ); + } + + if ( -1 == c3_sync(fid_i) ) { + fprintf(stderr, "disk: failed to sync lockfile: %s\r\n", + strerror(errno)); + goto fail; } - fclose(loq_u); c3_free(paf_c); + return fid_i; + +fail: + kill(getpid(), SIGTERM); + sleep(1); u3_assert(0); } -/* u3_disk_release(): release a lockfile. +/* _disk_release(): release a lockfile. */ static void -u3_disk_release(c3_c* pax_c) +_disk_release(c3_c* pax_c, c3_i fid_i) { c3_c* paf_c = _disk_lock(pax_c); - c3_unlink(paf_c); c3_free(paf_c); + close(fid_i); } /* u3_disk_exit(): close the log. @@ -959,7 +1007,7 @@ u3_disk_exit(u3_disk* log_u) } } - u3_disk_release(log_u->dir_u->pax_c); + _disk_release(log_u->dir_u->pax_c, log_u->lok_i); u3_dire_free(log_u->dir_u); u3_dire_free(log_u->urb_u); @@ -1817,6 +1865,7 @@ u3_disk* u3_disk_init(c3_c* pax_c, u3_disk_cb cb_u) { u3_disk* log_u = c3_calloc(sizeof(*log_u)); + log_u->lok_i = -1; log_u->liv_o = c3n; log_u->ted_o = c3n; log_u->cb_u = cb_u; @@ -1835,7 +1884,7 @@ u3_disk_init(c3_c* pax_c, u3_disk_cb cb_u) // acquire lockfile. // - u3_disk_acquire(pax_c); + log_u->lok_i = _disk_acquire(pax_c); // create/load $pier/.urb // diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index b26ad4b27e..dab95c5080 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -553,6 +553,7 @@ u3_dire* dir_u; // main pier directory u3_dire* urb_u; // urbit system data u3_dire* com_u; // log directory + c3_i lok_i; // lockfile c3_o liv_o; // live c3_w ver_w; // version (see version.h) void* mdb_u; // lmdb env of current epoch