linux capability编译报错
我最近刚开始研究linux capability机制,这是网上一段代码,编译时报错
test3.c: 在函数‘listCaps’中:
test3.c:18:3: 错误: 未知的类型名‘cap_t’
test3.c:21:10: 警告: 格式 ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
test3.c: 在函数‘main’中:
test3.c:35:3: 错误: 未知的类型名‘cap_t’
test3.c:38:3: 错误: 未知的类型名‘cap_value_t’
test3.c:41:22: 错误: ‘CAP_EFFECTIVE’未声明(在此函数内第一次使用)
test3.c:41:22: 附注: 每个未声明的标识符在其出现的函数内只报告一次
test3.c:41:56: 错误: ‘CAP_SET’未声明(在此函数内第一次使用)
test3.c:42:22: 错误: ‘CAP_INHERITABLE’未声明(在此函数内第一次使用
关键是这些变量声明在哪个头文件里,请各位大神帮忙
如下为源代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#undef _POSIX_SOURCE
#include <linux/capability.h>
extern int errno;
void whoami(void)
{
printf("uid=%i euid=%i gid=%i\n", getuid(), geteuid(), getgid());
}
void listCaps()
{
cap_t caps = cap_get_proc();
ssize_t y = 0;
printf("The process %d was give capabilities %s\n",
(int) getpid(), cap_to_text(caps, &y));
fflush(0);
cap_free(caps);
}
int main(int argc, char **argv)
{
int stat;
whoami();
stat = setuid(geteuid());
pid_t parentPid = getpid();
if(!parentPid)
return 1;
cap_t caps = cap_init();
cap_value_t capList[5] =
{ CAP_NET_RAW, CAP_NET_BIND_SERVICE , CAP_SETUID, CAP_SETGID,CAP_SETPCAP } ;
unsigned num_caps = 5;
cap_set_flag(caps, CAP_EFFECTIVE, num_caps, capList, CAP_SET);
cap_set_flag(caps, CAP_INHERITABLE, num_caps, capList, CAP_SET);
cap_set_flag(caps, CAP_PERMITTED, num_caps, capList, CAP_SET);
if (cap_set_proc(caps)) {
perror("capset()");
return EXIT_FAILURE;
}
listCaps();
printf("dropping caps\n");
cap_clear(caps); // resetting caps storage
if (cap_set_proc(caps)) {
perror("capset()");
return EXIT_FAILURE;
}
listCaps();
cap_free(caps);
return 0;
}