github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/libsnap-confine-private/fault-injection.h (about) 1 /* 2 * Copyright (C) 2017 Canonical Ltd 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 3 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 * 16 */ 17 18 #ifndef SNAP_CONFINE_FAULT_INJECTION_H 19 #define SNAP_CONFINE_FAULT_INJECTION_H 20 21 #include <stdbool.h> 22 23 /** 24 * Check for an injected fault. 25 * 26 * The name of the fault must match what was passed to sc_break(). The second 27 * argument can be modified by the fault callback function. The return value 28 * indicates if a fault was injected. It is assumed that once a fault was 29 * injected the passed pointer was used to modify the state in useful way. 30 * 31 * When the pre-processor macro _ENABLE_FAULT_INJECTION is not defined this 32 * function always returns false and does nothing at all. 33 **/ 34 bool sc_faulty(const char *name, void *ptr); 35 36 #ifdef _ENABLE_FAULT_INJECTION 37 38 struct sc_fault_state; 39 40 typedef bool (*sc_fault_fn)(struct sc_fault_state * state, void *ptr); 41 42 struct sc_fault_state { 43 int ncalls; 44 }; 45 46 /** 47 * Inject a fault for testing. 48 * 49 * The name of the fault must match the expected calls to sc_faulty(). The 50 * second argument is a callback that is invoked each time sc_faulty() is 51 * called. It is designed to inspect an argument passed to sc_faulty() and as 52 * well as the state of the fault injection point and return a boolean 53 * indicating that a fault has occurred. 54 * 55 * After testing faults should be reset using sc_reset_faults(). 56 **/ 57 58 void sc_break(const char *name, sc_fault_fn fn); 59 60 /** 61 * Remove all the injected faults. 62 **/ 63 void sc_reset_faults(void); 64 65 #endif // ifndef _ENABLE_FAULT_INJECTION 66 67 #endif