Cleaning error logs for rules applying utilities
authorZofia Abramowska <z.abramowska@samsung.com>
Tue, 6 Aug 2013 13:42:33 +0000 (15:42 +0200)
committerJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Wed, 23 Oct 2013 16:17:03 +0000 (19:17 +0300)
Removed perror logs from main utilities files. Moved error logs
inside /utils/common.c. Errors are logged from I/O operations and
applying rules/cipso from files.
(cherry picked from commit bcaf9018f41809cb93aaccf9e9fb49b59750dc7e)

utils/common.c
utils/smackcipso.c
utils/smackctl.c
utils/smackload.c

index 4700821..5ea22fe 100644 (file)
@@ -55,8 +55,10 @@ int clear(void)
 
        snprintf(path, sizeof path, "%s/load2", smack_mnt);
        fd = open(path, O_RDONLY);
-       if (fd < 0)
+       if (fd < 0) {
+               fprintf(stderr, "open() failed for '%s' : %s\n", path, strerror(errno));
                return -1;
+       }
 
        ret = apply_rules_file(fd, 1);
        close(fd);
@@ -69,18 +71,23 @@ int apply_rules(const char *path, int clear)
        int fd;
        int ret;
 
-       errno = 0;
-       if (stat(path, &sbuf))
+       if (stat(path, &sbuf)) {
+               fprintf(stderr, "stat() failed for '%s' : %s\n", path, strerror(errno));
                return -1;
+       }
 
        if (S_ISDIR(sbuf.st_mode))
                return nftw(path, apply_rules_cb, 1, FTW_PHYS|FTW_ACTIONRETVAL);
 
        fd = open(path, O_RDONLY);
-       if (fd < 0)
+       if (fd < 0) {
+               fprintf(stderr, "open() failed for '%s' : %s\n", path, strerror(errno));
                return -1;
+       }
 
        ret = apply_rules_file(fd, clear);
+       if (ret)
+               fprintf(stderr, "Applying rules failed for '%s'.\n",  path);
        close(fd);
        return ret;
 }
@@ -91,18 +98,23 @@ int apply_cipso(const char *path)
        int fd;
        int ret;
 
-       errno = 0;
-       if (stat(path, &sbuf))
+       if (stat(path, &sbuf)) {
+               fprintf(stderr, "stat() failed for '%s' : %s\n", path, strerror(errno));
                return -1;
+       }
 
        if (S_ISDIR(sbuf.st_mode))
                return nftw(path, apply_cipso_cb, 1, FTW_PHYS|FTW_ACTIONRETVAL);
 
        fd = open(path, O_RDONLY);
-       if (fd < 0)
+       if (fd < 0) {
+               fprintf(stderr, "open() failed for '%s' : %s\n", path, strerror(errno));
                return -1;
+       }
 
        ret = apply_cipso_file(fd);
+       if (ret)
+               fprintf(stderr, "Applying rules failed for '%s'.\n",  path);
        close(fd);
        return ret;
 }
@@ -164,10 +176,14 @@ static int apply_rules_cb(const char *fpath, const struct stat *sb, int typeflag
                return FTW_STOP;
 
        fd = open(fpath, O_RDONLY);
-       if (fd < 0)
+       if (fd < 0) {
+               fprintf(stderr, "open() failed for '%s' : %s\n", fpath, strerror(errno));
                return -1;
+       }
 
        ret = apply_rules_file(fd, 0) ? FTW_STOP : FTW_CONTINUE;
+       if (ret == FTW_STOP)
+               fprintf(stderr, "Applying rules failed for '%s'.\n",  fpath);
        close(fd);
        return ret;
 }
@@ -183,10 +199,14 @@ static int apply_cipso_cb(const char *fpath, const struct stat *sb, int typeflag
                return FTW_STOP;
 
        fd = open(fpath, O_RDONLY);
-       if (fd < 0)
+       if (fd < 0) {
+               fprintf(stderr, "open() failed for '%s' : %s\n", fpath, strerror(errno));
                return -1;
+       }
 
        ret = apply_cipso_file(fd) ? FTW_STOP : FTW_CONTINUE;
+       if (ret == FTW_STOP)
+               fprintf(stderr, "Applying rules failed for '%s'.\n",  fpath);
        close(fd);
        return ret;
 }
index 6e500d2..6c548b3 100644 (file)
@@ -36,15 +36,11 @@ int main(int argc, char **argv)
        }
 
        if (argc == 1) {
-               if (apply_cipso_file(STDIN_FILENO)) {
-                       perror("apply_cipso_file");
+               if (apply_cipso_file(STDIN_FILENO))
                        exit(1);
-               }
        } else {
-               if (apply_cipso(argv[1])) {
-                       perror("apply_cipso");
+               if (apply_cipso(argv[1]))
                        exit(1);
-               }
        }
 
        exit(0);
index 69b5b9a..d5a0a79 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "common.h"
 #include <sys/smack.h>
+#include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
@@ -35,10 +36,10 @@ static int apply_all(void)
                return -1;
 
        if (apply_rules(ACCESSES_D_PATH, 0))
-               perror("apply_rules");
+               return -1;
 
        if (apply_cipso(CIPSO_D_PATH))
-               perror("apply_cipso");
+               return -1;
 
        return 0;
 }
@@ -48,24 +49,24 @@ int main(int argc, char **argv)
        const char *tmp = smack_smackfs_path();
        if (argc < 2) {
                fprintf(stderr, "Usage: %s <action>\n", argv[0]);
-               return 1;
+               exit(1);
        }
 
        if (!strcmp(argv[1], "apply")) {
                if (apply_all())
-                       return 1;
+                       exit(1);
        } else if (!strcmp(argv[1], "clear")) {
                if (clear())
-                       return 1;
+                       exit(1);
        } else if (!strcmp(argv[1], "status")) {
                if (smack_smackfs_path())
                        printf("SmackFS is mounted to %s.\n",
                               smack_smackfs_path());
                else
                        printf("SmackFS is not mounted.\n");
-               return 0;
+               exit(0);
        }
 
        fprintf(stderr, "Uknown action: %s\n", argv[1]);
-       return 1;
+       exit(1);
 }
index cc62a66..7d68846 100644 (file)
@@ -50,15 +50,11 @@ int main(int argc, char **argv)
        }
 
        if (optind == argc) {
-               if (apply_rules_file(STDIN_FILENO, clear)) {
-                       perror("apply_rules_file");
+               if (apply_rules_file(STDIN_FILENO, clear))
                        exit(1);
-               }
        } else {
-               if (apply_rules(argv[optind], clear)) {
-                       perror("apply_rules");
+               if (apply_rules(argv[optind], clear))
                        exit(1);
-               }
        }
 
        exit(0);