Visit Our UNIX and Linux User Community


A question about the system call mount in a C program


 
Thread Tools Search this Thread
Top Forums Programming A question about the system call mount in a C program
# 1  
Old 08-16-2009
Question 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

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:
Code:
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:

Code:
/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
Code:
/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
Smilie
# 2  
Old 08-16-2009
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.
# 3  
Old 08-16-2009
Hello there,



Thanks for your answer.

Quote:
Originally Posted by Corona688
It probably doesn't like the options.
...
If you prefer those style options, it might be better to stick with the utilities as you have them than use hardcoded kernel calls.
Actually, the only reason that I wrote those options in my code was due to
what I read in 'man 2 mount'

Code:
#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
# 4  
Old 08-16-2009
Quote:
Originally Posted by dariyoosh
Code:
#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.

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.
If you have no specific arguments for it try ""
Quote:
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.
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.
# 5  
Old 08-16-2009
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.
# 6  
Old 08-16-2009
Quote:
Originally Posted by achenle
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #833
Difficulty: Medium
YAML version 1.2 is a superset of JSON.
True or False?

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Help with Execl system call in a C program?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: "Your a7.c program should use printf to print a nice message. (You can decide what to say.) Then the process... (9 Replies)
Discussion started by: miniviking10
9 Replies

2. HP-UX

how to mount a file system from a solaris server into an hp-ux system

Hi all I wonder if its possible to mount on a hp-ux server a file system that was previously mounted on a solaris 10 server. The LUN is on NetApp stoarge. The problem on hp-ux I cannot do pvcreate on the lun (disk) because contains data. Any help will be appreciated FR (2 Replies)
Discussion started by: fretagi
2 Replies

3. Programming

call program

I would need to call the program 'ethtool' in my C++ program, does anyone know how to do that (if its even possible)? (1 Reply)
Discussion started by: Freaky123
1 Replies

4. Homework & Coursework Questions

program to send messages to parent using pipes and select system call

Write a program using select, which will create some number of child processes that continuously send text messages to the parent process using pipes. Each child has its own pipe that it uses to communicate with the parent. The parent uses select () to decide what pipes should be processed to... (1 Reply)
Discussion started by: ripssingh
1 Replies

5. Programming

Notification email in C program, via system call? or?

Hello everyone! I'm quite new here, but this forum helped me a lot before without registering :-) I'll go directly to my problem, I have been searching a bit about this issue but I was not successful. I need to write a program in C code to notificate me (to my email) when some action is done... (7 Replies)
Discussion started by: RoNNo
7 Replies

6. Shell Programming and Scripting

Call a mainframe program

Is it possible to call a mainframe program in UNIX script. I am using HP-UNIX. If so can any let me know the way to do it. (1 Reply)
Discussion started by: atlantis
1 Replies

7. Programming

how to call c executable inside c program??

hi guys i have only basic knowledge of c so guys plz help me ..... i want 2 call c executable which requires file name as argument and i need to modify file contents before calling that executable now my question is how can i call this c executable inside another c program with arguments ?? i... (9 Replies)
Discussion started by: zedex
9 Replies

8. Shell Programming and Scripting

how to call another program

Hi, I would like to know how to call a program "cmp_size" ... where to put in progam to run it ex: program checkdisk is below, and it will call a nother problem "cmp_size" Do I just put the cmp_size program at the end of this program. Thank you very much, # check all directory for size... (3 Replies)
Discussion started by: xitrum
3 Replies

9. Shell Programming and Scripting

how to call awk in a csh Program

Hi Friends, Could you pleas help me out.. I have an awk program which works fine while running it in the command prompt. The awk program is =============== awk 'BEGIN { format="head -%d M2_Sales_N01.txt |tail -%d >M2_Sales_N01_%02d.txt\n" n=0 m=0 } { if (n==0) { tmp=$1 n=1 }... (5 Replies)
Discussion started by: bikas_jena
5 Replies

10. UNIX for Advanced & Expert Users

how to differentiate system call from library call

Hi, Ho do I differentiate system call from library call? for example if I am using chmod , how do I find out if it is a system call or library call? Thanks Muru (2 Replies)
Discussion started by: muru
2 Replies

Featured Tech Videos