public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* (un)mount ramfs from C code
@ 2009-09-22 16:04 Brian McGrew
  2009-09-22 16:26 ` Luciano Rocha
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Brian McGrew @ 2009-09-22 16:04 UTC (permalink / raw)
  To: linux-kernel

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: (un)mount ramfs from C code
  2009-09-22 16:04 (un)mount ramfs from C code Brian McGrew
@ 2009-09-22 16:26 ` Luciano Rocha
  2009-09-22 19:30   ` Brian McGrew
  2009-09-22 17:13 ` Nikos Chantziaras
  2009-09-22 17:28 ` Matthias Schniedermeyer
  2 siblings, 1 reply; 7+ messages in thread
From: Luciano Rocha @ 2009-09-22 16:26 UTC (permalink / raw)
  To: Brian McGrew; +Cc: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1118 bytes --]

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 <luciano@eurotux.com>
Eurotux Informática, S.A. <http://www.eurotux.com/>

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: (un)mount ramfs from C code
  2009-09-22 16:04 (un)mount ramfs from C code Brian McGrew
  2009-09-22 16:26 ` Luciano Rocha
@ 2009-09-22 17:13 ` Nikos Chantziaras
  2009-09-22 17:18   ` Nikos Chantziaras
  2009-09-22 17:28 ` Matthias Schniedermeyer
  2 siblings, 1 reply; 7+ messages in thread
From: Nikos Chantziaras @ 2009-09-22 17:13 UTC (permalink / raw)
  To: Brian McGrew; +Cc: linux-kernel

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: (un)mount ramfs from C code
  2009-09-22 17:13 ` Nikos Chantziaras
@ 2009-09-22 17:18   ` Nikos Chantziaras
  0 siblings, 0 replies; 7+ messages in thread
From: Nikos Chantziaras @ 2009-09-22 17:18 UTC (permalink / raw)
  Cc: Brian McGrew, linux-kernel

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: (un)mount ramfs from C code
  2009-09-22 16:04 (un)mount ramfs from C code Brian McGrew
  2009-09-22 16:26 ` Luciano Rocha
  2009-09-22 17:13 ` Nikos Chantziaras
@ 2009-09-22 17:28 ` Matthias Schniedermeyer
  2 siblings, 0 replies; 7+ messages in thread
From: Matthias Schniedermeyer @ 2009-09-22 17:28 UTC (permalink / raw)
  To: Brian McGrew; +Cc: linux-kernel

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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: (un)mount ramfs from C code
  2009-09-22 16:26 ` Luciano Rocha
@ 2009-09-22 19:30   ` Brian McGrew
  2009-09-22 19:53     ` Tim Walberg
  0 siblings, 1 reply; 7+ messages in thread
From: Brian McGrew @ 2009-09-22 19:30 UTC (permalink / raw)
  To: Luciano Rocha; +Cc: linux-kernel

> 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: (un)mount ramfs from C code
  2009-09-22 19:30   ` Brian McGrew
@ 2009-09-22 19:53     ` Tim Walberg
  0 siblings, 0 replies; 7+ messages in thread
From: Tim Walberg @ 2009-09-22 19:53 UTC (permalink / raw)
  To: Brian McGrew; +Cc: Luciano Rocha, linux-kernel

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 majordomo@vger.kernel.org
>>	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



-- 
twalberg@comcast.net

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2009-09-22 19:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-09-22 16:04 (un)mount ramfs from C code Brian McGrew
2009-09-22 16:26 ` Luciano Rocha
2009-09-22 19:30   ` Brian McGrew
2009-09-22 19:53     ` Tim Walberg
2009-09-22 17:13 ` Nikos Chantziaras
2009-09-22 17:18   ` Nikos Chantziaras
2009-09-22 17:28 ` Matthias Schniedermeyer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox