2 // Mono.Unix/Syscall.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Jonathan Pryor (jonpryor@vt.edu)
8 // (C) 2003 Novell, Inc.
9 // (C) 2004-2005 Jonathan Pryor
11 // This file implements the low-level syscall interface to the POSIX
14 // This file tries to stay close to the low-level API as much as possible
15 // using enumerations, structures and in a few cases, using existing .NET
18 // Implementation notes:
20 // Since the values for the various constants on the API changes
21 // from system to system (even Linux on different architectures will
22 // have different values), we define our own set of values, and we
23 // use a set of C helper routines to map from the constants we define
24 // to the values of the native OS.
26 // Bitfields are flagged with the [Map] attribute, and a helper program
27 // generates a set of routines that we can call to convert from our value
28 // definitions to the value definitions expected by the OS; see
29 // UnixConvert for the conversion routines.
31 // Methods that require tuning are bound as `private sys_NAME' methods
32 // and then a `NAME' method is exposed.
36 // Permission is hereby granted, free of charge, to any person obtaining
37 // a copy of this software and associated documentation files (the
38 // "Software"), to deal in the Software without restriction, including
39 // without limitation the rights to use, copy, modify, merge, publish,
40 // distribute, sublicense, and/or sell copies of the Software, and to
41 // permit persons to whom the Software is furnished to do so, subject to
42 // the following conditions:
44 // The above copyright notice and this permission notice shall be
45 // included in all copies or substantial portions of the Software.
47 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
51 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
52 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
53 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57 using System.Collections;
58 using System.Runtime.InteropServices;
62 [assembly:Mono.Unix.IncludeAttribute (
63 new string [] {"sys/types.h", "sys/stat.h", ">sys/poll.h", ">sys/wait.h",
64 "unistd.h", "fcntl.h", "signal.h", ">poll.h", ">grp.h", "errno.h", ">syslog.h"},
65 new string [] {"_GNU_SOURCE", "_XOPEN_SOURCE"})]
72 public enum SyslogOptions {
73 LOG_PID = 0x01, // log the pid with each message
74 LOG_CONS = 0x02, // log on the console if errors in sending
75 LOG_ODELAY = 0x04, // delay open until first syslog (default)
76 LOG_NDELAY = 0x08, // don't delay open
77 LOG_NOWAIT = 0x10, // don't wait for console forks; DEPRECATED
78 LOG_PERROR = 0x20 // log to stderr as well
82 public enum SyslogFacility {
93 LOG_AUTHPRIV = 10 << 3,
100 LOG_LOCAL5 = 21 << 3,
101 LOG_LOCAL6 = 22 << 3,
102 LOG_LOCAL7 = 23 << 3,
106 public enum SyslogLevel {
107 LOG_EMERG = 0, // system is unusable
108 LOG_ALERT = 1, // action must be taken immediately
109 LOG_CRIT = 2, // critical conditions
110 LOG_ERR = 3, // warning conditions
111 LOG_WARNING = 4, // warning conditions
112 LOG_NOTICE = 5, // normal but significant condition
113 LOG_INFO = 6, // informational
114 LOG_DEBUG = 7 // debug-level messages
118 public enum OpenFlags : int {
122 O_RDONLY = 0x00000000,
123 O_WRONLY = 0x00000001,
127 // Or-ed with zero or more of these
129 O_CREAT = 0x00000040,
131 O_NOCTTY = 0x00000100,
132 O_TRUNC = 0x00000200,
133 O_APPEND = 0x00000400,
134 O_NONBLOCK = 0x00000800,
138 // These are non-Posix. Using them will result in errors/exceptions on
139 // non-supported platforms.
141 // (For example, "C-wrapped" system calls -- calls with implementation in
142 // MonoPosixHelper -- will return -1 with errno=EINVAL. C#-wrapped system
143 // calls will generate an exception in UnixConvert, as the value can't be
144 // converted on the target platform.)
147 O_NOFOLLOW = 0x00020000,
148 O_DIRECTORY = 0x00010000,
149 O_DIRECT = 0x00004000,
150 O_ASYNC = 0x00002000,
151 O_LARGEFILE = 0x00008000
156 public enum FilePermissions : uint {
157 S_ISUID = 0x0800, // Set user ID on execution
158 S_ISGID = 0x0400, // Set gorup ID on execution
159 S_ISVTX = 0x0200, // Save swapped text after use (sticky).
160 S_IRUSR = 0x0100, // Read by owner
161 S_IWUSR = 0x0080, // Write by owner
162 S_IXUSR = 0x0040, // Execute by owner
163 S_IRGRP = 0x0020, // Read by group
164 S_IWGRP = 0x0010, // Write by group
165 S_IXGRP = 0x0008, // Execute by group
166 S_IROTH = 0x0004, // Read by other
167 S_IWOTH = 0x0002, // Write by other
168 S_IXOTH = 0x0001, // Execute by other
170 S_IRWXG = (S_IRGRP | S_IWGRP | S_IXGRP),
171 S_IRWXU = (S_IRUSR | S_IWUSR | S_IXUSR),
172 S_IRWXO = (S_IROTH | S_IWOTH | S_IXOTH),
173 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
174 ALLPERMS = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
175 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
178 // Why these are held in "mode_t" is beyond me...
179 S_IFMT = 0xF000, // Bits which determine file type
180 S_IFDIR = 0x4000, // Directory
181 S_IFCHR = 0x2000, // Character device
182 S_IFBLK = 0x6000, // Block device
183 S_IFREG = 0x8000, // Regular file
184 S_IFIFO = 0x1000, // FIFO
185 S_IFLNK = 0xA000, // Symbolic link
186 S_IFSOCK = 0xC000, // Socket
190 public enum FcntlCommand : int {
191 // Form /usr/include/bits/fcntl.h
192 F_DUPFD = 0, // Duplicate file descriptor.
193 F_GETFD = 1, // Get file descriptor flags.
194 F_SETFD = 2, // Set file descriptor flags.
195 F_GETFL = 3, // Get file status flags.
196 F_SETFL = 4, // Set file status flags.
197 F_GETLK = 12, // Get record locking info. [64]
198 F_SETLK = 13, // Set record locking info (non-blocking). [64]
199 F_SETLKW = 14, // Set record locking info (blocking). [64]
200 F_SETOWN = 8, // Set owner of socket (receiver of SIGIO).
201 F_GETOWN = 9, // Get owner of socket (receiver of SIGIO).
202 F_SETSIG = 10, // Set number of signal to be sent.
203 F_GETSIG = 11, // Get number of signal to be sent.
204 F_SETLEASE = 1024, // Set a lease.
205 F_GETLEASE = 1025, // Enquire what lease is active.
206 F_NOTIFY = 1026, // Required notifications on a directory
210 public enum LockType : short {
211 F_RDLCK = 0, // Read lock.
212 F_WRLCK = 1, // Write lock.
213 F_UNLCK = 2, // Remove lock.
217 public enum SeekFlags : short {
218 // values liberally copied from /usr/include/unistd.h
219 SEEK_SET = 0, // Seek from beginning of file.
220 SEEK_CUR = 1, // Seek from current position.
221 SEEK_END = 2, // Seek from end of file.
223 L_SET = SEEK_SET, // BSD alias for SEEK_SET
224 L_INCR = SEEK_CUR, // BSD alias for SEEK_CUR
225 L_XTND = SEEK_END, // BSD alias for SEEK_END
229 public enum DirectoryNotifyFlags : int {
230 // from /usr/include/bits/fcntl.h
231 DN_ACCESS = 0x00000001, // File accessed.
232 DN_MODIFY = 0x00000002, // File modified.
233 DN_CREATE = 0x00000004, // File created.
234 DN_DELETE = 0x00000008, // File removed.
235 DN_RENAME = 0x00000010, // File renamed.
236 DN_ATTRIB = 0x00000020, // File changed attributes.
237 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
241 public enum PosixFadviseAdvice : int {
242 POSIX_FADV_NORMAL = 0, // No further special treatment.
243 POSIX_FADV_RANDOM = 1, // Expect random page references.
244 POSIX_FADV_SEQUENTIAL = 2, // Expect sequential page references.
245 POSIX_FADV_WILLNEED = 3, // Will need these pages.
246 POSIX_FADV_DONTNEED = 4, // Don't need these pages.
247 POSIX_FADV_NOREUSE = 5, // Data will be accessed once.
251 public enum PosixMadviseAdvice : int {
252 POSIX_MADV_NORMAL = 0, // No further special treatment.
253 POSIX_MADV_RANDOM = 1, // Expect random page references.
254 POSIX_MADV_SEQUENTIAL = 2, // Expect sequential page references.
255 POSIX_MADV_WILLNEED = 3, // Will need these pages.
256 POSIX_MADV_DONTNEED = 4, // Don't need these pages.
260 public enum Signum : int {
261 SIGHUP = 1, // Hangup (POSIX).
262 SIGINT = 2, // Interrupt (ANSI).
263 SIGQUIT = 3, // Quit (POSIX).
264 SIGILL = 4, // Illegal instruction (ANSI).
265 SIGTRAP = 5, // Trace trap (POSIX).
266 SIGABRT = 6, // Abort (ANSI).
267 SIGIOT = 6, // IOT trap (4.2 BSD).
268 SIGBUS = 7, // BUS error (4.2 BSD).
269 SIGFPE = 8, // Floating-point exception (ANSI).
270 SIGKILL = 9, // Kill, unblockable (POSIX).
271 SIGUSR1 = 10, // User-defined signal 1 (POSIX).
272 SIGSEGV = 11, // Segmentation violation (ANSI).
273 SIGUSR2 = 12, // User-defined signal 2 (POSIX).
274 SIGPIPE = 13, // Broken pipe (POSIX).
275 SIGALRM = 14, // Alarm clock (POSIX).
276 SIGTERM = 15, // Termination (ANSI).
277 SIGSTKFLT = 16, // Stack fault.
278 SIGCLD = SIGCHLD, // Same as SIGCHLD (System V).
279 SIGCHLD = 17, // Child status has changed (POSIX).
280 SIGCONT = 18, // Continue (POSIX).
281 SIGSTOP = 19, // Stop, unblockable (POSIX).
282 SIGTSTP = 20, // Keyboard stop (POSIX).
283 SIGTTIN = 21, // Background read from tty (POSIX).
284 SIGTTOU = 22, // Background write to tty (POSIX).
285 SIGURG = 23, // Urgent condition on socket (4.2 BSD).
286 SIGXCPU = 24, // CPU limit exceeded (4.2 BSD).
287 SIGXFSZ = 25, // File size limit exceeded (4.2 BSD).
288 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
289 SIGPROF = 27, // Profiling alarm clock (4.2 BSD).
290 SIGWINCH = 28, // Window size change (4.3 BSD, Sun).
291 SIGPOLL = SIGIO, // Pollable event occurred (System V).
292 SIGIO = 29, // I/O now possible (4.2 BSD).
293 SIGPWR = 30, // Power failure restart (System V).
294 SIGSYS = 31, // Bad system call.
299 public enum WaitOptions : int {
300 WNOHANG = 1, // Don't block waiting
301 WUNTRACED = 2, // Report status of stopped children
305 public enum AccessMode : int {
313 public enum PathConf : int {
320 _PC_CHOWN_RESTRICTED,
328 _PC_REC_INCR_XFER_SIZE,
329 _PC_REC_MAX_XFER_SIZE,
330 _PC_REC_MIN_XFER_SIZE,
338 public enum SysConf : int {
348 _SC_REALTIME_SIGNALS,
349 _SC_PRIORITY_SCHEDULING,
358 _SC_MEMORY_PROTECTION,
361 _SC_SHARED_MEMORY_OBJECTS,
364 _SC_AIO_PRIO_DELTA_MAX,
375 /* Values for the argument to `sysconf'
376 corresponding to _POSIX2_* symbols. */
381 _SC_COLL_WEIGHTS_MAX,
386 _SC_CHARCLASS_NAME_MAX,
402 _SC_IOV_MAX = _SC_UIO_MAXIOV,
403 _SC_PII_INTERNET_STREAM,
404 _SC_PII_INTERNET_DGRAM,
409 /* Values according to POSIX 1003.1c (POSIX threads). */
411 _SC_THREAD_SAFE_FUNCTIONS,
412 _SC_GETGR_R_SIZE_MAX,
413 _SC_GETPW_R_SIZE_MAX,
416 _SC_THREAD_DESTRUCTOR_ITERATIONS,
418 _SC_THREAD_STACK_MIN,
419 _SC_THREAD_THREADS_MAX,
420 _SC_THREAD_ATTR_STACKADDR,
421 _SC_THREAD_ATTR_STACKSIZE,
422 _SC_THREAD_PRIORITY_SCHEDULING,
423 _SC_THREAD_PRIO_INHERIT,
424 _SC_THREAD_PRIO_PROTECT,
425 _SC_THREAD_PROCESS_SHARED,
426 _SC_NPROCESSORS_CONF,
427 _SC_NPROCESSORS_ONLN,
433 _SC_XOPEN_XCU_VERSION,
468 _SC_XBS5_ILP32_OFF32,
469 _SC_XBS5_ILP32_OFFBIG,
471 _SC_XBS5_LPBIG_OFFBIG,
474 _SC_XOPEN_REALTIME_THREADS,
479 _SC_C_LANG_SUPPORT_R,
485 _SC_DEVICE_SPECIFIC_R,
496 _SC_READER_WRITER_LOCKS,
504 _SC_THREAD_SPORADIC_SERVER,
506 _SC_SYSTEM_DATABASE_R,
508 _SC_TYPED_MEMORY_OBJECTS,
512 _SC_2_PBS_ACCOUNTING,
518 _SC_2_PBS_CHECKPOINT,
525 _SC_TRACE_EVENT_FILTER,
528 _SC_LEVEL1_ICACHE_SIZE,
529 _SC_LEVEL1_ICACHE_ASSOC,
530 _SC_LEVEL1_ICACHE_LINESIZE,
531 _SC_LEVEL1_DCACHE_SIZE,
532 _SC_LEVEL1_DCACHE_ASSOC,
533 _SC_LEVEL1_DCACHE_LINESIZE,
534 _SC_LEVEL2_CACHE_SIZE,
535 _SC_LEVEL2_CACHE_ASSOC,
536 _SC_LEVEL2_CACHE_LINESIZE,
537 _SC_LEVEL3_CACHE_SIZE,
538 _SC_LEVEL3_CACHE_ASSOC,
539 _SC_LEVEL3_CACHE_LINESIZE,
540 _SC_LEVEL4_CACHE_SIZE,
541 _SC_LEVEL4_CACHE_ASSOC,
542 _SC_LEVEL4_CACHE_LINESIZE
546 public enum ConfStr : int {
547 _CS_PATH, /* The default search path. */
548 _CS_V6_WIDTH_RESTRICTED_ENVS,
549 _CS_GNU_LIBC_VERSION,
550 _CS_GNU_LIBPTHREAD_VERSION,
551 _CS_LFS_CFLAGS = 1000,
559 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
560 _CS_XBS5_ILP32_OFF32_LDFLAGS,
561 _CS_XBS5_ILP32_OFF32_LIBS,
562 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
563 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
564 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
565 _CS_XBS5_ILP32_OFFBIG_LIBS,
566 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
567 _CS_XBS5_LP64_OFF64_CFLAGS,
568 _CS_XBS5_LP64_OFF64_LDFLAGS,
569 _CS_XBS5_LP64_OFF64_LIBS,
570 _CS_XBS5_LP64_OFF64_LINTFLAGS,
571 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
572 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
573 _CS_XBS5_LPBIG_OFFBIG_LIBS,
574 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
575 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
576 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
577 _CS_POSIX_V6_ILP32_OFF32_LIBS,
578 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
579 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
580 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
581 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
582 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
583 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
584 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
585 _CS_POSIX_V6_LP64_OFF64_LIBS,
586 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
587 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
588 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
589 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
590 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
594 public enum LockFlags : int {
595 F_ULOCK = 0, // Unlock a previously locked region.
596 F_LOCK = 1, // Lock a region for exclusive use.
597 F_TLOCK = 2, // Test and lock a region for exclusive use.
598 F_TEST = 3, // Test a region for other process locks.
602 public enum PollEvents : short {
603 POLLIN = 0x0001, // There is data to read
604 POLLPRI = 0x0002, // There is urgent data to read
605 POLLOUT = 0x0004, // Writing now will not block
606 POLLERR = 0x0008, // Error condition
607 POLLHUP = 0x0010, // Hung up
608 POLLNVAL = 0x0020, // Invalid request; fd not open
609 // XPG4.2 definitions (via _XOPEN_SOURCE)
610 POLLRDNORM = 0x0040, // Normal data bay be read
611 POLLRDBAND = 0x0080, // Priority data may be read
612 POLLWRNORM = 0x0100, // Writing now will not block
613 POLLWRBAND = 0x0200, // Priority data may be written
617 public enum XattrFlags : int {
627 public struct Flock {
628 public LockType l_type; // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
629 public SeekFlags l_whence; // How to interpret l_start
630 public /* off_t */ long l_start; // Starting offset for lock
631 public /* off_t */ long l_len; // Number of bytes to lock
632 public /* pid_t */ int l_pid; // PID of process blocking our lock (F_GETLK only)
635 [StructLayout(LayoutKind.Sequential)]
636 public struct Pollfd {
638 public PollEvents events;
639 public PollEvents revents;
643 public /* dev_t */ ulong st_dev; // device
644 public /* ino_t */ ulong st_ino; // inode
645 public FilePermissions st_mode; // protection
646 private uint _padding_; // padding for structure alignment
647 public /* nlink_t */ ulong st_nlink; // number of hard links
648 public /* uid_t */ uint st_uid; // user ID of owner
649 public /* gid_t */ uint st_gid; // group ID of owner
650 public /* dev_t */ ulong st_rdev; // device type (if inode device)
651 public /* off_t */ long st_size; // total size, in bytes
652 public /* blksize_t */ long st_blksize; // blocksize for filesystem I/O
653 public /* blkcnt_t */ long st_blocks; // number of blocks allocated
654 public /* time_t */ long st_atime; // time of last access
655 public /* time_t */ long st_mtime; // time of last modification
656 public /* time_t */ long st_ctime; // time of last status change
659 public struct Statvfs {
660 public ulong f_bsize; // file system block size
661 public ulong f_frsize; // fragment size
662 public /* fsblkcnt_t */ ulong f_blocks; // size of fs in f_frsize units
663 public /* fsblkcnt_t */ ulong f_bfree; // # free blocks
664 public /* fsblkcnt_t */ ulong f_bavail; // # free blocks for non-root
665 public /* fsfilcnt_t */ ulong f_files; // # inodes
666 public /* fsfilcnt_t */ ulong f_ffree; // # free inodes
667 public /* fsfilcnt_t */ ulong f_favail; // # free inodes for non-root
668 public ulong f_fsid; // file system id
669 public ulong f_flag; // mount flags
670 public ulong f_namemax; // maximum filename length
673 public struct Timeval {
674 public /* time_t */ long tv_sec; // seconds
675 public /* suseconds_t */ long tv_usec; // microseconds
678 public struct Timezone {
679 public int tz_minuteswest; // minutes W of Greenwich
680 private int tz_dsttime; // type of dst correction (OBSOLETE)
683 public struct Utimbuf {
684 public /* time_t */ long actime; // access time
685 public /* time_t */ long modtime; // modification time
692 public sealed class Dirent
694 public /* ino_t */ ulong d_ino;
695 public /* off_t */ long d_off;
696 public ushort d_reclen;
698 public string d_name;
700 public override int GetHashCode ()
702 return d_ino.GetHashCode () ^ d_off.GetHashCode () ^
703 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
704 d_name.GetHashCode ();
707 public override bool Equals (object obj)
709 if (obj == null || GetType() != obj.GetType())
711 Dirent d = (Dirent) obj;
712 return d.d_ino == d_ino && d.d_off == d_off &&
713 d.d_reclen == d_reclen && d.d_type == d_type &&
717 public override string ToString ()
722 public static bool operator== (Dirent lhs, Dirent rhs)
724 return Object.Equals (lhs, rhs);
727 public static bool operator!= (Dirent lhs, Dirent rhs)
729 return !Object.Equals (lhs, rhs);
733 public sealed class Fstab
735 public string fs_spec;
736 public string fs_file;
737 public string fs_vfstype;
738 public string fs_mntops;
739 public string fs_type;
741 public int fs_passno;
743 public override int GetHashCode ()
745 return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
746 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
747 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
750 public override bool Equals (object obj)
752 if (obj == null || GetType() != obj.GetType())
754 Fstab f = (Fstab) obj;
755 return f.fs_spec == fs_spec && f.fs_file == fs_file &&
756 f.fs_vfstype == fs_vfstype && f.fs_mntops == fs_mntops &&
757 f.fs_type == fs_type && f.fs_freq == fs_freq &&
758 f.fs_passno == fs_passno;
761 public override string ToString ()
766 public static bool operator== (Fstab lhs, Fstab rhs)
768 return Object.Equals (lhs, rhs);
771 public static bool operator!= (Fstab lhs, Fstab rhs)
773 return !Object.Equals (lhs, rhs);
777 public sealed class Group
779 public string gr_name;
780 public string gr_passwd;
781 public /* gid_t */ uint gr_gid;
782 public string[] gr_mem;
784 public override int GetHashCode ()
787 for (int i = 0; i < gr_mem.Length; ++i)
788 memhc ^= gr_mem[i].GetHashCode ();
790 return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^
791 gr_gid.GetHashCode () ^ memhc;
794 public override bool Equals (object obj)
796 if (obj == null || GetType() != obj.GetType())
798 Group g = (Group) obj;
799 if (g.gr_gid != gr_gid)
801 if (g.gr_gid == gr_gid && g.gr_name == gr_name &&
802 g.gr_passwd == gr_passwd) {
803 if (g.gr_mem == gr_mem)
805 if (g.gr_mem == null || gr_mem == null)
807 if (g.gr_mem.Length != gr_mem.Length)
809 for (int i = 0; i < gr_mem.Length; ++i)
810 if (gr_mem[i] != g.gr_mem[i])
817 // Generate string in /etc/group format
818 public override string ToString ()
820 StringBuilder sb = new StringBuilder ();
821 sb.AppendFormat ("{0}:{1}:{2}:", gr_name, gr_passwd, gr_gid);
822 GetMembers (sb, gr_mem);
823 return sb.ToString ();
826 private static void GetMembers (StringBuilder sb, string[] members)
828 if (members.Length > 0)
829 sb.Append (members[0]);
830 for (int i = 1; i < members.Length; ++i) {
832 sb.Append (members[i]);
836 public static bool operator== (Group lhs, Group rhs)
838 return Object.Equals (lhs, rhs);
841 public static bool operator!= (Group lhs, Group rhs)
843 return !Object.Equals (lhs, rhs);
847 public sealed class Passwd
849 public string pw_name;
850 public string pw_passwd;
851 public /* uid_t */ uint pw_uid;
852 public /* gid_t */ uint pw_gid;
853 public string pw_gecos;
854 public string pw_dir;
855 public string pw_shell;
857 public override int GetHashCode ()
859 return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^
860 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
861 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
862 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
865 public override bool Equals (object obj)
867 if (obj == null || GetType() != obj.GetType())
869 Passwd p = (Passwd) obj;
870 return p.pw_uid == pw_uid && p.pw_gid == pw_gid && p.pw_name == pw_name &&
871 p.pw_passwd == pw_passwd && p.pw_gecos == pw_gecos &&
872 p.pw_dir == pw_dir && p.pw_shell == pw_shell;
875 // Generate string in /etc/passwd format
876 public override string ToString ()
878 return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
879 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
882 public static bool operator== (Passwd lhs, Passwd rhs)
884 return Object.Equals (lhs, rhs);
887 public static bool operator!= (Passwd lhs, Passwd rhs)
889 return !Object.Equals (lhs, rhs);
894 // Convention: Functions *not* part of the standard C library AND part of
895 // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
897 // For example, the man page should be similar to:
899 // CONFORMING TO (or CONFORMS TO)
900 // XPG2, SUSv2, POSIX, etc.
902 // BSD- and GNU-specific exports can also be placed here.
904 // Non-POSIX/XPG/etc. functions can also be placed here if:
905 // (a) They'd be likely to be covered in a Steven's-like book
906 // (b) The functions would be present in libc.so (or equivalent).
908 // If a function has its own library, that's a STRONG indicator that the
909 // function should get a different binding, probably in its own assembly,
910 // so that package management can work sanely. (That is, we'd like to avoid
911 // scenarios where FooLib.dll is installed, but it requires libFooLib.so to
912 // run, and libFooLib.so doesn't exist. That would be confusing.)
914 // The only methods in here should be:
915 // (1) low-level functions
916 // (2) "Trivial" function overloads. For example, if the parameters to a
917 // function are related (e.g. getgroups(2))
918 // (3) The return type SHOULD NOT be changed. If you want to provide a
919 // convenience function with a nicer return type, place it into one of
920 // the Unix* wrapper classes, and give it a .NET-styled name.
921 // (4) Exceptions SHOULD NOT be thrown. EXCEPTIONS:
922 // - If you're wrapping *broken* methods which make assumptions about
923 // input data, such as that an argument refers to N bytes of data.
924 // This is currently limited to cuserid(3) and encrypt(3).
925 // - If you call functions which themselves generate exceptions.
926 // This is the case for using UnixConvert, which will throw an
927 // exception if an invalid/unsupported value is used.
929 public sealed class Syscall : Stdlib
931 new internal const string LIBC = "libc";
932 private const string CRYPT = "crypt";
934 private Syscall () {}
940 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3),
941 // aio_return(3), aio_suspend(3), aio_write(3)
943 // Then update UnixStream.BeginRead to use the aio* functions.
946 #region <attr/xattr.h> Declarations
948 // <attr/xattr.h> -- COMPLETE
952 // int setxattr (const char *path, const char *name,
953 // const void *value, size_t size, int flags);
954 [DllImport (MPH, SetLastError=true,
955 EntryPoint="Mono_Posix_Syscall_setxattr")]
956 public static extern int setxattr (string path, string name, byte[] value, ulong size, XattrFlags flags);
958 public static int setxattr (string path, string name, byte [] value, XattrFlags flags)
960 return setxattr (path, name, value, (ulong) value.Length, flags);
964 // int lsetxattr (const char *path, const char *name,
965 // const void *value, size_t size, int flags);
966 [DllImport (MPH, SetLastError=true,
967 EntryPoint="Mono_Posix_Syscall_lsetxattr")]
968 public static extern int lsetxattr (string path, string name, byte[] value, ulong size, XattrFlags flags);
970 public static int lsetxattr (string path, string name, byte [] value, XattrFlags flags)
972 return lsetxattr (path, name, value, (ulong) value.Length, flags);
976 // int fsetxattr (int fd, const char *name,
977 // const void *value, size_t size, int flags);
978 [DllImport (MPH, SetLastError=true,
979 EntryPoint="Mono_Posix_Syscall_fsetxattr")]
980 public static extern int fsetxattr (int fd, string name, byte[] value, ulong size, XattrFlags flags);
982 public static int fsetxattr (int fd, string name, byte [] value, XattrFlags flags)
984 return fsetxattr (fd, name, value, (ulong) value.Length, flags);
988 // ssize_t getxattr (const char *path, const char *name,
989 // void *value, size_t size);
990 [DllImport (MPH, SetLastError=true,
991 EntryPoint="Mono_Posix_Syscall_getxattr")]
992 public static extern long getxattr (string path, string name, byte[] value, ulong size);
994 public static long getxattr (string path, string name, byte [] value)
996 return getxattr (path, name, value, (ulong) value.Length);
999 public static long getxattr (string path, string name, out byte [] value)
1002 long size = getxattr (path, name, value, 0);
1006 value = new byte [size];
1007 return getxattr (path, name, value, (ulong) size);
1011 // ssize_t lgetxattr (const char *path, const char *name,
1012 // void *value, size_t size);
1013 [DllImport (MPH, SetLastError=true,
1014 EntryPoint="Mono_Posix_Syscall_lgetxattr")]
1015 public static extern long lgetxattr (string path, string name, byte[] value, ulong size);
1017 public static long lgetxattr (string path, string name, byte [] value)
1019 return lgetxattr (path, name, value, (ulong) value.Length);
1022 public static long lgetxattr (string path, string name, out byte [] value)
1025 long size = lgetxattr (path, name, value, 0);
1029 value = new byte [size];
1030 return lgetxattr (path, name, value, (ulong) size);
1034 // ssize_t fgetxattr (int fd, const char *name, void *value, size_t size);
1035 [DllImport (MPH, SetLastError=true,
1036 EntryPoint="Mono_Posix_Syscall_fgetxattr")]
1037 public static extern long fgetxattr (int fd, string name, byte[] value, ulong size);
1039 public static long fgetxattr (int fd, string name, byte [] value)
1041 return fgetxattr (fd, name, value, (ulong) value.Length);
1044 public static long fgetxattr (int fd, string name, out byte [] value)
1047 long size = fgetxattr (fd, name, value, 0);
1051 value = new byte [size];
1052 return fgetxattr (fd, name, value, (ulong) size);
1056 // ssize_t listxattr (const char *path, char *list, size_t size);
1057 [DllImport (MPH, SetLastError=true,
1058 EntryPoint="Mono_Posix_Syscall_listxattr")]
1059 public static extern long listxattr (string path, byte[] list, ulong size);
1061 // Slight modification: returns 0 on success, negative on error
1062 public static long listxattr (string path, Encoding encoding, out string [] values)
1065 long size = listxattr (path, null, 0);
1067 values = new string [0];
1071 byte[] list = new byte [size];
1072 long ret = listxattr (path, list, (ulong) size);
1076 string [] output = encoding.GetString (list).Split((char) 0);
1077 values = new string [output.Length - 1];
1078 Array.Copy (output, 0, values, 0, output.Length - 1);
1083 // ssize_t llistxattr (const char *path, char *list, size_t size);
1084 [DllImport (MPH, SetLastError=true,
1085 EntryPoint="Mono_Posix_Syscall_llistxattr")]
1086 public static extern long llistxattr (string path, byte[] list, ulong size);
1088 // Slight modification: returns 0 on success, negative on error
1089 public static long llistxattr (string path, Encoding encoding, out string [] values)
1092 long size = llistxattr (path, null, 0);
1094 values = new string [0];
1098 byte[] list = new byte [size];
1099 long ret = llistxattr (path, list, (ulong) size);
1103 string [] output = encoding.GetString (list).Split((char) 0);
1104 values = new string [output.Length - 1];
1105 Array.Copy (output, 0, values, 0, output.Length - 1);
1110 // ssize_t flistxattr (int fd, char *list, size_t size);
1111 [DllImport (MPH, SetLastError=true,
1112 EntryPoint="Mono_Posix_Syscall_flistxattr")]
1113 public static extern long flistxattr (int fd, byte[] list, ulong size);
1115 // Slight modification: returns 0 on success, negative on error
1116 public static long flistxattr (int fd, Encoding encoding, out string [] values)
1119 long size = flistxattr (fd, null, 0);
1121 values = new string [0];
1125 byte[] list = new byte [size];
1126 long ret = flistxattr (fd, list, (ulong) size);
1130 string [] output = encoding.GetString (list).Split((char) 0);
1131 values = new string [output.Length - 1];
1132 Array.Copy (output, 0, values, 0, output.Length - 1);
1136 [DllImport (LIBC, SetLastError=true)]
1137 public static extern int removexattr (string path, string name);
1139 [DllImport (LIBC, SetLastError=true)]
1140 public static extern int lremovexattr (string path, string name);
1142 [DllImport (LIBC, SetLastError=true)]
1143 public static extern int fremovexattr (int fd, string name);
1146 #region <dirent.h> Declarations
1150 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1152 [DllImport (LIBC, SetLastError=true)]
1153 public static extern IntPtr opendir (string name);
1155 [DllImport (LIBC, SetLastError=true)]
1156 public static extern int closedir (IntPtr dir);
1159 // void seekdir (DIR *dir, off_t offset);
1160 // Slight modification. Returns -1 on error, 0 on success.
1161 [DllImport (MPH, SetLastError=true,
1162 EntryPoint="Mono_Posix_Syscall_seekdir")]
1163 public static extern int seekdir (IntPtr dir, long offset);
1166 // off_t telldir(DIR *dir);
1167 [DllImport (MPH, SetLastError=true,
1168 EntryPoint="Mono_Posix_Syscall_telldir")]
1169 public static extern long telldir (IntPtr dir);
1171 [DllImport (LIBC, SetLastError=true)]
1172 public static extern void rewinddir (IntPtr dir);
1174 private struct _Dirent {
1175 public /* ino_t */ ulong d_ino;
1176 public /* off_t */ long d_off;
1177 public ushort d_reclen;
1179 public IntPtr d_name;
1182 private static void CopyDirent (Dirent to, ref _Dirent from)
1185 to.d_ino = from.d_ino;
1186 to.d_off = from.d_off;
1187 to.d_reclen = from.d_reclen;
1188 to.d_type = from.d_type;
1189 to.d_name = UnixMarshal.PtrToString (from.d_name);
1192 Stdlib.free (from.d_name);
1193 from.d_name = IntPtr.Zero;
1197 [DllImport (MPH, SetLastError=true,
1198 EntryPoint="Mono_Posix_Syscall_readdir")]
1199 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
1201 public static Dirent readdir (IntPtr dir)
1204 int r = sys_readdir (dir, out dentry);
1207 Dirent d = new Dirent ();
1208 CopyDirent (d, ref dentry);
1212 [DllImport (MPH, SetLastError=true,
1213 EntryPoint="Mono_Posix_Syscall_readdir_r")]
1214 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
1216 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
1222 entry.d_name = null;
1225 int r = sys_readdir_r (dirp, out _d, out result);
1227 if (r == 0 && result != IntPtr.Zero) {
1228 CopyDirent (entry, ref _d);
1234 [DllImport (LIBC, SetLastError=true)]
1235 public static extern int dirfd (IntPtr dir);
1238 #region <fcntl.h> Declarations
1240 // <fcntl.h> -- COMPLETE
1243 [DllImport (MPH, SetLastError=true,
1244 EntryPoint="Mono_Posix_Syscall_fcntl")]
1245 public static extern int fcntl (int fd, FcntlCommand cmd);
1247 [DllImport (MPH, SetLastError=true,
1248 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
1249 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
1251 [DllImport (MPH, SetLastError=true,
1252 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
1253 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
1255 [DllImport (MPH, SetLastError=true,
1256 EntryPoint="Mono_Posix_Syscall_open")]
1257 public static extern int open (string pathname, OpenFlags flags);
1260 // int open(const char *pathname, int flags, mode_t mode);
1261 [DllImport (MPH, SetLastError=true,
1262 EntryPoint="Mono_Posix_Syscall_open_mode")]
1263 public static extern int open (string pathname, OpenFlags flags, FilePermissions mode);
1266 // int creat(const char *pathname, mode_t mode);
1267 [DllImport (MPH, SetLastError=true,
1268 EntryPoint="Mono_Posix_Syscall_creat")]
1269 public static extern int creat (string pathname, FilePermissions mode);
1272 // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
1273 [DllImport (MPH, SetLastError=true,
1274 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
1275 public static extern int posix_fadvise (int fd, long offset,
1276 long len, PosixFadviseAdvice advice);
1278 // posix_fallocate(P)
1279 // int posix_fallocate(int fd, off_t offset, size_t len);
1280 [DllImport (MPH, SetLastError=true,
1281 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
1282 public static extern int posix_fallocate (int fd, long offset, long len);
1285 #region <fstab.h> Declarations
1287 // <fstab.h> -- COMPLETE
1289 private struct _Fstab {
1290 public IntPtr fs_spec;
1291 public IntPtr fs_file;
1292 public IntPtr fs_vfstype;
1293 public IntPtr fs_mntops;
1294 public IntPtr fs_type;
1296 public int fs_passno;
1297 public IntPtr _fs_buf_;
1300 private static void CopyFstab (Fstab to, ref _Fstab from)
1303 to.fs_spec = UnixMarshal.PtrToString (from.fs_spec);
1304 to.fs_file = UnixMarshal.PtrToString (from.fs_file);
1305 to.fs_vfstype = UnixMarshal.PtrToString (from.fs_vfstype);
1306 to.fs_mntops = UnixMarshal.PtrToString (from.fs_mntops);
1307 to.fs_type = UnixMarshal.PtrToString (from.fs_type);
1308 to.fs_freq = from.fs_freq;
1309 to.fs_passno = from.fs_passno;
1312 Stdlib.free (from._fs_buf_);
1313 from._fs_buf_ = IntPtr.Zero;
1317 [DllImport (MPH, SetLastError=true,
1318 EntryPoint="Mono_Posix_Syscall_endfsent")]
1319 public static extern void endfsent ();
1321 [DllImport (MPH, SetLastError=true,
1322 EntryPoint="Mono_Posix_Syscall_getfsent")]
1323 private static extern int sys_getfsent (out _Fstab fs);
1325 public static Fstab getfsent ()
1328 int r = sys_getfsent (out fsbuf);
1331 Fstab fs = new Fstab ();
1332 CopyFstab (fs, ref fsbuf);
1336 [DllImport (MPH, SetLastError=true,
1337 EntryPoint="Mono_Posix_Syscall_getfsfile")]
1338 private static extern int sys_getfsfile (string mount_point, out _Fstab fs);
1340 public static Fstab getfsfile (string mount_point)
1343 int r = sys_getfsfile (mount_point, out fsbuf);
1346 Fstab fs = new Fstab ();
1347 CopyFstab (fs, ref fsbuf);
1351 [DllImport (MPH, SetLastError=true,
1352 EntryPoint="Mono_Posix_Syscall_getfsspec")]
1353 private static extern int sys_getfsspec (string special_file, out _Fstab fs);
1355 public static Fstab getfsspec (string special_file)
1358 int r = sys_getfsspec (special_file, out fsbuf);
1361 Fstab fs = new Fstab ();
1362 CopyFstab (fs, ref fsbuf);
1366 [DllImport (MPH, SetLastError=true,
1367 EntryPoint="Mono_Posix_Syscall_setfsent")]
1368 public static extern int setfsent ();
1372 #region <grp.h> Declarations
1376 // TODO: putgrent(3), fgetgrent_r(), getgrouplist(2), initgroups(3)
1379 // int setgroups (size_t size, const gid_t *list);
1380 [DllImport (MPH, SetLastError=true,
1381 EntryPoint="Mono_Posix_Syscall_setgroups")]
1382 public static extern int setgroups (ulong size, uint[] list);
1384 public static int setgroups (uint [] list)
1386 return setgroups ((ulong) list.Length, list);
1389 private struct _Group
1391 public IntPtr gr_name;
1392 public IntPtr gr_passwd;
1393 public /* gid_t */ uint gr_gid;
1394 public int _gr_nmem_;
1395 public IntPtr gr_mem;
1396 public IntPtr _gr_buf_;
1399 private static void CopyGroup (Group to, ref _Group from)
1402 to.gr_gid = from.gr_gid;
1403 to.gr_name = UnixMarshal.PtrToString (from.gr_name);
1404 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
1405 to.gr_mem = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
1408 Stdlib.free (from.gr_mem);
1409 Stdlib.free (from._gr_buf_);
1410 from.gr_mem = IntPtr.Zero;
1411 from._gr_buf_ = IntPtr.Zero;
1415 [DllImport (MPH, SetLastError=true,
1416 EntryPoint="Mono_Posix_Syscall_getgrnam")]
1417 private static extern int sys_getgrnam (string name, out _Group group);
1419 public static Group getgrnam (string name)
1422 int r = sys_getgrnam (name, out group);
1425 Group gr = new Group ();
1426 CopyGroup (gr, ref group);
1431 // struct group *getgrgid(gid_t gid);
1432 [DllImport (MPH, SetLastError=true,
1433 EntryPoint="Mono_Posix_Syscall_getgrgid")]
1434 private static extern int sys_getgrgid (uint uid, out _Group group);
1436 public static Group getgrgid (uint uid)
1439 int r = sys_getgrgid (uid, out group);
1442 Group gr = new Group ();
1443 CopyGroup (gr, ref group);
1447 [DllImport (MPH, SetLastError=true,
1448 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
1449 private static extern int sys_getgrnam_r (string name, out _Group grbuf, out IntPtr grbufp);
1451 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
1456 int r = sys_getgrnam_r (name, out group, out _grbufp);
1457 if (r == 0 && _grbufp != IntPtr.Zero) {
1458 CopyGroup (grbuf, ref group);
1465 // int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
1466 // size_t buflen, struct group **gbufp);
1467 [DllImport (MPH, SetLastError=true,
1468 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
1469 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
1471 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
1476 int r = sys_getgrgid_r (uid, out group, out _grbufp);
1477 if (r == 0 && _grbufp != IntPtr.Zero) {
1478 CopyGroup (grbuf, ref group);
1484 [DllImport (MPH, SetLastError=true,
1485 EntryPoint="Mono_Posix_Syscall_getgrent")]
1486 private static extern int sys_getgrent (out _Group grbuf);
1488 public static Group getgrent ()
1491 int r = sys_getgrent (out group);
1494 Group gr = new Group();
1495 CopyGroup (gr, ref group);
1499 [DllImport (LIBC, SetLastError=true)]
1500 public static extern void setgrent ();
1502 [DllImport (LIBC, SetLastError=true)]
1503 public static extern void endgrent ();
1505 [DllImport (MPH, SetLastError=true,
1506 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
1507 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
1509 public static Group fgetgrent (IntPtr stream)
1512 int r = sys_fgetgrent (stream, out group);
1515 Group gr = new Group ();
1516 CopyGroup (gr, ref group);
1521 #region <pwd.h> Declarations
1525 // TODO: putpwent(3), fgetpwent_r()
1527 // SKIPPING: getpw(3): it's dangerous. Use getpwuid(3) instead.
1529 private struct _Passwd
1531 public IntPtr pw_name;
1532 public IntPtr pw_passwd;
1533 public /* uid_t */ uint pw_uid;
1534 public /* gid_t */ uint pw_gid;
1535 public IntPtr pw_gecos;
1536 public IntPtr pw_dir;
1537 public IntPtr pw_shell;
1538 public IntPtr _pw_buf_;
1541 private static void CopyPasswd (Passwd to, ref _Passwd from)
1544 to.pw_name = UnixMarshal.PtrToString (from.pw_name);
1545 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
1546 to.pw_uid = from.pw_uid;
1547 to.pw_gid = from.pw_gid;
1548 to.pw_gecos = UnixMarshal.PtrToString (from.pw_gecos);
1549 to.pw_dir = UnixMarshal.PtrToString (from.pw_dir);
1550 to.pw_shell = UnixMarshal.PtrToString (from.pw_shell);
1553 Stdlib.free (from._pw_buf_);
1554 from._pw_buf_ = IntPtr.Zero;
1558 [DllImport (MPH, SetLastError=true,
1559 EntryPoint="Mono_Posix_Syscall_getpwnam")]
1560 private static extern int sys_getpwnam (string name, out _Passwd passwd);
1562 public static Passwd getpwnam (string name)
1565 int r = sys_getpwnam (name, out passwd);
1568 Passwd pw = new Passwd ();
1569 CopyPasswd (pw, ref passwd);
1574 // struct passwd *getpwnuid(uid_t uid);
1575 [DllImport (MPH, SetLastError=true,
1576 EntryPoint="Mono_Posix_Syscall_getpwuid")]
1577 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
1579 public static Passwd getpwuid (uint uid)
1582 int r = sys_getpwuid (uid, out passwd);
1585 Passwd pw = new Passwd ();
1586 CopyPasswd (pw, ref passwd);
1590 [DllImport (MPH, SetLastError=true,
1591 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
1592 private static extern int sys_getpwnam_r (string name, out _Passwd pwbuf, out IntPtr pwbufp);
1594 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
1599 int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
1600 if (r == 0 && _pwbufp != IntPtr.Zero) {
1601 CopyPasswd (pwbuf, ref passwd);
1608 // int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
1609 // buflen, struct passwd **pwbufp);
1610 [DllImport (MPH, SetLastError=true,
1611 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
1612 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
1614 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
1619 int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
1620 if (r == 0 && _pwbufp != IntPtr.Zero) {
1621 CopyPasswd (pwbuf, ref passwd);
1627 [DllImport (MPH, SetLastError=true,
1628 EntryPoint="Mono_Posix_Syscall_getpwent")]
1629 private static extern int sys_getpwent (out _Passwd pwbuf);
1631 public static Passwd getpwent ()
1634 int r = sys_getpwent (out passwd);
1637 Passwd pw = new Passwd ();
1638 CopyPasswd (pw, ref passwd);
1642 [DllImport (LIBC, SetLastError=true)]
1643 public static extern void setpwent ();
1645 [DllImport (LIBC, SetLastError=true)]
1646 public static extern void endpwent ();
1648 [DllImport (MPH, SetLastError=true,
1649 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
1650 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
1652 public static Passwd fgetpwent (IntPtr stream)
1655 int r = sys_fgetpwent (stream, out passwd);
1658 Passwd pw = new Passwd ();
1659 CopyPasswd (pw, ref passwd);
1664 #region <signal.h> Declarations
1668 [DllImport (LIBC, SetLastError=true)]
1669 private static extern void psignal (int sig, string s);
1671 public static void psignal (Signum sig, string s)
1673 int signum = UnixConvert.FromSignum (sig);
1674 psignal (signum, s);
1678 // int kill(pid_t pid, int sig);
1679 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
1680 private static extern int sys_kill (int pid, int sig);
1682 public static int kill (int pid, Signum sig)
1684 int _sig = UnixConvert.FromSignum (sig);
1685 return sys_kill (pid, _sig);
1688 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
1689 private static extern IntPtr sys_strsignal (int sig);
1691 public static string strsignal (Signum sig)
1693 int s = UnixConvert.FromSignum (sig);
1694 IntPtr r = sys_strsignal (s);
1695 return UnixMarshal.PtrToString (r);
1698 // TODO: sigaction(2)
1699 // TODO: sigsuspend(2)
1700 // TODO: sigpending(2)
1704 #region <stdio.h> Declarations
1708 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
1709 private static extern int _L_ctermid ();
1711 public static readonly int L_ctermid = _L_ctermid ();
1713 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
1714 private static extern int _L_cuserid ();
1716 public static readonly int L_cuserid = _L_cuserid ();
1718 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
1719 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
1721 [Obsolete ("\"Nobody knows precisely what cuserid() does... " +
1722 "DO NOT USE cuserid().\n" +
1723 "`string' must hold L_cuserid characters. Use getlogin_r instead.")]
1724 public static string cuserid (StringBuilder @string)
1726 if (@string.Capacity < L_cuserid) {
1727 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
1729 IntPtr r = sys_cuserid (@string);
1730 return UnixMarshal.PtrToString (r);
1735 #region <stdlib.h> Declarations
1739 [DllImport (LIBC, SetLastError=true)]
1740 public static extern int ttyslot ();
1742 [DllImport (CRYPT, SetLastError=true)]
1743 public static extern void setkey (string key);
1747 #region <string.h> Declarations
1753 // int strerror_r(int errnum, char *buf, size_t n);
1754 [DllImport (MPH, SetLastError=true,
1755 EntryPoint="Mono_Posix_Syscall_strerror_r")]
1756 private static extern int sys_strerror_r (int errnum,
1757 [Out] StringBuilder buf, ulong n);
1759 public static int strerror_r (Error errnum, StringBuilder buf, ulong n)
1761 int e = UnixConvert.FromError (errnum);
1762 return sys_strerror_r (e, buf, n);
1765 public static int strerror_r (Error errnum, StringBuilder buf)
1767 return strerror_r (errnum, buf, (ulong) buf.Capacity);
1772 #region <sys/mman.h> Declarations
1778 // int posix_madvise(void *addr, size_t len, int advice);
1779 [DllImport (MPH, SetLastError=true,
1780 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
1781 public static extern int posix_madvise (IntPtr addr, ulong len,
1782 PosixMadviseAdvice advice);
1786 #region <sys/poll.h> Declarations
1788 // <sys/poll.h> -- COMPLETE
1791 private struct _pollfd {
1793 public short events;
1794 public short revents;
1797 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
1798 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
1800 public static int poll (Pollfd [] fds, uint nfds, int timeout)
1802 if (fds.Length < nfds)
1803 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
1805 _pollfd[] send = new _pollfd[nfds];
1807 for (int i = 0; i < send.Length; i++) {
1808 send [i].fd = fds [i].fd;
1809 send [i].events = UnixConvert.FromPollEvents (fds [i].events);
1812 int r = sys_poll (send, nfds, timeout);
1814 for (int i = 0; i < send.Length; i++) {
1815 fds [i].revents = UnixConvert.ToPollEvents (send [i].revents);
1821 public static int poll (Pollfd [] fds, int timeout)
1823 return poll (fds, (uint) fds.Length, timeout);
1836 // TODO: setrlimit(2)
1837 // TODO: getrlimit(2)
1838 // TODO: getrusage(2)
1842 #region <sys/sendfile.h> Declarations
1844 // <sys/sendfile.h> -- COMPLETE
1847 [DllImport (MPH, SetLastError=true,
1848 EntryPoint="Mono_Posix_Syscall_sendfile")]
1849 public static extern long sendfile (int out_fd, int in_fd,
1850 ref long offset, ulong count);
1854 #region <sys/stat.h> Declarations
1856 // <sys/stat.h> -- COMPLETE
1858 [DllImport (MPH, SetLastError=true,
1859 EntryPoint="Mono_Posix_Syscall_stat")]
1860 public static extern int stat (string file_name, out Stat buf);
1862 [DllImport (MPH, SetLastError=true,
1863 EntryPoint="Mono_Posix_Syscall_fstat")]
1864 public static extern int fstat (int filedes, out Stat buf);
1866 [DllImport (MPH, SetLastError=true,
1867 EntryPoint="Mono_Posix_Syscall_lstat")]
1868 public static extern int lstat (string file_name, out Stat buf);
1871 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
1872 // All take FilePermissions
1875 // int chmod(const char *path, mode_t mode);
1876 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
1877 private static extern int sys_chmod (string path, uint mode);
1879 public static int chmod (string path, FilePermissions mode)
1881 uint _mode = UnixConvert.FromFilePermissions (mode);
1882 return sys_chmod (path, _mode);
1886 // int chmod(int filedes, mode_t mode);
1887 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
1888 private static extern int sys_fchmod (int filedes, uint mode);
1890 public static int fchmod (int filedes, FilePermissions mode)
1892 uint _mode = UnixConvert.FromFilePermissions (mode);
1893 return sys_fchmod (filedes, _mode);
1897 // mode_t umask(mode_t mask);
1898 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
1899 private static extern uint sys_umask (uint mask);
1901 public static FilePermissions umask (FilePermissions mask)
1903 uint _mask = UnixConvert.FromFilePermissions (mask);
1904 uint r = sys_umask (_mask);
1905 return UnixConvert.ToFilePermissions (r);
1909 // int mkdir(const char *pathname, mode_t mode);
1910 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
1911 private static extern int sys_mkdir (string oldpath, uint mode);
1913 public static int mkdir (string oldpath, FilePermissions mode)
1915 uint _mode = UnixConvert.FromFilePermissions (mode);
1916 return sys_mkdir (oldpath, _mode);
1920 // int mknod (const char *pathname, mode_t mode, dev_t dev);
1921 [DllImport (MPH, SetLastError=true,
1922 EntryPoint="Mono_Posix_Syscall_mknod")]
1923 public static extern int mknod (string pathname, FilePermissions mode, ulong dev);
1926 // int mkfifo(const char *pathname, mode_t mode);
1927 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
1928 private static extern int sys_mkfifo (string pathname, uint mode);
1930 public static int mkfifo (string pathname, FilePermissions mode)
1932 uint _mode = UnixConvert.FromFilePermissions (mode);
1933 return sys_mkfifo (pathname, _mode);
1938 #region <sys/stat.h> Declarations
1943 [DllImport (MPH, SetLastError=true,
1944 EntryPoint="Mono_Posix_Syscall_statvfs")]
1945 public static extern int statvfs (string path, out Statvfs buf);
1947 [DllImport (MPH, SetLastError=true,
1948 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
1949 public static extern int fstatvfs (int fd, out Statvfs buf);
1953 #region <sys/time.h> Declarations
1957 // TODO: adjtime(), getitimer(2), setitimer(2), lutimes(), futimes()
1959 [DllImport (MPH, SetLastError=true,
1960 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1961 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
1963 [DllImport (MPH, SetLastError=true,
1964 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1965 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
1967 public static int gettimeofday (out Timeval tv)
1969 return gettimeofday (out tv, IntPtr.Zero);
1972 [DllImport (MPH, SetLastError=true,
1973 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1974 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
1976 public static int gettimeofday (out Timezone tz)
1978 return gettimeofday (IntPtr.Zero, out tz);
1981 [DllImport (MPH, SetLastError=true,
1982 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1983 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
1985 [DllImport (MPH, SetLastError=true,
1986 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1987 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
1989 public static int settimeofday (ref Timeval tv)
1991 return settimeofday (ref tv, IntPtr.Zero);
1994 [DllImport (MPH, SetLastError=true,
1995 EntryPoint="Mono_Posix_Syscall_utimes")]
1996 public static extern int utimes (string filename, ref Timeval tvp);
2012 #region <sys/wait.h> Declarations
2018 // pid_t wait(int *status);
2019 [DllImport (LIBC, SetLastError=true)]
2020 public static extern int wait (out int status);
2023 // pid_t waitpid(pid_t pid, int *status, int options);
2024 [DllImport (LIBC, SetLastError=true)]
2025 private static extern int waitpid (int pid, out int status, int options);
2027 public static int waitpid (int pid, out int status, WaitOptions options)
2029 int _options = UnixConvert.FromWaitOptions (options);
2030 return waitpid (pid, out status, _options);
2033 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
2034 private static extern int _WIFEXITED (int status);
2036 public static bool WIFEXITED (int status)
2038 return _WIFEXITED (status) != 0;
2041 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
2042 public static extern int WEXITSTATUS (int status);
2044 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
2045 private static extern int _WIFSIGNALED (int status);
2047 public static bool WIFSIGNALED (int status)
2049 return _WIFSIGNALED (status) != 0;
2052 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
2053 private static extern int _WTERMSIG (int status);
2055 public static Signum WTERMSIG (int status)
2057 int r = _WTERMSIG (status);
2058 return UnixConvert.ToSignum (r);
2061 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
2062 private static extern int _WIFSTOPPED (int status);
2064 public static bool WIFSTOPPED (int status)
2066 return _WIFSTOPPED (status) != 0;
2069 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
2070 private static extern int _WSTOPSIG (int status);
2072 public static Signum WSTOPSIG (int status)
2074 int r = _WSTOPSIG (status);
2075 return UnixConvert.ToSignum (r);
2084 #region <syslog.h> Declarations
2089 [DllImport (LIBC, EntryPoint="openlog")]
2090 private static extern void sys_openlog (IntPtr ident, int option, int facility);
2092 public static void openlog (IntPtr ident, SyslogOptions option,
2093 SyslogFacility defaultFacility)
2095 int _option = UnixConvert.FromSyslogOptions (option);
2096 int _facility = UnixConvert.FromSyslogFacility (defaultFacility);
2098 sys_openlog (ident, _option, _facility);
2101 [DllImport (LIBC, EntryPoint="syslog")]
2102 private static extern void sys_syslog (int priority, string message);
2104 public static void syslog (SyslogFacility facility, SyslogLevel level, string message)
2106 int _facility = UnixConvert.FromSyslogFacility (facility);
2107 int _level = UnixConvert.FromSyslogLevel (level);
2108 sys_syslog (_facility | _level, GetSyslogMessage (message));
2111 public static void syslog (SyslogLevel level, string message)
2113 int _level = UnixConvert.FromSyslogLevel (level);
2114 sys_syslog (_level, GetSyslogMessage (message));
2117 private static string GetSyslogMessage (string message)
2119 return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
2122 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2123 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
2124 public static void syslog (SyslogFacility facility, SyslogLevel level,
2125 string format, params object[] parameters)
2127 int _facility = UnixConvert.FromSyslogFacility (facility);
2128 int _level = UnixConvert.FromSyslogLevel (level);
2130 object[] _parameters = new object[checked(parameters.Length+2)];
2131 _parameters [0] = _facility | _level;
2132 _parameters [1] = format;
2133 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2134 XPrintfFunctions.syslog (_parameters);
2137 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2138 "Use syslog(SyslogLevel, string) instead.")]
2139 public static void syslog (SyslogLevel level, string format,
2140 params object[] parameters)
2142 int _level = UnixConvert.FromSyslogLevel (level);
2144 object[] _parameters = new object[checked(parameters.Length+2)];
2145 _parameters [0] = _level;
2146 _parameters [1] = format;
2147 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2148 XPrintfFunctions.syslog (_parameters);
2152 public static extern void closelog ();
2154 [DllImport (LIBC, EntryPoint="setlogmask")]
2155 private static extern int sys_setlogmask (int mask);
2157 public static int setlogmask (SyslogLevel mask)
2159 int _mask = UnixConvert.FromSyslogLevel (mask);
2160 return sys_setlogmask (_mask);
2165 #region <time.h> Declarations
2172 // int stime(time_t *t);
2173 [DllImport (MPH, SetLastError=true,
2174 EntryPoint="Mono_Posix_Syscall_stime")]
2175 public static extern int stime (ref long t);
2178 // time_t time(time_t *t);
2179 [DllImport (MPH, SetLastError=true,
2180 EntryPoint="Mono_Posix_Syscall_time")]
2181 public static extern long time (out long t);
2191 #region <unistd.h> Declarations
2195 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
2196 // other TODOs listed below.
2198 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
2199 private static extern int sys_access (string pathname, int mode);
2201 public static int access (string pathname, AccessMode mode)
2203 int _mode = UnixConvert.FromAccessMode (mode);
2204 return sys_access (pathname, _mode);
2208 // off_t lseek(int filedes, off_t offset, int whence);
2209 [DllImport (MPH, SetLastError=true,
2210 EntryPoint="Mono_Posix_Syscall_lseek")]
2211 public static extern long lseek (int fd, long offset, SeekFlags whence);
2213 [DllImport (LIBC, SetLastError=true)]
2214 public static extern int close (int fd);
2217 // ssize_t read(int fd, void *buf, size_t count);
2218 [DllImport (MPH, SetLastError=true,
2219 EntryPoint="Mono_Posix_Syscall_read")]
2220 public static extern long read (int fd, IntPtr buf, ulong count);
2222 public static unsafe long read (int fd, void *buf, ulong count)
2224 return read (fd, (IntPtr) buf, count);
2228 // ssize_t write(int fd, const void *buf, size_t count);
2229 [DllImport (MPH, SetLastError=true,
2230 EntryPoint="Mono_Posix_Syscall_write")]
2231 public static extern long write (int fd, IntPtr buf, ulong count);
2233 public static unsafe long write (int fd, void *buf, ulong count)
2235 return write (fd, (IntPtr) buf, count);
2239 // ssize_t pread(int fd, void *buf, size_t count, off_t offset);
2240 [DllImport (MPH, SetLastError=true,
2241 EntryPoint="Mono_Posix_Syscall_pread")]
2242 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
2244 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
2246 return pread (fd, (IntPtr) buf, count, offset);
2250 // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
2251 [DllImport (MPH, SetLastError=true,
2252 EntryPoint="Mono_Posix_Syscall_pwrite")]
2253 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
2255 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
2257 return pwrite (fd, (IntPtr) buf, count, offset);
2260 [DllImport (MPH, SetLastError=true,
2261 EntryPoint="Mono_Posix_Syscall_pipe")]
2262 public static extern int pipe (out int reading, out int writing);
2264 public static int pipe (int[] filedes)
2266 if (filedes == null || filedes.Length != 2) {
2270 int reading, writing;
2271 int r = pipe (out reading, out writing);
2272 filedes[0] = reading;
2273 filedes[1] = writing;
2277 [DllImport (LIBC, SetLastError=true)]
2278 public static extern uint alarm (uint seconds);
2280 [DllImport (LIBC, SetLastError=true)]
2281 public static extern uint sleep (uint seconds);
2283 [DllImport (LIBC, SetLastError=true)]
2284 public static extern uint ualarm (uint usecs, uint interval);
2286 [DllImport (LIBC, SetLastError=true)]
2287 public static extern int pause ();
2290 // int chown(const char *path, uid_t owner, gid_t group);
2291 [DllImport (LIBC, SetLastError=true)]
2292 public static extern int chown (string path, uint owner, uint group);
2295 // int fchown(int fd, uid_t owner, gid_t group);
2296 [DllImport (LIBC, SetLastError=true)]
2297 public static extern int fchown (int fd, uint owner, uint group);
2300 // int lchown(const char *path, uid_t owner, gid_t group);
2301 [DllImport (LIBC, SetLastError=true)]
2302 public static extern int lchown (string path, uint owner, uint group);
2304 [DllImport (LIBC, SetLastError=true)]
2305 public static extern int chdir (string path);
2307 [DllImport (LIBC, SetLastError=true)]
2308 public static extern int fchdir (int fd);
2311 // char *getcwd(char *buf, size_t size);
2312 [DllImport (MPH, SetLastError=true,
2313 EntryPoint="Mono_Posix_Syscall_getcwd")]
2314 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
2316 public static StringBuilder getcwd (StringBuilder buf)
2318 getcwd (buf, (ulong) buf.Capacity);
2322 // getwd(2) is deprecated; don't expose it.
2324 [DllImport (LIBC, SetLastError=true)]
2325 public static extern int dup (int fd);
2327 [DllImport (LIBC, SetLastError=true)]
2328 public static extern int dup2 (int fd, int fd2);
2330 // TODO: does Mono marshal arrays properly?
2331 [DllImport (LIBC, SetLastError=true)]
2332 public static extern int execve (string path, string[] argv, string[] envp);
2334 [DllImport (LIBC, SetLastError=true)]
2335 public static extern int fexecve (int fd, string[] argv, string[] envp);
2337 [DllImport (LIBC, SetLastError=true)]
2338 public static extern int execv (string path, string[] argv);
2340 // TODO: execle, execl, execlp
2341 [DllImport (LIBC, SetLastError=true)]
2342 public static extern int execvp (string path, string[] argv);
2344 [DllImport (LIBC, SetLastError=true)]
2345 public static extern int nice (int inc);
2347 [DllImport (LIBC, SetLastError=true)]
2348 public static extern int _exit (int status);
2350 [DllImport (MPH, SetLastError=true,
2351 EntryPoint="Mono_Posix_Syscall_fpathconf")]
2352 public static extern long fpathconf (int filedes, PathConf name);
2354 [DllImport (MPH, SetLastError=true,
2355 EntryPoint="Mono_Posix_Syscall_pathconf")]
2356 public static extern long pathconf (string path, PathConf name);
2358 [DllImport (MPH, SetLastError=true,
2359 EntryPoint="Mono_Posix_Syscall_sysconf")]
2360 public static extern long sysconf (SysConf name);
2363 // size_t confstr(int name, char *buf, size_t len);
2364 [DllImport (MPH, SetLastError=true,
2365 EntryPoint="Mono_Posix_Syscall_confstr")]
2366 public static extern ulong confstr (ConfStr name, [Out] StringBuilder buf, ulong len);
2369 // pid_t getpid(void);
2370 [DllImport (LIBC, SetLastError=true)]
2371 public static extern int getpid ();
2374 // pid_t getppid(void);
2375 [DllImport (LIBC, SetLastError=true)]
2376 public static extern int getppid ();
2379 // int setpgid(pid_t pid, pid_t pgid);
2380 [DllImport (LIBC, SetLastError=true)]
2381 public static extern int setpgid (int pid, int pgid);
2384 // pid_t getpgid(pid_t pid);
2385 [DllImport (LIBC, SetLastError=true)]
2386 public static extern int getpgid (int pid);
2388 [DllImport (LIBC, SetLastError=true)]
2389 public static extern int setpgrp ();
2392 // pid_t getpgrp(void);
2393 [DllImport (LIBC, SetLastError=true)]
2394 public static extern int getpgrp ();
2397 // pid_t setsid(void);
2398 [DllImport (LIBC, SetLastError=true)]
2399 public static extern int setsid ();
2402 // pid_t getsid(pid_t pid);
2403 [DllImport (LIBC, SetLastError=true)]
2404 public static extern int getsid (int pid);
2407 // uid_t getuid(void);
2408 [DllImport (LIBC, SetLastError=true)]
2409 public static extern uint getuid ();
2412 // uid_t geteuid(void);
2413 [DllImport (LIBC, SetLastError=true)]
2414 public static extern uint geteuid ();
2417 // gid_t getgid(void);
2418 [DllImport (LIBC, SetLastError=true)]
2419 public static extern uint getgid ();
2422 // gid_t getgid(void);
2423 [DllImport (LIBC, SetLastError=true)]
2424 public static extern uint getegid ();
2427 // int getgroups(int size, gid_t list[]);
2428 [DllImport (LIBC, SetLastError=true)]
2429 public static extern int getgroups (int size, uint[] list);
2431 public static int getgroups (uint[] list)
2433 return getgroups (list.Length, list);
2437 // int setuid(uid_t uid);
2438 [DllImport (LIBC, SetLastError=true)]
2439 public static extern int setuid (uint uid);
2442 // int setreuid(uid_t ruid, uid_t euid);
2443 [DllImport (LIBC, SetLastError=true)]
2444 public static extern int setreuid (uint ruid, uint euid);
2447 // int setregid(gid_t ruid, gid_t euid);
2448 [DllImport (LIBC, SetLastError=true)]
2449 public static extern int setregid (uint rgid, uint egid);
2452 // int seteuid(uid_t euid);
2453 [DllImport (LIBC, SetLastError=true)]
2454 public static extern int seteuid (uint euid);
2457 // int setegid(gid_t euid);
2458 [DllImport (LIBC, SetLastError=true)]
2459 public static extern int setegid (uint uid);
2462 // int setgid(gid_t gid);
2463 [DllImport (LIBC, SetLastError=true)]
2464 public static extern int setgid (uint gid);
2467 // int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
2468 [DllImport (LIBC, SetLastError=true)]
2469 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
2472 // int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
2473 [DllImport (LIBC, SetLastError=true)]
2474 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
2477 // int setresuid(uid_t ruid, uid_t euid, uid_t suid);
2478 [DllImport (LIBC, SetLastError=true)]
2479 public static extern int setresuid (uint ruid, uint euid, uint suid);
2482 // int setresgid(gid_t ruid, gid_t euid, gid_t suid);
2483 [DllImport (LIBC, SetLastError=true)]
2484 public static extern int setresgid (uint rgid, uint egid, uint sgid);
2487 // pid_t fork(void);
2488 [DllImport (LIBC, SetLastError=true)]
2489 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " +
2490 "shutdown code.\nUse System.Diagnostics.Process instead")]
2491 public static extern int fork ();
2494 // pid_t vfork(void);
2495 [DllImport (LIBC, SetLastError=true)]
2496 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " +
2497 "shutdown code.\nUse System.Diagnostics.Process instead")]
2498 public static extern int vfork ();
2500 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
2501 private static extern IntPtr sys_ttyname (int fd);
2503 [Obsolete ("Not re-entrant. Use ttyname_r instead.")]
2504 public static string ttyname (int fd)
2506 IntPtr r = sys_ttyname (fd);
2507 return UnixMarshal.PtrToString (r);
2511 // int ttyname_r(int fd, char *buf, size_t buflen);
2512 [DllImport (MPH, SetLastError=true,
2513 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
2514 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
2516 public static int ttyname_r (int fd, StringBuilder buf)
2518 return ttyname_r (fd, buf, (ulong) buf.Capacity);
2521 [DllImport (LIBC, EntryPoint="isatty")]
2522 private static extern int sys_isatty (int fd);
2524 public static bool isatty (int fd)
2526 return sys_isatty (fd) == 1;
2529 [DllImport (LIBC, SetLastError=true)]
2530 public static extern int link (string oldpath, string newpath);
2532 [DllImport (LIBC, SetLastError=true)]
2533 public static extern int symlink (string oldpath, string newpath);
2536 // int readlink(const char *path, char *buf, size_t bufsize);
2537 [DllImport (MPH, SetLastError=true,
2538 EntryPoint="Mono_Posix_Syscall_readlink")]
2539 public static extern int readlink (string path, [Out] StringBuilder buf, ulong bufsiz);
2541 public static int readlink (string path, [Out] StringBuilder buf)
2543 return readlink (path, buf, (ulong) buf.Capacity);
2546 [DllImport (LIBC, SetLastError=true)]
2547 public static extern int unlink (string pathname);
2549 [DllImport (LIBC, SetLastError=true)]
2550 public static extern int rmdir (string pathname);
2553 // pid_t tcgetpgrp(int fd);
2554 [DllImport (LIBC, SetLastError=true)]
2555 public static extern int tcgetpgrp (int fd);
2558 // int tcsetpgrp(int fd, pid_t pgrp);
2559 [DllImport (LIBC, SetLastError=true)]
2560 public static extern int tcsetpgrp (int fd, int pgrp);
2562 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
2563 private static extern IntPtr sys_getlogin ();
2565 [Obsolete ("Not re-entrant. Use getlogin_r instead.")]
2566 public static string getlogin ()
2568 IntPtr r = sys_getlogin ();
2569 return UnixMarshal.PtrToString (r);
2573 // int getlogin_r(char *buf, size_t bufsize);
2574 [DllImport (MPH, SetLastError=true,
2575 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
2576 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
2578 public static int getlogin_r (StringBuilder name)
2580 return getlogin_r (name, (ulong) name.Capacity);
2583 [DllImport (LIBC, SetLastError=true)]
2584 public static extern int setlogin (string name);
2587 // int gethostname(char *name, size_t len);
2588 [DllImport (MPH, SetLastError=true,
2589 EntryPoint="Mono_Posix_Syscall_gethostname")]
2590 public static extern int gethostname ([Out] StringBuilder name, ulong len);
2592 public static int gethostname (StringBuilder name)
2594 return gethostname (name, (ulong) name.Capacity);
2598 // int gethostname(const char *name, size_t len);
2599 [DllImport (MPH, SetLastError=true,
2600 EntryPoint="Mono_Posix_Syscall_gethostname")]
2601 public static extern int sethostname (string name, ulong len);
2603 public static int sethostname (string name)
2605 return sethostname (name, (ulong) name.Length);
2608 [DllImport (MPH, SetLastError=true,
2609 EntryPoint="Mono_Posix_Syscall_gethostid")]
2610 public static extern long gethostid ();
2612 [DllImport (MPH, SetLastError=true,
2613 EntryPoint="Mono_Posix_Syscall_sethostid")]
2614 public static extern int sethostid (long hostid);
2617 // int getdomainname(char *name, size_t len);
2618 [DllImport (MPH, SetLastError=true,
2619 EntryPoint="Mono_Posix_Syscall_getdomainname")]
2620 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
2622 public static int getdomainname (StringBuilder name)
2624 return getdomainname (name, (ulong) name.Capacity);
2628 // int setdomainname(const char *name, size_t len);
2629 [DllImport (MPH, SetLastError=true,
2630 EntryPoint="Mono_Posix_Syscall_setdomainname")]
2631 public static extern int setdomainname (string name, ulong len);
2633 public static int setdomainname (string name)
2635 return setdomainname (name, (ulong) name.Length);
2638 [DllImport (LIBC, SetLastError=true)]
2639 public static extern int vhangup ();
2641 // Revoke doesn't appear to be POSIX. Include it?
2642 [DllImport (LIBC, SetLastError=true)]
2643 public static extern int revoke (string file);
2645 // TODO: profil? It's not POSIX.
2647 [DllImport (LIBC, SetLastError=true)]
2648 public static extern int acct (string filename);
2650 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
2651 private static extern IntPtr sys_getusershell ();
2653 public static string getusershell ()
2655 IntPtr r = sys_getusershell ();
2656 return UnixMarshal.PtrToString (r);
2659 [DllImport (LIBC, SetLastError=true)]
2660 public static extern void setusershell ();
2662 [DllImport (LIBC, SetLastError=true)]
2663 public static extern void endusershell ();
2665 [DllImport (LIBC, SetLastError=true)]
2666 private static extern int daemon (int nochdir, int noclose);
2668 public static int daemon (bool nochdir, bool noclose)
2670 return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
2673 [DllImport (LIBC, SetLastError=true)]
2674 public static extern int chroot (string path);
2676 // skipping getpass(3) as the man page states:
2677 // This function is obsolete. Do not use it.
2679 [DllImport (LIBC, SetLastError=true)]
2680 public static extern int fsync (int fd);
2682 [DllImport (LIBC, SetLastError=true)]
2683 public static extern int fdatasync (int fd);
2685 [DllImport (LIBC, SetLastError=true)]
2686 public static extern void sync ();
2688 [DllImport (LIBC, SetLastError=true)]
2689 [Obsolete ("Dropped in POSIX 1003.1-2001. " +
2690 "Use Unistd.sysconf (SysConf._SC_PAGESIZE).")]
2691 public static extern int getpagesize ();
2694 // int truncate(const char *path, off_t length);
2695 [DllImport (MPH, SetLastError=true,
2696 EntryPoint="Mono_Posix_Syscall_truncate")]
2697 public static extern int truncate (string path, long length);
2700 // int ftruncate(int fd, off_t length);
2701 [DllImport (MPH, SetLastError=true,
2702 EntryPoint="Mono_Posix_Syscall_ftruncate")]
2703 public static extern int ftruncate (int fd, long length);
2705 [DllImport (LIBC, SetLastError=true)]
2706 public static extern int getdtablesize ();
2708 [DllImport (LIBC, SetLastError=true)]
2709 public static extern int brk (IntPtr end_data_segment);
2711 [DllImport (LIBC, SetLastError=true)]
2712 public static extern IntPtr sbrk (IntPtr increment);
2714 // TODO: syscall(2)?
2715 // Probably safer to skip entirely.
2718 // int lockf(int fd, int cmd, off_t len);
2719 [DllImport (MPH, SetLastError=true,
2720 EntryPoint="Mono_Posix_Syscall_lockf")]
2721 public static extern int lockf (int fd, LockFlags cmd, long len);
2723 [DllImport (CRYPT, SetLastError=true, EntryPoint="crypt")]
2724 private static extern IntPtr sys_crypt (string key, string salt);
2726 public static string crypt (string key, string salt)
2728 IntPtr r = sys_crypt (key, salt);
2729 return UnixMarshal.PtrToString (r);
2732 [DllImport (CRYPT, SetLastError=true, EntryPoint="encrypt")]
2733 private static extern void sys_encrypt ([In, Out] byte[] block, int edflag);
2735 public static void encrypt (byte[] block, bool decode)
2737 if (block.Length < 64)
2738 throw new ArgumentOutOfRangeException ("block", "Must refer to at least 64 bytes");
2739 sys_encrypt (block, decode ? 1 : 0);
2743 // void swab(const void *from, void *to, ssize_t n);
2744 [DllImport (MPH, SetLastError=true,
2745 EntryPoint="Mono_Posix_Syscall_swab")]
2746 public static extern void swab (IntPtr from, IntPtr to, long n);
2748 public static unsafe void swab (void* from, void* to, long n)
2750 swab ((IntPtr) from, (IntPtr) to, n);
2755 #region <utime.h> Declarations
2757 // <utime.h> -- COMPLETE
2760 [DllImport (MPH, SetLastError=true,
2761 EntryPoint="Mono_Posix_Syscall_utime")]
2762 public static extern int utime (string filename, ref Utimbuf buf);
2764 [DllImport (MPH, SetLastError=true,
2765 EntryPoint="Mono_Posix_Syscall_utime")]
2766 private static extern int utime (string filename, IntPtr buf);
2768 public static int utime (string filename)
2770 return utime (filename, IntPtr.Zero);