Path: blob/main/lib/libcasper/services/cap_fileargs/cap_fileargs.h
48255 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2018 Mariusz Zaborski <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#ifndef _FILEARGS_H_29#define _FILEARGS_H_3031#include <sys/cdefs.h>32#include <sys/dnv.h>33#include <sys/nv.h>3435#include <stdbool.h>3637#define FA_OPEN 138#define FA_LSTAT 239#define FA_REALPATH 44041#ifdef WITH_CASPER42struct fileargs;43typedef struct fileargs fileargs_t;44struct stat;4546__BEGIN_DECLS4748fileargs_t *fileargs_init(int argc, char *argv[], int flags, mode_t mode,49cap_rights_t *rightsp, int operations);50fileargs_t *fileargs_cinit(cap_channel_t *cas, int argc, char *argv[],51int flags, mode_t mode, cap_rights_t *rightsp, int operations);52fileargs_t *fileargs_initnv(nvlist_t *limits);53fileargs_t *fileargs_cinitnv(cap_channel_t *cas, nvlist_t *limits);54int fileargs_lstat(fileargs_t *fa, const char *name, struct stat *sb);55int fileargs_open(fileargs_t *fa, const char *name);56char *fileargs_realpath(fileargs_t *fa, const char *pathname,57char *reserved_path);58void fileargs_free(fileargs_t *fa);59FILE *fileargs_fopen(fileargs_t *fa, const char *name, const char *mode);6061fileargs_t *fileargs_wrap(cap_channel_t *chan, int fdflags);62cap_channel_t *fileargs_unwrap(fileargs_t *fa, int *fdflags);6364__END_DECLS6566#else67typedef struct fileargs {68int fa_flags;69mode_t fa_mode;70} fileargs_t;7172static inline fileargs_t *73fileargs_init(int argc __unused, char *argv[] __unused, int flags, mode_t mode,74cap_rights_t *rightsp __unused, int operations __unused) {75fileargs_t *fa;7677fa = (fileargs_t *)malloc(sizeof(*fa));78if (fa != NULL) {79fa->fa_flags = flags;80fa->fa_mode = mode;81}8283return (fa);84}8586static inline fileargs_t *87fileargs_cinit(cap_channel_t *cas __unused, int argc, char *argv[], int flags,88mode_t mode, cap_rights_t *rightsp, int operations)89{9091return (fileargs_init(argc, argv, flags, mode, rightsp, operations));92}9394static inline fileargs_t *95fileargs_initnv(nvlist_t *limits)96{97fileargs_t *fa;9899fa = fileargs_init(0, NULL,100nvlist_get_number(limits, "flags"),101dnvlist_get_number(limits, "mode", 0),102NULL,103nvlist_get_number(limits, "operations"));104nvlist_destroy(limits);105106return (fa);107}108109static inline fileargs_t *110fileargs_cinitnv(cap_channel_t *cas __unused, nvlist_t *limits)111{112113return (fileargs_initnv(limits));114}115116#define fileargs_lstat(fa, name, sb) \117lstat(name, sb)118#define fileargs_open(fa, name) \119open(name, fa->fa_flags, fa->fa_mode)120#define fileargs_realpath(fa, pathname, reserved_path) \121realpath(pathname, reserved_path)122123static inline124FILE *fileargs_fopen(fileargs_t *fa, const char *name, const char *mode)125{126(void) fa;127return (fopen(name, mode));128}129#define fileargs_free(fa) (free(fa))130131static inline fileargs_t *132fileargs_wrap(cap_channel_t *chan, int fdflags)133{134135cap_close(chan);136return (fileargs_init(0, NULL, fdflags, 0, NULL, 0));137}138139static inline cap_channel_t *140fileargs_unwrap(fileargs_t *fa, int *fdflags)141{142143if (fdflags != NULL) {144*fdflags = fa->fa_flags;145}146fileargs_free(fa);147return (cap_init());148}149150#endif151152#endif /* !_FILEARGS_H_ */153154155