A real shared library should never use assert to detect an error,
but handle the error and report it to the user.
logsys.h:
drop all asserts within DECLARE. Those are executed always before main
and before an application is forked. It is safe to use fprintf and exit
here to report init errors. Also note that while a library should never
exit, the DECLARE_ macros are builtin within the application where the
use of exit is legitimate.
util.c:
needs to explicitly include assert.h that was sucked in via logsys.h
logsys.c:
- handle cutoff properly. cutoff is either == -1 or >= 0. If we have no
cutoff or cutoff is explicitly set to -1, then copy. Stop asserting as
there is no reason to.
- don't assert if _logsys_subsys_create is invoked with a NULL subsystem
but return error. Do the same if we cannot allocate a subsystem.
- don't assert if we have to parse more than 64 arguments, but simply
cutoff the parsing there. the function is void and there is no real easy
way to report an error there.
main.c:
check error returned from _logsys_subsys_create for IPC and TOTEM and
take proper action.
Fabio
Index: include/corosync/engine/logsys.h
===================================================================
--- include/corosync/engine/logsys.h (revision 2118)
+++ include/corosync/engine/logsys.h (working copy)
@@ -38,8 +38,8 @@
#define LOGSYS_H_DEFINED
#include <stdarg.h>
+#include <stdlib.h>
#include <syslog.h>
-#include <assert.h>
/*
* All of the LOGSYS_MODE's can be ORed together for combined behavior
@@ -254,20 +254,30 @@
__attribute__ ((constructor)) \
static void logsys_system_init (void) \
{ \
- int err; \
+ if (_logsys_system_setup (name,mode,debug,file,file_priority, \
+ syslog_facility,syslog_priority,tags) < 0) { \
+ fprintf (stderr, \
+ "Unable to setup logging system: %s.\n", name); \
+ exit (-1); \
+ } \
\
- err = _logsys_system_setup (name,mode,debug,file,file_priority, \
- syslog_facility,syslog_priority,tags); \
- assert (err == 0 && "_logsys_system_setup failed"); \
+ if (logsys_format_set (format) < 0) { \
+ fprintf (stderr, \
+ "Unable to setup logging format.\n"); \
+ exit (-1); \
+ } \
\
- err = logsys_format_set (format); \
- assert (err == 0 && "logsys_format_set failed"); \
+ if (_logsys_rec_init (rec_size) < 0) { \
+ fprintf (stderr, \
+ "Unable to initialize log fligh recoreder.\n"); \
+ exit (-1); \
+ } \
\
- err = _logsys_rec_init (rec_size); \
- assert (err == 0 && "_logsys_rec_init failed"); \
- \
- err = _logsys_wthread_create(); \
- assert (err == 0 && "_logsys_wthread_create failed"); \
+ if (_logsys_wthread_create() < 0) { \
+ fprintf (stderr, \
+ "Unable to initialize logging thread.\n"); \
+ exit (-1); \
+ } \
}
#define LOGSYS_DECLARE_SUBSYS(subsys) \
@@ -276,8 +286,11 @@
{ \
logsys_subsys_id = \
_logsys_subsys_create ((subsys)); \
- assert (logsys_subsys_id < LOGSYS_MAX_SUBSYS_COUNT && \
- "_logsys_subsys_create failed"); \
+ if (logsys_subsys_id == -1) { \
+ fprintf (stderr, \
+ "Unable to create logging subsystem: %s.\n", subsys); \
+ exit (-1); \
+ } \
}
#define log_rec(rec_ident, args...) \
Index: exec/util.c
===================================================================
--- exec/util.c (revision 2118)
+++ exec/util.c (working copy)
@@ -41,6 +41,7 @@
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>
+#include <assert.h>
#include <corosync/corotypes.h>
#include <corosync/list.h>
Index: exec/logsys.c
===================================================================
--- exec/logsys.c (revision 2118)
+++ exec/logsys.c (working copy)
@@ -37,7 +37,6 @@
#include <config.h>
-#include <assert.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
@@ -278,11 +277,10 @@
{
unsigned int len;
- if (cutoff == -1) {
+ if (cutoff <= 0) {
strcpy (dest, src);
return (strlen (dest));
} else {
- assert (cutoff > 0);
strncpy (dest, src, cutoff);
dest[cutoff] = '\0';
len = strlen (dest);
@@ -839,7 +837,9 @@
{
int i;
- assert (subsys != NULL);
+ if (subsys == NULL) {
+ return -1;
+ }
pthread_mutex_lock (&logsys_config_mutex);
@@ -856,7 +856,9 @@
}
}
- assert(i < LOGSYS_MAX_SUBSYS_COUNT);
+ if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
+ i = -1;
+ }
pthread_mutex_unlock (&logsys_config_mutex);
return i;
@@ -935,7 +937,6 @@
va_start (ap, rec_ident);
arguments = 3;
for (;;) {
- assert (arguments < 64);
buf_args[arguments] = va_arg (ap, void *);
if (buf_args[arguments] == LOGSYS_REC_END) {
break;
@@ -943,6 +944,9 @@
buf_len[arguments] = va_arg (ap, int);
record_reclaim_size += ((buf_len[arguments] + 3) >> 2) + 1;
arguments++;
+ if (arguments >= 64) {
+ break;
+ }
}
va_end (ap);
Index: exec/main.c
===================================================================
--- exec/main.c (revision 2118)
+++ exec/main.c (working copy)
@@ -869,6 +869,13 @@
totem_config.totem_logging_configuration = totem_logging_configuration;
totem_config.totem_logging_configuration.log_subsys_id =
_logsys_subsys_create ("TOTEM");
+
+ if (totem_config.totem_logging_configuration.log_subsys_id < 0) {
+ log_printf (LOGSYS_LEVEL_ERROR,
+ "Unable to initialize TOTEM logging subsystem\n");
+ corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
+ }
+
totem_config.totem_logging_configuration.log_level_security = LOGSYS_LEVEL_SECURITY;
totem_config.totem_logging_configuration.log_level_error = LOGSYS_LEVEL_ERROR;
totem_config.totem_logging_configuration.log_level_warning = LOGSYS_LEVEL_WARNING;
@@ -937,6 +944,11 @@
serialize_unlock);
ipc_subsys_id = _logsys_subsys_create ("IPC");
+ if (ipc_subsys_id < 0) {
+ log_printf (LOGSYS_LEVEL_ERROR,
+ "Could not initialize IPC logging subsystem\n");
+ corosync_exit_error (AIS_DONE_INIT_SERVICES);
+ }
ipc_init_state.sched_priority = sched_priority;
_______________________________________________
Openais mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/openais