chiark / gitweb /
service: when we complain about a notify message we cannot map to main pid because...
[elogind.git] / src / core / condition.c
index 5dad5248bbac8bbcb2068493e8d6b4263d16725d..436e7840d0e63a35883d9195565183a4bde384d6 100644 (file)
 #include <string.h>
 #include <unistd.h>
 #include <sys/capability.h>
+#include <sys/statvfs.h>
+#include <fnmatch.h>
 
-#ifdef HAVE_SELINUX
-#include <selinux/selinux.h>
-#endif
-
+#include <systemd/sd-id128.h>
 #include "util.h"
 #include "condition.h"
 #include "virt.h"
+#include "path-util.h"
+#include "fileio.h"
+#include "unit.h"
+#include "smack-util.h"
+#include "apparmor-util.h"
+#include "ima-util.h"
+#include "selinux-util.h"
 
 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
         Condition *c;
@@ -80,14 +86,11 @@ static bool test_kernel_command_line(const char *parameter) {
 
         assert(parameter);
 
-        if (detect_container(NULL) > 0)
-                return false;
-
-        r = read_one_line_file("/proc/cmdline", &line);
-        if (r < 0) {
+        r = proc_cmdline(&line);
+        if (r < 0)
                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
+        if (r <= 0)
                 return false;
-        }
 
         equal = !!strchr(parameter, '=');
         pl = strlen(parameter);
@@ -153,10 +156,14 @@ static bool test_virtualization(const char *parameter) {
 }
 
 static bool test_security(const char *parameter) {
-#ifdef HAVE_SELINUX
         if (streq(parameter, "selinux"))
-                return is_selinux_enabled() > 0;
-#endif
+                return use_selinux();
+        if (streq(parameter, "apparmor"))
+                return use_apparmor();
+        if (streq(parameter, "ima"))
+                return use_ima();
+        if (streq(parameter, "smack"))
+                return use_smack();
         return false;
 }
 
@@ -192,7 +199,42 @@ static bool test_capability(const char *parameter) {
         return !!(capabilities & (1ULL << value));
 }
 
-bool condition_test(Condition *c) {
+static bool test_host(const char *parameter) {
+        sd_id128_t x, y;
+        char *h;
+        int r;
+        bool b;
+
+        if (sd_id128_from_string(parameter, &x) >= 0) {
+
+                r = sd_id128_get_machine(&y);
+                if (r < 0)
+                        return false;
+
+                return sd_id128_equal(x, y);
+        }
+
+        h = gethostname_malloc();
+        if (!h)
+                return false;
+
+        b = fnmatch(parameter, h, FNM_CASEFOLD) == 0;
+        free(h);
+
+        return b;
+}
+
+static bool test_ac_power(const char *parameter) {
+        int r;
+
+        r = parse_boolean(parameter);
+        if (r < 0)
+                return true;
+
+        return (on_ac_power() != 0) == !!r;
+}
+
+static bool condition_test(Condition *c) {
         assert(c);
 
         switch(c->type) {
@@ -222,6 +264,9 @@ bool condition_test(Condition *c) {
         case CONDITION_PATH_IS_MOUNT_POINT:
                 return (path_is_mount_point(c->parameter, true) > 0) == !c->negate;
 
+        case CONDITION_PATH_IS_READ_WRITE:
+                return (path_is_read_only_fs(c->parameter) > 0) == c->negate;
+
         case CONDITION_DIRECTORY_NOT_EMPTY: {
                 int k;
 
@@ -229,6 +274,15 @@ bool condition_test(Condition *c) {
                 return !(k == -ENOENT || k > 0) == !c->negate;
         }
 
+        case CONDITION_FILE_NOT_EMPTY: {
+                struct stat st;
+
+                if (stat(c->parameter, &st) < 0)
+                        return c->negate;
+
+                return (S_ISREG(st.st_mode) && st.st_size > 0) == !c->negate;
+        }
+
         case CONDITION_FILE_IS_EXECUTABLE: {
                 struct stat st;
 
@@ -250,6 +304,12 @@ bool condition_test(Condition *c) {
         case CONDITION_CAPABILITY:
                 return test_capability(c->parameter) == !c->negate;
 
+        case CONDITION_HOST:
+                return test_host(c->parameter) == !c->negate;
+
+        case CONDITION_AC_POWER:
+                return test_ac_power(c->parameter) == !c->negate;
+
         case CONDITION_NULL:
                 return !c->negate;
 
@@ -258,7 +318,7 @@ bool condition_test(Condition *c) {
         }
 }
 
-bool condition_test_list(Condition *first) {
+bool condition_test_list(const char *unit, Condition *first) {
         Condition *c;
         int triggered = -1;
 
@@ -273,6 +333,16 @@ bool condition_test_list(Condition *first) {
                 bool b;
 
                 b = condition_test(c);
+                if (unit)
+                        log_debug_unit(unit,
+                                       "%s=%s%s%s %s for %s.",
+                                       condition_type_to_string(c->type),
+                                       c->trigger ? "|" : "",
+                                       c->negate ? "!" : "",
+                                       c->parameter,
+                                       b ? "succeeded" : "failed",
+                                       unit);
+                c->state = b ? 1 : -1;
 
                 if (!c->trigger && !b)
                         return false;
@@ -292,12 +362,13 @@ void condition_dump(Condition *c, FILE *f, const char *prefix) {
                 prefix = "";
 
         fprintf(f,
-                "%s\t%s: %s%s%s\n",
+                "%s\t%s: %s%s%s %s\n",
                 prefix,
                 condition_type_to_string(c->type),
                 c->trigger ? "|" : "",
                 c->negate ? "!" : "",
-                c->parameter);
+                c->parameter,
+                c->state < 0 ? "failed" : c->state > 0 ? "succeeded" : "untested");
 }
 
 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
@@ -313,10 +384,16 @@ static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
         [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
         [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
         [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
+        [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
         [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
+        [CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
+        [CONDITION_FILE_IS_EXECUTABLE] = "ConditionFileIsExecutable",
         [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
         [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
         [CONDITION_SECURITY] = "ConditionSecurity",
+        [CONDITION_CAPABILITY] = "ConditionCapability",
+        [CONDITION_HOST] = "ConditionHost",
+        [CONDITION_AC_POWER] = "ConditionACPower",
         [CONDITION_NULL] = "ConditionNull"
 };