2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "tskm_port_pf.h"
29 #include <native_service/cl_process.h>
30 #include <native_service/cl_monitor.h>
31 #include <system_service/ss_services.h>
32 #include <system_service/ss_sm_client_if.h>
34 #include "system_service/tskm_svc.h"
35 #include "tskm_debug.h"
37 #define TSKM_PORTPF_IN() TSKM_PRINTF(TSKM_LOG_PORTPF, "%s:IN", __FUNCTION__)
38 #define TSKM_PORTPF_IN_ARG(format, ...) TSKM_PRINTF(TSKM_LOG_PORTPF, "%s:IN:" format, __FUNCTION__, __VA_ARGS__)
39 #define TSKM_PORTPF_OUT() TSKM_PRINTF(TSKM_LOG_PORTPF, "%s:OUT", __FUNCTION__)
41 #define TSKM_PRIO_MIN 1
42 #define TSKM_PRIO_MAX 99
43 #define TSKM_PRIO_MIN_TSS -20
44 #define TSKM_PRIO_MAX_TSS 19
46 #define TSKM_TMP_DIRECTORY "/tmp"
47 #define TSKM_TMP_TSKM_DIRECTORY TSKM_TMP_DIRECTORY "/tskm"
48 #define TSKM_TMP_NV_INFO_FILEPATH TSKM_TMP_TSKM_DIRECTORY "/nvInfo"
50 #define TSKM_NPP_NV_FILE_TAG SS_TASK_MANAGER "/nvInfo"
52 /*********************************************************
53 * user->uid/gid conversion
54 *********************************************************/
55 TSKM_STATIC int cnvUserName2UidGid(const char *user, uid_t *uid, gid_t *gid) {
58 if (0 == strcmp(user, "")) {
62 static __thread size_t bufSize = 0;
63 static __thread char *buf = NULL;
65 struct passwd *result;
72 if (0 != stat("/etc/passwd", &statInfo) || 0 >= statInfo.st_size) { // LCOV_EXCL_START 8: Because buf is a static variable and cannot be passed test
73 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
79 bufSize = statInfo.st_size * 2;
81 // Since TaskManager is a resident service, the area allocated here is expected to be released at process termination and is not explicitly released by free()
82 buf = (char *) malloc(sizeof(char) * bufSize); // NOLINT (runtime/printf)
83 if (NULL == buf) { // LCOV_EXCL_BR_LINE 5: Error branching of the standard-function malloc
84 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
85 TSKM_ASSERT_ERRNO(0); // LCOV_EXCL_LINE 5: Error branching of the standard-function malloc
86 goto ERROR; // LCOV_EXCL_LINE 5: Error branching of the standard-function malloc
90 if (NULL == buf) { // LCOV_EXCL_BR_LINE 8: Because buf is a static variable and cannot be passed test
91 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
92 TSKM_ASSERT(0); // LCOV_EXCL_LINE 8: Because buf is a static variable and cannot be passed test
93 goto ERROR; // LCOV_EXCL_LINE 8: Because buf is a static variable and cannot be passed test
96 ret = getpwnam_r(user, &pwd, buf, bufSize, &result);
97 if (ret != 0 || result == NULL) {
98 TSKM_ASSERT_PRINT(0, "ret = %d[%s]", ret, user);
113 /*********************************************************
114 * Convert Scheduling Policy Definitions Values (TSKM -> CL)
115 *********************************************************/
116 TSKM_STATIC CL_ProcessSchedPolicy_t cnvTskmPolicy2ClPolicy(
117 TSKM_SVC_POLICY_t policy) {
119 case TSKM_SVC_POLICY_FIFO:
120 return CL_PROCESS_SCHED_POLICY_FIFO;
121 case TSKM_SVC_POLICY_TSS:
122 return CL_PROCESS_SCHED_POLICY_OTHER;
123 case TSKM_SVC_POLICY_RR:
124 return CL_PROCESS_SCHED_POLICY_RR;
129 return CL_PROCESS_SCHED_POLICY_OTHER;
132 /*********************************************************
133 * Process priority valid value determination
134 *********************************************************/
135 TSKM_STATIC int chkPrioValue(TSKM_SVC_POLICY_t policy, int prio) {
137 case TSKM_SVC_POLICY_FIFO:
138 case TSKM_SVC_POLICY_RR:
139 if ((TSKM_PRIO_MIN > prio) || (prio > TSKM_PRIO_MAX)) {
143 case TSKM_SVC_POLICY_TSS:
145 if ((TSKM_PRIO_MIN_TSS > prio) || (prio > TSKM_PRIO_MAX_TSS)) {
154 /*********************************************************
155 * COMMONLIB initialization (at system startup)
156 *********************************************************/
157 int tskm_pf_sysInit() {
163 fd = CL_ProcessInit();
164 if (fd == -1) { // LCOV_EXCL_BR_LINE 6: Error branching during process initialization
165 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
166 TSKM_ASSERT_ERRNO(0); // LCOV_EXCL_LINE 6: Error branching during process initialization
167 goto ERROR; // LCOV_EXCL_LINE 6: Error branching during process initialization
170 ret = CL_MonitorInit(CL_MONITOR_INIT_USER);
171 if (ret != 0) { // LCOV_EXCL_BR_LINE 4: NSFW error case.
172 TSKM_ASSERT_ERRNO(0);
177 // LCOV_EXCL_START 6: Error branching during process initialization
179 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
184 /*********************************************************
185 * COMMONLIB initialization (at process startup)
186 *********************************************************/
187 int tskm_pf_procInit() {
189 // Originally called the _sys_Setup_CWORD64_API (NULL), but changed to empty function along to derete "_sys"
195 /*********************************************************
197 *********************************************************/
198 pid_t tskm_pf_createProc(TSKM_SVC_ATTR_t* p_svcAttr) {
203 CL_ProcessAttr_t attr;
209 if (0 != CL_ProcessCreateAttrInit(&attr)) { // LCOV_EXCL_BR_LINE 6: Error branching during process initialization
210 // LCOV_EXCL_START 6: Error branching during process initialization
211 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
217 // Process name setting(from basename)
218 if (strlen(p_svcAttr->path) < sizeof(procPath)) {
219 strcpy(procPath, p_svcAttr->path); // NOLINT (runtime/printf)
225 procName = basename(procPath);
226 if (strlen(procName) >= 16) {
231 if (0 != CL_ProcessCreateAttrSetName(&attr, procName)) { // LCOV_EXCL_BR_LINE 4: NSFW error case.
232 // LCOV_EXCL_START 4: NSFW error case.
233 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
234 TSKM_ASSERT_ERRNO(0);
239 if (0 != cnvUserName2UidGid(p_svcAttr->user, &setUid, &setGid)) { // LCOV_EXCL_BR_LINE 4: NSFW error case.
240 // LCOV_EXCL_START 4: NSFW error case.
241 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
248 if (0 != CL_ProcessCreateAttrSetUid(&attr, setUid)) { // LCOV_EXCL_BR_LINE 4: NSFW error case.
249 // LCOV_EXCL_START 4: NSFW error case.
250 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
251 TSKM_ASSERT_ERRNO(0);
256 if (0 != CL_ProcessCreateAttrSetGid(&attr, setGid)) { // LCOV_EXCL_BR_LINE 4: NSFW error case.
257 // LCOV_EXCL_START 4: NSFW error case.
258 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
259 TSKM_ASSERT_ERRNO(0);
264 // All are group leaders in order to recover to the child processes of the service
265 if (0 != CL_ProcessCreateAttrSetGroup(&attr, 1)) { // LCOV_EXCL_BR_LINE 4: NSFW error case.
266 // LCOV_EXCL_START 4: NSFW error case.
267 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
268 TSKM_ASSERT_ERRNO(0);
272 if (0 != CL_ProcessCreateAttrSetCpuAssign(&attr, p_svcAttr->cpuAssign)) {
273 TSKM_ASSERT_ERRNO(0);
278 != CL_ProcessCreateAttrSetSchedule(
279 &attr, cnvTskmPolicy2ClPolicy(p_svcAttr->policy),
280 chkPrioValue(p_svcAttr->policy, p_svcAttr->prio))) {
281 TSKM_ASSERT_ERRNO(0);
285 pid = CL_ProcessCreate(p_svcAttr->path, p_svcAttr->args, NULL, &attr);
287 TSKM_ASSERT_ERRNO(0);
298 /*********************************************************
299 * Recover termination processes
300 *********************************************************/
301 int tskm_pf_cleanupProc(int sigFd, pid_t* p_pid, TSKM_ERR_t* p_err) {
303 CL_ProcessCleanupInfo_t info;
305 ret = CL_ProcessCleanup(sigFd, &info);
306 if (ret != 0 && ret != 1) {
307 TSKM_ASSERT_PRINT(0, "ret = %d", ret);
308 TSKM_ASSERT_ERRNO(0);
312 TSKM_PRINTF(TSKM_LOG_STATE, "SVC TERM:pid:%d code:%d status:%d", info.pid,
313 info.code, info.status);
317 // Not comes here usually
318 TSKM_PRINTF(TSKM_LOG_STATE, "svc stoped\n");
321 // Comes here only during debugging
322 TSKM_PRINTF(TSKM_LOG_STATE, "svc traped\n");
325 // Not comes here usually
326 TSKM_PRINTF(TSKM_LOG_STATE, "svc continue\n");
334 (info.code != CLD_EXITED) ? TSKM_E_NG :
335 (info.code == CLD_EXITED && info.status != EXIT_SUCCESS) ?
336 TSKM_E_NG : TSKM_E_OK;
338 if (TSKM_E_OK != *p_err) {
339 TSKM_PRINTF(TSKM_LOG_SYSTEMDATA, "SVC ERR TERM:pid:%d code:%d status:%d",
340 info.pid, info.code, info.status);
347 /*********************************************************
348 * Force Terminating a Process Group
349 *********************************************************/
350 int tskm_pf_terminateProcGroup(uint16_t pid) {
360 TSKM_PRINTF(TSKM_LOG_STATE, "TERM SVC GROUP:pid:%dpgid:%d", pid, pgid);
362 ret = CL_ProcessEuthanizeGroup(pgid);
364 TSKM_ASSERT_ERRNO(0);
372 /*********************************************************
373 * Opening shared memory and trancating
374 *********************************************************/
375 int tskm_pf_shmCreate_Open_Ftrn(const char* name, int32_t size, void** p_addr) {
379 fd = shm_open(name, O_CREAT | O_RDWR | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
380 if (fd == -1) { // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
381 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
382 TSKM_ASSERT_ERRNO(0); // LCOV_EXCL_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
383 ret = -1; // LCOV_EXCL_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
384 if (fd > 0) { // LCOV_EXCL_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
385 TSKM_ASSERT_ERRNO(close(fd) == 0); // LCOV_EXCL_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
386 } // LCOV_EXCL_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
387 return ret; // LCOV_EXCL_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
390 ret = ftruncate(fd, size);
391 if (ret != 0) { // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
392 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
393 TSKM_ASSERT_ERRNO(0); // LCOV_EXCL_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
394 if (fd > 0) { // LCOV_EXCL_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
395 TSKM_ASSERT_ERRNO(close(fd) == 0); // LCOV_EXCL_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
396 } // LCOV_EXCL_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
397 return ret; // LCOV_EXCL_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded.
400 if (p_addr) { // LCOV_EXCL_BR_LINE 8: Because p_addr is set to NULL only
401 // LCOV_EXCL_START 8: Because p_addr is set to NULL only
402 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
404 TSKM_ASSERT_ERRNO(0);
407 TSKM_ASSERT_ERRNO(close(fd) == 0);
415 TSKM_ASSERT_ERRNO(close(fd) == 0);
420 /*********************************************************
421 * Creating shared memory
422 *********************************************************/
423 int tskm_pf_shmCreate(const char* name, int32_t size, void** p_addr) {
424 TSKM_PORTPF_IN_ARG("%s,%d", name, size);
425 struct stat statInfo;
428 if (stat(name, &statInfo) == 0) {
429 TSKM_ASSERT_PRINT(0, "Already Exist %s", name);
430 ret = 0; // To be Succeeded
435 ret = tskm_pf_shmCreate_Open_Ftrn(name, size, p_addr);
441 /*********************************************************
442 * Deleting shared memory
443 *********************************************************/
444 int tskm_pf_shmDelete(const char* name) {
445 TSKM_PORTPF_IN_ARG("%s", name);
448 if (0 != shm_unlink(name)) {
449 TSKM_ASSERT_ERRNO(0);
456 /*********************************************************
458 *********************************************************/
459 int tskm_pf_createThread(void* (*threadFunc)(void*), void* prm,
460 uint32_t priority, const char* threadName,
465 CL_ThreadAttr_t cl_attr;
467 ret = CL_ThreadCreateAttrInit(&cl_attr);
468 if (ret != 0) { // LCOV_EXCL_BR_LINE 6: For processing initializing process
469 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
470 TSKM_ASSERT_ERRNO(0); // LCOV_EXCL_LINE 6: For processing initializing process
471 goto ERROR; // LCOV_EXCL_LINE 6: For processing initializing process
475 ret = CL_ThreadCreateAttrSetName(&cl_attr, threadName);
476 TSKM_ASSERT_ERRNO(ret == 0);
479 ret = CL_ThreadCreate(p_thId, NULL, &cl_attr, threadFunc, prm);
480 if (ret != 0) { // LCOV_EXCL_BR_LINE 6: For processing initializing process
481 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
482 TSKM_ASSERT_ERRNO(0); // LCOV_EXCL_LINE 6: For processing initializing process
483 goto ERROR; // LCOV_EXCL_LINE 6: For processing initializing process
486 struct sched_param sparam;
488 pthread_getschedparam(*p_thId, &sched_policy, &sparam);
490 sparam.sched_priority = priority;
491 if ((priority != 0) && (sched_policy == SCHED_OTHER)) {
492 sched_policy = SCHED_FIFO;
493 } else if ((priority == 0) && (sched_policy != SCHED_OTHER)) {
494 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
495 sched_policy = SCHED_OTHER; // LCOV_EXCL_LINE 6: For processing initializing process
498 ret = pthread_setschedparam(*p_thId, sched_policy, &sparam);
499 TSKM_ASSERT(ret == 0);
504 /*********************************************************
505 * Send Stop Complete Response
506 *********************************************************/
507 int tskm_pf_sendStopCompResp() {
508 EFrameworkunifiedStatus l_eStatus;
511 l_eStatus = SendInterfaceunifiedOnStopResponseToSystemManager(eFrameworkunifiedStatusOK);
512 if (l_eStatus != eFrameworkunifiedStatusOK) {
520 /*********************************************************
522 *********************************************************/
523 int tskm_pf_abort() {
527 sleep(1); // Sleep a little so that the logging at the time of abnormality may be interrupted
530 printf("%d\n", *(int *) 0); // NOLINT (readability/casting)
536 /*********************************************************
538 *********************************************************/
539 int tskm_pf_exit(int status) { //LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
540 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
550 /*********************************************************
552 *********************************************************/
553 int tskm_pf_touch(char* path) {
556 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
558 TSKM_ASSERT_ERRNO(0);
562 TSKM_ASSERT_ERRNO(close(fd) == 0);
569 /*********************************************************
571 *********************************************************/
572 int tskm_pf_mkTouchFileName(pid_t pid, char name[32]) {
575 sprintf(name, "/tmp/tskm_touch%d", pid); // NOLINT (runtime/printf)
581 /*********************************************************
583 *********************************************************/
584 static ssize_t readFile(const char* filePath, void* l_buf, ssize_t bufSize) {
586 ssize_t readSize, totalReadSize = 0;
587 char *p_buf = (char*) l_buf; // NOLINT (readability/casting)
588 fd = open(filePath, O_RDONLY);
590 TSKM_ASSERT_ERRNO(0);
595 readSize = read(fd, p_buf, bufSize);
596 if (readSize == -1) {
597 if (errno == EINTR) {
600 TSKM_ASSERT_ERRNO(0);
603 } else if (readSize == 0) {
609 totalReadSize += readSize;
610 } while (bufSize > 0);
612 ERROR: if (fd != -1) {
616 return totalReadSize;
619 /*********************************************************
621 *********************************************************/
622 static ssize_t writeFile(const char *filePath, const void* l_buf,
627 const char *p_buf = (const char*) l_buf;
629 ssize_t writeSize, totalWriteSize = 0;
631 fd = open(filePath, O_RDWR | O_TRUNC | O_CREAT, S_IRWXU);
632 if (fd == -1) { // LCOV_EXCL_BR_LINE 5: Error branching of the standard-function open()
633 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
634 TSKM_ASSERT_ERRNO(0); // LCOV_EXCL_LINE 5: Error branching of the standard-function open()
635 goto ERROR; // LCOV_EXCL_LINE 5: Error branching of the standard-function open()
639 writeSize = write(fd, p_buf, bufSize);
640 if (writeSize == -1) { // LCOV_EXCL_BR_LINE 5: Error branching of the standard-function write()
641 // LCOV_EXCL_START 5: Error branching of the standard-function write()
642 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
643 if (errno == EINTR) {
646 TSKM_ASSERT_ERRNO(0);
650 } else if (writeSize == 0) { // LCOV_EXCL_BR_LINE 5: Error branching of the standard-function write()
651 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
652 break; // LCOV_EXCL_LINE 5: Error branching of the standard-function write()
655 bufSize -= writeSize;
656 totalWriteSize += writeSize;
657 } while (bufSize > 0); // LCOV_EXCL_BR_LINE 5: Error branching of the standard-function write()
660 if (ret == -1) { // LCOV_EXCL_BR_LINE 5: Error branching of the standard-function fsync()
661 // LCOV_EXCL_START 5: Error branching of the standard-function fsync()
662 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
663 TSKM_ASSERT_ERRNO(0);
668 ERROR: if (fd != -1) { // LCOV_EXCL_BR_LINE 5: Error branching of the standard-function open()
672 return totalWriteSize;
675 /*********************************************************
676 * Checksum calculation
677 *********************************************************/
678 static uint32_t calcCheckSum(const void* p_buf, size_t bufSize) {
682 uint32_t *p_calcBuf = (uint32_t*) p_buf; // NOLINT (readability/casting)
684 blockNum = static_cast<int>(bufSize) / static_cast<int>(sizeof(uint32_t));
686 for (ii = 0; ii < blockNum; ii++) {
687 sum += p_calcBuf[ii];
693 /*********************************************************
695 *********************************************************/
696 static TSKM_BOOL_t checkFile(const char *filePath) { // LCOV_EXCL_START 6: Because the condition cannot be set
697 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
701 TSKM_NV_INFO_t nvInfo;
702 TSKM_BOOL_t isFileOk = TSKM_FALSE;
704 if (stat(filePath, &st) != 0) {
708 readSize = readFile(filePath, &nvInfo, sizeof(TSKM_NV_INFO_t));
709 if (readSize != sizeof(TSKM_NV_INFO_t)) {
714 checkSum = calcCheckSum(&nvInfo, offsetof(TSKM_NV_INFO_t, footer.checkSum));
716 if (checkSum != nvInfo.footer.checkSum) {
721 if (strncmp(nvInfo.header.version, TSKM_NV_STRUCT_VERSION,
722 sizeof(TSKM_NV_STRUCT_VERSION)) != 0) {
727 isFileOk = TSKM_TRUE;
728 ERROR: return isFileOk;
731 /*********************************************************
732 * Non-volatile file initialization (Including checksum write)
733 *********************************************************/
734 static int initNvFile() {
737 TSKM_NV_INFO_t nvInfo;
739 TSKM_STATIC_ASSERT(sizeof(TSKM_NV_INFO_t) == TSKM_NV_SIZE_ALL);
741 offsetof(TSKM_NV_INFO_t, footer.checkSum) % sizeof(uint32_t) == 0);
743 memset(&nvInfo, 0, sizeof(TSKM_NV_INFO_t));
744 sprintf(nvInfo.header.version, TSKM_NV_STRUCT_VERSION); // NOLINT (readability/casting)
745 nvInfo.body.rsvSvcNum = 0;
746 for (ii = 0; ii < TSKM_SVC_RESERVE_MAX; ii++) {
747 nvInfo.body.rsvSvcs[ii] = TSKM_SVCID_NONE;
749 nvInfo.footer.checkSum = calcCheckSum(
750 &nvInfo, offsetof(TSKM_NV_INFO_t, footer.checkSum));
752 writeSize = writeFile(TSKM_TMP_NV_INFO_FILEPATH, &nvInfo,
753 sizeof(TSKM_NV_INFO_t));
754 if (writeSize != sizeof(TSKM_NV_INFO_t)) { // LCOV_EXCL_BR_LINE 6: writeSize must be the size of TSKM_NV_INFO_t
755 // LCOV_EXCL_START 6: writeSize must be the size of TSKM_NV_INFO_t
756 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
766 /*********************************************************
767 * Non-volatile file initialization
768 *********************************************************/
769 int tskm_pf_nvFileInit(HANDLE hApp) {
772 EFrameworkunifiedStatus l_eStatus;
774 if (stat(TSKM_TMP_TSKM_DIRECTORY, &st) != 0) { // LCOV_EXCL_BR_LINE 5:Standard C function return value
775 // LCOV_EXCL_START 5:Standard C function return value
776 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
777 ret = mkdir(TSKM_TMP_TSKM_DIRECTORY, S_IRWXU);
779 TSKM_ASSERT_ERRNO(0);
785 l_eStatus = FrameworkunifiedNPRegisterPersistentFile(hApp, TSKM_NPP_NV_FILE_TAG);
786 if (eFrameworkunifiedStatusOK != l_eStatus) { // LCOV_EXCL_BR_LINE 6: For processing initializing process
787 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
788 TSKM_ASSERT(0); // LCOV_EXCL_LINE 6: For processing initializing process
789 goto ERROR; // LCOV_EXCL_LINE 6: For processing initializing process
792 l_eStatus = FrameworkunifiedNPLoadPersistentFile(hApp, TSKM_TMP_NV_INFO_FILEPATH, TSKM_NPP_NV_FILE_TAG);
793 if (eFrameworkunifiedStatusOK != l_eStatus) { // LCOV_EXCL_BR_LINE 6: For processing initializing process
794 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
795 TSKM_ASSERT(0); // LCOV_EXCL_LINE 6: For processing initializing process
796 goto ERROR; // LCOV_EXCL_LINE 6: For processing initializing process
803 /*********************************************************
804 * Non-volatile file read
805 *********************************************************/
806 int tskm_pf_nvFileRead(HANDLE hApp, TSKM_NV_INFO_t* p_nvInfo) {
807 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
808 NC_LoadPersistedAck l_tMsgAck;
810 TSKM_BOOL_t isTmpFileOk = TSKM_FALSE;
812 if (sizeof(l_tMsgAck) != FrameworkunifiedGetMsgLength(hApp)) {
815 } else if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, &l_tMsgAck, sizeof(l_tMsgAck)))) { // LCOV_EXCL_BR_LINE 4: NSFW error case // NOLINT[whitespace/line_length]
816 // LCOV_EXCL_START 4: NSFW error case
817 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
823 if ((strncmp(l_tMsgAck.cTag, TSKM_NPP_NV_FILE_TAG, sizeof(TSKM_NPP_NV_FILE_TAG)) == 0)
824 && (eFrameworkunifiedStatusOK == l_tMsgAck.eStatus)) {
825 if (checkFile(TSKM_TMP_NV_INFO_FILEPATH) == TSKM_TRUE) {
826 isTmpFileOk = TSKM_TRUE;
830 if (isTmpFileOk == TSKM_FALSE) {
834 if (ret == -1) { // LCOV_EXCL_BR_LINE 6: must return ok
835 // LCOV_EXCL_START 6: must return ok
836 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
843 readSize = readFile(TSKM_TMP_NV_INFO_FILEPATH, p_nvInfo,
844 sizeof(TSKM_NV_INFO_t));
845 if (readSize != sizeof(TSKM_NV_INFO_t)) { // LCOV_EXCL_BR_LINE 6: readSize must be the size of TSKM_NV_INFO_t
846 // LCOV_EXCL_START 6: readSize must be the size of TSKM_NV_INFO_t
847 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
853 l_eStatus = FrameworkunifiedNPReleasePersistentFile(hApp, eFrameworkunifiedPersistOnShutdown, TSKM_NPP_NV_FILE_TAG, TSKM_TMP_NV_INFO_FILEPATH);
854 if (eFrameworkunifiedStatusOK != l_eStatus) {
862 /*********************************************************
863 * Write non-volatile file (Including checksum)
864 *********************************************************/
865 int tskm_pf_nvFileWrite(const TSKM_NV_INFO_t* p_nvInfo) {
867 TSKM_NV_INFO_t nvInfo;
869 memcpy(&nvInfo, p_nvInfo, sizeof(TSKM_NV_INFO_t));
871 nvInfo.footer.checkSum = calcCheckSum(
872 &nvInfo, offsetof(TSKM_NV_INFO_t, footer.checkSum));
874 writeSize = writeFile(TSKM_TMP_NV_INFO_FILEPATH, &nvInfo,
875 sizeof(TSKM_NV_INFO_t));
876 if (writeSize != sizeof(TSKM_NV_INFO_t)) { // LCOV_EXCL_BR_LINE 6: writeSize must be the size of TSKM_NV_INFO_t
877 // LCOV_EXCL_START 6: writeSize must be the size of TSKM_NV_INFO_t
878 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
884 } // LCOV_EXCL_BR_LINE 10: Final line