libroken: Build on windows
[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
13 #include <roken.h>
14
15 #include <afs/stds.h>
16 #include <sys/types.h>
17 #ifdef AFS_NT40_ENV
18 #include <winsock2.h>
19 #else
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 #include <sys/time.h>
24 #endif
25 #include <afs/com_err.h>
26 #include <afs/bubasics.h>
27 #include "bc.h"
28 #include "error_macros.h"
29 #include "bucoord_internal.h"
30 #include "bucoord_prototypes.h"
31
32 extern dlqlinkT statusHead;     /* chain of status blocks */
33 extern struct Lock statusQueueLock;     /* access control for status chain */
34 extern struct Lock cmdLineLock; /* lock on the cmdLine */
35
36 /* task status management
37  *
38  * These routines are common the backup coordinator and tape coordinator
39  */
40
41 void
42 initStatus(void)
43 {
44     dlqInit(&statusHead);
45     Lock_Init(&statusQueueLock);
46     Lock_Init(&cmdLineLock);
47 }
48
49 /* lock managment */
50
51 void
52 lock_Status(void)
53 {
54     ObtainWriteLock(&statusQueueLock);
55 }
56
57 void
58 unlock_Status(void)
59 {
60     ReleaseWriteLock(&statusQueueLock);
61 }
62
63 void
64 lock_cmdLine(void)
65 {
66     ObtainWriteLock(&cmdLineLock);
67 }
68 void
69 unlock_cmdLine(void)
70 {
71     ReleaseWriteLock(&cmdLineLock);
72 }
73
74 /* general */
75
76 void
77 clearStatus(afs_uint32 taskId, afs_uint32 flags)
78 {
79     statusP ptr;
80
81     ObtainWriteLock(&statusQueueLock);
82     ptr = findStatus(taskId);
83     if (ptr == 0) {
84         ReleaseWriteLock(&statusQueueLock);
85         return;
86     }
87
88     ptr->flags &= ~flags;
89     ReleaseWriteLock(&statusQueueLock);
90 }
91
92 statusP
93 createStatusNode(void)
94 {
95     statusP ptr;
96
97     ptr = (statusP) malloc(sizeof(*ptr));
98     if (ptr == 0) {
99         return (0);
100     }
101     memset(ptr, 0, sizeof(*ptr));
102
103     /* link it onto the chain of status entries */
104     ObtainWriteLock(&statusQueueLock);
105     dlqLinkb(&statusHead, (dlqlinkP) ptr);
106     ptr->flags = STARTING;
107     ReleaseWriteLock(&statusQueueLock);
108
109     return (ptr);
110 }
111
112 void
113 deleteStatusNode(statusP ptr)
114 {
115     ObtainWriteLock(&statusQueueLock);
116     dlqUnlink((dlqlinkP) ptr);
117
118     if (ptr->cmdLine)
119         free(ptr->cmdLine);
120     free(ptr);
121     ReleaseWriteLock(&statusQueueLock);
122 }
123
124 statusP
125 findStatus(afs_uint32 taskId)
126 {
127     statusP ptr = 0;
128     dlqlinkP dlqPtr;
129
130     dlqPtr = statusHead.dlq_next;
131     while (dlqPtr != &statusHead) {
132         if (((statusP) dlqPtr)->taskId == taskId) {
133             ptr = (statusP) dlqPtr;
134             break;
135         }
136         dlqPtr = dlqPtr->dlq_next;
137     }
138
139     return (ptr);
140 }
141
142 void
143 setStatus(afs_uint32 taskId, afs_uint32 flags)
144 {
145     statusP ptr;
146
147     ObtainWriteLock(&statusQueueLock);
148     ptr = findStatus(taskId);
149     if (ptr == 0) {
150         ReleaseWriteLock(&statusQueueLock);
151         return;
152     }
153
154     ptr->flags |= flags;
155     ReleaseWriteLock(&statusQueueLock);
156 }