svc: Re-entrant replacement to svc_iterator() --> svc_iterator1()

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2017-12-18 00:18:13 +01:00
1 parent 23e0928e5c
commit bf004d9ec4
2 files changed
+37 -15

No files matched your search

+36 -15
View File
@@ -133,6 +133,37 @@ int svc_del(svc_t *svc)
return 0; return 0;
} }
/**
* svc_iterator1 - Naive iterator over all registered services.
* @pos: Iterator variable, preserves state between calls
* @first: Get first &svc_t object, or next until end.
*
* Returns:
* The first &svc_t when @first is set, otherwise the next &svc_t until
* the end when %NULL is returned.
*/
svc_t *svc_iterator1(int *pos, int first)
{
int i;
svc_t *list = __connect_shm();
if (first)
i = 0;
else
i = *(int *)pos;
while (i < MAX_NUM_SVC) {
svc_t *svc = &list[i++];
if (svc->type != SVC_TYPE_FREE) {
*(int *)pos = i;
return svc;
}
}
return NULL;
}
/** /**
* svc_iterator - Naive iterator over all registered services. * svc_iterator - Naive iterator over all registered services.
* @first: Get first &svc_t object, or next until end. * @first: Get first &svc_t object, or next until end.
@@ -143,36 +174,26 @@ int svc_del(svc_t *svc)
*/ */
svc_t *svc_iterator(int first) svc_t *svc_iterator(int first)
{ {
static int i = 0; static int i;
svc_t *list = __connect_shm();
if (first) return svc_iterator1(&i, first);
i = 0;
while (i < MAX_NUM_SVC) {
svc_t *svc = &list[i++];
if (svc->type != SVC_TYPE_FREE)
return svc;
}
return NULL;
} }
/** /**
* svc_inetd_iterator - Naive iterator over all registered inetd services. * svc_inetd_iterator - Naive iterator over all registered inetd services.
* @pos: Iterator variable, preserves state between calls
* @first: Get first &svc_t object, or next until end. * @first: Get first &svc_t object, or next until end.
* *
* Returns: * Returns:
* The first inetd &svc_t when @first is set, otherwise the next * The first inetd &svc_t when @first is set, otherwise the next
* inetd &svc_t until the end when %NULL is returned. * inetd &svc_t until the end when %NULL is returned.
*/ */
svc_t *svc_inetd_iterator(int first) svc_t *svc_inetd_iterator(int *pos, int first)
{ {
svc_t *svc; svc_t *svc;
for (svc = svc_iterator(first); svc; svc = svc_iterator(0)) { for (svc = svc_iterator1(pos, first); svc; svc = svc_iterator1(pos, 0)) {
if (svc_is_inetd(svc)) if (svc_is_inetd(svc))
return svc; return svc;
} }
+1
View File
@@ -159,6 +159,7 @@ svc_t *svc_inetd_iterator (int first);
svc_t *svc_dynamic_iterator (int first); svc_t *svc_dynamic_iterator (int first);
svc_t *svc_named_iterator (int first, char *cmd); svc_t *svc_named_iterator (int first, char *cmd);
svc_t *svc_job_iterator (int first, int job); svc_t *svc_job_iterator (int first, int job);
svc_t *svc_iterator1 (int *pos, int first);
void svc_foreach (void (*cb)(svc_t *)); void svc_foreach (void (*cb)(svc_t *));
void svc_foreach_dynamic (void (*cb)(svc_t *)); void svc_foreach_dynamic (void (*cb)(svc_t *));