2016-06-21 21:33:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/syslog/syslog_filechannel.c
|
|
|
|
*
|
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
|
2016-06-21 21:33:17 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-21 21:33:17 +02: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.
|
2016-06-21 21:33:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2021-06-03 15:51:10 +02:00
|
|
|
#include <unistd.h>
|
2016-06-22 00:39:28 +02:00
|
|
|
#include <sched.h>
|
2016-06-21 21:33:17 +02:00
|
|
|
#include <fcntl.h>
|
2021-03-29 14:36:58 +02:00
|
|
|
#include <errno.h>
|
2021-06-03 15:51:10 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2021-06-04 11:12:25 +02:00
|
|
|
#include <sys/types.h>
|
2016-06-21 21:33:17 +02:00
|
|
|
|
|
|
|
#include <nuttx/syslog/syslog.h>
|
2021-06-04 11:12:25 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2021-06-08 12:17:31 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2016-06-21 21:33:17 +02:00
|
|
|
|
|
|
|
#include "syslog.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSLOG_FILE
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define OPEN_FLAGS (O_WRONLY | O_CREAT | O_APPEND)
|
|
|
|
#define OPEN_MODE (S_IROTH | S_IRGRP | S_IRUSR | S_IWUSR)
|
|
|
|
|
2021-06-03 15:51:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-08 12:17:31 +02:00
|
|
|
#ifdef CONFIG_SYSLOG_FILE_SEPARATE
|
|
|
|
static void log_separate(FAR const char *log_file)
|
|
|
|
{
|
|
|
|
struct file fp;
|
|
|
|
|
2022-06-04 01:19:35 +02:00
|
|
|
if (file_open(&fp, log_file, (O_WRONLY | O_APPEND)) < 0)
|
2021-06-08 12:17:31 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_write(&fp, "\n\n", 2);
|
|
|
|
|
|
|
|
file_close(&fp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-04 11:12:25 +02:00
|
|
|
#if CONFIG_SYSLOG_FILE_ROTATIONS > 0
|
2021-06-03 15:51:10 +02:00
|
|
|
static void log_rotate(FAR const char *log_file)
|
|
|
|
{
|
2021-06-04 11:12:25 +02:00
|
|
|
int i;
|
2021-06-03 15:51:10 +02:00
|
|
|
off_t size;
|
|
|
|
struct stat f_stat;
|
2021-06-04 11:12:25 +02:00
|
|
|
size_t name_size;
|
|
|
|
FAR char *rotate_to;
|
|
|
|
FAR char *rotate_from;
|
2021-06-03 15:51:10 +02:00
|
|
|
|
|
|
|
/* Get the size of the current log file. */
|
|
|
|
|
2021-06-04 11:12:25 +02:00
|
|
|
if (stat(log_file, &f_stat) < 0)
|
2021-06-03 15:51:10 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = f_stat.st_size;
|
|
|
|
|
|
|
|
/* If it does not exceed the limit we are OK. */
|
|
|
|
|
|
|
|
if (size < CONFIG_SYSLOG_FILE_SIZE_LIMIT)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-04 11:12:25 +02:00
|
|
|
/* Rotated file names. */
|
2021-06-03 15:51:10 +02:00
|
|
|
|
2021-06-04 11:12:25 +02:00
|
|
|
name_size = strlen(log_file) + 8;
|
|
|
|
rotate_to = kmm_malloc(name_size);
|
|
|
|
rotate_from = kmm_malloc(name_size);
|
|
|
|
if ((rotate_to == NULL) || (rotate_from == NULL))
|
2021-06-03 15:51:10 +02:00
|
|
|
{
|
2021-06-04 11:12:25 +02:00
|
|
|
goto end;
|
2021-06-03 15:51:10 +02:00
|
|
|
}
|
|
|
|
|
2021-06-04 11:12:25 +02:00
|
|
|
/* Rotate the logs. */
|
2021-06-03 15:51:10 +02:00
|
|
|
|
2021-06-04 11:12:25 +02:00
|
|
|
for (i = (CONFIG_SYSLOG_FILE_ROTATIONS - 1); i > 0; i--)
|
2021-06-03 15:51:10 +02:00
|
|
|
{
|
2021-06-04 11:12:25 +02:00
|
|
|
snprintf(rotate_to, name_size, "%s.%d", log_file, i);
|
|
|
|
snprintf(rotate_from, name_size, "%s.%d", log_file, i - 1);
|
|
|
|
|
|
|
|
rename(rotate_from, rotate_to);
|
2021-06-03 15:51:10 +02:00
|
|
|
}
|
|
|
|
|
2021-06-04 11:12:25 +02:00
|
|
|
snprintf(rotate_to, name_size, "%s.0", log_file);
|
|
|
|
|
|
|
|
rename(log_file, rotate_to);
|
2021-06-03 15:51:10 +02:00
|
|
|
|
2021-06-04 11:12:25 +02:00
|
|
|
end:
|
|
|
|
kmm_free(rotate_to);
|
|
|
|
kmm_free(rotate_from);
|
2021-06-03 15:51:10 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-21 21:33:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: syslog_file_channel
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure to use a file in a mounted file system at 'devpath' as the
|
|
|
|
* SYSLOG channel.
|
|
|
|
*
|
|
|
|
* This tiny function is simply a wrapper around syslog_dev_initialize()
|
|
|
|
* and syslog_channel(). It calls syslog_dev_initialize() to configure
|
|
|
|
* the character file at 'devpath then calls syslog_channel() to use that
|
|
|
|
* device as the SYSLOG output channel.
|
|
|
|
*
|
|
|
|
* File SYSLOG channels differ from other SYSLOG channels in that they
|
|
|
|
* cannot be established until after fully booting and mounting the target
|
|
|
|
* file system. This function would need to be called from board-specific
|
|
|
|
* bring-up logic AFTER mounting the file system containing 'devpath'.
|
|
|
|
*
|
|
|
|
* SYSLOG data generated prior to calling syslog_file_channel will, of
|
|
|
|
* course, not be included in the file.
|
|
|
|
*
|
|
|
|
* NOTE interrupt level SYSLOG output will be lost in this case unless
|
|
|
|
* the interrupt buffer is used.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the file to be used for SYSLOG output.
|
|
|
|
* This may be an existing file or not. If the file exists,
|
|
|
|
* syslog_file_channel() will append new SYSLOG data to the end of the
|
|
|
|
* file. If it does not, then syslog_file_channel() will create the
|
|
|
|
* file.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2021-06-03 12:24:49 +02:00
|
|
|
* A pointer to the new SYSLOG channel; NULL is returned on any failure.
|
2016-06-21 21:33:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-03 12:24:49 +02:00
|
|
|
FAR struct syslog_channel_s *syslog_file_channel(FAR const char *devpath)
|
2016-06-21 21:33:17 +02:00
|
|
|
{
|
2022-01-13 13:45:44 +01:00
|
|
|
FAR struct syslog_channel_s *file_channel;
|
|
|
|
|
2016-06-21 21:33:17 +02:00
|
|
|
/* Reset the default SYSLOG channel so that we can safely modify the
|
|
|
|
* SYSLOG device. This is an atomic operation and we should be safe
|
|
|
|
* after the default channel has been selected.
|
|
|
|
*
|
|
|
|
* We disable pre-emption only so that we are not suspended and a lot of
|
2016-06-22 00:39:28 +02:00
|
|
|
* important debug output is lost while we futz with the channels.
|
2016-06-21 21:33:17 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-22 00:39:28 +02:00
|
|
|
sched_lock();
|
2016-06-21 21:33:17 +02:00
|
|
|
|
2021-06-03 15:51:10 +02:00
|
|
|
/* Rotate the log file, if needed. */
|
|
|
|
|
2021-06-04 11:12:25 +02:00
|
|
|
#if CONFIG_SYSLOG_FILE_ROTATIONS > 0
|
2021-06-03 15:51:10 +02:00
|
|
|
log_rotate(devpath);
|
|
|
|
#endif
|
|
|
|
|
2021-06-08 12:17:31 +02:00
|
|
|
/* Separate the old log entries. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSLOG_FILE_SEPARATE
|
|
|
|
log_separate(devpath);
|
|
|
|
#endif
|
|
|
|
|
2016-06-21 21:33:17 +02:00
|
|
|
/* Then initialize the file interface */
|
|
|
|
|
2022-01-13 13:45:44 +01:00
|
|
|
file_channel = syslog_dev_initialize(devpath, OPEN_FLAGS, OPEN_MODE);
|
|
|
|
if (file_channel == NULL)
|
2016-06-21 21:33:17 +02:00
|
|
|
{
|
2016-06-22 00:39:28 +02:00
|
|
|
goto errout_with_lock;
|
2016-06-21 21:33:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-22 00:39:28 +02:00
|
|
|
/* Use the file as the SYSLOG channel. If this fails we are pretty much
|
|
|
|
* screwed.
|
|
|
|
*/
|
|
|
|
|
2022-01-13 13:45:44 +01:00
|
|
|
if (syslog_channel(file_channel) != OK)
|
2021-06-03 12:24:49 +02:00
|
|
|
{
|
2022-01-13 13:45:44 +01:00
|
|
|
syslog_dev_uninitialize(file_channel);
|
|
|
|
file_channel = NULL;
|
2021-06-03 12:24:49 +02:00
|
|
|
}
|
2016-06-21 21:33:17 +02:00
|
|
|
|
2016-06-22 00:39:28 +02:00
|
|
|
errout_with_lock:
|
|
|
|
sched_unlock();
|
2022-01-13 13:45:44 +01:00
|
|
|
return file_channel;
|
2016-06-21 21:33:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_SYSLOG_FILE */
|