L4Re/departure

Annotated libfsserver/lib/pipes/pipe_opener_resource.cc

645:3047b11cc814
7 months ago Paul Boddie Fixed the file_data_available result to return zero if the populated span has somehow become less than the current position in the memory region.
paul@93 1
/*
paul@93 2
 * A pipe opener resource.
paul@93 3
 *
paul@300 4
 * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk>
paul@93 5
 *
paul@93 6
 * This program is free software; you can redistribute it and/or
paul@93 7
 * modify it under the terms of the GNU General Public License as
paul@93 8
 * published by the Free Software Foundation; either version 2 of
paul@93 9
 * the License, or (at your option) any later version.
paul@93 10
 *
paul@93 11
 * This program is distributed in the hope that it will be useful,
paul@93 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@93 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@93 14
 * GNU General Public License for more details.
paul@93 15
 *
paul@93 16
 * You should have received a copy of the GNU General Public License
paul@93 17
 * along with this program; if not, write to the Free Software
paul@93 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@93 19
 * Boston, MA  02110-1301, USA
paul@93 20
 */
paul@93 21
paul@161 22
#include <ipc/cap_alloc.h>
paul@535 23
#include <resource/resource_server.h>
paul@161 24
paul@300 25
#include <l4/cxx/exceptions>
paul@300 26
paul@65 27
#include "pipe_opener_resource.h"
paul@65 28
#include "pipe_opener_server.h"
paul@65 29
#include "pipe_pager.h"
paul@65 30
paul@65 31
paul@65 32
paul@65 33
/* Support for providing access to pipes. */
paul@65 34
paul@70 35
PipeOpenerResource::PipeOpenerResource(Memory *memory)
paul@70 36
: _memory(memory)
paul@70 37
{
paul@70 38
}
paul@70 39
paul@459 40
ipc_server_default_config_type PipeOpenerResource::config()
paul@65 41
{
paul@459 42
    return config_PipeOpener;
paul@65 43
}
paul@65 44
paul@65 45
paul@65 46
paul@65 47
/* Pipe opener interface methods. */
paul@65 48
paul@66 49
long PipeOpenerResource::pipe(offset_t size, l4_cap_idx_t *reader, l4_cap_idx_t *writer)
paul@65 50
{
paul@65 51
    /* Both endpoints will employ a common paging coordinator. */
paul@65 52
paul@300 53
    PipePaging *paging;
paul@300 54
paul@300 55
    try
paul@300 56
    {
paul@300 57
        paging = new PipePaging(_memory, size);
paul@300 58
    }
paul@300 59
    catch (L4::Out_of_memory const &e)
paul@300 60
    {
paul@300 61
        return -L4_ENOMEM;
paul@300 62
    }
paul@65 63
 
paul@65 64
    /* Each endpoint will have its own pager. */
paul@65 65
paul@161 66
    long err = open_endpoint(paging, true, writer);
paul@161 67
paul@161 68
    if (err)
paul@161 69
        return err;
paul@161 70
paul@161 71
    err = open_endpoint(paging, false, reader);
paul@65 72
paul@161 73
    /* Failure to open an endpoint should invalidate both, plus the paging
paul@161 74
       object. Also, any active server thread would need to be cancelled. The
paul@161 75
       freeing of the writer capability attempts to do this. */
paul@161 76
paul@161 77
    if (err)
paul@161 78
    {
paul@161 79
        ipc_cap_free_um(*writer);
paul@161 80
        return err;
paul@161 81
    }
paul@161 82
paul@161 83
    return L4_EOK;
paul@65 84
}
paul@65 85
paul@120 86
/* Start the endpoint server in a new thread. If the thread does not start, the
paul@120 87
   resource (including pager) will be finalised. */
paul@120 88
paul@66 89
long PipeOpenerResource::open_endpoint(PipePaging *paging, bool writing, l4_cap_idx_t *endpoint)
paul@65 90
{
paul@66 91
    PipePager *pager = new PipePager(paging, writing);
paul@65 92
paul@120 93
    return ResourceServer(pager).start_thread(endpoint);
paul@65 94
}
paul@65 95
paul@65 96
// vim: tabstop=4 expandtab shiftwidth=4