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