A question about the system call mount in a C program

Dear all,

Currently I'm working on a C program (OS = ubuntu 9.0.4)in which a USB key will
be mounted and umounted for several times. I read the man page
of the mount system call.

I use the following test code

#include <sys/mount.h>

int main(int argc, char *argv[])
{
    if (mount("/dev/sdg1", "/media/flashCorsaire/", "fuseblk",
               MS_MGC_VAL,"rw,nosuid,nodev,allow_other,blksize=4096") != 0)
    {
        fprintf(stderr, "Error: The program doesn't seem to be able ");
        fprintf(stderr, "to control the USB device\n");
        fprintf(stderr, "%s\n", strerror(errno));
        return 1;
    }

    return 0;
}

Whether the USB key has already been mounted or not, when I run this program as root I get
the following error message:

Error: The program doesn't seem to be able to control the USB device
Invalid argument

I don't really understand what is invalid argument in my code, because here is the line that I put
in /etc/fstab:

/dev/sdg1    /media/flashCorsaire    auto    defaults    0    0

And also here is what I see in /etc/mtab when I connect the USB to my PC

/dev/sdg1 /media/flashCorsaire fuseblk rw,nosuid,nodev,allow_other,blksize=4096 0 0

Therefore the file system is 'fuseblk' and options are rw,nosuid,nodev,allow_other,blksize=4096
So what's the problem? Where did I make a mistake in the code that generates this error
message?

Thanks in advance,
Kind Regards,
Dariyoosh
:slight_smile:

It probably doesn't like the options. The options string for the kernel mount() call are not the same as given to the commandline utility, or put in /etc/fstab. Most of those options belong to the mountflags bitfield, whereas data is entirely filesystem-specific flags. If you prefer those style options, it might be better to stick with the utilities as you have them than use hardcoded kernel calls.

Hello there,

Thanks for your answer.

Actually, the only reason that I wrote those options in my code was due to
what I read in 'man 2 mount'

#include <sys/mount.h>

       int mount(const char *source, const char *target,
                 const char *filesystemtype, unsigned long mountflags,
                 const void *data);
.
.
.
The  data  argument  is  interpreted  by  the different file systems.
Typically it is a string of comma-separated options understood by this file
system.  See mount(8) for details of the options  available  for  each 
filesystem type.

Well, I looked mount(8) but I found nothing for the file system 'fuseblk'
so I didn't really know what to put as argument for the last parameter
of the mount system call (that is, const void *data). Therefore I thought
maybe it is the same as /etc/mtab, which seems that it is not the case.

I really need to do this mount and umount within the C code. If it was a
script (bash, KornShell, etc.) it would have been much easier, but
what I want to do is part of a C application and therefore I need to use
a system call. I wonder whether there is other system calls allowing
to mount/umount devices. Because I looked the POSIX specification

The Open Group Base Specifications Issue 7

and it seems that mount is linux specific (there is no mount.h), because I
couldn't find the system call among the list of POSIX system calls. Do you
know any other system call?

Regards,
Dariyoosh

If you have no specific arguments for it try ""

No, you don't. You can use system() or fork() and exec() or even popen() to do it, arguably better if you don't want to rewrite half of the commandline utilities from scratch anyway. But the system call should work if you don't feed generic options into the filesystem-specific flags.

By the way, are you typing up your replies in notepad then copy-pasting them into your browser? You don't need to do that, and shouldn't do so, since it adds dozens of pointless line breaks.

You can also try running the command-line "mount" under strace or a debugger and seeing what options are actually passed to the "mount()" system call.

I actually tried "" and it mounted with that.