a590dc5fecf02fbc5344f7e2418310f743970e8e
[openafs.git] / src / bucoord / status.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <roken.h>
15
16 #include <afs/com_err.h>
17 #include <afs/bubasics.h>
18
19 #include "bc.h"
20 #include "error_macros.h"
21 #include "bucoord_internal.h"
22 #include "bucoord_prototypes.h"
23
24 extern dlqlinkT statusHead;     /* chain of status blocks */
25 extern struct Lock statusQueueLock;     /* access control for status chain */
26 extern struct Lock cmdLineLock; /* lock on the cmdLine */
27
28 /* task status management
29  *
30  * These routines are common the backup coordinator and tape coordinator
31  */
32
33 void
34 initStatus(void)
35 {
36     dlqInit(&statusHead);
37     Lock_Init(&statusQueueLock);
38     Lock_Init(&cmdLineLock);
39 }
40
41 /* lock managment */
42
43 void
44 lock_Status(void)
45 {
46     ObtainWriteLock(&statusQueueLock);
47 }
48
49 void
50 unlock_Status(void)
51 {
52     ReleaseWriteLock(&statusQueueLock);
53 }
54
55 void
56 lock_cmdLine(void)
57 {
58     ObtainWriteLock(&cmdLineLock);
59 }
60 void
61 unlock_cmdLine(void)
62 {
63     ReleaseWriteLock(&cmdLineLock);
64 }
65
66 /* general */
67
68 void
69 clearStatus(afs_uint32 taskId, afs_uint32 flags)
70 {
71     statusP ptr;
72
73     ObtainWriteLock(&statusQueueLock);
74     ptr = findStatus(taskId);
75     if (ptr == 0) {
76         ReleaseWriteLock(&statusQueueLock);
77         return;
78     }
79
80     ptr->flags &= ~flags;
81     ReleaseWriteLock(&statusQueueLock);
82 }
83
84 statusP
85 createStatusNode(void)
86 {
87     statusP ptr;
88
89     ptr = calloc(1, sizeof(*ptr));
90     if (ptr == 0) {
91         return (0);
92     }
93
94     /* link it onto the chain of status entries */
95     ObtainWriteLock(&statusQueueLock);
96     dlqLinkb(&statusHead, (dlqlinkP) ptr);
97     ptr->flags = STARTING;
98     ReleaseWriteLock(&statusQueueLock);
99
100     return (ptr);
101 }
102
103 void
104 deleteStatusNode(statusP ptr)
105 {
106     ObtainWriteLock(&statusQueueLock);
107     dlqUnlink((dlqlinkP) ptr);
108
109     if (ptr->cmdLine)
110         free(ptr->cmdLine);
111     free(ptr);
112     ReleaseWriteLock(&statusQueueLock);
113 }
114
115 statusP
116 findStatus(afs_uint32 taskId)
117 {
118     statusP ptr = 0;
119     dlqlinkP dlqPtr;
120
121     dlqPtr = statusHead.dlq_next;
122     while (dlqPtr != &statusHead) {
123         if (((statusP) dlqPtr)->taskId == taskId) {
124             ptr = (statusP) dlqPtr;
125             break;
126         }
127         dlqPtr = dlqPtr->dlq_next;
128     }
129
130     return (ptr);
131 }
132
133 void
134 setStatus(afs_uint32 taskId, afs_uint32 flags)
135 {
136     statusP ptr;
137
138     ObtainWriteLock(&statusQueueLock);
139     ptr = findStatus(taskId);
140     if (ptr == 0) {
141         ReleaseWriteLock(&statusQueueLock);
142         return;
143     }
144
145     ptr->flags |= flags;
146     ReleaseWriteLock(&statusQueueLock);
147 }