Build fix: implicit declaration of function 'PERROR' on Solaris
[lttng-tools.git] / src / lib / lttng-ctl / filter / memstream.h
CommitLineData
53a80697
MD
1#ifndef _LTTNG_CTL_MEMSTREAM_H
2#define _LTTNG_CTL_MEMSTREAM_H
3
4/*
5 * src/lib/lttng-ctl/memstream.h
6 *
7 * Copyright 2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * memstream compatibility layer.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
53a80697
MD
22#ifdef LTTNG_HAVE_FMEMOPEN
23#include <stdio.h>
24
25static inline
26FILE *lttng_fmemopen(void *buf, size_t size, const char *mode)
27{
28 return fmemopen(buf, size, mode);
29}
30
31#else /* LTTNG_HAVE_FMEMOPEN */
32
33#include <stdlib.h>
34#include <stdio.h>
9cd8e997 35#include <common/error.h>
53a80697
MD
36
37/*
38 * Fallback for systems which don't have fmemopen. Copy buffer to a
39 * temporary file, and use that file as FILE * input.
40 */
41static inline
42FILE *lttng_fmemopen(void *buf, size_t size, const char *mode)
43{
44 char tmpname[PATH_MAX];
45 size_t len;
46 FILE *fp;
47 int ret;
48
49 /*
50 * Support reading only.
51 */
52 if (strcmp(mode, "rb") != 0) {
53 return NULL;
54 }
55 strncpy(tmpname, "/tmp/lttng-tmp-XXXXXX", PATH_MAX);
56 ret = mkstemp(tmpname);
57 if (ret < 0) {
58 return NULL;
59 }
60 /*
61 * We need to write to the file.
62 */
63 fp = fdopen(ret, "w+");
64 if (!fp) {
65 goto error_unlink;
66 }
67 /* Copy the entire buffer to the file */
68 len = fwrite(buf, sizeof(char), size, fp);
69 if (len != size) {
70 goto error_close;
71 }
72 ret = fseek(fp, 0L, SEEK_SET);
73 if (ret < 0) {
6f04ed72 74 PERROR("fseek");
53a80697
MD
75 goto error_close;
76 }
77 /* We keep the handle open, but can unlink the file on the VFS. */
78 ret = unlink(tmpname);
79 if (ret < 0) {
6f04ed72 80 PERROR("unlink");
53a80697
MD
81 }
82 return fp;
83
84error_close:
85 ret = fclose(fp);
86 if (ret < 0) {
6f04ed72 87 PERROR("close");
53a80697
MD
88 }
89error_unlink:
90 ret = unlink(tmpname);
91 if (ret < 0) {
6f04ed72 92 PERROR("unlink");
53a80697
MD
93 }
94 return NULL;
95}
96
97#endif /* LTTNG_HAVE_FMEMOPEN */
98
53a80697 99#endif /* _LTTNG_CTL_MEMSTREAM_H */
This page took 0.046216 seconds and 4 git commands to generate.