Coverage Report

Created: 2024-05-20 01:00

/src/openiked-portable/iked/log.c
Line
Count
Source (jump to first uncovered line)
1
/*  $OpenBSD: log.c,v 1.12 2017/03/21 12:06:55 bluhm Exp $  */
2
3
/*
4
 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <stdarg.h>
22
#include <string.h>
23
#include <syslog.h>
24
#include <errno.h>
25
#include <time.h>
26
27
static int   debug;
28
static int   verbose;
29
const char  *log_procname;
30
31
void  log_init(int, int);
32
void  log_procinit(const char *);
33
void  log_setverbose(int);
34
int log_getverbose(void);
35
void  log_warn(const char *, ...)
36
      __attribute__((__format__ (printf, 1, 2)));
37
void  log_warnx(const char *, ...)
38
      __attribute__((__format__ (printf, 1, 2)));
39
void  log_info(const char *, ...)
40
      __attribute__((__format__ (printf, 1, 2)));
41
void  log_debug(const char *, ...)
42
      __attribute__((__format__ (printf, 1, 2)));
43
void  logit(int, const char *, ...)
44
      __attribute__((__format__ (printf, 2, 3)));
45
void  vlog(int, const char *, va_list)
46
      __attribute__((__format__ (printf, 2, 0)));
47
__dead void fatal(const char *, ...)
48
      __attribute__((__format__ (printf, 1, 2)));
49
__dead void fatalx(const char *, ...)
50
      __attribute__((__format__ (printf, 1, 2)));
51
52
void
53
log_init(int n_debug, int facility)
54
0
{
55
0
  extern char *__progname;
56
57
0
  debug = n_debug;
58
0
  verbose = n_debug;
59
0
  log_procinit(__progname);
60
61
0
  if (!debug)
62
0
    openlog(__progname, LOG_PID | LOG_NDELAY, facility);
63
64
0
  tzset();
65
0
}
66
67
void
68
log_procinit(const char *procname)
69
0
{
70
0
  if (procname != NULL)
71
0
    log_procname = procname;
72
0
}
73
74
void
75
log_setverbose(int v)
76
0
{
77
0
  verbose = v;
78
0
}
79
80
int
81
log_getverbose(void)
82
198k
{
83
198k
  return (verbose);
84
198k
}
85
86
void
87
logit(int pri, const char *fmt, ...)
88
0
{
89
0
  va_list ap;
90
91
0
  va_start(ap, fmt);
92
0
  vlog(pri, fmt, ap);
93
0
  va_end(ap);
94
0
}
95
96
void
97
vlog(int pri, const char *fmt, va_list ap)
98
16.3k
{
99
16.3k
  char  *nfmt;
100
16.3k
  int  saved_errno = errno;
101
102
16.3k
  if (debug) {
103
    /* best effort in out of mem situations */
104
0
    if (asprintf(&nfmt, "%s\n", fmt) == -1) {
105
0
      vfprintf(stderr, fmt, ap);
106
0
      fprintf(stderr, "\n");
107
0
    } else {
108
0
      vfprintf(stderr, nfmt, ap);
109
0
      free(nfmt);
110
0
    }
111
0
    fflush(stderr);
112
0
  } else
113
16.3k
    vsyslog(pri, fmt, ap);
114
115
16.3k
  errno = saved_errno;
116
16.3k
}
117
118
void
119
log_warn(const char *emsg, ...)
120
0
{
121
0
  char    *nfmt;
122
0
  va_list    ap;
123
0
  int    saved_errno = errno;
124
125
  /* best effort to even work in out of memory situations */
126
0
  if (emsg == NULL)
127
0
    logit(LOG_ERR, "%s", strerror(saved_errno));
128
0
  else {
129
0
    va_start(ap, emsg);
130
131
0
    if (asprintf(&nfmt, "%s: %s", emsg,
132
0
        strerror(saved_errno)) == -1) {
133
      /* we tried it... */
134
0
      vlog(LOG_ERR, emsg, ap);
135
0
      logit(LOG_ERR, "%s", strerror(saved_errno));
136
0
    } else {
137
0
      vlog(LOG_ERR, nfmt, ap);
138
0
      free(nfmt);
139
0
    }
140
0
    va_end(ap);
141
0
  }
142
143
0
  errno = saved_errno;
144
0
}
145
146
void
147
log_warnx(const char *emsg, ...)
148
0
{
149
0
  va_list  ap;
150
151
0
  va_start(ap, emsg);
152
0
  vlog(LOG_ERR, emsg, ap);
153
0
  va_end(ap);
154
0
}
155
156
void
157
log_info(const char *emsg, ...)
158
16.3k
{
159
16.3k
  va_list  ap;
160
161
16.3k
  va_start(ap, emsg);
162
16.3k
  vlog(LOG_INFO, emsg, ap);
163
16.3k
  va_end(ap);
164
16.3k
}
165
166
void
167
log_debug(const char *emsg, ...)
168
769k
{
169
769k
  va_list  ap;
170
171
769k
  if (verbose > 1) {
172
0
    va_start(ap, emsg);
173
0
    vlog(LOG_DEBUG, emsg, ap);
174
0
    va_end(ap);
175
0
  }
176
769k
}
177
178
static void
179
vfatalc(int code, const char *emsg, va_list ap)
180
0
{
181
0
  static char s[BUFSIZ];
182
0
  const char  *sep;
183
184
0
  if (emsg != NULL) {
185
0
    (void)vsnprintf(s, sizeof(s), emsg, ap);
186
0
    sep = ": ";
187
0
  } else {
188
0
    s[0] = '\0';
189
0
    sep = "";
190
0
  }
191
0
  if (code)
192
0
    logit(LOG_CRIT, "%s: %s%s%s",
193
0
        log_procname, s, sep, strerror(code));
194
0
  else
195
0
    logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
196
0
}
197
198
void
199
fatal(const char *emsg, ...)
200
0
{
201
0
  va_list ap;
202
203
0
  va_start(ap, emsg);
204
0
  vfatalc(errno, emsg, ap);
205
0
  va_end(ap);
206
0
  exit(1);
207
0
}
208
209
void
210
fatalx(const char *emsg, ...)
211
0
{
212
0
  va_list ap;
213
214
0
  va_start(ap, emsg);
215
0
  vfatalc(0, emsg, ap);
216
0
  va_end(ap);
217
0
  exit(1);
218
0
}