2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
16 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
24 #include <afs/com_err.h>
25 #include <afs/bubasics.h>
27 #include <afs/tcdata.h>
29 #include "error_macros.h"
31 #define SET_FLAG(set) \
33 curPollPtr->flags |= (set); \
36 #define CLEAR_FLAG(clear) \
38 curPollPtr->flags &= ~(clear); \
41 extern struct bc_config *bc_globalConfig;
42 extern afs_int32 bc_GetConn(struct bc_config *aconfig, afs_int32 aport, struct rx_connection **tconn);
43 extern statusP findStatus(afs_uint32 taskId);
45 /* globals for backup coordinator status management */
47 dlqlinkT statusHead; /* chain of status blocks */
48 struct Lock statusQueueLock; /* access control for status chain */
49 struct Lock cmdLineLock; /* lock on the cmdLine */
51 afs_int32 lastTaskCode; /* Error code from task that last finished */
54 * get next item for status interrogation, if any.
62 ptr = (dlqlinkP) linkPtr;
64 /* if last known item has terminated, reset ptr */
73 /* if we're back at the head again */
74 if (ptr == &statusHead)
76 return ((statusP) ptr);
86 ptr = (dlqlinkP) linkPtr;
88 /* if last known item has terminated, reset ptr */
97 /* if we're back at the head again */
98 if (ptr == &statusHead) {
101 return ((statusP) ptr);
111 char **targv[MAXV]; /*Ptr to parsed argv stuff */
112 afs_int32 targc; /*Num parsed arguments */
114 char *internalCmdLine;
116 internalCmdLine = cmdLine;
119 code = cmd_ParseLine(internalCmdLine, targv, &targc, MAXV);
121 printf("Couldn't parse line: '%s'", afs_error_message(code));
124 free(internalCmdLine);
127 * Because the "-at" option cannot be wildcarded, we cannot fall
128 * into recusive loop here by setting dispatchCount to 1.
130 doDispatch(targc, targv, 1);
137 struct rx_connection *tconn = (struct rc_connection *)0;
138 statusP curPollPtr = 0;
140 struct tciStatusS statusPtr;
142 /* task information */
143 afs_uint32 taskFlags;
144 afs_uint32 localTaskFlags;
145 afs_uint32 temp; /* for flag manipulation */
158 rx_DestroyConnection(tconn);
159 tconn = (struct rc_connection *)0;
162 curPollPtr = nextItem(curPollPtr);
164 if (curPollPtr == 0) {
165 #ifdef AFS_PTHREAD_ENV
166 struct timespec delaytime;
168 delayTime.tv_sec = 5;
169 delayTime.tv_nsec = 0;
170 pthread_delay_np(&delayTime);
173 IOMGR_Sleep(5); /* wait a while */
174 #endif /*else AFS_PTHREAD_ENV */
178 /* save useful information */
179 localTaskFlags = curPollPtr->flags;
180 taskId = curPollPtr->taskId;
181 port = curPollPtr->port;
182 atTime = curPollPtr->scheduledDump;
183 jobNumber = curPollPtr->jobNumber;
186 /* reset certain flags; local kill; */
187 CLEAR_FLAG(ABORT_LOCAL);
189 /* An abort request before the command even started */
190 if (atTime && (localTaskFlags & ABORT_REQUEST)) {
191 if (localTaskFlags & NOREMOVE) {
192 curPollPtr->flags |= (STARTING | ABORT_DONE); /* Will ignore on other passes */
193 curPollPtr->scheduledDump = 0;
195 deleteStatusNode(curPollPtr);
201 /* A task not started yet - check its start time */
202 if (localTaskFlags & STARTING || atTime) {
204 * Start a timed dump if its time has come. When the job is
205 * started, it will allocate its own status structure so this
206 * one is no longer needed: delete it.
208 * Avoid multiple processes trouncing the cmdLine by placing
211 if (atTime && (atTime <= time(0))) {
212 lock_cmdLine(); /* Will unlock in cmdDispatch */
214 cmdLine = curPollPtr->cmdLine;
216 curPollPtr->cmdLine = 0;
219 printf("Starting scheduled dump: job %d\n", jobNumber);
220 printf("schedD> %s\n", cmdLine);
223 LWP_CreateProcess(cmdDispatch, 16384, LWP_NORMAL_PRIORITY,
224 (void *)2, "cmdDispatch", &dispatchPid);
229 printf("Couldn't create cmdDispatch task\n");
232 if (localTaskFlags & NOREMOVE) {
233 curPollPtr->flags |= STARTING; /* Will ignore on other passes */
234 curPollPtr->flags |= (code ? TASK_ERROR : TASK_DONE);
235 curPollPtr->scheduledDump = 0;
237 deleteStatusNode(curPollPtr);
244 if (localTaskFlags & ABORT_LOCAL) {
245 /* kill the local task */
246 if ((localTaskFlags & CONTACT_LOST) != 0) {
247 printf("Job %d: in contact with butc at port %d\n", jobNumber,
249 printf("Job %d cont: Local kill ignored - use normal kill\n",
254 code = (afs_int32) bc_GetConn(bc_globalConfig, port, &tconn);
256 SET_FLAG(CONTACT_LOST);
260 if (CheckTCVersion(tconn)) {
261 SET_FLAG(CONTACT_LOST);
265 /* Send abort to TC requst if we have to */
266 if (localTaskFlags & ABORT_REQUEST) {
267 code = TC_RequestAbort(tconn, taskId);
269 afs_com_err("statusWatcher", code, "; Can't post abort request");
270 afs_com_err("statusWatcher", 0, "...Deleting job");
271 if (localTaskFlags & NOREMOVE) {
272 curPollPtr->flags |= (STARTING | TASK_ERROR);
273 curPollPtr->scheduledDump = 0;
275 deleteStatusNode(curPollPtr);
281 curPollPtr->flags &= ~ABORT_REQUEST;
282 curPollPtr->flags |= ABORT_SENT;
287 /* otherwise just get the status */
288 code = TC_GetStatus(tconn, taskId, &statusPtr);
290 if (code == TC_NODENOTFOUND) {
291 printf("Job %d: %s - no such task on port %d, deleting\n",
292 jobNumber, curPollPtr->taskName, port);
294 if (localTaskFlags & NOREMOVE) {
295 curPollPtr->flags |= (STARTING | TASK_ERROR);
296 curPollPtr->scheduledDump = 0;
298 deleteStatusNode(curPollPtr); /* delete this status node */
304 SET_FLAG(CONTACT_LOST);
308 /* in case we previously lost contact or couldn't find */
309 CLEAR_FLAG(CONTACT_LOST);
311 /* extract useful status */
312 taskFlags = statusPtr.flags;
314 /* update local status */
317 /* remember some status flags in local struct */
319 (DRIVE_WAIT | OPR_WAIT | CALL_WAIT | TASK_DONE | ABORT_DONE |
321 curPollPtr->flags &= ~temp; /* clear */
322 curPollPtr->flags |= (taskFlags & temp); /* update */
324 curPollPtr->dbDumpId = statusPtr.dbDumpId;
325 curPollPtr->nKBytes = statusPtr.nKBytes;
326 strcpy(curPollPtr->volumeName, statusPtr.volumeName);
327 curPollPtr->volsFailed = statusPtr.volsFailed;
328 curPollPtr->lastPolled = statusPtr.lastPolled;
332 if (taskFlags & TASK_DONE) { /*done */
333 if (taskFlags & ABORT_DONE) {
334 if (curPollPtr->dbDumpId)
335 printf("Job %d: %s: DumpID %u Aborted", jobNumber,
336 curPollPtr->taskName, curPollPtr->dbDumpId);
338 printf("Job %d: %s Aborted", jobNumber,
339 curPollPtr->taskName);
341 if (taskFlags & TASK_ERROR)
342 printf(" with errors\n");
349 else if (taskFlags & TASK_ERROR) {
350 if (!(localTaskFlags & SILENT)) {
351 if (curPollPtr->dbDumpId)
352 printf("Job %d: DumpID %u Failed with errors\n",
353 jobNumber, curPollPtr->dbDumpId);
355 printf("Job %d Failed with errors\n", jobNumber);
361 if (!(localTaskFlags & SILENT)) {
362 if (curPollPtr->dbDumpId)
363 printf("Job %d: %s: DumpID %u finished", jobNumber,
364 curPollPtr->taskName, curPollPtr->dbDumpId);
366 printf("Job %d: %s finished", jobNumber,
367 curPollPtr->taskName);
369 if (curPollPtr->volsTotal) {
370 printf(". %d volumes dumped",
371 (curPollPtr->volsTotal -
372 curPollPtr->volsFailed));
373 if (curPollPtr->volsFailed)
374 printf(", %d failed", curPollPtr->volsFailed);
382 /* make call to destroy task on server */
383 code = TC_EndStatus(tconn, taskId);
385 printf("Job %d: %s, error in job termination cleanup\n",
386 jobNumber, curPollPtr->taskName);
388 if (localTaskFlags & NOREMOVE) {
389 curPollPtr->flags |= STARTING;
390 curPollPtr->scheduledDump = 0;
392 deleteStatusNode(curPollPtr); /* unlink and destroy local task */
400 * Allocate a job number. Computes the maximum of all the job numbers
401 * and then returns the maximum+1.
402 * If no jobs are found, returns 1.
408 afs_int32 retval = 0;
411 ptr = statusHead.dlq_next;
412 while (ptr != &statusHead) {
413 /* compute max of all job numbers */
414 if (((statusP) ptr)->jobNumber > retval)
415 retval = ((statusP) ptr)->jobNumber;
424 * Wait for a specific task to finish and then return.
425 * Return the task's flags when it's done. If the job
426 * had been cleaned up, then just return 0.
432 afs_int32 done = 0, rcode, t;
434 t = (TASK_DONE | ABORT_DONE | TASK_ERROR);
436 /* Sleep 2 seconds */
437 #ifdef AFS_PTHREAD_ENV
438 struct timespec delaytime;
439 delayTime.tv_sec = 2;
440 delayTime.tv_nsec = 0;
441 pthread_delay_np(&delayTime);
444 #endif /*else AFS_PTHREAD_ENV */
446 /* Check if we are done */
448 ptr = findStatus(taskId);
449 if (!ptr || (ptr->flags & t)) {
450 rcode = (ptr ? ptr->flags : 0);