xref: /freebsd/usr.sbin/sa/db.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007 Diomidis Spinellis
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/types.h>
34 #include <sys/acct.h>
35 
36 #include <ctype.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <db.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <limits.h>
45 
46 #include "extern.h"
47 
48 /* Key used to store the version of the database data elements. */
49 static char VERSION_KEY[] = "\0VERSION";
50 
51 /*
52  * Create the in-memory database, *mdb.
53  * If iflag is not set, fill-in mdb with the records of the disk-based
54  * database dbname.
55  * Upgrade old-version records by calling v1_to_v2.
56  * Return 0 if OK, -1 on error.
57  */
58 int
59 db_copy_in(DB **mdb, const char *dbname, const char *uname, BTREEINFO *bti,
60     int (*v1_to_v2)(DBT *key, DBT *data))
61 {
62 	DBT key, data;
63 	DB *ddb;
64 	int error, rv, version;
65 
66 	if ((*mdb = dbopen(NULL, O_RDWR, 0, DB_BTREE, bti)) == NULL)
67 		return (-1);
68 
69 	if (iflag)
70 		return (0);
71 
72 	if ((ddb = dbopen(dbname, O_RDONLY, 0, DB_BTREE, bti)) == NULL) {
73 		if (errno == ENOENT)
74 			return (0);
75 		warn("retrieving %s summary", uname);
76 		db_destroy(*mdb, uname);
77 		return (-1);
78 	}
79 
80 	error = 0;
81 
82 	/* Obtain/set version. */
83 	version = 1;
84 	key.data = (void*)&VERSION_KEY;
85 	key.size = sizeof(VERSION_KEY);
86 
87 	rv = DB_GET(ddb, &key, &data, 0);
88 	if (rv < 0) {
89 		warn("get version key from %s stats", uname);
90 		error = -1;
91 		goto closeout;
92 	} else if (rv == 0) {	/* It's there; verify version. */
93 		if (data.size != sizeof(version)) {
94 			warnx("invalid version size %zd in %s",
95 			    data.size, uname);
96 			error = -1;
97 			goto closeout;
98 		}
99 		memcpy(&version, data.data, data.size);
100 		if (version != 2) {
101 			warnx("unsupported version %d in %s",
102 			    version, uname);
103 			error = -1;
104 			goto closeout;
105 		}
106 	}
107 
108 	for (rv = DB_SEQ(ddb, &key, &data, R_FIRST); rv == 0;
109 	    rv = DB_SEQ(ddb, &key, &data, R_NEXT)) {
110 
111 		/* See if this is a version record. */
112 		if (key.size == sizeof(VERSION_KEY) &&
113 		    memcmp(key.data, VERSION_KEY, sizeof(VERSION_KEY)) == 0)
114 			continue;
115 
116 		/* Convert record from v1, if needed. */
117 		if (version == 1 && v1_to_v2(&key, &data) < 0) {
118 			warn("converting %s stats", uname);
119 			error = -1;
120 			goto closeout;
121 		}
122 
123 		/* Copy record to the in-memory database. */
124 		if ((rv = DB_PUT(*mdb, &key, &data, 0)) < 0) {
125 			warn("initializing %s stats", uname);
126 			error = -1;
127 			goto closeout;
128 		}
129 	}
130 	if (rv < 0) {
131 		warn("retrieving %s summary", uname);
132 		error = -1;
133 	}
134 
135 closeout:
136 	if (DB_CLOSE(ddb) < 0) {
137 		warn("closing %s summary", uname);
138 		error = -1;
139 	}
140 
141 	if (error)
142 		db_destroy(*mdb, uname);
143 	return (error);
144 }
145 
146 /*
147  * Save the in-memory database mdb to the disk database dbname.
148  * Return 0 if OK, -1 on error.
149  */
150 int
151 db_copy_out(DB *mdb, const char *dbname, const char *uname, BTREEINFO *bti)
152 {
153 	DB *ddb;
154 	DBT key, data;
155 	int error, rv, version;
156 
157 	if ((ddb = dbopen(dbname, O_RDWR|O_CREAT|O_TRUNC, 0644,
158 	    DB_BTREE, bti)) == NULL) {
159 		warn("creating %s summary", uname);
160 		return (-1);
161 	}
162 
163 	error = 0;
164 
165 	for (rv = DB_SEQ(mdb, &key, &data, R_FIRST);
166 	    rv == 0; rv = DB_SEQ(mdb, &key, &data, R_NEXT)) {
167 		if ((rv = DB_PUT(ddb, &key, &data, 0)) < 0) {
168 			warn("saving %s summary", uname);
169 			error = -1;
170 			goto out;
171 		}
172 	}
173 	if (rv < 0) {
174 		warn("retrieving %s stats", uname);
175 		error = -1;
176 	}
177 
178 out:
179 	/* Add a version record. */
180 	key.data = (void*)&VERSION_KEY;
181 	key.size = sizeof(VERSION_KEY);
182 	version = 2;
183 	data.data = &version;
184 	data.size = sizeof(version);
185 	if ((rv = DB_PUT(ddb, &key, &data, 0)) < 0) {
186 		warn("add version record to %s stats", uname);
187 		error = -1;
188 	} else if (rv == 1) {
189 		warnx("duplicate version record in %s stats", uname);
190 		error = -1;
191 	}
192 
193 	if (DB_SYNC(ddb, 0) < 0) {
194 		warn("syncing %s summary", uname);
195 		error = -1;
196 	}
197 	if (DB_CLOSE(ddb) < 0) {
198 		warn("closing %s summary", uname);
199 		error = -1;
200 	}
201 	return error;
202 }
203 
204 void
205 db_destroy(DB *db, const char *uname)
206 {
207 	if (DB_CLOSE(db) < 0)
208 		warn("destroying %s stats", uname);
209 }
210