chiark / gitweb /
condition: unify condition logic in one file
[elogind.git] / src / core / condition.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "condition.h"
23 #include "unit.h"
24
25 bool condition_test_list(const char *unit, Condition *first) {
26         Condition *c;
27         int triggered = -1;
28
29         /* If the condition list is empty, then it is true */
30         if (!first)
31                 return true;
32
33         /* Otherwise, if all of the non-trigger conditions apply and
34          * if any of the trigger conditions apply (unless there are
35          * none) we return true */
36         LIST_FOREACH(conditions, c, first) {
37                 int r;
38
39                 r = condition_test(c);
40                 if (r < 0)
41                         log_warning_unit(unit,
42                                          "Couldn't determine result for %s=%s%s%s for %s, assuming failed: %s",
43                                          condition_type_to_string(c->type),
44                                          c->trigger ? "|" : "",
45                                          c->negate ? "!" : "",
46                                          c->parameter,
47                                          unit,
48                                          strerror(-r));
49                 else
50                         log_debug_unit(unit,
51                                        "%s=%s%s%s %s for %s.",
52                                        condition_type_to_string(c->type),
53                                        c->trigger ? "|" : "",
54                                        c->negate ? "!" : "",
55                                        c->parameter,
56                                        r > 0 ? "succeeded" : "failed",
57                                        unit);
58
59                 c->state = r > 0 ? CONDITION_STATE_SUCCEEDED : CONDITION_STATE_FAILED;
60
61                 if (!c->trigger && r <= 0)
62                         return false;
63
64                 if (c->trigger && triggered <= 0)
65                         triggered = r > 0;
66         }
67
68         return triggered != 0;
69 }