code mount in c language

HI All:
Where I can find out the mount lib or function to view
because I think about using c to mount a file system
without shell command.

Thanks

You can mount a filesystem through your C program. Check the man page of mount(2). Just remember, the mount command th atactually mounts a filesystem can only be run by root.

I am always very disappointed when I see code like:
system("cat /some/file.txt");
in a C program. But sometimes building a command line and passing it to system() is the more sensible approach. I have to argue that mounting a file system is an example of that. At first it was the mount() system call. Then it was vfs_mount(). Now it is mount() again. At least, that was the progression on HP-UX. Posix does not attempt to control mounting system calls or commands so your OS may differ. A C program that invokes the mount command via system() would have been largely insulated from this evolution. Besides the chaos at the system call level, mounting a filesystem rivals thread operations when it comes to splitting the work between user mode and kernel mode. But the system call only performs the actual kernel mode operations. The additional work required varies from system to system. It may include stuff like error checking, adding an entry to /etc/mnttab, initializing quotas, and auditing. And it could include other stuff that I don't remember. But you will need to figure it out and get it right in your program. You also need to maintain it as it varies from release to release. This is why I think that mounting a filesystem is too daunting for a user program.

Hi All:
I am coming here again ,i have viewed doucment and write a code as following. but the program con not runs, I have tryed login super user.
thanks :slight_smile:

#include <sys/param.h>
#include <sys/mount.h>
#include <errno.h>
#include <string.h>
int main()
{

const char path[]="data/test/my";
const char Workstationpath[]="/export/home/data/testpgm/"; // a location computer

int flag;

if ((flag = mount(Workstationpath,path,"MNT_RDONLY")))
printf("message %s",strerror(errno));

return 0;

}

Did you read Perderabo's comments? What you did won't work....

I modify the code as following
produce a message : "Invalid argument "

#include <sys/param.h>
#include <sys/mount.h>
#include <errno.h>
#include <string.h>
int main()
{

const char javapath[]="data/test/my";//need to be mounted

const char workstationpath[]="/export/home/testpgm/";
int flag;

if ((flag = mount("MNT_RDONLY",workstationpath,javapath)))
printf("PLEASE LOGIN SUPER SUER %s",strerror(errno));

return 0;

}

Thanks

I'll try this one last time. You C code needs:

system("/usr/sbin/mount <put your options & arguments here>");

which will work from a privileged account.

Calling mount() will probably not do what you want, if I understand what you are doing.

HI All:
i am back and find the way to use that,
as following is my code but not all
may be you can try it.

#include <sys/param.h>
#include <sys/mount.h>
#include <errno.h>
#include <string.h>
#include <sys/uio.h>
int main()
{
const char cTestPrag[]="/data/test/Test";
int flag;
mount(need to be mounted,"point mount path,flag,"MNT_RDONLY");

//varible flag you can omit but you will get a waring "have no catch ......"

printf("%s",strerror(errno));
return 0;
}

i think use system() to execute mount. maybe use shell than c.
the unix 95 % use c code to write if you want to be a expert onto
c that i think it is necessary.

thanks All