-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdcm_utils.c
More file actions
262 lines (231 loc) · 6.84 KB
/
Copy pathdcm_utils.c
File metadata and controls
262 lines (231 loc) · 6.84 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
/*
* 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 <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <dirent.h>
#include <errno.h>
#include "dcm_types.h"
#include "dcm_utils.h"
#include "dcm_parseconf.h"
#ifdef RDK_LOGGER_ENABLED
INT32 g_rdk_logger_enabled = 0;
#endif
#define IARM_BUS_DCM_NAME "DCM"
void DCMLOGInit()
{
#ifdef RDK_LOGGER_ENABLED
if (0 == rdk_logger_init(DEBUG_INI_NAME)) {
g_rdk_logger_enabled = 1;
}
#endif
}
/** @brief This Function checks if the file present or not.
*
* @param[in] file_name file path
*
* @return Returns the status of the operation.
* @retval Returns DCM_SUCCESS if present, DCM_FAILURE otherwise.
*/
INT32 dcmUtilsFilePresentCheck(const INT8 *file_name)
{
INT32 ret = DCM_SUCCESS;
if(file_name == NULL) {
DCMError("Invalid Parameter\n");
return DCM_FAILURE;
}
struct stat sfile;
memset(&sfile, '\0', sizeof(sfile));
if(stat(file_name, &sfile)) {
return DCM_FAILURE;
}
return ret;
}
/** @brief This Function executes the system command and copies the output.
*
* @param[in] cmd command to be executed
* @param[out] out Output buffer
* @param[in] len Output buffer length
*
* @return None.
* @retval None.
*/
VOID dcmUtilsCopyCommandOutput (INT8 *cmd, INT8 *out, INT32 len)
{
FILE *fp;
if(out != NULL)
out[0] = 0;
fp = popen (cmd, "r");
if (fp) {
if(out) {
if (fgets (out, len, fp) != NULL) {
size_t str_len = strlen (out);
if ((str_len > 0) && (out[str_len - 1] == '\n'))
out[str_len - 1] = 0;
}
}
pclose (fp);
}
else {
DCMWarn("Failed to run the command: %s\n", cmd);
}
}
/** @brief This Function executes the system command.
*
* @param[in] cmd command to be executed
*
* @return Returns the status of the operation.
* @retval Returns DCM_SUCCESS on success, DCM_FAILURE otherwise.
*/
INT32 dcmUtilsSysCmdExec(INT8 *cmd)
{
INT32 ret = DCM_SUCCESS;
if (cmd == NULL) {
DCMError("parameter is NULL\n");
return DCM_FAILURE;
}
dcmUtilsCopyCommandOutput(cmd, NULL, 0);
return ret;
}
/** @brief This Function checks if daemon is already running or not.
*
* @param[in] None
*
* @return Returns the status of the daemon.
* @retval Returns DCM_SUCCESS if daemon not running, DCM_FAILURE otherwise.
*/
INT32 dcmUtilsCheckDaemonStatus()
{
FILE *fp = NULL;
INT8 PID[32];
INT8 filePath[64];
fp = fopen(DCM_PID_FILE, "r");
if (NULL != fp) {
/* exit if an instance is already running */
fgets(PID, sizeof(PID), fp);
fclose(fp);
snprintf(filePath, sizeof(filePath), "/proc/%s", PID);
if (dcmUtilsFilePresentCheck(filePath) == 0) {
DCMWarn("Daemon is already running %s\n", filePath);
return DCM_FAILURE;
}
DCMInfo("pid file not present %s\n", filePath);
}
DCMInfo("Opening new pid file\n");
fp = fopen(DCM_PID_FILE, "w");
if(fp == NULL) {
DCMWarn("Failed to open PID file %s\n", DCM_PID_FILE);
return DCM_FAILURE;
}
fprintf(fp, "%d", getpid());
fclose(fp);
return DCM_SUCCESS;
}
/** @brief This Function removes the dcm PID file.
*
* @param[in] None
*
* @return None.
* @retval None.
*/
VOID dcmUtilsRemovePIDfile()
{
FILE *fp = NULL;
fp = fopen(DCM_PID_FILE, "r");
if(fp) {
fclose(fp);
errno = 0;
if (remove(DCM_PID_FILE) != 0) {
if (errno != ENOENT) {
DCMError("Failed to remove PID file: %s errno=%d (%s)\n",
DCM_PID_FILE, errno, strerror(errno));
}
}
}
}
/** @brief This Function sends events to MM using IARM bus.
*
* @param[in] status Event Status
*
* @return Returns the status of the operation.
* @retval Returns DCM_SUCCESS on success, DCM_FAILURE otherwise.
*/
INT32 dcmIARMEvntSend(INT32 status)
{
INT32 ret = DCM_SUCCESS;
return ret;
}
/**
* @brief Fetch the value of a given entry from a file (simple key=value format).
*
* This function searches for a line in the form "key=value" within the specified file and returns
* a heap-allocated string containing the associated value for the given key.
*
* @param[in] fileName Path to the file to search (must not be NULL).
* @param[in] searchEntry Entry key to search for (must not be NULL).
*
* @return Pointer to a heap-allocated string containing the entry value (must be freed by the caller),
* or NULL if the entry is not found or if an error occurs.
*
* @note This function expects:
* - The file contains lines in the format "key=value" (no comments).
* - There is no whitespace before or after the '=' character.
* - The search is case-sensitive.
*/
INT8* dcmUtilsGetFileEntry(const INT8* fileName, const INT8* searchEntry)
{
if(fileName == NULL || searchEntry == NULL)
{
return NULL;
}
FILE *fp = NULL;
char* entryValue = NULL;
fp = fopen(fileName, "r");
if(NULL != fp)
{
char line[512];
size_t entryLen = strlen(searchEntry);
while (fgets(line, sizeof(line), fp))
{
if (line[entryLen] == '=' && strncmp(line, searchEntry, entryLen) == 0 )
{
char *value = line + entryLen + 1;
char *checkNewline = strpbrk(value, "\r\n"); // just a saftey check
if (checkNewline)
{
*checkNewline = '\0';
}
entryValue = strdup(value);
break;
}
}
fclose(fp);
}
return entryValue;
}