Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/lib/libcasper/services/cap_fileargs/cap_fileargs.h
48255 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2018 Mariusz Zaborski <[email protected]>
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
29
#ifndef _FILEARGS_H_
30
#define _FILEARGS_H_
31
32
#include <sys/cdefs.h>
33
#include <sys/dnv.h>
34
#include <sys/nv.h>
35
36
#include <stdbool.h>
37
38
#define FA_OPEN 1
39
#define FA_LSTAT 2
40
#define FA_REALPATH 4
41
42
#ifdef WITH_CASPER
43
struct fileargs;
44
typedef struct fileargs fileargs_t;
45
struct stat;
46
47
__BEGIN_DECLS
48
49
fileargs_t *fileargs_init(int argc, char *argv[], int flags, mode_t mode,
50
cap_rights_t *rightsp, int operations);
51
fileargs_t *fileargs_cinit(cap_channel_t *cas, int argc, char *argv[],
52
int flags, mode_t mode, cap_rights_t *rightsp, int operations);
53
fileargs_t *fileargs_initnv(nvlist_t *limits);
54
fileargs_t *fileargs_cinitnv(cap_channel_t *cas, nvlist_t *limits);
55
int fileargs_lstat(fileargs_t *fa, const char *name, struct stat *sb);
56
int fileargs_open(fileargs_t *fa, const char *name);
57
char *fileargs_realpath(fileargs_t *fa, const char *pathname,
58
char *reserved_path);
59
void fileargs_free(fileargs_t *fa);
60
FILE *fileargs_fopen(fileargs_t *fa, const char *name, const char *mode);
61
62
fileargs_t *fileargs_wrap(cap_channel_t *chan, int fdflags);
63
cap_channel_t *fileargs_unwrap(fileargs_t *fa, int *fdflags);
64
65
__END_DECLS
66
67
#else
68
typedef struct fileargs {
69
int fa_flags;
70
mode_t fa_mode;
71
} fileargs_t;
72
73
static inline fileargs_t *
74
fileargs_init(int argc __unused, char *argv[] __unused, int flags, mode_t mode,
75
cap_rights_t *rightsp __unused, int operations __unused) {
76
fileargs_t *fa;
77
78
fa = (fileargs_t *)malloc(sizeof(*fa));
79
if (fa != NULL) {
80
fa->fa_flags = flags;
81
fa->fa_mode = mode;
82
}
83
84
return (fa);
85
}
86
87
static inline fileargs_t *
88
fileargs_cinit(cap_channel_t *cas __unused, int argc, char *argv[], int flags,
89
mode_t mode, cap_rights_t *rightsp, int operations)
90
{
91
92
return (fileargs_init(argc, argv, flags, mode, rightsp, operations));
93
}
94
95
static inline fileargs_t *
96
fileargs_initnv(nvlist_t *limits)
97
{
98
fileargs_t *fa;
99
100
fa = fileargs_init(0, NULL,
101
nvlist_get_number(limits, "flags"),
102
dnvlist_get_number(limits, "mode", 0),
103
NULL,
104
nvlist_get_number(limits, "operations"));
105
nvlist_destroy(limits);
106
107
return (fa);
108
}
109
110
static inline fileargs_t *
111
fileargs_cinitnv(cap_channel_t *cas __unused, nvlist_t *limits)
112
{
113
114
return (fileargs_initnv(limits));
115
}
116
117
#define fileargs_lstat(fa, name, sb) \
118
lstat(name, sb)
119
#define fileargs_open(fa, name) \
120
open(name, fa->fa_flags, fa->fa_mode)
121
#define fileargs_realpath(fa, pathname, reserved_path) \
122
realpath(pathname, reserved_path)
123
124
static inline
125
FILE *fileargs_fopen(fileargs_t *fa, const char *name, const char *mode)
126
{
127
(void) fa;
128
return (fopen(name, mode));
129
}
130
#define fileargs_free(fa) (free(fa))
131
132
static inline fileargs_t *
133
fileargs_wrap(cap_channel_t *chan, int fdflags)
134
{
135
136
cap_close(chan);
137
return (fileargs_init(0, NULL, fdflags, 0, NULL, 0));
138
}
139
140
static inline cap_channel_t *
141
fileargs_unwrap(fileargs_t *fa, int *fdflags)
142
{
143
144
if (fdflags != NULL) {
145
*fdflags = fa->fa_flags;
146
}
147
fileargs_free(fa);
148
return (cap_init());
149
}
150
151
#endif
152
153
#endif /* !_FILEARGS_H_ */
154
155