cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
rdbms_auto_mount.c File Reference

Automatic RDBMS provider detection and runtime mounting. More...

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <cwist/sys/app/app.h>
#include <cwist/core/log.h>
#include <cwist/core/mem/alloc.h>
Include dependency graph for rdbms_auto_mount.c:

Go to the source code of this file.

Functions

cwist_rdbms_provider_t cwist_rdbms_probe_port (int port)
 Probe a TCP port on 127.0.0.1 to detect the RDBMS provider.
 
bool cwist_rdbms_mount_runtime (cwist_app *app, cwist_rdbms_provider_t provider, int port)
 Allocate and attach an RDBMS runtime record to an app.
 
bool cwist_app_auto_rdbms (cwist_app *app, int port)
 Auto-detect an RDBMS on a local port and mount its runtime on an app.
 

Detailed Description

Automatic RDBMS provider detection and runtime mounting.

Definition in file rdbms_auto_mount.c.

Function Documentation

◆ cwist_app_auto_rdbms()

bool cwist_app_auto_rdbms ( cwist_app *  app,
int  port 
)

Auto-detect an RDBMS on a local port and mount its runtime on an app.

Auto-detect and mount an RDBMS runtime by probing a TCP port.

Probes port via cwist_rdbms_probe_port(); on success the detected provider's runtime is mounted with cwist_rdbms_mount_runtime().

Parameters
appApp to attach the runtime to; must not be NULL.
portTCP port on localhost to probe.
Returns
true if detection and mounting succeed, false otherwise.

Definition at line 189 of file rdbms_auto_mount.c.

References CWIST_LOG_ERROR, cwist_rdbms_mount_runtime(), CWIST_RDBMS_NONE, cwist_rdbms_probe_port(), cwist_rdbms_runtime::port, and cwist_rdbms_runtime::provider.

◆ cwist_rdbms_mount_runtime()

bool cwist_rdbms_mount_runtime ( cwist_app *  app,
cwist_rdbms_provider_t  provider,
int  port 
)

Allocate and attach an RDBMS runtime record to an app.

Mount an RDBMS runtime for the given provider and port.

Idempotent: if app already has an RDBMS runtime mounted, this returns true without allocating. On success the new runtime is stored in app->rdbms with ready set to true.

Parameters
appApp to attach the runtime to; must not be NULL.
providerDetected provider stored in the runtime.
portPort the provider was detected on.
Returns
true if a runtime is mounted, false on NULL app or allocation failure.

Definition at line 155 of file rdbms_auto_mount.c.

References cap, cwist_alloc_array(), CWIST_LOG_ERROR, CWIST_LOG_INFO, cwist_rdbms_runtime::port, cwist_rdbms_runtime::provider, and cwist_app::rdbms.

Referenced by cwist_app_auto_rdbms().

◆ cwist_rdbms_probe_port()

cwist_rdbms_provider_t cwist_rdbms_probe_port ( int  port)

Probe a TCP port on 127.0.0.1 to detect the RDBMS provider.

Probe a TCP port for an RDBMS wire protocol.

Connects to the given port with a 2-second send/recv timeout, inspects any unsolicited handshake bytes as a MySQL/MariaDB handshake, and if that does not match, sends a PostgreSQL StartupMessage and waits for an AuthenticationRequest response.

Parameters
portTCP port on localhost to probe.
Returns
Detected provider (CWIST_RDBMS_MYSQL, CWIST_RDBMS_MARIADB, or CWIST_RDBMS_POSTGRES) or CWIST_RDBMS_NONE if detection fails. The socket is always closed before returning.

Definition at line 83 of file rdbms_auto_mount.c.

References cap, CWIST_LOG_ERROR, CWIST_RDBMS_NONE, and CWIST_RDBMS_POSTGRES.

Referenced by cwist_app_auto_rdbms().