-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdcm.c
More file actions
executable file
·420 lines (360 loc) · 11.5 KB
/
dcm.c
File metadata and controls
executable file
·420 lines (360 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
* Copyright 2024 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include "dcm_types.h"
#include "dcm_utils.h"
#include "dcm_parseconf.h"
#include "dcm.h"
#include "dcm_rbus.h"
#include "dcm_cronparse.h"
#include "dcm_schedjob.h"
#include "uploadstblogs.h"
static DCMDHandle *g_pdcmHandle = NULL;
/** @brief Call back function from Scheduler. This function
* Initiates the job
*
* @param[in] profileName Scheduler name
*
* @return None.
* @retval None.
*/
static VOID dcmRunJobs(const INT8* profileName, VOID *pHandle)
{
DCMDHandle *pdcmHandle = (DCMDHandle *)pHandle;
if(pHandle == NULL) {
DCMError("Input Handle is NULL\n");
return;
}
if(dcmSettingsGetMMFlag()) {
DCMInfo("Maintenance manager enabled device - Cron job schedules for maintenance activities are disabled\n");
return;
}
INT8 *pRDKPath = dcmSettingsGetRDKPath(pdcmHandle->pDcmSetHandle);
INT8 *pExecBuff = pdcmHandle->pExecBuff;
if(pRDKPath == NULL) {
DCMWarn("RDK Patch is NULL, using %s\n", DCM_LIB_PATH);
pRDKPath = DCM_LIB_PATH;
}
if(strcmp(profileName, DCM_LOGUPLOAD_SCHED) == 0) {
INT8 *pPrctl = dcmSettingsGetUploadProtocol(pdcmHandle->pDcmSetHandle);
INT8 *pURL = dcmSettingsGetUploadURL(pdcmHandle->pDcmSetHandle);
if(pPrctl == NULL) {
DCMWarn("Log Upload protocol is NULL, using HTTP\n");
pPrctl = "HTTP";
}
if(pURL == NULL) {
DCMWarn("Log Upload URL is NULL, using %s\n", DCM_DEF_LOG_URL);
pURL = DCM_DEF_LOG_URL;
}
DCMInfo("\nStart log upload via library API\n");
// Call uploadstblogs library API instead of shell script
UploadSTBLogsParams params = {
.flag = 0,
.dcm_flag = 1,
.upload_on_reboot = false,
.upload_protocol = pPrctl,
.upload_http_link = pURL,
.trigger_type = TRIGGER_SCHEDULED,
.rrd_flag = false,
.rrd_file = NULL
};
#ifndef GTEST_ENABLE
int result = uploadstblogs_run(¶ms);
if (result != 0) {
DCMError("Log upload failed with error code: %d\n", result);
} else {
DCMInfo("Log upload completed successfully\n");
}
#endif
}
else if(strcmp(profileName, DCM_DIFD_SCHED) == 0) {
DCMInfo("Start FW update Script\n");
snprintf(pExecBuff, EXECMD_BUFF_SIZE, "/bin/sh %s/swupdate_utility.sh 0 2 >> /opt/logs/swupdate.log 2>&1",
pRDKPath);
}
dcmUtilsSysCmdExec(pExecBuff);
}
/** @brief Signal handler, un-intializes the module before exiting
*
* @param[in] sig signal type
*
* @return None.
* @retval None.
*/
static VOID sig_handler(INT32 sig)
{
INT32 ret = DCM_SUCCESS;
if ( sig == SIGINT || sig == SIGTERM ||
sig == SIGKILL || sig == SIGABRT) {
DCMDebug("SIGINT received!\n");
// As per the script, send MAINT_DCM_ERROR, MAINT_RFC_ERROR, MAINT_FWDOWNLOAD_ERROR,
// MAINT_LOGUPLOAD_ERROR
ret = dcmIARMEvntSend(DCM_IARM_ERROR);
if(ret) {
DCMWarn("Failed to send Event\n");
}
dcmDaemonMainUnInit(g_pdcmHandle);
exit(1);
}
}
/** @brief Initializes all modules
*
* @param[in] None
*
* @return Returns the status of the operation.
* @retval Returns DCM_SUCCESS on Success, DCM_FAILURE otherwise.
*/
INT32 dcmDaemonMainInit(DCMDHandle *pdcmHandle)
{
INT32 ret = DCM_SUCCESS;
INT8 t2_ver[32];
/* Check if the Daemon is already running */
ret = dcmUtilsCheckDaemonStatus();
if(ret) {
pdcmHandle->isDCMRunning = true;
DCMError("DCM Daemon is already running\n");
return ret;
}
/* Initialize the Parser */
ret = dcmSettingsInit(&pdcmHandle->pDcmSetHandle);
if(ret) {
DCMError("Failed to init settings\n");
return ret;
}
/* register the signals */
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGKILL, sig_handler);
signal(SIGABRT, sig_handler);
ret = dcmRbusInit(&pdcmHandle->pRbusHandle);
if (ret != DCM_SUCCESS) {
DCMError("Failed to init rbus\n");
return ret;
}
DCMInfo("T2 is enabled\n");
ret = dcmRbusGetT2Version(pdcmHandle->pRbusHandle, t2_ver);
DCMInfo("T2 Version: %s\n", t2_ver);
/* Initialize Rbus */
ret = dcmRbusSubscribeEvents(pdcmHandle->pRbusHandle);
if (ret != DCM_SUCCESS) {
DCMError("Failed to init rbus\n");
return ret;
}
pdcmHandle->pExecBuff = malloc(EXECMD_BUFF_SIZE);
if(pdcmHandle->pExecBuff == NULL) {
DCMError("Failed to allocate memeory\n");
return DCM_FAILURE;
}
/* Initialize Schecduler */
ret = dcmSchedInit();
if(ret) {
DCMError("Failed to init scheduler\n");
return ret;
}
/* Add log upload job to Schecduler */
pdcmHandle->pLogSchedHandle = dcmSchedAddJob(DCM_LOGUPLOAD_SCHED,
(DCMSchedCB)dcmRunJobs,
(VOID *) pdcmHandle);
if(pdcmHandle->pLogSchedHandle == NULL) {
DCMError("Failed to Add Log Scheduler jobs\n");
return DCM_FAILURE;
}
/* Add FW update job to Schecduler */
pdcmHandle->pDifdSchedHandle = dcmSchedAddJob(DCM_DIFD_SCHED,
(DCMSchedCB)dcmRunJobs,
(VOID *) pdcmHandle);
if(pdcmHandle->pDifdSchedHandle == NULL) {
DCMError("Failed to Add FW Updater Scheduler jobs\n");
return DCM_FAILURE;
}
return ret;
}
/** @brief De-Initializes all modules
*
* @param[in] None
*
* @return None.
* @retval None.
*/
VOID dcmDaemonMainUnInit(DCMDHandle *pdcmHandle)
{
if(pdcmHandle->isDCMRunning == false) {
dcmUtilsRemovePIDfile();
}
if(pdcmHandle->pExecBuff) {
free(pdcmHandle->pExecBuff);
}
dcmSettingsUnInit(pdcmHandle->pDcmSetHandle);
dcmRbusUnInit(pdcmHandle->pRbusHandle);
dcmSchedStopJob(pdcmHandle->pLogSchedHandle);
dcmSchedStopJob(pdcmHandle->pDifdSchedHandle);
dcmSchedRemoveJob(pdcmHandle->pLogSchedHandle);
dcmSchedRemoveJob(pdcmHandle->pDifdSchedHandle);
dcmSchedUnInit();
memset(pdcmHandle, 0, sizeof(DCMDHandle));
}
/** @brief Main function
*
* @param[in] argc No. of arguments
* @param[in] argv arguments array
*
* @return status.
* @retval status.
*/
#ifndef GTEST_ENABLE
int main(int argc, char* argv[])
{
pid_t process_id = 0;
pid_t sid = 0;
INT32 ret = DCM_SUCCESS;
INT32 count = 0;
DCMLOGInit();
g_pdcmHandle = malloc(sizeof(DCMDHandle));
if(g_pdcmHandle == NULL) {
DCMError("Memory allocation failed, Closing the daemon\n");
return DCM_FAILURE;
}
memset(g_pdcmHandle, 0, sizeof(DCMDHandle));
g_pdcmHandle->isDebugEnabled = true;
DCMInfo("Starting DCM Process: %d\n", getpid());
/* Create child process */
process_id = fork();
if (process_id < 0) {
DCMError("fork failed!\n");
ret = DCM_FAILURE;
goto exit2;
} else if (process_id > 0) {
DCMInfo("Exiting the main process\n");
return 0;
}
/* unmask the file mode */
umask(0);
/* set new session */
sid = setsid();
if (sid < 0) {
DCMError("setsid failed!\n");
ret = DCM_FAILURE;
goto exit2;
}
/* Change the current working directory to root. */
chdir("/");
if (g_pdcmHandle->isDebugEnabled != true) {
/* Close stdin. stdout and stderr */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
DCMDebug("Initializing DCM Component: %d\n", getpid());
ret = dcmDaemonMainInit(g_pdcmHandle);
if(ret) {
DCMError("Initialization of DCM Process Failed, closing the DCM Process!!!\n");
goto exit1;
}
DCMDebug("Initializing DCM Component Done\n");
//#ifdef DCM_DEFAULT_BOOTCONFIG // make this default as this is inline with legacy implementation
DCMInfo("Loading the Default config\n");
ret = dcmSettingDefaultBoot();
if(ret != DCM_SUCCESS) {
DCMError("Failed to load default config file\n");
}
//#endif
while(1) {
if(count > 10) {
DCMInfo("Waiting for Telemetry to up and running to Subscribe the events\n");
count = 0;
}
count++;
if(dcmRbusGetEventSubStatus(g_pdcmHandle->pRbusHandle)) {
break;
}
sleep(1);
}
DCMInfo("Telemetry Events subscriptions is success\n");
ret = dcmRbusSendEvent(g_pdcmHandle->pRbusHandle);
if(ret) {
DCMError("Reload config event failed!!!\n");
}
DCMInfo("Sent Event to telemetry for configuraion path\n");
while(1) {
INT32 ret = DCM_SUCCESS;
INT8 *pconfPath = NULL;
/* Wait for event Device.DCM.Processconfig */
if(dcmRbusSchedJobStatus(g_pdcmHandle->pRbusHandle)) {
DCMInfo("Start Scheduling\n");
pconfPath = dcmRbusGetConfPath(g_pdcmHandle->pRbusHandle);
if(pconfPath == NULL) {
DCMWarn("conf file pointer is null\n");
sleep(1);
continue;
}
ret = dcmSettingParseConf(g_pdcmHandle->pDcmSetHandle, pconfPath,
g_pdcmHandle->logCron,
g_pdcmHandle->difdCron);
if(ret == DCM_SUCCESS) {
dcmSchedStartJob(g_pdcmHandle->pLogSchedHandle, g_pdcmHandle->logCron);
dcmSchedStartJob(g_pdcmHandle->pDifdSchedHandle, g_pdcmHandle->difdCron);
ret = dcmIARMEvntSend(DCM_IARM_COMPLETE);
if(ret) {
DCMError("Failed to send Event\n");
}
}
else {
DCMWarn("Failed to parse the conf file\n");
}
dcmRbusSchedResetStatus(g_pdcmHandle->pRbusHandle);
}
sleep(1);
} //while(1)
DCMInfo("Exiting DCM Component\n");
exit1:
dcmDaemonMainUnInit(g_pdcmHandle);
exit2:
if(g_pdcmHandle) {
free(g_pdcmHandle);
g_pdcmHandle = NULL;
}
if(ret) {
ret = dcmIARMEvntSend(DCM_IARM_ERROR);
if(ret) {
DCMError("Failed to send Event\n");
}
}
return ret;
}
#endif
#ifdef GTEST_ENABLE
void get_dcmRunJobs(const INT8* profileName, VOID *pHandle)
{
dcmRunJobs(profileName, pHandle);
}
void get_sig_handler(INT32 sig)
{
sig_handler(sig);
}
#endif