title: setuid() return -1 when specified user ID is the same as the effective user ID
body: <p>I've compiled a binary <code>Authorization</code> and here's its permission</p>
  
  <blockquote>
    <p>-rwsr-sr-x  1 root  wheel  18464 10 26 22:07 ./Authorization</p>
  </blockquote>
  
  <p>I ran it with the root permission</p>
  
  <blockquote>
    <p>sudo ./Authorization</p>
  </blockquote>
  
  <p>so in the beginning, the <strong>uid(real uid)</strong> and <strong>euid(effective uid)</strong> of my process is</p>
  
  <blockquote>
    <p><strong>uid</strong>:0 <strong>euid</strong>:0</p>
  </blockquote>
  
  <p>then my program would invoke <strong>seteuid(501)</strong> to change the <strong>euid</strong>, now it's</p>
  
  <blockquote>
    <p><strong>uid</strong>:0 <strong>euid</strong>:501</p>
  </blockquote>
  
  <p>At last, my program would invoke <strong>setuid(501)</strong>, I expected the result is</p>
  
  <blockquote>
    <p><strong>uid:501</strong> <strong>euid: 501</strong></p>
  </blockquote>
  
  <p>According to manual of of <strong>setuid</strong>()</p>
  
  <blockquote>
    <p>The setuid() function is permitted if the effective user ID is that of the super
         user, <strong>or if the specified user ID is the same as the effective user ID.</strong></p>
  </blockquote>
  
  <p>However, <strong>setuid(501)</strong> return -1 which is not expected, and not the behavior described in the manual, <strong>WHY</strong>??</p>
  
  <p><strong>Here's my code</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  int main(int argc, const char * argv[]) {
      printf("uid: %d euid: %d\n", getuid(), geteuid());
  
      if (seteuid(501) == -1) {
          printf("seteuid error\n");
      }
      printf("seteuid(501)&gt; uid: %d euid: %d\n", getuid(), geteuid());
  
      if (setuid(501) == -1) {
          printf("setuid error\n");
      }
      printf("setuid(501)&gt; uid: %d euid: %d\n", getuid(), geteuid());
  
      return 0;
  }
  </code></pre>