xref: /freebsd/crypto/heimdal/lib/roken/vsyslog.c (revision c19800e8)
1b528cefcSMark Murray /*
25e9cd1aeSAssar Westerlund  * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include <config.h>
35b528cefcSMark Murray 
36c19800e8SDoug Rabson #ifndef HAVE_VSYSLOG
37b528cefcSMark Murray 
38b528cefcSMark Murray #include <stdio.h>
39b528cefcSMark Murray #include <syslog.h>
40b528cefcSMark Murray #include <stdarg.h>
41b528cefcSMark Murray 
42b528cefcSMark Murray #include "roken.h"
43b528cefcSMark Murray 
44b528cefcSMark Murray /*
45b528cefcSMark Murray  * the theory behind this is that we might be trying to call vsyslog
46b528cefcSMark Murray  * when there's no memory left, and we should try to be as useful as
475e9cd1aeSAssar Westerlund  * possible.  And the format string should say something about what's
485e9cd1aeSAssar Westerlund  * failing.
495e9cd1aeSAssar Westerlund  */
505e9cd1aeSAssar Westerlund 
515e9cd1aeSAssar Westerlund static void
simple_vsyslog(int pri,const char * fmt,va_list ap)525e9cd1aeSAssar Westerlund simple_vsyslog(int pri, const char *fmt, va_list ap)
535e9cd1aeSAssar Westerlund {
545e9cd1aeSAssar Westerlund     syslog (pri, "%s", fmt);
555e9cd1aeSAssar Westerlund }
565e9cd1aeSAssar Westerlund 
575e9cd1aeSAssar Westerlund /*
585e9cd1aeSAssar Westerlund  * do like syslog but with a `va_list'
595e9cd1aeSAssar Westerlund  */
605e9cd1aeSAssar Westerlund 
615e9cd1aeSAssar Westerlund ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
vsyslog(int pri,const char * fmt,va_list ap)625e9cd1aeSAssar Westerlund vsyslog(int pri, const char *fmt, va_list ap)
635e9cd1aeSAssar Westerlund {
64c19800e8SDoug Rabson     char *fmt2;
65b528cefcSMark Murray     const char *p;
66b528cefcSMark Murray     char *p2;
675e9cd1aeSAssar Westerlund     int ret;
685e9cd1aeSAssar Westerlund     int saved_errno = errno;
695e9cd1aeSAssar Westerlund     int fmt_len  = strlen (fmt);
705e9cd1aeSAssar Westerlund     int fmt2_len = fmt_len;
715e9cd1aeSAssar Westerlund     char *buf;
725e9cd1aeSAssar Westerlund 
735e9cd1aeSAssar Westerlund     fmt2 = malloc (fmt_len + 1);
74b528cefcSMark Murray     if (fmt2 == NULL) {
755e9cd1aeSAssar Westerlund 	simple_vsyslog (pri, fmt, ap);
765e9cd1aeSAssar Westerlund 	return;
775e9cd1aeSAssar Westerlund     }
785e9cd1aeSAssar Westerlund 
79b528cefcSMark Murray     for (p = fmt, p2 = fmt2; *p != '\0'; ++p) {
80b528cefcSMark Murray 	if (p[0] == '%' && p[1] == 'm') {
815e9cd1aeSAssar Westerlund 	    const char *e = strerror (saved_errno);
825e9cd1aeSAssar Westerlund 	    int e_len = strlen (e);
835e9cd1aeSAssar Westerlund 	    char *tmp;
845e9cd1aeSAssar Westerlund 	    int pos;
855e9cd1aeSAssar Westerlund 
865e9cd1aeSAssar Westerlund 	    pos = p2 - fmt2;
875e9cd1aeSAssar Westerlund 	    fmt2_len += e_len - 2;
885e9cd1aeSAssar Westerlund 	    tmp = realloc (fmt2, fmt2_len + 1);
895e9cd1aeSAssar Westerlund 	    if (tmp == NULL) {
905e9cd1aeSAssar Westerlund 		free (fmt2);
915e9cd1aeSAssar Westerlund 		simple_vsyslog (pri, fmt, ap);
925e9cd1aeSAssar Westerlund 		return;
935e9cd1aeSAssar Westerlund 	    }
945e9cd1aeSAssar Westerlund 	    fmt2 = tmp;
955e9cd1aeSAssar Westerlund 	    p2   = fmt2 + pos;
965e9cd1aeSAssar Westerlund 	    memmove (p2, e, e_len);
975e9cd1aeSAssar Westerlund 	    p2 += e_len;
985e9cd1aeSAssar Westerlund 	    ++p;
995e9cd1aeSAssar Westerlund 	} else
1005e9cd1aeSAssar Westerlund 	    *p2++ = *p;
1015e9cd1aeSAssar Westerlund     }
1025e9cd1aeSAssar Westerlund     *p2 = '\0';
1035e9cd1aeSAssar Westerlund 
1045e9cd1aeSAssar Westerlund     ret = vasprintf (&buf, fmt2, ap);
1055e9cd1aeSAssar Westerlund     free (fmt2);
1065e9cd1aeSAssar Westerlund     if (ret < 0 || buf == NULL) {
1075e9cd1aeSAssar Westerlund 	simple_vsyslog (pri, fmt, ap);
1085e9cd1aeSAssar Westerlund 	return;
1095e9cd1aeSAssar Westerlund     }
1105e9cd1aeSAssar Westerlund     syslog (pri, "%s", buf);
1115e9cd1aeSAssar Westerlund     free (buf);
1125e9cd1aeSAssar Westerlund }
1135e9cd1aeSAssar Westerlund #endif
1145e9cd1aeSAssar Westerlund