TR181-XPON  1.4.0
TR-181 PON manager.
persistency.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2022 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or
8 ** without modification, are permitted provided that the following
9 ** conditions are met:
10 **
11 ** 1. Redistributions of source code must retain the above copyright
12 ** notice, this list of conditions and the following disclaimer.
13 **
14 ** 2. Redistributions in binary form must reproduce the above
15 ** copyright notice, this list of conditions and the following
16 ** disclaimer in the documentation and/or other materials provided
17 ** with the distribution.
18 **
19 ** Subject to the terms and conditions of this license, each
20 ** copyright holder and contributor hereby grants to those receiving
21 ** rights under this license a perpetual, worldwide, non-exclusive,
22 ** no-charge, royalty-free, irrevocable (except for failure to
23 ** satisfy the conditions of this license) patent license to make,
24 ** have made, use, offer to sell, sell, import, and otherwise
25 ** transfer this software, where such license applies only to those
26 ** patent claims, already acquired or hereafter acquired, licensable
27 ** by such copyright holder or contributor that are necessarily
28 ** infringed by:
29 **
30 ** (a) their Contribution(s) (the licensed copyrights of copyright
31 ** holders and non-copyrightable additions of contributors, in
32 ** source or binary form) alone; or
33 **
34 ** (b) combination of their Contribution(s) with the work of
35 ** authorship to which such Contribution(s) was added by such
36 ** copyright holder or contributor, if, at the time the Contribution
37 ** is added, such addition causes such combination to be necessarily
38 ** infringed. The patent license shall not apply to any other
39 ** combinations which include the Contribution.
40 **
41 ** Except as expressly stated above, no rights or licenses from any
42 ** copyright holder or contributor is granted under this license,
43 ** whether expressly, by implication, estoppel or otherwise.
44 **
45 ** DISCLAIMER
46 **
47 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
48 ** CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
49 ** INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
50 ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 ** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
52 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
55 ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
56 ** AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
58 ** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 ** POSSIBILITY OF SUCH DAMAGE.
60 **
61 ****************************************************************************/
62 
91 #ifndef _GNU_SOURCE
92 #define _GNU_SOURCE
93 #endif
94 
95 /* Related header */
96 #include "persistency.h"
97 
98 /* System headers */
99 #include <errno.h>
100 #include <stdio.h> /* fopen() */
101 #include <stdlib.h> /* free() */
102 #include <string.h>
103 #include <sys/stat.h> /* mkdir() */
104 #include <sys/types.h> /* mkdir() */
105 #include <unistd.h> /* access() */
106 
107 /* Other libraries' headers */
108 #include <amxc/amxc_macros.h> /* when_null() */
109 
110 /* Own headers */
111 #include "dm_xpon_mngr.h"
112 #include "xpon_trace.h"
113 
114 /* Use this storage dir if the one returned by parser does not exist */
115 static const char* const STORAGE_DIR = "/etc/config/tr181-xpon/";
116 
117 /* Path to storage dir of this plugin, e.g., "/etc/config/tr181-xpon/". */
118 static char* s_storage_dir = NULL;
119 
127 static char* get_storage_dir(void) {
128 
129  char* result = NULL;
130  amxo_parser_t* parser = xpon_mngr_get_parser();
131  when_null(parser, exit);
132 
133  amxc_string_t dir;
134  amxc_string_init(&dir, 0);
135  const char* storage_dir = GETP_CHAR(&parser->config, "storage-path");
136  if(!storage_dir) {
137  amxc_string_clean(&dir);
138  goto exit;
139  }
140  amxc_string_setf(&dir, "%s", storage_dir);
141  amxc_string_resolve(&dir, &parser->config);
142  result = amxc_string_take_buffer(&dir);
143 
144 exit:
145  return result;
146 }
147 
161 void persistency_init(void) {
162 
163  char* dir = get_storage_dir();
164  SAH_TRACEZ_DEBUG(ME, "dir='%s'", dir ? dir : "NULL");
165 
166  s_storage_dir = strdup(dir ? dir : STORAGE_DIR);
167 
168  when_null_trace(s_storage_dir, exit, ERROR,
169  "Failed to allocate mem for s_storage_dir");
170 
171  if(access(s_storage_dir, R_OK | W_OK | X_OK) == 0) {
172  goto exit;
173  }
174 
175  SAH_TRACEZ_DEBUG(ME, "Create %s", s_storage_dir);
176 
183  if(mkdir(s_storage_dir, 0777) == -1) {
184  SAH_TRACEZ_ERROR(ME, "Failed to create %s", s_storage_dir);
185  free(s_storage_dir);
186  s_storage_dir = NULL;
187  }
188 
189 exit:
190  free(dir);
191 }
192 
199  if(s_storage_dir) {
200  free(s_storage_dir);
201  s_storage_dir = NULL;
202  }
203 }
204 
210 const char* persistency_get_folder(void) {
211  return s_storage_dir;
212 }
213 
219 static void touch_file(const char* const file) {
220  /* Avoid writing the file if it already exists */
221  if(access(file, F_OK) == 0) {
222  SAH_TRACEZ_DEBUG2(ME, "%s already exists", file);
223  return;
224  }
225 
226  FILE* f = fopen(file, "w");
227  if(NULL == f) {
228  SAH_TRACEZ_ERROR(ME, "Failed to create '%s': %s", file, strerror(errno));
229  } else {
230  fclose(f);
231  }
232 }
233 
234 static void create_enabled_file_path(amxc_string_t* const file_path,
235  const char* const object) {
236 
237  amxc_string_set(file_path, s_storage_dir);
238  amxc_string_appendf(file_path, "%s", object);
239  amxc_string_append(file_path, "_enabled.txt", 12);
240 }
241 
248 void persistency_enable(const char* const object, bool enable) {
249 
250  when_null_trace(s_storage_dir, exit, DEBUG, "No persistency");
251  when_null(object, exit);
252 
253  amxc_string_t file_path;
254  amxc_string_init(&file_path, 0);
255  create_enabled_file_path(&file_path, object);
256 
257  const char* const file_path_cstr = amxc_string_get(&file_path, 0);
258 
259  SAH_TRACEZ_DEBUG(ME, "object='%s enable=%d", object, enable);
260 
261  if(enable) {
262  touch_file(file_path_cstr);
263  } else {
264  unlink(file_path_cstr);
265  }
266 
267  amxc_string_clean(&file_path);
268 
269 exit:
270  return;
271 }
272 
280 bool persistency_is_enabled(const char* const object) {
281 
282  bool rv = false;
283  when_null_trace(s_storage_dir, exit, DEBUG, "No persistency");
284  when_null(object, exit);
285 
286  amxc_string_t file_path;
287  amxc_string_init(&file_path, 0);
288  create_enabled_file_path(&file_path, object);
289 
290  const char* const file_path_cstr = amxc_string_get(&file_path, 0);
291 
292  rv = (access(file_path_cstr, F_OK) == 0) ? true : false;
293 
294  amxc_string_clean(&file_path);
295 
296 exit:
297  return rv;
298 }
299 
amxo_parser_t *PRIVATE xpon_mngr_get_parser(void)
static void create_enabled_file_path(amxc_string_t *const file_path, const char *const object)
Definition: persistency.c:234
bool persistency_is_enabled(const char *const object)
Definition: persistency.c:280
static char * s_storage_dir
Definition: persistency.c:118
static char * get_storage_dir(void)
Definition: persistency.c:127
void persistency_init(void)
Definition: persistency.c:161
void persistency_enable(const char *const object, bool enable)
Definition: persistency.c:248
void persistency_cleanup(void)
Definition: persistency.c:198
static const char *const STORAGE_DIR
Definition: persistency.c:115
const char * persistency_get_folder(void)
Definition: persistency.c:210
static void touch_file(const char *const file)
Definition: persistency.c:219
#define SAH_TRACEZ_DEBUG(zone, format,...)
Definition: xpon_trace.h:115
#define SAH_TRACEZ_DEBUG2(zone, format,...)
Definition: xpon_trace.h:127
#define ME
Definition: xpon_trace.h:78