364f38d13d5a7d24fc35d8c5965afb36c9559221
[deb_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
17 #include <stdint.h>
18 #include <limits.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21
22 #include <rte_string_fns.h>
23 #include "eal_internal_cfg.h"
24
25 /* sets up platform-specific runtime data dir */
26 int
27 eal_create_runtime_dir(void);
28
29 /* returns runtime dir */
30 const char *
31 eal_get_runtime_dir(void);
32
33 static inline const char *
34 eal_runtime_config_path(void)
35 {
36         static char buffer[PATH_MAX]; /* static so auto-zeroed */
37         const char *directory = "/var/run";
38         const char *home_dir = getenv("HOME");
39
40         if (getuid() != 0 && home_dir != NULL)
41                 directory = home_dir;
42         snprintf(buffer, sizeof(buffer) - 1, RUNTIME_CONFIG_FMT, directory,
43                         internal_config.hugefile_prefix);
44         return buffer;
45 }
46
47 /** Path of primary/secondary communication unix socket file. */
48 #define MP_SOCKET_FNAME "mp_socket"
49 static inline const char *
50 eal_mp_socket_path(void)
51 {
52         static char buffer[PATH_MAX]; /* static so auto-zeroed */
53
54         snprintf(buffer, sizeof(buffer) - 1, "%s/%s", eal_get_runtime_dir(),
55                         MP_SOCKET_FNAME);
56         return buffer;
57 }
58
59 #define FBARRAY_NAME_FMT "%s/fbarray_%s"
60 static inline const char *
61 eal_get_fbarray_path(char *buffer, size_t buflen, const char *name) {
62         snprintf(buffer, buflen, FBARRAY_NAME_FMT, eal_get_runtime_dir(), name);
63         return buffer;
64 }
65
66 /** Path of hugepage info file. */
67 #define HUGEPAGE_INFO_FNAME "hugepage_info"
68 static inline const char *
69 eal_hugepage_info_path(void)
70 {
71         static char buffer[PATH_MAX]; /* static so auto-zeroed */
72
73         snprintf(buffer, sizeof(buffer) - 1, "%s/%s", eal_get_runtime_dir(),
74                         HUGEPAGE_INFO_FNAME);
75         return buffer;
76 }
77
78 /** Path of hugepage data file. */
79 #define HUGEPAGE_DATA_FNAME "hugepage_data"
80 static inline const char *
81 eal_hugepage_data_path(void)
82 {
83         static char buffer[PATH_MAX]; /* static so auto-zeroed */
84
85         snprintf(buffer, sizeof(buffer) - 1, "%s/%s", eal_get_runtime_dir(),
86                         HUGEPAGE_DATA_FNAME);
87         return buffer;
88 }
89
90 /** String format for hugepage map files. */
91 #define HUGEFILE_FMT "%s/%smap_%d"
92 static inline const char *
93 eal_get_hugefile_path(char *buffer, size_t buflen, const char *hugedir, int f_id)
94 {
95         snprintf(buffer, buflen, HUGEFILE_FMT, hugedir,
96                         internal_config.hugefile_prefix, f_id);
97         buffer[buflen - 1] = '\0';
98         return buffer;
99 }
100
101 /** String format for hugepage map lock files. */
102 #define HUGEFILE_LOCK_FMT "%s/map_%d.lock"
103 static inline const char *
104 eal_get_hugefile_lock_path(char *buffer, size_t buflen, int f_id)
105 {
106         snprintf(buffer, buflen, HUGEFILE_LOCK_FMT, eal_get_runtime_dir(),
107                         f_id);
108         buffer[buflen - 1] = '\0';
109         return buffer;
110 }
111
112 /** define the default filename prefix for the %s values above */
113 #define HUGEFILE_PREFIX_DEFAULT "rte"
114
115 /** Function to read a single numeric value from a file on the filesystem.
116  * Used to read information from files on /sys */
117 int eal_parse_sysfs_value(const char *filename, unsigned long *val);
118
119 #endif /* EAL_FILESYSTEM_H */