ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
main.h File Reference

Global application exit and signal handling API. More...

Go to the source code of this file.

Functions

bool should_exit (void)
void signal_exit (void)
void set_interrupt_callback (void(*cb)(void))
void setup_signal_handlers (void)

Variables

atomic_t g_should_exit

Detailed Description

Global application exit and signal handling API.

Provides a centralized exit mechanism and signal handler registration used by all modes (client, mirror, discovery, server) to coordinate graceful shutdown. All modes share a single global exit flag and interrupt callback.

Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
February 2026

Definition in file main.h.

Function Documentation

◆ set_interrupt_callback()

void set_interrupt_callback ( void(* cb )(void))

Register a mode-specific interrupt callback

Called by mode-specific code to register a callback that will be invoked when signal_exit() is called. Used to shut down network sockets so threads blocked in recv() unblock quickly instead of waiting for timeouts.

The callback is called synchronously from signal_exit(), so it must be async-signal-safe (only atomics, socket_shutdown(), no malloc/etc).

Only one callback can be registered at a time. Setting a new callback replaces the previous one.

Parameters
cbFunction to call on exit signal, or NULL to unregister

Definition at line 272 of file main.c.

272 {
273 g_interrupt_callback = cb;
274}

Referenced by acds_main(), client_main(), and discovery_main().

◆ setup_signal_handlers()

void setup_signal_handlers ( void )

Set up global signal handlers

Called once at program startup in main() BEFORE mode dispatch. Registers:

  • SIGTERM → graceful shutdown with logging
  • SIGPIPE → ignore (prevent crashes from broken pipes)
  • SIGUSR1 → synchronization debugging output (debug builds only)
  • SIGUSR2 → memory report (debug builds only)
  • Console Ctrl+C handler (all platforms)

SIGWINCH (terminal resize) is NOT registered here - client mode registers its own handler because it needs special processing.

Set up global signal handlers Called once at startup before mode dispatch

Definition at line 404 of file main.c.

404 {
405 if (!platform_set_console_ctrl_handler(console_ctrl_handler)) {
406 log_warn("Failed to register Windows console control handler: %d", platform_get_last_error());
407 }
408
409#ifdef _WIN32
410 /* Keep a CRT fallback for consoles that deliver Ctrl-C as SIGINT rather
411 * than invoking the process console handler (notably private test consoles). */
412 signal(SIGINT, windows_sigint_handler);
413#ifdef SIGBREAK
414 signal(SIGBREAK, windows_sigint_handler);
415#endif
416 log_dev("Signal handlers registered");
417#endif
418
419#ifndef _WIN32
420 platform_signal_handler_t handlers[] = {
421 {SIGINT, handle_sigterm},
422 {SIGTERM, handle_sigterm},
423 {SIGPIPE, SIG_IGN},
424 };
425 platform_register_signal_handlers(handlers, 3);
426 log_dev("Signal handlers registered");
427#endif
428}
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:575
int platform_get_last_error(void)
Definition util.c:101

References log_dev, log_warn, and platform_get_last_error().

Referenced by main().

◆ should_exit()

bool should_exit ( void )

Check if the application should exit

Called by mode main loops to detect shutdown requests from signals or errors.

Returns
true if shutdown has been requested, false otherwise

Definition at line 248 of file main.c.

248 {
250}
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
atomic_t g_should_exit
Global application exit flag (shared across all modes).
Definition globals.c:31

References atomic_load_bool(), and g_should_exit.

Referenced by api_server_main(), client_connection_start(), display_full_reset(), main(), mcp_http_run(), session_pipeline_run_main(), and session_render_loop().

◆ signal_exit()

void signal_exit ( void )

Signal that the application should exit

Sets the global exit flag and calls the registered interrupt callback if set. Safe to call from signal handlers (uses only atomics and function pointers). Called by signal handlers (SIGTERM, Ctrl+C) and normal code (errors, timeouts).

Definition at line 252 of file main.c.

252 {
253 // Note: This function may be called from a signal handler context where
254 // other threads may hold mutex locks. We avoid log_console() here to prevent
255 // deadlock (render loop holds terminal lock, signal handler can't acquire it).
256 // The shutdown will be logged by normal thread context later.
257 // Ctrl-C can arrive through both the console-control handler and the
258 // terminal input path. Only the first request may run the shutdown
259 // callback; running it twice races teardown and can leave the process stuck.
261 return;
262 log_info("Shutdown signal received");
263 // Interrupt telemetry network I/O immediately. Full drain/free remains in
264 // the normal teardown path, but cancellation must not wait for atexit.
266 void (*cb)(void) = g_interrupt_callback;
267 if (cb) {
268 cb();
269 }
270}
bool atomic_exchange_bool(atomic_t *a, bool new_value)
Atomically exchange a boolean and return the old value.
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
void ascii_otel_request_shutdown(void)
Definition otel.c:156

References ascii_otel_request_shutdown(), atomic_exchange_bool(), g_should_exit, and log_info.

Referenced by main(), session_display_write_ascii_frame(), session_render_loop(), ui_input_password(), and update_banner_install_update().

Variable Documentation

◆ g_should_exit

atomic_t g_should_exit
extern

Global exit flag used by all modes

Set to true when the application should exit (SIGTERM, Ctrl+C, error, etc). All modes check this flag regularly to exit gracefully.

Global shutdown flag referenced by lib/logging.c and lib/debug/lock.c In production builds, this is defined in main.c and set during shutdown. For tests, we stub it out as false (tests don't perform shutdown).

Definition at line 31 of file globals.c.

31{0};