前文已经向大家介绍了用脚本封装系统调用的方法。在本文中,我将向大家介绍使用.c文件封装系统调用的方法。
使用.c文件封装系统调用一般出于以下两个原因:一,系统调用已过时,可以使用新的系统调用替换旧的系统调用。系统调用的语义不变。二,封装后的函数改变了系统调用的语义。
stat系统调用的封装便是原因一的一个例子。
stat系统调用是106号系统调用。使用stat系统调用可以获取文件的属性,但是获取的属性值都是32位的,这对于现在的64位文件系统来说是有问题的。所以linux又定义了stat64系统调用,用于获取64位文件属性。所以,Linux内核中关于stat的系统调用有两个,且stat64系统调用可以替换stat系统调用。而在glibc中,stat系统调用也是使用stat64来封装的。
让我们观察stat系统调用的封装代码:
#undef stat
int
attribute_hidden
__stat (const char *file, struct stat *buf)
{
return __xstat (_STAT_VER, file, buf);
}
weak_hidden_alias (__stat, stat)
stat函数调用了__xstat函数完成函数的封装。
int
__xstat (int vers, const char *name, struct stat *buf)
{
int result;
if (vers == _STAT_VER_KERNEL)
return INLINE_SYSCALL (stat, 2, name, buf);
{
struct stat64 buf64;
INTERNAL_SYSCALL_DECL (err);
result = INTERNAL_SYSCALL (stat64, err, 2, name, &buf64);
if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (result, err)))
return INLINE_SYSCALL_ERROR_RETURN_VALUE (INTERNAL_SYSCALL_ERRNO (result,
err));
else
return __xstat32_conv (vers, &buf64, buf);
}
}
#undef INTERNAL_SYSCALL_DECL