Release version 0.15.20
[platform/core/appfw/launchpad.git] / src / launchpad-process-pool / src / launchpad_logger.c
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define _GNU_SOURCE
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <grp.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/smack.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <time.h>
29 #include <unistd.h>
30
31 #include "launchpad_logger.h"
32 #include "log_private.h"
33
34 #define LAUNCHPAD_LOG_APPFW_PATH "/var/log/appfw"
35 #define LAUNCHPAD_LOG_PATH "/var/log/appfw/launchpad"
36 #define LAUNCHPAD_LOG_MAX_SIZE 2500
37 #define LAUNCHPAD_LOG_MAX_BUFFER_SIZE 256
38
39 struct logger_s {
40         int fd;
41         int index;
42 };
43
44 static int __set_smack_label(const char *path, const char *label)
45 {
46         int ret;
47
48         ret = smack_setlabel(path, label, SMACK_LABEL_ACCESS);
49         if (ret != 0) {
50                 ret = -errno;
51                 _E("smack_setlabel() is failed. path(%s), label(%s), errno(%d)",
52                                 path, label, errno);
53                 return ret;
54         }
55
56         return 0;
57 }
58
59 static int __create_directory(const char *path)
60 {
61         mode_t mode;
62         int ret;
63
64         ret = access(path, F_OK);
65         if (ret == 0)
66                 return 0;
67
68         mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
69         ret = mkdir(path, mode);
70         if (ret < 0) {
71                 ret = -errno;
72                 _E("Failed to create directory(%s). errno(%d)", path, errno);
73                 return ret;
74         }
75
76         return 0;
77 }
78
79 static int __create_launchpad_directories(void)
80 {
81         int ret;
82
83         ret = __create_directory(LAUNCHPAD_LOG_APPFW_PATH);
84         if (ret < 0)
85                 return ret;
86
87         ret = __set_smack_label(LAUNCHPAD_LOG_APPFW_PATH, "_");
88         if (ret < 0)
89                 return ret;
90
91         ret = __create_directory(LAUNCHPAD_LOG_PATH);
92         if (ret < 0)
93                 return ret;
94
95         ret = __set_smack_label(LAUNCHPAD_LOG_PATH, "User");
96         if (ret < 0)
97                 return ret;
98
99         return 0;
100 }
101
102 int _logger_create(const char *path, logger_h *handle)
103 {
104         struct logger_s *logger;
105         off_t offset;
106         int ret;
107
108         if (!path || !handle) {
109                 _E("Invalid parameter");
110                 return -EINVAL;
111         }
112
113         ret = __create_launchpad_directories();
114         if (ret < 0)
115                 return ret;
116
117         logger = calloc(1, sizeof(struct logger_s));
118         if (!logger) {
119                 _E("Out of memory");
120                 return -ENOMEM;
121         }
122
123         logger->fd = open(path, O_CREAT | O_WRONLY, 0640);
124         if (logger->fd < 0) {
125                 ret = -errno;
126                 _E("Failed to open path(%s), errno(%d)", path, errno);
127                 _logger_destroy(logger);
128                 return ret;
129         }
130
131         ret = __set_smack_label(path, "User");
132         if (ret < 0) {
133                 _logger_destroy(logger);
134                 return ret;
135         }
136
137         offset = lseek(logger->fd, 0, SEEK_END);
138         if (offset != 0) {
139                 logger->index = (int)(offset / LAUNCHPAD_LOG_MAX_STRING_SIZE);
140                 if (logger->index >= LAUNCHPAD_LOG_MAX_SIZE) {
141                         logger->index = 0;
142                         lseek(logger->fd, 0, SEEK_SET);
143                 }
144         }
145
146         *handle = logger;
147
148         return 0;
149 }
150
151 int _logger_destroy(logger_h handle)
152 {
153         if (!handle) {
154                 _E("Invalid parameter");
155                 return -EINVAL;
156         }
157
158         if (handle->fd > 0)
159                 close(handle->fd);
160
161         free(handle);
162
163         return 0;
164 }
165
166 int _logger_print(logger_h handle, const char *tag, const char *format, ...)
167 {
168         char buf[LAUNCHPAD_LOG_MAX_BUFFER_SIZE];
169         char format_buf[128];
170         struct tm tm = { 0, };
171         time_t t;
172         ssize_t ret;
173         va_list ap;
174         off_t offset;
175
176         if (!handle || !tag || !format) {
177                 _E("Invalid parameter");
178                 return -EINVAL;
179         }
180
181         t = time(NULL);
182         localtime_r(&t, &tm);
183
184         if (handle->index != 0)
185                 offset = lseek(handle->fd, 0, SEEK_CUR);
186         else
187                 offset = lseek(handle->fd, 0, SEEK_SET);
188
189         if (offset == -1)
190                 _E("lseek() is failed. errno(%d)", errno);
191
192         va_start(ap, format);
193         vsnprintf(format_buf, sizeof(format_buf), format, ap);
194         va_end(ap);
195
196         snprintf(buf, sizeof(buf),
197                         "[%6d] %04d-%02d-%02d %02d:%02d:%02d %-16s %-100s\n",
198                         handle->index,
199                         tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
200                         tm.tm_hour, tm.tm_min, tm.tm_sec,
201                         tag, format_buf);
202         ret = write(handle->fd, buf, strlen(buf));
203         if (ret < 0) {
204                 ret = -errno;
205                 _E("Failed to write log message. errno(%d)", errno);
206                 return ret;
207         }
208
209         if (++handle->index >= LAUNCHPAD_LOG_MAX_SIZE)
210                 handle->index = 0;
211
212         return ret;
213 }
214
215 int _logger_get_fd(logger_h handle, int *fd)
216 {
217         if (!handle || !fd) {
218                 _E("Invalid parameter");
219                 return -EINVAL;
220         }
221
222         *fd = handle->fd;
223
224         return 0;
225 }