github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/libsnap-confine-private/cleanup-funcs.h (about) 1 /* 2 * Copyright (C) 2015 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_CLEANUP_FUNCS_H 19 #define SNAP_CONFINE_CLEANUP_FUNCS_H 20 21 #ifdef HAVE_CONFIG_H 22 #include "config.h" 23 #endif // HAVE_CONFIG_H 24 25 #include <stdlib.h> 26 #include <stdio.h> 27 #include <sys/types.h> 28 #include <dirent.h> 29 30 // SC_CLEANUP will run the given cleanup function when the variable next 31 // to it goes out of scope. 32 #define SC_CLEANUP(n) __attribute__((cleanup(n))) 33 34 /** 35 * Free a dynamically allocated string. 36 * 37 * This function is designed to be used with SC_CLEANUP() macro. 38 * The variable MUST be initialized for correct operation. 39 * The safe initialisation value is NULL. 40 **/ 41 void sc_cleanup_string(char **ptr); 42 43 /** 44 * Close an open file. 45 * 46 * This function is designed to be used with SC_CLEANUP() macro. 47 * The variable MUST be initialized for correct operation. 48 * The safe initialisation value is NULL. 49 **/ 50 void sc_cleanup_file(FILE ** ptr); 51 52 /** 53 * Close an open file with endmntent(3) 54 * 55 * This function is designed to be used with SC_CLEANUP() macro. 56 * The variable MUST be initialized for correct operation. 57 * The safe initialisation value is NULL. 58 **/ 59 void sc_cleanup_endmntent(FILE ** ptr); 60 61 /** 62 * Close an open directory with closedir(3) 63 * 64 * This function is designed to be used with SC_CLEANUP() macro. 65 * The variable MUST be initialized for correct operation. 66 * The safe initialisation value is NULL. 67 **/ 68 void sc_cleanup_closedir(DIR ** ptr); 69 70 /** 71 * Close an open file descriptor with close(2) 72 * 73 * This function is designed to be used with SC_CLEANUP() macro. 74 * The variable MUST be initialized for correct operation. 75 * The safe initialisation value is -1. 76 **/ 77 void sc_cleanup_close(int *ptr); 78 79 #endif