3 * Copyright (c) 2012-2020 Samsung Electronics Co., Ltd
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is furnished
10 * to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35 #include "deduplicate.h"
36 #include <dynamic_config.h>
38 #include <logcommon.h>
39 #include "logconfig.h"
40 #include "loglimiter.h"
42 #define DEFAULT_CONFIG_LIMITER false
43 #define DEFAULT_CONFIG_PLOG true
44 #define DEFAULT_CONFIG_DEBUGMODE 0
45 #define DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS 0
48 * @brief Points to a function which writes a log message
49 * @details The function pointed to depends on the backend used
50 * @param[in] log_id ID of the buffer to log to. Belongs to (LOG_ID_INVALID, LOG_ID_MAX) non-inclusive
51 * @param[in] prio Priority of the message.
52 * @param[in] tag The message tag, identifies the sender.
53 * @param[in] msg The contents of the message.
54 * @return Returns the number of bytes written on success and a negative error value on error.
55 * @see __dlog_init_backend
57 int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg, struct timespec *tp_mono) = NULL;
58 void (*destroy_backend)();
60 int (*stash_failed_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg) = NULL;
62 static int stash_critical(log_id_t log_id, log_priority prio, const char *tag, const char *msg);
65 pthread_rwlock_t log_limiter_lock = PTHREAD_RWLOCK_INITIALIZER;
66 static pthread_mutex_t log_construction_lock = PTHREAD_MUTEX_INITIALIZER;
67 static bool is_initialized = false;
69 extern void __dlog_init_pipe(const struct log_config *conf);
70 extern void __dlog_init_android(const struct log_config *conf);
73 static bool dynamic_config;
74 static bool plog[LOG_ID_MAX];
75 static bool plog_default_values[LOG_ID_MAX];
76 static bool enable_secure_logs = true;
77 static bool enable_critical = true;
80 static int fatal_assert;
81 static int limiter_apply_to_all_buffers;
82 static _Atomic log_priority priority_filter_level = DLOG_VERBOSE;
84 static void __configure_limiter(struct log_config *config)
91 limiter = __log_limiter_create(config);
94 static int __configure_backend(struct log_config *config)
98 const char *const backend = log_config_get(config, "backend");
102 if (!strcmp(backend, "pipe"))
103 __dlog_init_pipe(config);
104 else if (!strcmp(backend, "logger"))
105 __dlog_init_android(config);
112 static void __set_plog_default_values()
114 for (size_t i = 0; i < NELEMS(plog); ++i)
115 plog_default_values[i] = plog[i];
118 static void __initialize_plog(const struct log_config *config)
122 const bool plog_default = log_config_get_boolean(config, "plog", DEFAULT_CONFIG_PLOG);
123 for (size_t i = 0; i < NELEMS(plog); ++i)
124 plog[i] = plog_default;
125 plog[LOG_ID_APPS] = true; // the default does not apply here for backward compatibility reasons.
126 __set_plog_default_values();
129 static void __configure_parameters(struct log_config *config)
133 __initialize_plog(config);
134 __update_plog(config);
135 __set_plog_default_values();
137 enable_secure_logs = log_config_get_boolean(config, "enable_secure_logs", enable_secure_logs);
138 enable_critical = log_config_get_boolean(config, "enable_critical", enable_critical);
139 debugmode = log_config_get_int(config, "debugmode", DEFAULT_CONFIG_DEBUGMODE);
140 fatal_assert = access(DEBUGMODE_FILE, F_OK) != -1;
141 limiter = log_config_get_boolean(config, "limiter", DEFAULT_CONFIG_LIMITER);
142 limiter_apply_to_all_buffers = log_config_get_int(config,
143 "limiter_apply_to_all_buffers",
144 DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS);
146 const char *stash_failed_log_method = log_config_get(config, "stash_failed_log_method");
147 if (stash_failed_log_method) {
149 if (strcmp(stash_failed_log_method, "critical") == 0)
150 stash_failed_log = stash_critical;
155 void __update_plog(const struct log_config *conf)
159 for (size_t i = 0; i < NELEMS(plog); ++i) {
160 char key[MAX_CONF_KEY_LEN];
161 const int r = snprintf(key, sizeof key, "enable_%s", log_name_by_id((log_id_t)i));
164 plog[i] = log_config_get_boolean(conf, key, plog_default_values[i]);
169 * @brief Configure the library
170 * @details Reads relevant config values
171 * @remarks This is more or less a constructor, but there are some obstacles
172 * to using it as such (i.e. with attribute constructor):
174 * - some important pieces of the system link to dlog, they start very early
175 * such that dlog can't properly initialize (which lasts for program lifetime)
176 * but don't actually log anything until later on and would be fine under lazy
177 * initialisation. The way to do it "properly" would be to expose this function
178 * into the API so that people can manually call it when they're ready, but
179 * one of the design goals of the current API is that it requires absolutely no
180 * other calls than `dlog_print`. Changing it would require somebody with a
181 * bird's eye view of the system to produce a design so I wouldn't count on it.
183 * - the constructor would need to have as high of a priority as possible (so as
184 * to minimize the risk of another library's constructor using uninitialized data)
185 * but at the same time others might want some room to wrap functions before
186 * dlog uses them (think mprobe/mcheck). This would also require a design pass.
191 bool __configure(void)
193 __attribute__((cleanup(log_config_free))) struct log_config config;
195 if (log_config_read(&config) < 0)
198 dynamic_config = __dynamic_config_create(&config);
200 __configure_parameters(&config);
202 if (!__configure_backend(&config)) {
203 __dynamic_config_destroy();
204 dynamic_config = false;
208 __configure_deduplicate(&config);
209 __configure_limiter(&config);
213 static void __attribute__((constructor(101))) __install_pipe_handler(void)
215 /* We mask SIGPIPE signal because most applications do not install their
216 * own SIGPIPE handler. Default behaviour in SIGPIPE case is to abort the
217 * process. SIGPIPE occurs when e.g. dlog daemon closes read pipe endpoint.
219 * We do this in the library constructor (at maximum priority) and not
220 * during regular (lazy) initialisation so as to prevent overwriting the
221 * program's actual signal handler, if it has one.
223 * In theory this is not required for the Android logger backend; however,
224 * this early we don't yet know the backend and also it is good to behave
225 * consistently in this regard anyway.
227 * We don't revert this in a destructor because Unix signals are bonkers
228 * and we have no way to do this cleanly. Most libdlog users don't use
229 * runtime linking so this would mostly done at program exit either way. */
230 signal(SIGPIPE, SIG_IGN);
233 static void __attribute__((constructor(102))) __set_output_buffering(void)
235 /* If stdout and/or stderr is redirected to dlog (service, driver)
236 * it is best if we buffer lines, otherwise the following can happen:
237 * - no buffering: service/driver recives every single byte (precisely
238 * every single write*(2) call), causing it to either buffer the data
239 * anyway (service), or print logs at random places (driver)
240 * - full buffering: service/driver receives several hundred lines,
241 * which results in one giant entry being added (driver) or long delays
242 * before logs appear (service) */
243 if (getenv(DLOG_ENV_STDOUT_LINE_BUFFERED))
246 if (getenv(DLOG_ENV_STDERR_LINE_BUFFERED))
250 static bool first = true;
251 static bool initialize()
256 /* The mutex acts as a barrier, but otherwise the C language's
257 * machine abstraction is single-threaded. This means that the
258 * compiler is free to rearrange calls inside the mutex according
259 * to the as-if rule because it doesn't care if another thread can
260 * access it in parallel. In particular, `is_initialized = true`
261 * directly after `__configure()` could be rearranged to go in
262 * front of it because it is not touched inside that function
263 * if the compiler thinks it helps somehow (not unlikely: since
264 * it is checked before the mutex, it is very probable for it to
265 * still be in the CPU register or something like that). On top
266 * of that, some architectures (in particular, armv7l) don't have
267 * strict memory guarantees and can reorder actual memory stores
268 * on their own, even if the compiler didn't do anything fancy
269 * when creating machine code. For more info about the issue,
270 * see https://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf
272 * Ultimately this means that there needs to be some sort of
273 * barrier between `__configure` and `is_initialized = true`,
274 * and the simplest way to achieve that is to just wait until
275 * the second entry into the mutex. */
278 pthread_mutex_lock(&log_construction_lock);
280 first = !__configure();
282 is_initialized = true;
284 pthread_mutex_unlock(&log_construction_lock);
289 * @brief Fatal assertion
290 * @details Conditionally crash the sucka who sent the log
291 * @param[in] prio Priority of the log
293 static void __dlog_fatal_assert(int prio)
295 assert(!fatal_assert || (prio != DLOG_FATAL));
299 * @brief Check log validity
300 * @details Checks whether the log is valid and eligible for printing
301 * @param[in] log_id The target buffer ID
302 * @param[in] prio The log's priority
303 * @param[in] tag The log's tag
304 * @return DLOG_ERROR_NONE on success, else an error code.
305 * @retval DLOG_ERROR_INVALID_PARAMETER Invalid parameter
307 static int dlog_check_validity(log_id_t log_id, int prio, const char *tag)
311 return DLOG_ERROR_INVALID_PARAMETER;
313 if (log_id <= LOG_ID_INVALID || LOG_ID_MAX <= log_id)
314 return DLOG_ERROR_INVALID_PARAMETER;
316 return DLOG_ERROR_NONE;
320 * @brief Check log against limiter rules
321 * @details Checks whether the log passes current limiter rules
322 * @param[in] log_id The target buffer ID
323 * @param[in] prio The log's priority
324 * @param[in] tag The log's tag
325 * @return DLOG_ERROR_NONE on success, else an error code.
326 * @retval DLOG_ERROR_NOT_PERMITTED Not permitted
328 static int dlog_check_limiter(log_id_t log_id, int prio, const char *tag)
330 if (!debugmode && prio <= DLOG_DEBUG)
331 return DLOG_ERROR_NOT_PERMITTED;
334 __dynamic_config_update();
337 struct pass_log_result should_log = { .decision = DECISION_DENIED };
338 if (!pthread_rwlock_rdlock(&log_limiter_lock)) {
339 should_log = __log_limiter_pass_log(tag, prio);
340 pthread_rwlock_unlock(&log_limiter_lock);
343 switch (should_log.decision) {
344 case DECISION_DENIED:
345 return DLOG_ERROR_NOT_PERMITTED;
347 case DECISION_TAG_LIMIT_EXCEEDED_MESSAGE:
348 case DECISION_PID_LIMIT_EXCEEDED_MESSAGE: {
350 int result = clock_gettime(CLOCK_MONOTONIC, &tp);
352 return DLOG_ERROR_NOT_PERMITTED;
354 snprintf(buf, sizeof(buf),
355 "Your log has been blocked due to per-%s limit of %d logs per %d seconds.",
356 should_log.decision == DECISION_TAG_LIMIT_EXCEEDED_MESSAGE ? "tag" : "PID",
357 should_log.logs_per_period, should_log.period_s);
358 write_to_log(log_id, prio, tag, buf, &tp);
359 return DLOG_ERROR_NOT_PERMITTED;
362 case DECISION_ALLOWED:
367 /* This can change due to __dynamic_config_update(), but is atomic and its
368 * value implies nothing else so does not need to be under a lock. */
370 return DLOG_ERROR_NOT_PERMITTED;
372 return DLOG_ERROR_NONE;
375 static int __write_to_log_critical_section(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap, bool check_should_log)
377 if (check_should_log && prio < priority_filter_level)
378 return DLOG_ERROR_NONE;
380 if ((check_should_log || limiter_apply_to_all_buffers) && (dlog_check_limiter(log_id, prio, tag) < 0))
381 return DLOG_ERROR_NONE;
383 char buf[LOG_MAX_PAYLOAD_SIZE];
384 int len = vsnprintf(buf, sizeof buf, fmt, ap);
386 return DLOG_ERROR_NONE;
387 else if (len >= sizeof buf)
388 len = sizeof buf - 1;
392 if (deduplicate_func && !clock_gettime(CLOCK_MONOTONIC, &tp)) {
393 dlog_deduplicate_e ret = deduplicate_func(buf, len, &tp);
394 if (ret == DLOG_DEDUPLICATE)
395 return DLOG_ERROR_NONE;
396 else if (ret == DLOG_DO_NOT_DEDUPLICATE_BUT_WARN)
397 deduplicate_warn(buf, sizeof buf, len);
398 r = write_to_log(log_id, prio, tag, buf, &tp);
400 r = write_to_log(log_id, prio, tag, buf, NULL);
402 if (r < 0 && stash_failed_log)
403 r = stash_failed_log(log_id, prio, tag, buf);
408 static int __write_to_log(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap, bool check_should_log, bool secure_log)
410 int ret = dlog_check_validity(log_id, prio, tag);
414 /* Threads can be cancelled before they give up a lock.
415 * Therefore cancellation is temporarily disabled.
416 * This solution is comparatively simple and cheap.
417 * The other solutions (cleanup handlers, robust mutexes)
418 * would be much more complicated and also inflict larger
419 * runtime costs. The downside of disabling cancellation
420 * is not a problem in our case because it is temporary
421 * and very brief so we don't keep an obsolete thread
422 * for much longer than we otherwise would. */
423 int old_cancel_state;
424 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancel_state);
426 /* The only thing that needs to be protected here is `write_to_log` since
427 * all other resources already have their own specific locks (and even the
428 * pointer could be made to point at a null handler instead of a true NULL)
429 * but giving this guarantee makes everything a lot simpler as it removes
430 * the risk of something suddenly becoming NULL during processing. */
431 if (!initialize() || !write_to_log)
432 // TODO: We could consider stashing the failed log here
433 ret = DLOG_ERROR_NOT_PERMITTED;
434 else if (secure_log && !enable_secure_logs)
437 ret = __write_to_log_critical_section(log_id, prio, tag, fmt, ap, check_should_log);
439 pthread_setcancelstate(old_cancel_state, NULL);
444 int __critical_log_append_timestamp(char *buf, size_t buflen)
446 /* NB: the timestamp may slightly differ from the one that gets
447 * added onto the copy that goes into the regular buffer, and
448 * timestamp acquisition is duplicated. This would ideally be
449 * solved, but timestamps are currently added fairly deep in
450 * backend-specific functions so for now this will have to do.
451 * Also, since we're the sender, there is just this one set of
452 * timestamps, i.e. the send timestamp! The usual alternative
453 * set of receive timestamps will never have the opportunity
454 * to get added to the entry since this log is supposed to end
455 * up straight in the file (there's potentially the trusted
456 * writer binary but we're trying to keep the set of actions
457 * it needs to do to the minimum and those timestamps would
458 * in practice be the same anyway). */
461 clock_gettime(CLOCK_REALTIME, &ts);
462 const time_t tt = ts.tv_sec;
463 const long int real_millisec = ts.tv_nsec / 1000000;
464 clock_gettime(CLOCK_MONOTONIC, &ts);
466 struct tm *const ptm = localtime_r(&tt, &tmBuf);
467 assert(ptm); // we're in a short lived fork so asserts are fine and make things simple
469 int len = strftime(buf, buflen, "%m-%d %H:%M:%S", ptm);
472 int tmp_len = snprintf(buf + len, buflen - len, ".%03ld", real_millisec);
474 assert(tmp_len < buflen - len);
477 tmp_len = strftime(buf + len, buflen - len, "%z ", ptm);
478 assert(tmp_len != 0);
481 tmp_len = snprintf(buf + len, buflen - len, "%5lu.%03ld", ts.tv_sec, ts.tv_nsec / 1000000);
483 assert(tmp_len < buflen - len);
489 int __critical_log_build_msg(char *buf, size_t buflen, pid_t main_pid, pid_t main_tid, log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
491 int len = __critical_log_append_timestamp(buf, buflen);
492 const int metadata_len = snprintf(buf + len, buflen - len, " P%5d T%5d B%-6s %c/%-8s: ",
495 log_name_by_id(log_id),
496 filter_pri_to_char(prio),
497 tag ?: "CRITICAL_NO_TAG");
498 assert(metadata_len > 0);
499 if (metadata_len >= buflen - len)
500 return buflen - 1; // can genuinely happen with an exceedingly large tag
503 const int content_len = vsnprintf(buf + len, buflen - len, fmt, ap);
504 assert(content_len >= 0); // 0 is legit with format == ""
505 if (content_len >= buflen - len)
513 __attribute__ ((noreturn))
515 void __critical_log_child(pid_t main_pid, pid_t main_tid, log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
517 char buf[LOG_MAX_PAYLOAD_SIZE + 128]; // extra space for some metadata
518 const int len = __critical_log_build_msg(buf, sizeof buf - 1, main_pid, main_tid, log_id, prio, tag, fmt, ap);
522 static const char *const path = "/usr/libexec/dlog-log-critical";
523 execl(path, path /* argv[0] convention */, buf, (char *) NULL);
526 /* Compilers are sometimes smart enough to recognize _exit's
527 * noreturn attribute, even if we wrap it with something that
528 * returns. This causes it to behave in unexpected ways, for
529 * example it can blow up the program regardless or it can
530 * optimize some conditionals out (and incorrectly enter them
531 * after the exit call fails to actually exit). This makes it
532 * unsuitable for tests. */
534 _exit(1); // not the regular `exit` so as not to trigger any `atexit` handlers prematurely
538 #ifndef UNIT_TEST // contains forks and exits, these don't work well with wrapping (see above)
539 void __critical_log(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
541 /* Critical log functionality is mostly done in a separate binary
542 * to handle security correctly (else every process would have to
543 * possess the necessary privilege to write onto that file, which
544 * would be opening a fairly nasty can of worms from the security
545 * point of view). Our use of exec() is why a simple thread would
546 * not suffice and we're resorting to a fork.
548 * The double fork, much like a double barreled 12 gauge shotgun,
549 * is an elegant solution designed to stop a zombie army. We'd be
550 * creating zombie processes if we didn't wait() for the children
551 * we spawn, but we don't really want to do that since it results
552 * in a needless delay. Instead, the writer process is actually a
553 * grandchild, with our direct child exiting immediately just for
554 * us to have something to wait on that is guaranteed not to take
555 * too long. The orphaned grandchild is adopted by init, who will
556 * take care to reap it when it dies. In addition to avoiding the
557 * delay, the client will not have any unexpected children (which
558 * could ruin logic in its own waits).
561 * ┌───────┐ ┌─────────┐ ┌─────────────┐ ┌────────┐
562 * │ pid 1 ├──>│ libdlog ├──>│ immediately ├──>│ execs │
563 * │ init │ │ client │ │ exits │ │ writer │
564 * └───────┘ └─────────┘ └─────────────┘ └────────┘
566 * Afterwards, libdlog has no children:
567 * ┌───────┐ ┌─────────┐ ┌────────┐
568 * │ pid 1 ├──>│ libdlog │ ┌─────────>│ writer │
569 * │ init ├─┐ │ client │ │ │ binary │
570 * └───────┘ │ └─────────┘ │ └────────┘
571 * └──────────────────────┘
576 if (!enable_critical)
579 const pid_t main_pid = getpid();
580 const pid_t main_tid = gettid();
582 const int temporary_exiter_pid = fork();
583 if (temporary_exiter_pid < 0)
585 if (temporary_exiter_pid != 0) {
586 waitpid(temporary_exiter_pid, NULL, 0);
590 const int child_pid = fork();
596 __critical_log_child(main_pid, main_tid, log_id, prio, tag, fmt, ap);
599 static void stash_critical_inner(log_id_t log_id, log_priority prio, const char *tag, const char *fmt, ...)
604 __critical_log(log_id, prio, tag, fmt, ap);
608 static int stash_critical(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
610 stash_critical_inner(log_id, prio, tag, "FAILED TO LOG: %s", msg);
614 int __dlog_critical_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
619 __critical_log(log_id, prio, tag, fmt, ap);
623 int ret = __dlog_vprint(log_id, prio, tag, fmt, ap);
630 int dlog_set_minimum_priority(int priority)
632 if (priority < DLOG_DEFAULT || priority > DLOG_PRIO_MAX)
633 return DLOG_ERROR_INVALID_PARAMETER;
635 priority_filter_level = priority;
636 return DLOG_ERROR_NONE;
641 * @details Print a log line
642 * @param[in] log_id The target buffer ID
643 * @param[in] prio Priority
645 * @param[in] fmt Format (same as printf)
646 * @param[in] ap Argument list
647 * @return Bytes written, or negative error
649 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
651 int ret = __write_to_log(log_id, prio, tag, fmt, ap, true, false);
652 __dlog_fatal_assert(prio);
659 * @details Print a log line
660 * @param[in] log_id The target buffer ID
661 * @param[in] prio Priority
663 * @param[in] fmt Format (same as printf)
664 * @return Bytes written, or negative error
666 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
671 int ret = __dlog_vprint(log_id, prio, tag, fmt, ap);
679 * @details Print a log line
680 * @param[in] log_id The target buffer ID
681 * @param[in] prio Priority
683 * @param[in] fmt Format (same as printf)
684 * @return Bytes written, or negative error
686 int __dlog_sec_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
688 if (!enable_secure_logs)
694 int ret = __write_to_log(log_id, prio, tag, fmt, ap, true, true);
695 __dlog_fatal_assert(prio);
701 int dlog_vprint(log_priority prio, const char *tag, const char *fmt, va_list ap)
703 return __write_to_log(LOG_ID_APPS, prio, tag, fmt, ap, false, false);
706 int dlog_print(log_priority prio, const char *tag, const char *fmt, ...)
711 int ret = dlog_vprint(prio, tag, fmt, ap);
718 * @brief Finalize DLog
719 * @details Finalizes and deallocates the library
720 * @notes Used directly in tests; brings back the pre-init state
722 void __dlog_fini(void)
724 if (destroy_backend) {
726 destroy_backend = NULL;
729 is_initialized = false;
732 enable_secure_logs = true;
733 enable_critical = false;
734 __deduplicate_destroy();
735 __log_limiter_destroy();
736 __dynamic_config_destroy();