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