Skip to content

Commit b38d470

Browse files
author
zhengshuxin
committed
acl_master can manage the services which were not written by acl service template.
1 parent 5d3a333 commit b38d470

File tree

12 files changed

+532
-261
lines changed

12 files changed

+532
-261
lines changed

app/master/daemon/manage/action/service_stat.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ bool service_stat::stat_one(const char* path, serv_info_t& info)
1212
if (serv == NULL) {
1313
info.status = 404;
1414
info.conf = path;
15+
logger("service not found, path=%s", path);
1516
return false;
1617
}
1718

@@ -33,12 +34,15 @@ bool service_stat::stat_one(const char* path, serv_info_t& info)
3334
info.check_net = serv->check_net ? true : false;
3435
info.check_limits = serv->check_limits ? true : false;
3536

36-
if (serv->owner && *serv->owner)
37+
if (serv->owner && *serv->owner) {
3738
info.owner = serv->owner;
38-
if (serv->notify_addr && *serv->notify_addr)
39+
}
40+
if (serv->notify_addr && *serv->notify_addr) {
3941
info.notify_addr = serv->notify_addr;
40-
if (serv->notify_recipients && *serv->notify_recipients)
42+
}
43+
if (serv->notify_recipients && *serv->notify_recipients) {
4144
info.notify_recipients = serv->notify_recipients;
45+
}
4246

4347
info.version = serv->version;
4448

@@ -85,8 +89,9 @@ bool service_stat::handle(const stat_req_t& req, stat_res_t& res)
8589
cit = req.data.begin(); cit != req.data.end(); ++cit) {
8690

8791
serv_info_t info;
88-
if (stat_one((*cit).path.c_str(), info))
92+
if (stat_one((*cit).path.c_str(), info)) {
8993
n++;
94+
}
9095
res.data.push_back(info);
9196
}
9297

app/master/daemon/master/master.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ typedef struct ACL_MASTER_SERV {
9595
/*
9696
* Service types.
9797
*/
98-
#define ACL_MASTER_SERV_TYPE_NULL 0 /* invalid type */
98+
#define ACL_MASTER_SERV_TYPE_NONE 0 /* none type */
9999
#define ACL_MASTER_SERV_TYPE_UNIX 1 /* AF_UNIX domain socket */
100100
#define ACL_MASTER_SERV_TYPE_INET 2 /* AF_INET domain socket */
101101
#define ACL_MASTER_SERV_TYPE_FIFO 3 /* fifo (named pipe) */

app/master/daemon/master/master_api.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ static int check_command(ACL_MASTER_SERV *entry, const char *ext)
3737
char *path;
3838

3939
/* if ext name not NULL, then add it as the extern name of command */
40-
if (ext && *ext)
40+
if (ext && *ext) {
4141
path = acl_concatenate(entry->path, ext, NULL);
42-
else
42+
} else {
4343
path = acl_concatenate(entry->path, entry->cmdext, NULL);
44+
}
4445

4546
if (access(path, F_OK) != 0) {
4647
acl_msg_error("%s(%d), %s: command %s can't be executed, %s",
@@ -92,10 +93,12 @@ ACL_MASTER_SERV *acl_master_start(const char *path, int *nchilden,
9293
return NULL;
9394
}
9495

95-
if (nchilden)
96+
if (nchilden) {
9697
*nchilden = entry->prefork_proc;
97-
if (nsignaled)
98+
}
99+
if (nsignaled) {
98100
*nsignaled = entry->prefork_proc;
101+
}
99102

100103
(void) setup_callback(__FUNCTION__, entry, callback, ctx);
101104
entry->next = acl_var_master_head;
@@ -109,9 +112,10 @@ ACL_MASTER_SERV *acl_master_restart(const char *path, int *nchilden,
109112
{
110113
ACL_MASTER_SERV *serv = acl_master_lookup(path);
111114

112-
if (serv == NULL)
115+
if (serv == NULL) {
113116
return acl_master_start(path, nchilden, nsignaled,
114117
callback, ctx, ext);
118+
}
115119

116120
ACL_MASTER_SERV *entry = acl_master_ent_load(path);
117121
if (entry == NULL) {
@@ -132,24 +136,18 @@ ACL_MASTER_SERV *acl_master_restart(const char *path, int *nchilden,
132136

133137
/* kill processes of service according the master_service name in configure */
134138

135-
int acl_master_kill(const char *path)
139+
static int master_kill(const char *path, ACL_MASTER_SERV *serv)
136140
{
137-
ACL_MASTER_SERV *serv = acl_master_lookup(path);
138141
ACL_MASTER_SERV *iter, **servp;
139142

140-
if (serv == NULL) {
141-
acl_msg_error("%s(%d), %s: no service, path %s",
142-
__FILE__, __LINE__, __FUNCTION__, path);
143-
return -1;
144-
}
145-
146143
for (servp = &acl_var_master_head; (iter = *servp) != 0;) {
147144
if (SAME(iter->conf, path)) {
148145
*servp = iter->next;
149146
acl_master_service_kill(iter);
150147
return 0;
151-
} else
148+
} else {
152149
servp = &iter->next;
150+
}
153151
}
154152

155153
acl_msg_warn("%s(%d), %s: not found service - %s %d, path %s",
@@ -158,6 +156,18 @@ int acl_master_kill(const char *path)
158156
return -1;
159157
}
160158

159+
int acl_master_kill(const char *path)
160+
{
161+
ACL_MASTER_SERV *serv = acl_master_lookup(path);
162+
163+
if (serv == NULL) {
164+
acl_msg_error("%s(%d), %s: no service, path %s",
165+
__FILE__, __LINE__, __FUNCTION__, path);
166+
return -1;
167+
}
168+
return master_kill(path, serv);
169+
}
170+
161171
/* stop one service according the master_service name in configure */
162172

163173
int acl_master_stop(const char *path)
@@ -171,15 +181,20 @@ int acl_master_stop(const char *path)
171181
return -1;
172182
}
173183

184+
if (serv->type == ACL_MASTER_SERV_TYPE_NONE) {
185+
return master_kill(path, serv);
186+
}
187+
174188
for (servp = &acl_var_master_head; (iter = *servp) != 0;) {
175189
if (SAME(iter->conf, path)) {
176190
*servp = iter->next;
177191
// this service object will be freed after all
178192
// children of which exited.
179193
acl_master_service_stop(iter);
180194
return 0;
181-
} else
195+
} else {
182196
servp = &iter->next;
197+
}
183198
}
184199

185200
acl_msg_warn("%s(%d), %s: not found service - %s %d, path %s",
@@ -199,8 +214,9 @@ int acl_master_reload(const char *path, int *nchilden, int *nsignaled,
199214
return -1;
200215
}
201216

202-
if (nchilden)
217+
if (nchilden) {
203218
*nchilden = (int) acl_ring_size(&serv->children);
219+
}
204220

205221
(void) setup_callback(__FUNCTION__, serv, callback, ctx);
206222

app/master/daemon/master/master_avail.cpp

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ static int master_prefork(ACL_MASTER_SERV *serv)
1818
for (n = 0; n < serv->prefork_proc; n++)
1919
acl_master_spawn(serv);
2020

21-
if (acl_msg_verbose)
21+
if (acl_msg_verbose) {
2222
acl_msg_info("%s: service %s prefork %d processes ok",
2323
myname, serv->name, n);
24+
}
2425
return n;
2526
}
2627

@@ -29,15 +30,18 @@ static int master_prefork(ACL_MASTER_SERV *serv)
2930
int n = serv->max_proc - serv->total_proc;
3031

3132
/* xxx: sanity check */
32-
if (n > 0 && n < nproc)
33+
if (n > 0 && n < nproc) {
3334
nproc = n;
35+
}
3436

35-
for (n = 0 ; n < nproc; n++)
37+
for (n = 0 ; n < nproc; n++) {
3638
acl_master_spawn(serv);
39+
}
3740

38-
if (acl_msg_verbose)
41+
if (acl_msg_verbose) {
3942
acl_msg_info("%s: service %s prefork %d processes ok",
4043
myname, serv->name, n);
44+
}
4145
return nproc;
4246
}
4347

@@ -52,17 +56,18 @@ static void master_avail_event(int type, ACL_EVENT *event,
5256
ACL_MASTER_SERV *serv = (ACL_MASTER_SERV *) context;
5357
int n;
5458

55-
if (type == 0) /* XXX Can this happen? */
59+
if (type == 0) { /* XXX Can this happen? */
5660
acl_msg_panic("master_avail_event: null event");
57-
else if (ACL_MASTER_THROTTLED(serv)) { /* XXX interface botch */
61+
} else if (ACL_MASTER_THROTTLED(serv)) { /* XXX interface botch */
5862
for (n = 0; n < serv->listen_fd_count; n++) {
5963
acl_event_disable_readwrite(event,
6064
serv->listen_streams[n]);
6165
}
62-
} else if (serv->prefork_proc <= 0 || serv->max_proc <= 0)
66+
} else if (serv->prefork_proc <= 0 || serv->max_proc <= 0) {
6367
acl_master_spawn(serv);
64-
else if (master_prefork(serv) == 0 && serv->total_proc < serv->max_proc)
68+
} else if (master_prefork(serv) == 0 && serv->total_proc < serv->max_proc) {
6569
acl_master_spawn(serv);
70+
}
6671
}
6772

6873
/* acl_master_avail_listen - make sure that someone monitors the listen socket */
@@ -77,9 +82,10 @@ void acl_master_avail_listen(ACL_MASTER_SERV *serv)
7782
* this under the restriction that we have sufficient resources
7883
* to service a connection request.
7984
*/
80-
if (acl_msg_verbose)
85+
if (acl_msg_verbose) {
8186
acl_msg_info("%s: avail %d total %d max %d", myname,
8287
serv->avail_proc, serv->total_proc, serv->max_proc);
88+
}
8389

8490
/* when service is throttled or stopped, don't fork or listen again */
8591

@@ -92,32 +98,37 @@ void acl_master_avail_listen(ACL_MASTER_SERV *serv)
9298
} else if (ACL_MASTER_KILLED(serv)) {
9399
acl_msg_warn("service %s been delayed for been killed",
94100
serv->conf);
95-
} else
101+
} else {
96102
acl_master_avail_listen_force(serv);
103+
}
97104
}
98105

99106
void acl_master_avail_listen_force(ACL_MASTER_SERV *serv)
100107
{
101108
const char *myname = "acl_master_avail_listen_force";
102109
int i;
103110

104-
/* prefork services */
105-
if (serv->prefork_proc > 0 && master_prefork(serv) > 0)
111+
/* Prefork services */
112+
if (serv->prefork_proc > 0 && master_prefork(serv) > 0) {
106113
return;
114+
}
107115

108116
if ((serv->flags & ACL_MASTER_FLAG_RELOADING) == 0) {
109-
/* check if there're idle proc */
110-
if (serv->avail_proc > 0)
117+
/* Check if there're idle proc */
118+
if (serv->avail_proc > 0) {
111119
return;
120+
}
112121

113122
/* at last, check the proc limit */
114-
if (!ACL_MASTER_LIMIT_OK(serv->max_proc, serv->total_proc))
123+
if (!ACL_MASTER_LIMIT_OK(serv->max_proc, serv->total_proc)) {
115124
return;
125+
}
116126
}
117127

118-
if (acl_msg_verbose)
128+
if (acl_msg_verbose) {
119129
acl_msg_info("%s(%d), %s: enable events %s",
120130
__FILE__, __LINE__, myname, serv->name);
131+
}
121132

122133
for (i = 0; i < serv->listen_fd_count; i++) {
123134
acl_event_enable_read(acl_var_master_global_event,
@@ -132,10 +143,14 @@ void acl_master_avail_cleanup(ACL_MASTER_SERV *serv)
132143
{
133144
int n;
134145

146+
if (serv->type == ACL_MASTER_SERV_TYPE_NONE) {
147+
return;
148+
}
149+
135150
for (n = 0; n < serv->listen_fd_count; n++) {
136151
/* XXX must be last */
137152
acl_event_disable_readwrite(acl_var_master_global_event,
138-
serv->listen_streams[n]);
153+
serv->listen_streams[n]);
139154
}
140155
}
141156

@@ -151,19 +166,21 @@ void acl_master_avail_more(ACL_MASTER_SERV *serv, ACL_MASTER_PROC *proc)
151166
* requests, so we can stop monitoring the service's listen socket.
152167
* The child will do it for us.
153168
*/
154-
if (acl_msg_verbose)
169+
if (acl_msg_verbose) {
155170
acl_msg_info("%s: pid %d (%s)", myname, proc->pid,
156-
proc->serv->name);
157-
if (proc->avail == ACL_MASTER_STAT_AVAIL)
171+
proc->serv->name);
172+
}
173+
if (proc->avail == ACL_MASTER_STAT_AVAIL) {
158174
acl_msg_panic("%s(%d), %s: process already available",
159-
__FILE__, __LINE__, myname);
175+
__FILE__, __LINE__, myname);
176+
}
160177
serv->avail_proc++;
161178
proc->avail = ACL_MASTER_STAT_AVAIL;
162179
if (acl_msg_verbose)
163180
acl_msg_info("%s: disable events %s", myname, serv->name);
164181
for (n = 0; n < serv->listen_fd_count; n++) {
165182
acl_event_disable_readwrite(acl_var_master_global_event,
166-
serv->listen_streams[n]);
183+
serv->listen_streams[n]);
167184
}
168185
}
169186

@@ -178,12 +195,14 @@ void acl_master_avail_less(ACL_MASTER_SERV *serv, ACL_MASTER_PROC *proc)
178195
* requests. When no child processes are available, start monitoring
179196
* the service's listen socket for new connection requests.
180197
*/
181-
if (acl_msg_verbose)
198+
if (acl_msg_verbose) {
182199
acl_msg_info("%s: pid %d (%s)", myname,
183-
proc->pid, proc->serv->name);
184-
if (proc->avail != ACL_MASTER_STAT_AVAIL)
200+
proc->pid, proc->serv->name);
201+
}
202+
if (proc->avail != ACL_MASTER_STAT_AVAIL) {
185203
acl_msg_panic("%s(%d), %s: process not available",
186-
__FILE__, __LINE__, myname);
204+
__FILE__, __LINE__, myname);
205+
}
187206
serv->avail_proc--;
188207
proc->avail = ACL_MASTER_STAT_TAKEN;
189208
acl_master_avail_listen(serv);

0 commit comments

Comments
 (0)