-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathservice-management.c
More file actions
111 lines (85 loc) · 2.56 KB
/
service-management.c
File metadata and controls
111 lines (85 loc) · 2.56 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
// parse and execute services
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include "config.h" // imports rcFile, SERVICE_MAX_ARGS and SERVICE_MAX_BUFFER
#include "service-management.h"
void startServices()
{
FILE *serviceFile;
char lineBuffer[SERVICE_MAX_BUFFER];
serviceFile = fopen(rcFile, "r");
//serviceFile = fopen("/etc/rc.local", "r");
if (serviceFile == NULL)
{
fprintf(stderr, "Unable to read services file (/etc/rc.local). Does it exist?\n");
return;
}
// process every line of *fileLocation
while (fgets(lineBuffer, sizeof(lineBuffer), serviceFile) != NULL)
{
// skip line if it begins with # or //
// TODO: improve this garbage
if (lineBuffer[0] == '\n' || lineBuffer[0] == '#' || (lineBuffer[0] == '/' && lineBuffer[1] == '/'))
{
continue;
}
// remove newline character at the end of the string
lineBuffer[strcspn(lineBuffer, "\n")] = 0;
// switching to pointer to lineBuffer for efficiency
char *command = lineBuffer;
// skip spaces and tabs at line beginning: POINTER++ until it points to a real character
while (*command == ' ' || *command == '\t')
{
command++;
}
// skip line if it's empty after this stage
if (*command == '\0')
{
continue;
}
// self-explanatory
printf("[service] Executing %s\n", command);
/* forking to tokenize and execute command */
pid_t servicePid = fork();
if (servicePid == -1)
{
fprintf(stderr, "[service] Fork failed!!!\n");
}
// in child
else if (servicePid == 0)
{
char *args[SERVICE_MAX_ARGS];
int argCount = 0;
// tokenizer, parse entire string into arguments
char *token = strtok(command, " ");
while (token != NULL && argCount < SERVICE_MAX_ARGS-1) // SERVICE_MAX_ARGS-1 cause null
// terminator must fit
{
if (token[0] != '#' && (token[0] != '/' && token[1] != '/'))
args[argCount++] = token;
token = strtok(NULL, " "); // reminder for me: NULL is required to continue
// tokenizing the same string
}
args[argCount] = NULL; // null terminator
// execute
execvp(args[0], args);
fprintf(stderr, "[service] Failed to execute!!!\n"); // in case execvp returns
// (unreachable if executes)
}
// wait for the forked daemon to finish
int pidStatus;
waitpid(servicePid, &pidStatus, 0);
// check if process exited successfully
if (WIFEXITED(pidStatus) != 1)
{
fprintf(stderr, "[service] Process exited abnormally!!!\n");
}
// small 10 ms delay
usleep(10000);
}
// close file
fclose(serviceFile);
}