summaryrefslogtreecommitdiffstats
path: root/src/libnetdata/paths/paths.c
blob: c68ee805f262ccf44e4190c3e47e9f60a957f586 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// SPDX-License-Identifier: GPL-3.0-or-later

#include "paths.h"

static int is_procfs(const char *path, char **reason) {
#if defined(__APPLE__) || defined(__FreeBSD__)
    (void)path;
    (void)reason;
#else
    struct statfs stat;

    if (statfs(path, &stat) == -1) {
        if (reason)
            *reason = "failed to statfs()";
        return -1;
    }

#if defined PROC_SUPER_MAGIC
    if (stat.f_type != PROC_SUPER_MAGIC) {
        if (reason)
            *reason = "type is not procfs";
        return -1;
    }
#endif

#endif

    return 0;
}

static int is_sysfs(const char *path, char **reason) {
#if defined(__APPLE__) || defined(__FreeBSD__)
    (void)path;
    (void)reason;
#else
    struct statfs stat;

    if (statfs(path, &stat) == -1) {
        if (reason)
            *reason = "failed to statfs()";
        return -1;
    }

#if defined SYSFS_MAGIC
    if (stat.f_type != SYSFS_MAGIC) {
        if (reason)
            *reason = "type is not sysfs";
        return -1;
    }
#endif

#endif

    return 0;
}

int verify_netdata_host_prefix(bool log_msg) {
    if(!netdata_configured_host_prefix)
        netdata_configured_host_prefix = "";

    if(!*netdata_configured_host_prefix)
        return 0;

    char path[FILENAME_MAX];
    char *reason = "unknown reason";
    errno_clear();

    strncpyz(path, netdata_configured_host_prefix, sizeof(path) - 1);

    struct stat sb;
    if (stat(path, &sb) == -1) {
        reason = "failed to stat()";
        goto failed;
    }

    if((sb.st_mode & S_IFMT) != S_IFDIR) {
        errno = EINVAL;
        reason = "is not a directory";
        goto failed;
    }

    snprintfz(path, sizeof(path), "%s/proc", netdata_configured_host_prefix);
    if(is_procfs(path, &reason) == -1)
        goto failed;

    snprintfz(path, sizeof(path), "%s/sys", netdata_configured_host_prefix);
    if(is_sysfs(path, &reason) == -1)
        goto failed;

    if (netdata_configured_host_prefix && *netdata_configured_host_prefix) {
        if (log_msg)
            netdata_log_info("Using host prefix directory '%s'", netdata_configured_host_prefix);
    }

    return 0;

failed:
    if (log_msg)
        netdata_log_error("Ignoring host prefix '%s': path '%s' %s", netdata_configured_host_prefix, path, reason);

    netdata_configured_host_prefix = "";
    return -1;
}

size_t filename_from_path_entry(char out[FILENAME_MAX], const char *path, const char *entry, const char *extension) {
    if(unlikely(!path || !*path)) path = ".";
    if(unlikely(!entry)) entry = "";

    // skip trailing slashes in path
    size_t len = strlen(path);
    while(len > 0 && path[len - 1] == '/') len--;

    // skip leading slashes in subpath
    while(entry[0] == '/') entry++;

    // if the last character in path is / and (there is a subpath or path is now empty)
    // keep the trailing slash in path and remove the additional slash
    char *slash = "/";
    if(path[len] == '/' && (*entry || len == 0)) {
        slash = "";
        len++;
    }
    else if(!*entry) {
        // there is no entry
        // no need for trailing slash
        slash = "";
    }

    return snprintfz(out, FILENAME_MAX, "%.*s%s%s%s%s", (int)len, path, slash, entry,
                     extension && *extension ? "." : "",
                     extension && *extension ? extension : "");
}

char *filename_from_path_entry_strdupz(const char *path, const char *entry) {
    char filename[FILENAME_MAX];
    filename_from_path_entry(filename, path, entry, NULL);
    return strdupz(filename);
}

bool filename_is_dir(const char *filename, bool create_it) {
    CLEAN_CHAR_P *buffer = NULL;

    size_t max_links = 100;

    bool is_dir = false;
    struct stat st;
    while(max_links && stat(filename, &st) == 0) {
        if ((st.st_mode & S_IFMT) == S_IFDIR)
            is_dir = true;
        else if ((st.st_mode & S_IFMT) == S_IFLNK) {
            max_links--;

            if(!buffer)
                buffer = mallocz(FILENAME_MAX);

            char link_dst[FILENAME_MAX];
            ssize_t l = readlink(filename, link_dst, FILENAME_MAX - 1);
            if (l > 0) {
                link_dst[l] = '\0';
                strncpyz(buffer, link_dst, FILENAME_MAX - 1);
                filename = buffer;
                continue;
            }
        }

        break;
    }

    if(!is_dir && create_it && max_links == 100 && mkdir(filename, 0750) == 0)
        is_dir = true;

    return is_dir;
}

bool path_entry_is_dir(const char *path, const char *entry, bool create_it) {
    char filename[FILENAME_MAX];
    filename_from_path_entry(filename, path, entry, NULL);
    return filename_is_dir(filename, create_it);
}

bool filename_is_file(const char *filename) {
    CLEAN_CHAR_P *buffer = NULL;

    size_t max_links = 100;

    bool is_file = false;
    struct stat st;
    while(max_links && stat(filename, &st) == 0) {
        if((st.st_mode & S_IFMT) == S_IFREG)
            is_file = true;
        else if((st.st_mode & S_IFMT) == S_IFLNK) {
            max_links--;

            if(!buffer)
                buffer = mallocz(FILENAME_MAX);

            char link_dst[FILENAME_MAX];
            ssize_t l = readlink(filename, link_dst, FILENAME_MAX - 1);
            if(l > 0) {
                link_dst[l] = '\0';
                strncpyz(buffer, link_dst, FILENAME_MAX - 1);
                filename = buffer;
                continue;
            }
        }

        break;
    }

    return is_file;
}

bool path_entry_is_file(const char *path, const char *entry) {
    char filename[FILENAME_MAX];
    filename_from_path_entry(filename, path, entry, NULL);
    return filename_is_file(filename);
}

void recursive_config_double_dir_load(const char *user_path, const char *stock_path, const char *entry, int (*callback)(const char *filename, void *data, bool stock_config), void *data, size_t depth) {
    if(depth > 3) {
        netdata_log_error("CONFIG: Max directory depth reached while reading user path '%s', stock path '%s', subpath '%s'", user_path, stock_path,
                          entry);
        return;
    }

    if(!stock_path)
        stock_path = user_path;

    char *udir = filename_from_path_entry_strdupz(user_path, entry);
    char *sdir = filename_from_path_entry_strdupz(stock_path, entry);

    netdata_log_debug(D_HEALTH, "CONFIG traversing user-config directory '%s', stock config directory '%s'", udir, sdir);

    DIR *dir = opendir(udir);
    if (!dir) {
        netdata_log_error("CONFIG cannot open user-config directory '%s'.", udir);
    }
    else {
        struct dirent *de = NULL;
        while((de = readdir(dir))) {
            if(de->d_type == DT_DIR || de->d_type == DT_LNK) {
                if( !de->d_name[0] ||
                    (de->d_name[0] == '.' && de->d_name[1] == '\0') ||
                    (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
                ) {
                    netdata_log_debug(D_HEALTH, "CONFIG ignoring user-config directory '%s/%s'", udir, de->d_name);
                    continue;
                }

                if(path_entry_is_dir(udir, de->d_name, false)) {
                    recursive_config_double_dir_load(udir, sdir, de->d_name, callback, data, depth + 1);
                    continue;
                }
            }

            if(de->d_type == DT_UNKNOWN || de->d_type == DT_REG || de->d_type == DT_LNK) {
                size_t len = strlen(de->d_name);
                if(path_entry_is_file(udir, de->d_name) &&
                    len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
                    char *filename = filename_from_path_entry_strdupz(udir, de->d_name);
                    netdata_log_debug(D_HEALTH, "CONFIG calling callback for user file '%s'", filename);
                    callback(filename, data, false);
                    freez(filename);
                    continue;
                }
            }

            netdata_log_debug(D_HEALTH, "CONFIG ignoring user-config file '%s/%s' of type %d", udir, de->d_name, (int)de->d_type);
        }

        closedir(dir);
    }

    netdata_log_debug(D_HEALTH, "CONFIG traversing stock config directory '%s', user config directory '%s'", sdir, udir);

    dir = opendir(sdir);
    if (!dir) {
        netdata_log_error("CONFIG cannot open stock config directory '%s'.", sdir);
    }
    else {
        if (strcmp(udir, sdir)) {
            struct dirent *de = NULL;
            while((de = readdir(dir))) {
                if(de->d_type == DT_DIR || de->d_type == DT_LNK) {
                    if( !de->d_name[0] ||
                        (de->d_name[0] == '.' && de->d_name[1] == '\0') ||
                        (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
                    ) {
                        netdata_log_debug(D_HEALTH, "CONFIG ignoring stock config directory '%s/%s'", sdir, de->d_name);
                        continue;
                    }

                    if(path_entry_is_dir(sdir, de->d_name, false)) {
                        // we recurse in stock subdirectory, only when there is no corresponding
                        // user subdirectory - to avoid reading the files twice

                        if(!path_entry_is_dir(udir, de->d_name, false))
                            recursive_config_double_dir_load(udir, sdir, de->d_name, callback, data, depth + 1);

                        continue;
                    }
                }

                if(de->d_type == DT_UNKNOWN || de->d_type == DT_REG || de->d_type == DT_LNK) {
                    size_t len = strlen(de->d_name);
                    if(path_entry_is_file(sdir, de->d_name) && !path_entry_is_file(udir, de->d_name) &&
                        len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
                        char *filename = filename_from_path_entry_strdupz(sdir, de->d_name);
                        netdata_log_debug(D_HEALTH, "CONFIG calling callback for stock file '%s'", filename);
                        callback(filename, data, true);
                        freez(filename);
                        continue;
                    }

                }

                netdata_log_debug(D_HEALTH, "CONFIG ignoring stock-config file '%s/%s' of type %d", udir, de->d_name, (int)de->d_type);
            }
        }
        closedir(dir);
    }

    netdata_log_debug(D_HEALTH, "CONFIG done traversing user-config directory '%s', stock config directory '%s'", udir, sdir);

    freez(udir);
    freez(sdir);
}