include-afsconfig-before-param-h-20010712
[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 BUDB_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 = (char *)0;
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         bzero(dumpSyncPtr, 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, dumpSyncPtr->pipeFid[1],
156                                  "Database Dumper", &dumperPid);
157         if (code) goto error_exit;
158
159         dumpSyncPtr->dumperPid  = dumperPid;
160         dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
161
162         /* now create the watcher thread */
163         code = LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
164                                  "Database Dump Watchdog", &watcherPid);
165     }
166     else if (firstcall)
167         ERROR(BUDB_LOCKED);
168
169     /* now read the database and feed it to the rpc connection */
170
171     /* wait for data */
172     while ( dumpSyncPtr->ds_bytes == 0 )
173     {
174         /* if no more data */
175         if ( (dumpSyncPtr->ds_writerStatus == DS_DONE      ) ||
176              (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR) )
177         {
178             break;
179         }
180
181         if ( dumpSyncPtr->ds_writerStatus == DS_WAITING )
182         {
183             LogDebug(6, "wakup writer\n");
184             dumpSyncPtr->ds_writerStatus = 0;
185             code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
186             if ( code )
187                 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
188         }
189         LogDebug(6, "wait for writer\n");
190         dumpSyncPtr->ds_readerStatus = DS_WAITING;
191         ReleaseWriteLock(&dumpSyncPtr->ds_lock);
192         LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
193         ObtainWriteLock(&dumpSyncPtr->ds_lock);
194     }
195     
196     charListPtr->charListT_val = (char *) malloc(maxLength);
197     readSize = read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
198
199     /* reset the clock on dump timeout */
200     dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
201     
202     LogDebug(4, "read of len %d returned %d\n", maxLength, readSize);
203
204     charListPtr->charListT_len = readSize;
205
206     if ( readSize == 0 )                                /* last chunk */
207     {
208         *done = 1;
209         close(dumpSyncPtr->pipeFid[0]);
210         dumpSyncPtr->statusFlags = 0;
211     }
212     else
213         *done = 0;
214
215     dumpSyncPtr->ds_bytes -= readSize;
216     if ( dumpSyncPtr->ds_writerStatus == DS_WAITING )
217     {
218         dumpSyncPtr->ds_writerStatus = 0;
219         code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
220         if ( code )
221                 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
222     }
223
224 error_exit:
225     if ( !code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR) ) code = -1;
226     ReleaseWriteLock(&dumpSyncPtr->ds_lock);
227     return(code);
228 }
229
230 afs_int32 BUDB_RestoreDbHeader (call, header)
231      struct rx_call *call;
232      struct DbHeader *header;
233 {
234   afs_int32 code;
235
236   code = RestoreDbHeader (call, header);
237   osi_auditU (call, BUDB_RstDBHEvent, code, AUD_END);
238   return code;
239 }
240
241 afs_int32 RestoreDbHeader (call, header)
242      struct rx_call *call;
243      struct DbHeader *header;
244 {
245     struct ubik_trans *ut = 0;
246     afs_int32 code = 0;
247
248     extern struct memoryDB db;
249
250     if ( callPermitted(call) == 0 )
251         ERROR(BUDB_NOTPERMITTED);
252
253     code = InitRPC(&ut, LOCKWRITE, 1);
254     if ( code )
255         goto error_exit;
256
257     if ( header->dbversion != ntohl(db.h.version) )
258         ERROR(BUDB_VERSIONMISMATCH);
259
260     /* merge rather than replace the header information */
261     if ( db.h.lastDumpId < htonl(header->lastDumpId))
262         db.h.lastDumpId = htonl(header->lastDumpId);
263
264     if ( db.h.lastTapeId < htonl(header->lastTapeId))
265         db.h.lastTapeId = htonl(header->lastTapeId);
266
267     if ( db.h.lastInstanceId < htonl(header->lastInstanceId))
268         db.h.lastInstanceId = htonl(header->lastInstanceId);
269
270     code = dbwrite(ut, 0, (char *) &db.h, sizeof(db.h));
271     if ( code )
272         code = BUDB_IO;
273
274 error_exit:
275     if ( ut )
276         ubik_EndTrans(ut);
277     return(code);
278 }
279
280 /* dumpWatcher
281  *      monitors the state of a database dump. If the dump calls do not 
282  *      reset the time to live value, the dump times out. In that case,
283  *      we kill the database traversal thread and clean up all the other 
284  *      state. Most importantly, the database is unlocked so that other
285  *      transactions can proceed.
286  */
287
288 dumpWatcher()
289 {
290     afs_int32 code;
291
292     while ( 1 )
293     { /*w*/
294
295         /* printf("dumpWatcher\n"); */
296         ObtainWriteLock(&dumpSyncPtr->ds_lock);
297
298         if ( dumpSyncPtr->statusFlags == 0 )
299         {
300             /* dump has finished */
301             goto exit;
302         }
303
304         /* check time to live */
305         if ( time(0) > dumpSyncPtr->timeToLive )
306         { /*i*/
307             /* dump has exceeded the allocated time - terminate it */
308             LogError(0, "Database dump timeout exceeded: %s", ctime(&dumpSyncPtr->timeToLive));
309             LogError(0, "Terminating database dump\n");
310                      
311             close(dumpSyncPtr->pipeFid[0]);
312             close(dumpSyncPtr->pipeFid[1]);
313
314             code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
315             if (code) LogError(code, "dumpWatcher: failed to kill dump thread\n");
316
317             if ( dumpSyncPtr->ut )
318             {
319                 code = ubik_AbortTrans(dumpSyncPtr->ut);
320                 if (code) LogError(code, "Aborting dump transaction\n");
321             }
322
323             bzero(dumpSyncPtr, sizeof(*dumpSyncPtr));
324             goto exit;
325         } /*i*/
326
327         ReleaseWriteLock(&dumpSyncPtr->ds_lock);
328         IOMGR_Sleep(5);
329     } /*w*/
330
331 exit:
332     ReleaseWriteLock(&dumpSyncPtr->ds_lock);
333     /* printf("dumpWatcher exit\n"); */
334     return(0);
335 }