2 // Mono.Unix/UnixEnvironment.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2004 Jonathan Pryor
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
36 public sealed /* static */ class UnixEnvironment
38 private UnixEnvironment () {}
40 public static string CurrentDirectory {
42 return UnixDirectoryInfo.GetCurrentDirectory ();
45 UnixDirectoryInfo.SetCurrentDirectory (value);
49 public static string MachineName {
51 StringBuilder buf = new StringBuilder (8);
53 Native.Errno e = (Native.Errno) 0;
56 r = Native.Syscall.gethostname (buf);
57 } while (r == (-1) && ((e = Native.Stdlib.GetLastError()) == Native.Errno.EINVAL) ||
58 (e == Native.Errno.ENAMETOOLONG));
60 UnixMarshal.ThrowExceptionForLastError ();
61 return buf.ToString ();
64 int r = Native.Syscall.sethostname (value);
65 UnixMarshal.ThrowExceptionForLastErrorIf (r);
69 public static string UserName {
70 get {return UnixUserInfo.GetRealUser ().UserName;}
73 public static UnixGroupInfo RealGroup {
74 get {return new UnixGroupInfo (RealGroupId);}
75 // set can't be done as setgid(2) modifies effective gid as well
78 public static long RealGroupId {
79 get {return Native.Syscall.getgid ();}
80 // set can't be done as setgid(2) modifies effective gid as well
83 public static UnixUserInfo RealUser {
84 get {return new UnixUserInfo (RealUserId);}
85 // set can't be done as setuid(2) modifies effective uid as well
88 public static long RealUserId {
89 get {return Native.Syscall.getuid ();}
90 // set can't be done as setuid(2) modifies effective uid as well
93 public static UnixGroupInfo EffectiveGroup {
94 get {return new UnixGroupInfo (EffectiveGroupId);}
95 set {EffectiveGroupId = value.GroupId;}
98 public static long EffectiveGroupId {
99 get {return Native.Syscall.getegid ();}
100 set {Native.Syscall.setegid (Convert.ToUInt32 (value));}
103 public static UnixUserInfo EffectiveUser {
104 get {return new UnixUserInfo (EffectiveUserId);}
105 set {EffectiveUserId = value.UserId;}
108 public static long EffectiveUserId {
109 get {return Native.Syscall.geteuid ();}
110 set {Native.Syscall.seteuid (Convert.ToUInt32 (value));}
113 public static string Login {
114 get {return UnixUserInfo.GetRealUser ().UserName;}
117 [CLSCompliant (false)]
118 public static long GetConfigurationValue (Native.SysconfName name)
120 long r = Native.Syscall.sysconf (name);
121 if (r == -1 && Native.Stdlib.GetLastError() != (Native.Errno) 0)
122 UnixMarshal.ThrowExceptionForLastError ();
126 [CLSCompliant (false)]
127 public static string GetConfigurationString (Native.ConfstrName name)
129 ulong len = Native.Syscall.confstr (name, null, 0);
130 if (len == unchecked ((ulong) (-1)))
131 UnixMarshal.ThrowExceptionForLastError ();
134 StringBuilder buf = new StringBuilder ((int) len+1);
135 len = Native.Syscall.confstr (name, buf, len);
136 if (len == unchecked ((ulong) (-1)))
137 UnixMarshal.ThrowExceptionForLastError ();
138 return buf.ToString ();
141 public static void SetNiceValue (int inc)
143 int r = Native.Syscall.nice (inc);
144 UnixMarshal.ThrowExceptionForLastErrorIf (r);
147 public static int CreateSession ()
149 int s = Native.Syscall.setsid ();
150 UnixMarshal.ThrowExceptionForLastErrorIf (s);
154 public static void SetProcessGroup ()
156 int r = Native.Syscall.setpgrp ();
157 UnixMarshal.ThrowExceptionForLastErrorIf (r);
160 public static int GetProcessGroup ()
162 return Native.Syscall.getpgrp ();
165 public static UnixGroupInfo[] GetSupplementaryGroups ()
167 uint[] ids = _GetSupplementaryGroupIds ();
168 UnixGroupInfo[] groups = new UnixGroupInfo [ids.Length];
169 for (int i = 0; i < groups.Length; ++i)
170 groups [i] = new UnixGroupInfo (ids [i]);
174 private static uint[] _GetSupplementaryGroupIds ()
176 int ngroups = Native.Syscall.getgroups (0, new uint[]{});
178 UnixMarshal.ThrowExceptionForLastError ();
179 uint[] groups = new uint[ngroups];
180 int r = Native.Syscall.getgroups (groups);
181 UnixMarshal.ThrowExceptionForLastErrorIf (r);
185 public static void SetSupplementaryGroups (UnixGroupInfo[] groups)
187 uint[] list = new uint [groups.Length];
188 for (int i = 0; i < list.Length; ++i) {
189 list [i] = Convert.ToUInt32 (groups [i].GroupId);
191 int r = Native.Syscall.setgroups (list);
192 UnixMarshal.ThrowExceptionForLastErrorIf (r);
195 public static long[] GetSupplementaryGroupIds ()
197 uint[] _groups = _GetSupplementaryGroupIds ();
198 long[] groups = new long [_groups.Length];
199 for (int i = 0; i < groups.Length; ++i)
200 groups [i] = _groups [i];
204 public static void SetSupplementaryGroupIds (long[] list)
206 uint[] _list = new uint [list.Length];
207 for (int i = 0; i < _list.Length; ++i)
208 _list [i] = Convert.ToUInt32 (list [i]);
209 int r = Native.Syscall.setgroups (_list);
210 UnixMarshal.ThrowExceptionForLastErrorIf (r);
213 public static int GetParentProcessId ()
215 return Native.Syscall.getppid ();
218 public static UnixProcess GetParentProcess ()
220 return new UnixProcess (GetParentProcessId ());
223 public static string[] GetUserShells ()
225 ArrayList shells = new ArrayList ();
227 lock (Native.Syscall.usershell_lock) {
229 if (Native.Syscall.setusershell () != 0)
230 UnixMarshal.ThrowExceptionForLastError ();
232 while ((shell = Native.Syscall.getusershell ()) != null)
236 Native.Syscall.endusershell ();
240 return (string[]) shells.ToArray (typeof(string));