Reorganized and cleaned up smack.h/c.
authorJarkko Sakkinen <jarkko.j.sakkinen@gmail.com>
Wed, 2 Nov 2011 21:11:32 +0000 (23:11 +0200)
committerJarkko Sakkinen <jarkko.j.sakkinen@gmail.com>
Wed, 2 Nov 2011 21:11:32 +0000 (23:11 +0200)
src/smack.c
src/smack.h

index e577a0a..3c646a7 100644 (file)
@@ -264,22 +264,24 @@ int smack_have_access(const char *subject, const char *object,
        return buf[0] == '1';
 }
 
-char *smack_get_peer_label(int fd)
+char *smack_get_self_label()
 {
-       char dummy;
-       int ret;
-       socklen_t length = 1;
        char *label;
+       int fd;
+       int ret;
 
-       ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
-       if (ret < 0 && errno != ERANGE)
+       label = calloc(LABEL_LEN + 1, 1);
+       if (label == NULL)
                return NULL;
 
-       label = calloc(length, 1);
-       if (label == NULL)
+       fd = open(SELF_LABEL_FILE, O_RDONLY);
+       if (fd < 0) {
+               free(label);
                return NULL;
+       }
 
-       ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, label, &length);
+       ret = read(fd, label, LABEL_LEN);
+       close(fd);
        if (ret < 0) {
                free(label);
                return NULL;
@@ -288,24 +290,22 @@ char *smack_get_peer_label(int fd)
        return label;
 }
 
-char *smack_get_self_label()
+char *smack_get_peer_label(int fd)
 {
-       char *label;
-       int fd;
+       char dummy;
        int ret;
+       socklen_t length = 1;
+       char *label;
 
-       label = calloc(LABEL_LEN + 1, 1);
-       if (label == NULL)
+       ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
+       if (ret < 0 && errno != ERANGE)
                return NULL;
 
-       fd = open(SELF_LABEL_FILE, O_RDONLY);
-       if (fd < 0) {
-               free(label);
+       label = calloc(length, 1);
+       if (label == NULL)
                return NULL;
-       }
 
-       ret = read(fd, label, LABEL_LEN);
-       close(fd);
+       ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, label, &length);
        if (ret < 0) {
                free(label);
                return NULL;
index 5e097ad..44cf5ed 100644 (file)
@@ -106,20 +106,21 @@ extern int smack_have_access(const char *subject, const char *object,
                             const char *access_type);
 
 /*!
-  * Get the label that is associated with a peer on the other end of an
-  * Unix socket. Caller is responsible of freeing the returned label.
+  * Get the label that is associated with the callers process.
+  * Caller is responsible of freeing the returned label.
   *
-  * @param fd socket file descriptor
-  * @return label on success and NULL of failure.
+  * @return Callers label on success and NULL of failure.
   */
-extern char *smack_get_peer_label(int fd);
+extern char *smack_get_self_label();
 
 /*!
-  * Allow a process to determine its own smack label
+  * Get the label that is associated with a peer on the other end of an
+  * Unix socket. Caller is responsible of freeing the returned label.
   *
-  * @return The label of the process, NULL on error.
+  * @param fd socket file descriptor
+  * @return Peers label on success and NULL of failure.
   */
-extern char *smack_get_self_label();
+extern char *smack_get_peer_label(int fd);
 
 #ifdef __cplusplus
 }