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 http://www.opensolaris.org/os/licensing.
  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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #pragma ident   "%Z%%M% %I%     %E% SMI"
  27 
  28 #include <assert.h>
  29 #include <errno.h>
  30 #include <libintl.h>
  31 #include <string.h>
  32 #include <strings.h>
  33 #include <sys/types.h>
  34 #include <sys/statvfs.h>
  35 #include <sys/param.h>
  36 
  37 #include <sys/lx_debug.h>
  38 #include <sys/lx_misc.h>
  39 #include <sys/lx_statfs.h>
  40 
  41 /*
  42  * these defines must exist before we include regexp.h, see regexp(5)
  43  */
  44 #define RE_SIZE         1024
  45 #define INIT            char *sp = instring;
  46 #define GETC()          (*sp++)
  47 #define PEEKC()         (*sp)
  48 #define UNGETC(c)       (--sp)
  49 #define RETURN(c)       return (NULL);
  50 #define ERROR(c)        return ((char *)c);
  51 
  52 /*
  53  * for regular expressions we're using regexp(5).
  54  *
  55  * we'd really prefer to use some other nicer regular expressions
  56  * interfaces (like regcmp(3c), regcomp(3c), or re_comp(3c)) but we
  57  * can't because all these other interfaces rely on the ability
  58  * to allocate memory via libc malloc()/calloc() calls, which
  59  * we can't really do here.
  60  *
  61  * we could optionally use regexpr(3gen) but we don't since the
  62  * interfaces there are incredibly similar to the regexp(5)
  63  * interfaces we're already using and we'd have the added
  64  * requirement of linking against libgen.
  65  *
  66  * another option that was considered is fnmatch(3c) but the
  67  * limited pattern expansion capability of this interface would
  68  * force us to include more patterns to check against.
  69  */
  70 #include <regexp.h>
  71 
  72 static struct lx_ftype_path {
  73         char            *lfp_path;
  74         char            lfp_re[RE_SIZE];
  75         int             lfp_magic;
  76         char            *lfp_magic_str;
  77 } ftype_path_list[] = {
  78         { "^/dev/pts$",         "",
  79                 LX_DEVPTS_SUPER_MAGIC,  "LX_DEVPTS_SUPER_MAGIC" },
  80         { "^/dev/pts/$",        "",
  81                 LX_DEVPTS_SUPER_MAGIC,  "LX_DEVPTS_SUPER_MAGIC" },
  82         { "^/dev/pts/[0-9][0-9]*$",     "",
  83                 LX_DEVPTS_SUPER_MAGIC,  "LX_DEVPTS_SUPER_MAGIC" },
  84         { NULL,                 "",
  85                 0,                      NULL                    }
  86 };
  87 
  88 /*
  89  * For lack of linux equivalents, we present lofs and zfs as being ufs.
  90  */
  91 static struct lx_ftype_name {
  92         const char      *lfn_name;
  93         int             lfn_magic;
  94         char            *lfn_magic_str;
  95 } ftype_name_list[] = {
  96         { "hsfs",       LX_ISOFS_SUPER_MAGIC,   "LX_ISOFS_SUPER_MAGIC"  },
  97         { "nfs",        LX_NFS_SUPER_MAGIC,     "LX_NFS_SUPER_MAGIC"    },
  98         { "pcfs",       LX_MSDOS_SUPER_MAGIC,   "LX_MSDOS_SUPER_MAGIC"  },
  99         { "lx_proc",    LX_PROC_SUPER_MAGIC,    "LX_PROC_SUPER_MAGIC"   },
 100         { "ufs",        LX_UFS_MAGIC,           "LX_UFS_MAGIC"          },
 101         { "lofs",       LX_UFS_MAGIC,           "LX_UFS_MAGIC"          },
 102         { "zfs",        LX_UFS_MAGIC,           "LX_UFS_MAGIC"          },
 103         { NULL,         0,                      NULL    }
 104 };
 105 
 106 int
 107 lx_statfs_init()
 108 {
 109         int     i;
 110         char    *rv;
 111 
 112         for (i = 0; ftype_path_list[i].lfp_path != NULL; i++) {
 113                 rv = compile(
 114                     ftype_path_list[i].lfp_path,
 115                     ftype_path_list[i].lfp_re,
 116                     ftype_path_list[i].lfp_re + RE_SIZE, '\0');
 117                 if (rv == NULL)
 118                         continue;
 119 
 120                 lx_debug("lx_statfs_init compile(\"%s\") failed",
 121                     ftype_path_list[i].lfp_path);
 122                 return (1);
 123         }
 124         return (0);
 125 }
 126 
 127 static int
 128 stol_type(const char *path, const char *name)
 129 {
 130         int     i;
 131         lx_debug("\tstol_type(\"%s\", \"%s\")\n", path == NULL ? "NULL" : path,
 132             name == NULL ? "NULL" : name);
 133 
 134         if (path != NULL) {
 135                 char userpath[MAXPATHLEN];
 136 
 137                 if (uucopystr(path, userpath, MAXPATHLEN) == -1)
 138                         return (-errno);
 139 
 140                 for (i = 0; ftype_path_list[i].lfp_path != NULL; i++) {
 141                         if (step(userpath, ftype_path_list[i].lfp_re) == 0)
 142                                 continue;
 143 
 144                         /* got a match on the fs path */
 145                         lx_debug("\ttranslated f_type to 0x%x - %s",
 146                             ftype_path_list[i].lfp_magic,
 147                             ftype_path_list[i].lfp_magic_str);
 148                         return (ftype_path_list[i].lfp_magic);
 149                 }
 150         }
 151 
 152         assert(name != NULL);
 153         for (i = 0; ftype_name_list[i].lfn_name != NULL; i++) {
 154                 if (strcmp(name, ftype_name_list[i].lfn_name) == 0) {
 155 
 156                         /* got a match on the fs name */
 157                         lx_debug("\ttranslated f_type to 0x%x - %s",
 158                             ftype_name_list[i].lfn_magic,
 159                             ftype_name_list[i].lfn_magic_str);
 160                         return (ftype_name_list[i].lfn_magic);
 161                 }
 162         }
 163 
 164         /* we don't know what the fs type is so just set it to 0 */
 165         return (0);
 166 }
 167 
 168 /*
 169  * The Linux statfs() is similar to the Solaris statvfs() call, the main
 170  * difference being the use of a numeric 'f_type' identifier instead of the
 171  * 'f_basetype' string.
 172  */
 173 static int
 174 stol_statfs(const char *path, struct lx_statfs *l, struct statvfs *s)
 175 {
 176         int type;
 177 
 178         if ((type = stol_type(path, s->f_basetype)) < 0)
 179                 return (type);
 180 
 181         l->f_type = type;
 182         l->f_bsize = s->f_bsize;
 183         l->f_blocks = s->f_blocks;
 184         l->f_bfree = s->f_bfree;
 185         l->f_bavail = s->f_bavail;
 186         l->f_files = s->f_files;
 187         l->f_ffree = s->f_ffree;
 188         l->f_fsid = s->f_fsid;
 189         l->f_namelen = s->f_namemax;
 190         l->f_frsize = s->f_frsize;
 191         bzero(&(l->f_spare), sizeof (l->f_spare));
 192 
 193         return (0);
 194 }
 195 
 196 static int
 197 stol_statfs64(const char *path, struct lx_statfs64 *l, struct statvfs64 *s)
 198 {
 199         int type;
 200 
 201         if ((type = stol_type(path, s->f_basetype)) < 0)
 202                 return (type);
 203 
 204         l->f_type = type;
 205         l->f_bsize = s->f_bsize;
 206         l->f_blocks = s->f_blocks;
 207         l->f_bfree = s->f_bfree;
 208         l->f_bavail = s->f_bavail;
 209         l->f_files = s->f_files;
 210         l->f_ffree = s->f_ffree;
 211         l->f_fsid = s->f_fsid;
 212         l->f_namelen = s->f_namemax;
 213         l->f_frsize = s->f_frsize;
 214         bzero(&(l->f_spare), sizeof (l->f_spare));
 215 
 216         return (0);
 217 }
 218 
 219 int
 220 lx_statfs(uintptr_t p1, uintptr_t p2)
 221 {
 222         const char *path = (const char *)p1;
 223         struct lx_statfs lxfs, *fs = (struct lx_statfs *)p2;
 224         struct statvfs vfs;
 225         int err;
 226 
 227         lx_debug("\tfstatvfs(%s, 0x%p)", path, fs);
 228         if (statvfs(path, &vfs) != 0)
 229                 return (-errno);
 230 
 231         if ((err = stol_statfs(path, &lxfs, &vfs)) != 0)
 232                 return (err);
 233 
 234         if (uucopy(&lxfs, fs, sizeof (struct lx_statfs)) != 0)
 235                 return (-errno);
 236 
 237         return (0);
 238 }
 239 
 240 int
 241 lx_fstatfs(uintptr_t p1, uintptr_t p2)
 242 {
 243         struct lx_statfs lxfs, *fs = (struct lx_statfs *)p2;
 244         struct statvfs vfs;
 245         char *path, path_buf[MAXPATHLEN];
 246         int fd = (int)p1;
 247         int err;
 248 
 249         lx_debug("\tfstatvfs(%d, 0x%p)", fd, fs);
 250         if (fstatvfs(fd, &vfs) != 0)
 251                 return (-errno);
 252 
 253         path = lx_fd_to_path(fd, path_buf, sizeof (path_buf));
 254 
 255         if ((err = stol_statfs(path, &lxfs, &vfs)) != 0)
 256                 return (err);
 257 
 258         if (uucopy(&lxfs, fs, sizeof (struct lx_statfs)) != 0)
 259                 return (-errno);
 260 
 261         return (0);
 262 }
 263 
 264 /* ARGSUSED */
 265 int
 266 lx_statfs64(uintptr_t p1, uintptr_t p2, uintptr_t p3)
 267 {
 268         const char *path = (const char *)p1;
 269         struct lx_statfs64 lxfs, *fs = (struct lx_statfs64 *)p3;
 270         struct statvfs64 vfs;
 271         int err;
 272 
 273         lx_debug("\tstatvfs64(%s, %d, 0x%p)", path, p2, fs);
 274         if (statvfs64(path, &vfs) != 0)
 275                 return (-errno);
 276 
 277         if ((err = stol_statfs64(path, &lxfs, &vfs)) != 0)
 278                 return (err);
 279 
 280         if (uucopy(&lxfs, fs, sizeof (struct lx_statfs64)) != 0)
 281                 return (-errno);
 282 
 283         return (0);
 284 }
 285 
 286 /* ARGSUSED */
 287 int
 288 lx_fstatfs64(uintptr_t p1, uintptr_t p2, uintptr_t p3)
 289 {
 290         struct lx_statfs64 lxfs, *fs = (struct lx_statfs64 *)p3;
 291         struct statvfs64 vfs;
 292         char *path, path_buf[MAXPATHLEN];
 293         int fd = (int)p1;
 294         int err;
 295 
 296         lx_debug("\tfstatvfs64(%d, %d, 0x%p)", fd, p2, fs);
 297         if (fstatvfs64(fd, &vfs) != 0)
 298                 return (-errno);
 299 
 300         path = lx_fd_to_path(fd, path_buf, sizeof (path_buf));
 301 
 302         if ((err = stol_statfs64(path, &lxfs, &vfs)) != 0)
 303                 return (err);
 304 
 305         if (uucopy(&lxfs, fs, sizeof (struct lx_statfs64)) != 0)
 306                 return (-errno);
 307 
 308         return (0);
 309 }