From ac4b06011584f34b373aa5c0e3c40df405d98695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrea=20Palmat=C3=A8?= Date: Sat, 22 Jun 2024 19:31:57 +0200 Subject: [PATCH] Some minor close files fixes --- library/c.lib_rev.h | 6 +++--- library/crtbegin.c | 2 +- library/fcntl/open.c | 2 +- library/libc_init_global.c | 10 ++++++++++ library/posix/raise.c | 12 +++++++----- library/socket/select_signal.c | 1 - library/stdio/file_init.c | 6 +++--- library/stdio/flockfile.c | 8 +------- library/stdio/init_exit.c | 12 ++++++++---- library/stdlib/main.c | 3 +++ 10 files changed, 37 insertions(+), 25 deletions(-) diff --git a/library/c.lib_rev.h b/library/c.lib_rev.h index c568d545..4acfe6f7 100755 --- a/library/c.lib_rev.h +++ b/library/c.lib_rev.h @@ -2,7 +2,7 @@ #define REVISION 0 #define SUBREVISION 0 -#define DATE "20.06.2024" +#define DATE "22.06.2024" #define VERS "clib4.library 1.0.0" -#define VSTRING "clib4.library 1.0.0 (20.06.2024)\r\n" -#define VERSTAG "\0$VER: clib4.library 1.0.0 (20.06.2024)" +#define VSTRING "clib4.library 1.0.0 (22.06.2024)\r\n" +#define VERSTAG "\0$VER: clib4.library 1.0.0 (22.06.2024)" diff --git a/library/crtbegin.c b/library/crtbegin.c index 6fedbcf1..1a619100 100644 --- a/library/crtbegin.c +++ b/library/crtbegin.c @@ -123,7 +123,7 @@ clib4_start(char *args, int32 arglen, struct Library *sysbase) { iclib4 = (struct Clib4IFace *) OpenLibraryInterface(iexec, "clib4.library", 1); if (iclib4 != NULL) { struct Library *clib4base = ((struct Interface *) iclib4)->Data.LibBase; - if (clib4base->lib_Version == VERSION && clib4base->lib_Revision >= REVISION) { + if (clib4base->lib_Version >= VERSION && clib4base->lib_Revision >= REVISION) { IClib4 = iclib4; rc = iclib4->library_start(args, arglen, main, __CTOR_LIST__, __DTOR_LIST__); diff --git a/library/fcntl/open.c b/library/fcntl/open.c index 3e89a5d8..36295a39 100755 --- a/library/fcntl/open.c +++ b/library/fcntl/open.c @@ -68,7 +68,7 @@ open(const char *path_name, int open_flag, ... /* mode_t mode */) { goto out; } - /* We can open only files, but never directories. */ + /* We can open only directories. */ if (!EXD_IS_DIRECTORY(fib)) { SHOWMSG("we need a directory"); diff --git a/library/libc_init_global.c b/library/libc_init_global.c index 5ff9e68e..677b2b3d 100755 --- a/library/libc_init_global.c +++ b/library/libc_init_global.c @@ -411,26 +411,32 @@ reent_exit(struct _clib4 *__clib4, BOOL fallback) { /* We can't call close() in fallback reent, since destructors * are already called and function is no more available */ if (__clib4->randfd[0] >= 0) { + SHOWMSG("Closing randfd[0]"); close(__clib4->randfd[0]); } if (__clib4->randfd[1] >= 0) { + SHOWMSG("Closing randfd[1]"); close(__clib4->randfd[1]); } } /* Free wchar stuff */ if (__clib4->wide_status != NULL) { + SHOWMSG("Freeing wide_status"); FreeVec(__clib4->wide_status); __clib4->wide_status = NULL; } /* Remove random semaphore */ + SHOWMSG("Delete __random_lock semaphore"); __delete_semaphore(__clib4->__random_lock); /* Remove pipe semaphore */ + SHOWMSG("Delete __pipe_semaphore semaphore"); __delete_semaphore(__clib4->__pipe_semaphore); if (!fallback) { //TODO : Freeing memalign crash libExpunge and I don't know why /* Free memalign stuff */ if (__clib4->__memalign_pool) { + SHOWMSG("Freeing memalign pool"); /* Check if we have something created with posix_memalign and not freed yet. * But this is a good point also to free something allocated with memalign or * aligned_alloc and all other functions are using memalign_tree to allocate memory @@ -452,18 +458,22 @@ reent_exit(struct _clib4 *__clib4, BOOL fallback) { } FreeSysObject(ASOT_ITEMPOOL, __clib4->__memalign_pool); + SHOWMSG("Done"); } } /* Free dl stuff */ struct ElfIFace *IElf = __IElf; if (IElf && __clib4->__dl_root_handle != NULL) { + SHOWMSG("Closing __dl_root_handle"); CloseElfTags(__clib4->__dl_root_handle, CET_ReClose, TRUE, TAG_DONE); __clib4->__dl_root_handle = NULL; + SHOWMSG("Done"); } FreeVec(__clib4); __clib4 = NULL; + SHOWMSG("__clib4 destroyed correctly"); } } diff --git a/library/posix/raise.c b/library/posix/raise.c index d045e4cc..84659395 100644 --- a/library/posix/raise.c +++ b/library/posix/raise.c @@ -97,13 +97,15 @@ raise(int sig) { goto out; } else { - SHOWMSG("calling the handler"); - (*handler)(sig); + if (*handler != NULL) { + SHOWMSG("calling the handler"); + (*handler)(sig); - if (sig == SIGINT || sig == SIGTERM || sig == SIGKILL) - SetSignal(0, SIGBREAKF_CTRL_C); + if (sig == SIGINT || sig == SIGTERM || sig == SIGKILL) + SetSignal(0, SIGBREAKF_CTRL_C); - SHOWMSG("done."); + SHOWMSG("done."); + } } /* Unblock signal delivery again. */ diff --git a/library/socket/select_signal.c b/library/socket/select_signal.c index e43a64e6..30e071eb 100644 --- a/library/socket/select_signal.c +++ b/library/socket/select_signal.c @@ -844,7 +844,6 @@ __select(int num_fds, fd_set *read_fds, fd_set *write_fds, fd_set *except_fds, s else if (FLAG_IS_SET(fd->fd_Flags, FDF_TERMIOS)) { SHOWVALUE("FLAG_IS_SET(fd->fd_Flags, FDF_TERMIOS"); struct termios *tios = fd->fd_Aux; - SHOWVALUE(FLAG_IS_CLEAR(tios->c_lflag, ICANON)); if (WaitForChar(readFile, 1)) { got_input = TRUE; diff --git a/library/stdio/file_init.c b/library/stdio/file_init.c index 655a89c0..2f8bff2a 100644 --- a/library/stdio/file_init.c +++ b/library/stdio/file_init.c @@ -184,9 +184,9 @@ FILE_CONSTRUCTOR(stdio_file_init) { case STDERR_FILENO: default: - iob_flags = IOBF_IN_USE | IOBF_WRITE | IOBF_NO_NUL | IOBF_BUFFER_MODE_NONE; - fd_flags = FDF_IN_USE | FDF_WRITE; - default_file = BZERO; + iob_flags = IOBF_IN_USE | IOBF_WRITE | IOBF_NO_NUL | IOBF_BUFFER_MODE_LINE; + fd_flags = FDF_IN_USE | FDF_WRITE | FDF_NO_CLOSE | FDF_IS_INTERACTIVE; + default_file = ErrorOutput(); break; } diff --git a/library/stdio/flockfile.c b/library/stdio/flockfile.c index c8f6f4a4..f5af24f9 100644 --- a/library/stdio/flockfile.c +++ b/library/stdio/flockfile.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_flockfile.c,v 1.4 2006-01-08 12:04:24 clib4devs Exp $ + * $Id: stdio_flockfile.c,v 1.5 2024-06-19 12:04:24 clib4devs Exp $ */ #ifndef _STDIO_HEADERS_H @@ -10,11 +10,6 @@ void flockfile(FILE *stream) { struct iob *file = (struct iob *) stream; - ENTER(); - SHOWPOINTER(stream); - - assert(stream != NULL); - __check_abort(); if (stream == NULL) { @@ -39,6 +34,5 @@ flockfile(FILE *stream) { } out: - LEAVE(); return; } diff --git a/library/stdio/init_exit.c b/library/stdio/init_exit.c index c3e588a1..a6bca432 100644 --- a/library/stdio/init_exit.c +++ b/library/stdio/init_exit.c @@ -29,22 +29,26 @@ __close_all_files(struct _clib4 *__clib4) { if (__clib4->__num_iob > 0) { for (i = 0; i < __clib4->__num_iob; i++) { if (FLAG_IS_SET(__clib4->__iob[i]->iob_Flags, IOBF_IN_USE)) { + D(("Close __iob %ld\n", i)); fclose((FILE *) __clib4->__iob[i]); + __clib4->__num_iob--; } } - - __clib4->__num_iob = 0; } if (__clib4->__num_fd > 0) { for (i = 0; i < __clib4->__num_fd; i++) { /* If file is set as in use close it only if it isn't marked as FDF_NO_CLOSE */ if (FLAG_IS_SET(__clib4->__fd[i]->fd_Flags, FDF_IN_USE) && FLAG_IS_CLEAR(__clib4->__fd[i]->fd_Flags, FDF_NO_CLOSE)) { + D(("Close __fd %ld\n", i)); close(i); + UnlockMem(__clib4->__fd[i], sizeof(*__clib4->__fd[i])); + __clib4->__num_fd--; + } + else { + D(("Can't close __fd %d FDF_IN_USE=%d FDF_NO_CLOSE=%d \n", i, FLAG_IS_SET(__clib4->__fd[i]->fd_Flags, FDF_IN_USE), FLAG_IS_SET(__clib4->__fd[i]->fd_Flags, FDF_NO_CLOSE))); } - UnlockMem(__clib4->__fd[i], sizeof(*__clib4->__fd[i])); } - __clib4->__num_fd = 0; } __stdio_unlock(__clib4); diff --git a/library/stdlib/main.c b/library/stdlib/main.c index df26b362..c7648809 100644 --- a/library/stdlib/main.c +++ b/library/stdlib/main.c @@ -336,11 +336,14 @@ _main( SHOWMSG("Calling reent_exit on _clib4"); reent_exit(__clib4, FALSE); + SHOWMSG("Done"); if (sms) { Forbid(); ReplyMsg(&sms->sm_Message); } + SHOWMSG("Exit from _main"); + return rc; }