Extended MOTD with GUI
[tomato.git] / release / src / router / hotplug2 / childlist.c
bloba0ced04d5a836a5e43dc2b863e11cf86f641d656
1 /*****************************************************************************\
2 * _ _ _ _ ___ *
3 * | || | ___ | |_ _ __ | | _ _ __ _ |_ ) *
4 * | __ |/ _ \| _|| '_ \| || || |/ _` | / / *
5 * |_||_|\___/ \__|| .__/|_| \_,_|\__, |/___| *
6 * |_| |___/ *
7 \*****************************************************************************/
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <sys/wait.h>
13 #include <sys/types.h>
15 #include "mem_utils.h"
16 #include "hotplug2.h"
17 #include "childlist.h"
19 struct hotplug2_child_t *add_child(struct hotplug2_child_t *child, pid_t pid, event_seqnum_t seqnum) {
20 void *tmp;
22 if (child == NULL) {
23 child = xmalloc(sizeof(struct hotplug2_child_t));
24 tmp = NULL;
25 } else {
26 for (; child->next; child = child->next);
28 child->next = xmalloc(sizeof(struct hotplug2_child_t));
29 tmp = child;
30 child = child->next;
33 child->seqnum = seqnum;
34 child->pid = pid;
35 child->prev = tmp;
36 child->next = NULL;
38 return child;
41 struct hotplug2_child_t *remove_child_by_pid(struct hotplug2_child_t *child, pid_t pid, event_seqnum_t *largest_seqnum, int *child_c) {
42 struct hotplug2_child_t *tmp_child;
44 if (child == NULL) {
45 ERROR("remove_child_by_pid", "Invalid child list passed (NULL).");
46 return NULL;
49 tmp_child = child;
51 for (; child->prev && child->pid != pid; child = child->prev);
53 if (child->pid != pid) {
54 return tmp_child;
57 if (child->prev != NULL)
58 ((struct hotplug2_child_t *)(child->prev))->next = child->next;
60 if (child->next != NULL)
61 ((struct hotplug2_child_t *)(child->next))->prev = child->prev;
63 if (largest_seqnum != NULL)
64 if (child->seqnum > *largest_seqnum)
65 *largest_seqnum = child->seqnum;
67 if (child_c != NULL)
68 *child_c -= 1;
70 if (child == tmp_child) {
71 if (child->next != NULL)
72 tmp_child = child->next;
73 else if (child->prev != NULL)
74 tmp_child = child->prev;
75 else
76 tmp_child = NULL;
79 /* We always want the rightmost */
80 if (tmp_child != NULL)
81 for (; tmp_child->next; tmp_child = tmp_child->next);
83 free(child);
84 return tmp_child;