static char *gds_check_mtab(char *dev_path) { #ifdef HAVE_MNTENT_H FILE * mtab = NULL; struct mntent * part = NULL; if ((mtab = setmntent(MOUNTED_FILE, "r")) != NULL) { while (NULL != (part = getmntent(mtab))) { if ((NULL != part->mnt_dir) && (NULL != part->mnt_type) && (0 == strcmp(part->mnt_dir, dev_path))) { endmntent(mtab); return strdup(part->mnt_type); } } endmntent(mtab); } #endif return NULL; }
/** * Solaris special block device mountpoint method. Filesystem must be mounted. * In the case of success, mountpoint is stored in filesystem information * structure for later use. * * @param inf Information structure where resulting data will be stored * @param blockdev Identifies block special device * @return NULL in the case of failure otherwise mountpoint */ char *device_mountpoint_sysdep(Info_T inf, char *blockdev) { struct mnttab mnt; FILE *mntfd; ASSERT(inf); ASSERT(blockdev); if ((mntfd = fopen("/etc/mnttab", "r")) == NULL) { LogError("Cannot open /etc/mnttab file\n"); return NULL; } while (getmntent(mntfd, &mnt) == 0) { char real_mnt_special[PATH_MAX+1]; if (realpath(mnt.mnt_special, real_mnt_special) && IS(real_mnt_special, blockdev)) { fclose(mntfd); inf->priv.filesystem.mntpath = Str_dup(mnt.mnt_mountp); return inf->priv.filesystem.mntpath; } } fclose(mntfd); return NULL; }
int device_is_mounted(const char *dev) { FILE *mtab; struct mntent *mnt; int ret = 0; mtab = setmntent(MTAB, "r"); if (!mtab) return 0; while ((mnt = getmntent(mtab)) != NULL) { if (!mnt->mnt_fsname) continue; if (!strcmp(mnt->mnt_fsname, dev)) { ret = 1; break; } } endmntent(mtab); return ret; }
int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type) { FILE *fp; struct mntent *mnt; fp = setmntent(MOUNTED, "r"); if (fp == NULL) return 0; while ((mnt = getmntent(fp)) != NULL) { if ((strcmp(mnt->mnt_fsname, spec1) == 0 || strcmp(mnt->mnt_fsname, spec2) == 0) && (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) && (type == NULL || strcmp(mnt->mnt_type, type) == 0)) { endmntent(fp); return(EEXIST); } } endmntent(fp); return 0; }
int main (int argc, char **argv) { FILE *fp; struct mnttab mp; int ret; if (argc != 2) err_quit ("Usage: hasmntopt mount_option"); if ((fp = fopen ("/etc/mnttab", "r")) == NULL) err_msg ("Can't open /etc/mnttab"); while ((ret = getmntent (fp, &mp)) == 0) { if (hasmntopt (&mp, argv [1])) printf ("%s\n", mp.mnt_mountp); } if (ret != -1) err_quit ("Bad /etc/mnttab file.\n"); return (0); }
static int cachedev_get(char *filename) { FILE *mounts; struct mntent *ent; int cache_fd; pid_t pid = getpid(); struct stat st, st_mnt; /* * The file may not exist yet. If it does not exist, statfs the * directory instead. If a relative path without a '/' was specified, * use the cwd. */ if (stat(filename, &st) < 0) { fprintf(stderr, "flashcache_trim_unlink: nonexistent file %s\n", filename); exit(1); } mounts = setmntent("/etc/mtab", "r"); while ((ent = getmntent(mounts)) != NULL ) { stat(ent->mnt_dir, &st_mnt); if (memcmp(&st.st_dev, &st_mnt.st_dev, sizeof(dev_t)) == 0) break; } endmntent(mounts); if (ent == NULL) return -1; cache_fd = open(ent->mnt_fsname, O_RDONLY); if (cache_fd < 1) return -1; if (ioctl(cache_fd, FLASHCACHEADDNCPID, &pid) < 0) { close(cache_fd); return -1; } else (void)ioctl(cache_fd, FLASHCACHEDELNCPID, &pid); fprintf(stderr, "Found cache fd for fsname %s\n", ent->mnt_fsname); return cache_fd; }
bool isBlockDeviceMounted(const char *aBlkDevice) { FILE *f = fopen("/proc/mounts", "r"); struct mntent *entry; bool isMounted = false; struct stat blkDevStat; if (stat(aBlkDevice, &blkDevStat) != 0) { logmsg(LLVL_ERROR, "Unable to stat %s to determine if it's mounted. Assuming it is mounted for safety. Stat reported: %s\n", aBlkDevice, strerror(errno)); return true; } while ((entry = getmntent(f)) != NULL) { if (strcmp(entry->mnt_fsname, aBlkDevice) == 0) { /* Names match, definitely mounted! */ logmsg(LLVL_DEBUG, "%s mounted at %s\n", aBlkDevice, entry->mnt_dir); isMounted = true; break; } if (strcmp(entry->mnt_fsname, "none")) { /* Check major/minor number of device */ struct stat newDevStat; if (stat(entry->mnt_fsname, &newDevStat) == 0) { if (newDevStat.st_rdev == blkDevStat.st_rdev) { /* Major/minor is identical */ logmsg(LLVL_DEBUG, "%s has identical struct stat.st_rdev with %s, mounted at %s\n", aBlkDevice, entry->mnt_fsname, entry->mnt_dir); isMounted = true; break; } } } } fclose(f); return isMounted; }
struct mtab_list *xgetmountlist(char *path) { struct mtab_list *mtlist = 0, *mt; struct mntent *me; FILE *fp; char *p = path ? path : "/proc/mounts"; if (!(fp = setmntent(p, "r"))) perror_exit("bad %s", p); // The "test" part of the loop is done before the first time through and // again after each "increment", so putting the actual load there avoids // duplicating it. If the load was NULL, the loop stops. while ((me = getmntent(fp))) { mt = xzalloc(sizeof(struct mtab_list) + strlen(me->mnt_fsname) + strlen(me->mnt_dir) + strlen(me->mnt_type) + strlen(me->mnt_opts) + 4); dlist_add_nomalloc((void *)&mtlist, (void *)mt); // Collect details about mounted filesystem // Don't report errors, just leave data zeroed if (!path) { stat(me->mnt_dir, &(mt->stat)); statvfs(me->mnt_dir, &(mt->statvfs)); } // Remember information from /proc/mounts mt->dir = stpcpy(mt->type, me->mnt_type)+1; mt->device = stpcpy(mt->dir, me->mnt_dir)+1; mt->opts = stpcpy(mt->device, me->mnt_fsname)+1; strcpy(mt->opts, me->mnt_opts); octal_deslash(mt->dir); octal_deslash(mt->device); } endmntent(fp); return mtlist; }
static void read_table(void) { FILE *tab; struct mnttab ent; MountPoint *mp; # ifdef HAVE_FCNTL_H struct flock lb; # endif clear_table(); if(!file_exists(THE_FSTAB)) return; tab = fopen(THE_FSTAB, "r"); g_return_if_fail(tab != NULL); # ifdef HAVE_FCNTL_H lb.l_type = F_RDLCK; lb.l_whence = 0; lb.l_start = 0; lb.l_len = 0; fcntl(fileno(tab), F_SETLKW, &lb); # endif while (getmntent(tab, &ent)==0) { if (strcmp(ent.mnt_special, "swap") == 0) continue; mp = g_malloc(sizeof(MountPoint)); mp->dir = g_strdup(ent.mnt_mountp); mp->name = g_strdup(ent.mnt_special); g_hash_table_insert(fstab_mounts, mp->dir, mp); } fclose(tab); }
int lxc_cgroup_enter(const char *cgpath, pid_t pid) { char path[MAXPATHLEN]; FILE *file = NULL, *fout; struct mntent *mntent; int ret, retv = -1; file = setmntent(MTAB, "r"); if (!file) { SYSERROR("failed to open %s", MTAB); return -1; } while ((mntent = getmntent(file))) { if (strcmp(mntent->mnt_type, "cgroup")) continue; if (!mount_has_subsystem(mntent)) continue; ret = snprintf(path, MAXPATHLEN, "%s/%s/tasks", mntent->mnt_dir, cgpath); if (ret < 0 || ret >= MAXPATHLEN) { ERROR("entering cgroup"); goto out; } fout = fopen(path, "w"); if (!fout) { ERROR("entering cgroup"); goto out; } fprintf(fout, "%d\n", (int)pid); fclose(fout); } retv = 0; out: endmntent(file); return retv; }
/* Refreshes the mount list in the main window */ void ManagerWindow::RefreshMounts() { FILE *fp = setmntent("/proc/mounts","r"); struct mntent *fs; viewfs_mounts.clear(); ui->mountList->clear(); while((fs = getmntent(fp)) != NULL) { if(!strcmp(fs->mnt_type,"viewfs")) /* It's a mounted viewfs! */ { viewfs_mounts.push_back(fs); QString str; str.append(fs->mnt_fsname); str.append(" -> "); str.append(fs->mnt_dir); ui->mountList->addItem(str); } } endmntent(fp); }
struct mntent* findMountForPath(const char* path) { FILE* f = setmntent("/proc/mounts", "r"); struct mntent *ent, *rv = 0; if (!f) { LOG << "Cannot access /proc/mounts!\n"; return 0; } while ((ent = getmntent(f))) { if (strncmp(ent->mnt_dir, path, strlen(ent->mnt_dir)) == 0) { rv = ent; break; } } endmntent(f); return rv; }
void SolarisDiskSpace::PartitionName(int number, char* name) { FILE* mntfp; if (name != NULL && number > 0 && number <= mNumberOfPartitions) { struct mnttab mntent; mntfp = fopen ("/etc/mnttab", "r"); strncpy(mDiskPart[number - 1]->path, name, 255); if (mntfp != NULL) { while (getmntent (mntfp, &mntent) == 0) if (strcmp (name, mntent.mnt_mountp) == 0) { strncpy(mDiskPart[number - 1]->device, mntent.mnt_special, 255); break; } fclose (mntfp); } } }
void OGUIFileBrowser::readSystem() { struct mntent *ent; FILE *aFile; // ToDo (damiles): Windows and osx support aFile = setmntent("/proc/mounts", "r"); if (aFile == NULL) { perror("setmntent"); exit(1); } while (NULL != (ent = getmntent(aFile))) { if(ent->mnt_fsname[0]=='/'){ string mnt_dir(ent->mnt_dir); string filename; std::size_t found = mnt_dir.find_last_of("/\\"); filename= mnt_dir.substr(found+1) ; if(filename.compare("")==0) filename="/"; OGUIFile* file= new OGUIFile(_window, filename.c_str(),ent->mnt_dir, 2, "",0); _system.push_back(file); } } endmntent(aFile); }
unsigned long long SocamFSBusinessLogic::getMaxFileSystemSize() { struct mntent *ent; FILE *aFile; unsigned long long size = 0; aFile = setmntent("/proc/mounts", "r"); if (aFile == NULL) { perror("setmntent"); exit(1); } while (NULL != (ent = getmntent(aFile))) { if(!strcmp(ent->mnt_fsname, ROOTFS)) { size = getDiskSpace(ent->mnt_dir); break; } } endmntent(aFile); cout << "SocamFSBusinessLogic::getMaxFileSystemSize=" << size << endl; return size / 1024; }
void make_root_rprivate(const char * mounts_path) { FILE * mounts = setmntent(mounts_path, "r"); struct mntent * entry; while ((entry = getmntent(mounts)) != NULL) { if(strcmp(entry->mnt_dir, "/") == 0) { int x = mount(entry->mnt_dir, entry->mnt_dir, entry->mnt_type, MS_REC | MS_PRIVATE, entry->mnt_opts); if(x != 0) { char y[50]; sprintf(y, "Failed to make root shared %s from %s\t", entry->mnt_fsname, entry->mnt_dir); perror(y); exit(x); } else printf("Success making root recursively private.\n"); break; } } endmntent(mounts); }
struct mntent *findmntents(char *file, int swp, int (*func)(struct mntent *mnt, uint flags), uint flags) { struct mntent *mnt; struct stat st_buf; dev_t file_dev=0, file_rdev=0; ino_t file_ino=0; FILE *f; if ((f = setmntent(swp ? "/proc/swaps": "/proc/mounts", "r")) == NULL) return NULL; if (stat(file, &st_buf) == 0) { if (S_ISBLK(st_buf.st_mode)) { file_rdev = st_buf.st_rdev; } else { file_dev = st_buf.st_dev; file_ino = st_buf.st_ino; } } while ((mnt = getmntent(f)) != NULL) { /* Always ignore rootfs mount */ if (strcmp(mnt->mnt_fsname, "rootfs") == 0) continue; if (strcmp(file, mnt->mnt_fsname) == 0 || strcmp(file, mnt->mnt_dir) == 0 || is_same_device(mnt->mnt_fsname, file_rdev , file_dev, file_ino)) { if (func == NULL) break; (*func)(mnt, flags); } } endmntent(f); return mnt; }
static int check_mnt_point(const char *mpoint) { printf("\n\033[34m<<<<check_mnt_point\033[0m\n"); FILE *fp; struct mntent *ment; fp = setmntent("/etc/mtab", "r"); if (fp == NULL) { D_ERROR("check_mnt_point: Failed to open /etc/mtab\n"); return -1; } while (!feof(fp)) { ment = getmntent(fp); if (ment != NULL && !strcmp(ment->mnt_dir, mpoint)) { D_INFO("ment->mnt_opts:%s\n", ment->mnt_opts); D_INFO("ment->mnt_opts:[%c%c]\n", ment->mnt_opts[0], ment->mnt_opts[1]); if((ment->mnt_opts[0] == 'r') && (ment->mnt_opts[1] == 'w')) //rw: writable { D_INFO("%s\n", ment->mnt_dir); return 0; } else { D_INFO("\033[31mRead-only device\033[0m\n"); return -1; } } } return -1; }
int commandtool::deautomnt_part(const blockdevice *block) { const char *fstab_tmp = "/etc/fstab~"; struct mntent *m1 = NULL; FILE *f1= NULL,*f2 = NULL; f1 = setmntent(FSTAB_FILE, "r"); f2 = setmntent(fstab_tmp, "w+"); if (f1 == NULL || f2 == NULL) { util::log(QString("deautomount %1 error %2").arg(block->device).arg(errno)); return -1; } while ((m1 = getmntent(f1))) { QString fsname = QString(m1->mnt_fsname); bool has = false; foreach(const QString& symlink, block->symlinks) { if (fsname == symlink) { has = true; qDebug() <<symlink; break; } } if (!has) { addmntent(f2,m1); } } endmntent(f1); endmntent(f2); rename(fstab_tmp, FSTAB_FILE); return 0; }
/* * Return the mount point associated with the filesystem * on which "file" resides. Returns NULL on failure. */ static char * mountp( char *file, int *nfs) { struct stat sb; FILE *mtab; dev_t devno; static struct mnttab mnt; if (stat(file, &sb) < 0) { return( NULL ); } devno = sb.st_dev; if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) { return( NULL ); } while ( getmntent( mtab, &mnt ) == 0 ) { /* local fs */ if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) { fclose( mtab ); return mnt.mnt_mountp; } /* check for nfs. we probably should use * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */ if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) && strchr(mnt.mnt_special, ':')) { *nfs = 1; fclose( mtab ); return mnt.mnt_special; } } fclose( mtab ); return( NULL ); }
int main() { int entsuccess=0; FILE* fstab = setmntent("fstab", "r"); struct mntent* e; if (!fstab) { perror("setmntent()"); return 1; } while ((e = getmntent(fstab))) { entsuccess=1; #if 0 char *mnt_fsname; /* name of mounted file system */ char *mnt_dir; /* file system path prefix */ char *mnt_type; /* mount type (see mntent.h) */ char *mnt_opts; /* mount options (see mntent.h) */ int mnt_freq; /* dump frequency in days */ int mnt_passno; /* pass number on parallel fsck */ #endif printf("fsname %s\n dir %s\n type %s\n opts %s\n freq %d\n passno %d\n\n", e->mnt_fsname,e->mnt_dir,e->mnt_type,e->mnt_opts,e->mnt_freq,e->mnt_passno); } if ( !entsuccess ) { perror("getmntent()"); return 2; } printf("closing /etc/fstab\n"); assert ( 1 == endmntent(fstab)); printf("closing /etc/fstab again\n"); assert ( 1 == endmntent(fstab)); /* endmntent must always return 1 */ printf("entmntent(0)\n"); assert ( 1 == endmntent(0)); /* causes a segfault with diet libc */ return 0; }
/* Read mount information from /proc/mounts or /etc/mtab */ int tests_get_mount_info(struct mntent *info) { FILE *f; struct mntent *entry; int found = 0; f = fopen("/proc/mounts", "rb"); if (!f) f = fopen("/etc/mtab", "rb"); CHECK(f != NULL); while (!found) { entry = getmntent(f); if (entry) { if (strcmp(entry->mnt_dir, tests_file_system_mount_dir) == 0) { found = 1; *info = *entry; } } else break; } CHECK(fclose(f) == 0); return found; }
int VFS_FS_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { int ret = SYSINFO_RET_FAIL; struct mnttab mt; FILE *f; struct zbx_json j; zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); /* opening the mounted filesystems file */ if (NULL != (f = fopen("/etc/mnttab", "r"))) { /* fill mnttab structure from file */ while (-1 != getmntent(f, &mt)) { zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#FSNAME}", mt.mnt_mountp, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#FSTYPE}", mt.mnt_fstype, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); } zbx_fclose(f); ret = SYSINFO_RET_OK; } zbx_json_close(&j); SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer)); zbx_json_free(&j); return ret; }
/** * Linux special block device mountpoint method. Filesystem must be mounted. * In the case of success, mountpoint is stored in filesystem information * structure for later use. * * @param inf Information structure where resulting data will be stored * @param blockdev Identifies block special device * @return NULL in the case of failure otherwise mountpoint */ char *device_mountpoint_sysdep(Info_T inf, char *blockdev) { FILE *mntfd; struct mntent *mnt; ASSERT(inf); ASSERT(blockdev); if ((mntfd = setmntent("/etc/mtab", "r")) == NULL) { LogError("%s: Cannot open /etc/mtab file\n", prog); return NULL; } while ((mnt = getmntent(mntfd)) != NULL) { char realpathbuf[PATH_MAX+1]; /* Try to compare the the filesystem as is, if failed, try to use the symbolic link target */ if (IS(blockdev, mnt->mnt_fsname) || (realpath(mnt->mnt_fsname, realpathbuf) && ! strcasecmp(blockdev, realpathbuf))) { endmntent(mntfd); inf->priv.filesystem.mntpath = Str_dup(mnt->mnt_dir); return inf->priv.filesystem.mntpath; } } endmntent(mntfd); LogError("Device %s not found in /etc/mtab\n", blockdev); return NULL; }
static void searchmnttab(char **specialp, char **mountpointp) { FILE *mnttab; struct mnttab mntbuf; char *blockspecial; blockspecial = getfullblkname(*specialp); if (blockspecial == NULL) blockspecial = *specialp; if ((mnttab = fopen(MNTTAB, "r")) == NULL) return; while (getmntent(mnttab, &mntbuf) == NULL) if (strcmp(mntbuf.mnt_fstype, MNTTYPE_UFS) == 0) if ((strcmp(mntbuf.mnt_mountp, *specialp) == 0) || (strcmp(mntbuf.mnt_special, blockspecial) == 0) || (strcmp(mntbuf.mnt_special, *specialp) == 0)) { *specialp = strdup(mntbuf.mnt_special); *mountpointp = strdup(mntbuf.mnt_mountp); return; } fclose(mnttab); }
/* Return next entry in fstab, skipping ignore entries. I also put in some ugly hacks here to skip comments and blank lines. */ struct mntent * getfsent (void) { struct mntent *fstab; if (!F_fstab && !setfsent()) return 0; for (;;) { fstab = getmntent (F_fstab); if (fstab == NULL) { if (!feof (F_fstab) && !ferror (F_fstab)) continue; else break; } else if ((*fstab->mnt_fsname != '#') && !streq (fstab->mnt_type, MNTTYPE_IGNORE)) break; } return fstab; }
/* is path already mounted? */ int is_mounted(const char *path) { /* init mount information */ char *mount_file = MOUNT_FILE; FILE *file_pointer = NULL; struct mntent *file_system; /* get mount information */ file_pointer = setmntent(mount_file, "r"); if (file_pointer == NULL) { fprintf(stderr, "%s: %s: cannot open: %s (%s)\n", NAME, "ERROR", path, strerror(errno)); exit(1); } /* detect mount status of path */ int status = 0; while ((file_system = getmntent(file_pointer)) != NULL) if ((strcmp(file_system->mnt_fsname, path) == 0) || (strcmp(file_system->mnt_dir, path) == 0)) { printf("%s: %s: %s is mounted as %s\n", NAME, "ERROR", path, file_system->mnt_fsname); status = 1; } /* release pointer */ endmntent(file_pointer); /* exit */ return status; }
void generate_disk_report() { // let me get mnt first FILE* fp; struct mntent *fs; notf_file = fopen(notfile,"w"); fp = setmntent("/etc/mtab","r"); if ( fp == NULL ){ fprintf(stderr,": could not open: %s\n",strerror(errno)); exit(1); } while ( (fs = getmntent(fp)) != NULL ) do_statfs(fs);//print_mount(fs); endmntent(fp); fclose(notf_file); }
/* * for each mounted cgroup, attach a pid to the cgroup for the container */ int lxc_cgroup_attach(const char *name, pid_t pid) { struct mntent *mntent; FILE *file = NULL; int err = -1; int found = 0; file = setmntent(MTAB, "r"); if (!file) { SYSERROR("failed to open %s", MTAB); return -1; } while ((mntent = getmntent(file))) { DEBUG("checking '%s' (%s)", mntent->mnt_dir, mntent->mnt_type); if (strcmp(mntent->mnt_type, "cgroup")) continue; if (!mount_has_subsystem(mntent)) continue; INFO("[%d] found cgroup mounted at '%s',opts='%s'", ++found, mntent->mnt_dir, mntent->mnt_opts); err = lxc_one_cgroup_attach(name, mntent, pid); if (err) goto out; }; if (!found) ERROR("No cgroup mounted on the system"); out: endmntent(file); return err; }
struct mount_entry * read_file_system_list (bool need_fs_type) { struct mount_entry *mount_list; struct mount_entry *me; struct mount_entry **mtail = &mount_list; #ifdef MOUNTED_LISTMNTENT { struct tabmntent *mntlist, *p; struct mntent *mnt; struct mount_entry *me; /* the third and fourth arguments could be used to filter mounts, but Crays doesn't seem to have any mounts that we want to remove. Specifically, automount create normal NFS mounts. */ if (listmntent (&mntlist, KMTAB, NULL, NULL) < 0) return NULL; for (p = mntlist; p; p = p->next) { mnt = p->ment; me = xmalloc (sizeof *me); me->me_devname = xstrdup (mnt->mnt_fsname); me->me_mountdir = xstrdup (mnt->mnt_dir); me->me_type = xstrdup (mnt->mnt_type); me->me_type_malloced = 1; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = -1; *mtail = me; mtail = &me->me_next; } freemntlist (mntlist); } #endif #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */ { struct mntent *mnt; char *table = MOUNTED; FILE *fp; char *devopt; fp = setmntent (table, "r"); if (fp == NULL) return NULL; while ((mnt = getmntent (fp))) { me = xmalloc (sizeof *me); me->me_devname = xstrdup (mnt->mnt_fsname); me->me_mountdir = xstrdup (mnt->mnt_dir); me->me_type = xstrdup (mnt->mnt_type); me->me_type_malloced = 1; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); #if ! (__CYGWIN__ || __MSYS__) me->me_remote = ME_REMOTE (me->me_devname, me->me_type); #else me->me_remote = cygremote (me->me_devname, &me->me_type); #endif devopt = strstr (mnt->mnt_opts, "dev="); if (devopt) me->me_dev = strtoul (devopt + 4, NULL, 16); else me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ /* Add to the linked list. */ *mtail = me; mtail = &me->me_next; } if (endmntent (fp) == 0) goto free_then_fail; } #endif /* MOUNTED_GETMNTENT1. */ #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */ { struct statfs *fsp; int entries; entries = getmntinfo (&fsp, MNT_NOWAIT); if (entries < 0) return NULL; for (; entries-- > 0; fsp++) { char *fs_type = fsp_to_string (fsp); me = xmalloc (sizeof *me); me->me_devname = xstrdup (fsp->f_mntfromname); me->me_mountdir = xstrdup (fsp->f_mntonname); me->me_type = fs_type; me->me_type_malloced = 0; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ /* Add to the linked list. */ *mtail = me; mtail = &me->me_next; } } #endif /* MOUNTED_GETMNTINFO */ #ifdef MOUNTED_GETMNT /* Ultrix. */ { int offset = 0; int val; struct fs_data fsd; while (errno = 0, 0 < (val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY, (char *) 0))) { me = xmalloc (sizeof *me); me->me_devname = xstrdup (fsd.fd_req.devname); me->me_mountdir = xstrdup (fsd.fd_req.path); me->me_type = gt_names[fsd.fd_req.fstype]; me->me_type_malloced = 0; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = fsd.fd_req.dev; /* Add to the linked list. */ *mtail = me; mtail = &me->me_next; } if (val < 0) goto free_then_fail; } #endif /* MOUNTED_GETMNT. */ #if defined MOUNTED_FS_STAT_DEV /* BeOS */ { /* The next_dev() and fs_stat_dev() system calls give the list of all file systems, including the information returned by statvfs() (fs type, total blocks, free blocks etc.), but without the mount point. But on BeOS all file systems except / are mounted in the rootfs, directly under /. The directory name of the mount point is often, but not always, identical to the volume name of the device. We therefore get the list of subdirectories of /, and the list of all file systems, and match the two lists. */ DIR *dirp; struct rootdir_entry { char *name; dev_t dev; ino_t ino; struct rootdir_entry *next; }; struct rootdir_entry *rootdir_list; struct rootdir_entry **rootdir_tail; int32 pos; dev_t dev; fs_info fi; /* All volumes are mounted in the rootfs, directly under /. */ rootdir_list = NULL; rootdir_tail = &rootdir_list; dirp = opendir ("/"); if (dirp) { struct dirent *d; while ((d = readdir (dirp)) != NULL) { char *name; struct stat statbuf; if (strcmp (d->d_name, "..") == 0) continue; if (strcmp (d->d_name, ".") == 0) name = xstrdup ("/"); else { name = xmalloc (1 + strlen (d->d_name) + 1); name[0] = '/'; strcpy (name + 1, d->d_name); } if (lstat (name, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode)) { struct rootdir_entry *re = xmalloc (sizeof *re); re->name = name; re->dev = statbuf.st_dev; re->ino = statbuf.st_ino; /* Add to the linked list. */ *rootdir_tail = re; rootdir_tail = &re->next; } else free (name); } closedir (dirp); } *rootdir_tail = NULL; for (pos = 0; (dev = next_dev (&pos)) >= 0; ) if (fs_stat_dev (dev, &fi) >= 0) { /* Note: fi.dev == dev. */ struct rootdir_entry *re; for (re = rootdir_list; re; re = re->next) if (re->dev == fi.dev && re->ino == fi.root) break; me = xmalloc (sizeof *me); me->me_devname = xstrdup (fi.device_name[0] != '\0' ? fi.device_name : fi.fsh_name); me->me_mountdir = xstrdup (re != NULL ? re->name : fi.fsh_name); me->me_type = xstrdup (fi.fsh_name); me->me_type_malloced = 1; me->me_dev = fi.dev; me->me_dummy = 0; me->me_remote = (fi.flags & B_FS_IS_SHARED) != 0; /* Add to the linked list. */ *mtail = me; mtail = &me->me_next; } *mtail = NULL; while (rootdir_list != NULL) { struct rootdir_entry *re = rootdir_list; rootdir_list = re->next; free (re->name); free (re); } } #endif /* MOUNTED_FS_STAT_DEV */ #if defined MOUNTED_GETFSSTAT /* __alpha running OSF_1 */ { int numsys, counter; size_t bufsize; struct statfs *stats; numsys = getfsstat ((struct statfs *)0, 0L, MNT_NOWAIT); if (numsys < 0) return (NULL); if (SIZE_MAX / sizeof *stats <= numsys) xalloc_die (); bufsize = (1 + numsys) * sizeof *stats; stats = xmalloc (bufsize); numsys = getfsstat (stats, bufsize, MNT_NOWAIT); if (numsys < 0) { free (stats); return (NULL); } for (counter = 0; counter < numsys; counter++) { me = xmalloc (sizeof *me); me->me_devname = xstrdup (stats[counter].f_mntfromname); me->me_mountdir = xstrdup (stats[counter].f_mntonname); me->me_type = xstrdup (FS_TYPE (stats[counter])); me->me_type_malloced = 1; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ /* Add to the linked list. */ *mtail = me; mtail = &me->me_next; } free (stats); } #endif /* MOUNTED_GETFSSTAT */ #if defined MOUNTED_FREAD || defined MOUNTED_FREAD_FSTYP /* SVR[23]. */ { struct mnttab mnt; char *table = "/etc/mnttab"; FILE *fp; fp = fopen (table, "r"); if (fp == NULL) return NULL; while (fread (&mnt, sizeof mnt, 1, fp) > 0) { me = xmalloc (sizeof *me); # ifdef GETFSTYP /* SVR3. */ me->me_devname = xstrdup (mnt.mt_dev); # else me->me_devname = xmalloc (strlen (mnt.mt_dev) + 6); strcpy (me->me_devname, "/dev/"); strcpy (me->me_devname + 5, mnt.mt_dev); # endif me->me_mountdir = xstrdup (mnt.mt_filsys); me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ me->me_type = ""; me->me_type_malloced = 0; # ifdef GETFSTYP /* SVR3. */ if (need_fs_type) { struct statfs fsd; char typebuf[FSTYPSZ]; if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1) { me->me_type = xstrdup (typebuf); me->me_type_malloced = 1; } } # endif me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); /* Add to the linked list. */ *mtail = me; mtail = &me->me_next; } if (ferror (fp)) { /* The last fread() call must have failed. */ int saved_errno = errno; fclose (fp); errno = saved_errno; goto free_then_fail; } if (fclose (fp) == EOF) goto free_then_fail; } #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP. */ #ifdef MOUNTED_GETMNTTBL /* DolphinOS goes it's own way */ { struct mntent **mnttbl = getmnttbl (), **ent; for (ent=mnttbl;*ent;ent++) { me = xmalloc (sizeof *me); me->me_devname = xstrdup ( (*ent)->mt_resource); me->me_mountdir = xstrdup ( (*ent)->mt_directory); me->me_type = xstrdup ((*ent)->mt_fstype); me->me_type_malloced = 1; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ /* Add to the linked list. */ *mtail = me; mtail = &me->me_next; } endmnttbl (); } #endif #ifdef MOUNTED_GETMNTENT2 /* SVR4. */ { struct mnttab mnt; char *table = MNTTAB; FILE *fp; int ret; int lockfd = -1; # if defined F_RDLCK && defined F_SETLKW /* MNTTAB_LOCK is a macro name of our own invention; it's not present in e.g. Solaris 2.6. If the SVR4 folks ever define a macro for this file name, we should use their macro name instead. (Why not just lock MNTTAB directly? We don't know.) */ # ifndef MNTTAB_LOCK # define MNTTAB_LOCK "/etc/.mnttab.lock" # endif lockfd = open (MNTTAB_LOCK, O_RDONLY); if (0 <= lockfd) { struct flock flock; flock.l_type = F_RDLCK; flock.l_whence = SEEK_SET; flock.l_start = 0; flock.l_len = 0; while (fcntl (lockfd, F_SETLKW, &flock) == -1) if (errno != EINTR) { int saved_errno = errno; close (lockfd); errno = saved_errno; return NULL; } } else if (errno != ENOENT) return NULL; # endif errno = 0; fp = fopen (table, "r"); if (fp == NULL) ret = errno; else { while ((ret = getmntent (fp, &mnt)) == 0) { me = xmalloc (sizeof *me); me->me_devname = xstrdup (mnt.mnt_special); me->me_mountdir = xstrdup (mnt.mnt_mountp); me->me_type = xstrdup (mnt.mnt_fstype); me->me_type_malloced = 1; me->me_dummy = MNT_IGNORE (&mnt) != 0; me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ /* Add to the linked list. */ *mtail = me; mtail = &me->me_next; } ret = fclose (fp) == EOF ? errno : 0 < ret ? 0 : -1; } if (0 <= lockfd && close (lockfd) != 0) ret = errno; if (0 <= ret) { errno = ret; goto free_then_fail; } } #endif /* MOUNTED_GETMNTENT2. */ #ifdef MOUNTED_VMOUNT /* AIX. */ { int bufsize; char *entries, *thisent; struct vmount *vmp; int n_entries; int i; /* Ask how many bytes to allocate for the mounted file system info. */ if (mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize) != 0) return NULL; entries = xmalloc (bufsize); /* Get the list of mounted file systems. */ n_entries = mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries); if (n_entries < 0) { int saved_errno = errno; free (entries); errno = saved_errno; return NULL; } for (i = 0, thisent = entries; i < n_entries; i++, thisent += vmp->vmt_length) { char *options, *ignore; vmp = (struct vmount *) thisent; me = xmalloc (sizeof *me); if (vmp->vmt_flags & MNT_REMOTE) { char *host, *dir; me->me_remote = 1; /* Prepend the remote dirname. */ host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off; dir = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off; me->me_devname = xmalloc (strlen (host) + strlen (dir) + 2); strcpy (me->me_devname, host); strcat (me->me_devname, ":"); strcat (me->me_devname, dir); } else { me->me_remote = 0; me->me_devname = xstrdup (thisent + vmp->vmt_data[VMT_OBJECT].vmt_off); } me->me_mountdir = xstrdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off); me->me_type = xstrdup (fstype_to_string (vmp->vmt_gfstype)); me->me_type_malloced = 1; options = thisent + vmp->vmt_data[VMT_ARGS].vmt_off; ignore = strstr (options, "ignore"); me->me_dummy = (ignore && (ignore == options || ignore[-1] == ',') && (ignore[sizeof "ignore" - 1] == ',' || ignore[sizeof "ignore" - 1] == '\0')); me->me_dev = (dev_t) -1; /* vmt_fsid might be the info we want. */ /* Add to the linked list. */ *mtail = me; mtail = &me->me_next; } free (entries); } #endif /* MOUNTED_VMOUNT. */ *mtail = NULL; return mount_list; free_then_fail: { int saved_errno = errno; *mtail = NULL; while (mount_list) { me = mount_list->me_next; free (mount_list->me_devname); free (mount_list->me_mountdir); if (mount_list->me_type_malloced) free (mount_list->me_type); free (mount_list); mount_list = me; } errno = saved_errno; return NULL; } }