11bdd587ca3b25c6e73706c716cb7303c0163d98
[platform/core/system/dlog.git] / src / logger / logger_internal.h
1 #pragma once
2
3 #include "log_storage.h"
4
5 #include <syslog.h>
6
7 #include <ctype.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <pwd.h>
14 #include <grp.h>
15 #include <assert.h>
16 #include <linux/limits.h>
17 #include <sys/epoll.h>
18 #include <sys/socket.h>
19 #include <sys/wait.h>
20 #include <sys/resource.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <sys/un.h>
24 #include <logpipe.h>
25 #include <log_file.h>
26 #include <logconfig.h>
27 #include <ptrs_list.h>
28 #include <sd-daemon.h>
29 #include <backend_androidlogger.h>
30 #include <queued_entry_timestamp.h>
31 #include <dlogutil-internal.h>
32 #include "qos.h"
33 #include "fd_entity.h"
34 #include "reader_common.h"
35 #include "reader_logger.h"
36 #include "reader_pipe.h"
37 #include "socket.h"
38 #include "dlogutil_line.h"
39 #include "log_buffer.h"
40 #include "writer.h"
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 /* supported main() return codes */
47 enum {
48         DLOG_EXIT_SUCCESS = 0,
49         DLOG_EXIT_ERR_CONFIG,
50         DLOG_EXIT_ERR_RUNTIME
51 };
52
53 /* Size in bytes of the pipe given to libdlog clients.
54  * The default size (1 page, usually 4 kiB) is fairly small
55  * which leads programs that log a lot to easily clog the
56  * pipe. On the other hand, the amount of memory allocated
57  * to pipes specifically can be limited and on most relevant
58  * targets seems to be set to 16ki pages (64 MiB raw memory)
59  * so pipes should not be too large so as not to waste it. */
60 #define PIPE_REQUESTED_SIZE (64 * 1024) // 16 pages at 4 kiB per page
61
62 /* Dumping readers should ideally get a larger pipe than clients
63  * so that it doesn't get easily clogged by the odd spammer.
64  * We can afford more generosity here because these are invoked
65  * quite rarely yet require handling priority, either because
66  * they are called by a developer manually or because a crashing
67  * program caused a system state dump. */
68 #define DUMPING_READER_PIPE_SIZE (4 * PIPE_REQUESTED_SIZE)
69
70 #define FILE_PATH_SIZE (256)
71 #define MAX_CONNECTION_Q 100
72 #define DELIMITER " "
73 #define CONF_PREFIX "dlog_logger_conf_"
74
75 enum {
76         BUF_PARAM_TIME_MIN = 0,
77         BUF_PARAM_TIME_MAX = 3600,
78         BUF_PARAM_TIME_DEFAULT = 1,
79 };
80
81 enum {
82         BUF_PARAM_BYTES_MIN = 0,
83         BUF_PARAM_BYTES_MAX = 65536,
84         BUF_PARAM_BYTES_DEFAULT = 100
85 };
86
87 enum {
88         LOGGER_DEVICE_THROTTLING_DEFAULT = 100,
89 };
90
91 extern struct backend_data {
92         bool use_logger_by_default;
93         char logger_devices[LOG_ID_MAX][MAX_CONF_VAL_LEN];
94         int logger_device_throttling[LOG_ID_MAX];
95         struct reader_logger *logger_readers[LOG_ID_MAX];
96 } g_backend;
97
98
99 /* Writers and readers can be a bit confusing in what they refer to.
100  * Something that does write() calls is usually actually a reader,
101  * and something that read()s is typically a writer. This is because
102  * the terminology is from the point of view of the primary log storage
103  * and not the daemon itself. Additionally, under the Android Logger
104  * the situation is even more complex because there's a hybrid structure
105  * that sits at both edges of the daemon at the same time and read()s,
106  * then immediately write()s data. Nevertheless it is classified as a
107  * reader because of its relationship with the primary log storage.
108  *
109  * Here's a picture for Pipe backend:
110  *
111  *             ╔══════════════════════ daemon ════════════════════╗
112  * ┌─────────┐ ║  ┌────────┐    ┌─────────────────┐    ┌────────┐ ║  ┌─────────────────┐
113  * │ libdlog ├─╫─>│ struct ├───>│ primary storage ├───>│ struct ├─╫─>│ /var/log/dlog/x │
114  * │  client │ ║  │ writer │    │ char buffer[N]; │    │ reader │ ║  │   or  dlogutil  │
115  * └─────────┘ ║  └────────┘    └─────────────────┘    └────────┘ ║  └─────────────────┘
116  *             ╚══════════════════════════════════════════════════╝
117  *
118  * And here's one for the Android Logger backend:
119  *
120  *                                                  ╔═══ daemon ══╗
121  * ┌─────────┐                  ┌─────────────────┐ ║  ┌────────┐ ║  ┌─────────────────┐
122  * │ libdlog ├─────────────────>│ primary storage ├─╫─>│ struct ├─╫─>│ /var/log/dlog/x │
123  * │  client │                  │   /dev/log_x    │ ║  │ reader │ ║  │                 │
124  * └─────────┘                  └─────────────────┘ ║  └────────┘ ║  └─────────────────┘
125  *                                                  ╚═════════════╝
126  */
127
128
129 struct logger {
130         struct epoll_metadata epoll_common;
131         struct epoll_metadata epoll_socket;
132         int                   epolltime;
133         int                   lazy_polling_total;
134         int                   lazy_polling_sleep;
135         list_head             writers;
136         list_head             readers_logger;
137         struct log_buffer*    buffers[LOG_ID_MAX];
138         struct buf_params     buf_params;
139         int                   exiting;
140         struct now_t          time;
141         struct qos_module     qos;
142 };
143
144 struct logger_config_data {
145         struct buf_params buf_params;
146         list_head logfile_configs;
147         int lazy_polling_total;
148         int lazy_polling_sleep;
149         int epoll_time;
150         int is_buffer_enabled[LOG_ID_MAX];
151         struct buffer_config_data buffers[LOG_ID_MAX];
152         char *dynamic_config_dir;
153         char *qos_file_path;
154         int qos_max_throughput;
155         int qos_limit_duration;
156         int qos_threshold;
157         int qos_threshold_reapply;
158         char *first_time_file_path;
159 };
160
161 void remove_reader_fd_entities(struct logger *server, struct reader *reader);
162 void reader_deinit_common(struct reader *reader);
163 void check_if_fd_limit_reached(struct logger *server, int err);
164 int service_writer_kmsg(struct logger* server, struct writer* wr, struct epoll_event* event);
165 int service_writer_socket(struct logger* server, struct writer* wr, struct epoll_event* event);
166 int service_writer_syslog(struct logger* server, struct writer* wr, struct epoll_event* event);
167 void logger_add_writer(struct logger* l, struct writer* wr);
168 int create_fifo_fds(struct logger *server, int fifo_id, int *write_fd, int *read_fd, bool dump);
169 int add_reader_pipe(struct logger *server, struct reader_pipe *reader);
170
171 #ifdef __cplusplus
172 }
173 #endif