Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating "sticky bit" within a C program

Tags:

c

linux

How do we set, reset and check the "sticky bit" from within a C program?

Thanks

like image 842
Lipika Deka Avatar asked Jul 09 '11 04:07

Lipika Deka


1 Answers

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)

like image 54
Vinicius Kamakura Avatar answered Oct 25 '22 15:10

Vinicius Kamakura