Init basesystem source codes.
[staging/basesystem.git] / nsframework / backup_manager / server / src / bkup_process.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <dirent.h>
18 #include <fcntl.h>
19 #include <libgen.h>
20 #include <inttypes.h>
21 #include <mqueue.h>
22 #include <agl_thread.h>
23 #include <poll.h>
24 #include <signal.h>
25 #include <sys/epoll.h>
26 #include <sys/eventfd.h>
27 #include <sys/prctl.h>
28 #include <sys/stat.h>
29 #include <sys/syscall.h>
30 #include <sys/timerfd.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <native_service/frameworkunified_framework_if.h>
34 #include <system_service/ss_system_manager_if.h>
35 #include <nv_hal.h>
36 #include <aglpath.h>
37 #include <cerrno>
38 #include <cstdio>
39 #include <map>
40 #include <set>
41 #include <string>
42 #include <utility>
43 #include "bkup_api.h"
44 #include "bkup_backupmanagerlog.h"
45 #include "bkup_param.h"
46 #include "bkup_crc32.h"
47 #include "bkup_util.h"
48
49 #define BKUP_THREAD_NAND "NS_BkupNAND"
50 #define BKUP_THREAD_DELAY "NS_BkupDelay"
51 const char kBkupPrexixEncryption[] = "ENC_";
52 const size_t kBkupPrexixEncryptionSize = 4;
53
54 typedef void (* signal_handler)(int);
55 extern void SignalHandlerFuncForMsgHandleThread(int signum);
56 extern pthread_t g_delay_thread_id;
57 extern pthread_t g_nand_thread_id;
58
59 #define MAX_FD_EPOLL 8
60
61 /**
62  * internal state
63  */
64 typedef enum {
65   BKUP_STATE_INIT = 0,
66   BKUP_STATE_NORMAL,
67   BKUP_STATE_TERM,
68 } bkup_state_t;
69 static bkup_state_t g_bkup_internal_state = BKUP_STATE_INIT;
70
71
72 /**
73  * delay thread command
74  */
75 typedef enum {
76   BKUP_DELAY_CMD_TERM = 0,
77   BKUP_DELAY_CMD_PRE_ACCOFF,
78   BKUP_DELAY_CMD_REGIST,
79 } bkup_delay_cmd_t;
80
81
82 /**
83  * inter thread communication message
84  */
85 typedef union {
86   struct {
87     char client_name[16];
88     uint32_t seq_id;
89     char *rcv_buf;
90     int rcv_size;
91   } nand;
92   struct {
93
94 //    bool terminate;
95     bkup_delay_cmd_t cmd;
96
97     int event_fd;
98     char item_name[64];
99   } delay;
100 } bkup_itc_message_t;
101
102 /**
103  * reference response function
104  */
105 typedef struct {
106   int (*set_response)(void *, void *, int, EFrameworkunifiedStatus);
107   void *opt;
108 } bkup_response_t;
109
110 /**
111  * response MQ map(TLS)
112  */
113 typedef std::map<std::string, HANDLE> g_bkup_response_mq_t;
114 static __thread g_bkup_response_mq_t *g_bkup_response_mq;
115
116 /**
117  * NAND thread itc MQ
118  */
119 static mqd_t g_bkup_nand_mqd = -1;
120
121 /**
122  * Delay thread itc MQ
123  */
124 static mqd_t g_bkup_delay_mqd = -1;
125
126 static int g_bkup_nand_tid;
127 static int g_bkup_delay_tid;
128
129 /**
130  * prototype declared
131  */
132 static void BkupApilog(bkup_response_t *response, enum NvHalMedia current_media,
133                        bkup_protocol_header_t *hdr,
134                        bkup_query_result_t *query_result, char *buf,
135                        uint32_t crc32, EFrameworkunifiedStatus ret_value);
136
137 /**
138  * check status of file spicefied by path
139  */
140 static int BkupCheckStatFromNv(enum NvHalMedia media, const char *path,
141                                int check_size);
142
143 /**
144  * rwlock unlock
145  */
146 static inline void BkupRwlockUnlock(pthread_rwlock_t *rwlock, const char *path);
147
148 /**
149  * rwlock read lock
150  */
151 static inline void BkupRwlockRdlock(pthread_rwlock_t *rwlock, const char *path);
152
153 /**
154  * rwlock write lock
155  */
156 static inline void BkupRwlockWrlock(pthread_rwlock_t *rwlock, const char *path);
157
158 /**\ingroup CheckNv
159  * \~english @par Brief
160  *      Check the data stored on the storage device and check the data size.
161  * \~english @param [in] media
162  *      enum NvHalMedia           - Media type
163  * \~english @param [in] filename
164  *      const char*               - Concatenated string of "category name/item name"
165  * \~english @param [in] size
166  *      uint32_t                  - Data size to be checked
167  * \~english @retval eFrameworkunifiedStatusOK OK
168  * \~english @retval eFrameworkunifiedStatusFileLoadError The file does not exist.
169  * \~english @retval eFrameworkunifiedStatusAccessError Check size failed
170  * \~english @retval eFrameworkunifiedStatusInvldParam Parameter error
171  * \~english @par Preconditons
172  *      - none
173  * \~english @par Conditions of processing failure
174  *      - If the media (media) specified in the parameter is smaller than NVHALMEDIA_CACHEDRAM or larger than NVHALMEDIA_NAND.\n
175  *      [eFrameworkunifiedStatusInvldParam]
176  *      - The identifier specified in the arguments is a NULL.[eFrameworkunifiedStatusInvldParam]
177  *      - When the size specified in the arguments is 0.[eFrameworkunifiedStatusInvldParam]
178  *      - Internal I/O handling errors (lstat).[eFrameworkunifiedStatusFileLoadError]
179  *      - The size specified by the parameter does not correspond to the file size specified by the identifier.[eFrameworkunifiedStatusAccessError]
180  */
181 static EFrameworkunifiedStatus CheckNv(enum NvHalMedia media, const char *filename, uint32_t size) {
182   uint32_t size_;
183   EFrameworkunifiedStatus ret;
184   ret = GetSizeNv(media, filename, &size_);  // Get size
185   if (ret != eFrameworkunifiedStatusOK) {  // If the GetSizeNv fails, the error is returned unchanged.
186     return ret;
187   }
188   if (size_ != size) {  // Returns eFrameworkunifiedStatusAccessError if the sizes do not match.
189     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid size:%d\n", size);
190     return eFrameworkunifiedStatusAccessError;
191   }
192   return eFrameworkunifiedStatusOK;  // Returns eFrameworkunifiedStatusOK if size matches
193 }
194
195
196
197 /**
198  * read file spicefied by id
199  *
200  * \retval  0 read success
201  * \retval -1 crc error
202  * \retval -2 read failed
203  * \retval -3 invalid param
204  */
205 static int32_t BkupBufferReadFromNv(const enum NvHalMedia media, const char* path,
206                                     char *buf, size_t size, uint32_t *crc32) {
207   int32_t ret = 0;
208   EFrameworkunifiedStatus nv_ret = eFrameworkunifiedStatusFail;
209   const uint32_t size_all = static_cast<uint32_t>(size + sizeof(uint32_t));
210   void* buffer = MAP_FAILED;
211   uint8_t* read_buffer = NULL;
212   uint32_t calc_crc32, get_crc32;
213
214   if (path == NULL) {  // LCOV_EXCL_BR_LINE 6:double check
215     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
216     return -1;  // LCOV_EXCL_LINE 6:double check
217   }
218
219   if (!buf) {
220     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "buf is NULL");
221     return -1;
222   }
223   if ((buffer = BkupAnonMmap(size_all)) == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5:mmap's error case.
224     // LCOV_EXCL_START 5:mmap's error case.
225     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
226     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:map failed");
227     ret = -1;
228     goto exit;
229     // LCOV_EXCL_STOP 5:mmap's error case.
230   }
231
232   read_buffer = reinterpret_cast<uint8_t*>(buffer);
233   nv_ret = ReadNv(media, path, read_buffer, size_all);
234   if (nv_ret == eFrameworkunifiedStatusOK) {
235     const void* const src_buffer = reinterpret_cast<void *>(read_buffer + size);
236     void* dest_buffer = reinterpret_cast<void *>(&get_crc32);
237     memcpy(dest_buffer, src_buffer, sizeof(uint32_t));
238     calc_crc32 = BkupCrc32(read_buffer, size);  // LCOV_EXCL_BR_LINE 11:unexpected branch
239     if (get_crc32 != calc_crc32) {
240       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
241       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "invalid crc(%s):%#x:%#x", path, get_crc32, calc_crc32);
242       // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
243       ret = -1;
244       goto exit;
245     }
246     if (crc32 != NULL) {
247       *crc32 = get_crc32;
248     }
249     dest_buffer = reinterpret_cast<void *>(buf);
250     memcpy(dest_buffer, buffer, size);
251   } else if (nv_ret == eFrameworkunifiedStatusInvldParam) {  // LCOV_EXCL_BR_LINE 6:double check
252     ret = -3;
253   } else {
254     ret = -2;
255   }
256 exit:
257   if (buffer != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5:mmap's error case.
258     munmap(buffer, size_all);
259   }
260
261   return ret;
262 }
263
264 /*
265  * buffer -> file write
266  */
267 static int32_t BkupBufferWriteToNv(const enum NvHalMedia media, const char* path,
268                                    char *buf, size_t size, uint32_t *crc32) {
269   if (buf == NULL) {
270     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "buf is NULL");
271     return eFrameworkunifiedStatusNullPointer;
272   }
273   int32_t ret = 0;
274   EFrameworkunifiedStatus nv_ret = eFrameworkunifiedStatusFail;
275   const uint32_t size_all = static_cast<uint32_t>(size + sizeof(uint32_t));
276   char *buffer = reinterpret_cast<char *>(MAP_FAILED);
277   uint32_t calc_crc32;
278
279   if ((buffer = BkupAnonMmap(size_all)) == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5:mmap's error case.
280     // LCOV_EXCL_START 5:mmap's error case.
281     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
282     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
283     ret = -1;
284     goto exit;
285     // LCOV_EXCL_STOP 5:mmap's error case.
286   }
287
288   calc_crc32 = BkupCrc32(buf, size);  // LCOV_EXCL_BR_LINE 11:unexpected branch
289   if (crc32) {
290     *crc32 = calc_crc32;
291   }
292
293   memcpy(buffer, buf, size);
294   memcpy(buffer + size, &calc_crc32, sizeof(uint32_t));
295
296   nv_ret = WriteNv(media, path, reinterpret_cast<uint8_t *>(buffer), size_all);
297   if (nv_ret != eFrameworkunifiedStatusOK) {
298     // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
299     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "write to nv(%s):%s", path, strerror(ret));
300     // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
301     ret = -1;
302   }
303
304 exit:
305   if (buffer != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5:mmap's error case.
306     munmap(buffer, size_all);
307   }
308
309   return ret;
310 }
311
312 /**
313  * inter media copy
314  */
315 static int BkupMediaCopyFromNv(char *category_name, const char *item_name,
316                                enum NvHalMedia src, enum NvHalMedia dst,
317                                pthread_rwlock_t *src_rwlock,
318                                pthread_rwlock_t *dst_rwlock) {
319   if (category_name == NULL) {
320     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "category_name is NULL");
321     return eFrameworkunifiedStatusNullPointer;
322   }
323
324   if (item_name == NULL) {
325     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "item_name is NULL");
326     return eFrameworkunifiedStatusNullPointer;
327   }
328   int ret = -1;
329   uint32_t crc32;
330   char id_path[FILENAME_MAX] = {0};
331   char *buffer = reinterpret_cast<char *>(MAP_FAILED);
332
333   bkup_query_result_t query_result;
334   BkupParamGet(item_name, &query_result);  // LCOV_EXCL_BR_LINE 11:unexpected branch
335
336   char prefix_name[kBkupPrexixEncryptionSize + 1] = {0};
337   if (query_result.encrypt != false) {
338     strncpy(prefix_name, kBkupPrexixEncryption, kBkupPrexixEncryptionSize);
339   }
340   snprintf(id_path, FILENAME_MAX, "%s/%s%s", category_name, prefix_name, item_name);
341
342   if ((buffer = BkupAnonMmap(query_result.size)) == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5:mmap's error case.
343     // LCOV_EXCL_START 5:mmap's error case.
344     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
345     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:map failed");
346     ret = eFrameworkunifiedStatusFail;
347     goto exit;
348     // LCOV_EXCL_STOP 5:mmap's error case.
349   }
350
351   BkupRwlockRdlock(src_rwlock, id_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
352   if ((ret = BkupBufferReadFromNv(src, id_path, buffer, query_result.size,
353                                   &crc32)) >= 0) {
354     // LCOV_EXCL_BR_START 11:unexpected branch
355     BkupRwlockWrlock(dst_rwlock, id_path);
356     ret = BkupBufferWriteToNv(dst, id_path, buffer, query_result.size, &crc32);
357     BkupRwlockUnlock(dst_rwlock, id_path);
358     // LCOV_EXCL_BR_STOP 11:unexpected branch
359   }
360   BkupRwlockUnlock(src_rwlock, id_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
361
362 exit:
363   if (buffer != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5:mmap's error case.
364     munmap(buffer, query_result.size);
365   }
366
367   return ret;
368 }
369
370 /**
371  * make backup path
372  */
373 static int BkupMakeBkupPathToNvId(const char *category_name,
374                                   const char *item_name, char **path_buf,
375                                   int buf_size) {
376   int ret = -1;
377   char prefix_name[kBkupPrexixEncryptionSize + 1] = {0};
378
379   if ((*path_buf = BkupAnonMmap(buf_size)) == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5:mmap's error case.
380     // LCOV_EXCL_START 5:mmap's error case.
381     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
382     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
383     goto exit;
384     // LCOV_EXCL_STOP 5:mmap's error case.
385   }
386   bkup_query_result_t query_result;
387   if (BkupParamGet(item_name, &query_result) < 0) {  // LCOV_EXCL_BR_LINE 6:Excluded due to checking by upper-level function
388     // LCOV_EXCL_START 6:Excluded due to checking by upper-level function
389     AGL_ASSERT_NOT_TESTED();
390     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid item_name:%s", item_name);
391     goto exit;
392     // LCOV_EXCL_STOP 6:Excluded due to checking by upper-level function
393   }
394   if (query_result.encrypt != false) {
395     strncpy(prefix_name, kBkupPrexixEncryption, kBkupPrexixEncryptionSize);
396   }
397   snprintf(*path_buf, static_cast<size_t>(buf_size), "%s/%s%s",
398            category_name, prefix_name, item_name);
399   ret = 0;
400
401 exit:
402   return ret;
403 }
404
405 /**
406  * delete data
407  */
408 static int BkupDeleteDataFromNv(const enum NvHalMedia media, const char* const path) {
409   int ret = 0;
410   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
411
412   if (path == NULL) {  // LCOV_EXCL_BR_LINE 6:Excluded due to checking by upper-level function
413     // LCOV_EXCL_START 6:Excluded due to checking by upper-level function
414     AGL_ASSERT_NOT_TESTED();
415     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "path is NULL");
416     ret = -1;
417     return ret;
418     // LCOV_EXCL_STOP 6:Excluded due to checking by upper-level function
419   }
420   const size_t path_size = static_cast<size_t>(std::string(path).length());
421   if (path_size  > FILENAME_MAX) {  // LCOV_EXCL_BR_LINE 6:Excluded due to checking by upper-level function
422     // LCOV_EXCL_START 6:Excluded due to checking by upper-level function
423     AGL_ASSERT_NOT_TESTED();
424     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "path size error");
425     ret = -1;
426     return ret;
427     // LCOV_EXCL_STOP 6:Excluded due to checking by upper-level function
428   }
429
430   uint32_t size = 0;
431   e_status = GetSizeNv(media, path, &size);
432   if (e_status == eFrameworkunifiedStatusFileLoadError) {
433     // If the file does not exist, deleet it successfully
434     return ret;
435   } else if (e_status != eFrameworkunifiedStatusOK) {
436     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "GetSizeNv e_status:%d\n", e_status);
437     ret = -1;
438     return ret;
439   } else {
440     // no proc
441   }
442
443   e_status = DeleteNv(media, path);
444   if (e_status != eFrameworkunifiedStatusOK) {
445     ret = -1;
446     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "DeleteNv e_status:%d\n", e_status);
447   }
448   return ret;
449 }
450
451 /**
452  * get rwlock
453  */
454 static pthread_rwlock_t *BkupGetRwlock(const char *item_name,
455                                        enum NvHalMedia media,
456                                        bkup_query_result_t *query_result) {
457   int ret;
458   pthread_rwlock_t *rwlock, *org_rwlock;
459   pthread_rwlockattr_t attr;
460
461   if (query_result == NULL) {
462     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "query_result is NULL");
463     return NULL;
464   }
465
466   if (media != NVHALMEDIA_BACKUPDRAM || query_result->backup_dram == false || query_result->nand == false) {
467     return NULL;
468   }
469   // backupDram && nand
470   if (query_result->opt != NULL) {
471     return reinterpret_cast<pthread_rwlock_t *>(query_result->opt);
472   }
473
474   if ((ret = pthread_rwlockattr_init(&attr)) != 0) {   // LCOV_EXCL_BR_LINE 5:pthread_rwlockattr_init's error case.
475     // LCOV_EXCL_START 5:pthread_rwlockattr_init's error case.
476     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
477     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlockattr_init:%s", strerror(ret));
478     return NULL;
479     // LCOV_EXCL_STOP 5:pthread_rwlockattr_init's error case.
480   }
481
482   // LCOV_EXCL_BR_START 5:pthread_rwlockattr_setkind_np's error case.
483   if ((ret = pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP)) != 0) {
484   // LCOV_EXCL_BR_STOP 5:pthread_rwlockattr_setkind_np's error case.
485     // LCOV_EXCL_START 5:pthread_rwlockattr_setkind_np's error case.
486     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
487     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlockattr_setkind_np:%s", strerror(ret));
488     return NULL;
489     // LCOV_EXCL_STOP 5:pthread_rwlockattr_setkind_np's error case.
490   }
491
492   // LCOV_EXCL_BR_START 5:malloc's error case.
493   if ((rwlock = reinterpret_cast<pthread_rwlock_t *>(malloc(sizeof(pthread_rwlock_t)))) == NULL) {
494   // LCOV_EXCL_BR_STOP 5:malloc's error case.
495     // LCOV_EXCL_START 5:malloc's error case.
496     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
497     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "malloc failed");
498     return NULL;
499     // LCOV_EXCL_STOP 5:malloc's error case.
500   }
501
502   if ((ret = pthread_rwlock_init(rwlock, &attr)) != 0) {  // LCOV_EXCL_BR_LINE 5:pthread_rwlock_init's error case.
503     // LCOV_EXCL_START 5:pthread_rwlock_init's error case.
504     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
505     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlock_init:%s", strerror(ret));
506     free(rwlock);
507     return NULL;
508     // LCOV_EXCL_STOP 5:pthread_rwlock_init's error case.
509   }
510
511   // LCOV_EXCL_BR_START 11:unexpected branch
512   org_rwlock = reinterpret_cast<pthread_rwlock_t *>(BkupParamSetOpt(item_name, rwlock));
513   // LCOV_EXCL_BR_STOP 11:unexpected branch
514   if (org_rwlock == reinterpret_cast<void *>(-1)) {  // LCOV_EXCL_BR_LINE 6:org_rwlock will not be -1
515     // LCOV_EXCL_START 6:org_rwlock will not be -1
516     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
517     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid item_name:%s", item_name != 0 ? item_name : NULL);
518     free(rwlock);
519     return NULL;
520     // LCOV_EXCL_STOP 6:org_rwlock will not be -1
521   } else if (org_rwlock != NULL) {  // LCOV_EXCL_BR_LINE 5:gcc builtin function's error case
522     // conflict
523     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
524     free(rwlock);  // LCOV_EXCL_LINE 5:gcc  builtin function's error case
525     rwlock = org_rwlock;  // LCOV_EXCL_LINE 5:gcc  builtin function's error case
526   }
527
528   query_result->opt = rwlock;
529   return rwlock;
530 }
531
532 /**
533  * rwlock read lock
534  */
535 static inline void BkupRwlockRdlock(pthread_rwlock_t *rwlock, const char *path) {
536   if (rwlock) {
537     int ret;
538     if ((ret = pthread_rwlock_rdlock(rwlock)) != 0) {  // LCOV_EXCL_BR_LINE 5:pthread_rwlock_rdlock's error case.
539       // LCOV_EXCL_START 5:pthread_rwlock_rdlock's error case.
540       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
541       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlock_rdlock(%s):%s", path, strerror(ret));
542       // LCOV_EXCL_STOP 5:pthread_rwlock_rdlock's error case.
543     }
544   }
545 }
546
547 /**
548  * rwlock write lock
549  */
550 static inline void BkupRwlockWrlock(pthread_rwlock_t *rwlock, const char *path) {
551   if (rwlock) {
552     int ret;
553     if ((ret = pthread_rwlock_wrlock(rwlock)) != 0) {  // LCOV_EXCL_BR_LINE 5:pthread_rwlock_wrlock's error case.
554       // LCOV_EXCL_START 5:pthread_rwlock_wrlock's error case.
555       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
556       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlock_wrlock(%s):%s", path, strerror(ret));
557       // LCOV_EXCL_STOP 5:pthread_rwlock_wrlock's error case.
558     }
559   }
560 }
561
562 /**
563  * rwlock unlock
564  */
565 static inline void BkupRwlockUnlock(pthread_rwlock_t *rwlock, const char *path) {
566   if (rwlock) {
567     int ret;
568     if ((ret = pthread_rwlock_unlock(rwlock)) != 0) {  // LCOV_EXCL_BR_LINE 5:pthread_rwlock_unlock's error case.
569       // LCOV_EXCL_START 5:pthread_rwlock_unlock's error case.
570       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
571       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlock_unlock(%s):%s", path, strerror(ret));
572       // LCOV_EXCL_STOP 5:pthread_rwlock_unlock's error case.
573     }
574   }
575 }
576
577 /**
578  * inter thread communication MQ open
579  */
580 static mqd_t BkupItcMqopen(const char *mq_name, int flags) {
581   struct mq_attr attr;
582
583   if (mq_name == NULL) {
584     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "mq_name is NULL");
585     return -1;
586   }
587
588   attr.mq_flags = 0;
589   attr.mq_maxmsg = 256;
590   attr.mq_msgsize = sizeof(bkup_itc_message_t);
591
592   return mq_open(mq_name, flags, 0660, &attr);
593 }
594
595 /**
596  * write & rename
597  */
598 static int BkupWriteRename(const char *category_name, const char *item_name,
599                            enum NvHalMedia media, char *buf, int size,
600                            pthread_rwlock_t *rwlock) {
601   int ret = -1;
602   char *full_path = reinterpret_cast<char *>(MAP_FAILED);
603
604   // LCOV_EXCL_BR_START 5:mmap's error case.
605   if (BkupMakeBkupPathToNvId(category_name, item_name, &full_path, FILENAME_MAX) < 0) {
606   // LCOV_EXCL_BR_STOP 5:mmap's error case.
607     // LCOV_EXCL_START 5:mmap's error case.
608     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
609     goto exit;
610     // LCOV_EXCL_STOP 5:mmap's error case.
611   }
612
613   BkupRwlockWrlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
614   if (BkupBufferWriteToNv(media, full_path, buf, size, NULL) < 0) {
615     BkupRwlockUnlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
616     goto exit;
617   }
618   BkupRwlockUnlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
619
620   ret = size;
621
622 exit:
623   if (full_path != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5:mmap's error case.
624     munmap(full_path, FILENAME_MAX);
625   }
626   return ret;
627 }
628
629 /**
630  * stat check
631  *
632  * \retval 0 file exists
633  * \retval -1 file not exists
634  * \retval -2 size error
635  */
636 static int BkupCheckStatFromNv(enum NvHalMedia media, const char *path,
637                                int check_size) {
638   uint32_t size = check_size + static_cast<uint32_t>(sizeof(uint32_t));
639   const EFrameworkunifiedStatus ret = CheckNv(media, path, size);
640
641   if (path == NULL) {
642     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "path is NULL");
643     return -1;
644   }
645
646   if (eFrameworkunifiedStatusOK == ret) {
647     return 0;
648   } else if (eFrameworkunifiedStatusAccessError == ret) {
649     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "CheckNv e_status:%d", ret);
650     return -2;
651   } else {
652     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "CheckNv e_status:%d", ret);
653     return -1;
654   }
655 }
656
657 /**
658  * media stat
659  */
660 static int BkupMediaStat(const char *category_name, const char *item_name, enum NvHalMedia src, int size) {
661   int ret = -1;
662   char *path = reinterpret_cast<char *>(MAP_FAILED);
663
664   // LCOV_EXCL_BR_START 5:mmap's error case.
665   if (BkupMakeBkupPathToNvId(category_name, item_name, &path, FILENAME_MAX) < 0) {
666   // LCOV_EXCL_BR_STOP 5:mmap's error case.
667     // LCOV_EXCL_START 5:mmap's error case.
668     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
669     goto exit;
670     // LCOV_EXCL_STOP 5:mmap's error case.
671   }
672   ret = BkupCheckStatFromNv(src, path, size);  // LCOV_EXCL_BR_LINE 11:unexpected branch
673
674 exit:
675   if (path != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5:mmap's error case.
676     munmap(path, FILENAME_MAX);
677   }
678   return ret;
679 }
680
681 /**
682  * stat check & read
683  *
684  * \retval 0 file exists
685  * \retval -1 file not exists
686  */
687 static int BkupCheckStatRead(enum NvHalMedia current_media, const char *path,
688                              char *buf, int check_size, uint32_t *crc32,
689                              pthread_rwlock_t *rwlock) {
690   int ret;
691
692   if ((ret = BkupCheckStatFromNv(current_media, path, check_size)) < 0) {
693     return ret;
694   }
695
696   BkupRwlockRdlock(rwlock, path);
697
698   if (BkupBufferReadFromNv(current_media, path, buf, check_size, crc32) < 0) {
699     BkupRwlockUnlock(rwlock, path);
700     return -1;
701   }
702
703   BkupRwlockUnlock(rwlock, path);
704   return 0;
705 }
706
707 /**
708  * valid check
709  *
710  * \retval 0 file invalid & do nothing
711  * \retval -1 file invalid but create 0 fill file
712  * \retval 1 file valid
713  */
714 static int BkupCheckRead(const char *path, enum NvHalMedia current_media,
715                          bkup_protocol_command_t command, char *rcv_buf,
716                          char *read_buf, uint32_t *crc32,
717                          bkup_query_result_t *query_result,
718                          pthread_rwlock_t *rwlock) {
719   int ret = -1;
720
721   if (rcv_buf == NULL) {
722     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
723     return 0;
724   }
725   if (query_result == NULL) {
726     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
727     return 0;
728   }
729
730   bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
731
732   if (BkupCheckStatRead(current_media, path, read_buf, query_result->size, crc32, rwlock) < 0) {
733     // file not exists
734     if (current_media == NVHALMEDIA_NAND || query_result->nand == false) {
735       goto exit;
736     } else {
737       if (command == BKUP_CMD_READ) {
738         /*
739          * Read
740          */
741         if (BkupCheckStatFromNv(NVHALMEDIA_NAND, path, query_result->size) == 0) {
742           // do nothing
743           ret = 0;
744         } else {
745           // take preference BackupDRAM or CacheDRAM
746           ret = -1;
747         }
748       } else {
749         /*
750          * Write/Fill
751          */
752         pthread_rwlock_t *nand_rwlock = BkupGetRwlock(hdr->item_name, NVHALMEDIA_NAND, query_result);
753         if (BkupCheckStatRead(NVHALMEDIA_NAND, path, read_buf, query_result->size, NULL, nand_rwlock) == 0) {
754           // write back cache
755           if (BkupWriteRename(query_result->category_name, hdr->item_name,
756                               current_media, read_buf, query_result->size,
757                               rwlock) >= 0) {
758             ret = 1;
759           } else {
760             // copy error, do nothing
761             ret = 0;
762           }
763         } else {
764           // take preference BackupDRAM or CacheDRAM
765           ret = -1;
766         }
767       }
768       goto exit;
769     }
770   }
771   ret = 1;
772 exit:
773   return ret;
774 }
775
776 /**
777  * read command
778  */
779 static int BkupCmdRead(bkup_response_t *response, enum NvHalMedia current_media,
780                        char *rcv_buf, bkup_query_result_t *query_result) {
781   int ret = -1;
782   if (response == NULL) {
783     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "response is NULL");
784     return -1;
785   }
786   if (rcv_buf == NULL) {
787     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
788     return -1;
789   }
790   if (query_result == NULL) {
791     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
792     return -1;
793   }
794   bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
795   // LCOV_EXCL_BR_START 11:unexpected branch
796   pthread_rwlock_t *rwlock = BkupGetRwlock(hdr->item_name, current_media, query_result);
797   // LCOV_EXCL_BR_STOP 11:unexpected branch
798   char *full_path = reinterpret_cast<char *>(MAP_FAILED);
799   char *buf = reinterpret_cast<char *>(MAP_FAILED);
800   size_t buf_size = 0;
801   uint32_t crc32 = 0;
802   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
803
804   // LCOV_EXCL_BR_START 5:mmap's error case.
805   if (BkupMakeBkupPathToNvId(query_result->category_name, hdr->item_name, &full_path, FILENAME_MAX) < 0) {
806   // LCOV_EXCL_BR_STOP 5:mmap's error case.
807     // LCOV_EXCL_START 5:mmap's error case.
808     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
809     e_status = eFrameworkunifiedStatusFail;
810     goto exit;
811     // LCOV_EXCL_STOP 5:mmap's error case.
812   }
813
814   if ((buf = BkupAnonMmap(query_result->size)) == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5:mmap's error case.
815     // LCOV_EXCL_START 5:mmap's error case.
816     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
817     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
818     e_status = eFrameworkunifiedStatusFail;
819     goto exit;
820     // LCOV_EXCL_STOP 5:mmap's error case.
821   }
822   buf_size = query_result->size;
823
824   ret = BkupCheckRead(full_path, current_media, hdr->command, rcv_buf, buf,
825                       &crc32, query_result, rwlock);  // LCOV_EXCL_BR_LINE 11:unexpected branch
826   if (ret == -1) {
827     // return all 0
828     ret = hdr->size;
829     goto exit;
830   } else if (ret == 0) {
831     // do nothing
832     goto exit;
833   }
834
835   // success
836   ret = hdr->size;
837
838   // caching to memory
839   if (current_media == NVHALMEDIA_NAND) {
840     if (query_result->cache_dram) {
841       if (BkupMediaStat(query_result->category_name, hdr->item_name,
842                         NVHALMEDIA_CACHEDRAM, query_result->size) < 0) {
843         // LCOV_EXCL_BR_START 11:unexpected branch
844         pthread_rwlock_t *dst_rwlock =
845             BkupGetRwlock(hdr->item_name, NVHALMEDIA_CACHEDRAM, query_result);
846         // LCOV_EXCL_BR_STOP 11:unexpected branch
847         BkupWriteRename(query_result->category_name, hdr->item_name,
848                         NVHALMEDIA_CACHEDRAM, buf, query_result->size,
849                         dst_rwlock);  // LCOV_EXCL_BR_LINE 11:unexpected branch
850       }
851     }
852     if (query_result->backup_dram) {
853       if (BkupMediaStat(query_result->category_name, hdr->item_name,
854                         NVHALMEDIA_BACKUPDRAM, query_result->size) < 0) {
855         // LCOV_EXCL_BR_START 11:unexpected branch
856         pthread_rwlock_t *dst_rwlock =
857             BkupGetRwlock(hdr->item_name, NVHALMEDIA_BACKUPDRAM, query_result);
858         // LCOV_EXCL_BR_STOP 11:unexpected branch
859         BkupWriteRename(query_result->category_name, hdr->item_name,
860                         NVHALMEDIA_BACKUPDRAM, buf, query_result->size,
861                         dst_rwlock);  // LCOV_EXCL_BR_LINE 11:unexpected branch
862       }
863     }
864   }
865
866 exit:
867   if (ret != 0 && buf != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
868     // LCOV_EXCL_BR_START 11:unexpected branch
869     BkupApilog(response, current_media, hdr, query_result, buf, crc32, e_status);
870     // LCOV_EXCL_BR_STOP 11:unexpected branch
871     // LCOV_EXCL_BR_START 4: NSFW error case
872     if ((*response->set_response)(response->opt, buf + hdr->offset, hdr->size, e_status) < 0) {
873     // LCOV_EXCL_BR_STOP 4: NSFW error case
874       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
875       ret = -1;  // LCOV_EXCL_LINE 4: NSFW error case
876     }
877   }
878
879   if (full_path != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
880     munmap(full_path, FILENAME_MAX);
881   }
882   if (buf != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
883     munmap(buf, buf_size);
884   }
885   return ret;
886 }
887
888 /**
889  * write command
890  */
891 static int BkupCmdWrite(bkup_response_t *response,
892                         enum NvHalMedia current_media, char *rcv_buf,
893                         bkup_query_result_t *query_result) {
894   int ret = -1;
895
896   if (response == NULL) {
897     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "response is NULL");
898     return -1;
899   }
900   if (rcv_buf == NULL) {
901     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
902     return -1;
903   }
904   if (query_result == NULL) {
905     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
906     return -1;
907   }
908   bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
909   // LCOV_EXCL_BR_START 11:unexpected branch
910   pthread_rwlock_t *rwlock = BkupGetRwlock(hdr->item_name, current_media, query_result);
911   // LCOV_EXCL_BR_STOP 11:unexpected branch
912   char *full_path = reinterpret_cast<char *>(MAP_FAILED);
913   char *buf = reinterpret_cast<char *>(MAP_FAILED);
914   char *header = NULL;
915   uint32_t crc32 = 0;
916   int buf_size = 0;
917
918   // LCOV_EXCL_BR_START 5: mmap's error case.
919   if (BkupMakeBkupPathToNvId(query_result->category_name, hdr->item_name, &full_path, FILENAME_MAX) < 0) {
920   // LCOV_EXCL_BR_STOP 5: mmap's error case.
921     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
922     goto exit;  // LCOV_EXCL_LINE 5: mmap's error case.
923   }
924
925   if (hdr->offset != 0 || hdr->size != (uint32_t)query_result->size) {
926     /*
927      * partial write
928      */
929     if ((buf = BkupAnonMmap(query_result->size)) == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
930       // LCOV_EXCL_START 5: mmap's error case.
931       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
932       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
933       goto exit;
934       // LCOV_EXCL_STOP 5: mmap's error case.
935     }
936     buf_size = query_result->size;
937
938     // LCOV_EXCL_BR_START 11:unexpected branch
939     ret = BkupCheckRead(full_path, current_media, hdr->command, rcv_buf, buf, NULL, query_result, rwlock);
940     // LCOV_EXCL_BR_STOP 11:unexpected branch
941     if (ret == 0) {
942       // do nothing
943       goto exit;
944     }
945
946     header = buf;
947     memcpy(buf + hdr->offset, rcv_buf + sizeof(bkup_protocol_header_t), hdr->size);
948     BkupRwlockWrlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
949     if (BkupBufferWriteToNv(current_media, full_path, buf, query_result->size, &crc32) < 0) {
950       BkupRwlockUnlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
951       goto exit;
952     }
953     BkupRwlockUnlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
954   } else {
955     /*
956      * whole write
957      */
958     header = rcv_buf + sizeof(bkup_protocol_header_t);
959     BkupRwlockWrlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
960     if (BkupBufferWriteToNv(current_media, full_path,
961                 rcv_buf + sizeof(bkup_protocol_header_t), hdr->size, &crc32) < 0) {
962       BkupRwlockUnlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
963       goto exit;
964     }
965     BkupRwlockUnlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
966   }
967
968   // success
969   ret = hdr->size;
970
971 exit:
972   EFrameworkunifiedStatus e_status = ret < 0 ? eFrameworkunifiedStatusFail : eFrameworkunifiedStatusOK;
973   if (current_media == NVHALMEDIA_NAND) {
974     if (query_result->cache_dram == false && query_result->backup_dram == false) {
975       // LCOV_EXCL_BR_START 11:unexpected branch
976       BkupApilog(response, current_media, hdr, query_result, header, crc32, e_status);
977       // LCOV_EXCL_BR_STOP 11:unexpected branch
978     }
979     if ((*response->set_response)(response->opt, NULL, 0, e_status) < 0) {  // LCOV_EXCL_BR_LINE 4: NSFW error case
980       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
981       ret = -1;  // LCOV_EXCL_LINE 4: NSFW error case
982     }
983   } else {
984     // LCOV_EXCL_BR_START 11:unexpected branch
985     BkupApilog(response, current_media, hdr, query_result, header, crc32, e_status);
986     // LCOV_EXCL_BR_STOP 11:unexpected branch
987   }
988
989   if (buf != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
990     munmap(buf, buf_size);
991   }
992   if (full_path != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
993     munmap(full_path, FILENAME_MAX);
994   }
995   return ret;
996 }
997
998 /**
999  * fill command
1000  */
1001 static int BkupCmdFill(bkup_response_t *response, enum NvHalMedia current_media,
1002                        char *rcv_buf, bkup_query_result_t *query_result) {
1003   int ret = -1;
1004   bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1005   // LCOV_EXCL_BR_START 11:unexpected branch
1006   pthread_rwlock_t *rwlock = BkupGetRwlock(hdr->item_name, current_media, query_result);
1007   // LCOV_EXCL_BR_STOP 11:unexpected branch
1008   char *full_path = reinterpret_cast<char *>(MAP_FAILED);
1009   char *buf = reinterpret_cast<char *>(MAP_FAILED);
1010   uint32_t crc32 = 0;
1011
1012   if (response == NULL) {
1013     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "response is NULL");
1014     return -1;
1015   }
1016   if (rcv_buf == NULL) {
1017     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
1018     return -1;
1019   }
1020   if (query_result == NULL) {
1021     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
1022     return -1;
1023   }
1024   // LCOV_EXCL_BR_START 5: mmap's error case.
1025   if (BkupMakeBkupPathToNvId(query_result->category_name, hdr->item_name, &full_path, FILENAME_MAX) < 0) {
1026   // LCOV_EXCL_BR_STOP 5: mmap's error case.
1027     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1028     goto exit;  // LCOV_EXCL_LINE 5: mmap's error case.
1029   }
1030
1031   // LCOV_EXCL_BR_START 5: mmap's error case.
1032   if ((buf = BkupAnonMmap(query_result->size)) == MAP_FAILED) {
1033   // LCOV_EXCL_BR_STOP 5: mmap's error case.
1034     // LCOV_EXCL_START 5: mmap's error case.
1035     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1036     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
1037     goto exit;
1038     // LCOV_EXCL_STOP 5: mmap's error case.
1039   }
1040
1041   if (hdr->offset != 0 || hdr->size != (uint32_t)query_result->size) {
1042     /*
1043      * partial fill
1044      */
1045     // LCOV_EXCL_BR_START 11:unexpected branch
1046     ret = BkupCheckRead(full_path, current_media, hdr->command, rcv_buf, buf, NULL, query_result, rwlock);
1047     // LCOV_EXCL_BR_STOP 11:unexpected branch
1048     if (ret == 0) {
1049       // do nothing
1050       goto exit;
1051     }
1052   }
1053
1054   memset(buf + hdr->offset, hdr->fill_patern, hdr->size);
1055   BkupRwlockWrlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
1056   if (BkupBufferWriteToNv(current_media, full_path, buf, query_result->size, &crc32) < 0) {
1057     BkupRwlockUnlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
1058     goto exit;
1059   }
1060   BkupRwlockUnlock(rwlock, full_path);  // LCOV_EXCL_BR_LINE 11:unexpected branch
1061
1062   // success
1063   ret = hdr->size;
1064
1065 exit:
1066   EFrameworkunifiedStatus e_status = ret < 0 ? eFrameworkunifiedStatusFail : eFrameworkunifiedStatusOK;
1067   if (current_media == NVHALMEDIA_NAND) {
1068     if (query_result->cache_dram == false && query_result->backup_dram == false) {
1069       // LCOV_EXCL_BR_START 11:unexpected branch
1070       BkupApilog(response, current_media, hdr, query_result, buf, crc32, e_status);
1071       // LCOV_EXCL_BR_STOP 11:unexpected branch
1072     }
1073     if ((*response->set_response)(response->opt, NULL, 0, e_status) < 0) {  // LCOV_EXCL_BR_LINE 4: NSFW error case
1074       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1075       ret = -1;  // LCOV_EXCL_LINE 4: NSFW error case
1076     }
1077   } else {
1078     // LCOV_EXCL_BR_START 11:unexpected branch
1079     BkupApilog(response, current_media, hdr, query_result, buf, crc32, e_status);
1080     // LCOV_EXCL_BR_STOP 11:unexpected branch
1081   }
1082
1083   if (buf != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
1084     munmap(buf, query_result->size);
1085   }
1086   if (full_path != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
1087     munmap(full_path, FILENAME_MAX);
1088   }
1089   return ret;
1090 }
1091
1092 /**
1093  * copy command
1094  */
1095 static int BkupCmdCopy(bkup_response_t *response, enum NvHalMedia current_media,
1096                        char *rcv_buf, bkup_query_result_t *query_result) {
1097   if (rcv_buf == NULL) {
1098     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
1099     return -1;
1100   }
1101   if (query_result == NULL) {
1102     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
1103     return -1;
1104   }
1105   bkup_protocol_header_t *hdr =
1106       reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1107   pthread_rwlock_t *src_rwlock =
1108       BkupGetRwlock(hdr->item_name, hdr->src_media, query_result);
1109   pthread_rwlock_t *dst_rwlock =
1110       BkupGetRwlock(hdr->item_name, current_media, query_result);
1111
1112   int ret = BkupMediaCopyFromNv(query_result->category_name, hdr->item_name, hdr->src_media, current_media, src_rwlock,
1113                         dst_rwlock);
1114
1115   if (response == NULL) {
1116     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "response is NULL");
1117     return -1;
1118   }
1119
1120   if (current_media == NVHALMEDIA_NAND) {
1121     // LCOV_EXCL_BR_START 4: NSFW error case
1122     if ((*response->set_response)(response->opt, NULL, 0, ret < 0 ? eFrameworkunifiedStatusFail : eFrameworkunifiedStatusOK) < 0) {
1123     // LCOV_EXCL_BR_STOP 4: NSFW error case
1124       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1125       ret = -1;  // LCOV_EXCL_LINE 4: NSFW error case
1126     }
1127   }
1128   return ret;
1129 }
1130
1131 /**
1132  * check command
1133  */
1134 static int BkupCmdCheck(bkup_response_t *response,
1135                         enum NvHalMedia current_media, char *rcv_buf,
1136                         bkup_query_result_t *query_result) {
1137   int ret = 0;
1138   bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1139   char *full_path = reinterpret_cast<char *>(MAP_FAILED);
1140
1141   if (rcv_buf == NULL) {
1142     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
1143     return -1;
1144   }
1145   if (query_result == NULL) {
1146     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
1147     return -1;
1148   }
1149   // LCOV_EXCL_BR_START 5: mmap's error case.
1150   if (BkupMakeBkupPathToNvId(query_result->category_name, hdr->item_name, &full_path, FILENAME_MAX) < 0) {
1151   // LCOV_EXCL_BR_STOP 5: mmap's error case.
1152     // LCOV_EXCL_START 5: mmap's error case.
1153     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1154     ret = -3;
1155     goto exit;
1156     // LCOV_EXCL_STOP 5: mmap's error case.
1157   }
1158   ret = BkupCheckStatFromNv(current_media, full_path, query_result->size);  // LCOV_EXCL_BR_LINE 11:unexpected branch
1159
1160 exit:
1161   if (full_path != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
1162     munmap(full_path, FILENAME_MAX);
1163   }
1164   return ret;
1165 }
1166
1167 /**
1168  * delete command
1169  */
1170 static int BkupCmdDeleteData(bkup_response_t* const response,
1171                              const enum NvHalMedia current_media,
1172                              char* const rcv_buf,
1173                              bkup_query_result_t* const query_result) {
1174   int ret = -1;
1175   char *full_path = NULL;
1176   bkup_protocol_header_t* hdr = reinterpret_cast<bkup_protocol_header_t*>(NULL);
1177   pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>(NULL);
1178   if ((response == NULL) || (rcv_buf == NULL) || (query_result == NULL)) {  // LCOV_EXCL_BR_LINE 6:Excluded as no NULL is currently passed
1179     // LCOV_EXCL_START 6:Excluded as no NULL is currently passed
1180     AGL_ASSERT_NOT_TESTED();
1181     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Resp or Buf or Query is NULL");
1182     goto exit;
1183     // LCOV_EXCL_STOP 6:Excluded as no NULL is currently passed
1184   }
1185   hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1186   rwlock = BkupGetRwlock(hdr->item_name, current_media, query_result);
1187   if (BkupMakeBkupPathToNvId(query_result->category_name, hdr->item_name, &full_path, FILENAME_MAX) < 0) {  // LCOV_EXCL_BR_LINE 6:Excluded due to checking by upper-level function
1188     // LCOV_EXCL_START 6:Excluded due to checking by upper-level function
1189     AGL_ASSERT_NOT_TESTED();
1190     goto exit;
1191     // LCOV_EXCL_STOP 6:Excluded due to checking by upper-level function
1192   }
1193   BkupRwlockWrlock(rwlock, full_path);
1194   ret = BkupDeleteDataFromNv(current_media, full_path);
1195   if (ret != 0) {
1196     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "BkupDeleteDataFromNv Error");
1197   }
1198   BkupRwlockUnlock(rwlock, full_path);
1199
1200 exit:
1201   if ((response != NULL) && (query_result != NULL)  && (hdr != NULL)) {  // LCOV_EXCL_BR_LINE 6:Excluded as no NULL is currently passed
1202     EFrameworkunifiedStatus e_status = (ret < 0) ? eFrameworkunifiedStatusFail : eFrameworkunifiedStatusOK;
1203     if (current_media == NVHALMEDIA_NAND) {
1204       if ((query_result->cache_dram == false) && (query_result->backup_dram == false)) {
1205         BkupApilog(response, current_media, hdr, query_result, NULL, 0, e_status);
1206       }
1207       if ((*response->set_response)(response->opt, NULL, 0, e_status) < 0) {
1208         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Response Error");
1209         ret = -1;
1210       }
1211     } else {
1212       BkupApilog(response, current_media, hdr, query_result, NULL, 0, e_status);
1213     }
1214   }
1215
1216   if (full_path != NULL) {  // LCOV_EXCL_BR_LINE 6:Excluded because checked by BkupMakeBkupPathToNvId()
1217     munmap(full_path, FILENAME_MAX);
1218   }
1219   return ret;
1220 }
1221
1222 /**
1223  * common file handler
1224  */
1225 static int BkupHandlerFile(bkup_response_t *response,
1226                            enum NvHalMedia current_media, char *rcv_buf,
1227                            bkup_query_result_t *query_result) {
1228   int ret = 0;
1229   bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1230
1231   if (rcv_buf == NULL) {
1232     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
1233     return -1;
1234   }
1235
1236   switch (hdr->command) {  // LCOV_EXCL_BR_LINE 6:double check
1237     case BKUP_CMD_READ:
1238       ret = BkupCmdRead(response, current_media, rcv_buf, query_result);
1239       break;
1240
1241     case BKUP_CMD_WRITE:
1242       ret = BkupCmdWrite(response, current_media, rcv_buf, query_result);
1243       break;
1244
1245     case BKUP_CMD_FILL:
1246       ret = BkupCmdFill(response, current_media, rcv_buf, query_result);
1247       break;
1248
1249     case BKUP_CMD_COPY_INNER:
1250       ret = BkupCmdCopy(response, current_media, rcv_buf, query_result);
1251       break;
1252
1253     case BKUP_CMD_DELETE:
1254       ret = BkupCmdDeleteData(response, current_media, rcv_buf, query_result);
1255       break;
1256
1257     default:
1258       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1259       break;  // LCOV_EXCL_LINE 6:double check
1260   }
1261   return ret;
1262 }
1263
1264 /**
1265  * Main's response
1266  */
1267 static int BkupSetResponseNsfw(void *opt, void *buf, int size, EFrameworkunifiedStatus ret_value) {
1268   EFrameworkunifiedStatus e_status;
1269
1270   if (buf != NULL) {  // LCOV_EXCL_BR_LINE 6:double check
1271     // LCOV_EXCL_BR_START 4: NSFW error case
1272     if ((e_status = FrameworkunifiedSetSyncResponseData((HANDLE)opt, buf, size)) != eFrameworkunifiedStatusOK) {
1273     // LCOV_EXCL_BR_STOP 4: NSFW error case
1274       // LCOV_EXCL_START 4: NSFW error case
1275       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1276       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedSetSyncResponseData:%d", e_status);
1277       return -1;
1278       // LCOV_EXCL_STOP 4: NSFW error case
1279     }
1280   }
1281   return 0;
1282 }
1283
1284 /**
1285  * write apilog
1286  */
1287 static void BkupApilog(bkup_response_t *response, enum NvHalMedia current_media,
1288                        bkup_protocol_header_t *hdr,
1289                        bkup_query_result_t *query_result, char *buf,
1290                        uint32_t crc32, EFrameworkunifiedStatus ret_value) {
1291   const char *src;
1292   const char *cmd;
1293   char header[12] = "-- -- -- --";
1294   const char c[] = "0123456789abcdef";
1295
1296   if (response == NULL) {
1297     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "response is NULL");
1298     return;
1299   }
1300   if (hdr == NULL) {
1301     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "hdr is NULL");
1302     return;
1303   }
1304   if (query_result == NULL) {
1305     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
1306     return;
1307   }
1308   if (response->set_response == BkupSetResponseNsfw) {
1309     src = FrameworkunifiedGetMsgSrc((HANDLE)response->opt);  // LCOV_EXCL_BR_LINE 11:unexpected branch
1310   } else {
1311     // LCOV_EXCL_BR_START 11:unexpected branch
1312     src = (reinterpret_cast<bkup_itc_message_t *>(response->opt))->nand.client_name;
1313     // LCOV_EXCL_BR_STOP 11:unexpected branch
1314   }
1315
1316   switch (hdr->command) {  // LCOV_EXCL_BR_LINE 6:double check
1317     case BKUP_CMD_READ:
1318       cmd = "RD";
1319       break;
1320     case BKUP_CMD_WRITE:
1321       cmd = "WR";
1322       break;
1323     case BKUP_CMD_FILL:
1324       cmd = "FILL";
1325       break;
1326     case BKUP_CMD_READ_NUM:
1327       // LCOV_EXCL_START 6:double check
1328       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1329       cmd = "RDN";
1330       break;
1331       // LCOV_EXCL_STOP 6:double check
1332     case BKUP_CMD_SIZE_NUM:
1333       // LCOV_EXCL_START 6:double check
1334       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1335       cmd = "SZN";
1336       break;
1337       // LCOV_EXCL_STOP 6:double check
1338     case BKUP_CMD_DELETE:
1339       cmd = "DEL";
1340       break;
1341     default:
1342       // LCOV_EXCL_START 6:double check
1343       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1344       cmd = "UNKNOWN";
1345       break;
1346       // LCOV_EXCL_STOP 6:double check
1347   }
1348
1349   if (buf) {  // LCOV_EXCL_BR_LINE 6:double check
1350     int i;
1351     for (i = 0; i < 4; i++) {
1352       if (query_result->size > i) {
1353         header[i * 3] = c[*(buf + i) >> 4];
1354         header[i * 3 + 1] = c[*(buf + i) & 0xf];
1355       }
1356     }
1357   }
1358
1359   // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
1360   FRAMEWORKUNIFIEDLOG(ZONE_ALSA, "", "%s/%s/%s/o:%d/s:%d/h:%s/c:%x/r:%d", src, cmd, hdr->item_name, hdr->offset, hdr->size,
1361          header, crc32, ret_value);
1362   // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
1363 }
1364
1365 /**
1366  * Tweak command
1367  */
1368 static int BkupTweakCommand(enum NvHalMedia current_media, char **rcv_buf,
1369                             int *rcv_size, bkup_query_result_t *query_result) {
1370   int ret = -1;
1371   bkup_protocol_header_t update_hdr;
1372   bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(*rcv_buf);
1373
1374   if (current_media == NVHALMEDIA_NAND) {  // LCOV_EXCL_BR_LINE 6:current_media never will be NVHALMEDIA_NAND
1375     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1376     ret = 0;  // LCOV_EXCL_LINE 6:current_media never will be NVHALMEDIA_NAND
1377     goto exit;  // LCOV_EXCL_LINE 6:current_media never will be NVHALMEDIA_NAND
1378   }
1379   if (rcv_size == NULL) {
1380     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "rcv_size is NULL");
1381     return -1;
1382   }
1383   if (query_result == NULL) {
1384     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "query_resul is NULL");
1385     return -1;
1386   }
1387
1388   if ((hdr->command == BKUP_CMD_WRITE || hdr->command == BKUP_CMD_FILL) &&
1389       (hdr->offset != 0 || hdr->size != (uint32_t)query_result->size)) {
1390     // partial request
1391     if ((current_media == NVHALMEDIA_CACHEDRAM &&
1392          (query_result->nand || query_result->backup_dram)) ||
1393         (current_media == NVHALMEDIA_BACKUPDRAM && query_result->nand)) {
1394       memset(&update_hdr, 0, sizeof(update_hdr));
1395       BkupStrlcpy(update_hdr.item_name, hdr->item_name, sizeof(update_hdr.item_name));
1396       update_hdr.command = BKUP_CMD_COPY_INNER;
1397       update_hdr.src_media = current_media;
1398       munmap(*rcv_buf, *rcv_size);
1399       *rcv_size = 0;
1400       // LCOV_EXCL_BR_START 5: mmap's error case.
1401       if ((*rcv_buf = BkupAnonMmap(sizeof(bkup_protocol_header_t))) == MAP_FAILED) {
1402       // LCOV_EXCL_BR_STOP 5: mmap's error case.
1403         // LCOV_EXCL_START 5: mmap's error case.
1404         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1405         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
1406         goto exit;
1407         // LCOV_EXCL_STOP 5: mmap's error case.
1408       }
1409       *rcv_size = sizeof(bkup_protocol_header_t);
1410       memcpy(*rcv_buf, &update_hdr, sizeof(update_hdr));
1411     }
1412   }
1413   ret = 0;
1414
1415 exit:
1416   return ret;
1417 }
1418
1419 /**
1420  * Main callback handler
1421  */
1422 EFrameworkunifiedStatus BkupHandler(HANDLE h_app) {
1423   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
1424   uint32_t rcv_size = 0;
1425   char *rcv_buf = NULL;
1426   bkup_protocol_header_t *hdr;
1427   bkup_query_result_t query_result;
1428   bool hold_rcv_buf = false;
1429   int ret = 0;
1430   bkup_response_t response_nsfw = {BkupSetResponseNsfw, h_app};
1431
1432   if ((rcv_size = FrameworkunifiedGetMsgLength(h_app)) < sizeof(bkup_protocol_header_t)) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
1433     // LCOV_EXCL_START 4: NSFW error case.
1434     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1435     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedGetMsgLength: size fail:%d", rcv_size);
1436     e_status = eFrameworkunifiedStatusFail;
1437     goto exit;
1438     // LCOV_EXCL_STOP 4: NSFW error case.
1439   }
1440
1441   rcv_buf = reinterpret_cast<char *>(mmap(NULL, rcv_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0));
1442   if (rcv_buf == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
1443     // LCOV_EXCL_START 5: mmap's error case.
1444     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1445     rcv_size = 0;
1446     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
1447     e_status = eFrameworkunifiedStatusInvldParam;
1448     goto exit;
1449     // LCOV_EXCL_STOP 5: mmap's error case.
1450   }
1451
1452   // LCOV_EXCL_BR_START 4: NSFW error case.
1453   if ((e_status = FrameworkunifiedGetMsgDataOfSize(h_app, rcv_buf, rcv_size, eSMRRelease)) != eFrameworkunifiedStatusOK) {
1454   // LCOV_EXCL_BR_STOP 4: NSFW error case.
1455     // LCOV_EXCL_START 4: NSFW error case.
1456     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1457     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedGetMsgDataOfSize:%d", e_status);
1458     goto exit;
1459     // LCOV_EXCL_STOP 4: NSFW error case.
1460   }
1461
1462   hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1463   if (hdr->command < 0 || hdr->command >= BKUP_CMD_MAX) {  // LCOV_EXCL_BR_LINE 6:double check
1464     // LCOV_EXCL_START 6:double check
1465     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1466     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid command:%d", hdr->command);
1467     e_status = eFrameworkunifiedStatusInvldParam;
1468     goto exit;
1469     // LCOV_EXCL_STOP 6:double check
1470   }
1471
1472   if (hdr->command == BKUP_CMD_READ_NUM || hdr->command == BKUP_CMD_SIZE_NUM) {
1473     // LCOV_EXCL_BR_START 11:unexpected branch
1474     if (BkupParamGetNumid(hdr->num_id, &query_result, hdr->item_name, sizeof(hdr->item_name)) < 0) {
1475     // LCOV_EXCL_BR_STOP 11:unexpected branch
1476       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
1477       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", hdr->num_id);
1478       // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
1479       e_status = eFrameworkunifiedStatusInvldParam;
1480       goto exit;
1481     }
1482
1483     if (hdr->command == BKUP_CMD_READ_NUM) {
1484       hdr->command = BKUP_CMD_READ;
1485     }
1486   } else {
1487     if (BkupParamGet(hdr->item_name, &query_result) < 0) {  // LCOV_EXCL_BR_LINE 11:unexpected branch
1488       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
1489       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid item_name:%s", hdr->item_name != 0 ? hdr->item_name : NULL);
1490       // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
1491       e_status = eFrameworkunifiedStatusInvldParam;
1492       goto exit;
1493     }
1494   }
1495
1496   if (hdr->command == BKUP_CMD_READ || hdr->command == BKUP_CMD_WRITE || hdr->command == BKUP_CMD_FILL) {
1497     if (hdr->size == 0) {
1498       // do nothing
1499       goto exit;
1500     }
1501     if (hdr->size + hdr->offset > (uint32_t)query_result.size) {
1502       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
1503       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid size+offset:%d+%d", hdr->size, hdr->offset);
1504       // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
1505       e_status = eFrameworkunifiedStatusInvldParam;
1506       goto exit;
1507     }
1508   }
1509
1510   // LCOV_EXCL_BR_START 6: g_bkup_internal_state will not be BKUP_STATE_INIT
1511   if (g_bkup_internal_state == BKUP_STATE_INIT) {
1512   // LCOV_EXCL_BR_STOP 6: g_bkup_internal_state will not be BKUP_STATE_INIT
1513     // LCOV_EXCL_START 6: same as above
1514     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1515     e_status = eFrameworkunifiedStatusErrOther;
1516     goto exit;
1517     // LCOV_EXCL_STOP 6: same as above
1518   }
1519   // LCOV_EXCL_BR_START 6: g_bkup_internal_state will not be BKUP_STATE_TERM
1520   if (g_bkup_internal_state == BKUP_STATE_TERM) {
1521   // LCOV_EXCL_BR_STOP 6: g_bkup_internal_state will not be BKUP_STATE_TERM
1522     // LCOV_EXCL_START 6: same as above
1523     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1524     e_status = eFrameworkunifiedStatusExit;
1525     goto exit;
1526     // LCOV_EXCL_STOP 6: same as above
1527   }
1528
1529   if (hdr->command == BKUP_CMD_SIZE || hdr->command == BKUP_CMD_SIZE_NUM) {
1530     // LCOV_EXCL_BR_START 11:unexpected branch
1531     BkupSetResponseNsfw(h_app, &query_result.size, sizeof(query_result.size), eFrameworkunifiedStatusOK);
1532     // LCOV_EXCL_BR_STOP 11:unexpected branch
1533   } else if (hdr->command == BKUP_CMD_CHECK) {
1534     if (query_result.nand) {
1535       // LCOV_EXCL_BR_START 11:unexpected branch
1536       ret = BkupCmdCheck(&response_nsfw, NVHALMEDIA_NAND, rcv_buf, &query_result);
1537       // LCOV_EXCL_BR_STOP 11:unexpected branch
1538     } else if (query_result.backup_dram) {
1539       // LCOV_EXCL_BR_START 11:unexpected branch
1540       ret = BkupCmdCheck(&response_nsfw, NVHALMEDIA_BACKUPDRAM, rcv_buf, &query_result);
1541       // LCOV_EXCL_BR_STOP 11:unexpected branch
1542     } else if (query_result.cache_dram) {
1543       // LCOV_EXCL_BR_START 11:unexpected branch
1544       ret = BkupCmdCheck(&response_nsfw, NVHALMEDIA_CACHEDRAM, rcv_buf, &query_result);
1545       // LCOV_EXCL_BR_STOP 11:unexpected branch
1546     } else {
1547       ret = -3;
1548     }
1549
1550     if (ret == -1) {
1551       // not exists
1552       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
1553       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Not exists:%s", hdr->item_name != 0 ? hdr->item_name : NULL);
1554       // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
1555       e_status = eFrameworkunifiedStatusFileLoadError;
1556     } else if (ret == -2) {
1557       // size error
1558       e_status = eFrameworkunifiedStatusAccessError;
1559     } else if (ret == -3) {
1560       e_status = eFrameworkunifiedStatusFail;
1561     }
1562   } else {
1563     /*
1564      * Cache DRAM
1565      */
1566     if (query_result.cache_dram) {
1567       // LCOV_EXCL_BR_START 11:unexpected branch
1568       ret = BkupHandlerFile(&response_nsfw, NVHALMEDIA_CACHEDRAM, rcv_buf, &query_result);
1569       // LCOV_EXCL_BR_STOP 11:unexpected branch
1570       if (ret > 0) {
1571         if (hdr->command == BKUP_CMD_READ) {
1572           goto exit;
1573         }
1574         // LCOV_EXCL_BR_START 5: mmap's error case.
1575         if (BkupTweakCommand(NVHALMEDIA_CACHEDRAM, &rcv_buf,
1576                              reinterpret_cast<int *>(&rcv_size),
1577                              &query_result) < 0) {
1578         // LCOV_EXCL_BR_STOP 5: mmap's error case.
1579           // LCOV_EXCL_START 5: mmap's error case.
1580           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1581           e_status = eFrameworkunifiedStatusFail;
1582           goto exit;
1583           // LCOV_EXCL_STOP 5: mmap's error case.
1584         }
1585         hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1586       }
1587     }
1588
1589     /*
1590      * Backup DRAM
1591      */
1592     if (query_result.backup_dram) {
1593       // LCOV_EXCL_BR_START 11:unexpected branch
1594       ret = BkupHandlerFile(&response_nsfw, NVHALMEDIA_BACKUPDRAM, rcv_buf, &query_result);
1595       // LCOV_EXCL_BR_STOP 11:unexpected branch
1596       if (ret > 0) {
1597         if (hdr->command == BKUP_CMD_READ) {
1598           goto exit;
1599         }
1600         // LCOV_EXCL_BR_START 5: mmap's error case.
1601         if (BkupTweakCommand(NVHALMEDIA_BACKUPDRAM, &rcv_buf,
1602                              reinterpret_cast<int *>(&rcv_size),
1603                              &query_result) < 0) {
1604         // LCOV_EXCL_BR_STOP 5: mmap's error case.
1605           // LCOV_EXCL_START 5: mmap's error case.
1606           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1607           e_status = eFrameworkunifiedStatusFail;
1608           goto exit;
1609           // LCOV_EXCL_STOP 5: mmap's error case.
1610         }
1611         hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1612       }
1613     }
1614
1615     /*
1616      * NAND
1617      */
1618     if (query_result.nand) {
1619       bkup_itc_message_t snd_msg;
1620
1621       if ((hdr->command == BKUP_CMD_READ) || (hdr->command == BKUP_CMD_DELETE) || (query_result.sync) || (ret == 0)) {
1622         /*
1623          * operation offload
1624          */
1625         const char *client_name;
1626         char system_info[MAX_SYS_INFO_SIZE];
1627
1628         if (g_bkup_nand_mqd == -1) {
1629           // LCOV_EXCL_BR_START 5: mq_open's error case.
1630           if ((g_bkup_nand_mqd = BkupItcMqopen("/" BKUP_THREAD_NAND, O_WRONLY | O_CREAT | O_NONBLOCK | O_CLOEXEC)) < 0) {
1631           // LCOV_EXCL_BR_STOP 5: mq_open's error case.
1632             // LCOV_EXCL_START 5: mq_open's error case.
1633             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1634             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
1635             exit(EXIT_FAILURE);
1636             // LCOV_EXCL_STOP 5: mq_open's error case.
1637           }
1638         }
1639
1640         if ((client_name = FrameworkunifiedGetMsgSrc(h_app)) == NULL) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
1641           // LCOV_EXCL_START 4: NSFW error case.
1642           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1643           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "invalid client_name");
1644           e_status = eFrameworkunifiedStatusFail;
1645           goto exit;
1646           // LCOV_EXCL_STOP 4: NSFW error case.
1647         }
1648         FrameworkunifiedGetSystemInfo(h_app, system_info);  // LCOV_EXCL_BR_LINE 11:unexpected branch.
1649
1650         memset(&snd_msg, 0, sizeof(snd_msg));
1651         BkupStrlcpy(snd_msg.nand.client_name, client_name, sizeof(snd_msg.nand.client_name));
1652         snd_msg.nand.seq_id = static_cast<uint32_t>(strtoul(system_info, NULL, 16));
1653         snd_msg.nand.rcv_buf = rcv_buf;
1654         snd_msg.nand.rcv_size = rcv_size;
1655
1656         // LCOV_EXCL_BR_START 5: mq_send's error case.
1657         if (mq_send(g_bkup_nand_mqd, reinterpret_cast<char *>(&snd_msg), sizeof(snd_msg), 0) < 0) {
1658         // LCOV_EXCL_BR_STOP 5: mq_send's error case.
1659           // LCOV_EXCL_START 5: mq_send's error case.
1660           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1661           int save_errno = errno;
1662           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_send:%s", strerror(errno));
1663           e_status = eFrameworkunifiedStatusFail;
1664           if (save_errno == EAGAIN) {
1665             kill(g_bkup_nand_tid, SIGABRT);
1666           }
1667           goto exit;
1668           // LCOV_EXCL_STOP 5: mq_send's error case.
1669         }
1670         FrameworkunifiedSetDeferredSyncResponse(h_app);  // LCOV_EXCL_BR_LINE 11:unexpected branch.
1671         hold_rcv_buf = true;
1672       } else {
1673         /*
1674          * delay write
1675          */
1676         if (g_bkup_delay_mqd == -1) {
1677           // LCOV_EXCL_BR_START 5: mq_open's error case.
1678           if ((g_bkup_delay_mqd = BkupItcMqopen("/" BKUP_THREAD_DELAY, O_WRONLY | O_CREAT | O_NONBLOCK | O_CLOEXEC)) < 0) {
1679           // LCOV_EXCL_BR_STOP 5: mq_open's error case.
1680             // LCOV_EXCL_START 5: mq_open's error case.
1681             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1682             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
1683             exit(EXIT_FAILURE);
1684             // LCOV_EXCL_STOP 5: mq_open's error case.
1685           }
1686         }
1687
1688         memset(&snd_msg, 0, sizeof(snd_msg));
1689
1690 //        snd_msg.delay.terminate = false;
1691         snd_msg.delay.cmd = BKUP_DELAY_CMD_REGIST;
1692
1693         BkupStrlcpy(snd_msg.delay.item_name, hdr->item_name, sizeof(snd_msg.delay.item_name));
1694
1695         // LCOV_EXCL_BR_START 5: mq_send's error case.
1696         if (mq_send(g_bkup_delay_mqd, reinterpret_cast<char *>(&snd_msg), sizeof(snd_msg), 0) < 0) {
1697         // LCOV_EXCL_BR_STOP 5: mq_send's error case.
1698           // LCOV_EXCL_START 5: mq_send's error case.
1699           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1700           int save_errno = errno;
1701           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_send:%s", strerror(errno));
1702           e_status = eFrameworkunifiedStatusFail;
1703           if (save_errno == EAGAIN) {
1704             kill(g_bkup_delay_tid, SIGABRT);
1705           }
1706           goto exit;
1707           // LCOV_EXCL_STOP 5: mq_send's error case.
1708         }
1709       }
1710     }
1711   }
1712
1713 exit:
1714   if (hold_rcv_buf == false && rcv_buf != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case
1715     munmap(rcv_buf, rcv_size);
1716   }
1717   return e_status;
1718 }
1719
1720
1721 /**
1722  * Power callback handler
1723  */
1724 EFrameworkunifiedStatus bkup_power_handler(HANDLE hApp) {
1725   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1726   uint32_t rcv_size = 0;
1727   T_SS_SM_UserModeOnOffNotification_StructType power_status;
1728
1729   if ((rcv_size = FrameworkunifiedGetMsgLength(hApp)) != sizeof(power_status)) {  // LCOV_EXCL_BR_START 4: NSFW error case
1730     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1731     // LCOV_EXCL_START 4: NSFW error case
1732     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedGetMsgLength: size fail:%d", rcv_size);
1733     eStatus = eFrameworkunifiedStatusFail;
1734     goto exit;
1735     // LCOV_EXCL_STOP 4: NSFW error case
1736   }
1737
1738   // LCOV_EXCL_BR_START 4: NSFW error case
1739   if ((eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, &power_status, rcv_size, eSMRRelease)) != eFrameworkunifiedStatusOK) {
1740   // LCOV_EXCL_BR_STOP 4: NSFW error case
1741     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1742     // LCOV_EXCL_START 4: NSFW error case
1743     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedGetMsgDataOfSize:%d", eStatus);
1744     goto exit;
1745     // LCOV_EXCL_STOP 4: NSFW error case
1746   }
1747
1748   if (g_bkup_internal_state != BKUP_STATE_NORMAL) {  // LCOV_EXCL_BR_LINE 6:double check
1749     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1750     // LCOV_EXCL_START 6:double check
1751     FRAMEWORKUNIFIEDLOG(ZONE_WARN, __func__, "Invalid state:%d", g_bkup_internal_state);
1752     goto exit;
1753     // LCOV_EXCL_STOP 6:double check
1754   }
1755
1756   if (power_status.isUserModeOn == FALSE) {
1757     bkup_itc_message_t snd_msg;
1758
1759     if (g_bkup_delay_mqd == -1) {
1760       // LCOV_EXCL_BR_START 5: mq_open's error case.
1761       if ((g_bkup_delay_mqd = BkupItcMqopen("/" BKUP_THREAD_DELAY, O_WRONLY|O_CREAT|O_NONBLOCK|O_CLOEXEC)) < 0) {
1762       // LCOV_EXCL_BR_STOP 5: mq_open's error case.
1763         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1764         // LCOV_EXCL_START 5: mq_open's error case.
1765         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
1766         eStatus = eFrameworkunifiedStatusFail;
1767         goto exit;
1768         // LCOV_EXCL_STOP 5: mq_open's error case.
1769       }
1770     }
1771
1772     memset(&snd_msg, 0, sizeof(snd_msg));
1773     snd_msg.delay.cmd = BKUP_DELAY_CMD_PRE_ACCOFF;
1774
1775     // LCOV_EXCL_BR_START 5: mq_send's error case.
1776     if (mq_send(g_bkup_delay_mqd, (char *)&snd_msg, sizeof(snd_msg), 0) < 0) {
1777     // LCOV_EXCL_BR_START 5: mq_send's error case.
1778       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1779       // LCOV_EXCL_START 5: mq_send's error case.
1780       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_send:%s", strerror(errno));
1781       eStatus = eFrameworkunifiedStatusFail;
1782       goto exit;
1783       // LCOV_EXCL_STOP 5: mq_send's error case.
1784     }
1785   }
1786
1787 exit:
1788   return eStatus;
1789 }
1790
1791
1792 /**
1793  * thread's response
1794  */
1795 static int BkupSetResponseThread(void *opt, void *buf, int size, EFrameworkunifiedStatus ret_value) {
1796   HANDLE response_mq;
1797   char invoker_name[MAX_QUEUE_NAME_SIZE];
1798   if (opt == NULL) {
1799     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "opt is NULL");
1800     return -1;
1801   }
1802   bkup_itc_message_t *msg = reinterpret_cast<bkup_itc_message_t *>(opt);
1803   EFrameworkunifiedStatus e_status;
1804   // LCOV_EXCL_BR_START 11:unexpected branch.
1805   McCreateInvokerName(msg->nand.client_name, 0, invoker_name, sizeof(invoker_name));
1806   // LCOV_EXCL_BR_STOP 11:unexpected branch.
1807
1808   if (g_bkup_response_mq == NULL) {
1809     g_bkup_response_mq = new g_bkup_response_mq_t;  // LCOV_EXCL_BR_LINE 11:except,C++ operator
1810   }
1811   g_bkup_response_mq_t::iterator mq_it = g_bkup_response_mq->find(invoker_name);  // LCOV_EXCL_BR_LINE 11:except,C++ STL
1812   if (mq_it != g_bkup_response_mq->end()) {  // LCOV_EXCL_BR_LINE 11:except,C++ STL
1813     response_mq = mq_it->second;  // LCOV_EXCL_BR_LINE 11:except,C++ STL
1814   } else {
1815     if ((response_mq = McOpenSyncSender(invoker_name)) == NULL) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
1816       // LCOV_EXCL_START 4: NSFW error case.
1817       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1818       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mcOpenSyncSender");
1819       return -1;
1820       // LCOV_EXCL_STOP 4: NSFW error case.
1821     }
1822     g_bkup_response_mq->insert(std::make_pair(invoker_name, response_mq));  // LCOV_EXCL_BR_LINE 11:except,C++ STL
1823   }
1824
1825   // LCOV_EXCL_BR_START 11:unexpected branch.
1826   e_status = McSendSyncResponse(response_mq, MN_NS_BACKUPMGR, BACKUP_CID, msg->nand.seq_id, ret_value, size, buf);
1827   // LCOV_EXCL_BR_STOP 11:unexpected branch.
1828   return (e_status == eFrameworkunifiedStatusOK) ? 0 : -1;  // LCOV_EXCL_BR_LINE 11:unexpected branch
1829 }
1830
1831 /**
1832  * NAND Thread
1833  */
1834 void *BkupNandThread(void *arg) {
1835   mqd_t mqd;
1836   struct pollfd fds[1];
1837   bkup_itc_message_t rcv_msg;
1838   bkup_protocol_header_t *hdr;
1839   bkup_query_result_t query_result;
1840   bkup_response_t response_thread = {BkupSetResponseThread, NULL};
1841
1842   signal_handler p_signal = SignalHandlerFuncForMsgHandleThread;
1843   signal(SIGUSR1, p_signal);
1844   g_nand_thread_id = pthread_self();
1845
1846   if (prctl(PR_SET_NAME, BKUP_THREAD_NAND) < 0) {  // LCOV_EXCL_BR_LINE 5: prctl's error case.
1847     // LCOV_EXCL_START 5: prctl's error case.
1848     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1849     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "prctl(PR_SET_NAME):%s", strerror(errno));
1850     exit(EXIT_FAILURE);
1851     // LCOV_EXCL_STOP 5: prctl's error case.
1852   }
1853
1854   // LCOV_EXCL_BR_START 5: mq_open's error case.
1855   if ((mqd = BkupItcMqopen("/" BKUP_THREAD_NAND, O_RDONLY | O_CREAT | O_CLOEXEC)) < -1) {
1856   // LCOV_EXCL_BR_STOP 5: mq_open's error case.
1857     // LCOV_EXCL_START 5: mq_open's error case.
1858     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1859     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
1860     exit(EXIT_FAILURE);
1861     // LCOV_EXCL_STOP 5: mq_open's error case.
1862   }
1863   g_bkup_nand_tid = static_cast<int>(syscall(__NR_gettid));
1864
1865   fds[0].fd = mqd;
1866   fds[0].events = POLLIN;
1867
1868   while (1) {
1869     int ret = poll(fds, sizeof(fds) / sizeof(struct pollfd), -1);  // LCOV_EXCL_BR_LINE 11:unexpected branch
1870     if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: poll's error case.
1871       // LCOV_EXCL_START 5: poll's error case.
1872       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1873       if (errno == EINTR) {
1874         continue;
1875       }
1876       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "poll errno:%s", strerror(errno));
1877       // LCOV_EXCL_STOP 5: poll's error case.
1878     } else {
1879       if ((fds[0].revents & POLLIN) != 0) {
1880         // LCOV_EXCL_BR_START 5: mq_receive's error case.
1881         if (mq_receive(mqd, reinterpret_cast<char *>(&rcv_msg), sizeof(rcv_msg), NULL) < 0) {
1882         // LCOV_EXCL_BR_STOP 5: mq_receive's error case.
1883           // LCOV_EXCL_START 5: mq_receive's error case.
1884           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1885           if (errno != EINTR) {
1886             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_receive:%s", strerror(errno));
1887           }
1888           continue;
1889           // LCOV_EXCL_STOP 5: mq_receive's error case.
1890         }
1891         hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_msg.nand.rcv_buf);
1892         BkupParamGet(hdr->item_name, &query_result);  // LCOV_EXCL_BR_LINE 11:unexpected branch
1893         response_thread.opt = &rcv_msg;
1894         // LCOV_EXCL_BR_START 11:unexpected branch
1895         ret = BkupHandlerFile(&response_thread, NVHALMEDIA_NAND, rcv_msg.nand.rcv_buf, &query_result);
1896         // LCOV_EXCL_BR_STOP 11:unexpected branch
1897
1898         munmap(rcv_msg.nand.rcv_buf, rcv_msg.nand.rcv_size);
1899       }
1900     }
1901   }
1902
1903   return NULL;
1904 }
1905
1906 /*
1907  * delay backup thread
1908  */
1909 typedef struct {
1910   int fd;
1911   std::string name;
1912 } bkup_delay_t;  // LCOV_EXCL_BR_LINE 11:except,C++ STL
1913
1914 static int BkupDelayTimerAdd(int efd, const char *item_name, time_t backup_cycle) {
1915   int ret = -1;
1916   struct epoll_event ev;
1917   bkup_delay_t *timer_ev = new bkup_delay_t;  // LCOV_EXCL_BR_LINE 11:except,C++ operator
1918   struct itimerspec itime;
1919
1920   if ((timer_ev->fd = timerfd_create(CLOCK_MONOTONIC, 0)) < 0) {  // LCOV_EXCL_BR_LINE 5: timerfd_create's error case.
1921     // LCOV_EXCL_START 5: timerfd_create's error case.
1922     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1923     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "timerfd_create:%s", strerror(errno));
1924     goto exit;
1925     // LCOV_EXCL_STOP 5: timerfd_create's error case.
1926   }
1927   timer_ev->name = item_name;  // LCOV_EXCL_BR_LINE 11:except,C++ STL
1928
1929   ev.events = EPOLLIN;
1930   ev.data.ptr = timer_ev;
1931   if (epoll_ctl(efd, EPOLL_CTL_ADD, timer_ev->fd, &ev) < 0) {  // LCOV_EXCL_BR_LINE 5: epoll_ctl's error case.
1932     // LCOV_EXCL_START 5: epoll_ctl's error case.
1933     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1934     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "epoll_ctl:%s", strerror(errno));
1935     goto exit;
1936     // LCOV_EXCL_STOP 5: epoll_ctl's error case.
1937   }
1938
1939   memset(&itime, 0, sizeof(itime));
1940   itime.it_value.tv_sec = backup_cycle;
1941   itime.it_interval.tv_sec = backup_cycle;
1942
1943   if (timerfd_settime(timer_ev->fd, 0, &itime, NULL) < 0) {  // LCOV_EXCL_BR_LINE 5: timerfd_settime's error case.
1944     // LCOV_EXCL_START 5: timerfd_settime's error case.
1945     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1946     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "timerfd_settime:%s", strerror(errno));
1947     epoll_ctl(efd, EPOLL_CTL_DEL, timer_ev->fd, &ev);
1948     goto exit;
1949     // LCOV_EXCL_STOP 5: timerfd_settime's error case.
1950   }
1951   ret = 0;
1952
1953 exit:
1954   if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: C API's error case.
1955     // LCOV_EXCL_START 5: C API's error case.
1956     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1957     if (timer_ev->fd >= 0) {
1958       if (close(timer_ev->fd) < 0) {
1959         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "timerfd_settime:%s", strerror(errno));
1960       }
1961     }
1962     delete timer_ev;
1963   }
1964   // LCOV_EXCL_STOP 5: C API's error case.
1965   return ret;
1966 }
1967
1968 static uint64_t *g_bkup_syncreq;
1969 static pthread_rwlock_t g_syncreq_rwlock = PTHREAD_RWLOCK_INITIALIZER;
1970 #define BKUP_SYNCREQ_SIZE (4096)
1971 #define BKUP_SYNCREQ_ID_MAX (BKUP_SYNCREQ_SIZE * 8 - 1)
1972 #define BKUP_SYNCREQ_NAME "syncreq"
1973
1974 static int BkupSyncreqInit(void) {
1975   int ret = -1;
1976   char *map;
1977
1978   map = reinterpret_cast<char *>(mmap(NULL, BKUP_SYNCREQ_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0));
1979   if (map == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
1980     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1981     // LCOV_EXCL_START 5: mmap's error case.
1982     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap failed");
1983     goto exit;
1984     // LCOV_EXCL_STOP 5: mmap's error case.
1985   }
1986   g_bkup_syncreq = reinterpret_cast<uint64_t *>(map);
1987   memset(g_bkup_syncreq, 0, BKUP_SYNCREQ_SIZE);
1988
1989   pthread_rwlock_wrlock(&g_syncreq_rwlock);
1990   BkupBufferWriteToNv(NVHALMEDIA_BACKUPDRAM, BKUP_SYNCREQ_NAME,
1991                       reinterpret_cast<char *>(g_bkup_syncreq),
1992                       BKUP_SYNCREQ_SIZE, NULL);
1993   pthread_rwlock_unlock(&g_syncreq_rwlock);
1994   ret = 0;
1995
1996 exit:
1997   return ret;
1998 }
1999
2000 static void BkupSyncreqTerm(void) {
2001   if (g_bkup_syncreq != NULL) {  // LCOV_EXCL_BR_LINE 6:g_bkup_syncreq will not be NULL
2002     munmap(g_bkup_syncreq, BKUP_SYNCREQ_SIZE);
2003     g_bkup_syncreq = NULL;
2004   }
2005 }
2006
2007 static int BkupSyncreqSet(int id) {
2008   if (g_bkup_syncreq == NULL) {  // LCOV_EXCL_BR_LINE 6:g_bkup_syncreq will not be NULL
2009     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2010     return -1;  // LCOV_EXCL_LINE 5: mmap's error case.
2011   }
2012   if (id > BKUP_SYNCREQ_ID_MAX) {
2013     return -1;
2014   }
2015   if (!(*(g_bkup_syncreq + id / 64) & (1ULL << (id % 64)))) {
2016     *(g_bkup_syncreq + id / 64) |= (1ULL << (id % 64));
2017     pthread_rwlock_wrlock(&g_syncreq_rwlock);
2018     BkupBufferWriteToNv(NVHALMEDIA_BACKUPDRAM, BKUP_SYNCREQ_NAME,
2019                         reinterpret_cast<char *>(g_bkup_syncreq),
2020                         BKUP_SYNCREQ_SIZE, NULL);
2021     pthread_rwlock_unlock(&g_syncreq_rwlock);
2022   }
2023   return 0;
2024 }
2025
2026 static int BkupSyncreqClear(int id) {
2027   if (g_bkup_syncreq == NULL) {  // LCOV_EXCL_BR_LINE 6:g_bkup_syncreq will not be NULL
2028     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2029     return -1;  // LCOV_EXCL_LINE 5: mmap's error case.
2030   }
2031   if (id > BKUP_SYNCREQ_ID_MAX) {
2032     return -1;
2033   }
2034   if (*(g_bkup_syncreq + id / 64) & (1ULL << (id % 64))) {
2035     *(g_bkup_syncreq + id / 64) &= ~(1ULL << (id % 64));
2036     pthread_rwlock_wrlock(&g_syncreq_rwlock);
2037     BkupBufferWriteToNv(NVHALMEDIA_BACKUPDRAM, BKUP_SYNCREQ_NAME,
2038                         reinterpret_cast<char *>(g_bkup_syncreq),
2039                         BKUP_SYNCREQ_SIZE, NULL);
2040     pthread_rwlock_unlock(&g_syncreq_rwlock);
2041   }
2042
2043   return 0;
2044 }
2045
2046 static inline int BkupPopcnt64(uint64_t x) {
2047   uint64_t n;
2048
2049   n = (x >> 1) & 0x7777777777777777ULL;
2050   x = x - n;
2051   n = (n >> 1) & 0x7777777777777777ULL;
2052   x = x - n;
2053   n = (n >> 1) & 0x7777777777777777ULL;
2054   x = x - n;
2055   x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
2056   x = x * 0x0101010101010101ULL;
2057   return static_cast<int>(x >> 56);
2058 }
2059
2060 static int BkupSyncreqSearch(void) {
2061   int i;
2062
2063   if (g_bkup_syncreq == NULL) {  // LCOV_EXCL_BR_LINE 6:g_bkup_syncreq will not be NULL
2064     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2065     return -1;  // LCOV_EXCL_LINE 5: mmap's error case.
2066   }
2067
2068   for (i = 0; i < static_cast<int>(BKUP_SYNCREQ_SIZE / sizeof(uint64_t)); i++) {
2069     if (*(g_bkup_syncreq + i) != 0) {
2070       uint64_t bits, mrb1;  // most right bit 1
2071       bits = *(g_bkup_syncreq + i);
2072       mrb1 = bits & (-bits);
2073       return i * 64 + BkupPopcnt64(mrb1 - 1);
2074     }
2075   }
2076
2077   return -1;
2078 }
2079
2080 static void BkupSyncreqDone(bool skip_cache) {
2081   int id;
2082   uint64_t start, end;
2083
2084   start = BkupTimerReal();
2085
2086   while ((id = BkupSyncreqSearch()) >= 0) {
2087     char item_name[64];
2088     bkup_query_result_t query_result;
2089
2090     if (BkupParamGetNumid(id, &query_result, item_name, sizeof(item_name)) < 0) {
2091       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2092       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", id);
2093       // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2094     } else {
2095       if (query_result.cache_dram) {
2096         if (!skip_cache) {
2097           BkupMediaCopyFromNv(query_result.category_name, item_name,
2098                               NVHALMEDIA_CACHEDRAM, NVHALMEDIA_NAND, NULL,
2099                               NULL);
2100         }
2101       } else if (query_result.backup_dram) {
2102         // LCOV_EXCL_BR_START 11:unexpected branch
2103         BkupMediaCopyFromNv(query_result.category_name, item_name,
2104                             NVHALMEDIA_BACKUPDRAM, NVHALMEDIA_NAND, NULL, NULL);
2105         // LCOV_EXCL_BR_STOP 11:unexpected branch
2106       }
2107     }
2108
2109     BkupSyncreqClear(id);  // LCOV_EXCL_BR_LINE 11:unexpected branch
2110
2111   }
2112
2113   end = BkupTimerReal();
2114   // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2115   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "spent:%" PRIu64 ".%06" PRIu64 "ms",
2116          static_cast<uint64_t>((end - start) / 1000000),
2117          static_cast<uint64_t>((end - start) % 1000000));
2118   // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2119 }
2120
2121 void BkupSyncreqRestart(void) {
2122   char *map;
2123
2124   map = reinterpret_cast<char *>(mmap(NULL, BKUP_SYNCREQ_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0));
2125   if (map == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: mmap's error case.
2126     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2127     // LCOV_EXCL_START 5: mmap's error case.
2128     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap failed");
2129     goto exit;
2130     // LCOV_EXCL_STOP 5: mmap's error case.
2131   }
2132   g_bkup_syncreq = reinterpret_cast<uint64_t *>(map);
2133
2134   if (BkupBufferReadFromNv(NVHALMEDIA_BACKUPDRAM, BKUP_SYNCREQ_NAME,
2135                            reinterpret_cast<char *>(g_bkup_syncreq),
2136                            BKUP_SYNCREQ_SIZE, NULL) < 0) {
2137     goto exit;
2138   }
2139   BkupSyncreqDone(true);
2140   BkupSyncreqTerm();
2141
2142 exit:
2143   return;
2144 }
2145
2146 void *BkupDelayThread(void *arg) {
2147   mqd_t mqd;
2148   int efd;
2149   struct epoll_event ev;
2150   bkup_delay_t *mq_ev;
2151   bkup_itc_message_t rcv_msg;
2152   std::map<std::string, int> delay_req;  // LCOV_EXCL_BR_LINE 11:except,C++ STL
2153
2154   signal_handler p_signal = SignalHandlerFuncForMsgHandleThread;
2155   signal(SIGUSR1, p_signal);
2156   g_delay_thread_id = pthread_self();
2157
2158
2159   if (prctl(PR_SET_NAME, BKUP_THREAD_DELAY) < 0) {  // LCOV_EXCL_BR_LINE 5: prctl's error case.
2160     // LCOV_EXCL_START 5: prctl's error case.
2161     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2162     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "prctl(PR_SET_NAME):%s", strerror(errno));
2163     exit(EXIT_FAILURE);
2164     // LCOV_EXCL_STOP 5: prctl's error case.
2165   }
2166
2167   // LCOV_EXCL_BR_START 5: mq_open's error case.
2168   if ((mqd = BkupItcMqopen("/" BKUP_THREAD_DELAY, O_RDONLY | O_CREAT | O_CLOEXEC)) < 0) {
2169   // LCOV_EXCL_BR_STOP 5: mq_open's error case.
2170     // LCOV_EXCL_START 5: mq_open's error case.
2171     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2172     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
2173     exit(EXIT_FAILURE);
2174     // LCOV_EXCL_STOP 5: mq_open's error case.
2175   }
2176
2177   if ((efd = epoll_create(MAX_FD_EPOLL)) < 0) {  // LCOV_EXCL_BR_LINE 5: epoll_create's error case.
2178     // LCOV_EXCL_START 5: epoll_create's error case.
2179     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2180     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "epoll_create:%s", strerror(errno));
2181     exit(EXIT_FAILURE);
2182     // LCOV_EXCL_STOP 5: epoll_create's error case.
2183   }
2184   g_bkup_delay_tid = static_cast<int>(syscall(__NR_gettid));
2185
2186   mq_ev = new bkup_delay_t;  // LCOV_EXCL_BR_LINE 11:except,C++ operator
2187   mq_ev->fd = static_cast<int>(mqd);
2188   mq_ev->name = "mq";  // LCOV_EXCL_BR_LINE 11:except,C++ STL
2189
2190   ev.events = EPOLLIN;
2191   ev.data.ptr = mq_ev;
2192   // LCOV_EXCL_BR_START 5: epoll_ctl's error case.
2193   if (epoll_ctl(efd, EPOLL_CTL_ADD, static_cast<int>(mqd), &ev) < 0) {
2194   // LCOV_EXCL_BR_STOP 5: epoll_ctl's error case.
2195     // LCOV_EXCL_START 5: epoll_ctl's error case.
2196     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2197     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "epoll_ctl:%s", strerror(errno));
2198     exit(EXIT_FAILURE);
2199     // LCOV_EXCL_STOP 5: epoll_ctl's error case.
2200   }
2201
2202   while (1) {
2203     struct epoll_event events[MAX_FD_EPOLL];
2204     int nfds;
2205
2206     nfds = epoll_wait(efd, events, MAX_FD_EPOLL, -1);  // LCOV_EXCL_BR_LINE 11:unexpected branch
2207     if (nfds < 0) {  // LCOV_EXCL_BR_LINE 5: epoll_wait's error case.
2208       // LCOV_EXCL_START 5: epoll_wait's error case.
2209       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2210       if (errno != EINTR) {
2211         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "epoll_wait:%s", strerror(errno));
2212       }
2213       continue;
2214       // LCOV_EXCL_STOP 5: epoll_wait's error case.
2215     }
2216
2217     for (int i = 0; i < nfds; i++) {
2218       bkup_delay_t *cur_ev = reinterpret_cast<bkup_delay_t *>(events[i].data.ptr);
2219
2220       if (cur_ev->fd == mqd) {
2221         /*
2222          * mq
2223          */
2224         // LCOV_EXCL_BR_START 5: mq_receive's error case.
2225         if (mq_receive(mqd, reinterpret_cast<char *>(&rcv_msg), sizeof(rcv_msg), NULL) < 0) {
2226         // LCOV_EXCL_BR_STOP 5: mq_receive's error case.
2227           // LCOV_EXCL_START 5: mq_receive's error case.
2228           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2229           if (errno != EINTR) {
2230             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_receive:%s", strerror(errno));
2231           }
2232           continue;
2233           // LCOV_EXCL_STOP 5: mq_receive's error case.
2234         }
2235
2236 #if 0
2237         if (rcv_msg.delay.terminate) {
2238           // terminate request
2239           // shutdown sync
2240           BkupSyncreqDone(false);  // LCOV_EXCL_BR_LINE 11:unexpected branch
2241
2242           for (std::map<std::string, int>::iterator i = delay_req.begin(); i != delay_req.end(); ++i) {
2243             i->second = 0;  // LCOV_EXCL_BR_LINE 11:except,C++ STL
2244           }
2245
2246           // terminate finish response
2247           int64_t term_event = 1;
2248           // LCOV_EXCL_BR_START 5: write's error case.
2249           if (write(rcv_msg.delay.event_fd, &term_event, sizeof(term_event)) < 0) {
2250           // LCOV_EXCL_BR_STOP 5: write's error case.
2251             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2252             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "write:%s", strerror(errno));  // LCOV_EXCL_LINE 5: write's error case.
2253           }
2254         } else {
2255           // delay request
2256           bkup_query_result_t query_result;
2257           BkupParamGet(rcv_msg.delay.item_name, &query_result);  // LCOV_EXCL_BR_LINE 11:unexpected branch
2258
2259           if (query_result.backup_cycle > 0) {  // LCOV_EXCL_BR_LINE 11:unexpected branch
2260             if (delay_req.find(rcv_msg.delay.item_name) != delay_req.end()) {  // LCOV_EXCL_BR_LINE 11:except,C++ STL
2261               delay_req[rcv_msg.delay.item_name]++;  // LCOV_EXCL_BR_LINE 11:except,C++ STL
2262             } else {
2263               // LCOV_EXCL_BR_START 11:unexpected branch
2264               BkupDelayTimerAdd(efd, rcv_msg.delay.item_name, query_result.backup_cycle);
2265               // LCOV_EXCL_BR_STOP 11:unexpected branch
2266               delay_req[rcv_msg.delay.item_name] = 1;  // LCOV_EXCL_BR_LINE 11:unexpected branch
2267             }
2268
2269             if (BkupSyncreqSet(query_result.id) < 0) {
2270               // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2271               FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", query_result.id);
2272               // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2273             }
2274           } else {
2275             // write to ACC OFF
2276             bkup_query_result_t query_result;
2277             if (BkupParamGet(rcv_msg.delay.item_name, &query_result) < 0) {  // LCOV_EXCL_BR_LINE 11:except,C++ STL
2278               // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2279               FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid item_name:%s",
2280                   rcv_msg.delay.item_name != 0 ? rcv_msg.delay.item_name : NULL);
2281               // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2282             } else {
2283               if (BkupSyncreqSet(query_result.id) < 0) {
2284                 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2285                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", query_result.id);
2286                 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2287               }
2288             }
2289           }
2290         }
2291 #endif
2292         switch (rcv_msg.delay.cmd) {
2293           case BKUP_DELAY_CMD_TERM:
2294           case BKUP_DELAY_CMD_PRE_ACCOFF:
2295             /*
2296              * terminate or pre-acc_off request
2297              */
2298             // shutdown sync
2299             BkupSyncreqDone(false);
2300
2301             for (std::map<std::string, int>::iterator i = delay_req.begin(); i != delay_req.end(); ++i) {
2302               i->second = 0;
2303             }
2304
2305             if (rcv_msg.delay.cmd == BKUP_DELAY_CMD_TERM) {
2306               BkupSyncreqTerm();
2307
2308               // terminate finish response
2309               int64_t term_event = 1;
2310               // LCOV_EXCL_BR_START 5: write's error case.
2311               if (write(rcv_msg.delay.event_fd, &term_event, sizeof(term_event)) < 0) {
2312               // LCOV_EXCL_BR_STOP 5: write's error case.
2313                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2314                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "write:%s", strerror(errno));  // LCOV_EXCL_LINE 5: write's error case.
2315               }
2316             }
2317             break;
2318
2319           case BKUP_DELAY_CMD_REGIST:
2320             // delay regist request
2321             bkup_query_result_t query_result;
2322             BkupParamGet(rcv_msg.delay.item_name, &query_result);
2323
2324             if (query_result.backup_cycle > 0) {
2325               if (delay_req.find(rcv_msg.delay.item_name) != delay_req.end()) {
2326                 delay_req[rcv_msg.delay.item_name]++;
2327               } else {
2328                 BkupDelayTimerAdd(efd, rcv_msg.delay.item_name, query_result.backup_cycle);
2329                 delay_req[rcv_msg.delay.item_name] = 1;
2330               }
2331
2332               if (BkupSyncreqSet(query_result.id) < 0) {
2333                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", query_result.id);
2334               }
2335             } else {
2336               // wite to ACC OFF
2337               bkup_query_result_t query_result;
2338               if (BkupParamGet(rcv_msg.delay.item_name, &query_result) < 0) {  // LCOV_EXCL_BR_LINE 6:double check
2339                 // LCOV_EXCL_START 6:double check
2340                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2341                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid item_name:%s", rcv_msg.delay.item_name);
2342                 // LCOV_EXCL_STOP 6:double check
2343               } else {
2344                 if (BkupSyncreqSet(query_result.id) < 0) {
2345                   FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", query_result.id);
2346                 }
2347               }
2348             }
2349             break;
2350           }
2351
2352       } else {
2353         /*
2354          * timerfd
2355          */
2356         uint64_t exp;
2357         if (read(cur_ev->fd, &exp, sizeof(exp)) < 0) {  // LCOV_EXCL_BR_LINE 5: read's error case.
2358           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2359           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "read:%s", strerror(errno));  // LCOV_EXCL_LINE 5: read's error case.
2360         }
2361
2362         if (delay_req[cur_ev->name] > 0) {  // LCOV_EXCL_BR_LINE 11:unexpected branch
2363           bkup_query_result_t query_result;
2364           BkupParamGet(cur_ev->name.c_str(), &query_result);  // LCOV_EXCL_BR_LINE 11:unexpected branch
2365
2366           if (query_result.cache_dram) {
2367             // LCOV_EXCL_BR_START 11:unexpected branch
2368             pthread_rwlock_t *src_rwlock = BkupGetRwlock(
2369                 cur_ev->name.c_str(), NVHALMEDIA_CACHEDRAM, &query_result);
2370             pthread_rwlock_t *dst_rwlock = BkupGetRwlock(
2371                 cur_ev->name.c_str(), NVHALMEDIA_NAND, &query_result);
2372             BkupMediaCopyFromNv(query_result.category_name,
2373                                 cur_ev->name.c_str(), NVHALMEDIA_CACHEDRAM,
2374                                 NVHALMEDIA_NAND, src_rwlock, dst_rwlock);
2375             BkupMediaCopyFromNv(query_result.category_name,
2376                                 cur_ev->name.c_str(), NVHALMEDIA_CACHEDRAM,
2377                                 NVHALMEDIA_NAND, NULL, NULL);
2378             // LCOV_EXCL_BR_STOP 11:unexpected branch
2379           } else if (query_result.backup_dram) {
2380             // LCOV_EXCL_BR_START 11:unexpected branch
2381             pthread_rwlock_t *src_rwlock = BkupGetRwlock(
2382                 cur_ev->name.c_str(), NVHALMEDIA_BACKUPDRAM, &query_result);
2383             pthread_rwlock_t *dst_rwlock = BkupGetRwlock(
2384                 cur_ev->name.c_str(), NVHALMEDIA_NAND, &query_result);
2385             BkupMediaCopyFromNv(query_result.category_name,
2386                                 cur_ev->name.c_str(), NVHALMEDIA_BACKUPDRAM,
2387                                 NVHALMEDIA_NAND, src_rwlock, dst_rwlock);
2388             // LCOV_EXCL_BR_STOP 11:unexpected branch
2389           }
2390           delay_req[cur_ev->name] = 0;  // LCOV_EXCL_BR_LINE 11:unexpected branch
2391           if (BkupSyncreqClear(query_result.id) < 0) {
2392             // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2393             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", query_result.id);
2394             // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2395           }
2396         } else {
2397           // not update. timer delte
2398           // LCOV_EXCL_BR_START 5: epoll_ctl's error case.
2399           if (epoll_ctl(efd, EPOLL_CTL_DEL, cur_ev->fd, &events[i]) < 0) {
2400           // LCOV_EXCL_BR_STOP 5: epoll_ctl's error case.
2401             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2402             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "epoll_ctl:%s", strerror(errno));  // LCOV_EXCL_LINE 5: epoll_ctl's error case.
2403           }
2404           if (close(cur_ev->fd) < 0) {  // LCOV_EXCL_BR_LINE 5: close's error case.
2405             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2406             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "close:%s", strerror(errno));  // LCOV_EXCL_LINE 5: close's error case.
2407           }
2408           delay_req.erase(cur_ev->name);  // LCOV_EXCL_BR_LINE 11:unexpected branch
2409           delete cur_ev;  // LCOV_EXCL_BR_LINE 11:except,C++ operator
2410         }
2411       }
2412     }
2413   }
2414
2415   return NULL;
2416 }
2417
2418 /**
2419  * Terminate handler
2420  */
2421 EFrameworkunifiedStatus BkupTerminateHandler(HANDLE h_app) {
2422   bkup_itc_message_t snd_msg;
2423   int event_fd;
2424   int64_t term_event;
2425
2426   g_bkup_internal_state = BKUP_STATE_TERM;
2427
2428   if (g_bkup_delay_mqd == -1) {
2429     // LCOV_EXCL_BR_START 5: mq_open's error case.
2430     if ((g_bkup_delay_mqd = BkupItcMqopen("/" BKUP_THREAD_DELAY, O_WRONLY | O_CREAT | O_NONBLOCK | O_CLOEXEC)) < 0) {
2431     // LCOV_EXCL_BR_STOP 5: mq_open's error case.
2432       // LCOV_EXCL_START 5: mq_open's error case.
2433       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2434       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
2435       return eFrameworkunifiedStatusFail;
2436       // LCOV_EXCL_STOP 5: mq_open's error case.
2437     }
2438   }
2439
2440   if ((event_fd = eventfd(0, 0)) < 0) {  // LCOV_EXCL_BR_LINE 5: eventfd's error case.
2441     // LCOV_EXCL_START 5: eventfd's error case.
2442     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2443     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "eventfd:%s", strerror(errno));
2444     return eFrameworkunifiedStatusFail;
2445     // LCOV_EXCL_STOP 5: eventfd's error case.
2446   }
2447
2448   memset(&snd_msg, 0, sizeof(snd_msg));
2449
2450 //  snd_msg.delay.terminate = true;
2451   snd_msg.delay.cmd = BKUP_DELAY_CMD_TERM;
2452
2453   snd_msg.delay.event_fd = event_fd;
2454
2455   // LCOV_EXCL_BR_START 5: mq_send's error case.
2456   if (mq_send(g_bkup_delay_mqd, reinterpret_cast<char *>(&snd_msg), sizeof(snd_msg), 0) < 0) {
2457   // LCOV_EXCL_BR_STOP 5: mq_send's error case.
2458     // LCOV_EXCL_START 5: mq_send's error case.
2459     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2460     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_send:%s", strerror(errno));
2461     return eFrameworkunifiedStatusFail;
2462     // LCOV_EXCL_STOP 5: mq_send's error case.
2463   }
2464
2465   while (1) {
2466     if (read(event_fd, &term_event, sizeof(term_event)) < 0) {  // LCOV_EXCL_BR_LINE 5: read's error case.
2467       // LCOV_EXCL_START 5: read's error case.
2468       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2469       if (errno == EINTR) {
2470         continue;
2471       }
2472       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "read:%s", strerror(errno));
2473       return eFrameworkunifiedStatusFail;
2474       // LCOV_EXCL_STOP 5: read's error case.
2475     } else {
2476       break;
2477     }
2478   }
2479   if (close(event_fd) < 0) {  // LCOV_EXCL_BR_LINE 5: close's error case.
2480     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2481     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "close:%s", strerror(errno));  // LCOV_EXCL_LINE 5: close's error case.
2482   }
2483
2484   return eFrameworkunifiedStatusOK;
2485 }
2486
2487 /**
2488  * Init handler
2489  */
2490 EFrameworkunifiedStatus BkupInitHandler(HANDLE h_app) {
2491   BkupSyncreqRestart();
2492   BkupSyncreqInit();
2493
2494   g_bkup_internal_state = BKUP_STATE_NORMAL;
2495
2496   return eFrameworkunifiedStatusOK;
2497 }