xref: /illumos-gate/usr/src/cmd/dumpadm/minfree.c (revision 7c478bd9)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
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  * Copyright (c) 2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/statvfs.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 
34 #include <unistd.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <fcntl.h>
38 
39 #include "utils.h"
40 
41 static FILE *
minfree_open(const char * dir,int oflags,const char * fmode)42 minfree_open(const char *dir, int oflags, const char *fmode)
43 {
44 	char path[MAXPATHLEN];
45 	int fd;
46 
47 	(void) snprintf(path, sizeof (path), "%s/minfree", dir);
48 
49 	if ((fd = open(path, oflags, S_IRUSR | S_IWUSR)) >= 0)
50 		return (fdopen(fd, fmode));
51 
52 	return (NULL);
53 }
54 
55 int
minfree_read(const char * dir,unsigned long long * ullp)56 minfree_read(const char *dir, unsigned long long *ullp)
57 {
58 	FILE *fp = minfree_open(dir, O_RDONLY, "r");
59 
60 	if (fp != NULL) {
61 		char buf[BUFSIZ];
62 		int status = -1;
63 
64 		if (fgets(buf, BUFSIZ, fp) != NULL) {
65 			if (valid_str2ull(buf, ullp))
66 				status = 0;
67 			else
68 				warn(gettext("\"%s/minfree\": invalid minfree "
69 				    "value -- %s\n"), dir, buf);
70 		}
71 
72 		(void) fclose(fp);
73 		return (status);
74 	}
75 
76 	return (-1);
77 }
78 
79 int
minfree_write(const char * dir,unsigned long long ull)80 minfree_write(const char *dir, unsigned long long ull)
81 {
82 	FILE *fp = minfree_open(dir, O_WRONLY | O_CREAT | O_TRUNC, "w");
83 
84 	if (fp != NULL) {
85 		int status = fprintf(fp, "%llu\n", ull);
86 		(void) fclose(fp);
87 		return (status);
88 	}
89 
90 	return (-1);
91 }
92 
93 int
minfree_compute(const char * dir,char * s,unsigned long long * ullp)94 minfree_compute(const char *dir, char *s, unsigned long long *ullp)
95 {
96 	size_t len = strlen(s);
97 	unsigned long long m = 1;
98 
99 	struct statvfs64 fsb;
100 	int pct;
101 
102 	switch (s[len - 1]) {
103 	case '%':
104 		s[len - 1] = '\0';
105 
106 		if (!valid_str2int(s, &pct) || pct > 100) {
107 			warn(gettext("invalid minfree %% -- %s\n"), s);
108 			return (-1);
109 		}
110 
111 		if (statvfs64(dir, &fsb) == -1) {
112 			warn(gettext("failed to statvfs %s"), dir);
113 			return (-1);
114 		}
115 
116 		*ullp = fsb.f_blocks * fsb.f_frsize *
117 		    (u_longlong_t)pct / 100ULL / 1024ULL;
118 
119 		return (0);
120 
121 	case 'm':
122 	case 'M':
123 		m = 1024ULL;
124 		/*FALLTHRU*/
125 
126 	case 'k':
127 	case 'K':
128 		s[len - 1] = '\0';
129 
130 		if (valid_str2ull(s, ullp)) {
131 			*ullp *= m;
132 			return (0);
133 		}
134 
135 		warn(gettext("invalid minfree value -- %s\n"), s);
136 		return (-1);
137 
138 	default:
139 		warn(gettext("expected m, k, or %% unit after "
140 		    "minfree -- %s\n"), s);
141 		return (-1);
142 	}
143 }
144