2009-09-22 16:04:16

by Brian D. McGrew

[permalink] [raw]
Subject: (un)mount ramfs from C code

Good morning all!

So I'm using a ramfs for temporary files, thank you whoever designed that,
it works great!

I can mkdir, mount, chmoud, readand write and then umount the thing from the
command line just fine.

What I need now is some method from within my C/C++ code to determine if the
ramfs is mounted, if not, then mount it so I can use it and unmount it when
I'm done, without making a system call.

Can this be done? Is there any access to mount/unmount from C/C++?

-b


2009-09-22 16:27:04

by Luciano Rocha

[permalink] [raw]
Subject: Re: (un)mount ramfs from C code

On Tue, Sep 22, 2009 at 09:04:15AM -0700, Brian McGrew wrote:
> Good morning all!
>
> So I'm using a ramfs for temporary files, thank you whoever designed that,
> it works great!
>
> I can mkdir, mount, chmoud, readand write and then umount the thing from the
> command line just fine.
>
> What I need now is some method from within my C/C++ code to determine if the
> ramfs is mounted, if not, then mount it so I can use it and unmount it when
> I'm done, without making a system call.

You mean without using system(3), right? System call is how your program
interacts with the outside world.

The relevant system calls are:
- mount(2): mount("none", "/mnt", "ramfs", 0, NULL);
- umount(2): umount("/mnt");

The function(section) is the standard Unix way of specifying the manual
section of the function. Use it like this:
$ man 2 mount

About checking whether it is already mounted, you could parse the file
/proc/mounts, or check the result of statfs(2).

Regards,
Luciano Rocha

--
Luciano Rocha <[email protected]>
Eurotux Inform?tica, S.A. <http://www.eurotux.com/>


Attachments:
(No filename) (1.06 kB)
(No filename) (197.00 B)
Download all attachments

2009-09-22 17:13:42

by Nikos Chantziaras

[permalink] [raw]
Subject: Re: (un)mount ramfs from C code

On 09/22/2009 07:04 PM, Brian McGrew wrote:
> Good morning all!
>
> So I'm using a ramfs for temporary files, thank you whoever designed that,
> it works great!
>
> I can mkdir, mount, chmoud, readand write and then umount the thing from the
> command line just fine.
>
> What I need now is some method from within my C/C++ code to determine if the
> ramfs is mounted, if not, then mount it so I can use it and unmount it when
> I'm done, without making a system call.
>
> Can this be done? Is there any access to mount/unmount from C/C++?

I suppose looking at the source code of the 'mount' utility could prove
very enlightening. You can find it at:

http://www.kernel.org/pub/linux/utils/util-linux-ng

2009-09-22 17:18:38

by Nikos Chantziaras

[permalink] [raw]
Subject: Re: (un)mount ramfs from C code

On 09/22/2009 08:13 PM, Nikos Chantziaras wrote:
> On 09/22/2009 07:04 PM, Brian McGrew wrote:
>> Good morning all!
>>
>> So I'm using a ramfs for temporary files, thank you whoever designed
>> that,
>> it works great!
>>
>> I can mkdir, mount, chmoud, readand write and then umount the thing
>> from the
>> command line just fine.
>>
>> What I need now is some method from within my C/C++ code to determine
>> if the
>> ramfs is mounted, if not, then mount it so I can use it and unmount it
>> when
>> I'm done, without making a system call.
>>
>> Can this be done? Is there any access to mount/unmount from C/C++?
>
> I suppose looking at the source code of the 'mount' utility could prove
> very enlightening. You can find it at:
>
> http://www.kernel.org/pub/linux/utils/util-linux-ng

Also, "man 2 mount" will provide docs about the the mount() function in
<sys/mount.h>. If for some reason that man page isn't provided in your
system, you can find a copy at:

http://linux.die.net/man/2/mount

2009-09-22 17:28:28

by Matthias Schniedermeyer

[permalink] [raw]
Subject: Re: (un)mount ramfs from C code

On 22.09.2009 09:04, Brian McGrew wrote:
> Good morning all!
>
> So I'm using a ramfs for temporary files, thank you whoever designed that,
> it works great!
>
> I can mkdir, mount, chmoud, readand write and then umount the thing from the
> command line just fine.
>
> What I need now is some method from within my C/C++ code to determine if the
> ramfs is mounted, if not, then mount it so I can use it and unmount it when
> I'm done, without making a system call.
>
> Can this be done? Is there any access to mount/unmount from C/C++?

The "/bin/mountpoint"-util appears to stat the mountpoint and it's
parent directory and compares if they have the same device-no (st_dev).
If they are different, then something is mounted.






Bis denn

--
Real Programmers consider "what you see is what you get" to be just as
bad a concept in Text Editors as it is in women. No, the Real Programmer
wants a "you asked for it, you got it" text editor -- complicated,
cryptic, powerful, unforgiving, dangerous.

2009-09-22 19:30:13

by Brian D. McGrew

[permalink] [raw]
Subject: Re: (un)mount ramfs from C code

> You mean without using system(3), right? System call is how your program
> interacts with the outside world.
>
> The relevant system calls are:
> - mount(2): mount("none", "/mnt", "ramfs", 0, NULL);
> - umount(2): umount("/mnt");
>

Thanks for the help! I'm getting there. Considering the following:

If (mount("ramfs", rd_path, "ramfs", MS_NOEXEC | MS_NOSUID, "size=2000m") <
0) {
strerror(errno);
} else {
std::cout << "mounted";
}

Works great! Thank you. However, even though the filesystem is mounted and
/bin/mountpoint confirms it's a mountpoint, I do not see the mountpoint
listed in /etc/mtab.

Also, according to what I've read about ramfs, the size is supposed to be
limited to size=, however, I can cat /dev/zero until the box it out of
memory (and swap).

I think I'm just missing something and google's not helping a whole lot
since ramfs isn't the most popular subject today.

Thanks,

-b

2009-09-22 19:59:51

by Tim Walberg

[permalink] [raw]
Subject: Re: (un)mount ramfs from C code

mount(2) (i.e. the system-call/library function) does not update /etc/mtab, IIRC...
mount(8) (the command-line utility) does...

In either case, I would expect the mount to show up in /proc/mounts (assuming
/proc is mounted).



On 09/22/2009 12:30 -0700, Brian McGrew wrote:
>> > You mean without using system(3), right? System call is how your program
>> > interacts with the outside world.
>> >
>> > The relevant system calls are:
>> > - mount(2): mount("none", "/mnt", "ramfs", 0, NULL);
>> > - umount(2): umount("/mnt");
>> >
>>
>> Thanks for the help! I'm getting there. Considering the following:
>>
>> If (mount("ramfs", rd_path, "ramfs", MS_NOEXEC | MS_NOSUID, "size=2000m") <
>> 0) {
>> strerror(errno);
>> } else {
>> std::cout << "mounted";
>> }
>>
>> Works great! Thank you. However, even though the filesystem is mounted and
>> /bin/mountpoint confirms it's a mountpoint, I do not see the mountpoint
>> listed in /etc/mtab.
>>
>> Also, according to what I've read about ramfs, the size is supposed to be
>> limited to size=, however, I can cat /dev/zero until the box it out of
>> memory (and swap).
>>
>> I think I'm just missing something and google's not helping a whole lot
>> since ramfs isn't the most popular subject today.
>>
>> Thanks,
>>
>> -b
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
>> the body of a message to [email protected]
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>> Please read the FAQ at http://www.tux.org/lkml/
End of included message



--
[email protected]