2 * Copyright (c) 2005-2008, The Android Open Source Project
3 * Copyright (c) 2009-2015, Samsung Electronics Co., Ltd. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <dlog_ioctl.h>
23 #include <logcommon.h>
24 #include <logconfig.h>
26 #define DEV_KMSG "/dev/kmsg"
27 #define BACKUP_SIZE 1048576
29 int g_minors[LOG_ID_MAX] = {-1, -1, -1, -1, -1};
30 int g_sizes[LOG_ID_MAX] = {-1, -1, -1, -1, -1};
33 * @brief Create KMSG edvices
34 * @details Spawns the KMSG devices for logging to be possible
35 * @param[in] fd The /dev/kmsg file descriptor
36 * @param[in] conf The configuration
37 * @return 0 on success, nonzero on failure
39 static int create_kmsg_devs(int fd, struct log_config *conf)
42 struct kmsg_cmd_buffer_add cmd = {
46 for (i = 0; i < LOG_ID_MAX; i++) {
47 char key[MAX_CONF_KEY_LEN];
51 snprintf(key, MAX_CONF_KEY_LEN, "%s_size", log_name_by_id(i));
52 size_str = log_config_get(conf, key);
56 cmd.size = BACKUP_SIZE;
58 cmd.size = strtol(size_str, &size_str_end, 10);
59 if (errno || (cmd.size <= 0) || (*size_str_end != '\0'))
60 cmd.size = BACKUP_SIZE;
63 g_sizes[i] = cmd.size;
65 if (0 > ioctl(fd, KMSG_CMD_BUFFER_ADD, &cmd)) {
66 _E("ioctl KMSG_CMD_BUFFER_ADD failed. (%d)\n", errno);
69 g_minors[i] = cmd.minor;
75 * @brief Remove KMSG devices
76 * @details Removes KMSG devices
77 * @param[in] fd The /dev/kmsg file descriptor
79 static void remove_kmsg_devs(int fd)
83 for (i = 0; i < LOG_ID_MAX; i++) {
86 if (0 > ioctl(fd, KMSG_CMD_BUFFER_DEL, &g_minors[i]))
87 _E("ioctl KMSG_CMD_BUFFER_DEL failed. (%d)\n", errno);
94 struct log_config conf_in;
95 struct log_config conf_out;
97 log_config_read(&conf_in);
99 memset(&conf_out, 0, sizeof(struct log_config));
101 kmsg_fd = open(DEV_KMSG, O_RDWR);
103 _E("Unable to open kmsg device. %d\n", errno);
107 if (0 > create_kmsg_devs(kmsg_fd, &conf_in))
110 log_config_free(&conf_in);
112 for (i = 0; i < LOG_ID_MAX; ++i) {
113 char key[MAX_CONF_KEY_LEN];
114 char val[MAX_CONF_VAL_LEN];
116 snprintf(key, MAX_CONF_KEY_LEN, "%s_size", log_name_by_id(i));
117 snprintf(val, MAX_CONF_VAL_LEN, "%d", g_sizes[i]);
118 log_config_push(&conf_out, key, val);
120 snprintf(val, MAX_CONF_VAL_LEN, "%s%d", DEV_KMSG, g_minors[i]);
121 log_config_push(&conf_out, log_name_by_id(i), val);
124 return !log_config_write(&conf_out, KMSG_CONFIG_PATH);
127 remove_kmsg_devs(kmsg_fd);