Skip to content

Commit

Permalink
Added fallback reent struct. Code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
afxgroup committed Jul 6, 2023
1 parent 5d5576e commit 21e6d58
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 40 deletions.
2 changes: 1 addition & 1 deletion POSIX.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| <arpa/inet.h> | Functions for manipulating numeric IP addresses (part of Berkley sockets) | Complete |
| <assert.h> | Verify assumptions | Complete |
| <complex.h> | Complex Arithmetic | Complete |
| <cpio.h> | Magic numbers for the cpio archive format | Not present |
| <cpio.h> | Magic numbers for the cpio archive format | Complete |
| <ctype.h> | Character types | Complete | |
| <dirent.h> | Allows the opening and listing of directories | Complete |
| <dlfcn.h> | Dynamic linking | Complete |
Expand Down
12 changes: 11 additions & 1 deletion library/getclib2.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@ __getClib2(void) {

struct Task *t = FindTask(NULL);
if (NT_PROCESS == t->tc_Node.ln_Type) {
r = (struct _clib2 *) ((struct Process *) t)->pr_UID;
uint32 value;
/* Get _clib2 address stored into process pr_UID field */
GetOwnerInfoTags(OI_ProcessInput, 0, OI_OwnerUID, &value, TAG_END);
r = (struct _clib2 *) value;
}

if (!r) {
SHOWMSG("_clib2 not found in current process/task. Using fallback clib2");
struct Clib2Resource *res = (APTR) OpenResource(RESOURCE_NAME);
if (res) {
return res->fallbackClib;
}
}
return r;
}
29 changes: 29 additions & 0 deletions library/include/cpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef _CPIO_H
#define _CPIO_H

#define MAGIC "070707"

#define C_IRUSR 000400
#define C_IWUSR 000200
#define C_IXUSR 000100
#define C_IRGRP 000040
#define C_IWGRP 000020
#define C_IXGRP 000010
#define C_IROTH 000004
#define C_IWOTH 000002
#define C_IXOTH 000001

#define C_ISUID 004000
#define C_ISGID 002000
#define C_ISVTX 001000

#define C_ISBLK 060000
#define C_ISCHR 020000
#define C_ISDIR 040000
#define C_ISFIFO 010000
#define C_ISSOCK 0140000
#define C_ISLNK 0120000
#define C_ISCTG 0110000
#define C_ISREG 0100000

#endif
18 changes: 10 additions & 8 deletions library/libc_init_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,25 +410,27 @@ reent_init(struct _clib2 *__clib2) {
out:

if (!success) {
reent_exit(__clib2);
reent_exit(__clib2, FALSE);
}
}

void
reent_exit(struct _clib2 *__clib2) {
reent_exit(struct _clib2 *__clib2, BOOL fallback) {
ENTER();

/* Free global clib structure */
if (__clib2) {
struct ElfIFace *IElf = __IElf;

/* Check for getrandom fd */
if (__clib2->randfd[0] >= 0) {
close(__clib2->randfd[0]);
}
if (!fallback) {
if (__clib2->randfd[0] >= 0) {
close(__clib2->randfd[0]);
}

if (__clib2->randfd[1] >= 0) {
close(__clib2->randfd[1]);
if (__clib2->randfd[1] >= 0) {
close(__clib2->randfd[1]);
}
}

/* Free wchar stuff */
Expand Down Expand Up @@ -480,7 +482,7 @@ reent_exit(struct _clib2 *__clib2) {
}

/* Free dl stuff */
if (__clib2->__dl_elf_handle != NULL) {
if (IElf && __clib2->__dl_elf_handle != NULL) {
SHOWMSG("Closing elf handle");
CloseElfTags(__clib2->__dl_elf_handle, CET_ReClose, TRUE, TAG_DONE);
__clib2->__dl_elf_handle = NULL;
Expand Down
13 changes: 13 additions & 0 deletions library/shared_library/clib2.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ BPTR libExpunge(struct LibraryManagerInterface *Self) {
struct Clib2Resource *res = (APTR) IExec->OpenResource(RESOURCE_NAME);
if (res) {
hashmap_free(res->uxSocketsMap);
if (res->fallbackClib) {
reent_exit(res->fallbackClib, TRUE);
IExec->FreeVec(res->fallbackClib);
}

IExec->RemResource(res);
IExec->FreeVec(res);
Expand Down Expand Up @@ -356,7 +360,16 @@ struct Clib2Base *libInit(struct Clib2Base *libBase, BPTR seglist, struct ExecIF

iexec->InitSemaphore(&res->semaphore);
iexec->NewList(&res->nodes);
/* Initialize unix sockets hashmap */
res->uxSocketsMap = hashmap_new(sizeof(struct UnixSocket), 0, 0, 0, unixSocketHash, unixSocketCompare, NULL, NULL);
/* Initialize fallback clib2 reent structure */
res->fallbackClib = (struct _clib2 *) iexec->AllocVecTags(sizeof(struct _clib2),
AVT_Type, MEMF_SHARED,
AVT_ClearWithValue, 0,
TAG_DONE);
reent_init(res->fallbackClib);
res->fallbackClib->__check_abort_enabled = TRUE;

iexec->AddResource(res);
} else {
goto out;
Expand Down
1 change: 1 addition & 0 deletions library/shared_library/clib2.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct Clib2Resource {
struct SignalSemaphore semaphore; /* for list arbitration */
struct List nodes; /* list of parent nodes */
struct hashmap *uxSocketsMap;
struct _clib2 *fallbackClib;
};

struct Clib2Node {
Expand Down
43 changes: 14 additions & 29 deletions library/stdlib/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,13 @@ _main(
int (*start_main)(int, char **),
void (*__EXT_CTOR_LIST__[])(void),
void (*__EXT_DTOR_LIST__[])(void)) {
struct WBStartup *volatile startup_message;
volatile APTR old_window_pointer = NULL;
volatile BOOL old_window_pointer_valid = FALSE;
struct WBStartup *volatile startup_message = NULL;
struct Process *this_process;
int return_code = RETURN_FAIL;
ULONG current_stack_size;
struct _clib2 *__clib2 = NULL;
char uuid[UUID4_LEN + 1] = {0};
DECLARE_UTILITYBASE();

/* Pick up the Workbench startup message, if available. */
this_process = (struct Process *) FindTask(NULL);
Expand All @@ -320,10 +319,8 @@ _main(
struct MsgPort *mp = &this_process->pr_MsgPort;
WaitPort(mp);
startup_message = (struct WBStartup *) GetMsg(mp);

} else {
startup_message = NULL;
}

uint32 pid = GetPID(0, GPID_PROCESS);

/* If all libraries are opened correctly we can initialize clib2 reent structure */
Expand All @@ -337,7 +334,7 @@ _main(
goto out;

/* Get the current task pointer */
__clib2->self = (struct Process *) FindTask(NULL);
__clib2->self = this_process;

SHOWPOINTER(__clib2);

Expand All @@ -347,21 +344,14 @@ _main(
uuid4_generate(uuid);
__clib2->uuid = uuid;

/* Set _clib2 pointer into process pr_UID
* This field is copied to any spawned process created by this exe and/or its children
*/
this_process->pr_UID = (uint32) __clib2;
//SetOwnerInfoTags(OI_ProcessInput, 0, OI_OwnerUID, __clib2, TAG_END);

__clib2->__WBenchMsg = (struct WBStartup *) startup_message;

if (__clib2->__disable_dos_requesters) {
/* Don't display any requesters. */
old_window_pointer = __set_process_window((APTR) - 1);
} else {
/* Just remember the original pointer. */
old_window_pointer = __set_process_window(NULL);
__set_process_window(old_window_pointer);
}

old_window_pointer_valid = TRUE;

/* After reent structure we can call clib2 constructors */
D(("Calling clib2 ctors"));
_start_ctors(__CTOR_LIST__);
Expand Down Expand Up @@ -391,14 +381,14 @@ _main(
store its result in the global __stack_size variable. */
if (__get_default_stack_size != NULL) {
unsigned int size = (*__get_default_stack_size)();
if (size > 0)
if (size > 0) {
__clib2->__stack_size = size;
}
}

/* How much stack space was provided? */
current_stack_size = get_stack_size();

DECLARE_UTILITYBASE();
/* Set default terminal mode to "amiga-clib2" if not set */
char term_buffer[32] = {0};
LONG term_len = GetVar("TERM", (STRPTR) term_buffer, 32, 0);
Expand Down Expand Up @@ -451,8 +441,7 @@ _main(
#endif /* NDEBUG */

SHOWMSG("Swap Stack and Call Main");
return_code = __swap_stack_and_call(stk, (APTR) call_main(argstr, arglen, start_main, __EXT_CTOR_LIST__,
__EXT_DTOR_LIST__, __clib2));
return_code = __swap_stack_and_call(stk, (APTR) call_main(argstr, arglen, start_main, __EXT_CTOR_LIST__, __EXT_DTOR_LIST__, __clib2));

FreeVec(new_stack);
FreeVec(stk);
Expand All @@ -466,22 +455,18 @@ _main(
SetTaskPri((struct Task *) this_process, old_priority);

out:

SHOWMSG("Calling clib2 dtors");
_end_ctors(__DTOR_LIST__);
SHOWMSG("Done. All destructors called");

SHOWMSG("Calling reent_exit on _clib2");
reent_exit(__clib2);

if (old_window_pointer_valid)
__set_process_window(old_window_pointer);
reent_exit(__clib2, FALSE);

if (startup_message != NULL) {
if (!this_process->pr_CLI) {
Forbid();

ReplyMsg((struct Message *) startup_message);
}

SHOWMSG("Return from main");
return return_code;
}
2 changes: 1 addition & 1 deletion library/stdlib/stdlib_protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ extern void __stack_usage_exit(void);
extern void _exit(int return_code);

extern void reent_init(struct _clib2 *__clib2);
extern void reent_exit(struct _clib2 *__clib2);
extern void reent_exit(struct _clib2 *__clib2, BOOL fallback);

/****************************************************************************/

Expand Down

0 comments on commit 21e6d58

Please sign in to comment.