Example #1
0
File: rmnt.c Project: OPSF/uClinux
struct mounts *
readmnt()
{
    char *dn = (char *)NULL;
    char *ln;
    FILE *mfp;
    struct mntent *mp;
    struct mounts *mtp;
    char *opt, *opte;
    struct stat sb;

    if (Lmi || Lmist)
        return(Lmi);
    /*
     * Open access to the mount table.
     */
    if (!(mfp = setmntent(MOUNTED, "r"))) {
        (void) fprintf(stderr, "%s: can't access %s\n", Pn, MOUNTED);
        Exit(1);
    }
    /*
     * Read mount table entries.
     */
    while ((mp = getmntent(mfp))) {

#if	defined(MNTSKIP)
        /*
         * Specfy in the MNTSKIP macro the decisions needed to determine
         * that this entry should be skipped.
         *
         * Typically entries whose mnt_type is MNTTYPE_IGNORE are skipped.
         *
         * The MNTSKIP macro allows the caller to use other tests.
         */
        MNTSKIP
#endif	/* MNTSKIP */

        /*
         * Interpolate a possible symbolic directory link.
         */
        if (dn)
            (void) free((FREE_P *)dn);
        if (!(dn = mkstrcpy(mp->mnt_dir, (MALLOC_S *)NULL)))
            goto no_space_for_mount;
        if (!(ln = Readlink(dn))) {
            if (!Fwarn)
                (void) fprintf(stderr,
                               "      Output information may be incomplete.\n");
            continue;
        }
        if (ln != dn) {
            (void) free((FREE_P *)dn);
            dn = ln;
        }
        if (*dn != '/')
            continue;
        /*
         * Stat() the directory.
         */
        if (statsafely(dn, &sb)) {
            if (!Fwarn) {
                (void) fprintf(stderr, "%s: WARNING: can't stat() ", Pn);
                safestrprt(mp->mnt_type, stderr, 0);
                (void) fprintf(stderr, " file system ");
                safestrprt(mp->mnt_dir, stderr, 1);
                (void) fprintf(stderr,
                               "      Output information may be incomplete.\n");
            }
            if ((opt = strstr(mp->mnt_opts, "dev="))) {
                (void) zeromem(&sb, sizeof(sb));
                if ((opte = x2dev(opt + 4, (dev_t *)&sb.st_dev))) {
                    sb.st_mode = S_IFDIR | 0777;
                    if (!Fwarn)
                        (void) fprintf(stderr,
                                       "      assuming \"%.*s\" from %s\n",
                                       (int)(opte - opt), opt, MOUNTED);
                } else
                    opt = (char *)NULL;
            }
            if (!opt)
                continue;
        }
        /*
         * Allocate and fill a local mounts structure with the directory
         * (mounted) information.
         */
        if (!(mtp = (struct mounts *)malloc(sizeof(struct mounts)))) {

no_space_for_mount:

            (void) fprintf(stderr, "%s: no space for mount at ", Pn);
            safestrprt(mp->mnt_fsname, stderr, 0);
            (void) fprintf(stderr, " (");
            safestrprt(mp->mnt_dir, stderr, 0);
            (void) fprintf(stderr, ")\n");
            Exit(1);
        }
        mtp->dir = dn;
        dn = (char *)NULL;
        mtp->next = Lmi;
        mtp->dev = RMNT_EXPDEV(sb.st_dev);
        mtp->rdev = RMNT_EXPDEV(sb.st_rdev);
        mtp->inode = (INODETYPE)sb.st_ino;
        mtp->mode = sb.st_mode;

# if	defined(RMNT_FSTYPE) && defined(MOUNTS_FSTYPE)
        /*
         * Make a copy of RMNT_FSTYPE in MOUNTS_FSTYPE.
         */
        if (!(mtp->MOUNTS_FSTYPE = mkstrcpy(mp->RMNT_FSTYPE,
                                            (MALLOC_S *)NULL)))
        {
            (void) fprintf(stderr, "%s: no space for fstype (%s): %s\n",
                           Pn, mtp->dir, mp->RMNT_FSTYPE);
            Exit(1);
        }
        (void) strcpy(mtp->MOUNTS_FSTYPE, mp->RMNT_FSTYPE);
# endif	/* defined(RMNT_FSTYP) && defined(MOUNTS_FSTYP) */

# if	defined(RMNT_STAT_FSTYPE) && defined(MOUNTS_STAT_FSTYPE)
        /*
         * Make a copy of RMNT_STAT_FSTYPE in MOUNTS_STAT_FSTYPE.
         */
        mtp->MOUNTS_STAT_FSTYPE = (int)sb.RMNT_STAT_FSTYPE;
# endif	/* defined(RMNT_STAT_FSTYP) && defined(MOUNTS_STAT_FSTYP) */

        /*
         * Interpolate a possible file system (mounted-on device) name link.
         */
        if (!(dn = mkstrcpy(mp->mnt_fsname, (MALLOC_S *)NULL)))
            goto no_space_for_mount;
        mtp->fsname = dn;
        ln = Readlink(dn);
        dn = (char *)NULL;
        /*
         * Stat() the file system (mounted-on) name and add file system
         * information to the local mounts structure.
         */
        if (!ln || statsafely(ln, &sb))
            sb.st_mode = 0;
        mtp->fsnmres = ln;
        mtp->fs_mode = sb.st_mode;
        Lmi = mtp;
    }
    (void) endmntent(mfp);
    /*
     * Clean up and return the local nount info table address.
     */
    if (dn)
        (void) free((FREE_P *)dn);
    Lmist = 1;
    return(Lmi);
}
Example #2
0
struct mounts *
readmnt()
{
	char *dn = (char *)NULL;
	char *ln;
	struct mnttab me;
	FILE *mfp;
	struct mounts *mtp;
	char *opt, *opte;
	struct stat sb;

#if	defined(HASPROCFS)
	int procfs = 0;
#endif

	if (Lmi || Lmist)
	    return(Lmi);
/*
 * Open access to the mount table and read mount table entries.
 */
	if (!(mfp = fopen(MNTTAB, "r"))) {
	    (void) fprintf(stderr, "%s: can't access %s\n", Pn, MNTTAB);
	    return(0);
        }
	while (!getmntent(mfp, &me)) {

	/*
	 * Skip loop-back mounts, since they are aliases for legitimate file
	 * systems and there is no way to determine that a vnode refers to a
	 * loop-back alias.
	 *
	 * Also skip file systems of type MNTTYPE_IGNORE or with the option
	 * MNTOPT_IGNORE, since they are auto-mounted file systems whose
	 * real entries (if they are mounted) will be separately identified
	 * by getmntent().
	 */
	    if (!strcmp(me.mnt_fstype, MNTTYPE_LO)
	    ||  !strcmp(me.mnt_fstype, MNTTYPE_IGNORE))
		continue;
	/*
	 * Interpolate a possible symbolic directory link.
	 */
	    if (dn)
		(void) free((FREE_P *)dn);
	    if (!(dn = mkstrcpy(me.mnt_mountp, (MALLOC_S *)NULL))) {

no_space_for_mount:

		(void) fprintf(stderr, "%s: no space for mount at ",Pn);
		safestrprt(me.mnt_special, stderr, 0);
		(void) fprintf(stderr, " (");
		safestrprt(me.mnt_mountp, stderr, 0);
		(void) fprintf(stderr, ")\n");
		Exit(1);
	    }
	    if (!(ln = Readlink(dn))) {
		if (!Fwarn) {
		    (void) fprintf(stderr,
			"      Output information may be incomplete.\n");
		}
		continue;
	    }
	    if (ln != dn) {
		(void) free((FREE_P *)dn);
		dn = ln;
	    }
	    if (*dn != '/')
		continue;
	/*
	 * Stat() the directory.
	 */
	    if (statsafely(dn, &sb)) {
		if (!Fwarn) {
		    (void) fprintf(stderr, "%s: can't stat()", Pn);

#if	defined(HASFSTYPE)
		    putc(' ', stderr);
		    safestrprt(me.mnt_fstype, stderr, 0);
#endif	/* defined(HASFSTYPE) */

		    (void) fprintf(stderr, " file system ");
		    safestrprt(me.mnt_mountp, stderr, 1);
		    (void) fprintf(stderr,
			"      Output information may be incomplete.\n");
		}
		if (!(opt = strstr(me.mnt_mntopts, "dev="))) {
		    (void) memset(&sb, 0, sizeof(sb));
		    if (!(opte = x2dev(opt + 4, &sb.st_dev))) {
			sb.st_mode = S_IFDIR | 0777;

#if	defined(HASFSTYPE)
			(void) strncpy(sb.st_fstype, me.mnt_fstype,
				       sizeof(sb.st_fstype));
			sb.st_fstype[sizeof(sb.st_fstype) - 1 ] = '\0';
#endif	/* HASFSTYPE */

			if (!Fwarn) {
			    (void) fprintf(stderr,
				"      assuming \"%.*s\" from %s\n",
				(opte - opt), opt, MNTTAB);
			}
		    } else
			opt = (char *)NULL;
		}
		if (!opt)
		    continue;
	    }
	/*
	 * Allocate and fill a local mount structure.
	 */
	    if (!(mtp = (struct mounts *)malloc(sizeof(struct mounts))))
		goto no_space_for_mount;

#if	defined(HASFSTYPE)
	    if (!(mtp->fstype = mkstrcpy(sb.st_fstype, (MALLOC_S *)NULL)))
		goto no_space_for_mount;
#endif	/* HASFSTYPE */

	    mtp->dir = dn;
	    dn = (char *)NULL;
	    mtp->next = Lmi;
	    mtp->dev = sb.st_dev;
	    mtp->rdev = sb.st_rdev;
	    mtp->inode = (INODETYPE)sb.st_ino;
	    mtp->mode = sb.st_mode;

#if	defined(HASPROCFS)
# if	defined(HASFSTYPE)
	    if (!strcmp(sb.st_fstype, HASPROCFS))
# else	/* !defined*HASFSTYPE) */
	    if (!strcmp(me.mnt_special, "/proc"))
# endif	/* defined(HASFSTYPE) */

	    {

	    /*
	     * Save information on exactly one procfs file system.
	     */
		if (procfs)
		    Mtprocfs = (struct mounts *)NULL;
		else {
		    procfs = 1;
		    Mtprocfs = mtp;
		}
	    }
#endif	/* defined(HASPROCFS) */

	/*
	 * Interpolate a possible file system (mounted-on device) name link.
	 */
	    if (!(dn = mkstrcpy(me.mnt_special, (MALLOC_S *)NULL)))
		goto no_space_for_mount;
	    mtp->fsname = dn;
	    ln = Readlink(dn);
	    dn = (char *)NULL;
	/*
	 * Stat() the file system (mounted-on) name and add file system
	 * information to the local mounts structure.
	 */
	    if (!ln || statsafely(ln, &sb))
		sb.st_mode = 0;
	    mtp->fsnmres = ln;
	    mtp->fs_mode = sb.st_mode;
	    Lmi = mtp;
        }
	(void) fclose(mfp);
/*
 * Clean up and return local mount info table address.
 */
	if (dn)
	    (void) free((FREE_P *)dn);
	Lmist = 1;
	return(Lmi);
}