title: How do I chattr +i an open file descriptor in C?
body: <p>I want to make a binary that does the equivalent to chattr +i (immutable file) in C. I have attempted with ioctl() and had no success with it. If possible I would like it to be done without the need to include any additional libraries that are not installed/defined in a standard installation.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/ioctl.h&gt;
  #include &lt;linux/fs.h&gt;
  
  int main()
  {
      FILE *fp;
      char shovel[16] = "I have a shovel!";
      fp = fopen("shovel.txt", "w+");
      fwrite(shovel, sizeof(shovel[0]), sizeof(shovel)/sizeof(shovel[0]), fp);
      int immutable = FS_IMMUTABLE_FL;
      if(ioctl(fileno(fp), FS_IOC_SETFLAGS, &amp;immutable) &lt; 0)
          perror("ioctl(2) error");
      fclose(fp);
      return 0;
  }
  </code></pre>
  
  <p>perror returns "ioctl(2) error: Operation not supported" when ran as root with global rwx on the file.</p>
  
  <p>I have tested that chattr +i works on the command line. The file becomes immutable as expected.</p>
  
  <p>Also the question marked as duplicate is my question on an account I no longer have access to. It's linked to one of many of my google accounts I created.</p>
  
  <p>The script was tested on a friends server last night after my vps went down for maintenance. The feedback I gave on that thread was based on feedback from the friend over skype. What you see above is the results of my own testing on my own server.</p>
  
  <p>Not a duplicate, the original was reporting "Bad Address"</p>
  
  <pre><code>-bash-3.2# cat /proc/mounts
  /dev/simfs / simfs rw,relatime,usrquota,grpquota 0 0
  proc /proc proc rw,relatime 0 0
  sysfs /sys sysfs rw,relatime 0 0
  none /dev tmpfs rw,relatime,mode=755 0 0
  none /dev/pts devpts rw,relatime,gid=5,mode=620,ptmxmode=000 0 0
  none /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0
  
  -bash-3.2# rm shovel.txt
  -bash-3.2# ./1
  ioctl(2) error: Operation not supported
  -bash-3.2# lsattr shovel.txt
  ------------- shovel.txt
  -bash-3.2#
  </code></pre>