2023-05-13 17:30:09 +00:00
|
|
|
/*
|
2024-01-13 22:56:58 +00:00
|
|
|
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
|
2023-05-13 17:30:09 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <Runtime.h>
|
|
|
|
#include <Array.h>
|
|
|
|
#include <HashMap.h>
|
|
|
|
#include <Stream.h>
|
|
|
|
#include <Log.h>
|
|
|
|
#include <Memory.h>
|
|
|
|
#include <Str.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2023-06-04 18:44:37 +00:00
|
|
|
#define ERR_BUF_SIZE 128
|
|
|
|
|
2023-05-13 17:30:09 +00:00
|
|
|
/* Specified by POSIX to contain environment variables */
|
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
/* The linking program is expected to provide Main */
|
|
|
|
extern int Main(Array *, HashMap *);
|
|
|
|
|
2023-06-04 18:44:37 +00:00
|
|
|
/* Functions in the Memory API that are not exported via header */
|
|
|
|
extern int MemoryRuntimeInit(void);
|
|
|
|
extern int MemoryRuntimeDestroy(void);
|
|
|
|
|
2023-05-13 17:30:09 +00:00
|
|
|
typedef struct MainArgs
|
|
|
|
{
|
|
|
|
Array *args;
|
|
|
|
HashMap *env;
|
2023-05-26 21:05:06 +00:00
|
|
|
int ret;
|
2023-05-13 17:30:09 +00:00
|
|
|
} MainArgs;
|
|
|
|
|
|
|
|
static void *
|
|
|
|
MainThread(void *argp)
|
|
|
|
{
|
|
|
|
MainArgs *args = argp;
|
|
|
|
|
|
|
|
args = argp;
|
2023-05-26 21:05:06 +00:00
|
|
|
args->ret = Main(args->args, args->env);
|
2023-05-13 17:30:09 +00:00
|
|
|
|
2023-05-26 21:05:06 +00:00
|
|
|
return NULL;
|
2023-05-13 17:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
pthread_t mainThread;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
char *key;
|
|
|
|
char *val;
|
|
|
|
char **envp;
|
|
|
|
|
|
|
|
MainArgs args;
|
|
|
|
|
2023-06-04 18:44:37 +00:00
|
|
|
char errBuf[ERR_BUF_SIZE];
|
|
|
|
int errLen;
|
2023-05-23 22:43:37 +00:00
|
|
|
|
2023-05-13 17:30:09 +00:00
|
|
|
args.args = NULL;
|
|
|
|
args.env = NULL;
|
2023-05-26 21:05:06 +00:00
|
|
|
args.ret = EXIT_FAILURE;
|
2023-05-13 17:30:09 +00:00
|
|
|
|
2023-06-04 18:44:37 +00:00
|
|
|
if (!MemoryRuntimeInit())
|
|
|
|
{
|
2023-06-18 02:53:52 +00:00
|
|
|
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to initialize Memory runtime.\n");
|
2023-06-04 18:44:37 +00:00
|
|
|
write(STDERR_FILENO, errBuf, errLen);
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-05-13 17:30:09 +00:00
|
|
|
args.args = ArrayCreate();
|
|
|
|
|
|
|
|
if (!args.args)
|
|
|
|
{
|
2023-06-04 18:44:37 +00:00
|
|
|
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to allocate heap memory for program arguments.\n");
|
|
|
|
write(STDERR_FILENO, errBuf, errLen);
|
2023-05-13 17:30:09 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.env = HashMapCreate();
|
|
|
|
if (!args.env)
|
|
|
|
{
|
2023-06-04 18:44:37 +00:00
|
|
|
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to allocate heap memory for program environment.\n");
|
|
|
|
write(STDERR_FILENO, errBuf, errLen);
|
2023-05-13 17:30:09 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < (size_t) argc; i++)
|
|
|
|
{
|
2023-06-04 18:44:37 +00:00
|
|
|
char *arg = StrDuplicate(argv[i]);
|
2023-06-18 02:53:52 +00:00
|
|
|
|
2023-06-04 18:44:37 +00:00
|
|
|
if (!arg || !ArrayAdd(args.args, arg))
|
|
|
|
{
|
|
|
|
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to allocate heap memory for program argument.\n");
|
|
|
|
write(STDERR_FILENO, errBuf, errLen);
|
|
|
|
goto finish;
|
|
|
|
}
|
2023-05-13 17:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
envp = environ;
|
|
|
|
while (*envp)
|
|
|
|
{
|
2023-05-15 22:38:52 +00:00
|
|
|
size_t valInd;
|
2023-05-13 17:30:09 +00:00
|
|
|
|
|
|
|
/* It is unclear whether or not envp strings are writable, so
|
|
|
|
* we make our own copy to manipulate it */
|
|
|
|
key = StrDuplicate(*envp);
|
2023-06-04 18:44:37 +00:00
|
|
|
|
|
|
|
if (!key)
|
|
|
|
{
|
|
|
|
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to allocate heap memory for program environment variable.\n");
|
|
|
|
write(STDERR_FILENO, errBuf, errLen);
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-05-13 17:30:09 +00:00
|
|
|
valInd = strcspn(key, "=");
|
|
|
|
|
|
|
|
key[valInd] = '\0';
|
|
|
|
val = key + valInd + 1;
|
|
|
|
HashMapSet(args.env, key, StrDuplicate(val));
|
|
|
|
Free(key);
|
2023-05-15 22:38:52 +00:00
|
|
|
envp++;
|
2023-05-13 17:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_create(&mainThread, NULL, MainThread, &args) != 0)
|
|
|
|
{
|
2023-06-04 18:44:37 +00:00
|
|
|
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to create main thread.\n");
|
|
|
|
write(STDERR_FILENO, errBuf, errLen);
|
2023-05-13 17:30:09 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:05:06 +00:00
|
|
|
if (pthread_join(mainThread, NULL) != 0)
|
2023-05-13 17:30:09 +00:00
|
|
|
{
|
2023-06-04 18:44:37 +00:00
|
|
|
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to join main thread.\n");
|
|
|
|
write(STDERR_FILENO, errBuf, errLen);
|
2023-05-26 21:05:06 +00:00
|
|
|
args.ret = EXIT_FAILURE;
|
2023-05-13 17:30:09 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
|
|
|
if (args.args)
|
|
|
|
{
|
|
|
|
for (i = 0; i < ArraySize(args.args); i++)
|
|
|
|
{
|
|
|
|
Free(ArrayGet(args.args, i));
|
|
|
|
}
|
|
|
|
ArrayFree(args.args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.env)
|
|
|
|
{
|
|
|
|
while (HashMapIterate(args.env, &key, (void **) &val))
|
|
|
|
{
|
|
|
|
Free(val);
|
|
|
|
}
|
|
|
|
HashMapFree(args.env);
|
|
|
|
}
|
|
|
|
|
|
|
|
LogConfigFree(LogConfigGlobal());
|
|
|
|
|
|
|
|
StreamClose(StreamStdout());
|
|
|
|
StreamClose(StreamStdin());
|
|
|
|
StreamClose(StreamStderr());
|
|
|
|
|
2023-05-15 22:38:52 +00:00
|
|
|
GenerateMemoryReport(argc, argv);
|
2023-05-13 17:30:09 +00:00
|
|
|
|
2023-06-04 18:44:37 +00:00
|
|
|
MemoryRuntimeDestroy();
|
2023-05-13 17:30:09 +00:00
|
|
|
|
2023-05-26 21:05:06 +00:00
|
|
|
return args.ret;
|
2023-05-13 17:30:09 +00:00
|
|
|
}
|