]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/utils.c
utils: add lxc_deslashify
[mirror_lxc.git] / src / lxc / utils.c
index ba20c43df0f82e4c2db83e30f2b4bd02f5a76a5e..9a6ef4b37528f1cb9460164db8d380b4decff15a 100644 (file)
 
 #include "config.h"
 
+#include <assert.h>
+#include <dirent.h>
 #include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
+#include <fcntl.h>
+#include <libgen.h>
 #include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
-#include <sys/vfs.h>
-#include <sys/stat.h>
+#include <unistd.h>
 #include <sys/mman.h>
-#include <sys/param.h>
 #include <sys/mount.h>
-#include <dirent.h>
-#include <fcntl.h>
-#include <libgen.h>
+#include <sys/param.h>
+#include <sys/prctl.h>
+#include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/vfs.h>
 #include <sys/wait.h>
-#include <assert.h>
-#include <sys/prctl.h>
 
-#include "utils.h"
 #include "log.h"
 #include "lxclock.h"
 #include "namespace.h"
+#include "utils.h"
 
 #ifndef PR_SET_MM
 #define PR_SET_MM 35
@@ -90,7 +90,7 @@ extern bool btrfs_try_remove_subvol(const char *path);
 static int _recursive_rmdir(char *dirname, dev_t pdev,
                            const char *exclude, int level, bool onedev)
 {
-       struct dirent dirent, *direntp;
+       struct dirent *direntp;
        DIR *dir;
        int ret, failed=0;
        char pathname[MAXPATHLEN];
@@ -102,7 +102,7 @@ static int _recursive_rmdir(char *dirname, dev_t pdev,
                return -1;
        }
 
-       while (!readdir_r(dir, &dirent, &direntp)) {
+       while ((direntp = readdir(dir))) {
                struct stat mystat;
                int rc;
 
@@ -716,6 +716,24 @@ char **lxc_normalize_path(const char *path)
        return components;
 }
 
+bool lxc_deslashify(char *path)
+{
+       char **parts = NULL, *path2;
+
+       parts = lxc_normalize_path(path);
+       if (!parts)
+               return false;
+
+       path2 = lxc_string_join("/", (const char **) parts, *path == '/');
+       lxc_free_array((void **) parts, free);
+       if (!path2)
+               return false;
+
+       strncpy(path, path2, strlen(path));
+       free(path2);
+       return true;
+}
+
 char *lxc_append_paths(const char *first, const char *second)
 {
        size_t len = strlen(first) + strlen(second) + 1;
@@ -941,7 +959,7 @@ void **lxc_append_null_to_array(void **array, size_t count)
        if (count) {
                temp = realloc(array, (count + 1) * sizeof(*array));
                if (!temp) {
-                       int i;
+                       size_t i;
                        for (i = 0; i < count; i++)
                                free(array[i]);
                        free(array);
@@ -1821,17 +1839,16 @@ void *lxc_strmmap(void *addr, size_t length, int prot, int flags, int fd,
         * underlying file. The pages handed to us are zero filled. */
        tmp = mmap(addr, length + 1, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        if (tmp == MAP_FAILED)
-               goto out;
+               return tmp;
 
        /* Now we establish a fixed-address mapping starting at the address we
         * received from our anonymous mapping and replace all bytes excluding
         * the additional \0-byte with the file. This allows us to use normal
-        * string-handling function. */
+        * string-handling functions. */
        overlap = mmap(tmp, length, prot, MAP_FIXED | flags, fd, offset);
        if (overlap == MAP_FAILED)
-               goto out;
+               munmap(tmp, length + 1);
 
-out:
        return overlap;
 }
 
@@ -1839,3 +1856,41 @@ int lxc_strmunmap(void *addr, size_t length)
 {
        return munmap(addr, length + 1);
 }
+
+/* Check whether a signal is blocked by a process. */
+bool task_blocking_signal(pid_t pid, int signal)
+{
+       bool bret = false;
+       char *line = NULL;
+       long unsigned int sigblk = 0;
+       size_t n = 0;
+       int ret;
+       FILE *f;
+
+       /* The largest integer that can fit into long int is 2^64. This is a
+        * 20-digit number. */
+       size_t len = /* /proc */ 5 + /* /pid-to-str */ 21 + /* /status */ 7 + /* \0 */ 1;
+       char status[len];
+
+       ret = snprintf(status, len, "/proc/%d/status", pid);
+       if (ret < 0 || ret >= len)
+               return bret;
+
+       f = fopen(status, "r");
+       if (!f)
+               return bret;
+
+       while (getline(&line, &n, f) != -1) {
+               if (!strncmp(line, "SigBlk:\t", 8))
+                       if (sscanf(line + 8, "%lx", &sigblk) != 1)
+                               goto out;
+       }
+
+       if (sigblk & signal)
+               bret = true;
+
+out:
+       free(line);
+       fclose(f);
+       return bret;
+}