1*877a840cSConrad Meyer /* 2*877a840cSConrad Meyer * Copyright (c) 2016, EMC / Isilon Storage Division 3*877a840cSConrad Meyer * All rights reserved. 4*877a840cSConrad Meyer * 5*877a840cSConrad Meyer * Redistribution and use in source and binary forms, with or without 6*877a840cSConrad Meyer * modification, are permitted provided that the following conditions 7*877a840cSConrad Meyer * are met: 8*877a840cSConrad Meyer * 1. Redistributions of source code must retain the above copyright 9*877a840cSConrad Meyer * notice, this list of conditions and the following disclaimer. 10*877a840cSConrad Meyer * 2. Redistributions in binary form must reproduce the above copyright 11*877a840cSConrad Meyer * notice, this list of conditions and the following disclaimer in the 12*877a840cSConrad Meyer * documentation and/or other materials provided with the distribution. 13*877a840cSConrad Meyer * 14*877a840cSConrad Meyer * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS 15*877a840cSConrad Meyer * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 16*877a840cSConrad Meyer * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17*877a840cSConrad Meyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR 18*877a840cSConrad Meyer * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19*877a840cSConrad Meyer * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20*877a840cSConrad Meyer * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 21*877a840cSConrad Meyer * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22*877a840cSConrad Meyer * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23*877a840cSConrad Meyer * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 24*877a840cSConrad Meyer * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25*877a840cSConrad Meyer */ 26*877a840cSConrad Meyer 27*877a840cSConrad Meyer #include <sys/cdefs.h> 28*877a840cSConrad Meyer __FBSDID("$FreeBSD$"); 29*877a840cSConrad Meyer 30*877a840cSConrad Meyer #include <sys/fcntl.h> 31*877a840cSConrad Meyer 32*877a840cSConrad Meyer #include <errno.h> 33*877a840cSConrad Meyer #include <stdio.h> 34*877a840cSConrad Meyer #include <stdlib.h> 35*877a840cSConrad Meyer 36*877a840cSConrad Meyer #include "local.h" 37*877a840cSConrad Meyer 38*877a840cSConrad Meyer struct fopencookie_thunk { 39*877a840cSConrad Meyer void *foc_cookie; 40*877a840cSConrad Meyer cookie_io_functions_t foc_io; 41*877a840cSConrad Meyer }; 42*877a840cSConrad Meyer 43*877a840cSConrad Meyer static int _fopencookie_read(void *, char *, int); 44*877a840cSConrad Meyer static int _fopencookie_write(void *, const char *, int); 45*877a840cSConrad Meyer static fpos_t _fopencookie_seek(void *, fpos_t, int); 46*877a840cSConrad Meyer static int _fopencookie_close(void *); 47*877a840cSConrad Meyer 48*877a840cSConrad Meyer FILE * 49*877a840cSConrad Meyer fopencookie(void *cookie, const char *mode, cookie_io_functions_t io_funcs) 50*877a840cSConrad Meyer { 51*877a840cSConrad Meyer int (*readfn)(void *, char *, int); 52*877a840cSConrad Meyer int (*writefn)(void *, const char *, int); 53*877a840cSConrad Meyer struct fopencookie_thunk *thunk; 54*877a840cSConrad Meyer FILE *fp; 55*877a840cSConrad Meyer int flags, oflags; 56*877a840cSConrad Meyer 57*877a840cSConrad Meyer if ((flags = __sflags(mode, &oflags)) == 0) 58*877a840cSConrad Meyer return (NULL); 59*877a840cSConrad Meyer 60*877a840cSConrad Meyer thunk = malloc(sizeof(*thunk)); 61*877a840cSConrad Meyer if (thunk == NULL) 62*877a840cSConrad Meyer return (NULL); 63*877a840cSConrad Meyer 64*877a840cSConrad Meyer thunk->foc_cookie = cookie; 65*877a840cSConrad Meyer thunk->foc_io = io_funcs; 66*877a840cSConrad Meyer 67*877a840cSConrad Meyer readfn = _fopencookie_read; 68*877a840cSConrad Meyer writefn = _fopencookie_write; 69*877a840cSConrad Meyer if (flags == __SWR) 70*877a840cSConrad Meyer readfn = NULL; 71*877a840cSConrad Meyer else if (flags == __SRD) 72*877a840cSConrad Meyer writefn = NULL; 73*877a840cSConrad Meyer 74*877a840cSConrad Meyer fp = funopen(thunk, readfn, writefn, _fopencookie_seek, 75*877a840cSConrad Meyer _fopencookie_close); 76*877a840cSConrad Meyer if (fp == NULL) { 77*877a840cSConrad Meyer free(thunk); 78*877a840cSConrad Meyer return (NULL); 79*877a840cSConrad Meyer } 80*877a840cSConrad Meyer 81*877a840cSConrad Meyer if ((oflags & O_APPEND) != 0) 82*877a840cSConrad Meyer fp->_flags |= __SAPP; 83*877a840cSConrad Meyer 84*877a840cSConrad Meyer return (fp); 85*877a840cSConrad Meyer } 86*877a840cSConrad Meyer 87*877a840cSConrad Meyer static int 88*877a840cSConrad Meyer _fopencookie_read(void *cookie, char *buf, int size) 89*877a840cSConrad Meyer { 90*877a840cSConrad Meyer struct fopencookie_thunk *thunk; 91*877a840cSConrad Meyer 92*877a840cSConrad Meyer thunk = cookie; 93*877a840cSConrad Meyer 94*877a840cSConrad Meyer /* Reads from a stream with NULL read return EOF. */ 95*877a840cSConrad Meyer if (thunk->foc_io.read == NULL) 96*877a840cSConrad Meyer return (0); 97*877a840cSConrad Meyer 98*877a840cSConrad Meyer return ((int)thunk->foc_io.read(thunk->foc_cookie, buf, (size_t)size)); 99*877a840cSConrad Meyer } 100*877a840cSConrad Meyer 101*877a840cSConrad Meyer static int 102*877a840cSConrad Meyer _fopencookie_write(void *cookie, const char *buf, int size) 103*877a840cSConrad Meyer { 104*877a840cSConrad Meyer struct fopencookie_thunk *thunk; 105*877a840cSConrad Meyer 106*877a840cSConrad Meyer thunk = cookie; 107*877a840cSConrad Meyer 108*877a840cSConrad Meyer /* Writes to a stream with NULL write discard data. */ 109*877a840cSConrad Meyer if (thunk->foc_io.write == NULL) 110*877a840cSConrad Meyer return (size); 111*877a840cSConrad Meyer 112*877a840cSConrad Meyer return ((int)thunk->foc_io.write(thunk->foc_cookie, buf, 113*877a840cSConrad Meyer (size_t)size)); 114*877a840cSConrad Meyer } 115*877a840cSConrad Meyer 116*877a840cSConrad Meyer static fpos_t 117*877a840cSConrad Meyer _fopencookie_seek(void *cookie, fpos_t offset, int whence) 118*877a840cSConrad Meyer { 119*877a840cSConrad Meyer struct fopencookie_thunk *thunk; 120*877a840cSConrad Meyer off64_t off64; 121*877a840cSConrad Meyer int res; 122*877a840cSConrad Meyer 123*877a840cSConrad Meyer switch (whence) { 124*877a840cSConrad Meyer case SEEK_SET: 125*877a840cSConrad Meyer case SEEK_CUR: 126*877a840cSConrad Meyer case SEEK_END: 127*877a840cSConrad Meyer break; 128*877a840cSConrad Meyer default: 129*877a840cSConrad Meyer /* fopencookie(3) only allows these three seek modes. */ 130*877a840cSConrad Meyer errno = EINVAL; 131*877a840cSConrad Meyer return (-1); 132*877a840cSConrad Meyer } 133*877a840cSConrad Meyer 134*877a840cSConrad Meyer thunk = cookie; 135*877a840cSConrad Meyer 136*877a840cSConrad Meyer /* 137*877a840cSConrad Meyer * If seek is NULL, it is not possible to perform seek operations on 138*877a840cSConrad Meyer * the stream. 139*877a840cSConrad Meyer */ 140*877a840cSConrad Meyer if (thunk->foc_io.seek == NULL) { 141*877a840cSConrad Meyer errno = ENOTSUP; 142*877a840cSConrad Meyer return (-1); 143*877a840cSConrad Meyer } 144*877a840cSConrad Meyer 145*877a840cSConrad Meyer off64 = (off64_t)offset; 146*877a840cSConrad Meyer res = thunk->foc_io.seek(thunk->foc_cookie, &off64, whence); 147*877a840cSConrad Meyer if (res < 0) 148*877a840cSConrad Meyer return (res); 149*877a840cSConrad Meyer 150*877a840cSConrad Meyer return ((fpos_t)off64); 151*877a840cSConrad Meyer } 152*877a840cSConrad Meyer 153*877a840cSConrad Meyer static int 154*877a840cSConrad Meyer _fopencookie_close(void *cookie) 155*877a840cSConrad Meyer { 156*877a840cSConrad Meyer struct fopencookie_thunk *thunk; 157*877a840cSConrad Meyer int ret, serrno; 158*877a840cSConrad Meyer 159*877a840cSConrad Meyer ret = 0; 160*877a840cSConrad Meyer thunk = cookie; 161*877a840cSConrad Meyer if (thunk->foc_io.close != NULL) 162*877a840cSConrad Meyer ret = thunk->foc_io.close(thunk->foc_cookie); 163*877a840cSConrad Meyer 164*877a840cSConrad Meyer serrno = errno; 165*877a840cSConrad Meyer free(thunk); 166*877a840cSConrad Meyer errno = serrno; 167*877a840cSConrad Meyer return (ret); 168*877a840cSConrad Meyer } 169