Backport: relayd: use the fd-tracker to track stream_fd fds
[lttng-tools.git] / src / common / utils.c
index d8a78091a4076fd90bb9d9956286dd1dfb9b8f2b..5ce597f5eaa59528e7df8fe38b47cf4159de1a7d 100644 (file)
@@ -712,7 +712,8 @@ int utils_mkdir_recursive(const char *path, mode_t mode, int uid, int gid)
  *
  * Return 0 on success or else a negative value.
  */
-static int utils_stream_file_name(char *path,
+LTTNG_HIDDEN
+int utils_stream_file_name(char *path,
                const char *path_name, const char *file_name,
                uint64_t size, uint64_t count,
                const char *suffix)
@@ -831,6 +832,36 @@ error:
        return ret;
 }
 
+LTTNG_HIDDEN
+void utils_stream_file_rotation_get_new_count(uint64_t count,
+               uint64_t *new_count, bool *should_unlink)
+{
+       if (count > 0) {
+               /*
+                * In tracefile rotation, for the relay daemon we need
+                * to unlink the old file if present, because it may
+                * still be open in reading by the live thread, and we
+                * need to ensure that we do not overwrite the content
+                * between get_index and get_packet. Since we have no
+                * way to verify integrity of the data content compared
+                * to the associated index, we need to ensure the reader
+                * has exclusive access to the file content, and that
+                * the open of the data file is performed in get_index.
+                * Unlinking the old file rather than overwriting it
+                * achieves this.
+                */
+               if (new_count) {
+                       *new_count = (*new_count + 1) % count;
+               }
+               *should_unlink = true;
+       } else {
+               if (new_count) {
+                       (*new_count)++;
+               }
+               *should_unlink = false;
+       }
+}
+
 /*
  * Change the output tracefile according to the given size and count The
  * new_count pointer is set during this operation.
@@ -846,9 +877,13 @@ int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
                int *stream_fd)
 {
        int ret;
+       bool should_unlink;
 
        assert(stream_fd);
 
+       utils_stream_file_rotation_get_new_count(count, new_count,
+                       &should_unlink);
+
        ret = close(out_fd);
        if (ret < 0) {
                PERROR("Closing tracefile");
@@ -856,32 +891,12 @@ int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
        }
        *stream_fd = -1;
 
-       if (count > 0) {
-               /*
-                * In tracefile rotation, for the relay daemon we need
-                * to unlink the old file if present, because it may
-                * still be open in reading by the live thread, and we
-                * need to ensure that we do not overwrite the content
-                * between get_index and get_packet. Since we have no
-                * way to verify integrity of the data content compared
-                * to the associated index, we need to ensure the reader
-                * has exclusive access to the file content, and that
-                * the open of the data file is performed in get_index.
-                * Unlinking the old file rather than overwriting it
-                * achieves this.
-                */
-               if (new_count) {
-                       *new_count = (*new_count + 1) % count;
-               }
+       if (should_unlink) {
                ret = utils_unlink_stream_file(path_name, file_name, size,
                                new_count ? *new_count : 0, uid, gid, 0);
                if (ret < 0 && errno != ENOENT) {
                        goto error;
                }
-       } else {
-               if (new_count) {
-                       (*new_count)++;
-               }
        }
 
        ret = utils_create_stream_file(path_name, file_name, size,
This page took 0.0277269999999999 seconds and 5 git commands to generate.