Home | History | Annotate | Download | only in src
      1 /* Author: Joshua Brindle <jbrindle (at) tresys.co
      2  *         Jason Tang     <jtang (at) tresys.com>
      3  *         Ivan Gyurdiev  <ivg2 (at) 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 
     31 int semanage_msg_get_level(semanage_handle_t * handle)
     32 {
     33 	return handle->msg_level;
     34 }
     35 
     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 
     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 
     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 
     91 #ifdef __GNUC__
     92 __attribute__ ((format(printf, 3, 4)))
     93 #endif
     94 void hidden semanage_msg_relay_handler(void *varg,
     95 				       sepol_handle_t * sepolh,
     96 				       const char *fmt, ...)
     97 {
     98 	va_list ap;
     99 	semanage_handle_t *sh = varg;
    100 	char buffer[1024];
    101 
    102 	if (!sh->msg_callback)
    103 		return;
    104 
    105 	va_start(ap, fmt);
    106 	vsnprintf(buffer, sizeof(buffer), fmt, ap);
    107 	va_end(ap);
    108 
    109 	sh->msg_fname = sepol_msg_get_fname(sepolh);
    110 	sh->msg_channel = sepol_msg_get_channel(sepolh);
    111 	sh->msg_level = sepol_msg_get_level(sepolh);	/* XXX should map values */
    112 	sh->msg_callback(sh->msg_callback_arg, sh, "%s", buffer);
    113 	return;
    114 }
    115 
    116 extern void semanage_msg_set_callback(semanage_handle_t * handle,
    117 #ifdef __GNUC__
    118 				      __attribute__ ((format(printf, 3, 4)))
    119 #endif
    120 				      void (*msg_callback) (void *varg,
    121 							    semanage_handle_t *
    122 							    handle,
    123 							    const char *fmt,
    124 							    ...),
    125 				      void *msg_callback_arg)
    126 {
    127 
    128 	handle->msg_callback = msg_callback;
    129 	handle->msg_callback_arg = msg_callback_arg;
    130 }
    131