# The mount() function .



## tunixbsd (Apr 13, 2014)

Hi, I would like to know how to use the mount() function, I have read the mount(2) but i haven't understood it. So please, post an C or C++  code example and sorry for my bad English..


----------



## trh411 (Apr 13, 2014)

Does How to use mount function from c help? There are literally tons of c coding examples on the net for those willing to do a bit of Googleing.


----------



## tunixbsd (Apr 13, 2014)

trh411 said:
			
		

> Does How to use mount function from c help? There are literally tons of c coding examples on the net for those willing to do a bit of Googleing.



I think that the "Linux's mount() system call":


```
int
mount(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags,const void *data);
```

is different of  FreeBSD's mount() system call


```
int
mount(const char *type, const char *dir, int flags, void *data);
```


----------



## trh411 (Apr 13, 2014)

Okay then, suggest you download source and `cd` into /usr/src/sbin/mount. Study the source code to see how FreeBSD mounts filesystems.


----------



## tunixbsd (Apr 14, 2014)

I tried the follow:


```
#include <iostream>
#include <cstring>
#include <sys/mount.h>
#include  <sys/param.h>
#include <sys/uio.h>
using namespace std;

void build_iovec(struct iovec **iov,int *iovlen,const char *line,
	void *val,size_t len);

int main()
{
iovec *iov = new iovec;
int  iovlen = 0;
char fstype[]="ufs";
char fspath[]="/dev/da0a";
char from[]="/media";

build_iovec(&iov,&iovlen,"fstype",fstype,(size_t)-1);
build_iovec(&iov,&iovlen,"fspath",fspath,(size_t)-1);
build_iovec(&iov,&iovlen,"from",from,(size_t)-1);

	if(nmount(iov,iovlen,0) < 0)
	{
	cerr<<"\nFailed ";
	return -1;
	}



delete [] iov;
	return 0;
}






void build_iovec(struct iovec **iov,int *iovlen,const char *line,
	void *val,size_t len)
{
	int number;
	
	if (*iovlen < 0)
	return;
		
	number = *iovlen;
	
	(*iov)[number].iov_base = strdup(line);
	(*iov)[number].iov_len = strlen(line);
	number++;
	(*iov)[number].iov_base = val;
		if( len == (size_t) -1)
		{
		if(val != NULL)
			len = strlen((const char *)val) + 1;
		else
			len = 0;

		}
	(*iov)[number].iov_len = (int)len;
	*iovlen = ++number;

}
```

But always I run it I have the output: "Failed". I tried run it as root but returns the same. Can someone help me ?


----------



## ljboiler (Apr 14, 2014)

The iovecs passed to nmount(2) have to be in pairs: one with the parameter name, followed by one with the parameter value.
I made the following changes and it works when run as root:

```
#include <iostream>
#include <cstring>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/uio.h>

using namespace std;

void build_iovec_pair(struct iovec **iov,int *iovlen,const char *name,
void *val,size_t len);

int main()
{
  iovec *iov = new iovec[6];
  int iovlen = 0;
  char fstypeName[]="fstype";
  char fstypeValue[]="ufs";
  char fspathName[]="fspath";
  char fspathValue[]="/media";
  char fromName[]="from";
  char fromValue[]="/dev/da0a";

  build_iovec_pair(&iov,&iovlen,fstypeName,fstypeValue,(size_t)-1);
  build_iovec_pair(&iov,&iovlen,fspathName,fspathValue,(size_t)-1);
  build_iovec_pair(&iov,&iovlen,fromName,fromValue,(size_t)-1);

  if(nmount(iov,iovlen,0) < 0)
  {
    cerr<<"\nFailed ";
    return -1;
  }

  delete [] iov;
  return 0;
}






void build_iovec_pair(struct iovec **iov,int *iovlen,const char *name,
void *val,size_t len)
{
  int number;

  if (*iovlen < 0)
    return;

  number = *iovlen;

  (*iov)[number].iov_base = (void *)name;
  (*iov)[number].iov_len = strlen(name) + 1;
  number++;
  (*iov)[number].iov_base = val;
  if( len == (size_t) -1)
  {
    if(val != NULL)
      len = strlen((const char *)val) + 1;
    else
      len = 0;
  }
  (*iov)[number].iov_len = (int)len;
  *iovlen = ++number;

}
```


----------



## tunixbsd (Apr 14, 2014)

Thank you,your code works fine. But which is the difference of use the cast (void *) to use the strdup() function  ? And does have  the pointer returned by strdup() to be passed to free() function ?
Sorry for my bad english.


----------



## ljboiler (Apr 14, 2014)

No difference really; in my code, the space for the string was already allocated, so there was no need to make another copy with strdup, and there is no need to worry about freeing up the space allocated by strdup.  You did have the "fspath" and "from" pieces reversed:  the "from" is the device to mount, and the "fspath" is the place where it is to be mounted.


----------

