-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
84 lines (58 loc) · 1.47 KB
/
main.c
File metadata and controls
84 lines (58 loc) · 1.47 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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "main.h"
#include "parser.h"
void* service_thread(void *arg) {
service_t *svc = (service_t*)arg;
int status;
printf("Starting service: %s\n", svc->name);
pid_t pid = fork();
if (pid == 0) {
execl("/bin/sh", "sh", "-c", svc->exec, NULL);
perror("exec");
exit(1);
}
wait(&status);
printf("Service exited: %s with status: %d\n", svc->name, status);
free(svc);
return NULL;
}
int start_service(char *service) {
char path[512];
snprintf(path, sizeof(path), "%s/%s", SERVICE_DIR, service);
printf("SERVICE: %s\n", service);
service_t *svc = malloc(sizeof(service_t));
memset(svc, 0, sizeof(service_t));
strcpy(svc->name, service);
parse_service(path, svc);
if (strlen(svc->exec) == 0) {
free(svc);
return -1;
}
pthread_t thread;
pthread_create(&thread, NULL, service_thread, svc);
pthread_detach(thread);
}
int main() {
printf("LSID init starting...\n");
DIR *dir = opendir(SERVICE_DIR);
if (!dir) {
perror("opendir");
return 1;
}
struct dirent *entry;
while ((entry = readdir(dir))) {
if (!strstr(entry->d_name, ".service"))
continue;
start_service(entry->d_name);
}
closedir(dir);
while (1)
sleep(60);
return 0;
}