chiark / gitweb /
execute: load environment files at time of execution, not when we load the service...
[elogind.git] / src / condition.c
index 8c2db2d3e92ab42d0a5e344b39816cab7dabf118..630350ed365ea818b4ac0806bfb3cb8940322746 100644 (file)
@@ -3,7 +3,7 @@
 /***
   This file is part of systemd.
 
-  Copyright 2010 ProFUSION embedded systems
+  Copyright 2010 Lennart Poettering
 
   systemd is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
@@ -34,10 +34,11 @@ Condition* condition_new(ConditionType type, const char *parameter, bool negate)
         c->type = type;
         c->negate = negate;
 
-        if (!(c->parameter = strdup(parameter))) {
-                free(c);
-                return NULL;
-        }
+        if (parameter)
+                if (!(c->parameter = strdup(parameter))) {
+                        free(c);
+                        return NULL;
+                }
 
         return c;
 }
@@ -63,6 +64,8 @@ static bool test_kernel_command_line(const char *parameter) {
         size_t l, pl;
         bool found = false;
 
+        assert(parameter);
+
         if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
                 return false;
@@ -97,6 +100,28 @@ static bool test_kernel_command_line(const char *parameter) {
         return found;
 }
 
+static bool test_virtualization(const char *parameter) {
+        int r, b;
+        const char *id;
+
+        assert(parameter);
+
+        if ((r = detect_virtualization(&id)) < 0) {
+                log_warning("Failed to detect virtualization, ignoring: %s", strerror(-r));
+                return false;
+        }
+
+        b = parse_boolean(parameter);
+
+        if (r > 0 && b > 0)
+                return true;
+
+        if (r == 0 && b == 0)
+                return true;
+
+        return streq(parameter, id);
+}
+
 bool condition_test(Condition *c) {
         assert(c);
 
@@ -105,8 +130,21 @@ bool condition_test(Condition *c) {
         case CONDITION_PATH_EXISTS:
                 return (access(c->parameter, F_OK) >= 0) == !c->negate;
 
+        case CONDITION_DIRECTORY_NOT_EMPTY: {
+                int k;
+
+                k = dir_is_empty(c->parameter);
+                return !(k == -ENOENT || k > 0) == !c->negate;
+        }
+
         case CONDITION_KERNEL_COMMAND_LINE:
-                return !!test_kernel_command_line(c->parameter) == !c->negate;
+                return test_kernel_command_line(c->parameter) == !c->negate;
+
+        case CONDITION_VIRTUALIZATION:
+                return test_virtualization(c->parameter) == !c->negate;
+
+        case CONDITION_NULL:
+                return !c->negate;
 
         default:
                 assert_not_reached("Invalid condition type.");
@@ -152,7 +190,8 @@ void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
 
 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
         [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
-        [CONDITION_PATH_EXISTS] = "ConditionPathExists"
+        [CONDITION_PATH_EXISTS] = "ConditionPathExists",
+        [CONDITION_NULL] = "ConditionNull"
 };
 
 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);