condition.c revision a8b409dbc9e1e853a0f92d92603d9bb74592b1ff
/*-*- 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 Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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>
#include <sys/capability.h>
#include <fnmatch.h>
#ifdef HAVE_SELINUX
#endif
#include <systemd/sd-id128.h>
#include "util.h"
#include "condition.h"
#include "virt.h"
#include "path-util.h"
#include "fileio.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 b;
const char *id;
v = detect_virtualization(&id);
if (v < 0) {
return false;
}
b = parse_boolean(parameter);
if (v > 0 && b > 0)
return true;
if (v == 0 && b == 0)
return true;
/* Then, compare categorization */
return true;
return true;
/* Finally compare id */
}
static bool test_apparmor_enabled(void) {
int r;
_cleanup_free_ char *p = NULL;
r = read_one_line_file("/sys/module/apparmor/parameters/enabled", &p);
if (r < 0)
return false;
return parse_boolean(p) > 0;
}
static bool test_security(const char *parameter) {
#ifdef HAVE_SELINUX
return is_selinux_enabled() > 0;
#endif
return test_apparmor_enabled();
return false;
}
static bool test_capability(const char *parameter) {
FILE *f;
unsigned long long capabilities = (unsigned long long) -1;
/* If it's an invalid capability, we don't have it */
return false;
/* If it's a valid capability we default to assume
* that we have it */
if (!f)
return true;
break;
}
}
fclose(f);
}
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;
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;
}
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_NOT_EMPTY: {
return c->negate;
}
case CONDITION_FILE_IS_EXECUTABLE: {
return c->negate;
}
case CONDITION_VIRTUALIZATION:
case CONDITION_SECURITY:
case CONDITION_CAPABILITY:
case CONDITION_HOST:
case CONDITION_AC_POWER:
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_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"
};