Avoid logging service stop after failure to start.
[oweals/dinit.git] / src / service.cc
1 #include <cstring>
2 #include <cerrno>
3 #include <iterator>
4 #include <memory>
5 #include <cstddef>
6
7 #include <sys/ioctl.h>
8 #include <fcntl.h>
9 #include <termios.h>
10
11 #include "dinit.h"
12 #include "service.h"
13 #include "dinit-log.h"
14 #include "dinit-socket.h"
15 #include "dinit-util.h"
16 #include "baseproc-sys.h"
17
18 /*
19  * service.cc - Service management.
20  * See service.h for details.
21  */
22
23 // Find the requested service by name
24 static service_record * find_service(const std::list<service_record *> & records,
25                                     const char *name) noexcept
26 {
27     using std::list;
28     list<service_record *>::const_iterator i = records.begin();
29     for ( ; i != records.end(); i++ ) {
30         if (strcmp((*i)->get_name().c_str(), name) == 0) {
31             return *i;
32         }
33     }
34     return nullptr;
35 }
36
37 service_record * service_set::find_service(const std::string &name) noexcept
38 {
39     return ::find_service(records, name.c_str());
40 }
41
42 void service_set::stop_service(const std::string & name) noexcept
43 {
44     service_record *record = find_service(name);
45     if (record != nullptr) {
46         record->stop();
47         process_queues();
48     }
49 }
50
51 // Called when a service has actually stopped; dependents have stopped already, unless this stop
52 // is due to an unexpected process termination.
53 void service_record::stopped() noexcept
54 {
55     if (have_console) {
56         bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
57         discard_console_log_buffer();
58         release_console();
59     }
60
61     force_stop = false;
62
63     // If we are a soft dependency of another target, break the acquisition from that target now:
64     for (auto & dependent : dependents) {
65         if (dependent->dep_type != dependency_type::REGULAR) {
66             if (dependent->holding_acq) {
67                 dependent->holding_acq = false;
68                 release();
69             }
70         }
71     }
72
73     bool will_restart = (desired_state == service_state_t::STARTED)
74             && services->get_auto_restart();
75
76     for (auto dependency : depends_on) {
77         // we signal dependencies in case they are waiting for us to stop:
78         dependency.get_to()->dependent_stopped();
79     }
80
81     service_state = service_state_t::STOPPED;
82
83     if (will_restart) {
84         // Desired state is "started".
85         restarting = true;
86         start(false);
87     }
88     else {
89         becoming_inactive();
90         
91         if (start_explicit) {
92             // If we were explicitly started, our required_by count must be at least 1. Use
93             // release() to correctly release, mark inactive and release dependencies.
94             start_explicit = false;
95             release();
96         }
97         else if (required_by == 0) {
98             // This can only be the case if we didn't have start_explicit, since required_by would
99             // otherwise by non-zero.
100             prop_release = !prop_require;
101             prop_require = false;
102             services->add_prop_queue(this);
103             services->service_inactive(this);
104         }
105     }
106
107     // Start failure will have been logged already, only log if we are stopped for other reasons:
108     if (! start_failed) {
109         log_service_stopped(service_name);
110     }
111     notify_listeners(service_event_t::STOPPED);
112 }
113
114 bool service_record::do_auto_restart() noexcept
115 {
116     if (auto_restart) {
117         return services->get_auto_restart();
118     }
119     return false;
120 }
121
122 void service_record::require() noexcept
123 {
124     if (required_by++ == 0) {
125         prop_require = !prop_release;
126         prop_release = false;
127         services->add_prop_queue(this);
128         if (service_state != service_state_t::STARTING && service_state != service_state_t::STARTED) {
129             prop_start = true;
130         }
131     }
132 }
133
134 void service_record::release(bool issue_stop) noexcept
135 {
136     if (--required_by == 0) {
137         desired_state = service_state_t::STOPPED;
138
139         // Can stop, and can release dependencies now. We don't need to issue a release if
140         // the require was pending though:
141         prop_release = !prop_require;
142         prop_require = false;
143         services->add_prop_queue(this);
144
145         if (service_state == service_state_t::STOPPED) {
146             services->service_inactive(this);
147         }
148         else if (issue_stop) {
149             do_stop();
150         }
151     }
152 }
153
154 void service_record::release_dependencies() noexcept
155 {
156     for (auto & dependency : depends_on) {
157         service_record * dep_to = dependency.get_to();
158         if (dependency.holding_acq) {
159             // We must clear holding_acq before calling release, otherwise the dependency
160             // may decide to stop, check this link and release itself a second time.
161             dependency.holding_acq = false;
162             dep_to->release();
163         }
164     }
165 }
166
167 void service_record::start(bool activate) noexcept
168 {
169     if (activate && ! start_explicit) {
170         require();
171         start_explicit = true;
172     }
173     
174     if (desired_state == service_state_t::STARTED && service_state != service_state_t::STOPPED) return;
175
176     bool was_active = service_state != service_state_t::STOPPED || desired_state != service_state_t::STOPPED;
177     desired_state = service_state_t::STARTED;
178     
179     if (service_state != service_state_t::STOPPED) {
180         // We're already starting/started, or we are stopping and need to wait for
181         // that the complete.
182         if (service_state != service_state_t::STOPPING || ! can_interrupt_stop()) {
183             return;
184         }
185         // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
186         // but if so they are waiting (for us), so they too can be instantly returned to
187         // STARTING state.
188         notify_listeners(service_event_t::STOPCANCELLED);
189     }
190     else if (! was_active) {
191         services->service_active(this);
192     }
193
194     start_failed = false;
195     service_state = service_state_t::STARTING;
196     waiting_for_deps = true;
197
198     if (start_check_dependencies()) {
199         services->add_transition_queue(this);
200     }
201 }
202
203 void service_record::do_propagation() noexcept
204 {
205     if (prop_require) {
206         // Need to require all our dependencies
207         for (auto & dep : depends_on) {
208             dep.get_to()->require();
209             dep.holding_acq = true;
210         }
211         prop_require = false;
212     }
213     
214     if (prop_release) {
215         release_dependencies();
216         prop_release = false;
217     }
218     
219     if (prop_failure) {
220         prop_failure = false;
221         failed_to_start(true);
222     }
223     
224     if (prop_start) {
225         prop_start = false;
226         start(false);
227     }
228
229     if (prop_stop) {
230         prop_stop = false;
231         do_stop();
232     }
233 }
234
235 void service_record::execute_transition() noexcept
236 {
237     // state is STARTED with restarting set true if we are running a smooth recovery.
238     if (service_state == service_state_t::STARTING || (service_state == service_state_t::STARTED
239             && restarting)) {
240         if (check_deps_started()) {
241             all_deps_started();
242         }
243     }
244     else if (service_state == service_state_t::STOPPING) {
245         if (stop_check_dependents()) {
246             waiting_for_deps = false;
247             bring_down();
248         }
249     }
250 }
251
252 void service_record::do_start() noexcept
253 {
254     if (pinned_stopped) return;
255     
256     if (service_state != service_state_t::STARTING) {
257         return;
258     }
259     
260     service_state = service_state_t::STARTING;
261
262     waiting_for_deps = true;
263
264     // Ask dependencies to start, mark them as being waited on.
265     if (check_deps_started()) {
266         // Once all dependencies are started, we start properly:
267         all_deps_started();
268     }
269 }
270
271 void service_record::dependency_started() noexcept
272 {
273     // Note that we check for STARTED state here in case the service is in smooth recovery while pinned.
274     // In that case it will wait for dependencies to start before restarting the process.
275     if ((service_state == service_state_t::STARTING || service_state == service_state_t::STARTED)
276             && waiting_for_deps) {
277         services->add_transition_queue(this);
278     }
279 }
280
281 bool service_record::start_check_dependencies() noexcept
282 {
283     bool all_deps_started = true;
284
285     for (auto & dep : depends_on) {
286         service_record * to = dep.get_to();
287         if (to->service_state != service_state_t::STARTED) {
288             if (to->service_state != service_state_t::STARTING) {
289                 to->prop_start = true;
290                 services->add_prop_queue(to);
291             }
292             dep.waiting_on = true;
293             all_deps_started = false;
294         }
295     }
296     
297     return all_deps_started;
298 }
299
300 bool service_record::check_deps_started() noexcept
301 {
302     for (auto & dep : depends_on) {
303         if (dep.waiting_on) {
304             return false;
305         }
306     }
307
308     return true;
309 }
310
311 void service_record::all_deps_started() noexcept
312 {
313     if (onstart_flags.starts_on_console && ! have_console) {
314         queue_for_console();
315         return;
316     }
317     
318     waiting_for_deps = false;
319
320     if (! can_proceed_to_start()) {
321         waiting_for_deps = true;
322         return;
323     }
324
325     bool start_success = bring_up();
326     if (! start_success) {
327         failed_to_start();
328     }
329 }
330
331 void service_record::acquired_console() noexcept
332 {
333     waiting_for_console = false;
334     have_console = true;
335
336     if (service_state != service_state_t::STARTING) {
337         // We got the console but no longer want it.
338         release_console();
339     }
340     else if (check_deps_started()) {
341         all_deps_started();
342     }
343     else {
344         // We got the console but can't use it yet.
345         release_console();
346     }
347 }
348
349 void service_record::started() noexcept
350 {
351     // If we start on console but don't keep it, release it now:
352     if (have_console && ! onstart_flags.runs_on_console) {
353         bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
354         release_console();
355     }
356
357     log_service_started(get_name());
358     service_state = service_state_t::STARTED;
359     notify_listeners(service_event_t::STARTED);
360
361     if (onstart_flags.rw_ready) {
362         open_control_socket();
363     }
364     if (onstart_flags.log_ready) {
365         setup_external_log();
366     }
367
368     if (force_stop || desired_state == service_state_t::STOPPED) {
369         // We must now stop.
370         do_stop();
371         return;
372     }
373
374     // Notify any dependents whose desired state is STARTED:
375     for (auto dept : dependents) {
376         dept->get_from()->dependency_started();
377         dept->waiting_on = false;
378     }
379 }
380
381 void service_record::failed_to_start(bool depfailed, bool immediate_stop) noexcept
382 {
383     if (waiting_for_console) {
384         services->unqueue_console(this);
385         waiting_for_console = false;
386     }
387
388     if (start_explicit) {
389         start_explicit = false;
390         release(false);
391     }
392
393     // Cancel start of dependents:
394     for (auto & dept : dependents) {
395         switch (dept->dep_type) {
396         case dependency_type::REGULAR:
397         case dependency_type::MILESTONE:
398             if (dept->get_from()->service_state == service_state_t::STARTING) {
399                 dept->get_from()->prop_failure = true;
400                 services->add_prop_queue(dept->get_from());
401             }
402             break;
403         case dependency_type::WAITS_FOR:
404         case dependency_type::SOFT:
405             if (dept->waiting_on) {
406                 dept->waiting_on = false;
407                 dept->get_from()->dependency_started();
408             }
409         }
410
411         // Always release now, so that our desired state will be STOPPED before we call
412         // stopped() below (if we do so). Otherwise it may decide to restart us.
413         if (dept->holding_acq) {
414             dept->holding_acq = false;
415             release(false);
416         }
417     }
418
419     start_failed = true;
420     log_service_failed(get_name());
421     notify_listeners(service_event_t::FAILEDSTART);
422
423     if (immediate_stop) {
424         stopped();
425     }
426 }
427
428 bool service_record::bring_up() noexcept
429 {
430     // default implementation: there is no process, so we are started.
431     started();
432     return true;
433 }
434
435 // Mark this and all dependent services as force-stopped.
436 void service_record::forced_stop() noexcept
437 {
438     if (service_state != service_state_t::STOPPED) {
439         force_stop = true;
440         if (! pinned_started) {
441             prop_stop = true;
442             services->add_transition_queue(this);
443         }
444     }
445 }
446
447 void service_record::dependent_stopped() noexcept
448 {
449     if (service_state == service_state_t::STOPPING && waiting_for_deps) {
450         services->add_transition_queue(this);
451     }
452 }
453
454 void service_record::stop(bool bring_down) noexcept
455 {
456     if (start_explicit) {
457         start_explicit = false;
458         release();
459     }
460
461     if (bring_down) {
462         do_stop();
463     }
464 }
465
466 void service_record::do_stop() noexcept
467 {
468     // A service that does actually stop for any reason should have its explicit activation released, unless
469     // it will restart:
470     if (start_explicit && ! do_auto_restart()) {
471         start_explicit = false;
472         release(false);
473     }
474
475     bool all_deps_stopped = stop_dependents();
476
477     if (service_state != service_state_t::STARTED) {
478         if (service_state == service_state_t::STARTING) {
479             // If waiting for a dependency, or waiting for the console, we can interrupt start. Otherwise,
480             // we need to delegate to can_interrupt_start() (which can be overridden).
481             if (! waiting_for_deps && ! waiting_for_console) {
482                 if (! can_interrupt_start()) {
483                     // Well this is awkward: we're going to have to continue starting. We can stop once we've
484                     // reached the started state.
485                     return;
486                 }
487
488                 if (! interrupt_start()) {
489                     // Now wait for service startup to actually end; we don't need to handle it here.
490                     notify_listeners(service_event_t::STARTCANCELLED);
491                     return;
492                 }
493             }
494             else if (waiting_for_console) {
495                 services->unqueue_console(this);
496                 waiting_for_console = false;
497             }
498
499             // We must have had desired_state == STARTED.
500             notify_listeners(service_event_t::STARTCANCELLED);
501
502             // Reaching this point, we are starting interruptibly - so we
503             // stop now (by falling through to below).
504         }
505         else {
506             // If we're starting we need to wait for that to complete.
507             // If we're already stopping/stopped there's nothing to do.
508             return;
509         }
510     }
511
512     if (pinned_started) return;
513
514     service_state = service_state_t::STOPPING;
515     waiting_for_deps = true;
516     if (all_deps_stopped) {
517         services->add_transition_queue(this);
518     }
519 }
520
521 bool service_record::stop_check_dependents() noexcept
522 {
523     bool all_deps_stopped = true;
524     for (auto dept : dependents) {
525         if (dept->dep_type == dependency_type::REGULAR && ! dept->get_from()->is_stopped()) {
526             all_deps_stopped = false;
527             break;
528         }
529     }
530     
531     return all_deps_stopped;
532 }
533
534 bool service_record::stop_dependents() noexcept
535 {
536     bool all_deps_stopped = true;
537     for (auto dept : dependents) {
538         if (dept->dep_type == dependency_type::REGULAR ||
539                 (dept->dep_type == dependency_type::MILESTONE &&
540                 dept->get_from()->service_state != service_state_t::STARTED)) {
541             if (! dept->get_from()->is_stopped()) {
542                 // Note we check *first* since if the dependent service is not stopped,
543                 // 1. We will issue a stop to it shortly and
544                 // 2. It will notify us when stopped, at which point the stop_check_dependents()
545                 //    check is run anyway.
546                 all_deps_stopped = false;
547             }
548
549             if (force_stop) {
550                 // If this service is to be forcefully stopped, dependents must also be.
551                 dept->get_from()->forced_stop();
552             }
553
554             dept->get_from()->prop_stop = true;
555             services->add_prop_queue(dept->get_from());
556         }
557         else {
558             // waits-for or soft dependency:
559             if (dept->waiting_on) {
560                 dept->waiting_on = false;
561                 dept->get_from()->dependency_started();
562             }
563             if (dept->holding_acq) {
564                 dept->holding_acq = false;
565                 release();
566             }
567         }
568     }
569
570     return all_deps_stopped;
571 }
572
573 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
574 void service_record::bring_down() noexcept
575 {
576     waiting_for_deps = false;
577     stopped();
578 }
579
580 void service_record::unpin() noexcept
581 {
582     if (pinned_started) {
583         pinned_started = false;
584         if (desired_state == service_state_t::STOPPED || force_stop) {
585             do_stop();
586             services->process_queues();
587         }
588     }
589     if (pinned_stopped) {
590         pinned_stopped = false;
591         if (desired_state == service_state_t::STARTED) {
592             do_start();
593             services->process_queues();
594         }
595     }
596 }
597
598 void service_record::queue_for_console() noexcept
599 {
600     waiting_for_console = true;
601     services->append_console_queue(this);
602 }
603
604 void service_record::release_console() noexcept
605 {
606     have_console = false;
607     services->pull_console_queue();
608 }
609
610 bool service_record::interrupt_start() noexcept
611 {
612     return true;
613 }
614
615 void service_set::service_active(service_record *sr) noexcept
616 {
617     active_services++;
618 }
619
620 void service_set::service_inactive(service_record *sr) noexcept
621 {
622     active_services--;
623 }