restart stopped instances on update
[project/procd.git] / instance.c
index 112db6f540d9fdbfa95b4f44a453f26601588ad4..a1459b74dcadc62299a62250bb0d0644241510bb 100644 (file)
@@ -1,3 +1,17 @@
+/*
+ * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
 #include <sys/resource.h>
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -90,7 +104,7 @@ instance_start(struct service_instance *in)
                return;
        }
 
-       DPRINTF("Started instance %s::%s\n", in->srv->name, in->name);
+       DEBUG(1, "Started instance %s::%s\n", in->srv->name, in->name);
        in->proc.pid = pid;
        uloop_process_add(&in->proc);
 }
@@ -112,7 +126,7 @@ instance_exit(struct uloop_process *p, int ret)
        struct service_instance *in;
 
        in = container_of(p, struct service_instance, proc);
-       DPRINTF("Instance %s::%s exit with error code %d\n", in->srv->name, in->name, ret);
+       DEBUG(1, "Instance %s::%s exit with error code %d\n", in->srv->name, in->name, ret);
        uloop_timeout_cancel(&in->timeout);
        if (in->restart)
                instance_start(in);
@@ -308,13 +322,20 @@ bool
 instance_update(struct service_instance *in, struct service_instance *in_new)
 {
        bool changed = instance_config_changed(in, in_new);
+       bool running = in->proc.pending;
 
-       if (!changed)
+       if (!changed && running)
                return false;
 
-       in->restart = true;
-       instance_stop(in, true);
-       instance_config_move(in, in_new);
+       if (!running) {
+               if (changed)
+                       instance_config_move(in, in_new);
+               instance_start(in);
+       } else {
+               in->restart = true;
+               instance_stop(in, true);
+               instance_config_move(in, in_new);
+       }
        return true;
 }