Skip to content

Commit 4592c21

Browse files
jfsmigfvennetier
authored andcommitted
gridinit/children: Less nested funcs, More assertions
1 parent 8e4b08d commit 4592c21

File tree

2 files changed

+51
-73
lines changed

2 files changed

+51
-73
lines changed

main/children.c

Lines changed: 48 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,21 @@ supervisor_children_set_flag(const char *key, guint32 mask, gboolean enabled)
204204
return _child_set_flag(sd, mask, enabled);
205205
}
206206

207+
static long
208+
i64tolong(gint64 i64)
209+
{
210+
long l;
211+
if (i64 >= G_MAXLONG)
212+
return G_MAXLONG;
213+
if (i64 < 0)
214+
return -1L;
215+
l = i64;
216+
return l;
217+
}
218+
207219
static void
208220
_child_get_info(struct child_s *c, struct child_info_s *ci)
209221
{
210-
long i64tolong(gint64 i64) {
211-
long l;
212-
if (i64 >= G_MAXLONG)
213-
return G_MAXLONG;
214-
if (i64 < 0)
215-
return -1L;
216-
l = i64;
217-
return l;
218-
}
219-
220222
memset(ci, 0x00, sizeof(*ci));
221223
ci->key = c->key;
222224
ci->cmd = c->command;
@@ -811,34 +813,32 @@ supervisor_children_register(const gchar *key, const gchar *cmd)
811813
return TRUE;
812814
}
813815

814-
gboolean
815-
supervisor_run_services(void *udata, supervisor_cb_f callback)
816+
gint
817+
supervisor_run_services(void *udata, supervisor_run_cb_f callback)
816818
{
817-
struct child_info_s ci;
818819
struct child_s *sd;
820+
gint count = 0U;
819821

820-
if (!callback) {
821-
errno = EINVAL;
822-
return FALSE;
823-
}
822+
g_assert_nonnull(callback);
824823

825824
FOREACH_CHILD(sd) {
825+
struct child_info_s ci = {};
826826
if (FLAG_HAS(sd, MASK_OBSOLETE))
827827
continue;
828828
_child_get_info(sd, &ci);
829-
callback(udata, &ci);
829+
if (callback(udata, &ci)) {
830+
count++;
831+
}
830832
}
831833

832-
return TRUE;
834+
return count;
833835
}
834836

835837
guint
836838
supervisor_children_kill_disabled(void)
837839
{
838-
guint count;
839840
struct child_s *sd;
840-
841-
count = 0U;
841+
guint count = 0U;
842842

843843
FOREACH_CHILD(sd) {
844844
/* Stop child that needs to be restarted */
@@ -860,12 +860,9 @@ supervisor_children_kill_disabled(void)
860860
int
861861
supervisor_children_enable(const char *key, gboolean enable)
862862
{
863-
struct child_s *sd;
863+
g_assert_nonnull(key);
864864

865-
if (!key) {
866-
errno = EINVAL;
867-
return -1;
868-
}
865+
struct child_s *sd;
869866
if (!(sd = supervisor_get_child(key))) {
870867
errno = ENOENT;
871868
return -1;
@@ -948,10 +945,8 @@ supervisor_children_set_limit(const gchar *key, enum supervisor_limit_e what, gi
948945
{
949946
struct child_s *sd;
950947

951-
if (!key) {
952-
errno = EINVAL;
953-
return -1;
954-
}
948+
g_assert_nonnull(key);
949+
955950
if (what < SUPERV_LIMIT_THREAD_STACK || what > SUPERV_LIMIT_CORE_SIZE) {
956951
errno = EINVAL;
957952
return -1;
@@ -982,12 +977,10 @@ supervisor_children_set_limit(const gchar *key, enum supervisor_limit_e what, gi
982977
int
983978
supervisor_children_set_working_directory(const gchar *key, const gchar *dir)
984979
{
985-
struct child_s *sd;
980+
g_assert_nonnull(key);
981+
g_assert_nonnull(dir);
986982

987-
if (!key || !dir) {
988-
errno = EINVAL;
989-
return -1;
990-
}
983+
struct child_s *sd;
991984
if (!(sd = supervisor_get_child(key))) {
992985
errno = ENOENT;
993986
return -1;
@@ -1005,12 +998,11 @@ int
1005998
supervisor_children_setenv(const gchar *key, const gchar *envkey,
1006999
const gchar *envval, gchar separator)
10071000
{
1008-
struct child_s *sd;
1001+
g_assert_nonnull(key);
1002+
g_assert_nonnull(envkey);
1003+
g_assert_nonnull(envval);
10091004

1010-
if (!key || !envkey ||!envval) {
1011-
errno = EINVAL;
1012-
return -1;
1013-
}
1005+
struct child_s *sd;
10141006
if (!(sd = supervisor_get_child(key))) {
10151007
errno = ENOENT;
10161008
return -1;
@@ -1056,23 +1048,21 @@ supervisor_children_inherit_env(const gchar *key)
10561048
}
10571049
}
10581050

1051+
static void _free(gpointer p1, gpointer p2) { (void) p2; if (p1) g_free(p1); }
1052+
10591053
int
10601054
supervisor_children_clearenv(const gchar *key)
10611055
{
1062-
void __free(gpointer p1, gpointer p2) { (void) p2; if (p1) g_free(p1); }
1063-
struct child_s *sd;
1056+
g_assert_nonnull(key);
10641057

1065-
if (!key) {
1066-
errno = EINVAL;
1067-
return -1;
1068-
}
1058+
struct child_s *sd;
10691059
if (!(sd = supervisor_get_child(key))) {
10701060
errno = ENOENT;
10711061
return -1;
10721062
}
10731063

10741064
if (sd->env) {
1075-
g_slist_foreach(sd->env, __free, NULL);
1065+
g_slist_foreach(sd->env, _free, NULL);
10761066
g_slist_free(sd->env);
10771067
}
10781068
sd->env = NULL;
@@ -1083,12 +1073,9 @@ supervisor_children_clearenv(const gchar *key)
10831073
int
10841074
supervisor_children_set_user_flags(const gchar *key, guint32 flags)
10851075
{
1086-
struct child_s *sd;
1076+
g_assert_nonnull(key);
10871077

1088-
if (!key) {
1089-
errno = EINVAL;
1090-
return -1;
1091-
}
1078+
struct child_s *sd;
10921079
if (!(sd = supervisor_get_child(key))) {
10931080
errno = ENOENT;
10941081
return -1;
@@ -1102,12 +1089,9 @@ supervisor_children_set_user_flags(const gchar *key, guint32 flags)
11021089
int
11031090
supervisor_children_del_user_flags(const gchar *key, guint32 flags)
11041091
{
1105-
struct child_s *sd;
1092+
g_assert_nonnull(key);
11061093

1107-
if (!key) {
1108-
errno = EINVAL;
1109-
return -1;
1110-
}
1094+
struct child_s *sd;
11111095
if (!(sd = supervisor_get_child(key))) {
11121096
errno = ENOENT;
11131097
return -1;
@@ -1121,12 +1105,9 @@ supervisor_children_del_user_flags(const gchar *key, guint32 flags)
11211105
int
11221106
supervisor_children_set_group(const gchar *key, const gchar *group)
11231107
{
1124-
struct child_s *sd;
1108+
g_assert_nonnull(key);
11251109

1126-
if (!key) {
1127-
errno = EINVAL;
1128-
return -1;
1129-
}
1110+
struct child_s *sd;
11301111
if (!(sd = supervisor_get_child(key))) {
11311112
errno = ENOENT;
11321113
return -1;
@@ -1144,12 +1125,9 @@ supervisor_children_set_group(const gchar *key, const gchar *group)
11441125
int
11451126
supervisor_children_set_ids(const gchar *key, gint32 uid, gint32 gid)
11461127
{
1147-
struct child_s *sd;
1128+
g_assert_nonnull(key);
11481129

1149-
if (!key) {
1150-
errno = EINVAL;
1151-
return -1;
1152-
}
1130+
struct child_s *sd;
11531131
if (!(sd = supervisor_get_child(key))) {
11541132
errno = ENOENT;
11551133
return -1;
@@ -1164,10 +1142,8 @@ supervisor_children_set_ids(const gchar *key, gint32 uid, gint32 gid)
11641142
int
11651143
supervisor_children_set_delay_sigkill(const char *key, time_t delay)
11661144
{
1167-
if (!key) {
1168-
errno = EINVAL;
1169-
return -1;
1170-
}
1145+
g_assert_nonnull(key);
1146+
11711147
struct child_s *sd;
11721148
if (!(sd = supervisor_get_child(key))) {
11731149
errno = ENOENT;

main/gridinit-utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ struct child_info_s {
8888

8989
typedef void (supervisor_cb_f) (void *udata, struct child_info_s *ci);
9090

91+
typedef gboolean (supervisor_run_cb_f) (void *udata, struct child_info_s *ci);
92+
9193

9294
void supervisor_children_init(void);
9395

@@ -143,7 +145,7 @@ int supervisor_children_set_limit(const gchar *key,
143145
enum supervisor_limit_e what, gint64 value);
144146

145147
/* Runs the children list and call the callback fnction on each element */
146-
gboolean supervisor_run_services(void *ptr, supervisor_cb_f callback);
148+
gint supervisor_run_services(void *ptr, supervisor_run_cb_f callback);
147149

148150
int supervisor_children_set_working_directory(const gchar *key,
149151
const gchar *dir);

0 commit comments

Comments
 (0)