diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..b0a14c0 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,7 @@ +Project Rules + +Git Operations: +Do NOT run any git commands (commit, push, merge, rebase, checkout, etc.) without explicit approval. Always present the intended git operation and wait for confirmation before executing. + +File Modifications: +Do NOT modify, create, or delete any files without explicit approval. When changes are needed, present the proposed changes and wait for confirmation before writing. diff --git a/src/events.c b/src/events.c index 7ecdd2e..a2eaa09 100644 --- a/src/events.c +++ b/src/events.c @@ -254,6 +254,14 @@ void *event_executor_thread(void *arg) { if (read(fds[1].fd, &val, sizeof(val)) < 0) {} } + /* Drain signal eventfd — reset counter so poll() can block. + Must happen AFTER the pop attempt so any signal arriving during + pop is not drained away (it will be caught next poll iteration). */ + if (fds[0].revents & POLLIN) { + uint64_t val; + if (read(fds[0].fd, &val, sizeof(val)) < 0) {} + } + /* Keepalive: warm up REST connection every 30s */ if (now_ka - last_keepalive_ms >= 30000 || last_keepalive_ms == 0) { executor_keepalive(exec); diff --git a/src/fill_handler.c b/src/fill_handler.c index 52fed2f..b088e9c 100644 --- a/src/fill_handler.c +++ b/src/fill_handler.c @@ -64,7 +64,7 @@ bool fill_channel_push(fill_channel_t *ch, const fill_event_t *ev) { atomic_store_explicit(&ch->tail, next, memory_order_release); uint64_t one = 1; - write(ch->wake_fd, &one, sizeof(one)); + if (write(ch->wake_fd, &one, sizeof(one)) < 0) {} return true; } @@ -106,8 +106,12 @@ bool fill_channel_await(fill_channel_t *ch, const char *client_oid, total_funds += ev.match_price * ev.match_size; total_fee += ev.match_fee; match_count++; - if (!order_id[0] && ev.order_id[0]) - strncpy(order_id, ev.order_id, sizeof(order_id) - 1); + if (!order_id[0] && ev.order_id[0]) { + size_t ol = strlen(ev.order_id); + if (ol >= sizeof(order_id)) ol = sizeof(order_id) - 1; + memcpy(order_id, ev.order_id, ol); + order_id[ol] = '\0'; + } if (ev.is_terminal && match_count > 0) { out->total_size = total_size; @@ -115,7 +119,10 @@ bool fill_channel_await(fill_channel_t *ch, const char *client_oid, out->total_fee = total_fee; out->avg_price = total_size > 0 ? total_funds / total_size : 0; out->match_count = match_count; - strncpy(out->order_id, order_id, sizeof(out->order_id) - 1); + size_t ol = strlen(order_id); + if (ol >= sizeof(out->order_id)) ol = sizeof(out->order_id) - 1; + memcpy(out->order_id, order_id, ol); + out->order_id[ol] = '\0'; return true; } } @@ -128,7 +135,7 @@ bool fill_channel_await(fill_channel_t *ch, const char *client_oid, int ret = poll(&pfd, 1, remaining); if (ret > 0) { uint64_t val; - read(ch->wake_fd, &val, sizeof(val)); + if (read(ch->wake_fd, &val, sizeof(val)) < 0) {} } } @@ -139,7 +146,10 @@ bool fill_channel_await(fill_channel_t *ch, const char *client_oid, out->total_fee = total_fee; out->avg_price = total_size > 0 ? total_funds / total_size : 0; out->match_count = match_count; - strncpy(out->order_id, order_id, sizeof(out->order_id) - 1); + size_t ol = strlen(order_id); + if (ol >= sizeof(out->order_id)) ol = sizeof(out->order_id) - 1; + memcpy(out->order_id, order_id, ol); + out->order_id[ol] = '\0'; } return false; } diff --git a/src/http_client.c b/src/http_client.c index c62e4b0..1b2eaa7 100644 --- a/src/http_client.c +++ b/src/http_client.c @@ -420,9 +420,11 @@ char *https_post_auth(const char *host, int port, const char *path, while (*p && !(*p == '0' && (p[1] == '\r' || p[1] == '\n'))) { int cl = 0; while (*p && *p != '\r' && *p != '\n') { char h = *p; cl <<= 4; cl += (h >= '0' && h <= '9') ? (h - '0') : ((h & 0x1f) + 9); p++; } - if (*p == '\r') p++; if (*p == '\n') p++; + if (*p == '\r') p++; + if (*p == '\n') p++; if (cl > 0 && op + cl < HTTP_BUFFER_SIZE - 1) { memcpy(out + op, p, cl); op += cl; p += cl; } - if (*p == '\r') p++; if (*p == '\n') p++; + if (*p == '\r') p++; + if (*p == '\n') p++; } out[op] = '\0'; free(resp); diff --git a/src/log.c b/src/log.c index 3075d94..57e42b2 100644 --- a/src/log.c +++ b/src/log.c @@ -37,9 +37,9 @@ static void *log_worker(void *arg) { while (atomic_load(&log_running)) { ssize_t n = read(log_pipe[0], buf, sizeof(buf)); if (n > 0) { - write(STDERR_FILENO, buf, (size_t)n); + if (write(STDERR_FILENO, buf, (size_t)n) < 0) {} if (log_file_fd >= 0) - write(log_file_fd, buf, (size_t)n); + if (write(log_file_fd, buf, (size_t)n) < 0) {} } else if (n < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { usleep(100); @@ -111,7 +111,7 @@ void log_write(const char *fmt, ...) { int total = ts_len + (msg_len > 0 ? msg_len : 0); if (total > 0) { - write(log_pipe[1], buf, (size_t)total); + if (write(log_pipe[1], buf, (size_t)total) < 0) {} } } @@ -140,6 +140,6 @@ void log_write_screen(const char *fmt, ...) { int total = ts_len + (msg_len > 0 ? msg_len : 0); if (total > 0) { - write(STDERR_FILENO, buf, (size_t)total); + if (write(STDERR_FILENO, buf, (size_t)total) < 0) {} } }