-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstartup-scripts.c
More file actions
165 lines (132 loc) · 4.2 KB
/
startup-scripts.c
File metadata and controls
165 lines (132 loc) · 4.2 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
/*
startup-scripts.c:
parse and execute startup commands
Shitty runCommands() function is garbage and definitely needs a rewrite.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include "config.h" /* imports: rcFile,
SERVICE_MAX_ARGS,
SERVICE_MAX_BUFFER,
MAX_SERVICES
*/
#include "startup-scripts.h"
struct services_struct services[MAX_SERVICES];
int service_count = 0;
void runCommands()
{
FILE *commandsFile;
char line_buffer[SERVICE_MAX_BUFFER];
commandsFile = fopen(rcFile, "r");
if (commandsFile == NULL)
{
fprintf(stderr, "[service] Unable to read rc file %s. Does it exist?\n", rcFile);
perror("fopen");
return;
}
/* Parse the file */
while (fgets(line_buffer, sizeof(line_buffer), commandsFile) != NULL)
{
/* skip line if it begins with # or // */
if (line_buffer[0] == '\n' || line_buffer[0] == '#' || (line_buffer[0] == '/' && line_buffer[1] == '/'))
continue;
// remove newline character at the end of the string
line_buffer[strcspn(line_buffer, "\n")] = 0;
// switching to pointer to lineBuffer for efficiency
char *command = line_buffer;
// skip spaces and tabs at line beginning
while (*command == ' ' || *command == '\t')
command++;
// skip line if it's empty after this stage
if (*command == '\0')
continue;
printf("[service] Executing %s\n", command);
/* tokenize and execute command */
pid_t service_pid = fork();
if (service_pid == -1)
perror("[service] Fork failed");
else if (service_pid == 0) /* in child */
/* final parsing & execution */
{
/* redirect stdout to not clobber TTY with useless stuff */
/* stderr will still spit errors which is at least useful for debugging something */
freopen("/dev/null", "w", stdout);
char *args[SERVICE_MAX_ARGS];
int arg_count = 0;
// final tokenizer, parse the string into arguments
// TODO: reinvent this shit and pull at least some of this code out of the child
char *token = strtok(command, " ");
while (token != NULL && arg_count < SERVICE_MAX_ARGS-1) // null terminator must fit
{
/* skip line if an argument if starts with comment */
if (token[0] == '#' || token[0] == '\n' || (token[0] == '/' && token[1] == '/'))
break;
args[arg_count++] = token;
token = strtok(NULL, " ");
}
args[arg_count] = NULL; // null terminator
/* execute final command */
execvp(args[0], args);
fprintf(stderr, "[service] Failed to execute\n"); // in case execvp returns (unreachable if executes)
_exit(1);
}
else /* In parent when child forks,
store the fork's:
* PID
* command (including the comment, i'll fix this)
* status (active: 1 or dead: 0)
for monitoring (see monitorServices(), which is triggered by killZombies).
So-called NUTS service supervising, lol.
*/
{
services[service_count].pid = service_pid;
strncpy(services[service_count].command, command, SERVICE_MAX_BUFFER-1);
services[service_count].command[SERVICE_MAX_BUFFER-1] = '\0'; /* to prevent error if a 255 string gets copied */
/* and there isn't room for terminator */
services[service_count].is_active = 1;
service_count++;
}
// small 10 ms delay between service startups (because why not? this init isn't about being the fastest)
usleep(10000);
}
// close file
fclose(commandsFile);
return;
}
/*
void monitorServices()
{
int status;
pid_t result;
for (int i = 0; i < service_count; i++)
{
if (!services[i].is_active)
continue;
result = waitpid(services[i].pid, &status, WNOHANG);
if (result == -1)
{
fprintf(stderr, "[monitor] Something went wrong with service '%s' (PID %d)\n",
services[i].command, services[i].pid);
services[i].is_active = 0;
}
else if (result > 0)
{
if (WIFEXITED(status))
{
printf("[monitor] Service '%s' (PID %d) exited normally with status %d\n",
services[i].command, services[i].pid, WEXITSTATUS(status));
}
else if (WIFSIGNALED(status))
{
printf("[monitor] Service '%s' (PID %d) was killed by signal %d\n",
services[i].command, services[i].pid, WTERMSIG(status));
}
services[i].is_active = 0;
}
}
return;
}
*/