Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*	$NetBSD: fstest_puffs.c,v 1.11 2013/09/09 19:47:38 pooka Exp $	*/

/*
 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/statvfs.h>
#include <sys/wait.h>

#include <assert.h>
#include <atf-c.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <puffs.h>
#include <puffsdump.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include <rump/rump.h>
#include <rump/rump_syscalls.h>

#include "h_fsmacros.h"

#define BUFSIZE (128*1024)
#define DTFS_DUMP "-o","dump"

static bool mayquit = false;

static ssize_t
xread(int fd, void *vp, size_t n)
{
	size_t left;

	left = n;
	do {
		ssize_t ssz;

		ssz = read(fd, vp, left);
		if (ssz == -1) {
			return ssz;
		}
		left -= ssz;
		vp = (char *)vp + ssz;
	} while (left > 0);
	return n;
}

static ssize_t
xwrite(int fd, const void *vp, size_t n)
{
	size_t left;

	left = n;
	do {
		ssize_t ssz;

		ssz = write(fd, vp, left);
		if (ssz == -1) {
			return ssz;
		}
		left -= ssz;
		vp = (const char *)vp + ssz;
	} while (left > 0);
	return n;
}

/*
 * Threads which shovel data between comfd and /dev/puffs.
 * (cannot use polling since fd's are in different namespaces)
 */
static void *
readshovel(void *arg)
{
	struct putter_hdr *phdr;
	struct puffs_req *preq;
	struct puffstestargs *args = arg;
	char buf[BUFSIZE];
	ssize_t n;
	int comfd, puffsfd;

	comfd = args->pta_servfd;
	puffsfd = args->pta_rumpfd;

	phdr = (void *)buf;
	preq = (void *)buf;

	rump_pub_lwproc_newlwp(1);

	for (;;) {
		n = rump_sys_read(puffsfd, buf, sizeof(*phdr));
		if (n <= 0) {
			fprintf(stderr, "readshovel r1 %zd / %d\n", n, errno);
			break;
		}

		assert(phdr->pth_framelen < BUFSIZE);
		n = rump_sys_read(puffsfd, buf+sizeof(*phdr), 
		    phdr->pth_framelen - sizeof(*phdr));
		if (n <= 0) {
			fprintf(stderr, "readshovel r2 %zd / %d\n", n, errno);
			break;
		}

		/* Analyze request */
		if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VFS) {
			assert(preq->preq_optype < PUFFS_VFS_MAX);
			args->pta_vfs_toserv_ops[preq->preq_optype]++;
		} else if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN) {
			assert(preq->preq_optype < PUFFS_VN_MAX);
			args->pta_vn_toserv_ops[preq->preq_optype]++;
		}

		n = phdr->pth_framelen;
		if (xwrite(comfd, buf, n) != n) {
			fprintf(stderr, "readshovel write %zd / %d\n", n, errno);
			break;
		}
	}

	if (n != 0 && mayquit == false)
		abort();
	return NULL;
}

static void *
writeshovel(void *arg)
{
	struct puffstestargs *args = arg;
	struct putter_hdr *phdr;
	struct puffs_req *preq;
	char buf[BUFSIZE];
	size_t toread;
	ssize_t n;
	int comfd, puffsfd;

	rump_pub_lwproc_newlwp(1);

	comfd = args->pta_servfd;
	puffsfd = args->pta_rumpfd;

	phdr = (struct putter_hdr *)buf;
	preq = (void *)buf;

	for (;;) {
		uint64_t off;

		/*
		 * Need to write everything to the "kernel" in one chunk,
		 * so make sure we have it here.
		 */
		off = 0;
		toread = sizeof(struct putter_hdr);
		assert(toread < BUFSIZE);
		do {
			n = xread(comfd, buf+off, toread);
			if (n <= 0) {
				fprintf(stderr, "writeshovel read %zd / %d\n",
				    n, errno);
				goto out;
			}
			off += n;
			if (off >= sizeof(struct putter_hdr))
				toread = phdr->pth_framelen - off;
			else
				toread = off - sizeof(struct putter_hdr);
		} while (toread);

		if (__predict_false(
		    PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VFS
		    && preq->preq_optype == PUFFS_VFS_UNMOUNT)) {
			if (preq->preq_rv == 0)
				mayquit = true;
		}

		n = rump_sys_write(puffsfd, buf, phdr->pth_framelen);
		if ((size_t)n != phdr->pth_framelen) {
			fprintf(stderr, "writeshovel wr %zd / %d\n", n, errno);
			break;
		}
	}

 out:
	if (n != 0)
		abort();
	return NULL;
}

static void
rumpshovels(struct puffstestargs *args)
{
	pthread_t pt;
	int rv;

	if ((rv = rump_init()) == -1)
		err(1, "rump_init");

	if (pthread_create(&pt, NULL, readshovel, args) == -1)
		err(1, "read shovel");
	pthread_detach(pt);

	if (pthread_create(&pt, NULL, writeshovel, args) == -1)
		err(1, "write shovel");
	pthread_detach(pt);
}

static void
childfail(int sign)
{

	atf_tc_fail("child died"); /* almost signal-safe */
}

struct puffstestargs *theargs; /* XXX */

/* XXX: we don't support size */
static int
donewfs(const atf_tc_t *tc, void **argp,
	const char *image, off_t size, void *fspriv, char **theargv)
{
	struct puffstestargs *args;
	pid_t childpid;
	int *pflags;
	char comfd[16];
	int sv[2];
	int mntflags;
	size_t len;
	ssize_t n;

	*argp = NULL;

	args = malloc(sizeof(*args));
	if (args == NULL)
		return errno;
	memset(args, 0, sizeof(*args));

	pflags = &args->pta_pflags;

	/* Create sucketpair for communication with the real file server */
	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) == -1)
		return errno;

	signal(SIGCHLD, childfail);

	switch ((childpid = fork())) {
	case 0:
		close(sv[1]);
		snprintf(comfd, sizeof(sv[0]), "%d", sv[0]);
		if (setenv("PUFFS_COMFD", comfd, 1) == -1)
			return errno;

		if (execvp(theargv[0], theargv) == -1)
			return errno;
	case -1:
		return errno;
	default:
		close(sv[0]);
		break;
	}

	/* read args */
	if ((n = xread(sv[1], &len, sizeof(len))) != sizeof(len))
		err(1, "mp 1 %zd", n);
	if (len > MAXPATHLEN)
		err(1, "mntpath > MAXPATHLEN");
	if ((size_t)xread(sv[1], args->pta_dir, len) != len)
		err(1, "mp 2");
	if (xread(sv[1], &len, sizeof(len)) != sizeof(len))
		err(1, "fn 1");
	if (len > MAXPATHLEN)
		err(1, "devpath > MAXPATHLEN");
	if ((size_t)xread(sv[1], args->pta_dev, len) != len)
		err(1, "fn 2");
	if (xread(sv[1], &mntflags, sizeof(mntflags)) != sizeof(mntflags))
		err(1, "mntflags");
	if (xread(sv[1], &args->pta_pargslen, sizeof(args->pta_pargslen))
	    != sizeof(args->pta_pargslen))
		err(1, "puffstest_args len");
	args->pta_pargs = malloc(args->pta_pargslen);
	if (args->pta_pargs == NULL)
		err(1, "malloc");
	if (xread(sv[1], args->pta_pargs, args->pta_pargslen)
	    != (ssize_t)args->pta_pargslen)
		err(1, "puffstest_args");
	if (xread(sv[1], pflags, sizeof(*pflags)) != sizeof(*pflags))
		err(1, "pflags");

	args->pta_childpid = childpid;
	args->pta_servfd = sv[1];
	strlcpy(args->pta_dev, image, sizeof(args->pta_dev));

	*argp = theargs = args;

	return 0;
}

int
puffs_fstest_newfs(const atf_tc_t *tc, void **argp,
	const char *image, off_t size, void *fspriv)
{
	char dtfs_path[MAXPATHLEN];
	char *dtfsargv[6];
	char **theargv;

	/* build dtfs exec path from atf test dir */
	sprintf(dtfs_path, "%s/../puffs/h_dtfs/h_dtfs",
	    atf_tc_get_config_var(tc, "srcdir"));

	if (fspriv) {
		theargv = fspriv;
		theargv[0] = dtfs_path;
	} else {
		dtfsargv[0] = dtfs_path;
		dtfsargv[1] = __UNCONST("-i");
		dtfsargv[2] = __UNCONST("-s");
		dtfsargv[3] = __UNCONST("dtfs");
		dtfsargv[4] = __UNCONST("fictional");
		dtfsargv[5] = NULL;

		theargv = dtfsargv;
	}

	return donewfs(tc, argp, image, size, fspriv, theargv);
}

int
p2k_ffs_fstest_newfs(const atf_tc_t *tc, void **argp,
	const char *image, off_t size, void *fspriv)
{
	char *rumpffs_argv[5];
	int rv;

	rump_init();
	if ((rv = ffs_fstest_newfs(tc, argp, image, size, fspriv)) != 0)
		return rv;
	if (mkdir("p2kffsfake", 0777) == -1 && errno != EEXIST)
		return errno;

	setenv("P2K_NODETACH", "1", 1);
	rumpffs_argv[0] = __UNCONST("rump_ffs");
	rumpffs_argv[1] = __UNCONST(image);
	rumpffs_argv[2] = __UNCONST("p2kffsfake"); /* NOTUSED */
	rumpffs_argv[3] = NULL;

	if ((rv = donewfs(tc, argp, image, size, fspriv, rumpffs_argv)) != 0)
		ffs_fstest_delfs(tc, argp);
	return rv;
}

int
puffs_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
{
	struct puffstestargs *pargs = arg;
	int fd;

	rump_init();
	fd = rump_sys_open("/dev/puffs", O_RDWR);
	if (fd == -1)
		return fd;

	if (rump_sys_mkdir(path, 0777) == -1)
		return -1;

	if (rump_sys_mount(MOUNT_PUFFS, path, flags,
	    pargs->pta_pargs, pargs->pta_pargslen) == -1) {
		/* apply "to kill a child" to avoid atf hang (kludge) */
		kill(pargs->pta_childpid, SIGKILL);
		return -1;
	}

	pargs->pta_rumpfd = fd;
	rumpshovels(pargs);

	return 0;
}
__strong_alias(p2k_ffs_fstest_mount,puffs_fstest_mount);

int
puffs_fstest_delfs(const atf_tc_t *tc, void *arg)
{

	/* useless ... */
	return 0;
}

int
p2k_ffs_fstest_delfs(const atf_tc_t *tc, void *arg)
{

	return ffs_fstest_delfs(tc, arg);
}

int
puffs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
{
	struct puffstestargs *pargs = theargs;
	int status;
	int rv;

	/* ok, child might exit here */
	signal(SIGCHLD, SIG_IGN);

	rv = rump_sys_unmount(path, flags);
	if (rv)	
		return rv;

	if ((rv = rump_sys_rmdir(path)) != 0)
		return rv;

	if (waitpid(pargs->pta_childpid, &status, WNOHANG) > 0)
		return 0;
	kill(pargs->pta_childpid, SIGTERM);
	usleep(10);
	if (waitpid(pargs->pta_childpid, &status, WNOHANG) > 0)
		return 0;
	kill(pargs->pta_childpid, SIGKILL);
	usleep(500);
	wait(&status);

	rmdir("p2kffsfake");

	return 0;
}
__strong_alias(p2k_ffs_fstest_unmount,puffs_fstest_unmount);