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