X-Git-Url: http://git.lttng.org./?a=blobdiff_plain;f=libringbuffer%2Fshm.c;h=563903877f5eb59a62409ce32662a8bd6f7600bd;hb=a88038977008704e88544601cef0291b138f8387;hp=86e8d9184e3c8a3fb066df69e2808028d790fe74;hpb=7a9c21bd702ae4d54d8c1f623e9837e0248b0643;p=lttng-ust.git diff --git a/libringbuffer/shm.c b/libringbuffer/shm.c index 86e8d918..56390387 100644 --- a/libringbuffer/shm.c +++ b/libringbuffer/shm.c @@ -67,8 +67,18 @@ struct shm_object *shm_object_table_append(struct shm_object_table *table, * We specifically do _not_ use the / at the beginning of the * pathname so that some OS implementations can keep it local to * the process (POSIX leaves this implementation-defined). + * Ignore the shm_unlink errors, because we handle leaks that + * could occur by applications crashing between shm_open and + * shm_unlink by unlinking the shm before every open. Therefore, + * we can only leak one single shm (and only if the application + * crashes between shm_open and the following shm_unlink). */ do { + ret = shm_unlink("ust-shm-tmp"); + if (ret < 0 && errno != ENOENT) { + PERROR("shm_unlink"); + goto error_shm_unlink; + } shmfd = shm_open("ust-shm-tmp", O_CREAT | O_EXCL | O_RDWR, 0700); } while (shmfd < 0 && errno == EEXIST); @@ -77,9 +87,9 @@ struct shm_object *shm_object_table_append(struct shm_object_table *table, goto error_shm_open; } ret = shm_unlink("ust-shm-tmp"); - if (ret) { + if (ret < 0 && errno != ENOENT) { PERROR("shm_unlink"); - goto error_unlink; + goto error_shm_release; } ret = ftruncate(shmfd, memory_map_size); if (ret) { @@ -98,18 +108,19 @@ struct shm_object *shm_object_table_append(struct shm_object_table *table, obj->memory_map = memory_map; obj->memory_map_size = memory_map_size; obj->allocated_len = 0; + obj->index = table->allocated_len++; - table->allocated_len++; return obj; error_mmap: error_ftruncate: -error_unlink: +error_shm_release: ret = close(shmfd); if (ret) { PERROR("close"); assert(0); } +error_shm_unlink: error_shm_open: error_fcntl: for (i = 0; i < 2; i++) { @@ -120,7 +131,6 @@ error_fcntl: } } error_pipe: - free(obj); return NULL; }