Remove unused directories and files in video_in_hal
[staging/basesystem.git] / service / system / logger_service / server / src / ss_logger_error_event_responses.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 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_SS_LoggerService
19 /// \brief    This file supports error event logging.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/wait.h>
25 #include <boost/bind.hpp>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/klog.h>
30 #include <native_service/frameworkunified_framework_if.h>
31 #include <native_service/frameworkunified_multithreading.h>
32 #include <native_service/ns_sharedmem.h>
33 #include <native_service/ns_sharedmem_reader.h>
34 #include <native_service/ns_ringbuffer.h>
35 #include <native_service/ns_logger_if.h>
36 #include <system_service/ss_sm_client_if.h>
37 #include <system_service/ss_logger_service_protocol.h>
38 #include <system_service/ss_services.h>
39 #include <system_service/ss_templates.h>
40 #include <native_service/cl_process.h>
41 #include <native_service/cl_lockid.h>
42 #include <sstream>
43 #include <string>
44 #include "loggerservicedebug_loggerservicelog.h"
45 #include "loggerservicedebug_thread_if.h"
46 #include "ss_logger_error_event.h"
47 #include "ss_logger_service_callbacks.h"
48 #include "ss_logger_scrshot.h"
49 #include "ss_logger_fs_directory.h"
50 #include "ss_logger_common.h"
51 #include "ss_logger_types.h"
52
53 EFrameworkunifiedStatus SSLogger_SendtoSM(HANDLE f_hApp, TLoggingArtifact f_artifact) {
54   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
55   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
56   l_eStatus = SendLogArtifactRequestToSystemManager(f_artifact.ArtifactId);
57   if (eFrameworkunifiedStatusOK != l_eStatus) {
58     FRAMEWORKUNIFIEDLOG(
59         ZONE_ERR,
60         __FUNCTION__,
61         " Error. SendLogArtifactRequestToSystemManager returned: %d for artifact ID: %d",
62         l_eStatus, f_artifact.ArtifactId);
63   }
64   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
65   return (l_eStatus);
66 }
67
68 EFrameworkunifiedStatus SSLogger_SendtoSelf(HANDLE f_hApp, TLoggingArtifact f_artifact) {
69   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
70   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusNullPointer;
71   if (NULL != f_hApp) {  // LCOV_EXCL_BR_LINE 6: double check
72     l_eStatus = FrameworkunifiedSendSelf(f_hApp, SS_LOGGER_ERROR_EVENT_ARTIFACT_REQ,
73                             sizeof(f_artifact.ArtifactId),
74                             &f_artifact.ArtifactId);
75
76     if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
77       // LCOV_EXCL_START 4: NSFW error case.
78       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
79       FRAMEWORKUNIFIEDLOG(
80           ZONE_ERR,
81           __FUNCTION__,
82           " Error. FrameworkunifiedSendSelf(SS_LOGGER_ERROR_EVENT_ARTIFACT_REQ) returned error: %d for artifact ID: %d.",
83           l_eStatus, f_artifact.ArtifactId);
84       // LCOV_EXCL_STOP
85     }
86   }
87   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
88   return (l_eStatus);
89 }
90 ///////////////////////////////////////////////////////////////////////////
91 //  Function : SendLogArtifactResponseToSelf
92 //  brief    : Collect frameworkunifiedlog artifact and return a response to
93 //             SS_Logger.
94 ///////////////////////////////////////////////////////////////////////////
95 EFrameworkunifiedStatus CErrorEvent::SendLogArtifactResponseToSelf(
96     HANDLE f_hApp, EArtifactId f_artifactId,
97     std::string f_artifactFilePathAndName) {
98   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
99   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
100   ARTIFACT_RESPONSE l_artifactResponse;
101
102   l_artifactResponse.ArtifactId = f_artifactId;
103
104   strncpy(l_artifactResponse.FilePathAndName, f_artifactFilePathAndName.c_str(),
105           sizeof(l_artifactResponse.FilePathAndName) - 1);
106
107   l_eStatus = FrameworkunifiedSendSelf(f_hApp, SS_SM_ERROR_EVENT_ARTIFACT_RSPN,
108                           sizeof(l_artifactResponse), &l_artifactResponse);
109
110   LOG_STATUS_IF_ERRORED(l_eStatus,
111                         "FrameworkunifiedSendSelf(SS_SM_ERROR_EVENT_ARTIFACT_RSPN)");
112
113   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
114   return (l_eStatus);
115 }
116
117 ///////////////////////////////////////////////////////////////////////////
118 //  Function : OnObtainLoggerserviceLogRequest
119 //  brief    : Collect frameworkunifiedlog artifact and return a response to
120 //             SS_Logger.
121 ///////////////////////////////////////////////////////////////////////////
122 EFrameworkunifiedStatus CErrorEvent::OnObtainLoggerserviceLogRequest(HANDLE f_hApp) {
123   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
124   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
125   std::string dir_path = DEBUG_LOG_PATH_DIR;
126
127   l_eStatus = CFSDirectory::CreateDirectory(dir_path);
128   if (l_eStatus != eFrameworkunifiedStatusOK) {
129     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: Create FRAMEWORKUNIFIEDLOG tmp directory %d",
130            l_eStatus);
131   } else {
132     UI_32 l_DumpSize = 0;
133     std::string src_file_path;
134     std::string dst_file_path;
135     PCSTR file_name;
136     UI_32 num;
137     bool log_clear = true;
138
139     switch (m_errorEventNtfData.EventType) {
140       // _DIAG
141       case eErrorEventTypeUserInvokedUserForceReset:
142       case eErrorEventTypeUserInvokedCollectAllLogs:
143       case eErrorEventTypeUserInvokedCollectScreenShot:
144       case eErrorEventTypeUserInvokedCollectInterfaceunifiedLogs:
145       case eErrorEventTypeDiagEvent:
146       case eErrorEventTypeCanEvent:
147       case eErrorEventTypeDtcEvent:
148       case eErrorEventTypeModuleInvokedCollectDebugLogs:
149         // _DEBUG
150       case eErrorEventTypeUserInvokedCollectDevLogs:
151         log_clear = false;
152         break;
153       default:
154         break;
155     }
156
157     num = NSLogGetFrameworkunifiedlogFileTotalNum();
158     for (UI_32 cont = 0; cont < num; cont++) {
159       file_name = NSLogGetFrameworkunifiedlogFileName(cont);
160       if (file_name == NULL) {
161         break;
162       }
163       src_file_path = FRAMEWORKUNIFIEDLOG_RAMDISC_PATH;
164       src_file_path.append("/");
165       src_file_path.append(file_name);
166       dst_file_path = DEBUG_LOG_PATH_DIR;
167       dst_file_path.append("/");
168       dst_file_path.append(file_name);
169       CNSRingBuffer* l_pLoggerservicelog;
170       int index;
171       index = NSLogGetFrameworkunifiedlogIndex(src_file_path.c_str());
172       l_pLoggerservicelog = new (std::nothrow) CNSRingBuffer(
173           src_file_path, 0, index + LOCK_NSLOG_ACCES_IF_1);
174       if (l_pLoggerservicelog == NULL) {  // LCOV_EXCL_BR_LINE 5: new's error case
175         // LCOV_EXCL_START 5: new's error case
176         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
177         l_eStatus = eFrameworkunifiedStatusInvldHandle;
178         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
179                "Error: FRAMEWORKUNIFIEDLOG RingBuffer handle is NULL %s",
180                src_file_path.c_str());
181         // LCOV_EXCL_STOP
182       } else if (eFrameworkunifiedStatusOK != (l_eStatus = l_pLoggerservicelog->Open())) {
183         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
184                "Error: FRAMEWORKUNIFIEDLOG RingBuffer Open failed %s %d",
185                src_file_path.c_str(), l_eStatus);
186       } else if (eFrameworkunifiedStatusOK
187           != (l_eStatus = l_pLoggerservicelog->DumpToFile(dst_file_path.c_str(),
188                                                 &l_DumpSize))) {
189         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
190                "Error: FRAMEWORKUNIFIEDLOG RingBuffer DumpToFile failed %s %d",
191                src_file_path.c_str(), l_eStatus);
192       }
193       if (NULL != l_pLoggerservicelog) {  // LCOV_EXCL_BR_LINE 5: new's error case
194         if (log_clear) {
195           if (eFrameworkunifiedStatusOK != (l_eStatus = l_pLoggerservicelog->ClearBuf())) {
196             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
197                    "Error: FRAMEWORKUNIFIEDLOG RingBuffer ClearBuf failed %s %d",
198                    src_file_path.c_str(), l_eStatus);
199           }
200         }
201         (void) l_pLoggerservicelog->Close();
202         delete l_pLoggerservicelog;
203       }
204     }
205     l_eStatus = SendLogArtifactResponseToSelf(f_hApp, eArtifactIdInterfaceunifiedDebugLog,
206                                               DEBUG_LOG_PATH_DIR);
207     LOG_STATUS_IF_ERRORED(
208         l_eStatus, "SendLogArtifactResponseToSelf(eArtifactIdInterfaceunifiedDebugLog)");
209   }
210   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
211   return (l_eStatus);
212 }
213
214 ///////////////////////////////////////////////////////////////////////////
215 //  Function : OnObtainLoggerserviceLogRequest
216 //  brief    : Collect frameworkunifiedlog artifact and return a response to
217 //             SS_Logger.
218 ///////////////////////////////////////////////////////////////////////////
219 EFrameworkunifiedStatus CErrorEvent::OnObtainTransmitLogRequest(HANDLE f_hApp) {
220   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
221   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
222   std::string l_RootPath;
223
224   l_eStatus = m_pReaderWriterControl->FlushCache(eReaderWriterLogTransmit);
225   LOG_STATUS_IF_ERRORED(l_eStatus, "m_pReaderWriterControl->FlushCache(eReaderWriterLogTransmit)");
226   l_RootPath = TRANSMIT_LOG_PATH_FN;
227   if (access(l_RootPath.c_str(), R_OK) != 0) {
228     l_RootPath = "";
229   }
230   l_eStatus = SendLogArtifactResponseToSelf(f_hApp, eArtifactIdTransmitLog,
231                                             l_RootPath.c_str());
232
233   LOG_STATUS_IF_ERRORED(l_eStatus,
234                         "SendLogArtifactResponseToSelf(eArtifactIdTransmitLog)");
235
236   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
237   return (l_eStatus);
238 }
239
240 ///////////////////////////////////////////////////////////////////////////
241 //  Function : OnObtainLoggerserviceLogRequest
242 //  brief    : Collect frameworkunifiedlog artifact and return a response to
243 //             SS_Logger.
244 ///////////////////////////////////////////////////////////////////////////
245 EFrameworkunifiedStatus CErrorEvent::OnObtainPerformanceLogRequest(HANDLE f_hApp) {
246   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
247   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
248
249   l_eStatus = m_pReaderWriterControl->FlushCache(eReaderWriterLogPerformance);
250   LOG_STATUS_IF_ERRORED(
251       l_eStatus,
252       "m_pReaderWriterControl->FlushCache(eReaderWriterLogPerformance)");
253   l_eStatus = SendLogArtifactResponseToSelf(f_hApp, eArtifactIdPerformanceLog,
254                                             PERFORMANCE_LOG_PATH_FN);
255
256   LOG_STATUS_IF_ERRORED(
257       l_eStatus, "SendLogArtifactResponseToSelf(eArtifactIdPerformanceLog)");
258
259   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
260   return (l_eStatus);
261 }
262
263 ///////////////////////////////////////////////////////////////////////////
264 //  Function : OnObtainScreenShotRequest
265 //  brief    : Collect screen shot artifact from PLM and return a response
266 //             to SS_Logger.
267 ///////////////////////////////////////////////////////////////////////////
268 EFrameworkunifiedStatus CErrorEvent::OnObtainScreenShotRequest(HANDLE f_hApp) {
269   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
270
271   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
272
273   l_eStatus = SendLogArtifactResponseToSelf(f_hApp, eArtifactIdScreenShot,
274   SCREEN_SHOT_PATH);
275   LOG_STATUS_IF_ERRORED(
276       l_eStatus, "SendLogArtifactResponseToSelf(eArtifactIdScreenShot)");
277   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
278   return l_eStatus;
279 }
280
281 ///////////////////////////////////////////////////////////////////////////
282 //  Function : OnObtainScreenShotResponse
283 //  brief    : Callback from PLM when the screen shot file has been written
284 //             to storage.  The data received contains the file path and
285 //             name to the written screen shot file. See
286 //             SS_LOGGER_SCREENCAPTURE_EVT_ACK.
287 ///////////////////////////////////////////////////////////////////////////
288 EFrameworkunifiedStatus CErrorEvent::OnObtainScreenShotResponse(HANDLE f_hApp) {
289   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
290   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
291   STScreenCaptureEvt l_ScreenCaptureEvtAck;
292
293   if (eFrameworkunifiedStatusOK != (l_eStatus = ReadMsg<STScreenCaptureEvt>(f_hApp, l_ScreenCaptureEvtAck))) {  // LCOV_EXCL_BR_LINE 200:To ensure success  // NOLINT[whitespace/line_length]
294     // LCOV_EXCL_START 200:To ensure success
295     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
296     LOG_ERROR("ReadMsg()");
297     // LCOV_EXCL_STOP
298   } else {
299     if (FALSE == l_ScreenCaptureEvtAck.fSucessful) {
300       FRAMEWORKUNIFIEDLOG(
301           ZONE_ERR,
302           __FUNCTION__,
303           " Error. Screen shot acquisition failed. l_ScreenCaptureEvtAck.fSucessful == FALSE.");
304       l_ScreenCaptureEvtAck.strNameAndLocation[0] = '\0';
305     }
306
307     l_eStatus = SendLogArtifactResponseToSelf(
308         f_hApp, eArtifactIdScreenShot,
309         l_ScreenCaptureEvtAck.strNameAndLocation);  // LCOV_EXCL_BR_LINE 11:Unexpected branch
310
311     LOG_STATUS_IF_ERRORED(  // LCOV_EXCL_BR_LINE 5:macro
312         l_eStatus, "SendLogArtifactResponseToSelf(eArtifactIdScreenShot)");
313   }
314
315   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
316   return (l_eStatus);
317 }
318
319 ///////////////////////////////////////////////////////////////////////////
320 //  Function : OnObtainKernelLogInfoRequest
321 //  brief    : Collect kernel log artifact and return a response to
322 //             SS_Logger.
323 ///////////////////////////////////////////////////////////////////////////
324 EFrameworkunifiedStatus CErrorEvent::OnObtainKernelLogInfoRequest(HANDLE f_hApp) {
325   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
326
327   EFrameworkunifiedStatus l_eStatus = CreateKernelLog(f_hApp, SS_LOGGER_KLOG_GET);
328
329   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
330   return (l_eStatus);
331 }
332
333 ///////////////////////////////////////////////////////////////////////////
334 //  Function : OnObtainDRInitialLogRequest
335 //  brief    : Collect DRInitialLog artifact and return a response to
336 //             SS_Logger.
337 ///////////////////////////////////////////////////////////////////////////
338 EFrameworkunifiedStatus CErrorEvent::OnObtainDRInitialLogRequest(HANDLE f_hApp) {
339   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
340   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
341   UI_32 l_DumpSize = 0;
342
343   CNSSharedMemReader* l_pDRInitial = new (std::nothrow) CNSSharedMemReader(
344       SHMEM_DRINITIALLOG, FALSE);
345
346   if (NULL == l_pDRInitial) {  // LCOV_EXCL_BR_LINE 5: new's error case
347     l_eStatus = eFrameworkunifiedStatusInvldHandle;
348     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: DRInitial SharedMem handle is NULL");
349   } else if (eFrameworkunifiedStatusOK != (l_eStatus = l_pDRInitial->Open())) {
350     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: DRInitial Open failed %d", l_eStatus);
351   } else if (eFrameworkunifiedStatusOK
352       != (l_eStatus = l_pDRInitial->DumpToFile(DRINITIAL_LOG_PATH_FN,
353                                                &l_DumpSize))) {
354     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: DRInitial DumpToFile failed %d",
355            l_eStatus);
356   } else {
357     l_eStatus = SendLogArtifactResponseToSelf(f_hApp, eArtifactIdDRInitialLog,
358                                               DRINITIAL_LOG_PATH_FN);
359     LOG_STATUS_IF_ERRORED(
360         l_eStatus, "SendLogArtifactResponseToSelf(eArtifactIdDRInitialLog)");
361   }
362
363   if (NULL != l_pDRInitial) {  // LCOV_EXCL_BR_LINE 5: new's error case
364     (void) l_pDRInitial->Close();
365   }
366
367   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
368   return (l_eStatus);
369 }
370
371 ///////////////////////////////////////////////////////////////////////////
372 //  Function : OnObtainDRLocationLogRequest
373 //  brief    : Collect DRLocationLog artifact and return a response to
374 //             SS_Logger.
375 ///////////////////////////////////////////////////////////////////////////
376 EFrameworkunifiedStatus CErrorEvent::OnObtainDRLocationLogRequest(HANDLE f_hApp) {
377   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
378   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
379   UI_32 l_DumpSize = 0;
380
381   CNSSharedMemReader* l_pDRLocation = new (std::nothrow) CNSSharedMemReader(
382       SHMEM_DRLOCATIONLOG, FALSE);
383
384   if (NULL == l_pDRLocation) {  // LCOV_EXCL_BR_LINE 5: new's error case
385     l_eStatus = eFrameworkunifiedStatusInvldHandle;
386     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: DRInitial SharedMem handle is NULL");
387   } else if (eFrameworkunifiedStatusOK != (l_eStatus = l_pDRLocation->Open())) {
388     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: DRLocation Open failed %d",
389            l_eStatus);
390   } else if (eFrameworkunifiedStatusOK
391       != (l_eStatus = l_pDRLocation->DumpToFile(DRLOCATION_LOG_PATH_FN,
392                                                 &l_DumpSize))) {
393     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: DRLocation DumpToFile failed %d",
394            l_eStatus);
395   } else {
396     l_eStatus = SendLogArtifactResponseToSelf(f_hApp, eArtifactIdDRLocationLog,
397                                               DRLOCATION_LOG_PATH_FN);
398
399     LOG_STATUS_IF_ERRORED(
400         l_eStatus, "SendLogArtifactResponseToSelf(eArtifactIdDRLocationLog)");
401   }
402
403   if (NULL != l_pDRLocation) {  // LCOV_EXCL_BR_LINE 5: new's error case
404     (void) l_pDRLocation->Close();
405     delete l_pDRLocation;
406   }
407
408   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
409   return (l_eStatus);
410 }
411
412 ///////////////////////////////////////////////////////////////////////////
413 //  Function : OnClearAllLogRequest
414 //  brief    : Callback from LoggerService when the event log file has been delete
415 //             from storage.  The data received contains the file path.
416 ///////////////////////////////////////////////////////////////////////////
417 EFrameworkunifiedStatus CErrorEvent::OnClearAllLogRequest(HANDLE f_hApp) {
418   std::string clear_file_path = CLEAR_LOG_PATH_FN;
419   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
420   EFrameworkunifiedStatus l_eStatus;
421
422   if (CFSDirectory::RemoveSubDirectory(clear_file_path)) {
423     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
424            "SS_Logger Clear %s directory delete successful.",
425            clear_file_path.c_str());
426   } else {
427     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
428            "SS_Logger Clear %s directory delete failed.",
429            clear_file_path.c_str());
430   }
431
432   clear_file_path = "/nv/BS/ss/logger_service/rwdata/log2/awlog/";
433   if (CFSDirectory::RemoveSubDirectory(clear_file_path)) {
434     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
435            "SS_Logger Clear %s directory delete successful.",
436            clear_file_path.c_str());
437   } else {
438     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
439            "SS_Logger Clear %s directory delete failed.",
440            clear_file_path.c_str());
441   }
442
443   clear_file_path = "/nv/BS/ss/logger_service/rwdata/log2/core/";
444   if (CFSDirectory::RemoveSubDirectory(clear_file_path)) {
445     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
446            "SS_Logger Clear %s directory delete successful.",
447            clear_file_path.c_str());
448   } else {
449     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
450            "SS_Logger Clear %s directory delete failed.",
451            clear_file_path.c_str());
452   }
453
454   l_eStatus = SendLogArtifactResponseToSelf(f_hApp, eArtifactIdClearAllLog,
455                                             clear_file_path.c_str());
456   LOG_STATUS_IF_ERRORED(l_eStatus,
457                         "SendLogArtifactResponseToSelf(eArtifactIdClearAllLog)");
458   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
459   return (l_eStatus);
460 }
461
462 ///////////////////////////////////////////////////////////////////////////
463 //  Function : OnNaviLogRequest
464 //  brief    : Callback from LoggerService when the event log file has been xxxxxx BUGBUG
465 //             from storage.  The data received contains the file path.
466 ///////////////////////////////////////////////////////////////////////////
467 EFrameworkunifiedStatus CErrorEvent::OnNaviLogRequest(HANDLE f_hApp) {
468   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
469   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
470
471   EFrameworkunifiedStatus naviLog_status;
472   switch (m_errorEventNtfData.EventType) {
473     case eErrorEventTypeProcessCrash:
474     case eErrorEventTypeProcessExit:
475     case eErrorEventTypeHeartBeatFailure:
476     case eErrorEventTypeSystemLowMemory:
477     case eErrorEventTypeBootMicroReset:
478     case eErrorEventTypeModConnFailed:
479     case eErrorEventTypeStartRespFailed:
480     case eErrorEventTypeModuleInvokedResetRequest:
481     case eErrorEventTypeGroupRelaunch:
482       naviLog_status = SaveNaviLog(epssdmsdtGENERIC_ERROR_RESET);
483       break;
484     default:
485       naviLog_status = SaveNaviLog(epssdmsdtNORMAL_RESET);
486       break;
487   }
488   FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: Fail to save Navi Log %d",
489          naviLog_status);
490
491   l_eStatus = SendLogArtifactResponseToSelf(f_hApp, eArtifactIdNaviLog, "");
492   LOG_STATUS_IF_ERRORED(l_eStatus,
493                         "SendLogArtifactResponseToSelf(eArtifactIdNaviLog)");
494   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
495   return (l_eStatus);
496 }
497
498 EFrameworkunifiedStatus CErrorEvent::CheckPathForArtifact(HANDLE f_hApp,
499                                              TLoggingArtifact f_artifact) {
500   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
501   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldParam;
502   std::string l_Path;
503
504   if (f_artifact.Information.Cb.Type == eCbTypePath) {
505     l_Path = f_artifact.Information.Cb.Path;
506     if (access(l_Path.c_str(), R_OK) != 0) {
507       FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
508              "File/Directory %s not found. Returning empty string.",
509              l_Path.c_str());
510       l_Path = "";
511     }
512     l_eStatus = SendLogArtifactResponseToSelf(f_hApp, f_artifact.ArtifactId,
513                                               l_Path.c_str());
514
515     LOG_STATUS_IF_ERRORED(l_eStatus, "SendLogArtifactResponseToSelf()");
516   }
517
518   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
519   return (l_eStatus);
520 }
521
522 /* Read all messages remaining in the ring buffer. (allowed for non-root) */
523 #define SYSLOG_ACTION_READ_ALL       3
524 /* Return size of the log buffer */
525 #define SYSLOG_ACTION_SIZE_BUFFER   10
526
527 EFrameworkunifiedStatus CErrorEvent::CreateKernelLog(HANDLE f_hApp,
528                                         SS_LOGGER_KLOG_OPE_TYPE type) {
529   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
530   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
531   int len;
532   int fd;
533   char* buf;
534   ssize_t wr_len;
535
536   len = klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0);
537   if (len < 0) {  // LCOV_EXCL_BR_LINE 5: klogctl's error case
538     // LCOV_EXCL_START 5: klogctl's error case
539     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
540     l_eStatus = eFrameworkunifiedStatusFail;
541     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
542            "Error. Failed to get syslog buffer size: %d, %s", errno,
543            strerror(errno));
544     // LCOV_EXCL_STOP
545   } else {
546     buf = reinterpret_cast<char*>(malloc(len));
547     if (buf != NULL) {  // LCOV_EXCL_BR_LINE 5: new's error case
548       len = klogctl(SYSLOG_ACTION_READ_ALL, buf, len);
549       if ((len < 0) || (len == 0)) {  // LCOV_EXCL_BR_LINE 5: klogctl's error case
550         // LCOV_EXCL_START 5: klogctl's error case
551         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
552         l_eStatus = eFrameworkunifiedStatusFail;
553         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
554                "Error. Failed to read syslog buffer: %d, %s", errno,
555                strerror(errno));
556         // LCOV_EXCL_STOP
557       } else {
558         std::string file_put_path = "";
559         EArtifactId artifact_id = eArtifactIdMaxValue;
560         switch (type) {
561           case SS_LOGGER_KLOG_GET:
562             file_put_path = KERNEL_LOG_PATH_FN;
563             artifact_id = eArtifactIdKernelLog;
564             fd = open(file_put_path.c_str(),
565                       O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
566             break;
567           case SS_LOGGER_KBOOTLOG_CREATE:
568             file_put_path = KERNEL_BOOT_LOG_PATH_FN;
569             artifact_id = eArtifactIdKernelBootLog;
570             fd = open(file_put_path.c_str(),
571                       O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
572             break;
573           default:
574             fd = -1;
575             break;
576         }
577
578         if (fd == -1) {
579           l_eStatus = eFrameworkunifiedStatusFail;
580           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
581                  "Error. Failed to open file: %s: %d, %s",
582                  file_put_path.c_str(), errno, strerror(errno));
583         } else {
584           wr_len = write(fd, buf, len);
585           if (wr_len == -1) {  // LCOV_EXCL_BR_LINE 5: write's error case
586             // LCOV_EXCL_START 5: write's error case
587             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
588             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
589                    " Error. File write failed. errno: %d, %s", errno,
590                    strerror(errno));
591             // LCOV_EXCL_STOP
592           }
593           fsync(fd);
594           close(fd);
595
596           if (SS_LOGGER_KLOG_GET == type) {
597             l_eStatus = SendLogArtifactResponseToSelf(f_hApp, artifact_id,
598                                                       file_put_path.c_str());
599             if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
600               // LCOV_EXCL_START 4: NSFW error case.
601               AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
602               FRAMEWORKUNIFIEDLOG(
603                   ZONE_ERR,
604                   __FUNCTION__,
605                   "SendLogArtifactResponseToSelf failed. EFrameworkunifiedStatus:%d, ArtifactID:%d, Path:%s",
606                   l_eStatus, artifact_id, file_put_path.c_str());
607               // LCOV_EXCL_STOP
608             }
609           }
610         }
611       }
612       free(buf);
613     } else {
614       // LCOV_EXCL_START 5: new's error case
615       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
616       l_eStatus = eFrameworkunifiedStatusFail;
617       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Memory allocation Failed %s",
618              strerror(errno));
619       // LCOV_EXCL_STOP
620     }
621   }
622   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
623   return (l_eStatus);
624 }
625