New upstream version 18.02
[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 static const char *default_config_dir = "/var/run";
26
27 static inline const char *
28 eal_runtime_config_path(void)
29 {
30         static char buffer[PATH_MAX]; /* static so auto-zeroed */
31         const char *directory = default_config_dir;
32         const char *home_dir = getenv("HOME");
33
34         if (getuid() != 0 && home_dir != NULL)
35                 directory = home_dir;
36         snprintf(buffer, sizeof(buffer) - 1, RUNTIME_CONFIG_FMT, directory,
37                         internal_config.hugefile_prefix);
38         return buffer;
39 }
40
41 /** Path of primary/secondary communication unix socket file. */
42 #define MP_SOCKET_PATH_FMT "%s/.%s_unix"
43 static inline const char *
44 eal_mp_socket_path(void)
45 {
46         static char buffer[PATH_MAX]; /* static so auto-zeroed */
47         const char *directory = default_config_dir;
48         const char *home_dir = getenv("HOME");
49
50         if (getuid() != 0 && home_dir != NULL)
51                 directory = home_dir;
52         snprintf(buffer, sizeof(buffer) - 1, MP_SOCKET_PATH_FMT,
53                  directory, internal_config.hugefile_prefix);
54
55         return buffer;
56 }
57
58 /** Path of hugepage info file. */
59 #define HUGEPAGE_INFO_FMT "%s/.%s_hugepage_info"
60
61 static inline const char *
62 eal_hugepage_info_path(void)
63 {
64         static char buffer[PATH_MAX]; /* static so auto-zeroed */
65         const char *directory = default_config_dir;
66         const char *home_dir = getenv("HOME");
67
68         if (getuid() != 0 && home_dir != NULL)
69                 directory = home_dir;
70         snprintf(buffer, sizeof(buffer) - 1, HUGEPAGE_INFO_FMT, directory,
71                         internal_config.hugefile_prefix);
72         return buffer;
73 }
74
75 /** String format for hugepage map files. */
76 #define HUGEFILE_FMT "%s/%smap_%d"
77 #define TEMP_HUGEFILE_FMT "%s/%smap_temp_%d"
78
79 static inline const char *
80 eal_get_hugefile_path(char *buffer, size_t buflen, const char *hugedir, int f_id)
81 {
82         snprintf(buffer, buflen, HUGEFILE_FMT, hugedir,
83                         internal_config.hugefile_prefix, f_id);
84         buffer[buflen - 1] = '\0';
85         return buffer;
86 }
87
88 /** define the default filename prefix for the %s values above */
89 #define HUGEFILE_PREFIX_DEFAULT "rte"
90
91 /** Function to read a single numeric value from a file on the filesystem.
92  * Used to read information from files on /sys */
93 int eal_parse_sysfs_value(const char *filename, unsigned long *val);
94
95 #endif /* EAL_FILESYSTEM_H */