Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / lib / lttng-ctl / filter / memstream.h
CommitLineData
53a80697 1/*
ab5be9fa 2 * Copyright 2012 (C) Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
53a80697 3 *
ab5be9fa 4 * SPDX-License-Identifier: MIT
53a80697 5 *
53a80697
MD
6 */
7
ab5be9fa
MJ
8#ifndef _LTTNG_CTL_MEMSTREAM_H
9#define _LTTNG_CTL_MEMSTREAM_H
10
53a80697
MD
11#ifdef LTTNG_HAVE_FMEMOPEN
12#include <stdio.h>
13
14static inline
15FILE *lttng_fmemopen(void *buf, size_t size, const char *mode)
16{
17 return fmemopen(buf, size, mode);
18}
19
20#else /* LTTNG_HAVE_FMEMOPEN */
21
22#include <stdlib.h>
23#include <stdio.h>
24
25/*
26 * Fallback for systems which don't have fmemopen. Copy buffer to a
27 * temporary file, and use that file as FILE * input.
28 */
29static inline
30FILE *lttng_fmemopen(void *buf, size_t size, const char *mode)
31{
32 char tmpname[PATH_MAX];
33 size_t len;
34 FILE *fp;
35 int ret;
36
37 /*
38 * Support reading only.
39 */
40 if (strcmp(mode, "rb") != 0) {
41 return NULL;
42 }
43 strncpy(tmpname, "/tmp/lttng-tmp-XXXXXX", PATH_MAX);
44 ret = mkstemp(tmpname);
45 if (ret < 0) {
46 return NULL;
47 }
48 /*
49 * We need to write to the file.
50 */
51 fp = fdopen(ret, "w+");
52 if (!fp) {
53 goto error_unlink;
54 }
55 /* Copy the entire buffer to the file */
56 len = fwrite(buf, sizeof(char), size, fp);
57 if (len != size) {
58 goto error_close;
59 }
60 ret = fseek(fp, 0L, SEEK_SET);
61 if (ret < 0) {
6f04ed72 62 PERROR("fseek");
53a80697
MD
63 goto error_close;
64 }
65 /* We keep the handle open, but can unlink the file on the VFS. */
66 ret = unlink(tmpname);
67 if (ret < 0) {
6f04ed72 68 PERROR("unlink");
53a80697
MD
69 }
70 return fp;
71
72error_close:
73 ret = fclose(fp);
74 if (ret < 0) {
6f04ed72 75 PERROR("close");
53a80697
MD
76 }
77error_unlink:
78 ret = unlink(tmpname);
79 if (ret < 0) {
6f04ed72 80 PERROR("unlink");
53a80697
MD
81 }
82 return NULL;
83}
84
85#endif /* LTTNG_HAVE_FMEMOPEN */
86
53a80697 87#endif /* _LTTNG_CTL_MEMSTREAM_H */
This page took 0.064756 seconds and 5 git commands to generate.