From patchwork Mon Mar 23 15:48:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Tu X-Patchwork-Id: 1260116 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.133; helo=hemlock.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=WwwPU3b+; dkim-atps=neutral Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48mJhs2Rd8z9sRN for ; Tue, 24 Mar 2020 02:49:17 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id C167588184; Mon, 23 Mar 2020 15:49:14 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id iMQnePgNC6wy; Mon, 23 Mar 2020 15:49:11 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by hemlock.osuosl.org (Postfix) with ESMTP id AE58B87F9C; Mon, 23 Mar 2020 15:49:11 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 8C25CC1AE2; Mon, 23 Mar 2020 15:49:11 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from fraxinus.osuosl.org (smtp4.osuosl.org [140.211.166.137]) by lists.linuxfoundation.org (Postfix) with ESMTP id 2722EC0177 for ; Mon, 23 Mar 2020 15:49:10 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by fraxinus.osuosl.org (Postfix) with ESMTP id 15A4285886 for ; Mon, 23 Mar 2020 15:49:10 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from fraxinus.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZfS9Ry06gpME for ; Mon, 23 Mar 2020 15:49:09 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-qt1-f195.google.com (mail-qt1-f195.google.com [209.85.160.195]) by fraxinus.osuosl.org (Postfix) with ESMTPS id 464C385691 for ; Mon, 23 Mar 2020 15:49:09 +0000 (UTC) Received: by mail-qt1-f195.google.com with SMTP id 10so12058994qtp.1 for ; Mon, 23 Mar 2020 08:49:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=+jBEJFkdhYKAxeCPf5il71yvfmxTrlO3jjGsubujLQE=; b=WwwPU3b+Uqsb0C063XaNdLLXVCM1Oe01das37hWtWOD8GUQy1yOnW07z3hvQKN4xzF n83rOBNha7XDzgUDTWhen+GMJORq3He/g4ke+ZsjXEP7+c3PCmzMBFf4i5Qf5IN7i8VB BRA0RVSFUhYbZXbogJc6r5YcAJreS+i5iEvtvsxjG43/hk/LZ6YmqScmj1c4H+gFG6nT m55z5G9/lrVbWGFMlG1XcsZ5TXeBuEf6iSWBcLP140KAf0QuQbk2vyQqkdcEQRw8jjpO dfSngG36tJ+fa4eyBJK1shQi9aeL2YZgATQbVHNjvcWZFA8oynFsgMs0VUYVobcogke8 M/+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=+jBEJFkdhYKAxeCPf5il71yvfmxTrlO3jjGsubujLQE=; b=ukSm3LT6uBH4GOl/znJPpt2Yz572VHi3M8B3/oEV7Vf2qpgISdRjbyJvxww0oOa0x8 KWY5zDkJ++mxxuBfavokcE9zUNoQIlZamdYI+QFPtBqq8CEOdQf6meM75iQpuvmXVvHG xgpeoS0zYO/BhGquOrys7KWJO5ja5qT+zn1enztCHERSIP54Zpglk0KDw4pviP8WBt3X TVl1DaVoL4BtyeS93b37DLWq+BRKwEycAPxoPp5uRpAHQuKYyDmKuP3aox0vvMT8ZF0r bskZZMMoCneXhZgeS6Bex7Kytnjt18cdSCnus1H65H9Ftgl6fyAhwBEbeFwjOlorwchK umrg== X-Gm-Message-State: ANhLgQ2DHdNLBwWGpQoaUynGtwGnSg8efgWqy+wfEDYjr5LzA+SCxIHj nJTobVsfWZt3SprxUn4vlVACfU4e X-Google-Smtp-Source: ADFU+vtjIN0PpVte5JIc6jwTh+RIt3zjoxbkJbqWc5P0wP2Rl3yjZwXF7sduC1AIF/3JBcywUP2PLg== X-Received: by 2002:aed:2be5:: with SMTP id e92mr22540839qtd.374.1584978547927; Mon, 23 Mar 2020 08:49:07 -0700 (PDT) Received: from sc9-mailhost3.vmware.com (c-76-21-95-192.hsd1.ca.comcast.net. [76.21.95.192]) by smtp.gmail.com with ESMTPSA id x74sm10596256qkb.40.2020.03.23.08.49.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 23 Mar 2020 08:49:07 -0700 (PDT) From: William Tu To: dev@openvswitch.org Date: Mon, 23 Mar 2020 08:48:32 -0700 Message-Id: <1584978512-29875-1-git-send-email-u9012063@gmail.com> X-Mailer: git-send-email 2.7.4 Subject: [ovs-dev] [PATCHv2] fatal-signal: Log backtrace when no monitor daemon. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Currently the backtrace logging is only available when monitor daemon is running. This patch enables backtrace logging when no monitor daemon exists. At signal handling context, it detects whether monitor daemon exists. If not, write directly the backtrace to the vlog fd. Note that using VLOG_* macro doesn't work due to it's buffer I/O, so this patch directly issue write() syscall to the file descriptor. For some system we stop using monitor daemon and use systemd to monitor ovs-vswitchd, thus need this patch. Example of ovs-vswitchd.log (note that there is no timestamp printed): 2020-03-23T14:42:12.949Z|00049|memory|INFO|175332 kB peak resident 2020-03-23T14:42:12.949Z|00050|memory|INFO|handlers:2 ports:3 reva SIGSEGV detected, backtrace: 0x0000000000486969 0x00007f7f5e57f4b0 0x000000000047daa8 0x0000000000504edd 0x00007f7f5f0476ba 0x00007f7f5e65141d 0x0000000000000000 <+0x0> Signed-off-by: William Tu Acked-by: Ben Pfaff --- v2: check log_fd < 0 and return add example to the commit message --- include/openvswitch/vlog.h | 3 +++ lib/daemon-private.h | 1 + lib/daemon-unix.c | 2 +- lib/fatal-signal.c | 27 ++++++++++++++++++++++++++- lib/vlog.c | 6 ++++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/openvswitch/vlog.h b/include/openvswitch/vlog.h index 19da4ab62320..476bf3d6d5b4 100644 --- a/include/openvswitch/vlog.h +++ b/include/openvswitch/vlog.h @@ -143,6 +143,9 @@ void vlog_set_syslog_method(const char *method); /* Configure syslog target. */ void vlog_set_syslog_target(const char *target); +/* Return the log_fd. */ +int vlog_get_fd(void); + /* Initialization. */ void vlog_init(void); void vlog_enable_async(void); diff --git a/lib/daemon-private.h b/lib/daemon-private.h index 4e0667601001..2b90e004235c 100644 --- a/lib/daemon-private.h +++ b/lib/daemon-private.h @@ -20,6 +20,7 @@ extern bool detach; extern char *pidfile; extern int daemonize_fd; +extern bool monitor; char *make_pidfile_name(const char *name); diff --git a/lib/daemon-unix.c b/lib/daemon-unix.c index 6b2a5b9bd4e6..ae59ecf2c2b5 100644 --- a/lib/daemon-unix.c +++ b/lib/daemon-unix.c @@ -80,7 +80,7 @@ int daemonize_fd = -1; /* --monitor: Should a supervisory process monitor the daemon and restart it if * it dies due to an error signal? */ -static bool monitor; +bool monitor; /* --user: Only root can use this option. Switch to new uid:gid after * initially running as root. */ diff --git a/lib/fatal-signal.c b/lib/fatal-signal.c index ba7f5bfd314d..4965c1ae82c0 100644 --- a/lib/fatal-signal.c +++ b/lib/fatal-signal.c @@ -187,7 +187,32 @@ send_backtrace_to_monitor(void) { dep++; } - ignore(write(daemonize_fd, unw_bt, dep * sizeof(struct unw_backtrace))); + if (monitor) { + ignore(write(daemonize_fd, unw_bt, + dep * sizeof(struct unw_backtrace))); + } else { + /* Since there is no monitor daemon running, write backtrace + * in current process. This is not asyn-signal-safe due to + * use of snprintf(). + */ + char str[] = "SIGSEGV detected, backtrace:\n"; + + if (vlog_get_fd() < 0) { + return; + } + + ignore(write(vlog_get_fd(), str, strlen(str))); + + for (int i = 0; i < dep; i++) { + char line[64]; + + snprintf(line, 64, "0x%016"PRIxPTR" <%s+0x%"PRIxPTR">\n", + unw_bt[i].ip, + unw_bt[i].func, + unw_bt[i].offset); + ignore(write(vlog_get_fd(), line, strlen(line))); + } + } } #else static inline void diff --git a/lib/vlog.c b/lib/vlog.c index 559943d87937..502b33fc831e 100644 --- a/lib/vlog.c +++ b/lib/vlog.c @@ -612,6 +612,12 @@ vlog_set_syslog_target(const char *target) ovs_rwlock_unlock(&pattern_rwlock); } +int +vlog_get_fd(void) +{ + return log_fd; +} + /* Returns 'false' if 'facility' is not a valid string. If 'facility' * is a valid string, sets 'value' with the integer value of 'facility' * and returns 'true'. */