Added function smack_label_from_path()
authorJarkko Sakkinen <jarkko.sakkinen@intel.com>
Tue, 21 May 2013 03:24:46 +0000 (20:24 -0700)
committerJarkko Sakkinen <jarkko.sakkinen@intel.com>
Tue, 21 May 2013 03:24:46 +0000 (20:24 -0700)
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@intel.com>
libsmack/libsmack.c
libsmack/sys/smack.h

index f023b94..0e9da03 100644 (file)
@@ -540,6 +540,31 @@ int smack_new_label_from_socket(int fd, char **label)
        return 0;
 }
 
+int smack_new_label_from_path(const char *path, const char *xattr,
+                             const char **label)
+{
+       char *result;
+       ssize_t ret = 0;
+
+       ret = getxattr(path, xattr, NULL, 0);
+       if (ret < 0 && errno != ERANGE)
+               return -1;
+
+       result = calloc(ret + 1, 1);
+       if (result == NULL)
+               return -1;
+
+       ret = getxattr(path, xattr, result, ret);
+       if (ret < 0) {
+               free(result);
+               return -1;
+       }
+
+       *label = result;
+       return 0;
+}
+
+
 int smack_set_label_for_self(const char *label)
 {
        int len;
index 3bb73f7..73392f7 100644 (file)
@@ -200,6 +200,18 @@ int smack_new_label_from_self(char **label);
 int smack_new_label_from_socket(int fd, char **label);
 
 /*!
+  * Get the label that is contained in an extended attribute.
+  * Caller is responsible of freeing the returned label.
+  *
+  * @param path path of the file
+  * @param xattr extended attribute containing the label
+  * @param label returned label
+  * @return 0 on success and negative value on failure.
+  */
+int smack_new_label_from_path(const char *path, const char *xattr,
+                             const char **label);
+
+/*!
  * Set the label associated with the callers process.
  * Caller must be run by privileged user to succeed.
  *