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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2016 Lawrence Livermore National Security, LLC.
 */

/*
 * An extended attribute (xattr) correctness test.  This program creates
 * N files and sets M attrs on them of size S.  Optionally is will verify
 * a pattern stored in the xattr.
 */
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <sys/xattr.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <linux/limits.h>

extern char *program_invocation_short_name;

#define	ERROR(fmt, ...)                                                 \
	fprintf(stderr, "%s: %s:%d: %s: " fmt "\n",                     \
		program_invocation_short_name, __FILE__, __LINE__,      \
		__func__, ## __VA_ARGS__);

static const char shortopts[] = "hvycdn:f:x:s:p:t:e:rRko:";
static const struct option longopts[] = {
	{ "help",		no_argument,		0,	'h' },
	{ "verbose",		no_argument,		0,	'v' },
	{ "verify",		no_argument,		0,	'y' },
	{ "nth",		required_argument,	0,	'n' },
	{ "files",		required_argument,	0,	'f' },
	{ "xattrs",		required_argument,	0,	'x' },
	{ "size",		required_argument,	0,	's' },
	{ "path",		required_argument,	0,	'p' },
	{ "synccaches", 	no_argument,		0,	'c' },
	{ "dropcaches",		no_argument,		0,	'd' },
	{ "script",		required_argument,	0,	't' },
	{ "seed",		required_argument,	0,	'e' },
	{ "random",		no_argument,		0,	'r' },
	{ "randomvalue",	no_argument,		0,	'R' },
	{ "keep",		no_argument,		0,	'k' },
	{ "only",		required_argument,	0,	'o' },
	{ 0,			0,			0,	0   }
};

enum phases {
	PHASE_ALL = 0,
	PHASE_CREATE,
	PHASE_SETXATTR,
	PHASE_GETXATTR,
	PHASE_UNLINK,
	PHASE_INVAL
};

static int verbose = 0;
static int verify = 0;
static int synccaches = 0;
static int dropcaches = 0;
static int nth = 0;
static int files = 1000;
static int xattrs = 1;
static int size = 6;
static int size_is_random = 0;
static int value_is_random = 0;
static int keep_files = 0;
static int phase = PHASE_ALL;
static char path[PATH_MAX] = "/tmp/xattrtest";
static char script[PATH_MAX] = "/bin/true";
static char xattrbytes[XATTR_SIZE_MAX];

static int
usage(int argc, char **argv)
{
	fprintf(stderr,
	    "usage: %s [-hvycdrRk] [-n <nth>] [-f <files>] [-x <xattrs>]\n"
	    "       [-s <bytes>] [-p <path>] [-t <script> ] [-o <phase>]\n",
	    argv[0]);

	fprintf(stderr,
	    "  --help        -h           This help\n"
	    "  --verbose     -v           Increase verbosity\n"
	    "  --verify      -y           Verify xattr contents\n"
	    "  --nth         -n <nth>     Print every nth file\n"
	    "  --files       -f <files>   Set xattrs on N files\n"
	    "  --xattrs      -x <xattrs>  Set N xattrs on each file\n"
	    "  --size        -s <bytes>   Set N bytes per xattr\n"
	    "  --path        -p <path>    Path to files\n"
	    "  --synccaches  -c           Sync caches between phases\n"
	    "  --dropcaches  -d           Drop caches between phases\n"
	    "  --script      -t <script>  Exec script between phases\n"
	    "  --seed        -e <seed>    Random seed value\n"
	    "  --random      -r           Randomly sized xattrs [16-size]\n"
	    "  --randomvalue -R           Random xattr values\n"
	    "  --keep        -k           Don't unlink files\n"
	    "  --only        -o <num>     Only run phase N\n"
	    "                             0=all, 1=create, 2=setxattr,\n"
	    "                             3=getxattr, 4=unlink\n\n");

	return (1);
}

static int
parse_args(int argc, char **argv)
{
	long seed = time(NULL);
	int c;
	int rc = 0;

	while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
		switch (c) {
		case 'h':
			return (usage(argc, argv));
		case 'v':
			verbose++;
			break;
		case 'y':
			verify = 1;
			break;
		case 'n':
			nth = strtol(optarg, NULL, 0);
			break;
		case 'f':
			files = strtol(optarg, NULL, 0);
			break;
		case 'x':
			xattrs = strtol(optarg, NULL, 0);
			break;
		case 's':
			size = strtol(optarg, NULL, 0);
			if (size > XATTR_SIZE_MAX) {
				fprintf(stderr, "Error: the -s value may not "
				    "be greater than %d\n", XATTR_SIZE_MAX);
				rc = 1;
			}
			break;
		case 'p':
			strncpy(path, optarg, PATH_MAX);
			path[PATH_MAX - 1] = '\0';
			break;
		case 'c':
			synccaches = 1;
			break;
		case 'd':
			dropcaches = 1;
			break;
		case 't':
			strncpy(script, optarg, PATH_MAX);
			script[PATH_MAX - 1] = '\0';
			break;
		case 'e':
			seed = strtol(optarg, NULL, 0);
			break;
		case 'r':
			size_is_random = 1;
			break;
		case 'R':
			value_is_random = 1;
			break;
		case 'k':
			keep_files = 1;
			break;
		case 'o':
			phase = strtol(optarg, NULL, 0);
			if (phase <= PHASE_ALL || phase >= PHASE_INVAL) {
				fprintf(stderr, "Error: the -o value must be "
				    "greater than %d and less than %d\n",
				    PHASE_ALL, PHASE_INVAL);
				rc = 1;
			}
			break;
		default:
			rc = 1;
			break;
		}
	}

	if (rc != 0)
		return (rc);

	srandom(seed);

	if (verbose) {
		fprintf(stdout, "verbose:          %d\n", verbose);
		fprintf(stdout, "verify:           %d\n", verify);
		fprintf(stdout, "nth:              %d\n", nth);
		fprintf(stdout, "files:            %d\n", files);
		fprintf(stdout, "xattrs:           %d\n", xattrs);
		fprintf(stdout, "size:             %d\n", size);
		fprintf(stdout, "path:             %s\n", path);
		fprintf(stdout, "synccaches:       %d\n", synccaches);
		fprintf(stdout, "dropcaches:       %d\n", dropcaches);
		fprintf(stdout, "script:           %s\n", script);
		fprintf(stdout, "seed:             %ld\n", seed);
		fprintf(stdout, "random size:      %d\n", size_is_random);
		fprintf(stdout, "random value:     %d\n", value_is_random);
		fprintf(stdout, "keep:             %d\n", keep_files);
		fprintf(stdout, "only:             %d\n", phase);
		fprintf(stdout, "%s", "\n");
	}

	return (rc);
}

static int
drop_caches(void)
{
	char file[] = "/proc/sys/vm/drop_caches";
	int fd, rc;

	fd = open(file, O_WRONLY);
	if (fd == -1) {
		ERROR("Error %d: open(\"%s\", O_WRONLY)\n", errno, file);
		return (errno);
	}

	rc = write(fd, "3", 1);
	if ((rc == -1) || (rc != 1)) {
		ERROR("Error %d: write(%d, \"3\", 1)\n", errno, fd);
		(void) close(fd);
		return (errno);
	}

	rc = close(fd);
	if (rc == -1) {
		ERROR("Error %d: close(%d)\n", errno, fd);
		return (errno);
	}

	return (0);
}

static int
run_process(const char *path, char *argv[])
{
	pid_t pid;
	int rc, devnull_fd;

	pid = vfork();
	if (pid == 0) {
		devnull_fd = open("/dev/null", O_WRONLY);

		if (devnull_fd < 0)
			_exit(-1);

		(void) dup2(devnull_fd, STDOUT_FILENO);
		(void) dup2(devnull_fd, STDERR_FILENO);
		close(devnull_fd);

		(void) execvp(path, argv);
		_exit(-1);
	} else if (pid > 0) {
		int status;

		while ((rc = waitpid(pid, &status, 0)) == -1 &&
		    errno == EINTR) { }

		if (rc < 0 || !WIFEXITED(status))
			return (-1);

		return (WEXITSTATUS(status));
	}

	return (-1);
}

static int
post_hook(char *phase)
{
	char *argv[3] = { script, phase, (char *)0 };
	int rc;

	if (synccaches)
		sync();

	if (dropcaches) {
		rc = drop_caches();
		if (rc)
			return (rc);
	}

	rc = run_process(script, argv);
	if (rc)
		return (rc);

	return (0);
}

#define	USEC_PER_SEC	1000000

static void
timeval_normalize(struct timeval *tv, time_t sec, suseconds_t usec)
{
	while (usec >= USEC_PER_SEC) {
		usec -= USEC_PER_SEC;
		sec++;
	}

	while (usec < 0) {
		usec += USEC_PER_SEC;
		sec--;
	}

	tv->tv_sec = sec;
	tv->tv_usec = usec;
}

static void
timeval_sub(struct timeval *delta, struct timeval *tv1, struct timeval *tv2)
{
	timeval_normalize(delta,
	    tv1->tv_sec - tv2->tv_sec,
	    tv1->tv_usec - tv2->tv_usec);
}

static double
timeval_sub_seconds(struct timeval *tv1, struct timeval *tv2)
{
	struct timeval delta;

	timeval_sub(&delta, tv1, tv2);
	return ((double)delta.tv_usec / USEC_PER_SEC + delta.tv_sec);
}

static int
create_files(void)
{
	int i, rc;
	char *file = NULL;
	struct timeval start, stop;
	double seconds;
	size_t fsize;

	fsize = PATH_MAX;
	file = malloc(fsize);
	if (file == NULL) {
		rc = ENOMEM;
		ERROR("Error %d: malloc(%d) bytes for file name\n", rc,
		    PATH_MAX);
		goto out;
	}

	(void) gettimeofday(&start, NULL);

	for (i = 1; i <= files; i++) {
		if (snprintf(file, fsize, "%s/file-%d", path, i) >= fsize) {
			rc = EINVAL;
			ERROR("Error %d: path too long\n", rc);
			goto out;
		}

		if (nth && ((i % nth) == 0))
			fprintf(stdout, "create: %s\n", file);

		rc = unlink(file);
		if ((rc == -1) && (errno != ENOENT)) {
			ERROR("Error %d: unlink(%s)\n", errno, file);
			rc = errno;
			goto out;
		}

		rc = open(file, O_CREAT, 0644);
		if (rc == -1) {
			ERROR("Error %d: open(%s, O_CREATE, 0644)\n",
			    errno, file);
			rc = errno;
			goto out;
		}

		rc = close(rc);
		if (rc == -1) {
			ERROR("Error %d: close(%d)\n", errno, rc);
			rc = errno;
			goto out;
		}
	}

	(void) gettimeofday(&stop, NULL);
	seconds = timeval_sub_seconds(&stop, &start);
	fprintf(stdout, "create:   %f seconds %f creates/second\n",
	    seconds, files / seconds);

	rc = post_hook("post");
out:
	if (file)
		free(file);

	return (rc);
}

static int
get_random_bytes(char *buf, size_t bytes)
{
	int rand;
	ssize_t bytes_read = 0;

	rand = open("/dev/urandom", O_RDONLY);

	if (rand < 0)
		return (rand);

	while (bytes_read < bytes) {
		ssize_t rc = read(rand, buf + bytes_read, bytes - bytes_read);
		if (rc < 0)
			break;
		bytes_read += rc;
	}

	(void) close(rand);

	return (bytes_read);
}

static int
setxattrs(void)
{
	int i, j, rnd_size = size, shift, rc = 0;
	char name[XATTR_NAME_MAX];
	char *value = NULL;
	char *file = NULL;
	struct timeval start, stop;
	double seconds;
	size_t fsize;

	value = malloc(XATTR_SIZE_MAX);
	if (value == NULL) {
		rc = ENOMEM;
		ERROR("Error %d: malloc(%d) bytes for xattr value\n", rc,
		    XATTR_SIZE_MAX);
		goto out;
	}

	fsize = PATH_MAX;
	file = malloc(fsize);
	if (file == NULL) {
		rc = ENOMEM;
		ERROR("Error %d: malloc(%d) bytes for file name\n", rc,
		    PATH_MAX);
		goto out;
	}

	(void) gettimeofday(&start, NULL);

	for (i = 1; i <= files; i++) {
		if (snprintf(file, fsize, "%s/file-%d", path, i) >= fsize) {
			rc = EINVAL;
			ERROR("Error %d: path too long\n", rc);
			goto out;
		}

		if (nth && ((i % nth) == 0))
			fprintf(stdout, "setxattr: %s\n", file);

		for (j = 1; j <= xattrs; j++) {
			if (size_is_random)
				rnd_size = (random() % (size - 16)) + 16;

			(void) sprintf(name, "user.%d", j);
			shift = sprintf(value, "size=%d ", rnd_size);
			memcpy(value + shift, xattrbytes,
			    sizeof (xattrbytes) - shift);

			rc = lsetxattr(file, name, value, rnd_size, 0);
			if (rc == -1) {
				ERROR("Error %d: lsetxattr(%s, %s, ..., %d)\n",
				    errno, file, name, rnd_size);
				goto out;
			}
		}
	}

	(void) gettimeofday(&stop, NULL);
	seconds = timeval_sub_seconds(&stop, &start);
	fprintf(stdout, "setxattr: %f seconds %f setxattrs/second\n",
	    seconds, (files * xattrs) / seconds);

	rc = post_hook("post");
out:
	if (file)
		free(file);

	if (value)
		free(value);

	return (rc);
}

static int
getxattrs(void)
{
	int i, j, rnd_size, shift, rc = 0;
	char name[XATTR_NAME_MAX];
	char *verify_value = NULL;
	char *verify_string;
	char *value = NULL;
	char *value_string;
	char *file = NULL;
	struct timeval start, stop;
	double seconds;
	size_t fsize;

	verify_value = malloc(XATTR_SIZE_MAX);
	if (verify_value == NULL) {
		rc = ENOMEM;
		ERROR("Error %d: malloc(%d) bytes for xattr verify\n", rc,
		    XATTR_SIZE_MAX);
		goto out;
	}

	value = malloc(XATTR_SIZE_MAX);
	if (value == NULL) {
		rc = ENOMEM;
		ERROR("Error %d: malloc(%d) bytes for xattr value\n", rc,
		    XATTR_SIZE_MAX);
		goto out;
	}

	verify_string = value_is_random ? "<random>" : verify_value;
	value_string = value_is_random ? "<random>" : value;

	fsize = PATH_MAX;
	file = malloc(fsize);

	if (file == NULL) {
		rc = ENOMEM;
		ERROR("Error %d: malloc(%d) bytes for file name\n", rc,
		    PATH_MAX);
		goto out;
	}

	(void) gettimeofday(&start, NULL);

	for (i = 1; i <= files; i++) {
		if (snprintf(file, fsize, "%s/file-%d", path, i) >= fsize) {
			rc = EINVAL;
			ERROR("Error %d: path too long\n", rc);
			goto out;
		}

		if (nth && ((i % nth) == 0))
			fprintf(stdout, "getxattr: %s\n", file);

		for (j = 1; j <= xattrs; j++) {
			(void) sprintf(name, "user.%d", j);

			rc = lgetxattr(file, name, value, XATTR_SIZE_MAX);
			if (rc == -1) {
				ERROR("Error %d: lgetxattr(%s, %s, ..., %d)\n",
				    errno, file, name, XATTR_SIZE_MAX);
				goto out;
			}

			if (!verify)
				continue;

			sscanf(value, "size=%d [a-z]", &rnd_size);
			shift = sprintf(verify_value, "size=%d ",
			    rnd_size);
			memcpy(verify_value + shift, xattrbytes,
			    sizeof (xattrbytes) - shift);

			if (rnd_size != rc ||
			    memcmp(verify_value, value, rnd_size)) {
				ERROR("Error %d: verify failed\n "
				    "verify: %s\n value:  %s\n", EINVAL,
				    verify_string, value_string);
				rc = 1;
				goto out;
			}
		}
	}

	(void) gettimeofday(&stop, NULL);
	seconds = timeval_sub_seconds(&stop, &start);
	fprintf(stdout, "getxattr: %f seconds %f getxattrs/second\n",
	    seconds, (files * xattrs) / seconds);

	rc = post_hook("post");
out:
	if (file)
		free(file);

	if (value)
		free(value);

	if (verify_value)
		free(verify_value);

	return (rc);
}

static int
unlink_files(void)
{
	int i, rc;
	char *file = NULL;
	struct timeval start, stop;
	double seconds;
	size_t fsize;

	fsize = PATH_MAX;
	file = malloc(fsize);
	if (file == NULL) {
		rc = ENOMEM;
		ERROR("Error %d: malloc(%d) bytes for file name\n",
		    rc, PATH_MAX);
		goto out;
	}

	(void) gettimeofday(&start, NULL);

	for (i = 1; i <= files; i++) {
		if (snprintf(file, fsize, "%s/file-%d", path, i) >= fsize) {
			rc = EINVAL;
			ERROR("Error %d: path too long\n", rc);
			goto out;
		}

		if (nth && ((i % nth) == 0))
			fprintf(stdout, "unlink: %s\n", file);

		rc = unlink(file);
		if ((rc == -1) && (errno != ENOENT)) {
			ERROR("Error %d: unlink(%s)\n", errno, file);
			free(file);
			return (errno);
		}
	}

	(void) gettimeofday(&stop, NULL);
	seconds = timeval_sub_seconds(&stop, &start);
	fprintf(stdout, "unlink:   %f seconds %f unlinks/second\n",
	    seconds, files / seconds);

	rc = post_hook("post");
out:
	if (file)
		free(file);

	return (rc);
}

int
main(int argc, char **argv)
{
	int rc;

	rc = parse_args(argc, argv);
	if (rc)
		return (rc);

	if (value_is_random) {
		size_t rndsz = sizeof (xattrbytes);

		rc = get_random_bytes(xattrbytes, rndsz);
		if (rc < rndsz) {
			ERROR("Error %d: get_random_bytes() wanted %zd "
			    "got %d\n", errno, rndsz, rc);
			return (rc);
		}
	} else {
		memset(xattrbytes, 'x', sizeof (xattrbytes));
	}

	if (phase == PHASE_ALL || phase == PHASE_CREATE) {
		rc = create_files();
		if (rc)
			return (rc);
	}

	if (phase == PHASE_ALL || phase == PHASE_SETXATTR) {
		rc = setxattrs();
		if (rc)
			return (rc);
	}

	if (phase == PHASE_ALL || phase == PHASE_GETXATTR) {
		rc = getxattrs();
		if (rc)
			return (rc);
	}

	if (!keep_files && (phase == PHASE_ALL || phase == PHASE_UNLINK)) {
		rc = unlink_files();
		if (rc)
			return (rc);
	}

	return (0);
}