]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/notlist.c
Initial revision
[nfs-utils.git] / utils / statd / notlist.c
1 /*
2  * Copyright (C) 1995, 1997-1999 Jeffrey A. Uphoff
3  * Modified by Olaf Kirch, 1996.
4  * Modified by H.J. Lu, 1998.
5  *
6  * NSM for Linux.
7  */
8
9 /*
10  * Simple list management for notify list
11  */
12
13 #include "config.h"
14
15 #include <string.h>
16 #include "misc.h"
17 #include "statd.h"
18 #include "notlist.h"
19
20 notify_list *
21 nlist_new(char *my_name, char *mon_name, int state)
22 {
23         notify_list     *new;
24
25         if (!(new = (notify_list *) xmalloc(sizeof(notify_list))))
26                 return NULL;
27         memset(new, 0, sizeof(*new));
28
29         NL_TIMES(new) = MAX_TRIES;
30         NL_STATE(new) = state;
31         if (!(NL_MY_NAME(new) = xstrdup(my_name))
32          || !(NL_MON_NAME(new) = xstrdup(mon_name)))
33                 return NULL;
34
35         return new;
36 }
37
38 void
39 nlist_insert(notify_list **head, notify_list *entry)
40 {
41         notify_list     *next = *head, *tail = entry;
42
43         /* Find end of list to be inserted */
44         while (tail->next)
45                 tail = tail->next;
46
47         if (next)
48                 next->prev = tail;
49         tail->next = next;
50         *head = entry;
51 }
52
53 void
54 nlist_insert_timer(notify_list **head, notify_list *entry)
55 {
56         /* Find first entry with higher timeout value */
57         while (*head && NL_WHEN(*head) <= NL_WHEN(entry))
58                 head = &(*head)->next;
59         nlist_insert(head, entry);
60 }
61
62 void
63 nlist_remove(notify_list **head, notify_list *entry)
64 {
65         notify_list     *prev = entry->prev,
66                         *next = entry->next;
67
68         if (next)
69                 next->prev = prev;
70         if (prev)
71                 prev->next = next;
72         else
73                 *head = next;
74         entry->next = entry->prev = NULL;
75 }
76
77 notify_list *
78 nlist_clone(notify_list *entry)
79 {
80         notify_list     *new;
81
82         new = nlist_new(NL_MY_NAME(entry), NL_MON_NAME(entry), NL_STATE(entry));
83         NL_MY_PROG(new) = NL_MY_PROG(entry);
84         NL_MY_VERS(new) = NL_MY_VERS(entry);
85         NL_MY_PROC(new) = NL_MY_PROC(entry);
86         NL_ADDR(new)    = NL_ADDR(entry);
87         memcpy(NL_PRIV(new), NL_PRIV(entry), SM_PRIV_SIZE);
88
89         return new;
90 }
91
92 void
93 nlist_free(notify_list **head, notify_list *entry)
94 {
95         if (head)
96                 nlist_remove(head, entry);
97         if (NL_MY_NAME(entry))
98                 free(NL_MY_NAME(entry));
99         if (NL_MON_NAME(entry))
100                 free(NL_MON_NAME(entry));
101         free(entry);
102 }
103
104 void
105 nlist_kill(notify_list **head)
106 {
107         while (*head)
108                 nlist_free(head, *head);
109 }
110
111 /*
112  * Walk a list looking for a matching name in the NL_MON_NAME field.
113  */
114 notify_list *
115 nlist_gethost(notify_list *list, char *host, int myname)
116 {
117         notify_list     *lp;
118
119         for (lp = list; lp; lp = lp->next) {
120                 if (matchhostname(host, myname? NL_MY_NAME(lp) : NL_MON_NAME(lp)))
121                         return lp;
122         }
123
124         return (notify_list *) NULL;
125 }