TR181-XPON  1.4.0
TR-181 PON manager.
upgrade_persistency.h File Reference
#include <stdbool.h>

Go to the source code of this file.

Functions

void upgr_persistency_init (void)
 
void upgr_persistency_cleanup (void)
 
void upgr_persistency_set_password (const char *const ani_path, const char *const password, bool hex)
 
bool upgr_persistency_get_password (const char *const ani_path, char *const password, bool *hex)
 

Detailed Description

Functionality related to saving and querying the upgrade persistent settings.

Definition in file upgrade_persistency.h.

Function Documentation

◆ upgr_persistency_cleanup()

void upgr_persistency_cleanup ( void  )

Clean up the part responsible for the upgrade persistent settings.

The plugin must call this function once when stopping.

Definition at line 152 of file upgrade_persistency.c.

152  {
153  if(s_upgr_storage_dir) {
154  free(s_upgr_storage_dir);
155  s_upgr_storage_dir = NULL;
156  }
157 }
static char * s_upgr_storage_dir
Here is the caller graph for this function:

◆ upgr_persistency_get_password()

bool upgr_persistency_get_password ( const char *const  ani_path,
char *const  password,
bool *  hex 
)

Get the saved password for a certain ANI instance.

Parameters
[in]ani_pathpath to ANI instance, e.g. "XPON.ONU.1.ANI.1"
[in,out]passwordthe function writes the saved password to this parameter if a saved password for the ANI exists. If no saved password exists, the function does not write to this parameter. The caller must pass a buffer of size MAX_PASSWORD_LEN_PLUS_ONE to ensure this function has enough space to write a password.
[in,out]hexthe function sets this parameter to true if the password it writes to password is in hexadecimal format.

If no saved password exists for the ANI, the function does not update password or hex, and returns true.

Returns
true on success, else false

Definition at line 242 of file upgrade_persistency.c.

244  {
245 
246  bool rv = false;
247  int i;
248  char password_saved[MAX_PASSWORD_LEN_PLUS_ONE] = { 0 };
249  const char* password_file_used = NULL;
250 
251  amxc_string_t file_path_ascii;
252  amxc_string_t file_path_hex;
253  amxc_string_init(&file_path_ascii, 0);
254  amxc_string_init(&file_path_hex, 0);
255 
256  create_password_file_path(ani_path, /*hex=*/ false, &file_path_ascii);
257  create_password_file_path(ani_path, /*hex=*/ true, &file_path_hex);
258  const char* files[2] = { 0 };
259  files[ASCII_PASSWORD_FILE_INDEX] = amxc_string_get(&file_path_ascii, 0);
260  files[HEX_PASSWORD_FILE_INDEX] = amxc_string_get(&file_path_hex, 0);
261 
262  for(i = 0; i < 2; ++i) {
263  if(access(files[i], F_OK) == 0) {
264  if(!read_first_line_from_file(files[i], password_saved,
266  SAH_TRACEZ_ERROR(ME, "Failed get password from %s", files[i]);
267  goto exit;
268  }
269  password_file_used = files[i];
270  if(HEX_PASSWORD_FILE_INDEX == i) {
271  *hex = true;
272  }
273  break;
274  }
275  }
276  if(password_file_used == NULL) {
277  SAH_TRACEZ_DEBUG(ME, "%s: no password", ani_path);
278  rv = true;
279  goto exit;
280  }
281 
282 
283  if(strlen(password_saved) == 0) {
284  SAH_TRACEZ_ERROR(ME, "Password read from %s is empty",
285  password_file_used);
286  goto exit;
287  }
288 
289  if(snprintf(password, MAX_PASSWORD_LEN_PLUS_ONE, "%s",
290  password_saved) < 0) {
291  SAH_TRACEZ_ERROR(ME, "snprintf() to copy password failed");
292  goto exit;
293  }
294  SAH_TRACEZ_DEBUG(ME, "ani='%s': password='%s' hex=%d", ani_path,
295  password, *hex);
296  rv = true;
297 
298 exit:
299  amxc_string_clean(&file_path_ascii);
300  amxc_string_clean(&file_path_hex);
301  return rv;
302 }
bool read_first_line_from_file(const char *const path, char *line, size_t len)
Definition: file_utils.c:212
#define MAX_PASSWORD_LEN_PLUS_ONE
#define HEX_PASSWORD_FILE_INDEX
static void create_password_file_path(const char *const ani_object, bool hex, amxc_string_t *const file_path)
#define ASCII_PASSWORD_FILE_INDEX
#define SAH_TRACEZ_DEBUG(zone, format,...)
Definition: xpon_trace.h:115
#define ME
Definition: xpon_trace.h:78
Here is the call graph for this function:
Here is the caller graph for this function:

◆ upgr_persistency_init()

void upgr_persistency_init ( void  )

Initialize the part responsible for the upgrade persistent settings.

The plugin must call this function once at startup after it has called persistency_init(): if this function can not use the folder for upgrade persistent files, it uses the folder for reboot persistent files created by persistency_init().

Definition at line 112 of file upgrade_persistency.c.

112  {
113 
114  if(access(UPGR_STORAGE_DIR_OVERALL, R_OK | W_OK | X_OK) == 0) {
115  if(access(UPGR_STORAGE_DIR_COMPONENT, R_OK | W_OK | X_OK) == 0) {
117  when_null_trace(s_upgr_storage_dir, exit, ERROR,
118  "Failed to allocate mem for s_upgr_storage_dir");
119  goto exit;
120  }
121  if(mkdir(UPGR_STORAGE_DIR_COMPONENT, 0777) == -1) {
122  SAH_TRACEZ_ERROR(ME, "Failed to create %s: %s", UPGR_STORAGE_DIR_COMPONENT,
123  strerror(errno));
124  goto exit;
125  }
127  when_null_trace(s_upgr_storage_dir, exit, ERROR,
128  "Failed to allocate mem for s_upgr_storage_dir");
129  } else {
130  SAH_TRACEZ_WARNING(ME, "No folder for upgrade persistent settings");
131  SAH_TRACEZ_WARNING(ME, "Use folder for reboot persistent settings");
132  const char* const reboot_persistent = persistency_get_folder();
133  if(access(reboot_persistent, R_OK | W_OK | X_OK) != 0) {
134  SAH_TRACEZ_ERROR(ME, "No folder for any persistent settings");
135  goto exit;
136  }
137  s_upgr_storage_dir = strdup(reboot_persistent);
138  when_null_trace(s_upgr_storage_dir, exit, ERROR,
139  "Failed to allocate mem for s_upgr_storage_dir");
140  }
141 
142 exit:
143  SAH_TRACEZ_DEBUG(ME, "dir='%s'", s_upgr_storage_dir ? : "NULL");
144  return;
145 }
const char * persistency_get_folder(void)
Definition: persistency.c:210
#define UPGR_STORAGE_DIR_OVERALL
#define UPGR_STORAGE_DIR_COMPONENT
Here is the call graph for this function:
Here is the caller graph for this function:

◆ upgr_persistency_set_password()

void upgr_persistency_set_password ( const char *const  ani_path,
const char *const  password,
bool  hex 
)

Save the password of a certain ANI instance.

Parameters
[in]ani_pathpath to ANI instance, e.g. "XPON.ONU.1.ANI.1"
[in]passwordnew value for the parameter Password of the ANI referred to by ani_path
[in]hexif true password is in hexadecimal format; if false, password is in ASCII format. The function only uses this parameter if password is not empty.

If password is empty, the function removes any files storing the password for the ANI instance.

Definition at line 185 of file upgrade_persistency.c.

187  {
188 
189  when_null_trace(s_upgr_storage_dir, exit, DEBUG, "No persistency");
190  when_null(ani_path, exit);
191  when_null(password, exit);
192 
193  SAH_TRACEZ_DEBUG(ME, "ani_path='%s' password='%s' hex=%d",
194  ani_path, password, hex);
195 
196  amxc_string_t file_path_ascii;
197  amxc_string_t file_path_hex;
198  amxc_string_init(&file_path_ascii, 0);
199  amxc_string_init(&file_path_hex, 0);
200 
201  create_password_file_path(ani_path, /*hex=*/ false, &file_path_ascii);
202  create_password_file_path(ani_path, /*hex=*/ true, &file_path_hex);
203 
204  const char* const ascii_cstr = amxc_string_get(&file_path_ascii, 0);
205  const char* const hex_cstr = amxc_string_get(&file_path_hex, 0);
206 
207  if(strlen(password) == 0) {
208  unlink(ascii_cstr);
209  unlink(hex_cstr);
210  } else {
211  write_file(hex ? hex_cstr : ascii_cstr, password);
212  unlink(hex ? ascii_cstr : hex_cstr);
213  }
214 
215  amxc_string_clean(&file_path_ascii);
216  amxc_string_clean(&file_path_hex);
217 
218 exit:
219  return;
220 }
bool write_file(const char *const path, const char *const value)
Definition: file_utils.c:154
Here is the call graph for this function:
Here is the caller graph for this function: