Skip to content

Commit 8e4b08d

Browse files
jfsmigfvennetier
authored andcommitted
gridinit: Remove nested funcs, add assertions
1 parent e4e1596 commit 8e4b08d

File tree

1 file changed

+128
-84
lines changed

1 file changed

+128
-84
lines changed

main/gridinit.c

Lines changed: 128 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -161,37 +161,37 @@ _str_set_array(gboolean concat, gchar ***dst, gchar *str)
161161
/* Process management helpers ---------------------------------------------- */
162162

163163
static void
164-
alert_proc_died(void *udata, struct child_info_s *ci)
164+
alert_proc_died(void *udata UNUSED, struct child_info_s *ci)
165165
{
166-
(void) udata;
167-
168166
if (ci->started)
169167
supervisor_children_set_user_flags(ci->key, USERFLAG_PROCESS_DIED);
170168
}
171169

172-
static void
173-
alert_send_deferred(void *udata, struct child_info_s *ci)
170+
static gboolean
171+
alert_send_deferred(void *udata UNUSED, struct child_info_s *ci)
174172
{
175-
(void) udata;
173+
gboolean rc = FALSE;
176174

177175
/* Handle the alerting of broken services */
178176
if ((ci->user_flags & USERFLAG_PROCESS_DIED) && ci->broken) {
179177
supervisor_children_del_user_flags(ci->key, USERFLAG_PROCESS_DIED);
180178
ERROR("Process broken [%s] %s", ci->key, ci->cmd);
179+
rc = TRUE;
181180
}
182181

183182
/* Handle the alerting of successfully restarted services */
184183
if (!(ci->user_flags & USERFLAG_PROCESS_DIED) && (ci->user_flags & USERFLAG_PROCESS_RESTARTED)) {
185184
supervisor_children_del_user_flags(ci->key, USERFLAG_PROCESS_RESTARTED);
186185
NOTICE("Process restarted [%s] %s", ci->key, ci->cmd);
186+
rc = TRUE;
187187
}
188+
189+
return rc;
188190
}
189191

190192
static void
191-
alert_proc_started(void *udata, struct child_info_s *ci)
193+
alert_proc_started(void *udata UNUSED, struct child_info_s *ci)
192194
{
193-
(void) udata;
194-
195195
/* Note service has restarted */
196196
if (ci->user_flags & USERFLAG_PROCESS_DIED) {
197197
supervisor_children_del_user_flags(ci->key, USERFLAG_PROCESS_DIED);
@@ -222,62 +222,80 @@ thread_ignore_signals(void)
222222

223223
/* COMMANDS management ----------------------------------------------------- */
224224

225+
typedef void (_on_proc_f) (GString *out, struct child_info_s *ci);
225226

226-
static void
227-
service_run_groupv(int nb_groups, char **groupv, GString *out, supervisor_cb_f cb)
227+
struct _run_ctx_s {
228+
const char *group;
229+
GString *out;
230+
_on_proc_f *cb;
231+
};
232+
233+
static gboolean
234+
_group_filter(gpointer u, struct child_info_s *ci)
228235
{
229-
guint count;
230-
231-
void group_filter(void *u1, struct child_info_s *ci) {
232-
const char *group = u1;
233-
if (group && !gridinit_group_in_set(group, ci->group)) {
234-
TRACE("start: Skipping [%s] with group [%s]", ci->key, ci->group);
235-
} else {
236-
TRACE("Calback on service [%s]", ci->key);
237-
cb(out, ci);
238-
++ count;
239-
}
236+
g_assert_nonnull(u);
237+
g_assert_nonnull(ci);
238+
239+
struct _run_ctx_s *ctx = u;
240+
if (ctx->group && !gridinit_group_in_set(ctx->group, ci->group)) {
241+
TRACE("start: Skipping [%s] with group [%s]", ci->key, ci->group);
242+
return FALSE;
243+
} else {
244+
TRACE("Calback on service [%s]", ci->key);
245+
ctx->cb(ctx->out, ci);
246+
return TRUE;
240247
}
248+
}
249+
250+
static void
251+
service_run_groupv(int nb_groups, char **groupv, GString *out, _on_proc_f cb)
252+
{
253+
g_assert_nonnull(out);
254+
g_assert_nonnull(cb);
255+
256+
struct _run_ctx_s ctx = {NULL, out, cb};
241257

242258
if (!nb_groups || !groupv) {
243-
supervisor_run_services(NULL, group_filter);
244-
} else {
245-
for (int i=0; i<nb_groups ;i++) {
246-
char *what = groupv[i];
247-
if (*what == '@') {
248-
TRACE("Callback on group [%s]", what);
249-
count = 0;
250-
supervisor_run_services(what+1, group_filter);
251-
if (!count && out) {
252-
/* notifies the client the group has not been found */
253-
g_string_append_printf(out, "%d %s\n", ENOENT, what);
254-
}
259+
(void) supervisor_run_services(&ctx, _group_filter);
260+
return;
261+
}
262+
for (int i=0; i<nb_groups ;i++) {
263+
char *what = groupv[i];
264+
if (*what == '@') {
265+
TRACE("Callback on group [%s]", what);
266+
ctx.group = what + 1;
267+
gint count = supervisor_run_services(&ctx, _group_filter);
268+
if (count > 0 && out) {
269+
/* notifies the client the group has not been found */
270+
g_string_append_printf(out, "%d %s\n", ENOENT, what);
255271
}
256-
else {
257-
struct child_info_s ci = {};
258-
if (0 == supervisor_children_get_info(what, &ci)) {
259-
TRACE("Calback on service [%s]", what);
260-
cb(out, &ci);
261-
} else {
262-
if (out)
263-
g_string_append_printf(out, "%d %s\n", errno, what);
264-
if (errno == ENOENT)
265-
TRACE("Service not found [%s]\n", what);
266-
else
267-
ERROR("Internal error [%s]: %s", what, strerror(errno));
268-
}
272+
}
273+
else {
274+
struct child_info_s ci = {};
275+
if (0 == supervisor_children_get_info(what, &ci)) {
276+
TRACE("Calback on service [%s]", what);
277+
cb(out, &ci);
278+
} else {
279+
if (out)
280+
g_string_append_printf(out, "%d %s\n", errno, what);
281+
if (errno == ENOENT)
282+
TRACE("Service not found [%s]\n", what);
283+
else
284+
ERROR("Internal error [%s]: %s", what, strerror(errno));
269285
}
270286
}
271287
}
272288
}
273289

274290
static void
275-
command_start(GString *out, int argc, char **argv)
291+
start_process(GString *out, struct child_info_s *ci)
276292
{
277-
void start_process(void *u UNUSED, struct child_info_s *ci) {
278-
supervisor_children_repair(ci->key);
293+
g_assert_nonnull(out);
294+
g_assert_nonnull(ci);
279295

280-
switch (supervisor_children_status(ci->key, TRUE)) {
296+
supervisor_children_repair(ci->key);
297+
298+
switch (supervisor_children_status(ci->key, TRUE)) {
281299
case 0:
282300
INFO("Already started [%s]", ci->key);
283301
g_string_append_printf(out, "%d %s\n", EALREADY, ci->key);
@@ -290,18 +308,23 @@ command_start(GString *out, int argc, char **argv)
290308
WARN("Cannot start [%s]: %s", ci->key, strerror(errno));
291309
g_string_append_printf(out, "%d %s\n", errno, ci->key);
292310
return;
293-
}
294311
}
312+
}
295313

314+
static void
315+
command_start(GString *out, int argc, char **argv)
316+
{
296317
g_assert_nonnull(out);
297318
return service_run_groupv(argc, argv, out, start_process);
298319
}
299320

300321
static void
301-
command_stop(GString *out, int argc, char **argv)
322+
stop_process(GString *out, struct child_info_s *ci)
302323
{
303-
void stop_process(void *u UNUSED, struct child_info_s *ci) {
304-
switch (supervisor_children_status(ci->key, FALSE)) {
324+
g_assert_nonnull(out);
325+
g_assert_nonnull(ci);
326+
327+
switch (supervisor_children_status(ci->key, FALSE)) {
305328
case 0:
306329
INFO("Already stopped [%s]", ci->key);
307330
g_string_append_printf(out, "%d %s\n", EALREADY, ci->key);
@@ -314,18 +337,23 @@ command_stop(GString *out, int argc, char **argv)
314337
WARN("Cannot stop [%s]: %s", ci->key, strerror(errno));
315338
g_string_append_printf(out, "%d %s\n", errno, ci->key);
316339
return;
317-
}
318340
}
341+
}
319342

343+
static void
344+
command_stop(GString *out, int argc, char **argv)
345+
{
320346
g_assert_nonnull(out);
321347
return service_run_groupv(argc, argv, out, stop_process);
322348
}
323349

324350
static void
325-
command_restart(GString *out, int argc, char **argv)
351+
restart_process(GString *out, struct child_info_s *ci)
326352
{
327-
void restart_process(void *u UNUSED, struct child_info_s *ci) {
328-
switch (supervisor_children_restart(ci->key)) {
353+
g_assert_nonnull(out);
354+
g_assert_nonnull(ci);
355+
356+
switch (supervisor_children_restart(ci->key)) {
329357
case 0:
330358
INFO("Already restarted [%s]", ci->key);
331359
g_string_append_printf(out, "%d %s\n", EALREADY, ci->key);
@@ -338,51 +366,64 @@ command_restart(GString *out, int argc, char **argv)
338366
WARN("Cannot restart [%s]: %s", ci->key, strerror(errno));
339367
g_string_append_printf(out, "%d %s\n", errno, ci->key);
340368
return;
341-
}
342369
}
370+
}
343371

372+
static void
373+
command_restart(GString *out, int argc, char **argv)
374+
{
344375
g_assert_nonnull(out);
345376
return service_run_groupv(argc, argv, out, restart_process);
346377
}
347378

348379
static void
349-
command_show(GString *out, int argc UNUSED, char **argv UNUSED)
380+
print_process(GString *out, struct child_info_s *ci)
350381
{
351-
void print_process(void *u UNUSED, struct child_info_s *ci) {
352-
g_string_append_printf(out,
353-
"%d "
354-
"%d %d %d "
355-
"%u %u "
356-
"%ld "
357-
"%ld %ld %ld "
358-
"%u %u "
359-
"%s %s %s\n",
382+
g_assert_nonnull(out);
383+
g_assert_nonnull(ci);
384+
385+
g_string_append_printf(out,
386+
"%d "
387+
"%d %d %d "
388+
"%u %u "
389+
"%ld "
390+
"%ld %ld %ld "
391+
"%u %u "
392+
"%s %s %s\n",
360393
ci->pid,
361394
BOOL(ci->enabled), BOOL(ci->broken), BOOL(ci->respawn),
362395
ci->counter_started, ci->counter_died,
363396
ci->last_start_attempt,
364397
ci->rlimits.core_size, ci->rlimits.stack_size, ci->rlimits.nb_files,
365398
ci->uid, ci->gid,
366399
ci->key, ci->group, ci->cmd);
367-
}
400+
}
368401

402+
static void
403+
command_show(GString *out, int argc UNUSED, char **argv UNUSED)
404+
{
369405
g_assert_nonnull(out);
370406
return service_run_groupv(0, NULL, out, print_process);
371407
}
372408

373409
static void
374-
command_repair(GString *out, int argc, char **argv)
410+
repair_process(GString *out, struct child_info_s *ci)
375411
{
376-
void repair_process(void *u UNUSED, struct child_info_s *ci) {
377-
if (0 == supervisor_children_repair(ci->key)) {
378-
INFO("Repaired [%s]", ci->key);
379-
g_string_append_printf(out, "%d %s\n", 0, ci->key);
380-
} else {
381-
WARN("Failed to repair [%s]: %s", ci->key, strerror(errno));
382-
g_string_append_printf(out, "%d %s\n", errno, ci->key);
383-
}
412+
g_assert_nonnull(out);
413+
g_assert_nonnull(ci);
414+
415+
if (0 == supervisor_children_repair(ci->key)) {
416+
INFO("Repaired [%s]", ci->key);
417+
g_string_append_printf(out, "%d %s\n", 0, ci->key);
418+
} else {
419+
WARN("Failed to repair [%s]: %s", ci->key, strerror(errno));
420+
g_string_append_printf(out, "%d %s\n", errno, ci->key);
384421
}
422+
}
385423

424+
static void
425+
command_repair(GString *out, int argc, char **argv)
426+
{
386427
g_assert_nonnull(out);
387428
return service_run_groupv(argc, argv, out, repair_process);
388429
}
@@ -1112,6 +1153,13 @@ _cfg_reload_file(GKeyFile *kf, gboolean services_only, GError **err)
11121153
return rc;
11131154
}
11141155

1156+
static int
1157+
notify_error(const char *path, int en)
1158+
{
1159+
NOTICE("errno=%d %s : %s", en, path, strerror(en));
1160+
return 0;
1161+
}
1162+
11151163
#define SETERRNO(ERR) do { if ((ERR) && *(ERR) && !(*(ERR))->code) (*(ERR))->code = errno; } while (0)
11161164
static gboolean
11171165
_cfg_reload(gboolean services_only, GError **err)
@@ -1134,10 +1182,6 @@ _cfg_reload(gboolean services_only, GError **err)
11341182

11351183
/* Then load "globbed" sub files, but only services */
11361184
if (config_subdir) {
1137-
int notify_error(const char *path, int en) {
1138-
NOTICE("errno=%d %s : %s", en, path, strerror(en));
1139-
return 0;
1140-
}
11411185
glob_t subfiles_glob = {};
11421186

11431187
DEBUG("Loading services files matching [%s]", config_subdir);

0 commit comments

Comments
 (0)