/src/openvswitch/lib/dirs.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c; buffer-read-only: t -*- */ |
2 | | #line 2 "./lib/dirs.c.in" |
3 | | /* |
4 | | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
5 | | * |
6 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | | * you may not use this file except in compliance with the License. |
8 | | * You may obtain a copy of the License at: |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, software |
13 | | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | * See the License for the specific language governing permissions and |
16 | | * limitations under the License. |
17 | | */ |
18 | | |
19 | | #include <config.h> |
20 | | #include "dirs.h" |
21 | | #include <stdlib.h> |
22 | | #include "ovs-thread.h" |
23 | | #include "util.h" |
24 | | |
25 | | struct directory { |
26 | | const char *value; /* Actual value; NULL if not yet determined. */ |
27 | | const char *default_value; /* Default value. */ |
28 | | const char *var_name; /* Environment variable to override default. */ |
29 | | struct ovsthread_once once; /* Ensures 'value' gets initialized once. */ |
30 | | }; |
31 | | |
32 | | static const char * |
33 | | get_dir(struct directory *d) |
34 | 0 | { |
35 | 0 | if (ovsthread_once_start(&d->once)) { |
36 | 0 | d->value = getenv(d->var_name); |
37 | 0 | if (!d->value || !d->value[0]) { |
38 | 0 | d->value = d->default_value; |
39 | 0 | } |
40 | 0 | ovsthread_once_done(&d->once); |
41 | 0 | } |
42 | 0 | return d->value; |
43 | 0 | } |
44 | | |
45 | | const char * |
46 | | ovs_sysconfdir(void) |
47 | 0 | { |
48 | 0 | static struct directory d = { |
49 | 0 | NULL, "/usr/local/etc", "OVS_SYSCONFDIR", OVSTHREAD_ONCE_INITIALIZER |
50 | 0 | }; |
51 | |
|
52 | 0 | return get_dir(&d); |
53 | 0 | } |
54 | | |
55 | | const char * |
56 | | ovs_pkgdatadir(void) |
57 | 0 | { |
58 | 0 | static struct directory d = { |
59 | 0 | NULL, "/usr/local/share/openvswitch", "OVS_PKGDATADIR", OVSTHREAD_ONCE_INITIALIZER |
60 | 0 | }; |
61 | |
|
62 | 0 | return get_dir(&d); |
63 | 0 | } |
64 | | |
65 | | const char * |
66 | | ovs_rundir(void) |
67 | 0 | { |
68 | 0 | static struct directory d = { |
69 | 0 | NULL, "/usr/local/var/run/openvswitch", "OVS_RUNDIR", OVSTHREAD_ONCE_INITIALIZER |
70 | 0 | }; |
71 | |
|
72 | 0 | return get_dir(&d); |
73 | 0 | } |
74 | | |
75 | | const char * |
76 | | ovs_logdir(void) |
77 | 0 | { |
78 | 0 | static struct directory d = { |
79 | 0 | NULL, "/usr/local/var/log/openvswitch", "OVS_LOGDIR", OVSTHREAD_ONCE_INITIALIZER |
80 | 0 | }; |
81 | |
|
82 | 0 | return get_dir(&d); |
83 | 0 | } |
84 | | |
85 | | const char * |
86 | | ovs_dbdir(void) |
87 | 0 | { |
88 | 0 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
89 | 0 | static const char *dbdir; |
90 | |
|
91 | 0 | if (ovsthread_once_start(&once)) { |
92 | 0 | dbdir = getenv("OVS_DBDIR"); |
93 | 0 | if (!dbdir || !dbdir[0]) { |
94 | 0 | char *sysconfdir = getenv("OVS_SYSCONFDIR"); |
95 | |
|
96 | 0 | dbdir = (sysconfdir |
97 | 0 | ? xasprintf("%s/openvswitch", sysconfdir) |
98 | 0 | : "/usr/local/etc/openvswitch"); |
99 | 0 | } |
100 | 0 | ovsthread_once_done(&once); |
101 | 0 | } |
102 | 0 | return dbdir; |
103 | 0 | } |
104 | | |
105 | | const char * |
106 | | ovs_bindir(void) |
107 | 0 | { |
108 | 0 | static struct directory d = { |
109 | 0 | NULL, "/usr/local/bin", "OVS_BINDIR", OVSTHREAD_ONCE_INITIALIZER |
110 | 0 | }; |
111 | |
|
112 | 0 | return get_dir(&d); |
113 | 0 | } |