ascii-chat 0.11.33
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(*)(void)  cb)

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 125 of file misc.c.

125 {
126 (void)callback;
127}

Referenced by 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 346 of file main.c.

346 {
347 platform_set_console_ctrl_handler(console_ctrl_handler);
348
349#ifndef _WIN32
350 platform_signal_handler_t handlers[] = {
351 {SIGINT, handle_sigterm},
352 {SIGTERM, handle_sigterm},
353 {SIGPIPE, SIG_IGN},
354 };
356 log_debug("Signal handlers registered");
357#endif
358}
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548
asciichat_error_t platform_register_signal_handlers(const platform_signal_handler_t *handlers, int count)
Register multiple signal handlers at once.
Signal handler descriptor for bulk registration.
Definition system.h:295
bool platform_set_console_ctrl_handler(console_ctrl_handler_t handler)
Register a console control handler (for Ctrl+C, etc.)

References log_debug, platform_register_signal_handlers(), and platform_set_console_ctrl_handler().

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 131 of file misc.c.

131 {
132 return false;
133}

References atomic_load_bool(), and g_should_exit.

Referenced by client_main(), connection_attempt_tcp(), connection_attempt_websocket(), display_full_reset(), main(), protocol_start_connection(), server_connection_establish(), session_client_like_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 129 of file misc.c.

129{}

References atomic_store_bool(), and g_should_exit.

Referenced by client_main(), main(), and session_render_loop().

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 34 of file globals.c.

34{0};