eal: add directory for runtime data
[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 /* sets up platform-specific runtime data dir */
29 int
30 eal_create_runtime_dir(void);
31
32 /* returns runtime dir */
33 const char *
34 eal_get_runtime_dir(void);
35
36 static inline const char *
37 eal_runtime_config_path(void)
38 {
39         static char buffer[PATH_MAX]; /* static so auto-zeroed */
40         const char *directory = default_config_dir;
41         const char *home_dir = getenv("HOME");
42
43         if (getuid() != 0 && home_dir != NULL)
44                 directory = home_dir;
45         snprintf(buffer, sizeof(buffer) - 1, RUNTIME_CONFIG_FMT, directory,
46                         internal_config.hugefile_prefix);
47         return buffer;
48 }
49
50 /** Path of primary/secondary communication unix socket file. */
51 #define MP_SOCKET_PATH_FMT "%s/.%s_unix"
52 static inline const char *
53 eal_mp_socket_path(void)
54 {
55         static char buffer[PATH_MAX]; /* static so auto-zeroed */
56         const char *directory = default_config_dir;
57         const char *home_dir = getenv("HOME");
58
59         if (getuid() != 0 && home_dir != NULL)
60                 directory = home_dir;
61         snprintf(buffer, sizeof(buffer) - 1, MP_SOCKET_PATH_FMT,
62                  directory, internal_config.hugefile_prefix);
63
64         return buffer;
65 }
66
67 static inline const char *
68 eal_get_fbarray_path(char *buffer, size_t buflen, const char *name) {
69         const char *directory = "/tmp";
70         const char *home_dir = getenv("HOME");
71
72         if (getuid() != 0 && home_dir != NULL)
73                 directory = home_dir;
74         snprintf(buffer, buflen - 1, FBARRAY_FMT, directory,
75                         internal_config.hugefile_prefix, name);
76         return buffer;
77 }
78
79 /** Path of hugepage info file. */
80 #define HUGEPAGE_INFO_FMT "%s/.%s_hugepage_info"
81
82 static inline const char *
83 eal_hugepage_info_path(void)
84 {
85         static char buffer[PATH_MAX]; /* static so auto-zeroed */
86         const char *directory = default_config_dir;
87         const char *home_dir = getenv("HOME");
88
89         if (getuid() != 0 && home_dir != NULL)
90                 directory = home_dir;
91         snprintf(buffer, sizeof(buffer) - 1, HUGEPAGE_INFO_FMT, directory,
92                         internal_config.hugefile_prefix);
93         return buffer;
94 }
95
96 /** Path of hugepage info file. */
97 #define HUGEPAGE_FILE_FMT "%s/.%s_hugepage_file"
98
99 static inline const char *
100 eal_hugepage_data_path(void)
101 {
102         static char buffer[PATH_MAX]; /* static so auto-zeroed */
103         const char *directory = default_config_dir;
104         const char *home_dir = getenv("HOME");
105
106         if (getuid() != 0 && home_dir != NULL)
107                 directory = home_dir;
108         snprintf(buffer, sizeof(buffer) - 1, HUGEPAGE_FILE_FMT, directory,
109                         internal_config.hugefile_prefix);
110         return buffer;
111 }
112
113 /** String format for hugepage map files. */
114 #define HUGEFILE_FMT "%s/%smap_%d"
115 static inline const char *
116 eal_get_hugefile_path(char *buffer, size_t buflen, const char *hugedir, int f_id)
117 {
118         snprintf(buffer, buflen, HUGEFILE_FMT, hugedir,
119                         internal_config.hugefile_prefix, f_id);
120         buffer[buflen - 1] = '\0';
121         return buffer;
122 }
123
124 /** String format for hugepage map lock files. */
125 #define HUGEFILE_LOCK_FMT "%s/.%smap_%d.lock"
126
127 static inline const char *
128 eal_get_hugefile_lock_path(char *buffer, size_t buflen, int f_id)
129 {
130         const char *directory = default_config_dir;
131         const char *home_dir = getenv("HOME");
132
133         if (getuid() != 0 && home_dir != NULL)
134                 directory = home_dir;
135         snprintf(buffer, buflen - 1, HUGEFILE_LOCK_FMT, directory,
136                         internal_config.hugefile_prefix, f_id);
137         buffer[buflen - 1] = '\0';
138         return buffer;
139 }
140
141 /** define the default filename prefix for the %s values above */
142 #define HUGEFILE_PREFIX_DEFAULT "rte"
143
144 /** Function to read a single numeric value from a file on the filesystem.
145  * Used to read information from files on /sys */
146 int eal_parse_sysfs_value(const char *filename, unsigned long *val);
147
148 #endif /* EAL_FILESYSTEM_H */