aboutsummaryrefslogtreecommitdiff
path: root/src/bin/src/exec.c
blob: 1796af97d972ed885775b20c736667710865f042 (plain) (blame)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * wnix, a Unix-like operating system for WebAssembly applications.
 * Copyright (C) 2025  Xavier Del Campo Romero
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <bin.h>
#include <bin/mod.h>
#include <bin/proc.h>
#include <bin/routines.h>
#include <bin/types.h>
#include <aio.h>
#include <caio.h>
#include <fs/fs.h>
#include <loop.h>
#include <kprintf.h>
#include <dynstr.h>
#include <nanowasm/nw.h>
#include <nanowasm/dbg.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>

struct exec
{
    struct bin_mod *m;
    struct bin_proc *p;
};

static int announced(const enum state state, void *const args)
{
    struct bin_proc *const p = args;
    struct bin_dbg *const d = &p->dbg;
    const struct nw_dbg_cfg cfg = {.inst = &p->instance};

    dynstr_free(&d->dstr);

    if (loop_rm_aio(d->aio))
        goto failure;

    aio_free(d->aio);

    if (state)
        kprintf("failed to announce process to debugger: %s\n", p->path);
    else
        nw_dbg_init(&d->dbg, &cfg);

    return 0;

failure:
    bin_proc_free(p);
    return -1;
}

static int announce(struct bin_proc *const p)
{
    struct aio *aio = NULL;
    struct bin_dbg *const dbg = &p->dbg;
    struct dynstr *const s = &dbg->dstr;
    const struct aio_done d =
    {
        .f = announced,
        .args = p
    };

    dynstr_init(s);

    if (dynstr_append(s, ";hi:%s:%u\n", p->path, (unsigned)p->pid))
        goto failure;

    const struct fs_write w =
    {
        .buf = s->str,
        .n = s->len + 1,
        .fd = &dbg->fd
    };

    if (!(aio = aio_write(&w, &d)) || loop_add_aio(aio))
        goto failure;

    dbg->aio = aio;
    return 0;

failure:
    aio_free(aio);
    return -1;
}

static int mod_opened(const enum state state, void *const args)
{
    struct exec *const e = args;
    struct bin_mod *const m = e->m;
    struct bin_proc *const p = e->p;
    struct nw_mod_cfg cfg = bin_mod_cfg;

    cfg.io.user = m;
    cfg.imp_indexes = m->import_indexes;
    nw_init(&m->mod, &cfg);

    if (state)
        goto failure;
    else if (bin_load(m)
        || bin_proc_init(p)
        || announce(p))
        goto failure;

    return 0;

failure:
    bin_mod_free(m);
    return -1;
}

static int init_mod(struct bin_proc *const p)
{
    struct caio *caio = NULL;
    char *const pathdup = strdup(p->path);
    struct exec *const e = malloc(sizeof *e);
    struct bin_mod *const m = malloc(sizeof *m);
    const struct caio_open op =
    {
        .flags = O_RDONLY,
        .path = p->path,
        .gid = p->gid,
        .uid = p->uid
    };

    const struct aio_done d =
    {
        .f = mod_opened,
        .args = e,
    };

    if (!pathdup || !e || !m || !(caio = caio_open(&op, &d)))
        goto failure;

    *m = (const struct bin_mod)
    {
        .path = pathdup,
        .caio = caio
    };

    *e = (const struct exec)
    {
        .m = m,
        .p = p
    };

    p->mod = m;
    return 0;

failure:
    caio_close(caio);
    free(pathdup);
    free(m);
    free(e);
    return -1;
}

static int header_read(struct bin_proc *const p)
{
    static const char shebang[] = "#! ", wasm[] = {'\0', 'a', 's', 'm'};
    const char *const h = p->header;

    if (!strncmp(h, shebang, strlen(shebang)))
    {
        /* TODO: interpret shebang */
    }
    else if (!memcmp(h, wasm, sizeof wasm))
    {
        if (init_mod(p))
            goto failure;
    }
    else
    {
        kprintf("Invalid file signature: {%hhx, %hhx, %hhx, %hhx}\n",
            h[0], h[1], h[2], h[3]);
        goto failure;
    }

    if (caio_seek(p->caio, 0))
        goto failure;

    return 0;

failure:
    bin_proc_free(p);
    return -1;
}

static int read_header(const enum state state, void *const args)
{
    struct bin_proc *const p = args;

    if (state)
        goto failure;

    const size_t rem = sizeof p->header - p->i;
    const struct aio_done d = {.f = read_header, .args = p};
    const int n = caio_read(p->caio, p->header + p->i, rem, &d);

    if (n < 0)
        goto failure;
    else if ((p->i += n) >= sizeof p->header)
        return header_read(p);

    return 0;

failure:
    bin_proc_free(p);
    return -1;
}

static int opened(const enum state state, void *const args)
{
    struct bin_proc *const p = args;

    if (state)
        goto failure;
    else if ((p->mod = bin_mod(p->path)))
        return bin_proc_init(p);

    p->i = 0;
    return read_header(STATE_OK, p);

failure:
    bin_proc_free(p);
    return -1;
}

static int serial_opened(const enum state state, void *const args)
{
    struct bin_proc *const p = args;
    struct bin_dbg *const dbg = &p->dbg;
    struct caio *caio = NULL;
    const struct aio_done d =
    {
        .f = opened,
        .args = p
    };

    const struct caio_open op =
    {
        .flags = O_RDONLY,
        .path = p->path,
        .uid = p->uid,
        .gid = p->gid
    };

    if (loop_rm_aio(dbg->aio))
        goto failure;

    aio_free(dbg->aio);
    dbg->aio = NULL;

    if (state
        || !(caio = caio_open(&op, &d))
        || caio_set_caches(caio, 3))
        goto failure;

    p->caio = caio;
    return 0;

failure:
    caio_close(caio);
    bin_proc_free(p);
    return -1;
}

static int copy_args(struct bin_proc *const p, const char *const path,
    const char *const *const argv)
{
    int nargs = 2;
    const char *const *parg = argv;
    char **args = NULL;

    while (*parg++)
        nargs++;

    if (!(args = malloc(nargs * sizeof *args)))
        goto failure;

    for (int i = 0; i < nargs; i++)
        args[i] = NULL;

    if (!(*args = strdup(path)))
        goto failure;

    for (int i = 1; i < nargs - 1; i++)
        if (!(args[i] = strdup(*(argv - 1))))
            goto failure;

    p->argc = nargs - 1;
    p->argv = args;
    return 0;

failure:

    if (args)
        for (int i = 0; i < nargs; i++)
            free(args[i]);

    free(args);
    return -1;
}

int bin_exec(const char *const path, const char *const *const argv,
    const struct fs_stdstreams *const ss, const pid_t parent, const uid_t uid,
    const gid_t gid)
{
    const pid_t pid = bin_pid();
    char *pathdup = NULL;
    struct aio *serial = NULL;
    struct bin_proc *const p = malloc(sizeof *p);

    if (!p)
        goto failure;

    struct bin_dbg *const dbg = &p->dbg;

    const struct aio_done d =
    {
        .f = serial_opened,
        .args = p
    };

    const struct fs_open op =
    {
        .path = "/dev/ttyS0",
        .flags = O_RDWR,
        .fd = &dbg->fd,
        .uid = uid,
        .gid = gid
    };

    if (!(pathdup = strdup(path))
        || !(serial = aio_open(&op, &d)))
        goto failure;

    *p = (const struct bin_proc)
    {
        .path = pathdup,
        .dbg.aio = serial,
        .parent = parent,
        .pid = pid,
        .uid = uid,
        .gid = gid,
        .ss = *ss,
        .umask = S_IWGRP | S_IWOTH
    };

    if (copy_args(p, path, argv)
        || loop_add_aio(serial))
        goto failure;

    return 0;

failure:
    aio_free(serial);
    free(pathdup);
    free(p);
    return -1;
}