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_private.h"
18 : : #include "eal_filesystem.h"
19 : :
20 : 205 : int eal_create_runtime_dir(void)
21 : : {
22 : : const char *directory;
23 : : char run_dir[PATH_MAX];
24 : : char tmp[PATH_MAX];
25 : : int ret;
26 : :
27 : : /* from RuntimeDirectory= see systemd.exec */
28 : 205 : directory = getenv("RUNTIME_DIRECTORY");
29 [ + - ]: 205 : if (directory == NULL) {
30 : : /*
31 : : * Used standard convention defined in
32 : : * XDG Base Directory Specification and
33 : : * Filesystem Hierarchy Standard.
34 : : */
35 [ - + ]: 205 : if (getuid() == 0)
36 : : directory = "/var/run";
37 : : else
38 [ # # ]: 0 : directory = getenv("XDG_RUNTIME_DIR") ? : "/tmp";
39 : : }
40 : :
41 : : /* create DPDK subdirectory under runtime dir */
42 : : ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
43 [ - + ]: 205 : if (ret < 0 || ret == sizeof(tmp)) {
44 : 0 : EAL_LOG(ERR, "Error creating DPDK runtime path name");
45 : 0 : return -1;
46 : : }
47 : :
48 : : /* create prefix-specific subdirectory under DPDK runtime dir */
49 : 205 : ret = snprintf(run_dir, sizeof(run_dir), "%s/%s",
50 : : tmp, eal_get_hugefile_prefix());
51 [ - + ]: 205 : if (ret < 0 || ret == sizeof(run_dir)) {
52 : 0 : EAL_LOG(ERR, "Error creating prefix-specific runtime path name");
53 : 0 : return -1;
54 : : }
55 : :
56 : : /* create the path if it doesn't exist. no "mkdir -p" here, so do it
57 : : * step by step.
58 : : */
59 : 205 : ret = mkdir(tmp, 0700);
60 [ + + - + ]: 205 : if (ret < 0 && errno != EEXIST) {
61 : 0 : EAL_LOG(ERR, "Error creating '%s': %s",
62 : : tmp, strerror(errno));
63 : 0 : return -1;
64 : : }
65 : :
66 : 205 : ret = mkdir(run_dir, 0700);
67 [ + + - + ]: 205 : if (ret < 0 && errno != EEXIST) {
68 : 0 : EAL_LOG(ERR, "Error creating '%s': %s",
69 : : run_dir, strerror(errno));
70 : 0 : return -1;
71 : : }
72 : :
73 [ - + ]: 205 : if (eal_set_runtime_dir(run_dir))
74 : 0 : return -1;
75 : :
76 : : return 0;
77 : : }
78 : :
79 : : /* parse a sysfs (or other) file containing one integer value */
80 : 50543 : int eal_parse_sysfs_value(const char *filename, unsigned long *val)
81 : : {
82 : : FILE *f;
83 : : char buf[BUFSIZ];
84 : 50543 : char *end = NULL;
85 : :
86 [ + + ]: 50543 : if ((f = fopen(filename, "r")) == NULL) {
87 : 1 : EAL_LOG(ERR, "%s(): cannot open sysfs value %s",
88 : : __func__, filename);
89 : 1 : return -1;
90 : : }
91 : :
92 [ + + ]: 50542 : if (fgets(buf, sizeof(buf), f) == NULL) {
93 : 1 : EAL_LOG(ERR, "%s(): cannot read sysfs value %s",
94 : : __func__, filename);
95 : 1 : fclose(f);
96 : 1 : return -1;
97 : : }
98 : 50541 : *val = strtoul(buf, &end, 0);
99 [ + - + - : 50541 : if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
+ + ]
100 : 3 : EAL_LOG(ERR, "%s(): cannot parse sysfs value %s",
101 : : __func__, filename);
102 : 3 : fclose(f);
103 : 3 : return -1;
104 : : }
105 : 50538 : fclose(f);
106 : 50538 : return 0;
107 : : }
|