2 * Copyright 2012 (C) Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * SPDX-License-Identifier: MIT
8 #ifndef _LTTNG_CTL_MEMSTREAM_H
9 #define _LTTNG_CTL_MEMSTREAM_H
11 #ifdef LTTNG_HAVE_FMEMOPEN
14 static inline FILE *lttng_fmemopen(void *buf, size_t size, const char *mode)
16 return fmemopen(buf, size, mode);
19 #else /* LTTNG_HAVE_FMEMOPEN */
21 #include <common/error.hpp>
27 * Fallback for systems which don't have fmemopen. Copy buffer to a
28 * temporary file, and use that file as FILE * input.
30 static inline FILE *lttng_fmemopen(void *buf, size_t size, const char *mode)
32 char tmpname[PATH_MAX];
38 * Support reading only.
40 if (strcmp(mode, "rb") != 0) {
43 strncpy(tmpname, "/tmp/lttng-tmp-XXXXXX", PATH_MAX);
44 ret = mkstemp(tmpname);
49 * We need to write to the file.
51 fp = fdopen(ret, "w+");
55 /* Copy the entire buffer to the file */
56 len = fwrite(buf, sizeof(char), size, fp);
60 ret = fseek(fp, 0L, SEEK_SET);
65 /* We keep the handle open, but can unlink the file on the VFS. */
66 ret = unlink(tmpname);
78 ret = unlink(tmpname);
85 #endif /* LTTNG_HAVE_FMEMOPEN */
87 #endif /* _LTTNG_CTL_MEMSTREAM_H */