0

I have some question.

I try to read some VFS attributes, for example s_magic value in struct super_block. but I cant read s_magic.

This is my code.

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<linux/fs.h>
int main()
{
    int fd;
    char boot[1024];
    struct super_block sb;
    fd = open("/dev/fd", O_RDONLY);
    read(fd, boot, 1024);
    read(fd, &sb, sizeof(struct super_block);
    printf("%x\n", sb.s_magic);
    close(fd);

    return 0;
 }

so, This code does't work with some error. In this error, storage size of ‘sb’ isn’t known and invalid application of ‘sizeof’ to incomplete type ‘struct super_block’

Thank you.

pamiers
  • 355
  • 1
  • 3
  • 11

1 Answers1

1

That's because your linux/fs.h does not contain super_block declaration. That's because you want to include linux/fs.h from Linux kernel but actually include linux/fs.h from Linux userspace. Supply -I <include path> option to gcc like this

gcc -I /usr/src/kernels/$(uname -r)/include

BUT!

You will get a million of errors because you can't just include kernel headers in your userspace program - you don't have all type and struct definitions.

The kernel headers are not written with user space in mind, and they can change at any time. The proper way for user-space applications to interface with the kernel is by way of the C library, which provides its own structures and, when necessary, translates them into whatever the current kernel expects. This separation helps to keep user-space programs from breaking when the kernel changes.

(source http://lwn.net/Articles/113349/)

So you have to revise your code.

P.S. I've given you and explanation why your code is not working, but I don't know how you can read super_block in userspace. You better ask another question regarding filesystem superblock API.

Alexander Dzyoba
  • 4,009
  • 1
  • 24
  • 29