Skip to content

Instantly share code, notes, and snippets.

@4ge32
Last active October 31, 2018 08:39
Show Gist options
  • Save 4ge32/67c012874040d3407e12aae69bbe1b77 to your computer and use it in GitHub Desktop.
Save 4ge32/67c012874040d3407e12aae69bbe1b77 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
enum test_type {
IOC_VERSION = 1,
IOC_FLAGS,
};
static int test_ioctl(const char *path, enum test_type test, int arg)
{
int fd;
int err;
int SET;
int GET;
switch (test) {
case IOC_VERSION:
SET = FS_IOC_SETVERSION;
GET = FS_IOC_GETVERSION;
break;
case IOC_FLAGS:
SET = FS_IOC_SETFLAGS;
GET = FS_IOC_GETFLAGS;
break;
default:
printf("BOOOO\n");
return -1;
}
if ((fd = open(path, O_RDWR)) < 0) {
perror("open : ");
return -1;
}
if ((err = ioctl(fd, SET, &arg)) < 0) {
perror("ioctl : ");
goto close_fd;
}
printf("set = %d\n", arg);
if ((err = ioctl(fd, GET, &arg)) < 0) {
perror("ioctl : ");
goto close_fd;
}
printf("get = %d\n", arg);
close_fd:
if ((err = close(fd)) < 0)
perror("close : ");
return err;
}
int main(int argc, char *argv[])
{
char *path;
if (argc != 2) {
fprintf(stderr, "usage: %s path\n", argv[0]);
return -1;
}
path = argv[1];
return test_ioctl(path, IOC_FLAGS, FS_IMMUTABLE_FL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment