File: C:/Ruby27-x64/share/doc/ruby/html/Process/UID.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>module Process::UID - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "../";
var index_rel_prefix = "../";
</script>
<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>
<link href="../css/fonts.css" rel="stylesheet">
<link href="../css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="module">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="../index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="../table_of_contents.html#pages">Pages</a>
<a href="../table_of_contents.html#classes">Classes</a>
<a href="../table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-change_privilege">::change_privilege</a>
<li ><a href="#method-c-eid">::eid</a>
<li ><a href="#method-c-from_name">::from_name</a>
<li ><a href="#method-c-grant_privilege">::grant_privilege</a>
<li ><a href="#method-c-re_exchange">::re_exchange</a>
<li ><a href="#method-c-re_exchangeable-3F">::re_exchangeable?</a>
<li ><a href="#method-c-rid">::rid</a>
<li ><a href="#method-c-sid_available-3F">::sid_available?</a>
<li ><a href="#method-c-switch">::switch</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="module-Process::UID">
<h1 id="module-Process::UID" class="module">
module Process::UID
</h1>
<section class="description">
<p>The <a href="UID.html"><code>Process::UID</code></a> module contains a collection of module functions which can be used to portably get, set, and switch the current process's real, effective, and saved user IDs.</p>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-change_privilege" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Process::UID.change_privilege(user) → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Change the current process's real and effective user ID to that specified by <em>user</em>. Returns the new user ID. Not available on all platforms.</p>
<pre class="ruby">[<span class="ruby-constant">Process</span>.<span class="ruby-identifier">uid</span>, <span class="ruby-constant">Process</span>.<span class="ruby-identifier">euid</span>] <span class="ruby-comment">#=> [0, 0]</span>
<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">UID</span>.<span class="ruby-identifier">change_privilege</span>(<span class="ruby-value">31</span>) <span class="ruby-comment">#=> 31</span>
[<span class="ruby-constant">Process</span>.<span class="ruby-identifier">uid</span>, <span class="ruby-constant">Process</span>.<span class="ruby-identifier">euid</span>] <span class="ruby-comment">#=> [31, 31]</span>
</pre>
<div class="method-source-code" id="change_privilege-source">
<pre>static VALUE
p_uid_change_privilege(VALUE obj, VALUE id)
{
rb_uid_t uid;
check_uid_switch();
uid = OBJ2UID(id);
if (geteuid() == 0) { /* root-user */
#if defined(HAVE_SETRESUID)
if (setresuid(uid, uid, uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
#elif defined(HAVE_SETUID)
if (setuid(uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
if (getuid() == uid) {
if (SAVED_USER_ID == uid) {
if (setreuid(-1, uid) < 0) rb_sys_fail(0);
}
else {
if (uid == 0) { /* (r,e,s) == (root, root, x) */
if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
if (setreuid(SAVED_USER_ID, 0) < 0) rb_sys_fail(0);
SAVED_USER_ID = 0; /* (r,e,s) == (x, root, root) */
if (setreuid(uid, uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
}
else {
if (setreuid(0, -1) < 0) rb_sys_fail(0);
SAVED_USER_ID = 0;
if (setreuid(uid, uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
}
}
}
else {
if (setreuid(uid, uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
}
#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
if (getuid() == uid) {
if (SAVED_USER_ID == uid) {
if (seteuid(uid) < 0) rb_sys_fail(0);
}
else {
if (uid == 0) {
if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
SAVED_USER_ID = 0;
if (setruid(0) < 0) rb_sys_fail(0);
}
else {
if (setruid(0) < 0) rb_sys_fail(0);
SAVED_USER_ID = 0;
if (seteuid(uid) < 0) rb_sys_fail(0);
if (setruid(uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
}
}
}
else {
if (seteuid(uid) < 0) rb_sys_fail(0);
if (setruid(uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
}
#else
(void)uid;
rb_notimplement();
#endif
}
else { /* unprivileged user */
#if defined(HAVE_SETRESUID)
if (setresuid((getuid() == uid)? (rb_uid_t)-1: uid,
(geteuid() == uid)? (rb_uid_t)-1: uid,
(SAVED_USER_ID == uid)? (rb_uid_t)-1: uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
if (SAVED_USER_ID == uid) {
if (setreuid((getuid() == uid)? (rb_uid_t)-1: uid,
(geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
rb_sys_fail(0);
}
else if (getuid() != uid) {
if (setreuid(uid, (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
rb_sys_fail(0);
SAVED_USER_ID = uid;
}
else if (/* getuid() == uid && */ geteuid() != uid) {
if (setreuid(geteuid(), uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
if (setreuid(uid, -1) < 0) rb_sys_fail(0);
}
else { /* getuid() == uid && geteuid() == uid */
if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
if (setreuid(SAVED_USER_ID, uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
if (setreuid(uid, -1) < 0) rb_sys_fail(0);
}
#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
if (SAVED_USER_ID == uid) {
if (geteuid() != uid && seteuid(uid) < 0) rb_sys_fail(0);
if (getuid() != uid && setruid(uid) < 0) rb_sys_fail(0);
}
else if (/* SAVED_USER_ID != uid && */ geteuid() == uid) {
if (getuid() != uid) {
if (setruid(uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
}
else {
if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
if (setruid(uid) < 0) rb_sys_fail(0);
}
}
else if (/* geteuid() != uid && */ getuid() == uid) {
if (seteuid(uid) < 0) rb_sys_fail(0);
if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
if (setruid(uid) < 0) rb_sys_fail(0);
}
else {
rb_syserr_fail(EPERM, 0);
}
#elif defined HAVE_44BSD_SETUID
if (getuid() == uid) {
/* (r,e,s)==(uid,?,?) ==> (uid,uid,uid) */
if (setuid(uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
}
else {
rb_syserr_fail(EPERM, 0);
}
#elif defined HAVE_SETEUID
if (getuid() == uid && SAVED_USER_ID == uid) {
if (seteuid(uid) < 0) rb_sys_fail(0);
}
else {
rb_syserr_fail(EPERM, 0);
}
#elif defined HAVE_SETUID
if (getuid() == uid && SAVED_USER_ID == uid) {
if (setuid(uid) < 0) rb_sys_fail(0);
}
else {
rb_syserr_fail(EPERM, 0);
}
#else
rb_notimplement();
#endif
}
return id;
}</pre>
</div>
</div>
</div>
<div id="method-c-eid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
euid → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::UID.eid → integer
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::Sys.geteuid → integer
</span>
</div>
<div class="method-description">
<p>Returns the effective user ID for this process.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">euid</span> <span class="ruby-comment">#=> 501</span>
</pre>
<div class="method-source-code" id="eid-source">
<pre>static VALUE
proc_geteuid(VALUE obj)
{
rb_uid_t euid = geteuid();
return UIDT2NUM(euid);
}</pre>
</div>
</div>
</div>
<div id="method-c-from_name" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Process::UID.from_name(name) → uid
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Get the user ID by the <em>name</em>. If the user is not found, <code>ArgumentError</code> will be raised.</p>
<pre class="ruby"><span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">UID</span>.<span class="ruby-identifier">from_name</span>(<span class="ruby-string">"root"</span>) <span class="ruby-comment">#=> 0</span>
<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">UID</span>.<span class="ruby-identifier">from_name</span>(<span class="ruby-string">"nosuchuser"</span>) <span class="ruby-comment">#=> can't find user for nosuchuser (ArgumentError)</span>
</pre>
<div class="method-source-code" id="from_name-source">
<pre>static VALUE
p_uid_from_name(VALUE self, VALUE id)
{
return UIDT2NUM(OBJ2UID(id));
}</pre>
</div>
</div>
</div>
<div id="method-c-grant_privilege" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Process::UID.grant_privilege(user) → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::UID.eid= user → integer
</span>
</div>
<div class="method-description">
<p><a href="../Set.html"><code>Set</code></a> the effective user ID, and if possible, the saved user ID of the process to the given <em>user</em>. Returns the new effective user ID. Not available on all platforms.</p>
<pre class="ruby">[<span class="ruby-constant">Process</span>.<span class="ruby-identifier">uid</span>, <span class="ruby-constant">Process</span>.<span class="ruby-identifier">euid</span>] <span class="ruby-comment">#=> [0, 0]</span>
<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">UID</span>.<span class="ruby-identifier">grant_privilege</span>(<span class="ruby-value">31</span>) <span class="ruby-comment">#=> 31</span>
[<span class="ruby-constant">Process</span>.<span class="ruby-identifier">uid</span>, <span class="ruby-constant">Process</span>.<span class="ruby-identifier">euid</span>] <span class="ruby-comment">#=> [0, 31]</span>
</pre>
<div class="method-source-code" id="grant_privilege-source">
<pre>static VALUE
p_uid_grant_privilege(VALUE obj, VALUE id)
{
rb_seteuid_core(OBJ2UID(id));
return id;
}</pre>
</div>
</div>
</div>
<div id="method-c-re_exchange" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Process::UID.re_exchange → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Exchange real and effective user IDs and return the new effective user ID. Not available on all platforms.</p>
<pre class="ruby">[<span class="ruby-constant">Process</span>.<span class="ruby-identifier">uid</span>, <span class="ruby-constant">Process</span>.<span class="ruby-identifier">euid</span>] <span class="ruby-comment">#=> [0, 31]</span>
<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">UID</span>.<span class="ruby-identifier">re_exchange</span> <span class="ruby-comment">#=> 0</span>
[<span class="ruby-constant">Process</span>.<span class="ruby-identifier">uid</span>, <span class="ruby-constant">Process</span>.<span class="ruby-identifier">euid</span>] <span class="ruby-comment">#=> [31, 0]</span>
</pre>
<div class="method-source-code" id="re_exchange-source">
<pre>static VALUE
p_uid_exchange(VALUE obj)
{
rb_uid_t uid;
#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
rb_uid_t euid;
#endif
check_uid_switch();
uid = getuid();
#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
euid = geteuid();
#endif
#if defined(HAVE_SETRESUID)
if (setresuid(euid, uid, uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
if (setreuid(euid,uid) < 0) rb_sys_fail(0);
SAVED_USER_ID = uid;
#else
rb_notimplement();
#endif
return UIDT2NUM(uid);
}</pre>
</div>
</div>
</div>
<div id="method-c-re_exchangeable-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Process::UID.re_exchangeable? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if the real and effective user IDs of a process may be exchanged on the current platform.</p>
<div class="method-source-code" id="re_exchangeable-3F-source">
<pre>static VALUE
p_uid_exchangeable(VALUE _)
{
#if defined(HAVE_SETRESUID)
return Qtrue;
#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
return Qtrue;
#else
return Qfalse;
#endif
}</pre>
</div>
</div>
</div>
<div id="method-c-rid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
uid → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::UID.rid → integer
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::Sys.getuid → integer
</span>
</div>
<div class="method-description">
<p>Returns the (real) user ID of this process.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">uid</span> <span class="ruby-comment">#=> 501</span>
</pre>
<div class="method-source-code" id="rid-source">
<pre>static VALUE
proc_getuid(VALUE obj)
{
rb_uid_t uid = getuid();
return UIDT2NUM(uid);
}</pre>
</div>
</div>
</div>
<div id="method-c-sid_available-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Process::UID.sid_available? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if the current platform has saved user ID functionality.</p>
<div class="method-source-code" id="sid_available-3F-source">
<pre>static VALUE
p_uid_have_saved_id(VALUE _)
{
#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
return Qtrue;
#else
return Qfalse;
#endif
}</pre>
</div>
</div>
</div>
<div id="method-c-switch" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
Process::UID.switch → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::UID.switch {|| block} → object
</span>
</div>
<div class="method-description">
<p>Switch the effective and real user IDs of the current process. If a <em>block</em> 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.</p>
<div class="method-source-code" id="switch-source">
<pre>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 {
rb_syserr_fail(EPERM, 0);
}
UNREACHABLE_RETURN(Qnil);
}</pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>