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
15 FILE *lttng_fmemopen(void *buf, size_t size, const char *mode)
17 return fmemopen(buf, size, mode);
20 #else /* LTTNG_HAVE_FMEMOPEN */
24 #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.
31 FILE *lttng_fmemopen(void *buf, size_t size, const char *mode)
33 char tmpname[PATH_MAX];
39 * Support reading only.
41 if (strcmp(mode, "rb") != 0) {
44 strncpy(tmpname, "/tmp/lttng-tmp-XXXXXX", PATH_MAX);
45 ret = mkstemp(tmpname);
50 * We need to write to the file.
52 fp = fdopen(ret, "w+");
56 /* Copy the entire buffer to the file */
57 len = fwrite(buf, sizeof(char), size, fp);
61 ret = fseek(fp, 0L, SEEK_SET);
66 /* We keep the handle open, but can unlink the file on the VFS. */
67 ret = unlink(tmpname);
79 ret = unlink(tmpname);
86 #endif /* LTTNG_HAVE_FMEMOPEN */
88 #endif /* _LTTNG_CTL_MEMSTREAM_H */