[OpenWrt-Devel] [PATCH procd] service: start apps with LD_PRELOAD & lib disabling buffering

Rafał Miłecki zajec5 at gmail.com
Fri Jun 19 19:03:37 EDT 2015


Using pipe automatically switches service to block buffering which kind
of breaks our logging. We won't get anything from FD until the buffer
gets filled fully or the service exits. This makes log messages appear
with an unwanted delay.
This adds a tiny libsetnbf.so that disables stdout buffering and uses it
for every service started by procd.

Signed-off-by: Rafał Miłecki <zajec5 at gmail.com>
---
 CMakeLists.txt     |  7 +++++++
 service/instance.c | 12 +++++++++++-
 service/setnbf.c   |  6 ++++++
 3 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 service/setnbf.c

diff --git a/CMakeLists.txt b/CMakeLists.txt
index dfa9413..c3b7c1e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,6 +10,13 @@ IF(APPLE)
   LINK_DIRECTORIES(/opt/local/lib)
 ENDIF()
 
+
+ADD_LIBRARY(setnbf SHARED service/setnbf.c)
+INSTALL(TARGETS setnbf
+	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..726f859 100644
--- a/service/instance.c
+++ b/service/instance.c
@@ -224,6 +224,7 @@ instance_run(struct service_instance *in, int _stdout, int _stderr)
 	struct blobmsg_list_node *var;
 	struct blob_attr *cur;
 	char **argv;
+	char ld_preload[64] = {}; /* Has to be big enough for all cases */
 	int argc = 1; /* NULL terminated */
 	int rem, _stdin;
 
@@ -238,8 +239,17 @@ 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);
+		if (ld_preload[0])
+			strcat(ld_preload, ":");
+		strcat(ld_preload, "/lib/libpreload-seccomp.so");
 	}
+	if (_stdout >= 0) {
+		if (ld_preload[0])
+			strcat(ld_preload, ":");
+		strcat(ld_preload, "/lib/libsetnbf.so");
+	}
+	if (ld_preload[0])
+		setenv("LD_PRELOAD", ld_preload, 1);
 
 	blobmsg_list_for_each(&in->limits, var)
 		instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
diff --git a/service/setnbf.c b/service/setnbf.c
new file mode 100644
index 0000000..7b5f3bd
--- /dev/null
+++ b/service/setnbf.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+static void __attribute__((constructor)) setnbf(void)
+{
+	setbuf(stdout, NULL);
+}
-- 
1.8.4.5
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list