2012-08-02 19:09:25 +02:00
|
|
|
/****************************************************************************
|
2016-08-07 17:43:48 +02:00
|
|
|
* drivers/syslog/syslog_device.c
|
2012-08-02 19:09:25 +02:00
|
|
|
*
|
2016-06-19 16:46:46 +02:00
|
|
|
* Copyright (C) 2012, 2016 Gregory Nutt. All rights reserved.
|
2012-08-02 19:09:25 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
|
|
|
* COPYRIGHT OWNER 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2013-01-28 18:43:55 +01:00
|
|
|
#include <stdio.h>
|
2012-08-02 19:09:25 +02:00
|
|
|
#include <unistd.h>
|
2016-06-19 21:03:20 +02:00
|
|
|
#include <string.h>
|
2016-06-21 20:25:15 +02:00
|
|
|
#include <sched.h>
|
2012-08-02 19:09:25 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2012-08-02 20:43:01 +02:00
|
|
|
#include <nuttx/arch.h>
|
2016-06-21 20:25:15 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2014-04-10 17:29:30 +02:00
|
|
|
#include <nuttx/syslog/syslog.h>
|
2012-08-02 19:09:25 +02:00
|
|
|
|
2016-06-19 21:03:20 +02:00
|
|
|
#include "syslog.h"
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2012-08-02 19:09:25 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Open the device/file write-only, try to create (file) it if it doesn't
|
|
|
|
* exist, if the file that already exists, then append the new log data to
|
|
|
|
* end of the file.
|
|
|
|
*/
|
2012-08-02 19:09:25 +02:00
|
|
|
|
|
|
|
#define SYSLOG_OFLAGS (O_WRONLY | O_CREAT | O_APPEND)
|
|
|
|
|
2012-08-02 20:43:01 +02:00
|
|
|
/* An invalid thread ID */
|
|
|
|
|
|
|
|
#define NO_HOLDER ((pid_t)-1)
|
|
|
|
|
2012-08-02 19:09:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This enumeration represents the state of the SYSLOG device interface */
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
enum syslog_dev_state
|
2012-08-02 19:09:25 +02:00
|
|
|
{
|
|
|
|
SYSLOG_UNINITIALIZED = 0, /* SYSLOG has not been initialized */
|
2012-08-02 20:43:01 +02:00
|
|
|
SYSLOG_INITIALIZING, /* SYSLOG is being initialized */
|
2012-08-02 19:09:25 +02:00
|
|
|
SYSLOG_REOPEN, /* SYSLOG open failed... try again later */
|
|
|
|
SYSLOG_FAILURE, /* SYSLOG open failed... don't try again */
|
|
|
|
SYSLOG_OPENED, /* SYSLOG device is open and ready to use */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure contains all SYSLOGing state information */
|
|
|
|
|
|
|
|
struct syslog_dev_s
|
|
|
|
{
|
2016-06-21 20:25:15 +02:00
|
|
|
uint8_t sl_state; /* See enum syslog_dev_state */
|
|
|
|
uint8_t sl_oflags; /* Saved open mode (for re-open) */
|
|
|
|
uint16_t sl_mode; /* Saved open flags (for re-open) */
|
|
|
|
sem_t sl_sem; /* Enforces mutually exclusive access */
|
|
|
|
pid_t sl_holder; /* PID of the thread that holds the semaphore */
|
|
|
|
struct file sl_file; /* The syslog file structure */
|
|
|
|
FAR char *sl_devpath; /* Full path to the character device */
|
2012-08-02 19:09:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This is the device structure for the console or syslogging function. */
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
static struct syslog_dev_s g_syslog_dev;
|
2015-10-11 19:39:29 +02:00
|
|
|
static const uint8_t g_syscrlf[2] =
|
|
|
|
{
|
|
|
|
'\r', '\n'
|
|
|
|
};
|
2012-08-02 19:09:25 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-08-02 20:43:01 +02:00
|
|
|
/****************************************************************************
|
2016-06-19 19:16:12 +02:00
|
|
|
* Name: syslog_dev_takesem
|
2012-08-02 20:43:01 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to the syslog device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
static inline int syslog_dev_takesem(void)
|
2012-08-02 20:43:01 +02:00
|
|
|
{
|
|
|
|
pid_t me = getpid();
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Does this thread already hold the semaphore? That could happen if
|
2016-07-05 20:12:44 +02:00
|
|
|
* we were called recursively, i.e., if the logic kicked off by
|
|
|
|
* file_write() where to generate more debug output. Return an
|
|
|
|
* error in that case.
|
2012-08-02 20:43:01 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
if (g_syslog_dev.sl_holder == me)
|
2012-08-02 20:43:01 +02:00
|
|
|
{
|
|
|
|
/* Return an error (instead of deadlocking) */
|
|
|
|
|
|
|
|
return -EWOULDBLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Either the semaphore is available or is currently held by another
|
|
|
|
* thread. Wait for it to become available.
|
|
|
|
*/
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
ret = sem_wait(&g_syslog_dev.sl_sem);
|
2012-08-02 20:43:01 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return -get_errno();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We hold the semaphore. We can safely mark ourself as the holder
|
|
|
|
* of the semaphore.
|
|
|
|
*/
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
g_syslog_dev.sl_holder = me;
|
2012-08-02 20:43:01 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-06-19 19:16:12 +02:00
|
|
|
* Name: syslog_dev_givesem
|
2012-08-02 20:43:01 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to the syslog device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
static inline void syslog_dev_givesem(void)
|
2012-08-02 20:43:01 +02:00
|
|
|
{
|
2016-06-13 19:48:20 +02:00
|
|
|
#ifdef CONFIG_DEBUG_ASSERTIONS
|
2012-08-02 20:43:01 +02:00
|
|
|
pid_t me = getpid();
|
2016-06-19 19:16:12 +02:00
|
|
|
DEBUGASSERT(g_syslog_dev.sl_holder == me);
|
2013-01-29 18:42:58 +01:00
|
|
|
#endif
|
2012-08-02 20:43:01 +02:00
|
|
|
|
|
|
|
/* Relinquish the semaphore */
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
g_syslog_dev.sl_holder = NO_HOLDER;
|
|
|
|
sem_post(&g_syslog_dev.sl_sem);
|
2012-08-02 20:43:01 +02:00
|
|
|
}
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2017-05-10 22:42:43 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: syslog_dev_outputready
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Ignore any output:
|
|
|
|
*
|
|
|
|
* (1) Before the SYSLOG device has been initialized. This could happen
|
|
|
|
* from debug output that occurs early in the boot sequence before
|
|
|
|
* syslog_dev_initialize() is called (SYSLOG_UNINITIALIZED).
|
|
|
|
* (2) While the device is being initialized. The case could happen if
|
|
|
|
* debug output is generated while syslog_dev_initialize() executes
|
|
|
|
* (SYSLOG_INITIALIZING).
|
|
|
|
* (3) While we are generating SYSLOG output. The case could happen if
|
|
|
|
* debug output is generated while syslog_dev_putc() executes
|
|
|
|
* (This case is actually handled inside of syslog_semtake()).
|
|
|
|
* (4) Any debug output generated from interrupt handlers. A disadvantage
|
|
|
|
* of using the generic character device for the SYSLOG is that it
|
|
|
|
* cannot handle debug output generated from interrupt level handlers.
|
|
|
|
* (5) Any debug output generated from the IDLE loop. The character
|
|
|
|
* driver interface is blocking and the IDLE thread is not permitted
|
|
|
|
* to block.
|
|
|
|
* (6) If an irrecoverable failure occurred during initialization. In
|
|
|
|
* this case, we won't ever bother to try again (ever).
|
|
|
|
*
|
|
|
|
* NOTE: That the third case is different. It applies only to the thread
|
|
|
|
* that currently holds the sl_sem sempaphore. Other threads should wait.
|
|
|
|
* that is why that case is handled in syslog_semtake().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* ch - The character to add to the SYSLOG (must be positive).
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, the character is echoed back to the caller. A negated
|
|
|
|
* errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int syslog_dev_outputready(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Cases (4) and (5) */
|
|
|
|
|
|
|
|
if (up_interrupt_context() || getpid() == 0)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can save checks in the usual case: That after the SYSLOG device
|
|
|
|
* has been successfully opened.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_syslog_dev.sl_state != SYSLOG_OPENED)
|
|
|
|
{
|
|
|
|
/* Case (1) and (2) */
|
|
|
|
|
|
|
|
if (g_syslog_dev.sl_state == SYSLOG_UNINITIALIZED ||
|
|
|
|
g_syslog_dev.sl_state == SYSLOG_INITIALIZING)
|
|
|
|
{
|
|
|
|
return -EAGAIN; /* Can't access the SYSLOG now... maybe next time? */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Case (6) */
|
|
|
|
|
|
|
|
if (g_syslog_dev.sl_state == SYSLOG_FAILURE)
|
|
|
|
{
|
|
|
|
return -ENXIO; /* There is no SYSLOG device */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* syslog_dev_initialize() is called as soon as enough of the operating
|
|
|
|
* system is in place to support the open operation... but it is
|
|
|
|
* possible that the SYSLOG device is not yet registered at that time.
|
|
|
|
* In this case, we know that the system is sufficiently initialized
|
|
|
|
* to support an attempt to re-open the SYSLOG device.
|
|
|
|
*
|
|
|
|
* NOTE that the scheduler is locked. That is because we do not have
|
|
|
|
* fully initialized semaphore capability until the SYSLOG device is
|
|
|
|
* successfully initialized
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
if (g_syslog_dev.sl_state == SYSLOG_REOPEN)
|
|
|
|
{
|
|
|
|
/* Try again to initialize the device. We may do this repeatedly
|
|
|
|
* because the log device might be something that was not ready
|
|
|
|
* the first time that syslog_dev_initializee() was called (such as a
|
|
|
|
* USB serial device that has not yet been connected or a file in
|
|
|
|
* an NFS mounted file system that has not yet been mounted).
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(g_syslog_dev.sl_devpath != NULL);
|
|
|
|
ret = syslog_dev_initialize(g_syslog_dev.sl_devpath,
|
|
|
|
(int)g_syslog_dev.sl_oflags,
|
|
|
|
(int)g_syslog_dev.sl_mode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
sched_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
DEBUGASSERT(g_syslog_dev.sl_state == SYSLOG_OPENED);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-08-02 19:09:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-06-19 17:12:33 +02:00
|
|
|
* Name: syslog_dev_initialize
|
2012-08-02 19:09:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize to use the character device (or file) at
|
|
|
|
* CONFIG_SYSLOG_DEVPATH as the SYSLOG sink.
|
|
|
|
*
|
2016-06-19 17:12:33 +02:00
|
|
|
* One power up, the SYSLOG facility is non-existent or limited to very
|
|
|
|
* low-level output. This function may be called later in the
|
|
|
|
* intialization sequence after full driver support has been initialized.
|
|
|
|
* (via syslog_initialize()) It installs the configured SYSLOG drivers
|
|
|
|
* and enables full SYSLOGing capability.
|
|
|
|
*
|
2012-08-02 19:09:25 +02:00
|
|
|
* NOTE that this implementation excludes using a network connection as
|
|
|
|
* SYSLOG device. That would be a good extension.
|
|
|
|
*
|
2016-06-19 17:12:33 +02:00
|
|
|
* Input Parameters:
|
2016-06-19 21:03:20 +02:00
|
|
|
* devpath - The full path to the character device to be used.
|
2016-06-21 20:25:15 +02:00
|
|
|
* oflags - File open flags
|
|
|
|
* mode - File open mode (only if oflags include O_CREAT)
|
2016-06-19 17:12:33 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
2012-08-02 19:09:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-21 20:25:15 +02:00
|
|
|
int syslog_dev_initialize(FAR const char *devpath, int oflags, int mode)
|
2012-08-02 19:09:25 +02:00
|
|
|
{
|
2016-06-19 16:46:46 +02:00
|
|
|
int fd;
|
|
|
|
int ret;
|
2012-08-02 19:09:25 +02:00
|
|
|
|
|
|
|
/* At this point, the only expected states are SYSLOG_UNINITIALIZED or
|
2012-08-02 20:43:01 +02:00
|
|
|
* SYSLOG_REOPEN.. Not SYSLOG_INITIALIZING, SYSLOG_FAILURE, SYSLOG_OPENED.
|
2012-08-02 19:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
DEBUGASSERT(g_syslog_dev.sl_state == SYSLOG_UNINITIALIZED ||
|
|
|
|
g_syslog_dev.sl_state == SYSLOG_REOPEN);
|
2012-08-02 19:09:25 +02:00
|
|
|
|
2017-05-11 21:35:56 +02:00
|
|
|
/* Save the path to the device in case we have to re-open it.
|
2016-06-19 21:03:20 +02:00
|
|
|
* If we get here and sl_devpath is not equal to NULL, that is a clue
|
|
|
|
* that we will are re-openingthe file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_syslog_dev.sl_state == SYSLOG_REOPEN)
|
|
|
|
{
|
|
|
|
/* Re-opening: Then we should already have a copy of the path to the
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(g_syslog_dev.sl_devpath != NULL &&
|
|
|
|
strcmp(g_syslog_dev.sl_devpath, devpath) == 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Initializing. Copy the device path so that we can use it if we
|
|
|
|
* have to re-open the file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(g_syslog_dev.sl_devpath == NULL);
|
2016-06-21 20:25:15 +02:00
|
|
|
g_syslog_dev.sl_oflags = oflags;
|
|
|
|
g_syslog_dev.sl_mode = mode;
|
2016-06-19 21:03:20 +02:00
|
|
|
g_syslog_dev.sl_devpath = strdup(devpath);
|
|
|
|
DEBUGASSERT(g_syslog_dev.sl_devpath != NULL);
|
|
|
|
}
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
g_syslog_dev.sl_state = SYSLOG_INITIALIZING;
|
2012-08-02 19:09:25 +02:00
|
|
|
|
2016-06-19 16:46:46 +02:00
|
|
|
/* Open the device driver. */
|
2012-08-02 19:09:25 +02:00
|
|
|
|
2016-06-21 20:25:15 +02:00
|
|
|
fd = open(devpath, oflags, mode);
|
2016-06-19 16:46:46 +02:00
|
|
|
if (fd < 0)
|
2012-08-02 19:09:25 +02:00
|
|
|
{
|
2016-06-19 16:46:46 +02:00
|
|
|
int errcode = get_errno();
|
|
|
|
DEBUGASSERT(errcode > 0);
|
|
|
|
|
|
|
|
/* We failed to open the file. Perhaps it does exist? Perhaps it
|
|
|
|
* exists, but is not ready because it depends on insertion of a
|
|
|
|
* removable device?
|
|
|
|
*
|
|
|
|
* In any case we will attempt to re-open the device repeatedly.
|
|
|
|
* The assumption is that the device path is valid but that the
|
|
|
|
* driver has not yet been registered or a removable device has
|
|
|
|
* not yet been installed.
|
2012-08-02 19:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
g_syslog_dev.sl_state = SYSLOG_REOPEN;
|
2016-06-19 16:46:46 +02:00
|
|
|
return -errcode;
|
2012-08-02 19:09:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-19 16:46:46 +02:00
|
|
|
/* Detach the file descriptor from the file structure. The file
|
|
|
|
* descriptor is a task-specific concept. Detaching the file
|
|
|
|
* descriptor allows us to use the device on all threads in all tasks.
|
2012-08-02 19:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
ret = file_detach(fd, &g_syslog_dev.sl_file);
|
2016-06-19 16:46:46 +02:00
|
|
|
if (ret < 0)
|
2012-08-02 19:09:25 +02:00
|
|
|
{
|
2016-06-19 16:46:46 +02:00
|
|
|
/* This should not happen and means that something very bad has
|
|
|
|
* occurred.
|
2012-08-02 19:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
g_syslog_dev.sl_state = SYSLOG_FAILURE;
|
2016-06-19 16:46:46 +02:00
|
|
|
close(fd);
|
|
|
|
return ret;
|
2012-08-02 19:09:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The SYSLOG device is open and ready for writing. */
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
sem_init(&g_syslog_dev.sl_sem, 0, 1);
|
|
|
|
g_syslog_dev.sl_holder = NO_HOLDER;
|
|
|
|
g_syslog_dev.sl_state = SYSLOG_OPENED;
|
2012-08-02 19:09:25 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2016-06-21 20:25:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: syslog_dev_uninitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called to disable the last device/file channel in preparation to use
|
|
|
|
* a different SYSLOG device. Currently only used for CONFIG_SYSLOG_FILE.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The caller has already switched the SYSLOG source to some safe channel
|
|
|
|
* (the default channel).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSLOG_FILE /* Currently only used in this configuration */
|
|
|
|
int syslog_dev_uninitialize(void)
|
|
|
|
{
|
|
|
|
/* Attempt to flush any buffered data */
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
(void)syslog_dev_flush();
|
|
|
|
|
|
|
|
/* Close the detached file instance */
|
|
|
|
|
|
|
|
(void)file_close_detached(&g_syslog_dev.sl_file);
|
|
|
|
|
|
|
|
/* Free the device path */
|
|
|
|
|
|
|
|
if (g_syslog_dev.sl_devpath != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(g_syslog_dev.sl_devpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy the semaphore */
|
|
|
|
|
|
|
|
sem_destroy(&g_syslog_dev.sl_sem);
|
|
|
|
|
|
|
|
/* Reset the state structure */
|
|
|
|
|
|
|
|
memset(&g_syslog_dev, 0, sizeof(struct syslog_dev_s));
|
|
|
|
sched_unlock();
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_SYSLOG_FILE */
|
|
|
|
|
2012-08-02 19:09:25 +02:00
|
|
|
/****************************************************************************
|
2017-05-10 22:42:43 +02:00
|
|
|
* Name: syslog_dev_write
|
2012-08-02 19:09:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-05-10 22:42:43 +02:00
|
|
|
* This is the low-level, multile byte, system logging interface provided
|
|
|
|
* for the character driver interface.
|
2016-06-19 21:03:20 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-05-10 22:42:43 +02:00
|
|
|
* buffer - The buffer containing the data to be output
|
|
|
|
* buflen - The number of bytes in the buffer
|
2016-06-19 21:03:20 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-05-10 22:42:43 +02:00
|
|
|
* On success, the character is echoed back to the caller. Minus one
|
|
|
|
* is returned on any failure with the errno set correctly.
|
2012-08-02 19:09:25 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-10 22:42:43 +02:00
|
|
|
ssize_t syslog_dev_write(FAR const char *buffer, size_t buflen)
|
2012-08-02 19:09:25 +02:00
|
|
|
{
|
2017-05-10 22:42:43 +02:00
|
|
|
FAR const char *endptr;
|
|
|
|
ssize_t nwritten;
|
|
|
|
size_t writelen;
|
2017-05-11 00:13:21 +02:00
|
|
|
size_t remaining;
|
2013-01-28 18:43:55 +01:00
|
|
|
int errcode;
|
2012-08-02 19:09:25 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-05-10 22:42:43 +02:00
|
|
|
/* Check if the system is ready to do output operations */
|
2012-08-02 19:09:25 +02:00
|
|
|
|
2017-05-10 22:42:43 +02:00
|
|
|
ret = syslog_dev_outputready();
|
|
|
|
if (ret < 0)
|
2012-08-02 19:09:25 +02:00
|
|
|
{
|
2017-05-10 22:42:43 +02:00
|
|
|
errcode = -ret;
|
2013-01-28 18:43:55 +01:00
|
|
|
goto errout_with_errcode;
|
2012-08-02 19:09:25 +02:00
|
|
|
}
|
|
|
|
|
2017-05-10 22:42:43 +02:00
|
|
|
/* The syslog device is ready for writing */
|
2012-08-02 19:09:25 +02:00
|
|
|
|
2017-05-10 22:42:43 +02:00
|
|
|
ret = syslog_dev_takesem();
|
|
|
|
if (ret < 0)
|
2012-08-02 19:09:25 +02:00
|
|
|
{
|
2017-05-10 22:42:43 +02:00
|
|
|
/* We probably already hold the semaphore and were probably
|
|
|
|
* re-entered by the logic kicked off by file_write().
|
|
|
|
* We might also have been interrupted by a signal. Either
|
|
|
|
* way, we are outta here.
|
|
|
|
*/
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2017-05-10 22:42:43 +02:00
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_errcode;
|
|
|
|
}
|
2012-08-04 00:04:14 +02:00
|
|
|
|
2017-05-10 22:42:43 +02:00
|
|
|
/* Loop until we have output all characters */
|
2012-08-04 00:04:14 +02:00
|
|
|
|
2017-05-11 00:13:21 +02:00
|
|
|
for (endptr = buffer, remaining = buflen;
|
|
|
|
remaining > 0;
|
|
|
|
endptr++, remaining--)
|
2017-05-10 22:42:43 +02:00
|
|
|
{
|
2017-05-12 16:50:56 +02:00
|
|
|
/* Check for carriage return or line feed */
|
2017-05-12 15:31:50 +02:00
|
|
|
|
2017-05-12 16:50:56 +02:00
|
|
|
if (*endptr == '\r' || *endptr == '\n')
|
2017-05-12 15:31:50 +02:00
|
|
|
{
|
2017-05-12 16:50:56 +02:00
|
|
|
/* Check for pre-formatted CR-LF sequence */
|
2017-05-11 00:13:21 +02:00
|
|
|
|
2017-05-12 16:50:56 +02:00
|
|
|
if (remaining > 1 &&
|
|
|
|
((endptr[0] == '\r' && endptr[1] == '\n') ||
|
|
|
|
(endptr[0] == '\n' && endptr[1] == '\r')))
|
|
|
|
{
|
|
|
|
/* Just skip over pre-formatted CR-LF or LF-CR sequence */
|
2017-05-11 00:13:21 +02:00
|
|
|
|
2017-05-12 16:50:56 +02:00
|
|
|
endptr++;
|
|
|
|
remaining--;
|
|
|
|
}
|
|
|
|
else
|
2017-05-10 22:42:43 +02:00
|
|
|
{
|
2017-05-12 16:50:56 +02:00
|
|
|
/* Write everything up to the position of the special
|
|
|
|
* character.
|
|
|
|
*
|
|
|
|
* - buffer points to next byte to output.
|
|
|
|
* - endptr points to the special character.
|
|
|
|
*/
|
|
|
|
|
|
|
|
writelen = (size_t)((uintptr_t)endptr - (uintptr_t)buffer);
|
|
|
|
if (writelen > 0)
|
2017-05-10 22:42:43 +02:00
|
|
|
{
|
2017-05-12 16:50:56 +02:00
|
|
|
nwritten = file_write(&g_syslog_dev.sl_file, buffer, writelen);
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
errcode = -nwritten;
|
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
2017-05-10 22:42:43 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 16:50:56 +02:00
|
|
|
/* Ignore the carriage return, but for the linefeed, output
|
|
|
|
* both a carriage return and a linefeed.
|
|
|
|
*/
|
2017-05-10 22:42:43 +02:00
|
|
|
|
2017-05-12 16:50:56 +02:00
|
|
|
if (*endptr == '\n')
|
2017-05-10 22:42:43 +02:00
|
|
|
{
|
2017-05-12 16:50:56 +02:00
|
|
|
nwritten = file_write(&g_syslog_dev.sl_file, g_syscrlf, 2);
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
errcode = -nwritten;
|
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
2017-05-10 22:42:43 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 16:50:56 +02:00
|
|
|
/* Adjust pointers */
|
2017-05-11 00:13:21 +02:00
|
|
|
|
2017-05-12 16:50:56 +02:00
|
|
|
writelen++; /* Skip the special character */
|
|
|
|
buffer += writelen; /* Points past the special character */
|
|
|
|
}
|
2012-08-04 00:04:14 +02:00
|
|
|
}
|
2017-05-10 22:42:43 +02:00
|
|
|
}
|
2012-08-04 00:04:14 +02:00
|
|
|
|
2017-05-11 00:13:21 +02:00
|
|
|
/* Write any unterminated data at the end of the buffer.
|
2017-05-10 22:42:43 +02:00
|
|
|
*
|
|
|
|
* - buffer points to next byte to output.
|
2017-05-11 00:13:21 +02:00
|
|
|
* - endptr points to the end of the buffer plus 1.
|
2017-05-10 22:42:43 +02:00
|
|
|
*/
|
2012-08-02 19:09:25 +02:00
|
|
|
|
2017-05-10 22:42:43 +02:00
|
|
|
writelen = (size_t)((uintptr_t)endptr - (uintptr_t)buffer);
|
|
|
|
if (writelen > 0)
|
|
|
|
{
|
|
|
|
nwritten = file_write(&g_syslog_dev.sl_file, buffer, writelen);
|
|
|
|
if (nwritten < 0)
|
2012-08-02 19:09:25 +02:00
|
|
|
{
|
2017-05-10 22:42:43 +02:00
|
|
|
errcode = -nwritten;
|
|
|
|
goto errout_with_sem;
|
2012-08-02 19:09:25 +02:00
|
|
|
}
|
2017-05-10 22:42:43 +02:00
|
|
|
}
|
2012-08-02 19:09:25 +02:00
|
|
|
|
2017-05-10 22:42:43 +02:00
|
|
|
syslog_dev_givesem();
|
|
|
|
return buflen;
|
|
|
|
|
|
|
|
errout_with_sem:
|
|
|
|
syslog_dev_givesem();
|
|
|
|
errout_with_errcode:
|
|
|
|
set_errno(errcode);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: syslog_dev_putc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the low-level, single character, system logging interface
|
|
|
|
* provided for the character driver interface.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* ch - The character to add to the SYSLOG (must be positive).
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, the character is echoed back to the caller. Minus one
|
|
|
|
* is returned on any failure with the errno set correctly.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int syslog_dev_putc(int ch)
|
|
|
|
{
|
|
|
|
ssize_t nbytes;
|
|
|
|
uint8_t uch;
|
|
|
|
int errcode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Check if the system is ready to do output operations */
|
|
|
|
|
|
|
|
ret = syslog_dev_outputready();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_errcode;
|
2012-08-04 00:04:14 +02:00
|
|
|
}
|
2012-08-02 19:09:25 +02:00
|
|
|
|
|
|
|
/* Ignore carriage returns */
|
|
|
|
|
|
|
|
if (ch == '\r')
|
|
|
|
{
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The syslog device is ready for writing and we have something of
|
|
|
|
* value to write.
|
|
|
|
*/
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
ret = syslog_dev_takesem();
|
2012-08-02 19:09:25 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-08-02 20:43:01 +02:00
|
|
|
/* We probably already hold the semaphore and were probably
|
2016-07-05 20:12:44 +02:00
|
|
|
* re-entered by the logic kicked off by file_write().
|
2012-08-02 20:43:01 +02:00
|
|
|
* We might also have been interrupted by a signal. Either
|
|
|
|
* way, we are outta here.
|
|
|
|
*/
|
|
|
|
|
2013-01-28 18:43:55 +01:00
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_errcode;
|
2012-08-02 19:09:25 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 20:43:01 +02:00
|
|
|
/* Pre-pend a newline with a carriage return. */
|
2012-08-02 19:09:25 +02:00
|
|
|
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
/* Write the CR-LF sequence */
|
|
|
|
|
2016-07-05 20:12:44 +02:00
|
|
|
nbytes = file_write(&g_syslog_dev.sl_file, g_syscrlf, 2);
|
2012-08-02 19:09:25 +02:00
|
|
|
|
|
|
|
/* Synchronize the file when each CR-LF is encountered (i.e.,
|
|
|
|
* implements line buffering always).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
2016-06-19 21:03:20 +02:00
|
|
|
(void)syslog_dev_flush();
|
2012-08-02 19:09:25 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Write the non-newline character (and don't flush) */
|
|
|
|
|
2013-01-28 18:43:55 +01:00
|
|
|
uch = (uint8_t)ch;
|
2016-07-05 20:12:44 +02:00
|
|
|
nbytes = file_write(&g_syslog_dev.sl_file, &uch, 1);
|
2012-08-02 19:09:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-19 19:16:12 +02:00
|
|
|
syslog_dev_givesem();
|
2012-08-02 19:09:25 +02:00
|
|
|
|
2013-01-28 18:43:55 +01:00
|
|
|
/* Check if the write was successful. If not, nbytes will be
|
|
|
|
* a negated errno value.
|
|
|
|
*/
|
2012-08-02 19:09:25 +02:00
|
|
|
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
2013-01-28 18:43:55 +01:00
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_errcode;
|
2012-08-02 19:09:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ch;
|
2013-01-28 18:43:55 +01:00
|
|
|
|
|
|
|
errout_with_errcode:
|
|
|
|
set_errno(errcode);
|
|
|
|
return EOF;
|
2012-08-02 19:09:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-19 21:03:20 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: syslog_dev_flush
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Flush any buffer data in the file system to media.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int syslog_dev_flush(void)
|
|
|
|
{
|
2016-06-22 21:35:53 +02:00
|
|
|
#if defined(CONFIG_SYSLOG_FILE) && !defined(CONFIG_DISABLE_MOUNTPOINT)
|
2016-07-05 20:12:44 +02:00
|
|
|
/* Ignore return value, always return success. file_fsync() could fail
|
|
|
|
* because the file is not open, the inode is not a mountpoint, or the
|
|
|
|
* mountpoint does not support the sync() method.
|
|
|
|
*/
|
2016-06-19 21:03:20 +02:00
|
|
|
|
2016-07-05 20:12:44 +02:00
|
|
|
(void)file_fsync(&g_syslog_dev.sl_file);
|
2016-06-19 21:03:20 +02:00
|
|
|
#endif
|
|
|
|
|
2016-07-05 20:12:44 +02:00
|
|
|
return OK;
|
2016-06-19 21:03:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NFILE_DESCRIPTORS > 0 */
|