771b4bc
From 04d1a9bf02d38024e6c8c6428b2e2da7519d64b8 Mon Sep 17 00:00:00 2001
771b4bc
From: Lennart Poettering <lennart@poettering.net>
771b4bc
Date: Thu, 12 Apr 2012 02:39:02 +0200
771b4bc
Subject: [PATCH] unit: introduce ConditionPathIsReadWrite (cherry picked from
771b4bc
 commit d051610953754ce2b79d23b83c1d5c167defd5be)
771b4bc
771b4bc
---
771b4bc
 man/systemd.unit.xml             |   22 +++++++++++++++-------
771b4bc
 src/core/condition.c             |   11 +++++++++++
771b4bc
 src/core/condition.h             |    1 +
771b4bc
 src/load-fragment-gperf.gperf.m4 |    1 +
771b4bc
 4 files changed, 28 insertions(+), 7 deletions(-)
771b4bc
771b4bc
diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml
771b4bc
index 3cc126b..fac6388 100644
771b4bc
--- a/man/systemd.unit.xml
771b4bc
+++ b/man/systemd.unit.xml
771b4bc
@@ -684,6 +684,7 @@
771b4bc
                                 <term><varname>ConditionPathIsDirectory=</varname></term>
771b4bc
                                 <term><varname>ConditionPathIsSymbolicLink=</varname></term>
771b4bc
                                 <term><varname>ConditionPathIsMountPoint=</varname></term>
771b4bc
+                                <term><varname>ConditionPathIsReadWrite=</varname></term>
771b4bc
                                 <term><varname>ConditionDirectoryNotEmpty=</varname></term>
771b4bc
                                 <term><varname>ConditionFileIsExecutable=</varname></term>
771b4bc
                                 <term><varname>ConditionKernelCommandLine=</varname></term>
771b4bc
@@ -731,7 +732,13 @@
771b4bc
                                 <varname>ConditionPathExists=</varname>
771b4bc
                                 but verifies whether a certain path
771b4bc
                                 exists and is a mount
771b4bc
-                                point. <varname>ConditionFileIsExecutable=</varname>
771b4bc
+                                point. <varname>ConditionPathIsReadWrite=</varname>
771b4bc
+                                is similar to
771b4bc
+                                <varname>ConditionPathExists=</varname>
771b4bc
+                                but verifies whether the underlying
771b4bc
+                                file system is read and writable
771b4bc
+                                (i.e. not mounted
771b4bc
+                                read-only). <varname>ConditionFileIsExecutable=</varname>
771b4bc
                                 is similar to
771b4bc
                                 <varname>ConditionPathExists=</varname>
771b4bc
                                 but verifies whether a certain path
771b4bc
@@ -780,12 +787,13 @@
771b4bc
                                 <varname>openvz</varname>,
771b4bc
                                 <varname>lxc</varname>,
771b4bc
                                 <varname>lxc-libvirt</varname>,
771b4bc
-                                <varname>systemd-nspawn</varname> to test
771b4bc
-                                against a specific implementation. If
771b4bc
-                                multiple virtualization technologies
771b4bc
-                                are nested only the innermost is
771b4bc
-                                considered. The test may be negated by
771b4bc
-                                prepending an exclamation mark.
771b4bc
+                                <varname>systemd-nspawn</varname> to
771b4bc
+                                test against a specific
771b4bc
+                                implementation. If multiple
771b4bc
+                                virtualization technologies are nested
771b4bc
+                                only the innermost is considered. The
771b4bc
+                                test may be negated by prepending an
771b4bc
+                                exclamation mark.
771b4bc
                                 <varname>ConditionSecurity=</varname>
771b4bc
                                 may be used to check whether the given
771b4bc
                                 security module is enabled on the
771b4bc
diff --git a/src/core/condition.c b/src/core/condition.c
771b4bc
index 2b51a16..4ed4eee 100644
771b4bc
--- a/src/core/condition.c
771b4bc
+++ b/src/core/condition.c
771b4bc
@@ -24,6 +24,7 @@
771b4bc
 #include <string.h>
771b4bc
 #include <unistd.h>
771b4bc
 #include <sys/capability.h>
771b4bc
+#include <sys/statvfs.h>
771b4bc
 
771b4bc
 #ifdef HAVE_SELINUX
771b4bc
 #include <selinux/selinux.h>
771b4bc
@@ -222,6 +223,15 @@ bool condition_test(Condition *c) {
771b4bc
         case CONDITION_PATH_IS_MOUNT_POINT:
771b4bc
                 return (path_is_mount_point(c->parameter, true) > 0) == !c->negate;
771b4bc
 
771b4bc
+        case CONDITION_PATH_IS_READ_WRITE: {
771b4bc
+                struct statvfs st;
771b4bc
+
771b4bc
+                if (statvfs(c->parameter, &st) < 0)
771b4bc
+                        return c->negate;
771b4bc
+
771b4bc
+                return !(st.f_flag & ST_RDONLY) == !c->negate;
771b4bc
+        }
771b4bc
+
771b4bc
         case CONDITION_DIRECTORY_NOT_EMPTY: {
771b4bc
                 int k;
771b4bc
 
771b4bc
@@ -313,6 +323,7 @@ static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
771b4bc
         [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
771b4bc
         [CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
771b4bc
         [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
771b4bc
+        [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
771b4bc
         [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
771b4bc
         [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
771b4bc
         [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
771b4bc
diff --git a/src/core/condition.h b/src/core/condition.h
771b4bc
index 71b1c67..087010c 100644
771b4bc
--- a/src/core/condition.h
771b4bc
+++ b/src/core/condition.h
771b4bc
@@ -32,6 +32,7 @@ typedef enum ConditionType {
771b4bc
         CONDITION_PATH_IS_DIRECTORY,
771b4bc
         CONDITION_PATH_IS_SYMBOLIC_LINK,
771b4bc
         CONDITION_PATH_IS_MOUNT_POINT,
771b4bc
+        CONDITION_PATH_IS_READ_WRITE,
771b4bc
         CONDITION_DIRECTORY_NOT_EMPTY,
771b4bc
         CONDITION_FILE_IS_EXECUTABLE,
771b4bc
         CONDITION_KERNEL_COMMAND_LINE,
771b4bc
diff --git a/src/load-fragment-gperf.gperf.m4 b/src/load-fragment-gperf.gperf.m4
771b4bc
index 4b02e31..c65db30 100644
771b4bc
--- a/src/load-fragment-gperf.gperf.m4
771b4bc
+++ b/src/load-fragment-gperf.gperf.m4
771b4bc
@@ -118,6 +118,7 @@ Unit.ConditionPathExistsGlob,    config_parse_unit_condition_path,   CONDITION_P
771b4bc
 Unit.ConditionPathIsDirectory,   config_parse_unit_condition_path,   CONDITION_PATH_IS_DIRECTORY,   0
771b4bc
 Unit.ConditionPathIsSymbolicLink,config_parse_unit_condition_path,   CONDITION_PATH_IS_SYMBOLIC_LINK,0
771b4bc
 Unit.ConditionPathIsMountPoint,  config_parse_unit_condition_path,   CONDITION_PATH_IS_MOUNT_POINT, 0
771b4bc
+Unit.ConditionPathIsReadWrite,   config_parse_unit_condition_path,   CONDITION_PATH_IS_READ_WRITE,  0
771b4bc
 Unit.ConditionDirectoryNotEmpty, config_parse_unit_condition_path,   CONDITION_DIRECTORY_NOT_EMPTY, 0
771b4bc
 Unit.ConditionFileIsExecutable,  config_parse_unit_condition_path,   CONDITION_FILE_IS_EXECUTABLE,  0
771b4bc
 Unit.ConditionKernelCommandLine, config_parse_unit_condition_string, CONDITION_KERNEL_COMMAND_LINE, 0