]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/smack-setup: add helper function for openat+fdopen
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 4 Apr 2019 09:27:21 +0000 (11:27 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 12 Apr 2019 09:44:57 +0000 (11:44 +0200)
Unlocked operations are used in all three places. I don't see why just one was
special.

This also improves logging, since we don't just log the final component of the
path, but the full name.

src/core/smack-setup.c

index cd7fb01416484e9f09bb2224a8f94f271b5adbfd..931e8a52270b8391058c06d354af99b4a1b77b40 100644 (file)
 
 #if ENABLE_SMACK
 
-static int write_access2_rules(const char* srcdir) {
+static int fdopen_unlocked_at(int dfd, const char *dir, const char *name, int *status, FILE **ret_file) {
+        int fd, r;
+        FILE *f;
+
+        fd = openat(dfd, name, O_RDONLY|O_CLOEXEC);
+        if (fd < 0) {
+                if (*status == 0)
+                        *status = -errno;
+
+                return log_warning_errno(errno, "Failed to open \"%s/%s\": %m", dir, name);
+        }
+
+        r = fdopen_unlocked(fd, "r", &f);
+        if (r < 0) {
+                if (*status == 0)
+                        *status = r;
+
+                safe_close(fd);
+                return log_error_errno(r, "Failed to open \"%s/%s\": %m", dir, name);
+        }
+
+        *ret_file = f;
+        return 0;
+}
+
+static int write_access2_rules(const char *srcdir) {
         _cleanup_close_ int load2_fd = -1, change_fd = -1;
         _cleanup_closedir_ DIR *dir = NULL;
         struct dirent *entry;
-        int dfd = -1;
-        int r = 0;
+        int dfd = -1, r = 0;
 
         load2_fd = open("/sys/fs/smackfs/load2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
         if (load2_fd < 0)  {
@@ -59,28 +83,13 @@ static int write_access2_rules(const char* srcdir) {
         assert(dfd >= 0);
 
         FOREACH_DIRENT(entry, dir, return 0) {
-                int fd;
                 _cleanup_fclose_ FILE *policy = NULL;
 
                 if (!dirent_is_file(entry))
                         continue;
 
-                fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
-                if (fd < 0) {
-                        if (r == 0)
-                                r = -errno;
-                        log_warning_errno(errno, "Failed to open '%s': %m", entry->d_name);
+                if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
                         continue;
-                }
-
-                policy = fdopen(fd, "r");
-                if (!policy) {
-                        if (r == 0)
-                                r = -errno;
-                        safe_close(fd);
-                        log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
-                        continue;
-                }
 
                 /* load2 write rules in the kernel require a line buffered stream */
                 for (;;) {
@@ -115,12 +124,11 @@ static int write_access2_rules(const char* srcdir) {
         return r;
 }
 
-static int write_cipso2_rules(const charsrcdir) {
+static int write_cipso2_rules(const char *srcdir) {
         _cleanup_close_ int cipso2_fd = -1;
         _cleanup_closedir_ DIR *dir = NULL;
         struct dirent *entry;
-        int dfd = -1;
-        int r = 0;
+        int dfd = -1, r = 0;
 
         cipso2_fd = open("/sys/fs/smackfs/cipso2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
         if (cipso2_fd < 0)  {
@@ -141,28 +149,13 @@ static int write_cipso2_rules(const char* srcdir) {
         assert(dfd >= 0);
 
         FOREACH_DIRENT(entry, dir, return 0) {
-                int fd;
                 _cleanup_fclose_ FILE *policy = NULL;
 
                 if (!dirent_is_file(entry))
                         continue;
 
-                fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
-                if (fd < 0) {
-                        if (r == 0)
-                                r = -errno;
-                        log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
-                        continue;
-                }
-
-                policy = fdopen(fd, "r");
-                if (!policy) {
-                        if (r == 0)
-                                r = -errno;
-                        safe_close(fd);
-                        log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
+                if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
                         continue;
-                }
 
                 /* cipso2 write rules in the kernel require a line buffered stream */
                 for (;;) {
@@ -191,12 +184,11 @@ static int write_cipso2_rules(const char* srcdir) {
         return r;
 }
 
-static int write_netlabel_rules(const charsrcdir) {
+static int write_netlabel_rules(const char *srcdir) {
         _cleanup_fclose_ FILE *dst = NULL;
         _cleanup_closedir_ DIR *dir = NULL;
         struct dirent *entry;
-        int dfd = -1;
-        int r = 0;
+        int dfd = -1, r = 0;
 
         dst = fopen("/sys/fs/smackfs/netlabel", "we");
         if (!dst)  {
@@ -217,27 +209,10 @@ static int write_netlabel_rules(const char* srcdir) {
         assert(dfd >= 0);
 
         FOREACH_DIRENT(entry, dir, return 0) {
-                int fd;
                 _cleanup_fclose_ FILE *policy = NULL;
 
-                fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
-                if (fd < 0) {
-                        if (r == 0)
-                                r = -errno;
-                        log_warning_errno(errno, "Failed to open %s: %m", entry->d_name);
+                if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0)
                         continue;
-                }
-
-                policy = fdopen(fd, "r");
-                if (!policy) {
-                        if (r == 0)
-                                r = -errno;
-                        safe_close(fd);
-                        log_error_errno(errno, "Failed to open %s: %m", entry->d_name);
-                        continue;
-                }
-
-                (void) __fsetlocking(policy, FSETLOCKING_BYCALLER);
 
                 /* load2 write rules in the kernel require a line buffered stream */
                 for (;;) {