Skip to content

Commit ba60540

Browse files
committed
Add utility to return the effective user identity
1 parent 27ebd83 commit ba60540

12 files changed

Lines changed: 390 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# geteuid
2+
3+
> Return the effective numeric user identity of the calling process.
4+
5+
<section class="usage">
6+
7+
## Usage
8+
9+
```javascript
10+
var geteuid = require( '@stdlib/process/geteuid' );
11+
```
12+
13+
#### geteuid()
14+
15+
Returns the effective numeric user identity of the calling process.
16+
17+
```javascript
18+
var id = geteuid();
19+
```
20+
21+
</section>
22+
23+
<!-- /.usage -->
24+
25+
<section class="notes">
26+
27+
## Notes
28+
29+
- The function **only** returns an `integer` user identity on POSIX platforms. For all other platforms (e.g., Windows and Android), the function returns `null`.
30+
- See [geteuid(2)][geteuid].
31+
32+
</section>
33+
34+
<section class="examples">
35+
36+
## Examples
37+
38+
<!-- eslint no-undef: "error" -->
39+
40+
```javascript
41+
var geteuid = require( '@stdlib/process/geteuid' );
42+
43+
var uid = geteuid();
44+
if ( uid === 0 ) {
45+
console.log( 'Effectively running as root.' );
46+
} else {
47+
console.log( 'uid: %d', uid );
48+
}
49+
```
50+
51+
</section>
52+
53+
<!-- /.examples -->
54+
55+
<section class="links">
56+
57+
[geteuid]: http://man7.org/linux/man-pages/man2/geteuid.2.html
58+
59+
</section>
60+
61+
<!-- /.links -->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var isNull = require( '@stdlib/assert/is-null' );
7+
var pkg = require( './../package.json' ).name;
8+
var geteuid = require( './../lib/polyfill.js' );
9+
10+
11+
// MAIN //
12+
13+
bench( pkg+'::browser', function benchmark( b ) {
14+
var uid;
15+
var i;
16+
17+
b.tic();
18+
for ( i = 0; i < b.iterations; i++ ) {
19+
// Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear.
20+
uid = geteuid();
21+
if ( !isNull( uid ) ) {
22+
b.fail( 'should return null' );
23+
}
24+
}
25+
b.toc();
26+
if ( !isNull( uid ) ) {
27+
b.fail( 'should return null' );
28+
}
29+
b.pass( 'benchmark finished' );
30+
b.end();
31+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
7+
var isNull = require( '@stdlib/assert/is-null' );
8+
var pkg = require( './../package.json' ).name;
9+
var geteuid = require( './../lib' );
10+
11+
12+
// MAIN //
13+
14+
bench( pkg, function benchmark( b ) {
15+
var uid;
16+
var i;
17+
18+
b.tic();
19+
for ( i = 0; i < b.iterations; i++ ) {
20+
// Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear.
21+
uid = geteuid();
22+
if ( !isNumber( uid ) && !isNull( uid ) ) {
23+
b.fail( 'should return a number or null' );
24+
}
25+
}
26+
b.toc();
27+
if ( !isNumber( uid ) && !isNull( uid ) ) {
28+
b.fail( 'should return a number or null' );
29+
}
30+
b.pass( 'benchmark finished' );
31+
b.end();
32+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
{{alias}}()
3+
Returns the effective numeric user identity of the calling process.
4+
5+
The function only returns a user identity on POSIX platforms. For all other
6+
platforms (e.g., Windows and Android), the function returns `null`.
7+
8+
Returns
9+
-------
10+
id: integer|null
11+
Effective numeric user identity.
12+
13+
Examples
14+
--------
15+
> var uid = {{alias}}()
16+
17+
See Also
18+
--------
19+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
var geteuid = require( './../lib' );
4+
5+
var uid = geteuid();
6+
if ( uid === 0 ) {
7+
console.log( 'Effectively running as root.' );
8+
} else {
9+
console.log( 'uid: %d', uid );
10+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
/**
4+
* Return the effective numeric user identity of the calling process.
5+
*
6+
* @module @stdlib/process/geteuid
7+
*
8+
* @example
9+
* var geteuid = require( '@stdlib/process/geteuid' );
10+
*
11+
* var uid = geteuid();
12+
*/
13+
14+
// MODULES //
15+
16+
var geteuid = require( './main.js' );
17+
18+
19+
// EXPORTS //
20+
21+
module.exports = geteuid;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var isFunction = require( '@stdlib/assert/is-function' );
6+
var polyfill = require( './polyfill.js' );
7+
var builtin = require( './native.js' );
8+
9+
10+
// MAIN //
11+
12+
/**
13+
* Returns the effective numeric user identity of the calling process.
14+
*
15+
* @name geteuid
16+
* @type {Function}
17+
* @returns {(integer|null)} effective numeric user identity or null
18+
*
19+
* @example
20+
* var uid = geteuid();
21+
*/
22+
var geteuid = ( isFunction( builtin ) ) ? builtin : polyfill;
23+
24+
25+
// EXPORTS //
26+
27+
module.exports = geteuid;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
/**
4+
* Returns the effective numeric user identity of the calling process.
5+
*
6+
* @private
7+
* @name geteuid
8+
* @type {Function}
9+
* @returns {integer} effective numeric user identity
10+
*
11+
* @example
12+
* var uid = geteuid();
13+
*/
14+
var geteuid = process.geteuid;
15+
16+
17+
// EXPORTS //
18+
19+
module.exports = geteuid;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
// MAIN //
4+
5+
/**
6+
* Polyfill for returning an effective numeric user identity for environments which do not provide native support.
7+
*
8+
* @private
9+
* @name geteuid
10+
* @type {Function}
11+
* @returns {null} null
12+
*
13+
* @example
14+
* var uid = geteuid();
15+
* // returns null
16+
*/
17+
function geteuid() {
18+
return null;
19+
}
20+
21+
22+
// EXPORTS //
23+
24+
module.exports = geteuid;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"name": "@stdlib/process/geteuid",
3+
"version": "0.0.0",
4+
"description": "Return the effective numeric user identity of a calling process.",
5+
"license": "Apache-2.0",
6+
"author": {
7+
"name": "The Stdlib Authors",
8+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9+
},
10+
"contributors": [
11+
{
12+
"name": "The Stdlib Authors",
13+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14+
}
15+
],
16+
"main": "./lib",
17+
"browser": "./lib/polyfill.js",
18+
"directories": {
19+
"benchmark": "./benchmark",
20+
"doc": "./docs",
21+
"example": "./examples",
22+
"lib": "./lib",
23+
"test": "./test"
24+
},
25+
"scripts": {},
26+
"homepage": "https://github.com/stdlib-js/stdlib",
27+
"repository": {
28+
"type": "git",
29+
"url": "git://github.com/stdlib-js/stdlib.git"
30+
},
31+
"bugs": {
32+
"url": "https://github.com/stdlib-js/stdlib/issues"
33+
},
34+
"dependencies": {},
35+
"devDependencies": {},
36+
"engines": {
37+
"node": ">=0.10.0",
38+
"npm": ">2.7.0"
39+
},
40+
"os": [
41+
"aix",
42+
"darwin",
43+
"freebsd",
44+
"linux",
45+
"macos",
46+
"openbsd",
47+
"sunos",
48+
"win32",
49+
"windows"
50+
],
51+
"keywords": [
52+
"stdlib",
53+
"stdutils",
54+
"stdutil",
55+
"utils",
56+
"util",
57+
"utilities",
58+
"utility",
59+
"process",
60+
"geteuid",
61+
"getuid",
62+
"posix",
63+
"linux",
64+
"darwin",
65+
"effective",
66+
"uid",
67+
"user",
68+
"identity",
69+
"id"
70+
]
71+
}

0 commit comments

Comments
 (0)