ad059ef37ed91d247de89af9f533b7b12195799f
[dpdk.git] / lib / librte_eal / common / eal_filesystem.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 /**
6  * @file
7  * Stores functions and path defines for files and directories
8  * on the filesystem for Linux, that are used by the Linux EAL.
9  */
10
11 #ifndef EAL_FILESYSTEM_H
12 #define EAL_FILESYSTEM_H
13
14 /** Path of rte config file. */
15 #define RUNTIME_CONFIG_FMT "%s/.%s_config"
16 #define FBARRAY_FMT "%s/%s_%s"
17
18 #include <stdint.h>
19 #include <limits.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22
23 #include <rte_string_fns.h>
24 #include "eal_internal_cfg.h"
25
26 static const char *default_config_dir = "/var/run";
27
28 static inline const char *
29 eal_runtime_config_path(void)
30 {
31         static char buffer[PATH_MAX]; /* static so auto-zeroed */
32         const char *directory = default_config_dir;
33         const char *home_dir = getenv("HOME");
34
35         if (getuid() != 0 && home_dir != NULL)
36                 directory = home_dir;
37         snprintf(buffer, sizeof(buffer) - 1, RUNTIME_CONFIG_FMT, directory,
38                         internal_config.hugefile_prefix);
39         return buffer;
40 }
41
42 /** Path of primary/secondary communication unix socket file. */
43 #define MP_SOCKET_PATH_FMT "%s/.%s_unix"
44 static inline const char *
45 eal_mp_socket_path(void)
46 {
47         static char buffer[PATH_MAX]; /* static so auto-zeroed */
48         const char *directory = default_config_dir;
49         const char *home_dir = getenv("HOME");
50
51         if (getuid() != 0 && home_dir != NULL)
52                 directory = home_dir;
53         snprintf(buffer, sizeof(buffer) - 1, MP_SOCKET_PATH_FMT,
54                  directory, internal_config.hugefile_prefix);
55
56         return buffer;
57 }
58
59 static inline const char *
60 eal_get_fbarray_path(char *buffer, size_t buflen, const char *name) {
61         const char *directory = "/tmp";
62         const char *home_dir = getenv("HOME");
63
64         if (getuid() != 0 && home_dir != NULL)
65                 directory = home_dir;
66         snprintf(buffer, buflen - 1, FBARRAY_FMT, directory,
67                         internal_config.hugefile_prefix, name);
68         return buffer;
69 }
70
71 /** Path of hugepage info file. */
72 #define HUGEPAGE_INFO_FMT "%s/.%s_hugepage_info"
73
74 static inline const char *
75 eal_hugepage_info_path(void)
76 {
77         static char buffer[PATH_MAX]; /* static so auto-zeroed */
78         const char *directory = default_config_dir;
79         const char *home_dir = getenv("HOME");
80
81         if (getuid() != 0 && home_dir != NULL)
82                 directory = home_dir;
83         snprintf(buffer, sizeof(buffer) - 1, HUGEPAGE_INFO_FMT, directory,
84                         internal_config.hugefile_prefix);
85         return buffer;
86 }
87
88 /** Path of hugepage info file. */
89 #define HUGEPAGE_FILE_FMT "%s/.%s_hugepage_file"
90
91 static inline const char *
92 eal_hugepage_file_path(void)
93 {
94         static char buffer[PATH_MAX]; /* static so auto-zeroed */
95         const char *directory = default_config_dir;
96         const char *home_dir = getenv("HOME");
97
98         if (getuid() != 0 && home_dir != NULL)
99                 directory = home_dir;
100         snprintf(buffer, sizeof(buffer) - 1, HUGEPAGE_FILE_FMT, directory,
101                         internal_config.hugefile_prefix);
102         return buffer;
103 }
104
105 /** String format for hugepage map files. */
106 #define HUGEFILE_FMT "%s/%smap_%d"
107 #define TEMP_HUGEFILE_FMT "%s/%smap_temp_%d"
108
109 static inline const char *
110 eal_get_hugefile_path(char *buffer, size_t buflen, const char *hugedir, int f_id)
111 {
112         snprintf(buffer, buflen, HUGEFILE_FMT, hugedir,
113                         internal_config.hugefile_prefix, f_id);
114         buffer[buflen - 1] = '\0';
115         return buffer;
116 }
117
118 /** define the default filename prefix for the %s values above */
119 #define HUGEFILE_PREFIX_DEFAULT "rte"
120
121 /** Function to read a single numeric value from a file on the filesystem.
122  * Used to read information from files on /sys */
123 int eal_parse_sysfs_value(const char *filename, unsigned long *val);
124
125 #endif /* EAL_FILESYSTEM_H */