• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
semanage_msg_get_level(semanage_handle_t * handle)31 int semanage_msg_get_level(semanage_handle_t * handle)
32 {
33 	return handle->msg_level;
34 }
35 
hidden_def(semanage_msg_get_level)36 hidden_def(semanage_msg_get_level)
37 
38 const char *semanage_msg_get_channel(semanage_handle_t * handle)
39 {
40 	return handle->msg_channel;
41 }
42 
hidden_def(semanage_msg_get_channel)43 hidden_def(semanage_msg_get_channel)
44 
45 const char *semanage_msg_get_fname(semanage_handle_t * handle)
46 {
47 	return handle->msg_fname;
48 }
49 
hidden_def(semanage_msg_get_fname)50 hidden_def(semanage_msg_get_fname)
51 #ifdef __GNUC__
52     __attribute__ ((format(printf, 3, 4)))
53 #endif
54 void 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 		stream = stderr;
66 		errsv = errno;
67 		break;
68 	case SEMANAGE_MSG_WARN:
69 		stream = stderr;
70 		break;
71 	default:
72 		stream = stdout;
73 		break;
74 	}
75 
76 	fprintf(stream, "%s.%s: ",
77 		semanage_msg_get_channel(handle),
78 		semanage_msg_get_fname(handle));
79 
80 	va_list ap;
81 	va_start(ap, fmt);
82 	vfprintf(stream, fmt, ap);
83 	va_end(ap);
84 
85 	if (errsv && errsv != ENOMEM)
86 		fprintf(stream, " (%s).", strerror(errsv));
87 
88 	fprintf(stream, "\n");
89 
90 	varg = NULL;
91 }
92 
93 #ifdef __GNUC__
94 __attribute__ ((format(printf, 3, 4)))
95 #endif
semanage_msg_relay_handler(void * varg,sepol_handle_t * sepolh,const char * fmt,...)96 void hidden semanage_msg_relay_handler(void *varg,
97 				       sepol_handle_t * sepolh,
98 				       const char *fmt, ...)
99 {
100 	va_list ap;
101 	semanage_handle_t *sh = varg;
102 	char buffer[1024];
103 
104 	if (!sh->msg_callback)
105 		return;
106 
107 	va_start(ap, fmt);
108 	vsnprintf(buffer, sizeof(buffer), fmt, ap);
109 	va_end(ap);
110 
111 	sh->msg_fname = sepol_msg_get_fname(sepolh);
112 	sh->msg_channel = sepol_msg_get_channel(sepolh);
113 	sh->msg_level = sepol_msg_get_level(sepolh);	/* XXX should map values */
114 	sh->msg_callback(sh->msg_callback_arg, sh, "%s", buffer);
115 	return;
116 }
117 
semanage_msg_set_callback(semanage_handle_t * handle,void (* msg_callback)(void * varg,semanage_handle_t * handle,const char * fmt,...),void * msg_callback_arg)118 extern void semanage_msg_set_callback(semanage_handle_t * handle,
119 #ifdef __GNUC__
120 				      __attribute__ ((format(printf, 3, 4)))
121 #endif
122 				      void (*msg_callback) (void *varg,
123 							    semanage_handle_t *
124 							    handle,
125 							    const char *fmt,
126 							    ...),
127 				      void *msg_callback_arg)
128 {
129 
130 	handle->msg_callback = msg_callback;
131 	handle->msg_callback_arg = msg_callback_arg;
132 }
133