From 2f732df8dd61cc4d30259d5526e01b296013cf78 Mon Sep 17 00:00:00 2001 From: Vladislav Vaintroub Date: Wed, 30 Sep 2026 16:38:08 +0200 Subject: [PATCH] MDEV-11111: wire the launcher into mysql_server_init() Glue (embedded_glue.c) registers the launcher with libmariadb through mariadb_set_embedded_hooks(), so that the plain client API is enough: mysql_server_init(argc, argv) starts the server, connecting to localhost reaches it, mysql_server_end() stops it. launcher_test now uses only that. Points libmariadb at the MDEV-11111-embedded-hooks branch (57875d2c). That commit exists only locally and must be pushed to the libmariadb repository before this one can be. Co-Authored-By: Claude Sonnet 5.5 --- libmariadb | 2 +- libmysqld/launcher/embedded_glue.c | 58 ++++++++++++++++++++++++++ libmysqld/launcher/embedded_launcher.h | 4 ++ libmysqld/launcher/launcher_test.c | 36 ++++++---------- 4 files changed, 75 insertions(+), 25 deletions(-) create mode 100644 libmysqld/launcher/embedded_glue.c diff --git a/libmariadb b/libmariadb index 8153da40fe7..cc793e6c9f2 160000 --- a/libmariadb +++ b/libmariadb @@ -1 +1 @@ -Subproject commit 8153da40fe720b1d25db12aa3f993918da15bd3b +Subproject commit cc793e6c9f249481120d33372a8c53e91c9ff4fe diff --git a/libmysqld/launcher/embedded_glue.c b/libmysqld/launcher/embedded_glue.c new file mode 100644 index 00000000000..5a60bd037ce --- /dev/null +++ b/libmysqld/launcher/embedded_glue.c @@ -0,0 +1,58 @@ +/* Copyright (c) 2026, MariaDB plc + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +/* + MDEV-11111: connect the launcher to libmariadb. + + After mariadb_embedded_register(), the ordinary client API is enough: + mysql_server_init(argc, argv, groups) starts the server, mysql_real_connect() + to the local host (no host, "localhost", "." on Windows) reaches it, and + mysql_server_end() stops it. + + A shared libmariadbd is built with MARIADB_EMBEDDED_AUTOREGISTER, so + merely loading it registers the hooks; a static build calls + mariadb_embedded_register() explicitly. +*/ + +#include +#include +#include "embedded_launcher.h" + +static int init_hook(int argc, char **argv, char **groups) +{ + if (mariadb_embedded_start(argc, argv, groups)) + { + fprintf(stderr, "mysql_server_init: %s\n", mariadb_embedded_error()); + return 1; + } + return 0; +} + +static void end_hook(void) +{ + mariadb_embedded_stop(); +} + +void mariadb_embedded_register(void) +{ + mariadb_set_embedded_hooks(init_hook, end_hook, mariadb_embedded_socket); +} + +#if defined(MARIADB_EMBEDDED_AUTOREGISTER) && defined(__GNUC__) +__attribute__((constructor)) static void autoregister(void) +{ + mariadb_embedded_register(); +} +#endif diff --git a/libmysqld/launcher/embedded_launcher.h b/libmysqld/launcher/embedded_launcher.h index 3eb30fe3bc4..f45d17ef9d1 100644 --- a/libmysqld/launcher/embedded_launcher.h +++ b/libmysqld/launcher/embedded_launcher.h @@ -56,6 +56,10 @@ const char *mariadb_embedded_socket(void); /* Last error message */ const char *mariadb_embedded_error(void); +/* Register the launcher with libmariadb (embedded_glue.c), so that + mysql_server_init()/mysql_server_end() start and stop the server */ +void mariadb_embedded_register(void); + #ifdef __cplusplus } #endif diff --git a/libmysqld/launcher/launcher_test.c b/libmysqld/launcher/launcher_test.c index f20fd5ea10d..a024a1741cc 100644 --- a/libmysqld/launcher/launcher_test.c +++ b/libmysqld/launcher/launcher_test.c @@ -1,20 +1,20 @@ /* - MDEV-11111: smoke test for the embedded launcher. + MDEV-11111: smoke test for the embedded server. Usage: launcher_test [server options...] - Starts the server, runs a query through the normal client library over the - private socket, stops the server. + Uses only the ordinary client API: mysql_server_init() starts the server, + a connection to "localhost" reaches it, mysql_server_end() stops it. */ #include #include -#include -#include "embedded_launcher.h" #ifdef _WIN32 #include -#define Sleep_or_pause() Sleep(1000) +#define pause_1s() Sleep(1000) #else #include -#define Sleep_or_pause() sleep(1) +#define pause_1s() sleep(1) #endif +#include +#include "embedded_launcher.h" int main(int argc, char **argv) { @@ -23,23 +23,12 @@ int main(int argc, char **argv) MYSQL_ROW row; int rc= 1; - if (mariadb_embedded_start(argc, argv, NULL)) - { - fprintf(stderr, "start failed: %s\n", mariadb_embedded_error()); + mariadb_embedded_register(); /* not needed with a shared libmariadbd */ + if (mysql_server_init(argc, argv, NULL)) return 1; - } - printf("socket: %s\n", mariadb_embedded_socket()); - mysql_library_init(0, NULL, NULL); /* reads MARIADB_UNIX_PORT */ m= mysql_init(NULL); -#ifdef _WIN32 - /* libmariadb ignores MARIADB_UNIX_PORT for named pipes, pass it explicitly */ - const char *host= "."; -#else - const char *host= "localhost"; /* default socket: $MARIADB_UNIX_PORT */ -#endif - if (!mysql_real_connect(m, host, "root", NULL, NULL, 0, - mariadb_embedded_socket(), 0)) + if (!mysql_real_connect(m, "localhost", "root", NULL, NULL, 0, NULL, 0)) fprintf(stderr, "connect failed: %s\n", mysql_error(m)); else if (mysql_query(m, "SELECT @@version, 1+1") || !(res= mysql_store_result(m))) @@ -53,10 +42,9 @@ int main(int argc, char **argv) } if (getenv("LAUNCHER_TEST_HANG")) /* to kill us and watch the server */ for (;;) - Sleep_or_pause(); + pause_1s(); mysql_close(m); - mysql_library_end(); - mariadb_embedded_stop(); + mysql_server_end(); puts(rc ? "FAIL" : "OK"); return rc; }