Program equivalent of "mount" command

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
My SBC has a host USB port that can accept a flash memory stick.  After
inserting the memory stick I have verified that I can do this in shell commands:

mount /dev/sda1 /mnt
cp filename /mnt/filename
umount /mnt

So now I would like to access this memory stick from my C application.  But I
cannot require that the memory stick be inserted first.  I must allow the memory
stick to be inserted after my program has begun.  I must be able to write to a
file on that memory stick, or detect that no memory stick is inserted.  I guess
I need to do the equivalent of the mount command programatically.  How is this
normally done?




Robert Scott
Ypsilanti, Michigan

Re: Program equivalent of "mount" command
Quoted text here. Click to load it
commands:
Quoted text here. Click to load it
memory
Quoted text here. Click to load it

Through the mount(2) syscall

Read the mount(2) manpage ("man 2 mount") for the syntax of the call.



MOUNT(2)                   Linux Programmer's Manual                  MOUNT(2)

NAME
       mount, umount - mount and unmount filesystems

SYNOPSIS
       #include <sys/mount.h>

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

       int umount(const char *target);

       int umount2(const char *target, int flags);

DESCRIPTION
       mount()  attaches  the filesystem specified by source (which is often a
       device name, but can also be a directory name or a dummy) to the direc-
       tory specified by target.


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
We've slightly trimmed the long signature. Click to see the full one.
Re: Program equivalent of "mount" command
Quoted text here. Click to load it

Thanks, I got it working.  But several things still confuse me.  One is the
filesystemtype.  I used "msdos" and it worked.  I was able to write a file to
the memory stick and that file appeared normally when plugged into my WinXP
system.  But is "msdos" right?  For example, should it handle long file names,
or will it be limited to 8.3 names as in DOS?  I thought there would be some
sort of an "auto" selection like there is in the mount shell command.

The second question has to do with handling the case where no memory stick is
plugged in.  Of course the mount() call fails, and I check for that in my
application and give the user appropriate feedback through an LCD screen.  The
the shell serial port also has some error messages, starting with "Device not
ready" and continuing for 8-10 lines more of info about the error.  I guess this
is not a problem for me since the shell port is  going to be disconnected during
normal operation.  So I can ignore whatever gets sent to it.  But I would like
to understand why this particular system call produces shell port error messages
while other systems calls (like open) are content to let my program handle error
returns.


Robert Scott
Ypsilanti, Michigan

Re: Program equivalent of "mount" command
Hello,

Quoted text here. Click to load it

You could try out "auto", which worked at least once for me using the mount
program.

"msdos" is limited to 8.3 names; the newer system is called "vfat".

Quoted text here. Click to load it

Does this happen using the mount program, too? If yes, it shouldn't be
anything serious.

Quoted text here. Click to load it

To open files that don't exist is (imho) normal behaviour and expected. To
open a device which is not ready is related to kernel mode drivers (and thus
important to the administrator, kernel and system stability). It is the same
as if you're trying to use a floppy disk which has become very bad.
Moreover, you might be tampering with the file system structure which could
harm other running processes.

You could try to make the kernel talk less about things like this, they
won't be logged then. But if the shell port is disconnected in normal
operation, I wouldn't care. It might be more important to notice errors when
things don't work as expected - and I would probably forget to make the
kernel talk to me again. If you want to do it, you can find it in
/proc/sys/kernel/printk .

Hope this helps,
Sebastian



Re: Program equivalent of "mount" command

Quoted text here. Click to load it

..just tried it.  mount fails.  "..no such device.."

Quoted text here. Click to load it

I tried filesystemtype="vfat" and it worked fine with my USB memory stick.
Thanks.


Robert Scott
Ypsilanti, Michigan

Re: Program equivalent of "mount" command
Quoted text here. Click to load it

This job can be done by udev automatically for you. It also could start your
application in the case someone inserts a memory stick (if you want). Or
send your application a signal, when the memory stick was inserted and
successfully mounted. Or everything else you want (this includes it could
also drive you crazy....).

JB

Site Timeline