Cleaned up init code.
authorJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Tue, 1 Oct 2013 23:56:06 +0000 (02:56 +0300)
committerJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Mon, 14 Oct 2013 10:00:02 +0000 (13:00 +0300)
There was lot of clutter and misleading comments in the code
and some minor coding convention issues. This patch cleans up
init.c for the most part.
(cherry picked from commit c59864f9d4a67dcf76ba01d9c4db2b12fada6339)

libsmack/init.c
libsmack/libsmack.c

index 1711904..4ea47a6 100644 (file)
 #include <stdint.h>
 #include <limits.h>
 
-/*
- *  * smackfs magic number
- *   */
-#define SMACK_MAGIC     0x43415d53 /* "SMAC" */
-
-/* smack file system type */
-#define SMACKFS "smackfs"
+#define SMACK_MAGIC    0x43415d53 /* "SMAC" */
+#define SMACKFS                "smackfs"
+#define SMACKFSMNT     "/sys/fs/smackfs/"
+#define OLDSMACKFSMNT  "/smack"
 
-#define SMACKFSMNT "/sys/fs/smackfs/"
-#define OLDSMACKFSMNT "/smack"
+char *smackfs_mnt = NULL;
 
-char *smack_mnt = NULL;
-
-void set_smackmnt(const char *mnt)
-{
-       smack_mnt = strdup(mnt);
-}
-
-/* Verify the mount point for smack file system has a smackfs.
- * If the file system:
- * Exist,
- * Is mounted with an smack file system,
- * The file system is read/write
- * then set this as the default file system.
- */
-static int verify_smackmnt(const char *mnt)
+static int verify_smackfs_mnt(const char *mnt)
 {
        struct statfs sfbuf;
        int rc;
@@ -71,9 +53,8 @@ static int verify_smackmnt(const char *mnt)
                        struct statvfs vfsbuf;
                        rc = statvfs(mnt, &vfsbuf);
                        if (rc == 0) {
-                               if (!(vfsbuf.f_flag & ST_RDONLY)) {
-                                       set_smackmnt(mnt);
-                               }
+                               if (!(vfsbuf.f_flag & ST_RDONLY))
+                                       smackfs_mnt = strdup(mnt);
                                return 0;
                        }
                }
@@ -82,7 +63,7 @@ static int verify_smackmnt(const char *mnt)
        return -1;
 }
 
-int smackfs_exists(void)
+static int smackfs_exists(void)
 {
        int exists = 0;
        FILE *fp = NULL;
@@ -90,9 +71,12 @@ int smackfs_exists(void)
        size_t len;
        ssize_t num;
 
+       /* Fail as SmackFS would exist since we are checking mounts after
+        * this.
+        */
        fp = fopen("/proc/filesystems", "r");
        if (!fp)
-               return 1; /* Fail as if it exists */
+               return 1;
 
        __fsetlocking(fp, FSETLOCKING_BYCALLER);
 
@@ -117,21 +101,18 @@ static void init_smackmnt(void)
        size_t len;
        ssize_t num;
 
-       if (smack_mnt)
+       if (smackfs_mnt)
                return;
 
-       if (verify_smackmnt(SMACKFSMNT) == 0) 
+       if (verify_smackfs_mnt(SMACKFSMNT) == 0) 
                return;
 
-       if (verify_smackmnt(OLDSMACKFSMNT) == 0) 
+       if (verify_smackfs_mnt(OLDSMACKFSMNT) == 0) 
                return;
 
-       /* Drop back to detecting it the long way. */
        if (!smackfs_exists())
                goto out;
 
-       /* At this point, the usual spot doesn't have an smackfs so
-        * we look around for it */
        fp = fopen("/proc/mounts", "r");
        if (!fp)
                goto out;
@@ -154,11 +135,10 @@ static void init_smackmnt(void)
                }
        }
 
-       /* If we found something, dup it */
        if (num > 0)
-               verify_smackmnt(p);
+               verify_smackfs_mnt(p);
 
-      out:
+out:
        free(buf);
        if (fp)
                fclose(fp);
@@ -167,8 +147,8 @@ static void init_smackmnt(void)
 
 void fini_smackmnt(void)
 {
-       free(smack_mnt);
-       smack_mnt = NULL;
+       free(smackfs_mnt);
+       smackfs_mnt = NULL;
 }
 
 static void init_lib(void) __attribute__ ((constructor));
index d98f233..d2e4b85 100644 (file)
@@ -51,7 +51,7 @@
 #define READ_BUF_SIZE LOAD_LEN + 1
 #define SELF_LABEL_FILE "/proc/self/attr/current"
 
-extern char *smack_mnt;
+extern char *smackfs_mnt;
 
 struct smack_rule {
        char subject[SMACK_LABEL_LEN + 1];
@@ -287,18 +287,18 @@ int smack_have_access(const char *subject, const char *object,
        int access2 = 1;
        char path[PATH_MAX];
 
-       if (!smack_mnt) {
+       if (!smackfs_mnt) {
                errno = EFAULT;
                return -1; 
        }
        
-       snprintf(path, sizeof path, "%s/access2", smack_mnt);
+       snprintf(path, sizeof path, "%s/access2", smackfs_mnt);
        fd = open(path, O_RDWR);
        if (fd < 0) {
                if (errno != ENOENT)
                        return -1;
                
-               snprintf(path, sizeof path, "%s/access", smack_mnt);
+               snprintf(path, sizeof path, "%s/access", smackfs_mnt);
                fd = open(path, O_RDWR);
                if (fd < 0)
                        return -1;
@@ -369,12 +369,12 @@ int smack_cipso_apply(struct smack_cipso *cipso)
        char path[PATH_MAX];
        int offset=0;
 
-       if (!smack_mnt) {
+       if (!smackfs_mnt) {
                errno = EFAULT;
                return -1;
        }
 
-       snprintf(path, sizeof path, "%s/cipso2", smack_mnt);
+       snprintf(path, sizeof path, "%s/cipso2", smackfs_mnt);
        fd = open(path, O_WRONLY);
        if (fd < 0)
                return -1;
@@ -492,7 +492,7 @@ err_out:
 
 const char *smack_smackfs_path(void)
 {
-       return smack_mnt;
+       return smackfs_mnt;
 }
 
 ssize_t smack_new_label_from_self(char **label)
@@ -606,7 +606,7 @@ int smack_revoke_subject(const char *subject)
        if (len > SMACK_LABEL_LEN)
                return -1;
 
-       snprintf(path, sizeof path, "%s/revoke-subject", smack_mnt);
+       snprintf(path, sizeof path, "%s/revoke-subject", smackfs_mnt);
        fd = open(path, O_WRONLY);
        if (fd < 0)
                return -1;
@@ -628,18 +628,18 @@ static int accesses_apply(struct smack_accesses *handle, int clear)
        int load2 = 1;
        char path[PATH_MAX];
 
-       if (!smack_mnt) {
+       if (!smackfs_mnt) {
                errno = EFAULT;
                return -1; 
        }
        
-       snprintf(path, sizeof path, "%s/load2", smack_mnt);
+       snprintf(path, sizeof path, "%s/load2", smackfs_mnt);
        load_fd = open(path, O_WRONLY);
        if (load_fd < 0) {
                if (errno != ENOENT)
                        return -1;
                /* fallback */
-               snprintf(path, sizeof path, "%s/load", smack_mnt);
+               snprintf(path, sizeof path, "%s/load", smackfs_mnt);
                load_fd = open(path, O_WRONLY);
                /* Try to continue if the file doesn't exist, we might not need it. */
                if (load_fd < 0 && errno != ENOENT)
@@ -647,7 +647,7 @@ static int accesses_apply(struct smack_accesses *handle, int clear)
                load2 = 0;
        }
 
-       snprintf(path, sizeof path, "%s/change-rule", smack_mnt);
+       snprintf(path, sizeof path, "%s/change-rule", smackfs_mnt);
        change_fd = open(path, O_WRONLY);
        /* Try to continue if the file doesn't exist, we might not need it. */
        if (change_fd < 0 && errno != ENOENT) {