chiark / gitweb /
unit: automatically connect to syslog when it becomes available
[elogind.git] / util.c
diff --git a/util.c b/util.c
index 7306ddde34c4ee26db5cac1389f5647cfb05d409..4ae57bbd65857b684a1136cbe2bef0a8b2f24d80 100644 (file)
--- a/util.c
+++ b/util.c
@@ -32,6 +32,7 @@
 #include <linux/sched.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <fcntl.h>
 
 #include "macro.h"
 #include "util.h"
@@ -1058,16 +1059,16 @@ bool path_startswith(const char *path, const char *prefix) {
         }
 }
 
-char *ascii_strlower(char *path) {
+char *ascii_strlower(char *t) {
         char *p;
 
-        assert(path);
+        assert(t);
 
-        for (p = path; *p; p++)
+        for (p = t; *p; p++)
                 if (*p >= 'A' && *p <= 'Z')
                         *p = *p - 'A' + 'a';
 
-        return p;
+        return t;
 }
 
 bool ignore_file(const char *filename) {
@@ -1084,6 +1085,44 @@ bool ignore_file(const char *filename) {
                 endswith(filename, ".swp");
 }
 
+int fd_nonblock(int fd, bool nonblock) {
+        int flags;
+
+        assert(fd >= 0);
+
+        if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
+                return -errno;
+
+        if (nonblock)
+                flags |= O_NONBLOCK;
+        else
+                flags &= ~O_NONBLOCK;
+
+        if (fcntl(fd, F_SETFL, flags) < 0)
+                return -errno;
+
+        return 0;
+}
+
+int fd_cloexec(int fd, bool cloexec) {
+        int flags;
+
+        assert(fd >= 0);
+
+        if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
+                return -errno;
+
+        if (cloexec)
+                flags |= FD_CLOEXEC;
+        else
+                flags &= ~FD_CLOEXEC;
+
+        if (fcntl(fd, F_SETFD, flags) < 0)
+                return -errno;
+
+        return 0;
+}
+
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",