From e70b4248262f9d4af3933828c0b0ef59bc4d8420 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Fri, 2 Feb 2018 14:37:49 +0000 Subject: [PATCH] enhanced the vm cleanup to be more error resistent --- moo/configure.ac | 1 + moo/lib/exec.c | 22 ++++++++++++++++------ moo/lib/logfmt.c | 7 ++++++- moo/lib/main.c | 20 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/moo/configure.ac b/moo/configure.ac index ad9d667..c3787b5 100644 --- a/moo/configure.ac +++ b/moo/configure.ac @@ -174,6 +174,7 @@ AC_CHECK_FUNCS([makecontext swapcontext getcontext setcontext]) AC_CHECK_FUNCS([clock_nanosleep nanosleep usleep]) AC_CHECK_FUNCS([snprintf _vsnprintf _vsnwprintf strerror_r]) AC_CHECK_FUNCS([accept4]) +AC_CHECK_FUNCS([isatty]) AC_CHECK_LIB([dyncall_s], [dcNewCallVM], [ diff --git a/moo/lib/exec.c b/moo/lib/exec.c index 2123b8c..d386d2d 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -125,7 +125,7 @@ static MOO_INLINE const char* proc_state_to_string (int state) # define __PRIMITIVE_NAME__ (&__FUNCTION__[0]) #endif -static int delete_from_sem_io (moo_t* moo, moo_oop_semaphore_t sem); +static int delete_from_sem_io (moo_t* moo, moo_oop_semaphore_t sem, int force); static void signal_io_semaphore (moo_t* moo, moo_ooi_t io_handle, moo_ooi_t mask); static int send_message (moo_t* moo, moo_oop_char_t selector, int to_super, moo_ooi_t nargs); static int send_message_with_str (moo_t* moo, const moo_ooch_t* nameptr, moo_oow_t namelen, int to_super, moo_ooi_t nargs); @@ -168,12 +168,14 @@ static MOO_INLINE void vm_cleanup (moo_t* moo) if (moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_INPUT]) { - delete_from_sem_io (moo, moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_INPUT]); + delete_from_sem_io (moo, moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_INPUT], 1); } if (moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_OUTPUT]) { - delete_from_sem_io (moo, moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_OUTPUT]); + delete_from_sem_io (moo, moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_OUTPUT], 1); } + + MOO_ASSERT (moo, moo->sem_io_map[i] <= -1); } else { @@ -1247,7 +1249,7 @@ static int add_to_sem_io (moo_t* moo, moo_oop_semaphore_t sem, moo_ooi_t io_hand return 0; } -static int delete_from_sem_io (moo_t* moo, moo_oop_semaphore_t sem) +static int delete_from_sem_io (moo_t* moo, moo_oop_semaphore_t sem, int force) { moo_ooi_t index; moo_ooi_t new_mask, io_handle, io_type; @@ -1280,7 +1282,15 @@ static int delete_from_sem_io (moo_t* moo, moo_oop_semaphore_t sem) if (x <= -1) { MOO_LOG3 (moo, MOO_LOG_WARN, "Failed to delete an IO semaphore at index %zd of type %d on handle %zd\n", index, (int)io_type, io_handle); - return -1; + if (!force) return -1; + + /* NOTE: + * this means there could be some issue handling the file handles. + * the file handle might have been closed before reaching here. + * assuming the callback works correctly, it's not likely that the + * underlying operating system returns failure for no reason. + * i should inspect the overall vm implementation */ + MOO_LOG3 (moo, MOO_LOG_ERROR, "Forcibly unmapping the handle %zd despite failure\n", index, (int)io_type, io_handle); } MOO_LOG3 (moo, MOO_LOG_DEBUG, "Deleted an IO semaphore at index %zd of type %d on handle %zd\n", index, (int)io_type, io_handle); @@ -2613,7 +2623,7 @@ static moo_pfrc_t pf_system_remove_semaphore (moo_t* moo, moo_ooi_t nargs) MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_handle) && MOO_OOP_TO_SMOOI(sem->io_handle) >= 0); MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_type)); - if (delete_from_sem_io (moo, sem) <= -1) + if (delete_from_sem_io(moo, sem, 0) <= -1) { const moo_ooch_t* oldmsg = moo_backuperrmsg(moo); moo_seterrbfmt (moo, moo->errnum, "cannot delete the handle %zd from the multiplexer - %js", MOO_OOP_TO_SMOOI(sem->io_handle), oldmsg); diff --git a/moo/lib/logfmt.c b/moo/lib/logfmt.c index 05b9d9b..2a30c7f 100644 --- a/moo/lib/logfmt.c +++ b/moo/lib/logfmt.c @@ -614,7 +614,12 @@ moo_ooi_t moo_logbfmt (moo_t* moo, moo_oow_t mask, const moo_bch_t* fmt, ...) if (moo->log.default_type_mask & MOO_LOG_ALL_TYPES) { - mask &= ~MOO_LOG_UNTYPED; + /* if a type is given, it's not untyped any more. + * mask off the UNTYPED bit */ + mask &= ~MOO_LOG_UNTYPED; + + /* if the default_type_mask has the UNTYPED bit on, + * it'll get turned back on */ mask |= (moo->log.default_type_mask & MOO_LOG_ALL_TYPES); } diff --git a/moo/lib/main.c b/moo/lib/main.c index fbbfef5..fdad514 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -197,6 +197,7 @@ struct xtn_t int logfd; int logmask; + int logfd_istty; #if defined(_WIN32) HANDLE waitable_timer; @@ -777,6 +778,14 @@ static void log_write (moo_t* moo, moo_oow_t mask, const moo_ooch_t* msg, moo_oo write_all (logfd, ts, tslen); } + + if (xtn->logfd_istty) + { + if (mask & MOO_LOG_FATAL) write_all (logfd, "\x1B[1;31m", 7); + else if (mask & MOO_LOG_ERROR) write_all (logfd, "\x1B[1;36m", 7); + else if (mask & MOO_LOG_WARN) write_all (logfd, "\x1B[1;33m", 7); + } + #if defined(MOO_OOCH_IS_UCH) msgidx = 0; while (len > 0) @@ -815,6 +824,11 @@ static void log_write (moo_t* moo, moo_oow_t mask, const moo_ooch_t* msg, moo_oo write_all (logfd, msg, len); #endif + if (xtn->logfd_istty) + { + if (mask & (MOO_LOG_FATAL | MOO_LOG_ERROR | MOO_LOG_WARN)) write_all (logfd, "\x1B[0m", 4); + } + #endif } @@ -2022,6 +2036,7 @@ static void close_moo (moo_t* moo) { close (xtn->logfd); xtn->logfd = -1; + xtn->logfd_istty = 0; } moo_close (moo); } @@ -2091,6 +2106,10 @@ static int handle_logopt (moo_t* moo, const moo_bch_t* str) return -1; } +#if defined(HAVE_ISATTY) + xtn->logfd_istty = isatty(xtn->logfd)) +#endif + if (str != xstr) moo_freemem (moo, xstr); return 0; } @@ -2263,6 +2282,7 @@ int main (int argc, char* argv[]) xtn = moo_getxtn (moo); xtn->logfd = -1; + xtn->logfd_istty = 0; if (logopt) {