summaryrefslogtreecommitdiff
path: root/fs/fd.cc
blob: 5fc8ab0e4f627cba06f28d42166fb83b04ebd0ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

/*
 * fd.cc — File descriptor table management
 */

#include <lume/fdtable.h>
#include <lume/file.h>
#include <lume/slab.h>
#include <lume/errno.h>

/* ================================================================== */
/*  Bitmap helpers                                                    */
/* ================================================================== */

static inline bool bit_test(const uint64* bmap, int bit) {
    return (bmap[bit / 64] >> (bit % 64)) & 1;
}
static inline void bit_set(uint64* bmap, int bit) {
    bmap[bit / 64] |= (1ULL << (bit % 64));
}
static inline void bit_clear(uint64* bmap, int bit) {
    bmap[bit / 64] &= ~(1ULL << (bit % 64));
}

/* ================================================================== */
/*  alloc_files_struct                                                */
/* ================================================================== */

files_struct* alloc_files_struct()
{
    auto* files = new files_struct{};
    if (!files) return nullptr;

    files->count.store(1);
    files->next_fd = 0;
    files->max_fds = NR_OPEN_DEFAULT;

    for (int i = 0; i < NR_OPEN_DEFAULT; i++)
        files->fd_array[i] = nullptr;

    int bmap_words = (NR_OPEN_DEFAULT + 63) / 64;
    for (int i = 0; i < bmap_words; i++) {
        files->close_on_exec_init[i] = 0;
        files->open_fds_init[i] = 0;
    }

    return files;
}

/* ================================================================== */
/*  put_files_struct — drop reference, free on last                   */
/* ================================================================== */

void put_files_struct(files_struct* files)
{
    if (!files) return;

    int old = files->count.fetch_sub(1);
    if (old > 1) return;

    /* Close all open fds */
    for (int i = 0; i < files->max_fds; i++) {
        if (files->fd_array[i]) {
            fput(files->fd_array[i]);
            files->fd_array[i] = nullptr;
        }
    }

    delete files;
}

/* ================================================================== */
/*  dup_fd — duplicate entire fd table (for fork)                     */
/* ================================================================== */

files_struct* dup_fd(files_struct* old_files)
{
    auto* new_files = alloc_files_struct();
    if (!new_files) return nullptr;

    LockGuard guard(old_files->file_lock);

    for (int i = 0; i < old_files->max_fds; i++) {
        file* fp = old_files->fd_array[i];
        if (fp) {
            get_file(fp);
            new_files->fd_array[i] = fp;
        }
    }

    /* Copy bitmaps */
    int bmap_words = (NR_OPEN_DEFAULT + 63) / 64;
    for (int i = 0; i < bmap_words; i++) {
        new_files->open_fds_init[i] = old_files->open_fds_init[i];
        new_files->close_on_exec_init[i] = old_files->close_on_exec_init[i];
    }

    new_files->next_fd = old_files->next_fd;

    return new_files;
}

/* ================================================================== */
/*  alloc_fd — find lowest free fd >= start                           */
/* ================================================================== */

int alloc_fd(files_struct* files, unsigned int start)
{
    LockGuard guard(files->file_lock);

    for (int fd = static_cast<int>(start); fd < files->max_fds; fd++) {
        if (!bit_test(files->open_fds_init, fd)) {
            bit_set(files->open_fds_init, fd);
            files->fd_array[fd] = nullptr;  /* placeholder, fd_install sets real file */
            if (fd >= files->next_fd)
                files->next_fd = fd + 1;
            return fd;
        }
    }

    return -EMFILE;
}

/* ================================================================== */
/*  fd_install — place a file* at a given fd                          */
/* ================================================================== */

void fd_install(files_struct* files, int fd, file* fp)
{
    LockGuard guard(files->file_lock);
    files->fd_array[fd] = fp;
}

/* ================================================================== */
/*  fcheck — look up file by fd (no refcount bump)                    */
/* ================================================================== */

file* fcheck(files_struct* files, int fd)
{
    if (fd < 0 || fd >= files->max_fds) return nullptr;
    return files->fd_array[fd];
}

/* ================================================================== */
/*  close_fd                                                          */
/* ================================================================== */

int close_fd(files_struct* files, int fd)
{
    file* fp;
    {
        LockGuard guard(files->file_lock);
        if (fd < 0 || fd >= files->max_fds) return -EBADF;

        fp = files->fd_array[fd];
        if (!fp) return -EBADF;

        files->fd_array[fd] = nullptr;
        bit_clear(files->open_fds_init, fd);
        bit_clear(files->close_on_exec_init, fd);
    }

    fput(fp);
    return 0;
}

/* ================================================================== */
/*  do_dup2 — duplicate oldfd to newfd                                */
/* ================================================================== */

int do_dup2(files_struct* files, int oldfd, int newfd, int flags)
{
    if (oldfd == newfd) return -EINVAL;

    file* existing = nullptr;
    {
        LockGuard guard(files->file_lock);

        if (oldfd < 0 || oldfd >= files->max_fds) return -EBADF;
        if (newfd < 0 || newfd >= files->max_fds) return -EBADF;

        file* old_fp = files->fd_array[oldfd];
        if (!old_fp) return -EBADF;

        existing = files->fd_array[newfd];

        get_file(old_fp);
        files->fd_array[newfd] = old_fp;
        bit_set(files->open_fds_init, newfd);

        if (flags & O_CLOEXEC)
            bit_set(files->close_on_exec_init, newfd);
        else
            bit_clear(files->close_on_exec_init, newfd);
    }

    if (existing)
        fput(existing);

    return newfd;
}

/* ================================================================== */
/*  do_close_on_exec — close all fds with CLOEXEC set                 */
/* ================================================================== */

void do_close_on_exec(files_struct* files)
{
    for (int fd = 0; fd < files->max_fds; fd++) {
        if (bit_test(files->close_on_exec_init, fd)) {
            close_fd(files, fd);
        }
    }
}