Skip to content

Commit

Permalink
Merge develop to next/kelvin/410 (#707)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkova authored Aug 13, 2024
2 parents 59082ba + aadd406 commit 2c33ef6
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 68 deletions.
8 changes: 4 additions & 4 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions pkg/noun/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,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);
}
}
Expand Down Expand Up @@ -873,12 +874,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);
}
}
Expand Down Expand Up @@ -1325,13 +1327,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;
}
}
Expand Down
161 changes: 105 additions & 56 deletions pkg/vere/disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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
//
Expand Down
6 changes: 4 additions & 2 deletions pkg/vere/io/ames/stun.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions pkg/vere/vere.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c33ef6

Please sign in to comment.