17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5c5c4113dSnw141292 * Common Development and Distribution License (the "License"). 6c5c4113dSnw141292 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 215b7f77adStw21770 227c478bd9Sstevel@tonic-gate /* 235b7f77adStw21770 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 298918dff3Sjwadams /* 308918dff3Sjwadams * sqlite is not compatible with _FILE_OFFSET_BITS=64, but we need to 318918dff3Sjwadams * be able to statvfs(2) possibly large systems. This define gives us 328918dff3Sjwadams * access to the transitional interfaces. See lfcompile64(5) for how 338918dff3Sjwadams * _LARGEFILE64_SOURCE works. 348918dff3Sjwadams */ 358918dff3Sjwadams #define _LARGEFILE64_SOURCE 368918dff3Sjwadams 377c478bd9Sstevel@tonic-gate #include <assert.h> 387c478bd9Sstevel@tonic-gate #include <door.h> 397c478bd9Sstevel@tonic-gate #include <dirent.h> 407c478bd9Sstevel@tonic-gate #include <errno.h> 417c478bd9Sstevel@tonic-gate #include <fcntl.h> 427c478bd9Sstevel@tonic-gate #include <limits.h> 437c478bd9Sstevel@tonic-gate #include <pthread.h> 447c478bd9Sstevel@tonic-gate #include <stdarg.h> 457c478bd9Sstevel@tonic-gate #include <stdio.h> 467c478bd9Sstevel@tonic-gate #include <stdlib.h> 477c478bd9Sstevel@tonic-gate #include <string.h> 488918dff3Sjwadams #include <sys/stat.h> 498918dff3Sjwadams #include <sys/statvfs.h> 507c478bd9Sstevel@tonic-gate #include <unistd.h> 517c478bd9Sstevel@tonic-gate #include <zone.h> 52*c0889d7aSstevep #include <libscf_priv.h> 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate #include "configd.h" 557c478bd9Sstevel@tonic-gate #include "repcache_protocol.h" 567c478bd9Sstevel@tonic-gate 57c5c4113dSnw141292 #include <sqlite.h> 58c5c4113dSnw141292 #include <sqlite-misc.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * This file has two purposes: 627c478bd9Sstevel@tonic-gate * 637c478bd9Sstevel@tonic-gate * 1. It contains the database schema, and the code for setting up our backend 647c478bd9Sstevel@tonic-gate * databases, including installing said schema. 657c478bd9Sstevel@tonic-gate * 667c478bd9Sstevel@tonic-gate * 2. It provides a simplified interface to the SQL database library, and 677c478bd9Sstevel@tonic-gate * synchronizes MT access to the database. 687c478bd9Sstevel@tonic-gate */ 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate typedef struct backend_spent { 717c478bd9Sstevel@tonic-gate uint64_t bs_count; 727c478bd9Sstevel@tonic-gate hrtime_t bs_time; 737c478bd9Sstevel@tonic-gate hrtime_t bs_vtime; 747c478bd9Sstevel@tonic-gate } backend_spent_t; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate typedef struct backend_totals { 777c478bd9Sstevel@tonic-gate backend_spent_t bt_lock; /* waiting for lock */ 787c478bd9Sstevel@tonic-gate backend_spent_t bt_exec; /* time spent executing SQL */ 797c478bd9Sstevel@tonic-gate } backend_totals_t; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate typedef struct sqlite_backend { 827c478bd9Sstevel@tonic-gate pthread_mutex_t be_lock; 837c478bd9Sstevel@tonic-gate pthread_t be_thread; /* thread holding lock */ 847c478bd9Sstevel@tonic-gate struct sqlite *be_db; 857c478bd9Sstevel@tonic-gate const char *be_path; /* path to db */ 868918dff3Sjwadams int be_readonly; /* readonly at start, and still is */ 877c478bd9Sstevel@tonic-gate int be_writing; /* held for writing */ 887c478bd9Sstevel@tonic-gate backend_type_t be_type; /* type of db */ 898918dff3Sjwadams hrtime_t be_lastcheck; /* time of last read-only check */ 907c478bd9Sstevel@tonic-gate backend_totals_t be_totals[2]; /* one for reading, one for writing */ 917c478bd9Sstevel@tonic-gate } sqlite_backend_t; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate struct backend_tx { 947c478bd9Sstevel@tonic-gate sqlite_backend_t *bt_be; 957c478bd9Sstevel@tonic-gate int bt_readonly; 967c478bd9Sstevel@tonic-gate int bt_type; 977c478bd9Sstevel@tonic-gate int bt_full; /* SQLITE_FULL during tx */ 987c478bd9Sstevel@tonic-gate }; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate #define UPDATE_TOTALS_WR(sb, writing, field, ts, vts) { \ 1017c478bd9Sstevel@tonic-gate backend_spent_t *__bsp = &(sb)->be_totals[!!(writing)].field; \ 1027c478bd9Sstevel@tonic-gate __bsp->bs_count++; \ 1037c478bd9Sstevel@tonic-gate __bsp->bs_time += (gethrtime() - ts); \ 1047c478bd9Sstevel@tonic-gate __bsp->bs_vtime += (gethrvtime() - vts); \ 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate #define UPDATE_TOTALS(sb, field, ts, vts) \ 1087c478bd9Sstevel@tonic-gate UPDATE_TOTALS_WR(sb, (sb)->be_writing, field, ts, vts) 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate struct backend_query { 1117c478bd9Sstevel@tonic-gate char *bq_buf; 1127c478bd9Sstevel@tonic-gate size_t bq_size; 1137c478bd9Sstevel@tonic-gate }; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate struct backend_tbl_info { 1167c478bd9Sstevel@tonic-gate const char *bti_name; 1177c478bd9Sstevel@tonic-gate const char *bti_cols; 1187c478bd9Sstevel@tonic-gate }; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate struct backend_idx_info { 1217c478bd9Sstevel@tonic-gate const char *bxi_tbl; 1227c478bd9Sstevel@tonic-gate const char *bxi_idx; 1237c478bd9Sstevel@tonic-gate const char *bxi_cols; 1247c478bd9Sstevel@tonic-gate }; 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate static pthread_mutex_t backend_panic_lock = PTHREAD_MUTEX_INITIALIZER; 1277c478bd9Sstevel@tonic-gate static pthread_cond_t backend_panic_cv = PTHREAD_COND_INITIALIZER; 1287c478bd9Sstevel@tonic-gate pthread_t backend_panic_thread = 0; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate int backend_do_trace = 0; /* invoke tracing callback */ 1317c478bd9Sstevel@tonic-gate int backend_print_trace = 0; /* tracing callback prints SQL */ 1327c478bd9Sstevel@tonic-gate int backend_panic_abort = 0; /* abort when panicking */ 1337c478bd9Sstevel@tonic-gate 1348918dff3Sjwadams /* interval between read-only checks while starting up */ 1358918dff3Sjwadams #define BACKEND_READONLY_CHECK_INTERVAL (2 * (hrtime_t)NANOSEC) 1368918dff3Sjwadams 1377c478bd9Sstevel@tonic-gate /* 1387c478bd9Sstevel@tonic-gate * Any change to the below schema should bump the version number 1397c478bd9Sstevel@tonic-gate */ 1407c478bd9Sstevel@tonic-gate #define BACKEND_SCHEMA_VERSION 5 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate static struct backend_tbl_info tbls_normal[] = { /* BACKEND_TYPE_NORMAL */ 1437c478bd9Sstevel@tonic-gate /* 1447c478bd9Sstevel@tonic-gate * service_tbl holds all services. svc_id is the identifier of the 1457c478bd9Sstevel@tonic-gate * service. 1467c478bd9Sstevel@tonic-gate */ 1477c478bd9Sstevel@tonic-gate { 1487c478bd9Sstevel@tonic-gate "service_tbl", 1497c478bd9Sstevel@tonic-gate "svc_id INTEGER PRIMARY KEY," 1507c478bd9Sstevel@tonic-gate "svc_name CHAR(256) NOT NULL" 1517c478bd9Sstevel@tonic-gate }, 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate /* 1547c478bd9Sstevel@tonic-gate * instance_tbl holds all of the instances. The parent service id 1557c478bd9Sstevel@tonic-gate * is instance_svc. 1567c478bd9Sstevel@tonic-gate */ 1577c478bd9Sstevel@tonic-gate { 1587c478bd9Sstevel@tonic-gate "instance_tbl", 1597c478bd9Sstevel@tonic-gate "instance_id INTEGER PRIMARY KEY," 1607c478bd9Sstevel@tonic-gate "instance_name CHAR(256) NOT NULL," 1617c478bd9Sstevel@tonic-gate "instance_svc INTEGER NOT NULL" 1627c478bd9Sstevel@tonic-gate }, 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate * snapshot_lnk_tbl links (instance, snapshot name) with snapshots. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate { 1687c478bd9Sstevel@tonic-gate "snapshot_lnk_tbl", 1697c478bd9Sstevel@tonic-gate "lnk_id INTEGER PRIMARY KEY," 1707c478bd9Sstevel@tonic-gate "lnk_inst_id INTEGER NOT NULL," 1717c478bd9Sstevel@tonic-gate "lnk_snap_name CHAR(256) NOT NULL," 1727c478bd9Sstevel@tonic-gate "lnk_snap_id INTEGER NOT NULL" 1737c478bd9Sstevel@tonic-gate }, 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* 1767c478bd9Sstevel@tonic-gate * snaplevel_tbl maps a snapshot id to a set of named, ordered 1777c478bd9Sstevel@tonic-gate * snaplevels. 1787c478bd9Sstevel@tonic-gate */ 1797c478bd9Sstevel@tonic-gate { 1807c478bd9Sstevel@tonic-gate "snaplevel_tbl", 1817c478bd9Sstevel@tonic-gate "snap_id INTEGER NOT NULL," 1827c478bd9Sstevel@tonic-gate "snap_level_num INTEGER NOT NULL," 1837c478bd9Sstevel@tonic-gate "snap_level_id INTEGER NOT NULL," 1847c478bd9Sstevel@tonic-gate "snap_level_service_id INTEGER NOT NULL," 1857c478bd9Sstevel@tonic-gate "snap_level_service CHAR(256) NOT NULL," 1867c478bd9Sstevel@tonic-gate "snap_level_instance_id INTEGER NULL," 1877c478bd9Sstevel@tonic-gate "snap_level_instance CHAR(256) NULL" 1887c478bd9Sstevel@tonic-gate }, 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate /* 1917c478bd9Sstevel@tonic-gate * snaplevel_lnk_tbl links snaplevels to property groups. 1927c478bd9Sstevel@tonic-gate * snaplvl_pg_* is identical to the original property group, 1937c478bd9Sstevel@tonic-gate * and snaplvl_gen_id overrides the generation number. 1947c478bd9Sstevel@tonic-gate * The service/instance ids are as in the snaplevel. 1957c478bd9Sstevel@tonic-gate */ 1967c478bd9Sstevel@tonic-gate { 1977c478bd9Sstevel@tonic-gate "snaplevel_lnk_tbl", 1987c478bd9Sstevel@tonic-gate "snaplvl_level_id INTEGER NOT NULL," 1997c478bd9Sstevel@tonic-gate "snaplvl_pg_id INTEGER NOT NULL," 2007c478bd9Sstevel@tonic-gate "snaplvl_pg_name CHAR(256) NOT NULL," 2017c478bd9Sstevel@tonic-gate "snaplvl_pg_type CHAR(256) NOT NULL," 2027c478bd9Sstevel@tonic-gate "snaplvl_pg_flags INTEGER NOT NULL," 2037c478bd9Sstevel@tonic-gate "snaplvl_gen_id INTEGER NOT NULL" 2047c478bd9Sstevel@tonic-gate }, 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate { NULL, NULL } 2077c478bd9Sstevel@tonic-gate }; 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate static struct backend_idx_info idxs_normal[] = { /* BACKEND_TYPE_NORMAL */ 2107c478bd9Sstevel@tonic-gate { "service_tbl", "name", "svc_name" }, 2117c478bd9Sstevel@tonic-gate { "instance_tbl", "name", "instance_svc, instance_name" }, 2127c478bd9Sstevel@tonic-gate { "snapshot_lnk_tbl", "name", "lnk_inst_id, lnk_snap_name" }, 2137c478bd9Sstevel@tonic-gate { "snapshot_lnk_tbl", "snapid", "lnk_snap_id" }, 2147c478bd9Sstevel@tonic-gate { "snaplevel_tbl", "id", "snap_id" }, 2157c478bd9Sstevel@tonic-gate { "snaplevel_lnk_tbl", "id", "snaplvl_pg_id" }, 2167c478bd9Sstevel@tonic-gate { "snaplevel_lnk_tbl", "level", "snaplvl_level_id" }, 2177c478bd9Sstevel@tonic-gate { NULL, NULL, NULL } 2187c478bd9Sstevel@tonic-gate }; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate static struct backend_tbl_info tbls_np[] = { /* BACKEND_TYPE_NONPERSIST */ 2217c478bd9Sstevel@tonic-gate { NULL, NULL } 2227c478bd9Sstevel@tonic-gate }; 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate static struct backend_idx_info idxs_np[] = { /* BACKEND_TYPE_NONPERSIST */ 2257c478bd9Sstevel@tonic-gate { NULL, NULL, NULL } 2267c478bd9Sstevel@tonic-gate }; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate static struct backend_tbl_info tbls_common[] = { /* all backend types */ 2297c478bd9Sstevel@tonic-gate /* 2307c478bd9Sstevel@tonic-gate * pg_tbl defines property groups. They are associated with a single 2317c478bd9Sstevel@tonic-gate * service or instance. The pg_gen_id links them with the latest 2327c478bd9Sstevel@tonic-gate * "edited" version of its properties. 2337c478bd9Sstevel@tonic-gate */ 2347c478bd9Sstevel@tonic-gate { 2357c478bd9Sstevel@tonic-gate "pg_tbl", 2367c478bd9Sstevel@tonic-gate "pg_id INTEGER PRIMARY KEY," 2377c478bd9Sstevel@tonic-gate "pg_parent_id INTEGER NOT NULL," 2387c478bd9Sstevel@tonic-gate "pg_name CHAR(256) NOT NULL," 2397c478bd9Sstevel@tonic-gate "pg_type CHAR(256) NOT NULL," 2407c478bd9Sstevel@tonic-gate "pg_flags INTEGER NOT NULL," 2417c478bd9Sstevel@tonic-gate "pg_gen_id INTEGER NOT NULL" 2427c478bd9Sstevel@tonic-gate }, 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate /* 2457c478bd9Sstevel@tonic-gate * prop_lnk_tbl links a particular pg_id and gen_id to a set of 2467c478bd9Sstevel@tonic-gate * (prop_name, prop_type, val_id) trios. 2477c478bd9Sstevel@tonic-gate */ 2487c478bd9Sstevel@tonic-gate { 2497c478bd9Sstevel@tonic-gate "prop_lnk_tbl", 2507c478bd9Sstevel@tonic-gate "lnk_prop_id INTEGER PRIMARY KEY," 2517c478bd9Sstevel@tonic-gate "lnk_pg_id INTEGER NOT NULL," 2527c478bd9Sstevel@tonic-gate "lnk_gen_id INTEGER NOT NULL," 2537c478bd9Sstevel@tonic-gate "lnk_prop_name CHAR(256) NOT NULL," 2547c478bd9Sstevel@tonic-gate "lnk_prop_type CHAR(2) NOT NULL," 2557c478bd9Sstevel@tonic-gate "lnk_val_id INTEGER" 2567c478bd9Sstevel@tonic-gate }, 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate /* 2597c478bd9Sstevel@tonic-gate * value_tbl maps a value_id to a set of values. For any given 2607c478bd9Sstevel@tonic-gate * value_id, value_type is constant. 2617c478bd9Sstevel@tonic-gate */ 2627c478bd9Sstevel@tonic-gate { 2637c478bd9Sstevel@tonic-gate "value_tbl", 2647c478bd9Sstevel@tonic-gate "value_id INTEGER NOT NULL," 2657c478bd9Sstevel@tonic-gate "value_type CHAR(1) NOT NULL," 2667c478bd9Sstevel@tonic-gate "value_value VARCHAR NOT NULL" 2677c478bd9Sstevel@tonic-gate }, 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* 2707c478bd9Sstevel@tonic-gate * id_tbl has one row per id space 2717c478bd9Sstevel@tonic-gate */ 2727c478bd9Sstevel@tonic-gate { 2737c478bd9Sstevel@tonic-gate "id_tbl", 2747c478bd9Sstevel@tonic-gate "id_name STRING NOT NULL," 2757c478bd9Sstevel@tonic-gate "id_next INTEGER NOT NULL" 2767c478bd9Sstevel@tonic-gate }, 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate /* 2797c478bd9Sstevel@tonic-gate * schema_version has a single row, which contains 2807c478bd9Sstevel@tonic-gate * BACKEND_SCHEMA_VERSION at the time of creation. 2817c478bd9Sstevel@tonic-gate */ 2827c478bd9Sstevel@tonic-gate { 2837c478bd9Sstevel@tonic-gate "schema_version", 2847c478bd9Sstevel@tonic-gate "schema_version INTEGER" 2857c478bd9Sstevel@tonic-gate }, 2867c478bd9Sstevel@tonic-gate { NULL, NULL } 2877c478bd9Sstevel@tonic-gate }; 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate static struct backend_idx_info idxs_common[] = { /* all backend types */ 2907c478bd9Sstevel@tonic-gate { "pg_tbl", "parent", "pg_parent_id" }, 2917c478bd9Sstevel@tonic-gate { "pg_tbl", "name", "pg_parent_id, pg_name" }, 2927c478bd9Sstevel@tonic-gate { "pg_tbl", "type", "pg_parent_id, pg_type" }, 2937c478bd9Sstevel@tonic-gate { "prop_lnk_tbl", "base", "lnk_pg_id, lnk_gen_id" }, 2947c478bd9Sstevel@tonic-gate { "prop_lnk_tbl", "val", "lnk_val_id" }, 2957c478bd9Sstevel@tonic-gate { "value_tbl", "id", "value_id" }, 2967c478bd9Sstevel@tonic-gate { "id_tbl", "id", "id_name" }, 2977c478bd9Sstevel@tonic-gate { NULL, NULL, NULL } 2987c478bd9Sstevel@tonic-gate }; 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate struct run_single_int_info { 3017c478bd9Sstevel@tonic-gate uint32_t *rs_out; 3027c478bd9Sstevel@tonic-gate int rs_result; 3037c478bd9Sstevel@tonic-gate }; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3067c478bd9Sstevel@tonic-gate static int 3077c478bd9Sstevel@tonic-gate run_single_int_callback(void *arg, int columns, char **vals, char **names) 3087c478bd9Sstevel@tonic-gate { 3097c478bd9Sstevel@tonic-gate struct run_single_int_info *info = arg; 3107c478bd9Sstevel@tonic-gate uint32_t val; 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate char *endptr = vals[0]; 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate assert(info->rs_result != REP_PROTOCOL_SUCCESS); 3157c478bd9Sstevel@tonic-gate assert(columns == 1); 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate if (vals[0] == NULL) 3187c478bd9Sstevel@tonic-gate return (BACKEND_CALLBACK_CONTINUE); 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate errno = 0; 3217c478bd9Sstevel@tonic-gate val = strtoul(vals[0], &endptr, 10); 3227c478bd9Sstevel@tonic-gate if ((val == 0 && endptr == vals[0]) || *endptr != 0 || errno != 0) 3237c478bd9Sstevel@tonic-gate backend_panic("malformed integer \"%20s\"", vals[0]); 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate *info->rs_out = val; 3267c478bd9Sstevel@tonic-gate info->rs_result = REP_PROTOCOL_SUCCESS; 3277c478bd9Sstevel@tonic-gate return (BACKEND_CALLBACK_CONTINUE); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3317c478bd9Sstevel@tonic-gate int 3327c478bd9Sstevel@tonic-gate backend_fail_if_seen(void *arg, int columns, char **vals, char **names) 3337c478bd9Sstevel@tonic-gate { 3347c478bd9Sstevel@tonic-gate return (BACKEND_CALLBACK_ABORT); 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate 3378918dff3Sjwadams /* 3388918dff3Sjwadams * check to see if we can successfully start a transaction; if not, the 3398918dff3Sjwadams * filesystem is mounted read-only. 3408918dff3Sjwadams */ 3417c478bd9Sstevel@tonic-gate static int 3428918dff3Sjwadams backend_is_readonly(struct sqlite *db, const char *path) 3437c478bd9Sstevel@tonic-gate { 3448918dff3Sjwadams int r; 3458918dff3Sjwadams statvfs64_t stat; 3468918dff3Sjwadams 3478918dff3Sjwadams if (statvfs64(path, &stat) == 0 && (stat.f_flag & ST_RDONLY)) 3488918dff3Sjwadams return (SQLITE_READONLY); 3498918dff3Sjwadams 3508918dff3Sjwadams r = sqlite_exec(db, 3517c478bd9Sstevel@tonic-gate "BEGIN TRANSACTION; " 3527c478bd9Sstevel@tonic-gate "UPDATE schema_version SET schema_version = schema_version; ", 3538918dff3Sjwadams NULL, NULL, NULL); 3547c478bd9Sstevel@tonic-gate (void) sqlite_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, NULL); 3557c478bd9Sstevel@tonic-gate return (r); 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate static void 3597c478bd9Sstevel@tonic-gate backend_trace_sql(void *arg, const char *sql) 3607c478bd9Sstevel@tonic-gate { 3617c478bd9Sstevel@tonic-gate sqlite_backend_t *be = arg; 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate if (backend_print_trace) { 3647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%d: %s\n", be->be_type, sql); 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate static sqlite_backend_t be_info[BACKEND_TYPE_TOTAL]; 3697c478bd9Sstevel@tonic-gate static sqlite_backend_t *bes[BACKEND_TYPE_TOTAL]; 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate #define BACKEND_PANIC_TIMEOUT (50 * MILLISEC) 3727c478bd9Sstevel@tonic-gate /* 3737c478bd9Sstevel@tonic-gate * backend_panic() -- some kind of database problem or corruption has been hit. 3747c478bd9Sstevel@tonic-gate * We attempt to quiesce the other database users -- all of the backend sql 3757c478bd9Sstevel@tonic-gate * entry points will call backend_panic(NULL) if a panic is in progress, as 3767c478bd9Sstevel@tonic-gate * will any attempt to start a transaction. 3777c478bd9Sstevel@tonic-gate * 3787c478bd9Sstevel@tonic-gate * We give threads holding a backend lock 50ms (BACKEND_PANIC_TIMEOUT) to 3797c478bd9Sstevel@tonic-gate * either drop the lock or call backend_panic(). If they don't respond in 3807c478bd9Sstevel@tonic-gate * time, we'll just exit anyway. 3817c478bd9Sstevel@tonic-gate */ 3827c478bd9Sstevel@tonic-gate void 3837c478bd9Sstevel@tonic-gate backend_panic(const char *format, ...) 3847c478bd9Sstevel@tonic-gate { 3857c478bd9Sstevel@tonic-gate int i; 3867c478bd9Sstevel@tonic-gate va_list args; 3877c478bd9Sstevel@tonic-gate int failed = 0; 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&backend_panic_lock); 3907c478bd9Sstevel@tonic-gate if (backend_panic_thread != 0) { 3917c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&backend_panic_lock); 3927c478bd9Sstevel@tonic-gate /* 3937c478bd9Sstevel@tonic-gate * first, drop any backend locks we're holding, then 3947c478bd9Sstevel@tonic-gate * sleep forever on the panic_cv. 3957c478bd9Sstevel@tonic-gate */ 3967c478bd9Sstevel@tonic-gate for (i = 0; i < BACKEND_TYPE_TOTAL; i++) { 3977c478bd9Sstevel@tonic-gate if (bes[i] != NULL && 3987c478bd9Sstevel@tonic-gate bes[i]->be_thread == pthread_self()) 3997c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&bes[i]->be_lock); 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&backend_panic_lock); 4027c478bd9Sstevel@tonic-gate for (;;) 4037c478bd9Sstevel@tonic-gate (void) pthread_cond_wait(&backend_panic_cv, 4047c478bd9Sstevel@tonic-gate &backend_panic_lock); 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate backend_panic_thread = pthread_self(); 4077c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&backend_panic_lock); 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate for (i = 0; i < BACKEND_TYPE_TOTAL; i++) { 4107c478bd9Sstevel@tonic-gate if (bes[i] != NULL && bes[i]->be_thread == pthread_self()) 4117c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&bes[i]->be_lock); 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate va_start(args, format); 4157c478bd9Sstevel@tonic-gate configd_vcritical(format, args); 4167c478bd9Sstevel@tonic-gate va_end(args); 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate for (i = 0; i < BACKEND_TYPE_TOTAL; i++) { 4197c478bd9Sstevel@tonic-gate timespec_t rel; 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate rel.tv_sec = 0; 4227c478bd9Sstevel@tonic-gate rel.tv_nsec = BACKEND_PANIC_TIMEOUT; 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate if (bes[i] != NULL && bes[i]->be_thread != pthread_self()) { 4257c478bd9Sstevel@tonic-gate if (pthread_mutex_reltimedlock_np(&bes[i]->be_lock, 4267c478bd9Sstevel@tonic-gate &rel) != 0) 4277c478bd9Sstevel@tonic-gate failed++; 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate if (failed) { 4317c478bd9Sstevel@tonic-gate configd_critical("unable to quiesce database\n"); 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate if (backend_panic_abort) 4357c478bd9Sstevel@tonic-gate abort(); 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate exit(CONFIGD_EXIT_DATABASE_BAD); 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate /* 4417c478bd9Sstevel@tonic-gate * Returns 4427c478bd9Sstevel@tonic-gate * _SUCCESS 4437c478bd9Sstevel@tonic-gate * _DONE - callback aborted query 4447c478bd9Sstevel@tonic-gate * _NO_RESOURCES - out of memory (_FULL & _TOOBIG?) 4457c478bd9Sstevel@tonic-gate */ 4467c478bd9Sstevel@tonic-gate static int 4477c478bd9Sstevel@tonic-gate backend_error(sqlite_backend_t *be, int error, char *errmsg) 4487c478bd9Sstevel@tonic-gate { 4497c478bd9Sstevel@tonic-gate if (error == SQLITE_OK) 4507c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate switch (error) { 4537c478bd9Sstevel@tonic-gate case SQLITE_ABORT: 4547c478bd9Sstevel@tonic-gate free(errmsg); 4557c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_DONE); 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate case SQLITE_NOMEM: 4587c478bd9Sstevel@tonic-gate case SQLITE_FULL: 4597c478bd9Sstevel@tonic-gate case SQLITE_TOOBIG: 4607c478bd9Sstevel@tonic-gate free(errmsg); 4617c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_NO_RESOURCES); 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate default: 4647c478bd9Sstevel@tonic-gate backend_panic("%s: db error: %s", be->be_path, errmsg); 4657c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 4667c478bd9Sstevel@tonic-gate } 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate static void 4707c478bd9Sstevel@tonic-gate backend_backup_cleanup(const char **out_arg, ssize_t out_sz) 4717c478bd9Sstevel@tonic-gate { 4727c478bd9Sstevel@tonic-gate char **out = (char **)out_arg; 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate while (out_sz-- > 0) 4757c478bd9Sstevel@tonic-gate free(*out++); 4767c478bd9Sstevel@tonic-gate free(out_arg); 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate /* 4807c478bd9Sstevel@tonic-gate * builds a inverse-time-sorted array of backup files. The path is a 4817c478bd9Sstevel@tonic-gate * a single buffer, and the pointers look like: 4827c478bd9Sstevel@tonic-gate * 4837c478bd9Sstevel@tonic-gate * /this/is/a/full/path/to/repository-name-YYYYMMDDHHMMSS 4847c478bd9Sstevel@tonic-gate * ^pathname ^ ^(pathname+pathlen) 4857c478bd9Sstevel@tonic-gate * basename 4867c478bd9Sstevel@tonic-gate * 4877c478bd9Sstevel@tonic-gate * dirname will either be pathname, or ".". 4887c478bd9Sstevel@tonic-gate * 4897c478bd9Sstevel@tonic-gate * Returns the number of elements in the array, 0 if there are no previous 4907c478bd9Sstevel@tonic-gate * backups, or -1 on error. 4917c478bd9Sstevel@tonic-gate */ 4927c478bd9Sstevel@tonic-gate static ssize_t 4937c478bd9Sstevel@tonic-gate backend_backup_get_prev(char *pathname, size_t pathlen, const char ***out_arg) 4947c478bd9Sstevel@tonic-gate { 4957c478bd9Sstevel@tonic-gate char b_start, b_end; 4967c478bd9Sstevel@tonic-gate DIR *dir; 4977c478bd9Sstevel@tonic-gate char **out = NULL; 4987c478bd9Sstevel@tonic-gate char *name, *p; 4997c478bd9Sstevel@tonic-gate char *dirname, *basename; 5007c478bd9Sstevel@tonic-gate char *pathend; 5017c478bd9Sstevel@tonic-gate struct dirent *ent; 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate size_t count = 0; 5047c478bd9Sstevel@tonic-gate size_t baselen; 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate /* 5077c478bd9Sstevel@tonic-gate * year, month, day, hour, min, sec, plus an '_'. 5087c478bd9Sstevel@tonic-gate */ 5097c478bd9Sstevel@tonic-gate const size_t ndigits = 4 + 5*2 + 1; 5107c478bd9Sstevel@tonic-gate const size_t baroffset = 4 + 2*2; 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate size_t idx; 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate pathend = pathname + pathlen; 5157c478bd9Sstevel@tonic-gate b_end = *pathend; 5167c478bd9Sstevel@tonic-gate *pathend = '\0'; 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate basename = strrchr(pathname, '/'); 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate if (basename != NULL) { 5217c478bd9Sstevel@tonic-gate assert(pathend > pathname && basename < pathend); 5227c478bd9Sstevel@tonic-gate basename++; 5237c478bd9Sstevel@tonic-gate dirname = pathname; 5247c478bd9Sstevel@tonic-gate } else { 5257c478bd9Sstevel@tonic-gate basename = pathname; 5267c478bd9Sstevel@tonic-gate dirname = "."; 5277c478bd9Sstevel@tonic-gate } 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate baselen = strlen(basename); 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate /* 5327c478bd9Sstevel@tonic-gate * munge the string temporarily for the opendir(), then restore it. 5337c478bd9Sstevel@tonic-gate */ 5347c478bd9Sstevel@tonic-gate b_start = basename[0]; 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate basename[0] = '\0'; 5377c478bd9Sstevel@tonic-gate dir = opendir(dirname); 5387c478bd9Sstevel@tonic-gate basename[0] = b_start; /* restore path */ 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate if (dir == NULL) 5417c478bd9Sstevel@tonic-gate goto fail; 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate while ((ent = readdir(dir)) != NULL) { 5457c478bd9Sstevel@tonic-gate /* 5467c478bd9Sstevel@tonic-gate * Must match: 5477c478bd9Sstevel@tonic-gate * basename-YYYYMMDD_HHMMSS 5487c478bd9Sstevel@tonic-gate * or we ignore it. 5497c478bd9Sstevel@tonic-gate */ 5507c478bd9Sstevel@tonic-gate if (strncmp(ent->d_name, basename, baselen) != 0) 5517c478bd9Sstevel@tonic-gate continue; 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate name = ent->d_name; 5547c478bd9Sstevel@tonic-gate if (name[baselen] != '-') 5557c478bd9Sstevel@tonic-gate continue; 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate p = name + baselen + 1; 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate for (idx = 0; idx < ndigits; idx++) { 5607c478bd9Sstevel@tonic-gate char c = p[idx]; 5617c478bd9Sstevel@tonic-gate if (idx == baroffset && c != '_') 5627c478bd9Sstevel@tonic-gate break; 5637c478bd9Sstevel@tonic-gate if (idx != baroffset && (c < '0' || c > '9')) 5647c478bd9Sstevel@tonic-gate break; 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate if (idx != ndigits || p[idx] != '\0') 5677c478bd9Sstevel@tonic-gate continue; 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate /* 5707c478bd9Sstevel@tonic-gate * We have a match. insertion-sort it into our list. 5717c478bd9Sstevel@tonic-gate */ 5727c478bd9Sstevel@tonic-gate name = strdup(name); 5737c478bd9Sstevel@tonic-gate if (name == NULL) 5747c478bd9Sstevel@tonic-gate goto fail_closedir; 5757c478bd9Sstevel@tonic-gate p = strrchr(name, '-'); 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate for (idx = 0; idx < count; idx++) { 5787c478bd9Sstevel@tonic-gate char *tmp = out[idx]; 5797c478bd9Sstevel@tonic-gate char *tp = strrchr(tmp, '-'); 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate int cmp = strcmp(p, tp); 5827c478bd9Sstevel@tonic-gate if (cmp == 0) 5837c478bd9Sstevel@tonic-gate cmp = strcmp(name, tmp); 5847c478bd9Sstevel@tonic-gate 5857c478bd9Sstevel@tonic-gate if (cmp == 0) { 5867c478bd9Sstevel@tonic-gate free(name); 5877c478bd9Sstevel@tonic-gate name = NULL; 5887c478bd9Sstevel@tonic-gate break; 5897c478bd9Sstevel@tonic-gate } else if (cmp > 0) { 5907c478bd9Sstevel@tonic-gate out[idx] = name; 5917c478bd9Sstevel@tonic-gate name = tmp; 5927c478bd9Sstevel@tonic-gate p = tp; 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate if (idx == count) { 5977c478bd9Sstevel@tonic-gate char **new_out = realloc(out, 5987c478bd9Sstevel@tonic-gate (count + 1) * sizeof (*out)); 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate if (new_out == NULL) { 6017c478bd9Sstevel@tonic-gate free(name); 6027c478bd9Sstevel@tonic-gate goto fail_closedir; 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate out = new_out; 6067c478bd9Sstevel@tonic-gate out[count++] = name; 6077c478bd9Sstevel@tonic-gate } else { 6087c478bd9Sstevel@tonic-gate assert(name == NULL); 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate (void) closedir(dir); 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate basename[baselen] = b_end; 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate *out_arg = (const char **)out; 6167c478bd9Sstevel@tonic-gate return (count); 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate fail_closedir: 6197c478bd9Sstevel@tonic-gate (void) closedir(dir); 6207c478bd9Sstevel@tonic-gate fail: 6217c478bd9Sstevel@tonic-gate basename[0] = b_start; 6227c478bd9Sstevel@tonic-gate *pathend = b_end; 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate backend_backup_cleanup((const char **)out, count); 6257c478bd9Sstevel@tonic-gate 6267c478bd9Sstevel@tonic-gate *out_arg = NULL; 6277c478bd9Sstevel@tonic-gate return (-1); 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate /* 6317c478bd9Sstevel@tonic-gate * Copies the repository path into out, a buffer of out_len bytes, 6327c478bd9Sstevel@tonic-gate * removes the ".db" (or whatever) extension, and, if name is non-NULL, 6337c478bd9Sstevel@tonic-gate * appends "-name" to it. If name is non-NULL, it can fail with: 6347c478bd9Sstevel@tonic-gate * 6357c478bd9Sstevel@tonic-gate * _TRUNCATED will not fit in buffer. 6367c478bd9Sstevel@tonic-gate * _BAD_REQUEST name is not a valid identifier 6377c478bd9Sstevel@tonic-gate */ 6387c478bd9Sstevel@tonic-gate static rep_protocol_responseid_t 6397c478bd9Sstevel@tonic-gate backend_backup_base(sqlite_backend_t *be, const char *name, 6407c478bd9Sstevel@tonic-gate char *out, size_t out_len) 6417c478bd9Sstevel@tonic-gate { 6427c478bd9Sstevel@tonic-gate char *p, *q; 6437c478bd9Sstevel@tonic-gate size_t len; 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate /* 6467c478bd9Sstevel@tonic-gate * for paths of the form /path/to/foo.db, we truncate at the final 6477c478bd9Sstevel@tonic-gate * '.'. 6487c478bd9Sstevel@tonic-gate */ 6497c478bd9Sstevel@tonic-gate (void) strlcpy(out, be->be_path, out_len); 6507c478bd9Sstevel@tonic-gate 6517c478bd9Sstevel@tonic-gate p = strrchr(out, '/'); 6527c478bd9Sstevel@tonic-gate q = strrchr(out, '.'); 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate if (p != NULL && q != NULL && q > p) 6557c478bd9Sstevel@tonic-gate *q = 0; 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate if (name != NULL) { 6587c478bd9Sstevel@tonic-gate len = strlen(out); 6597c478bd9Sstevel@tonic-gate assert(len < out_len); 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate out += len; 6627c478bd9Sstevel@tonic-gate out_len -= len; 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate len = strlen(name); 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate /* 6677c478bd9Sstevel@tonic-gate * verify that the name tag is entirely alphabetic, 6687c478bd9Sstevel@tonic-gate * non-empty, and not too long. 6697c478bd9Sstevel@tonic-gate */ 6707c478bd9Sstevel@tonic-gate if (len == 0 || len >= REP_PROTOCOL_NAME_LEN || 6717c478bd9Sstevel@tonic-gate uu_check_name(name, UU_NAME_DOMAIN) < 0) 6727c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST); 6737c478bd9Sstevel@tonic-gate 6747c478bd9Sstevel@tonic-gate if (snprintf(out, out_len, "-%s", name) >= out_len) 6757c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_TRUNCATED); 6767c478bd9Sstevel@tonic-gate } 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 6797c478bd9Sstevel@tonic-gate } 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate /* 6828918dff3Sjwadams * See if a backup is needed. We do a backup unless both files are 6838918dff3Sjwadams * byte-for-byte identical. 6848918dff3Sjwadams */ 6858918dff3Sjwadams static int 6868918dff3Sjwadams backend_check_backup_needed(const char *rep_name, const char *backup_name) 6878918dff3Sjwadams { 6888918dff3Sjwadams int repfd = open(rep_name, O_RDONLY); 6898918dff3Sjwadams int fd = open(backup_name, O_RDONLY); 6908918dff3Sjwadams struct stat s_rep, s_backup; 6918918dff3Sjwadams int c1, c2; 6928918dff3Sjwadams 6938918dff3Sjwadams FILE *f_rep = NULL; 6948918dff3Sjwadams FILE *f_backup = NULL; 6958918dff3Sjwadams 6968918dff3Sjwadams if (repfd < 0 || fd < 0) 6978918dff3Sjwadams goto fail; 6988918dff3Sjwadams 6998918dff3Sjwadams if (fstat(repfd, &s_rep) < 0 || fstat(fd, &s_backup) < 0) 7008918dff3Sjwadams goto fail; 7018918dff3Sjwadams 7028918dff3Sjwadams /* 7038918dff3Sjwadams * if they are the same file, we need to do a backup to break the 7048918dff3Sjwadams * hard link or symlink involved. 7058918dff3Sjwadams */ 7068918dff3Sjwadams if (s_rep.st_ino == s_backup.st_ino && s_rep.st_dev == s_backup.st_dev) 7078918dff3Sjwadams goto fail; 7088918dff3Sjwadams 7098918dff3Sjwadams if (s_rep.st_size != s_backup.st_size) 7108918dff3Sjwadams goto fail; 7118918dff3Sjwadams 7128918dff3Sjwadams if ((f_rep = fdopen(repfd, "r")) == NULL || 7138918dff3Sjwadams (f_backup = fdopen(fd, "r")) == NULL) 7148918dff3Sjwadams goto fail; 7158918dff3Sjwadams 7168918dff3Sjwadams do { 7178918dff3Sjwadams c1 = getc(f_rep); 7188918dff3Sjwadams c2 = getc(f_backup); 7198918dff3Sjwadams if (c1 != c2) 7208918dff3Sjwadams goto fail; 7218918dff3Sjwadams } while (c1 != EOF); 7228918dff3Sjwadams 7238918dff3Sjwadams if (!ferror(f_rep) && !ferror(f_backup)) { 7248918dff3Sjwadams (void) fclose(f_rep); 7258918dff3Sjwadams (void) fclose(f_backup); 7268918dff3Sjwadams (void) close(repfd); 7278918dff3Sjwadams (void) close(fd); 7288918dff3Sjwadams return (0); 7298918dff3Sjwadams } 7308918dff3Sjwadams 7318918dff3Sjwadams fail: 7328918dff3Sjwadams if (f_rep != NULL) 7338918dff3Sjwadams (void) fclose(f_rep); 7348918dff3Sjwadams if (f_backup != NULL) 7358918dff3Sjwadams (void) fclose(f_backup); 7368918dff3Sjwadams if (repfd >= 0) 7378918dff3Sjwadams (void) close(repfd); 7388918dff3Sjwadams if (fd >= 0) 7398918dff3Sjwadams (void) close(fd); 7408918dff3Sjwadams return (1); 7418918dff3Sjwadams } 7428918dff3Sjwadams 7438918dff3Sjwadams /* 744*c0889d7aSstevep * This interface is called to perform the actual copy 745*c0889d7aSstevep * 746*c0889d7aSstevep * Return: 747*c0889d7aSstevep * _FAIL_UNKNOWN read/write fails 748*c0889d7aSstevep * _FAIL_NO_RESOURCES out of memory 749*c0889d7aSstevep * _SUCCESS copy succeeds 750*c0889d7aSstevep */ 751*c0889d7aSstevep static rep_protocol_responseid_t 752*c0889d7aSstevep backend_do_copy(const char *src, int srcfd, const char *dst, 753*c0889d7aSstevep int dstfd, size_t *sz) 754*c0889d7aSstevep { 755*c0889d7aSstevep char *buf; 756*c0889d7aSstevep off_t nrd, nwr, n, r_off = 0, w_off = 0; 757*c0889d7aSstevep 758*c0889d7aSstevep if ((buf = malloc(8192)) == NULL) 759*c0889d7aSstevep return (REP_PROTOCOL_FAIL_NO_RESOURCES); 760*c0889d7aSstevep 761*c0889d7aSstevep while ((nrd = read(srcfd, buf, 8192)) != 0) { 762*c0889d7aSstevep if (nrd < 0) { 763*c0889d7aSstevep if (errno == EINTR) 764*c0889d7aSstevep continue; 765*c0889d7aSstevep 766*c0889d7aSstevep configd_critical( 767*c0889d7aSstevep "Backend copy failed: fails to read from %s " 768*c0889d7aSstevep "at offset %d: %s\n", src, r_off, strerror(errno)); 769*c0889d7aSstevep free(buf); 770*c0889d7aSstevep return (REP_PROTOCOL_FAIL_UNKNOWN); 771*c0889d7aSstevep } 772*c0889d7aSstevep 773*c0889d7aSstevep r_off += nrd; 774*c0889d7aSstevep 775*c0889d7aSstevep nwr = 0; 776*c0889d7aSstevep do { 777*c0889d7aSstevep if ((n = write(dstfd, &buf[nwr], nrd - nwr)) < 0) { 778*c0889d7aSstevep if (errno == EINTR) 779*c0889d7aSstevep continue; 780*c0889d7aSstevep 781*c0889d7aSstevep configd_critical( 782*c0889d7aSstevep "Backend copy failed: fails to write to %s " 783*c0889d7aSstevep "at offset %d: %s\n", dst, w_off, 784*c0889d7aSstevep strerror(errno)); 785*c0889d7aSstevep free(buf); 786*c0889d7aSstevep return (REP_PROTOCOL_FAIL_UNKNOWN); 787*c0889d7aSstevep } 788*c0889d7aSstevep 789*c0889d7aSstevep nwr += n; 790*c0889d7aSstevep w_off += n; 791*c0889d7aSstevep 792*c0889d7aSstevep } while (nwr < nrd); 793*c0889d7aSstevep } 794*c0889d7aSstevep 795*c0889d7aSstevep if (sz) 796*c0889d7aSstevep *sz = w_off; 797*c0889d7aSstevep 798*c0889d7aSstevep free(buf); 799*c0889d7aSstevep return (REP_PROTOCOL_SUCCESS); 800*c0889d7aSstevep } 801*c0889d7aSstevep 802*c0889d7aSstevep /* 8037c478bd9Sstevel@tonic-gate * Can return: 8047c478bd9Sstevel@tonic-gate * _BAD_REQUEST name is not valid 8057c478bd9Sstevel@tonic-gate * _TRUNCATED name is too long for current repository path 8067c478bd9Sstevel@tonic-gate * _UNKNOWN failed for unknown reason (details written to 8077c478bd9Sstevel@tonic-gate * console) 8087c478bd9Sstevel@tonic-gate * _BACKEND_READONLY backend is not writable 809*c0889d7aSstevep * _NO_RESOURCES out of memory 8107c478bd9Sstevel@tonic-gate * _SUCCESS Backup completed successfully. 8117c478bd9Sstevel@tonic-gate */ 8127c478bd9Sstevel@tonic-gate static rep_protocol_responseid_t 8137c478bd9Sstevel@tonic-gate backend_create_backup_locked(sqlite_backend_t *be, const char *name) 8147c478bd9Sstevel@tonic-gate { 8157c478bd9Sstevel@tonic-gate const char **old_list; 8167c478bd9Sstevel@tonic-gate ssize_t old_sz; 8177c478bd9Sstevel@tonic-gate ssize_t old_max = max_repository_backups; 8187c478bd9Sstevel@tonic-gate ssize_t cur; 8197c478bd9Sstevel@tonic-gate char *finalname; 820*c0889d7aSstevep char *finalpath; 821*c0889d7aSstevep char *tmppath; 8227c478bd9Sstevel@tonic-gate int infd, outfd; 8237c478bd9Sstevel@tonic-gate size_t len; 8247c478bd9Sstevel@tonic-gate time_t now; 8257c478bd9Sstevel@tonic-gate struct tm now_tm; 8267c478bd9Sstevel@tonic-gate rep_protocol_responseid_t result; 8277c478bd9Sstevel@tonic-gate 828*c0889d7aSstevep if ((finalpath = malloc(PATH_MAX)) == NULL) 829*c0889d7aSstevep return (REP_PROTOCOL_FAIL_NO_RESOURCES); 8307c478bd9Sstevel@tonic-gate 831*c0889d7aSstevep if ((tmppath = malloc(PATH_MAX)) == NULL) { 832*c0889d7aSstevep free(finalpath); 833*c0889d7aSstevep return (REP_PROTOCOL_FAIL_NO_RESOURCES); 834*c0889d7aSstevep } 835*c0889d7aSstevep 836*c0889d7aSstevep if (be->be_readonly) { 837*c0889d7aSstevep result = REP_PROTOCOL_FAIL_BACKEND_READONLY; 838*c0889d7aSstevep goto out; 839*c0889d7aSstevep } 840*c0889d7aSstevep 841*c0889d7aSstevep result = backend_backup_base(be, name, finalpath, PATH_MAX); 8427c478bd9Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 843*c0889d7aSstevep goto out; 8447c478bd9Sstevel@tonic-gate 8458918dff3Sjwadams if (!backend_check_backup_needed(be->be_path, finalpath)) { 846*c0889d7aSstevep result = REP_PROTOCOL_SUCCESS; 847*c0889d7aSstevep goto out; 8488918dff3Sjwadams } 8498918dff3Sjwadams 8507c478bd9Sstevel@tonic-gate /* 8517c478bd9Sstevel@tonic-gate * remember the original length, and the basename location 8527c478bd9Sstevel@tonic-gate */ 8537c478bd9Sstevel@tonic-gate len = strlen(finalpath); 8547c478bd9Sstevel@tonic-gate finalname = strrchr(finalpath, '/'); 8557c478bd9Sstevel@tonic-gate if (finalname != NULL) 8567c478bd9Sstevel@tonic-gate finalname++; 8577c478bd9Sstevel@tonic-gate else 8587c478bd9Sstevel@tonic-gate finalname = finalpath; 8597c478bd9Sstevel@tonic-gate 860*c0889d7aSstevep (void) strlcpy(tmppath, finalpath, PATH_MAX); 861*c0889d7aSstevep if (strlcat(tmppath, "-tmpXXXXXX", PATH_MAX) >= PATH_MAX) { 862*c0889d7aSstevep result = REP_PROTOCOL_FAIL_TRUNCATED; 863*c0889d7aSstevep goto out; 864*c0889d7aSstevep } 8657c478bd9Sstevel@tonic-gate 8667c478bd9Sstevel@tonic-gate now = time(NULL); 8677c478bd9Sstevel@tonic-gate if (localtime_r(&now, &now_tm) == NULL) { 8687c478bd9Sstevel@tonic-gate configd_critical( 8697c478bd9Sstevel@tonic-gate "\"%s\" backup failed: localtime(3C) failed: %s\n", name, 8707c478bd9Sstevel@tonic-gate be->be_path, strerror(errno)); 871*c0889d7aSstevep result = REP_PROTOCOL_FAIL_UNKNOWN; 872*c0889d7aSstevep goto out; 8737c478bd9Sstevel@tonic-gate } 8747c478bd9Sstevel@tonic-gate 875*c0889d7aSstevep if (strftime(finalpath + len, PATH_MAX - len, 876*c0889d7aSstevep "-%Y""%m""%d""_""%H""%M""%S", &now_tm) >= PATH_MAX - len) { 877*c0889d7aSstevep result = REP_PROTOCOL_FAIL_TRUNCATED; 878*c0889d7aSstevep goto out; 8797c478bd9Sstevel@tonic-gate } 8807c478bd9Sstevel@tonic-gate 8817c478bd9Sstevel@tonic-gate infd = open(be->be_path, O_RDONLY); 8827c478bd9Sstevel@tonic-gate if (infd < 0) { 8837c478bd9Sstevel@tonic-gate configd_critical("\"%s\" backup failed: opening %s: %s\n", name, 8847c478bd9Sstevel@tonic-gate be->be_path, strerror(errno)); 885*c0889d7aSstevep result = REP_PROTOCOL_FAIL_UNKNOWN; 886*c0889d7aSstevep goto out; 8877c478bd9Sstevel@tonic-gate } 8887c478bd9Sstevel@tonic-gate 8897c478bd9Sstevel@tonic-gate outfd = mkstemp(tmppath); 8907c478bd9Sstevel@tonic-gate if (outfd < 0) { 8917c478bd9Sstevel@tonic-gate configd_critical("\"%s\" backup failed: mkstemp(%s): %s\n", 8927c478bd9Sstevel@tonic-gate name, tmppath, strerror(errno)); 8937c478bd9Sstevel@tonic-gate (void) close(infd); 8947c478bd9Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_UNKNOWN; 895*c0889d7aSstevep goto out; 8967c478bd9Sstevel@tonic-gate } 8977c478bd9Sstevel@tonic-gate 898*c0889d7aSstevep if ((result = backend_do_copy((const char *)be->be_path, infd, 899*c0889d7aSstevep (const char *)tmppath, outfd, NULL)) != REP_PROTOCOL_SUCCESS) 9007c478bd9Sstevel@tonic-gate goto fail; 9017c478bd9Sstevel@tonic-gate 9027c478bd9Sstevel@tonic-gate /* 9037c478bd9Sstevel@tonic-gate * grab the old list before doing our re-name. 9047c478bd9Sstevel@tonic-gate */ 9057c478bd9Sstevel@tonic-gate if (old_max > 0) 9067c478bd9Sstevel@tonic-gate old_sz = backend_backup_get_prev(finalpath, len, &old_list); 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate if (rename(tmppath, finalpath) < 0) { 9097c478bd9Sstevel@tonic-gate configd_critical( 9107c478bd9Sstevel@tonic-gate "\"%s\" backup failed: rename(%s, %s): %s\n", 9117c478bd9Sstevel@tonic-gate name, tmppath, finalpath, strerror(errno)); 9127c478bd9Sstevel@tonic-gate result = REP_PROTOCOL_FAIL_UNKNOWN; 9137c478bd9Sstevel@tonic-gate goto fail; 9147c478bd9Sstevel@tonic-gate } 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate tmppath[len] = 0; /* strip -XXXXXX, for reference symlink */ 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate (void) unlink(tmppath); 9197c478bd9Sstevel@tonic-gate if (symlink(finalname, tmppath) < 0) { 9207c478bd9Sstevel@tonic-gate configd_critical( 9217c478bd9Sstevel@tonic-gate "\"%s\" backup completed, but updating " 9227c478bd9Sstevel@tonic-gate "\"%s\" symlink to \"%s\" failed: %s\n", 9237c478bd9Sstevel@tonic-gate name, tmppath, finalname, strerror(errno)); 9247c478bd9Sstevel@tonic-gate } 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate if (old_max > 0 && old_sz > 0) { 9277c478bd9Sstevel@tonic-gate /* unlink all but the first (old_max - 1) files */ 9287c478bd9Sstevel@tonic-gate for (cur = old_max - 1; cur < old_sz; cur++) { 9297c478bd9Sstevel@tonic-gate (void) strlcpy(finalname, old_list[cur], 930*c0889d7aSstevep PATH_MAX - (finalname - finalpath)); 9317c478bd9Sstevel@tonic-gate if (unlink(finalpath) < 0) 9327c478bd9Sstevel@tonic-gate configd_critical( 9337c478bd9Sstevel@tonic-gate "\"%s\" backup completed, but removing old " 9347c478bd9Sstevel@tonic-gate "file \"%s\" failed: %s\n", 9357c478bd9Sstevel@tonic-gate name, finalpath, strerror(errno)); 9367c478bd9Sstevel@tonic-gate } 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate backend_backup_cleanup(old_list, old_sz); 9397c478bd9Sstevel@tonic-gate } 9407c478bd9Sstevel@tonic-gate 9417c478bd9Sstevel@tonic-gate result = REP_PROTOCOL_SUCCESS; 9427c478bd9Sstevel@tonic-gate 9437c478bd9Sstevel@tonic-gate fail: 9447c478bd9Sstevel@tonic-gate (void) close(infd); 9457c478bd9Sstevel@tonic-gate (void) close(outfd); 9467c478bd9Sstevel@tonic-gate if (result != REP_PROTOCOL_SUCCESS) 9477c478bd9Sstevel@tonic-gate (void) unlink(tmppath); 9487c478bd9Sstevel@tonic-gate 949*c0889d7aSstevep out: 950*c0889d7aSstevep free(finalpath); 951*c0889d7aSstevep free(tmppath); 952*c0889d7aSstevep 9537c478bd9Sstevel@tonic-gate return (result); 9547c478bd9Sstevel@tonic-gate } 9557c478bd9Sstevel@tonic-gate 9568918dff3Sjwadams static int 9578918dff3Sjwadams backend_check_readonly(sqlite_backend_t *be, int writing, hrtime_t t) 9588918dff3Sjwadams { 9598918dff3Sjwadams char *errp; 9608918dff3Sjwadams struct sqlite *new; 9618918dff3Sjwadams int r; 9628918dff3Sjwadams 9638918dff3Sjwadams assert(be->be_readonly); 9648918dff3Sjwadams assert(be == bes[BACKEND_TYPE_NORMAL]); 9658918dff3Sjwadams 9668918dff3Sjwadams /* 9678918dff3Sjwadams * If we don't *need* to be writable, only check every once in a 9688918dff3Sjwadams * while. 9698918dff3Sjwadams */ 9708918dff3Sjwadams if (!writing) { 9718918dff3Sjwadams if ((uint64_t)(t - be->be_lastcheck) < 9728918dff3Sjwadams BACKEND_READONLY_CHECK_INTERVAL) 9738918dff3Sjwadams return (REP_PROTOCOL_SUCCESS); 9748918dff3Sjwadams be->be_lastcheck = t; 9758918dff3Sjwadams } 9768918dff3Sjwadams 9778918dff3Sjwadams new = sqlite_open(be->be_path, 0600, &errp); 9788918dff3Sjwadams if (new == NULL) { 9798918dff3Sjwadams backend_panic("reopening %s: %s\n", be->be_path, errp); 9808918dff3Sjwadams /*NOTREACHED*/ 9818918dff3Sjwadams } 9828918dff3Sjwadams r = backend_is_readonly(new, be->be_path); 9838918dff3Sjwadams 9848918dff3Sjwadams if (r != SQLITE_OK) { 9858918dff3Sjwadams sqlite_close(new); 9868918dff3Sjwadams if (writing) 9878918dff3Sjwadams return (REP_PROTOCOL_FAIL_BACKEND_READONLY); 9888918dff3Sjwadams return (REP_PROTOCOL_SUCCESS); 9898918dff3Sjwadams } 9908918dff3Sjwadams 9918918dff3Sjwadams /* 9928918dff3Sjwadams * We can write! Swap the db handles, mark ourself writable, 9938918dff3Sjwadams * and make a backup. 9948918dff3Sjwadams */ 9958918dff3Sjwadams sqlite_close(be->be_db); 9968918dff3Sjwadams be->be_db = new; 9978918dff3Sjwadams be->be_readonly = 0; 9988918dff3Sjwadams 9998918dff3Sjwadams if (backend_create_backup_locked(be, REPOSITORY_BOOT_BACKUP) != 10008918dff3Sjwadams REP_PROTOCOL_SUCCESS) { 10018918dff3Sjwadams configd_critical( 10028918dff3Sjwadams "unable to create \"%s\" backup of \"%s\"\n", 10038918dff3Sjwadams REPOSITORY_BOOT_BACKUP, be->be_path); 10048918dff3Sjwadams } 10058918dff3Sjwadams 10068918dff3Sjwadams return (REP_PROTOCOL_SUCCESS); 10078918dff3Sjwadams } 10087c478bd9Sstevel@tonic-gate 10097c478bd9Sstevel@tonic-gate /* 10107c478bd9Sstevel@tonic-gate * If t is not BACKEND_TYPE_NORMAL, can fail with 10117c478bd9Sstevel@tonic-gate * _BACKEND_ACCESS - backend does not exist 10127c478bd9Sstevel@tonic-gate * 10137c478bd9Sstevel@tonic-gate * If writing is nonzero, can also fail with 10147c478bd9Sstevel@tonic-gate * _BACKEND_READONLY - backend is read-only 10157c478bd9Sstevel@tonic-gate */ 10167c478bd9Sstevel@tonic-gate static int 10177c478bd9Sstevel@tonic-gate backend_lock(backend_type_t t, int writing, sqlite_backend_t **bep) 10187c478bd9Sstevel@tonic-gate { 10197c478bd9Sstevel@tonic-gate sqlite_backend_t *be = NULL; 10207c478bd9Sstevel@tonic-gate hrtime_t ts, vts; 10217c478bd9Sstevel@tonic-gate 10227c478bd9Sstevel@tonic-gate *bep = NULL; 10237c478bd9Sstevel@tonic-gate 10247c478bd9Sstevel@tonic-gate assert(t == BACKEND_TYPE_NORMAL || 10257c478bd9Sstevel@tonic-gate t == BACKEND_TYPE_NONPERSIST); 10267c478bd9Sstevel@tonic-gate 10277c478bd9Sstevel@tonic-gate be = bes[t]; 10287c478bd9Sstevel@tonic-gate if (t == BACKEND_TYPE_NORMAL) 10297c478bd9Sstevel@tonic-gate assert(be != NULL); /* should always be there */ 10307c478bd9Sstevel@tonic-gate 10317c478bd9Sstevel@tonic-gate if (be == NULL) 10327c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BACKEND_ACCESS); 10337c478bd9Sstevel@tonic-gate 10347c478bd9Sstevel@tonic-gate if (backend_panic_thread != 0) 10357c478bd9Sstevel@tonic-gate backend_panic(NULL); /* don't proceed */ 10367c478bd9Sstevel@tonic-gate 10377c478bd9Sstevel@tonic-gate ts = gethrtime(); 10387c478bd9Sstevel@tonic-gate vts = gethrvtime(); 10397c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&be->be_lock); 10407c478bd9Sstevel@tonic-gate UPDATE_TOTALS_WR(be, writing, bt_lock, ts, vts); 10417c478bd9Sstevel@tonic-gate 10427c478bd9Sstevel@tonic-gate if (backend_panic_thread != 0) { 10437c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&be->be_lock); 10447c478bd9Sstevel@tonic-gate backend_panic(NULL); /* don't proceed */ 10457c478bd9Sstevel@tonic-gate } 10467c478bd9Sstevel@tonic-gate be->be_thread = pthread_self(); 10477c478bd9Sstevel@tonic-gate 10488918dff3Sjwadams if (be->be_readonly) { 10497c478bd9Sstevel@tonic-gate int r; 10507c478bd9Sstevel@tonic-gate assert(t == BACKEND_TYPE_NORMAL); 10517c478bd9Sstevel@tonic-gate 10528918dff3Sjwadams r = backend_check_readonly(be, writing, ts); 10538918dff3Sjwadams if (r != REP_PROTOCOL_SUCCESS) { 10548918dff3Sjwadams be->be_thread = 0; 10558918dff3Sjwadams (void) pthread_mutex_unlock(&be->be_lock); 10568918dff3Sjwadams return (r); 10577c478bd9Sstevel@tonic-gate } 10588918dff3Sjwadams } 10598918dff3Sjwadams 10607c478bd9Sstevel@tonic-gate if (backend_do_trace) 10617c478bd9Sstevel@tonic-gate (void) sqlite_trace(be->be_db, backend_trace_sql, be); 10627c478bd9Sstevel@tonic-gate else 10637c478bd9Sstevel@tonic-gate (void) sqlite_trace(be->be_db, NULL, NULL); 10647c478bd9Sstevel@tonic-gate 10657c478bd9Sstevel@tonic-gate be->be_writing = writing; 10667c478bd9Sstevel@tonic-gate *bep = be; 10677c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 10687c478bd9Sstevel@tonic-gate } 10697c478bd9Sstevel@tonic-gate 10707c478bd9Sstevel@tonic-gate static void 10717c478bd9Sstevel@tonic-gate backend_unlock(sqlite_backend_t *be) 10727c478bd9Sstevel@tonic-gate { 10737c478bd9Sstevel@tonic-gate be->be_writing = 0; 10747c478bd9Sstevel@tonic-gate be->be_thread = 0; 10757c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&be->be_lock); 10767c478bd9Sstevel@tonic-gate } 10777c478bd9Sstevel@tonic-gate 10787c478bd9Sstevel@tonic-gate static void 10797c478bd9Sstevel@tonic-gate backend_destroy(sqlite_backend_t *be) 10807c478bd9Sstevel@tonic-gate { 10817c478bd9Sstevel@tonic-gate if (be->be_db != NULL) { 10827c478bd9Sstevel@tonic-gate sqlite_close(be->be_db); 10837c478bd9Sstevel@tonic-gate be->be_db = NULL; 10847c478bd9Sstevel@tonic-gate } 10857c478bd9Sstevel@tonic-gate be->be_thread = 0; 10867c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&be->be_lock); 10877c478bd9Sstevel@tonic-gate (void) pthread_mutex_destroy(&be->be_lock); 10887c478bd9Sstevel@tonic-gate } 10897c478bd9Sstevel@tonic-gate 10907c478bd9Sstevel@tonic-gate static void 10917c478bd9Sstevel@tonic-gate backend_create_finish(backend_type_t backend_id, sqlite_backend_t *be) 10927c478bd9Sstevel@tonic-gate { 10937c478bd9Sstevel@tonic-gate assert(MUTEX_HELD(&be->be_lock)); 10947c478bd9Sstevel@tonic-gate assert(be == &be_info[backend_id]); 10957c478bd9Sstevel@tonic-gate 10967c478bd9Sstevel@tonic-gate bes[backend_id] = be; 10977c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&be->be_lock); 10987c478bd9Sstevel@tonic-gate } 10997c478bd9Sstevel@tonic-gate 11007c478bd9Sstevel@tonic-gate static int 11017c478bd9Sstevel@tonic-gate backend_fd_write(int fd, const char *mess) 11027c478bd9Sstevel@tonic-gate { 11037c478bd9Sstevel@tonic-gate int len = strlen(mess); 11047c478bd9Sstevel@tonic-gate int written; 11057c478bd9Sstevel@tonic-gate 11067c478bd9Sstevel@tonic-gate while (len > 0) { 11077c478bd9Sstevel@tonic-gate if ((written = write(fd, mess, len)) < 0) 11087c478bd9Sstevel@tonic-gate return (-1); 11097c478bd9Sstevel@tonic-gate mess += written; 11107c478bd9Sstevel@tonic-gate len -= written; 11117c478bd9Sstevel@tonic-gate } 11127c478bd9Sstevel@tonic-gate return (0); 11137c478bd9Sstevel@tonic-gate } 11147c478bd9Sstevel@tonic-gate 11157c478bd9Sstevel@tonic-gate /* 11167c478bd9Sstevel@tonic-gate * Can return: 11177c478bd9Sstevel@tonic-gate * _BAD_REQUEST name is not valid 11187c478bd9Sstevel@tonic-gate * _TRUNCATED name is too long for current repository path 11197c478bd9Sstevel@tonic-gate * _UNKNOWN failed for unknown reason (details written to 11207c478bd9Sstevel@tonic-gate * console) 11217c478bd9Sstevel@tonic-gate * _BACKEND_READONLY backend is not writable 1122*c0889d7aSstevep * _NO_RESOURCES out of memory 11237c478bd9Sstevel@tonic-gate * _SUCCESS Backup completed successfully. 11247c478bd9Sstevel@tonic-gate */ 11257c478bd9Sstevel@tonic-gate rep_protocol_responseid_t 11267c478bd9Sstevel@tonic-gate backend_create_backup(const char *name) 11277c478bd9Sstevel@tonic-gate { 11287c478bd9Sstevel@tonic-gate rep_protocol_responseid_t result; 11297c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 11307c478bd9Sstevel@tonic-gate 11317c478bd9Sstevel@tonic-gate result = backend_lock(BACKEND_TYPE_NORMAL, 0, &be); 1132*c0889d7aSstevep assert(result == REP_PROTOCOL_SUCCESS); 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate result = backend_create_backup_locked(be, name); 11357c478bd9Sstevel@tonic-gate backend_unlock(be); 11367c478bd9Sstevel@tonic-gate 11377c478bd9Sstevel@tonic-gate return (result); 11387c478bd9Sstevel@tonic-gate } 11397c478bd9Sstevel@tonic-gate 1140*c0889d7aSstevep /* 1141*c0889d7aSstevep * Copy the repository. If the sw_back flag is not set, we are 1142*c0889d7aSstevep * copying the repository from the default location under /etc/svc to 1143*c0889d7aSstevep * the tmpfs /etc/svc/volatile location. If the flag is set, we are 1144*c0889d7aSstevep * copying back to the /etc/svc location from the volatile location 1145*c0889d7aSstevep * after manifest-import is completed. 1146*c0889d7aSstevep * 1147*c0889d7aSstevep * Can return: 1148*c0889d7aSstevep * 1149*c0889d7aSstevep * REP_PROTOCOL_SUCCESS successful copy and rename 1150*c0889d7aSstevep * REP_PROTOCOL_FAIL_UNKNOWN file operation error 1151*c0889d7aSstevep * REP_PROTOCOL_FAIL_NO_RESOURCES out of memory 1152*c0889d7aSstevep */ 1153*c0889d7aSstevep static rep_protocol_responseid_t 1154*c0889d7aSstevep backend_switch_copy(const char *src, const char *dst, int sw_back) 1155*c0889d7aSstevep { 1156*c0889d7aSstevep int srcfd, dstfd; 1157*c0889d7aSstevep char *tmppath = malloc(PATH_MAX); 1158*c0889d7aSstevep rep_protocol_responseid_t res = REP_PROTOCOL_SUCCESS; 1159*c0889d7aSstevep struct stat s_buf; 1160*c0889d7aSstevep size_t cpsz, sz; 1161*c0889d7aSstevep 1162*c0889d7aSstevep if (tmppath == NULL) { 1163*c0889d7aSstevep res = REP_PROTOCOL_FAIL_NO_RESOURCES; 1164*c0889d7aSstevep goto out; 1165*c0889d7aSstevep } 1166*c0889d7aSstevep 1167*c0889d7aSstevep /* 1168*c0889d7aSstevep * Create and open the related db files 1169*c0889d7aSstevep */ 1170*c0889d7aSstevep (void) strlcpy(tmppath, dst, PATH_MAX); 1171*c0889d7aSstevep sz = strlcat(tmppath, "-XXXXXX", PATH_MAX); 1172*c0889d7aSstevep assert(sz < PATH_MAX); 1173*c0889d7aSstevep if (sz >= PATH_MAX) { 1174*c0889d7aSstevep configd_critical( 1175*c0889d7aSstevep "Backend copy failed: strlcat %s: overflow\n", tmppath); 1176*c0889d7aSstevep abort(); 1177*c0889d7aSstevep } 1178*c0889d7aSstevep 1179*c0889d7aSstevep if ((dstfd = mkstemp(tmppath)) < 0) { 1180*c0889d7aSstevep configd_critical("Backend copy failed: mkstemp %s: %s\n", 1181*c0889d7aSstevep tmppath, strerror(errno)); 1182*c0889d7aSstevep res = REP_PROTOCOL_FAIL_UNKNOWN; 1183*c0889d7aSstevep goto out; 1184*c0889d7aSstevep } 1185*c0889d7aSstevep 1186*c0889d7aSstevep if ((srcfd = open(src, O_RDONLY)) < 0) { 1187*c0889d7aSstevep configd_critical("Backend copy failed: opening %s: %s\n", 1188*c0889d7aSstevep src, strerror(errno)); 1189*c0889d7aSstevep res = REP_PROTOCOL_FAIL_UNKNOWN; 1190*c0889d7aSstevep goto errexit; 1191*c0889d7aSstevep } 1192*c0889d7aSstevep 1193*c0889d7aSstevep /* 1194*c0889d7aSstevep * fstat the backend before copy for sanity check. 1195*c0889d7aSstevep */ 1196*c0889d7aSstevep if (fstat(srcfd, &s_buf) < 0) { 1197*c0889d7aSstevep configd_critical("Backend copy failed: fstat %s: %s\n", 1198*c0889d7aSstevep src, strerror(errno)); 1199*c0889d7aSstevep res = REP_PROTOCOL_FAIL_UNKNOWN; 1200*c0889d7aSstevep goto errexit; 1201*c0889d7aSstevep } 1202*c0889d7aSstevep 1203*c0889d7aSstevep if ((res = backend_do_copy(src, srcfd, dst, dstfd, &cpsz)) != 1204*c0889d7aSstevep REP_PROTOCOL_SUCCESS) 1205*c0889d7aSstevep goto errexit; 1206*c0889d7aSstevep 1207*c0889d7aSstevep if (cpsz != s_buf.st_size) { 1208*c0889d7aSstevep configd_critical("Backend copy failed: incomplete copy\n"); 1209*c0889d7aSstevep res = REP_PROTOCOL_FAIL_UNKNOWN; 1210*c0889d7aSstevep goto errexit; 1211*c0889d7aSstevep } 1212*c0889d7aSstevep 1213*c0889d7aSstevep /* 1214*c0889d7aSstevep * Rename tmppath to dst 1215*c0889d7aSstevep */ 1216*c0889d7aSstevep if (rename(tmppath, dst) < 0) { 1217*c0889d7aSstevep configd_critical( 1218*c0889d7aSstevep "Backend copy failed: rename %s to %s: %s\n", 1219*c0889d7aSstevep tmppath, dst, strerror(errno)); 1220*c0889d7aSstevep res = REP_PROTOCOL_FAIL_UNKNOWN; 1221*c0889d7aSstevep } 1222*c0889d7aSstevep 1223*c0889d7aSstevep errexit: 1224*c0889d7aSstevep if (res != REP_PROTOCOL_SUCCESS && unlink(tmppath) < 0) 1225*c0889d7aSstevep configd_critical( 1226*c0889d7aSstevep "Backend copy failed: remove %s: %s\n", 1227*c0889d7aSstevep tmppath, strerror(errno)); 1228*c0889d7aSstevep 1229*c0889d7aSstevep (void) close(srcfd); 1230*c0889d7aSstevep (void) close(dstfd); 1231*c0889d7aSstevep 1232*c0889d7aSstevep out: 1233*c0889d7aSstevep free(tmppath); 1234*c0889d7aSstevep if (sw_back) { 1235*c0889d7aSstevep if (unlink(src) < 0) 1236*c0889d7aSstevep configd_critical( 1237*c0889d7aSstevep "Backend copy failed: remove %s: %s\n", 1238*c0889d7aSstevep src, strerror(errno)); 1239*c0889d7aSstevep } 1240*c0889d7aSstevep 1241*c0889d7aSstevep return (res); 1242*c0889d7aSstevep } 1243*c0889d7aSstevep 1244*c0889d7aSstevep /* 1245*c0889d7aSstevep * Perform sanity check on the repository. 1246*c0889d7aSstevep * Return 0 if check succeeds or -1 if fails. 1247*c0889d7aSstevep */ 1248*c0889d7aSstevep static int 1249*c0889d7aSstevep backend_switch_check(struct sqlite *be_db, char **errp) 1250*c0889d7aSstevep { 1251*c0889d7aSstevep struct run_single_int_info info; 1252*c0889d7aSstevep uint32_t val = -1UL; 1253*c0889d7aSstevep int r; 1254*c0889d7aSstevep 1255*c0889d7aSstevep info.rs_out = &val; 1256*c0889d7aSstevep info.rs_result = REP_PROTOCOL_FAIL_NOT_FOUND; 1257*c0889d7aSstevep 1258*c0889d7aSstevep r = sqlite_exec(be_db, 1259*c0889d7aSstevep "SELECT schema_version FROM schema_version;", 1260*c0889d7aSstevep run_single_int_callback, &info, errp); 1261*c0889d7aSstevep 1262*c0889d7aSstevep if (r == SQLITE_OK && 1263*c0889d7aSstevep info.rs_result != REP_PROTOCOL_FAIL_NOT_FOUND && 1264*c0889d7aSstevep val == BACKEND_SCHEMA_VERSION) 1265*c0889d7aSstevep return (0); 1266*c0889d7aSstevep else 1267*c0889d7aSstevep return (-1); 1268*c0889d7aSstevep } 1269*c0889d7aSstevep 1270*c0889d7aSstevep /* 1271*c0889d7aSstevep * Backend switch entry point. It is called to perform the backend copy and 1272*c0889d7aSstevep * switch from src to dst. First, it blocks all other clients from accessing 1273*c0889d7aSstevep * the repository by calling backend_lock to lock the repository. Upon 1274*c0889d7aSstevep * successful lock, copying and switching of the repository are performed. 1275*c0889d7aSstevep * 1276*c0889d7aSstevep * Can return: 1277*c0889d7aSstevep * REP_PROTOCOL_SUCCESS successful switch 1278*c0889d7aSstevep * REP_PROTOCOL_FAIL_BACKEND_ACCESS backen access fails 1279*c0889d7aSstevep * REP_PROTOCOL_FAIL_BACKEND_READONLY backend is not writable 1280*c0889d7aSstevep * REP_PROTOCOL_FAIL_UNKNOWN file operation error 1281*c0889d7aSstevep * REP_PROTOCOL_FAIL_NO_RESOURCES out of memory 1282*c0889d7aSstevep */ 1283*c0889d7aSstevep rep_protocol_responseid_t 1284*c0889d7aSstevep backend_switch(int sw_back) 1285*c0889d7aSstevep { 1286*c0889d7aSstevep rep_protocol_responseid_t result; 1287*c0889d7aSstevep sqlite_backend_t *be; 1288*c0889d7aSstevep struct sqlite *new; 1289*c0889d7aSstevep char *errp; 1290*c0889d7aSstevep const char *dst; 1291*c0889d7aSstevep 1292*c0889d7aSstevep result = backend_lock(BACKEND_TYPE_NORMAL, 1, &be); 1293*c0889d7aSstevep if (result != REP_PROTOCOL_SUCCESS) 1294*c0889d7aSstevep return (result); 1295*c0889d7aSstevep 1296*c0889d7aSstevep if (sw_back) { 1297*c0889d7aSstevep dst = REPOSITORY_DB; 1298*c0889d7aSstevep } else { 1299*c0889d7aSstevep dst = FAST_REPOSITORY_DB; 1300*c0889d7aSstevep } 1301*c0889d7aSstevep 1302*c0889d7aSstevep /* 1303*c0889d7aSstevep * Do the actual copy and rename 1304*c0889d7aSstevep */ 1305*c0889d7aSstevep result = backend_switch_copy(be->be_path, dst, sw_back); 1306*c0889d7aSstevep if (result != REP_PROTOCOL_SUCCESS) { 1307*c0889d7aSstevep goto errout; 1308*c0889d7aSstevep } 1309*c0889d7aSstevep 1310*c0889d7aSstevep /* 1311*c0889d7aSstevep * Do the backend sanity check and switch 1312*c0889d7aSstevep */ 1313*c0889d7aSstevep new = sqlite_open(dst, 0600, &errp); 1314*c0889d7aSstevep if (new != NULL) { 1315*c0889d7aSstevep /* 1316*c0889d7aSstevep * Sanity check 1317*c0889d7aSstevep */ 1318*c0889d7aSstevep if (backend_switch_check(new, &errp) == 0) { 1319*c0889d7aSstevep free((char *)be->be_path); 1320*c0889d7aSstevep be->be_path = strdup(dst); 1321*c0889d7aSstevep if (be->be_path == NULL) { 1322*c0889d7aSstevep configd_critical( 1323*c0889d7aSstevep "Backend switch failed: strdup %s: %s\n", 1324*c0889d7aSstevep dst, strerror(errno)); 1325*c0889d7aSstevep result = REP_PROTOCOL_FAIL_NO_RESOURCES; 1326*c0889d7aSstevep sqlite_close(new); 1327*c0889d7aSstevep } else { 1328*c0889d7aSstevep sqlite_close(be->be_db); 1329*c0889d7aSstevep be->be_db = new; 1330*c0889d7aSstevep } 1331*c0889d7aSstevep } else { 1332*c0889d7aSstevep configd_critical( 1333*c0889d7aSstevep "Backend switch failed: integrity check %s: %s\n", 1334*c0889d7aSstevep dst, errp); 1335*c0889d7aSstevep result = REP_PROTOCOL_FAIL_BACKEND_ACCESS; 1336*c0889d7aSstevep } 1337*c0889d7aSstevep } else { 1338*c0889d7aSstevep configd_critical("Backend switch failed: sqlite_open %s: %s\n", 1339*c0889d7aSstevep dst, errp); 1340*c0889d7aSstevep result = REP_PROTOCOL_FAIL_BACKEND_ACCESS; 1341*c0889d7aSstevep } 1342*c0889d7aSstevep 1343*c0889d7aSstevep errout: 1344*c0889d7aSstevep backend_unlock(be); 1345*c0889d7aSstevep return (result); 1346*c0889d7aSstevep } 1347*c0889d7aSstevep 1348*c0889d7aSstevep /* 1349*c0889d7aSstevep * This routine is called to attempt the recovery of 1350*c0889d7aSstevep * the most recent valid repository if possible when configd 1351*c0889d7aSstevep * is restarted for some reasons or when system crashes 1352*c0889d7aSstevep * during the switch operation. The repository databases 1353*c0889d7aSstevep * referenced here are indicators of successful switch 1354*c0889d7aSstevep * operations. 1355*c0889d7aSstevep */ 1356*c0889d7aSstevep static void 1357*c0889d7aSstevep backend_switch_recovery(void) 1358*c0889d7aSstevep { 1359*c0889d7aSstevep const char *fast_db = FAST_REPOSITORY_DB; 1360*c0889d7aSstevep char *errp; 1361*c0889d7aSstevep struct stat s_buf; 1362*c0889d7aSstevep struct sqlite *be_db; 1363*c0889d7aSstevep 1364*c0889d7aSstevep 1365*c0889d7aSstevep /* 1366*c0889d7aSstevep * A good transient db containing most recent data can 1367*c0889d7aSstevep * exist if system or svc.configd crashes during the 1368*c0889d7aSstevep * switch operation. If that is the case, check its 1369*c0889d7aSstevep * integrity and use it. 1370*c0889d7aSstevep */ 1371*c0889d7aSstevep if (stat(fast_db, &s_buf) < 0) { 1372*c0889d7aSstevep return; 1373*c0889d7aSstevep } 1374*c0889d7aSstevep 1375*c0889d7aSstevep /* 1376*c0889d7aSstevep * Do sanity check on the db 1377*c0889d7aSstevep */ 1378*c0889d7aSstevep be_db = sqlite_open(fast_db, 0600, &errp); 1379*c0889d7aSstevep 1380*c0889d7aSstevep if (be_db != NULL) { 1381*c0889d7aSstevep if (backend_switch_check(be_db, &errp) == 0) 1382*c0889d7aSstevep (void) backend_switch_copy(fast_db, REPOSITORY_DB, 1); 1383*c0889d7aSstevep } 1384*c0889d7aSstevep 1385*c0889d7aSstevep (void) unlink(fast_db); 1386*c0889d7aSstevep } 1387*c0889d7aSstevep 13887c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13897c478bd9Sstevel@tonic-gate static int 13907c478bd9Sstevel@tonic-gate backend_integrity_callback(void *private, int narg, char **vals, char **cols) 13917c478bd9Sstevel@tonic-gate { 13927c478bd9Sstevel@tonic-gate char **out = private; 13937c478bd9Sstevel@tonic-gate char *old = *out; 13947c478bd9Sstevel@tonic-gate char *new; 13957c478bd9Sstevel@tonic-gate const char *info; 13967c478bd9Sstevel@tonic-gate size_t len; 13977c478bd9Sstevel@tonic-gate int x; 13987c478bd9Sstevel@tonic-gate 13997c478bd9Sstevel@tonic-gate for (x = 0; x < narg; x++) { 14007c478bd9Sstevel@tonic-gate if ((info = vals[x]) != NULL && 14017c478bd9Sstevel@tonic-gate strcmp(info, "ok") != 0) { 14027c478bd9Sstevel@tonic-gate len = (old == NULL)? 0 : strlen(old); 14037c478bd9Sstevel@tonic-gate len += strlen(info) + 2; /* '\n' + '\0' */ 14047c478bd9Sstevel@tonic-gate 14057c478bd9Sstevel@tonic-gate new = realloc(old, len); 14067c478bd9Sstevel@tonic-gate if (new == NULL) 14077c478bd9Sstevel@tonic-gate return (BACKEND_CALLBACK_ABORT); 14087c478bd9Sstevel@tonic-gate if (old == NULL) 14097c478bd9Sstevel@tonic-gate new[0] = 0; 14107c478bd9Sstevel@tonic-gate old = *out = new; 14117c478bd9Sstevel@tonic-gate (void) strlcat(new, info, len); 14127c478bd9Sstevel@tonic-gate (void) strlcat(new, "\n", len); 14137c478bd9Sstevel@tonic-gate } 14147c478bd9Sstevel@tonic-gate } 14157c478bd9Sstevel@tonic-gate return (BACKEND_CALLBACK_CONTINUE); 14167c478bd9Sstevel@tonic-gate } 14177c478bd9Sstevel@tonic-gate 14187c478bd9Sstevel@tonic-gate #define BACKEND_CREATE_LOCKED -2 14197c478bd9Sstevel@tonic-gate #define BACKEND_CREATE_FAIL -1 14207c478bd9Sstevel@tonic-gate #define BACKEND_CREATE_SUCCESS 0 14217c478bd9Sstevel@tonic-gate #define BACKEND_CREATE_READONLY 1 14227c478bd9Sstevel@tonic-gate #define BACKEND_CREATE_NEED_INIT 2 14237c478bd9Sstevel@tonic-gate static int 14247c478bd9Sstevel@tonic-gate backend_create(backend_type_t backend_id, const char *db_file, 14257c478bd9Sstevel@tonic-gate sqlite_backend_t **bep) 14267c478bd9Sstevel@tonic-gate { 14277c478bd9Sstevel@tonic-gate char *errp; 14287c478bd9Sstevel@tonic-gate char *integrity_results = NULL; 14297c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 14307c478bd9Sstevel@tonic-gate int r; 14317c478bd9Sstevel@tonic-gate uint32_t val = -1UL; 14327c478bd9Sstevel@tonic-gate struct run_single_int_info info; 14337c478bd9Sstevel@tonic-gate int fd; 14347c478bd9Sstevel@tonic-gate 14357c478bd9Sstevel@tonic-gate assert(backend_id >= 0 && backend_id < BACKEND_TYPE_TOTAL); 14367c478bd9Sstevel@tonic-gate 14377c478bd9Sstevel@tonic-gate be = &be_info[backend_id]; 14387c478bd9Sstevel@tonic-gate assert(be->be_db == NULL); 14397c478bd9Sstevel@tonic-gate 14407c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&be->be_lock, NULL); 14417c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&be->be_lock); 14427c478bd9Sstevel@tonic-gate 14437c478bd9Sstevel@tonic-gate be->be_type = backend_id; 14447c478bd9Sstevel@tonic-gate be->be_path = strdup(db_file); 14457c478bd9Sstevel@tonic-gate if (be->be_path == NULL) { 14467c478bd9Sstevel@tonic-gate perror("malloc"); 14477c478bd9Sstevel@tonic-gate goto fail; 14487c478bd9Sstevel@tonic-gate } 14497c478bd9Sstevel@tonic-gate 14507c478bd9Sstevel@tonic-gate be->be_db = sqlite_open(be->be_path, 0600, &errp); 14517c478bd9Sstevel@tonic-gate 14527c478bd9Sstevel@tonic-gate if (be->be_db == NULL) { 14537c478bd9Sstevel@tonic-gate if (strstr(errp, "out of memory") != NULL) { 14547c478bd9Sstevel@tonic-gate configd_critical("%s: %s\n", db_file, errp); 14557c478bd9Sstevel@tonic-gate free(errp); 14567c478bd9Sstevel@tonic-gate 14577c478bd9Sstevel@tonic-gate goto fail; 14587c478bd9Sstevel@tonic-gate } 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate /* report it as an integrity failure */ 14617c478bd9Sstevel@tonic-gate integrity_results = errp; 14627c478bd9Sstevel@tonic-gate errp = NULL; 14637c478bd9Sstevel@tonic-gate goto integrity_fail; 14647c478bd9Sstevel@tonic-gate } 14657c478bd9Sstevel@tonic-gate 14667c478bd9Sstevel@tonic-gate /* 14677c478bd9Sstevel@tonic-gate * check if we are inited and of the correct schema version 14687c478bd9Sstevel@tonic-gate * 14697c478bd9Sstevel@tonic-gate * Eventually, we'll support schema upgrade here. 14707c478bd9Sstevel@tonic-gate */ 14717c478bd9Sstevel@tonic-gate info.rs_out = &val; 14727c478bd9Sstevel@tonic-gate info.rs_result = REP_PROTOCOL_FAIL_NOT_FOUND; 14737c478bd9Sstevel@tonic-gate 14747c478bd9Sstevel@tonic-gate r = sqlite_exec(be->be_db, "SELECT schema_version FROM schema_version;", 14757c478bd9Sstevel@tonic-gate run_single_int_callback, &info, &errp); 14767c478bd9Sstevel@tonic-gate if (r == SQLITE_ERROR && 14777c478bd9Sstevel@tonic-gate strcmp("no such table: schema_version", errp) == 0) { 14787c478bd9Sstevel@tonic-gate free(errp); 14797c478bd9Sstevel@tonic-gate /* 14807c478bd9Sstevel@tonic-gate * Could be an empty repository, could be pre-schema_version 14817c478bd9Sstevel@tonic-gate * schema. Check for id_tbl, which has always been there. 14827c478bd9Sstevel@tonic-gate */ 14837c478bd9Sstevel@tonic-gate r = sqlite_exec(be->be_db, "SELECT count() FROM id_tbl;", 14847c478bd9Sstevel@tonic-gate NULL, NULL, &errp); 14857c478bd9Sstevel@tonic-gate if (r == SQLITE_ERROR && 14867c478bd9Sstevel@tonic-gate strcmp("no such table: id_tbl", errp) == 0) { 14877c478bd9Sstevel@tonic-gate free(errp); 14887c478bd9Sstevel@tonic-gate *bep = be; 14897c478bd9Sstevel@tonic-gate return (BACKEND_CREATE_NEED_INIT); 14907c478bd9Sstevel@tonic-gate } 14917c478bd9Sstevel@tonic-gate 14927c478bd9Sstevel@tonic-gate configd_critical("%s: schema version mismatch\n", db_file); 14937c478bd9Sstevel@tonic-gate goto fail; 14947c478bd9Sstevel@tonic-gate } 14957c478bd9Sstevel@tonic-gate if (r == SQLITE_BUSY || r == SQLITE_LOCKED) { 14967c478bd9Sstevel@tonic-gate free(errp); 14977c478bd9Sstevel@tonic-gate *bep = NULL; 14987c478bd9Sstevel@tonic-gate backend_destroy(be); 14997c478bd9Sstevel@tonic-gate return (BACKEND_CREATE_LOCKED); 15007c478bd9Sstevel@tonic-gate } 15017c478bd9Sstevel@tonic-gate if (r == SQLITE_OK) { 15027c478bd9Sstevel@tonic-gate if (info.rs_result == REP_PROTOCOL_FAIL_NOT_FOUND || 15037c478bd9Sstevel@tonic-gate val != BACKEND_SCHEMA_VERSION) { 15047c478bd9Sstevel@tonic-gate configd_critical("%s: schema version mismatch\n", 15057c478bd9Sstevel@tonic-gate db_file); 15067c478bd9Sstevel@tonic-gate goto fail; 15077c478bd9Sstevel@tonic-gate } 15087c478bd9Sstevel@tonic-gate } 15097c478bd9Sstevel@tonic-gate 15107c478bd9Sstevel@tonic-gate /* 15117c478bd9Sstevel@tonic-gate * pull in the whole database sequentially. 15127c478bd9Sstevel@tonic-gate */ 15137c478bd9Sstevel@tonic-gate if ((fd = open(db_file, O_RDONLY)) >= 0) { 15147c478bd9Sstevel@tonic-gate size_t sz = 64 * 1024; 15157c478bd9Sstevel@tonic-gate char *buffer = malloc(sz); 15167c478bd9Sstevel@tonic-gate if (buffer != NULL) { 15177c478bd9Sstevel@tonic-gate while (read(fd, buffer, sz) > 0) 15187c478bd9Sstevel@tonic-gate ; 15197c478bd9Sstevel@tonic-gate free(buffer); 15207c478bd9Sstevel@tonic-gate } 15217c478bd9Sstevel@tonic-gate (void) close(fd); 15227c478bd9Sstevel@tonic-gate } 15237c478bd9Sstevel@tonic-gate 15247c478bd9Sstevel@tonic-gate /* 15257c478bd9Sstevel@tonic-gate * run an integrity check 15267c478bd9Sstevel@tonic-gate */ 15277c478bd9Sstevel@tonic-gate r = sqlite_exec(be->be_db, "PRAGMA integrity_check;", 15287c478bd9Sstevel@tonic-gate backend_integrity_callback, &integrity_results, &errp); 15297c478bd9Sstevel@tonic-gate 15307c478bd9Sstevel@tonic-gate if (r == SQLITE_BUSY || r == SQLITE_LOCKED) { 15317c478bd9Sstevel@tonic-gate free(errp); 15327c478bd9Sstevel@tonic-gate *bep = NULL; 15337c478bd9Sstevel@tonic-gate backend_destroy(be); 15347c478bd9Sstevel@tonic-gate return (BACKEND_CREATE_LOCKED); 15357c478bd9Sstevel@tonic-gate } 15367c478bd9Sstevel@tonic-gate if (r == SQLITE_ABORT) { 15377c478bd9Sstevel@tonic-gate free(errp); 15387c478bd9Sstevel@tonic-gate errp = NULL; 15397c478bd9Sstevel@tonic-gate integrity_results = "out of memory running integrity check\n"; 15407c478bd9Sstevel@tonic-gate } else if (r != SQLITE_OK && integrity_results == NULL) { 15417c478bd9Sstevel@tonic-gate integrity_results = errp; 15427c478bd9Sstevel@tonic-gate errp = NULL; 15437c478bd9Sstevel@tonic-gate } 15447c478bd9Sstevel@tonic-gate 15457c478bd9Sstevel@tonic-gate integrity_fail: 15467c478bd9Sstevel@tonic-gate if (integrity_results != NULL) { 15477c478bd9Sstevel@tonic-gate const char *fname = "/etc/svc/volatile/db_errors"; 15487c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_CREAT|O_WRONLY|O_APPEND, 0600)) < 0) { 15497c478bd9Sstevel@tonic-gate fname = NULL; 15507c478bd9Sstevel@tonic-gate } else { 15517c478bd9Sstevel@tonic-gate if (backend_fd_write(fd, "\n\n") < 0 || 15527c478bd9Sstevel@tonic-gate backend_fd_write(fd, db_file) < 0 || 15537c478bd9Sstevel@tonic-gate backend_fd_write(fd, 15547c478bd9Sstevel@tonic-gate ": PRAGMA integrity_check; failed. Results:\n") < 15557c478bd9Sstevel@tonic-gate 0 || backend_fd_write(fd, integrity_results) < 0 || 15567c478bd9Sstevel@tonic-gate backend_fd_write(fd, "\n\n") < 0) { 15577c478bd9Sstevel@tonic-gate fname = NULL; 15587c478bd9Sstevel@tonic-gate } 15597c478bd9Sstevel@tonic-gate (void) close(fd); 15607c478bd9Sstevel@tonic-gate } 15617c478bd9Sstevel@tonic-gate 15627c478bd9Sstevel@tonic-gate if (!is_main_repository || 15637c478bd9Sstevel@tonic-gate backend_id == BACKEND_TYPE_NONPERSIST) { 15647c478bd9Sstevel@tonic-gate if (fname != NULL) 15657c478bd9Sstevel@tonic-gate configd_critical( 15667c478bd9Sstevel@tonic-gate "%s: integrity check failed. Details in " 15677c478bd9Sstevel@tonic-gate "%s\n", db_file, fname); 15687c478bd9Sstevel@tonic-gate else 15697c478bd9Sstevel@tonic-gate configd_critical( 1570db11989eSpjung "%s: integrity check failed.\n", 15717c478bd9Sstevel@tonic-gate db_file); 15727c478bd9Sstevel@tonic-gate } else { 15737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 15747c478bd9Sstevel@tonic-gate "\n" 15757c478bd9Sstevel@tonic-gate "svc.configd: smf(5) database integrity check of:\n" 15767c478bd9Sstevel@tonic-gate "\n" 15777c478bd9Sstevel@tonic-gate " %s\n" 15787c478bd9Sstevel@tonic-gate "\n" 15797c478bd9Sstevel@tonic-gate " failed. The database might be damaged or a media error might have\n" 15807c478bd9Sstevel@tonic-gate " prevented it from being verified. Additional information useful to\n" 15817c478bd9Sstevel@tonic-gate " your service provider%s%s\n" 15827c478bd9Sstevel@tonic-gate "\n" 15837c478bd9Sstevel@tonic-gate " The system will not be able to boot until you have restored a working\n" 15847c478bd9Sstevel@tonic-gate " database. svc.startd(1M) will provide a sulogin(1M) prompt for recovery\n" 15857c478bd9Sstevel@tonic-gate " purposes. The command:\n" 15867c478bd9Sstevel@tonic-gate "\n" 15877c478bd9Sstevel@tonic-gate " /lib/svc/bin/restore_repository\n" 15887c478bd9Sstevel@tonic-gate "\n" 15897c478bd9Sstevel@tonic-gate " can be run to restore a backup version of your repository. See\n" 15907c478bd9Sstevel@tonic-gate " http://sun.com/msg/SMF-8000-MY for more information.\n" 15917c478bd9Sstevel@tonic-gate "\n", 15927c478bd9Sstevel@tonic-gate db_file, 15937c478bd9Sstevel@tonic-gate (fname == NULL)? ":\n\n" : " is in:\n\n ", 15947c478bd9Sstevel@tonic-gate (fname == NULL)? integrity_results : fname); 15957c478bd9Sstevel@tonic-gate } 15967c478bd9Sstevel@tonic-gate free(errp); 15977c478bd9Sstevel@tonic-gate goto fail; 15987c478bd9Sstevel@tonic-gate } 15997c478bd9Sstevel@tonic-gate 16007c478bd9Sstevel@tonic-gate /* 16017c478bd9Sstevel@tonic-gate * check if we are writable 16027c478bd9Sstevel@tonic-gate */ 16038918dff3Sjwadams r = backend_is_readonly(be->be_db, be->be_path); 16047c478bd9Sstevel@tonic-gate 16057c478bd9Sstevel@tonic-gate if (r == SQLITE_BUSY || r == SQLITE_LOCKED) { 16067c478bd9Sstevel@tonic-gate free(errp); 16077c478bd9Sstevel@tonic-gate *bep = NULL; 16087c478bd9Sstevel@tonic-gate backend_destroy(be); 16097c478bd9Sstevel@tonic-gate return (BACKEND_CREATE_LOCKED); 16107c478bd9Sstevel@tonic-gate } 16117c478bd9Sstevel@tonic-gate if (r != SQLITE_OK && r != SQLITE_FULL) { 16127c478bd9Sstevel@tonic-gate free(errp); 16137c478bd9Sstevel@tonic-gate be->be_readonly = 1; 16147c478bd9Sstevel@tonic-gate *bep = be; 16157c478bd9Sstevel@tonic-gate return (BACKEND_CREATE_READONLY); 16167c478bd9Sstevel@tonic-gate } 16177c478bd9Sstevel@tonic-gate *bep = be; 16187c478bd9Sstevel@tonic-gate return (BACKEND_CREATE_SUCCESS); 16197c478bd9Sstevel@tonic-gate 16207c478bd9Sstevel@tonic-gate fail: 16217c478bd9Sstevel@tonic-gate *bep = NULL; 16227c478bd9Sstevel@tonic-gate backend_destroy(be); 16237c478bd9Sstevel@tonic-gate return (BACKEND_CREATE_FAIL); 16247c478bd9Sstevel@tonic-gate } 16257c478bd9Sstevel@tonic-gate 16267c478bd9Sstevel@tonic-gate /* 16277c478bd9Sstevel@tonic-gate * (arg & -arg) is, through the magic of twos-complement arithmetic, the 16287c478bd9Sstevel@tonic-gate * lowest set bit in arg. 16297c478bd9Sstevel@tonic-gate */ 16307c478bd9Sstevel@tonic-gate static size_t 16317c478bd9Sstevel@tonic-gate round_up_to_p2(size_t arg) 16327c478bd9Sstevel@tonic-gate { 16337c478bd9Sstevel@tonic-gate /* 16347c478bd9Sstevel@tonic-gate * Don't allow a zero result. 16357c478bd9Sstevel@tonic-gate */ 16367c478bd9Sstevel@tonic-gate assert(arg > 0 && ((ssize_t)arg > 0)); 16377c478bd9Sstevel@tonic-gate 16387c478bd9Sstevel@tonic-gate while ((arg & (arg - 1)) != 0) 16397c478bd9Sstevel@tonic-gate arg += (arg & -arg); 16407c478bd9Sstevel@tonic-gate 16417c478bd9Sstevel@tonic-gate return (arg); 16427c478bd9Sstevel@tonic-gate } 16437c478bd9Sstevel@tonic-gate 16447c478bd9Sstevel@tonic-gate /* 16457c478bd9Sstevel@tonic-gate * Returns 16467c478bd9Sstevel@tonic-gate * _NO_RESOURCES - out of memory 16477c478bd9Sstevel@tonic-gate * _BACKEND_ACCESS - backend type t (other than _NORMAL) doesn't exist 16487c478bd9Sstevel@tonic-gate * _DONE - callback aborted query 16497c478bd9Sstevel@tonic-gate * _SUCCESS 16507c478bd9Sstevel@tonic-gate */ 16517c478bd9Sstevel@tonic-gate int 16527c478bd9Sstevel@tonic-gate backend_run(backend_type_t t, backend_query_t *q, 16537c478bd9Sstevel@tonic-gate backend_run_callback_f *cb, void *data) 16547c478bd9Sstevel@tonic-gate { 16557c478bd9Sstevel@tonic-gate char *errmsg = NULL; 16567c478bd9Sstevel@tonic-gate int ret; 16577c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 16587c478bd9Sstevel@tonic-gate hrtime_t ts, vts; 16597c478bd9Sstevel@tonic-gate 16607c478bd9Sstevel@tonic-gate if (q == NULL || q->bq_buf == NULL) 16617c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_NO_RESOURCES); 16627c478bd9Sstevel@tonic-gate 16637c478bd9Sstevel@tonic-gate if ((ret = backend_lock(t, 0, &be)) != REP_PROTOCOL_SUCCESS) 16647c478bd9Sstevel@tonic-gate return (ret); 16657c478bd9Sstevel@tonic-gate 16667c478bd9Sstevel@tonic-gate ts = gethrtime(); 16677c478bd9Sstevel@tonic-gate vts = gethrvtime(); 16687c478bd9Sstevel@tonic-gate ret = sqlite_exec(be->be_db, q->bq_buf, cb, data, &errmsg); 16697c478bd9Sstevel@tonic-gate UPDATE_TOTALS(be, bt_exec, ts, vts); 16707c478bd9Sstevel@tonic-gate ret = backend_error(be, ret, errmsg); 16717c478bd9Sstevel@tonic-gate backend_unlock(be); 16727c478bd9Sstevel@tonic-gate 16737c478bd9Sstevel@tonic-gate return (ret); 16747c478bd9Sstevel@tonic-gate } 16757c478bd9Sstevel@tonic-gate 16767c478bd9Sstevel@tonic-gate /* 16777c478bd9Sstevel@tonic-gate * Starts a "read-only" transaction -- i.e., locks out writers as long 16787c478bd9Sstevel@tonic-gate * as it is active. 16797c478bd9Sstevel@tonic-gate * 16807c478bd9Sstevel@tonic-gate * Fails with 16817c478bd9Sstevel@tonic-gate * _NO_RESOURCES - out of memory 16827c478bd9Sstevel@tonic-gate * 16837c478bd9Sstevel@tonic-gate * If t is not _NORMAL, can also fail with 16847c478bd9Sstevel@tonic-gate * _BACKEND_ACCESS - backend does not exist 16857c478bd9Sstevel@tonic-gate * 16867c478bd9Sstevel@tonic-gate * If writable is true, can also fail with 16877c478bd9Sstevel@tonic-gate * _BACKEND_READONLY 16887c478bd9Sstevel@tonic-gate */ 16897c478bd9Sstevel@tonic-gate static int 16907c478bd9Sstevel@tonic-gate backend_tx_begin_common(backend_type_t t, backend_tx_t **txp, int writable) 16917c478bd9Sstevel@tonic-gate { 16927c478bd9Sstevel@tonic-gate backend_tx_t *ret; 16937c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 16947c478bd9Sstevel@tonic-gate int r; 16957c478bd9Sstevel@tonic-gate 16967c478bd9Sstevel@tonic-gate *txp = NULL; 16977c478bd9Sstevel@tonic-gate 16987c478bd9Sstevel@tonic-gate ret = uu_zalloc(sizeof (*ret)); 16997c478bd9Sstevel@tonic-gate if (ret == NULL) 17007c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_NO_RESOURCES); 17017c478bd9Sstevel@tonic-gate 17027c478bd9Sstevel@tonic-gate if ((r = backend_lock(t, writable, &be)) != REP_PROTOCOL_SUCCESS) { 17037c478bd9Sstevel@tonic-gate uu_free(ret); 17047c478bd9Sstevel@tonic-gate return (r); 17057c478bd9Sstevel@tonic-gate } 17067c478bd9Sstevel@tonic-gate 17077c478bd9Sstevel@tonic-gate ret->bt_be = be; 17087c478bd9Sstevel@tonic-gate ret->bt_readonly = !writable; 17097c478bd9Sstevel@tonic-gate ret->bt_type = t; 17107c478bd9Sstevel@tonic-gate ret->bt_full = 0; 17117c478bd9Sstevel@tonic-gate 17127c478bd9Sstevel@tonic-gate *txp = ret; 17137c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 17147c478bd9Sstevel@tonic-gate } 17157c478bd9Sstevel@tonic-gate 17167c478bd9Sstevel@tonic-gate int 17177c478bd9Sstevel@tonic-gate backend_tx_begin_ro(backend_type_t t, backend_tx_t **txp) 17187c478bd9Sstevel@tonic-gate { 17197c478bd9Sstevel@tonic-gate return (backend_tx_begin_common(t, txp, 0)); 17207c478bd9Sstevel@tonic-gate } 17217c478bd9Sstevel@tonic-gate 17227c478bd9Sstevel@tonic-gate static void 17237c478bd9Sstevel@tonic-gate backend_tx_end(backend_tx_t *tx) 17247c478bd9Sstevel@tonic-gate { 17257c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 17267c478bd9Sstevel@tonic-gate 17277c478bd9Sstevel@tonic-gate be = tx->bt_be; 17287c478bd9Sstevel@tonic-gate 17297c478bd9Sstevel@tonic-gate if (tx->bt_full) { 17307c478bd9Sstevel@tonic-gate struct sqlite *new; 17317c478bd9Sstevel@tonic-gate 17327c478bd9Sstevel@tonic-gate /* 17337c478bd9Sstevel@tonic-gate * sqlite tends to be sticky with SQLITE_FULL, so we try 17347c478bd9Sstevel@tonic-gate * to get a fresh database handle if we got a FULL warning 17357c478bd9Sstevel@tonic-gate * along the way. If that fails, no harm done. 17367c478bd9Sstevel@tonic-gate */ 17377c478bd9Sstevel@tonic-gate new = sqlite_open(be->be_path, 0600, NULL); 17387c478bd9Sstevel@tonic-gate if (new != NULL) { 17397c478bd9Sstevel@tonic-gate sqlite_close(be->be_db); 17407c478bd9Sstevel@tonic-gate be->be_db = new; 17417c478bd9Sstevel@tonic-gate } 17427c478bd9Sstevel@tonic-gate } 17437c478bd9Sstevel@tonic-gate backend_unlock(be); 17447c478bd9Sstevel@tonic-gate tx->bt_be = NULL; 17457c478bd9Sstevel@tonic-gate uu_free(tx); 17467c478bd9Sstevel@tonic-gate } 17477c478bd9Sstevel@tonic-gate 17487c478bd9Sstevel@tonic-gate void 17497c478bd9Sstevel@tonic-gate backend_tx_end_ro(backend_tx_t *tx) 17507c478bd9Sstevel@tonic-gate { 17517c478bd9Sstevel@tonic-gate assert(tx->bt_readonly); 17527c478bd9Sstevel@tonic-gate backend_tx_end(tx); 17537c478bd9Sstevel@tonic-gate } 17547c478bd9Sstevel@tonic-gate 17557c478bd9Sstevel@tonic-gate /* 17567c478bd9Sstevel@tonic-gate * Fails with 17577c478bd9Sstevel@tonic-gate * _NO_RESOURCES - out of memory 17587c478bd9Sstevel@tonic-gate * _BACKEND_ACCESS 17597c478bd9Sstevel@tonic-gate * _BACKEND_READONLY 17607c478bd9Sstevel@tonic-gate */ 17617c478bd9Sstevel@tonic-gate int 17627c478bd9Sstevel@tonic-gate backend_tx_begin(backend_type_t t, backend_tx_t **txp) 17637c478bd9Sstevel@tonic-gate { 17647c478bd9Sstevel@tonic-gate int r; 17657c478bd9Sstevel@tonic-gate char *errmsg; 17667c478bd9Sstevel@tonic-gate hrtime_t ts, vts; 17677c478bd9Sstevel@tonic-gate 17687c478bd9Sstevel@tonic-gate r = backend_tx_begin_common(t, txp, 1); 17697c478bd9Sstevel@tonic-gate if (r != REP_PROTOCOL_SUCCESS) 17707c478bd9Sstevel@tonic-gate return (r); 17717c478bd9Sstevel@tonic-gate 17727c478bd9Sstevel@tonic-gate ts = gethrtime(); 17737c478bd9Sstevel@tonic-gate vts = gethrvtime(); 17747c478bd9Sstevel@tonic-gate r = sqlite_exec((*txp)->bt_be->be_db, "BEGIN TRANSACTION", NULL, NULL, 17757c478bd9Sstevel@tonic-gate &errmsg); 17767c478bd9Sstevel@tonic-gate UPDATE_TOTALS((*txp)->bt_be, bt_exec, ts, vts); 17777c478bd9Sstevel@tonic-gate if (r == SQLITE_FULL) 17787c478bd9Sstevel@tonic-gate (*txp)->bt_full = 1; 17797c478bd9Sstevel@tonic-gate r = backend_error((*txp)->bt_be, r, errmsg); 17807c478bd9Sstevel@tonic-gate 17817c478bd9Sstevel@tonic-gate if (r != REP_PROTOCOL_SUCCESS) { 17827c478bd9Sstevel@tonic-gate assert(r != REP_PROTOCOL_DONE); 17837c478bd9Sstevel@tonic-gate (void) sqlite_exec((*txp)->bt_be->be_db, 17847c478bd9Sstevel@tonic-gate "ROLLBACK TRANSACTION", NULL, NULL, NULL); 17857c478bd9Sstevel@tonic-gate backend_tx_end(*txp); 17867c478bd9Sstevel@tonic-gate *txp = NULL; 17877c478bd9Sstevel@tonic-gate return (r); 17887c478bd9Sstevel@tonic-gate } 17897c478bd9Sstevel@tonic-gate 17907c478bd9Sstevel@tonic-gate (*txp)->bt_readonly = 0; 17917c478bd9Sstevel@tonic-gate 17927c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 17937c478bd9Sstevel@tonic-gate } 17947c478bd9Sstevel@tonic-gate 17957c478bd9Sstevel@tonic-gate void 17967c478bd9Sstevel@tonic-gate backend_tx_rollback(backend_tx_t *tx) 17977c478bd9Sstevel@tonic-gate { 17987c478bd9Sstevel@tonic-gate int r; 17997c478bd9Sstevel@tonic-gate char *errmsg; 18007c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 18017c478bd9Sstevel@tonic-gate hrtime_t ts, vts; 18027c478bd9Sstevel@tonic-gate 18037c478bd9Sstevel@tonic-gate assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly); 18047c478bd9Sstevel@tonic-gate be = tx->bt_be; 18057c478bd9Sstevel@tonic-gate 18067c478bd9Sstevel@tonic-gate ts = gethrtime(); 18077c478bd9Sstevel@tonic-gate vts = gethrvtime(); 18087c478bd9Sstevel@tonic-gate r = sqlite_exec(be->be_db, "ROLLBACK TRANSACTION", NULL, NULL, 18097c478bd9Sstevel@tonic-gate &errmsg); 18107c478bd9Sstevel@tonic-gate UPDATE_TOTALS(be, bt_exec, ts, vts); 18117c478bd9Sstevel@tonic-gate if (r == SQLITE_FULL) 18127c478bd9Sstevel@tonic-gate tx->bt_full = 1; 18137c478bd9Sstevel@tonic-gate (void) backend_error(be, r, errmsg); 18147c478bd9Sstevel@tonic-gate 18157c478bd9Sstevel@tonic-gate backend_tx_end(tx); 18167c478bd9Sstevel@tonic-gate } 18177c478bd9Sstevel@tonic-gate 18187c478bd9Sstevel@tonic-gate /* 18197c478bd9Sstevel@tonic-gate * Fails with 18207c478bd9Sstevel@tonic-gate * _NO_RESOURCES - out of memory 18217c478bd9Sstevel@tonic-gate */ 18227c478bd9Sstevel@tonic-gate int 18237c478bd9Sstevel@tonic-gate backend_tx_commit(backend_tx_t *tx) 18247c478bd9Sstevel@tonic-gate { 18257c478bd9Sstevel@tonic-gate int r, r2; 18267c478bd9Sstevel@tonic-gate char *errmsg; 18277c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 18287c478bd9Sstevel@tonic-gate hrtime_t ts, vts; 18297c478bd9Sstevel@tonic-gate 18307c478bd9Sstevel@tonic-gate assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly); 18317c478bd9Sstevel@tonic-gate be = tx->bt_be; 18327c478bd9Sstevel@tonic-gate ts = gethrtime(); 18337c478bd9Sstevel@tonic-gate vts = gethrvtime(); 18347c478bd9Sstevel@tonic-gate r = sqlite_exec(be->be_db, "COMMIT TRANSACTION", NULL, NULL, 18357c478bd9Sstevel@tonic-gate &errmsg); 18367c478bd9Sstevel@tonic-gate UPDATE_TOTALS(be, bt_exec, ts, vts); 18377c478bd9Sstevel@tonic-gate if (r == SQLITE_FULL) 18387c478bd9Sstevel@tonic-gate tx->bt_full = 1; 18397c478bd9Sstevel@tonic-gate 18407c478bd9Sstevel@tonic-gate r = backend_error(be, r, errmsg); 18417c478bd9Sstevel@tonic-gate assert(r != REP_PROTOCOL_DONE); 18427c478bd9Sstevel@tonic-gate 18437c478bd9Sstevel@tonic-gate if (r != REP_PROTOCOL_SUCCESS) { 18447c478bd9Sstevel@tonic-gate r2 = sqlite_exec(be->be_db, "ROLLBACK TRANSACTION", NULL, NULL, 18457c478bd9Sstevel@tonic-gate &errmsg); 18467c478bd9Sstevel@tonic-gate r2 = backend_error(be, r2, errmsg); 18477c478bd9Sstevel@tonic-gate if (r2 != REP_PROTOCOL_SUCCESS) 18487c478bd9Sstevel@tonic-gate backend_panic("cannot rollback failed commit"); 18497c478bd9Sstevel@tonic-gate 18507c478bd9Sstevel@tonic-gate backend_tx_end(tx); 18517c478bd9Sstevel@tonic-gate return (r); 18527c478bd9Sstevel@tonic-gate } 18537c478bd9Sstevel@tonic-gate backend_tx_end(tx); 18547c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS); 18557c478bd9Sstevel@tonic-gate } 18567c478bd9Sstevel@tonic-gate 18577c478bd9Sstevel@tonic-gate static const char * 18587c478bd9Sstevel@tonic-gate id_space_to_name(enum id_space id) 18597c478bd9Sstevel@tonic-gate { 18607c478bd9Sstevel@tonic-gate switch (id) { 18617c478bd9Sstevel@tonic-gate case BACKEND_ID_SERVICE_INSTANCE: 18627c478bd9Sstevel@tonic-gate return ("SI"); 18637c478bd9Sstevel@tonic-gate case BACKEND_ID_PROPERTYGRP: 18647c478bd9Sstevel@tonic-gate return ("PG"); 18657c478bd9Sstevel@tonic-gate case BACKEND_ID_GENERATION: 18667c478bd9Sstevel@tonic-gate return ("GEN"); 18677c478bd9Sstevel@tonic-gate case BACKEND_ID_PROPERTY: 18687c478bd9Sstevel@tonic-gate return ("PROP"); 18697c478bd9Sstevel@tonic-gate case BACKEND_ID_VALUE: 18707c478bd9Sstevel@tonic-gate return ("VAL"); 18717c478bd9Sstevel@tonic-gate case BACKEND_ID_SNAPNAME: 18727c478bd9Sstevel@tonic-gate return ("SNAME"); 18737c478bd9Sstevel@tonic-gate case BACKEND_ID_SNAPSHOT: 18747c478bd9Sstevel@tonic-gate return ("SHOT"); 18757c478bd9Sstevel@tonic-gate case BACKEND_ID_SNAPLEVEL: 18767c478bd9Sstevel@tonic-gate return ("SLVL"); 18777c478bd9Sstevel@tonic-gate default: 18787c478bd9Sstevel@tonic-gate abort(); 18797c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 18807c478bd9Sstevel@tonic-gate } 18817c478bd9Sstevel@tonic-gate } 18827c478bd9Sstevel@tonic-gate 18837c478bd9Sstevel@tonic-gate /* 18847c478bd9Sstevel@tonic-gate * Returns a new id or 0 if the id argument is invalid or the query fails. 18857c478bd9Sstevel@tonic-gate */ 18867c478bd9Sstevel@tonic-gate uint32_t 18877c478bd9Sstevel@tonic-gate backend_new_id(backend_tx_t *tx, enum id_space id) 18887c478bd9Sstevel@tonic-gate { 18897c478bd9Sstevel@tonic-gate struct run_single_int_info info; 18907c478bd9Sstevel@tonic-gate uint32_t new_id = 0; 18917c478bd9Sstevel@tonic-gate const char *name = id_space_to_name(id); 18927c478bd9Sstevel@tonic-gate char *errmsg; 18937c478bd9Sstevel@tonic-gate int ret; 18947c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 18957c478bd9Sstevel@tonic-gate hrtime_t ts, vts; 18967c478bd9Sstevel@tonic-gate 18977c478bd9Sstevel@tonic-gate assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly); 18987c478bd9Sstevel@tonic-gate be = tx->bt_be; 18997c478bd9Sstevel@tonic-gate 19007c478bd9Sstevel@tonic-gate info.rs_out = &new_id; 19017c478bd9Sstevel@tonic-gate info.rs_result = REP_PROTOCOL_FAIL_NOT_FOUND; 19027c478bd9Sstevel@tonic-gate 19037c478bd9Sstevel@tonic-gate ts = gethrtime(); 19047c478bd9Sstevel@tonic-gate vts = gethrvtime(); 19057c478bd9Sstevel@tonic-gate ret = sqlite_exec_printf(be->be_db, 19067c478bd9Sstevel@tonic-gate "SELECT id_next FROM id_tbl WHERE (id_name = '%q');" 19077c478bd9Sstevel@tonic-gate "UPDATE id_tbl SET id_next = id_next + 1 WHERE (id_name = '%q');", 19087c478bd9Sstevel@tonic-gate run_single_int_callback, &info, &errmsg, name, name); 19097c478bd9Sstevel@tonic-gate UPDATE_TOTALS(be, bt_exec, ts, vts); 19107c478bd9Sstevel@tonic-gate if (ret == SQLITE_FULL) 19117c478bd9Sstevel@tonic-gate tx->bt_full = 1; 19127c478bd9Sstevel@tonic-gate 19137c478bd9Sstevel@tonic-gate ret = backend_error(be, ret, errmsg); 19147c478bd9Sstevel@tonic-gate 19157c478bd9Sstevel@tonic-gate if (ret != REP_PROTOCOL_SUCCESS) { 19167c478bd9Sstevel@tonic-gate return (0); 19177c478bd9Sstevel@tonic-gate } 19187c478bd9Sstevel@tonic-gate 19197c478bd9Sstevel@tonic-gate return (new_id); 19207c478bd9Sstevel@tonic-gate } 19217c478bd9Sstevel@tonic-gate 19227c478bd9Sstevel@tonic-gate /* 19237c478bd9Sstevel@tonic-gate * Returns 19247c478bd9Sstevel@tonic-gate * _NO_RESOURCES - out of memory 19257c478bd9Sstevel@tonic-gate * _DONE - callback aborted query 19267c478bd9Sstevel@tonic-gate * _SUCCESS 19277c478bd9Sstevel@tonic-gate */ 19287c478bd9Sstevel@tonic-gate int 19297c478bd9Sstevel@tonic-gate backend_tx_run(backend_tx_t *tx, backend_query_t *q, 19307c478bd9Sstevel@tonic-gate backend_run_callback_f *cb, void *data) 19317c478bd9Sstevel@tonic-gate { 19327c478bd9Sstevel@tonic-gate char *errmsg = NULL; 19337c478bd9Sstevel@tonic-gate int ret; 19347c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 19357c478bd9Sstevel@tonic-gate hrtime_t ts, vts; 19367c478bd9Sstevel@tonic-gate 19377c478bd9Sstevel@tonic-gate assert(tx != NULL && tx->bt_be != NULL); 19387c478bd9Sstevel@tonic-gate be = tx->bt_be; 19397c478bd9Sstevel@tonic-gate 19407c478bd9Sstevel@tonic-gate if (q == NULL || q->bq_buf == NULL) 19417c478bd9Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_NO_RESOURCES); 19427c478bd9Sstevel@tonic-gate 19437c478bd9Sstevel@tonic-gate ts = gethrtime(); 19447c478bd9Sstevel@tonic-gate vts = gethrvtime(); 19457c478bd9Sstevel@tonic-gate ret = sqlite_exec(be->be_db, q->bq_buf, cb, data, &errmsg); 19467c478bd9Sstevel@tonic-gate UPDATE_TOTALS(be, bt_exec, ts, vts); 19477c478bd9Sstevel@tonic-gate if (ret == SQLITE_FULL) 19487c478bd9Sstevel@tonic-gate tx->bt_full = 1; 19497c478bd9Sstevel@tonic-gate ret = backend_error(be, ret, errmsg); 19507c478bd9Sstevel@tonic-gate 19517c478bd9Sstevel@tonic-gate return (ret); 19527c478bd9Sstevel@tonic-gate } 19537c478bd9Sstevel@tonic-gate 19547c478bd9Sstevel@tonic-gate /* 19557c478bd9Sstevel@tonic-gate * Returns 19567c478bd9Sstevel@tonic-gate * _NO_RESOURCES - out of memory 19577c478bd9Sstevel@tonic-gate * _NOT_FOUND - the query returned no results 19587c478bd9Sstevel@tonic-gate * _SUCCESS - the query returned a single integer 19597c478bd9Sstevel@tonic-gate */ 19607c478bd9Sstevel@tonic-gate int 19617c478bd9Sstevel@tonic-gate backend_tx_run_single_int(backend_tx_t *tx, backend_query_t *q, uint32_t *buf) 19627c478bd9Sstevel@tonic-gate { 19637c478bd9Sstevel@tonic-gate struct run_single_int_info info; 19647c478bd9Sstevel@tonic-gate int ret; 19657c478bd9Sstevel@tonic-gate 19667c478bd9Sstevel@tonic-gate info.rs_out = buf; 19677c478bd9Sstevel@tonic-gate info.rs_result = REP_PROTOCOL_FAIL_NOT_FOUND; 19687c478bd9Sstevel@tonic-gate 19697c478bd9Sstevel@tonic-gate ret = backend_tx_run(tx, q, run_single_int_callback, &info); 19707c478bd9Sstevel@tonic-gate assert(ret != REP_PROTOCOL_DONE); 19717c478bd9Sstevel@tonic-gate 19727c478bd9Sstevel@tonic-gate if (ret != REP_PROTOCOL_SUCCESS) 19737c478bd9Sstevel@tonic-gate return (ret); 19747c478bd9Sstevel@tonic-gate 19757c478bd9Sstevel@tonic-gate return (info.rs_result); 19767c478bd9Sstevel@tonic-gate } 19777c478bd9Sstevel@tonic-gate 19787c478bd9Sstevel@tonic-gate /* 19797c478bd9Sstevel@tonic-gate * Fails with 19807c478bd9Sstevel@tonic-gate * _NO_RESOURCES - out of memory 19817c478bd9Sstevel@tonic-gate */ 19827c478bd9Sstevel@tonic-gate int 19837c478bd9Sstevel@tonic-gate backend_tx_run_update(backend_tx_t *tx, const char *format, ...) 19847c478bd9Sstevel@tonic-gate { 19857c478bd9Sstevel@tonic-gate va_list a; 19867c478bd9Sstevel@tonic-gate char *errmsg; 19877c478bd9Sstevel@tonic-gate int ret; 19887c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 19897c478bd9Sstevel@tonic-gate hrtime_t ts, vts; 19907c478bd9Sstevel@tonic-gate 19917c478bd9Sstevel@tonic-gate assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly); 19927c478bd9Sstevel@tonic-gate be = tx->bt_be; 19937c478bd9Sstevel@tonic-gate 19947c478bd9Sstevel@tonic-gate va_start(a, format); 19957c478bd9Sstevel@tonic-gate ts = gethrtime(); 19967c478bd9Sstevel@tonic-gate vts = gethrvtime(); 19977c478bd9Sstevel@tonic-gate ret = sqlite_exec_vprintf(be->be_db, format, NULL, NULL, &errmsg, a); 19987c478bd9Sstevel@tonic-gate UPDATE_TOTALS(be, bt_exec, ts, vts); 19997c478bd9Sstevel@tonic-gate if (ret == SQLITE_FULL) 20007c478bd9Sstevel@tonic-gate tx->bt_full = 1; 20017c478bd9Sstevel@tonic-gate va_end(a); 20027c478bd9Sstevel@tonic-gate ret = backend_error(be, ret, errmsg); 20037c478bd9Sstevel@tonic-gate assert(ret != REP_PROTOCOL_DONE); 20047c478bd9Sstevel@tonic-gate 20057c478bd9Sstevel@tonic-gate return (ret); 20067c478bd9Sstevel@tonic-gate } 20077c478bd9Sstevel@tonic-gate 20087c478bd9Sstevel@tonic-gate /* 20097c478bd9Sstevel@tonic-gate * returns REP_PROTOCOL_FAIL_NOT_FOUND if no changes occured 20107c478bd9Sstevel@tonic-gate */ 20117c478bd9Sstevel@tonic-gate int 20127c478bd9Sstevel@tonic-gate backend_tx_run_update_changed(backend_tx_t *tx, const char *format, ...) 20137c478bd9Sstevel@tonic-gate { 20147c478bd9Sstevel@tonic-gate va_list a; 20157c478bd9Sstevel@tonic-gate char *errmsg; 20167c478bd9Sstevel@tonic-gate int ret; 20177c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 20187c478bd9Sstevel@tonic-gate hrtime_t ts, vts; 20197c478bd9Sstevel@tonic-gate 20207c478bd9Sstevel@tonic-gate assert(tx != NULL && tx->bt_be != NULL && !tx->bt_readonly); 20217c478bd9Sstevel@tonic-gate be = tx->bt_be; 20227c478bd9Sstevel@tonic-gate 20237c478bd9Sstevel@tonic-gate va_start(a, format); 20247c478bd9Sstevel@tonic-gate ts = gethrtime(); 20257c478bd9Sstevel@tonic-gate vts = gethrvtime(); 20267c478bd9Sstevel@tonic-gate ret = sqlite_exec_vprintf(be->be_db, format, NULL, NULL, &errmsg, a); 20277c478bd9Sstevel@tonic-gate UPDATE_TOTALS(be, bt_exec, ts, vts); 20287c478bd9Sstevel@tonic-gate if (ret == SQLITE_FULL) 20297c478bd9Sstevel@tonic-gate tx->bt_full = 1; 20307c478bd9Sstevel@tonic-gate va_end(a); 20317c478bd9Sstevel@tonic-gate 20327c478bd9Sstevel@tonic-gate ret = backend_error(be, ret, errmsg); 20337c478bd9Sstevel@tonic-gate 20347c478bd9Sstevel@tonic-gate return (ret); 20357c478bd9Sstevel@tonic-gate } 20367c478bd9Sstevel@tonic-gate 20377c478bd9Sstevel@tonic-gate #define BACKEND_ADD_SCHEMA(be, file, tbls, idxs) \ 20387c478bd9Sstevel@tonic-gate (backend_add_schema((be), (file), \ 20397c478bd9Sstevel@tonic-gate (tbls), sizeof (tbls) / sizeof (*(tbls)), \ 20407c478bd9Sstevel@tonic-gate (idxs), sizeof (idxs) / sizeof (*(idxs)))) 20417c478bd9Sstevel@tonic-gate 20427c478bd9Sstevel@tonic-gate static int 20437c478bd9Sstevel@tonic-gate backend_add_schema(sqlite_backend_t *be, const char *file, 20447c478bd9Sstevel@tonic-gate struct backend_tbl_info *tbls, int tbl_count, 20457c478bd9Sstevel@tonic-gate struct backend_idx_info *idxs, int idx_count) 20467c478bd9Sstevel@tonic-gate { 20477c478bd9Sstevel@tonic-gate int i; 20487c478bd9Sstevel@tonic-gate char *errmsg; 20497c478bd9Sstevel@tonic-gate int ret; 20507c478bd9Sstevel@tonic-gate 20517c478bd9Sstevel@tonic-gate /* 20527c478bd9Sstevel@tonic-gate * Create the tables. 20537c478bd9Sstevel@tonic-gate */ 20547c478bd9Sstevel@tonic-gate for (i = 0; i < tbl_count; i++) { 20557c478bd9Sstevel@tonic-gate if (tbls[i].bti_name == NULL) { 20567c478bd9Sstevel@tonic-gate assert(i + 1 == tbl_count); 20577c478bd9Sstevel@tonic-gate break; 20587c478bd9Sstevel@tonic-gate } 20597c478bd9Sstevel@tonic-gate ret = sqlite_exec_printf(be->be_db, 20607c478bd9Sstevel@tonic-gate "CREATE TABLE %s (%s);\n", 20617c478bd9Sstevel@tonic-gate NULL, NULL, &errmsg, tbls[i].bti_name, tbls[i].bti_cols); 20627c478bd9Sstevel@tonic-gate 20637c478bd9Sstevel@tonic-gate if (ret != SQLITE_OK) { 20647c478bd9Sstevel@tonic-gate configd_critical( 20657c478bd9Sstevel@tonic-gate "%s: %s table creation fails: %s\n", file, 20667c478bd9Sstevel@tonic-gate tbls[i].bti_name, errmsg); 20677c478bd9Sstevel@tonic-gate free(errmsg); 20687c478bd9Sstevel@tonic-gate return (-1); 20697c478bd9Sstevel@tonic-gate } 20707c478bd9Sstevel@tonic-gate } 20717c478bd9Sstevel@tonic-gate 20727c478bd9Sstevel@tonic-gate /* 20737c478bd9Sstevel@tonic-gate * Make indices on key tables and columns. 20747c478bd9Sstevel@tonic-gate */ 20757c478bd9Sstevel@tonic-gate for (i = 0; i < idx_count; i++) { 20767c478bd9Sstevel@tonic-gate if (idxs[i].bxi_tbl == NULL) { 20777c478bd9Sstevel@tonic-gate assert(i + 1 == idx_count); 20787c478bd9Sstevel@tonic-gate break; 20797c478bd9Sstevel@tonic-gate } 20807c478bd9Sstevel@tonic-gate 20817c478bd9Sstevel@tonic-gate ret = sqlite_exec_printf(be->be_db, 20827c478bd9Sstevel@tonic-gate "CREATE INDEX %s_%s ON %s (%s);\n", 20837c478bd9Sstevel@tonic-gate NULL, NULL, &errmsg, idxs[i].bxi_tbl, idxs[i].bxi_idx, 20847c478bd9Sstevel@tonic-gate idxs[i].bxi_tbl, idxs[i].bxi_cols); 20857c478bd9Sstevel@tonic-gate 20867c478bd9Sstevel@tonic-gate if (ret != SQLITE_OK) { 20877c478bd9Sstevel@tonic-gate configd_critical( 20887c478bd9Sstevel@tonic-gate "%s: %s_%s index creation fails: %s\n", file, 20897c478bd9Sstevel@tonic-gate idxs[i].bxi_tbl, idxs[i].bxi_idx, errmsg); 20907c478bd9Sstevel@tonic-gate free(errmsg); 20917c478bd9Sstevel@tonic-gate return (-1); 20927c478bd9Sstevel@tonic-gate } 20937c478bd9Sstevel@tonic-gate } 20947c478bd9Sstevel@tonic-gate return (0); 20957c478bd9Sstevel@tonic-gate } 20967c478bd9Sstevel@tonic-gate 20977c478bd9Sstevel@tonic-gate static int 20987c478bd9Sstevel@tonic-gate backend_init_schema(sqlite_backend_t *be, const char *db_file, backend_type_t t) 20997c478bd9Sstevel@tonic-gate { 21007c478bd9Sstevel@tonic-gate int i; 21017c478bd9Sstevel@tonic-gate char *errmsg; 21027c478bd9Sstevel@tonic-gate int ret; 21037c478bd9Sstevel@tonic-gate 21047c478bd9Sstevel@tonic-gate assert(t == BACKEND_TYPE_NORMAL || t == BACKEND_TYPE_NONPERSIST); 21057c478bd9Sstevel@tonic-gate 21067c478bd9Sstevel@tonic-gate if (t == BACKEND_TYPE_NORMAL) { 21077c478bd9Sstevel@tonic-gate ret = BACKEND_ADD_SCHEMA(be, db_file, tbls_normal, idxs_normal); 21087c478bd9Sstevel@tonic-gate } else if (t == BACKEND_TYPE_NONPERSIST) { 21097c478bd9Sstevel@tonic-gate ret = BACKEND_ADD_SCHEMA(be, db_file, tbls_np, idxs_np); 21107c478bd9Sstevel@tonic-gate } else { 21117c478bd9Sstevel@tonic-gate abort(); /* can't happen */ 21127c478bd9Sstevel@tonic-gate } 21137c478bd9Sstevel@tonic-gate 21147c478bd9Sstevel@tonic-gate if (ret < 0) { 21157c478bd9Sstevel@tonic-gate return (ret); 21167c478bd9Sstevel@tonic-gate } 21177c478bd9Sstevel@tonic-gate 21187c478bd9Sstevel@tonic-gate ret = BACKEND_ADD_SCHEMA(be, db_file, tbls_common, idxs_common); 21197c478bd9Sstevel@tonic-gate if (ret < 0) { 21207c478bd9Sstevel@tonic-gate return (ret); 21217c478bd9Sstevel@tonic-gate } 21227c478bd9Sstevel@tonic-gate 21237c478bd9Sstevel@tonic-gate /* 21247c478bd9Sstevel@tonic-gate * Add the schema version to the table 21257c478bd9Sstevel@tonic-gate */ 21267c478bd9Sstevel@tonic-gate ret = sqlite_exec_printf(be->be_db, 21277c478bd9Sstevel@tonic-gate "INSERT INTO schema_version (schema_version) VALUES (%d)", 21287c478bd9Sstevel@tonic-gate NULL, NULL, &errmsg, BACKEND_SCHEMA_VERSION); 21297c478bd9Sstevel@tonic-gate if (ret != SQLITE_OK) { 21307c478bd9Sstevel@tonic-gate configd_critical( 21317c478bd9Sstevel@tonic-gate "setting schema version fails: %s\n", errmsg); 21327c478bd9Sstevel@tonic-gate free(errmsg); 21337c478bd9Sstevel@tonic-gate } 21347c478bd9Sstevel@tonic-gate 21357c478bd9Sstevel@tonic-gate /* 21367c478bd9Sstevel@tonic-gate * Populate id_tbl with initial IDs. 21377c478bd9Sstevel@tonic-gate */ 21387c478bd9Sstevel@tonic-gate for (i = 0; i < BACKEND_ID_INVALID; i++) { 21397c478bd9Sstevel@tonic-gate const char *name = id_space_to_name(i); 21407c478bd9Sstevel@tonic-gate 21417c478bd9Sstevel@tonic-gate ret = sqlite_exec_printf(be->be_db, 21427c478bd9Sstevel@tonic-gate "INSERT INTO id_tbl (id_name, id_next) " 21437c478bd9Sstevel@tonic-gate "VALUES ('%q', %d);", NULL, NULL, &errmsg, name, 1); 21447c478bd9Sstevel@tonic-gate if (ret != SQLITE_OK) { 21457c478bd9Sstevel@tonic-gate configd_critical( 21467c478bd9Sstevel@tonic-gate "id insertion for %s fails: %s\n", name, errmsg); 21477c478bd9Sstevel@tonic-gate free(errmsg); 21487c478bd9Sstevel@tonic-gate return (-1); 21497c478bd9Sstevel@tonic-gate } 21507c478bd9Sstevel@tonic-gate } 21517c478bd9Sstevel@tonic-gate /* 21527c478bd9Sstevel@tonic-gate * Set the persistance of the database. The normal database is marked 21537c478bd9Sstevel@tonic-gate * "synchronous", so that all writes are synchronized to stable storage 21547c478bd9Sstevel@tonic-gate * before proceeding. 21557c478bd9Sstevel@tonic-gate */ 21567c478bd9Sstevel@tonic-gate ret = sqlite_exec_printf(be->be_db, 21577c478bd9Sstevel@tonic-gate "PRAGMA default_synchronous = %s; PRAGMA synchronous = %s;", 21587c478bd9Sstevel@tonic-gate NULL, NULL, &errmsg, 21597c478bd9Sstevel@tonic-gate (t == BACKEND_TYPE_NORMAL)? "ON" : "OFF", 21607c478bd9Sstevel@tonic-gate (t == BACKEND_TYPE_NORMAL)? "ON" : "OFF"); 21617c478bd9Sstevel@tonic-gate if (ret != SQLITE_OK) { 21627c478bd9Sstevel@tonic-gate configd_critical("pragma setting fails: %s\n", errmsg); 21637c478bd9Sstevel@tonic-gate free(errmsg); 21647c478bd9Sstevel@tonic-gate return (-1); 21657c478bd9Sstevel@tonic-gate } 21667c478bd9Sstevel@tonic-gate 21677c478bd9Sstevel@tonic-gate return (0); 21687c478bd9Sstevel@tonic-gate } 21697c478bd9Sstevel@tonic-gate 21707c478bd9Sstevel@tonic-gate int 21717c478bd9Sstevel@tonic-gate backend_init(const char *db_file, const char *npdb_file, int have_np) 21727c478bd9Sstevel@tonic-gate { 21737c478bd9Sstevel@tonic-gate sqlite_backend_t *be; 21747c478bd9Sstevel@tonic-gate int r; 21757c478bd9Sstevel@tonic-gate int writable_persist = 1; 21767c478bd9Sstevel@tonic-gate 21777c478bd9Sstevel@tonic-gate /* set up our temporary directory */ 21787c478bd9Sstevel@tonic-gate sqlite_temp_directory = "/etc/svc/volatile"; 21797c478bd9Sstevel@tonic-gate 21807c478bd9Sstevel@tonic-gate if (strcmp(SQLITE_VERSION, sqlite_version) != 0) { 21817c478bd9Sstevel@tonic-gate configd_critical("Mismatched link! (%s should be %s)\n", 21827c478bd9Sstevel@tonic-gate sqlite_version, SQLITE_VERSION); 21837c478bd9Sstevel@tonic-gate return (CONFIGD_EXIT_DATABASE_INIT_FAILED); 21847c478bd9Sstevel@tonic-gate } 2185*c0889d7aSstevep 2186*c0889d7aSstevep /* 2187*c0889d7aSstevep * If the system crashed during a backend switch, there might 2188*c0889d7aSstevep * be a leftover transient database which contains useful 2189*c0889d7aSstevep * information which can be used for recovery. 2190*c0889d7aSstevep */ 2191*c0889d7aSstevep backend_switch_recovery(); 2192*c0889d7aSstevep 21937c478bd9Sstevel@tonic-gate if (db_file == NULL) 21947c478bd9Sstevel@tonic-gate db_file = REPOSITORY_DB; 21955b7f77adStw21770 if (strcmp(db_file, REPOSITORY_DB) != 0) { 21965b7f77adStw21770 is_main_repository = 0; 21975b7f77adStw21770 } 21987c478bd9Sstevel@tonic-gate 21997c478bd9Sstevel@tonic-gate r = backend_create(BACKEND_TYPE_NORMAL, db_file, &be); 22007c478bd9Sstevel@tonic-gate switch (r) { 22017c478bd9Sstevel@tonic-gate case BACKEND_CREATE_FAIL: 22027c478bd9Sstevel@tonic-gate return (CONFIGD_EXIT_DATABASE_INIT_FAILED); 22037c478bd9Sstevel@tonic-gate case BACKEND_CREATE_LOCKED: 22047c478bd9Sstevel@tonic-gate return (CONFIGD_EXIT_DATABASE_LOCKED); 22057c478bd9Sstevel@tonic-gate case BACKEND_CREATE_SUCCESS: 22067c478bd9Sstevel@tonic-gate break; /* success */ 22077c478bd9Sstevel@tonic-gate case BACKEND_CREATE_READONLY: 22087c478bd9Sstevel@tonic-gate writable_persist = 0; 22097c478bd9Sstevel@tonic-gate break; 22107c478bd9Sstevel@tonic-gate case BACKEND_CREATE_NEED_INIT: 22117c478bd9Sstevel@tonic-gate if (backend_init_schema(be, db_file, BACKEND_TYPE_NORMAL)) { 22127c478bd9Sstevel@tonic-gate backend_destroy(be); 22137c478bd9Sstevel@tonic-gate return (CONFIGD_EXIT_DATABASE_INIT_FAILED); 22147c478bd9Sstevel@tonic-gate } 22157c478bd9Sstevel@tonic-gate break; 22167c478bd9Sstevel@tonic-gate default: 22177c478bd9Sstevel@tonic-gate abort(); 22187c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 22197c478bd9Sstevel@tonic-gate } 22207c478bd9Sstevel@tonic-gate backend_create_finish(BACKEND_TYPE_NORMAL, be); 22217c478bd9Sstevel@tonic-gate 22227c478bd9Sstevel@tonic-gate if (have_np) { 22237c478bd9Sstevel@tonic-gate if (npdb_file == NULL) 22247c478bd9Sstevel@tonic-gate npdb_file = NONPERSIST_DB; 22257c478bd9Sstevel@tonic-gate 22267c478bd9Sstevel@tonic-gate r = backend_create(BACKEND_TYPE_NONPERSIST, npdb_file, &be); 22277c478bd9Sstevel@tonic-gate switch (r) { 22287c478bd9Sstevel@tonic-gate case BACKEND_CREATE_SUCCESS: 22297c478bd9Sstevel@tonic-gate break; /* success */ 22307c478bd9Sstevel@tonic-gate case BACKEND_CREATE_FAIL: 22317c478bd9Sstevel@tonic-gate return (CONFIGD_EXIT_DATABASE_INIT_FAILED); 22327c478bd9Sstevel@tonic-gate case BACKEND_CREATE_LOCKED: 22337c478bd9Sstevel@tonic-gate return (CONFIGD_EXIT_DATABASE_LOCKED); 22347c478bd9Sstevel@tonic-gate case BACKEND_CREATE_READONLY: 22357c478bd9Sstevel@tonic-gate configd_critical("%s: unable to write\n", npdb_file); 22367c478bd9Sstevel@tonic-gate return (CONFIGD_EXIT_DATABASE_INIT_FAILED); 22377c478bd9Sstevel@tonic-gate case BACKEND_CREATE_NEED_INIT: 22387c478bd9Sstevel@tonic-gate if (backend_init_schema(be, db_file, 22397c478bd9Sstevel@tonic-gate BACKEND_TYPE_NONPERSIST)) { 22407c478bd9Sstevel@tonic-gate backend_destroy(be); 22417c478bd9Sstevel@tonic-gate return (CONFIGD_EXIT_DATABASE_INIT_FAILED); 22427c478bd9Sstevel@tonic-gate } 22437c478bd9Sstevel@tonic-gate break; 22447c478bd9Sstevel@tonic-gate default: 22457c478bd9Sstevel@tonic-gate abort(); 22467c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 22477c478bd9Sstevel@tonic-gate } 22487c478bd9Sstevel@tonic-gate backend_create_finish(BACKEND_TYPE_NONPERSIST, be); 22497c478bd9Sstevel@tonic-gate 22507c478bd9Sstevel@tonic-gate /* 22517c478bd9Sstevel@tonic-gate * If we started up with a writable filesystem, but the 22527c478bd9Sstevel@tonic-gate * non-persistent database needed initialization, we 22537c478bd9Sstevel@tonic-gate * are booting a non-global zone, so do a backup. 22547c478bd9Sstevel@tonic-gate */ 22557c478bd9Sstevel@tonic-gate if (r == BACKEND_CREATE_NEED_INIT && writable_persist && 22567c478bd9Sstevel@tonic-gate backend_lock(BACKEND_TYPE_NORMAL, 0, &be) == 22577c478bd9Sstevel@tonic-gate REP_PROTOCOL_SUCCESS) { 22587c478bd9Sstevel@tonic-gate if (backend_create_backup_locked(be, 22597c478bd9Sstevel@tonic-gate REPOSITORY_BOOT_BACKUP) != REP_PROTOCOL_SUCCESS) { 22607c478bd9Sstevel@tonic-gate configd_critical( 22617c478bd9Sstevel@tonic-gate "unable to create \"%s\" backup of " 22627c478bd9Sstevel@tonic-gate "\"%s\"\n", REPOSITORY_BOOT_BACKUP, 22637c478bd9Sstevel@tonic-gate be->be_path); 22647c478bd9Sstevel@tonic-gate } 22657c478bd9Sstevel@tonic-gate backend_unlock(be); 22667c478bd9Sstevel@tonic-gate } 22677c478bd9Sstevel@tonic-gate } 22687c478bd9Sstevel@tonic-gate return (CONFIGD_EXIT_OKAY); 22697c478bd9Sstevel@tonic-gate } 22707c478bd9Sstevel@tonic-gate 22717c478bd9Sstevel@tonic-gate /* 22727c478bd9Sstevel@tonic-gate * quiesce all database activity prior to exiting 22737c478bd9Sstevel@tonic-gate */ 22747c478bd9Sstevel@tonic-gate void 22757c478bd9Sstevel@tonic-gate backend_fini(void) 22767c478bd9Sstevel@tonic-gate { 22777c478bd9Sstevel@tonic-gate sqlite_backend_t *be_normal, *be_np; 22787c478bd9Sstevel@tonic-gate 22797c478bd9Sstevel@tonic-gate (void) backend_lock(BACKEND_TYPE_NORMAL, 1, &be_normal); 22807c478bd9Sstevel@tonic-gate (void) backend_lock(BACKEND_TYPE_NONPERSIST, 1, &be_np); 22817c478bd9Sstevel@tonic-gate } 22827c478bd9Sstevel@tonic-gate 22837c478bd9Sstevel@tonic-gate #define QUERY_BASE 128 22847c478bd9Sstevel@tonic-gate backend_query_t * 22857c478bd9Sstevel@tonic-gate backend_query_alloc(void) 22867c478bd9Sstevel@tonic-gate { 22877c478bd9Sstevel@tonic-gate backend_query_t *q; 22887c478bd9Sstevel@tonic-gate q = calloc(1, sizeof (backend_query_t)); 22897c478bd9Sstevel@tonic-gate if (q != NULL) { 22907c478bd9Sstevel@tonic-gate q->bq_size = QUERY_BASE; 22917c478bd9Sstevel@tonic-gate q->bq_buf = calloc(1, q->bq_size); 22927c478bd9Sstevel@tonic-gate if (q->bq_buf == NULL) { 22937c478bd9Sstevel@tonic-gate q->bq_size = 0; 22947c478bd9Sstevel@tonic-gate } 22957c478bd9Sstevel@tonic-gate 22967c478bd9Sstevel@tonic-gate } 22977c478bd9Sstevel@tonic-gate return (q); 22987c478bd9Sstevel@tonic-gate } 22997c478bd9Sstevel@tonic-gate 23007c478bd9Sstevel@tonic-gate void 23017c478bd9Sstevel@tonic-gate backend_query_append(backend_query_t *q, const char *value) 23027c478bd9Sstevel@tonic-gate { 23037c478bd9Sstevel@tonic-gate char *alloc; 23047c478bd9Sstevel@tonic-gate int count; 23057c478bd9Sstevel@tonic-gate size_t size, old_len; 23067c478bd9Sstevel@tonic-gate 23077c478bd9Sstevel@tonic-gate if (q == NULL) { 23087c478bd9Sstevel@tonic-gate /* We'll discover the error when we try to run the query. */ 23097c478bd9Sstevel@tonic-gate return; 23107c478bd9Sstevel@tonic-gate } 23117c478bd9Sstevel@tonic-gate 23127c478bd9Sstevel@tonic-gate while (q->bq_buf != NULL) { 23137c478bd9Sstevel@tonic-gate old_len = strlen(q->bq_buf); 23147c478bd9Sstevel@tonic-gate size = q->bq_size; 23157c478bd9Sstevel@tonic-gate count = strlcat(q->bq_buf, value, size); 23167c478bd9Sstevel@tonic-gate 23177c478bd9Sstevel@tonic-gate if (count < size) 23187c478bd9Sstevel@tonic-gate break; /* success */ 23197c478bd9Sstevel@tonic-gate 23207c478bd9Sstevel@tonic-gate q->bq_buf[old_len] = 0; 23217c478bd9Sstevel@tonic-gate size = round_up_to_p2(count + 1); 23227c478bd9Sstevel@tonic-gate 23237c478bd9Sstevel@tonic-gate assert(size > q->bq_size); 23247c478bd9Sstevel@tonic-gate alloc = realloc(q->bq_buf, size); 23257c478bd9Sstevel@tonic-gate if (alloc == NULL) { 23267c478bd9Sstevel@tonic-gate free(q->bq_buf); 23277c478bd9Sstevel@tonic-gate q->bq_buf = NULL; 23287c478bd9Sstevel@tonic-gate break; /* can't grow */ 23297c478bd9Sstevel@tonic-gate } 23307c478bd9Sstevel@tonic-gate 23317c478bd9Sstevel@tonic-gate q->bq_buf = alloc; 23327c478bd9Sstevel@tonic-gate q->bq_size = size; 23337c478bd9Sstevel@tonic-gate } 23347c478bd9Sstevel@tonic-gate } 23357c478bd9Sstevel@tonic-gate 23367c478bd9Sstevel@tonic-gate void 23377c478bd9Sstevel@tonic-gate backend_query_add(backend_query_t *q, const char *format, ...) 23387c478bd9Sstevel@tonic-gate { 23397c478bd9Sstevel@tonic-gate va_list args; 23407c478bd9Sstevel@tonic-gate char *new; 23417c478bd9Sstevel@tonic-gate 23427c478bd9Sstevel@tonic-gate if (q == NULL || q->bq_buf == NULL) 23437c478bd9Sstevel@tonic-gate return; 23447c478bd9Sstevel@tonic-gate 23457c478bd9Sstevel@tonic-gate va_start(args, format); 23467c478bd9Sstevel@tonic-gate new = sqlite_vmprintf(format, args); 23477c478bd9Sstevel@tonic-gate va_end(args); 23487c478bd9Sstevel@tonic-gate 23497c478bd9Sstevel@tonic-gate if (new == NULL) { 23507c478bd9Sstevel@tonic-gate free(q->bq_buf); 23517c478bd9Sstevel@tonic-gate q->bq_buf = NULL; 23527c478bd9Sstevel@tonic-gate return; 23537c478bd9Sstevel@tonic-gate } 23547c478bd9Sstevel@tonic-gate 23557c478bd9Sstevel@tonic-gate backend_query_append(q, new); 23567c478bd9Sstevel@tonic-gate 23577c478bd9Sstevel@tonic-gate free(new); 23587c478bd9Sstevel@tonic-gate } 23597c478bd9Sstevel@tonic-gate 23607c478bd9Sstevel@tonic-gate void 23617c478bd9Sstevel@tonic-gate backend_query_free(backend_query_t *q) 23627c478bd9Sstevel@tonic-gate { 23637c478bd9Sstevel@tonic-gate if (q != NULL) { 23647c478bd9Sstevel@tonic-gate if (q->bq_buf != NULL) { 23657c478bd9Sstevel@tonic-gate free(q->bq_buf); 23667c478bd9Sstevel@tonic-gate } 23677c478bd9Sstevel@tonic-gate free(q); 23687c478bd9Sstevel@tonic-gate } 23697c478bd9Sstevel@tonic-gate } 2370