summaryrefslogtreecommitdiffstats
path: root/service
diff options
context:
space:
mode:
authorRafał Miłecki <zajec5@gmail.com>2015-07-06 23:37:11 +0200
committerSteven Barth <steven@midlink.org>2015-07-07 10:36:04 +0200
commit6f841e89dfe9200a6f3a2f93a9dd83f66f8ef550 (patch)
treec97c99efe03a6bcb985ed356cd92bae6fa819fb2 /service
parent8f33afd87f118bfea52f50f7787843b694a38e72 (diff)
downloadunitd-6f841e89dfe9200a6f3a2f93a9dd83f66f8ef550.tar
unitd-6f841e89dfe9200a6f3a2f93a9dd83f66f8ef550.zip
service: if logging start app with LD_PRELOAD & lib for line buffering
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 <zajec5@gmail.com> Modified to use no buffering to align with stderr. Several cleanups. Signed-off-by: Steven Barth <steven@midlink.org>
Diffstat (limited to 'service')
-rw-r--r--service/instance.c15
-rw-r--r--service/setlbf.c6
2 files changed, 18 insertions, 3 deletions
diff --git a/service/instance.c b/service/instance.c
index 35b2def..40ff021 100644
--- a/service/instance.c
+++ b/service/instance.c
@@ -12,6 +12,7 @@
* GNU General Public License for more details.
*/
+#define _GNU_SOURCE
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/socket.h>
@@ -19,6 +20,7 @@
#include <net/if.h>
#include <unistd.h>
#include <stdint.h>
+#include <stdio.h>
#include <fcntl.h>
#include <pwd.h>
#include <libgen.h>
@@ -224,8 +226,11 @@ instance_run(struct service_instance *in, int _stdout, int _stderr)
struct blobmsg_list_node *var;
struct blob_attr *cur;
char **argv;
+ char *ld_preload;
int argc = 1; /* NULL terminated */
int rem, _stdin;
+ bool seccomp = !in->trace && !in->has_jail && in->seccomp;
+ bool setlbf = _stdout >= 0;
if (in->nice)
setpriority(PRIO_PROCESS, 0, in->nice);
@@ -236,10 +241,14 @@ instance_run(struct service_instance *in, int _stdout, int _stderr)
blobmsg_list_for_each(&in->env, var)
setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
- if (!in->trace && !in->has_jail && in->seccomp) {
+ if (seccomp)
setenv("SECCOMP_FILE", in->seccomp, 1);
- setenv("LD_PRELOAD", "/lib/libpreload-seccomp.so", 1);
- }
+
+ if ((seccomp || setlbf) && asprintf(&ld_preload, "LD_PRELOAD=%s%s%s",
+ seccomp ? "/lib/libpreload-seccomp.so" : "",
+ seccomp && setlbf ? ":" : "",
+ setlbf ? "/lib/libsetlbf.so" : "") > 0)
+ putenv(ld_preload);
blobmsg_list_for_each(&in->limits, var)
instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
diff --git a/service/setlbf.c b/service/setlbf.c
new file mode 100644
index 0000000..94c251e
--- /dev/null
+++ b/service/setlbf.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+static void __attribute__((constructor)) setlbf(void)
+{
+ setbuf(stdout, NULL);
+}