1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2018 Mariusz Zaborski <oshogbo@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifndef _FILEARGS_H_ 32 #define _FILEARGS_H_ 33 34 #include <sys/dnv.h> 35 #include <sys/nv.h> 36 37 #include <stdbool.h> 38 39 #define FA_OPEN 1 40 #define FA_LSTAT 2 41 42 #ifdef WITH_CASPER 43 struct fileargs; 44 typedef struct fileargs fileargs_t; 45 struct stat; 46 47 fileargs_t *fileargs_init(int argc, char *argv[], int flags, mode_t mode, 48 cap_rights_t *rightsp, int operations); 49 fileargs_t *fileargs_cinit(cap_channel_t *cas, int argc, char *argv[], 50 int flags, mode_t mode, cap_rights_t *rightsp, int operations); 51 fileargs_t *fileargs_initnv(nvlist_t *limits); 52 fileargs_t *fileargs_cinitnv(cap_channel_t *cas, nvlist_t *limits); 53 int fileargs_lstat(fileargs_t *fa, const char *name, struct stat *sb); 54 int fileargs_open(fileargs_t *fa, const char *name); 55 void fileargs_free(fileargs_t *fa); 56 FILE *fileargs_fopen(fileargs_t *fa, const char *name, const char *mode); 57 #else 58 typedef struct fileargs { 59 int fa_flags; 60 mode_t fa_mode; 61 } fileargs_t; 62 63 static inline fileargs_t * 64 fileargs_init(int argc __unused, char *argv[] __unused, int flags, mode_t mode, 65 cap_rights_t *rightsp __unused, int operations __unused) { 66 fileargs_t *fa; 67 68 fa = malloc(sizeof(*fa)); 69 if (fa != NULL) { 70 fa->fa_flags = flags; 71 fa->fa_mode = mode; 72 } 73 74 return (fa); 75 } 76 77 static inline fileargs_t * 78 fileargs_cinit(cap_channel_t *cas __unused, int argc, char *argv[], int flags, 79 mode_t mode, cap_rights_t *rightsp, int operations) 80 { 81 82 return (fileargs_init(argc, argv, flags, mode, rightsp, operations)); 83 } 84 85 static inline fileargs_t * 86 fileargs_initnv(nvlist_t *limits) 87 { 88 fileargs_t *fa; 89 90 fa = fileargs_init(0, NULL, 91 nvlist_get_number(limits, "flags"), 92 dnvlist_get_number(limits, "mode", 0), 93 NULL, 94 nvlist_get_number(limits, "operations")); 95 nvlist_destroy(limits); 96 97 return (fa); 98 } 99 100 static inline fileargs_t * 101 fileargs_cinitnv(cap_channel_t *cas __unused, nvlist_t *limits) 102 { 103 104 return (fileargs_initnv(limits)); 105 } 106 107 #define fileargs_lstat(fa, name, sb) \ 108 lstat(name, sb) 109 #define fileargs_open(fa, name) \ 110 open(name, fa->fa_flags, fa->fa_mode) 111 #define fileargs_fopen(fa, name, mode) \ 112 fopen(name, mode) 113 #define fileargs_free(fa) (free(fa)) 114 #endif 115 116 #endif /* !_FILEARGS_H_ */ 117