Fixed compile warnings - handled write() return values and removed unused variable

This commit is contained in:
Your Name
2025-08-17 12:20:38 -04:00
parent 57201af725
commit 44c7737fa7
4 changed files with 111337 additions and 18 deletions

View File

@@ -56,8 +56,6 @@ struct main_context {
pthread_mutex_t result_mutex;
};
// Global reference to main context for signal handler (unused but kept for future use)
static main_context_t* g_main_context = NULL;
// Mining context for workers (keeping legacy fields for now during transition)
struct mining_context {
@@ -110,7 +108,7 @@ static void cleanup_context(mining_context_t* ctx);
// Signal handling and debugging functions
static void signal_handler(int sig);
static void install_signal_handlers(void);
static void log_thread_exit(int thread_id, void* exit_status, const char* reason);
static void log_thread_exit(int thread_id, void* exit_status, const char* reason, int verbose);
static const char* get_signal_name(int sig);
static void emergency_shutdown(void);
@@ -282,7 +280,11 @@ static const char* get_signal_name(int sig) {
}
}
static void log_thread_exit(int thread_id, void* exit_status, const char* reason) {
static void log_thread_exit(int thread_id, void* exit_status, const char* reason, int verbose) {
if (!verbose) {
return;
}
time_t current_time = time(NULL);
struct tm* local_time = localtime(&current_time);
@@ -319,14 +321,14 @@ static void signal_handler(int sig) {
// Log the signal (async-signal-safe functions only)
const char* sig_name = get_signal_name(sig);
// Write to stderr using async-signal-safe functions
write(STDERR_FILENO, "\n[SIGNAL] Received ", 19);
write(STDERR_FILENO, sig_name, strlen(sig_name));
write(STDERR_FILENO, "\n", 1);
// Write to stderr using async-signal-safe functions (ignore return values in signal handler)
(void)write(STDERR_FILENO, "\n[SIGNAL] Received ", 19);
(void)write(STDERR_FILENO, sig_name, strlen(sig_name));
(void)write(STDERR_FILENO, "\n", 1);
// For fatal signals, try emergency shutdown
if (sig == SIGSEGV || sig == SIGABRT || sig == SIGFPE || sig == SIGBUS) {
write(STDERR_FILENO, "[SIGNAL] Attempting emergency shutdown...\n", 42);
(void)write(STDERR_FILENO, "[SIGNAL] Attempting emergency shutdown...\n", 42);
emergency_shutdown();
// Reset signal handler to default and re-raise
@@ -334,7 +336,7 @@ static void signal_handler(int sig) {
raise(sig);
} else if (sig == SIGINT || sig == SIGTERM) {
// Graceful shutdown for interrupt signals
write(STDERR_FILENO, "[SIGNAL] Initiating graceful shutdown...\n", 41);
(void)write(STDERR_FILENO, "[SIGNAL] Initiating graceful shutdown...\n", 41);
emergency_shutdown();
}
@@ -416,7 +418,7 @@ static void* miner_thread(void* arg) {
// Create a copy of the event for this thread
char* event_str = cJSON_Print(ctx->event);
if (!event_str) {
log_thread_exit(ctx->thread_id, exit_status, "JSON serialization failed");
log_thread_exit(ctx->thread_id, exit_status, "JSON serialization failed", ctx->verbose_enabled);
return exit_status;
}
@@ -424,7 +426,7 @@ static void* miner_thread(void* arg) {
free(event_str);
if (!local_event) {
log_thread_exit(ctx->thread_id, exit_status, "Event parsing failed");
log_thread_exit(ctx->thread_id, exit_status, "Event parsing failed", ctx->verbose_enabled);
return exit_status;
}
@@ -450,14 +452,14 @@ static void* miner_thread(void* arg) {
ctx->solution_callback(local_event, ctx);
}
exit_status = (void*)(intptr_t)THREAD_EXIT_SUCCESS;
log_thread_exit(ctx->thread_id, exit_status, "Solution found");
log_thread_exit(ctx->thread_id, exit_status, "Solution found", ctx->verbose_enabled);
break; // Exit after reporting solution
}
// Check for emergency shutdown
if (g_shutdown_requested) {
exit_status = (void*)(intptr_t)THREAD_EXIT_STOPPED;
log_thread_exit(ctx->thread_id, exit_status, "Emergency shutdown");
log_thread_exit(ctx->thread_id, exit_status, "Emergency shutdown", ctx->verbose_enabled);
break;
}
@@ -468,7 +470,7 @@ static void* miner_thread(void* arg) {
// Normal stop by main thread
if (ctx->should_stop && !g_shutdown_requested) {
exit_status = (void*)(intptr_t)THREAD_EXIT_STOPPED;
log_thread_exit(ctx->thread_id, exit_status, "Stopped by main thread");
log_thread_exit(ctx->thread_id, exit_status, "Stopped by main thread", ctx->verbose_enabled);
}
cJSON_Delete(local_event);
@@ -542,7 +544,7 @@ static int mine_event(mining_context_t* ctx) {
for (int j = 0; j < i; j++) {
void* exit_status;
pthread_join(threads[j], &exit_status);
log_thread_exit(j, exit_status, "Cleanup after creation failure");
log_thread_exit(j, exit_status, "Cleanup after creation failure", ctx->verbose_enabled);
}
free(threads);
free(worker_contexts);
@@ -598,7 +600,7 @@ static int mine_event(mining_context_t* ctx) {
// Log exit status if it wasn't already logged by the thread
long status_code = (long)exit_status;
if (status_code == THREAD_EXIT_ERROR) {
log_thread_exit(i, exit_status, "Thread error (not previously logged)");
log_thread_exit(i, exit_status, "Thread error (not previously logged)", ctx->verbose_enabled);
}
}