3 #include "log_storage.h"
14 #include <linux/limits.h>
15 #include <sys/epoll.h>
16 #include <sys/socket.h>
18 #include <sys/resource.h>
20 #include <sys/types.h>
24 #include <logconfig.h>
25 #include <ptrs_list.h>
26 #include <sd-daemon.h>
27 #include <backend_androidlogger.h>
28 #include <queued_entry_timestamp.h>
29 #include <dlogutil-internal.h>
31 #include "fd_entity.h"
32 #include "reader_common.h"
33 #include "reader_logger.h"
34 #include "reader_memory.h"
35 #include "reader_pipe.h"
37 #include "dlogutil_line.h"
38 #include "log_buffer.h"
45 /* supported main() return codes */
47 DLOG_EXIT_SUCCESS = 0,
52 /* Size in bytes of the pipe given to libdlog clients.
53 * The default size (1 page, usually 4 kiB) is fairly small
54 * which leads programs that log a lot to easily clog the
55 * pipe. On the other hand, the amount of memory allocated
56 * to pipes specifically can be limited and on most relevant
57 * targets seems to be set to 16ki pages (64 MiB raw memory)
58 * so pipes should not be too large so as not to waste it. */
59 #define PIPE_REQUESTED_SIZE (64 * 1024) // 16 pages at 4 kiB per page
61 /* Dumping readers should ideally get a larger pipe than clients
62 * so that it doesn't get easily clogged by the odd spammer.
63 * We can afford more generosity here because these are invoked
64 * quite rarely yet require handling priority, either because
65 * they are called by a developer manually or because a crashing
66 * program caused a system state dump. */
67 #define DUMPING_READER_PIPE_SIZE (4 * PIPE_REQUESTED_SIZE)
69 #define FILE_PATH_SIZE (256)
70 #define MAX_CONNECTION_Q 100
72 #define CONF_PREFIX "dlog_logger_conf_"
75 BUF_PARAM_TIME_MIN = 0,
76 BUF_PARAM_TIME_MAX = 3600,
77 BUF_PARAM_TIME_DEFAULT = 10,
81 BUF_PARAM_BYTES_MIN = 0,
82 BUF_PARAM_BYTES_MAX = 1 << 24,
83 BUF_PARAM_BYTES_DEFAULT = 1 << 20
87 LOGGER_DEVICE_THROTTLING_DEFAULT = 100,
90 extern struct backend_data {
91 bool use_logger_by_default;
92 char logger_devices[LOG_ID_MAX][MAX_CONF_VAL_LEN];
93 int logger_device_throttling[LOG_ID_MAX];
94 struct reader_logger *logger_readers[LOG_ID_MAX];
96 /* Lazy polling is Android Logger specific, since in that case
97 * the immediate storage is handled by the kernel and the logs
98 * aren't going anywhere, so the daemon can avoid doing anything
99 * at boot time (which would just waste system resources).
101 * For Pipe, the daemon needs to be actively gathering logs for
102 * reliability (not missing anything in case of, say, crash dump)
103 * and to unclog pipes. */
104 int lazy_polling_total;
105 int lazy_polling_sleep;
109 /* Writers and readers can be a bit confusing in what they refer to.
110 * Something that does write() calls is usually actually a reader,
111 * and something that read()s is typically a writer. This is because
112 * the terminology is from the point of view of the primary log storage
113 * and not the daemon itself. Additionally, under the Android Logger
114 * the situation is even more complex because there's a hybrid structure
115 * that sits at both edges of the daemon at the same time and read()s,
116 * then immediately write()s data. Nevertheless it is classified as a
117 * reader because of its relationship with the primary log storage.
119 * Here's a picture for Pipe backend:
121 * ╔══════════════════════ daemon ════════════════════╗
122 * ┌─────────┐ ║ ┌────────┐ ┌─────────────────┐ ┌────────┐ ║ ┌─────────────────┐
123 * │ libdlog ├─╫─>│ struct ├───>│ primary storage ├───>│ struct ├─╫─>│ /var/log/dlog/x │
124 * │ client │ ║ │ writer │ │ char buffer[N]; │ │ reader │ ║ │ or dlogutil │
125 * └─────────┘ ║ └────────┘ └─────────────────┘ └────────┘ ║ └─────────────────┘
126 * ╚══════════════════════════════════════════════════╝
128 * And here's one for the Android Logger and Zero Copy backends:
131 * ┌─────────┐ ┌─────────────────┐ ║ ┌────────┐ ║ ┌─────────────────┐
132 * │ libdlog ├─────────────────>│ primary storage ├─╫─>│ struct ├─╫─>│ /var/log/dlog/x │
133 * │ client │ │ /dev/log_x │ ║ │ reader │ ║ │ │
134 * └─────────┘ └─────────────────┘ ║ └────────┘ ║ └─────────────────┘
137 * Of course, the null backend has no relevant data structures. */
141 struct epoll_metadata epoll_common;
142 struct epoll_metadata epoll_socket;
145 list_head readers_logger;
146 struct log_buffer* buffers[LOG_ID_MAX];
147 struct buf_params buf_params;
149 struct qos_module qos;
150 list_head compressed_memories;
153 struct logger_config_data {
154 struct buf_params buf_params;
155 list_head logfile_configs;
157 int is_buffer_enabled[LOG_ID_MAX];
158 struct buffer_config_data buffers[LOG_ID_MAX];
159 char *dynamic_config_dir;
161 int qos_max_throughput;
162 int qos_limit_duration;
164 int qos_threshold_reapply;
165 qos_distribution_func distribution_func;
166 char *first_time_file_path;
167 log_print_format default_format;
170 void remove_reader_fd_entities(struct logger *server, struct reader_common *reader);
171 void check_if_fd_limit_reached(struct logger *server, int err);
172 int service_writer_kmsg(struct logger *server, struct writer *wr, struct epoll_event *event);
173 int service_writer_socket(struct logger *server, struct writer *wr, struct epoll_event *event);
174 int service_writer_syslog(struct logger *server, struct writer *wr, struct epoll_event *event);
175 void logger_add_writer(struct logger *l, struct writer *wr);
176 int create_fifo_fds(struct logger *server, int pipe_fd[2], int flags, bool dump);
177 int add_reader_pipe(struct logger *server, struct reader_pipe *reader);
178 int add_reader_memory(struct logger *server, struct reader_memory *reader);
179 void flush_logfile_timely(struct log_file *file, struct timespec ts, int flush_time);
180 int get_now(struct now_t *now);