[BACK]Return to filesys-os.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rdistd

Annotation of src/usr.bin/rdistd/filesys-os.c, Revision 1.7

1.7     ! deraadt     1: /*     $OpenBSD: filesys-os.c,v 1.6 1999/02/04 23:18:57 millert Exp $  */
1.3       deraadt     2:
1.1       dm          3: /*
                      4:  * Copyright (c) 1983 Regents of the University of California.
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
1.5       millert    37: #if 0
1.1       dm         38: static char RCSid[] =
1.5       millert    39: "$From: filesys-os.c,v 6.17 1996/01/17 21:02:45 mcooper Exp mcooper $";
                     40: #else
                     41: static char RCSid[] =
1.7     ! deraadt    42: "$OpenBSD: filesys-os.c,v 1.6 1999/02/04 23:18:57 millert Exp $";
1.5       millert    43: #endif
1.1       dm         44:
                     45: static char sccsid[] = "@(#)filesys-os.c";
                     46:
                     47: static char copyright[] =
                     48: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
                     49:  All rights reserved.\n";
                     50: #endif /* not lint */
                     51:
                     52: /*
                     53:  * OS specific file system routines
                     54:  */
                     55:
                     56: #include "defs.h"
                     57: #include "filesys.h"
                     58:
                     59: #if    FSI_TYPE == FSI_GETFSSTAT
                     60: static struct statfs   *mnt = NULL;
                     61: #if    FSTYPENAME
1.5       millert    62: #define        f_type_eq(a, b) (! strcmp (((struct statfs *) (a))->f_fstypename, (b)))
1.1       dm         63: #else  /* !FSTYPENAME */
                     64: #define        f_type_eq(a, b) (((struct statfs *) a)->f_type == (b))
1.5       millert    65: #endif /* !FSTYPENAME */
1.1       dm         66: #endif /* FSI_GETFSSTAT */
                     67:
                     68: #if    FSI_TYPE == FSI_MNTCTL
                     69: static struct vmount   *mnt = NULL;
                     70: #endif /* FSI_MNTCTL */
                     71:
                     72: #if    (FSI_TYPE == FSI_MNTCTL) || (FSI_TYPE == FSI_GETFSSTAT)
                     73: static char           *mntbuf = NULL;
                     74: static int             entries_left;
                     75: #endif /* FSI_MNTCTL || FSI_GETFSSTAT */
                     76:
                     77: #if    FSI_TYPE == FSI_MNTCTL
                     78: /*
                     79:  * AIX version of setmountent()
                     80:  */
                     81: FILE *setmountent(file, mode)
                     82:        /*ARGSUSED*/
                     83:        char *file;
                     84:        char *mode;
                     85: {
1.5       millert    86:        ulong size;
1.1       dm         87:
                     88:        if (mntbuf)
                     89:                (void) free(mntbuf);
                     90:
                     91:        mntctl(MCTL_QUERY, sizeof(size), &size);
                     92:        mntbuf = (char *) xmalloc(size);
                     93:
                     94:        entries_left = mntctl(MCTL_QUERY, size, mntbuf);
                     95:        if (!entries_left)
1.4       kstailey   96:                return(NULL);
1.1       dm         97:
                     98:        mnt = (struct vmount *)mntbuf;
                     99:        return((FILE *) 1);
                    100: }
                    101: #endif /* FSI_MNTCTL */
                    102:
                    103: #if    FSI_TYPE == FSI_GETFSSTAT
                    104: /*
                    105:  * getfsstat() version of get mount info routines.
                    106:  */
                    107: FILE *setmountent(file, mode)
                    108:        /*ARGSUSED*/
                    109:        char *file;
                    110:        char *mode;
                    111: {
1.5       millert   112:        long size;
1.1       dm        113:
                    114:        if (mntbuf)
                    115:                (void) free(mntbuf);
                    116:
1.4       kstailey  117:        size = getfsstat(NULL, 0, MNT_WAIT);
1.2       deraadt   118:        if (size == -1)
1.4       kstailey  119:                return (NULL);
1.1       dm        120:        size *= sizeof(struct statfs);
                    121:        mntbuf = (char *) xmalloc(size);
                    122:
                    123:        entries_left = getfsstat((struct statfs *)mntbuf, size, MNT_WAIT);
                    124:        if (entries_left == -1)
1.4       kstailey  125:                return(NULL);
1.1       dm        126:
                    127:        mnt = (struct statfs *) mntbuf;
                    128:
                    129:        return((FILE *) 1);
                    130: }
                    131: #endif /* FSI_GETFSSTAT */
                    132:
                    133: #if    FSI_TYPE == FSI_MNTCTL
                    134: /*
                    135:  * AIX version of getmountent()
                    136:  */
                    137: /*
                    138:  * Iterate over mount entries
                    139:  */
                    140: mntent_t *getmountent(fptr)
                    141:        /*ARGSUSED*/
                    142:        FILE *fptr;
                    143: {
                    144:        static mntent_t mntstruct;
                    145:
                    146:        if (!entries_left)
                    147:                return((mntent_t*)0);
                    148:
                    149:        bzero((char *) &mntstruct, sizeof(mntstruct));
                    150:
                    151:        if (mnt->vmt_flags & MNT_READONLY)
                    152:                mntstruct.me_flags |= MEFLAG_READONLY;
                    153:
                    154:        mntstruct.me_path = vmt2dataptr(mnt, VMT_STUB);
1.5       millert   155:        switch ((ulong)(struct vmount*)mnt->vmt_gfstype) {
1.1       dm        156:              case MNT_NFS:
                    157:                mntstruct.me_type = METYPE_NFS;
                    158:                break;
                    159:              default:
                    160:                mntstruct.me_type = METYPE_OTHER;
                    161:                break;
                    162:        }
                    163:
                    164:        mnt = (struct vmount*)((mnt->vmt_length)+(char *)mnt);
                    165:        entries_left--;
                    166:
                    167:        return(&mntstruct);
                    168: }
                    169: #endif /* FSI_MNTCTL */
                    170:
                    171: #if    FSI_TYPE == FSI_GETFSSTAT
                    172: /*
                    173:  * getfsstat() version of getmountent()
                    174:  */
                    175: mntent_t *getmountent(fptr)
                    176:        /*ARGSUSED*/
                    177:        FILE *fptr;
                    178: {
                    179:        static mntent_t mntstruct;
                    180:        static char remote_dev[MAXHOSTNAMELEN+MAXPATHLEN+1];
                    181:
                    182:        if (!entries_left)
                    183:                return((mntent_t*)0);
                    184:
                    185:        bzero((char *) &mntstruct, sizeof(mntstruct));
                    186:
                    187: #if    defined(MNT_RDONLY)
                    188:        if (mnt->f_flags & MNT_RDONLY)
                    189:                mntstruct.me_flags |= MEFLAG_READONLY;
                    190: #endif
                    191: #if    defined(M_RDONLY)
                    192:        if (mnt->f_flags & M_RDONLY)
                    193:                mntstruct.me_flags |= MEFLAG_READONLY;
                    194: #endif
1.5       millert   195:        if (f_type_eq(mnt, MOUNT_NFS)) {
1.7     ! deraadt   196:                (void) snprintf(remote_dev, sizeof remote_dev,
        !           197:                    "%s", mnt->f_mntfromname);
1.1       dm        198:                mntstruct.me_path = remote_dev;
                    199:                mntstruct.me_type = METYPE_NFS;
1.5       millert   200:        } else {
1.1       dm        201:                mntstruct.me_path = mnt->f_mntonname;
                    202:                mntstruct.me_type = METYPE_OTHER;
                    203:        }
                    204:
                    205:        mnt++;
                    206:        entries_left--;
                    207:
                    208:        return(&mntstruct);
                    209: }
                    210: #endif
                    211:
                    212: #if    (FSI_TYPE == FSI_MNTCTL) || (FSI_TYPE == FSI_GETFSSTAT)
                    213: /*
                    214:  * Done with iterations
                    215:  */
                    216: void endmountent(fptr)
                    217:        /*ARGSUSED*/
                    218:        FILE *fptr;
                    219: {
                    220:        mnt = NULL;
                    221:
                    222:        if (mntbuf) {
                    223:                (void) free(mntbuf);
1.4       kstailey  224:                mntbuf = NULL;
1.1       dm        225:        }
                    226: }
                    227: #endif /* FSI_MNTCTL || FSI_GETFSSTAT */
                    228:
                    229: #if    FSI_TYPE == FSI_GETMNTENT2
                    230: /*
                    231:  * Prepare to iterate over mounted filesystem list
                    232:  */
                    233: FILE *setmountent(file, mode)
                    234:        /*ARGSUSED*/
                    235:        char *file;
                    236:        char *mode;
                    237: {
                    238:        return(fopen(file, mode));
                    239: }
                    240:
                    241: /*
                    242:  * Done with iteration
                    243:  */
                    244: void endmountent(fptr)
                    245:        /*ARGSUSED*/
                    246:        FILE *fptr;
                    247: {
                    248:        fclose(fptr);
                    249: }
                    250:
                    251: /*
                    252:  * Iterate over mount entries
                    253:  */
                    254: mntent_t *getmountent(fptr)
                    255:        FILE *fptr;
                    256: {
                    257:        static mntent_t me;
                    258:        static struct mnttab mntent;
                    259:
                    260:        bzero((char *)&me, sizeof(mntent_t));
                    261:
                    262: #if     defined(UNICOS)
                    263:         if (getmntent(fptr, &mntent) != NULL) {
                    264: #else
                    265:         if (getmntent(fptr, &mntent) != -1) {
                    266: #endif
                    267:                me.me_path = mntent.mnt_mountp;
                    268:                me.me_type = mntent.mnt_fstype;
                    269:                if (mntent.mnt_mntopts && hasmntopt(&mntent, MNTOPT_RO))
                    270:                        me.me_flags |= MEFLAG_READONLY;
                    271:
                    272: #if    defined(MNTTYPE_IGNORE)
                    273:                if (strcmp(mntent.mnt_fstype, MNTTYPE_IGNORE) == 0)
                    274:                        me.me_flags |= MEFLAG_IGNORE;
                    275: #endif /* MNTTYPE_IGNORE */
                    276: #if    defined(MNTTYPE_SWAP)
                    277:                if (strcmp(mntent.mnt_fstype, MNTTYPE_SWAP) == 0)
                    278:                        me.me_flags |= MEFLAG_IGNORE;
                    279: #endif /* MNTTYPE_SWAP */
                    280:
                    281:                return(&me);
                    282:        } else
1.4       kstailey  283:                return(NULL);
1.1       dm        284: }
                    285: #endif /* FSI_GETMNTNET2 */
                    286:
                    287: #if    FSI_TYPE == FSI_GETMNTENT
                    288: /*
                    289:  * Prepare to iterate over mounted filesystem list
                    290:  */
                    291: FILE *setmountent(file, mode)
                    292:        /*ARGSUSED*/
                    293:        char *file;
                    294:        char *mode;
                    295: {
                    296:        return(setmntent(file, mode));
                    297: }
                    298:
                    299: /*
                    300:  * Done with iteration
                    301:  */
                    302: void endmountent(fptr)
                    303:        /*ARGSUSED*/
                    304:        FILE *fptr;
                    305: {
                    306:        endmntent(fptr);
                    307: }
                    308:
                    309: /*
                    310:  * Iterate over mount entries
                    311:  */
                    312: mntent_t *getmountent(fptr)
                    313:        FILE *fptr;
                    314: {
                    315:        static mntent_t me;
                    316:        struct mntent *mntent;
                    317:
                    318:        bzero((char *)&me, sizeof(mntent_t));
                    319:
                    320:        if (mntent = getmntent(fptr)) {
                    321:                me.me_path = mntent->mnt_dir;
                    322:                me.me_type = mntent->mnt_type;
                    323:                if (mntent->mnt_opts && hasmntopt(mntent, MNTOPT_RO))
                    324:                        me.me_flags |= MEFLAG_READONLY;
                    325:
                    326: #if    defined(MNTTYPE_IGNORE)
                    327:                if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
                    328:                        me.me_flags |= MEFLAG_IGNORE;
                    329: #endif /* MNTTYPE_IGNORE */
                    330: #if    defined(MNTTYPE_SWAP)
                    331:                if (strcmp(mntent->mnt_type, MNTTYPE_SWAP) == 0)
                    332:                        me.me_flags |= MEFLAG_IGNORE;
                    333: #endif /* MNTTYPE_SWAP */
                    334:
                    335:                return(&me);
                    336:        } else
1.4       kstailey  337:                return(NULL);
1.1       dm        338: }
                    339: #endif /* FSI_GETMNTNET */
                    340:
                    341: #if    FSI_TYPE == FSI_GETMNT
                    342: /*
                    343:  * getmnt() interface (Ultrix)
                    344:  */
                    345:
                    346: #include <sys/fs_types.h>
                    347:
                    348: static int startmounts = 0;
                    349:
                    350: FILE *setmountent(file, mode)
                    351:        /*ARGSUSED*/
                    352:        char *file;
                    353:        char *mode;
                    354: {
                    355:        startmounts = 0;
1.5       millert   356:        return(stdin);          /* XXX - need to return something! */
1.1       dm        357: }
                    358:
                    359: void endmountent(fptr)
                    360:        /*ARGSUSED*/
                    361:        FILE *fptr;
                    362: {
                    363:        /* NOOP */
                    364: }
                    365:
                    366: /*
                    367:  * Iterate over mounted filesystems using getmnt()
                    368:  */
                    369: mntent_t *getmountent(fptr)
                    370:        /*ARGSUSED*/
                    371:        FILE *fptr;
                    372: {
                    373:        struct fs_data fs_data;
                    374:        static mntent_t me;
                    375:
                    376:        if (getmnt(&startmounts, &fs_data, sizeof(fs_data), NOSTAT_MANY,
1.4       kstailey  377:                   NULL) <= 0)
                    378:                return(NULL);
1.1       dm        379:
                    380:        bzero((char *)&me, sizeof(mntent_t));
                    381:        me.me_path = fs_data.fd_path;
                    382:        if (fs_data.fd_fstype == GT_NFS)
                    383:                me.me_type = METYPE_NFS;
                    384:        else
                    385:                me.me_type = METYPE_OTHER;
                    386:
                    387:        if (fs_data.fd_flags & M_RONLY)
                    388:                me.me_flags |= MEFLAG_READONLY;
                    389:
                    390:        return(&me);
                    391: }
                    392: #endif /* FSI_GETMNT */
                    393:
                    394: /*
                    395:  * Make a new (copy) of a mntent structure.
                    396:  */
                    397: mntent_t *newmountent(old)
                    398:        mntent_t *old;
                    399: {
                    400:        mntent_t *new;
                    401:
                    402:        if (!old)
1.4       kstailey  403:                return(NULL);
1.1       dm        404:
                    405:        new = (mntent_t *) xcalloc(1, sizeof(mntent_t));
1.6       millert   406:        new->me_path = xstrdup(old->me_path);
                    407:        new->me_type = xstrdup(old->me_type);
1.1       dm        408:        new->me_flags = old->me_flags;
                    409:
                    410:        return(new);
                    411: }