debug.c revision 9a990aa9f7e8c105e0cfeea8d8cbdc776c2d5d7a
2353f65833a3da763392f771223250cd50b8d873Christian Maeder/*
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder Authors:
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder Simo Sorce <ssorce@redhat.com>
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian Maeder Stephen Gallagher <sgallagh@redhat.com>
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b4fbc96e05117839ca409f5f20f97b3ac872d1edTill Mossakowski Copyright (C) 2009 Red Hat
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder This program is free software; you can redistribute it and/or modify
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder it under the terms of the GNU General Public License as published by
f3a94a197960e548ecd6520bb768cb0d547457bbChristian Maeder the Free Software Foundation; either version 3 of the License, or
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder (at your option) any later version.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder This program is distributed in the hope that it will be useful,
2353f65833a3da763392f771223250cd50b8d873Christian Maeder but WITHOUT ANY WARRANTY; without even the implied warranty of
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder GNU General Public License for more details.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder You should have received a copy of the GNU General Public License
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder along with this program. If not, see <http://www.gnu.org/licenses/>.
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder*/
4b1833c7d3af466e6bcba24f16304e0a78e8da87Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#include "config.h"
fbc7d11880751ef87862b1f4650b16c01c6763f1Klaus Luettich
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#include <stdio.h>
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#include <stdarg.h>
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#include <stdlib.h>
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder#include <fcntl.h>
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#include <sys/types.h>
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#include <sys/stat.h>
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#include <sys/time.h>
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#ifdef WITH_JOURNALD
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#include <systemd/sd-journal.h>
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#endif
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
25a0b76bc87e80c0f697951d9817862755a71d33Christian Maeder#include "util/util.h"
25a0b76bc87e80c0f697951d9817862755a71d33Christian Maeder
25a0b76bc87e80c0f697951d9817862755a71d33Christian Maederconst char *debug_prg_name = "sssd";
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint debug_level = SSSDBG_UNRESOLVED;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint debug_timestamps = SSSDBG_TIMESTAMP_UNRESOLVED;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint debug_microseconds = SSSDBG_MICROSECONDS_UNRESOLVED;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maederint debug_to_file = 0;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint debug_to_stderr = 0;
3a9d784341454573b50b32fa1b494e7418df3086Christian Maederconst char *debug_log_file = "sssd";
b53688bfed888214b485cf76439d57262d80e0a7Christian MaederFILE *debug_file = NULL;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maedererrno_t set_debug_file_from_fd(const int fd)
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder{
fbc7d11880751ef87862b1f4650b16c01c6763f1Klaus Luettich FILE *dummy;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder errno_t ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder errno = 0;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder dummy = fdopen(fd, "a");
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (dummy == NULL) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ret = errno;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder "fdopen failed [%d][%s].\n", ret, strerror(ret));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder sss_log(SSS_LOG_ERR,
4b1833c7d3af466e6bcba24f16304e0a78e8da87Christian Maeder "Could not open debug file descriptor [%d]. "
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder "Debug messages will not be written to the file "
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder "for this child process [%s][%s]\n",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder fd, debug_prg_name, strerror(ret));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_file = dummy;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return EOK;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint debug_convert_old_level(int old_level)
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if ((old_level != 0) && !(old_level & 0x000F))
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return old_level;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int new_level = SSSDBG_FATAL_FAILURE;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (old_level <= 0)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return new_level;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (old_level >= 1)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder new_level |= SSSDBG_CRIT_FAILURE;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (old_level >= 2)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder new_level |= SSSDBG_OP_FAILURE;
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (old_level >= 3)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder new_level |= SSSDBG_MINOR_FAILURE;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (old_level >= 4)
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder new_level |= SSSDBG_CONF_SETTINGS;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (old_level >= 5)
2353f65833a3da763392f771223250cd50b8d873Christian Maeder new_level |= SSSDBG_FUNC_DATA;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (old_level >= 6)
2353f65833a3da763392f771223250cd50b8d873Christian Maeder new_level |= SSSDBG_TRACE_FUNC;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (old_level >= 7)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder new_level |= SSSDBG_TRACE_LIBS;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (old_level >= 8)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder new_level |= SSSDBG_TRACE_INTERNAL;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder if (old_level >= 9)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder new_level |= SSSDBG_TRACE_ALL;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maeder return new_level;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder}
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maederstatic void debug_fflush(void)
2353f65833a3da763392f771223250cd50b8d873Christian Maeder{
2353f65833a3da763392f771223250cd50b8d873Christian Maeder fflush(debug_file ? debug_file : stderr);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maederstatic void debug_vprintf(const char *format, va_list ap)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder vfprintf(debug_file ? debug_file : stderr, format, ap);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstatic void debug_printf(const char *format, ...)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder SSS_ATTRIBUTE_PRINTF(1, 2);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederstatic void debug_printf(const char *format, ...)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder va_list ap;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder va_start(ap, format);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_vprintf(format, ap);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder va_end(ap);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#ifdef WITH_JOURNALD
b53688bfed888214b485cf76439d57262d80e0a7Christian Maedererrno_t journal_send(const char *file,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder long line,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *function,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int level,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *format,
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder va_list ap)
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder{
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder errno_t ret;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder int res;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder char *message = NULL;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder char *code_file = NULL;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder char *code_line = NULL;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder const char *domain;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder /* First, evaluate the message to be sent */
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder ret = vasprintf(&message, format, ap);
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder if (ret == -1) {
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder /* ENOMEM, just return */
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder return ENOMEM;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder res = asprintf(&code_file, "CODE_FILE=%s", file);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (res == -1) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ENOMEM;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder goto journal_done;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder res = asprintf(&code_line, "CODE_LINE=%ld", line);
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder if (res == -1) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = ENOMEM;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder goto journal_done;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* If this log message was sent from a provider,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * track the domain.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder */
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder domain = getenv(SSS_DOM_ENV);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (domain == NULL) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder domain = "";
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* Send the log message to journald, specifying the
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * source code location and other tracking data.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder res = sd_journal_send_with_location(
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder code_file, code_line, function,
2353f65833a3da763392f771223250cd50b8d873Christian Maeder "MESSAGE=%s", message,
2353f65833a3da763392f771223250cd50b8d873Christian Maeder "PRIORITY=%i", LOG_DEBUG,
2353f65833a3da763392f771223250cd50b8d873Christian Maeder "SSSD_DOMAIN=%s", domain,
2353f65833a3da763392f771223250cd50b8d873Christian Maeder "SSSD_PRG_NAME=%s", debug_prg_name,
2353f65833a3da763392f771223250cd50b8d873Christian Maeder "SSSD_DEBUG_LEVEL=%x", level,
2353f65833a3da763392f771223250cd50b8d873Christian Maeder NULL);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder ret = -res;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
2353f65833a3da763392f771223250cd50b8d873Christian Maederjournal_done:
2353f65833a3da763392f771223250cd50b8d873Christian Maeder free(code_line);
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder free(code_file);
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder free(message);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder return ret;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#endif /* WiTH_JOURNALD */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maedervoid debug_fn(const char *file,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder long line,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *function,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int level,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *format, ...)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder va_list ap;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct timeval tv;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder struct tm *tm;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder char datetime[20];
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int year;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#ifdef WITH_JOURNALD
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder errno_t ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder va_list ap_fallback;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!debug_file && !debug_to_stderr) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* If we are not outputting logs to files, we should be sending them
2353f65833a3da763392f771223250cd50b8d873Christian Maeder * to journald.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * NOTE: on modern systems, this is where stdout/stderr will end up
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * from system services anyway. The only difference here is that we
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * can also provide extra structuring data to make it more easily
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * searchable.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder va_start(ap, format);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder va_copy(ap_fallback, ap);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = journal_send(file, line, function, level, format, ap);
4b1833c7d3af466e6bcba24f16304e0a78e8da87Christian Maeder va_end(ap);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != EOK) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* Emergency fallback, send to STDERR */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_vprintf(format, ap_fallback);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_fflush();
2353f65833a3da763392f771223250cd50b8d873Christian Maeder }
2353f65833a3da763392f771223250cd50b8d873Christian Maeder va_end(ap_fallback);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder#endif
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (debug_timestamps) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder gettimeofday(&tv, NULL);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder tm = localtime(&tv.tv_sec);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder year = tm->tm_year + 1900;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* get date time without year */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder memcpy(datetime, ctime(&tv.tv_sec), 19);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder datetime[19] = '\0';
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (debug_microseconds) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_printf("(%s:%.6ld %d) [%s] [%s] (%#.4x): ",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder datetime, tv.tv_usec,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder year, debug_prg_name,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder function, level);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder } else {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_printf("(%s %d) [%s] [%s] (%#.4x): ",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder datetime, year,
4b1833c7d3af466e6bcba24f16304e0a78e8da87Christian Maeder debug_prg_name, function, level);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder } else {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_printf("[%s] [%s] (%#.4x): ",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_prg_name, function, level);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder va_start(ap, format);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_vprintf(format, ap);
3a9d784341454573b50b32fa1b494e7418df3086Christian Maeder va_end(ap);
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder debug_fflush();
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maedervoid ldb_debug_messages(void *context, enum ldb_debug_level level,
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *fmt, va_list ap)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
9d6562465b41f17c7967d4e5678f34811d958cb2Christian Maeder int loglevel = SSSDBG_UNRESOLVED;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder char * message = NULL;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder switch(level) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder case LDB_DEBUG_FATAL:
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder loglevel = SSSDBG_FATAL_FAILURE;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder break;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder case LDB_DEBUG_ERROR:
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder loglevel = SSSDBG_CRIT_FAILURE;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder break;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder case LDB_DEBUG_WARNING:
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder loglevel = SSSDBG_TRACE_FUNC;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder break;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder case LDB_DEBUG_TRACE:
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder loglevel = SSSDBG_TRACE_ALL;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder break;
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder }
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder ret = vasprintf(&message, fmt, ap);
f39b8dd9651dfcc38b06191cda23cacbfc298323Christian Maeder if (ret < 0) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder /* ENOMEM */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (DEBUG_IS_SET(loglevel))
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_fn(__FILE__, __LINE__, "ldb", loglevel, "%s\n", message);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder free(message);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint open_debug_file_ex(const char *filename, FILE **filep, bool want_cloexec)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder FILE *f = NULL;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder char *logpath;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder const char *log_file;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder mode_t old_umask;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int debug_fd;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int flags;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (filename == NULL) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder log_file = debug_log_file;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder } else {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder log_file = filename;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = asprintf(&logpath, "%s/%s.log", LOG_PATH, log_file);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret == -1) {
2353f65833a3da763392f771223250cd50b8d873Christian Maeder return ENOMEM;
2353f65833a3da763392f771223250cd50b8d873Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (debug_file && !filep) fclose(debug_file);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder old_umask = umask(0177);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder errno = 0;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder f = fopen(logpath, "a");
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (f == NULL) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder sss_log(SSS_LOG_EMERG, "Could not open file [%s]. Error: [%d][%s]\n",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder logpath, errno, strerror(errno));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder free(logpath);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return EIO;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder umask(old_umask);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_fd = fileno(f);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (debug_fd == -1) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder fclose(f);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder free(logpath);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return EIO;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if(want_cloexec) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder flags = fcntl(debug_fd, F_GETFD, 0);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder (void) fcntl(debug_fd, F_SETFD, flags | FD_CLOEXEC);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (filep == NULL) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_file = f;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder } else {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder *filep = f;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder free(logpath);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return EOK;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint open_debug_file(void)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return open_debug_file_ex(NULL, NULL, true);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maederint rotate_debug_files(void)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder int ret;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder errno_t error;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (!debug_to_file) return EOK;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder do {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder error = 0;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder ret = fclose(debug_file);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (ret != 0) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder error = errno;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* Check for EINTR, which means we should retry
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * because the system call was interrupted by a
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * signal
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder } while (error == EINTR);
2353f65833a3da763392f771223250cd50b8d873Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder if (error != 0) {
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder /* Even if we were unable to close the debug log, we need to make
2353f65833a3da763392f771223250cd50b8d873Christian Maeder * sure that we open up a new one. Log rotation will remove the
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * current file, so all debug messages will be disappearing.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder *
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * We should write an error to the syslog warning of the resource
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder * leak and then proceed with opening the new file.
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder */
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder sss_log(SSS_LOG_ALERT, "Could not close debug file [%s]. [%d][%s]\n",
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_log_file, error, strerror(error));
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder sss_log(SSS_LOG_ALERT, "Attempting to open new file anyway. "
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder "Be aware that this is a resource leak\n");
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder }
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder debug_file = NULL;
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder return open_debug_file();
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder
b53688bfed888214b485cf76439d57262d80e0a7Christian Maedervoid talloc_log_fn(const char *message)
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder{
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder DEBUG(SSSDBG_FATAL_FAILURE, "%s", message);
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder}
b53688bfed888214b485cf76439d57262d80e0a7Christian Maeder