xref: /titanic_44/usr/src/cmd/svc/configd/backend.c (revision 6e1d2b428379e5270ccda99d403eb225e8699c3e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c5c4113dSnw141292  * Common Development and Distribution License (the "License").
6c5c4113dSnw141292  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215b7f77adStw21770 
227c478bd9Sstevel@tonic-gate /*
235b7f77adStw21770  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
298918dff3Sjwadams /*
308918dff3Sjwadams  * sqlite is not compatible with _FILE_OFFSET_BITS=64, but we need to
318918dff3Sjwadams  * be able to statvfs(2) possibly large systems.  This define gives us
328918dff3Sjwadams  * access to the transitional interfaces.  See lfcompile64(5) for how
338918dff3Sjwadams  * _LARGEFILE64_SOURCE works.
348918dff3Sjwadams  */
358918dff3Sjwadams #define	_LARGEFILE64_SOURCE
368918dff3Sjwadams 
377c478bd9Sstevel@tonic-gate #include <assert.h>
387c478bd9Sstevel@tonic-gate #include <door.h>
397c478bd9Sstevel@tonic-gate #include <dirent.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <limits.h>
437c478bd9Sstevel@tonic-gate #include <pthread.h>
447c478bd9Sstevel@tonic-gate #include <stdarg.h>
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <string.h>
488918dff3Sjwadams #include <sys/stat.h>
498918dff3Sjwadams #include <sys/statvfs.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <zone.h>
52c0889d7aSstevep #include <libscf_priv.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include "configd.h"
557c478bd9Sstevel@tonic-gate #include "repcache_protocol.h"
567c478bd9Sstevel@tonic-gate 
57c5c4113dSnw141292 #include <sqlite.h>
58c5c4113dSnw141292 #include <sqlite-misc.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * This file has two purposes:
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * 1. It contains the database schema, and the code for setting up our backend
647c478bd9Sstevel@tonic-gate  *    databases, including installing said schema.
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  * 2. It provides a simplified interface to the SQL database library, and
677c478bd9Sstevel@tonic-gate  *    synchronizes MT access to the database.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate typedef struct backend_spent {
717c478bd9Sstevel@tonic-gate 	uint64_t bs_count;
727c478bd9Sstevel@tonic-gate 	hrtime_t bs_time;
737c478bd9Sstevel@tonic-gate 	hrtime_t bs_vtime;
747c478bd9Sstevel@tonic-gate } backend_spent_t;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate typedef struct backend_totals {
777c478bd9Sstevel@tonic-gate 	backend_spent_t	bt_lock;	/* waiting for lock */
787c478bd9Sstevel@tonic-gate 	backend_spent_t	bt_exec;	/* time spent executing SQL */
797c478bd9Sstevel@tonic-gate } backend_totals_t;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate typedef struct sqlite_backend {
827c478bd9Sstevel@tonic-gate 	pthread_mutex_t	be_lock;
837c478bd9Sstevel@tonic-gate 	pthread_t	be_thread;	/* thread holding lock */
847c478bd9Sstevel@tonic-gate 	struct sqlite	*be_db;
857c478bd9Sstevel@tonic-gate 	const char	*be_path;	/* path to db */
868918dff3Sjwadams 	int		be_readonly;	/* readonly at start, and still is */
877c478bd9Sstevel@tonic-gate 	int		be_writing;	/* held for writing */
887c478bd9Sstevel@tonic-gate 	backend_type_t	be_type;	/* type of db */
898918dff3Sjwadams 	hrtime_t	be_lastcheck;	/* time of last read-only check */
907c478bd9Sstevel@tonic-gate 	backend_totals_t be_totals[2];	/* one for reading, one for writing */
917c478bd9Sstevel@tonic-gate } sqlite_backend_t;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate struct backend_tx {
947c478bd9Sstevel@tonic-gate 	sqlite_backend_t	*bt_be;
957c478bd9Sstevel@tonic-gate 	int			bt_readonly;
967c478bd9Sstevel@tonic-gate 	int			bt_type;
977c478bd9Sstevel@tonic-gate 	int			bt_full;	/* SQLITE_FULL during tx */
987c478bd9Sstevel@tonic-gate };
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #define	UPDATE_TOTALS_WR(sb, writing, field, ts, vts) { \
1017c478bd9Sstevel@tonic-gate 	backend_spent_t *__bsp = &(sb)->be_totals[!!(writing)].field; \
1027c478bd9Sstevel@tonic-gate 	__bsp->bs_count++;						\
1037c478bd9Sstevel@tonic-gate 	__bsp->bs_time += (gethrtime() - ts);				\
1047c478bd9Sstevel@tonic-gate 	__bsp->bs_vtime += (gethrvtime() - vts);			\
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	UPDATE_TOTALS(sb, field, ts, vts) \
1087c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS_WR(sb, (sb)->be_writing, field, ts, vts)
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate struct backend_query {
1117c478bd9Sstevel@tonic-gate 	char	*bq_buf;
1127c478bd9Sstevel@tonic-gate 	size_t	bq_size;
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate struct backend_tbl_info {
1167c478bd9Sstevel@tonic-gate 	const char *bti_name;
1177c478bd9Sstevel@tonic-gate 	const char *bti_cols;
1187c478bd9Sstevel@tonic-gate };
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate struct backend_idx_info {
1217c478bd9Sstevel@tonic-gate 	const char *bxi_tbl;
1227c478bd9Sstevel@tonic-gate 	const char *bxi_idx;
1237c478bd9Sstevel@tonic-gate 	const char *bxi_cols;
1247c478bd9Sstevel@tonic-gate };
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static pthread_mutex_t backend_panic_lock = PTHREAD_MUTEX_INITIALIZER;
1277c478bd9Sstevel@tonic-gate static pthread_cond_t backend_panic_cv = PTHREAD_COND_INITIALIZER;
1287c478bd9Sstevel@tonic-gate pthread_t backend_panic_thread = 0;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate int backend_do_trace = 0;		/* invoke tracing callback */
1317c478bd9Sstevel@tonic-gate int backend_print_trace = 0;		/* tracing callback prints SQL */
1327c478bd9Sstevel@tonic-gate int backend_panic_abort = 0;		/* abort when panicking */
1337c478bd9Sstevel@tonic-gate 
1348918dff3Sjwadams /* interval between read-only checks while starting up */
1358918dff3Sjwadams #define	BACKEND_READONLY_CHECK_INTERVAL	(2 * (hrtime_t)NANOSEC)
1368918dff3Sjwadams 
1377c478bd9Sstevel@tonic-gate /*
138*6e1d2b42Samaguire  * Any incompatible change to the below schema should bump the version number.
139*6e1d2b42Samaguire  * The schema has been changed to support value ordering,  but this change
140*6e1d2b42Samaguire  * is backwards-compatible - i.e. a previous svc.configd can use a
141*6e1d2b42Samaguire  * repository database with the new schema perfectly well.  As a result,
142*6e1d2b42Samaguire  * the schema version has not been updated,  allowing downgrade of systems
143*6e1d2b42Samaguire  * without losing repository data.
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate #define	BACKEND_SCHEMA_VERSION		5
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static struct backend_tbl_info tbls_normal[] = { /* BACKEND_TYPE_NORMAL */
1487c478bd9Sstevel@tonic-gate 	/*
1497c478bd9Sstevel@tonic-gate 	 * service_tbl holds all services.  svc_id is the identifier of the
1507c478bd9Sstevel@tonic-gate 	 * service.
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	{
1537c478bd9Sstevel@tonic-gate 		"service_tbl",
1547c478bd9Sstevel@tonic-gate 		"svc_id          INTEGER PRIMARY KEY,"
1557c478bd9Sstevel@tonic-gate 		"svc_name        CHAR(256) NOT NULL"
1567c478bd9Sstevel@tonic-gate 	},
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * instance_tbl holds all of the instances.  The parent service id
1607c478bd9Sstevel@tonic-gate 	 * is instance_svc.
1617c478bd9Sstevel@tonic-gate 	 */
1627c478bd9Sstevel@tonic-gate 	{
1637c478bd9Sstevel@tonic-gate 		"instance_tbl",
1647c478bd9Sstevel@tonic-gate 		"instance_id     INTEGER PRIMARY KEY,"
1657c478bd9Sstevel@tonic-gate 		"instance_name   CHAR(256) NOT NULL,"
1667c478bd9Sstevel@tonic-gate 		"instance_svc    INTEGER NOT NULL"
1677c478bd9Sstevel@tonic-gate 	},
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * snapshot_lnk_tbl links (instance, snapshot name) with snapshots.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	{
1737c478bd9Sstevel@tonic-gate 		"snapshot_lnk_tbl",
1747c478bd9Sstevel@tonic-gate 		"lnk_id          INTEGER PRIMARY KEY,"
1757c478bd9Sstevel@tonic-gate 		"lnk_inst_id     INTEGER NOT NULL,"
1767c478bd9Sstevel@tonic-gate 		"lnk_snap_name   CHAR(256) NOT NULL,"
1777c478bd9Sstevel@tonic-gate 		"lnk_snap_id     INTEGER NOT NULL"
1787c478bd9Sstevel@tonic-gate 	},
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 * snaplevel_tbl maps a snapshot id to a set of named, ordered
1827c478bd9Sstevel@tonic-gate 	 * snaplevels.
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	{
1857c478bd9Sstevel@tonic-gate 		"snaplevel_tbl",
1867c478bd9Sstevel@tonic-gate 		"snap_id                 INTEGER NOT NULL,"
1877c478bd9Sstevel@tonic-gate 		"snap_level_num          INTEGER NOT NULL,"
1887c478bd9Sstevel@tonic-gate 		"snap_level_id           INTEGER NOT NULL,"
1897c478bd9Sstevel@tonic-gate 		"snap_level_service_id   INTEGER NOT NULL,"
1907c478bd9Sstevel@tonic-gate 		"snap_level_service      CHAR(256) NOT NULL,"
1917c478bd9Sstevel@tonic-gate 		"snap_level_instance_id  INTEGER NULL,"
1927c478bd9Sstevel@tonic-gate 		"snap_level_instance     CHAR(256) NULL"
1937c478bd9Sstevel@tonic-gate 	},
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/*
1967c478bd9Sstevel@tonic-gate 	 * snaplevel_lnk_tbl links snaplevels to property groups.
1977c478bd9Sstevel@tonic-gate 	 * snaplvl_pg_* is identical to the original property group,
1987c478bd9Sstevel@tonic-gate 	 * and snaplvl_gen_id overrides the generation number.
1997c478bd9Sstevel@tonic-gate 	 * The service/instance ids are as in the snaplevel.
2007c478bd9Sstevel@tonic-gate 	 */
2017c478bd9Sstevel@tonic-gate 	{
2027c478bd9Sstevel@tonic-gate 		"snaplevel_lnk_tbl",
2037c478bd9Sstevel@tonic-gate 		"snaplvl_level_id INTEGER NOT NULL,"
2047c478bd9Sstevel@tonic-gate 		"snaplvl_pg_id    INTEGER NOT NULL,"
2057c478bd9Sstevel@tonic-gate 		"snaplvl_pg_name  CHAR(256) NOT NULL,"
2067c478bd9Sstevel@tonic-gate 		"snaplvl_pg_type  CHAR(256) NOT NULL,"
2077c478bd9Sstevel@tonic-gate 		"snaplvl_pg_flags INTEGER NOT NULL,"
2087c478bd9Sstevel@tonic-gate 		"snaplvl_gen_id   INTEGER NOT NULL"
2097c478bd9Sstevel@tonic-gate 	},
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	{ NULL, NULL }
2127c478bd9Sstevel@tonic-gate };
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static struct backend_idx_info idxs_normal[] = { /* BACKEND_TYPE_NORMAL */
2157c478bd9Sstevel@tonic-gate 	{ "service_tbl",	"name",	"svc_name" },
2167c478bd9Sstevel@tonic-gate 	{ "instance_tbl",	"name",	"instance_svc, instance_name" },
2177c478bd9Sstevel@tonic-gate 	{ "snapshot_lnk_tbl",	"name",	"lnk_inst_id, lnk_snap_name" },
2187c478bd9Sstevel@tonic-gate 	{ "snapshot_lnk_tbl",	"snapid", "lnk_snap_id" },
2197c478bd9Sstevel@tonic-gate 	{ "snaplevel_tbl",	"id",	"snap_id" },
2207c478bd9Sstevel@tonic-gate 	{ "snaplevel_lnk_tbl",	"id",	"snaplvl_pg_id" },
2217c478bd9Sstevel@tonic-gate 	{ "snaplevel_lnk_tbl",	"level", "snaplvl_level_id" },
2227c478bd9Sstevel@tonic-gate 	{ NULL, NULL, NULL }
2237c478bd9Sstevel@tonic-gate };
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate static struct backend_tbl_info tbls_np[] = { /* BACKEND_TYPE_NONPERSIST */
2267c478bd9Sstevel@tonic-gate 	{ NULL, NULL }
2277c478bd9Sstevel@tonic-gate };
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static struct backend_idx_info idxs_np[] = {	/* BACKEND_TYPE_NONPERSIST */
2307c478bd9Sstevel@tonic-gate 	{ NULL, NULL, NULL }
2317c478bd9Sstevel@tonic-gate };
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static struct backend_tbl_info tbls_common[] = { /* all backend types */
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * pg_tbl defines property groups.  They are associated with a single
2367c478bd9Sstevel@tonic-gate 	 * service or instance.  The pg_gen_id links them with the latest
2377c478bd9Sstevel@tonic-gate 	 * "edited" version of its properties.
2387c478bd9Sstevel@tonic-gate 	 */
2397c478bd9Sstevel@tonic-gate 	{
2407c478bd9Sstevel@tonic-gate 		"pg_tbl",
2417c478bd9Sstevel@tonic-gate 		"pg_id           INTEGER PRIMARY KEY,"
2427c478bd9Sstevel@tonic-gate 		"pg_parent_id    INTEGER NOT NULL,"
2437c478bd9Sstevel@tonic-gate 		"pg_name         CHAR(256) NOT NULL,"
2447c478bd9Sstevel@tonic-gate 		"pg_type         CHAR(256) NOT NULL,"
2457c478bd9Sstevel@tonic-gate 		"pg_flags        INTEGER NOT NULL,"
2467c478bd9Sstevel@tonic-gate 		"pg_gen_id       INTEGER NOT NULL"
2477c478bd9Sstevel@tonic-gate 	},
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * prop_lnk_tbl links a particular pg_id and gen_id to a set of
2517c478bd9Sstevel@tonic-gate 	 * (prop_name, prop_type, val_id) trios.
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	{
2547c478bd9Sstevel@tonic-gate 		"prop_lnk_tbl",
2557c478bd9Sstevel@tonic-gate 		"lnk_prop_id     INTEGER PRIMARY KEY,"
2567c478bd9Sstevel@tonic-gate 		"lnk_pg_id       INTEGER NOT NULL,"
2577c478bd9Sstevel@tonic-gate 		"lnk_gen_id      INTEGER NOT NULL,"
2587c478bd9Sstevel@tonic-gate 		"lnk_prop_name   CHAR(256) NOT NULL,"
2597c478bd9Sstevel@tonic-gate 		"lnk_prop_type   CHAR(2) NOT NULL,"
2607c478bd9Sstevel@tonic-gate 		"lnk_val_id      INTEGER"
2617c478bd9Sstevel@tonic-gate 	},
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * value_tbl maps a value_id to a set of values.  For any given
265*6e1d2b42Samaguire 	 * value_id, value_type is constant.  The table definition here
266*6e1d2b42Samaguire 	 * is repeated in backend_check_upgrade(),  and must be kept in-sync.
2677c478bd9Sstevel@tonic-gate 	 */
2687c478bd9Sstevel@tonic-gate 	{
2697c478bd9Sstevel@tonic-gate 		"value_tbl",
2707c478bd9Sstevel@tonic-gate 		"value_id        INTEGER NOT NULL,"
2717c478bd9Sstevel@tonic-gate 		"value_type      CHAR(1) NOT NULL,"
272*6e1d2b42Samaguire 		"value_value     VARCHAR NOT NULL,"
273*6e1d2b42Samaguire 		"value_order     INTEGER DEFAULT 0"
2747c478bd9Sstevel@tonic-gate 	},
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * id_tbl has one row per id space
2787c478bd9Sstevel@tonic-gate 	 */
2797c478bd9Sstevel@tonic-gate 	{
2807c478bd9Sstevel@tonic-gate 		"id_tbl",
2817c478bd9Sstevel@tonic-gate 		"id_name         STRING NOT NULL,"
2827c478bd9Sstevel@tonic-gate 		"id_next         INTEGER NOT NULL"
2837c478bd9Sstevel@tonic-gate 	},
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * schema_version has a single row, which contains
2877c478bd9Sstevel@tonic-gate 	 * BACKEND_SCHEMA_VERSION at the time of creation.
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 	{
2907c478bd9Sstevel@tonic-gate 		"schema_version",
2917c478bd9Sstevel@tonic-gate 		"schema_version  INTEGER"
2927c478bd9Sstevel@tonic-gate 	},
2937c478bd9Sstevel@tonic-gate 	{ NULL, NULL }
2947c478bd9Sstevel@tonic-gate };
2957c478bd9Sstevel@tonic-gate 
296*6e1d2b42Samaguire /*
297*6e1d2b42Samaguire  * The indexing of value_tbl is repeated in backend_check_upgrade() and
298*6e1d2b42Samaguire  * must be kept in sync with the indexing specification here.
299*6e1d2b42Samaguire  */
3007c478bd9Sstevel@tonic-gate static struct backend_idx_info idxs_common[] = { /* all backend types */
3017c478bd9Sstevel@tonic-gate 	{ "pg_tbl",		"parent", "pg_parent_id" },
3027c478bd9Sstevel@tonic-gate 	{ "pg_tbl",		"name",	"pg_parent_id, pg_name" },
3037c478bd9Sstevel@tonic-gate 	{ "pg_tbl",		"type",	"pg_parent_id, pg_type" },
3047c478bd9Sstevel@tonic-gate 	{ "prop_lnk_tbl",	"base",	"lnk_pg_id, lnk_gen_id" },
3057c478bd9Sstevel@tonic-gate 	{ "prop_lnk_tbl",	"val",	"lnk_val_id" },
3067c478bd9Sstevel@tonic-gate 	{ "value_tbl",		"id",	"value_id" },
3077c478bd9Sstevel@tonic-gate 	{ "id_tbl",		"id",	"id_name" },
3087c478bd9Sstevel@tonic-gate 	{ NULL, NULL, NULL }
3097c478bd9Sstevel@tonic-gate };
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate struct run_single_int_info {
3127c478bd9Sstevel@tonic-gate 	uint32_t	*rs_out;
3137c478bd9Sstevel@tonic-gate 	int		rs_result;
3147c478bd9Sstevel@tonic-gate };
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3177c478bd9Sstevel@tonic-gate static int
3187c478bd9Sstevel@tonic-gate run_single_int_callback(void *arg, int columns, char **vals, char **names)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	struct run_single_int_info *info = arg;
3217c478bd9Sstevel@tonic-gate 	uint32_t val;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	char *endptr = vals[0];
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	assert(info->rs_result != REP_PROTOCOL_SUCCESS);
3267c478bd9Sstevel@tonic-gate 	assert(columns == 1);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (vals[0] == NULL)
3297c478bd9Sstevel@tonic-gate 		return (BACKEND_CALLBACK_CONTINUE);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	errno = 0;
3327c478bd9Sstevel@tonic-gate 	val = strtoul(vals[0], &endptr, 10);
3337c478bd9Sstevel@tonic-gate 	if ((val == 0 && endptr == vals[0]) || *endptr != 0 || errno != 0)
3347c478bd9Sstevel@tonic-gate 		backend_panic("malformed integer \"%20s\"", vals[0]);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	*info->rs_out = val;
3377c478bd9Sstevel@tonic-gate 	info->rs_result = REP_PROTOCOL_SUCCESS;
3387c478bd9Sstevel@tonic-gate 	return (BACKEND_CALLBACK_CONTINUE);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3427c478bd9Sstevel@tonic-gate int
3437c478bd9Sstevel@tonic-gate backend_fail_if_seen(void *arg, int columns, char **vals, char **names)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	return (BACKEND_CALLBACK_ABORT);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3488918dff3Sjwadams /*
3498918dff3Sjwadams  * check to see if we can successfully start a transaction;  if not, the
3508918dff3Sjwadams  * filesystem is mounted read-only.
3518918dff3Sjwadams  */
3527c478bd9Sstevel@tonic-gate static int
3538918dff3Sjwadams backend_is_readonly(struct sqlite *db, const char *path)
3547c478bd9Sstevel@tonic-gate {
3558918dff3Sjwadams 	int r;
3568918dff3Sjwadams 	statvfs64_t stat;
3578918dff3Sjwadams 
3588918dff3Sjwadams 	if (statvfs64(path, &stat) == 0 && (stat.f_flag & ST_RDONLY))
3598918dff3Sjwadams 		return (SQLITE_READONLY);
3608918dff3Sjwadams 
3618918dff3Sjwadams 	r = sqlite_exec(db,
3627c478bd9Sstevel@tonic-gate 	    "BEGIN TRANSACTION; "
3637c478bd9Sstevel@tonic-gate 	    "UPDATE schema_version SET schema_version = schema_version; ",
3648918dff3Sjwadams 	    NULL, NULL, NULL);
3657c478bd9Sstevel@tonic-gate 	(void) sqlite_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, NULL);
3667c478bd9Sstevel@tonic-gate 	return (r);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate static void
3707c478bd9Sstevel@tonic-gate backend_trace_sql(void *arg, const char *sql)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be = arg;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (backend_print_trace) {
3757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%d: %s\n", be->be_type, sql);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate static sqlite_backend_t be_info[BACKEND_TYPE_TOTAL];
3807c478bd9Sstevel@tonic-gate static sqlite_backend_t *bes[BACKEND_TYPE_TOTAL];
3817c478bd9Sstevel@tonic-gate 
382*6e1d2b42Samaguire /*
383*6e1d2b42Samaguire  * For a native build,  repositories are created from scratch, so upgrade
384*6e1d2b42Samaguire  * is not an issue.  This variable is implicitly protected by
385*6e1d2b42Samaguire  * bes[BACKEND_TYPE_NORMAL]->be_lock.
386*6e1d2b42Samaguire  */
387*6e1d2b42Samaguire #ifdef NATIVE_BUILD
388*6e1d2b42Samaguire static boolean_t be_normal_upgraded = B_TRUE;
389*6e1d2b42Samaguire #else
390*6e1d2b42Samaguire static boolean_t be_normal_upgraded = B_FALSE;
391*6e1d2b42Samaguire #endif	/* NATIVE_BUILD */
392*6e1d2b42Samaguire 
393*6e1d2b42Samaguire /*
394*6e1d2b42Samaguire  * Has backend been upgraded? In nonpersistent case, answer is always
395*6e1d2b42Samaguire  * yes.
396*6e1d2b42Samaguire  */
397*6e1d2b42Samaguire boolean_t
398*6e1d2b42Samaguire backend_is_upgraded(backend_tx_t *bt)
399*6e1d2b42Samaguire {
400*6e1d2b42Samaguire 	if (bt->bt_type == BACKEND_TYPE_NONPERSIST)
401*6e1d2b42Samaguire 		return (B_TRUE);
402*6e1d2b42Samaguire 	return (be_normal_upgraded);
403*6e1d2b42Samaguire }
404*6e1d2b42Samaguire 
4057c478bd9Sstevel@tonic-gate #define	BACKEND_PANIC_TIMEOUT	(50 * MILLISEC)
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate  * backend_panic() -- some kind of database problem or corruption has been hit.
4087c478bd9Sstevel@tonic-gate  * We attempt to quiesce the other database users -- all of the backend sql
4097c478bd9Sstevel@tonic-gate  * entry points will call backend_panic(NULL) if a panic is in progress, as
4107c478bd9Sstevel@tonic-gate  * will any attempt to start a transaction.
4117c478bd9Sstevel@tonic-gate  *
4127c478bd9Sstevel@tonic-gate  * We give threads holding a backend lock 50ms (BACKEND_PANIC_TIMEOUT) to
4137c478bd9Sstevel@tonic-gate  * either drop the lock or call backend_panic().  If they don't respond in
4147c478bd9Sstevel@tonic-gate  * time, we'll just exit anyway.
4157c478bd9Sstevel@tonic-gate  */
4167c478bd9Sstevel@tonic-gate void
4177c478bd9Sstevel@tonic-gate backend_panic(const char *format, ...)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	int i;
4207c478bd9Sstevel@tonic-gate 	va_list args;
4217c478bd9Sstevel@tonic-gate 	int failed = 0;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&backend_panic_lock);
4247c478bd9Sstevel@tonic-gate 	if (backend_panic_thread != 0) {
4257c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&backend_panic_lock);
4267c478bd9Sstevel@tonic-gate 		/*
4277c478bd9Sstevel@tonic-gate 		 * first, drop any backend locks we're holding, then
4287c478bd9Sstevel@tonic-gate 		 * sleep forever on the panic_cv.
4297c478bd9Sstevel@tonic-gate 		 */
4307c478bd9Sstevel@tonic-gate 		for (i = 0; i < BACKEND_TYPE_TOTAL; i++) {
4317c478bd9Sstevel@tonic-gate 			if (bes[i] != NULL &&
4327c478bd9Sstevel@tonic-gate 			    bes[i]->be_thread == pthread_self())
4337c478bd9Sstevel@tonic-gate 				(void) pthread_mutex_unlock(&bes[i]->be_lock);
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&backend_panic_lock);
4367c478bd9Sstevel@tonic-gate 		for (;;)
4377c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&backend_panic_cv,
4387c478bd9Sstevel@tonic-gate 			    &backend_panic_lock);
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 	backend_panic_thread = pthread_self();
4417c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&backend_panic_lock);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	for (i = 0; i < BACKEND_TYPE_TOTAL; i++) {
4447c478bd9Sstevel@tonic-gate 		if (bes[i] != NULL && bes[i]->be_thread == pthread_self())
4457c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&bes[i]->be_lock);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	va_start(args, format);
4497c478bd9Sstevel@tonic-gate 	configd_vcritical(format, args);
4507c478bd9Sstevel@tonic-gate 	va_end(args);
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	for (i = 0; i < BACKEND_TYPE_TOTAL; i++) {
4537c478bd9Sstevel@tonic-gate 		timespec_t rel;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		rel.tv_sec = 0;
4567c478bd9Sstevel@tonic-gate 		rel.tv_nsec = BACKEND_PANIC_TIMEOUT;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		if (bes[i] != NULL && bes[i]->be_thread != pthread_self()) {
4597c478bd9Sstevel@tonic-gate 			if (pthread_mutex_reltimedlock_np(&bes[i]->be_lock,
4607c478bd9Sstevel@tonic-gate 			    &rel) != 0)
4617c478bd9Sstevel@tonic-gate 				failed++;
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	if (failed) {
4657c478bd9Sstevel@tonic-gate 		configd_critical("unable to quiesce database\n");
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (backend_panic_abort)
4697c478bd9Sstevel@tonic-gate 		abort();
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	exit(CONFIGD_EXIT_DATABASE_BAD);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate  * Returns
4767c478bd9Sstevel@tonic-gate  *   _SUCCESS
4777c478bd9Sstevel@tonic-gate  *   _DONE - callback aborted query
4787c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory (_FULL & _TOOBIG?)
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate static int
4817c478bd9Sstevel@tonic-gate backend_error(sqlite_backend_t *be, int error, char *errmsg)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	if (error == SQLITE_OK)
4847c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_SUCCESS);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	switch (error) {
4877c478bd9Sstevel@tonic-gate 	case SQLITE_ABORT:
4887c478bd9Sstevel@tonic-gate 		free(errmsg);
4897c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_DONE);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	case SQLITE_NOMEM:
4927c478bd9Sstevel@tonic-gate 	case SQLITE_FULL:
4937c478bd9Sstevel@tonic-gate 	case SQLITE_TOOBIG:
4947c478bd9Sstevel@tonic-gate 		free(errmsg);
4957c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	default:
4987c478bd9Sstevel@tonic-gate 		backend_panic("%s: db error: %s", be->be_path, errmsg);
4997c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate static void
5047c478bd9Sstevel@tonic-gate backend_backup_cleanup(const char **out_arg, ssize_t out_sz)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	char **out = (char **)out_arg;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	while (out_sz-- > 0)
5097c478bd9Sstevel@tonic-gate 		free(*out++);
5107c478bd9Sstevel@tonic-gate 	free(out_arg);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate  * builds a inverse-time-sorted array of backup files.  The path is a
5157c478bd9Sstevel@tonic-gate  * a single buffer, and the pointers look like:
5167c478bd9Sstevel@tonic-gate  *
5177c478bd9Sstevel@tonic-gate  *	/this/is/a/full/path/to/repository-name-YYYYMMDDHHMMSS
5187c478bd9Sstevel@tonic-gate  *	^pathname		^	       ^(pathname+pathlen)
5197c478bd9Sstevel@tonic-gate  *				basename
5207c478bd9Sstevel@tonic-gate  *
5217c478bd9Sstevel@tonic-gate  * dirname will either be pathname, or ".".
5227c478bd9Sstevel@tonic-gate  *
5237c478bd9Sstevel@tonic-gate  * Returns the number of elements in the array, 0 if there are no previous
5247c478bd9Sstevel@tonic-gate  * backups, or -1 on error.
5257c478bd9Sstevel@tonic-gate  */
5267c478bd9Sstevel@tonic-gate static ssize_t
5277c478bd9Sstevel@tonic-gate backend_backup_get_prev(char *pathname, size_t pathlen, const char ***out_arg)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	char b_start, b_end;
5307c478bd9Sstevel@tonic-gate 	DIR *dir;
5317c478bd9Sstevel@tonic-gate 	char **out = NULL;
5327c478bd9Sstevel@tonic-gate 	char *name, *p;
5337c478bd9Sstevel@tonic-gate 	char *dirname, *basename;
5347c478bd9Sstevel@tonic-gate 	char *pathend;
5357c478bd9Sstevel@tonic-gate 	struct dirent *ent;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	size_t count = 0;
5387c478bd9Sstevel@tonic-gate 	size_t baselen;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/*
5417c478bd9Sstevel@tonic-gate 	 * year, month, day, hour, min, sec, plus an '_'.
5427c478bd9Sstevel@tonic-gate 	 */
5437c478bd9Sstevel@tonic-gate 	const size_t ndigits = 4 + 5*2 + 1;
5447c478bd9Sstevel@tonic-gate 	const size_t baroffset = 4 + 2*2;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	size_t idx;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	pathend = pathname + pathlen;
5497c478bd9Sstevel@tonic-gate 	b_end = *pathend;
5507c478bd9Sstevel@tonic-gate 	*pathend = '\0';
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	basename = strrchr(pathname, '/');
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (basename != NULL) {
5557c478bd9Sstevel@tonic-gate 		assert(pathend > pathname && basename < pathend);
5567c478bd9Sstevel@tonic-gate 		basename++;
5577c478bd9Sstevel@tonic-gate 		dirname = pathname;
5587c478bd9Sstevel@tonic-gate 	} else {
5597c478bd9Sstevel@tonic-gate 		basename = pathname;
5607c478bd9Sstevel@tonic-gate 		dirname = ".";
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	baselen = strlen(basename);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	/*
5667c478bd9Sstevel@tonic-gate 	 * munge the string temporarily for the opendir(), then restore it.
5677c478bd9Sstevel@tonic-gate 	 */
5687c478bd9Sstevel@tonic-gate 	b_start = basename[0];
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	basename[0] = '\0';
5717c478bd9Sstevel@tonic-gate 	dir = opendir(dirname);
5727c478bd9Sstevel@tonic-gate 	basename[0] = b_start;		/* restore path */
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	if (dir == NULL)
5757c478bd9Sstevel@tonic-gate 		goto fail;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	while ((ent = readdir(dir)) != NULL) {
5797c478bd9Sstevel@tonic-gate 		/*
5807c478bd9Sstevel@tonic-gate 		 * Must match:
5817c478bd9Sstevel@tonic-gate 		 *	basename-YYYYMMDD_HHMMSS
5827c478bd9Sstevel@tonic-gate 		 * or we ignore it.
5837c478bd9Sstevel@tonic-gate 		 */
5847c478bd9Sstevel@tonic-gate 		if (strncmp(ent->d_name, basename, baselen) != 0)
5857c478bd9Sstevel@tonic-gate 			continue;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		name = ent->d_name;
5887c478bd9Sstevel@tonic-gate 		if (name[baselen] != '-')
5897c478bd9Sstevel@tonic-gate 			continue;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		p = name + baselen + 1;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < ndigits; idx++) {
5947c478bd9Sstevel@tonic-gate 			char c = p[idx];
5957c478bd9Sstevel@tonic-gate 			if (idx == baroffset && c != '_')
5967c478bd9Sstevel@tonic-gate 				break;
5977c478bd9Sstevel@tonic-gate 			if (idx != baroffset && (c < '0' || c > '9'))
5987c478bd9Sstevel@tonic-gate 				break;
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 		if (idx != ndigits || p[idx] != '\0')
6017c478bd9Sstevel@tonic-gate 			continue;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		/*
6047c478bd9Sstevel@tonic-gate 		 * We have a match.  insertion-sort it into our list.
6057c478bd9Sstevel@tonic-gate 		 */
6067c478bd9Sstevel@tonic-gate 		name = strdup(name);
6077c478bd9Sstevel@tonic-gate 		if (name == NULL)
6087c478bd9Sstevel@tonic-gate 			goto fail_closedir;
6097c478bd9Sstevel@tonic-gate 		p = strrchr(name, '-');
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < count; idx++) {
6127c478bd9Sstevel@tonic-gate 			char *tmp = out[idx];
6137c478bd9Sstevel@tonic-gate 			char *tp = strrchr(tmp, '-');
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 			int cmp = strcmp(p, tp);
6167c478bd9Sstevel@tonic-gate 			if (cmp == 0)
6177c478bd9Sstevel@tonic-gate 				cmp = strcmp(name, tmp);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 			if (cmp == 0) {
6207c478bd9Sstevel@tonic-gate 				free(name);
6217c478bd9Sstevel@tonic-gate 				name = NULL;
6227c478bd9Sstevel@tonic-gate 				break;
6237c478bd9Sstevel@tonic-gate 			} else if (cmp > 0) {
6247c478bd9Sstevel@tonic-gate 				out[idx] = name;
6257c478bd9Sstevel@tonic-gate 				name = tmp;
6267c478bd9Sstevel@tonic-gate 				p = tp;
6277c478bd9Sstevel@tonic-gate 			}
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		if (idx == count) {
6317c478bd9Sstevel@tonic-gate 			char **new_out = realloc(out,
6327c478bd9Sstevel@tonic-gate 			    (count + 1) * sizeof (*out));
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 			if (new_out == NULL) {
6357c478bd9Sstevel@tonic-gate 				free(name);
6367c478bd9Sstevel@tonic-gate 				goto fail_closedir;
6377c478bd9Sstevel@tonic-gate 			}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 			out = new_out;
6407c478bd9Sstevel@tonic-gate 			out[count++] = name;
6417c478bd9Sstevel@tonic-gate 		} else {
6427c478bd9Sstevel@tonic-gate 			assert(name == NULL);
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	(void) closedir(dir);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	basename[baselen] = b_end;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	*out_arg = (const char **)out;
6507c478bd9Sstevel@tonic-gate 	return (count);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate fail_closedir:
6537c478bd9Sstevel@tonic-gate 	(void) closedir(dir);
6547c478bd9Sstevel@tonic-gate fail:
6557c478bd9Sstevel@tonic-gate 	basename[0] = b_start;
6567c478bd9Sstevel@tonic-gate 	*pathend = b_end;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	backend_backup_cleanup((const char **)out, count);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	*out_arg = NULL;
6617c478bd9Sstevel@tonic-gate 	return (-1);
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate  * Copies the repository path into out, a buffer of out_len bytes,
6667c478bd9Sstevel@tonic-gate  * removes the ".db" (or whatever) extension, and, if name is non-NULL,
6677c478bd9Sstevel@tonic-gate  * appends "-name" to it.  If name is non-NULL, it can fail with:
6687c478bd9Sstevel@tonic-gate  *
6697c478bd9Sstevel@tonic-gate  *	_TRUNCATED	will not fit in buffer.
6707c478bd9Sstevel@tonic-gate  *	_BAD_REQUEST	name is not a valid identifier
6717c478bd9Sstevel@tonic-gate  */
6727c478bd9Sstevel@tonic-gate static rep_protocol_responseid_t
6737c478bd9Sstevel@tonic-gate backend_backup_base(sqlite_backend_t *be, const char *name,
6747c478bd9Sstevel@tonic-gate     char *out, size_t out_len)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	char *p, *q;
6777c478bd9Sstevel@tonic-gate 	size_t len;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/*
6807c478bd9Sstevel@tonic-gate 	 * for paths of the form /path/to/foo.db, we truncate at the final
6817c478bd9Sstevel@tonic-gate 	 * '.'.
6827c478bd9Sstevel@tonic-gate 	 */
6837c478bd9Sstevel@tonic-gate 	(void) strlcpy(out, be->be_path, out_len);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	p = strrchr(out, '/');
6867c478bd9Sstevel@tonic-gate 	q = strrchr(out, '.');
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if (p != NULL && q != NULL && q > p)
6897c478bd9Sstevel@tonic-gate 		*q = 0;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	if (name != NULL) {
6927c478bd9Sstevel@tonic-gate 		len = strlen(out);
6937c478bd9Sstevel@tonic-gate 		assert(len < out_len);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		out += len;
6967c478bd9Sstevel@tonic-gate 		out_len -= len;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		len = strlen(name);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		/*
7017c478bd9Sstevel@tonic-gate 		 * verify that the name tag is entirely alphabetic,
7027c478bd9Sstevel@tonic-gate 		 * non-empty, and not too long.
7037c478bd9Sstevel@tonic-gate 		 */
7047c478bd9Sstevel@tonic-gate 		if (len == 0 || len >= REP_PROTOCOL_NAME_LEN ||
7057c478bd9Sstevel@tonic-gate 		    uu_check_name(name, UU_NAME_DOMAIN) < 0)
7067c478bd9Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 		if (snprintf(out, out_len, "-%s", name) >= out_len)
7097c478bd9Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_TRUNCATED);
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate /*
7168918dff3Sjwadams  * See if a backup is needed.  We do a backup unless both files are
7178918dff3Sjwadams  * byte-for-byte identical.
7188918dff3Sjwadams  */
7198918dff3Sjwadams static int
7208918dff3Sjwadams backend_check_backup_needed(const char *rep_name, const char *backup_name)
7218918dff3Sjwadams {
7228918dff3Sjwadams 	int repfd = open(rep_name, O_RDONLY);
7238918dff3Sjwadams 	int fd = open(backup_name, O_RDONLY);
7248918dff3Sjwadams 	struct stat s_rep, s_backup;
7258918dff3Sjwadams 	int c1, c2;
7268918dff3Sjwadams 
7278918dff3Sjwadams 	FILE *f_rep = NULL;
7288918dff3Sjwadams 	FILE *f_backup = NULL;
7298918dff3Sjwadams 
7308918dff3Sjwadams 	if (repfd < 0 || fd < 0)
7318918dff3Sjwadams 		goto fail;
7328918dff3Sjwadams 
7338918dff3Sjwadams 	if (fstat(repfd, &s_rep) < 0 || fstat(fd, &s_backup) < 0)
7348918dff3Sjwadams 		goto fail;
7358918dff3Sjwadams 
7368918dff3Sjwadams 	/*
7378918dff3Sjwadams 	 * if they are the same file, we need to do a backup to break the
7388918dff3Sjwadams 	 * hard link or symlink involved.
7398918dff3Sjwadams 	 */
7408918dff3Sjwadams 	if (s_rep.st_ino == s_backup.st_ino && s_rep.st_dev == s_backup.st_dev)
7418918dff3Sjwadams 		goto fail;
7428918dff3Sjwadams 
7438918dff3Sjwadams 	if (s_rep.st_size != s_backup.st_size)
7448918dff3Sjwadams 		goto fail;
7458918dff3Sjwadams 
7468918dff3Sjwadams 	if ((f_rep = fdopen(repfd, "r")) == NULL ||
7478918dff3Sjwadams 	    (f_backup = fdopen(fd, "r")) == NULL)
7488918dff3Sjwadams 		goto fail;
7498918dff3Sjwadams 
7508918dff3Sjwadams 	do {
7518918dff3Sjwadams 		c1 = getc(f_rep);
7528918dff3Sjwadams 		c2 = getc(f_backup);
7538918dff3Sjwadams 		if (c1 != c2)
7548918dff3Sjwadams 			goto fail;
7558918dff3Sjwadams 	} while (c1 != EOF);
7568918dff3Sjwadams 
7578918dff3Sjwadams 	if (!ferror(f_rep) && !ferror(f_backup)) {
7588918dff3Sjwadams 		(void) fclose(f_rep);
7598918dff3Sjwadams 		(void) fclose(f_backup);
7608918dff3Sjwadams 		(void) close(repfd);
7618918dff3Sjwadams 		(void) close(fd);
7628918dff3Sjwadams 		return (0);
7638918dff3Sjwadams 	}
7648918dff3Sjwadams 
7658918dff3Sjwadams fail:
7668918dff3Sjwadams 	if (f_rep != NULL)
7678918dff3Sjwadams 		(void) fclose(f_rep);
7688918dff3Sjwadams 	if (f_backup != NULL)
7698918dff3Sjwadams 		(void) fclose(f_backup);
7708918dff3Sjwadams 	if (repfd >= 0)
7718918dff3Sjwadams 		(void) close(repfd);
7728918dff3Sjwadams 	if (fd >= 0)
7738918dff3Sjwadams 		(void) close(fd);
7748918dff3Sjwadams 	return (1);
7758918dff3Sjwadams }
7768918dff3Sjwadams 
7778918dff3Sjwadams /*
778c0889d7aSstevep  * This interface is called to perform the actual copy
779c0889d7aSstevep  *
780c0889d7aSstevep  * Return:
781c0889d7aSstevep  *	_FAIL_UNKNOWN		read/write fails
782c0889d7aSstevep  *	_FAIL_NO_RESOURCES	out of memory
783c0889d7aSstevep  *	_SUCCESS		copy succeeds
784c0889d7aSstevep  */
785c0889d7aSstevep static rep_protocol_responseid_t
786c0889d7aSstevep backend_do_copy(const char *src, int srcfd, const char *dst,
787c0889d7aSstevep     int dstfd, size_t *sz)
788c0889d7aSstevep {
789c0889d7aSstevep 	char *buf;
790c0889d7aSstevep 	off_t nrd, nwr, n, r_off = 0, w_off = 0;
791c0889d7aSstevep 
792c0889d7aSstevep 	if ((buf = malloc(8192)) == NULL)
793c0889d7aSstevep 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
794c0889d7aSstevep 
795c0889d7aSstevep 	while ((nrd = read(srcfd, buf, 8192)) != 0) {
796c0889d7aSstevep 		if (nrd < 0) {
797c0889d7aSstevep 			if (errno == EINTR)
798c0889d7aSstevep 				continue;
799c0889d7aSstevep 
800c0889d7aSstevep 			configd_critical(
801c0889d7aSstevep 			    "Backend copy failed: fails to read from %s "
802c0889d7aSstevep 			    "at offset %d: %s\n", src, r_off, strerror(errno));
803c0889d7aSstevep 			free(buf);
804c0889d7aSstevep 			return (REP_PROTOCOL_FAIL_UNKNOWN);
805c0889d7aSstevep 		}
806c0889d7aSstevep 
807c0889d7aSstevep 		r_off += nrd;
808c0889d7aSstevep 
809c0889d7aSstevep 		nwr = 0;
810c0889d7aSstevep 		do {
811c0889d7aSstevep 			if ((n = write(dstfd, &buf[nwr], nrd - nwr)) < 0) {
812c0889d7aSstevep 				if (errno == EINTR)
813c0889d7aSstevep 					continue;
814c0889d7aSstevep 
815c0889d7aSstevep 				configd_critical(
816c0889d7aSstevep 				    "Backend copy failed: fails to write to %s "
817c0889d7aSstevep 				    "at offset %d: %s\n", dst, w_off,
818c0889d7aSstevep 				    strerror(errno));
819c0889d7aSstevep 				free(buf);
820c0889d7aSstevep 				return (REP_PROTOCOL_FAIL_UNKNOWN);
821c0889d7aSstevep 			}
822c0889d7aSstevep 
823c0889d7aSstevep 			nwr += n;
824c0889d7aSstevep 			w_off += n;
825c0889d7aSstevep 
826c0889d7aSstevep 		} while (nwr < nrd);
827c0889d7aSstevep 	}
828c0889d7aSstevep 
829c0889d7aSstevep 	if (sz)
830c0889d7aSstevep 		*sz = w_off;
831c0889d7aSstevep 
832c0889d7aSstevep 	free(buf);
833c0889d7aSstevep 	return (REP_PROTOCOL_SUCCESS);
834c0889d7aSstevep }
835c0889d7aSstevep 
836c0889d7aSstevep /*
8377c478bd9Sstevel@tonic-gate  * Can return:
8387c478bd9Sstevel@tonic-gate  *	_BAD_REQUEST		name is not valid
8397c478bd9Sstevel@tonic-gate  *	_TRUNCATED		name is too long for current repository path
8407c478bd9Sstevel@tonic-gate  *	_UNKNOWN		failed for unknown reason (details written to
8417c478bd9Sstevel@tonic-gate  *				console)
8427c478bd9Sstevel@tonic-gate  *	_BACKEND_READONLY	backend is not writable
843c0889d7aSstevep  *	_NO_RESOURCES		out of memory
8447c478bd9Sstevel@tonic-gate  *	_SUCCESS		Backup completed successfully.
8457c478bd9Sstevel@tonic-gate  */
8467c478bd9Sstevel@tonic-gate static rep_protocol_responseid_t
8477c478bd9Sstevel@tonic-gate backend_create_backup_locked(sqlite_backend_t *be, const char *name)
8487c478bd9Sstevel@tonic-gate {
8497c478bd9Sstevel@tonic-gate 	const char **old_list;
8507c478bd9Sstevel@tonic-gate 	ssize_t old_sz;
8517c478bd9Sstevel@tonic-gate 	ssize_t old_max = max_repository_backups;
8527c478bd9Sstevel@tonic-gate 	ssize_t cur;
8537c478bd9Sstevel@tonic-gate 	char *finalname;
854c0889d7aSstevep 	char *finalpath;
855c0889d7aSstevep 	char *tmppath;
8567c478bd9Sstevel@tonic-gate 	int infd, outfd;
8577c478bd9Sstevel@tonic-gate 	size_t len;
8587c478bd9Sstevel@tonic-gate 	time_t now;
8597c478bd9Sstevel@tonic-gate 	struct tm now_tm;
8607c478bd9Sstevel@tonic-gate 	rep_protocol_responseid_t result;
8617c478bd9Sstevel@tonic-gate 
862c0889d7aSstevep 	if ((finalpath = malloc(PATH_MAX)) == NULL)
863c0889d7aSstevep 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
8647c478bd9Sstevel@tonic-gate 
865c0889d7aSstevep 	if ((tmppath = malloc(PATH_MAX)) == NULL) {
866c0889d7aSstevep 		free(finalpath);
867c0889d7aSstevep 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
868c0889d7aSstevep 	}
869c0889d7aSstevep 
870c0889d7aSstevep 	if (be->be_readonly) {
871c0889d7aSstevep 		result = REP_PROTOCOL_FAIL_BACKEND_READONLY;
872c0889d7aSstevep 		goto out;
873c0889d7aSstevep 	}
874c0889d7aSstevep 
875c0889d7aSstevep 	result = backend_backup_base(be, name, finalpath, PATH_MAX);
8767c478bd9Sstevel@tonic-gate 	if (result != REP_PROTOCOL_SUCCESS)
877c0889d7aSstevep 		goto out;
8787c478bd9Sstevel@tonic-gate 
8798918dff3Sjwadams 	if (!backend_check_backup_needed(be->be_path, finalpath)) {
880c0889d7aSstevep 		result = REP_PROTOCOL_SUCCESS;
881c0889d7aSstevep 		goto out;
8828918dff3Sjwadams 	}
8838918dff3Sjwadams 
8847c478bd9Sstevel@tonic-gate 	/*
8857c478bd9Sstevel@tonic-gate 	 * remember the original length, and the basename location
8867c478bd9Sstevel@tonic-gate 	 */
8877c478bd9Sstevel@tonic-gate 	len = strlen(finalpath);
8887c478bd9Sstevel@tonic-gate 	finalname = strrchr(finalpath, '/');
8897c478bd9Sstevel@tonic-gate 	if (finalname != NULL)
8907c478bd9Sstevel@tonic-gate 		finalname++;
8917c478bd9Sstevel@tonic-gate 	else
8927c478bd9Sstevel@tonic-gate 		finalname = finalpath;
8937c478bd9Sstevel@tonic-gate 
894c0889d7aSstevep 	(void) strlcpy(tmppath, finalpath, PATH_MAX);
895c0889d7aSstevep 	if (strlcat(tmppath, "-tmpXXXXXX", PATH_MAX) >= PATH_MAX) {
896c0889d7aSstevep 		result = REP_PROTOCOL_FAIL_TRUNCATED;
897c0889d7aSstevep 		goto out;
898c0889d7aSstevep 	}
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	now = time(NULL);
9017c478bd9Sstevel@tonic-gate 	if (localtime_r(&now, &now_tm) == NULL) {
9027c478bd9Sstevel@tonic-gate 		configd_critical(
9037c478bd9Sstevel@tonic-gate 		    "\"%s\" backup failed: localtime(3C) failed: %s\n", name,
9047c478bd9Sstevel@tonic-gate 		    be->be_path, strerror(errno));
905c0889d7aSstevep 		result = REP_PROTOCOL_FAIL_UNKNOWN;
906c0889d7aSstevep 		goto out;
9077c478bd9Sstevel@tonic-gate 	}
9087c478bd9Sstevel@tonic-gate 
909c0889d7aSstevep 	if (strftime(finalpath + len, PATH_MAX - len,
910c0889d7aSstevep 	    "-%Y""%m""%d""_""%H""%M""%S", &now_tm) >= PATH_MAX - len) {
911c0889d7aSstevep 		result = REP_PROTOCOL_FAIL_TRUNCATED;
912c0889d7aSstevep 		goto out;
9137c478bd9Sstevel@tonic-gate 	}
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	infd = open(be->be_path, O_RDONLY);
9167c478bd9Sstevel@tonic-gate 	if (infd < 0) {
9177c478bd9Sstevel@tonic-gate 		configd_critical("\"%s\" backup failed: opening %s: %s\n", name,
9187c478bd9Sstevel@tonic-gate 		    be->be_path, strerror(errno));
919c0889d7aSstevep 		result = REP_PROTOCOL_FAIL_UNKNOWN;
920c0889d7aSstevep 		goto out;
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	outfd = mkstemp(tmppath);
9247c478bd9Sstevel@tonic-gate 	if (outfd < 0) {
9257c478bd9Sstevel@tonic-gate 		configd_critical("\"%s\" backup failed: mkstemp(%s): %s\n",
9267c478bd9Sstevel@tonic-gate 		    name, tmppath, strerror(errno));
9277c478bd9Sstevel@tonic-gate 		(void) close(infd);
9287c478bd9Sstevel@tonic-gate 		result = REP_PROTOCOL_FAIL_UNKNOWN;
929c0889d7aSstevep 		goto out;
9307c478bd9Sstevel@tonic-gate 	}
9317c478bd9Sstevel@tonic-gate 
932c0889d7aSstevep 	if ((result = backend_do_copy((const char *)be->be_path, infd,
933c0889d7aSstevep 	    (const char *)tmppath, outfd, NULL)) != REP_PROTOCOL_SUCCESS)
9347c478bd9Sstevel@tonic-gate 		goto fail;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	/*
9377c478bd9Sstevel@tonic-gate 	 * grab the old list before doing our re-name.
9387c478bd9Sstevel@tonic-gate 	 */
9397c478bd9Sstevel@tonic-gate 	if (old_max > 0)
9407c478bd9Sstevel@tonic-gate 		old_sz = backend_backup_get_prev(finalpath, len, &old_list);
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	if (rename(tmppath, finalpath) < 0) {
9437c478bd9Sstevel@tonic-gate 		configd_critical(
9447c478bd9Sstevel@tonic-gate 		    "\"%s\" backup failed: rename(%s, %s): %s\n",
9457c478bd9Sstevel@tonic-gate 		    name, tmppath, finalpath, strerror(errno));
9467c478bd9Sstevel@tonic-gate 		result = REP_PROTOCOL_FAIL_UNKNOWN;
9477c478bd9Sstevel@tonic-gate 		goto fail;
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	tmppath[len] = 0;	/* strip -XXXXXX, for reference symlink */
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	(void) unlink(tmppath);
9537c478bd9Sstevel@tonic-gate 	if (symlink(finalname, tmppath) < 0) {
9547c478bd9Sstevel@tonic-gate 		configd_critical(
9557c478bd9Sstevel@tonic-gate 		    "\"%s\" backup completed, but updating "
9567c478bd9Sstevel@tonic-gate 		    "\"%s\" symlink to \"%s\" failed: %s\n",
9577c478bd9Sstevel@tonic-gate 		    name, tmppath, finalname, strerror(errno));
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	if (old_max > 0 && old_sz > 0) {
9617c478bd9Sstevel@tonic-gate 		/* unlink all but the first (old_max - 1) files */
9627c478bd9Sstevel@tonic-gate 		for (cur = old_max - 1; cur < old_sz; cur++) {
9637c478bd9Sstevel@tonic-gate 			(void) strlcpy(finalname, old_list[cur],
964c0889d7aSstevep 			    PATH_MAX - (finalname - finalpath));
9657c478bd9Sstevel@tonic-gate 			if (unlink(finalpath) < 0)
9667c478bd9Sstevel@tonic-gate 				configd_critical(
9677c478bd9Sstevel@tonic-gate 				    "\"%s\" backup completed, but removing old "
9687c478bd9Sstevel@tonic-gate 				    "file \"%s\" failed: %s\n",
9697c478bd9Sstevel@tonic-gate 				    name, finalpath, strerror(errno));
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 		backend_backup_cleanup(old_list, old_sz);
9737c478bd9Sstevel@tonic-gate 	}
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	result = REP_PROTOCOL_SUCCESS;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate fail:
9787c478bd9Sstevel@tonic-gate 	(void) close(infd);
9797c478bd9Sstevel@tonic-gate 	(void) close(outfd);
9807c478bd9Sstevel@tonic-gate 	if (result != REP_PROTOCOL_SUCCESS)
9817c478bd9Sstevel@tonic-gate 		(void) unlink(tmppath);
9827c478bd9Sstevel@tonic-gate 
983c0889d7aSstevep out:
984c0889d7aSstevep 	free(finalpath);
985c0889d7aSstevep 	free(tmppath);
986c0889d7aSstevep 
9877c478bd9Sstevel@tonic-gate 	return (result);
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate 
990*6e1d2b42Samaguire /*
991*6e1d2b42Samaguire  * Check if value_tbl has been upgraded in the main database,  and
992*6e1d2b42Samaguire  * if not (if the value_order column is not present),  and do_upgrade is true,
993*6e1d2b42Samaguire  * upgrade value_tbl in repository to contain the additional value_order
994*6e1d2b42Samaguire  * column. The version of sqlite used means ALTER TABLE is not
995*6e1d2b42Samaguire  * available, so we cannot simply use "ALTER TABLE value_tbl ADD COLUMN".
996*6e1d2b42Samaguire  * Rather we need to create a temporary table with the additional column,
997*6e1d2b42Samaguire  * import the value_tbl, drop the original value_tbl, recreate the value_tbl
998*6e1d2b42Samaguire  * with the additional column, import the values from value_tbl_tmp,
999*6e1d2b42Samaguire  * reindex and finally drop value_tbl_tmp.  During boot, we wish to check
1000*6e1d2b42Samaguire  * if the repository has been upgraded before it is writable,  so that
1001*6e1d2b42Samaguire  * property value retrieval can use the appropriate form of the SELECT
1002*6e1d2b42Samaguire  * statement that retrieves property values.  As a result, we need to check
1003*6e1d2b42Samaguire  * if the repository has been upgraded prior to the point when we can
1004*6e1d2b42Samaguire  * actually carry out the update.
1005*6e1d2b42Samaguire  */
1006*6e1d2b42Samaguire void
1007*6e1d2b42Samaguire backend_check_upgrade(sqlite_backend_t *be, boolean_t do_upgrade)
1008*6e1d2b42Samaguire {
1009*6e1d2b42Samaguire 	char *errp;
1010*6e1d2b42Samaguire 	int r;
1011*6e1d2b42Samaguire 
1012*6e1d2b42Samaguire 	if (be_normal_upgraded)
1013*6e1d2b42Samaguire 		return;
1014*6e1d2b42Samaguire 	/*
1015*6e1d2b42Samaguire 	 * Test if upgrade is needed. If value_order column does not exist,
1016*6e1d2b42Samaguire 	 * we need to upgrade the schema.
1017*6e1d2b42Samaguire 	 */
1018*6e1d2b42Samaguire 	r = sqlite_exec(be->be_db, "SELECT value_order FROM value_tbl LIMIT 1;",
1019*6e1d2b42Samaguire 	    NULL, NULL, NULL);
1020*6e1d2b42Samaguire 	if (r == SQLITE_ERROR && do_upgrade) {
1021*6e1d2b42Samaguire 		/* No value_order column - needs upgrade */
1022*6e1d2b42Samaguire 		configd_info("Upgrading SMF repository format...");
1023*6e1d2b42Samaguire 		r = sqlite_exec(be->be_db,
1024*6e1d2b42Samaguire 		    "BEGIN TRANSACTION; "
1025*6e1d2b42Samaguire 		    "CREATE TABLE value_tbl_tmp ( "
1026*6e1d2b42Samaguire 		    "value_id   INTEGER NOT NULL, "
1027*6e1d2b42Samaguire 		    "value_type CHAR(1) NOT NULL, "
1028*6e1d2b42Samaguire 		    "value_value VARCHAR NOT NULL, "
1029*6e1d2b42Samaguire 		    "value_order INTEGER DEFAULT 0); "
1030*6e1d2b42Samaguire 		    "INSERT INTO value_tbl_tmp "
1031*6e1d2b42Samaguire 		    "(value_id, value_type, value_value) "
1032*6e1d2b42Samaguire 		    "SELECT value_id, value_type, value_value FROM value_tbl; "
1033*6e1d2b42Samaguire 		    "DROP TABLE value_tbl; "
1034*6e1d2b42Samaguire 		    "CREATE TABLE value_tbl( "
1035*6e1d2b42Samaguire 		    "value_id   INTEGER NOT NULL, "
1036*6e1d2b42Samaguire 		    "value_type CHAR(1) NOT NULL, "
1037*6e1d2b42Samaguire 		    "value_value VARCHAR NOT NULL, "
1038*6e1d2b42Samaguire 		    "value_order INTEGER DEFAULT 0); "
1039*6e1d2b42Samaguire 		    "INSERT INTO value_tbl SELECT * FROM value_tbl_tmp; "
1040*6e1d2b42Samaguire 		    "CREATE INDEX value_tbl_id ON value_tbl (value_id); "
1041*6e1d2b42Samaguire 		    "DROP TABLE value_tbl_tmp; "
1042*6e1d2b42Samaguire 		    "COMMIT TRANSACTION; "
1043*6e1d2b42Samaguire 		    "VACUUM; ",
1044*6e1d2b42Samaguire 		    NULL, NULL, &errp);
1045*6e1d2b42Samaguire 		if (r == SQLITE_OK) {
1046*6e1d2b42Samaguire 			configd_info("SMF repository upgrade is complete.");
1047*6e1d2b42Samaguire 		} else {
1048*6e1d2b42Samaguire 			backend_panic("%s: repository upgrade failed: %s",
1049*6e1d2b42Samaguire 			    be->be_path, errp);
1050*6e1d2b42Samaguire 			/* NOTREACHED */
1051*6e1d2b42Samaguire 		}
1052*6e1d2b42Samaguire 	}
1053*6e1d2b42Samaguire 	if (r == SQLITE_OK)
1054*6e1d2b42Samaguire 		be_normal_upgraded = B_TRUE;
1055*6e1d2b42Samaguire 	else
1056*6e1d2b42Samaguire 		be_normal_upgraded = B_FALSE;
1057*6e1d2b42Samaguire }
1058*6e1d2b42Samaguire 
10598918dff3Sjwadams static int
10608918dff3Sjwadams backend_check_readonly(sqlite_backend_t *be, int writing, hrtime_t t)
10618918dff3Sjwadams {
10628918dff3Sjwadams 	char *errp;
10638918dff3Sjwadams 	struct sqlite *new;
10648918dff3Sjwadams 	int r;
10658918dff3Sjwadams 
10668918dff3Sjwadams 	assert(be->be_readonly);
10678918dff3Sjwadams 	assert(be == bes[BACKEND_TYPE_NORMAL]);
10688918dff3Sjwadams 
10698918dff3Sjwadams 	/*
10708918dff3Sjwadams 	 * If we don't *need* to be writable, only check every once in a
10718918dff3Sjwadams 	 * while.
10728918dff3Sjwadams 	 */
10738918dff3Sjwadams 	if (!writing) {
10748918dff3Sjwadams 		if ((uint64_t)(t - be->be_lastcheck) <
10758918dff3Sjwadams 		    BACKEND_READONLY_CHECK_INTERVAL)
10768918dff3Sjwadams 			return (REP_PROTOCOL_SUCCESS);
10778918dff3Sjwadams 		be->be_lastcheck = t;
10788918dff3Sjwadams 	}
10798918dff3Sjwadams 
10808918dff3Sjwadams 	new = sqlite_open(be->be_path, 0600, &errp);
10818918dff3Sjwadams 	if (new == NULL) {
10828918dff3Sjwadams 		backend_panic("reopening %s: %s\n", be->be_path, errp);
10838918dff3Sjwadams 		/*NOTREACHED*/
10848918dff3Sjwadams 	}
10858918dff3Sjwadams 	r = backend_is_readonly(new, be->be_path);
10868918dff3Sjwadams 
10878918dff3Sjwadams 	if (r != SQLITE_OK) {
10888918dff3Sjwadams 		sqlite_close(new);
10898918dff3Sjwadams 		if (writing)
10908918dff3Sjwadams 			return (REP_PROTOCOL_FAIL_BACKEND_READONLY);
10918918dff3Sjwadams 		return (REP_PROTOCOL_SUCCESS);
10928918dff3Sjwadams 	}
10938918dff3Sjwadams 
10948918dff3Sjwadams 	/*
10958918dff3Sjwadams 	 * We can write!  Swap the db handles, mark ourself writable,
1096*6e1d2b42Samaguire 	 * upgrade if necessary,  and make a backup.
10978918dff3Sjwadams 	 */
10988918dff3Sjwadams 	sqlite_close(be->be_db);
10998918dff3Sjwadams 	be->be_db = new;
11008918dff3Sjwadams 	be->be_readonly = 0;
11018918dff3Sjwadams 
1102*6e1d2b42Samaguire 	if (be->be_type == BACKEND_TYPE_NORMAL)
1103*6e1d2b42Samaguire 		backend_check_upgrade(be, B_TRUE);
1104*6e1d2b42Samaguire 
11058918dff3Sjwadams 	if (backend_create_backup_locked(be, REPOSITORY_BOOT_BACKUP) !=
11068918dff3Sjwadams 	    REP_PROTOCOL_SUCCESS) {
11078918dff3Sjwadams 		configd_critical(
11088918dff3Sjwadams 		    "unable to create \"%s\" backup of \"%s\"\n",
11098918dff3Sjwadams 		    REPOSITORY_BOOT_BACKUP, be->be_path);
11108918dff3Sjwadams 	}
11118918dff3Sjwadams 
11128918dff3Sjwadams 	return (REP_PROTOCOL_SUCCESS);
11138918dff3Sjwadams }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate /*
11167c478bd9Sstevel@tonic-gate  * If t is not BACKEND_TYPE_NORMAL, can fail with
11177c478bd9Sstevel@tonic-gate  *   _BACKEND_ACCESS - backend does not exist
11187c478bd9Sstevel@tonic-gate  *
11197c478bd9Sstevel@tonic-gate  * If writing is nonzero, can also fail with
11207c478bd9Sstevel@tonic-gate  *   _BACKEND_READONLY - backend is read-only
11217c478bd9Sstevel@tonic-gate  */
11227c478bd9Sstevel@tonic-gate static int
11237c478bd9Sstevel@tonic-gate backend_lock(backend_type_t t, int writing, sqlite_backend_t **bep)
11247c478bd9Sstevel@tonic-gate {
11257c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be = NULL;
11267c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	*bep = NULL;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	assert(t == BACKEND_TYPE_NORMAL ||
11317c478bd9Sstevel@tonic-gate 	    t == BACKEND_TYPE_NONPERSIST);
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	be = bes[t];
11347c478bd9Sstevel@tonic-gate 	if (t == BACKEND_TYPE_NORMAL)
11357c478bd9Sstevel@tonic-gate 		assert(be != NULL);		/* should always be there */
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	if (be == NULL)
11387c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_BACKEND_ACCESS);
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	if (backend_panic_thread != 0)
11417c478bd9Sstevel@tonic-gate 		backend_panic(NULL);		/* don't proceed */
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	ts = gethrtime();
11447c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
11457c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&be->be_lock);
11467c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS_WR(be, writing, bt_lock, ts, vts);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	if (backend_panic_thread != 0) {
11497c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&be->be_lock);
11507c478bd9Sstevel@tonic-gate 		backend_panic(NULL);		/* don't proceed */
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 	be->be_thread = pthread_self();
11537c478bd9Sstevel@tonic-gate 
11548918dff3Sjwadams 	if (be->be_readonly) {
11557c478bd9Sstevel@tonic-gate 		int r;
11567c478bd9Sstevel@tonic-gate 		assert(t == BACKEND_TYPE_NORMAL);
11577c478bd9Sstevel@tonic-gate 
11588918dff3Sjwadams 		r = backend_check_readonly(be, writing, ts);
11598918dff3Sjwadams 		if (r != REP_PROTOCOL_SUCCESS) {
11608918dff3Sjwadams 			be->be_thread = 0;
11618918dff3Sjwadams 			(void) pthread_mutex_unlock(&be->be_lock);
11628918dff3Sjwadams 			return (r);
11637c478bd9Sstevel@tonic-gate 		}
11648918dff3Sjwadams 	}
11658918dff3Sjwadams 
11667c478bd9Sstevel@tonic-gate 	if (backend_do_trace)
11677c478bd9Sstevel@tonic-gate 		(void) sqlite_trace(be->be_db, backend_trace_sql, be);
11687c478bd9Sstevel@tonic-gate 	else
11697c478bd9Sstevel@tonic-gate 		(void) sqlite_trace(be->be_db, NULL, NULL);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	be->be_writing = writing;
11727c478bd9Sstevel@tonic-gate 	*bep = be;
11737c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate static void
11777c478bd9Sstevel@tonic-gate backend_unlock(sqlite_backend_t *be)
11787c478bd9Sstevel@tonic-gate {
11797c478bd9Sstevel@tonic-gate 	be->be_writing = 0;
11807c478bd9Sstevel@tonic-gate 	be->be_thread = 0;
11817c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&be->be_lock);
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate static void
11857c478bd9Sstevel@tonic-gate backend_destroy(sqlite_backend_t *be)
11867c478bd9Sstevel@tonic-gate {
11877c478bd9Sstevel@tonic-gate 	if (be->be_db != NULL) {
11887c478bd9Sstevel@tonic-gate 		sqlite_close(be->be_db);
11897c478bd9Sstevel@tonic-gate 		be->be_db = NULL;
11907c478bd9Sstevel@tonic-gate 	}
11917c478bd9Sstevel@tonic-gate 	be->be_thread = 0;
11927c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&be->be_lock);
11937c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&be->be_lock);
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate static void
11977c478bd9Sstevel@tonic-gate backend_create_finish(backend_type_t backend_id, sqlite_backend_t *be)
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&be->be_lock));
12007c478bd9Sstevel@tonic-gate 	assert(be == &be_info[backend_id]);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	bes[backend_id] = be;
12037c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&be->be_lock);
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate static int
12077c478bd9Sstevel@tonic-gate backend_fd_write(int fd, const char *mess)
12087c478bd9Sstevel@tonic-gate {
12097c478bd9Sstevel@tonic-gate 	int len = strlen(mess);
12107c478bd9Sstevel@tonic-gate 	int written;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	while (len > 0) {
12137c478bd9Sstevel@tonic-gate 		if ((written = write(fd, mess, len)) < 0)
12147c478bd9Sstevel@tonic-gate 			return (-1);
12157c478bd9Sstevel@tonic-gate 		mess += written;
12167c478bd9Sstevel@tonic-gate 		len -= written;
12177c478bd9Sstevel@tonic-gate 	}
12187c478bd9Sstevel@tonic-gate 	return (0);
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate /*
12227c478bd9Sstevel@tonic-gate  * Can return:
12237c478bd9Sstevel@tonic-gate  *	_BAD_REQUEST		name is not valid
12247c478bd9Sstevel@tonic-gate  *	_TRUNCATED		name is too long for current repository path
12257c478bd9Sstevel@tonic-gate  *	_UNKNOWN		failed for unknown reason (details written to
12267c478bd9Sstevel@tonic-gate  *				console)
12277c478bd9Sstevel@tonic-gate  *	_BACKEND_READONLY	backend is not writable
1228c0889d7aSstevep  *	_NO_RESOURCES		out of memory
12297c478bd9Sstevel@tonic-gate  *	_SUCCESS		Backup completed successfully.
12307c478bd9Sstevel@tonic-gate  */
12317c478bd9Sstevel@tonic-gate rep_protocol_responseid_t
12327c478bd9Sstevel@tonic-gate backend_create_backup(const char *name)
12337c478bd9Sstevel@tonic-gate {
12347c478bd9Sstevel@tonic-gate 	rep_protocol_responseid_t result;
12357c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	result = backend_lock(BACKEND_TYPE_NORMAL, 0, &be);
1238c0889d7aSstevep 	assert(result == REP_PROTOCOL_SUCCESS);
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	result = backend_create_backup_locked(be, name);
12417c478bd9Sstevel@tonic-gate 	backend_unlock(be);
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	return (result);
12447c478bd9Sstevel@tonic-gate }
12457c478bd9Sstevel@tonic-gate 
1246c0889d7aSstevep /*
1247c0889d7aSstevep  * Copy the repository.  If the sw_back flag is not set, we are
1248c0889d7aSstevep  * copying the repository from the default location under /etc/svc to
1249c0889d7aSstevep  * the tmpfs /etc/svc/volatile location.  If the flag is set, we are
1250c0889d7aSstevep  * copying back to the /etc/svc location from the volatile location
1251c0889d7aSstevep  * after manifest-import is completed.
1252c0889d7aSstevep  *
1253c0889d7aSstevep  * Can return:
1254c0889d7aSstevep  *
1255c0889d7aSstevep  *	REP_PROTOCOL_SUCCESS		successful copy and rename
1256c0889d7aSstevep  *	REP_PROTOCOL_FAIL_UNKNOWN	file operation error
1257c0889d7aSstevep  *	REP_PROTOCOL_FAIL_NO_RESOURCES	out of memory
1258c0889d7aSstevep  */
1259c0889d7aSstevep static rep_protocol_responseid_t
1260c0889d7aSstevep backend_switch_copy(const char *src, const char *dst, int sw_back)
1261c0889d7aSstevep {
1262c0889d7aSstevep 	int srcfd, dstfd;
1263c0889d7aSstevep 	char *tmppath = malloc(PATH_MAX);
1264c0889d7aSstevep 	rep_protocol_responseid_t res = REP_PROTOCOL_SUCCESS;
1265c0889d7aSstevep 	struct stat s_buf;
1266c0889d7aSstevep 	size_t cpsz, sz;
1267c0889d7aSstevep 
1268c0889d7aSstevep 	if (tmppath == NULL) {
1269c0889d7aSstevep 		res = REP_PROTOCOL_FAIL_NO_RESOURCES;
1270c0889d7aSstevep 		goto out;
1271c0889d7aSstevep 	}
1272c0889d7aSstevep 
1273c0889d7aSstevep 	/*
1274c0889d7aSstevep 	 * Create and open the related db files
1275c0889d7aSstevep 	 */
1276c0889d7aSstevep 	(void) strlcpy(tmppath, dst, PATH_MAX);
1277c0889d7aSstevep 	sz = strlcat(tmppath, "-XXXXXX", PATH_MAX);
1278c0889d7aSstevep 	assert(sz < PATH_MAX);
1279c0889d7aSstevep 	if (sz >= PATH_MAX) {
1280c0889d7aSstevep 		configd_critical(
1281c0889d7aSstevep 		    "Backend copy failed: strlcat %s: overflow\n", tmppath);
1282c0889d7aSstevep 		abort();
1283c0889d7aSstevep 	}
1284c0889d7aSstevep 
1285c0889d7aSstevep 	if ((dstfd = mkstemp(tmppath)) < 0) {
1286c0889d7aSstevep 		configd_critical("Backend copy failed: mkstemp %s: %s\n",
1287c0889d7aSstevep 		    tmppath, strerror(errno));
1288c0889d7aSstevep 		res = REP_PROTOCOL_FAIL_UNKNOWN;
1289c0889d7aSstevep 		goto out;
1290c0889d7aSstevep 	}
1291c0889d7aSstevep 
1292c0889d7aSstevep 	if ((srcfd = open(src, O_RDONLY)) < 0) {
1293c0889d7aSstevep 		configd_critical("Backend copy failed: opening %s: %s\n",
1294c0889d7aSstevep 		    src, strerror(errno));
1295c0889d7aSstevep 		res = REP_PROTOCOL_FAIL_UNKNOWN;
1296c0889d7aSstevep 		goto errexit;
1297c0889d7aSstevep 	}
1298c0889d7aSstevep 
1299c0889d7aSstevep 	/*
1300c0889d7aSstevep 	 * fstat the backend before copy for sanity check.
1301c0889d7aSstevep 	 */
1302c0889d7aSstevep 	if (fstat(srcfd, &s_buf) < 0) {
1303c0889d7aSstevep 		configd_critical("Backend copy failed: fstat %s: %s\n",
1304c0889d7aSstevep 		    src, strerror(errno));
1305c0889d7aSstevep 		res = REP_PROTOCOL_FAIL_UNKNOWN;
1306c0889d7aSstevep 		goto errexit;
1307c0889d7aSstevep 	}
1308c0889d7aSstevep 
1309c0889d7aSstevep 	if ((res = backend_do_copy(src, srcfd, dst, dstfd, &cpsz)) !=
1310c0889d7aSstevep 	    REP_PROTOCOL_SUCCESS)
1311c0889d7aSstevep 		goto errexit;
1312c0889d7aSstevep 
1313c0889d7aSstevep 	if (cpsz != s_buf.st_size) {
1314c0889d7aSstevep 		configd_critical("Backend copy failed: incomplete copy\n");
1315c0889d7aSstevep 		res = REP_PROTOCOL_FAIL_UNKNOWN;
1316c0889d7aSstevep 		goto errexit;
1317c0889d7aSstevep 	}
1318c0889d7aSstevep 
1319c0889d7aSstevep 	/*
1320c0889d7aSstevep 	 * Rename tmppath to dst
1321c0889d7aSstevep 	 */
1322c0889d7aSstevep 	if (rename(tmppath, dst) < 0) {
1323c0889d7aSstevep 		configd_critical(
1324c0889d7aSstevep 		    "Backend copy failed: rename %s to %s: %s\n",
1325c0889d7aSstevep 		    tmppath, dst, strerror(errno));
1326c0889d7aSstevep 		res = REP_PROTOCOL_FAIL_UNKNOWN;
1327c0889d7aSstevep 	}
1328c0889d7aSstevep 
1329c0889d7aSstevep errexit:
1330c0889d7aSstevep 	if (res != REP_PROTOCOL_SUCCESS && unlink(tmppath) < 0)
1331c0889d7aSstevep 		configd_critical(
1332c0889d7aSstevep 		    "Backend copy failed: remove %s: %s\n",
1333c0889d7aSstevep 		    tmppath, strerror(errno));
1334c0889d7aSstevep 
1335c0889d7aSstevep 	(void) close(srcfd);
1336c0889d7aSstevep 	(void) close(dstfd);
1337c0889d7aSstevep 
1338c0889d7aSstevep out:
1339c0889d7aSstevep 	free(tmppath);
1340c0889d7aSstevep 	if (sw_back) {
1341c0889d7aSstevep 		if (unlink(src) < 0)
1342c0889d7aSstevep 			configd_critical(
1343c0889d7aSstevep 			    "Backend copy failed: remove %s: %s\n",
1344c0889d7aSstevep 			    src, strerror(errno));
1345c0889d7aSstevep 	}
1346c0889d7aSstevep 
1347c0889d7aSstevep 	return (res);
1348c0889d7aSstevep }
1349c0889d7aSstevep 
1350c0889d7aSstevep /*
1351c0889d7aSstevep  * Perform sanity check on the repository.
1352c0889d7aSstevep  * Return 0 if check succeeds or -1 if fails.
1353c0889d7aSstevep  */
1354c0889d7aSstevep static int
1355c0889d7aSstevep backend_switch_check(struct sqlite *be_db, char **errp)
1356c0889d7aSstevep {
1357c0889d7aSstevep 	struct run_single_int_info info;
1358c0889d7aSstevep 	uint32_t val = -1UL;
1359c0889d7aSstevep 	int r;
1360c0889d7aSstevep 
1361c0889d7aSstevep 	info.rs_out = &val;
1362c0889d7aSstevep 	info.rs_result = REP_PROTOCOL_FAIL_NOT_FOUND;
1363c0889d7aSstevep 
1364c0889d7aSstevep 	r = sqlite_exec(be_db,
1365c0889d7aSstevep 	    "SELECT schema_version FROM schema_version;",
1366c0889d7aSstevep 	    run_single_int_callback, &info, errp);
1367c0889d7aSstevep 
1368c0889d7aSstevep 	if (r == SQLITE_OK &&
1369c0889d7aSstevep 	    info.rs_result != REP_PROTOCOL_FAIL_NOT_FOUND &&
1370c0889d7aSstevep 	    val == BACKEND_SCHEMA_VERSION)
1371c0889d7aSstevep 		return (0);
1372c0889d7aSstevep 	else
1373c0889d7aSstevep 		return (-1);
1374c0889d7aSstevep }
1375c0889d7aSstevep 
1376c0889d7aSstevep /*
1377c0889d7aSstevep  * Backend switch entry point.  It is called to perform the backend copy and
1378c0889d7aSstevep  * switch from src to dst.  First, it blocks all other clients from accessing
1379c0889d7aSstevep  * the repository by calling backend_lock to lock the repository.  Upon
1380c0889d7aSstevep  * successful lock, copying and switching of the repository are performed.
1381c0889d7aSstevep  *
1382c0889d7aSstevep  * Can return:
1383c0889d7aSstevep  *	REP_PROTOCOL_SUCCESS			successful switch
1384c0889d7aSstevep  *	REP_PROTOCOL_FAIL_BACKEND_ACCESS	backen access fails
1385c0889d7aSstevep  *	REP_PROTOCOL_FAIL_BACKEND_READONLY	backend is not writable
1386c0889d7aSstevep  *	REP_PROTOCOL_FAIL_UNKNOWN		file operation error
1387c0889d7aSstevep  *	REP_PROTOCOL_FAIL_NO_RESOURCES		out of memory
1388c0889d7aSstevep  */
1389c0889d7aSstevep rep_protocol_responseid_t
1390c0889d7aSstevep backend_switch(int sw_back)
1391c0889d7aSstevep {
1392c0889d7aSstevep 	rep_protocol_responseid_t result;
1393c0889d7aSstevep 	sqlite_backend_t *be;
1394c0889d7aSstevep 	struct sqlite *new;
1395c0889d7aSstevep 	char *errp;
1396c0889d7aSstevep 	const char *dst;
1397c0889d7aSstevep 
1398c0889d7aSstevep 	result = backend_lock(BACKEND_TYPE_NORMAL, 1, &be);
1399c0889d7aSstevep 	if (result != REP_PROTOCOL_SUCCESS)
1400c0889d7aSstevep 		return (result);
1401c0889d7aSstevep 
1402c0889d7aSstevep 	if (sw_back) {
1403c0889d7aSstevep 		dst = REPOSITORY_DB;
1404c0889d7aSstevep 	} else {
1405c0889d7aSstevep 		dst = FAST_REPOSITORY_DB;
1406c0889d7aSstevep 	}
1407c0889d7aSstevep 
1408c0889d7aSstevep 	/*
1409c0889d7aSstevep 	 * Do the actual copy and rename
1410c0889d7aSstevep 	 */
1411c0889d7aSstevep 	result = backend_switch_copy(be->be_path, dst, sw_back);
1412c0889d7aSstevep 	if (result != REP_PROTOCOL_SUCCESS) {
1413c0889d7aSstevep 		goto errout;
1414c0889d7aSstevep 	}
1415c0889d7aSstevep 
1416c0889d7aSstevep 	/*
1417c0889d7aSstevep 	 * Do the backend sanity check and switch
1418c0889d7aSstevep 	 */
1419c0889d7aSstevep 	new = sqlite_open(dst, 0600, &errp);
1420c0889d7aSstevep 	if (new != NULL) {
1421c0889d7aSstevep 		/*
1422c0889d7aSstevep 		 * Sanity check
1423c0889d7aSstevep 		 */
1424c0889d7aSstevep 		if (backend_switch_check(new, &errp) == 0) {
1425c0889d7aSstevep 			free((char *)be->be_path);
1426c0889d7aSstevep 			be->be_path = strdup(dst);
1427c0889d7aSstevep 			if (be->be_path == NULL) {
1428c0889d7aSstevep 				configd_critical(
1429c0889d7aSstevep 				    "Backend switch failed: strdup %s: %s\n",
1430c0889d7aSstevep 				    dst, strerror(errno));
1431c0889d7aSstevep 				result = REP_PROTOCOL_FAIL_NO_RESOURCES;
1432c0889d7aSstevep 				sqlite_close(new);
1433c0889d7aSstevep 			} else {
1434c0889d7aSstevep 				sqlite_close(be->be_db);
1435c0889d7aSstevep 				be->be_db = new;
1436c0889d7aSstevep 			}
1437c0889d7aSstevep 		} else {
1438c0889d7aSstevep 			configd_critical(
1439c0889d7aSstevep 			    "Backend switch failed: integrity check %s: %s\n",
1440c0889d7aSstevep 			    dst, errp);
1441c0889d7aSstevep 			result = REP_PROTOCOL_FAIL_BACKEND_ACCESS;
1442c0889d7aSstevep 		}
1443c0889d7aSstevep 	} else {
1444c0889d7aSstevep 		configd_critical("Backend switch failed: sqlite_open %s: %s\n",
1445c0889d7aSstevep 		    dst, errp);
1446c0889d7aSstevep 		result = REP_PROTOCOL_FAIL_BACKEND_ACCESS;
1447c0889d7aSstevep 	}
1448c0889d7aSstevep 
1449c0889d7aSstevep errout:
1450c0889d7aSstevep 	backend_unlock(be);
1451c0889d7aSstevep 	return (result);
1452c0889d7aSstevep }
1453c0889d7aSstevep 
1454c0889d7aSstevep /*
1455c0889d7aSstevep  * This routine is called to attempt the recovery of
1456c0889d7aSstevep  * the most recent valid repository if possible when configd
1457c0889d7aSstevep  * is restarted for some reasons or when system crashes
1458c0889d7aSstevep  * during the switch operation.  The repository databases
1459c0889d7aSstevep  * referenced here are indicators of successful switch
1460c0889d7aSstevep  * operations.
1461c0889d7aSstevep  */
1462c0889d7aSstevep static void
1463c0889d7aSstevep backend_switch_recovery(void)
1464c0889d7aSstevep {
1465c0889d7aSstevep 	const char *fast_db = FAST_REPOSITORY_DB;
1466c0889d7aSstevep 	char *errp;
1467c0889d7aSstevep 	struct stat s_buf;
1468c0889d7aSstevep 	struct sqlite *be_db;
1469c0889d7aSstevep 
1470c0889d7aSstevep 
1471c0889d7aSstevep 	/*
1472c0889d7aSstevep 	 * A good transient db containing most recent data can
1473c0889d7aSstevep 	 * exist if system or svc.configd crashes during the
1474c0889d7aSstevep 	 * switch operation.  If that is the case, check its
1475c0889d7aSstevep 	 * integrity and use it.
1476c0889d7aSstevep 	 */
1477c0889d7aSstevep 	if (stat(fast_db, &s_buf) < 0) {
1478c0889d7aSstevep 		return;
1479c0889d7aSstevep 	}
1480c0889d7aSstevep 
1481c0889d7aSstevep 	/*
1482c0889d7aSstevep 	 * Do sanity check on the db
1483c0889d7aSstevep 	 */
1484c0889d7aSstevep 	be_db = sqlite_open(fast_db, 0600, &errp);
1485c0889d7aSstevep 
1486c0889d7aSstevep 	if (be_db != NULL) {
1487c0889d7aSstevep 		if (backend_switch_check(be_db, &errp) == 0)
1488c0889d7aSstevep 			(void) backend_switch_copy(fast_db, REPOSITORY_DB, 1);
1489c0889d7aSstevep 	}
1490c0889d7aSstevep 
1491c0889d7aSstevep 	(void) unlink(fast_db);
1492c0889d7aSstevep }
1493c0889d7aSstevep 
14947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14957c478bd9Sstevel@tonic-gate static int
14967c478bd9Sstevel@tonic-gate backend_integrity_callback(void *private, int narg, char **vals, char **cols)
14977c478bd9Sstevel@tonic-gate {
14987c478bd9Sstevel@tonic-gate 	char **out = private;
14997c478bd9Sstevel@tonic-gate 	char *old = *out;
15007c478bd9Sstevel@tonic-gate 	char *new;
15017c478bd9Sstevel@tonic-gate 	const char *info;
15027c478bd9Sstevel@tonic-gate 	size_t len;
15037c478bd9Sstevel@tonic-gate 	int x;
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	for (x = 0; x < narg; x++) {
15067c478bd9Sstevel@tonic-gate 		if ((info = vals[x]) != NULL &&
15077c478bd9Sstevel@tonic-gate 		    strcmp(info, "ok") != 0) {
15087c478bd9Sstevel@tonic-gate 			len = (old == NULL)? 0 : strlen(old);
15097c478bd9Sstevel@tonic-gate 			len += strlen(info) + 2;	/* '\n' + '\0' */
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 			new = realloc(old, len);
15127c478bd9Sstevel@tonic-gate 			if (new == NULL)
15137c478bd9Sstevel@tonic-gate 				return (BACKEND_CALLBACK_ABORT);
15147c478bd9Sstevel@tonic-gate 			if (old == NULL)
15157c478bd9Sstevel@tonic-gate 				new[0] = 0;
15167c478bd9Sstevel@tonic-gate 			old = *out = new;
15177c478bd9Sstevel@tonic-gate 			(void) strlcat(new, info, len);
15187c478bd9Sstevel@tonic-gate 			(void) strlcat(new, "\n", len);
15197c478bd9Sstevel@tonic-gate 		}
15207c478bd9Sstevel@tonic-gate 	}
15217c478bd9Sstevel@tonic-gate 	return (BACKEND_CALLBACK_CONTINUE);
15227c478bd9Sstevel@tonic-gate }
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate #define	BACKEND_CREATE_LOCKED		-2
15257c478bd9Sstevel@tonic-gate #define	BACKEND_CREATE_FAIL		-1
15267c478bd9Sstevel@tonic-gate #define	BACKEND_CREATE_SUCCESS		0
15277c478bd9Sstevel@tonic-gate #define	BACKEND_CREATE_READONLY		1
15287c478bd9Sstevel@tonic-gate #define	BACKEND_CREATE_NEED_INIT	2
15297c478bd9Sstevel@tonic-gate static int
15307c478bd9Sstevel@tonic-gate backend_create(backend_type_t backend_id, const char *db_file,
15317c478bd9Sstevel@tonic-gate     sqlite_backend_t **bep)
15327c478bd9Sstevel@tonic-gate {
15337c478bd9Sstevel@tonic-gate 	char *errp;
15347c478bd9Sstevel@tonic-gate 	char *integrity_results = NULL;
15357c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
15367c478bd9Sstevel@tonic-gate 	int r;
15377c478bd9Sstevel@tonic-gate 	uint32_t val = -1UL;
15387c478bd9Sstevel@tonic-gate 	struct run_single_int_info info;
15397c478bd9Sstevel@tonic-gate 	int fd;
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	assert(backend_id >= 0 && backend_id < BACKEND_TYPE_TOTAL);
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	be = &be_info[backend_id];
1544*6e1d2b42Samaguire 
15457c478bd9Sstevel@tonic-gate 	assert(be->be_db == NULL);
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&be->be_lock, NULL);
15487c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&be->be_lock);
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	be->be_type = backend_id;
15517c478bd9Sstevel@tonic-gate 	be->be_path = strdup(db_file);
15527c478bd9Sstevel@tonic-gate 	if (be->be_path == NULL) {
15537c478bd9Sstevel@tonic-gate 		perror("malloc");
15547c478bd9Sstevel@tonic-gate 		goto fail;
15557c478bd9Sstevel@tonic-gate 	}
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	be->be_db = sqlite_open(be->be_path, 0600, &errp);
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	if (be->be_db == NULL) {
15607c478bd9Sstevel@tonic-gate 		if (strstr(errp, "out of memory") != NULL) {
15617c478bd9Sstevel@tonic-gate 			configd_critical("%s: %s\n", db_file, errp);
15627c478bd9Sstevel@tonic-gate 			free(errp);
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 			goto fail;
15657c478bd9Sstevel@tonic-gate 		}
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 		/* report it as an integrity failure */
15687c478bd9Sstevel@tonic-gate 		integrity_results = errp;
15697c478bd9Sstevel@tonic-gate 		errp = NULL;
15707c478bd9Sstevel@tonic-gate 		goto integrity_fail;
15717c478bd9Sstevel@tonic-gate 	}
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	/*
15747c478bd9Sstevel@tonic-gate 	 * check if we are inited and of the correct schema version
15757c478bd9Sstevel@tonic-gate 	 *
15767c478bd9Sstevel@tonic-gate 	 */
15777c478bd9Sstevel@tonic-gate 	info.rs_out = &val;
15787c478bd9Sstevel@tonic-gate 	info.rs_result = REP_PROTOCOL_FAIL_NOT_FOUND;
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	r = sqlite_exec(be->be_db, "SELECT schema_version FROM schema_version;",
15817c478bd9Sstevel@tonic-gate 	    run_single_int_callback, &info, &errp);
15827c478bd9Sstevel@tonic-gate 	if (r == SQLITE_ERROR &&
15837c478bd9Sstevel@tonic-gate 	    strcmp("no such table: schema_version", errp) == 0) {
15847c478bd9Sstevel@tonic-gate 		free(errp);
15857c478bd9Sstevel@tonic-gate 		/*
15867c478bd9Sstevel@tonic-gate 		 * Could be an empty repository, could be pre-schema_version
15877c478bd9Sstevel@tonic-gate 		 * schema.  Check for id_tbl, which has always been there.
15887c478bd9Sstevel@tonic-gate 		 */
15897c478bd9Sstevel@tonic-gate 		r = sqlite_exec(be->be_db, "SELECT count() FROM id_tbl;",
15907c478bd9Sstevel@tonic-gate 		    NULL, NULL, &errp);
15917c478bd9Sstevel@tonic-gate 		if (r == SQLITE_ERROR &&
15927c478bd9Sstevel@tonic-gate 		    strcmp("no such table: id_tbl", errp) == 0) {
15937c478bd9Sstevel@tonic-gate 			free(errp);
15947c478bd9Sstevel@tonic-gate 			*bep = be;
15957c478bd9Sstevel@tonic-gate 			return (BACKEND_CREATE_NEED_INIT);
15967c478bd9Sstevel@tonic-gate 		}
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 		configd_critical("%s: schema version mismatch\n", db_file);
15997c478bd9Sstevel@tonic-gate 		goto fail;
16007c478bd9Sstevel@tonic-gate 	}
16017c478bd9Sstevel@tonic-gate 	if (r == SQLITE_BUSY || r == SQLITE_LOCKED) {
16027c478bd9Sstevel@tonic-gate 		free(errp);
16037c478bd9Sstevel@tonic-gate 		*bep = NULL;
16047c478bd9Sstevel@tonic-gate 		backend_destroy(be);
16057c478bd9Sstevel@tonic-gate 		return (BACKEND_CREATE_LOCKED);
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate 	if (r == SQLITE_OK) {
16087c478bd9Sstevel@tonic-gate 		if (info.rs_result == REP_PROTOCOL_FAIL_NOT_FOUND ||
16097c478bd9Sstevel@tonic-gate 		    val != BACKEND_SCHEMA_VERSION) {
16107c478bd9Sstevel@tonic-gate 			configd_critical("%s: schema version mismatch\n",
16117c478bd9Sstevel@tonic-gate 			    db_file);
16127c478bd9Sstevel@tonic-gate 			goto fail;
16137c478bd9Sstevel@tonic-gate 		}
16147c478bd9Sstevel@tonic-gate 	}
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	/*
16177c478bd9Sstevel@tonic-gate 	 * pull in the whole database sequentially.
16187c478bd9Sstevel@tonic-gate 	 */
16197c478bd9Sstevel@tonic-gate 	if ((fd = open(db_file, O_RDONLY)) >= 0) {
16207c478bd9Sstevel@tonic-gate 		size_t sz = 64 * 1024;
16217c478bd9Sstevel@tonic-gate 		char *buffer = malloc(sz);
16227c478bd9Sstevel@tonic-gate 		if (buffer != NULL) {
16237c478bd9Sstevel@tonic-gate 			while (read(fd, buffer, sz) > 0)
16247c478bd9Sstevel@tonic-gate 				;
16257c478bd9Sstevel@tonic-gate 			free(buffer);
16267c478bd9Sstevel@tonic-gate 		}
16277c478bd9Sstevel@tonic-gate 		(void) close(fd);
16287c478bd9Sstevel@tonic-gate 	}
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	/*
16317c478bd9Sstevel@tonic-gate 	 * run an integrity check
16327c478bd9Sstevel@tonic-gate 	 */
16337c478bd9Sstevel@tonic-gate 	r = sqlite_exec(be->be_db, "PRAGMA integrity_check;",
16347c478bd9Sstevel@tonic-gate 	    backend_integrity_callback, &integrity_results, &errp);
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	if (r == SQLITE_BUSY || r == SQLITE_LOCKED) {
16377c478bd9Sstevel@tonic-gate 		free(errp);
16387c478bd9Sstevel@tonic-gate 		*bep = NULL;
16397c478bd9Sstevel@tonic-gate 		backend_destroy(be);
16407c478bd9Sstevel@tonic-gate 		return (BACKEND_CREATE_LOCKED);
16417c478bd9Sstevel@tonic-gate 	}
16427c478bd9Sstevel@tonic-gate 	if (r == SQLITE_ABORT) {
16437c478bd9Sstevel@tonic-gate 		free(errp);
16447c478bd9Sstevel@tonic-gate 		errp = NULL;
16457c478bd9Sstevel@tonic-gate 		integrity_results = "out of memory running integrity check\n";
16467c478bd9Sstevel@tonic-gate 	} else if (r != SQLITE_OK && integrity_results == NULL) {
16477c478bd9Sstevel@tonic-gate 		integrity_results = errp;
16487c478bd9Sstevel@tonic-gate 		errp = NULL;
16497c478bd9Sstevel@tonic-gate 	}
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate integrity_fail:
16527c478bd9Sstevel@tonic-gate 	if (integrity_results != NULL) {
16537c478bd9Sstevel@tonic-gate 		const char *fname = "/etc/svc/volatile/db_errors";
16547c478bd9Sstevel@tonic-gate 		if ((fd = open(fname, O_CREAT|O_WRONLY|O_APPEND, 0600)) < 0) {
16557c478bd9Sstevel@tonic-gate 			fname = NULL;
16567c478bd9Sstevel@tonic-gate 		} else {
16577c478bd9Sstevel@tonic-gate 			if (backend_fd_write(fd, "\n\n") < 0 ||
16587c478bd9Sstevel@tonic-gate 			    backend_fd_write(fd, db_file) < 0 ||
16597c478bd9Sstevel@tonic-gate 			    backend_fd_write(fd,
16607c478bd9Sstevel@tonic-gate 			    ": PRAGMA integrity_check; failed.  Results:\n") <
16617c478bd9Sstevel@tonic-gate 			    0 || backend_fd_write(fd, integrity_results) < 0 ||
16627c478bd9Sstevel@tonic-gate 			    backend_fd_write(fd, "\n\n") < 0) {
16637c478bd9Sstevel@tonic-gate 				fname = NULL;
16647c478bd9Sstevel@tonic-gate 			}
16657c478bd9Sstevel@tonic-gate 			(void) close(fd);
16667c478bd9Sstevel@tonic-gate 		}
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 		if (!is_main_repository ||
16697c478bd9Sstevel@tonic-gate 		    backend_id == BACKEND_TYPE_NONPERSIST) {
16707c478bd9Sstevel@tonic-gate 			if (fname != NULL)
16717c478bd9Sstevel@tonic-gate 				configd_critical(
16727c478bd9Sstevel@tonic-gate 				    "%s: integrity check failed. Details in "
16737c478bd9Sstevel@tonic-gate 				    "%s\n", db_file, fname);
16747c478bd9Sstevel@tonic-gate 			else
16757c478bd9Sstevel@tonic-gate 				configd_critical(
1676db11989eSpjung 				    "%s: integrity check failed.\n",
16777c478bd9Sstevel@tonic-gate 				    db_file);
16787c478bd9Sstevel@tonic-gate 		} else {
16797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
16807c478bd9Sstevel@tonic-gate "\n"
16817c478bd9Sstevel@tonic-gate "svc.configd: smf(5) database integrity check of:\n"
16827c478bd9Sstevel@tonic-gate "\n"
16837c478bd9Sstevel@tonic-gate "    %s\n"
16847c478bd9Sstevel@tonic-gate "\n"
16857c478bd9Sstevel@tonic-gate "  failed. The database might be damaged or a media error might have\n"
16867c478bd9Sstevel@tonic-gate "  prevented it from being verified.  Additional information useful to\n"
16877c478bd9Sstevel@tonic-gate "  your service provider%s%s\n"
16887c478bd9Sstevel@tonic-gate "\n"
16897c478bd9Sstevel@tonic-gate "  The system will not be able to boot until you have restored a working\n"
16907c478bd9Sstevel@tonic-gate "  database.  svc.startd(1M) will provide a sulogin(1M) prompt for recovery\n"
16917c478bd9Sstevel@tonic-gate "  purposes.  The command:\n"
16927c478bd9Sstevel@tonic-gate "\n"
16937c478bd9Sstevel@tonic-gate "    /lib/svc/bin/restore_repository\n"
16947c478bd9Sstevel@tonic-gate "\n"
16957c478bd9Sstevel@tonic-gate "  can be run to restore a backup version of your repository.  See\n"
16967c478bd9Sstevel@tonic-gate "  http://sun.com/msg/SMF-8000-MY for more information.\n"
16977c478bd9Sstevel@tonic-gate "\n",
16987c478bd9Sstevel@tonic-gate 			    db_file,
16997c478bd9Sstevel@tonic-gate 			    (fname == NULL)? ":\n\n" : " is in:\n\n    ",
17007c478bd9Sstevel@tonic-gate 			    (fname == NULL)? integrity_results : fname);
17017c478bd9Sstevel@tonic-gate 		}
17027c478bd9Sstevel@tonic-gate 		free(errp);
17037c478bd9Sstevel@tonic-gate 		goto fail;
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	/*
1707*6e1d2b42Samaguire 	 * Simply do check if backend has been upgraded.  We do not wish
1708*6e1d2b42Samaguire 	 * to actually carry out upgrade here - the main repository may
1709*6e1d2b42Samaguire 	 * not be writable at this point.  Actual upgrade is carried out
1710*6e1d2b42Samaguire 	 * via backend_check_readonly().  This check is done so that
1711*6e1d2b42Samaguire 	 * we determine repository state - upgraded or not - and then
1712*6e1d2b42Samaguire 	 * the appropriate SELECT statement (value-ordered or not)
1713*6e1d2b42Samaguire 	 * can be used when retrieving property values early in boot.
1714*6e1d2b42Samaguire 	 */
1715*6e1d2b42Samaguire 	if (backend_id == BACKEND_TYPE_NORMAL)
1716*6e1d2b42Samaguire 		backend_check_upgrade(be, B_FALSE);
1717*6e1d2b42Samaguire 	/*
17187c478bd9Sstevel@tonic-gate 	 * check if we are writable
17197c478bd9Sstevel@tonic-gate 	 */
17208918dff3Sjwadams 	r = backend_is_readonly(be->be_db, be->be_path);
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	if (r == SQLITE_BUSY || r == SQLITE_LOCKED) {
17237c478bd9Sstevel@tonic-gate 		free(errp);
17247c478bd9Sstevel@tonic-gate 		*bep = NULL;
17257c478bd9Sstevel@tonic-gate 		backend_destroy(be);
17267c478bd9Sstevel@tonic-gate 		return (BACKEND_CREATE_LOCKED);
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 	if (r != SQLITE_OK && r != SQLITE_FULL) {
17297c478bd9Sstevel@tonic-gate 		free(errp);
17307c478bd9Sstevel@tonic-gate 		be->be_readonly = 1;
17317c478bd9Sstevel@tonic-gate 		*bep = be;
17327c478bd9Sstevel@tonic-gate 		return (BACKEND_CREATE_READONLY);
17337c478bd9Sstevel@tonic-gate 	}
1734*6e1d2b42Samaguire 
17357c478bd9Sstevel@tonic-gate 	*bep = be;
17367c478bd9Sstevel@tonic-gate 	return (BACKEND_CREATE_SUCCESS);
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate fail:
17397c478bd9Sstevel@tonic-gate 	*bep = NULL;
17407c478bd9Sstevel@tonic-gate 	backend_destroy(be);
17417c478bd9Sstevel@tonic-gate 	return (BACKEND_CREATE_FAIL);
17427c478bd9Sstevel@tonic-gate }
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate /*
17457c478bd9Sstevel@tonic-gate  * (arg & -arg) is, through the magic of twos-complement arithmetic, the
17467c478bd9Sstevel@tonic-gate  * lowest set bit in arg.
17477c478bd9Sstevel@tonic-gate  */
17487c478bd9Sstevel@tonic-gate static size_t
17497c478bd9Sstevel@tonic-gate round_up_to_p2(size_t arg)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	/*
17527c478bd9Sstevel@tonic-gate 	 * Don't allow a zero result.
17537c478bd9Sstevel@tonic-gate 	 */
17547c478bd9Sstevel@tonic-gate 	assert(arg > 0 && ((ssize_t)arg > 0));
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	while ((arg & (arg - 1)) != 0)
17577c478bd9Sstevel@tonic-gate 		arg += (arg & -arg);
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	return (arg);
17607c478bd9Sstevel@tonic-gate }
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate /*
17637c478bd9Sstevel@tonic-gate  * Returns
17647c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
17657c478bd9Sstevel@tonic-gate  *   _BACKEND_ACCESS - backend type t (other than _NORMAL) doesn't exist
17667c478bd9Sstevel@tonic-gate  *   _DONE - callback aborted query
17677c478bd9Sstevel@tonic-gate  *   _SUCCESS
17687c478bd9Sstevel@tonic-gate  */
17697c478bd9Sstevel@tonic-gate int
17707c478bd9Sstevel@tonic-gate backend_run(backend_type_t t, backend_query_t *q,
17717c478bd9Sstevel@tonic-gate     backend_run_callback_f *cb, void *data)
17727c478bd9Sstevel@tonic-gate {
17737c478bd9Sstevel@tonic-gate 	char *errmsg = NULL;
17747c478bd9Sstevel@tonic-gate 	int ret;
17757c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
17767c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	if (q == NULL || q->bq_buf == NULL)
17797c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	if ((ret = backend_lock(t, 0, &be)) != REP_PROTOCOL_SUCCESS)
17827c478bd9Sstevel@tonic-gate 		return (ret);
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	ts = gethrtime();
17857c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
17867c478bd9Sstevel@tonic-gate 	ret = sqlite_exec(be->be_db, q->bq_buf, cb, data, &errmsg);
17877c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
17887c478bd9Sstevel@tonic-gate 	ret = backend_error(be, ret, errmsg);
17897c478bd9Sstevel@tonic-gate 	backend_unlock(be);
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	return (ret);
17927c478bd9Sstevel@tonic-gate }
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate /*
17957c478bd9Sstevel@tonic-gate  * Starts a "read-only" transaction -- i.e., locks out writers as long
17967c478bd9Sstevel@tonic-gate  * as it is active.
17977c478bd9Sstevel@tonic-gate  *
17987c478bd9Sstevel@tonic-gate  * Fails with
17997c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
18007c478bd9Sstevel@tonic-gate  *
18017c478bd9Sstevel@tonic-gate  * If t is not _NORMAL, can also fail with
18027c478bd9Sstevel@tonic-gate  *   _BACKEND_ACCESS - backend does not exist
18037c478bd9Sstevel@tonic-gate  *
18047c478bd9Sstevel@tonic-gate  * If writable is true, can also fail with
18057c478bd9Sstevel@tonic-gate  *   _BACKEND_READONLY
18067c478bd9Sstevel@tonic-gate  */
18077c478bd9Sstevel@tonic-gate static int
18087c478bd9Sstevel@tonic-gate backend_tx_begin_common(backend_type_t t, backend_tx_t **txp, int writable)
18097c478bd9Sstevel@tonic-gate {
18107c478bd9Sstevel@tonic-gate 	backend_tx_t *ret;
18117c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
18127c478bd9Sstevel@tonic-gate 	int r;
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	*txp = NULL;
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (*ret));
18177c478bd9Sstevel@tonic-gate 	if (ret == NULL)
18187c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	if ((r = backend_lock(t, writable, &be)) != REP_PROTOCOL_SUCCESS) {
18217c478bd9Sstevel@tonic-gate 		uu_free(ret);
18227c478bd9Sstevel@tonic-gate 		return (r);
18237c478bd9Sstevel@tonic-gate 	}
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	ret->bt_be = be;
18267c478bd9Sstevel@tonic-gate 	ret->bt_readonly = !writable;
18277c478bd9Sstevel@tonic-gate 	ret->bt_type = t;
18287c478bd9Sstevel@tonic-gate 	ret->bt_full = 0;
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	*txp = ret;
18317c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
18327c478bd9Sstevel@tonic-gate }
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate int
18357c478bd9Sstevel@tonic-gate backend_tx_begin_ro(backend_type_t t, backend_tx_t **txp)
18367c478bd9Sstevel@tonic-gate {
18377c478bd9Sstevel@tonic-gate 	return (backend_tx_begin_common(t, txp, 0));
18387c478bd9Sstevel@tonic-gate }
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate static void
18417c478bd9Sstevel@tonic-gate backend_tx_end(backend_tx_t *tx)
18427c478bd9Sstevel@tonic-gate {
18437c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	if (tx->bt_full) {
18487c478bd9Sstevel@tonic-gate 		struct sqlite *new;
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 		/*
18517c478bd9Sstevel@tonic-gate 		 * sqlite tends to be sticky with SQLITE_FULL, so we try
18527c478bd9Sstevel@tonic-gate 		 * to get a fresh database handle if we got a FULL warning
18537c478bd9Sstevel@tonic-gate 		 * along the way.  If that fails, no harm done.
18547c478bd9Sstevel@tonic-gate 		 */
18557c478bd9Sstevel@tonic-gate 		new = sqlite_open(be->be_path, 0600, NULL);
18567c478bd9Sstevel@tonic-gate 		if (new != NULL) {
18577c478bd9Sstevel@tonic-gate 			sqlite_close(be->be_db);
18587c478bd9Sstevel@tonic-gate 			be->be_db = new;
18597c478bd9Sstevel@tonic-gate 		}
18607c478bd9Sstevel@tonic-gate 	}
18617c478bd9Sstevel@tonic-gate 	backend_unlock(be);
18627c478bd9Sstevel@tonic-gate 	tx->bt_be = NULL;
18637c478bd9Sstevel@tonic-gate 	uu_free(tx);
18647c478bd9Sstevel@tonic-gate }
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate void
18677c478bd9Sstevel@tonic-gate backend_tx_end_ro(backend_tx_t *tx)
18687c478bd9Sstevel@tonic-gate {
18697c478bd9Sstevel@tonic-gate 	assert(tx->bt_readonly);
18707c478bd9Sstevel@tonic-gate 	backend_tx_end(tx);
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate /*
18747c478bd9Sstevel@tonic-gate  * Fails with
18757c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
18767c478bd9Sstevel@tonic-gate  *   _BACKEND_ACCESS
18777c478bd9Sstevel@tonic-gate  *   _BACKEND_READONLY
18787c478bd9Sstevel@tonic-gate  */
18797c478bd9Sstevel@tonic-gate int
18807c478bd9Sstevel@tonic-gate backend_tx_begin(backend_type_t t, backend_tx_t **txp)
18817c478bd9Sstevel@tonic-gate {
18827c478bd9Sstevel@tonic-gate 	int r;
18837c478bd9Sstevel@tonic-gate 	char *errmsg;
18847c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	r = backend_tx_begin_common(t, txp, 1);
18877c478bd9Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS)
18887c478bd9Sstevel@tonic-gate 		return (r);
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	ts = gethrtime();
18917c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
18927c478bd9Sstevel@tonic-gate 	r = sqlite_exec((*txp)->bt_be->be_db, "BEGIN TRANSACTION", NULL, NULL,
18937c478bd9Sstevel@tonic-gate 	    &errmsg);
18947c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS((*txp)->bt_be, bt_exec, ts, vts);
18957c478bd9Sstevel@tonic-gate 	if (r == SQLITE_FULL)
18967c478bd9Sstevel@tonic-gate 		(*txp)->bt_full = 1;
18977c478bd9Sstevel@tonic-gate 	r = backend_error((*txp)->bt_be, r, errmsg);
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS) {
19007c478bd9Sstevel@tonic-gate 		assert(r != REP_PROTOCOL_DONE);
19017c478bd9Sstevel@tonic-gate 		(void) sqlite_exec((*txp)->bt_be->be_db,
19027c478bd9Sstevel@tonic-gate 		    "ROLLBACK TRANSACTION", NULL, NULL, NULL);
19037c478bd9Sstevel@tonic-gate 		backend_tx_end(*txp);
19047c478bd9Sstevel@tonic-gate 		*txp = NULL;
19057c478bd9Sstevel@tonic-gate 		return (r);
19067c478bd9Sstevel@tonic-gate 	}
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 	(*txp)->bt_readonly = 0;
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
19117c478bd9Sstevel@tonic-gate }
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate void
19147c478bd9Sstevel@tonic-gate backend_tx_rollback(backend_tx_t *tx)
19157c478bd9Sstevel@tonic-gate {
19167c478bd9Sstevel@tonic-gate 	int r;
19177c478bd9Sstevel@tonic-gate 	char *errmsg;
19187c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
19197c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly);
19227c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	ts = gethrtime();
19257c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
19267c478bd9Sstevel@tonic-gate 	r = sqlite_exec(be->be_db, "ROLLBACK TRANSACTION", NULL, NULL,
19277c478bd9Sstevel@tonic-gate 	    &errmsg);
19287c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
19297c478bd9Sstevel@tonic-gate 	if (r == SQLITE_FULL)
19307c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
19317c478bd9Sstevel@tonic-gate 	(void) backend_error(be, r, errmsg);
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	backend_tx_end(tx);
19347c478bd9Sstevel@tonic-gate }
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate /*
19377c478bd9Sstevel@tonic-gate  * Fails with
19387c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
19397c478bd9Sstevel@tonic-gate  */
19407c478bd9Sstevel@tonic-gate int
19417c478bd9Sstevel@tonic-gate backend_tx_commit(backend_tx_t *tx)
19427c478bd9Sstevel@tonic-gate {
19437c478bd9Sstevel@tonic-gate 	int r, r2;
19447c478bd9Sstevel@tonic-gate 	char *errmsg;
19457c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
19467c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly);
19497c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
19507c478bd9Sstevel@tonic-gate 	ts = gethrtime();
19517c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
19527c478bd9Sstevel@tonic-gate 	r = sqlite_exec(be->be_db, "COMMIT TRANSACTION", NULL, NULL,
19537c478bd9Sstevel@tonic-gate 	    &errmsg);
19547c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
19557c478bd9Sstevel@tonic-gate 	if (r == SQLITE_FULL)
19567c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	r = backend_error(be, r, errmsg);
19597c478bd9Sstevel@tonic-gate 	assert(r != REP_PROTOCOL_DONE);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS) {
19627c478bd9Sstevel@tonic-gate 		r2 = sqlite_exec(be->be_db, "ROLLBACK TRANSACTION", NULL, NULL,
19637c478bd9Sstevel@tonic-gate 		    &errmsg);
19647c478bd9Sstevel@tonic-gate 		r2 = backend_error(be, r2, errmsg);
19657c478bd9Sstevel@tonic-gate 		if (r2 != REP_PROTOCOL_SUCCESS)
19667c478bd9Sstevel@tonic-gate 			backend_panic("cannot rollback failed commit");
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 		backend_tx_end(tx);
19697c478bd9Sstevel@tonic-gate 		return (r);
19707c478bd9Sstevel@tonic-gate 	}
19717c478bd9Sstevel@tonic-gate 	backend_tx_end(tx);
19727c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
19737c478bd9Sstevel@tonic-gate }
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate static const char *
19767c478bd9Sstevel@tonic-gate id_space_to_name(enum id_space id)
19777c478bd9Sstevel@tonic-gate {
19787c478bd9Sstevel@tonic-gate 	switch (id) {
19797c478bd9Sstevel@tonic-gate 	case BACKEND_ID_SERVICE_INSTANCE:
19807c478bd9Sstevel@tonic-gate 		return ("SI");
19817c478bd9Sstevel@tonic-gate 	case BACKEND_ID_PROPERTYGRP:
19827c478bd9Sstevel@tonic-gate 		return ("PG");
19837c478bd9Sstevel@tonic-gate 	case BACKEND_ID_GENERATION:
19847c478bd9Sstevel@tonic-gate 		return ("GEN");
19857c478bd9Sstevel@tonic-gate 	case BACKEND_ID_PROPERTY:
19867c478bd9Sstevel@tonic-gate 		return ("PROP");
19877c478bd9Sstevel@tonic-gate 	case BACKEND_ID_VALUE:
19887c478bd9Sstevel@tonic-gate 		return ("VAL");
19897c478bd9Sstevel@tonic-gate 	case BACKEND_ID_SNAPNAME:
19907c478bd9Sstevel@tonic-gate 		return ("SNAME");
19917c478bd9Sstevel@tonic-gate 	case BACKEND_ID_SNAPSHOT:
19927c478bd9Sstevel@tonic-gate 		return ("SHOT");
19937c478bd9Sstevel@tonic-gate 	case BACKEND_ID_SNAPLEVEL:
19947c478bd9Sstevel@tonic-gate 		return ("SLVL");
19957c478bd9Sstevel@tonic-gate 	default:
19967c478bd9Sstevel@tonic-gate 		abort();
19977c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
19987c478bd9Sstevel@tonic-gate 	}
19997c478bd9Sstevel@tonic-gate }
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate /*
20027c478bd9Sstevel@tonic-gate  * Returns a new id or 0 if the id argument is invalid or the query fails.
20037c478bd9Sstevel@tonic-gate  */
20047c478bd9Sstevel@tonic-gate uint32_t
20057c478bd9Sstevel@tonic-gate backend_new_id(backend_tx_t *tx, enum id_space id)
20067c478bd9Sstevel@tonic-gate {
20077c478bd9Sstevel@tonic-gate 	struct run_single_int_info info;
20087c478bd9Sstevel@tonic-gate 	uint32_t new_id = 0;
20097c478bd9Sstevel@tonic-gate 	const char *name = id_space_to_name(id);
20107c478bd9Sstevel@tonic-gate 	char *errmsg;
20117c478bd9Sstevel@tonic-gate 	int ret;
20127c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
20137c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly);
20167c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	info.rs_out = &new_id;
20197c478bd9Sstevel@tonic-gate 	info.rs_result = REP_PROTOCOL_FAIL_NOT_FOUND;
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	ts = gethrtime();
20227c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
20237c478bd9Sstevel@tonic-gate 	ret = sqlite_exec_printf(be->be_db,
20247c478bd9Sstevel@tonic-gate 	    "SELECT id_next FROM id_tbl WHERE (id_name = '%q');"
20257c478bd9Sstevel@tonic-gate 	    "UPDATE id_tbl SET id_next = id_next + 1 WHERE (id_name = '%q');",
20267c478bd9Sstevel@tonic-gate 	    run_single_int_callback, &info, &errmsg, name, name);
20277c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
20287c478bd9Sstevel@tonic-gate 	if (ret == SQLITE_FULL)
20297c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	ret = backend_error(be, ret, errmsg);
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	if (ret != REP_PROTOCOL_SUCCESS) {
20347c478bd9Sstevel@tonic-gate 		return (0);
20357c478bd9Sstevel@tonic-gate 	}
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	return (new_id);
20387c478bd9Sstevel@tonic-gate }
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate /*
20417c478bd9Sstevel@tonic-gate  * Returns
20427c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
20437c478bd9Sstevel@tonic-gate  *   _DONE - callback aborted query
20447c478bd9Sstevel@tonic-gate  *   _SUCCESS
20457c478bd9Sstevel@tonic-gate  */
20467c478bd9Sstevel@tonic-gate int
20477c478bd9Sstevel@tonic-gate backend_tx_run(backend_tx_t *tx, backend_query_t *q,
20487c478bd9Sstevel@tonic-gate     backend_run_callback_f *cb, void *data)
20497c478bd9Sstevel@tonic-gate {
20507c478bd9Sstevel@tonic-gate 	char *errmsg = NULL;
20517c478bd9Sstevel@tonic-gate 	int ret;
20527c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
20537c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL);
20567c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	if (q == NULL || q->bq_buf == NULL)
20597c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 	ts = gethrtime();
20627c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
20637c478bd9Sstevel@tonic-gate 	ret = sqlite_exec(be->be_db, q->bq_buf, cb, data, &errmsg);
20647c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
20657c478bd9Sstevel@tonic-gate 	if (ret == SQLITE_FULL)
20667c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
20677c478bd9Sstevel@tonic-gate 	ret = backend_error(be, ret, errmsg);
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	return (ret);
20707c478bd9Sstevel@tonic-gate }
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate /*
20737c478bd9Sstevel@tonic-gate  * Returns
20747c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
20757c478bd9Sstevel@tonic-gate  *   _NOT_FOUND - the query returned no results
20767c478bd9Sstevel@tonic-gate  *   _SUCCESS - the query returned a single integer
20777c478bd9Sstevel@tonic-gate  */
20787c478bd9Sstevel@tonic-gate int
20797c478bd9Sstevel@tonic-gate backend_tx_run_single_int(backend_tx_t *tx, backend_query_t *q, uint32_t *buf)
20807c478bd9Sstevel@tonic-gate {
20817c478bd9Sstevel@tonic-gate 	struct run_single_int_info info;
20827c478bd9Sstevel@tonic-gate 	int ret;
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	info.rs_out = buf;
20857c478bd9Sstevel@tonic-gate 	info.rs_result = REP_PROTOCOL_FAIL_NOT_FOUND;
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	ret = backend_tx_run(tx, q, run_single_int_callback, &info);
20887c478bd9Sstevel@tonic-gate 	assert(ret != REP_PROTOCOL_DONE);
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	if (ret != REP_PROTOCOL_SUCCESS)
20917c478bd9Sstevel@tonic-gate 		return (ret);
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	return (info.rs_result);
20947c478bd9Sstevel@tonic-gate }
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate /*
20977c478bd9Sstevel@tonic-gate  * Fails with
20987c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
20997c478bd9Sstevel@tonic-gate  */
21007c478bd9Sstevel@tonic-gate int
21017c478bd9Sstevel@tonic-gate backend_tx_run_update(backend_tx_t *tx, const char *format, ...)
21027c478bd9Sstevel@tonic-gate {
21037c478bd9Sstevel@tonic-gate 	va_list a;
21047c478bd9Sstevel@tonic-gate 	char *errmsg;
21057c478bd9Sstevel@tonic-gate 	int ret;
21067c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
21077c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly);
21107c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	va_start(a, format);
21137c478bd9Sstevel@tonic-gate 	ts = gethrtime();
21147c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
21157c478bd9Sstevel@tonic-gate 	ret = sqlite_exec_vprintf(be->be_db, format, NULL, NULL, &errmsg, a);
21167c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
21177c478bd9Sstevel@tonic-gate 	if (ret == SQLITE_FULL)
21187c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
21197c478bd9Sstevel@tonic-gate 	va_end(a);
21207c478bd9Sstevel@tonic-gate 	ret = backend_error(be, ret, errmsg);
21217c478bd9Sstevel@tonic-gate 	assert(ret != REP_PROTOCOL_DONE);
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	return (ret);
21247c478bd9Sstevel@tonic-gate }
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate /*
21277c478bd9Sstevel@tonic-gate  * returns REP_PROTOCOL_FAIL_NOT_FOUND if no changes occured
21287c478bd9Sstevel@tonic-gate  */
21297c478bd9Sstevel@tonic-gate int
21307c478bd9Sstevel@tonic-gate backend_tx_run_update_changed(backend_tx_t *tx, const char *format, ...)
21317c478bd9Sstevel@tonic-gate {
21327c478bd9Sstevel@tonic-gate 	va_list a;
21337c478bd9Sstevel@tonic-gate 	char *errmsg;
21347c478bd9Sstevel@tonic-gate 	int ret;
21357c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
21367c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly);
21397c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	va_start(a, format);
21427c478bd9Sstevel@tonic-gate 	ts = gethrtime();
21437c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
21447c478bd9Sstevel@tonic-gate 	ret = sqlite_exec_vprintf(be->be_db, format, NULL, NULL, &errmsg, a);
21457c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
21467c478bd9Sstevel@tonic-gate 	if (ret == SQLITE_FULL)
21477c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
21487c478bd9Sstevel@tonic-gate 	va_end(a);
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	ret = backend_error(be, ret, errmsg);
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	return (ret);
21537c478bd9Sstevel@tonic-gate }
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate #define	BACKEND_ADD_SCHEMA(be, file, tbls, idxs) \
21567c478bd9Sstevel@tonic-gate 	(backend_add_schema((be), (file), \
21577c478bd9Sstevel@tonic-gate 	    (tbls), sizeof (tbls) / sizeof (*(tbls)), \
21587c478bd9Sstevel@tonic-gate 	    (idxs), sizeof (idxs) / sizeof (*(idxs))))
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate static int
21617c478bd9Sstevel@tonic-gate backend_add_schema(sqlite_backend_t *be, const char *file,
21627c478bd9Sstevel@tonic-gate     struct backend_tbl_info *tbls, int tbl_count,
21637c478bd9Sstevel@tonic-gate     struct backend_idx_info *idxs, int idx_count)
21647c478bd9Sstevel@tonic-gate {
21657c478bd9Sstevel@tonic-gate 	int i;
21667c478bd9Sstevel@tonic-gate 	char *errmsg;
21677c478bd9Sstevel@tonic-gate 	int ret;
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	/*
21707c478bd9Sstevel@tonic-gate 	 * Create the tables.
21717c478bd9Sstevel@tonic-gate 	 */
21727c478bd9Sstevel@tonic-gate 	for (i = 0; i < tbl_count; i++) {
21737c478bd9Sstevel@tonic-gate 		if (tbls[i].bti_name == NULL) {
21747c478bd9Sstevel@tonic-gate 			assert(i + 1 == tbl_count);
21757c478bd9Sstevel@tonic-gate 			break;
21767c478bd9Sstevel@tonic-gate 		}
21777c478bd9Sstevel@tonic-gate 		ret = sqlite_exec_printf(be->be_db,
21787c478bd9Sstevel@tonic-gate 		    "CREATE TABLE %s (%s);\n",
21797c478bd9Sstevel@tonic-gate 		    NULL, NULL, &errmsg, tbls[i].bti_name, tbls[i].bti_cols);
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 		if (ret != SQLITE_OK) {
21827c478bd9Sstevel@tonic-gate 			configd_critical(
21837c478bd9Sstevel@tonic-gate 			    "%s: %s table creation fails: %s\n", file,
21847c478bd9Sstevel@tonic-gate 			    tbls[i].bti_name, errmsg);
21857c478bd9Sstevel@tonic-gate 			free(errmsg);
21867c478bd9Sstevel@tonic-gate 			return (-1);
21877c478bd9Sstevel@tonic-gate 		}
21887c478bd9Sstevel@tonic-gate 	}
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	/*
21917c478bd9Sstevel@tonic-gate 	 * Make indices on key tables and columns.
21927c478bd9Sstevel@tonic-gate 	 */
21937c478bd9Sstevel@tonic-gate 	for (i = 0; i < idx_count; i++) {
21947c478bd9Sstevel@tonic-gate 		if (idxs[i].bxi_tbl == NULL) {
21957c478bd9Sstevel@tonic-gate 			assert(i + 1 == idx_count);
21967c478bd9Sstevel@tonic-gate 			break;
21977c478bd9Sstevel@tonic-gate 		}
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 		ret = sqlite_exec_printf(be->be_db,
22007c478bd9Sstevel@tonic-gate 		    "CREATE INDEX %s_%s ON %s (%s);\n",
22017c478bd9Sstevel@tonic-gate 		    NULL, NULL, &errmsg, idxs[i].bxi_tbl, idxs[i].bxi_idx,
22027c478bd9Sstevel@tonic-gate 		    idxs[i].bxi_tbl, idxs[i].bxi_cols);
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 		if (ret != SQLITE_OK) {
22057c478bd9Sstevel@tonic-gate 			configd_critical(
22067c478bd9Sstevel@tonic-gate 			    "%s: %s_%s index creation fails: %s\n", file,
22077c478bd9Sstevel@tonic-gate 			    idxs[i].bxi_tbl, idxs[i].bxi_idx, errmsg);
22087c478bd9Sstevel@tonic-gate 			free(errmsg);
22097c478bd9Sstevel@tonic-gate 			return (-1);
22107c478bd9Sstevel@tonic-gate 		}
22117c478bd9Sstevel@tonic-gate 	}
22127c478bd9Sstevel@tonic-gate 	return (0);
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate static int
22167c478bd9Sstevel@tonic-gate backend_init_schema(sqlite_backend_t *be, const char *db_file, backend_type_t t)
22177c478bd9Sstevel@tonic-gate {
22187c478bd9Sstevel@tonic-gate 	int i;
22197c478bd9Sstevel@tonic-gate 	char *errmsg;
22207c478bd9Sstevel@tonic-gate 	int ret;
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	assert(t == BACKEND_TYPE_NORMAL || t == BACKEND_TYPE_NONPERSIST);
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	if (t == BACKEND_TYPE_NORMAL) {
22257c478bd9Sstevel@tonic-gate 		ret = BACKEND_ADD_SCHEMA(be, db_file, tbls_normal, idxs_normal);
22267c478bd9Sstevel@tonic-gate 	} else if (t == BACKEND_TYPE_NONPERSIST) {
22277c478bd9Sstevel@tonic-gate 		ret = BACKEND_ADD_SCHEMA(be, db_file, tbls_np, idxs_np);
22287c478bd9Sstevel@tonic-gate 	} else {
22297c478bd9Sstevel@tonic-gate 		abort();		/* can't happen */
22307c478bd9Sstevel@tonic-gate 	}
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	if (ret < 0) {
22337c478bd9Sstevel@tonic-gate 		return (ret);
22347c478bd9Sstevel@tonic-gate 	}
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	ret = BACKEND_ADD_SCHEMA(be, db_file, tbls_common, idxs_common);
22377c478bd9Sstevel@tonic-gate 	if (ret < 0) {
22387c478bd9Sstevel@tonic-gate 		return (ret);
22397c478bd9Sstevel@tonic-gate 	}
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 	/*
22427c478bd9Sstevel@tonic-gate 	 * Add the schema version to the table
22437c478bd9Sstevel@tonic-gate 	 */
22447c478bd9Sstevel@tonic-gate 	ret = sqlite_exec_printf(be->be_db,
22457c478bd9Sstevel@tonic-gate 	    "INSERT INTO schema_version (schema_version) VALUES (%d)",
22467c478bd9Sstevel@tonic-gate 	    NULL, NULL, &errmsg, BACKEND_SCHEMA_VERSION);
22477c478bd9Sstevel@tonic-gate 	if (ret != SQLITE_OK) {
22487c478bd9Sstevel@tonic-gate 		configd_critical(
22497c478bd9Sstevel@tonic-gate 		    "setting schema version fails: %s\n", errmsg);
22507c478bd9Sstevel@tonic-gate 		free(errmsg);
22517c478bd9Sstevel@tonic-gate 	}
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	/*
22547c478bd9Sstevel@tonic-gate 	 * Populate id_tbl with initial IDs.
22557c478bd9Sstevel@tonic-gate 	 */
22567c478bd9Sstevel@tonic-gate 	for (i = 0; i < BACKEND_ID_INVALID; i++) {
22577c478bd9Sstevel@tonic-gate 		const char *name = id_space_to_name(i);
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 		ret = sqlite_exec_printf(be->be_db,
22607c478bd9Sstevel@tonic-gate 		    "INSERT INTO id_tbl (id_name, id_next) "
22617c478bd9Sstevel@tonic-gate 		    "VALUES ('%q', %d);", NULL, NULL, &errmsg, name, 1);
22627c478bd9Sstevel@tonic-gate 		if (ret != SQLITE_OK) {
22637c478bd9Sstevel@tonic-gate 			configd_critical(
22647c478bd9Sstevel@tonic-gate 			    "id insertion for %s fails: %s\n", name, errmsg);
22657c478bd9Sstevel@tonic-gate 			free(errmsg);
22667c478bd9Sstevel@tonic-gate 			return (-1);
22677c478bd9Sstevel@tonic-gate 		}
22687c478bd9Sstevel@tonic-gate 	}
22697c478bd9Sstevel@tonic-gate 	/*
22707c478bd9Sstevel@tonic-gate 	 * Set the persistance of the database.  The normal database is marked
22717c478bd9Sstevel@tonic-gate 	 * "synchronous", so that all writes are synchronized to stable storage
22727c478bd9Sstevel@tonic-gate 	 * before proceeding.
22737c478bd9Sstevel@tonic-gate 	 */
22747c478bd9Sstevel@tonic-gate 	ret = sqlite_exec_printf(be->be_db,
22757c478bd9Sstevel@tonic-gate 	    "PRAGMA default_synchronous = %s; PRAGMA synchronous = %s;",
22767c478bd9Sstevel@tonic-gate 	    NULL, NULL, &errmsg,
22777c478bd9Sstevel@tonic-gate 	    (t == BACKEND_TYPE_NORMAL)? "ON" : "OFF",
22787c478bd9Sstevel@tonic-gate 	    (t == BACKEND_TYPE_NORMAL)? "ON" : "OFF");
22797c478bd9Sstevel@tonic-gate 	if (ret != SQLITE_OK) {
22807c478bd9Sstevel@tonic-gate 		configd_critical("pragma setting fails: %s\n", errmsg);
22817c478bd9Sstevel@tonic-gate 		free(errmsg);
22827c478bd9Sstevel@tonic-gate 		return (-1);
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	return (0);
22867c478bd9Sstevel@tonic-gate }
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate int
22897c478bd9Sstevel@tonic-gate backend_init(const char *db_file, const char *npdb_file, int have_np)
22907c478bd9Sstevel@tonic-gate {
22917c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
22927c478bd9Sstevel@tonic-gate 	int r;
22937c478bd9Sstevel@tonic-gate 	int writable_persist = 1;
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 	/* set up our temporary directory */
22967c478bd9Sstevel@tonic-gate 	sqlite_temp_directory = "/etc/svc/volatile";
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	if (strcmp(SQLITE_VERSION, sqlite_version) != 0) {
22997c478bd9Sstevel@tonic-gate 		configd_critical("Mismatched link!  (%s should be %s)\n",
23007c478bd9Sstevel@tonic-gate 		    sqlite_version, SQLITE_VERSION);
23017c478bd9Sstevel@tonic-gate 		return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
23027c478bd9Sstevel@tonic-gate 	}
2303c0889d7aSstevep 
2304c0889d7aSstevep 	/*
2305c0889d7aSstevep 	 * If the system crashed during a backend switch, there might
2306c0889d7aSstevep 	 * be a leftover transient database which contains useful
2307c0889d7aSstevep 	 * information which can be used for recovery.
2308c0889d7aSstevep 	 */
2309c0889d7aSstevep 	backend_switch_recovery();
2310c0889d7aSstevep 
23117c478bd9Sstevel@tonic-gate 	if (db_file == NULL)
23127c478bd9Sstevel@tonic-gate 		db_file = REPOSITORY_DB;
23135b7f77adStw21770 	if (strcmp(db_file, REPOSITORY_DB) != 0) {
23145b7f77adStw21770 		is_main_repository = 0;
23155b7f77adStw21770 	}
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	r = backend_create(BACKEND_TYPE_NORMAL, db_file, &be);
23187c478bd9Sstevel@tonic-gate 	switch (r) {
23197c478bd9Sstevel@tonic-gate 	case BACKEND_CREATE_FAIL:
23207c478bd9Sstevel@tonic-gate 		return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
23217c478bd9Sstevel@tonic-gate 	case BACKEND_CREATE_LOCKED:
23227c478bd9Sstevel@tonic-gate 		return (CONFIGD_EXIT_DATABASE_LOCKED);
23237c478bd9Sstevel@tonic-gate 	case BACKEND_CREATE_SUCCESS:
23247c478bd9Sstevel@tonic-gate 		break;		/* success */
23257c478bd9Sstevel@tonic-gate 	case BACKEND_CREATE_READONLY:
23267c478bd9Sstevel@tonic-gate 		writable_persist = 0;
23277c478bd9Sstevel@tonic-gate 		break;
23287c478bd9Sstevel@tonic-gate 	case BACKEND_CREATE_NEED_INIT:
23297c478bd9Sstevel@tonic-gate 		if (backend_init_schema(be, db_file, BACKEND_TYPE_NORMAL)) {
23307c478bd9Sstevel@tonic-gate 			backend_destroy(be);
23317c478bd9Sstevel@tonic-gate 			return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
23327c478bd9Sstevel@tonic-gate 		}
23337c478bd9Sstevel@tonic-gate 		break;
23347c478bd9Sstevel@tonic-gate 	default:
23357c478bd9Sstevel@tonic-gate 		abort();
23367c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
23377c478bd9Sstevel@tonic-gate 	}
23387c478bd9Sstevel@tonic-gate 	backend_create_finish(BACKEND_TYPE_NORMAL, be);
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 	if (have_np) {
23417c478bd9Sstevel@tonic-gate 		if (npdb_file == NULL)
23427c478bd9Sstevel@tonic-gate 			npdb_file = NONPERSIST_DB;
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 		r = backend_create(BACKEND_TYPE_NONPERSIST, npdb_file, &be);
23457c478bd9Sstevel@tonic-gate 		switch (r) {
23467c478bd9Sstevel@tonic-gate 		case BACKEND_CREATE_SUCCESS:
23477c478bd9Sstevel@tonic-gate 			break;		/* success */
23487c478bd9Sstevel@tonic-gate 		case BACKEND_CREATE_FAIL:
23497c478bd9Sstevel@tonic-gate 			return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
23507c478bd9Sstevel@tonic-gate 		case BACKEND_CREATE_LOCKED:
23517c478bd9Sstevel@tonic-gate 			return (CONFIGD_EXIT_DATABASE_LOCKED);
23527c478bd9Sstevel@tonic-gate 		case BACKEND_CREATE_READONLY:
23537c478bd9Sstevel@tonic-gate 			configd_critical("%s: unable to write\n", npdb_file);
23547c478bd9Sstevel@tonic-gate 			return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
23557c478bd9Sstevel@tonic-gate 		case BACKEND_CREATE_NEED_INIT:
23567c478bd9Sstevel@tonic-gate 			if (backend_init_schema(be, db_file,
23577c478bd9Sstevel@tonic-gate 			    BACKEND_TYPE_NONPERSIST)) {
23587c478bd9Sstevel@tonic-gate 				backend_destroy(be);
23597c478bd9Sstevel@tonic-gate 				return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
23607c478bd9Sstevel@tonic-gate 			}
23617c478bd9Sstevel@tonic-gate 			break;
23627c478bd9Sstevel@tonic-gate 		default:
23637c478bd9Sstevel@tonic-gate 			abort();
23647c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
23657c478bd9Sstevel@tonic-gate 		}
23667c478bd9Sstevel@tonic-gate 		backend_create_finish(BACKEND_TYPE_NONPERSIST, be);
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 		/*
23697c478bd9Sstevel@tonic-gate 		 * If we started up with a writable filesystem, but the
23707c478bd9Sstevel@tonic-gate 		 * non-persistent database needed initialization, we
2371*6e1d2b42Samaguire 		 * are booting a non-global zone, so do a backup, and carry
2372*6e1d2b42Samaguire 		 * out upgrade if necessary.
23737c478bd9Sstevel@tonic-gate 		 */
23747c478bd9Sstevel@tonic-gate 		if (r == BACKEND_CREATE_NEED_INIT && writable_persist &&
23757c478bd9Sstevel@tonic-gate 		    backend_lock(BACKEND_TYPE_NORMAL, 0, &be) ==
23767c478bd9Sstevel@tonic-gate 		    REP_PROTOCOL_SUCCESS) {
23777c478bd9Sstevel@tonic-gate 			if (backend_create_backup_locked(be,
23787c478bd9Sstevel@tonic-gate 			    REPOSITORY_BOOT_BACKUP) != REP_PROTOCOL_SUCCESS) {
23797c478bd9Sstevel@tonic-gate 				configd_critical(
23807c478bd9Sstevel@tonic-gate 				    "unable to create \"%s\" backup of "
23817c478bd9Sstevel@tonic-gate 				    "\"%s\"\n", REPOSITORY_BOOT_BACKUP,
23827c478bd9Sstevel@tonic-gate 				    be->be_path);
23837c478bd9Sstevel@tonic-gate 			}
2384*6e1d2b42Samaguire 			backend_check_upgrade(be, B_TRUE);
23857c478bd9Sstevel@tonic-gate 			backend_unlock(be);
23867c478bd9Sstevel@tonic-gate 		}
23877c478bd9Sstevel@tonic-gate 	}
23887c478bd9Sstevel@tonic-gate 	return (CONFIGD_EXIT_OKAY);
23897c478bd9Sstevel@tonic-gate }
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate /*
23927c478bd9Sstevel@tonic-gate  * quiesce all database activity prior to exiting
23937c478bd9Sstevel@tonic-gate  */
23947c478bd9Sstevel@tonic-gate void
23957c478bd9Sstevel@tonic-gate backend_fini(void)
23967c478bd9Sstevel@tonic-gate {
23977c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be_normal, *be_np;
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 	(void) backend_lock(BACKEND_TYPE_NORMAL, 1, &be_normal);
24007c478bd9Sstevel@tonic-gate 	(void) backend_lock(BACKEND_TYPE_NONPERSIST, 1, &be_np);
24017c478bd9Sstevel@tonic-gate }
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate #define	QUERY_BASE	128
24047c478bd9Sstevel@tonic-gate backend_query_t *
24057c478bd9Sstevel@tonic-gate backend_query_alloc(void)
24067c478bd9Sstevel@tonic-gate {
24077c478bd9Sstevel@tonic-gate 	backend_query_t *q;
24087c478bd9Sstevel@tonic-gate 	q = calloc(1, sizeof (backend_query_t));
24097c478bd9Sstevel@tonic-gate 	if (q != NULL) {
24107c478bd9Sstevel@tonic-gate 		q->bq_size = QUERY_BASE;
24117c478bd9Sstevel@tonic-gate 		q->bq_buf = calloc(1, q->bq_size);
24127c478bd9Sstevel@tonic-gate 		if (q->bq_buf == NULL) {
24137c478bd9Sstevel@tonic-gate 			q->bq_size = 0;
24147c478bd9Sstevel@tonic-gate 		}
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	}
24177c478bd9Sstevel@tonic-gate 	return (q);
24187c478bd9Sstevel@tonic-gate }
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate void
24217c478bd9Sstevel@tonic-gate backend_query_append(backend_query_t *q, const char *value)
24227c478bd9Sstevel@tonic-gate {
24237c478bd9Sstevel@tonic-gate 	char *alloc;
24247c478bd9Sstevel@tonic-gate 	int count;
24257c478bd9Sstevel@tonic-gate 	size_t size, old_len;
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	if (q == NULL) {
24287c478bd9Sstevel@tonic-gate 		/* We'll discover the error when we try to run the query. */
24297c478bd9Sstevel@tonic-gate 		return;
24307c478bd9Sstevel@tonic-gate 	}
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 	while (q->bq_buf != NULL) {
24337c478bd9Sstevel@tonic-gate 		old_len = strlen(q->bq_buf);
24347c478bd9Sstevel@tonic-gate 		size = q->bq_size;
24357c478bd9Sstevel@tonic-gate 		count = strlcat(q->bq_buf, value, size);
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 		if (count < size)
24387c478bd9Sstevel@tonic-gate 			break;				/* success */
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 		q->bq_buf[old_len] = 0;
24417c478bd9Sstevel@tonic-gate 		size = round_up_to_p2(count + 1);
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 		assert(size > q->bq_size);
24447c478bd9Sstevel@tonic-gate 		alloc = realloc(q->bq_buf, size);
24457c478bd9Sstevel@tonic-gate 		if (alloc == NULL) {
24467c478bd9Sstevel@tonic-gate 			free(q->bq_buf);
24477c478bd9Sstevel@tonic-gate 			q->bq_buf = NULL;
24487c478bd9Sstevel@tonic-gate 			break;				/* can't grow */
24497c478bd9Sstevel@tonic-gate 		}
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 		q->bq_buf = alloc;
24527c478bd9Sstevel@tonic-gate 		q->bq_size = size;
24537c478bd9Sstevel@tonic-gate 	}
24547c478bd9Sstevel@tonic-gate }
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate void
24577c478bd9Sstevel@tonic-gate backend_query_add(backend_query_t *q, const char *format, ...)
24587c478bd9Sstevel@tonic-gate {
24597c478bd9Sstevel@tonic-gate 	va_list args;
24607c478bd9Sstevel@tonic-gate 	char *new;
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	if (q == NULL || q->bq_buf == NULL)
24637c478bd9Sstevel@tonic-gate 		return;
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	va_start(args, format);
24667c478bd9Sstevel@tonic-gate 	new = sqlite_vmprintf(format, args);
24677c478bd9Sstevel@tonic-gate 	va_end(args);
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate 	if (new == NULL) {
24707c478bd9Sstevel@tonic-gate 		free(q->bq_buf);
24717c478bd9Sstevel@tonic-gate 		q->bq_buf = NULL;
24727c478bd9Sstevel@tonic-gate 		return;
24737c478bd9Sstevel@tonic-gate 	}
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	backend_query_append(q, new);
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	free(new);
24787c478bd9Sstevel@tonic-gate }
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate void
24817c478bd9Sstevel@tonic-gate backend_query_free(backend_query_t *q)
24827c478bd9Sstevel@tonic-gate {
24837c478bd9Sstevel@tonic-gate 	if (q != NULL) {
24847c478bd9Sstevel@tonic-gate 		if (q->bq_buf != NULL) {
24857c478bd9Sstevel@tonic-gate 			free(q->bq_buf);
24867c478bd9Sstevel@tonic-gate 		}
24877c478bd9Sstevel@tonic-gate 		free(q);
24887c478bd9Sstevel@tonic-gate 	}
24897c478bd9Sstevel@tonic-gate }
2490