sysfsシステムコール
登録されているファイルシステム名/インデックスを取得するシステムコールです。インデックスは0オリジンです。
オプション1:引数のファイルシステムのインデックス
オプション2:引数のインデックスのファイルシステム名
オプション3:登録されているファイルシステム数
インデックスはシステムに登録された順序になります。
オプション1:引数のファイルシステムのインデックス
オプション2:引数のインデックスのファイルシステム名
オプション3:登録されているファイルシステム数
インデックスはシステムに登録された順序になります。
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
int err;
char arg[64];
err = syscall(SYS_sysfs, 1, "ramfs", NULL);
printf("%d\n", err);
err = syscall(SYS_sysfs, 2, 20, arg);
printf("%s\n", arg);
err = syscall(SYS_sysfs, 3, NULL, NULL);
printf("%d\n", err);
}
[root@localhost kitamura]# cat /proc/filesystems | nl
:
14 nodev anon_inodefs
15 nodev configfs
16 nodev devpts
17 ext3
18 ext2
19 ext4
20 nodev ramfs
21 nodev hugetlbfs
22 iso9660
23 nodev autofs
24 nodev pstore
25 nodev mqueue
26 nodev binfmt_misc
27 nodev rpc_pipefs
[root@localhost kitamura]# ./a.out
19
hugetlbfs
27
リストヘッドとするfile_systemsを走査しているだけです。try_module_get()はファイルシステムのモジュールがMODULE_STATE_LIVEかチェックします。
SYSCALL_DEFINE3(sysfs, int, option, unsigned long, arg1, unsigned long, arg2)
{
int retval = -EINVAL;
switch (option) {
case 1:
retval = fs_index((const char __user *) arg1);
break;
case 2:
retval = fs_name(arg1, (char __user *) arg2);
break;
case 3:
retval = fs_maxindex();
break;
}
return retval;
}
static int fs_index(const char __user * __name)
{
struct file_system_type * tmp;
char * name;
int err, index;
name = getname(__name);
err = PTR_ERR(name);
if (IS_ERR(name))
return err;
err = -EINVAL;
read_lock(&file_systems_lock);
for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
if (strcmp(tmp->name,name) == 0) {
err = index;
break;
}
}
read_unlock(&file_systems_lock);
putname(name);
return err;
}
static int fs_name(unsigned int index, char __user * buf)
{
struct file_system_type * tmp;
int len, res;
read_lock(&file_systems_lock);
for (tmp = file_systems; tmp; tmp = tmp->next, index--)
if (index <= 0 && try_module_get(tmp->owner))
break;
read_unlock(&file_systems_lock);
if (!tmp)
return -EINVAL;
len = strlen(tmp->name) + 1;
res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
put_filesystem(tmp);
return res;
}
static int fs_maxindex(void)
{
struct file_system_type * tmp;
int index;
read_lock(&file_systems_lock);
for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
;
read_unlock(&file_systems_lock);
return index;
}






