| /** @file | |
| Crash logging helper support | |
| @par License | |
| Licensed to the Apache Software Foundation (ASF) under one | |
| or more contributor license agreements. See the NOTICE file | |
| distributed with this work for additional information | |
| regarding copyright ownership. The ASF licenses this file | |
| to you under the Apache License, Version 2.0 (the | |
| "License"); you may not use this file except in compliance | |
| with the License. You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software | |
| distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License. | |
| */ | |
| #include "Crash.h" | |
| #include "swoc/swoc_file.h" | |
| #include "records/RecCore.h" | |
| #include "tscore/Layout.h" | |
| #include "tscore/Version.h" | |
| #include "tscore/signals.h" | |
| #include <string> | |
| #include <unistd.h> | |
| #include <fcntl.h> | |
| #if defined(__linux__) | |
| #include <sys/prctl.h> | |
| #include <sys/syscall.h> | |
| #endif | |
| // ucontext.h is deprecated on Darwin, and we really only need it on Linux, so only | |
| // include it if we are planning to use it. | |
| #if defined(__linux__) | |
| #include <ucontext.h> | |
| #endif | |
| namespace | |
| { | |
| constexpr int NO_FD = swoc::file::NO_FD; | |
| pid_t crash_logger_pid = -1; | |
| int crash_logger_fd = NO_FD; | |
| DbgCtl dbg_ctl_server{"server"}; | |
| char * | |
| create_logger_path() | |
| { | |
| std::string bindir; | |
| ats_scoped_str fullpath; | |
| auto rec_str{RecGetRecordStringAlloc("proxy.config.crash_log_helper")}; | |
| if (!rec_str) { | |
| return nullptr; | |
| } | |
| RecString name{ats_stringdup(rec_str)}; | |
| // Take an absolute path as it is ... | |
| if (name == nullptr || *name == '/') { | |
| return name; | |
| } | |
| // Otherwise locate it relative to $BINDIR. | |
| bindir = RecConfigReadBinDir(); | |
| fullpath = Layout::relative_to(bindir, name); | |
| ats_free(name); | |
| return fullpath.release(); | |
| } | |
| bool | |
| check_logger_path(const char *path) | |
| { | |
| struct stat sbuf; | |
| if (stat(path, &sbuf) != 0) { | |
| Error("failed to access crash log helper '%s': %s", path, strerror(errno)); | |
| return false; | |
| } | |
| if (!S_ISREG(sbuf.st_mode) || access(path, X_OK) != 0) { | |
| Error("crash log helper '%s' is not executable", path); | |
| return false; | |
| } | |
| return true; | |
| } | |
| } // end anonymous namespace | |
| void | |
| crash_logger_init(const char *user) | |
| { | |
| ats_scoped_str logger(create_logger_path()); | |
| const char *basename; | |
| pid_t child; | |
| int status; | |
| int pipe[2]; | |
| // Do nothing the log helper was set to NULL, or we can't find it. | |
| if (!logger || !check_logger_path(logger)) { | |
| return; | |
| } | |
| // By this point, we have an absolute path, so we'd better be able to find the basename. | |
| basename = strrchr(logger, '/') + 1; | |
| socketpair(AF_UNIX, SOCK_STREAM, 0, pipe); | |
| child = fork(); | |
| switch (child) { | |
| case -1: | |
| Error("failed to fork crash log helper: %s", strerror(errno)); | |
| crash_logger_pid = -1; | |
| crash_logger_fd = NO_FD; | |
| return; | |
| case 0: | |
| // Dupe the child socket to stdin. | |
| dup2(pipe[1], STDIN_FILENO); | |
| close(pipe[0]); | |
| close(pipe[1]); | |
| // Starting after stderr, keep closing file descriptors until we run out. | |
| for (int fd = STDERR_FILENO + 1; fd; ++fd) { | |
| if (close(fd) == -1) { | |
| break; | |
| } | |
| } | |
| ink_release_assert(execl(logger, basename, "--syslog", "--wait", "--host", TS_BUILD_CANONICAL_HOST, "--user", user, NULL) != | |
| -1); | |
| return; // not reached. | |
| } | |
| close(pipe[1]); | |
| crash_logger_pid = child; | |
| crash_logger_fd = pipe[0]; | |
| // Don't leak this socket into later fork+exec children; the logger relies on seeing EOF | |
| // here when we (its parent) exit. | |
| fcntl(crash_logger_fd, F_SETFD, FD_CLOEXEC); | |
| #if defined(__linux__) | |
| // Allow the crash logger to ptrace us. Without this, Yama's ptrace_scope=1 | |
| // (the default on many distros) prevents a child process from tracing its parent. | |
| prctl(PR_SET_PTRACER, crash_logger_pid, 0, 0, 0); | |
| #endif | |
| // Wait for the helper to stop | |
| if (waitpid(crash_logger_pid, &status, WUNTRACED) > 0) { | |
| Dbg(dbg_ctl_server, "waited on PID %ld, %s", (long)crash_logger_pid, WIFSTOPPED(status) ? "STOPPED" : "???"); | |
| if (WIFEXITED(status)) { | |
| Warning("crash logger '%s' unexpectedly exited with status %d", (const char *)logger, WEXITSTATUS(status)); | |
| close(crash_logger_pid); | |
| crash_logger_pid = -1; | |
| crash_logger_fd = NO_FD; | |
| } | |
| } | |
| } | |
| void | |
| crash_logger_invoke(int signo, siginfo_t *info, void *ctx) | |
| { | |
| if (crash_logger_pid != -1) { | |
| int status; | |
| // Let the crash logger free ... | |
| kill(crash_logger_pid, SIGCONT); | |
| // Notify the logger that this is a real crash; a plain exit closes the socket (EOF) | |
| // instead. Written on every platform, ahead of the Linux-only thread payload below. | |
| ATS_UNUSED_RETURN(write(crash_logger_fd, &signo, sizeof(signo))); | |
| #if defined(__linux__) | |
| // Write the crashing thread information to the crash logger. While the siginfo_t is blesses by POSIX, the | |
| // ucontext_t can contain pointers, so it's highly platform dependent. On Linux with glibc, however, it is | |
| // a single memory block that we can just puke out. | |
| pid_t crashing_tid = static_cast<pid_t>(syscall(SYS_gettid)); | |
| ATS_UNUSED_RETURN(write(crash_logger_fd, &crashing_tid, sizeof(crashing_tid))); | |
| ATS_UNUSED_RETURN(write(crash_logger_fd, info, sizeof(siginfo_t))); | |
| ATS_UNUSED_RETURN(write(crash_logger_fd, static_cast<ucontext_t *>(ctx), sizeof(ucontext_t))); | |
| // Send zero-length backtrace. We cannot safely generate a backtrace here | |
| // because backtrace() can acquire locks (e.g., in the dynamic linker) that | |
| // the crashing thread might be holding, causing a deadlock. The crash | |
| // logger will get the backtrace via ptrace from a separate process instead. | |
| uint32_t bt_len = 0; | |
| ATS_UNUSED_RETURN(write(crash_logger_fd, &bt_len, sizeof(bt_len))); | |
| #endif | |
| close(crash_logger_fd); | |
| crash_logger_fd = NO_FD; | |
| // Wait for the logger to finish ... | |
| waitpid(crash_logger_pid, &status, 0); | |
| } | |
| // Log the signal, dump a stack trace and core. | |
| signal_format_siginfo(signo, info, AppVersionInfo::get_version().application()); // XXX Add timestamp ... | |
| signal_crash_handler(signo, info, ctx); | |
| } |