Set proper executable bits
[platform/core/system/dlog.git] / src / loginit / loginit.c
1 /*
2  * Copyright (c) 2005-2008, The Android Open Source Project
3  * Copyright (c) 2009-2015, Samsung Electronics Co., Ltd. All rights reserved.
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <stdlib.h>
19 #include <dlog_ioctl.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22
23 #include <logcommon.h>
24 #include <logconfig.h>
25
26 #define DEV_KMSG    "/dev/kmsg"
27 #define BACKUP_SIZE 1048576
28
29 int g_minors[LOG_ID_MAX] = {-1, -1, -1, -1, -1};
30 int g_sizes[LOG_ID_MAX] = {-1, -1, -1, -1, -1};
31
32 /**
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
38  */
39 static int create_kmsg_devs(int fd, struct log_config *conf)
40 {
41         int i;
42         struct kmsg_cmd_buffer_add cmd = {
43                 .mode = 0662,
44         };
45
46         for (i = 0; i < LOG_ID_MAX; i++) {
47                 char key[MAX_CONF_KEY_LEN];
48                 const char *size_str;
49                 char *size_str_end;
50
51                 snprintf(key, MAX_CONF_KEY_LEN, "%s_size", log_name_by_id(i));
52                 size_str = log_config_get(conf, key);
53                 errno = 0;
54
55                 if (!size_str) {
56                         cmd.size = BACKUP_SIZE;
57                 } else {
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;
61                 }
62
63                 g_sizes[i] = cmd.size;
64
65                 if (0 > ioctl(fd, KMSG_CMD_BUFFER_ADD, &cmd)) {
66                         _E("ioctl KMSG_CMD_BUFFER_ADD failed. (%d)\n", errno);
67                         return -1;
68                 }
69                 g_minors[i] = cmd.minor;
70         }
71         return 0;
72 }
73
74 /**
75  * @brief Remove KMSG devices
76  * @details Removes KMSG devices
77  * @param[in] fd The /dev/kmsg file descriptor
78  */
79 static void remove_kmsg_devs(int fd)
80 {
81         int i;
82
83         for (i = 0; i < LOG_ID_MAX; i++) {
84                 if (g_minors[i] < 0)
85                         continue;
86                 if (0 > ioctl(fd, KMSG_CMD_BUFFER_DEL, &g_minors[i]))
87                         _E("ioctl KMSG_CMD_BUFFER_DEL failed. (%d)\n", errno);
88         }
89 }
90
91 int main()
92 {
93         int kmsg_fd, i;
94         struct log_config conf_in;
95         struct log_config conf_out;
96
97         log_config_read(&conf_in);
98
99         memset(&conf_out, 0, sizeof(struct log_config));
100
101         kmsg_fd = open(DEV_KMSG, O_RDWR);
102         if (kmsg_fd < 0) {
103                 _E("Unable to open kmsg device. %d\n", errno);
104                 exit(EXIT_FAILURE);
105         }
106
107         if (0 > create_kmsg_devs(kmsg_fd, &conf_in))
108                 goto error;
109
110         log_config_free(&conf_in);
111
112         for (i = 0; i < LOG_ID_MAX; ++i) {
113                 char key[MAX_CONF_KEY_LEN];
114                 char val[MAX_CONF_VAL_LEN];
115
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);
119
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);
122         }
123
124         return !log_config_write(&conf_out, KMSG_CONFIG_PATH);
125
126 error:
127         remove_kmsg_devs(kmsg_fd);
128         exit(EXIT_FAILURE);
129 }