-
Notifications
You must be signed in to change notification settings - Fork 1
/
Posix.cs
159 lines (149 loc) · 7.42 KB
/
Posix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma warning disable SA1300
#pragma warning disable SA1629
#pragma warning disable S3218
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Perlang.Attributes;
namespace Perlang.Stdlib
{
/// <summary>
/// Provides support for calling standard POSIX functions.
///
/// The method names are deliberately using "POSIX-style", i.e. short, C-oriented names for methods like `getuid`,
/// `getpid` and so forth. This is to make it simple for people with experience from POSIX-based systems to find the
/// method they are looking for.
///
/// This class only contains POSIX-specific functions, i.e. functions available on POSIX-compliant systems like BSD,
/// GNU/Linux and macOS, but not available on Windows. For C functions available on all supported platforms, see the
/// list below.
///
/// Code which uses this class will only run on POSIX-compliant systems. If an attempt to use these methods is made
/// on Windows, a compile-time error is emitted.
///
/// ### List of methods defined in POSIX but available elsewhere:
///
/// * <see cref="Libc.environ"/>
/// * <see cref="Libc.getcwd"/>
/// * <see cref="Libc.getenv"/>
/// * <see cref="Libc.getpid"/>
/// </summary>
/// <remarks>
/// The XML method descriptions are based on `man` pages in [the NetBSD source code](https://github.com/NetBSD/src).
/// The full license of these man pages can be found at https://gitlab.perlang.org/perlang/perlang/-/blob/master/NOTICE.md
///
/// There might be subtle differences between systems on some of these functions. For information on how these work
/// on e.g. GNU/Linux, use a command like `man 2 getgid` (replace `getgid` with the name of the function you are
/// interested in). You can also consult the Linux man pages project:
/// https://man7.org/linux/man-pages/dir_all_alphabetic.html/.
/// </remarks>
[GlobalClass(PlatformID.Unix, PlatformID.MacOSX)]
public static class Posix
{
// Internal class which contains the P/Invoke definitions, to avoid exposing them directly to our callers.
[EditorBrowsable(EditorBrowsableState.Never)]
private static class Internal
{
[DllImport("libc")]
public static extern int getegid();
[DllImport("libc")]
public static extern int geteuid();
[DllImport("libc")]
public static extern int getgid();
[DllImport("libc")]
public static extern int getppid();
[DllImport("libc")]
public static extern int getuid();
}
/// <summary>
/// The <see cref="getgid">`getgid()`</see> function returns the real group ID of the calling process, <see
/// cref="getegid">getegid()</see> returns the effective group ID of the calling process.
///
/// The real group ID is specified at login time.
///
/// The real group ID is the group of the user who invoked the program. As the effective group ID gives the
/// process additional permissions during the execution of `set-group-ID` mode processes, <see
/// cref="getgid">getgid()</see> is used to determine the real-group-id of the calling process.
///
/// <see cref="getgid">getgid()</see> and <see cref="getegid">getegid()</see> conform to ISO/IEC 9945-1:1990
/// (`POSIX.1`).
/// </summary>
/// <seealso cref="getuid"/>
/// <returns>The effective group ID of the calling process.</returns>
// TODO: add when we support it: <seealso cref="setgid"/>
// TODO: add when we support it: <seealso cref="setgroups"/>
// TODO: add when we support it: <seealso cref="setregid"/>
public static int getegid()
{
return Internal.getegid();
}
/// <summary>
/// The <see cref="getuid">getuid()</see> function returns the real user ID of the calling process. The <see
/// cref="geteuid">geteuid()</see> function returns the effective user ID of the calling process.
///
/// The real user ID is that of the user who has invoked the program. As the effective user ID gives the
/// process additional permissions during execution of `set-user-ID` mode processes, <see
/// cref="getuid">getuid()</see> is used to determine the real-user-id of the calling process.
///
/// The <see cref="geteuid">geteuid()</see> and <see cref="getuid">getuid()</see> functions conform to ISO/IEC
/// 9945-1:1990 (`POSIX.1`).
/// </summary>
/// <returns>The effective user ID of the calling process.</returns>
/// <seealso cref="getgid"/>
// TODO: add when we support it: <seealso cref="setreuid"/>
public static int geteuid()
{
return Internal.geteuid();
}
/// <summary>
/// The <see cref="getgid">`getgid()`</see> function returns the real group ID of the calling process, <see
/// cref="getegid">getegid()</see> returns the effective group ID of the calling process.
///
/// The real group ID is specified at login time.
///
/// The real group ID is the group of the user who invoked the program. As the effective group ID gives the
/// process additional permissions during the execution of `set-group-ID` mode processes, <see
/// cref="getgid">getgid()</see> is used to determine the real-group-id of the calling process.
///
/// <see cref="getgid">getgid()</see> and <see cref="getegid">getegid()</see> conform to ISO/IEC 9945-1:1990
/// (`POSIX.1`).
/// </summary>
/// <returns>The real group ID of the calling process.</returns>
// TODO: add when we support it: <seealso cref="setgid"/>
// TODO: add when we support it: <seealso cref="setgroups"/>
// TODO: add when we support it: <seealso cref="setregid"/>
public static int getgid()
{
return Internal.getgid();
}
/// <summary>
/// Returns the process ID of the parent of the calling process.
///
/// <see cref="getppid">getppid()</see> conform to ISO/IEC 9945-1:1990 (`POSIX.1`).
/// </summary>
/// <returns>The parent process ID.</returns>
/// <seealso cref="Libc.getpid"/>
public static int getppid()
{
return Internal.getppid();
}
/// <summary>
/// The <see cref="getuid">getuid()</see> function returns the real user ID of the calling process. The <see
/// cref="geteuid">geteuid()</see> function returns the effective user ID of the calling process.
///
/// The real user ID is that of the user who has invoked the program. As the effective user ID gives the
/// process additional permissions during execution of `set-user-ID` mode processes, <see
/// cref="getuid">getuid()</see> is used to determine the real-user-id of the calling process.
///
/// The <see cref="geteuid">geteuid()</see> and <see cref="getuid">getuid()</see> functions conform to ISO/IEC
/// 9945-1:1990 (`POSIX.1`).
/// </summary>
/// <returns>The real user ID of the calling process.</returns>
/// <seealso cref="getgid"/>
// TODO: add when we support it: <seealso cref="setreuid"/>
public static int getuid()
{
return Internal.getuid();
}
}
}