xref: /illumos-gate/usr/src/test/zfs-tests/cmd/largest_file/largest_file.c (revision 1ed6b69a5ca1ca3ee5e9a4931f74e2237c7e1c9f)
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  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright (c) 2012 by Delphix. All rights reserved.
29  */
30 
31 #include "../file_common.h"
32 #include <sys/param.h>
33 #include <signal.h>
34 #include <stdio.h>
35 
36 /*
37  * --------------------------------------------------------------
38  *
39  *	Assertion:
40  *		The last byte of the largest file size can be
41  *		accessed without any errors.  Also, the writing
42  *		beyond the last byte of the largest file size
43  *		will produce an errno of EFBIG.
44  *
45  * --------------------------------------------------------------
46  *	If the write() system call below returns a "1",
47  *	then the last byte can be accessed.
48  * --------------------------------------------------------------
49  */
50 static void	sigxfsz(int);
51 static void	usage(char *);
52 
53 int
54 main(int argc, char **argv)
55 {
56 	int		fd = 0;
57 	offset_t	offset = (MAXOFFSET_T - 1);
58 	offset_t	llseek_ret = 0;
59 	int		write_ret = 0;
60 	int		err = 0;
61 	char		mybuf[5];
62 	char		*testfile;
63 	mode_t		mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
64 
65 	if (argc != 2) {
66 		usage(argv[0]);
67 	}
68 
69 	(void) sigset(SIGXFSZ, sigxfsz);
70 
71 	testfile = strdup(argv[1]);
72 
73 	fd = open(testfile, O_CREAT | O_RDWR, mode);
74 	if (fd < 0) {
75 		perror("Failed to create testfile");
76 		err = errno;
77 		goto out;
78 	}
79 
80 	llseek_ret = llseek(fd, offset, SEEK_SET);
81 	if (llseek_ret < 0) {
82 		perror("Failed to seek to end of testfile");
83 		err = errno;
84 		goto out;
85 	}
86 
87 	write_ret = write(fd, mybuf, 1);
88 	if (write_ret < 0) {
89 		perror("Failed to write to end of file");
90 		err = errno;
91 		goto out;
92 	}
93 
94 	offset = 0;
95 	llseek_ret = llseek(fd, offset, SEEK_CUR);
96 	if (llseek_ret < 0) {
97 		perror("Failed to seek to end of file");
98 		err = errno;
99 		goto out;
100 	}
101 
102 	write_ret = write(fd, mybuf, 1);
103 	if (write_ret < 0) {
104 		if (errno == EFBIG) {
105 			(void) printf("write errno=EFBIG: success\n");
106 			err = 0;
107 		} else {
108 			perror("Did not receive EFBIG");
109 			err = errno;
110 		}
111 	} else {
112 		(void) printf("write completed successfully, test failed\n");
113 		err = 1;
114 	}
115 
116 out:
117 	(void) unlink(testfile);
118 	free(testfile);
119 	return (err);
120 }
121 
122 static void
123 usage(char *name)
124 {
125 	(void) printf("%s <testfile>\n", name);
126 	exit(1);
127 }
128 
129 /* ARGSUSED */
130 static void
131 sigxfsz(int signo)
132 {
133 	(void) printf("\nlargest_file: sigxfsz() caught SIGXFSZ\n");
134 }
135