xref: /freebsd/tests/sys/cddl/zfs/bin/largest_file.c (revision 0957b409)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  * $FreeBSD$
21  */
22 
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"@(#)largest_file.c	1.3	07/05/25 SMI"
29 
30 #include "file_common.h"
31 #include <sys/param.h>
32 #include <signal.h>
33 #include <stdio.h>
34 
35 /*
36  * --------------------------------------------------------------
37  *
38  *	Assertion:
39  *		The last byte of the largest file size can be
40  *		accessed without any errors.  Also, the writing
41  *		beyond the last byte of the largest file size
42  *		will produce an errno of EFBIG.
43  *
44  * --------------------------------------------------------------
45  *	If the write() system call below returns a "1",
46  *	then the last byte can be accessed.
47  * --------------------------------------------------------------
48  */
49 static void	sigxfsz(int);
50 static void	usage(char *);
51 
52 int
53 main(int argc, char **argv)
54 {
55 	int		fd = 0;
56 	off_t		offset = (OFF_MAX - 1);
57 	off_t		lseek_ret = 0;
58 	int		write_ret = 0;
59 	int		err = 0;
60 	char		mybuf[5];
61 	char		*testfile;
62 
63 	if (argc != 2) {
64 		usage(argv[0]);
65 	}
66 
67 	(void) sigset(SIGXFSZ, sigxfsz);
68 
69 	testfile = strdup(argv[1]);
70 
71 	fd = open(testfile, O_CREAT | O_RDWR);
72 	if (fd < 0) {
73 		perror("Failed to create testfile");
74 		err = errno;
75 		goto out;
76 	}
77 
78 	lseek_ret = lseek(fd, offset, SEEK_SET);
79 	if (lseek_ret < 0) {
80 		perror("Failed to seek to end of testfile");
81 		err = errno;
82 		goto out;
83 	}
84 
85 	write_ret = write(fd, mybuf, 1);
86 	if (write_ret < 0) {
87 		perror("Failed to write to end of file");
88 		err = errno;
89 		goto out;
90 	}
91 
92 	offset = 0;
93 	lseek_ret = lseek(fd, offset, SEEK_CUR);
94 	if (lseek_ret < 0) {
95 		perror("Failed to seek to end of file");
96 		err = errno;
97 		goto out;
98 	}
99 
100 	write_ret = write(fd, mybuf, 1);
101 	if (write_ret < 0) {
102 		if (errno == EFBIG) {
103 			(void) printf("write errno=EFBIG: success\n");
104 			err = 0;
105 		} else {
106 			perror("Did not receive EFBIG");
107 			err = errno;
108 		}
109 	} else {
110 		(void) printf("write completed successfully, test failed\n");
111 		err = 1;
112 	}
113 
114 out:
115 	(void) unlink(testfile);
116 	free(testfile);
117 	return (err);
118 }
119 
120 static void
121 usage(char *name)
122 {
123 	(void) printf("%s <testfile>\n", name);
124 	exit(1);
125 }
126 
127 /* ARGSUSED */
128 static void
129 sigxfsz(int signo)
130 {
131 	(void) printf("\nlargest_file: sigxfsz() caught SIGXFSZ\n");
132 }
133