From patchwork Mon Jul 6 21:37:11 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 491834 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 5CFD6140345 for ; Tue, 7 Jul 2015 07:37:50 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=giZ90ZVC; dkim-atps=neutral Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 98C2F28AD38; Mon, 6 Jul 2015 23:37:17 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, T_DKIM_INVALID autolearn=no version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id D07BB28073C for ; Mon, 6 Jul 2015 23:37:13 +0200 (CEST) X-policyd-weight: using cached result; rate: -8.5 Received: from mail-la0-f47.google.com (mail-la0-f47.google.com [209.85.215.47]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Mon, 6 Jul 2015 23:37:13 +0200 (CEST) Received: by lagc2 with SMTP id c2so170753036lag.3 for ; Mon, 06 Jul 2015 14:37:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=trbXKdST07jEJDPL9eStmzHyyyJbBSyA5428GY6/Zas=; b=giZ90ZVC/9bpYP+aMP9AAfZXAmYz84HZ88VkwsyFZ3AEFht6fAldsU5hk9XYYueG0Q ASktYd6GbsbMs4KkfV+mzis4wPhdcE27ob4ZNJgQKA/j+vziHRB8S9tCuyzoHPNyfSw9 XjSkO1ope37ACZpMbzj/bvinL8HzjlLlu9UCOfVqyQHcyKV9aVeVES2xh4LRyPisO7mj CdUC4BIBZ4rwGT3plVDG05cN0ZDrYWp2IqNM837WhBAzyc6PBpgEFSVH36vyAPDGxqkh RfAHP03xqEeaUA3zPdyuGaRdla+a+4ahWBug5AK46Brui8cQXEa85u8ovX7bFbFSlcEh UHvQ== X-Received: by 10.152.36.102 with SMTP id p6mr873992laj.19.1436218649989; Mon, 06 Jul 2015 14:37:29 -0700 (PDT) Received: from linux-tdhb.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by mx.google.com with ESMTPSA id r6sm5088551lbw.10.2015.07.06.14.37.28 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Jul 2015 14:37:29 -0700 (PDT) From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= To: Felix Fietkau , John Crispin Date: Mon, 6 Jul 2015 23:37:11 +0200 Message-Id: <1436218631-13495-1-git-send-email-zajec5@gmail.com> X-Mailer: git-send-email 1.8.4.5 In-Reply-To: <1434829370-29540-1-git-send-email-zajec5@gmail.com> References: <1434829370-29540-1-git-send-email-zajec5@gmail.com> MIME-Version: 1.0 Cc: openwrt-devel@lists.openwrt.org, Steven Barth Subject: [OpenWrt-Devel] [PATCH V3] service: if logging start app with LD_PRELOAD & lib for line buffering X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" Using pipe automatically switches service to block buffering which kind of breaks our logging. We won't get anything from stdout FD until the buffer gets filled fully or the service exits. This makes log messages appear with an unwanted delay. This change adds a tiny libsetlbf.so switching stdout to line buffering and uses this lib for every logging-enabled service started by procd. We don't need any extra change for stderr as it's unbuffered by default. Signed-off-by: Rafał Miłecki --- V2: Use strncat for safety Use line buffering instead of no buffering V3: Use asprintf & putenv as suggested byu cyrus --- CMakeLists.txt | 7 +++++++ service/instance.c | 19 ++++++++++++++++++- service/setlbf.c | 6 ++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 service/setlbf.c diff --git a/CMakeLists.txt b/CMakeLists.txt index dfa9413..6af17a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,13 @@ IF(APPLE) LINK_DIRECTORIES(/opt/local/lib) ENDIF() + +ADD_LIBRARY(setlbf SHARED service/setlbf.c) +INSTALL(TARGETS setlbf + LIBRARY DESTINATION lib +) + + SET(SOURCES procd.c signal.c watchdog.c state.c inittab.c rcS.c ubus.c system.c service/service.c service/instance.c service/validate.c service/trigger.c service/watch.c plug/coldplug.c plug/hotplug.c utils/utils.c) diff --git a/service/instance.c b/service/instance.c index 35b2def..0bd0bed 100644 --- a/service/instance.c +++ b/service/instance.c @@ -12,6 +12,7 @@ * GNU General Public License for more details. */ +#define _GNU_SOURCE #include #include #include @@ -19,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -224,6 +226,9 @@ instance_run(struct service_instance *in, int _stdout, int _stderr) struct blobmsg_list_node *var; struct blob_attr *cur; char **argv; + char *ld_preload; + bool ld_preload_seccomp = false; + bool ld_preload_setlbf = false; int argc = 1; /* NULL terminated */ int rem, _stdin; @@ -238,7 +243,19 @@ instance_run(struct service_instance *in, int _stdout, int _stderr) if (!in->trace && !in->has_jail && in->seccomp) { setenv("SECCOMP_FILE", in->seccomp, 1); - setenv("LD_PRELOAD", "/lib/libpreload-seccomp.so", 1); + ld_preload_seccomp = true; + } + if (_stdout >= 0) { + ld_preload_setlbf = true; + } + + if (ld_preload_seccomp || ld_preload_setlbf) { + int bytes = asprintf(&ld_preload, "LD_PRELOAD=%s%s%s", + ld_preload_seccomp ? "/lib/libpreload-seccomp.so" : "", + ld_preload_seccomp && ld_preload_setlbf ? ":" : "", + ld_preload_setlbf ? "/lib/libsetlbf.so" : ""); + if (bytes) + putenv(ld_preload); } blobmsg_list_for_each(&in->limits, var) diff --git a/service/setlbf.c b/service/setlbf.c new file mode 100644 index 0000000..df00366 --- /dev/null +++ b/service/setlbf.c @@ -0,0 +1,6 @@ +#include + +static void __attribute__((constructor)) setlbf(void) +{ + setlinebuf(stdout); +}