1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * db_log.cc
24*0Sstevel@tonic-gate *
25*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
26*0Sstevel@tonic-gate * Use is subject to license terms.
27*0Sstevel@tonic-gate */
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate #include <stdio.h>
32*0Sstevel@tonic-gate #include <errno.h>
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate #include <malloc.h>
35*0Sstevel@tonic-gate #include <string.h>
36*0Sstevel@tonic-gate #ifdef TDRPC
37*0Sstevel@tonic-gate #include <sysent.h>
38*0Sstevel@tonic-gate #endif
39*0Sstevel@tonic-gate #include <unistd.h>
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #include "db_headers.h"
42*0Sstevel@tonic-gate #include "db_log.h"
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate #include "nisdb_mt.h"
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate static void
delete_log_entry(db_log_entry * lentry)47*0Sstevel@tonic-gate delete_log_entry(db_log_entry *lentry)
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate db_query *q;
50*0Sstevel@tonic-gate entry_object *obj;
51*0Sstevel@tonic-gate if (lentry) {
52*0Sstevel@tonic-gate if ((q = lentry->get_query())) {
53*0Sstevel@tonic-gate delete q;
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate if ((obj = lentry->get_object())) {
56*0Sstevel@tonic-gate free_entry(obj);
57*0Sstevel@tonic-gate }
58*0Sstevel@tonic-gate delete lentry;
59*0Sstevel@tonic-gate }
60*0Sstevel@tonic-gate }
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate * Execute given function 'func' on log.
64*0Sstevel@tonic-gate * function takes as arguments: pointer to log entry, character pointer to
65*0Sstevel@tonic-gate * another argument, and pointer to an integer, which is used as a counter.
66*0Sstevel@tonic-gate * 'func' should increment this value for each successful application.
67*0Sstevel@tonic-gate * The log is traversed until either 'func' returns FALSE, or when the log
68*0Sstevel@tonic-gate * is exhausted. The second argument to 'execute_on_log' is passed as the
69*0Sstevel@tonic-gate * second argument to 'func'. The third argument, 'clean' determines whether
70*0Sstevel@tonic-gate * the log entry is deleted after the function has been applied.
71*0Sstevel@tonic-gate * Returns the number of times that 'func' incremented its third argument.
72*0Sstevel@tonic-gate */
73*0Sstevel@tonic-gate int
execute_on_log(bool_t (* func)(db_log_entry *,char *,int *),char * arg,bool_t clean)74*0Sstevel@tonic-gate db_log::execute_on_log(bool_t (*func) (db_log_entry *, char *, int *),
75*0Sstevel@tonic-gate char* arg, bool_t clean)
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate db_log_entry *j;
78*0Sstevel@tonic-gate int count = 0;
79*0Sstevel@tonic-gate bool_t done = FALSE;
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate WRITELOCK(this, 0, "w db_log::execute_on_log");
82*0Sstevel@tonic-gate if (open() == TRUE) { // open log
83*0Sstevel@tonic-gate while (!done) {
84*0Sstevel@tonic-gate j = get();
85*0Sstevel@tonic-gate if (j == NULL)
86*0Sstevel@tonic-gate break;
87*0Sstevel@tonic-gate if ((*func)(j, arg, &count) == FALSE) done = TRUE;
88*0Sstevel@tonic-gate if (clean) delete_log_entry(j);
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate sync_log();
92*0Sstevel@tonic-gate close();
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate WRITEUNLOCK(this, count, "wu db_log::execute_on_log");
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate return (count);
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate static bool_t
print_log_entry(db_log_entry * j,char *,int * count)100*0Sstevel@tonic-gate print_log_entry(db_log_entry *j, char * /* dummy */, int *count)
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate j->print();
103*0Sstevel@tonic-gate ++ *count;
104*0Sstevel@tonic-gate return (TRUE);
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate /* Print contents of log file to stdout */
108*0Sstevel@tonic-gate int
print()109*0Sstevel@tonic-gate db_log::print()
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate return (execute_on_log(&(print_log_entry), NULL));
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate /* Make copy of current log to log pointed to by 'f'. */
115*0Sstevel@tonic-gate int
copy(db_log * f)116*0Sstevel@tonic-gate db_log::copy(db_log *f)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate db_log_entry *j;
119*0Sstevel@tonic-gate int l, ret = 0;
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate WRITELOCK(f, -1, "w f db_log::copy");
122*0Sstevel@tonic-gate if ((l = acqnonexcl()) != 0) {
123*0Sstevel@tonic-gate WRITEUNLOCK(f, l, "wu f db_log::copy");
124*0Sstevel@tonic-gate return (l);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate for (;;) {
127*0Sstevel@tonic-gate j = get();
128*0Sstevel@tonic-gate if (j == NULL)
129*0Sstevel@tonic-gate break;
130*0Sstevel@tonic-gate if (f->append(j) < 0) {
131*0Sstevel@tonic-gate WARNING_M(
132*0Sstevel@tonic-gate "db_log::copy: could not append to log file: ");
133*0Sstevel@tonic-gate ret = -1;
134*0Sstevel@tonic-gate break;
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate delete_log_entry(j);
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate if ((l = relnonexcl()) != 0) {
139*0Sstevel@tonic-gate ret = l;
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate WRITEUNLOCK(f, ret, "wu f db_log::copy");
142*0Sstevel@tonic-gate return (ret);
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate /* Rewinds current log */
146*0Sstevel@tonic-gate int
rewind()147*0Sstevel@tonic-gate db_log::rewind()
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate return (fseek(file, 0L, 0));
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate /*
153*0Sstevel@tonic-gate * Return the next element in current log; return NULL if end of log or error.
154*0Sstevel@tonic-gate * Log must have been opened for READ.
155*0Sstevel@tonic-gate */
156*0Sstevel@tonic-gate db_log_entry
get()157*0Sstevel@tonic-gate *db_log::get()
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate db_log_entry *j;
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate READLOCK(this, NULL, "r db_log::get");
162*0Sstevel@tonic-gate if (mode != PICKLE_READ) {
163*0Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_log::get");
164*0Sstevel@tonic-gate return (NULL);
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate j = new db_log_entry;
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate if (j == NULL) {
170*0Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_log::get");
171*0Sstevel@tonic-gate return (NULL);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate if (xdr_db_log_entry(&(xdr), j) == FALSE) {
174*0Sstevel@tonic-gate delete_log_entry (j);
175*0Sstevel@tonic-gate /* WARNING("Could not sucessfully finish reading log"); */
176*0Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_log::get");
177*0Sstevel@tonic-gate return (NULL);
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate if (! j->sane()) {
180*0Sstevel@tonic-gate WARNING("truncated log entry found");
181*0Sstevel@tonic-gate delete_log_entry(j);
182*0Sstevel@tonic-gate j = NULL;
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate READUNLOCK(this, j, "ru db_log::get");
185*0Sstevel@tonic-gate return (j);
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate /* Append given log entry to log. */
189*0Sstevel@tonic-gate int
append(db_log_entry * j)190*0Sstevel@tonic-gate db_log::append(db_log_entry *j)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate int status;
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate WRITELOCK(this, -1, "w db_log::append");
195*0Sstevel@tonic-gate if (mode != PICKLE_APPEND) {
196*0Sstevel@tonic-gate WRITEUNLOCK(this, -1, "wu db_log::append");
197*0Sstevel@tonic-gate return (-1);
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate /* xdr returns TRUE if successful, FALSE otherwise */
201*0Sstevel@tonic-gate status = ((xdr_db_log_entry(&(xdr), j)) ? 0 : -1);
202*0Sstevel@tonic-gate if (status < 0) {
203*0Sstevel@tonic-gate WARNING("db_log: could not write log entry");
204*0Sstevel@tonic-gate } else {
205*0Sstevel@tonic-gate syncstate++;
206*0Sstevel@tonic-gate }
207*0Sstevel@tonic-gate WRITEUNLOCK(this, status, "wu db_log::append");
208*0Sstevel@tonic-gate return (status);
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gate int
copy_log_file(char * oldname,char * newname)212*0Sstevel@tonic-gate copy_log_file(char *oldname, char *newname) {
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gate int from, to, ret = 0;
215*0Sstevel@tonic-gate ssize_t size, w, b;
216*0Sstevel@tonic-gate char buf[8192];
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate if ((from = open(oldname, O_RDONLY, 0666)) < 0) {
219*0Sstevel@tonic-gate if (errno == ENOENT) {
220*0Sstevel@tonic-gate return (0);
221*0Sstevel@tonic-gate } else {
222*0Sstevel@tonic-gate return (errno);
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate if ((to = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0) {
226*0Sstevel@tonic-gate ret = errno;
227*0Sstevel@tonic-gate (void) close(from);
228*0Sstevel@tonic-gate return (ret);
229*0Sstevel@tonic-gate }
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gate while ((size = read(from, buf, sizeof (buf))) > 0) {
232*0Sstevel@tonic-gate b = 0;
233*0Sstevel@tonic-gate while (size > 0) {
234*0Sstevel@tonic-gate w = write(to, &buf[b], size);
235*0Sstevel@tonic-gate if (w < 0) {
236*0Sstevel@tonic-gate size == -1;
237*0Sstevel@tonic-gate break;
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate size -= w;
240*0Sstevel@tonic-gate b += w;
241*0Sstevel@tonic-gate }
242*0Sstevel@tonic-gate if (size != 0) {
243*0Sstevel@tonic-gate ret = errno;
244*0Sstevel@tonic-gate break;
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate }
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate (void) close(from);
249*0Sstevel@tonic-gate
250*0Sstevel@tonic-gate if (ret != 0) {
251*0Sstevel@tonic-gate errno = ret;
252*0Sstevel@tonic-gate WARNING_M("db_log: error copying log file")
253*0Sstevel@tonic-gate (void) close(to);
254*0Sstevel@tonic-gate return (ret);
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gate if (fsync(to) != 0) {
258*0Sstevel@tonic-gate ret = errno;
259*0Sstevel@tonic-gate WARNING_M("db_log: error syncing log file");
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate (void) close(to);
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate return (ret);
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gate }
267*0Sstevel@tonic-gate
268*0Sstevel@tonic-gate /*
269*0Sstevel@tonic-gate * Return value is expected to be the usual C convention of non-zero
270*0Sstevel@tonic-gate * for success, 0 for failure.
271*0Sstevel@tonic-gate */
272*0Sstevel@tonic-gate int
sync_log()273*0Sstevel@tonic-gate db_log::sync_log()
274*0Sstevel@tonic-gate {
275*0Sstevel@tonic-gate int status, err;
276*0Sstevel@tonic-gate
277*0Sstevel@tonic-gate WRITELOCK(this, -1, "w db_log::sync_log");
278*0Sstevel@tonic-gate status = fflush(file);
279*0Sstevel@tonic-gate if (status < 0) {
280*0Sstevel@tonic-gate WARNING("db_log: could not flush log entry to disk");
281*0Sstevel@tonic-gate WRITEUNLOCK(this, status, "wu db_log::sync_log");
282*0Sstevel@tonic-gate return (status);
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate status = fsync(fileno(file));
286*0Sstevel@tonic-gate if (status < 0) {
287*0Sstevel@tonic-gate WARNING("db_log: could not sync log entry to disk");
288*0Sstevel@tonic-gate } else if (tmplog != 0) {
289*0Sstevel@tonic-gate if (syncstate == 0) {
290*0Sstevel@tonic-gate /* Log already stable; nothing to do */
291*0Sstevel@tonic-gate err = 0;
292*0Sstevel@tonic-gate } else if ((err = copy_log_file(tmplog, stablelog)) == 0) {
293*0Sstevel@tonic-gate if (rename(stablelog, oldlog) != 0) {
294*0Sstevel@tonic-gate WARNING_M("db_log: could not mv stable log");
295*0Sstevel@tonic-gate } else {
296*0Sstevel@tonic-gate syncstate = 0;
297*0Sstevel@tonic-gate }
298*0Sstevel@tonic-gate } else {
299*0Sstevel@tonic-gate errno = err;
300*0Sstevel@tonic-gate WARNING_M("db_log: could not stabilize log");
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate status = (err == 0);
303*0Sstevel@tonic-gate } else {
304*0Sstevel@tonic-gate /*
305*0Sstevel@tonic-gate * Successful sync of file, but no tmplog to sync
306*0Sstevel@tonic-gate * so we make sure we return 'success'.
307*0Sstevel@tonic-gate */
308*0Sstevel@tonic-gate status = 1;
309*0Sstevel@tonic-gate }
310*0Sstevel@tonic-gate WRITEUNLOCK(this, status, "wu db_log::sync_log");
311*0Sstevel@tonic-gate return (status);
312*0Sstevel@tonic-gate }
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate int
close()315*0Sstevel@tonic-gate db_log::close() {
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gate int ret;
318*0Sstevel@tonic-gate
319*0Sstevel@tonic-gate WRITELOCK(this, -1, "w db_log::close");
320*0Sstevel@tonic-gate if (mode != PICKLE_READ && oldlog != 0) {
321*0Sstevel@tonic-gate if (syncstate != 0) {
322*0Sstevel@tonic-gate WARNING("db_log: closing unstable tmp log");
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate filename = oldlog;
325*0Sstevel@tonic-gate oldlog = 0;
326*0Sstevel@tonic-gate }
327*0Sstevel@tonic-gate
328*0Sstevel@tonic-gate ret = pickle_file::close();
329*0Sstevel@tonic-gate if (tmplog != 0) {
330*0Sstevel@tonic-gate (void) unlink(tmplog);
331*0Sstevel@tonic-gate delete tmplog;
332*0Sstevel@tonic-gate tmplog = 0;
333*0Sstevel@tonic-gate }
334*0Sstevel@tonic-gate if (stablelog != 0) {
335*0Sstevel@tonic-gate delete stablelog;
336*0Sstevel@tonic-gate stablelog = 0;
337*0Sstevel@tonic-gate }
338*0Sstevel@tonic-gate WRITEUNLOCK(this, ret, "wu db_log::close");
339*0Sstevel@tonic-gate return (ret);
340*0Sstevel@tonic-gate }
341*0Sstevel@tonic-gate
342*0Sstevel@tonic-gate bool_t
open(void)343*0Sstevel@tonic-gate db_log::open(void) {
344*0Sstevel@tonic-gate
345*0Sstevel@tonic-gate int len, cpstat;
346*0Sstevel@tonic-gate bool_t ret;
347*0Sstevel@tonic-gate
348*0Sstevel@tonic-gate WRITELOCK(this, FALSE, "w db_log::open");
349*0Sstevel@tonic-gate if (mode == PICKLE_READ || (!copylog)) {
350*0Sstevel@tonic-gate ret = pickle_file::open();
351*0Sstevel@tonic-gate WRITEUNLOCK(this, ret, "wu db_log::open");
352*0Sstevel@tonic-gate return (ret);
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate
355*0Sstevel@tonic-gate len = strlen(filename);
356*0Sstevel@tonic-gate tmplog = new char[len + sizeof (".tmp")];
357*0Sstevel@tonic-gate if (tmplog == 0) {
358*0Sstevel@tonic-gate WARNING("db_log: could not allocate tmp log name");
359*0Sstevel@tonic-gate ret = pickle_file::open();
360*0Sstevel@tonic-gate WRITEUNLOCK(this, ret, "wu db_log::open");
361*0Sstevel@tonic-gate return (ret);
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate stablelog = new char[len + sizeof (".stable")];
364*0Sstevel@tonic-gate if (stablelog == 0) {
365*0Sstevel@tonic-gate WARNING("db_log: could not allocate stable log name");
366*0Sstevel@tonic-gate delete tmplog;
367*0Sstevel@tonic-gate tmplog = 0;
368*0Sstevel@tonic-gate ret = pickle_file::open();
369*0Sstevel@tonic-gate WRITEUNLOCK(this, ret, "wu db_log::open");
370*0Sstevel@tonic-gate return (ret);
371*0Sstevel@tonic-gate }
372*0Sstevel@tonic-gate sprintf(tmplog, "%s.tmp", filename);
373*0Sstevel@tonic-gate sprintf(stablelog, "%s.stable", filename);
374*0Sstevel@tonic-gate
375*0Sstevel@tonic-gate if ((cpstat = copy_log_file(filename, tmplog)) == 0) {
376*0Sstevel@tonic-gate oldlog = filename;
377*0Sstevel@tonic-gate filename = tmplog;
378*0Sstevel@tonic-gate } else {
379*0Sstevel@tonic-gate syslog(LOG_WARNING,
380*0Sstevel@tonic-gate "db_log: Error copying \"%s\" to \"%s\": %s",
381*0Sstevel@tonic-gate filename, tmplog, strerror(cpstat));
382*0Sstevel@tonic-gate delete tmplog;
383*0Sstevel@tonic-gate tmplog = 0;
384*0Sstevel@tonic-gate delete stablelog;
385*0Sstevel@tonic-gate stablelog = 0;
386*0Sstevel@tonic-gate }
387*0Sstevel@tonic-gate
388*0Sstevel@tonic-gate ret = pickle_file::open();
389*0Sstevel@tonic-gate WRITEUNLOCK(this, ret, "wu db_log::open");
390*0Sstevel@tonic-gate return (ret);
391*0Sstevel@tonic-gate }
392