2 * collectd - contrib/docker/rootfs_prefix/rootfs_prefix.c
3 * Copyright (C) 2016-2018 Marc Fournier
4 * Copyright (C) 2016-2018 Ruben Kerkhof
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
27 * Marc Fournier <marc.fournier at camptocamp.com>
28 * Ruben Kerkhof <ruben at rubenkerkhof.com>
40 #define PREFIX "/rootfs"
43 const char *add_prefix(const char *orig, char *prefixed) {
44 if ((strncmp(orig, "/proc", strlen("/proc")) != 0) &&
45 (strncmp(orig, "/sys", strlen("/sys")) != 0))
48 int status = snprintf(prefixed, BUFSIZE, "%s%s", PREFIX, orig);
50 error(status, errno, "adding '%s' prefix to file path failed: '%s' -> '%s'",
51 PREFIX, orig, prefixed);
53 } else if ((unsigned int)status >= BUFSIZE) {
54 error(status, ENAMETOOLONG,
55 "'%s' got truncated when adding '%s' prefix: '%s'", orig, PREFIX,
59 return (const char *)prefixed;
63 FILE *fopen(const char *path, const char *mode) {
64 char filename[BUFSIZE] = "\0";
66 FILE *(*original_fopen)(const char *, const char *);
67 original_fopen = dlsym(RTLD_NEXT, "fopen");
69 return (*original_fopen)(add_prefix(path, filename), mode);
72 DIR *opendir(const char *name) {
73 char filename[BUFSIZE] = "\0";
75 DIR *(*original_opendir)(const char *);
76 original_opendir = dlsym(RTLD_NEXT, "opendir");
78 return (*original_opendir)(add_prefix(name, filename));
81 int *open(const char *pathname, int flags) {
82 char filename[BUFSIZE] = "\0";
84 int *(*original_open)(const char *, int);
85 original_open = dlsym(RTLD_NEXT, "open");
87 return (*original_open)(add_prefix(pathname, filename), flags);