subr_prf.c: Port to RTEMS

This commit is contained in:
Sebastian Huber 2016-05-04 13:04:42 +02:00
parent d4402ddbc8
commit 54a577d44f
10 changed files with 334 additions and 194 deletions

View File

@ -49,15 +49,21 @@ __FBSDID("$FreeBSD$");
#include <sys/mutex.h>
#include <sys/sx.h>
#include <sys/kernel.h>
#ifndef __rtems__
#include <sys/msgbuf.h>
#endif /* __rtems__ */
#include <sys/malloc.h>
#include <sys/priv.h>
#include <sys/proc.h>
#include <sys/stddef.h>
#include <sys/sysctl.h>
#ifndef __rtems__
#include <sys/tty.h>
#endif /* __rtems__ */
#include <sys/syslog.h>
#ifndef __rtems__
#include <sys/cons.h>
#endif /* __rtems__ */
#include <sys/uio.h>
#include <sys/ctype.h>
@ -71,13 +77,16 @@ __FBSDID("$FreeBSD$");
*/
#include <machine/stdarg.h>
#ifndef __rtems__
#define TOCONS 0x01
#define TOTTY 0x02
#define TOLOG 0x04
#endif /* __rtems__ */
/* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
#define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
#ifndef __rtems__
struct putchar_arg {
int flags;
int pri;
@ -87,20 +96,24 @@ struct putchar_arg {
char *p_next;
size_t remain;
};
#endif /* __rtems__ */
struct snprintf_arg {
char *str;
size_t remain;
};
#ifndef __rtems__
extern int log_open;
static void msglogchar(int c, int pri);
static void msglogstr(char *str, int pri, int filter_cr);
static void putchar(int ch, void *arg);
#endif /* __rtems__ */
static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len, int upper);
static void snprintf_func(int ch, void *arg);
#ifndef __rtems__
static int msgbufmapped; /* Set when safe to use msgbuf */
int msgbuftrigger;
@ -281,6 +294,10 @@ _vprintf(int level, int flags, const char *fmt, va_list ap)
return (retval);
}
#else /* __rtems__ */
#include <rtems/bsd/bsd.h>
#define _vprintf(level, flags, fmt, ap) rtems_bsd_vprintf(level, fmt, ap)
#endif /* __rtems__ */
/*
* Log writes to the log buffer, and guarantees not to sleep (so can be
@ -296,9 +313,12 @@ log(int level, const char *fmt, ...)
(void)_vprintf(level, log_open ? TOLOG : TOCONS, fmt, ap);
va_end(ap);
#ifndef __rtems__
msgbuftrigger = 1;
#endif /* __rtems__ */
}
#ifndef __rtems__
#define CONSCHUNK 128
void
@ -364,6 +384,7 @@ log_console(struct uio *uio)
free(consbuffer, M_TEMP);
return;
}
#endif /* __rtems__ */
int
printf(const char *fmt, ...)
@ -385,12 +406,15 @@ vprintf(const char *fmt, va_list ap)
retval = _vprintf(-1, TOCONS | TOLOG, fmt, ap);
#ifndef __rtems__
if (!panicstr)
msgbuftrigger = 1;
#endif /* __rtems__ */
return (retval);
}
#ifndef __rtems__
static void
putbuf(int c, struct putchar_arg *ap)
{
@ -474,6 +498,7 @@ putchar(int c, void *arg)
putbuf(c, ap);
}
}
#endif /* __rtems__ */
/*
* Scaled down version of sprintf(3).
@ -927,6 +952,7 @@ number:
#undef PCHAR
}
#ifndef __rtems__
/*
* Put character in log buffer with a particular priority.
*/
@ -1124,3 +1150,4 @@ hexdump(const void *ptr, int length, const char *hdr, int flags)
}
}
#endif /* __rtems__ */

View File

@ -77,7 +77,6 @@ def rtems(mm):
'rtems/rtems-bsd-init.c',
'rtems/rtems-bsd-irqs.c',
'rtems/rtems-bsd-jail.c',
'rtems/rtems-bsd-log.c',
'rtems/rtems-bsd-malloc.c',
'rtems/rtems-bsd-mbuf.c',
'rtems/rtems-bsd-mutex.c',
@ -100,10 +99,10 @@ def rtems(mm):
'rtems/rtems-bsd-sysctlnametomib.c',
'rtems/rtems-bsd-thread.c',
'rtems/rtems-bsd-timesupport.c',
'rtems/rtems-bsd-vprintf.c',
'rtems/rtems-bsdnet-rtrequest.c',
'rtems/rtems-kvm.c',
'rtems/rtems_mii_ioctl_kern.c',
'rtems/rtems-syslog-initialize.c',
'rtems/syslog.c',
'ftpd/ftpd.c',
'mdns/mdns.c',
@ -312,6 +311,7 @@ def base(mm):
'sys/kern/subr_kobj.c',
'sys/kern/subr_lock.c',
'sys/kern/subr_module.c',
'sys/kern/subr_prf.c',
'sys/kern/subr_rman.c',
'sys/kern/subr_sbuf.c',
'sys/kern/subr_sleepqueue.c',
@ -326,6 +326,7 @@ def base(mm):
'sys/kern/uipc_sockbuf.c',
'sys/kern/uipc_socket.c',
'sys/kern/uipc_usrreq.c',
'sys/libkern/bcd.c',
'sys/libkern/arc4random.c',
'sys/libkern/fls.c',
'sys/libkern/inet_ntoa.c',
@ -2447,6 +2448,7 @@ def tests(mm):
mod.addTest(mm.generator['test']('media01', ['test_main'], runTest = False))
mod.addTest(mm.generator['test']('vlan01', ['test_main'], netTest = True))
mod.addTest(mm.generator['test']('lagg01', ['test_main'], netTest = True))
mod.addTest(mm.generator['test']('log01', ['test_main']))
return mod
#

View File

@ -736,6 +736,7 @@ def build(bld):
'freebsd/sys/kern/subr_kobj.c',
'freebsd/sys/kern/subr_lock.c',
'freebsd/sys/kern/subr_module.c',
'freebsd/sys/kern/subr_prf.c',
'freebsd/sys/kern/subr_rman.c',
'freebsd/sys/kern/subr_sbuf.c',
'freebsd/sys/kern/subr_sleepqueue.c',
@ -753,6 +754,7 @@ def build(bld):
'freebsd/sys/kern/uipc_syscalls.c',
'freebsd/sys/kern/uipc_usrreq.c',
'freebsd/sys/libkern/arc4random.c',
'freebsd/sys/libkern/bcd.c',
'freebsd/sys/libkern/fls.c',
'freebsd/sys/libkern/inet_ntoa.c',
'freebsd/sys/libkern/random.c',
@ -989,7 +991,6 @@ def build(bld):
'rtemsbsd/rtems/rtems-bsd-init.c',
'rtemsbsd/rtems/rtems-bsd-irqs.c',
'rtemsbsd/rtems/rtems-bsd-jail.c',
'rtemsbsd/rtems/rtems-bsd-log.c',
'rtemsbsd/rtems/rtems-bsd-malloc.c',
'rtemsbsd/rtems/rtems-bsd-mbuf.c',
'rtemsbsd/rtems/rtems-bsd-mutex.c',
@ -1013,9 +1014,9 @@ def build(bld):
'rtemsbsd/rtems/rtems-bsd-sysctlnametomib.c',
'rtemsbsd/rtems/rtems-bsd-thread.c',
'rtemsbsd/rtems/rtems-bsd-timesupport.c',
'rtemsbsd/rtems/rtems-bsd-vprintf.c',
'rtemsbsd/rtems/rtems-bsdnet-rtrequest.c',
'rtemsbsd/rtems/rtems-kvm.c',
'rtemsbsd/rtems/rtems-syslog-initialize.c',
'rtemsbsd/rtems/rtems_mii_ioctl_kern.c',
'rtemsbsd/rtems/syslog.c',
'rtemsbsd/sys/dev/dw_mmc/dw_mmc.c',
@ -1209,6 +1210,16 @@ def build(bld):
lib = ["m", "z"],
install_path = None)
test_log01 = ['testsuite/log01/test_main.c']
bld.program(target = "log01.exe",
features = "cprogram",
cflags = cflags,
includes = includes,
source = test_log01,
use = ["bsd"],
lib = ["m", "z"],
install_path = None)
test_loopback01 = ['testsuite/loopback01/test_main.c']
bld.program(target = "loopback01.exe",
features = "cprogram",

View File

@ -54,8 +54,6 @@
#include <machine/rtems-bsd-kernel-namespace.h>
#include <machine/rtems-bsd-printf-to-iprintf.h>
/* Assert */
void rtems_bsd_assert_func(const char *file, int line, const char *func, const char *expr);

View File

@ -44,6 +44,8 @@
#include <sys/queue.h>
#include <sys/kernel.h>
#include <stdarg.h>
#include <rtems.h>
#ifdef __cplusplus
@ -191,6 +193,27 @@ int rtems_bsd_bus_root_resume(void);
*/
int rtems_bsd_bus_root_detach(void);
/**
* @brief Sets the output back-end for logging functions.
*
* @param new_vprintf_handler The new output back-end for logging functions.
*
* @see rtems_bsd_vprintf().
*/
void rtems_bsd_set_vprintf_handler(int (*new_vprintf_handler)
(int, const char *, va_list));
/**
* @brief Output back-end for logging functions.
*
* Used by kernel space printf(), vprintf(), log() and vlog(). Used by user
* space syslog() and vsyslog().
*
* The default uses putchar() and uses a mutex to serialize the output. It may
* be customized via rtems_bsd_set_vprintf_handler().
*/
int rtems_bsd_vprintf(int level, const char *fmt, va_list ap);
/** @} */
#ifdef __cplusplus

View File

@ -1,46 +0,0 @@
/**
* @file
*
* @ingroup rtems_bsd_rtems
*
* @brief TODO.
*/
/*
* COPYRIGHT (c) 2012. On-Line Applications Research Corporation (OAR).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <machine/rtems-bsd-kernel-space.h>
#include <rtems/bsd/sys/types.h>
#include <sys/systm.h>
#include <stdio.h>
void
log(int level, const char *fmt, ...)
{
puts(fmt);
}

View File

@ -0,0 +1,119 @@
/**
* @file
*
* @ingroup rtems_bsd_rtems
*
* @brief TODO.
*/
/*
* Copyright (c) 2014, 2016 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <machine/rtems-bsd-kernel-space.h>
#include <rtems/bsd/sys/types.h>
#include <sys/lock.h>
#include <sys/systm.h>
#include <sys/syslog.h>
#include <stdio.h>
#include <rtems/bsd/bsd.h>
#undef printf
#define VPRINTF_LOCK() _Mutex_Acquire(&vprintf_mtx)
#define VPRINTF_UNLOCK() _Mutex_Release(&vprintf_mtx)
static const char * const log_priorities[] = {
[LOG_EMERG] = "emerg",
[LOG_ALERT] = "alert",
[LOG_CRIT] = "crit",
[LOG_ERR] = "err",
[LOG_WARNING] = "warning",
[LOG_NOTICE] = "notice",
[LOG_INFO] = "info",
[LOG_DEBUG] = "debug"
};
/* Use a <sys/lock.h> mutex due to the static initialization capability */
static struct _Mutex_Control vprintf_mtx = _MUTEX_INITIALIZER;
static void
vprintf_putchar(int c, void *arg)
{
int *last;
last = arg;
*last = c;
putchar(c);
}
static int
default_vprintf_handler(int level, const char *fmt, va_list ap)
{
int n;
int last;
VPRINTF_LOCK();
if (level != LOG_PRINTF) {
printf("%s: ", log_priorities[LOG_PRI(level)]);
}
last = -1;
n = kvprintf(fmt, vprintf_putchar, &last, 10, ap);
if (level != LOG_PRINTF && last != '\n') {
putchar('\n');
}
VPRINTF_UNLOCK();
return (n);
}
static int (*vprintf_handler)(int, const char *, va_list) =
default_vprintf_handler;
void
rtems_bsd_set_vprintf_handler(int (*new_vprintf_handler)
(int, const char *, va_list))
{
vprintf_handler = new_vprintf_handler;
}
int
rtems_bsd_vprintf(int level, const char *fmt, va_list ap)
{
return ((*vprintf_handler)(level, fmt, ap));
}

View File

@ -1,21 +0,0 @@
/*
* RTEMS version of syslog initialization
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
struct in_addr rtems_bsdnet_log_host_address = {0};
int rtems_syslog_initialize(const char *log_host)
{
struct in_addr *i;
i = &rtems_bsdnet_log_host_address;
if (log_host)
i->s_addr = inet_addr(log_host);
return 0;
}

View File

@ -7,7 +7,7 @@
*/
/*
* Copyright (c) 2014 embedded brains GmbH. All rights reserved.
* Copyright (c) 2014, 2016 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
@ -37,46 +37,9 @@
* SUCH DAMAGE.
*/
#include <machine/rtems-bsd-user-space.h>
#include <machine/rtems-bsd-program.h>
#include <syslog.h>
#include <stdarg.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
typedef struct {
int mask;
} rtems_bsd_syslog_context;
/* FIXME: This should be thread specific */
static rtems_bsd_syslog_context rtems_bsd_syslog_instance = {
.mask = LOG_UPTO(LOG_DEBUG)
};
static const char * const rtems_bsd_syslog_priorities[] = {
[LOG_EMERG] = "emerg",
[LOG_ALERT] = "alert",
[LOG_CRIT] = "crit",
[LOG_ERR] = "err",
[LOG_WARNING] = "warning",
[LOG_NOTICE] = "notice",
[LOG_INFO] = "info",
[LOG_DEBUG] = "debug"
};
static rtems_bsd_syslog_context *
rtems_bsd_syslog_get_context(void)
{
return &rtems_bsd_syslog_instance;
}
static void
rtems_bsd_syslog_format_buffer_overflow(void)
{
fputs("err: syslog: format buffer overflow\n", stderr);
}
#include <rtems/bsd/bsd.h>
void
syslog(int priority, const char *format, ...)
@ -91,83 +54,8 @@ syslog(int priority, const char *format, ...)
void
vsyslog(int priority, const char *format, va_list ap)
{
rtems_bsd_syslog_context *ctx = rtems_bsd_syslog_get_context();
int pri = LOG_PRI(priority);
char fmt[128];
char buf[128];
const char *src;
char *dst;
size_t rem;
char *m;
int n;
size_t len;
if ((LOG_MASK(pri) & ctx->mask) == 0) {
return;
}
/* Expand the %m in the format string and add a newline character */
src = format;
dst = &fmt[0];
rem = sizeof(fmt) - 2;
while ((m = strstr(src, "%m")) != NULL) {
size_t c = m - src;
if (c > rem) {
rtems_bsd_syslog_format_buffer_overflow();
return;
}
memcpy(dst, src, c);
dst += c;
src += c + 2;
rem -= c;
n = sniprintf(dst, rem, "%s", strerror(errno));
if (n > rem || n < 0) {
rtems_bsd_syslog_format_buffer_overflow();
return;
}
dst += (size_t) n;
rem -= (size_t) n;
}
len = strlen(src);
if (len > rem) {
rtems_bsd_syslog_format_buffer_overflow();
return;
}
memcpy(dst, src, len);
dst += len;
dst[0] = '\n';
dst[1] = '\0';
/* Print into buffer */
dst = &buf[0];
rem = sizeof(buf) - 1;
n = sniprintf(dst, rem, "%s: ", rtems_bsd_syslog_priorities[pri]);
if (n <= rem) {
dst += (size_t) n;
rem -= (size_t) n;
}
n = sniprintf(dst, rem, "%s: ", rtems_bsd_program_get_name());
if (n <= rem) {
dst += (size_t) n;
rem -= (size_t) n;
}
vsniprintf(dst, rem, &fmt[0], ap);
/* Write in one rush */
fputs(&buf[0], stderr);
rtems_bsd_vprintf(priority, format, ap);
}
void
@ -185,10 +73,5 @@ closelog(void)
int
setlogmask(int mask)
{
rtems_bsd_syslog_context *ctx = rtems_bsd_syslog_get_context();
int cur = ctx->mask;
ctx->mask = mask & LOG_UPTO(LOG_DEBUG);
return cur;
/* TODO */
}

144
testsuite/log01/test_main.c Normal file
View File

@ -0,0 +1,144 @@
/*
* Copyright (c) 2016 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <machine/rtems-bsd-kernel-space.h>
#include <rtems/bsd/sys/types.h>
#include <sys/systm.h>
#include <syslog.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEST_NAME "LIBBSD LOG 1"
typedef struct {
char a[99];
char b[99];
size_t i;
int expected_level;
} test_context;
static test_context test_instance;
static void
vprintf_putchar(int c, void *arg)
{
test_context *ctx;
ctx = arg;
ctx->a[ctx->i] = (char) c;
++ctx->i;
}
static int
vprintf_handler(int level, const char *fmt, va_list ap)
{
test_context *ctx;
int n;
ctx = &test_instance;
assert(level == ctx->expected_level);
n = (kvprintf(fmt, vprintf_putchar, &test_instance, 10, ap));
ctx->a[n] = '\0';
return (n);
}
static void
reset(test_context *ctx, int expected_level)
{
ctx->i = 0;
ctx->expected_level = expected_level;
memset(&ctx->a[0], '?', sizeof(ctx->a));
memset(&ctx->b[0], '?', sizeof(ctx->b));
}
static void
check(test_context *ctx, const char *b)
{
memcpy(&ctx->b[0], b, strlen(b) + 1);
assert(memcmp(&ctx->a[0], &ctx->b[0], sizeof(ctx->a)) == 0);
}
static void
test_main(void)
{
test_context *ctx;
int n;
ctx = &test_instance;
rtems_bsd_set_vprintf_handler(vprintf_handler);
reset(ctx, 0);
n = sprintf(&ctx->a[0], "%04i", 123);
assert(n == 4);
check(ctx, "0123");
reset(ctx, 0);
n = snprintf(&ctx->a[0], 3, "%04i", 123);
assert(n == 4);
check(ctx, "01");
reset(ctx, 0);
n = snprintf(&ctx->a[0], sizeof(ctx->a), "%i%f%i", 123, 0.0, 456);
assert(n == 7);
check(ctx, "123%f%i");
reset(ctx, LOG_INFO);
log(LOG_INFO, "log %i", 456);
check(ctx, "log 456");
reset(ctx, LOG_WARNING);
syslog(LOG_WARNING, "syslog %i", 789);
check(ctx, "syslog 789");
reset(ctx, LOG_PRINTF);
printf("printf %i", 0);
check(ctx, "printf 0");
reset(ctx, LOG_PRINTF);
printf("reg=%b", 3, "\10\2BITTWO\1BITONE");
check(ctx, "reg=3<BITTWO,BITONE>");
reset(ctx, LOG_PRINTF);
printf("out: %4D", "AAAA", ":");
check(ctx, "out: 41:41:41:41");
exit(0);
}
#include <rtems/bsd/test/default-init.h>