[Git][NTPsec/ntpsec][master] 3 commits: mysyslog: enable early logging to stderr by default.

Gary E. Miller gitlab at mg.gitlab.com
Fri Jun 2 23:44:38 UTC 2017


Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
410adf67 by Gary E. Miller at 2017-06-02T15:25:45-07:00
mysyslog: enable early logging to stderr by default.

As before, no log to stderr after fork.  Early mysyslog() output
only showed up in the syslog and never got seen.

- - - - -
f32540d5 by Gary E. Miller at 2017-06-02T16:36:54-07:00
assert: move the few parts of backtrace.c that work into assert.c

Next libisc/backtrace.c goes bye-bye.

- - - - -
be8edf18 by Gary E. Miller at 2017-06-02T16:40:14-07:00
assert: remove unused libisc backtrace.[ch]

Good riddance, prolly never worked, replaced with better code
in libntp/assert.c

- - - - -


6 changed files:

- − libisc/backtrace.c
- − libisc/include/isc/backtrace.h
- libisc/wscript
- libntp/assert.c
- libntp/msyslog.c
- ntpd/ntpd.c


Changes:

=====================================
libisc/backtrace.c deleted
=====================================
--- a/libisc/backtrace.c
+++ /dev/null
@@ -1,120 +0,0 @@
-/*
- * Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
- * Copyright 2015 by the NTPsec project contributors
- * SPDX-License-Identifier: ISC
- */
-
-/*! \file */
-
-#include "config.h"
-
-#include <string.h>
-#include <stdlib.h>
-
-#include "isc/backtrace.h"
-#include "isc/result.h"
-#include "isc/util.h"
-
-#ifdef USEBACKTRACE
-/*
- * Getting a back trace of a running process is tricky and highly platform
- * dependent.  Our current approach is as follows:
- * 1. If the system library supports the "backtrace()" function, use it.
- *    OS X support this starting at with SDK 10.5.  glibc since version 2.1
- * 2. Otherwise, if unwind.h exists then use the __Unwind_Backtrace() function.
- *    This function is available on Linux, OS X, and FreeBSD.  It is defined
- *    in Linux Standard Base since version 4.1
- * 3. Otherwise, tough luck.
- */
-#ifdef HAVE_BACKTRACE_SYMBOLS_FD
-#define BACKTRACE_LIBC
-#elif defined(HAVE__UNWIND_BACKTRACE)
-#define BACKTRACE_UNWIND
-#else
-#define BACKTRACE_DISABLED
-#endif  /* HAVE_BACKTRACE_SYMBOLS_FD */
-#else	/* !USEBACKTRACE */
-#define BACKTRACE_DISABLED
-#endif	/* USEBACKTRACE */
-
-#ifdef BACKTRACE_LIBC
-#include <execinfo.h>
-
-isc_result_t
-isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
-	int n;
-
-	/*
-	 * Validate the arguments: intentionally avoid using REQUIRE().
-	 * See notes in backtrace.h.
-	 */
-	if (addrs == NULL || nframes == NULL)
-		return (ISC_R_FAILURE);
-
-	/*
-	 * backtrace(3) includes this function itself in the address array,
-	 * which should be eliminated from the returned sequence.
-	 */
-	n = backtrace(addrs, maxaddrs);
-	if (n < 2)
-		return (ISC_R_NOTFOUND);
-	n--;
-	memmove(addrs, &addrs[1], sizeof(void *) * (size_t)n);
-	*nframes = n;
-	return (ISC_R_SUCCESS);
-}
-#elif defined(BACKTRACE_UNWIND)
-#include <unwind.h>
-
-typedef struct {
-	void **result;
-	int max_depth;
-	int skip_count;
-	int count;
-} trace_arg_t;
-
-static _Unwind_Reason_Code
-btcallback(struct _Unwind_Context *uc, void *opq) {
-	trace_arg_t *arg = (trace_arg_t *)opq;
-
-	if (arg->skip_count > 0)
-		arg->skip_count--;
-	else
-		arg->result[arg->count++] = (void *)_Unwind_GetIP(uc);
-	if (arg->count == arg->max_depth)
-		return (5); /* _URC_END_OF_STACK */
-
-	return (0); /* _URC_NO_REASON */
-}
-
-isc_result_t
-isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
-	trace_arg_t arg;
-
-	/* Argument validation: see above. */
-	if (addrs == NULL || nframes == NULL)
-		return (ISC_R_FAILURE);
-
-	arg.skip_count = 1;
-	arg.result = addrs;
-	arg.max_depth = maxaddrs;
-	arg.count = 0;
-	_Unwind_Backtrace(btcallback, &arg);
-
-	*nframes = arg.count;
-
-	return (ISC_R_SUCCESS);
-}
-#elif defined(BACKTRACE_DISABLED)
-isc_result_t
-isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
-	/* Argument validation: see above. */
-	if (addrs == NULL || nframes == NULL)
-		return (ISC_R_FAILURE);
-
-	UNUSED(maxaddrs);
-
-	return (ISC_R_NOTIMPLEMENTED);
-}
-#endif
-


=====================================
libisc/include/isc/backtrace.h deleted
=====================================
--- a/libisc/include/isc/backtrace.h
+++ /dev/null
@@ -1,86 +0,0 @@
-/*! \file isc/backtrace.h
- * \brief provide a back trace of the running process to help debug problems.
- *
- * This module tries to get a back trace of the process using some platform
- * dependent way when available.  It also manages an internal symbol table
- * that maps function addresses used in the process to their textual symbols.
- * This module is expected to be used to help debug when some fatal error
- * happens.
- *
- * IMPORTANT NOTE: since the (major) intended use case of this module is
- * dumping a back trace on a fatal error, normally followed by self termination,
- * functions defined in this module generally doesn't employ assertion checks
- * (if it did, a program bug could cause infinite recursive calls to a
- * backtrace function).  These functions still perform minimal checks and return
- * ISC_R_FAILURE if they detect an error, but the caller should therefore be
- * very careful about the use of these functions, and generally discouraged to
- * use them except in an exit path.
- *
- * Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
- * Copyright 2015 by the NTPsec project contributors
- * SPDX-License-Identifier: ISC
- */
-
-#ifndef GUARD_ISC_BACKTRACE_H
-#define GUARD_ISC_BACKTRACE_H 1
-
-/***
- ***	Imports
- ***/
-
-#include "isc/types.h"
-
-/***
- *** Types
- ***/
-struct isc_backtrace_symmap {
-	void		*addr;
-	const char	*symbol;
-};
-
-/***
- *** Functions
- ***/
-
-isc_result_t
-isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes)
-		       __attribute__((const));
-/*%<
- * Get a back trace of the running process above this function itself.  On
- * success, addrs[i] will store the address of the call point of the i-th
- * stack frame (addrs[0] is the caller of this function).  *nframes will store
- * the total number of frames.
- *
- * Requires (note that these are not ensured by assertion checks, see above):
- *
- *\li	'addrs' is a valid array containing at least 'maxaddrs' void * entries.
- *
- *\li	'nframes' must be non NULL.
- *
- * Returns:
- *
- *\li	#ISC_R_SUCCESS
- *\li	#ISC_R_FAILURE
- *\li	#ISC_R_NOTFOUND
- *\li	#ISC_R_NOTIMPLEMENTED
- */
-
-/*%<
- * Returns the content of the internal symbol table of the given index.
- * On success, *addrsp and *symbolp point to the address and the symbol of
- * the 'index'th entry of the table, respectively.  If 'idx' is not in the
- * range of the symbol table, ISC_R_RANGE will be returned.
- *
- * Requires
- *
- *\li	'addrp' must be non NULL && '*addrp' == NULL.
- *
- *\li	'symbolp' must be non NULL && '*symbolp' == NULL.
- *
- * Returns:
- *
- *\li	#ISC_R_SUCCESS
- *\li	#ISC_R_RANGE
- */
-
-#endif	/* GUARD_ISC_BACKTRACE_H */


=====================================
libisc/wscript
=====================================
--- a/libisc/wscript
+++ b/libisc/wscript
@@ -2,7 +2,6 @@
 def build(ctx):
 
     libisc_source = [
-        "backtrace.c",
         "error.c",
         "netaddr.c",
         "interfaceiter.c",


=====================================
libntp/assert.c
=====================================
--- a/libntp/assert.c
+++ b/libntp/assert.c
@@ -16,9 +16,104 @@
 #include "ntp_debug.h"
 #include "ntp_syslog.h"
 #include "ntp_assert.h"
-#include "isc/backtrace.h"
 #include "isc/result.h"
 
+#ifdef USEBACKTRACE
+
+/*
+ * Getting a back trace of a running process is tricky and highly platform
+ * dependent.  Our current approach is as follows:
+ * 1. If the system library supports the "backtrace()" function, use it.
+ *    OS X support this starting at with SDK 10.5.  glibc since version 2.1
+ * 2. Otherwise, if unwind.h exists then use the __Unwind_Backtrace() function.
+ *    This function is available on Linux, OS X, and FreeBSD.  It is defined
+ *    in Linux Standard Base since version 4.1
+ * 3. Otherwise, tough luck.
+ */
+
+/*
+ * The maximum number of stack frames to dump on assertion failure.
+ */
+#ifndef BACKTRACE_MAXFRAME
+#define BACKTRACE_MAXFRAME 128
+#endif
+
+
+# ifdef HAVE_BACKTRACE_SYMBOLS_FD
+#include <execinfo.h>
+
+void backtrace_log(void);
+
+void
+backtrace_log(void) {
+	int j, nptrs;
+	void *buffer[BACKTRACE_MAXFRAME];
+	char **strings;
+
+	nptrs = backtrace(buffer, BACKTRACE_MAXFRAME);
+	strings = backtrace_symbols(buffer, nptrs);
+	msyslog(LOG_ERR, "Stack trace:\n");
+	if (strings) {
+	   /* skip trace of this shim function */
+	   for (j = 1; j < nptrs; j++)
+	       msyslog(LOG_ERR, "  %s\n", strings[j]);
+
+	   free(strings);
+	}
+}
+# elif defined(HAVE__UNWIND_BACKTRACE)
+#include <unwind.h>
+
+typedef struct {
+	void **result;
+	int max_depth;
+	int skip_count;
+	int count;
+} trace_arg_t;
+
+static _Unwind_Reason_Code
+btcallback(struct _Unwind_Context *uc, void *opq) {
+	trace_arg_t *arg = (trace_arg_t *)opq;
+
+	if (arg->skip_count > 0)
+		arg->skip_count--;
+	else
+		arg->result[arg->count++] = (void *)_Unwind_GetIP(uc);
+	if (arg->count == arg->max_depth)
+		return (5); /* _URC_END_OF_STACK */
+
+	return (0); /* _URC_NO_REASON */
+}
+
+isc_result_t backtrace_log(void);
+
+isc_result_t
+backtrace_log(void) {
+	trace_arg_t arg;
+	void *buffer[BACKTRACE_MAXFRAME];
+	int i;
+
+	arg.skip_count = 1;
+	arg.result = buffer;
+	arg.max_depth = BACKTRACE_MAXFRAME;
+	arg.count = 0;
+	_Unwind_Backtrace(btcallback, &arg);
+
+	msyslog(LOG_ERR, "Stack trace:\n");
+	/* skip trace of this shim function */
+	for (i = 1; i < arg.count; i++) {
+	    msyslog(LOG_ERR, "#%d %p in ??\n", i, buffer[i]);
+	}
+
+	return (ISC_R_SUCCESS);
+}
+# else
+#  define(BACKTRACE_DISABLED)
+# endif
+#else   /* ! USEBACKTRACE */
+# define(BACKTRACE_DISABLED)
+#endif /* USEBACKTRACE */
+
 static const char *
 assertion_typetotext(assertiontype_t type)
 			__attribute__((const));
@@ -69,6 +164,9 @@ assertion_failed(
 
 	msyslog(LOG_ERR, "%s:%d: %s(%s) failed",
 		file, line, assertion_typetotext(type), cond);
+#ifndef BACKTRACE_DISABLED
+        backtrace_log();
+#endif
 	msyslog(LOG_ERR, "exiting (due to assertion failure)");
 
 	abort();


=====================================
libntp/msyslog.c
=====================================
--- a/libntp/msyslog.c
+++ b/libntp/msyslog.c
@@ -16,8 +16,9 @@
 #include "ntp_debug.h"
 #include "ntp_syslog.h"
 
-bool	syslogit = true;	/* log messages to syslog */
-bool	termlogit = false;	/* duplicate to stdout/err */
+/* start out with syslog and stderr, otherwise startup errors lost */
+bool    syslogit = true;        /* log messages to syslog */
+bool    termlogit = true;       /* duplicate to stdout/err */
 bool	termlogit_pid = true;
 bool	msyslog_include_timestamp = true;
 FILE *	syslog_file;


=====================================
ntpd/ntpd.c
=====================================
--- a/ntpd/ntpd.c
+++ b/ntpd/ntpd.c
@@ -585,6 +585,7 @@ ntpdmain(
 	    /* -w requires a fork() even with debug > 0 */
 	    nofork = false;
 	    if (pipe(pipe_fds)) {
+		termlogit = true;
 		exit_code = (errno) ? errno : -1;
 		msyslog(LOG_ERR,
 			"Pipe creation failed for --wait-sync: %m");
@@ -620,6 +621,7 @@ ntpdmain(
 		 * close all open files excepting waitsync_fd_to_close.
 		 * msyslog() unreliable until after init_logging().
 		 */
+		termlogit = false;    /* do not use stderr after fork */
 		closelog();
 		if (syslog_file != NULL) {
 			fclose(syslog_file);



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/cb80377cf80dc32c335126a85ce5da342240274d...be8edf18080ce7b428a03500fd59095ee86bb052

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/cb80377cf80dc32c335126a85ce5da342240274d...be8edf18080ce7b428a03500fd59095ee86bb052
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170602/8902ed1c/attachment.html>


More information about the vc mailing list