]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
util: add functions to keep capabilities
authorTaku Izumi <izumi.taku@jp.fujitsu.com>
Tue, 31 Jan 2012 04:50:00 +0000 (23:50 -0500)
committerLaine Stump <laine@laine.org>
Tue, 31 Jan 2012 18:36:28 +0000 (13:36 -0500)
This patch introduces virSetCapabilities() function and implements
virCommandAllowCap() function.

Existing virClearCapabilities() is function to clear all capabilities.
Instead virSetCapabilities() is function to set arbitrary capabilities.

Signed-off-by: Taku Izumi <izumi.taku@jp.fujitsu.com>
Signed-off-by: Shota Hirae <m11g1401@hibikino.ne.jp>
src/util/command.c
src/util/command.h

index dc3cfc54306c009c3ee69a1e4abbb55bc1f41032..6b43584d4cc243eb1e9184bde4ac73478ccdd6eb 100644 (file)
@@ -103,6 +103,8 @@ struct _virCommand {
     pid_t pid;
     char *pidfile;
     bool reap;
+
+    unsigned long long capabilities;
 };
 
 /*
@@ -168,6 +170,7 @@ virCommandFDSet(int fd,
 #ifndef WIN32
 
 # if HAVE_CAPNG
+static int virClearCapabilities(void) ATTRIBUTE_UNUSED;
 static int virClearCapabilities(void)
 {
     int ret;
@@ -182,6 +185,33 @@ static int virClearCapabilities(void)
 
     return 0;
 }
+
+/**
+ * virSetCapabilities:
+ *  @capabilities - capability flag to set.
+ *                  In case of 0, this function is identical to
+ *                  virClearCapabilities()
+ *
+ */
+static int virSetCapabilities(unsigned long long capabilities)
+{
+    int ret, i;
+
+    capng_clear(CAPNG_SELECT_BOTH);
+
+    for (i = 0; i <= CAP_LAST_CAP; i++) {
+        if (capabilities & (1ULL << i))
+            capng_update(CAPNG_ADD, CAPNG_BOUNDING_SET, i);
+    }
+
+    if ((ret = capng_apply(CAPNG_SELECT_BOTH)) < 0) {
+        virCommandError(VIR_ERR_INTERNAL_ERROR,
+                        _("cannot apply process capabilities %d"), ret);
+        return -1;
+    }
+
+    return 0;
+}
 # else
 static int virClearCapabilities(void)
 {
@@ -189,6 +219,11 @@ static int virClearCapabilities(void)
 //             "capabilities");
     return 0;
 }
+
+static int virSetCapabilities(unsigned long long capabilities)
+{
+    return 0;
+}
 # endif
 
 /**
@@ -883,26 +918,23 @@ virCommandClearCaps(virCommandPtr cmd)
     cmd->flags |= VIR_EXEC_CLEAR_CAPS;
 }
 
-#if 0 /* XXX Enable if we have a need for capability management.  */
-
 /**
  * virCommandAllowCap:
  * @cmd: the command to modify
  * @capability: what to allow
  *
- * Re-allow a specific capability
+ * Allow specific capabilities
  */
 void
 virCommandAllowCap(virCommandPtr cmd,
-                   int capability ATTRIBUTE_UNUSED)
+                   int capability)
 {
     if (!cmd || cmd->has_error)
         return;
 
-    /* XXX ? */
+    cmd->capabilities |= (1ULL << capability);
 }
 
-#endif /* 0 */
 
 
 /**
index 1386d57ad5f20b0b7c91946896b48d4875c8a821..07aa0b32e94bc6574cd546d2006fe098fdb31de2 100644 (file)
@@ -60,10 +60,8 @@ void virCommandSetPidFile(virCommandPtr cmd,
 
 void virCommandClearCaps(virCommandPtr cmd);
 
-# if 0
 void virCommandAllowCap(virCommandPtr cmd,
                         int capability);
-# endif
 
 void virCommandDaemonize(virCommandPtr cmd);