xref: /titanic_44/usr/src/cmd/svc/configd/backend.c (revision 8918dff3e162b85faa068f02fc6f2ca350150e71)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*8918dff3Sjwadams  * Copyright 2005 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 
29*8918dff3Sjwadams /*
30*8918dff3Sjwadams  * sqlite is not compatible with _FILE_OFFSET_BITS=64, but we need to
31*8918dff3Sjwadams  * be able to statvfs(2) possibly large systems.  This define gives us
32*8918dff3Sjwadams  * access to the transitional interfaces.  See lfcompile64(5) for how
33*8918dff3Sjwadams  * _LARGEFILE64_SOURCE works.
34*8918dff3Sjwadams  */
35*8918dff3Sjwadams #define	_LARGEFILE64_SOURCE
36*8918dff3Sjwadams 
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>
48*8918dff3Sjwadams #include <sys/stat.h>
49*8918dff3Sjwadams #include <sys/statvfs.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <zone.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include "configd.h"
547c478bd9Sstevel@tonic-gate #include "repcache_protocol.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include "sqlite/sqlite.h"
577c478bd9Sstevel@tonic-gate #include "sqlite/sqlite-misc.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * This file has two purposes:
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * 1. It contains the database schema, and the code for setting up our backend
637c478bd9Sstevel@tonic-gate  *    databases, including installing said schema.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * 2. It provides a simplified interface to the SQL database library, and
667c478bd9Sstevel@tonic-gate  *    synchronizes MT access to the database.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate typedef struct backend_spent {
707c478bd9Sstevel@tonic-gate 	uint64_t bs_count;
717c478bd9Sstevel@tonic-gate 	hrtime_t bs_time;
727c478bd9Sstevel@tonic-gate 	hrtime_t bs_vtime;
737c478bd9Sstevel@tonic-gate } backend_spent_t;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate typedef struct backend_totals {
767c478bd9Sstevel@tonic-gate 	backend_spent_t	bt_lock;	/* waiting for lock */
777c478bd9Sstevel@tonic-gate 	backend_spent_t	bt_exec;	/* time spent executing SQL */
787c478bd9Sstevel@tonic-gate } backend_totals_t;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate typedef struct sqlite_backend {
817c478bd9Sstevel@tonic-gate 	pthread_mutex_t	be_lock;
827c478bd9Sstevel@tonic-gate 	pthread_t	be_thread;	/* thread holding lock */
837c478bd9Sstevel@tonic-gate 	struct sqlite	*be_db;
847c478bd9Sstevel@tonic-gate 	const char	*be_path;	/* path to db */
85*8918dff3Sjwadams 	int		be_readonly;	/* readonly at start, and still is */
867c478bd9Sstevel@tonic-gate 	int		be_writing;	/* held for writing */
877c478bd9Sstevel@tonic-gate 	backend_type_t	be_type;	/* type of db */
88*8918dff3Sjwadams 	hrtime_t	be_lastcheck;	/* time of last read-only check */
897c478bd9Sstevel@tonic-gate 	backend_totals_t be_totals[2];	/* one for reading, one for writing */
907c478bd9Sstevel@tonic-gate } sqlite_backend_t;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate struct backend_tx {
937c478bd9Sstevel@tonic-gate 	sqlite_backend_t	*bt_be;
947c478bd9Sstevel@tonic-gate 	int			bt_readonly;
957c478bd9Sstevel@tonic-gate 	int			bt_type;
967c478bd9Sstevel@tonic-gate 	int			bt_full;	/* SQLITE_FULL during tx */
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define	UPDATE_TOTALS_WR(sb, writing, field, ts, vts) { \
1007c478bd9Sstevel@tonic-gate 	backend_spent_t *__bsp = &(sb)->be_totals[!!(writing)].field; \
1017c478bd9Sstevel@tonic-gate 	__bsp->bs_count++;						\
1027c478bd9Sstevel@tonic-gate 	__bsp->bs_time += (gethrtime() - ts);				\
1037c478bd9Sstevel@tonic-gate 	__bsp->bs_vtime += (gethrvtime() - vts);			\
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define	UPDATE_TOTALS(sb, field, ts, vts) \
1077c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS_WR(sb, (sb)->be_writing, field, ts, vts)
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate struct backend_query {
1107c478bd9Sstevel@tonic-gate 	char	*bq_buf;
1117c478bd9Sstevel@tonic-gate 	size_t	bq_size;
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate struct backend_tbl_info {
1157c478bd9Sstevel@tonic-gate 	const char *bti_name;
1167c478bd9Sstevel@tonic-gate 	const char *bti_cols;
1177c478bd9Sstevel@tonic-gate };
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate struct backend_idx_info {
1207c478bd9Sstevel@tonic-gate 	const char *bxi_tbl;
1217c478bd9Sstevel@tonic-gate 	const char *bxi_idx;
1227c478bd9Sstevel@tonic-gate 	const char *bxi_cols;
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static pthread_mutex_t backend_panic_lock = PTHREAD_MUTEX_INITIALIZER;
1267c478bd9Sstevel@tonic-gate static pthread_cond_t backend_panic_cv = PTHREAD_COND_INITIALIZER;
1277c478bd9Sstevel@tonic-gate pthread_t backend_panic_thread = 0;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate int backend_do_trace = 0;		/* invoke tracing callback */
1307c478bd9Sstevel@tonic-gate int backend_print_trace = 0;		/* tracing callback prints SQL */
1317c478bd9Sstevel@tonic-gate int backend_panic_abort = 0;		/* abort when panicking */
1327c478bd9Sstevel@tonic-gate 
133*8918dff3Sjwadams /* interval between read-only checks while starting up */
134*8918dff3Sjwadams #define	BACKEND_READONLY_CHECK_INTERVAL	(2 * (hrtime_t)NANOSEC)
135*8918dff3Sjwadams 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Any change to the below schema should bump the version number
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate #define	BACKEND_SCHEMA_VERSION		5
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static struct backend_tbl_info tbls_normal[] = { /* BACKEND_TYPE_NORMAL */
1427c478bd9Sstevel@tonic-gate 	/*
1437c478bd9Sstevel@tonic-gate 	 * service_tbl holds all services.  svc_id is the identifier of the
1447c478bd9Sstevel@tonic-gate 	 * service.
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	{
1477c478bd9Sstevel@tonic-gate 		"service_tbl",
1487c478bd9Sstevel@tonic-gate 		"svc_id          INTEGER PRIMARY KEY,"
1497c478bd9Sstevel@tonic-gate 		"svc_name        CHAR(256) NOT NULL"
1507c478bd9Sstevel@tonic-gate 	},
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/*
1537c478bd9Sstevel@tonic-gate 	 * instance_tbl holds all of the instances.  The parent service id
1547c478bd9Sstevel@tonic-gate 	 * is instance_svc.
1557c478bd9Sstevel@tonic-gate 	 */
1567c478bd9Sstevel@tonic-gate 	{
1577c478bd9Sstevel@tonic-gate 		"instance_tbl",
1587c478bd9Sstevel@tonic-gate 		"instance_id     INTEGER PRIMARY KEY,"
1597c478bd9Sstevel@tonic-gate 		"instance_name   CHAR(256) NOT NULL,"
1607c478bd9Sstevel@tonic-gate 		"instance_svc    INTEGER NOT NULL"
1617c478bd9Sstevel@tonic-gate 	},
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * snapshot_lnk_tbl links (instance, snapshot name) with snapshots.
1657c478bd9Sstevel@tonic-gate 	 */
1667c478bd9Sstevel@tonic-gate 	{
1677c478bd9Sstevel@tonic-gate 		"snapshot_lnk_tbl",
1687c478bd9Sstevel@tonic-gate 		"lnk_id          INTEGER PRIMARY KEY,"
1697c478bd9Sstevel@tonic-gate 		"lnk_inst_id     INTEGER NOT NULL,"
1707c478bd9Sstevel@tonic-gate 		"lnk_snap_name   CHAR(256) NOT NULL,"
1717c478bd9Sstevel@tonic-gate 		"lnk_snap_id     INTEGER NOT NULL"
1727c478bd9Sstevel@tonic-gate 	},
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/*
1757c478bd9Sstevel@tonic-gate 	 * snaplevel_tbl maps a snapshot id to a set of named, ordered
1767c478bd9Sstevel@tonic-gate 	 * snaplevels.
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	{
1797c478bd9Sstevel@tonic-gate 		"snaplevel_tbl",
1807c478bd9Sstevel@tonic-gate 		"snap_id                 INTEGER NOT NULL,"
1817c478bd9Sstevel@tonic-gate 		"snap_level_num          INTEGER NOT NULL,"
1827c478bd9Sstevel@tonic-gate 		"snap_level_id           INTEGER NOT NULL,"
1837c478bd9Sstevel@tonic-gate 		"snap_level_service_id   INTEGER NOT NULL,"
1847c478bd9Sstevel@tonic-gate 		"snap_level_service      CHAR(256) NOT NULL,"
1857c478bd9Sstevel@tonic-gate 		"snap_level_instance_id  INTEGER NULL,"
1867c478bd9Sstevel@tonic-gate 		"snap_level_instance     CHAR(256) NULL"
1877c478bd9Sstevel@tonic-gate 	},
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/*
1907c478bd9Sstevel@tonic-gate 	 * snaplevel_lnk_tbl links snaplevels to property groups.
1917c478bd9Sstevel@tonic-gate 	 * snaplvl_pg_* is identical to the original property group,
1927c478bd9Sstevel@tonic-gate 	 * and snaplvl_gen_id overrides the generation number.
1937c478bd9Sstevel@tonic-gate 	 * The service/instance ids are as in the snaplevel.
1947c478bd9Sstevel@tonic-gate 	 */
1957c478bd9Sstevel@tonic-gate 	{
1967c478bd9Sstevel@tonic-gate 		"snaplevel_lnk_tbl",
1977c478bd9Sstevel@tonic-gate 		"snaplvl_level_id INTEGER NOT NULL,"
1987c478bd9Sstevel@tonic-gate 		"snaplvl_pg_id    INTEGER NOT NULL,"
1997c478bd9Sstevel@tonic-gate 		"snaplvl_pg_name  CHAR(256) NOT NULL,"
2007c478bd9Sstevel@tonic-gate 		"snaplvl_pg_type  CHAR(256) NOT NULL,"
2017c478bd9Sstevel@tonic-gate 		"snaplvl_pg_flags INTEGER NOT NULL,"
2027c478bd9Sstevel@tonic-gate 		"snaplvl_gen_id   INTEGER NOT NULL"
2037c478bd9Sstevel@tonic-gate 	},
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	{ NULL, NULL }
2067c478bd9Sstevel@tonic-gate };
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static struct backend_idx_info idxs_normal[] = { /* BACKEND_TYPE_NORMAL */
2097c478bd9Sstevel@tonic-gate 	{ "service_tbl",	"name",	"svc_name" },
2107c478bd9Sstevel@tonic-gate 	{ "instance_tbl",	"name",	"instance_svc, instance_name" },
2117c478bd9Sstevel@tonic-gate 	{ "snapshot_lnk_tbl",	"name",	"lnk_inst_id, lnk_snap_name" },
2127c478bd9Sstevel@tonic-gate 	{ "snapshot_lnk_tbl",	"snapid", "lnk_snap_id" },
2137c478bd9Sstevel@tonic-gate 	{ "snaplevel_tbl",	"id",	"snap_id" },
2147c478bd9Sstevel@tonic-gate 	{ "snaplevel_lnk_tbl",	"id",	"snaplvl_pg_id" },
2157c478bd9Sstevel@tonic-gate 	{ "snaplevel_lnk_tbl",	"level", "snaplvl_level_id" },
2167c478bd9Sstevel@tonic-gate 	{ NULL, NULL, NULL }
2177c478bd9Sstevel@tonic-gate };
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static struct backend_tbl_info tbls_np[] = { /* BACKEND_TYPE_NONPERSIST */
2207c478bd9Sstevel@tonic-gate 	{ NULL, NULL }
2217c478bd9Sstevel@tonic-gate };
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static struct backend_idx_info idxs_np[] = {	/* BACKEND_TYPE_NONPERSIST */
2247c478bd9Sstevel@tonic-gate 	{ NULL, NULL, NULL }
2257c478bd9Sstevel@tonic-gate };
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static struct backend_tbl_info tbls_common[] = { /* all backend types */
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * pg_tbl defines property groups.  They are associated with a single
2307c478bd9Sstevel@tonic-gate 	 * service or instance.  The pg_gen_id links them with the latest
2317c478bd9Sstevel@tonic-gate 	 * "edited" version of its properties.
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 	{
2347c478bd9Sstevel@tonic-gate 		"pg_tbl",
2357c478bd9Sstevel@tonic-gate 		"pg_id           INTEGER PRIMARY KEY,"
2367c478bd9Sstevel@tonic-gate 		"pg_parent_id    INTEGER NOT NULL,"
2377c478bd9Sstevel@tonic-gate 		"pg_name         CHAR(256) NOT NULL,"
2387c478bd9Sstevel@tonic-gate 		"pg_type         CHAR(256) NOT NULL,"
2397c478bd9Sstevel@tonic-gate 		"pg_flags        INTEGER NOT NULL,"
2407c478bd9Sstevel@tonic-gate 		"pg_gen_id       INTEGER NOT NULL"
2417c478bd9Sstevel@tonic-gate 	},
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/*
2447c478bd9Sstevel@tonic-gate 	 * prop_lnk_tbl links a particular pg_id and gen_id to a set of
2457c478bd9Sstevel@tonic-gate 	 * (prop_name, prop_type, val_id) trios.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	{
2487c478bd9Sstevel@tonic-gate 		"prop_lnk_tbl",
2497c478bd9Sstevel@tonic-gate 		"lnk_prop_id     INTEGER PRIMARY KEY,"
2507c478bd9Sstevel@tonic-gate 		"lnk_pg_id       INTEGER NOT NULL,"
2517c478bd9Sstevel@tonic-gate 		"lnk_gen_id      INTEGER NOT NULL,"
2527c478bd9Sstevel@tonic-gate 		"lnk_prop_name   CHAR(256) NOT NULL,"
2537c478bd9Sstevel@tonic-gate 		"lnk_prop_type   CHAR(2) NOT NULL,"
2547c478bd9Sstevel@tonic-gate 		"lnk_val_id      INTEGER"
2557c478bd9Sstevel@tonic-gate 	},
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/*
2587c478bd9Sstevel@tonic-gate 	 * value_tbl maps a value_id to a set of values.  For any given
2597c478bd9Sstevel@tonic-gate 	 * value_id, value_type is constant.
2607c478bd9Sstevel@tonic-gate 	 */
2617c478bd9Sstevel@tonic-gate 	{
2627c478bd9Sstevel@tonic-gate 		"value_tbl",
2637c478bd9Sstevel@tonic-gate 		"value_id        INTEGER NOT NULL,"
2647c478bd9Sstevel@tonic-gate 		"value_type      CHAR(1) NOT NULL,"
2657c478bd9Sstevel@tonic-gate 		"value_value     VARCHAR NOT NULL"
2667c478bd9Sstevel@tonic-gate 	},
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	/*
2697c478bd9Sstevel@tonic-gate 	 * id_tbl has one row per id space
2707c478bd9Sstevel@tonic-gate 	 */
2717c478bd9Sstevel@tonic-gate 	{
2727c478bd9Sstevel@tonic-gate 		"id_tbl",
2737c478bd9Sstevel@tonic-gate 		"id_name         STRING NOT NULL,"
2747c478bd9Sstevel@tonic-gate 		"id_next         INTEGER NOT NULL"
2757c478bd9Sstevel@tonic-gate 	},
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * schema_version has a single row, which contains
2797c478bd9Sstevel@tonic-gate 	 * BACKEND_SCHEMA_VERSION at the time of creation.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	{
2827c478bd9Sstevel@tonic-gate 		"schema_version",
2837c478bd9Sstevel@tonic-gate 		"schema_version  INTEGER"
2847c478bd9Sstevel@tonic-gate 	},
2857c478bd9Sstevel@tonic-gate 	{ NULL, NULL }
2867c478bd9Sstevel@tonic-gate };
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static struct backend_idx_info idxs_common[] = { /* all backend types */
2897c478bd9Sstevel@tonic-gate 	{ "pg_tbl",		"parent", "pg_parent_id" },
2907c478bd9Sstevel@tonic-gate 	{ "pg_tbl",		"name",	"pg_parent_id, pg_name" },
2917c478bd9Sstevel@tonic-gate 	{ "pg_tbl",		"type",	"pg_parent_id, pg_type" },
2927c478bd9Sstevel@tonic-gate 	{ "prop_lnk_tbl",	"base",	"lnk_pg_id, lnk_gen_id" },
2937c478bd9Sstevel@tonic-gate 	{ "prop_lnk_tbl",	"val",	"lnk_val_id" },
2947c478bd9Sstevel@tonic-gate 	{ "value_tbl",		"id",	"value_id" },
2957c478bd9Sstevel@tonic-gate 	{ "id_tbl",		"id",	"id_name" },
2967c478bd9Sstevel@tonic-gate 	{ NULL, NULL, NULL }
2977c478bd9Sstevel@tonic-gate };
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate struct run_single_int_info {
3007c478bd9Sstevel@tonic-gate 	uint32_t	*rs_out;
3017c478bd9Sstevel@tonic-gate 	int		rs_result;
3027c478bd9Sstevel@tonic-gate };
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3057c478bd9Sstevel@tonic-gate static int
3067c478bd9Sstevel@tonic-gate run_single_int_callback(void *arg, int columns, char **vals, char **names)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	struct run_single_int_info *info = arg;
3097c478bd9Sstevel@tonic-gate 	uint32_t val;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	char *endptr = vals[0];
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	assert(info->rs_result != REP_PROTOCOL_SUCCESS);
3147c478bd9Sstevel@tonic-gate 	assert(columns == 1);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if (vals[0] == NULL)
3177c478bd9Sstevel@tonic-gate 		return (BACKEND_CALLBACK_CONTINUE);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	errno = 0;
3207c478bd9Sstevel@tonic-gate 	val = strtoul(vals[0], &endptr, 10);
3217c478bd9Sstevel@tonic-gate 	if ((val == 0 && endptr == vals[0]) || *endptr != 0 || errno != 0)
3227c478bd9Sstevel@tonic-gate 		backend_panic("malformed integer \"%20s\"", vals[0]);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	*info->rs_out = val;
3257c478bd9Sstevel@tonic-gate 	info->rs_result = REP_PROTOCOL_SUCCESS;
3267c478bd9Sstevel@tonic-gate 	return (BACKEND_CALLBACK_CONTINUE);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3307c478bd9Sstevel@tonic-gate int
3317c478bd9Sstevel@tonic-gate backend_fail_if_seen(void *arg, int columns, char **vals, char **names)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	return (BACKEND_CALLBACK_ABORT);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
336*8918dff3Sjwadams /*
337*8918dff3Sjwadams  * check to see if we can successfully start a transaction;  if not, the
338*8918dff3Sjwadams  * filesystem is mounted read-only.
339*8918dff3Sjwadams  */
3407c478bd9Sstevel@tonic-gate static int
341*8918dff3Sjwadams backend_is_readonly(struct sqlite *db, const char *path)
3427c478bd9Sstevel@tonic-gate {
343*8918dff3Sjwadams 	int r;
344*8918dff3Sjwadams 	statvfs64_t stat;
345*8918dff3Sjwadams 
346*8918dff3Sjwadams 	if (statvfs64(path, &stat) == 0 && (stat.f_flag & ST_RDONLY))
347*8918dff3Sjwadams 		return (SQLITE_READONLY);
348*8918dff3Sjwadams 
349*8918dff3Sjwadams 	r = sqlite_exec(db,
3507c478bd9Sstevel@tonic-gate 	    "BEGIN TRANSACTION; "
3517c478bd9Sstevel@tonic-gate 	    "UPDATE schema_version SET schema_version = schema_version; ",
352*8918dff3Sjwadams 	    NULL, NULL, NULL);
3537c478bd9Sstevel@tonic-gate 	(void) sqlite_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, NULL);
3547c478bd9Sstevel@tonic-gate 	return (r);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
357*8918dff3Sjwadams /*
358*8918dff3Sjwadams  * Check to see if the administrator has removed the writable bits on the
359*8918dff3Sjwadams  * repository file.  If they have, we don't allow modifications.
360*8918dff3Sjwadams  *
361*8918dff3Sjwadams  * Since we normally run with PRIV_FILE_DAC_WRITE, we have to use a separate
362*8918dff3Sjwadams  * check.
363*8918dff3Sjwadams  */
364*8918dff3Sjwadams static int
365*8918dff3Sjwadams backend_check_perm(const char *path)
366*8918dff3Sjwadams {
367*8918dff3Sjwadams 	struct stat64 stat;
368*8918dff3Sjwadams 
369*8918dff3Sjwadams 	if (access(path, W_OK) < 0)
370*8918dff3Sjwadams 		return (SQLITE_READONLY);
371*8918dff3Sjwadams 
372*8918dff3Sjwadams 	if (stat64(path, &stat) == 0 &&
373*8918dff3Sjwadams 	    !(stat.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
374*8918dff3Sjwadams 		return (SQLITE_READONLY);
375*8918dff3Sjwadams 
376*8918dff3Sjwadams 	return (SQLITE_OK);
377*8918dff3Sjwadams }
378*8918dff3Sjwadams 
3797c478bd9Sstevel@tonic-gate static void
3807c478bd9Sstevel@tonic-gate backend_trace_sql(void *arg, const char *sql)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be = arg;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (backend_print_trace) {
3857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%d: %s\n", be->be_type, sql);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate static sqlite_backend_t be_info[BACKEND_TYPE_TOTAL];
3907c478bd9Sstevel@tonic-gate static sqlite_backend_t *bes[BACKEND_TYPE_TOTAL];
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate #define	BACKEND_PANIC_TIMEOUT	(50 * MILLISEC)
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate  * backend_panic() -- some kind of database problem or corruption has been hit.
3957c478bd9Sstevel@tonic-gate  * We attempt to quiesce the other database users -- all of the backend sql
3967c478bd9Sstevel@tonic-gate  * entry points will call backend_panic(NULL) if a panic is in progress, as
3977c478bd9Sstevel@tonic-gate  * will any attempt to start a transaction.
3987c478bd9Sstevel@tonic-gate  *
3997c478bd9Sstevel@tonic-gate  * We give threads holding a backend lock 50ms (BACKEND_PANIC_TIMEOUT) to
4007c478bd9Sstevel@tonic-gate  * either drop the lock or call backend_panic().  If they don't respond in
4017c478bd9Sstevel@tonic-gate  * time, we'll just exit anyway.
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate void
4047c478bd9Sstevel@tonic-gate backend_panic(const char *format, ...)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	int i;
4077c478bd9Sstevel@tonic-gate 	va_list args;
4087c478bd9Sstevel@tonic-gate 	int failed = 0;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&backend_panic_lock);
4117c478bd9Sstevel@tonic-gate 	if (backend_panic_thread != 0) {
4127c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&backend_panic_lock);
4137c478bd9Sstevel@tonic-gate 		/*
4147c478bd9Sstevel@tonic-gate 		 * first, drop any backend locks we're holding, then
4157c478bd9Sstevel@tonic-gate 		 * sleep forever on the panic_cv.
4167c478bd9Sstevel@tonic-gate 		 */
4177c478bd9Sstevel@tonic-gate 		for (i = 0; i < BACKEND_TYPE_TOTAL; i++) {
4187c478bd9Sstevel@tonic-gate 			if (bes[i] != NULL &&
4197c478bd9Sstevel@tonic-gate 			    bes[i]->be_thread == pthread_self())
4207c478bd9Sstevel@tonic-gate 				(void) pthread_mutex_unlock(&bes[i]->be_lock);
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&backend_panic_lock);
4237c478bd9Sstevel@tonic-gate 		for (;;)
4247c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&backend_panic_cv,
4257c478bd9Sstevel@tonic-gate 			    &backend_panic_lock);
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 	backend_panic_thread = pthread_self();
4287c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&backend_panic_lock);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	for (i = 0; i < BACKEND_TYPE_TOTAL; i++) {
4317c478bd9Sstevel@tonic-gate 		if (bes[i] != NULL && bes[i]->be_thread == pthread_self())
4327c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&bes[i]->be_lock);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	va_start(args, format);
4367c478bd9Sstevel@tonic-gate 	configd_vcritical(format, args);
4377c478bd9Sstevel@tonic-gate 	va_end(args);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	for (i = 0; i < BACKEND_TYPE_TOTAL; i++) {
4407c478bd9Sstevel@tonic-gate 		timespec_t rel;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		rel.tv_sec = 0;
4437c478bd9Sstevel@tonic-gate 		rel.tv_nsec = BACKEND_PANIC_TIMEOUT;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		if (bes[i] != NULL && bes[i]->be_thread != pthread_self()) {
4467c478bd9Sstevel@tonic-gate 			if (pthread_mutex_reltimedlock_np(&bes[i]->be_lock,
4477c478bd9Sstevel@tonic-gate 			    &rel) != 0)
4487c478bd9Sstevel@tonic-gate 				failed++;
4497c478bd9Sstevel@tonic-gate 		}
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 	if (failed) {
4527c478bd9Sstevel@tonic-gate 		configd_critical("unable to quiesce database\n");
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (backend_panic_abort)
4567c478bd9Sstevel@tonic-gate 		abort();
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	exit(CONFIGD_EXIT_DATABASE_BAD);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate /*
4627c478bd9Sstevel@tonic-gate  * Returns
4637c478bd9Sstevel@tonic-gate  *   _SUCCESS
4647c478bd9Sstevel@tonic-gate  *   _DONE - callback aborted query
4657c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory (_FULL & _TOOBIG?)
4667c478bd9Sstevel@tonic-gate  */
4677c478bd9Sstevel@tonic-gate static int
4687c478bd9Sstevel@tonic-gate backend_error(sqlite_backend_t *be, int error, char *errmsg)
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate 	if (error == SQLITE_OK)
4717c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_SUCCESS);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	switch (error) {
4747c478bd9Sstevel@tonic-gate 	case SQLITE_ABORT:
4757c478bd9Sstevel@tonic-gate 		free(errmsg);
4767c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_DONE);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	case SQLITE_NOMEM:
4797c478bd9Sstevel@tonic-gate 	case SQLITE_FULL:
4807c478bd9Sstevel@tonic-gate 	case SQLITE_TOOBIG:
4817c478bd9Sstevel@tonic-gate 		free(errmsg);
4827c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	default:
4857c478bd9Sstevel@tonic-gate 		backend_panic("%s: db error: %s", be->be_path, errmsg);
4867c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate static void
4917c478bd9Sstevel@tonic-gate backend_backup_cleanup(const char **out_arg, ssize_t out_sz)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	char **out = (char **)out_arg;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	while (out_sz-- > 0)
4967c478bd9Sstevel@tonic-gate 		free(*out++);
4977c478bd9Sstevel@tonic-gate 	free(out_arg);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * builds a inverse-time-sorted array of backup files.  The path is a
5027c478bd9Sstevel@tonic-gate  * a single buffer, and the pointers look like:
5037c478bd9Sstevel@tonic-gate  *
5047c478bd9Sstevel@tonic-gate  *	/this/is/a/full/path/to/repository-name-YYYYMMDDHHMMSS
5057c478bd9Sstevel@tonic-gate  *	^pathname		^	       ^(pathname+pathlen)
5067c478bd9Sstevel@tonic-gate  *				basename
5077c478bd9Sstevel@tonic-gate  *
5087c478bd9Sstevel@tonic-gate  * dirname will either be pathname, or ".".
5097c478bd9Sstevel@tonic-gate  *
5107c478bd9Sstevel@tonic-gate  * Returns the number of elements in the array, 0 if there are no previous
5117c478bd9Sstevel@tonic-gate  * backups, or -1 on error.
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate static ssize_t
5147c478bd9Sstevel@tonic-gate backend_backup_get_prev(char *pathname, size_t pathlen, const char ***out_arg)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	char b_start, b_end;
5177c478bd9Sstevel@tonic-gate 	DIR *dir;
5187c478bd9Sstevel@tonic-gate 	char **out = NULL;
5197c478bd9Sstevel@tonic-gate 	char *name, *p;
5207c478bd9Sstevel@tonic-gate 	char *dirname, *basename;
5217c478bd9Sstevel@tonic-gate 	char *pathend;
5227c478bd9Sstevel@tonic-gate 	struct dirent *ent;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	size_t count = 0;
5257c478bd9Sstevel@tonic-gate 	size_t baselen;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	/*
5287c478bd9Sstevel@tonic-gate 	 * year, month, day, hour, min, sec, plus an '_'.
5297c478bd9Sstevel@tonic-gate 	 */
5307c478bd9Sstevel@tonic-gate 	const size_t ndigits = 4 + 5*2 + 1;
5317c478bd9Sstevel@tonic-gate 	const size_t baroffset = 4 + 2*2;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	size_t idx;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	pathend = pathname + pathlen;
5367c478bd9Sstevel@tonic-gate 	b_end = *pathend;
5377c478bd9Sstevel@tonic-gate 	*pathend = '\0';
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	basename = strrchr(pathname, '/');
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (basename != NULL) {
5427c478bd9Sstevel@tonic-gate 		assert(pathend > pathname && basename < pathend);
5437c478bd9Sstevel@tonic-gate 		basename++;
5447c478bd9Sstevel@tonic-gate 		dirname = pathname;
5457c478bd9Sstevel@tonic-gate 	} else {
5467c478bd9Sstevel@tonic-gate 		basename = pathname;
5477c478bd9Sstevel@tonic-gate 		dirname = ".";
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	baselen = strlen(basename);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * munge the string temporarily for the opendir(), then restore it.
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 	b_start = basename[0];
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	basename[0] = '\0';
5587c478bd9Sstevel@tonic-gate 	dir = opendir(dirname);
5597c478bd9Sstevel@tonic-gate 	basename[0] = b_start;		/* restore path */
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	if (dir == NULL)
5627c478bd9Sstevel@tonic-gate 		goto fail;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	while ((ent = readdir(dir)) != NULL) {
5667c478bd9Sstevel@tonic-gate 		/*
5677c478bd9Sstevel@tonic-gate 		 * Must match:
5687c478bd9Sstevel@tonic-gate 		 *	basename-YYYYMMDD_HHMMSS
5697c478bd9Sstevel@tonic-gate 		 * or we ignore it.
5707c478bd9Sstevel@tonic-gate 		 */
5717c478bd9Sstevel@tonic-gate 		if (strncmp(ent->d_name, basename, baselen) != 0)
5727c478bd9Sstevel@tonic-gate 			continue;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		name = ent->d_name;
5757c478bd9Sstevel@tonic-gate 		if (name[baselen] != '-')
5767c478bd9Sstevel@tonic-gate 			continue;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		p = name + baselen + 1;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < ndigits; idx++) {
5817c478bd9Sstevel@tonic-gate 			char c = p[idx];
5827c478bd9Sstevel@tonic-gate 			if (idx == baroffset && c != '_')
5837c478bd9Sstevel@tonic-gate 				break;
5847c478bd9Sstevel@tonic-gate 			if (idx != baroffset && (c < '0' || c > '9'))
5857c478bd9Sstevel@tonic-gate 				break;
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 		if (idx != ndigits || p[idx] != '\0')
5887c478bd9Sstevel@tonic-gate 			continue;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		/*
5917c478bd9Sstevel@tonic-gate 		 * We have a match.  insertion-sort it into our list.
5927c478bd9Sstevel@tonic-gate 		 */
5937c478bd9Sstevel@tonic-gate 		name = strdup(name);
5947c478bd9Sstevel@tonic-gate 		if (name == NULL)
5957c478bd9Sstevel@tonic-gate 			goto fail_closedir;
5967c478bd9Sstevel@tonic-gate 		p = strrchr(name, '-');
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < count; idx++) {
5997c478bd9Sstevel@tonic-gate 			char *tmp = out[idx];
6007c478bd9Sstevel@tonic-gate 			char *tp = strrchr(tmp, '-');
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 			int cmp = strcmp(p, tp);
6037c478bd9Sstevel@tonic-gate 			if (cmp == 0)
6047c478bd9Sstevel@tonic-gate 				cmp = strcmp(name, tmp);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 			if (cmp == 0) {
6077c478bd9Sstevel@tonic-gate 				free(name);
6087c478bd9Sstevel@tonic-gate 				name = NULL;
6097c478bd9Sstevel@tonic-gate 				break;
6107c478bd9Sstevel@tonic-gate 			} else if (cmp > 0) {
6117c478bd9Sstevel@tonic-gate 				out[idx] = name;
6127c478bd9Sstevel@tonic-gate 				name = tmp;
6137c478bd9Sstevel@tonic-gate 				p = tp;
6147c478bd9Sstevel@tonic-gate 			}
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 		if (idx == count) {
6187c478bd9Sstevel@tonic-gate 			char **new_out = realloc(out,
6197c478bd9Sstevel@tonic-gate 			    (count + 1) * sizeof (*out));
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 			if (new_out == NULL) {
6227c478bd9Sstevel@tonic-gate 				free(name);
6237c478bd9Sstevel@tonic-gate 				goto fail_closedir;
6247c478bd9Sstevel@tonic-gate 			}
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 			out = new_out;
6277c478bd9Sstevel@tonic-gate 			out[count++] = name;
6287c478bd9Sstevel@tonic-gate 		} else {
6297c478bd9Sstevel@tonic-gate 			assert(name == NULL);
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 	(void) closedir(dir);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	basename[baselen] = b_end;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	*out_arg = (const char **)out;
6377c478bd9Sstevel@tonic-gate 	return (count);
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate fail_closedir:
6407c478bd9Sstevel@tonic-gate 	(void) closedir(dir);
6417c478bd9Sstevel@tonic-gate fail:
6427c478bd9Sstevel@tonic-gate 	basename[0] = b_start;
6437c478bd9Sstevel@tonic-gate 	*pathend = b_end;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	backend_backup_cleanup((const char **)out, count);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	*out_arg = NULL;
6487c478bd9Sstevel@tonic-gate 	return (-1);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate  * Copies the repository path into out, a buffer of out_len bytes,
6537c478bd9Sstevel@tonic-gate  * removes the ".db" (or whatever) extension, and, if name is non-NULL,
6547c478bd9Sstevel@tonic-gate  * appends "-name" to it.  If name is non-NULL, it can fail with:
6557c478bd9Sstevel@tonic-gate  *
6567c478bd9Sstevel@tonic-gate  *	_TRUNCATED	will not fit in buffer.
6577c478bd9Sstevel@tonic-gate  *	_BAD_REQUEST	name is not a valid identifier
6587c478bd9Sstevel@tonic-gate  */
6597c478bd9Sstevel@tonic-gate static rep_protocol_responseid_t
6607c478bd9Sstevel@tonic-gate backend_backup_base(sqlite_backend_t *be, const char *name,
6617c478bd9Sstevel@tonic-gate     char *out, size_t out_len)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	char *p, *q;
6647c478bd9Sstevel@tonic-gate 	size_t len;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/*
6677c478bd9Sstevel@tonic-gate 	 * for paths of the form /path/to/foo.db, we truncate at the final
6687c478bd9Sstevel@tonic-gate 	 * '.'.
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	(void) strlcpy(out, be->be_path, out_len);
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	p = strrchr(out, '/');
6737c478bd9Sstevel@tonic-gate 	q = strrchr(out, '.');
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if (p != NULL && q != NULL && q > p)
6767c478bd9Sstevel@tonic-gate 		*q = 0;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	if (name != NULL) {
6797c478bd9Sstevel@tonic-gate 		len = strlen(out);
6807c478bd9Sstevel@tonic-gate 		assert(len < out_len);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		out += len;
6837c478bd9Sstevel@tonic-gate 		out_len -= len;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 		len = strlen(name);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		/*
6887c478bd9Sstevel@tonic-gate 		 * verify that the name tag is entirely alphabetic,
6897c478bd9Sstevel@tonic-gate 		 * non-empty, and not too long.
6907c478bd9Sstevel@tonic-gate 		 */
6917c478bd9Sstevel@tonic-gate 		if (len == 0 || len >= REP_PROTOCOL_NAME_LEN ||
6927c478bd9Sstevel@tonic-gate 		    uu_check_name(name, UU_NAME_DOMAIN) < 0)
6937c478bd9Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		if (snprintf(out, out_len, "-%s", name) >= out_len)
6967c478bd9Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_TRUNCATED);
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate /*
703*8918dff3Sjwadams  * See if a backup is needed.  We do a backup unless both files are
704*8918dff3Sjwadams  * byte-for-byte identical.
705*8918dff3Sjwadams  */
706*8918dff3Sjwadams static int
707*8918dff3Sjwadams backend_check_backup_needed(const char *rep_name, const char *backup_name)
708*8918dff3Sjwadams {
709*8918dff3Sjwadams 	int repfd = open(rep_name, O_RDONLY);
710*8918dff3Sjwadams 	int fd = open(backup_name, O_RDONLY);
711*8918dff3Sjwadams 	struct stat s_rep, s_backup;
712*8918dff3Sjwadams 	int c1, c2;
713*8918dff3Sjwadams 
714*8918dff3Sjwadams 	FILE *f_rep = NULL;
715*8918dff3Sjwadams 	FILE *f_backup = NULL;
716*8918dff3Sjwadams 
717*8918dff3Sjwadams 	if (repfd < 0 || fd < 0)
718*8918dff3Sjwadams 		goto fail;
719*8918dff3Sjwadams 
720*8918dff3Sjwadams 	if (fstat(repfd, &s_rep) < 0 || fstat(fd, &s_backup) < 0)
721*8918dff3Sjwadams 		goto fail;
722*8918dff3Sjwadams 
723*8918dff3Sjwadams 	/*
724*8918dff3Sjwadams 	 * if they are the same file, we need to do a backup to break the
725*8918dff3Sjwadams 	 * hard link or symlink involved.
726*8918dff3Sjwadams 	 */
727*8918dff3Sjwadams 	if (s_rep.st_ino == s_backup.st_ino && s_rep.st_dev == s_backup.st_dev)
728*8918dff3Sjwadams 		goto fail;
729*8918dff3Sjwadams 
730*8918dff3Sjwadams 	if (s_rep.st_size != s_backup.st_size)
731*8918dff3Sjwadams 		goto fail;
732*8918dff3Sjwadams 
733*8918dff3Sjwadams 	if ((f_rep = fdopen(repfd, "r")) == NULL ||
734*8918dff3Sjwadams 	    (f_backup = fdopen(fd, "r")) == NULL)
735*8918dff3Sjwadams 		goto fail;
736*8918dff3Sjwadams 
737*8918dff3Sjwadams 	do {
738*8918dff3Sjwadams 		c1 = getc(f_rep);
739*8918dff3Sjwadams 		c2 = getc(f_backup);
740*8918dff3Sjwadams 		if (c1 != c2)
741*8918dff3Sjwadams 			goto fail;
742*8918dff3Sjwadams 	} while (c1 != EOF);
743*8918dff3Sjwadams 
744*8918dff3Sjwadams 	if (!ferror(f_rep) && !ferror(f_backup)) {
745*8918dff3Sjwadams 		(void) fclose(f_rep);
746*8918dff3Sjwadams 		(void) fclose(f_backup);
747*8918dff3Sjwadams 		(void) close(repfd);
748*8918dff3Sjwadams 		(void) close(fd);
749*8918dff3Sjwadams 		return (0);
750*8918dff3Sjwadams 	}
751*8918dff3Sjwadams 
752*8918dff3Sjwadams fail:
753*8918dff3Sjwadams 	if (f_rep != NULL)
754*8918dff3Sjwadams 		(void) fclose(f_rep);
755*8918dff3Sjwadams 	if (f_backup != NULL)
756*8918dff3Sjwadams 		(void) fclose(f_backup);
757*8918dff3Sjwadams 	if (repfd >= 0)
758*8918dff3Sjwadams 		(void) close(repfd);
759*8918dff3Sjwadams 	if (fd >= 0)
760*8918dff3Sjwadams 		(void) close(fd);
761*8918dff3Sjwadams 	return (1);
762*8918dff3Sjwadams }
763*8918dff3Sjwadams 
764*8918dff3Sjwadams /*
7657c478bd9Sstevel@tonic-gate  * Can return:
7667c478bd9Sstevel@tonic-gate  *	_BAD_REQUEST		name is not valid
7677c478bd9Sstevel@tonic-gate  *	_TRUNCATED		name is too long for current repository path
7687c478bd9Sstevel@tonic-gate  *	_UNKNOWN		failed for unknown reason (details written to
7697c478bd9Sstevel@tonic-gate  *				console)
7707c478bd9Sstevel@tonic-gate  *	_BACKEND_READONLY	backend is not writable
7717c478bd9Sstevel@tonic-gate  *
7727c478bd9Sstevel@tonic-gate  *	_SUCCESS		Backup completed successfully.
7737c478bd9Sstevel@tonic-gate  */
7747c478bd9Sstevel@tonic-gate static rep_protocol_responseid_t
7757c478bd9Sstevel@tonic-gate backend_create_backup_locked(sqlite_backend_t *be, const char *name)
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate 	const char **old_list;
7787c478bd9Sstevel@tonic-gate 	ssize_t old_sz;
7797c478bd9Sstevel@tonic-gate 	ssize_t old_max = max_repository_backups;
7807c478bd9Sstevel@tonic-gate 	ssize_t cur;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	char *finalname;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	char finalpath[PATH_MAX];
7857c478bd9Sstevel@tonic-gate 	char tmppath[PATH_MAX];
7867c478bd9Sstevel@tonic-gate 	char buf[8192];
7877c478bd9Sstevel@tonic-gate 	int infd, outfd;
7887c478bd9Sstevel@tonic-gate 	size_t len;
7897c478bd9Sstevel@tonic-gate 	off_t inlen, outlen, offset;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	time_t now;
7927c478bd9Sstevel@tonic-gate 	struct tm now_tm;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	rep_protocol_responseid_t result;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if (be->be_readonly)
7977c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_BACKEND_READONLY);
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	result = backend_backup_base(be, name, finalpath, sizeof (finalpath));
8007c478bd9Sstevel@tonic-gate 	if (result != REP_PROTOCOL_SUCCESS)
8017c478bd9Sstevel@tonic-gate 		return (result);
8027c478bd9Sstevel@tonic-gate 
803*8918dff3Sjwadams 	if (!backend_check_backup_needed(be->be_path, finalpath)) {
804*8918dff3Sjwadams 		return (REP_PROTOCOL_SUCCESS);
805*8918dff3Sjwadams 	}
806*8918dff3Sjwadams 
8077c478bd9Sstevel@tonic-gate 	/*
8087c478bd9Sstevel@tonic-gate 	 * remember the original length, and the basename location
8097c478bd9Sstevel@tonic-gate 	 */
8107c478bd9Sstevel@tonic-gate 	len = strlen(finalpath);
8117c478bd9Sstevel@tonic-gate 	finalname = strrchr(finalpath, '/');
8127c478bd9Sstevel@tonic-gate 	if (finalname != NULL)
8137c478bd9Sstevel@tonic-gate 		finalname++;
8147c478bd9Sstevel@tonic-gate 	else
8157c478bd9Sstevel@tonic-gate 		finalname = finalpath;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmppath, finalpath, sizeof (tmppath));
8187c478bd9Sstevel@tonic-gate 	if (strlcat(tmppath, "-tmpXXXXXX", sizeof (tmppath)) >=
8197c478bd9Sstevel@tonic-gate 	    sizeof (tmppath))
8207c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_TRUNCATED);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	now = time(NULL);
8237c478bd9Sstevel@tonic-gate 	if (localtime_r(&now, &now_tm) == NULL) {
8247c478bd9Sstevel@tonic-gate 		configd_critical(
8257c478bd9Sstevel@tonic-gate 		    "\"%s\" backup failed: localtime(3C) failed: %s\n", name,
8267c478bd9Sstevel@tonic-gate 		    be->be_path, strerror(errno));
8277c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_UNKNOWN);
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	if (strftime(finalpath + len, sizeof (finalpath) - len,
8317c478bd9Sstevel@tonic-gate 	    "-%Y""%m""%d""_""%H""%M""%S", &now_tm) >=
8327c478bd9Sstevel@tonic-gate 	    sizeof (finalpath) - len) {
8337c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_TRUNCATED);
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	infd = open(be->be_path, O_RDONLY);
8377c478bd9Sstevel@tonic-gate 	if (infd < 0) {
8387c478bd9Sstevel@tonic-gate 		configd_critical("\"%s\" backup failed: opening %s: %s\n", name,
8397c478bd9Sstevel@tonic-gate 		    be->be_path, strerror(errno));
8407c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_UNKNOWN);
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	outfd = mkstemp(tmppath);
8447c478bd9Sstevel@tonic-gate 	if (outfd < 0) {
8457c478bd9Sstevel@tonic-gate 		configd_critical("\"%s\" backup failed: mkstemp(%s): %s\n",
8467c478bd9Sstevel@tonic-gate 		    name, tmppath, strerror(errno));
8477c478bd9Sstevel@tonic-gate 		(void) close(infd);
8487c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_UNKNOWN);
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	for (;;) {
8527c478bd9Sstevel@tonic-gate 		do {
8537c478bd9Sstevel@tonic-gate 			inlen = read(infd, buf, sizeof (buf));
8547c478bd9Sstevel@tonic-gate 		} while (inlen < 0 && errno == EINTR);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 		if (inlen <= 0)
8577c478bd9Sstevel@tonic-gate 			break;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 		for (offset = 0; offset < inlen; offset += outlen) {
8607c478bd9Sstevel@tonic-gate 			do {
8617c478bd9Sstevel@tonic-gate 				outlen = write(outfd, buf + offset,
8627c478bd9Sstevel@tonic-gate 				    inlen - offset);
8637c478bd9Sstevel@tonic-gate 			} while (outlen < 0 && errno == EINTR);
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 			if (outlen >= 0)
8667c478bd9Sstevel@tonic-gate 				continue;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 			configd_critical(
8697c478bd9Sstevel@tonic-gate 			    "\"%s\" backup failed: write to %s: %s\n",
8707c478bd9Sstevel@tonic-gate 			    name, tmppath, strerror(errno));
8717c478bd9Sstevel@tonic-gate 			result = REP_PROTOCOL_FAIL_UNKNOWN;
8727c478bd9Sstevel@tonic-gate 			goto fail;
8737c478bd9Sstevel@tonic-gate 		}
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	if (inlen < 0) {
8777c478bd9Sstevel@tonic-gate 		configd_critical(
8787c478bd9Sstevel@tonic-gate 		    "\"%s\" backup failed: read from %s: %s\n",
8797c478bd9Sstevel@tonic-gate 		    name, be->be_path, strerror(errno));
8807c478bd9Sstevel@tonic-gate 		goto fail;
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * grab the old list before doing our re-name.
8857c478bd9Sstevel@tonic-gate 	 */
8867c478bd9Sstevel@tonic-gate 	if (old_max > 0)
8877c478bd9Sstevel@tonic-gate 		old_sz = backend_backup_get_prev(finalpath, len, &old_list);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	if (rename(tmppath, finalpath) < 0) {
8907c478bd9Sstevel@tonic-gate 		configd_critical(
8917c478bd9Sstevel@tonic-gate 		    "\"%s\" backup failed: rename(%s, %s): %s\n",
8927c478bd9Sstevel@tonic-gate 		    name, tmppath, finalpath, strerror(errno));
8937c478bd9Sstevel@tonic-gate 		result = REP_PROTOCOL_FAIL_UNKNOWN;
8947c478bd9Sstevel@tonic-gate 		goto fail;
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	tmppath[len] = 0;	/* strip -XXXXXX, for reference symlink */
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	(void) unlink(tmppath);
9007c478bd9Sstevel@tonic-gate 	if (symlink(finalname, tmppath) < 0) {
9017c478bd9Sstevel@tonic-gate 		configd_critical(
9027c478bd9Sstevel@tonic-gate 		    "\"%s\" backup completed, but updating "
9037c478bd9Sstevel@tonic-gate 		    "\"%s\" symlink to \"%s\" failed: %s\n",
9047c478bd9Sstevel@tonic-gate 		    name, tmppath, finalname, strerror(errno));
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	if (old_max > 0 && old_sz > 0) {
9087c478bd9Sstevel@tonic-gate 		/* unlink all but the first (old_max - 1) files */
9097c478bd9Sstevel@tonic-gate 		for (cur = old_max - 1; cur < old_sz; cur++) {
9107c478bd9Sstevel@tonic-gate 			(void) strlcpy(finalname, old_list[cur],
9117c478bd9Sstevel@tonic-gate 			    sizeof (finalpath) - (finalname - finalpath));
9127c478bd9Sstevel@tonic-gate 			if (unlink(finalpath) < 0)
9137c478bd9Sstevel@tonic-gate 				configd_critical(
9147c478bd9Sstevel@tonic-gate 				    "\"%s\" backup completed, but removing old "
9157c478bd9Sstevel@tonic-gate 				    "file \"%s\" failed: %s\n",
9167c478bd9Sstevel@tonic-gate 				    name, finalpath, strerror(errno));
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 		backend_backup_cleanup(old_list, old_sz);
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	result = REP_PROTOCOL_SUCCESS;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate fail:
9257c478bd9Sstevel@tonic-gate 	(void) close(infd);
9267c478bd9Sstevel@tonic-gate 	(void) close(outfd);
9277c478bd9Sstevel@tonic-gate 	if (result != REP_PROTOCOL_SUCCESS)
9287c478bd9Sstevel@tonic-gate 		(void) unlink(tmppath);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	return (result);
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
933*8918dff3Sjwadams static int
934*8918dff3Sjwadams backend_check_readonly(sqlite_backend_t *be, int writing, hrtime_t t)
935*8918dff3Sjwadams {
936*8918dff3Sjwadams 	char *errp;
937*8918dff3Sjwadams 	struct sqlite *new;
938*8918dff3Sjwadams 	int r;
939*8918dff3Sjwadams 
940*8918dff3Sjwadams 	assert(be->be_readonly);
941*8918dff3Sjwadams 	assert(be == bes[BACKEND_TYPE_NORMAL]);
942*8918dff3Sjwadams 
943*8918dff3Sjwadams 	/*
944*8918dff3Sjwadams 	 * If we don't *need* to be writable, only check every once in a
945*8918dff3Sjwadams 	 * while.
946*8918dff3Sjwadams 	 */
947*8918dff3Sjwadams 	if (!writing) {
948*8918dff3Sjwadams 		if ((uint64_t)(t - be->be_lastcheck) <
949*8918dff3Sjwadams 		    BACKEND_READONLY_CHECK_INTERVAL)
950*8918dff3Sjwadams 			return (REP_PROTOCOL_SUCCESS);
951*8918dff3Sjwadams 		be->be_lastcheck = t;
952*8918dff3Sjwadams 	}
953*8918dff3Sjwadams 
954*8918dff3Sjwadams 	new = sqlite_open(be->be_path, 0600, &errp);
955*8918dff3Sjwadams 	if (new == NULL) {
956*8918dff3Sjwadams 		backend_panic("reopening %s: %s\n", be->be_path, errp);
957*8918dff3Sjwadams 		/*NOTREACHED*/
958*8918dff3Sjwadams 	}
959*8918dff3Sjwadams 	r = backend_is_readonly(new, be->be_path);
960*8918dff3Sjwadams 
961*8918dff3Sjwadams 	if (r != SQLITE_OK) {
962*8918dff3Sjwadams 		sqlite_close(new);
963*8918dff3Sjwadams 		if (writing)
964*8918dff3Sjwadams 			return (REP_PROTOCOL_FAIL_BACKEND_READONLY);
965*8918dff3Sjwadams 		return (REP_PROTOCOL_SUCCESS);
966*8918dff3Sjwadams 	}
967*8918dff3Sjwadams 
968*8918dff3Sjwadams 	/*
969*8918dff3Sjwadams 	 * We can write!  Swap the db handles, mark ourself writable,
970*8918dff3Sjwadams 	 * and make a backup.
971*8918dff3Sjwadams 	 */
972*8918dff3Sjwadams 	sqlite_close(be->be_db);
973*8918dff3Sjwadams 	be->be_db = new;
974*8918dff3Sjwadams 	be->be_readonly = 0;
975*8918dff3Sjwadams 
976*8918dff3Sjwadams 	if (backend_create_backup_locked(be, REPOSITORY_BOOT_BACKUP) !=
977*8918dff3Sjwadams 	    REP_PROTOCOL_SUCCESS) {
978*8918dff3Sjwadams 		configd_critical(
979*8918dff3Sjwadams 		    "unable to create \"%s\" backup of \"%s\"\n",
980*8918dff3Sjwadams 		    REPOSITORY_BOOT_BACKUP, be->be_path);
981*8918dff3Sjwadams 	}
982*8918dff3Sjwadams 
983*8918dff3Sjwadams 	return (REP_PROTOCOL_SUCCESS);
984*8918dff3Sjwadams }
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate /*
9877c478bd9Sstevel@tonic-gate  * If t is not BACKEND_TYPE_NORMAL, can fail with
9887c478bd9Sstevel@tonic-gate  *   _BACKEND_ACCESS - backend does not exist
9897c478bd9Sstevel@tonic-gate  *
9907c478bd9Sstevel@tonic-gate  * If writing is nonzero, can also fail with
9917c478bd9Sstevel@tonic-gate  *   _BACKEND_READONLY - backend is read-only
9927c478bd9Sstevel@tonic-gate  */
9937c478bd9Sstevel@tonic-gate static int
9947c478bd9Sstevel@tonic-gate backend_lock(backend_type_t t, int writing, sqlite_backend_t **bep)
9957c478bd9Sstevel@tonic-gate {
9967c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be = NULL;
9977c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	*bep = NULL;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	assert(t == BACKEND_TYPE_NORMAL ||
10027c478bd9Sstevel@tonic-gate 	    t == BACKEND_TYPE_NONPERSIST);
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	be = bes[t];
10057c478bd9Sstevel@tonic-gate 	if (t == BACKEND_TYPE_NORMAL)
10067c478bd9Sstevel@tonic-gate 		assert(be != NULL);		/* should always be there */
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (be == NULL)
10097c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_BACKEND_ACCESS);
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	if (backend_panic_thread != 0)
10127c478bd9Sstevel@tonic-gate 		backend_panic(NULL);		/* don't proceed */
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	ts = gethrtime();
10157c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
10167c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&be->be_lock);
10177c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS_WR(be, writing, bt_lock, ts, vts);
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (backend_panic_thread != 0) {
10207c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&be->be_lock);
10217c478bd9Sstevel@tonic-gate 		backend_panic(NULL);		/* don't proceed */
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	be->be_thread = pthread_self();
10247c478bd9Sstevel@tonic-gate 
1025*8918dff3Sjwadams 	if (be->be_readonly) {
10267c478bd9Sstevel@tonic-gate 		int r;
10277c478bd9Sstevel@tonic-gate 		assert(t == BACKEND_TYPE_NORMAL);
10287c478bd9Sstevel@tonic-gate 
1029*8918dff3Sjwadams 		r = backend_check_readonly(be, writing, ts);
1030*8918dff3Sjwadams 		if (r != REP_PROTOCOL_SUCCESS) {
1031*8918dff3Sjwadams 			be->be_thread = 0;
1032*8918dff3Sjwadams 			(void) pthread_mutex_unlock(&be->be_lock);
1033*8918dff3Sjwadams 			return (r);
10347c478bd9Sstevel@tonic-gate 		}
1035*8918dff3Sjwadams 	}
1036*8918dff3Sjwadams 
1037*8918dff3Sjwadams 	if (writing && t == BACKEND_TYPE_NORMAL &&
1038*8918dff3Sjwadams 	    backend_check_perm(be->be_path) != SQLITE_OK) {
10397c478bd9Sstevel@tonic-gate 		be->be_thread = 0;
10407c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&be->be_lock);
10417c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_BACKEND_READONLY);
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	if (backend_do_trace)
10457c478bd9Sstevel@tonic-gate 		(void) sqlite_trace(be->be_db, backend_trace_sql, be);
10467c478bd9Sstevel@tonic-gate 	else
10477c478bd9Sstevel@tonic-gate 		(void) sqlite_trace(be->be_db, NULL, NULL);
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	be->be_writing = writing;
10507c478bd9Sstevel@tonic-gate 	*bep = be;
10517c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate static void
10557c478bd9Sstevel@tonic-gate backend_unlock(sqlite_backend_t *be)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	be->be_writing = 0;
10587c478bd9Sstevel@tonic-gate 	be->be_thread = 0;
10597c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&be->be_lock);
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate static void
10637c478bd9Sstevel@tonic-gate backend_destroy(sqlite_backend_t *be)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate 	if (be->be_db != NULL) {
10667c478bd9Sstevel@tonic-gate 		sqlite_close(be->be_db);
10677c478bd9Sstevel@tonic-gate 		be->be_db = NULL;
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 	be->be_thread = 0;
10707c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&be->be_lock);
10717c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&be->be_lock);
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate static void
10757c478bd9Sstevel@tonic-gate backend_create_finish(backend_type_t backend_id, sqlite_backend_t *be)
10767c478bd9Sstevel@tonic-gate {
10777c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&be->be_lock));
10787c478bd9Sstevel@tonic-gate 	assert(be == &be_info[backend_id]);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	bes[backend_id] = be;
10817c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&be->be_lock);
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate static int
10857c478bd9Sstevel@tonic-gate backend_fd_write(int fd, const char *mess)
10867c478bd9Sstevel@tonic-gate {
10877c478bd9Sstevel@tonic-gate 	int len = strlen(mess);
10887c478bd9Sstevel@tonic-gate 	int written;
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	while (len > 0) {
10917c478bd9Sstevel@tonic-gate 		if ((written = write(fd, mess, len)) < 0)
10927c478bd9Sstevel@tonic-gate 			return (-1);
10937c478bd9Sstevel@tonic-gate 		mess += written;
10947c478bd9Sstevel@tonic-gate 		len -= written;
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 	return (0);
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate /*
11007c478bd9Sstevel@tonic-gate  * Can return:
11017c478bd9Sstevel@tonic-gate  *	_BAD_REQUEST		name is not valid
11027c478bd9Sstevel@tonic-gate  *	_TRUNCATED		name is too long for current repository path
11037c478bd9Sstevel@tonic-gate  *	_UNKNOWN		failed for unknown reason (details written to
11047c478bd9Sstevel@tonic-gate  *				console)
11057c478bd9Sstevel@tonic-gate  *	_BACKEND_READONLY	backend is not writable
11067c478bd9Sstevel@tonic-gate  *
11077c478bd9Sstevel@tonic-gate  *	_SUCCESS		Backup completed successfully.
11087c478bd9Sstevel@tonic-gate  */
11097c478bd9Sstevel@tonic-gate rep_protocol_responseid_t
11107c478bd9Sstevel@tonic-gate backend_create_backup(const char *name)
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate 	rep_protocol_responseid_t result;
11137c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	result = backend_lock(BACKEND_TYPE_NORMAL, 0, &be);
11167c478bd9Sstevel@tonic-gate 	if (result != REP_PROTOCOL_SUCCESS)
11177c478bd9Sstevel@tonic-gate 		return (result);
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	result = backend_create_backup_locked(be, name);
11207c478bd9Sstevel@tonic-gate 	backend_unlock(be);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	return (result);
11237c478bd9Sstevel@tonic-gate }
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11267c478bd9Sstevel@tonic-gate static int
11277c478bd9Sstevel@tonic-gate backend_integrity_callback(void *private, int narg, char **vals, char **cols)
11287c478bd9Sstevel@tonic-gate {
11297c478bd9Sstevel@tonic-gate 	char **out = private;
11307c478bd9Sstevel@tonic-gate 	char *old = *out;
11317c478bd9Sstevel@tonic-gate 	char *new;
11327c478bd9Sstevel@tonic-gate 	const char *info;
11337c478bd9Sstevel@tonic-gate 	size_t len;
11347c478bd9Sstevel@tonic-gate 	int x;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	for (x = 0; x < narg; x++) {
11377c478bd9Sstevel@tonic-gate 		if ((info = vals[x]) != NULL &&
11387c478bd9Sstevel@tonic-gate 		    strcmp(info, "ok") != 0) {
11397c478bd9Sstevel@tonic-gate 			len = (old == NULL)? 0 : strlen(old);
11407c478bd9Sstevel@tonic-gate 			len += strlen(info) + 2;	/* '\n' + '\0' */
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 			new = realloc(old, len);
11437c478bd9Sstevel@tonic-gate 			if (new == NULL)
11447c478bd9Sstevel@tonic-gate 				return (BACKEND_CALLBACK_ABORT);
11457c478bd9Sstevel@tonic-gate 			if (old == NULL)
11467c478bd9Sstevel@tonic-gate 				new[0] = 0;
11477c478bd9Sstevel@tonic-gate 			old = *out = new;
11487c478bd9Sstevel@tonic-gate 			(void) strlcat(new, info, len);
11497c478bd9Sstevel@tonic-gate 			(void) strlcat(new, "\n", len);
11507c478bd9Sstevel@tonic-gate 		}
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 	return (BACKEND_CALLBACK_CONTINUE);
11537c478bd9Sstevel@tonic-gate }
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate #define	BACKEND_CREATE_LOCKED		-2
11567c478bd9Sstevel@tonic-gate #define	BACKEND_CREATE_FAIL		-1
11577c478bd9Sstevel@tonic-gate #define	BACKEND_CREATE_SUCCESS		0
11587c478bd9Sstevel@tonic-gate #define	BACKEND_CREATE_READONLY		1
11597c478bd9Sstevel@tonic-gate #define	BACKEND_CREATE_NEED_INIT	2
11607c478bd9Sstevel@tonic-gate static int
11617c478bd9Sstevel@tonic-gate backend_create(backend_type_t backend_id, const char *db_file,
11627c478bd9Sstevel@tonic-gate     sqlite_backend_t **bep)
11637c478bd9Sstevel@tonic-gate {
11647c478bd9Sstevel@tonic-gate 	char *errp;
11657c478bd9Sstevel@tonic-gate 	char *integrity_results = NULL;
11667c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
11677c478bd9Sstevel@tonic-gate 	int r;
11687c478bd9Sstevel@tonic-gate 	uint32_t val = -1UL;
11697c478bd9Sstevel@tonic-gate 	struct run_single_int_info info;
11707c478bd9Sstevel@tonic-gate 	int fd;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	assert(backend_id >= 0 && backend_id < BACKEND_TYPE_TOTAL);
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	be = &be_info[backend_id];
11757c478bd9Sstevel@tonic-gate 	assert(be->be_db == NULL);
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&be->be_lock, NULL);
11787c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&be->be_lock);
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	be->be_type = backend_id;
11817c478bd9Sstevel@tonic-gate 	be->be_path = strdup(db_file);
11827c478bd9Sstevel@tonic-gate 	if (be->be_path == NULL) {
11837c478bd9Sstevel@tonic-gate 		perror("malloc");
11847c478bd9Sstevel@tonic-gate 		goto fail;
11857c478bd9Sstevel@tonic-gate 	}
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	be->be_db = sqlite_open(be->be_path, 0600, &errp);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	if (be->be_db == NULL) {
11907c478bd9Sstevel@tonic-gate 		if (strstr(errp, "out of memory") != NULL) {
11917c478bd9Sstevel@tonic-gate 			configd_critical("%s: %s\n", db_file, errp);
11927c478bd9Sstevel@tonic-gate 			free(errp);
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 			goto fail;
11957c478bd9Sstevel@tonic-gate 		}
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 		/* report it as an integrity failure */
11987c478bd9Sstevel@tonic-gate 		integrity_results = errp;
11997c478bd9Sstevel@tonic-gate 		errp = NULL;
12007c478bd9Sstevel@tonic-gate 		goto integrity_fail;
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	/*
12047c478bd9Sstevel@tonic-gate 	 * check if we are inited and of the correct schema version
12057c478bd9Sstevel@tonic-gate 	 *
12067c478bd9Sstevel@tonic-gate 	 * Eventually, we'll support schema upgrade here.
12077c478bd9Sstevel@tonic-gate 	 */
12087c478bd9Sstevel@tonic-gate 	info.rs_out = &val;
12097c478bd9Sstevel@tonic-gate 	info.rs_result = REP_PROTOCOL_FAIL_NOT_FOUND;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	r = sqlite_exec(be->be_db, "SELECT schema_version FROM schema_version;",
12127c478bd9Sstevel@tonic-gate 	    run_single_int_callback, &info, &errp);
12137c478bd9Sstevel@tonic-gate 	if (r == SQLITE_ERROR &&
12147c478bd9Sstevel@tonic-gate 	    strcmp("no such table: schema_version", errp) == 0) {
12157c478bd9Sstevel@tonic-gate 		free(errp);
12167c478bd9Sstevel@tonic-gate 		/*
12177c478bd9Sstevel@tonic-gate 		 * Could be an empty repository, could be pre-schema_version
12187c478bd9Sstevel@tonic-gate 		 * schema.  Check for id_tbl, which has always been there.
12197c478bd9Sstevel@tonic-gate 		 */
12207c478bd9Sstevel@tonic-gate 		r = sqlite_exec(be->be_db, "SELECT count() FROM id_tbl;",
12217c478bd9Sstevel@tonic-gate 		    NULL, NULL, &errp);
12227c478bd9Sstevel@tonic-gate 		if (r == SQLITE_ERROR &&
12237c478bd9Sstevel@tonic-gate 		    strcmp("no such table: id_tbl", errp) == 0) {
12247c478bd9Sstevel@tonic-gate 			free(errp);
12257c478bd9Sstevel@tonic-gate 			*bep = be;
12267c478bd9Sstevel@tonic-gate 			return (BACKEND_CREATE_NEED_INIT);
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 		configd_critical("%s: schema version mismatch\n", db_file);
12307c478bd9Sstevel@tonic-gate 		goto fail;
12317c478bd9Sstevel@tonic-gate 	}
12327c478bd9Sstevel@tonic-gate 	if (r == SQLITE_BUSY || r == SQLITE_LOCKED) {
12337c478bd9Sstevel@tonic-gate 		free(errp);
12347c478bd9Sstevel@tonic-gate 		*bep = NULL;
12357c478bd9Sstevel@tonic-gate 		backend_destroy(be);
12367c478bd9Sstevel@tonic-gate 		return (BACKEND_CREATE_LOCKED);
12377c478bd9Sstevel@tonic-gate 	}
12387c478bd9Sstevel@tonic-gate 	if (r == SQLITE_OK) {
12397c478bd9Sstevel@tonic-gate 		if (info.rs_result == REP_PROTOCOL_FAIL_NOT_FOUND ||
12407c478bd9Sstevel@tonic-gate 		    val != BACKEND_SCHEMA_VERSION) {
12417c478bd9Sstevel@tonic-gate 			configd_critical("%s: schema version mismatch\n",
12427c478bd9Sstevel@tonic-gate 			    db_file);
12437c478bd9Sstevel@tonic-gate 			goto fail;
12447c478bd9Sstevel@tonic-gate 		}
12457c478bd9Sstevel@tonic-gate 	}
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	/*
12487c478bd9Sstevel@tonic-gate 	 * pull in the whole database sequentially.
12497c478bd9Sstevel@tonic-gate 	 */
12507c478bd9Sstevel@tonic-gate 	if ((fd = open(db_file, O_RDONLY)) >= 0) {
12517c478bd9Sstevel@tonic-gate 		size_t sz = 64 * 1024;
12527c478bd9Sstevel@tonic-gate 		char *buffer = malloc(sz);
12537c478bd9Sstevel@tonic-gate 		if (buffer != NULL) {
12547c478bd9Sstevel@tonic-gate 			while (read(fd, buffer, sz) > 0)
12557c478bd9Sstevel@tonic-gate 				;
12567c478bd9Sstevel@tonic-gate 			free(buffer);
12577c478bd9Sstevel@tonic-gate 		}
12587c478bd9Sstevel@tonic-gate 		(void) close(fd);
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	/*
12627c478bd9Sstevel@tonic-gate 	 * run an integrity check
12637c478bd9Sstevel@tonic-gate 	 */
12647c478bd9Sstevel@tonic-gate 	r = sqlite_exec(be->be_db, "PRAGMA integrity_check;",
12657c478bd9Sstevel@tonic-gate 	    backend_integrity_callback, &integrity_results, &errp);
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if (r == SQLITE_BUSY || r == SQLITE_LOCKED) {
12687c478bd9Sstevel@tonic-gate 		free(errp);
12697c478bd9Sstevel@tonic-gate 		*bep = NULL;
12707c478bd9Sstevel@tonic-gate 		backend_destroy(be);
12717c478bd9Sstevel@tonic-gate 		return (BACKEND_CREATE_LOCKED);
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 	if (r == SQLITE_ABORT) {
12747c478bd9Sstevel@tonic-gate 		free(errp);
12757c478bd9Sstevel@tonic-gate 		errp = NULL;
12767c478bd9Sstevel@tonic-gate 		integrity_results = "out of memory running integrity check\n";
12777c478bd9Sstevel@tonic-gate 	} else if (r != SQLITE_OK && integrity_results == NULL) {
12787c478bd9Sstevel@tonic-gate 		integrity_results = errp;
12797c478bd9Sstevel@tonic-gate 		errp = NULL;
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate integrity_fail:
12837c478bd9Sstevel@tonic-gate 	if (integrity_results != NULL) {
12847c478bd9Sstevel@tonic-gate 		const char *fname = "/etc/svc/volatile/db_errors";
12857c478bd9Sstevel@tonic-gate 		if ((fd = open(fname, O_CREAT|O_WRONLY|O_APPEND, 0600)) < 0) {
12867c478bd9Sstevel@tonic-gate 			fname = NULL;
12877c478bd9Sstevel@tonic-gate 		} else {
12887c478bd9Sstevel@tonic-gate 			if (backend_fd_write(fd, "\n\n") < 0 ||
12897c478bd9Sstevel@tonic-gate 			    backend_fd_write(fd, db_file) < 0 ||
12907c478bd9Sstevel@tonic-gate 			    backend_fd_write(fd,
12917c478bd9Sstevel@tonic-gate 			    ": PRAGMA integrity_check; failed.  Results:\n") <
12927c478bd9Sstevel@tonic-gate 			    0 || backend_fd_write(fd, integrity_results) < 0 ||
12937c478bd9Sstevel@tonic-gate 			    backend_fd_write(fd, "\n\n") < 0) {
12947c478bd9Sstevel@tonic-gate 				fname = NULL;
12957c478bd9Sstevel@tonic-gate 			}
12967c478bd9Sstevel@tonic-gate 			(void) close(fd);
12977c478bd9Sstevel@tonic-gate 		}
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 		if (!is_main_repository ||
13007c478bd9Sstevel@tonic-gate 		    backend_id == BACKEND_TYPE_NONPERSIST) {
13017c478bd9Sstevel@tonic-gate 			if (fname != NULL)
13027c478bd9Sstevel@tonic-gate 				configd_critical(
13037c478bd9Sstevel@tonic-gate 				    "%s: integrity check failed. Details in "
13047c478bd9Sstevel@tonic-gate 				    "%s\n", db_file, fname);
13057c478bd9Sstevel@tonic-gate 			else
13067c478bd9Sstevel@tonic-gate 				configd_critical(
13077c478bd9Sstevel@tonic-gate 				    "%s: integrity check failed: %s\n",
13087c478bd9Sstevel@tonic-gate 				    db_file);
13097c478bd9Sstevel@tonic-gate 		} else {
13107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13117c478bd9Sstevel@tonic-gate "\n"
13127c478bd9Sstevel@tonic-gate "svc.configd: smf(5) database integrity check of:\n"
13137c478bd9Sstevel@tonic-gate "\n"
13147c478bd9Sstevel@tonic-gate "    %s\n"
13157c478bd9Sstevel@tonic-gate "\n"
13167c478bd9Sstevel@tonic-gate "  failed. The database might be damaged or a media error might have\n"
13177c478bd9Sstevel@tonic-gate "  prevented it from being verified.  Additional information useful to\n"
13187c478bd9Sstevel@tonic-gate "  your service provider%s%s\n"
13197c478bd9Sstevel@tonic-gate "\n"
13207c478bd9Sstevel@tonic-gate "  The system will not be able to boot until you have restored a working\n"
13217c478bd9Sstevel@tonic-gate "  database.  svc.startd(1M) will provide a sulogin(1M) prompt for recovery\n"
13227c478bd9Sstevel@tonic-gate "  purposes.  The command:\n"
13237c478bd9Sstevel@tonic-gate "\n"
13247c478bd9Sstevel@tonic-gate "    /lib/svc/bin/restore_repository\n"
13257c478bd9Sstevel@tonic-gate "\n"
13267c478bd9Sstevel@tonic-gate "  can be run to restore a backup version of your repository.  See\n"
13277c478bd9Sstevel@tonic-gate "  http://sun.com/msg/SMF-8000-MY for more information.\n"
13287c478bd9Sstevel@tonic-gate "\n",
13297c478bd9Sstevel@tonic-gate 			db_file,
13307c478bd9Sstevel@tonic-gate 			(fname == NULL)? ":\n\n" : " is in:\n\n    ",
13317c478bd9Sstevel@tonic-gate 			(fname == NULL)? integrity_results : fname);
13327c478bd9Sstevel@tonic-gate 		}
13337c478bd9Sstevel@tonic-gate 		free(errp);
13347c478bd9Sstevel@tonic-gate 		goto fail;
13357c478bd9Sstevel@tonic-gate 	}
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	/*
13387c478bd9Sstevel@tonic-gate 	 * check if we are writable
13397c478bd9Sstevel@tonic-gate 	 */
1340*8918dff3Sjwadams 	r = backend_is_readonly(be->be_db, be->be_path);
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	if (r == SQLITE_BUSY || r == SQLITE_LOCKED) {
13437c478bd9Sstevel@tonic-gate 		free(errp);
13447c478bd9Sstevel@tonic-gate 		*bep = NULL;
13457c478bd9Sstevel@tonic-gate 		backend_destroy(be);
13467c478bd9Sstevel@tonic-gate 		return (BACKEND_CREATE_LOCKED);
13477c478bd9Sstevel@tonic-gate 	}
13487c478bd9Sstevel@tonic-gate 	if (r != SQLITE_OK && r != SQLITE_FULL) {
13497c478bd9Sstevel@tonic-gate 		free(errp);
13507c478bd9Sstevel@tonic-gate 		be->be_readonly = 1;
13517c478bd9Sstevel@tonic-gate 		*bep = be;
13527c478bd9Sstevel@tonic-gate 		return (BACKEND_CREATE_READONLY);
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 	*bep = be;
13557c478bd9Sstevel@tonic-gate 	return (BACKEND_CREATE_SUCCESS);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate fail:
13587c478bd9Sstevel@tonic-gate 	*bep = NULL;
13597c478bd9Sstevel@tonic-gate 	backend_destroy(be);
13607c478bd9Sstevel@tonic-gate 	return (BACKEND_CREATE_FAIL);
13617c478bd9Sstevel@tonic-gate }
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate /*
13647c478bd9Sstevel@tonic-gate  * (arg & -arg) is, through the magic of twos-complement arithmetic, the
13657c478bd9Sstevel@tonic-gate  * lowest set bit in arg.
13667c478bd9Sstevel@tonic-gate  */
13677c478bd9Sstevel@tonic-gate static size_t
13687c478bd9Sstevel@tonic-gate round_up_to_p2(size_t arg)
13697c478bd9Sstevel@tonic-gate {
13707c478bd9Sstevel@tonic-gate 	/*
13717c478bd9Sstevel@tonic-gate 	 * Don't allow a zero result.
13727c478bd9Sstevel@tonic-gate 	 */
13737c478bd9Sstevel@tonic-gate 	assert(arg > 0 && ((ssize_t)arg > 0));
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	while ((arg & (arg - 1)) != 0)
13767c478bd9Sstevel@tonic-gate 		arg += (arg & -arg);
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	return (arg);
13797c478bd9Sstevel@tonic-gate }
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate /*
13827c478bd9Sstevel@tonic-gate  * Returns
13837c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
13847c478bd9Sstevel@tonic-gate  *   _BACKEND_ACCESS - backend type t (other than _NORMAL) doesn't exist
13857c478bd9Sstevel@tonic-gate  *   _DONE - callback aborted query
13867c478bd9Sstevel@tonic-gate  *   _SUCCESS
13877c478bd9Sstevel@tonic-gate  */
13887c478bd9Sstevel@tonic-gate int
13897c478bd9Sstevel@tonic-gate backend_run(backend_type_t t, backend_query_t *q,
13907c478bd9Sstevel@tonic-gate     backend_run_callback_f *cb, void *data)
13917c478bd9Sstevel@tonic-gate {
13927c478bd9Sstevel@tonic-gate 	char *errmsg = NULL;
13937c478bd9Sstevel@tonic-gate 	int ret;
13947c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
13957c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	if (q == NULL || q->bq_buf == NULL)
13987c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	if ((ret = backend_lock(t, 0, &be)) != REP_PROTOCOL_SUCCESS)
14017c478bd9Sstevel@tonic-gate 		return (ret);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	ts = gethrtime();
14047c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
14057c478bd9Sstevel@tonic-gate 	ret = sqlite_exec(be->be_db, q->bq_buf, cb, data, &errmsg);
14067c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
14077c478bd9Sstevel@tonic-gate 	ret = backend_error(be, ret, errmsg);
14087c478bd9Sstevel@tonic-gate 	backend_unlock(be);
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	return (ret);
14117c478bd9Sstevel@tonic-gate }
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate /*
14147c478bd9Sstevel@tonic-gate  * Starts a "read-only" transaction -- i.e., locks out writers as long
14157c478bd9Sstevel@tonic-gate  * as it is active.
14167c478bd9Sstevel@tonic-gate  *
14177c478bd9Sstevel@tonic-gate  * Fails with
14187c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
14197c478bd9Sstevel@tonic-gate  *
14207c478bd9Sstevel@tonic-gate  * If t is not _NORMAL, can also fail with
14217c478bd9Sstevel@tonic-gate  *   _BACKEND_ACCESS - backend does not exist
14227c478bd9Sstevel@tonic-gate  *
14237c478bd9Sstevel@tonic-gate  * If writable is true, can also fail with
14247c478bd9Sstevel@tonic-gate  *   _BACKEND_READONLY
14257c478bd9Sstevel@tonic-gate  */
14267c478bd9Sstevel@tonic-gate static int
14277c478bd9Sstevel@tonic-gate backend_tx_begin_common(backend_type_t t, backend_tx_t **txp, int writable)
14287c478bd9Sstevel@tonic-gate {
14297c478bd9Sstevel@tonic-gate 	backend_tx_t *ret;
14307c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
14317c478bd9Sstevel@tonic-gate 	int r;
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	*txp = NULL;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (*ret));
14367c478bd9Sstevel@tonic-gate 	if (ret == NULL)
14377c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	if ((r = backend_lock(t, writable, &be)) != REP_PROTOCOL_SUCCESS) {
14407c478bd9Sstevel@tonic-gate 		uu_free(ret);
14417c478bd9Sstevel@tonic-gate 		return (r);
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	ret->bt_be = be;
14457c478bd9Sstevel@tonic-gate 	ret->bt_readonly = !writable;
14467c478bd9Sstevel@tonic-gate 	ret->bt_type = t;
14477c478bd9Sstevel@tonic-gate 	ret->bt_full = 0;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	*txp = ret;
14507c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
14517c478bd9Sstevel@tonic-gate }
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate int
14547c478bd9Sstevel@tonic-gate backend_tx_begin_ro(backend_type_t t, backend_tx_t **txp)
14557c478bd9Sstevel@tonic-gate {
14567c478bd9Sstevel@tonic-gate 	return (backend_tx_begin_common(t, txp, 0));
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate static void
14607c478bd9Sstevel@tonic-gate backend_tx_end(backend_tx_t *tx)
14617c478bd9Sstevel@tonic-gate {
14627c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	if (tx->bt_full) {
14677c478bd9Sstevel@tonic-gate 		struct sqlite *new;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 		/*
14707c478bd9Sstevel@tonic-gate 		 * sqlite tends to be sticky with SQLITE_FULL, so we try
14717c478bd9Sstevel@tonic-gate 		 * to get a fresh database handle if we got a FULL warning
14727c478bd9Sstevel@tonic-gate 		 * along the way.  If that fails, no harm done.
14737c478bd9Sstevel@tonic-gate 		 */
14747c478bd9Sstevel@tonic-gate 		new = sqlite_open(be->be_path, 0600, NULL);
14757c478bd9Sstevel@tonic-gate 		if (new != NULL) {
14767c478bd9Sstevel@tonic-gate 			sqlite_close(be->be_db);
14777c478bd9Sstevel@tonic-gate 			be->be_db = new;
14787c478bd9Sstevel@tonic-gate 		}
14797c478bd9Sstevel@tonic-gate 	}
14807c478bd9Sstevel@tonic-gate 	backend_unlock(be);
14817c478bd9Sstevel@tonic-gate 	tx->bt_be = NULL;
14827c478bd9Sstevel@tonic-gate 	uu_free(tx);
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate void
14867c478bd9Sstevel@tonic-gate backend_tx_end_ro(backend_tx_t *tx)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate 	assert(tx->bt_readonly);
14897c478bd9Sstevel@tonic-gate 	backend_tx_end(tx);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate /*
14937c478bd9Sstevel@tonic-gate  * Fails with
14947c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
14957c478bd9Sstevel@tonic-gate  *   _BACKEND_ACCESS
14967c478bd9Sstevel@tonic-gate  *   _BACKEND_READONLY
14977c478bd9Sstevel@tonic-gate  */
14987c478bd9Sstevel@tonic-gate int
14997c478bd9Sstevel@tonic-gate backend_tx_begin(backend_type_t t, backend_tx_t **txp)
15007c478bd9Sstevel@tonic-gate {
15017c478bd9Sstevel@tonic-gate 	int r;
15027c478bd9Sstevel@tonic-gate 	char *errmsg;
15037c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	r = backend_tx_begin_common(t, txp, 1);
15067c478bd9Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS)
15077c478bd9Sstevel@tonic-gate 		return (r);
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	ts = gethrtime();
15107c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
15117c478bd9Sstevel@tonic-gate 	r = sqlite_exec((*txp)->bt_be->be_db, "BEGIN TRANSACTION", NULL, NULL,
15127c478bd9Sstevel@tonic-gate 	    &errmsg);
15137c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS((*txp)->bt_be, bt_exec, ts, vts);
15147c478bd9Sstevel@tonic-gate 	if (r == SQLITE_FULL)
15157c478bd9Sstevel@tonic-gate 		(*txp)->bt_full = 1;
15167c478bd9Sstevel@tonic-gate 	r = backend_error((*txp)->bt_be, r, errmsg);
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS) {
15197c478bd9Sstevel@tonic-gate 		assert(r != REP_PROTOCOL_DONE);
15207c478bd9Sstevel@tonic-gate 		(void) sqlite_exec((*txp)->bt_be->be_db,
15217c478bd9Sstevel@tonic-gate 		    "ROLLBACK TRANSACTION", NULL, NULL, NULL);
15227c478bd9Sstevel@tonic-gate 		backend_tx_end(*txp);
15237c478bd9Sstevel@tonic-gate 		*txp = NULL;
15247c478bd9Sstevel@tonic-gate 		return (r);
15257c478bd9Sstevel@tonic-gate 	}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	(*txp)->bt_readonly = 0;
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate void
15337c478bd9Sstevel@tonic-gate backend_tx_rollback(backend_tx_t *tx)
15347c478bd9Sstevel@tonic-gate {
15357c478bd9Sstevel@tonic-gate 	int r;
15367c478bd9Sstevel@tonic-gate 	char *errmsg;
15377c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
15387c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly);
15417c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	ts = gethrtime();
15447c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
15457c478bd9Sstevel@tonic-gate 	r = sqlite_exec(be->be_db, "ROLLBACK TRANSACTION", NULL, NULL,
15467c478bd9Sstevel@tonic-gate 	    &errmsg);
15477c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
15487c478bd9Sstevel@tonic-gate 	if (r == SQLITE_FULL)
15497c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
15507c478bd9Sstevel@tonic-gate 	(void) backend_error(be, r, errmsg);
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	backend_tx_end(tx);
15537c478bd9Sstevel@tonic-gate }
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate /*
15567c478bd9Sstevel@tonic-gate  * Fails with
15577c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
15587c478bd9Sstevel@tonic-gate  */
15597c478bd9Sstevel@tonic-gate int
15607c478bd9Sstevel@tonic-gate backend_tx_commit(backend_tx_t *tx)
15617c478bd9Sstevel@tonic-gate {
15627c478bd9Sstevel@tonic-gate 	int r, r2;
15637c478bd9Sstevel@tonic-gate 	char *errmsg;
15647c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
15657c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly);
15687c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
15697c478bd9Sstevel@tonic-gate 	ts = gethrtime();
15707c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
15717c478bd9Sstevel@tonic-gate 	r = sqlite_exec(be->be_db, "COMMIT TRANSACTION", NULL, NULL,
15727c478bd9Sstevel@tonic-gate 	    &errmsg);
15737c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
15747c478bd9Sstevel@tonic-gate 	if (r == SQLITE_FULL)
15757c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	r = backend_error(be, r, errmsg);
15787c478bd9Sstevel@tonic-gate 	assert(r != REP_PROTOCOL_DONE);
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS) {
15817c478bd9Sstevel@tonic-gate 		r2 = sqlite_exec(be->be_db, "ROLLBACK TRANSACTION", NULL, NULL,
15827c478bd9Sstevel@tonic-gate 		    &errmsg);
15837c478bd9Sstevel@tonic-gate 		r2 = backend_error(be, r2, errmsg);
15847c478bd9Sstevel@tonic-gate 		if (r2 != REP_PROTOCOL_SUCCESS)
15857c478bd9Sstevel@tonic-gate 			backend_panic("cannot rollback failed commit");
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 		backend_tx_end(tx);
15887c478bd9Sstevel@tonic-gate 		return (r);
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 	backend_tx_end(tx);
15917c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
15927c478bd9Sstevel@tonic-gate }
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate static const char *
15957c478bd9Sstevel@tonic-gate id_space_to_name(enum id_space id)
15967c478bd9Sstevel@tonic-gate {
15977c478bd9Sstevel@tonic-gate 	switch (id) {
15987c478bd9Sstevel@tonic-gate 	case BACKEND_ID_SERVICE_INSTANCE:
15997c478bd9Sstevel@tonic-gate 		return ("SI");
16007c478bd9Sstevel@tonic-gate 	case BACKEND_ID_PROPERTYGRP:
16017c478bd9Sstevel@tonic-gate 		return ("PG");
16027c478bd9Sstevel@tonic-gate 	case BACKEND_ID_GENERATION:
16037c478bd9Sstevel@tonic-gate 		return ("GEN");
16047c478bd9Sstevel@tonic-gate 	case BACKEND_ID_PROPERTY:
16057c478bd9Sstevel@tonic-gate 		return ("PROP");
16067c478bd9Sstevel@tonic-gate 	case BACKEND_ID_VALUE:
16077c478bd9Sstevel@tonic-gate 		return ("VAL");
16087c478bd9Sstevel@tonic-gate 	case BACKEND_ID_SNAPNAME:
16097c478bd9Sstevel@tonic-gate 		return ("SNAME");
16107c478bd9Sstevel@tonic-gate 	case BACKEND_ID_SNAPSHOT:
16117c478bd9Sstevel@tonic-gate 		return ("SHOT");
16127c478bd9Sstevel@tonic-gate 	case BACKEND_ID_SNAPLEVEL:
16137c478bd9Sstevel@tonic-gate 		return ("SLVL");
16147c478bd9Sstevel@tonic-gate 	default:
16157c478bd9Sstevel@tonic-gate 		abort();
16167c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate }
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate /*
16217c478bd9Sstevel@tonic-gate  * Returns a new id or 0 if the id argument is invalid or the query fails.
16227c478bd9Sstevel@tonic-gate  */
16237c478bd9Sstevel@tonic-gate uint32_t
16247c478bd9Sstevel@tonic-gate backend_new_id(backend_tx_t *tx, enum id_space id)
16257c478bd9Sstevel@tonic-gate {
16267c478bd9Sstevel@tonic-gate 	struct run_single_int_info info;
16277c478bd9Sstevel@tonic-gate 	uint32_t new_id = 0;
16287c478bd9Sstevel@tonic-gate 	const char *name = id_space_to_name(id);
16297c478bd9Sstevel@tonic-gate 	char *errmsg;
16307c478bd9Sstevel@tonic-gate 	int ret;
16317c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
16327c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly);
16357c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	info.rs_out = &new_id;
16387c478bd9Sstevel@tonic-gate 	info.rs_result = REP_PROTOCOL_FAIL_NOT_FOUND;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	ts = gethrtime();
16417c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
16427c478bd9Sstevel@tonic-gate 	ret = sqlite_exec_printf(be->be_db,
16437c478bd9Sstevel@tonic-gate 	    "SELECT id_next FROM id_tbl WHERE (id_name = '%q');"
16447c478bd9Sstevel@tonic-gate 	    "UPDATE id_tbl SET id_next = id_next + 1 WHERE (id_name = '%q');",
16457c478bd9Sstevel@tonic-gate 	    run_single_int_callback, &info, &errmsg, name, name);
16467c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
16477c478bd9Sstevel@tonic-gate 	if (ret == SQLITE_FULL)
16487c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	ret = backend_error(be, ret, errmsg);
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	if (ret != REP_PROTOCOL_SUCCESS) {
16537c478bd9Sstevel@tonic-gate 		return (0);
16547c478bd9Sstevel@tonic-gate 	}
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	return (new_id);
16577c478bd9Sstevel@tonic-gate }
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate /*
16607c478bd9Sstevel@tonic-gate  * Returns
16617c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
16627c478bd9Sstevel@tonic-gate  *   _DONE - callback aborted query
16637c478bd9Sstevel@tonic-gate  *   _SUCCESS
16647c478bd9Sstevel@tonic-gate  */
16657c478bd9Sstevel@tonic-gate int
16667c478bd9Sstevel@tonic-gate backend_tx_run(backend_tx_t *tx, backend_query_t *q,
16677c478bd9Sstevel@tonic-gate     backend_run_callback_f *cb, void *data)
16687c478bd9Sstevel@tonic-gate {
16697c478bd9Sstevel@tonic-gate 	char *errmsg = NULL;
16707c478bd9Sstevel@tonic-gate 	int ret;
16717c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
16727c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL);
16757c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	if (q == NULL || q->bq_buf == NULL)
16787c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	ts = gethrtime();
16817c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
16827c478bd9Sstevel@tonic-gate 	ret = sqlite_exec(be->be_db, q->bq_buf, cb, data, &errmsg);
16837c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
16847c478bd9Sstevel@tonic-gate 	if (ret == SQLITE_FULL)
16857c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
16867c478bd9Sstevel@tonic-gate 	ret = backend_error(be, ret, errmsg);
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	return (ret);
16897c478bd9Sstevel@tonic-gate }
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate /*
16927c478bd9Sstevel@tonic-gate  * Returns
16937c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
16947c478bd9Sstevel@tonic-gate  *   _NOT_FOUND - the query returned no results
16957c478bd9Sstevel@tonic-gate  *   _SUCCESS - the query returned a single integer
16967c478bd9Sstevel@tonic-gate  */
16977c478bd9Sstevel@tonic-gate int
16987c478bd9Sstevel@tonic-gate backend_tx_run_single_int(backend_tx_t *tx, backend_query_t *q, uint32_t *buf)
16997c478bd9Sstevel@tonic-gate {
17007c478bd9Sstevel@tonic-gate 	struct run_single_int_info info;
17017c478bd9Sstevel@tonic-gate 	int ret;
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	info.rs_out = buf;
17047c478bd9Sstevel@tonic-gate 	info.rs_result = REP_PROTOCOL_FAIL_NOT_FOUND;
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	ret = backend_tx_run(tx, q, run_single_int_callback, &info);
17077c478bd9Sstevel@tonic-gate 	assert(ret != REP_PROTOCOL_DONE);
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	if (ret != REP_PROTOCOL_SUCCESS)
17107c478bd9Sstevel@tonic-gate 		return (ret);
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	return (info.rs_result);
17137c478bd9Sstevel@tonic-gate }
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate /*
17167c478bd9Sstevel@tonic-gate  * Fails with
17177c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - out of memory
17187c478bd9Sstevel@tonic-gate  */
17197c478bd9Sstevel@tonic-gate int
17207c478bd9Sstevel@tonic-gate backend_tx_run_update(backend_tx_t *tx, const char *format, ...)
17217c478bd9Sstevel@tonic-gate {
17227c478bd9Sstevel@tonic-gate 	va_list a;
17237c478bd9Sstevel@tonic-gate 	char *errmsg;
17247c478bd9Sstevel@tonic-gate 	int ret;
17257c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
17267c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly);
17297c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	va_start(a, format);
17327c478bd9Sstevel@tonic-gate 	ts = gethrtime();
17337c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
17347c478bd9Sstevel@tonic-gate 	ret = sqlite_exec_vprintf(be->be_db, format, NULL, NULL, &errmsg, a);
17357c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
17367c478bd9Sstevel@tonic-gate 	if (ret == SQLITE_FULL)
17377c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
17387c478bd9Sstevel@tonic-gate 	va_end(a);
17397c478bd9Sstevel@tonic-gate 	ret = backend_error(be, ret, errmsg);
17407c478bd9Sstevel@tonic-gate 	assert(ret != REP_PROTOCOL_DONE);
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	return (ret);
17437c478bd9Sstevel@tonic-gate }
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate /*
17467c478bd9Sstevel@tonic-gate  * returns REP_PROTOCOL_FAIL_NOT_FOUND if no changes occured
17477c478bd9Sstevel@tonic-gate  */
17487c478bd9Sstevel@tonic-gate int
17497c478bd9Sstevel@tonic-gate backend_tx_run_update_changed(backend_tx_t *tx, const char *format, ...)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	va_list a;
17527c478bd9Sstevel@tonic-gate 	char *errmsg;
17537c478bd9Sstevel@tonic-gate 	int ret;
17547c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
17557c478bd9Sstevel@tonic-gate 	hrtime_t ts, vts;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly);
17587c478bd9Sstevel@tonic-gate 	be = tx->bt_be;
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	va_start(a, format);
17617c478bd9Sstevel@tonic-gate 	ts = gethrtime();
17627c478bd9Sstevel@tonic-gate 	vts = gethrvtime();
17637c478bd9Sstevel@tonic-gate 	ret = sqlite_exec_vprintf(be->be_db, format, NULL, NULL, &errmsg, a);
17647c478bd9Sstevel@tonic-gate 	UPDATE_TOTALS(be, bt_exec, ts, vts);
17657c478bd9Sstevel@tonic-gate 	if (ret == SQLITE_FULL)
17667c478bd9Sstevel@tonic-gate 		tx->bt_full = 1;
17677c478bd9Sstevel@tonic-gate 	va_end(a);
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	ret = backend_error(be, ret, errmsg);
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	return (ret);
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate #define	BACKEND_ADD_SCHEMA(be, file, tbls, idxs) \
17757c478bd9Sstevel@tonic-gate 	(backend_add_schema((be), (file), \
17767c478bd9Sstevel@tonic-gate 	    (tbls), sizeof (tbls) / sizeof (*(tbls)), \
17777c478bd9Sstevel@tonic-gate 	    (idxs), sizeof (idxs) / sizeof (*(idxs))))
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate static int
17807c478bd9Sstevel@tonic-gate backend_add_schema(sqlite_backend_t *be, const char *file,
17817c478bd9Sstevel@tonic-gate     struct backend_tbl_info *tbls, int tbl_count,
17827c478bd9Sstevel@tonic-gate     struct backend_idx_info *idxs, int idx_count)
17837c478bd9Sstevel@tonic-gate {
17847c478bd9Sstevel@tonic-gate 	int i;
17857c478bd9Sstevel@tonic-gate 	char *errmsg;
17867c478bd9Sstevel@tonic-gate 	int ret;
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	/*
17897c478bd9Sstevel@tonic-gate 	 * Create the tables.
17907c478bd9Sstevel@tonic-gate 	 */
17917c478bd9Sstevel@tonic-gate 	for (i = 0; i < tbl_count; i++) {
17927c478bd9Sstevel@tonic-gate 		if (tbls[i].bti_name == NULL) {
17937c478bd9Sstevel@tonic-gate 			assert(i + 1 == tbl_count);
17947c478bd9Sstevel@tonic-gate 			break;
17957c478bd9Sstevel@tonic-gate 		}
17967c478bd9Sstevel@tonic-gate 		ret = sqlite_exec_printf(be->be_db,
17977c478bd9Sstevel@tonic-gate 		    "CREATE TABLE %s (%s);\n",
17987c478bd9Sstevel@tonic-gate 		    NULL, NULL, &errmsg, tbls[i].bti_name, tbls[i].bti_cols);
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 		if (ret != SQLITE_OK) {
18017c478bd9Sstevel@tonic-gate 			configd_critical(
18027c478bd9Sstevel@tonic-gate 			    "%s: %s table creation fails: %s\n", file,
18037c478bd9Sstevel@tonic-gate 			    tbls[i].bti_name, errmsg);
18047c478bd9Sstevel@tonic-gate 			free(errmsg);
18057c478bd9Sstevel@tonic-gate 			return (-1);
18067c478bd9Sstevel@tonic-gate 		}
18077c478bd9Sstevel@tonic-gate 	}
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	/*
18107c478bd9Sstevel@tonic-gate 	 * Make indices on key tables and columns.
18117c478bd9Sstevel@tonic-gate 	 */
18127c478bd9Sstevel@tonic-gate 	for (i = 0; i < idx_count; i++) {
18137c478bd9Sstevel@tonic-gate 		if (idxs[i].bxi_tbl == NULL) {
18147c478bd9Sstevel@tonic-gate 			assert(i + 1 == idx_count);
18157c478bd9Sstevel@tonic-gate 			break;
18167c478bd9Sstevel@tonic-gate 		}
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 		ret = sqlite_exec_printf(be->be_db,
18197c478bd9Sstevel@tonic-gate 		    "CREATE INDEX %s_%s ON %s (%s);\n",
18207c478bd9Sstevel@tonic-gate 		    NULL, NULL, &errmsg, idxs[i].bxi_tbl, idxs[i].bxi_idx,
18217c478bd9Sstevel@tonic-gate 		    idxs[i].bxi_tbl, idxs[i].bxi_cols);
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 		if (ret != SQLITE_OK) {
18247c478bd9Sstevel@tonic-gate 			configd_critical(
18257c478bd9Sstevel@tonic-gate 			    "%s: %s_%s index creation fails: %s\n", file,
18267c478bd9Sstevel@tonic-gate 			    idxs[i].bxi_tbl, idxs[i].bxi_idx, errmsg);
18277c478bd9Sstevel@tonic-gate 			free(errmsg);
18287c478bd9Sstevel@tonic-gate 			return (-1);
18297c478bd9Sstevel@tonic-gate 		}
18307c478bd9Sstevel@tonic-gate 	}
18317c478bd9Sstevel@tonic-gate 	return (0);
18327c478bd9Sstevel@tonic-gate }
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate static int
18357c478bd9Sstevel@tonic-gate backend_init_schema(sqlite_backend_t *be, const char *db_file, backend_type_t t)
18367c478bd9Sstevel@tonic-gate {
18377c478bd9Sstevel@tonic-gate 	int i;
18387c478bd9Sstevel@tonic-gate 	char *errmsg;
18397c478bd9Sstevel@tonic-gate 	int ret;
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	assert(t == BACKEND_TYPE_NORMAL || t == BACKEND_TYPE_NONPERSIST);
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	if (t == BACKEND_TYPE_NORMAL) {
18447c478bd9Sstevel@tonic-gate 		ret = BACKEND_ADD_SCHEMA(be, db_file, tbls_normal, idxs_normal);
18457c478bd9Sstevel@tonic-gate 	} else if (t == BACKEND_TYPE_NONPERSIST) {
18467c478bd9Sstevel@tonic-gate 		ret = BACKEND_ADD_SCHEMA(be, db_file, tbls_np, idxs_np);
18477c478bd9Sstevel@tonic-gate 	} else {
18487c478bd9Sstevel@tonic-gate 		abort();		/* can't happen */
18497c478bd9Sstevel@tonic-gate 	}
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	if (ret < 0) {
18527c478bd9Sstevel@tonic-gate 		return (ret);
18537c478bd9Sstevel@tonic-gate 	}
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	ret = BACKEND_ADD_SCHEMA(be, db_file, tbls_common, idxs_common);
18567c478bd9Sstevel@tonic-gate 	if (ret < 0) {
18577c478bd9Sstevel@tonic-gate 		return (ret);
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	/*
18617c478bd9Sstevel@tonic-gate 	 * Add the schema version to the table
18627c478bd9Sstevel@tonic-gate 	 */
18637c478bd9Sstevel@tonic-gate 	ret = sqlite_exec_printf(be->be_db,
18647c478bd9Sstevel@tonic-gate 	    "INSERT INTO schema_version (schema_version) VALUES (%d)",
18657c478bd9Sstevel@tonic-gate 	    NULL, NULL, &errmsg, BACKEND_SCHEMA_VERSION);
18667c478bd9Sstevel@tonic-gate 	if (ret != SQLITE_OK) {
18677c478bd9Sstevel@tonic-gate 		configd_critical(
18687c478bd9Sstevel@tonic-gate 		    "setting schema version fails: %s\n", errmsg);
18697c478bd9Sstevel@tonic-gate 		free(errmsg);
18707c478bd9Sstevel@tonic-gate 	}
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	/*
18737c478bd9Sstevel@tonic-gate 	 * Populate id_tbl with initial IDs.
18747c478bd9Sstevel@tonic-gate 	 */
18757c478bd9Sstevel@tonic-gate 	for (i = 0; i < BACKEND_ID_INVALID; i++) {
18767c478bd9Sstevel@tonic-gate 		const char *name = id_space_to_name(i);
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 		ret = sqlite_exec_printf(be->be_db,
18797c478bd9Sstevel@tonic-gate 		    "INSERT INTO id_tbl (id_name, id_next) "
18807c478bd9Sstevel@tonic-gate 		    "VALUES ('%q', %d);", NULL, NULL, &errmsg, name, 1);
18817c478bd9Sstevel@tonic-gate 		if (ret != SQLITE_OK) {
18827c478bd9Sstevel@tonic-gate 			configd_critical(
18837c478bd9Sstevel@tonic-gate 			    "id insertion for %s fails: %s\n", name, errmsg);
18847c478bd9Sstevel@tonic-gate 			free(errmsg);
18857c478bd9Sstevel@tonic-gate 			return (-1);
18867c478bd9Sstevel@tonic-gate 		}
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 	/*
18897c478bd9Sstevel@tonic-gate 	 * Set the persistance of the database.  The normal database is marked
18907c478bd9Sstevel@tonic-gate 	 * "synchronous", so that all writes are synchronized to stable storage
18917c478bd9Sstevel@tonic-gate 	 * before proceeding.
18927c478bd9Sstevel@tonic-gate 	 */
18937c478bd9Sstevel@tonic-gate 	ret = sqlite_exec_printf(be->be_db,
18947c478bd9Sstevel@tonic-gate 	    "PRAGMA default_synchronous = %s; PRAGMA synchronous = %s;",
18957c478bd9Sstevel@tonic-gate 	    NULL, NULL, &errmsg,
18967c478bd9Sstevel@tonic-gate 	    (t == BACKEND_TYPE_NORMAL)? "ON" : "OFF",
18977c478bd9Sstevel@tonic-gate 	    (t == BACKEND_TYPE_NORMAL)? "ON" : "OFF");
18987c478bd9Sstevel@tonic-gate 	if (ret != SQLITE_OK) {
18997c478bd9Sstevel@tonic-gate 		configd_critical("pragma setting fails: %s\n", errmsg);
19007c478bd9Sstevel@tonic-gate 		free(errmsg);
19017c478bd9Sstevel@tonic-gate 		return (-1);
19027c478bd9Sstevel@tonic-gate 	}
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	return (0);
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate int
19087c478bd9Sstevel@tonic-gate backend_init(const char *db_file, const char *npdb_file, int have_np)
19097c478bd9Sstevel@tonic-gate {
19107c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be;
19117c478bd9Sstevel@tonic-gate 	int r;
19127c478bd9Sstevel@tonic-gate 	int writable_persist = 1;
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	/* set up our temporary directory */
19157c478bd9Sstevel@tonic-gate 	sqlite_temp_directory = "/etc/svc/volatile";
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	if (strcmp(SQLITE_VERSION, sqlite_version) != 0) {
19187c478bd9Sstevel@tonic-gate 		configd_critical("Mismatched link!  (%s should be %s)\n",
19197c478bd9Sstevel@tonic-gate 		    sqlite_version, SQLITE_VERSION);
19207c478bd9Sstevel@tonic-gate 		return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
19217c478bd9Sstevel@tonic-gate 	}
19227c478bd9Sstevel@tonic-gate 	if (db_file == NULL)
19237c478bd9Sstevel@tonic-gate 		db_file = REPOSITORY_DB;
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	r = backend_create(BACKEND_TYPE_NORMAL, db_file, &be);
19267c478bd9Sstevel@tonic-gate 	switch (r) {
19277c478bd9Sstevel@tonic-gate 	case BACKEND_CREATE_FAIL:
19287c478bd9Sstevel@tonic-gate 		return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
19297c478bd9Sstevel@tonic-gate 	case BACKEND_CREATE_LOCKED:
19307c478bd9Sstevel@tonic-gate 		return (CONFIGD_EXIT_DATABASE_LOCKED);
19317c478bd9Sstevel@tonic-gate 	case BACKEND_CREATE_SUCCESS:
19327c478bd9Sstevel@tonic-gate 		break;		/* success */
19337c478bd9Sstevel@tonic-gate 	case BACKEND_CREATE_READONLY:
19347c478bd9Sstevel@tonic-gate 		writable_persist = 0;
19357c478bd9Sstevel@tonic-gate 		break;
19367c478bd9Sstevel@tonic-gate 	case BACKEND_CREATE_NEED_INIT:
19377c478bd9Sstevel@tonic-gate 		if (backend_init_schema(be, db_file, BACKEND_TYPE_NORMAL)) {
19387c478bd9Sstevel@tonic-gate 			backend_destroy(be);
19397c478bd9Sstevel@tonic-gate 			return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
19407c478bd9Sstevel@tonic-gate 		}
19417c478bd9Sstevel@tonic-gate 		break;
19427c478bd9Sstevel@tonic-gate 	default:
19437c478bd9Sstevel@tonic-gate 		abort();
19447c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
19457c478bd9Sstevel@tonic-gate 	}
19467c478bd9Sstevel@tonic-gate 	backend_create_finish(BACKEND_TYPE_NORMAL, be);
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	if (have_np) {
19497c478bd9Sstevel@tonic-gate 		if (npdb_file == NULL)
19507c478bd9Sstevel@tonic-gate 			npdb_file = NONPERSIST_DB;
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 		r = backend_create(BACKEND_TYPE_NONPERSIST, npdb_file, &be);
19537c478bd9Sstevel@tonic-gate 		switch (r) {
19547c478bd9Sstevel@tonic-gate 		case BACKEND_CREATE_SUCCESS:
19557c478bd9Sstevel@tonic-gate 			break;		/* success */
19567c478bd9Sstevel@tonic-gate 		case BACKEND_CREATE_FAIL:
19577c478bd9Sstevel@tonic-gate 			return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
19587c478bd9Sstevel@tonic-gate 		case BACKEND_CREATE_LOCKED:
19597c478bd9Sstevel@tonic-gate 			return (CONFIGD_EXIT_DATABASE_LOCKED);
19607c478bd9Sstevel@tonic-gate 		case BACKEND_CREATE_READONLY:
19617c478bd9Sstevel@tonic-gate 			configd_critical("%s: unable to write\n", npdb_file);
19627c478bd9Sstevel@tonic-gate 			return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
19637c478bd9Sstevel@tonic-gate 		case BACKEND_CREATE_NEED_INIT:
19647c478bd9Sstevel@tonic-gate 			if (backend_init_schema(be, db_file,
19657c478bd9Sstevel@tonic-gate 			    BACKEND_TYPE_NONPERSIST)) {
19667c478bd9Sstevel@tonic-gate 				backend_destroy(be);
19677c478bd9Sstevel@tonic-gate 				return (CONFIGD_EXIT_DATABASE_INIT_FAILED);
19687c478bd9Sstevel@tonic-gate 			}
19697c478bd9Sstevel@tonic-gate 			break;
19707c478bd9Sstevel@tonic-gate 		default:
19717c478bd9Sstevel@tonic-gate 			abort();
19727c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
19737c478bd9Sstevel@tonic-gate 		}
19747c478bd9Sstevel@tonic-gate 		backend_create_finish(BACKEND_TYPE_NONPERSIST, be);
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 		/*
19777c478bd9Sstevel@tonic-gate 		 * If we started up with a writable filesystem, but the
19787c478bd9Sstevel@tonic-gate 		 * non-persistent database needed initialization, we
19797c478bd9Sstevel@tonic-gate 		 * are booting a non-global zone, so do a backup.
19807c478bd9Sstevel@tonic-gate 		 */
19817c478bd9Sstevel@tonic-gate 		if (r == BACKEND_CREATE_NEED_INIT && writable_persist &&
19827c478bd9Sstevel@tonic-gate 		    backend_lock(BACKEND_TYPE_NORMAL, 0, &be) ==
19837c478bd9Sstevel@tonic-gate 		    REP_PROTOCOL_SUCCESS) {
19847c478bd9Sstevel@tonic-gate 			if (backend_create_backup_locked(be,
19857c478bd9Sstevel@tonic-gate 			    REPOSITORY_BOOT_BACKUP) != REP_PROTOCOL_SUCCESS) {
19867c478bd9Sstevel@tonic-gate 				configd_critical(
19877c478bd9Sstevel@tonic-gate 				    "unable to create \"%s\" backup of "
19887c478bd9Sstevel@tonic-gate 				    "\"%s\"\n", REPOSITORY_BOOT_BACKUP,
19897c478bd9Sstevel@tonic-gate 				    be->be_path);
19907c478bd9Sstevel@tonic-gate 			}
19917c478bd9Sstevel@tonic-gate 			backend_unlock(be);
19927c478bd9Sstevel@tonic-gate 		}
19937c478bd9Sstevel@tonic-gate 	}
19947c478bd9Sstevel@tonic-gate 	return (CONFIGD_EXIT_OKAY);
19957c478bd9Sstevel@tonic-gate }
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate /*
19987c478bd9Sstevel@tonic-gate  * quiesce all database activity prior to exiting
19997c478bd9Sstevel@tonic-gate  */
20007c478bd9Sstevel@tonic-gate void
20017c478bd9Sstevel@tonic-gate backend_fini(void)
20027c478bd9Sstevel@tonic-gate {
20037c478bd9Sstevel@tonic-gate 	sqlite_backend_t *be_normal, *be_np;
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	(void) backend_lock(BACKEND_TYPE_NORMAL, 1, &be_normal);
20067c478bd9Sstevel@tonic-gate 	(void) backend_lock(BACKEND_TYPE_NONPERSIST, 1, &be_np);
20077c478bd9Sstevel@tonic-gate }
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate #define	QUERY_BASE	128
20107c478bd9Sstevel@tonic-gate backend_query_t *
20117c478bd9Sstevel@tonic-gate backend_query_alloc(void)
20127c478bd9Sstevel@tonic-gate {
20137c478bd9Sstevel@tonic-gate 	backend_query_t *q;
20147c478bd9Sstevel@tonic-gate 	q = calloc(1, sizeof (backend_query_t));
20157c478bd9Sstevel@tonic-gate 	if (q != NULL) {
20167c478bd9Sstevel@tonic-gate 		q->bq_size = QUERY_BASE;
20177c478bd9Sstevel@tonic-gate 		q->bq_buf = calloc(1, q->bq_size);
20187c478bd9Sstevel@tonic-gate 		if (q->bq_buf == NULL) {
20197c478bd9Sstevel@tonic-gate 			q->bq_size = 0;
20207c478bd9Sstevel@tonic-gate 		}
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	}
20237c478bd9Sstevel@tonic-gate 	return (q);
20247c478bd9Sstevel@tonic-gate }
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate void
20277c478bd9Sstevel@tonic-gate backend_query_append(backend_query_t *q, const char *value)
20287c478bd9Sstevel@tonic-gate {
20297c478bd9Sstevel@tonic-gate 	char *alloc;
20307c478bd9Sstevel@tonic-gate 	int count;
20317c478bd9Sstevel@tonic-gate 	size_t size, old_len;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	if (q == NULL) {
20347c478bd9Sstevel@tonic-gate 		/* We'll discover the error when we try to run the query. */
20357c478bd9Sstevel@tonic-gate 		return;
20367c478bd9Sstevel@tonic-gate 	}
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	while (q->bq_buf != NULL) {
20397c478bd9Sstevel@tonic-gate 		old_len = strlen(q->bq_buf);
20407c478bd9Sstevel@tonic-gate 		size = q->bq_size;
20417c478bd9Sstevel@tonic-gate 		count = strlcat(q->bq_buf, value, size);
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 		if (count < size)
20447c478bd9Sstevel@tonic-gate 			break;				/* success */
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 		q->bq_buf[old_len] = 0;
20477c478bd9Sstevel@tonic-gate 		size = round_up_to_p2(count + 1);
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 		assert(size > q->bq_size);
20507c478bd9Sstevel@tonic-gate 		alloc = realloc(q->bq_buf, size);
20517c478bd9Sstevel@tonic-gate 		if (alloc == NULL) {
20527c478bd9Sstevel@tonic-gate 			free(q->bq_buf);
20537c478bd9Sstevel@tonic-gate 			q->bq_buf = NULL;
20547c478bd9Sstevel@tonic-gate 			break;				/* can't grow */
20557c478bd9Sstevel@tonic-gate 		}
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 		q->bq_buf = alloc;
20587c478bd9Sstevel@tonic-gate 		q->bq_size = size;
20597c478bd9Sstevel@tonic-gate 	}
20607c478bd9Sstevel@tonic-gate }
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate void
20637c478bd9Sstevel@tonic-gate backend_query_add(backend_query_t *q, const char *format, ...)
20647c478bd9Sstevel@tonic-gate {
20657c478bd9Sstevel@tonic-gate 	va_list args;
20667c478bd9Sstevel@tonic-gate 	char *new;
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	if (q == NULL || q->bq_buf == NULL)
20697c478bd9Sstevel@tonic-gate 		return;
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	va_start(args, format);
20727c478bd9Sstevel@tonic-gate 	new = sqlite_vmprintf(format, args);
20737c478bd9Sstevel@tonic-gate 	va_end(args);
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	if (new == NULL) {
20767c478bd9Sstevel@tonic-gate 		free(q->bq_buf);
20777c478bd9Sstevel@tonic-gate 		q->bq_buf = NULL;
20787c478bd9Sstevel@tonic-gate 		return;
20797c478bd9Sstevel@tonic-gate 	}
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	backend_query_append(q, new);
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	free(new);
20847c478bd9Sstevel@tonic-gate }
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate void
20877c478bd9Sstevel@tonic-gate backend_query_free(backend_query_t *q)
20887c478bd9Sstevel@tonic-gate {
20897c478bd9Sstevel@tonic-gate 	if (q != NULL) {
20907c478bd9Sstevel@tonic-gate 		if (q->bq_buf != NULL) {
20917c478bd9Sstevel@tonic-gate 			free(q->bq_buf);
20927c478bd9Sstevel@tonic-gate 		}
20937c478bd9Sstevel@tonic-gate 		free(q);
20947c478bd9Sstevel@tonic-gate 	}
20957c478bd9Sstevel@tonic-gate }
2096