service: close instance pipe fd on restart.
[project/procd.git] / service / instance.c
index 33678851e6321160c2a24609ee8a86dac599035d..29fb834e03cdbea9cdd0f1cc60956cb711dbc847 100644 (file)
@@ -295,6 +295,8 @@ instance_run(struct service_instance *in, int _stdout, int _stderr)
        exit(127);
 }
 
+static void instance_free_stdio(struct service_instance *in);
+
 void
 instance_start(struct service_instance *in)
 {
@@ -310,6 +312,7 @@ instance_start(struct service_instance *in)
        if (in->proc.pending)
                return;
 
+       instance_free_stdio(in);
        if (in->_stdout.fd.fd > -2) {
                if (pipe(opipe)) {
                        ULOG_WARN("pipe() failed: %d (%s)\n", errno, strerror(errno));
@@ -366,6 +369,10 @@ instance_stdio(struct ustream *s, int prio, struct service_instance *in)
        char *newline, *str, *arg0, ident[32];
        int len;
 
+       arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
+       snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
+       ulog_open(ULOG_SYSLOG, LOG_DAEMON, ident);
+
        do {
                str = ustream_get_read_buf(s, NULL);
                if (!str)
@@ -376,17 +383,13 @@ instance_stdio(struct ustream *s, int prio, struct service_instance *in)
                        break;
 
                *newline = 0;
-               len = newline + 1 - str;
-
-               arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
-               snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
-
-               ulog_open(ULOG_SYSLOG, LOG_DAEMON, ident);
                ulog(prio, "%s\n", str);
-               ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
 
+               len = newline + 1 - str;
                ustream_consume(s, len);
        } while (1);
+
+       ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
 }
 
 static void
@@ -820,19 +823,26 @@ instance_update(struct service_instance *in, struct service_instance *in_new)
        return true;
 }
 
-void
-instance_free(struct service_instance *in)
+static void
+instance_free_stdio(struct service_instance *in)
 {
        if (in->_stdout.fd.fd > -1) {
                ustream_free(&in->_stdout.stream);
                close(in->_stdout.fd.fd);
+               in->_stdout.fd.fd = -1;
        }
 
        if (in->_stderr.fd.fd > -1) {
                ustream_free(&in->_stderr.stream);
                close(in->_stderr.fd.fd);
+               in->_stderr.fd.fd = -1;
        }
+}
 
+void
+instance_free(struct service_instance *in)
+{
+       instance_free_stdio(in);
        uloop_process_delete(&in->proc);
        uloop_timeout_cancel(&in->timeout);
        trigger_del(in);
@@ -917,8 +927,8 @@ void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
 
        if (in->respawn) {
                void *r = blobmsg_open_table(b, "respawn");
-               blobmsg_add_u32(b, "timeout", in->respawn_timeout);
                blobmsg_add_u32(b, "threshold", in->respawn_threshold);
+               blobmsg_add_u32(b, "timeout", in->respawn_timeout);
                blobmsg_add_u32(b, "retry", in->respawn_retry);
                blobmsg_close_table(b, r);
        }