xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/file/largest_file.c (revision b197d4b893974c9eb4d7b38704c6d5c486235d6f)
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 https://opensource.org/licenses/CDDL-1.0.
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 #include <string.h>
36 #include <sys/stdtypes.h>
37 #include <unistd.h>
38 
39 /*
40  * --------------------------------------------------------------
41  *
42  *	Assertion:
43  *		The last byte of the largest file size can be
44  *		accessed without any errors.  Also, the writing
45  *		beyond the last byte of the largest file size
46  *		will produce an errno of EFBIG.
47  *
48  * --------------------------------------------------------------
49  *	If the write() system call below returns a "1",
50  *	then the last byte can be accessed.
51  * --------------------------------------------------------------
52  */
53 static void	sigxfsz(int);
54 static void	usage(char *);
55 
56 int
57 main(int argc, char **argv)
58 {
59 	int		fd = 0;
60 	offset_t	offset = (MAXOFFSET_T - 1);
61 	offset_t	llseek_ret = 0;
62 	int		write_ret = 0;
63 	int		err = 0;
64 	char		mybuf[5] = "aaaa\0";
65 	char		*testfile;
66 	mode_t		mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
67 	struct sigaction sa;
68 
69 	if (argc != 2) {
70 		usage(argv[0]);
71 	}
72 
73 	if (sigemptyset(&sa.sa_mask) == -1)
74 		return (errno);
75 	sa.sa_flags = 0;
76 	sa.sa_handler = sigxfsz;
77 	if (sigaction(SIGXFSZ, &sa, NULL) == -1)
78 		return (errno);
79 
80 	testfile = strdup(argv[1]);
81 
82 	fd = open(testfile, O_CREAT | O_RDWR, mode);
83 	if (fd < 0) {
84 		err = errno;
85 		perror("Failed to create testfile");
86 		free(testfile);
87 		return (err);
88 	}
89 
90 	llseek_ret = lseek64(fd, offset, SEEK_SET);
91 	if (llseek_ret < 0) {
92 		err = errno;
93 		perror("Failed to seek to end of testfile");
94 		goto out;
95 	}
96 
97 	write_ret = write(fd, mybuf, 1);
98 	if (write_ret < 0) {
99 		err = errno;
100 		perror("Failed to write to end of file");
101 		goto out;
102 	}
103 
104 	offset = 0;
105 	llseek_ret = lseek64(fd, offset, SEEK_CUR);
106 	if (llseek_ret < 0) {
107 		err = errno;
108 		perror("Failed to seek to end of file");
109 		goto out;
110 	}
111 
112 	write_ret = write(fd, mybuf, 1);
113 	if (write_ret < 0) {
114 		if (errno == EFBIG || errno == EINVAL) {
115 			(void) printf("write errno=EFBIG|EINVAL: success\n");
116 			err = 0;
117 		} else {
118 			err = errno;
119 			perror("Did not receive EFBIG");
120 		}
121 	} else {
122 		(void) printf("write completed successfully, test failed\n");
123 		err = 1;
124 	}
125 
126 out:
127 	(void) unlink(testfile);
128 	free(testfile);
129 	close(fd);
130 	return (err);
131 }
132 
133 static void
134 usage(char *name)
135 {
136 	(void) printf("%s <testfile>\n", name);
137 	exit(1);
138 }
139 
140 static void
141 sigxfsz(int signo)
142 {
143 	(void) signo;
144 	(void) printf("\nlargest_file: sigxfsz() caught SIGXFSZ\n");
145 }
146