ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
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)
 

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) 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 102 of file main.c.

102 {
103 g_interrupt_callback = cb;
104}

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 → lock debug output (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 167 of file main.c.

167 {
168 platform_set_console_ctrl_handler(console_ctrl_handler);
169
170#ifndef _WIN32
171 platform_signal_handler_t handlers[] = {
172 {SIGTERM, handle_sigterm},
173 {SIGPIPE, SIG_IGN},
174 };
175 platform_register_signal_handlers(handlers, 2);
176#endif
177}

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 90 of file main.c.

90 {
91 return atomic_load(&g_app_should_exit);
92}

◆ 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 94 of file main.c.

94 {
95 atomic_store(&g_app_should_exit, true);
96 void (*cb)(void) = g_interrupt_callback;
97 if (cb) {
98 cb();
99 }
100}