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 #include <sys/types.h> 28 #include <sys/statvfs.h> 29 #include <sys/param.h> 30 #include <sys/stat.h> 31 32 #include <unistd.h> 33 #include <string.h> 34 #include <stdio.h> 35 #include <fcntl.h> 36 37 #include "utils.h" 38 39 static FILE * 40 minfree_open(const char *dir, int oflags, const char *fmode) 41 { 42 char path[MAXPATHLEN]; 43 int fd; 44 45 (void) snprintf(path, sizeof (path), "%s/minfree", dir); 46 47 if ((fd = open(path, oflags, S_IRUSR | S_IWUSR)) >= 0) 48 return (fdopen(fd, fmode)); 49 50 return (NULL); 51 } 52 53 int 54 minfree_read(const char *dir, unsigned long long *ullp) 55 { 56 FILE *fp = minfree_open(dir, O_RDONLY, "r"); 57 58 if (fp != NULL) { 59 char buf[BUFSIZ]; 60 int status = -1; 61 62 if (fgets(buf, BUFSIZ, fp) != NULL) { 63 if (valid_str2ull(buf, ullp)) 64 status = 0; 65 else 66 warn(gettext("\"%s/minfree\": invalid minfree " 67 "value -- %s\n"), dir, buf); 68 } 69 70 (void) fclose(fp); 71 return (status); 72 } 73 74 return (-1); 75 } 76 77 int 78 minfree_write(const char *dir, unsigned long long ull) 79 { 80 FILE *fp = minfree_open(dir, O_WRONLY | O_CREAT | O_TRUNC, "w"); 81 82 if (fp != NULL) { 83 int status = fprintf(fp, "%llu\n", ull); 84 (void) fclose(fp); 85 return (status); 86 } 87 88 return (-1); 89 } 90 91 int 92 minfree_compute(const char *dir, char *s, unsigned long long *ullp) 93 { 94 size_t len = strlen(s); 95 unsigned long long m = 1; 96 97 struct statvfs64 fsb; 98 int pct; 99 100 switch (s[len - 1]) { 101 case '%': 102 s[len - 1] = '\0'; 103 104 if (!valid_str2int(s, &pct) || pct > 100) { 105 warn(gettext("invalid minfree %% -- %s\n"), s); 106 return (-1); 107 } 108 109 if (statvfs64(dir, &fsb) == -1) { 110 warn(gettext("failed to statvfs %s"), dir); 111 return (-1); 112 } 113 114 *ullp = fsb.f_blocks * fsb.f_frsize * 115 (u_longlong_t)pct / 100ULL / 1024ULL; 116 117 return (0); 118 119 case 'm': 120 case 'M': 121 m = 1024ULL; 122 /*FALLTHRU*/ 123 124 case 'k': 125 case 'K': 126 s[len - 1] = '\0'; 127 128 if (valid_str2ull(s, ullp)) { 129 *ullp *= m; 130 return (0); 131 } 132 133 warn(gettext("invalid minfree value -- %s\n"), s); 134 return (-1); 135 136 default: 137 warn(gettext("expected m, k, or %% unit after " 138 "minfree -- %s\n"), s); 139 return (-1); 140 } 141 } 142