1/*****************************************************************************\
2*  _  _       _          _              ___                                   *
3* | || | ___ | |_  _ __ | | _  _  __ _ |_  )                                  *
4* | __ |/ _ \|  _|| '_ \| || || |/ _` | / /                                   *
5* |_||_|\___/ \__|| .__/|_| \_,_|\__, |/___|                                  *
6*                 |_|            |___/                                        *
7\*****************************************************************************/
8
9#include <stdio.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <sys/wait.h>
13#include <sys/types.h>
14
15#include "mem_utils.h"
16#include "hotplug2.h"
17#include "childlist.h"
18
19 struct hotplug2_child_t *add_child(struct hotplug2_child_t *child, pid_t pid, event_seqnum_t seqnum) {
20	void *tmp;
21
22	if (child == NULL) {
23		child = xmalloc(sizeof(struct hotplug2_child_t));
24		tmp = NULL;
25	} else {
26		for (; child->next; child = child->next);
27
28		child->next = xmalloc(sizeof(struct hotplug2_child_t));
29		tmp = child;
30		child = child->next;
31	}
32
33	child->seqnum = seqnum;
34	child->pid = pid;
35	child->prev = tmp;
36	child->next = NULL;
37
38	return child;
39}
40
41struct 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;
43
44	if (child == NULL) {
45		ERROR("remove_child_by_pid", "Invalid child list passed (NULL).");
46		return NULL;
47	}
48
49	tmp_child = child;
50
51	for (; child->prev && child->pid != pid; child = child->prev);
52
53	if (child->pid != pid) {
54		return tmp_child;
55	}
56
57	if (child->prev != NULL)
58		((struct hotplug2_child_t *)(child->prev))->next = child->next;
59
60	if (child->next != NULL)
61		((struct hotplug2_child_t *)(child->next))->prev = child->prev;
62
63	if (largest_seqnum != NULL)
64		if (child->seqnum > *largest_seqnum)
65			*largest_seqnum = child->seqnum;
66
67	if (child_c != NULL)
68		*child_c -= 1;
69
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;
77	}
78
79	/* We always want the rightmost */
80	if (tmp_child != NULL)
81		for (; tmp_child->next; tmp_child = tmp_child->next);
82
83	free(child);
84	return tmp_child;
85}
86
87