gboolean
ToolsDaemonTcloMountHGFS(RpcInData *data) // IN
{
   VixError err = VIX_OK;
   static char resultBuffer[DEFAULT_RESULT_MSG_MAX_LENGTH];

#if defined(linux)
   /*
    * Look for a vmhgfs mount at /mnt/hgfs. If one exists, nothing
    * else needs to be done.  If one doesn't exist, then mount at
    * that location.
    */
   FILE *mtab;
   struct mntent *mnt;
   Bool vmhgfsMntFound = FALSE;
   if ((mtab = setmntent(_PATH_MOUNTED, "r")) == NULL) {
      err = VIX_E_FAIL;
   } else {
      while ((mnt = getmntent(mtab)) != NULL) {
         if ((strcmp(mnt->mnt_fsname, ".host:/") == 0) &&
             (strcmp(mnt->mnt_type, HGFS_NAME) == 0) &&
             (strcmp(mnt->mnt_dir, "/mnt/hgfs") == 0)) {
             vmhgfsMntFound = TRUE;
             break;
         }
      }
      endmntent(mtab);
   }

   if (!vmhgfsMntFound) {
   /*
    * We need to call the mount program, not the mount system call. The
    * mount program does several additional things, like compute the mount
    * options from the contents of /etc/fstab, and invoke custom mount
    * programs like the one needed for HGFS.
    */
      int ret = system("mount -t vmhgfs .host:/ /mnt/hgfs");
      if (ret == -1 || WIFSIGNALED(ret) ||
          (WIFEXITED(ret) && WEXITSTATUS(ret) != 0)) {
         err = VIX_E_FAIL;
      }
   }
#endif

   /*
    * All tools commands return results that start with an error
    * and a guest-OS-specific error.
    */
   Str_Sprintf(resultBuffer,
               sizeof(resultBuffer),
               "%"FMT64"d %d",
               err,
               Err_Errno());
   RPCIN_SETRETVALS(data, resultBuffer, TRUE);

   return TRUE;
} // ToolsDaemonTcloMountHGFS
Esempio n. 2
0
FSMap *
fs_map_load(void)
{
	FSMap *map = NULL;
	FILE *fp;

	fp = setmntent("/proc/mounts", "r");

	if(fp)
	{
		/* store available mount points in array */
		map = utils_new(1, FSMap);
		map->mps = utils_new(32, MountPoint *);
		map->size = 32;
		map->len = 0;

		struct mntent *ent = getmntent(fp);

		while(ent)
		{
			if(map->len == map->size - 1)
			{
				if(map->size > SIZE_MAX / 2)
				{
					FATAL("misc", "Integer overflow.");

					fprintf(stderr, _("Couldn't allocate memory.\n"));

					abort();
				}

				map->size *= 2;
				map->mps = utils_renew(map->mps, map->size, MountPoint *);
			}

			map->mps[map->len] = utils_new(1, MountPoint);

			memset(map->mps[map->len]->fs, 0, FS_NAME_MAX);
			strncpy(map->mps[map->len]->fs, ent->mnt_type, FS_NAME_MAX - 1);

			memset(map->mps[map->len]->path, 0, PATH_MAX);
			strncpy(map->mps[map->len]->path, ent->mnt_dir, PATH_MAX - 1);

			++map->len;
			ent = getmntent(fp);
		}

		endmntent(fp);

		/* sort array by path in descending order */
		if(map->len)
		{
			qsort(map->mps, map->len, sizeof(MountPoint *), &_fs_map_compare_entries);
		}
	}
	else
	{
Esempio n. 3
0
static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
{
	int rc;

	if (fakeIt) { return 0; }

	// Mount, with fallback to read-only if necessary.

	for(;;) {
		rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
				vfsflags, filteropts);
		if(!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
			break;
		bb_error_msg("%s is write-protected, mounting read-only",
				mp->mnt_fsname);
		vfsflags |= MS_RDONLY;
	}

	// Abort entirely if permission denied.

	if (rc && errno == EPERM)
		bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);

	/* If the mount was successful, and we're maintaining an old-style
	 * mtab file by hand, add the new entry to it now. */

	if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc) {
		FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
		int i;

		if(!mountTable)
			bb_error_msg("No %s\n",bb_path_mtab_file);

		// Add vfs string flags

		for(i=0; mount_options[i].flags != MS_REMOUNT; i++)
			if (mount_options[i].flags > 0)
				append_mount_options(&(mp->mnt_opts),
// Shut up about the darn const.  It's not important.  I don't care.
						(char *)mount_options[i].name);

		// Remove trailing / (if any) from directory we mounted on

		i = strlen(mp->mnt_dir);
		if(i>1 && mp->mnt_dir[i-1] == '/') mp->mnt_dir[i-1] = 0;

		// Write and close.

		if(!mp->mnt_type || !*mp->mnt_type) mp->mnt_type="--bind";
		addmntent(mountTable, mp);
		endmntent(mountTable);
		if (ENABLE_FEATURE_CLEAN_UP)
			if(strcmp(mp->mnt_type,"--bind")) mp->mnt_type = 0;
	}

	return rc;
}
Esempio n. 4
0
const char *mntpoint(const char *devname)
{
    FILE *f;
    struct mntent *mnt;
    
    f = setmntent("/etc/mtab", "r");
    if (f) {
        while (mnt = getmntent(f)) {
            if (strcmp(mnt->mnt_fsname, devname) == 0) {
                endmntent(f);
                return strdup(mnt->mnt_dir);
            }
        }
        endmntent(f);
    }

    return "???";
}
/** @todo Integrate into RTFsQueryMountpoint().  */
static bool VBoxServiceAutoMountShareIsMounted(const char *pszShare,
                                               char *pszMountPoint, size_t cbMountPoint)
{
    AssertPtrReturn(pszShare, VERR_INVALID_PARAMETER);
    AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);
    AssertReturn(cbMountPoint, VERR_INVALID_PARAMETER);

    bool fMounted = false;
    /* @todo What to do if we have a relative path in mtab instead
     *       of an absolute one ("temp" vs. "/media/temp")?
     * procfs contains the full path but not the actual share name ...
     * FILE *pFh = setmntent("/proc/mounts", "r+t"); */
#ifdef RT_OS_SOLARIS
    FILE *pFh = fopen(_PATH_MOUNTED, "r");
    if (!pFh)
        VBoxServiceError("VBoxServiceAutoMountShareIsMounted: Could not open mount tab \"%s\"!\n",
                         _PATH_MOUNTED);
    else
    {
        mnttab mntTab;
        while ((getmntent(pFh, &mntTab)))
        {
            if (!RTStrICmp(mntTab.mnt_special, pszShare))
            {
                fMounted = RTStrPrintf(pszMountPoint, cbMountPoint, "%s", mntTab.mnt_mountp)
                         ? true : false;
                break;
            }
        }
        fclose(pFh);
    }
#else
    FILE *pFh = setmntent(_PATH_MOUNTED, "r+t");
    if (pFh == NULL)
        VBoxServiceError("VBoxServiceAutoMountShareIsMounted: Could not open mount tab \"%s\"!\n",
                         _PATH_MOUNTED);
    else
    {
        mntent *pMntEnt;
        while ((pMntEnt = getmntent(pFh)))
        {
            if (!RTStrICmp(pMntEnt->mnt_fsname, pszShare))
            {
                fMounted = RTStrPrintf(pszMountPoint, cbMountPoint, "%s", pMntEnt->mnt_dir)
                         ? true : false;
                break;
            }
        }
        endmntent(pFh);
    }
#endif

    VBoxServiceVerbose(4, "VBoxServiceAutoMountShareIsMounted: Share \"%s\" at mount point \"%s\" = %s\n",
                       pszShare, fMounted ? pszMountPoint : "<None>", fMounted ? "Yes" : "No");
    return fMounted;
}
Esempio n. 6
0
int fsal_internal_path2fsname(char *rpath, char *fs_spec)
{
  FILE *fp;
  struct mntent mnt;
  struct mntent *pmnt;
  char work[MAXPATHLEN];
  char mntdir[MAXPATHLEN];

  size_t pathlen, outlen;
  int rc = -1;

  pathlen = 0;
  outlen = 0;

  if(!rpath || !fs_spec)
    return -1;

  fp = setmntent(MOUNTED, "r");

  if(fp == NULL)
    return -1;

  while((pmnt = getmntent_r(fp, &mnt, work, MAXPATHLEN)) != NULL)
    {
      /* get the longer path that matches export path */
      if(mnt.mnt_dir != NULL)
        {
          pathlen = strlen(mnt.mnt_dir);

          if((pathlen > outlen) && !strcmp(mnt.mnt_dir, "/"))
            {
              outlen = pathlen;
              strncpy(mntdir, mnt.mnt_dir, MAXPATHLEN);
              strncpy(fs_spec, mnt.mnt_fsname, MAXPATHLEN);
            }
          /* in other cases, the filesystem must be <mountpoint>/<smthg> or <mountpoint>\0 */
          else if((pathlen > outlen) &&
                  !strncmp(rpath, mnt.mnt_dir, pathlen) &&
                  ((rpath[pathlen] == '/') || (rpath[pathlen] == '\0')))
            {
              /* LogFullDebug(COMPONENT_FSAL, "%s is under mountpoint %s, type=%s, fs=%s\n", 
                 rpath, mnt.mnt_dir, mnt.mnt_type, mnt.mnt_fsname); */

              outlen = pathlen;
              strncpy(mntdir, mnt.mnt_dir, MAXPATHLEN);
              strncpy(fs_spec, mnt.mnt_fsname, MAXPATHLEN);
              rc = 0;
            }
        }

    }

  endmntent(fp);
  return rc;
}                               /* fsal_internal_path2fsname */
Esempio n. 7
0
/* Code to determine the mount point of a CD-ROM */
int loki_getmountpoint( const char *device, char *mntpt, int max_size ){
	char devpath[PATH_MAX], mntdevpath[PATH_MAX];
	FILE * mountfp;
	struct mntent *mntent;
	int mounted;

	/* Nothing to do with no device file */
	if ( device == NULL ) {
		*mntpt = '\0';
		return -1;
	}

	/* Get the fully qualified path of the CD-ROM device */
	if ( realpath( device, devpath ) == NULL ) {
		perror( "realpath() on your CD-ROM failed" );
		return( -1 );
	}

	/* Get the mount point */
	mounted = -1;
	memset( mntpt, 0, max_size );
	mountfp = setmntent( _PATH_MNTTAB, "r" );
	if ( mountfp != NULL ) {
		mounted = 0;
		while ( ( mntent = getmntent( mountfp ) ) != NULL )
		{
			char *tmp, mntdev[1024];

			strcpy( mntdev, mntent->mnt_fsname );
			if ( strcmp( mntent->mnt_type, "supermount" ) == 0 ) {
				tmp = strstr( mntent->mnt_opts, "dev=" );
				if ( tmp ) {
					strcpy( mntdev, tmp + strlen( "dev=" ) );
					tmp = strchr( mntdev, ',' );
					if ( tmp ) {
						*tmp = '\0';
					}
				}
			}
			if ( strncmp( mntdev, "/dev", 4 ) ||
				 realpath( mntdev, mntdevpath ) == NULL ) {
				continue;
			}
			if ( strcmp( mntdevpath, devpath ) == 0 ) {
				mounted = 1;
				assert( (int)strlen( mntent->mnt_dir ) < max_size );
				strncpy( mntpt, mntent->mnt_dir, max_size - 1 );
				mntpt[max_size - 1] = '\0';
				break;
			}
		}
		endmntent( mountfp );
	}
	return( mounted );
}
Esempio n. 8
0
json_t * rpi_storage_get_list(duda_request_t *dr, int parameter)
{
    struct mntent *ent;
    FILE *f;
    json_t *array;
    json_t *mount_object;
    char *full_path;
    struct statvfs svfs;
    double fragment_size, size, used, available, use;
    
    array = json->create_array();
    
    f = setmntent("/proc/mounts", "r");
    if (f == NULL) {
        return array;
    }
    
    while (NULL != (ent = getmntent(f))) {
        mount_object = json->create_object();
        json->add_to_array(array, mount_object);
        
        /* resolve symlinks in device names */
        full_path = NULL;
        if (ent->mnt_fsname[0] == '/') {
            full_path = realpath(ent->mnt_fsname, full_path);
        }
        if (full_path != NULL) {
            json->add_to_object(mount_object, "device", json->create_string(full_path));
            free(full_path);
        }
        else {
            json->add_to_object(mount_object, "device", json->create_string(ent->mnt_fsname));
        }
        json->add_to_object(mount_object, "mount", json->create_string(ent->mnt_dir));
        json->add_to_object(mount_object, "filesystem", json->create_string(ent->mnt_type));
        
        /* try to get usage data */
        if ((statvfs(ent->mnt_dir, &svfs) == 0) && (svfs.f_blocks != 0)) {
            fragment_size = (double)svfs.f_frsize;
            size = fragment_size*((double)svfs.f_blocks);
            used = size - fragment_size*((double)svfs.f_bfree);
            available = fragment_size*((double)svfs.f_bavail);
            use = (size - available)/size;
            
            json->add_to_object(mount_object, "size", json->create_number(size));
            json->add_to_object(mount_object, "used", json->create_number(used));
            json->add_to_object(mount_object, "available", json->create_number(available));
            json->add_to_object(mount_object, "use", json->create_number(use));
        }
    }
    
    endmntent(f);
    
    return array;
}
/*
 * Return disk mounted partitions as a list of tuples including device,
 * mount point and filesystem type
 */
static PyObject*
get_disk_partitions(PyObject* self, PyObject* args)
{
    FILE *file = NULL;
    struct mntent *entry;
    PyObject* py_retlist = PyList_New(0);
    PyObject* py_tuple = NULL;

    // MOUNTED constant comes from mntent.h and it's == '/etc/mtab'
    Py_BEGIN_ALLOW_THREADS
    file = setmntent(MOUNTED, "r");
    Py_END_ALLOW_THREADS
    if ((file == 0) || (file == NULL)) {
        PyErr_SetFromErrno(PyExc_OSError);
        goto error;
    }

    while ((entry = getmntent(file))) {
        if (entry == NULL) {
            PyErr_Format(PyExc_RuntimeError, "getmntent() failed");
            goto error;
        }
        py_tuple = Py_BuildValue("(ssss)", entry->mnt_fsname,  // device
                                           entry->mnt_dir,     // mount point
                                           entry->mnt_type,    // fs type
                                           entry->mnt_opts);   // options
        if (! py_tuple)
            goto error;
        if (PyList_Append(py_retlist, py_tuple))
            goto error;
        Py_DECREF(py_tuple);
    }
    endmntent(file);
    return py_retlist;

error:
    if (file != NULL)
        endmntent(file);
    Py_XDECREF(py_tuple);
    Py_DECREF(py_retlist);
    return NULL;
}
Esempio n. 10
0
int
fixmount_check_mount(char *host, struct in_addr hostaddr, char *path)
{
  FILE *mtab;
  mntent_t *ment;
  int found = 0;

  /* scan mtab for path */
  if (!(mtab = setmntent(_PATH_MTAB, "r"))) {
    perror(_PATH_MTAB);
    exit(1);
  }

  /*
   * setmntent() doesn't do locking in read-only mode. Too bad -- it seems to
   * rely on mount() and friends to do atomic updates by renaming the file.
   * Well, our patched amd rewrites mtab in place to avoid NFS lossage, so
   * better do the locking ourselves.
   */
#ifdef HAVE_FLOCK
  if (flock(fileno(mtab), LOCK_SH) < 0) {
#else /* not HAVE_FLOCK */
  if (lockf(fileno(mtab), F_LOCK, 0) < 0) {
#endif /* not HAVE_FLOCK */
    perror(_PATH_MTAB);
    exit(1);
  }

  while (!found && (ment = getmntent(mtab))) {
    char *colon;

    if ((colon = strchr(ment->mnt_fsname, ':'))) {
      *colon = '\0';
      if ((STREQ(colon + 1, path) ||
	   STREQ(ment->mnt_dir, path)) &&
	  is_same_host(ment->mnt_fsname, host, hostaddr))
	  found = 1;
    }
  }

  (void) endmntent(mtab);

  if (!found) {
    char *swap;

    /* swap files never show up in mtab, only root fs */
    if ((swap = strstr(path, "swap"))) {
      strncpy(swap, "root", 4);	/* this should NOT use xstrlcpy  */
      found = fixmount_check_mount(host, hostaddr, path);
      strncpy(swap, "swap", 4);	/* this should NOT use xstrlcpy  */
    }
  }
  return found;
}
Esempio n. 11
0
/* refresh t_mount_info infos in a GPtrArray containing struct t_disk * elements */
void disks_refresh(GPtrArray * pdisks)
{
	/* using getmntent to get filesystems mount information */
	
	FILE * fmtab = NULL ; // file /etc/mtab
	struct mntent * pmntent = NULL; // struct for mnt info
	struct statfs * pstatfs = NULL ;
	
	t_mount_info * mount_info ;
	t_disk * pdisk ;
	
	/*remove t_mount_info for all devices */
	disks_free_mount_info(pdisks);
	
	/*allocate new struct statfs */
	pstatfs = g_new0(struct statfs,1);
	
	/*open file*/
	fmtab = setmntent(MTAB,"r"); 
	
	/* start looking for mounted devices */
	for(pmntent = getmntent(fmtab) ; pmntent!=NULL ; pmntent = getmntent(fmtab))
	{
		/*getstat on disk */
		if (statfs(pmntent->mnt_dir,pstatfs)==0 && (pstatfs->f_blocks != 0))
		{	/*if we got the stat and they block number is non zero */
			
			/* get pointer on disk from pdisks */
			/*CHANGED to reflect change in disk_search*/
			pdisk = disks_search(pdisks,pmntent->mnt_dir);
			if (pdisk == NULL) //if disk is not found in pdisks
			{
				/*create a new struct t_disk and add it to pdisks */
				/* test for "/dev/" and mnt_dir != none */
				if ( !g_str_has_prefix(pmntent->mnt_fsname,"/dev/") || (g_ascii_strcasecmp(pmntent->mnt_dir,"none") == 0) ) {
					if ( g_strrstr(pmntent->mnt_fsname,":") == NULL)
					 continue ; }
		
				pdisk = disk_new(pmntent->mnt_fsname,pmntent->mnt_dir);
				g_ptr_array_add(pdisks,pdisk);
			}
			
			/* create new t_mount_info */	
			mount_info = mount_info_new_from_stat(pstatfs, pmntent->mnt_type, pmntent->mnt_dir) ;
			/* add it to pdisk */
			pdisk->mount_info = mount_info ;
		
		}
	}
	
	g_free(pstatfs);
	endmntent(fmtab); //close file
	return ;
}
Esempio n. 12
0
/* Check if an ecryptfs private device or mount point is mounted.
 * Return 1 if a filesystem in mtab matches dev && mnt && sig.
 * Return 0 otherwise.
 */
int ecryptfs_private_is_mounted(char *dev, char *mnt, char *sig, int mounting) {
	FILE *fh = NULL;
	struct mntent *m = NULL;
	char *opt = NULL;
	int mounted;
	if (sig && asprintf(&opt, "ecryptfs_sig=%s", sig) < 0) {
		perror("asprintf");
		return 0;
	}
	fh = setmntent("/proc/mounts", "r");
	if (fh == NULL) {
		perror("setmntent");
		return 0;
	}
	mounted = 0;
	flockfile(fh);
	while ((m = getmntent(fh)) != NULL) {
		if (strcmp(m->mnt_type, "ecryptfs") != 0)
			/* Skip if this entry is not an ecryptfs mount */
			continue;
		if (mounting == 1) {
			/* If mounting, return "already mounted" if EITHER the
 			 * dev or the mnt dir shows up in mtab/mounts;
 			 * regardless of the signature of such mounts;
 			 */
			if (dev != NULL && strcmp(m->mnt_fsname, dev) == 0) {
				mounted = 1;
				break;
			}
			if (mnt != NULL && strcmp(m->mnt_dir, mnt) == 0) {
				mounted = 1;
				break;
			}
		} else {
			/* Otherwise, we're unmounting, and we need to be
			 * very conservative in finding a perfect match
			 * to unmount.  The device, mountpoint, and signature
			 * must *all* match perfectly.
			 */
			if (
			    strcmp(m->mnt_fsname, dev) == 0 &&
			    strcmp(m->mnt_dir, mnt) == 0 &&
			    (!opt || hasmntopt(m, opt) != NULL)
			) {
				mounted = 1;
				break;
			}
		}
	}
	endmntent(fh);
	if (opt != NULL)
		free(opt);
	return mounted;
}
Esempio n. 13
0
int CollectorLinux::getDiskListByFs(const char *pszPath, DiskList& listUsage, DiskList& listLoad)
{
    FILE *mtab = setmntent("/etc/mtab", "r");
    if (mtab)
    {
        struct mntent *mntent;
        while ((mntent = getmntent(mtab)))
        {
            /* Skip rootfs entry, there must be another root mount. */
            if (strcmp(mntent->mnt_fsname, "rootfs") == 0)
                continue;
            if (strcmp(pszPath, mntent->mnt_dir) == 0)
            {
                char szDevName[128];
                char szFsName[1024];
                /* Try to resolve symbolic link if necessary. Yes, we access the file system here! */
                int rc = RTPathReal(mntent->mnt_fsname, szFsName, sizeof(szFsName));
                if (RT_FAILURE(rc))
                    continue; /* something got wrong, just ignore this path */
                /* check against the actual mtab entry, NOT the real path as /dev/mapper/xyz is
                 * often a symlink to something else */
                if (!strncmp(mntent->mnt_fsname, RT_STR_TUPLE("/dev/mapper")))
                {
                    /* LVM */
                    getDiskName(szDevName, sizeof(szDevName), mntent->mnt_fsname, false /*=fTrimDigits*/);
                    addVolumeDependencies(szDevName, listUsage);
                    listLoad = listUsage;
                }
                else if (!strncmp(szFsName, RT_STR_TUPLE("/dev/md")))
                {
                    /* Software RAID */
                    getDiskName(szDevName, sizeof(szDevName), szFsName, false /*=fTrimDigits*/);
                    listUsage.push_back(RTCString(szDevName));
                    addRaidDisks(szDevName, listLoad);
                }
                else
                {
                    /* Plain disk partition. Trim the trailing digits to get the drive name */
                    getDiskName(szDevName, sizeof(szDevName), szFsName, true /*=fTrimDigits*/);
                    listUsage.push_back(RTCString(szDevName));
                    listLoad.push_back(RTCString(szDevName));
                }
                if (listUsage.empty() || listLoad.empty())
                {
                    LogRel(("Failed to retrive disk info: getDiskName(%s) --> %s\n",
                           mntent->mnt_fsname, szDevName));
                }
                break;
            }
        }
        endmntent(mtab);
    }
    return VINF_SUCCESS;
}
void erase_mtab(const char *name)
{
	struct mntent entries[20];
	int count = 0;
	FILE *mountTable = setmntent(mtab_file, "r");
	struct mntent *m;

	/* Check if reading the mtab file failed */
	if (mountTable == 0
			/* Bummer.  fall back on trying the /proc filesystem */
			&& (mountTable = setmntent("/proc/mounts", "r")) == 0) {
		perror_msg("%s", mtab_file);
		return;
	}

	while ((m = getmntent(mountTable)) != 0) {
		entries[count].mnt_fsname = strdup(m->mnt_fsname);
		entries[count].mnt_dir = strdup(m->mnt_dir);
		entries[count].mnt_type = strdup(m->mnt_type);
		entries[count].mnt_opts = strdup(m->mnt_opts);
		entries[count].mnt_freq = m->mnt_freq;
		entries[count].mnt_passno = m->mnt_passno;
		count++;
	}
	endmntent(mountTable);
	if ((mountTable = setmntent(mtab_file, "w"))) {
		int i;

		for (i = 0; i < count; i++) {
			int result = (strcmp(entries[i].mnt_fsname, name) == 0
						  || strcmp(entries[i].mnt_dir, name) == 0);

			if (result)
				continue;
			else
				addmntent(mountTable, &entries[i]);
		}
		endmntent(mountTable);
	} else if (errno != EROFS)
		perror_msg("%s", mtab_file);
}
Esempio n. 15
0
File: mtab.c Progetto: dzo/busybox
void FAST_FUNC erase_mtab(const char *name)
{
	struct mntent *entries;
	int i, count;
	FILE *mountTable;
	struct mntent *m;

	mountTable = setmntent(bb_path_mtab_file, "r");
	/* Bummer. Fall back on trying the /proc filesystem */
	if (!mountTable) mountTable = setmntent("/proc/mounts", "r");
	if (!mountTable) {
		bb_perror_msg(bb_path_mtab_file);
		return;
	}

	entries = NULL;
	count = 0;
	while ((m = getmntent(mountTable)) != 0) {
		entries = xrealloc_vector(entries, 3, count);
		entries[count].mnt_fsname = xstrdup(m->mnt_fsname);
		entries[count].mnt_dir = xstrdup(m->mnt_dir);
		entries[count].mnt_type = xstrdup(m->mnt_type);
		entries[count].mnt_opts = xstrdup(m->mnt_opts);
		entries[count].mnt_freq = m->mnt_freq;
		entries[count].mnt_passno = m->mnt_passno;
		count++;
	}
	endmntent(mountTable);

//TODO: make update atomic
	mountTable = setmntent(bb_path_mtab_file, "w");
	if (mountTable) {
		for (i = 0; i < count; i++) {
			if (strcmp(entries[i].mnt_fsname, name) != 0
			 && strcmp(entries[i].mnt_dir, name) != 0)
				addmntent(mountTable, &entries[i]);
		}
		endmntent(mountTable);
	} else if (errno != EROFS)
		bb_perror_msg(bb_path_mtab_file);
}
Esempio n. 16
0
static int parse_fstab(void) {
        FILE *f;
        int r = 0;
        struct mntent *me;

        errno = 0;
        f = setmntent("/etc/fstab", "r");
        if (!f) {
                if (errno == ENOENT)
                        return 0;

                log_error("Failed to open /etc/fstab: %m");
                return -errno;
        }

        while ((me = getmntent(f))) {
                char *where, *what;
                int k;

                what = fstab_node_to_udev_node(me->mnt_fsname);
                if (!what) {
                        r = log_oom();
                        goto finish;
                }

                where = strdup(me->mnt_dir);
                if (!where) {
                        r = log_oom();
                        free(what);
                        goto finish;
                }

                if (is_path(where))
                        path_kill_slashes(where);

                log_debug("Found entry what=%s where=%s type=%s", what, where, me->mnt_type);

                if (streq(me->mnt_type, "swap"))
                        k = add_swap(what, me);
                else
                        k = add_mount(what, where, me);

                free(what);
                free(where);

                if (k < 0)
                        r = k;
        }

finish:
        endmntent(f);
        return r;
}
Esempio n. 17
0
/*
 * Unlock the mount table
 */
void
unlock_mntlist(void)
{
    /*
     * Release file lock, by closing the file
     */
    if (mnt_file) {
        dlog("unlock_mntlist: releasing");
        endmntent(mnt_file);
        mnt_file = NULL;
    }
}
Esempio n. 18
0
/*
 * If *path is NULL, initialize the fs table with all xfs mount points in mtab
 * If *path is specified, search for that path in mtab
 */
static int
fs_table_initialise_mounts(
	char		*path)
{
	struct mntent	*mnt;
	FILE		*mtp;
	char		*fslog, *fsrt;
	int		error, found;
	char		*rpath = NULL;

	error = found = 0;
	fslog = fsrt = NULL;

	if (!mtab_file) {
		mtab_file = PROC_MOUNTS;
		if (access(mtab_file, R_OK) != 0)
			mtab_file = MOUNTED;
	}

	if ((mtp = setmntent(mtab_file, "r")) == NULL)
		return ENOENT;

	/* Use realpath to resolve symlinks, relative paths, etc */
	if (path)
		if ((rpath = realpath(path, NULL)) == NULL)
			return ENOENT;

	while ((mnt = getmntent(mtp)) != NULL) {
		if (strcmp(mnt->mnt_type, "xfs") != 0)
			continue;
		if (path &&
		    ((strcmp(path, mnt->mnt_dir) != 0) &&
		     (strcmp(path, mnt->mnt_fsname) != 0) &&
		     (strcmp(rpath, mnt->mnt_dir) != 0) &&
		     (strcmp(rpath, mnt->mnt_fsname) != 0)))
			continue;
		if (fs_extract_mount_options(mnt, &fslog, &fsrt))
			continue;
		(void) fs_table_insert(mnt->mnt_dir, 0, FS_MOUNT_POINT,
					mnt->mnt_fsname, fslog, fsrt);
		if (path) {
			found = 1;
			break;
		}
	}
	endmntent(mtp);
	free(rpath);

	if (path && !found)
		error = ENXIO;

	return error;
}
Esempio n. 19
0
static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
{
	int ret = -1;
	SMB_STRUCT_STAT S;
	FILE *fp;
	struct mntent *mnt;
	SMB_DEV_T devno;

	/* find the block device file */

	if (!path||!mntpath||!bdev||!fs)
		smb_panic("sys_path_to_bdev: called with NULL pointer");

	(*mntpath) = NULL;
	(*bdev) = NULL;
	(*fs) = NULL;
	
	if ( sys_stat(path, &S) == -1 )
		return (-1);

	devno = S.st_dev ;

	fp = setmntent(MOUNTED,"r");
	if (fp == NULL) {
		return -1;
	}
  
	while ((mnt = getmntent(fp))) {
		if ( sys_stat(mnt->mnt_dir,&S) == -1 )
			continue ;

		if (S.st_dev == devno) {
			(*mntpath) = SMB_STRDUP(mnt->mnt_dir);
			(*bdev) = SMB_STRDUP(mnt->mnt_fsname);
			(*fs)   = SMB_STRDUP(mnt->mnt_type);
			if ((*mntpath)&&(*bdev)&&(*fs)) {
				ret = 0;
			} else {
				SAFE_FREE(*mntpath);
				SAFE_FREE(*bdev);
				SAFE_FREE(*fs);
				ret = -1;
			}

			break;
		}
	}

	endmntent(fp) ;

	return ret;
}
Esempio n. 20
0
glibtop_mountentry *
glibtop_get_mountlist_s(glibtop *server, glibtop_mountlist *buf, int all_fs)
{
  const struct mntent *mnt;
  FILE *fp;
  GArray* entries;
  IgnoreList* ig = NULL;

  memset(buf, 0, sizeof(glibtop_mountlist));

  /* wild guess, preallocate 8 entries
     on a desktop, almost everyone has / and a tmpfs for udev
     if all_fs, there are also proc, sys, fuse, binfmt, etc */
  entries = g_array_sized_new(FALSE, FALSE, sizeof(glibtop_mountentry), 8);

  if (!(fp = setmntent(MOUNTED, "r"))) {
      glibtop_warn_io_r(server, "Could not open %s", MOUNTED);
      goto out;
    }

  while ((mnt = getmntent(fp)))
    {
      glibtop_mountentry *me;
      gsize len;

      if (!all_fs && ignore_fs(mnt->mnt_type, &ig))
	continue;

      len = entries->len;
      g_array_set_size(entries, len + 1);
      me = &g_array_index(entries, glibtop_mountentry, len);

      g_strlcpy(me->devname, mnt->mnt_fsname, sizeof me->devname);
      g_strlcpy(me->mountdir, mnt->mnt_dir, sizeof me->mountdir);
      g_strlcpy(me->type, mnt->mnt_type, sizeof me->type);
    }

    endmntent(fp);


  out:
    ignore_list_delete(ig);

    buf->size = sizeof(glibtop_mountentry);
    buf->number = entries->len;
    buf->total = buf->number * buf->size;
    buf->flags = (1 << GLIBTOP_MOUNTLIST_SIZE)
    | (1 << GLIBTOP_MOUNTLIST_NUMBER)
    | (1 << GLIBTOP_MOUNTLIST_TOTAL);

  return (glibtop_mountentry*) g_array_free(entries, FALSE);
}
Esempio n. 21
0
bool ManagerWindow::checkUmProc()
{
    FILE *fp = setmntent("/proc/mounts","r");
    struct mntent *fs;
    bool umproc_loaded = false;
    while ((fs = getmntent(fp))  !=  NULL) /* detect if /proc/mounts is mounted by umproc */
    {
      if(!(strcmp(fs->mnt_fsname,"none") || strcmp(fs->mnt_dir,"/proc/mounts") || strcmp(fs->mnt_type,"proc")))
                      umproc_loaded = true;
    }
    endmntent(fp);
    return umproc_loaded;
}
Esempio n. 22
0
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;
}
Esempio n. 23
0
static int check_mtab_entry(char *spec, 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, spec) == 0 &&
			strcmp(mnt->mnt_dir, mtpt) == 0 &&
			strcmp(mnt->mnt_type, type) == 0) {
			endmntent(fp);
			return(EEXIST);
		}
	}
	endmntent(fp);

	return(0);
}
Esempio n. 24
0
static char *opal_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;
}
Esempio n. 25
0
static int isMounted(const char *mntPoint)
{
  int ret;
  FILE *fp;
  struct mntent *mntEnt;

  fp = setmntent("/proc/mounts", "r");
  if (!fp) {
    ret = errno;
    fprintf(stderr, "FUSE_TEST: isMounted(%s) failed to open /proc/mounts: "
            "error %d: %s", mntPoint, ret, strerror(ret));
    return -ret;
  }
  while ((mntEnt = getmntent(fp))) {
    if (!strcmp(mntEnt->mnt_dir, mntPoint)) {
      endmntent(fp);
      return 1;
    }
  }
  endmntent(fp);
  return 0;
}
Esempio n. 26
0
/*
 * collect_active_mounts returns a list of active hugetlbfs
 * mount points, and, if longest is not NULL, the number of
 * characters in the longest mount point to ease output
 * formatting.  Caller is expected to free the list of mounts.
 */
struct mount_list *collect_active_mounts(int *longest)
{
	FILE *mounts;
	struct mount_list *list, *current, *previous = NULL;
	int length;

	/* First try /proc/mounts, then /etc/mtab */
	mounts = setmntent(PROCMOUNTS, "r");
	if (!mounts) {
		mounts = setmntent(MOUNTED, "r");
		if (!mounts) {
			ERROR("unable to open %s or %s for reading",
				PROCMOUNTS, MOUNTED);
			exit(EXIT_FAILURE);
		}
	}

	list = malloc(sizeof(struct mount_list));
	if (!list) {
		ERROR("out of memory");
		exit(EXIT_FAILURE);
	}

	list->next = NULL;
	current = list;
	while (getmntent_r(mounts, &(current->entry), current->data, MAX_SIZE_MNTENT)) {
		if (strcasecmp(current->entry.mnt_type, FS_NAME) == 0) {
			length = strlen(current->entry.mnt_dir);
			if (longest && length > *longest)
				*longest = length;

			current->next = malloc(sizeof(struct mount_list));
			if (!current->next) {
				ERROR("out of memory");
				exit(EXIT_FAILURE);
			}
			previous = current;
			current = current->next;
			current->next = NULL;
		}
	}

	endmntent(mounts);

	if (previous) {
		free(previous->next);
		previous->next = NULL;
		return list;
	}
	return NULL;
}
Esempio n. 27
0
// Check if the SD card is mounted by searching through the mtab
static boolean check_sd(){
  FILE * mtab = NULL;
  struct mntent * part = NULL;
  if((mtab = setmntent("/etc/mtab", "r")) != NULL){
    while((part = getmntent(mtab)) != NULL){
      if((part->mnt_dir != NULL) && (strcmp(part->mnt_dir, SD_MOUNT_PATH)) == 0){
        return(true);
      }
    }
    endmntent(mtab);
  }
  trace_error("Unable to locate SD mount path: %s\n", SD_MOUNT_PATH);
  return(false);
}
Esempio n. 28
0
/*
 * 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;
}
Esempio n. 29
0
/**
 * checks one storage target path and creates a QuotaBlockDevice
 *
 * @param targetPath path to block device to check
 * @param targetNumID targetNumID of the storage target to check
 */
QuotaBlockDevice QuotaBlockDevice::getBlockDeviceOfTarget(std::string& targetPath,
   uint16_t targetNumID)
{
   static std::string mountInformationPath("/proc/mounts");

   std::string resMountPath;
   std::string resBlockDevicePath;
   QuotaBlockDeviceFsType resFsType = QuotaBlockDeviceFsType_UNKNOWN;

   struct mntent* mntData;
   FILE *mntFile = setmntent(mountInformationPath.c_str(), "r");

   while ( (mntData = getmntent(mntFile) ) )
   {
      std::string tmpMountPath(mntData->mnt_dir);

      // test all mounts, use the mount with the longest match and use the last match if multiple
      // mounts with the longest match exists
      if ( (targetPath.find(tmpMountPath) == 0) && (resMountPath.size() <= tmpMountPath.size() ) )
      {
         resMountPath = mntData->mnt_dir;
         resBlockDevicePath = mntData->mnt_fsname;
         std::string type(mntData->mnt_type);

         if ( type.compare("xfs") == 0 )
            resFsType = QuotaBlockDeviceFsType_XFS;
         else
         if ( type.find("ext") == 0 )
            resFsType = QuotaBlockDeviceFsType_EXTX;
         else
         if ( type.compare("zfs") == 0 )
            resFsType = QuotaBlockDeviceFsType_ZFS;
         else
            resFsType = QuotaBlockDeviceFsType_UNKNOWN;
      }
   }

   endmntent(mntFile);

   QuotaBlockDevice blockDevice(resMountPath, resBlockDevicePath, resFsType, targetPath);

   // check if the installed libzfs is compatible with implementation
   if(blockDevice.getFsType() == QuotaBlockDeviceFsType_ZFS)
   {
      if(!Program::getApp()->isDlOpenHandleLibZfsValid() )
         QuotaTk::checkRequiredLibZfsFunctions(&blockDevice, targetNumID);
   }

   return blockDevice;
}
Esempio n. 30
0
void df_all(void)
{
    FILE *f;
    struct mntent *mnt;
    
    f = setmntent("/etc/mtab", "r");
    if (f) {
        while (mnt = getmntent(f))
                df_path(mnt->mnt_dir, mnt->mnt_fsname, mnt->mnt_dir);
        endmntent(f);
    } else {
        fprintf(stderr, "df: cannot open /etc/mtab: %s\n", strerror(errno));
    }
}