2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2023-05-23 16:06:32 +02:00
|
|
|
* libs/libc/stream/lib_syslograwstream.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2023-01-04 06:58:51 +01:00
|
|
|
#include <stddef.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-05-11 01:36:08 +02:00
|
|
|
#include <nuttx/mm/iob.h>
|
2022-11-23 18:48:20 +01:00
|
|
|
#include <nuttx/streams.h>
|
2017-05-11 01:36:08 +02:00
|
|
|
#include <nuttx/syslog/syslog.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-23 14:14:58 +02:00
|
|
|
#ifdef CONFIG_SYSLOG_BUFFER
|
2017-05-12 14:58:33 +02:00
|
|
|
/****************************************************************************
|
2023-05-23 16:06:32 +02:00
|
|
|
* Name: syslograwstream_flush
|
2017-05-12 14:58:33 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
static int syslograwstream_flush(FAR struct lib_outstream_s *ostream)
|
2017-05-12 14:58:33 +02:00
|
|
|
{
|
2023-05-23 16:06:32 +02:00
|
|
|
FAR struct lib_syslograwstream_s *stream = (FAR void *)ostream;
|
2017-05-12 14:58:33 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
DEBUGASSERT(stream != NULL);
|
|
|
|
|
|
|
|
/* Do we have an IO buffer? Is there anything buffered? */
|
|
|
|
|
2023-05-23 14:14:58 +02:00
|
|
|
if (stream->base != NULL && stream->offset > 0)
|
2017-05-12 14:58:33 +02:00
|
|
|
{
|
|
|
|
/* Yes write the buffered data */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-05-23 14:14:58 +02:00
|
|
|
ssize_t nbytes = syslog_write(stream->base, stream->offset);
|
2017-05-12 14:58:33 +02:00
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
2022-11-23 18:48:20 +01:00
|
|
|
ret = nbytes;
|
2017-05-12 14:58:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (ret == -EINTR);
|
|
|
|
}
|
|
|
|
|
2023-05-23 14:14:58 +02:00
|
|
|
stream->offset = 0;
|
2017-05-12 14:58:33 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:31:50 +02:00
|
|
|
/****************************************************************************
|
2023-05-23 16:06:32 +02:00
|
|
|
* Name: syslograwstream_addchar
|
2017-05-12 15:31:50 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
static void syslograwstream_addchar(FAR struct lib_syslograwstream_s *stream,
|
|
|
|
int ch)
|
2017-05-12 15:31:50 +02:00
|
|
|
{
|
|
|
|
/* Add the incoming character to the buffer */
|
|
|
|
|
2023-05-23 14:14:58 +02:00
|
|
|
stream->base[stream->offset] = ch;
|
|
|
|
stream->offset++;
|
2017-05-12 15:31:50 +02:00
|
|
|
|
|
|
|
/* Increment the total number of bytes buffered. */
|
|
|
|
|
|
|
|
stream->public.nput++;
|
|
|
|
|
|
|
|
/* Is the buffer full? */
|
|
|
|
|
2023-05-23 14:14:58 +02:00
|
|
|
if (stream->offset >= stream->size)
|
2017-05-12 15:31:50 +02:00
|
|
|
{
|
|
|
|
/* Yes.. then flush the buffer */
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
syslograwstream_flush(&stream->public);
|
2017-05-12 15:31:50 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-04 06:58:51 +01:00
|
|
|
|
2023-05-23 14:14:58 +02:00
|
|
|
/****************************************************************************
|
2023-05-23 16:06:32 +02:00
|
|
|
* Name: syslograwstream_addstring
|
2023-05-23 14:14:58 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
static int
|
|
|
|
syslograwstream_addstring(FAR struct lib_syslograwstream_s *stream,
|
|
|
|
FAR const char *buff, int len)
|
2023-01-04 06:58:51 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-05-23 14:14:58 +02:00
|
|
|
int remain = stream->size - stream->offset;
|
2023-01-30 10:05:29 +01:00
|
|
|
remain = remain > len - ret ? len - ret : remain;
|
2023-05-23 14:14:58 +02:00
|
|
|
memcpy(stream->base + stream->offset, buff + ret, remain);
|
|
|
|
stream->offset += remain;
|
2023-01-04 06:58:51 +01:00
|
|
|
ret += remain;
|
|
|
|
|
|
|
|
/* Is the buffer enough? */
|
|
|
|
|
2023-05-23 14:14:58 +02:00
|
|
|
if (stream->offset >= stream->size)
|
2023-01-04 06:58:51 +01:00
|
|
|
{
|
|
|
|
/* Yes.. then flush the buffer */
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
syslograwstream_flush(&stream->public);
|
2023-01-04 06:58:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (ret < len);
|
|
|
|
|
|
|
|
/* Increment the total number of bytes buffered. */
|
|
|
|
|
|
|
|
stream->public.nput += len;
|
|
|
|
return len;
|
|
|
|
}
|
2017-05-12 15:57:45 +02:00
|
|
|
#endif
|
2017-05-12 15:31:50 +02:00
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2023-05-23 16:06:32 +02:00
|
|
|
* Name: syslograwstream_putc
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
static void syslograwstream_putc(FAR struct lib_outstream_s *this, int ch)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2023-05-23 16:06:32 +02:00
|
|
|
FAR struct lib_syslograwstream_s *stream =
|
|
|
|
(FAR struct lib_syslograwstream_s *)this;
|
2022-12-29 14:56:27 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(stream != NULL);
|
|
|
|
stream->last_ch = ch;
|
|
|
|
|
2017-05-11 00:13:21 +02:00
|
|
|
/* Discard carriage returns */
|
2017-05-10 22:42:43 +02:00
|
|
|
|
2017-05-11 00:13:21 +02:00
|
|
|
if (ch != '\r')
|
|
|
|
{
|
2023-05-23 14:14:58 +02:00
|
|
|
# ifdef CONFIG_SYSLOG_BUFFER
|
2017-05-11 01:36:08 +02:00
|
|
|
/* Do we have an IO buffer? */
|
2017-05-10 22:42:43 +02:00
|
|
|
|
2023-05-23 14:14:58 +02:00
|
|
|
if (stream->base != NULL)
|
2017-05-11 00:13:21 +02:00
|
|
|
{
|
2017-05-12 15:31:50 +02:00
|
|
|
/* Add the incoming character to the buffer */
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
syslograwstream_addchar(stream, ch);
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2017-05-11 01:36:08 +02:00
|
|
|
else
|
2023-05-23 14:14:58 +02:00
|
|
|
# endif
|
2017-05-11 01:36:08 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Try writing until the write was successful or until an
|
|
|
|
* irrecoverable error occurs.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-05-11 01:36:08 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Write the character to the supported logging device. On
|
2019-09-16 15:48:48 +02:00
|
|
|
* failure, syslog_putc returns a negated errno value.
|
2017-05-11 01:36:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = syslog_putc(ch);
|
2018-01-31 00:23:10 +01:00
|
|
|
if (ret >= 0)
|
2017-05-11 01:36:08 +02:00
|
|
|
{
|
|
|
|
this->nput++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-31 00:23:10 +01:00
|
|
|
/* The special return value -EINTR means that syslog_putc() was
|
2017-05-11 01:36:08 +02:00
|
|
|
* awakened by a signal. This is not a real error and must be
|
|
|
|
* ignored in this context.
|
|
|
|
*/
|
|
|
|
}
|
2018-01-31 00:23:10 +01:00
|
|
|
while (ret == -EINTR);
|
2017-05-11 01:36:08 +02:00
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2017-05-10 22:42:43 +02:00
|
|
|
}
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
static int syslograwstream_puts(FAR struct lib_outstream_s *this,
|
|
|
|
FAR const void *buff, int len)
|
2023-01-04 06:58:51 +01:00
|
|
|
{
|
2023-05-23 16:06:32 +02:00
|
|
|
FAR struct lib_syslograwstream_s *stream =
|
|
|
|
(FAR struct lib_syslograwstream_s *)this;
|
2023-03-13 15:44:33 +01:00
|
|
|
int ret;
|
2023-01-04 06:58:51 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(stream != NULL);
|
2023-03-13 15:42:00 +01:00
|
|
|
if (len <= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:44:33 +01:00
|
|
|
stream->last_ch = ((FAR const char *)buff)[len - 1];
|
2023-01-04 06:58:51 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SYSLOG_BUFFER
|
2023-05-23 14:14:58 +02:00
|
|
|
|
2023-01-04 06:58:51 +01:00
|
|
|
/* Do we have an IO buffer? */
|
|
|
|
|
2023-05-23 14:14:58 +02:00
|
|
|
if (stream->base != NULL)
|
2023-01-04 06:58:51 +01:00
|
|
|
{
|
|
|
|
/* Add the incoming string to the buffer */
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
ret = syslograwstream_addstring(stream, buff, len);
|
2023-01-04 06:58:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Try writing until the write was successful or until an
|
|
|
|
* irrecoverable error occurs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Write the buffer to the supported logging device. On
|
|
|
|
* failure, syslog_write returns a negated errno value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = syslog_write(buff, len);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
this->nput += ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The special return value -EINTR means that syslog_putc() was
|
|
|
|
* awakened by a signal. This is not a real error and must be
|
|
|
|
* ignored in this context.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
while (ret == -EINTR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-05-23 16:06:32 +02:00
|
|
|
* Name: lib_syslograwstream_open
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initializes a stream for use with the configured syslog interface.
|
2016-06-19 15:56:24 +02:00
|
|
|
* Only accessible from with the OS SYSLOG logic.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2016-06-19 15:56:24 +02:00
|
|
|
* stream - User allocated, uninitialized instance of struct
|
2023-05-23 16:06:32 +02:00
|
|
|
* lib_syslograwstream_s to be initialized.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None (User allocated instance initialized).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
void lib_syslograwstream_open(FAR struct lib_syslograwstream_s *stream)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2017-05-12 14:58:33 +02:00
|
|
|
DEBUGASSERT(stream != NULL);
|
2017-05-11 01:36:08 +02:00
|
|
|
|
|
|
|
/* Initialize the common fields */
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
stream->public.putc = syslograwstream_putc;
|
|
|
|
stream->public.puts = syslograwstream_puts;
|
2017-05-10 22:42:43 +02:00
|
|
|
stream->public.nput = 0;
|
2017-05-11 01:36:08 +02:00
|
|
|
|
2017-05-12 14:58:33 +02:00
|
|
|
#ifdef CONFIG_SYSLOG_BUFFER
|
2023-05-23 16:06:32 +02:00
|
|
|
stream->public.flush = syslograwstream_flush;
|
2023-05-23 14:14:58 +02:00
|
|
|
|
2017-05-11 01:36:08 +02:00
|
|
|
/* Allocate an IOB */
|
|
|
|
|
2023-05-23 14:14:58 +02:00
|
|
|
# ifdef CONFIG_MM_IOB
|
2022-12-15 15:51:14 +01:00
|
|
|
stream->iob = iob_tryalloc(true);
|
2023-05-23 14:14:58 +02:00
|
|
|
if (stream->iob != NULL)
|
|
|
|
{
|
|
|
|
stream->base = (FAR void *)stream->iob->io_data;
|
|
|
|
stream->size = sizeof(stream->iob->io_data);
|
|
|
|
}
|
2023-06-14 08:08:40 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
stream->base = NULL;
|
|
|
|
stream->size = 0;
|
|
|
|
}
|
2023-05-23 14:14:58 +02:00
|
|
|
# else
|
|
|
|
stream->base = stream->buffer;
|
|
|
|
stream->size = sizeof(stream->buffer);
|
|
|
|
# endif
|
2023-06-14 08:08:40 +02:00
|
|
|
stream->offset = 0;
|
2023-05-23 14:14:58 +02:00
|
|
|
#else
|
|
|
|
stream->public.flush = lib_noflush;
|
2017-05-10 22:42:43 +02:00
|
|
|
#endif
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2017-05-11 01:36:08 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2023-05-23 16:06:32 +02:00
|
|
|
* Name: lib_syslograwstream_close
|
2017-05-11 01:36:08 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free resources held by the syslog stream.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2017-05-11 01:36:08 +02:00
|
|
|
* stream - User allocated, uninitialized instance of struct
|
2023-05-23 16:06:32 +02:00
|
|
|
* lib_syslograwstream_s to be initialized.
|
2017-05-11 01:36:08 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None (Resources freed).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-12-30 10:09:45 +01:00
|
|
|
#ifdef CONFIG_SYSLOG_BUFFER
|
2023-05-23 16:06:32 +02:00
|
|
|
void lib_syslograwstream_close(FAR struct lib_syslograwstream_s *stream)
|
2017-05-11 01:36:08 +02:00
|
|
|
{
|
|
|
|
DEBUGASSERT(stream != NULL);
|
|
|
|
|
|
|
|
/* Verify that there is an IOB attached (there should be) */
|
|
|
|
|
2023-05-23 14:14:58 +02:00
|
|
|
# ifdef CONFIG_MM_IOB
|
2017-05-11 01:36:08 +02:00
|
|
|
if (stream->iob != NULL)
|
|
|
|
{
|
2017-05-12 14:58:33 +02:00
|
|
|
/* Flush the output buffered in the IOB */
|
|
|
|
|
2023-05-23 16:06:32 +02:00
|
|
|
syslograwstream_flush(&stream->public);
|
2017-05-12 14:58:33 +02:00
|
|
|
|
2017-05-11 01:36:08 +02:00
|
|
|
/* Free the IOB */
|
|
|
|
|
2022-08-08 04:21:03 +02:00
|
|
|
iob_free(stream->iob);
|
2017-05-11 01:36:08 +02:00
|
|
|
stream->iob = NULL;
|
|
|
|
}
|
2023-05-23 14:14:58 +02:00
|
|
|
# else
|
2023-05-23 16:06:32 +02:00
|
|
|
syslograwstream_flush(&stream->public);
|
2023-05-23 14:14:58 +02:00
|
|
|
# endif
|
2022-12-29 14:56:27 +01:00
|
|
|
}
|
2022-12-30 10:09:45 +01:00
|
|
|
#endif
|