death to trailing whitespace
[openafs.git] / src / butc / tcstatus.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
14 #include <sys/types.h>
15 #ifdef AFS_NT40_ENV
16 #include <winsock2.h>
17 #else
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <sys/socket.h>
21 #include <sys/time.h>
22 #include <strings.h>
23 #endif
24 #include <stdio.h>
25 #include <string.h>
26 #include <afs/com_err.h>
27 #include <lock.h>
28 #include <afs/bubasics.h>
29 #include <afs/tcdata.h>
30 #include <afs/butc.h>
31 #include <afs/budb_client.h>
32 #include <afs/bucoord_prototypes.h>
33 #include "butc_internal.h"
34
35 #include "error_macros.h"
36 #include "butc_xbsa.h"
37 /* tape coordinator - task status management */
38 extern afs_int32 xbsaType;
39
40 dlqlinkT statusHead;
41 struct Lock statusQueueLock;
42 struct Lock cmdLineLock;
43
44 /* STC_GetStatus
45  *      get the status of a task
46  * entry:
47  *      taskId - task for which status required
48  * exit:
49  *      statusPtr - filled in with task status
50  */
51
52 afs_int32
53 STC_GetStatus(struct rx_call *call, afs_uint32 taskId,
54               struct tciStatusS *statusPtr)
55 {
56     statusP ptr;
57     int retval = 0;
58
59     if (callPermitted(call) == 0)
60         return (TC_NOTPERMITTED);
61
62     lock_Status();
63     ptr = findStatus(taskId);
64     if (ptr) {
65         /* strcpy(statusPtr->status, ptr->status); */
66
67         strcpy(statusPtr->taskName, ptr->taskName);
68         strcpy(statusPtr->volumeName, ptr->volumeName);
69         statusPtr->taskId = ptr->taskId;
70         statusPtr->flags = ptr->flags;
71         statusPtr->nKBytes = ptr->nKBytes;
72         statusPtr->dbDumpId = ptr->dbDumpId;
73         statusPtr->lastPolled = ptr->lastPolled;
74         statusPtr->volsFailed = ptr->volsFailed;
75         ptr->lastPolled = time(0);
76     } else
77         retval = TC_NODENOTFOUND;
78     unlock_Status();
79
80     return (retval);
81 }
82
83 afs_int32
84 STC_EndStatus(struct rx_call *call, afs_uint32 taskId)
85 {
86     statusP ptr;
87     int retval = 0;
88
89     if (callPermitted(call) == 0)
90         return (TC_NOTPERMITTED);
91
92     lock_Status();
93     ptr = findStatus(taskId);
94     unlock_Status();
95
96     if (ptr)
97         deleteStatusNode(ptr);
98     else
99         retval = TC_NODENOTFOUND;
100
101     return (retval);
102 }
103
104 afs_int32
105 STC_RequestAbort(struct rx_call *call, afs_uint32 taskId)
106 {
107     statusP ptr;
108     int retval = 0;
109
110     if (callPermitted(call) == 0)
111         return (TC_NOTPERMITTED);
112
113     lock_Status();
114     ptr = findStatus(taskId);
115     if (ptr)
116         ptr->flags |= ABORT_REQUEST;
117     else
118         retval = TC_NODENOTFOUND;
119     unlock_Status();
120     return (retval);
121 }
122
123 /* STC_ScanStatus
124  *      Get status of all tasks on the butc, successively. Initial call
125  *      should come in with TSK_STAT_FIRST flag set to initialize the
126  *      scan.
127  * entry:
128  *      taskId - specifies the task whose status is to be returned
129  *              (unless TSK_STAT_FIRST set in which case it is ignored)
130  * exit:
131  *      taskId - id of next task in the list
132  *      flags - TSK_STAT_END will be set when one reaches the end of
133  *              the task list. taskId is not updated in this case.
134  * return values:
135  *      0 - normal
136  *      TC_NOTASKS - no tasks active
137  */
138
139 afs_int32
140 STC_ScanStatus(struct rx_call *call, afs_uint32 *taskId,
141                struct tciStatusS *statusPtr, afs_uint32 *flags)
142 {
143     statusP ptr = 0;
144     dlqlinkP dlqPtr;
145
146     if (callPermitted(call) == 0)
147         return (TC_NOTPERMITTED);
148
149     lock_Status();
150
151     if (CONF_XBSA)
152         *flags |= TSK_STAT_XBSA;
153     if (xbsaType == XBSA_SERVER_TYPE_ADSM)
154         *flags |= TSK_STAT_ADSM;
155
156     if (*flags & TSK_STAT_FIRST) {
157         /* find first status node */
158         dlqPtr = statusHead.dlq_next;
159         if (dlqPtr == &statusHead) {
160             /* no status nodes */
161             *flags |= (TSK_STAT_NOTFOUND | TSK_STAT_END);
162             unlock_Status();
163             return (0);
164         }
165         ptr = (statusP) dlqPtr;
166     } else {
167         ptr = findStatus(*taskId);
168         if (ptr == 0) {
169             /* in the event that the set of tasks has changed, just
170              * finish, letting the caller retry
171              */
172
173             *flags |= (TSK_STAT_NOTFOUND | TSK_STAT_END);
174             unlock_Status();
175             return (0);
176         }
177     }
178
179     /* ptr is now set to the status node we wish to return. Determine
180      * what the next node will be
181      */
182
183     if (ptr->link.dlq_next == &statusHead)
184         *flags |= TSK_STAT_END;
185     else
186         *taskId = ((statusP) ptr->link.dlq_next)->taskId;
187
188     strcpy(statusPtr->taskName, ptr->taskName);
189     strcpy(statusPtr->volumeName, ptr->volumeName);
190     statusPtr->taskId = ptr->taskId;
191     statusPtr->flags = ptr->flags;
192     statusPtr->nKBytes = ptr->nKBytes;
193     statusPtr->lastPolled = ptr->lastPolled;
194
195     unlock_Status();
196     return (0);
197 }
198
199
200 /* ---------------------------------
201  * misc. status management routines
202  * ---------------------------------
203  */
204
205 /* checkAbortByTaskId
206  * exit:
207  *      0 - continue
208  *      n - abort requested
209  */
210
211 int
212 checkAbortByTaskId(afs_uint32 taskId)
213 {
214     statusP statusPtr;
215     int retval = 0;
216
217     lock_Status();
218     statusPtr = findStatus(taskId);
219     if (statusPtr) {
220         retval = statusPtr->flags & ABORT_REQUEST;
221     }
222     unlock_Status();
223     return (retval);
224 }
225
226 /* getStatusFlag
227  *      For backwards compatibility. Queries flag status
228  * exit:
229  *      0 - flag clear
230  *      n - flag set
231  */
232
233 afs_uint32
234 getStatusFlag(afs_uint32 taskId, afs_uint32 flag)
235 {
236     statusP statusPtr;
237     int retval = 0;
238
239     lock_Status();
240     statusPtr = findStatus(taskId);
241     if (statusPtr) {
242         retval = statusPtr->flags & flag;
243     }
244     unlock_Status();
245     return (retval);
246 }