util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Karel Zak <kzak@redhat.com>
To: Amit <amit.uttam@gmail.com>
Cc: util-linux@vger.kernel.org
Subject: Re: Can't figure out how to use mnt_table_get_root_fs from libmount
Date: Mon, 10 Sep 2012 10:23:06 +0200	[thread overview]
Message-ID: <20120910082306.GA27276@x2.net.home> (raw)
In-Reply-To: <loom.20120908T195804-890@post.gmane.org>

On Sat, Sep 08, 2012 at 06:07:51PM +0000, Amit wrote:
> Hello,
> 
> I have a simple program using libmount that simply gets the path of the
> root filesystem. I can't seem to get it to work. I always get a
> segmentation fault. There is probably something basic that I am missing
> but can't figure it out.
> 
> #include <stdlib.h>
> #include <string.h>
> #include <stdio.h>
> #include <assert.h>
> 
> #include <libmount.h>
> 
> int main(int argc, char *argv[])
> {
>     struct libmnt_context *cxt;
>     struct libmnt_table *tab;
>     struct libmnt_fs *fs;
> 
>     /* Enable debugging */
>     mnt_init_debug(0xffff);
> 
>     /* Create new mount context */
>     cxt = mnt_new_context();
>     if (!cxt)
>         printf("Error creating new mount context\n");
> 
>     /* A mount table */
>     if (mnt_context_get_mtab(cxt, &tab) < 0)
>         printf("Error getting mtab\n");
> 
>     /* Get the root filesystem */
>     if (mnt_table_get_root_fs(tab, &fs) == -1)


   if (mnt_table_get_root_fs(tab, &fs) != 0)

Sorry, the docs for the function is incorrect. Almost all libmount
functions return zero on success and less than zero on error.


Note that mnt_table_get_root_fs() uses mountpoint Id and parent Id
from /proc/self/mountinfo to found root fs.

It means that you have to use the mountinfo file -- on systems with
regular mtab file the function mnt_context_get_mtab() parses /etc/mtab
rather than the mountinfo file.

  tab = mnt_new_table_from_file("/proc/self/mountinfo");
  if (tab && mnt_table_get_root_fs(tab, &fs) == 0)
     ....

the struct libmnt_context is unnecessary in this case.

The mnt_table_get_root_fs() is mostly designed for application where
you need to sort mountpoints to a tree. (for example findmnt(8))

If you not sure than mnt_table_find_target() is probably better
choice.

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

  parent reply	other threads:[~2012-09-10  8:23 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-08 18:07 Can't figure out how to use mnt_table_get_root_fs from libmount Amit
2012-09-08 20:19 ` Amit
2012-09-10  8:23 ` Karel Zak [this message]
2012-09-17  0:21   ` Amit Uttamchandani

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120910082306.GA27276@x2.net.home \
    --to=kzak@redhat.com \
    --cc=amit.uttam@gmail.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).