-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.c
93 lines (82 loc) · 2.67 KB
/
wrapper.c
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
/*******************************************************************************
* Author: Andrew Seligman
* Date: March 12, 2016
* File: wrapper.c
* Purpose: This program accepts a program name as a command line argument
* and creates a new process to run that program. Once the given
* program exits, this program will catch errors and restart
* the given program after a short period of time.
*******************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <logger.h>
//number of seconds to wait before restarting given program
#define RESTARTAFTER 30
#define LOGFILE "[path]/powerdetect/log/general.txt"
void createChild(char*);
int main(int argc, char** argv)
{
char* usage = "Usage: %s program_name\n";
if(argc != 2) {
printf(usage, argv[0]);
return 1;
}
while(1) {
createChild(argv[1]);
sleep(RESTARTAFTER);
}
return 0;
}
/*******************************************************************************
* Function name: createChild
*
* Description: Creates a child process to execute the program name passed in
*
* Parameters: char* program - IMPORT - name of the program to execute
*
* Return Value: none
*******************************************************************************/
void createChild(char* program)
{
char message[80];
int status;
pid_t pid;
sprintf(message, "Parent PID = %d creating new process", getpid());
logEvent(LOGFILE, message);
if((pid = fork()) < 0) {
perror("fork");
return;
}
if (pid == 0) {
//child process code
int childPID = getpid();
sprintf(message, "Child process created: PID = %d", childPID);
logEvent(LOGFILE, message);
if(system(program) == -1) {
perror("program init");
exit(-1);
}
exit(0);
}
//parent process code
pid = wait(&status);
if(WIFEXITED(status)) {
sprintf(message, "PID %d exits: %d", pid, WEXITSTATUS(status));
logEvent(LOGFILE, message);
}
else if(WIFSTOPPED(status)) {
sprintf(message, "PID %d stopped by: %d", pid, WSTOPSIG(status));
logEvent(LOGFILE, message);
}
else if(WIFSIGNALED(status)) {
sprintf(message, "PID %d killed by: %d", pid, WTERMSIG(status));
logEvent(LOGFILE, message);
}
else
perror("waitpid");
}