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