Manipulating "sticky bit" within a C program
How do we set, reset and check the "sticky bit" 开发者_高级运维from within a C program?
Thanks
To read the stick bit you use stat()
check the .st_mode
for S_ISVTX
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
struct stat file_stats;
stat("my_file", &file_stats);
if (file_stats.st_mode & S_ISVTX)
printf("sticky\n");
to reset it, you do it via chmod
struct stat file_stats;
stat("my_file", &file_stats);
mode_t new_mode = file_stats.st_mode & ~S_ISVTX;
chmod("my_file", new_mode);
to set it, chmod
it is
struct stat file_stats;
stat("my_file", &file_stats);
mode_t new_mode = file_stats.st_mode | S_ISVTX;
chmod("my_file", new_mode);
this code is untested.
man pages: stat(2) chmod(2)
It's bit 01000 (octal), so you can set it with chmod(dir, 01000 | perms)
. I'm sure if you poke around the headers, maybe stat.h, you'll find the correct name for the bit.
精彩评论