source: rtems-libbsd/rtemsbsd/rtems/syslog.c @ e58b898

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e58b898 was 982f72f, checked in by Sebastian Huber <sebastian.huber@…>, on 04/18/16 at 12:45:19

syslog: Include missing header file

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_rtems
5 *
6 * @brief TODO.
7 */
8
9/*
10 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40#include <machine/rtems-bsd-user-space.h>
41#include <machine/rtems-bsd-program.h>
42
43#include <syslog.h>
44#include <stdarg.h>
45#include <errno.h>
46#include <stdio.h>
47#include <string.h>
48
49typedef struct {
50  int mask;
51} rtems_bsd_syslog_context;
52
53/* FIXME: This should be thread specific */
54static rtems_bsd_syslog_context rtems_bsd_syslog_instance = {
55        .mask = LOG_UPTO(LOG_DEBUG)
56};
57
58static const char * const rtems_bsd_syslog_priorities[] = {
59        [LOG_EMERG] = "emerg",
60        [LOG_ALERT] = "alert",
61        [LOG_CRIT] = "crit",
62        [LOG_ERR] = "err",
63        [LOG_WARNING] = "warning",
64        [LOG_NOTICE] = "notice",
65        [LOG_INFO] = "info",
66        [LOG_DEBUG] = "debug"
67};
68
69static rtems_bsd_syslog_context *
70rtems_bsd_syslog_get_context(void)
71{
72        return &rtems_bsd_syslog_instance;
73}
74
75static void
76rtems_bsd_syslog_format_buffer_overflow(void)
77{
78        fputs("err: syslog: format buffer overflow\n", stderr);
79}
80
81void
82syslog(int priority, const char *format, ...)
83{
84        va_list ap;
85
86        va_start(ap, format);
87        vsyslog(priority, format, ap);
88        va_end(ap);
89}
90
91void
92vsyslog(int priority, const char *format, va_list ap)
93{
94        rtems_bsd_syslog_context *ctx = rtems_bsd_syslog_get_context();
95        int pri = LOG_PRI(priority);
96        char fmt[128];
97        char buf[128];
98        const char *src;
99        char *dst;
100        size_t rem;
101        char *m;
102        int n;
103        size_t len;
104
105        if ((LOG_MASK(pri) & ctx->mask) == 0) {
106                return;
107        }
108
109        /* Expand the %m in the format string and add a newline character */
110
111        src = format;
112        dst = &fmt[0];
113        rem = sizeof(fmt) - 2;
114
115        while ((m = strstr(src, "%m")) != NULL) {
116                size_t c = m - src;
117
118                if (c > rem) {
119                        rtems_bsd_syslog_format_buffer_overflow();
120                        return;
121                }
122
123                memcpy(dst, src, c);
124                dst += c;
125                src += c + 2;
126                rem -= c;
127
128                n = sniprintf(dst, rem, "%s", strerror(errno));
129                if (n > rem || n < 0) {
130                        rtems_bsd_syslog_format_buffer_overflow();
131                        return;
132                }
133
134                dst += (size_t) n;
135                rem -= (size_t) n;
136        }
137
138        len = strlen(src);
139        if (len > rem) {
140                rtems_bsd_syslog_format_buffer_overflow();
141                return;
142        }
143
144        memcpy(dst, src, len);
145        dst += len;
146        dst[0] = '\n';
147        dst[1] = '\0';
148
149        /* Print into buffer */
150
151        dst = &buf[0];
152        rem = sizeof(buf) - 1;
153
154        n = sniprintf(dst, rem, "%s: ", rtems_bsd_syslog_priorities[pri]);
155        if (n <= rem) {
156                dst += (size_t) n;
157                rem -= (size_t) n;
158        }
159
160        n = sniprintf(dst, rem, "%s: ", rtems_bsd_program_get_name());
161        if (n <= rem) {
162                dst += (size_t) n;
163                rem -= (size_t) n;
164        }
165
166        vsniprintf(dst, rem, &fmt[0], ap);
167
168        /* Write in one rush */
169
170        fputs(&buf[0], stderr);
171}
172
173void
174openlog(const char *ident, int option, int facility)
175{
176        /* TODO */
177}
178
179void
180closelog(void)
181{
182        /* TODO */
183}
184
185int
186setlogmask(int mask)
187{
188        rtems_bsd_syslog_context *ctx = rtems_bsd_syslog_get_context();
189        int cur = ctx->mask;
190
191        ctx->mask = mask & LOG_UPTO(LOG_DEBUG);
192
193        return cur;
194}
Note: See TracBrowser for help on using the repository browser.