OPENAFS-SA-2018-002 butc: Initialize OUT scalar value
[openafs.git] / src / butc / tcprocs.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 /* procedures invoked by the rpc stub */
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14
15 #include <afs/procmgmt.h>
16 #include <roken.h>
17
18 #include <afs/opr.h>
19 #include <rx/rx.h>
20 #include <afs/afsint.h>
21 #include <afs/prs_fs.h>
22 #include <afs/nfs.h>
23 #include <lwp.h>
24 #include <lock.h>
25 #include <afs/cellconfig.h>
26 #include <afs/keys.h>
27 #include <ubik.h>
28 #include <afs/tcdata.h>
29 #include <afs/budb_client.h>
30 #include <afs/bucoord_prototypes.h>
31
32 #include "error_macros.h"
33 #include "butc_xbsa.h"
34 #include "butc_prototypes.h"
35 #include "butc_internal.h"
36
37 static int CopyDumpDesc(struct tc_dumpDesc *, tc_dumpArray *);
38 static int CopyRestoreDesc(struct tc_restoreDesc *, tc_restoreArray *);
39 static int CopyTapeSetDesc(struct tc_tapeSet *, struct tc_tapeSet *);
40
41 int
42 callPermitted(struct rx_call *call)
43 {
44     /*
45      * Before this code can be used, the rx connection, on the bucoord side,
46      * must be changed so that it will set up for token passing instead of
47      * using a simple rx connection that, below, returns a value of
48      * RX_SECIDX_NULL from rx_SecurityClassOf.
49      */
50     return 1;
51 }
52
53 /* -----------------------------
54  * misc. routines
55  * -----------------------------
56  */
57
58 static int
59 CopyDumpDesc(struct tc_dumpDesc *toDump, tc_dumpArray *fromDump)
60 {
61     struct tc_dumpDesc *toPtr, *fromPtr;
62     int i;
63
64     toPtr = toDump;
65     fromPtr = fromDump->tc_dumpArray_val;
66     for (i = 0; i < fromDump->tc_dumpArray_len; i++) {
67         toPtr->vid = fromPtr->vid;
68         toPtr->vtype = fromPtr->vtype;
69         toPtr->partition = fromPtr->partition;
70         toPtr->date = fromPtr->date;
71         toPtr->cloneDate = fromPtr->cloneDate;
72         toPtr->hostAddr = fromPtr->hostAddr;
73         strcpy(toPtr->name, fromPtr->name);
74         fromPtr++;
75         toPtr++;
76     }
77     return 0;
78 }
79
80
81 static int
82 CopyRestoreDesc(struct tc_restoreDesc *toRestore, tc_restoreArray *fromRestore)
83 {
84     struct tc_restoreDesc *toPtr, *fromPtr;
85     int i;
86
87     toPtr = toRestore;
88     fromPtr = fromRestore->tc_restoreArray_val;
89     for (i = 0; i < fromRestore->tc_restoreArray_len; i++) {
90         toPtr->flags = fromPtr->flags;
91         toPtr->position = fromPtr->position;
92         strcpy(toPtr->tapeName, fromPtr->tapeName);
93         toPtr->dbDumpId = fromPtr->dbDumpId;
94         toPtr->initialDumpId = fromPtr->initialDumpId;
95         toPtr->origVid = fromPtr->origVid;
96         toPtr->vid = fromPtr->vid;
97         toPtr->partition = fromPtr->partition;
98         toPtr->dumpLevel = fromPtr->dumpLevel;
99         toPtr->hostAddr = fromPtr->hostAddr;
100         strcpy(toPtr->newName, fromPtr->newName);
101         strcpy(toPtr->oldName, fromPtr->oldName);
102         fromPtr++;
103         toPtr++;
104
105     }
106     return 0;
107 }
108
109 static int
110 CopyTapeSetDesc(struct tc_tapeSet *toPtr, struct tc_tapeSet *fromPtr)
111 {
112
113     toPtr->id = fromPtr->id;
114     toPtr->maxTapes = fromPtr->maxTapes;
115     toPtr->a = fromPtr->a;
116     toPtr->b = fromPtr->b;
117     strcpy(toPtr->tapeServer, fromPtr->tapeServer);
118     strcpy(toPtr->format, fromPtr->format);
119
120     toPtr->expDate = fromPtr->expDate;
121     toPtr->expType = fromPtr->expType;
122     return 0;
123 }
124
125 /* -------------------------
126  * butc - interface routines - alphabetic order
127  * -------------------------
128  */
129
130 afs_int32
131 STC_LabelTape(struct rx_call *acid, struct tc_tapeLabel *label, afs_uint32 *taskId)
132 {
133 #ifdef AFS_PTHREAD_ENV
134     pthread_t pid;
135     pthread_attr_t tattr;
136     AFS_SIGSET_DECL;
137 #else
138     PROCESS pid;
139 #endif
140     struct labelTapeIf *ptr;
141     statusP statusPtr = NULL;
142     afs_int32 code;
143
144 #ifdef xbsa
145     if (CONF_XBSA)
146         return (TC_BADTASK);    /* LabelTape does not apply if XBSA */
147 #endif
148
149     if (callPermitted(acid) == 0)
150         return (TC_NOTPERMITTED);
151
152     ptr = malloc(sizeof(*ptr));
153     if (!ptr)
154         ERROR_EXIT(TC_NOMEMORY);
155     memcpy(&ptr->label, label, sizeof(ptr->label));
156
157     /* set up the status node */
158     *taskId = allocTaskId();    /* for bucoord */
159     ptr->taskId = *taskId;
160
161     statusPtr = createStatusNode();
162     if (!statusPtr)
163         ERROR_EXIT(TC_INTERNALERROR);
164
165     lock_Status();
166     statusPtr->taskId = *taskId;
167     statusPtr->lastPolled = time(0);
168     statusPtr->flags &= ~STARTING;      /* ok to examine */
169     strncpy(statusPtr->taskName, "Labeltape", sizeof(statusPtr->taskName));
170     unlock_Status();
171
172     /* create the LWP to do the real work behind the scenes */
173 #ifdef AFS_PTHREAD_ENV
174     code = pthread_attr_init(&tattr);
175     if (code)
176         ERROR_EXIT(code);
177
178     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
179     if (code)
180         ERROR_EXIT(code);
181
182     AFS_SIGSET_CLEAR();
183     code = pthread_create(&pid, &tattr, Labeller, ptr);
184     AFS_SIGSET_RESTORE();
185 #else
186     code =
187         LWP_CreateProcess(Labeller, 32768, 1, (void *)ptr, "labeller process",
188                           &pid);
189 #endif
190
191   error_exit:
192     if (code) {
193         if (statusPtr)
194             deleteStatusNode(statusPtr);
195         if (ptr)
196             free(ptr);
197     }
198
199     return (code);
200 }
201
202 /* STC_PerformDump
203  *      Tape coordinator server routine to do a dump
204  */
205
206 afs_int32
207 STC_PerformDump(struct rx_call *rxCallId, struct tc_dumpInterface *tcdiPtr, tc_dumpArray *tc_dumpArrayPtr, afs_int32 *taskId)
208 {
209     struct dumpNode *newNode = 0;
210     statusP statusPtr = 0;
211 #ifdef AFS_PTHREAD_ENV
212     pthread_t pid;
213     pthread_attr_t tattr;
214     AFS_SIGSET_DECL;
215 #else
216     PROCESS pid;
217 #endif
218     afs_int32 code = 0;
219
220     if (callPermitted(rxCallId) == 0)
221         return (TC_NOTPERMITTED);
222
223     /* should be verifying parameter validity */
224     *taskId = 0;
225
226     /* this creates a node in list, alots an id for it and prepares it for locking */
227     CreateNode(&newNode);
228
229     /*set up the parameters in the node, to be used by LWP */
230     strcpy(newNode->dumpSetName, tcdiPtr->dumpName);
231
232     newNode->dumpName = strdup(tcdiPtr->dumpPath);
233     newNode->volumeSetName = strdup(tcdiPtr->volumeSetName);
234
235     CopyTapeSetDesc(&(newNode->tapeSetDesc), &tcdiPtr->tapeSet);
236
237     newNode->dumps = malloc(sizeof(struct tc_dumpDesc) *
238                             tc_dumpArrayPtr->tc_dumpArray_len);
239     newNode->arraySize = tc_dumpArrayPtr->tc_dumpArray_len;
240     CopyDumpDesc(newNode->dumps, tc_dumpArrayPtr);
241
242     newNode->parent = tcdiPtr->parentDumpId;
243     newNode->level = tcdiPtr->dumpLevel;
244     newNode->doAppend = tcdiPtr->doAppend;
245 #ifdef xbsa
246     if (CONF_XBSA)
247         newNode->doAppend = 0;  /* Append flag is ignored if talking to XBSA */
248 #endif
249
250     /* create the status node */
251     statusPtr = createStatusNode();
252     if (!statusPtr)
253         ERROR_EXIT(TC_INTERNALERROR);
254
255     lock_Status();
256     statusPtr->taskId = newNode->taskID;
257     statusPtr->lastPolled = time(0);
258     statusPtr->flags &= ~STARTING;      /* ok to examine */
259     strncpy(statusPtr->taskName, "Dump", sizeof(statusPtr->taskName));
260     unlock_Status();
261
262     newNode->statusNodePtr = statusPtr;
263
264     /* create the LWP to do the real work behind the scenes */
265 #ifdef AFS_PTHREAD_ENV
266     code = pthread_attr_init(&tattr);
267     if (code)
268         ERROR_EXIT(code);
269
270     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
271     if (code)
272         ERROR_EXIT(code);
273
274     AFS_SIGSET_CLEAR();
275     code = pthread_create(&pid, &tattr, Dumper, newNode);
276     AFS_SIGSET_RESTORE();
277 #else
278     code =
279         LWP_CreateProcess(Dumper, 32768, 1, (void *)newNode, "dumper process",
280                           &pid);
281 #endif
282     if (code)
283         ERROR_EXIT(code);
284
285     *taskId = newNode->taskID;
286
287   error_exit:
288     if (code) {
289         if (statusPtr)
290             deleteStatusNode(statusPtr);
291         FreeNode(newNode->taskID);      /*  failed to create LWP to do the dump. */
292     }
293
294     return (code);
295 }
296
297 afs_int32
298 STC_PerformRestore(struct rx_call *acid, char *dumpSetName, tc_restoreArray *arestores, afs_int32 *taskID)
299 {
300     struct dumpNode *newNode;
301     statusP statusPtr;
302     afs_int32 code = 0;
303 #ifdef AFS_PTHREAD_ENV
304     pthread_t pid;
305     pthread_attr_t tattr;
306     AFS_SIGSET_DECL;
307 #else
308     PROCESS pid;
309 #endif
310
311     if (callPermitted(acid) == 0)
312         return (TC_NOTPERMITTED);
313
314     /* should  verify parameter validity */
315
316     /* this creates a node in list, alots an id for it and prepares it for locking */
317     CreateNode(&newNode);
318
319     newNode->restores = malloc(sizeof(struct tc_restoreDesc) *
320                                arestores->tc_restoreArray_len);
321     newNode->arraySize = arestores->tc_restoreArray_len;
322     CopyRestoreDesc(newNode->restores, arestores);
323     *taskID = newNode->taskID;
324
325     /* should log the intent */
326
327     /* create the status node */
328     statusPtr = createStatusNode();
329     if (!statusPtr)
330         ERROR_EXIT(TC_INTERNALERROR);
331
332     lock_Status();
333     statusPtr->taskId = newNode->taskID;
334     statusPtr->flags &= ~STARTING;      /* ok to examine */
335     statusPtr->lastPolled = time(0);
336     strncpy(statusPtr->taskName, "Restore", sizeof(statusPtr->taskName));
337     unlock_Status();
338
339     newNode->statusNodePtr = statusPtr;
340
341     /* create the LWP to do the real work behind the scenes */
342 #ifdef AFS_PTHREAD_ENV
343     code = pthread_attr_init(&tattr);
344     if (code)
345         ERROR_EXIT(code);
346
347     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
348     if (code)
349         ERROR_EXIT(code);
350
351     AFS_SIGSET_CLEAR();
352     code = pthread_create(&pid, &tattr, Restorer, newNode);
353     AFS_SIGSET_RESTORE();
354 #else
355     code =
356         LWP_CreateProcess(Restorer, 65368, 1, (void *)newNode,
357                           "restorer process", &pid);
358 #endif
359
360   error_exit:
361     if (code) {
362         if (statusPtr)
363             deleteStatusNode(statusPtr);
364         FreeNode(newNode->taskID);      /*  failed to create LWP to do the dump. */
365     }
366
367     return (code);
368 }
369
370 afs_int32
371 STC_ReadLabel(struct rx_call *acid, struct tc_tapeLabel *label, afs_uint32 *taskId)
372 {
373     afs_int32 code;
374
375     memset(label, 0, sizeof(*label));
376     /* Synchronous, so no "real" ID; don't send stack garbage on the wire */
377     *taskId = 0;
378 #ifdef xbsa
379     if (CONF_XBSA)
380         return (TC_BADTASK);    /* ReadLabel does not apply if XBSA */
381 #endif
382
383     if (callPermitted(acid) == 0)
384         return (TC_NOTPERMITTED);
385
386     code = ReadLabel(label);    /* Synchronous */
387     return code;
388 }
389
390 /* STC_RestoreDb
391  *      restore the backup database from tape
392  */
393
394 afs_int32
395 STC_RestoreDb(struct rx_call *rxCall, afs_uint32 *taskId)
396 {
397 #ifdef AFS_PTHREAD_ENV
398     pthread_t pid;
399     pthread_attr_t tattr;
400     AFS_SIGSET_DECL;
401 #else
402     PROCESS pid;
403 #endif
404     statusP statusPtr;
405     afs_int32 code = 0;
406
407 #ifdef xbsa
408     if (CONF_XBSA)
409         return (TC_BADTASK);    /* LabelTape does not apply if XBSA */
410 #endif
411
412     if (callPermitted(rxCall) == 0)
413         return (TC_NOTPERMITTED);
414
415     *taskId = allocTaskId();
416
417     /* create the status node */
418     statusPtr = createStatusNode();
419     if (!statusPtr)
420         ERROR_EXIT(TC_INTERNALERROR);
421
422     lock_Status();
423     statusPtr->taskId = *taskId;
424     statusPtr->flags &= ~STARTING;      /* ok to examine */
425     statusPtr->lastPolled = time(0);
426     strncpy(statusPtr->taskName, "RestoreDb", sizeof(statusPtr->taskName));
427     unlock_Status();
428
429 #ifdef AFS_PTHREAD_ENV
430     code = pthread_attr_init(&tattr);
431     if (code)
432         ERROR_EXIT(code);
433
434     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
435     if (code)
436         ERROR_EXIT(code);
437
438     AFS_SIGSET_CLEAR();
439     code = pthread_create(&pid, &tattr, restoreDbFromTape, (void *)(intptr_t)*taskId);
440     AFS_SIGSET_RESTORE();
441 #else
442     code =
443         LWP_CreateProcess(restoreDbFromTape, 32768, 1, (void *)(intptr_t)*taskId,
444                           "Db restore", &pid);
445 #endif
446
447   error_exit:
448     if (code) {
449         if (statusPtr)
450             deleteStatusNode(statusPtr);
451     }
452
453     return (code);
454 }
455
456 /* STC_SaveDb
457  *      restore the backup database from tape
458  */
459
460 afs_int32
461 STC_SaveDb(struct rx_call *rxCall, Date archiveTime, afs_uint32 *taskId)
462 {
463 #ifdef AFS_PTHREAD_ENV
464     pthread_t pid;
465     pthread_attr_t tattr;
466     AFS_SIGSET_DECL;
467 #else
468     PROCESS pid;
469 #endif
470     statusP statusPtr = NULL;
471     afs_int32 code = 0;
472     struct saveDbIf *ptr;
473
474 #ifdef xbsa
475     if (CONF_XBSA)
476         return (TC_BADTASK);    /* LabelTape does not apply if XBSA */
477 #endif
478
479     if (callPermitted(rxCall) == 0)
480         return (TC_NOTPERMITTED);
481
482     *taskId = allocTaskId();
483
484     ptr = malloc(sizeof(struct saveDbIf));
485     if (!ptr)
486         ERROR_EXIT(TC_NOMEMORY);
487     ptr->archiveTime = archiveTime;
488     ptr->taskId = *taskId;
489
490     /* create the status node */
491     statusPtr = createStatusNode();
492     if (!statusPtr)
493         ERROR_EXIT(TC_INTERNALERROR);
494
495     lock_Status();
496     statusPtr->taskId = *taskId;
497     statusPtr->lastPolled = time(0);
498     statusPtr->flags &= ~STARTING;      /* ok to examine */
499     strncpy(statusPtr->taskName, "SaveDb", sizeof(statusPtr->taskName));
500     unlock_Status();
501
502     ptr->statusPtr = statusPtr;
503
504 #ifdef AFS_PTHREAD_ENV
505     code = pthread_attr_init(&tattr);
506     if (code)
507         ERROR_EXIT(code);
508
509     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
510     if (code)
511         ERROR_EXIT(code);
512
513     AFS_SIGSET_CLEAR();
514     code = pthread_create(&pid, &tattr, saveDbToTape, ptr);
515     AFS_SIGSET_RESTORE();
516 #else
517     code = LWP_CreateProcess(saveDbToTape, 32768, 1, ptr, "Db save", &pid);
518 #endif
519
520   error_exit:
521     if (code) {
522         if (statusPtr)
523             deleteStatusNode(statusPtr);
524         if (ptr)
525             free(ptr);
526     }
527
528     return (code);
529 }
530
531
532 /* STC_ScanDumps
533  *      read a dump (maybe more than one tape), and print out a summary
534  *      of its contents. If the flag is set, add to the database.
535  * entry:
536  *      addDbFlag - if set, the information will be added to the database
537  */
538
539 afs_int32
540 STC_ScanDumps(struct rx_call *acid, afs_int32 addDbFlag, afs_uint32 *taskId)
541 {
542 #ifdef AFS_PTHREAD_ENV
543     pthread_t pid;
544     pthread_attr_t tattr;
545     AFS_SIGSET_DECL;
546 #else
547     PROCESS pid;
548 #endif
549     struct scanTapeIf *ptr;
550     statusP statusPtr = NULL;
551     afs_int32 code = 0;
552
553 #ifdef xbsa
554     if (CONF_XBSA)
555         return (TC_BADTASK);    /* ScanDumps does not apply if XBSA */
556 #endif
557
558     if (callPermitted(acid) == 0)
559         return (TC_NOTPERMITTED);
560
561     *taskId = allocTaskId();
562
563     ptr = malloc(sizeof(*ptr));
564     if (!ptr)
565         ERROR_EXIT(TC_NOMEMORY);
566     ptr->addDbFlag = addDbFlag;
567     ptr->taskId = *taskId;
568
569     /* create the status node */
570     statusPtr = createStatusNode();
571     if (!statusPtr)
572         ERROR_EXIT(TC_INTERNALERROR);
573
574     lock_Status();
575     statusPtr->taskId = *taskId;
576     statusPtr->lastPolled = time(0);
577     statusPtr->flags &= ~STARTING;      /* ok to examine */
578     strncpy(statusPtr->taskName, "Scantape", sizeof(statusPtr->taskName));
579     unlock_Status();
580
581 #ifdef AFS_PTHREAD_ENV
582     code = pthread_attr_init(&tattr);
583     if (code)
584         ERROR_EXIT(code);
585
586     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
587     if (code)
588         ERROR_EXIT(code);
589
590     AFS_SIGSET_CLEAR();
591     code = pthread_create(&pid, &tattr, ScanDumps, ptr);
592     AFS_SIGSET_RESTORE();
593 #else
594     code =
595         LWP_CreateProcess(ScanDumps, 32768, 1, ptr, "scandump process", &pid);
596 #endif
597
598   error_exit:
599     if (code) {
600         if (statusPtr)
601             deleteStatusNode(statusPtr);
602         if (ptr)
603             free(ptr);
604     }
605
606     return code;
607 }
608
609 /* STC_TCInfo
610  *      return information about the tape coordinator. Currently this
611  *      is just the version number of the interface
612  */
613
614 afs_int32
615 STC_TCInfo(struct rx_call *acid, struct tc_tcInfo *tciptr)
616 {
617     if (callPermitted(acid) == 0)
618         return (TC_NOTPERMITTED);
619
620     tciptr->tcVersion = CUR_BUTC_VERSION;
621     return (0);
622 }
623
624 /* STC_DeleteDump
625  */
626 afs_int32
627 STC_DeleteDump(struct rx_call *acid, afs_uint32 dumpID, afs_uint32 *taskId)
628 {
629     afs_int32 code = TC_BADTASK;        /* If not compiled -Dxbsa then fail */
630 #ifdef xbsa
631     struct deleteDumpIf *ptr = 0;
632     statusP statusPtr = 0;
633 #ifdef AFS_PTHREAD_ENV
634     pthread_t pid;
635     pthread_attr_t tattr;
636     AFS_SIGSET_DECL;
637 #else
638     PROCESS pid;
639 #endif
640 #endif
641
642     *taskId = 0;
643     if (!CONF_XBSA)
644         return (TC_BADTASK);    /* Only do if butc is started as XBSA */
645
646 #ifdef xbsa
647     code = 0;
648     if (callPermitted(acid) == 0)
649         return (TC_NOTPERMITTED);
650
651     ptr = malloc(sizeof(*ptr));
652     if (!ptr)
653         ERROR_EXIT(TC_NOMEMORY);
654
655     *taskId = allocTaskId();
656     ptr->dumpID = dumpID;
657     ptr->taskId = *taskId;
658
659     statusPtr = createStatusNode();
660     if (!statusPtr)
661         ERROR_EXIT(TC_INTERNALERROR);
662
663     lock_Status();
664     statusPtr->taskId = *taskId;
665     statusPtr->lastPolled = time(0);
666     statusPtr->flags &= ~STARTING;
667     strncpy(statusPtr->taskName, "DeleteDump", sizeof(statusPtr->taskName));
668     unlock_Status();
669
670 #ifdef AFS_PTHREAD_ENV
671     code = pthread_attr_init(&tattr);
672     if (code)
673         ERROR_EXIT(code);
674
675     code = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
676     if (code)
677         ERROR_EXIT(code);
678
679     AFS_SIGSET_CLEAR();
680     code = pthread_create(&pid, &tattr, DeleteDump, ptr);
681     AFS_SIGSET_RESTORE();
682 #else
683     code =
684         LWP_CreateProcess(DeleteDump, 32768, 1, ptr, "deletedump process",
685                           &pid);
686 #endif
687
688   error_exit:
689     if (code) {
690         if (statusPtr)
691             deleteStatusNode(statusPtr);
692         if (ptr)
693             free(ptr);
694     }
695 #endif /* xbsa */
696
697     return (code);
698 }
699