Setting Immutable Flag using ioctl() in C

后端 未结 2 1528
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 10:28

I have attempted to make a script that creates a file and then sets it as immutable similar to the chattr +i command for linux. The script compiles

2条回答
  •  生来不讨喜
    2020-12-11 11:06

    The main problem is that the ioctl wants a pointer to the mask, not a direct constant. You have to define a int variable, store the mask (0x10) in it and pass its address as third argument of ioctl.

    Also, I'd add some hints:

    • other programs to change attributes are used to use low-level I/O directly (open, close...). Also, the file is usually opened with O_RDONLY.
    • Use FS_IMMUTABLE_FL istead the raw constant.
    • Get the current attribute mask first (FS_IOC_SETFLAGS) and mask it with the new flag, so other settings are not lost by the service.

提交回复
热议问题