Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2018 Intel Corporation.
3 : : * Copyright(c) 2012-2014 6WIND S.A.
4 : : */
5 : :
6 : : #include <errno.h>
7 : : #include <limits.h>
8 : : #include <stddef.h>
9 : : #include <stdio.h>
10 : : #include <stdlib.h>
11 : : #include <string.h>
12 : : #include <sys/stat.h>
13 : : #include <unistd.h>
14 : :
15 : : #include <rte_log.h>
16 : :
17 : : #include <eal_export.h>
18 : : #include "eal_private.h"
19 : : #include "eal_filesystem.h"
20 : :
21 : 206 : int eal_create_runtime_dir(void)
22 : : {
23 : : const char *directory;
24 : : char run_dir[PATH_MAX];
25 : : char tmp[PATH_MAX];
26 : : int ret;
27 : :
28 : : /* from RuntimeDirectory= see systemd.exec */
29 : 206 : directory = getenv("RUNTIME_DIRECTORY");
30 [ + - ]: 206 : if (directory == NULL) {
31 : : /*
32 : : * Used standard convention defined in
33 : : * XDG Base Directory Specification and
34 : : * Filesystem Hierarchy Standard.
35 : : */
36 [ - + ]: 206 : if (getuid() == 0)
37 : : directory = "/var/run";
38 : : else
39 [ # # ]: 0 : directory = getenv("XDG_RUNTIME_DIR") ? : "/tmp";
40 : : }
41 : :
42 : : /* create DPDK subdirectory under runtime dir */
43 : : ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
44 [ - + ]: 206 : if (ret < 0 || ret == sizeof(tmp)) {
45 : 0 : EAL_LOG(ERR, "Error creating DPDK runtime path name");
46 : 0 : return -1;
47 : : }
48 : :
49 : : /* create prefix-specific subdirectory under DPDK runtime dir */
50 : 206 : ret = snprintf(run_dir, sizeof(run_dir), "%s/%s",
51 : : tmp, eal_get_hugefile_prefix());
52 [ - + ]: 206 : if (ret < 0 || ret == sizeof(run_dir)) {
53 : 0 : EAL_LOG(ERR, "Error creating prefix-specific runtime path name");
54 : 0 : return -1;
55 : : }
56 : :
57 : : /* create the path if it doesn't exist. no "mkdir -p" here, so do it
58 : : * step by step.
59 : : */
60 : 206 : ret = mkdir(tmp, 0700);
61 [ + + - + ]: 206 : if (ret < 0 && errno != EEXIST) {
62 : 0 : EAL_LOG(ERR, "Error creating '%s': %s",
63 : : tmp, strerror(errno));
64 : 0 : return -1;
65 : : }
66 : :
67 : 206 : ret = mkdir(run_dir, 0700);
68 [ + + - + ]: 206 : if (ret < 0 && errno != EEXIST) {
69 : 0 : EAL_LOG(ERR, "Error creating '%s': %s",
70 : : run_dir, strerror(errno));
71 : 0 : return -1;
72 : : }
73 : :
74 [ - + ]: 206 : if (eal_set_runtime_dir(run_dir))
75 : 0 : return -1;
76 : :
77 : : return 0;
78 : : }
79 : :
80 : : /* parse a sysfs (or other) file containing one integer value */
81 : : RTE_EXPORT_SYMBOL(eal_parse_sysfs_value)
82 : 50817 : int eal_parse_sysfs_value(const char *filename, unsigned long *val)
83 : : {
84 : : FILE *f;
85 : : char buf[BUFSIZ];
86 : 50817 : char *end = NULL;
87 : :
88 [ + + ]: 50817 : if ((f = fopen(filename, "r")) == NULL) {
89 : 1 : EAL_LOG(ERR, "%s(): cannot open sysfs value %s",
90 : : __func__, filename);
91 : 1 : return -1;
92 : : }
93 : :
94 [ + + ]: 50816 : if (fgets(buf, sizeof(buf), f) == NULL) {
95 : 1 : EAL_LOG(ERR, "%s(): cannot read sysfs value %s",
96 : : __func__, filename);
97 : 1 : fclose(f);
98 : 1 : return -1;
99 : : }
100 : 50815 : *val = strtoul(buf, &end, 0);
101 [ + - + - : 50815 : if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
+ + ]
102 : 3 : EAL_LOG(ERR, "%s(): cannot parse sysfs value %s",
103 : : __func__, filename);
104 : 3 : fclose(f);
105 : 3 : return -1;
106 : : }
107 : 50812 : fclose(f);
108 : 50812 : return 0;
109 : : }
|