mod_tile 0.9.1
Renders map tiles with Mapnik and serves them using Apache HTTP Server
Loading...
Searching...
No Matches
renderd.c File Reference
#include <arpa/inet.h>
#include <errno.h>
#include <glib.h>
#include <limits.h>
#include <netdb.h>
#include <netinet/in.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <getopt.h>
#include <signal.h>
#include <time.h>
#include "config.h"
#include "g_logger.h"
#include "gen_tile.h"
#include "protocol.h"
#include "protocol_helper.h"
#include "render_config.h"
#include "renderd.h"
#include "renderd_config.h"
#include "request_queue.h"

Macros

#define PFD_LISTEN   0
 pollfd array index reserved for the incoming client listening socket.
#define PFD_EXIT_PIPE   1
 pollfd array index reserved for the self-pipe read end signaling graceful daemon shutdown.
#define PFD_SPECIAL_COUNT   2
 Number of fixed special descriptors preceding client connection slots in pfd[].

Functions

static const char * cmdStr (enum protoCmd c)
 Convert a wire protocol command enum value into a human-readable label for logging.
void send_response (struct item *item, enum protoCmd rsp, int render_time)
 Complete a metatile render request, notify all waiting clients, and free its queue items.
enum protoCmd rx_request (struct protocol *req, int fd)
 Normalize, validate, and enqueue an incoming wire protocol render or dirty request.
void request_exit (void)
 Async-signal-safe shutdown trigger waking the main poll() event loop via exit_pipe_fd.
void process_loop (int listen_fd)
 Main single-threaded poll() event loop accepting client connections and dispatching requests.
void * stats_writeout_thread (void *arg)
 Background thread entry point atomically writing queue and render statistics every 10 seconds.
int client_socket_init (renderd_config *sConfig)
 Open a client stream socket to a slave renderd instance over TCP or UNIX domain socket.
static int server_socket_init_tcp (renderd_config *sConfig)
 Create and bind a dual-stack IPv6 (or fallback IPv4) TCP listening socket.
int server_socket_init (renderd_config *sConfig)
 Create, bind, and start listening on the primary renderd TCP or UNIX domain server socket.
void * slave_thread (void *arg)
 Worker thread entry point forwarding queued metatile jobs to a remote slave renderd.
int main (int argc, char **argv)
 Entry point for the renderd Mapnik tile rendering daemon.

Variables

static pthread_t * render_threads
 Array of local Mapnik worker thread handles (config->num_threads entries).
static pthread_t * slave_threads
 Array of slave-dispatcher thread handles (num_slave_threads entries).
static struct sigaction sigPipeAction sigExitAction
 Signal handler configuration ignoring SIGPIPE on broken client/slave sockets.
static pthread_t stats_thread
 Background thread handle periodically flushing renderd statistics to disk.
static int exit_pipe_fd
 Write end of the self-pipe written by request_exit() to wake process_loop().
struct request_queue * render_request_queue
 Global thread-safe render request queue shared by network I/O, local workers, and slave threads.

Macro Definition Documentation

◆ PFD_EXIT_PIPE

#define PFD_EXIT_PIPE   1

pollfd array index reserved for the self-pipe read end signaling graceful daemon shutdown.

◆ PFD_LISTEN

#define PFD_LISTEN   0

pollfd array index reserved for the incoming client listening socket.

◆ PFD_SPECIAL_COUNT

#define PFD_SPECIAL_COUNT   2

Number of fixed special descriptors preceding client connection slots in pfd[].

Function Documentation

◆ client_socket_init()

int client_socket_init ( renderd_config * sConfig)

Open a client stream socket to a slave renderd instance over TCP or UNIX domain socket.

Parameters
sConfigSlave renderd_config specifying iphostname/ipport or socketname.
Returns
Connected socket file descriptor on success, or FD_INVALID (-1) on failure.

◆ cmdStr()

const char * cmdStr ( enum protoCmd c)
static

Convert a wire protocol command enum value into a human-readable label for logging.

Parameters
cWire protocol command opcode.
Returns
Static string name corresponding to c (or "Unknown" if unrecognized).

◆ main()

int main ( int argc,
char ** argv )

Entry point for the renderd Mapnik tile rendering daemon.

Parses command-line flags (-c/--config, -f/--foreground, -s/--slave, -h/--help, -V/--version), loads INI configuration, initializes the request queue, listening socket, signal handlers, Mapnik environment, optional daemonization/PID file, stats writer thread, local render threads, and slave threads, waits for map initialization, and runs process_loop().

Parameters
argcCommand-line argument count.
argvCommand-line argument vector.
Returns
0 on graceful exit, or non-zero exit code on configuration or startup failure.

◆ process_loop()

void process_loop ( int listen_fd)

Main single-threaded poll() event loop accepting client connections and dispatching requests.

Monitors listen_fd, the shutdown self-pipe, and up to MAX_CONNECTIONS active client sockets; reads incoming protocol packets via recv_cmd(), enqueues valid requests through rx_request(), and cleans up disconnected client file descriptors.

Parameters
listen_fdBound and listening server socket file descriptor.

◆ request_exit()

void request_exit ( void )

Async-signal-safe shutdown trigger waking the main poll() event loop via exit_pipe_fd.

May be invoked safely from POSIX signal handlers (SIGINT, SIGTERM, SIGHUP) as well as worker threads encountering fatal startup errors.

◆ rx_request()

enum protoCmd rx_request ( struct protocol * req,
int fd )

Normalize, validate, and enqueue an incoming wire protocol render or dirty request.

Upgrades protocol v1/v2 packets to v3 defaults, validates the command opcode, allocates a struct item, aligns (x, y) coordinates down to the METATILE grid origin (mx, my), and inserts the item into render_request_queue.

Parameters
reqParsed wire protocol request received from a client socket.
fdConnected client socket descriptor (ignored/set to FD_INVALID for cmdDirty).
Returns
Resulting queue status opcode (cmdIgnore when queued/coalesced, cmdDone if already satisfied, or cmdNotDone on validation/allocation/queue-full error).

◆ send_response()

void send_response ( struct item * item,
enum protoCmd rsp,
int render_time )

Complete a metatile render request, notify all waiting clients, and free its queue items.

Removes item from render_request_queue, iterates through item and its coalesced duplicates chain, sends rsp (cmdDone or cmdNotDone) over each valid client socket awaiting a synchronous render, and frees every struct item node.

Parameters
itemHead of the completed metatile request (and duplicate subscriber) chain.
rspCompletion status opcode (cmdDone or cmdNotDone).
render_timeElapsed metatile render duration in milliseconds (or -1 if delegated/not rendered).

◆ server_socket_init()

int server_socket_init ( renderd_config * sConfig)

Create, bind, and start listening on the primary renderd TCP or UNIX domain server socket.

When binding a UNIX domain socket, unlinks any stale socket file and temporarily sets umask(0) so Apache worker processes can connect to the daemon socket.

Parameters
sConfigActive renderd_config specifying TCP port or UNIX socket path.
Returns
Listening server socket file descriptor.

◆ server_socket_init_tcp()

int server_socket_init_tcp ( renderd_config * sConfig)
static

Create and bind a dual-stack IPv6 (or fallback IPv4) TCP listening socket.

Attempts to open an AF_INET6 socket with IPV6_V6ONLY disabled so both IPv6 and IPv4-mapped clients can connect; falls back to AF_INET if IPv6 is unavailable. Exits the daemon on fatal socket/bind errors.

Parameters
sConfigActive renderd_config specifying iphostname and ipport.
Returns
Bound TCP server socket file descriptor.

◆ slave_thread()

void * slave_thread ( void * arg)

Worker thread entry point forwarding queued metatile jobs to a remote slave renderd.

Spawned once per configured slave worker slot (config_slaves[i].num_threads), each thread fetches one request at a time from render_request_queue, sends a v3 cmdRender packet to its slave daemon, blocks for the reply, and relays the result back via send_response().

Parameters
argPointer to the target slave's renderd_config entry.
Returns
Always returns NULL on fatal allocation or unrecoverable socket send error.

◆ stats_writeout_thread()

void * stats_writeout_thread ( void * arg)

Background thread entry point atomically writing queue and render statistics every 10 seconds.

Snapshots render_request_queue metrics, writes them to <stats_filename>.tmp, and atomically renames the file over config->stats_filename for external monitoring tools (e.g., Munin).

Parameters
argUnused thread argument (NULL).
Returns
Always returns NULL if repeated file write/rename failures occur.

Variable Documentation

◆ exit_pipe_fd

int exit_pipe_fd
static

Write end of the self-pipe written by request_exit() to wake process_loop().

◆ render_request_queue

struct request_queue* render_request_queue

Global thread-safe render request queue shared by network I/O, local workers, and slave threads.

◆ render_threads

pthread_t* render_threads
static

Array of local Mapnik worker thread handles (config->num_threads entries).

◆ sigExitAction

struct sigaction sigPipeAction sigExitAction
static

Signal handler configuration ignoring SIGPIPE on broken client/slave sockets.

◆ slave_threads

pthread_t* slave_threads
static

Array of slave-dispatcher thread handles (num_slave_threads entries).

◆ stats_thread

pthread_t stats_thread
static

Background thread handle periodically flushing renderd statistics to disk.