-
Notifications
You must be signed in to change notification settings - Fork 0
/
zookld-exstack.c
225 lines (195 loc) · 5.57 KB
/
zookld-exstack.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <grp.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include "http.h"
#define DBG_ON 1
#define LOG(...) log_msg(TIME_NOW(),__FILE__, __func__, __LINE__,BLUE,DBG_ON,MSG,__VA_ARGS__)
#define SVC_CNT 2
#define SHARED_GID 1000
#define JAIL_ROOT "/var/okws/run"
enum svc_indexes {ZOOKD,HTTP_SVC,AUTH_SVC};
SVCS svcs[SVC_CNT] = {
{"zookd-exstack", "zookd-exstack", -1, -1, -1,500,500,0,{555}},
{"zookhttp-exstack","zookhttp-exstack",-1, -1, -1,600,600,0,{555}},
};
//gid_t group_list[1] = {SHARED_GID};
static pid_t launch_svc(int);
void handle_child_process();
void create_socket_pair();
void register_signal_handler();
void launch_child_processes();
int set_args(char *[], int );
void set_groups(int);
void list_files(char *dir_name);
struct sigaction sa;
char *PORT = "8080";
void print_cwd();
int main(int argc, char **argv) {
system("clear");
print_cwd();
if (argc == 2)
PORT = argv[1];
create_socket_pair();
register_signal_handler();
launch_child_processes();
pid_t pid;
while ((pid = wait(NULL)) > 0) {
for (int i = 0; i < SVC_CNT; i++) {
if (svcs[i].pid == pid) {
LOG("Process %s died, restarting", svcs[i].name);
launch_svc(i);
break;
}
}
}
}
/**
* Create a socket pairs and store the file descriptors in the config array
*/
void create_socket_pair() {
int socketPair[2];
for (int i = 1; i < SVC_CNT; i++) {
if (socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair))
LOG_ERROR("socketpair");
svcs[i].sockfd1 = socketPair[0];
svcs[i].sockfd2 = socketPair[1];
LOG("Created socket pair for %s , sockfd1 =%d, sockfd2 =%d,", svcs[i].name, svcs[i].sockfd1, svcs[i].sockfd2);
}
}
/**
* @brief register_signal_handler
* register the signal handler for child process (keep alive)
*/
void register_signal_handler() {
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handle_child_process;
sigaction(SIGCHLD, &sa, NULL);
}
/**
* @brief launch_child_processes
* launch all the child processes
*/
void launch_child_processes() {
for (int i = 0; i < SVC_CNT; i++)
launch_svc(i);
}
/**
* @brief handle_child_process
* keep alive handler for child processes
*/
void handle_child_process() {
pid_t pid;
int status;
while ((pid = waitpid(-1, &status, WNOHANG)) != -1) {
for (int i = 0; i < SVC_CNT; i++) {
if (svcs[i].pid == pid) {
LOG("Process %s died, restarting", svcs[i].name);
launch_svc(i);
return;
}
}
}
}
/**
* Launch a service
* @param service index of the service in the config array
* @return
*/
pid_t launch_svc(int svc_index) {
pid_t pid;
char *argv[32] = {0};
int args_len = 0;
switch (pid = fork()) {
case 0:
break;
case -1:
LOG_ERROR("fork %s", svcs[svc_index].path);
default:
svcs[svc_index].pid = pid;
LOG("%s:pid %d", svcs[svc_index].path, pid);
return pid;
}
chdir(JAIL_ROOT); // change the working directory to the jail root
LOG("CWD: %s", getcwd(NULL, 0));
// chroot("."); // chroot to the jail root
list_files(".");
uid_t uid = svcs[svc_index].uid;
gid_t gid = svcs[svc_index].gid;
printf("\n0)\tuid: %d, gid: %d\n", getuid(), getgid());
setresgid(gid, gid, gid);
set_groups(svc_index);
setresuid(uid, uid, uid);
args_len = set_args(argv, svc_index);
print_cwd();
list_files(".");
printf("\n0)\tuid: %d, gid: %d\n", getuid(), getgid());
LOG("execv path:%s args:%s",svcs[svc_index].path, TO_STR(argv, args_len));
signal(SIGCHLD, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
// char *aaa[2];
// aaa[0] = "/bin/ls";
// aaa[1] = NULL;
// execv( aaa[0], aaa );
execv(svcs[svc_index].path, argv);
LOG_ERROR("execv %s", svcs[svc_index].path);
return 0;
}
/**
* set the arguments for the child process
* args for zookd: [exec, port, fdCount, ....[fd,fdName]...]
* args for httpsvc: [exec, fd,fdName]
* @param argv string array to store the arguments
* @param service index of the service in the config array
*/
int set_args(char *argv[], int svc_index) {
int args_len = -1;
int fd_count = SVC_CNT - 1;
argv[++args_len] = svcs[svc_index].path;
if (svc_index == ZOOKD) {
argv[++args_len] = PORT;
argv[++args_len] = intToStr(fd_count);
for (int i = 1; i < SVC_CNT; i++) {
argv[++args_len] = intToStr(svcs[i].sockfd2);
argv[++args_len] = svcs[i].name;
}
} else {
argv[++args_len] = intToStr(svcs[svc_index].sockfd1);
argv[++args_len] = svcs[svc_index].name;
}
argv[++args_len] = NULL;
return args_len;
}
void set_groups(int svc_index){
int group_count = svcs[svc_index].ngrps;
if(group_count == 0)
return;
gid_t group_list[group_count];
for (int i = 0; i < group_count; i++) {
group_list[i] = svcs[svc_index].grps[i];
}
setgroups(group_count,group_list);
}
void print_cwd(){
char cwd[1024];
getcwd(cwd, sizeof(cwd));
printf("Current working dir: %s\n", cwd);
}
void list_files(char *dir_name) {
DIR *d;
struct dirent *dir;
d = opendir(dir_name);
printf("Files in %s:\n", dir_name);
if (d) {
while ((dir = readdir(d)) != NULL) {
printf("%s\n", dir->d_name);
}
closedir(d);
}
}