condition.c revision 0d60602c3b4d4b65da672d75c6146f2ea4b27f88
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd 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.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_SELINUX
#endif
#include "util.h"
#include "condition.h"
Condition *c;
if (!c)
return NULL;
if (parameter) {
if (!c->parameter) {
free(c);
return NULL;
}
}
return c;
}
void condition_free(Condition *c) {
assert(c);
free(c);
}
Condition *c, *n;
condition_free(c);
}
static bool test_kernel_command_line(const char *parameter) {
bool equal;
int r;
bool found = false;
if (detect_container(NULL) > 0)
return false;
if (r < 0) {
return false;
}
if (!word)
break;
if (equal) {
found = true;
break;
}
} else {
found = true;
break;
}
}
}
return found;
}
static bool test_virtualization(const char *parameter) {
int r, b;
const char *id;
r = detect_virtualization(&id);
if (r < 0) {
return false;
}
b = parse_boolean(parameter);
if (r > 0 && b > 0)
return true;
if (r == 0 && b == 0)
return true;
}
static bool test_security(const char *parameter) {
#ifdef HAVE_SELINUX
return is_selinux_enabled() > 0;
#endif
return false;
}
bool condition_test(Condition *c) {
assert(c);
switch(c->type) {
case CONDITION_PATH_EXISTS:
case CONDITION_PATH_IS_DIRECTORY: {
return !c->negate;
}
case CONDITION_PATH_IS_SYMBOLIC_LINK: {
return !c->negate;
}
case CONDITION_DIRECTORY_NOT_EMPTY: {
int k;
k = dir_is_empty(c->parameter);
}
case CONDITION_FILE_IS_EXECUTABLE: {
return !c->negate;
}
case CONDITION_VIRTUALIZATION:
case CONDITION_SECURITY:
case CONDITION_NULL:
return !c->negate;
default:
assert_not_reached("Invalid condition type.");
}
}
Condition *c;
int triggered = -1;
/* If the condition list is empty, then it is true */
if (!first)
return true;
/* Otherwise, if all of the non-trigger conditions apply and
* if any of the trigger conditions apply (unless there are
* none) we return true */
bool b;
b = condition_test(c);
if (!c->trigger && !b)
return false;
triggered = b;
}
return triggered != 0;
}
assert(c);
assert(f);
if (!prefix)
prefix = "";
fprintf(f,
"%s\t%s: %s%s%s\n",
c->parameter);
}
Condition *c;
condition_dump(c, f, prefix);
}
static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
[CONDITION_PATH_EXISTS] = "ConditionPathExists",
[CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
[CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
[CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
[CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
[CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
[CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
[CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
[CONDITION_SECURITY] = "ConditionSecurity",
[CONDITION_NULL] = "ConditionNull"
};