switch()
  public
  
    
    
Switch the effective and real user IDs of the current process. If a
block is given, the user IDs will be switched back after the block
is executed. Returns the new effective user ID if called without a block,
and the return value of the block if one is given.
   
  
    Show source    
    
      static VALUE
p_uid_switch(VALUE obj)
{
    rb_uid_t uid, euid;
    check_uid_switch();
    uid = getuid();
    euid = geteuid();
    if (uid != euid) {
        proc_seteuid(uid);
        if (rb_block_given_p()) {
            under_uid_switch = 1;
            return rb_ensure(rb_yield, Qnil, p_uid_sw_ensure, SAVED_USER_ID);
        }
        else {
            return UIDT2NUM(euid);
        }
    }
    else if (euid != SAVED_USER_ID) {
        proc_seteuid(SAVED_USER_ID);
        if (rb_block_given_p()) {
            under_uid_switch = 1;
            return rb_ensure(rb_yield, Qnil, p_uid_sw_ensure, euid);
        }
        else {
            return UIDT2NUM(uid);
        }
    }
    else {
        errno = EPERM;
        rb_sys_fail(0);
    }
    UNREACHABLE;
}