chiark / gitweb /
when resetting signal handlers, set them to SA_RESTART
[elogind.git] / util.c
diff --git a/util.c b/util.c
index 4774610265f61a39e683a72ed69054d8592cb484..654b93d79c084ceb25b944aa3e62a1112204303a 100644 (file)
--- a/util.c
+++ b/util.c
@@ -97,6 +97,14 @@ int close_nointr(int fd) {
         }
 }
 
+void close_nointr_nofail(int fd) {
+
+        /* like close_nointr() but cannot fail, and guarantees errno
+         * is unchanged */
+
+        assert_se(close_nointr(fd) == 0);
+}
+
 int parse_boolean(const char *v) {
         assert(v);
 
@@ -480,6 +488,7 @@ int reset_all_signal_handlers(void) {
 
                 zero(sa);
                 sa.sa_handler = SIG_DFL;
+                sa.sa_flags = SA_RESTART;
 
                 /* On Linux the first two RT signals are reserved by
                  * glibc, and sigaction() will return EINVAL for them. */
@@ -490,3 +499,51 @@ int reset_all_signal_handlers(void) {
 
     return 0;
 }
+
+char *strstrip(char *s) {
+        char *e, *l = NULL;
+
+        /* Drops trailing whitespace. Modifies the string in
+         * place. Returns pointer to first non-space character */
+
+        s += strspn(s, WHITESPACE);
+
+        for (e = s; *e; e++)
+                if (!strchr(WHITESPACE, *e))
+                        l = e;
+
+        if (l)
+                *(l+1) = 0;
+        else
+                *s = 0;
+
+        return s;
+
+}
+
+char *file_in_same_dir(const char *path, const char *filename) {
+        char *e, *r;
+        size_t k;
+
+        assert(path);
+        assert(filename);
+
+        /* This removes the last component of path and appends
+         * filename, unless the latter is absolute anyway or the
+         * former isn't */
+
+        if (path_is_absolute(filename))
+                return strdup(filename);
+
+        if (!(e = strrchr(path, '/')))
+                return strdup(filename);
+
+        k = strlen(filename);
+        if (!(r = new(char, e-path+1+k+1)))
+                return NULL;
+
+        memcpy(r, path, e-path+1);
+        memcpy(r+(e-path)+1, filename, k+1);
+
+        return r;
+}