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>
11 #include <afs/param.h>
21 #include <netinet/in.h>
23 #include <sys/resource.h>
27 #include <sys/types.h>
38 #include <afs/cellconfig.h>
41 #include "budb_errs.h"
43 #include "error_macros.h"
45 #include "afs/audit.h"
47 void *dumpWatcher(void *);
49 /* dump ubik database - interface routines */
52 * no checking for now.
64 * decode the arguments passed via LWP and dump the database.
68 setupDbDump(void *param)
70 int writeFid = (int)param;
73 code = InitRPC(&dumpSyncPtr->ut, LOCKREAD, 1);
77 code = writeDatabase(dumpSyncPtr->ut, writeFid);
79 LogError(code, "writeDatabase failed\n");
81 code = close(writeFid);
83 LogError(code, "pipe writer close failed\n");
85 LogDebug(5, "writeDatabase complete\n");
89 ubik_EndTrans(dumpSyncPtr->ut);
90 return (void *)(code);
94 afs_int32 DumpDB(), RestoreDbHeader();
96 SBUDB_DumpDB(call, firstcall, maxLength, charListPtr, done)
100 charListT *charListPtr;
105 code = DumpDB(call, firstcall, maxLength, charListPtr, done);
106 osi_auditU(call, BUDB_DmpDBEvent, code, AUD_END);
111 DumpDB(call, firstcall, maxLength, charListPtr, done)
112 struct rx_call *call;
113 int firstcall; /* 1 - init. 0 - no init */
115 charListT *charListPtr;
118 #ifdef AFS_PTHREAD_ENV
119 pthread_t dumperPid, watcherPid;
120 pthread_attr_t dumperPid_tattr;
121 pthread_attr_t watcherPid_tattr;
123 PROCESS dumperPid, watcherPid;
128 if (callPermitted(call) == 0)
129 ERROR(BUDB_NOTPERMITTED);
131 ObtainWriteLock(&dumpSyncPtr->ds_lock);
133 /* If asking for zero bytes, then this is a call to reset the timeToLive
134 * timer. Reset it if there is a dump in progress.
136 if (maxLength == 0) {
137 charListPtr->charListT_val = NULL;
138 charListPtr->charListT_len = 0;
140 *done = ((dumpSyncPtr->statusFlags == 0) ? 1 : 0);
142 /* reset the clock on dump timeout */
143 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
147 if (dumpSyncPtr->statusFlags == 0) {
149 ERROR(BUDB_DUMPFAILED);
151 LogDebug(5, "Setup dump\n");
153 /* no dump in progress - setup and retake lock */
154 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
155 /* ObtainWriteLock(&dumpSyncPtr->ds_lock); */
157 /* mark dump in progress */
158 dumpSyncPtr->statusFlags = 1;
160 code = pipe(dumpSyncPtr->pipeFid);
164 #ifdef AFS_PTHREAD_ENV
165 /* Initialize the condition variables and the mutexes we use
166 * to signal and synchronize the reader and writer threads.
168 assert(pthread_cond_init(&dumpSyncPtr->ds_readerStatus_cond, (const pthread_condattr_t *)0) == 0);
169 assert(pthread_cond_init(&dumpSyncPtr->ds_writerStatus_cond, (const pthread_condattr_t *)0) == 0);
170 assert(pthread_mutex_init(&dumpSyncPtr->ds_readerStatus_mutex, (const pthread_mutexattr_t *)0) == 0);
171 assert(pthread_mutex_init(&dumpSyncPtr->ds_writerStatus_mutex, (const pthread_mutexattr_t *)0) == 0);
173 /* Initialize the thread attributes and launch the thread */
175 assert(pthread_attr_init(&dumperPid_tattr) == 0);
176 assert(pthread_attr_setdetachstate(&dumperPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
177 assert(pthread_create(&dumperPid, &dumperPid_tattr, (void *)setupDbDump, NULL) == 0);
181 LWP_CreateProcess(setupDbDump, 16384, 1,
182 (void *)dumpSyncPtr->pipeFid[1],
183 "Database Dumper", &dumperPid);
188 dumpSyncPtr->dumperPid = dumperPid;
189 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
191 #ifdef AFS_PTHREAD_ENV
192 /* Initialize the thread attributes and launch the thread */
194 assert(pthread_attr_init(&watcherPid_tattr) == 0);
195 assert(pthread_attr_setdetachstate(&watcherPid_tattr, PTHREAD_CREATE_DETACHED) == 0);
196 assert(pthread_create(&watcherPid, &watcherPid_tattr, (void *)dumpWatcher, NULL) == 0);
198 /* now create the watcher thread */
200 LWP_CreateProcess(dumpWatcher, 16384, 1, 0,
201 "Database Dump Watchdog", &watcherPid);
203 } else if (firstcall)
206 /* now read the database and feed it to the rpc connection */
209 while (dumpSyncPtr->ds_bytes == 0) {
210 /* if no more data */
211 if ((dumpSyncPtr->ds_writerStatus == DS_DONE)
212 || (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR)) {
216 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
217 LogDebug(6, "wakup writer\n");
218 dumpSyncPtr->ds_writerStatus = 0;
219 #ifdef AFS_PTHREAD_ENV
220 assert(pthread_cond_broadcast(&dumpSyncPtr->ds_writerStatus_cond) == 0);
222 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
224 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
227 LogDebug(6, "wait for writer\n");
228 dumpSyncPtr->ds_readerStatus = DS_WAITING;
229 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
230 #ifdef AFS_PTHREAD_ENV
231 assert(pthread_mutex_lock(&dumpSyncPtr->ds_readerStatus_mutex) == 0);
232 assert(pthread_cond_wait(&dumpSyncPtr->ds_readerStatus_cond, &dumpSyncPtr->ds_readerStatus_mutex) == 0);
233 assert(pthread_mutex_unlock(&dumpSyncPtr->ds_readerStatus_mutex) == 0);
235 LWP_WaitProcess(&dumpSyncPtr->ds_readerStatus);
237 ObtainWriteLock(&dumpSyncPtr->ds_lock);
240 charListPtr->charListT_val = (char *)malloc(maxLength);
242 read(dumpSyncPtr->pipeFid[0], charListPtr->charListT_val, maxLength);
244 /* reset the clock on dump timeout */
245 dumpSyncPtr->timeToLive = time(0) + DUMP_TTL_INC;
247 LogDebug(4, "read of len %d returned %d\n", maxLength, readSize);
249 charListPtr->charListT_len = readSize;
251 if (readSize == 0) { /* last chunk */
253 close(dumpSyncPtr->pipeFid[0]);
254 dumpSyncPtr->statusFlags = 0;
258 dumpSyncPtr->ds_bytes -= readSize;
259 if (dumpSyncPtr->ds_writerStatus == DS_WAITING) {
260 dumpSyncPtr->ds_writerStatus = 0;
261 #ifdef AFS_PTHREAD_ENV
262 assert(pthread_cond_broadcast(&dumpSyncPtr->ds_writerStatus_cond) == 0);
264 code = LWP_SignalProcess(&dumpSyncPtr->ds_writerStatus);
266 LogError(code, "BUDB_DumpDB: signal delivery failed\n");
271 if (!code && (dumpSyncPtr->ds_writerStatus == DS_DONE_ERROR))
273 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
278 SBUDB_RestoreDbHeader(call, header)
279 struct rx_call *call;
280 struct DbHeader *header;
284 code = RestoreDbHeader(call, header);
285 osi_auditU(call, BUDB_RstDBHEvent, code, AUD_END);
290 RestoreDbHeader(call, header)
291 struct rx_call *call;
292 struct DbHeader *header;
294 struct ubik_trans *ut = 0;
297 extern struct memoryDB db;
299 if (callPermitted(call) == 0)
300 ERROR(BUDB_NOTPERMITTED);
302 code = InitRPC(&ut, LOCKWRITE, 1);
306 if (header->dbversion != ntohl(db.h.version))
307 ERROR(BUDB_VERSIONMISMATCH);
309 /* merge rather than replace the header information */
310 if (db.h.lastDumpId < htonl(header->lastDumpId))
311 db.h.lastDumpId = htonl(header->lastDumpId);
313 if (db.h.lastTapeId < htonl(header->lastTapeId))
314 db.h.lastTapeId = htonl(header->lastTapeId);
316 if (db.h.lastInstanceId < htonl(header->lastInstanceId))
317 db.h.lastInstanceId = htonl(header->lastInstanceId);
319 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
330 * monitors the state of a database dump. If the dump calls do not
331 * reset the time to live value, the dump times out. In that case,
332 * we kill the database traversal thread and clean up all the other
333 * state. Most importantly, the database is unlocked so that other
334 * transactions can proceed.
338 dumpWatcher(void *unused)
344 /* printf("dumpWatcher\n"); */
345 ObtainWriteLock(&dumpSyncPtr->ds_lock);
347 if (dumpSyncPtr->statusFlags == 0) {
348 /* dump has finished */
352 /* check time to live */
353 if (time(0) > dumpSyncPtr->timeToLive) { /*i */
354 /* dump has exceeded the allocated time - terminate it */
355 LogError(0, "Database dump timeout exceeded: %s",
356 ctime(&dumpSyncPtr->timeToLive));
357 LogError(0, "Terminating database dump\n");
359 close(dumpSyncPtr->pipeFid[0]);
360 close(dumpSyncPtr->pipeFid[1]);
361 #ifdef AFS_PTHREAD_ENV
362 assert(pthread_cancel(dumpSyncPtr->dumperPid) == 0);
364 code = LWP_DestroyProcess(dumpSyncPtr->dumperPid);
366 LogError(code, "dumpWatcher: failed to kill dump thread\n");
369 if (dumpSyncPtr->ut) {
370 code = ubik_AbortTrans(dumpSyncPtr->ut);
372 LogError(code, "Aborting dump transaction\n");
375 memset(dumpSyncPtr, 0, sizeof(*dumpSyncPtr));
379 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
380 #ifdef AFS_PTHREAD_ENV
388 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
389 /* printf("dumpWatcher exit\n"); */