Esempio n. 1
0
static void __oscap_vdlprintf(int level, const char *file, const char *fn, size_t line, const char *fmt, va_list ap)
{
	char  l;
	const char *f;

	__LOCK_FP;

	if (__debuglog_level == -1) {
		char *env;

		env = getenv(OSCAP_DEBUG_LEVEL_ENV);
		if (env == NULL)
			__debuglog_level = DBG_I;
		else
			__debuglog_level = atoi(env);
	}
	if (__debuglog_level < level) {
		__UNLOCK_FP;
		return;
	}
	if (__debuglog_fp == NULL) {
		char *logfile, pathbuf[4096];
		char *st;
		time_t ut;

		logfile = getenv(OSCAP_DEBUG_FILE_ENV);

		if (logfile == NULL)
			logfile = OSCAP_DEBUG_FILE;

		if (snprintf(pathbuf, sizeof pathbuf, "%s.%u",
			     logfile, (unsigned int)getpid()) >= (signed int) sizeof pathbuf)
		{
                        __UNLOCK_FP;
			return;
		}

                __debuglog_fp = fopen (pathbuf, "w");

		if (__debuglog_fp == NULL) {
			__UNLOCK_FP;
			return;
		}

		setbuf(__debuglog_fp, NULL);

		ut = time(NULL);
		st = ctime(&ut);

		fprintf(__debuglog_fp, "\n=============== LOG: %.24s ===============\n", st);
                atexit(&__oscap_debuglog_close);
	}
	if (__debuglog_pstrip == -1) {
		char *pstrip;

		pstrip = getenv(OSCAP_DEBUG_PATHSTRIP_ENV);

		if (pstrip == NULL)
			__debuglog_pstrip = 0;
		else
			__debuglog_pstrip = atol(pstrip);

	}
	if (__debuglog_pstrip != 0)
		f = __oscap_path_rstrip(file, __debuglog_pstrip);
	else
		f = file;

#if defined(__SVR4) && defined (__sun)
	if (lockf(fileno(__debuglog_fp), F_LOCK, 0L) == -1) {
#else
	if (flock(fileno(__debuglog_fp), LOCK_EX) == -1) {
#endif
		__UNLOCK_FP;
		return;
	}

	switch (level) {
	case DBG_E:
		l = 'E';
		break;
	case DBG_W:
		l = 'W';
		break;
	case DBG_I:
		l = 'I';
		break;
	default:
		l = '0';
	}
#if defined(OSCAP_THREAD_SAFE)
	/* XXX: non-portable usage of pthread_t */
	fprintf(__debuglog_fp, "(%ld:%llx) [%c:%s:%zu:%s] ", (long) getpid(),
		(unsigned long long) pthread_self(), l, f, line, fn);
#else
	fprintf(__debuglog_fp, "(%ld) [%c:%s:%zu:%s] ", (long) getpid(),
		l, f, line, fn);
#endif
	vfprintf(__debuglog_fp, fmt, ap);

#if defined(__SVR4) && defined (__sun)
	if (lockf(fileno(__debuglog_fp), F_ULOCK, 0L) == -1) {
#else
	if (flock(fileno(__debuglog_fp), LOCK_UN) == -1) {
#endif
		/* __UNLOCK_FP; */
		abort();
	}

	__UNLOCK_FP;
	return;
}

void __oscap_dlprintf(int level, const char *file, const char *fn, size_t line, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	__oscap_vdlprintf(level, file, fn, line, fmt, ap);
	va_end(ap);
}

void __oscap_debuglog_object (const char *file, const char *fn, size_t line, int objtype, void *obj)
{
        __LOCK_FP;

        if (__debuglog_fp == NULL) {
                char  *logfile, pathbuf[4096];
                char  *st;
                time_t ut;

                logfile = getenv (OSCAP_DEBUG_FILE_ENV);

                if (logfile == NULL)
                        logfile = OSCAP_DEBUG_FILE;

		if (snprintf(pathbuf, sizeof pathbuf, "%s.%ld",
			     logfile, (long)getpid()) >= (signed int) sizeof pathbuf)
		{
                        __UNLOCK_FP;
			return;
		}

                __debuglog_fp = fopen (pathbuf, "w");

                if (__debuglog_fp == NULL) {
                        __UNLOCK_FP;
                        return;
                }

                setbuf (__debuglog_fp, NULL);

                ut = time (NULL);
                st = ctime (&ut);

                fprintf (__debuglog_fp, "=============== LOG: %.24s ===============\n", st);
                atexit(&__oscap_debuglog_close);
        }
#if defined(__SVR4) && defined (__sun)
        if (lockf (fileno (__debuglog_fp), F_LOCK, 0L) == -1) {
#else
        if (flock (fileno (__debuglog_fp), LOCK_EX | LOCK_NB) == -1) {
#endif
                __UNLOCK_FP;
                return;
        }

#if defined(SEAP_THREAD_SAFE)
        /* XXX: non-portable usage of pthread_t */
	fprintf (__debuglog_fp, "(%ld:%llx) [%s:%zu:%s]\n------ \n", (long)getpid (), (unsigned long long)pthread_self(), file, line, fn);
#else
	fprintf (__debuglog_fp, "(%ld) [%s:%zu:%s]\n------\n ", (long)getpid (), file, line, fn);
#endif

        switch (objtype) {
        case OSCAP_DEBUGOBJ_SEXP:
                SEXP_fprintfa(__debuglog_fp, (SEXP_t *)obj);
        }

        fprintf(__debuglog_fp, "\n-----------\n");
#if defined(__SVR4) && defined (__sun)
        if (lockf (fileno (__debuglog_fp), F_ULOCK, 0L) == -1) {
#else
        if (flock (fileno (__debuglog_fp), LOCK_UN | LOCK_NB) == -1) {
#endif
                /* __UNLOCK_FP; */
                abort ();
        }

        __UNLOCK_FP;
        return;
}
Esempio n. 2
0
static void debug_message_start(int level, int indent)
{
	char  l;

	__LOCK_FP;
#if defined(__SVR4) && defined (__sun)
	if (lockf(fileno(__debuglog_fp), F_LOCK, 0L) == -1) {
#else
	if (flock(fileno(__debuglog_fp), LOCK_EX) == -1) {
#endif
		__UNLOCK_FP;
		return;
	}

	switch (level) {
	case DBG_E:
		l = 'E';
		break;
	case DBG_W:
		l = 'W';
		break;
	case DBG_I:
		l = 'I';
		break;
	case DBG_D:
		l = 'D';
		break;
	default:
		l = '0';
	}
	fprintf(__debuglog_fp, "%c: %s: ", l, GET_PROGRAM_NAME);
	for (int i = 0; i < indent; i++) {
		fprintf(__debuglog_fp, "  ");
	}
}

static void debug_message_devel_metadata(const char *file, const char *fn, size_t line)
{
	const char *f = __oscap_path_rstrip(file);
#if defined(OSCAP_THREAD_SAFE)
	char thread_name[THREAD_NAME_LEN];
	pthread_t thread = pthread_self();
#if defined(HAVE_PTHREAD_GETNAME_NP)
	pthread_getname_np(thread, thread_name, THREAD_NAME_LEN);
#else
	snprintf(thread_name, THREAD_NAME_LEN, "unknown");
#endif
	/* XXX: non-portable usage of pthread_t */
	fprintf(__debuglog_fp, " [%s(%ld):%s(%llx):%s:%zu:%s]",
		GET_PROGRAM_NAME, (long) getpid(), thread_name,
		(unsigned long long) thread, f, line, fn);
#else
	fprintf(__debuglog_fp, " [%ld:%s:%zu:%s]", (long) getpid(),
		f, line, fn);
#endif
}

static void debug_message_end()
{
	fputc('\n', __debuglog_fp);
#if defined(__SVR4) && defined (__sun)
	if (lockf(fileno(__debuglog_fp), F_ULOCK, 0L) == -1) {
#else
	if (flock(fileno(__debuglog_fp), LOCK_UN) == -1) {
#endif
		/* __UNLOCK_FP; */
		abort();
	}

	__UNLOCK_FP;
	return;
}

void __oscap_dlprintf(int level, const char *file, const char *fn, size_t line, int delta_indent, const char *fmt, ...)
{
	static int indent = 0;
	va_list ap;

	if (__debuglog_fp == NULL) {
		return;
	}
	indent += delta_indent;
	if (fmt == NULL) {
		return;
	}
	if (__debuglog_level < level) {
		return;
	}
	va_start(ap, fmt);
	debug_message_start(level, indent);
	vfprintf(__debuglog_fp, fmt, ap);
	if (__debuglog_level == DBG_D) {
		debug_message_devel_metadata(file, fn, line);
	}
	debug_message_end();
	va_end(ap);
}

void __oscap_debuglog_object (const char *file, const char *fn, size_t line, int objtype, void *obj)
{
	if (__debuglog_fp == NULL) {
		return;
	}
	if (__debuglog_level < DBG_D) {
		return;
	}
	debug_message_start(DBG_D, 0);
	switch (objtype) {
	case OSCAP_DEBUGOBJ_SEXP:
#if defined(OVAL_PROBES_ENABLED)
		SEXP_fprintfa(__debuglog_fp, (SEXP_t *)obj);
#endif
		break;
	default:
		fprintf(__debuglog_fp, "Attempt to dump a not supported object.");
	}
	debug_message_devel_metadata(file, fn, line);
	debug_message_end();
}