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 #ifdef WITH_CASPER
40 struct fileargs;
41 typedef struct fileargs fileargs_t;
42 
43 fileargs_t *fileargs_init(int argc, char *argv[], int flags, mode_t mode,
44     cap_rights_t *rightsp);
45 fileargs_t *fileargs_cinit(cap_channel_t *cas, int argc, char *argv[],
46     int flags, mode_t mode, cap_rights_t *rightsp);
47 fileargs_t *fileargs_initnv(nvlist_t *limits);
48 fileargs_t *fileargs_cinitnv(cap_channel_t *cas, nvlist_t *limits);
49 int fileargs_open(fileargs_t *fa, const char *name);
50 void fileargs_free(fileargs_t *fa);
51 FILE *fileargs_fopen(fileargs_t *fa, const char *name, const char *mode);
52 #else
53 typedef struct fileargs {
54 	int	fa_flags;
55 	mode_t	fa_mode;
56 } fileargs_t;
57 
58 static inline fileargs_t *
59 fileargs_init(int argc __unused, char *argv[] __unused, int flags, mode_t mode,
60     cap_rights_t *rightsp __unused) {
61 	fileargs_t *fa;
62 
63 	fa = malloc(sizeof(*fa));
64 	if (fa != NULL) {
65 		fa->fa_flags = flags;
66 		fa->fa_mode = mode;
67 	}
68 
69 	return (fa);
70 }
71 
72 static inline fileargs_t *
73 fileargs_cinit(cap_channel_t *cas __unused, int argc, char *argv[], int flags,
74     mode_t mode, cap_rights_t *rightsp)
75 {
76 
77 	return (fileargs_init(argc, argv, flags, mode, rightsp));
78 }
79 
80 static inline fileargs_t *
81 fileargs_initnv(nvlist_t *limits)
82 {
83 	fileargs_t *fa;
84 
85 	fa = fileargs_init(0, NULL,
86 	    nvlist_get_number(limits, "flags"),
87 	    dnvlist_get_number(limits, "mode", 0),
88 	    NULL);
89 	nvlist_destroy(limits);
90 
91 	return (fa);
92 }
93 
94 static inline fileargs_t *
95 fileargs_cinitnv(cap_channel_t *cas __unused, nvlist_t *limits)
96 {
97 
98 	return (fileargs_initnv(limits));
99 }
100 
101 #define	fileargs_open(fa, name)							\
102 	open(name, fa->fa_flags, fa->fa_mode)
103 #define	fileargs_fopen(fa, name, mode)						\
104 	fopen(name, mode)
105 #define	fileargs_free(fa)	(free(fa))
106 #endif
107 
108 #endif	/* !_FILEARGS_H_ */
109