Standardize License information
[openafs.git] / src / budb / dbs_dump.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 <afs/param.h>
11 #ifdef AFS_NT40_ENV
12 #include <winsock2.h>
13 #include <io.h>
14 #include <fcntl.h>
15 #else
16 #include <netinet/in.h>
17 #include <sys/time.h>
18 #include <sys/resource.h>
19 #include <sys/file.h>
20 #endif
21 #include <time.h>
22 #include <sys/types.h>
23 #include <afs/stds.h>
24 #include <stdio.h>
25 #include <lock.h>
26 #include <ubik.h>
27 #include <lwp.h>
28 #include <rx/xdr.h>
29 #include <rx/rx.h>
30 #include <rx/rxkad.h>
31 #include <string.h>
32 #include <des.h>
33 #include <afs/cellconfig.h>
34 #include <afs/auth.h>
35 #include <errno.h>
36 #include "budb.h"
37 #include "budb_errs.h"
38 #include "database.h"
39 #include "error_macros.h"
40 #include "globals.h"
41 #include "afs/audit.h"
42
43
44 /* dump ubik database - interface routines */
45
46 /* badEntry
47  *      no checking for now.
48  */
49
50 afs_int32
51 badEntry(dbAddr)
52      afs_uint32 dbAddr;
53 {
54     /* return entry ok */
55     return(0);
56 }
57
58 /* setupDbDump
59  *      decode the arguments passed via LWP and dump the database.
60  */
61
62 setupDbDump(writeFid)
63      int writeFid;
64 {
65     afs_int32 code = 0;
66
67     code = InitRPC(&dumpSyncPtr->ut, LOCKREAD, 1);
68     if ( code )
69         goto error_exit;
70
71     code = writeDatabase(dumpSyncPtr->ut, writeFid);
72     if ( code )
73         LogError(code, "writeDatabase failed\n");
74
75     code = close(writeFid);
76     if ( code )
77         LogError(code, "pipe writer close failed\n");
78
79     LogDebug(5, "writeDatabase complete\n");
80
81 error_exit:
82     if ( dumpSyncPtr->ut )
83         ubik_EndTrans(dumpSyncPtr->ut);
84     return(code);
85 }
86
87
88 afs_int32 DumpDB(), RestoreDbHeader();
89 afs_int32 BUDB_DumpDB (call, firstcall, maxLength, charListPtr, done)
90      struct rx_call *call;
91      int  firstcall;
92      afs_int32 maxLength;
93      charListT  *charListPtr;
94      afs_int32 *done;
95 {
96   afs_int32 code;
97
98   code = DumpDB (call, firstcall, maxLength, charListPtr, done);
99   osi_auditU (call, BUDB_DmpDBEvent, code, AUD_END);
100   return code;
101 }
102
103 afs_int32 DumpDB (call, firstcall, maxLength, charListPtr, done)
104      struct rx_call *call;
105      int  firstcall;                       /* 1 - init.  0 - no init */
106      afs_int32 maxLength;
107      charListT  *charListPtr;
108      afs_int32 *done;
109 {
110     PROCESS dumperPid, watcherPid;
111     int readSize;
112     afs_int32 code = 0;
113     extern dumpWatcher();
114
115     if ( callPermitted(call) == 0 )
116         ERROR(BUDB_NOTPERMITTED);
117
118     ObtainWriteLock(&dumpSyncPtr->ds_lock);
119
120     /* If asking for zero bytes, then this is a call to reset the timeToLive
121      * timer. Reset it if there is a dump in progress. 
122      */
123     if (maxLength == 0)
124     {
125         charListPtr->charListT_val = (char *)0;
126         charListPtr->charListT_len = 0;
127
128         *done = ((dumpSyncPtr->statusFlags == 0) ? 1 : 0);
129
130         /* reset the clock on dump timeout */
131         dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
132         goto error_exit;
133     }
134
135     if (dumpSyncPtr->statusFlags == 0)
136     {
137         if (!firstcall) ERROR(BUDB_DUMPFAILED);
138
139         LogDebug(5, "Setup dump\n");
140
141         /* no dump in progress - setup and retake lock */
142         bzero(dumpSyncPtr, sizeof(*dumpSyncPtr));
143 /*      ObtainWriteLock(&dumpSyncPtr->ds_lock); */
144
145         /* mark dump in progress */
146         dumpSyncPtr->statusFlags = 1;
147
148         code = pipe(dumpSyncPtr->pipeFid);
149         if (code) ERROR(errno);
150         
151         code = LWP_CreateProcess(setupDbDump, 16384, 1, dumpSyncPtr->pipeFid[1],
152                                  "Database Dumper", &dumperPid);
153         if (code) goto error_exit;
154
155         dumpSyncPtr->dumperPid  = dumperPid;
156         dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
157
158         /* now create the watcher thread */
159         code = LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
160                                  "Database Dump Watchdog", &watcherPid);
161     }
162     else if (firstcall)
163         ERROR(BUDB_LOCKED);
164
165     /* now read the database and feed it to the rpc connection */
166
167     /* wait for data */
168     while ( dumpSyncPtr->ds_bytes == 0 )
169     {
170         /* if no more data */
171         if ( (dumpSyncPtr->ds_writerStatus == DS_DONE      ) ||
172              (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR) )
173         {
174             break;
175         }
176
177         if ( dumpSyncPtr->ds_writerStatus == DS_WAITING )
178         {
179             LogDebug(6, "wakup writer\n");
180             dumpSyncPtr->ds_writerStatus = 0;
181             code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
182             if ( code )
183                 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
184         }
185         LogDebug(6, "wait for writer\n");
186         dumpSyncPtr->ds_readerStatus = DS_WAITING;
187         ReleaseWriteLock(&dumpSyncPtr->ds_lock);
188         LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
189         ObtainWriteLock(&dumpSyncPtr->ds_lock);
190     }
191     
192     charListPtr->charListT_val = (char *) malloc(maxLength);
193     readSize = read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
194
195     /* reset the clock on dump timeout */
196     dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
197     
198     LogDebug(4, "read of len %d returned %d\n", maxLength, readSize);
199
200     charListPtr->charListT_len = readSize;
201
202     if ( readSize == 0 )                                /* last chunk */
203     {
204         *done = 1;
205         close(dumpSyncPtr->pipeFid[0]);
206         dumpSyncPtr->statusFlags = 0;
207     }
208     else
209         *done = 0;
210
211     dumpSyncPtr->ds_bytes -= readSize;
212     if ( dumpSyncPtr->ds_writerStatus == DS_WAITING )
213     {
214         dumpSyncPtr->ds_writerStatus = 0;
215         code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
216         if ( code )
217                 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
218     }
219
220 error_exit:
221     if ( !code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR) ) code = -1;
222     ReleaseWriteLock(&dumpSyncPtr->ds_lock);
223     return(code);
224 }
225
226 afs_int32 BUDB_RestoreDbHeader (call, header)
227      struct rx_call *call;
228      struct DbHeader *header;
229 {
230   afs_int32 code;
231
232   code = RestoreDbHeader (call, header);
233   osi_auditU (call, BUDB_RstDBHEvent, code, AUD_END);
234   return code;
235 }
236
237 afs_int32 RestoreDbHeader (call, header)
238      struct rx_call *call;
239      struct DbHeader *header;
240 {
241     struct ubik_trans *ut = 0;
242     afs_int32 code = 0;
243
244     extern struct memoryDB db;
245
246     if ( callPermitted(call) == 0 )
247         ERROR(BUDB_NOTPERMITTED);
248
249     code = InitRPC(&ut, LOCKWRITE, 1);
250     if ( code )
251         goto error_exit;
252
253     if ( header->dbversion != ntohl(db.h.version) )
254         ERROR(BUDB_VERSIONMISMATCH);
255
256     /* merge rather than replace the header information */
257     if ( db.h.lastDumpId < htonl(header->lastDumpId))
258         db.h.lastDumpId = htonl(header->lastDumpId);
259
260     if ( db.h.lastTapeId < htonl(header->lastTapeId))
261         db.h.lastTapeId = htonl(header->lastTapeId);
262
263     if ( db.h.lastInstanceId < htonl(header->lastInstanceId))
264         db.h.lastInstanceId = htonl(header->lastInstanceId);
265
266     code = dbwrite(ut, 0, (char *) &db.h, sizeof(db.h));
267     if ( code )
268         code = BUDB_IO;
269
270 error_exit:
271     if ( ut )
272         ubik_EndTrans(ut);
273     return(code);
274 }
275
276 /* dumpWatcher
277  *      monitors the state of a database dump. If the dump calls do not 
278  *      reset the time to live value, the dump times out. In that case,
279  *      we kill the database traversal thread and clean up all the other 
280  *      state. Most importantly, the database is unlocked so that other
281  *      transactions can proceed.
282  */
283
284 dumpWatcher()
285 {
286     afs_int32 code;
287
288     while ( 1 )
289     { /*w*/
290
291         /* printf("dumpWatcher\n"); */
292         ObtainWriteLock(&dumpSyncPtr->ds_lock);
293
294         if ( dumpSyncPtr->statusFlags == 0 )
295         {
296             /* dump has finished */
297             goto exit;
298         }
299
300         /* check time to live */
301         if ( time(0) > dumpSyncPtr->timeToLive )
302         { /*i*/
303             /* dump has exceeded the allocated time - terminate it */
304             LogError(0, "Database dump timeout exceeded: %s", ctime(&dumpSyncPtr->timeToLive));
305             LogError(0, "Terminating database dump\n");
306                      
307             close(dumpSyncPtr->pipeFid[0]);
308             close(dumpSyncPtr->pipeFid[1]);
309
310             code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
311             if (code) LogError(code, "dumpWatcher: failed to kill dump thread\n");
312
313             if ( dumpSyncPtr->ut )
314             {
315                 code = ubik_AbortTrans(dumpSyncPtr->ut);
316                 if (code) LogError(code, "Aborting dump transaction\n");
317             }
318
319             bzero(dumpSyncPtr, sizeof(*dumpSyncPtr));
320             goto exit;
321         } /*i*/
322
323         ReleaseWriteLock(&dumpSyncPtr->ds_lock);
324         IOMGR_Sleep(5);
325     } /*w*/
326
327 exit:
328     ReleaseWriteLock(&dumpSyncPtr->ds_lock);
329     /* printf("dumpWatcher exit\n"); */
330     return(0);
331 }