Skip to content

Commit 81ee171

Browse files
committed
Move process control functions into process.c
1 parent 893b0fe commit 81ee171

File tree

4 files changed

+369
-185
lines changed

4 files changed

+369
-185
lines changed

src/apps.c

Lines changed: 23 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "apps.h"
2222
#include "config.h"
23+
#include "process.h"
2324
#include "log.h"
2425
#include "utils.h"
2526

@@ -165,213 +166,36 @@ int set_ini_file(char *path)
165166

166167
//------------------------------------------------------------------
167168

169+
// Wrapper functions for backward compatibility - delegate to process module
170+
// TODO : inline wrapper functions
168171
bool is_application_running(int i)
169172
{
170-
if(apps[i].pid <= 0)
171-
{
172-
return false;
173-
}
174-
175-
// Check if the application is running
176-
if(kill(apps[i].pid, 0) == 0)
177-
{
178-
return true; // Process is running
179-
}
180-
else if(errno == EPERM)
181-
{
182-
LOGE("No permission to check if process %s is running : %s", apps[i].name, strerror(errno));
183-
return true;
184-
}
185-
else
186-
{
187-
LOGD("Process %s is not running : %s", apps[i].name, strerror(errno));
188-
}
189-
190-
return false;
173+
return process_is_running(i);
191174
}
192175

193176
bool is_application_started(int i)
194177
{
195-
return apps[i].started;
178+
return process_is_started(i);
196179
}
197180

198181
bool is_application_start_time(int i)
199182
{
200-
return (get_uptime() - app_state.uptime) >= (long)apps[i].start_delay;
183+
return process_is_start_time(i);
201184
}
202185

203186
void start_application(int i)
204187
{
205-
apps[i].pid = 0;
206-
// Start the application on Linux
207-
pid_t pid = fork();
208-
209-
if(pid < 0)
210-
{
211-
LOGE("Failed to start process %s, error code: %d - %s", apps[i].name, errno, strerror(errno));
212-
}
213-
else if(pid == 0)
214-
{
215-
// Child process
216-
// Reset signals to default
217-
struct sigaction sa;
218-
sa.sa_handler = SIG_DFL;
219-
sigemptyset(&sa.sa_mask);
220-
sa.sa_flags = 0;
221-
sigaction(SIGINT, &sa, NULL);
222-
sigaction(SIGTERM, &sa, NULL);
223-
sigaction(SIGQUIT, &sa, NULL);
224-
sigaction(SIGUSR1, &sa, NULL);
225-
sigaction(SIGUSR2, &sa, NULL);
226-
LOGD("Starting the process %s with CMD : %s", apps[i].name, apps[i].cmd);
227-
run_command(apps[i].cmd);
228-
LOGE("Process %s stopped running", apps[i].name);
229-
exit(EXIT_FAILURE); // exit child process
230-
}
231-
else
232-
{
233-
// Parent process
234-
apps[i].started = true;
235-
apps[i].first_heartbeat = false;
236-
apps[i].pid = pid;
237-
LOGI("Process %s started (PID %d): %s", apps[i].name, apps[i].pid, apps[i].cmd);
238-
update_heartbeat_time(i);
239-
}
188+
process_start(i);
240189
}
241190

242191
void kill_application(int i)
243192
{
244-
bool killed = false;
245-
LOGD("Killing process %s", apps[i].name);
246-
247-
if(apps[i].pid <= 0)
248-
{
249-
return;
250-
}
251-
252-
if(kill(apps[i].pid, SIGTERM) < 0 && errno != ESRCH)
253-
{
254-
LOGE("Failed to terminate process %s, error: %d - %s", apps[i].name, errno, strerror(errno));
255-
}
256-
257-
int status;
258-
int max_wait = MAX_WAIT_PROCESS_TERMINATION; // [seconds]
259-
LOGD("Waiting for the process %s", apps[i].name);
260-
261-
do
262-
{
263-
sleep(1);
264-
int ret = waitpid(apps[i].pid, &status, WNOHANG | WUNTRACED | WCONTINUED);
265-
266-
if(ret == 0)
267-
{
268-
LOGD("Process %s is still running", apps[i].name);
269-
}
270-
else if(ret < 0)
271-
{
272-
if(errno == ECHILD)
273-
{
274-
LOGD("Process %s already terminated", apps[i].name);
275-
max_wait = 0;
276-
}
277-
else
278-
{
279-
LOGE("Failed to wait for process %s, error: %d - %s", apps[i].name, errno, strerror(errno));
280-
}
281-
}
282-
else if(ret > 0)
283-
{
284-
if(WIFEXITED(status))
285-
{
286-
LOGD("Process %s exited, status=%d", apps[i].name, WEXITSTATUS(status));
287-
max_wait = 0;
288-
}
289-
else if(WIFSIGNALED(status))
290-
{
291-
LOGD("Process %s killed by signal %d", apps[i].name, WTERMSIG(status));
292-
max_wait = 0;
293-
}
294-
else if(WIFSTOPPED(status))
295-
{
296-
LOGD("Process %s stopped by signal %d", apps[i].name, WSTOPSIG(status));
297-
max_wait = 0;
298-
}
299-
}
300-
301-
max_wait--;
302-
}
303-
while(max_wait > 0);
304-
305-
if(is_application_running(i))
306-
{
307-
LOGD("Sending SIGKILL to process %s", apps[i].name);
308-
309-
if(kill(apps[i].pid, SIGKILL) < 0 && errno != ESRCH)
310-
{
311-
LOGE("Failed to kill process %s, error: %d - %s", apps[i].name, errno, strerror(errno));
312-
}
313-
else
314-
{
315-
LOGI("Process %s killed", apps[i].name);
316-
317-
if(!is_application_running(i))
318-
{
319-
killed = true;
320-
}
321-
}
322-
}
323-
else
324-
{
325-
LOGI("Process %s terminated", apps[i].name);
326-
killed = true;
327-
}
328-
329-
if(killed)
330-
{
331-
apps[i].started = false;
332-
apps[i].first_heartbeat = false;
333-
apps[i].pid = 0;
334-
}
335-
else
336-
{
337-
LOGE("Failed to terminate process %s", apps[i].name);
338-
}
193+
process_kill(i);
339194
}
340195

341196
void restart_application(int i)
342197
{
343-
LOGD("Restarting process %s", apps[i].name);
344-
345-
if(is_application_running(i))
346-
{
347-
kill_application(i);
348-
}
349-
350-
start_application(i);
351-
// Wait for the application to start
352-
int wait_time = 0;
353-
354-
while(wait_time < MAX_WAIT_PROCESS_START)
355-
{
356-
sleep(1);
357-
358-
if(is_application_running(i))
359-
{
360-
break;
361-
}
362-
363-
wait_time++;
364-
}
365-
366-
if(!is_application_running(i))
367-
{
368-
LOGE("Failed to start process %s", apps[i].name);
369-
}
370-
else
371-
{
372-
update_heartbeat_time(i);
373-
LOGI("Process %s restarted successfully", apps[i].name);
374-
}
198+
process_restart(i);
375199
}
376200

377201
int get_app_count(void)
@@ -388,3 +212,17 @@ int get_udp_port(void)
388212
{
389213
return app_state.udp_port;
390214
}
215+
216+
//------------------------------------------------------------------
217+
// External access functions for other modules
218+
//------------------------------------------------------------------
219+
220+
Application_t* apps_get_array(void)
221+
{
222+
return apps;
223+
}
224+
225+
AppState_t* apps_get_state(void)
226+
{
227+
return &app_state;
228+
}

src/apps.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,18 @@ char *get_app_name(int i);
210210
*/
211211
int get_udp_port();
212212

213+
/**
214+
@brief Gets direct access to the applications array for other modules.
215+
216+
@return Pointer to the applications array.
217+
*/
218+
Application_t* apps_get_array(void);
219+
220+
/**
221+
@brief Gets direct access to the application state for other modules.
222+
223+
@return Pointer to the application state structure.
224+
*/
225+
AppState_t* apps_get_state(void);
226+
213227
#endif // APPS_H

0 commit comments

Comments
 (0)