Fix: application SIGBUS when starting in parallel with sessiond
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 6 Nov 2013 12:03:10 +0000 (07:03 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 6 Nov 2013 12:03:10 +0000 (07:03 -0500)
There is a race between application startup and sessiond startup, where
there is an intermediate state where applications can SIGBUS if they see
a zero-sized shm, if the shm has been created, but not ftruncated yet.

On the UST side, fix this by ensuring that UST can read the shared
memory file descriptor with a read() system call before they try
accessing it through a memory map (which triggers the SIGBUS if the
access goes beyond the file size).

On the sessiond side, another commit needs to ensure that the shared
memory is writeable by applications as long as its size is 0, which
allow applications to perform ftruncate and extend its size.

Fixes #623

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust/lttng-ust-comm.c

index 202ff1829c6b4746b8629c44df5fb411e9659079..a6e4ba35c9a68f9dae607d196f6c640f751c2989 100644 (file)
@@ -705,6 +705,30 @@ int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
         */
        wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
        if (wait_shm_fd >= 0) {
+               int32_t tmp_read;
+               ssize_t len;
+               size_t bytes_read = 0;
+
+               /*
+                * Try to read the fd. If unable to do so, try opening
+                * it in write mode.
+                */
+               do {
+                       len = read(wait_shm_fd,
+                               &((char *) &tmp_read)[bytes_read],
+                               sizeof(tmp_read) - bytes_read);
+                       if (len > 0) {
+                               bytes_read += len;
+                       }
+               } while ((len < 0 && errno == EINTR)
+                       || (len > 0 && bytes_read < sizeof(tmp_read)));
+               if (bytes_read != sizeof(tmp_read)) {
+                       ret = close(wait_shm_fd);
+                       if (ret) {
+                               ERR("close wait_shm_fd");
+                       }
+                       goto open_write;
+               }
                goto end;
        } else if (wait_shm_fd < 0 && errno != ENOENT) {
                /*
@@ -715,9 +739,11 @@ int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
                ERR("Error opening shm %s", sock_info->wait_shm_path);
                goto end;
        }
+
+open_write:
        /*
-        * If the open failed because the file did not exist, try
-        * creating it ourself.
+        * If the open failed because the file did not exist, or because
+        * the file was not truncated yet, try creating it ourself.
         */
        URCU_TLS(lttng_ust_nest_count)++;
        pid = fork();
This page took 0.027071 seconds and 4 git commands to generate.