debug.c revision f7dd4ca760de5f2dfa962749dddf8a99587f2257
1/* Author: Joshua Brindle <jbrindle@tresys.co
2 *         Jason Tang     <jtang@tresys.com>
3 *         Ivan Gyurdiev  <ivg2@cornell.edu>
4 *
5 * Copyright (C) 2004-2005 Tresys Technology, LLC
6 * Copyright (C) 2005 Red Hat Inc.
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the GNU Lesser General Public
10 *  License as published by the Free Software Foundation; either
11 *  version 2.1 of the License, or (at your option) any later version.
12 *
13 *  This library is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 *  Lesser General Public License for more details.
17 *
18 *  You should have received a copy of the GNU Lesser General Public
19 *  License along with this library; if not, write to the Free Software
20 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 */
22
23#include <stdarg.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <errno.h>
27#include <string.h>
28#include "handle.h"
29#include "debug.h"
30
31int semanage_msg_get_level(semanage_handle_t * handle)
32{
33	return handle->msg_level;
34}
35
36hidden_def(semanage_msg_get_level)
37
38const char *semanage_msg_get_channel(semanage_handle_t * handle)
39{
40	return handle->msg_channel;
41}
42
43hidden_def(semanage_msg_get_channel)
44
45const char *semanage_msg_get_fname(semanage_handle_t * handle)
46{
47	return handle->msg_fname;
48}
49
50hidden_def(semanage_msg_get_fname)
51#ifdef __GNUC__
52    __attribute__ ((format(printf, 3, 4)))
53#endif
54void hidden semanage_msg_default_handler(void *varg __attribute__ ((unused)),
55					 semanage_handle_t * handle,
56					 const char *fmt, ...)
57{
58
59	FILE *stream = NULL;
60	int errsv = 0;
61
62	switch (semanage_msg_get_level(handle)) {
63
64	case SEMANAGE_MSG_ERR:
65		errsv = errno;
66	case SEMANAGE_MSG_WARN:
67		stream = stderr;
68		break;
69	case SEMANAGE_MSG_INFO:
70	default:
71		stream = stdout;
72		break;
73	}
74
75	fprintf(stream, "%s.%s: ",
76		semanage_msg_get_channel(handle),
77		semanage_msg_get_fname(handle));
78
79	va_list ap;
80	va_start(ap, fmt);
81	vfprintf(stream, fmt, ap);
82	va_end(ap);
83
84	if (errsv && errsv != ENOMEM)
85		fprintf(stream, " (%s).", strerror(errsv));
86
87	fprintf(stream, "\n");
88
89	varg = NULL;
90}
91
92#ifdef __GNUC__
93__attribute__ ((format(printf, 3, 4)))
94#endif
95void hidden semanage_msg_relay_handler(void *varg,
96				       sepol_handle_t * sepolh,
97				       const char *fmt, ...)
98{
99	va_list ap;
100	semanage_handle_t *sh = varg;
101	char buffer[1024];
102
103	if (!sh->msg_callback)
104		return;
105
106	va_start(ap, fmt);
107	vsnprintf(buffer, sizeof(buffer), fmt, ap);
108	va_end(ap);
109
110	sh->msg_fname = sepol_msg_get_fname(sepolh);
111	sh->msg_channel = sepol_msg_get_channel(sepolh);
112	sh->msg_level = sepol_msg_get_level(sepolh);	/* XXX should map values */
113	sh->msg_callback(sh->msg_callback_arg, sh, "%s", buffer);
114	return;
115}
116
117extern void semanage_msg_set_callback(semanage_handle_t * handle,
118#ifdef __GNUC__
119				      __attribute__ ((format(printf, 3, 4)))
120#endif
121				      void (*msg_callback) (void *varg,
122							    semanage_handle_t *
123							    handle,
124							    const char *fmt,
125							    ...),
126				      void *msg_callback_arg)
127{
128
129	handle->msg_callback = msg_callback;
130	handle->msg_callback_arg = msg_callback_arg;
131}
132