/* * Read a mount table into memory */ mntlist * read_mtab(char *fs, const char *mnttabname) { mntlist **mpp, *mhp; struct statfs *mntbufp, *mntp; int nloc = getmntinfo(&mntbufp, MNT_NOWAIT); if (nloc == 0) { plog(XLOG_ERROR, "Can't read mount table"); return 0; } mpp = &mhp; for (mntp = mntbufp; mntp < mntbufp + nloc; mntp++) { /* * Allocate a new slot */ *mpp = ALLOC(struct mntlist); /* * Copy the data returned by getmntent */ (*mpp)->mnt = mnt_dup(mntp); /* * Move to next pointer */ mpp = &(*mpp)->mnext; } /* terminate the linked list */ *mpp = NULL; return mhp; }
/* * Read a mount table into memory */ mntlist * read_mtab(char *fs, const char *mnttabname) { mntlist **mpp, *mhp; int i; char *mntinfo = 0, *cp; struct vmount *vp; int ret; /* * First figure out size of mount table * and allocate space for a copy... * Then get mount table for real. */ ret = mntctl(MCTL_QUERY, sizeof(i), &i); if (ret == 0) { mntinfo = xmalloc(i); ret = mntctl(MCTL_QUERY, i, mntinfo); } if (ret <= 0) { plog(XLOG_ERROR, "mntctl: %m"); goto out; } mpp = &mhp; for (i = 0, cp = mntinfo; i < ret; i++, cp += vp->vmt_length) { vp = (struct vmount *) cp; /* * Allocate a new slot */ *mpp = ALLOC(struct mntlist); /* * Copy the data returned by mntctl */ (*mpp)->mnt = mnt_dup(vp); /* * Move to next pointer */ mpp = &(*mpp)->mnext; } *mpp = 0; out: if (mntinfo) XFREE(mntinfo); return mhp; }
/* * Read a mount table into memory */ mntlist * read_mtab(char *fs, const char *mnttabname) { mntlist **mpp, *mhp; mntent_t *mep; FILE *mfp = open_locked_mtab(mnttabname, "r+", fs); if (!mfp) return 0; mpp = &mhp; /* * XXX - In SunOS 4 there is (yet another) memory leak * which loses 1K the first time getmntent is called. * (jsp) */ while ((mep = getmntent(mfp))) { /* * Allocate a new slot */ *mpp = ALLOC(struct mntlist); /* * Copy the data returned by getmntent */ (*mpp)->mnt = mnt_dup(mep); /* * Move to next pointer */ mpp = &(*mpp)->mnext; } *mpp = NULL; #ifdef MOUNT_TABLE_ON_FILE /* * If we are not updating the mount table then we * can free the resources held here, otherwise they * must be held until the mount table update is complete */ mnt_file = mfp; #else /* not MOUNT_TABLE_ON_FILE */ endmntent(mfp); #endif /* not MOUNT_TABLE_ON_FILE */ return mhp; }
/* * Read a mount table into memory */ mntlist * read_mtab(char *fs, const char *mnttabname) { mntlist **mpp, *mhp; /* From: Piete Brooks <*****@*****.**> */ int fd; mntent_t mountbuffer[NMOUNT], *fs_data; int ret; int nmts; if (lockmnttab() != 0) return (mntlist *) 0; fd = open(mnttabname, O_RDONLY); if (fd < 0) { plog(XLOG_ERROR, "Can't open %s: %m", mnttabname); return (mntlist *) 0; } mpp = &mhp; while ((ret = read(fd, (char *) mountbuffer, NMOUNT * sizeof(mntent_t))) > 0) { nmts = ret / sizeof(mntent_t); for (fs_data = mountbuffer; fs_data < &mountbuffer[nmts]; fs_data++) { /* * Allocate a new slot */ *mpp = ALLOC(struct mntlist); /* * Copy the data returned by getmntent */ (*mpp)->mnt = mnt_dup(fs_data); /* * Move to next pointer */ mpp = &(*mpp)->mnext; } } if (ret < 0) { plog(XLOG_ERROR, "read error on %s: %m", mnttabname); unlockmnttab(); mhp = (mntlist *) 0; } *mpp = 0; close(fd); return mhp; }
/* * Read a mount table into memory */ mntlist * read_mtab(char *fs, const char *mnttabname) { mntlist **mpp, *mhp; FILE *fp; mntent_t mountbuf; int ret; if (lockmnttab() < 0) /* failed locking */ return NULL; fp = fopen(mnttabname, "r"); if (fp == NULL) { plog(XLOG_ERROR, "Can't open %s: %m", mnttabname); return NULL; } mpp = &mhp; while ((ret = getmntent(fp, &mountbuf)) == 0) { /* * Allocate a new slot */ *mpp = ALLOC(struct mntlist); /* * Copy the data returned by getmntent */ (*mpp)->mnt = mnt_dup(&mountbuf); /* * Move to next pointer */ mpp = &(*mpp)->mnext; } if (ret > 0) { plog(XLOG_ERROR, "read error on %s: %m", mnttabname); unlockmnttab(); mhp = NULL; } *mpp = NULL; fclose(fp); return mhp; }
/* * Read a mount table into memory */ mntlist * read_mtab(char *fs, const char *mnttabname) { mntlist **mpp, *mhp; /* From: Piete Brooks <*****@*****.**> */ int loc = 0; struct fs_data mountbuffer[NMOUNT], *fs_data; int ret; mpp = &mhp; while ((ret = getmountent(&loc, mountbuffer, NMOUNT)) > 0) { for (fs_data = mountbuffer; fs_data < &mountbuffer[ret]; fs_data++) { /* * Allocate a new slot */ *mpp = ALLOC(struct mntlist); /* * Copy the data returned by getmntent */ (*mpp)->mnt = mnt_dup(fs_data); /* * Move to next pointer */ mpp = &(*mpp)->mnext; } } if (ret < 0) { plog(XLOG_ERROR, "getmountent: %m"); return 0; } *mpp = NULL; return mhp; }
/* * Read a mount table into memory */ mntlist * read_mtab(char *fs, const char *mnttabname) { mntlist **mpp, *mhp; int i; char *mntinfo = 0, *cp; struct vmount *vp; int ret; int maxtry = 10; /* maximum number of times to try mntctl */ /* * Figure out size of mount table and allocate space for a copy. Then get * mount table for real. We repeat this loop at most 10 times to minimze * the chance of a race condition (something gets un/mounted in between * calls to mntctl() */ i = sizeof(int); do { if (mntinfo) XFREE(mntinfo); mntinfo = xmalloc(i); ret = mntctl(MCTL_QUERY, i, mntinfo); if (ret == 0) i = *(int*) mntinfo; if (--maxtry <= 0) { plog(XLOG_ERROR, "mntctl: could not get a stable result"); ret = -1; errno = EINVAL; break; } } while (ret == 0); if (ret < 0) { plog(XLOG_ERROR, "mntctl: %m"); goto out; } mpp = &mhp; for (i = 0, cp = mntinfo; i < ret; i++, cp += vp->vmt_length) { vp = (struct vmount *) cp; /* * Allocate a new slot */ *mpp = ALLOC(struct mntlist); /* * Copy the data returned by mntctl */ (*mpp)->mnt = mnt_dup(vp); /* * Move to next pointer */ mpp = &(*mpp)->mnext; } *mpp = 0; out: if (mntinfo) XFREE(mntinfo); return mhp; }