1c7046f76SMartin Matuska /*
2c7046f76SMartin Matuska * CDDL HEADER START
3c7046f76SMartin Matuska *
4c7046f76SMartin Matuska * The contents of this file are subject to the terms of the
5c7046f76SMartin Matuska * Common Development and Distribution License (the "License").
6c7046f76SMartin Matuska * You may not use this file except in compliance with the License.
7c7046f76SMartin Matuska *
8c7046f76SMartin Matuska * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c7046f76SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10c7046f76SMartin Matuska * See the License for the specific language governing permissions
11c7046f76SMartin Matuska * and limitations under the License.
12c7046f76SMartin Matuska *
13c7046f76SMartin Matuska * When distributing Covered Code, include this CDDL HEADER in each
14c7046f76SMartin Matuska * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c7046f76SMartin Matuska * If applicable, add the following below this CDDL HEADER, with the
16c7046f76SMartin Matuska * fields enclosed by brackets "[]" replaced with your own identifying
17c7046f76SMartin Matuska * information: Portions Copyright [yyyy] [name of copyright owner]
18c7046f76SMartin Matuska *
19c7046f76SMartin Matuska * CDDL HEADER END
20c7046f76SMartin Matuska */
21c7046f76SMartin Matuska
22c7046f76SMartin Matuska /*
23c7046f76SMartin Matuska * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24c7046f76SMartin Matuska * Use is subject to license terms.
25c7046f76SMartin Matuska */
26c7046f76SMartin Matuska
27c7046f76SMartin Matuska /*
28c7046f76SMartin Matuska * Copyright (c) 2022 by Information2 Software, Inc. All rights reserved.
29c7046f76SMartin Matuska */
30c7046f76SMartin Matuska
31c7046f76SMartin Matuska #include "file_common.h"
32c7046f76SMartin Matuska #include <sys/types.h>
33c7046f76SMartin Matuska #include <unistd.h>
34c7046f76SMartin Matuska #include <fcntl.h>
35c7046f76SMartin Matuska #include <string.h>
36c7046f76SMartin Matuska
37c7046f76SMartin Matuska /*
38c7046f76SMartin Matuska * Call fadvise to prefetch data
39c7046f76SMartin Matuska */
40c7046f76SMartin Matuska static const char *execname = "file_fadvise";
41c7046f76SMartin Matuska
42c7046f76SMartin Matuska static void
usage(void)43c7046f76SMartin Matuska usage(void)
44c7046f76SMartin Matuska {
45c7046f76SMartin Matuska (void) fprintf(stderr,
46c7046f76SMartin Matuska "usage: %s -f filename -a advise \n", execname);
47c7046f76SMartin Matuska }
48c7046f76SMartin Matuska
49c7046f76SMartin Matuska int
main(int argc,char * argv[])50c7046f76SMartin Matuska main(int argc, char *argv[])
51c7046f76SMartin Matuska {
52c7046f76SMartin Matuska char *filename = NULL;
53c7046f76SMartin Matuska int advise = 0;
54c7046f76SMartin Matuska int fd, ch;
55c7046f76SMartin Matuska int err = 0;
56c7046f76SMartin Matuska
57c7046f76SMartin Matuska while ((ch = getopt(argc, argv, "a:f:")) != EOF) {
58c7046f76SMartin Matuska switch (ch) {
59c7046f76SMartin Matuska case 'a':
60c7046f76SMartin Matuska advise = atoll(optarg);
61c7046f76SMartin Matuska break;
62c7046f76SMartin Matuska case 'f':
63c7046f76SMartin Matuska filename = optarg;
64c7046f76SMartin Matuska break;
65c7046f76SMartin Matuska case '?':
66c7046f76SMartin Matuska (void) printf("unknown arg %c\n", optopt);
67c7046f76SMartin Matuska usage();
68c7046f76SMartin Matuska break;
69c7046f76SMartin Matuska }
70c7046f76SMartin Matuska }
71c7046f76SMartin Matuska
72c7046f76SMartin Matuska if (!filename) {
73c7046f76SMartin Matuska (void) printf("Filename not specified (-f <file>)\n");
74c7046f76SMartin Matuska err++;
75c7046f76SMartin Matuska }
76c7046f76SMartin Matuska
77c7046f76SMartin Matuska if (advise < POSIX_FADV_NORMAL || advise > POSIX_FADV_NOREUSE) {
78c7046f76SMartin Matuska (void) printf("advise is invalid\n");
79c7046f76SMartin Matuska err++;
80c7046f76SMartin Matuska }
81c7046f76SMartin Matuska
82c7046f76SMartin Matuska if (err) {
83c7046f76SMartin Matuska usage(); /* no return */
84c7046f76SMartin Matuska return (1);
85c7046f76SMartin Matuska }
86c7046f76SMartin Matuska
87c7046f76SMartin Matuska if ((fd = open(filename, O_RDWR, 0666)) < 0) {
88c7046f76SMartin Matuska perror("open");
89c7046f76SMartin Matuska return (1);
90c7046f76SMartin Matuska }
91c7046f76SMartin Matuska
92*be181ee2SMartin Matuska if (posix_fadvise(fd, 0, 0, advise) != 0) {
93*be181ee2SMartin Matuska perror("posix_fadvise");
94*be181ee2SMartin Matuska close(fd);
95*be181ee2SMartin Matuska return (1);
96*be181ee2SMartin Matuska }
97c7046f76SMartin Matuska
98c7046f76SMartin Matuska close(fd);
99c7046f76SMartin Matuska
100c7046f76SMartin Matuska return (0);
101c7046f76SMartin Matuska }
102