ladybird/Libraries/LibMain/Main.cpp
ayeteadoe 7683f1285f AK: Expose helpers to invoke Windows runtime config directly in main()
When shutting down helper processes, PosixSocketHelper::close() in
SocketWindows when trying to close the socket fd with
WSANOTINITIALISED. This was due to us initiating WinSock2 during static
initialization and presumably also not terminating WinSock2 properly.

Similar to WinSock2 initiation, calling CRT during static
initialization is considered dangerous given the CRT DLL itself may not
yet be initialized.

Because of the above, we move to perform this Windows-specific
runtime setup/teardown calls into the main() functions.
2025-10-29 21:07:52 -06:00

62 lines
1.2 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Format.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
#include <LibMain/Main.h>
#include <string.h>
#include <time.h>
#if defined(AK_OS_WINDOWS)
# include <AK/Windows.h>
#endif
namespace Main {
static int s_return_code_for_errors = 1;
int return_code_for_errors()
{
return s_return_code_for_errors;
}
void set_return_code_for_errors(int code)
{
s_return_code_for_errors = code;
}
}
int main(int argc, char** argv)
{
tzset();
#if defined(AK_OS_WINDOWS)
windows_init();
#endif
Vector<StringView> arguments;
arguments.ensure_capacity(argc);
for (int i = 0; i < argc; ++i)
arguments.unchecked_append({ argv[i], strlen(argv[i]) });
auto result = ladybird_main({
.argc = argc,
.argv = argv,
.strings = arguments.span(),
});
#if defined(AK_OS_WINDOWS)
windows_shutdown();
#endif
if (result.is_error()) {
auto error = result.release_error();
warnln("\033[31;1mRuntime error\033[0m: {}", error);
return Main::return_code_for_errors();
}
return result.value();
}