xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/mmap_exec.c (revision 086ce467adec42d58414fdb4d54c2b6819cf0c07)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*
29  * Copyright (c) 2013 by Delphix. All rights reserved.
30  */
31 
32 #include <stdio.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <sys/types.h>
36 #include <sys/mman.h>
37 #include <errno.h>
38 
39 int
40 main(int argc, char *argv[])
41 {
42 	int error, fd;
43 	struct stat statbuf;
44 
45 	if (argc != 2) {
46 		(void) printf("Error: missing binary name.\n");
47 		(void) printf("Usage:\n\t%s <binary name>\n",
48 		    argv[0]);
49 		return (1);
50 	}
51 
52 	errno = 0;
53 
54 	if ((fd = open(argv[1], O_RDONLY)) < 0) {
55 		error = errno;
56 		perror("open");
57 		return (error);
58 	}
59 	if (fstat(fd, &statbuf) < 0) {
60 		error = errno;
61 		perror("fstat");
62 		return (error);
63 	}
64 
65 	if (mmap(0, statbuf.st_size,
66 	    PROT_EXEC, MAP_SHARED, fd, 0) == MAP_FAILED) {
67 		error = errno;
68 		perror("mmap");
69 		return (error);
70 	}
71 
72 	return (0);
73 }
74