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-2006 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 // NativeConvert 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;
59 using System.Security;
61 using Mono.Unix.Native;
63 namespace Mono.Unix.Native {
68 [CLSCompliant (false)]
69 public enum SyslogOptions {
70 LOG_PID = 0x01, // log the pid with each message
71 LOG_CONS = 0x02, // log on the console if errors in sending
72 LOG_ODELAY = 0x04, // delay open until first syslog (default)
73 LOG_NDELAY = 0x08, // don't delay open
74 LOG_NOWAIT = 0x10, // don't wait for console forks; DEPRECATED
75 LOG_PERROR = 0x20 // log to stderr as well
79 [CLSCompliant (false)]
80 public enum SyslogFacility {
91 LOG_AUTHPRIV = 10 << 3,
100 LOG_LOCAL7 = 23 << 3,
104 [CLSCompliant (false)]
105 public enum SyslogLevel {
106 LOG_EMERG = 0, // system is unusable
107 LOG_ALERT = 1, // action must be taken immediately
108 LOG_CRIT = 2, // critical conditions
109 LOG_ERR = 3, // warning conditions
110 LOG_WARNING = 4, // warning conditions
111 LOG_NOTICE = 5, // normal but significant condition
112 LOG_INFO = 6, // informational
113 LOG_DEBUG = 7 // debug-level messages
117 [CLSCompliant (false)]
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 NativeConvert, 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 [CLSCompliant (false)]
157 public enum FilePermissions : uint {
158 S_ISUID = 0x0800, // Set user ID on execution
159 S_ISGID = 0x0400, // Set group ID on execution
160 S_ISVTX = 0x0200, // Save swapped text after use (sticky).
161 S_IRUSR = 0x0100, // Read by owner
162 S_IWUSR = 0x0080, // Write by owner
163 S_IXUSR = 0x0040, // Execute by owner
164 S_IRGRP = 0x0020, // Read by group
165 S_IWGRP = 0x0010, // Write by group
166 S_IXGRP = 0x0008, // Execute by group
167 S_IROTH = 0x0004, // Read by other
168 S_IWOTH = 0x0002, // Write by other
169 S_IXOTH = 0x0001, // Execute by other
171 S_IRWXG = (S_IRGRP | S_IWGRP | S_IXGRP),
172 S_IRWXU = (S_IRUSR | S_IWUSR | S_IXUSR),
173 S_IRWXO = (S_IROTH | S_IWOTH | S_IXOTH),
174 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
175 ALLPERMS = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
176 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
179 // Why these are held in "mode_t" is beyond me...
180 S_IFMT = 0xF000, // Bits which determine file type
181 [Map(SuppressFlags="S_IFMT")]
182 S_IFDIR = 0x4000, // Directory
183 [Map(SuppressFlags="S_IFMT")]
184 S_IFCHR = 0x2000, // Character device
185 [Map(SuppressFlags="S_IFMT")]
186 S_IFBLK = 0x6000, // Block device
187 [Map(SuppressFlags="S_IFMT")]
188 S_IFREG = 0x8000, // Regular file
189 [Map(SuppressFlags="S_IFMT")]
190 S_IFIFO = 0x1000, // FIFO
191 [Map(SuppressFlags="S_IFMT")]
192 S_IFLNK = 0xA000, // Symbolic link
193 [Map(SuppressFlags="S_IFMT")]
194 S_IFSOCK = 0xC000, // Socket
198 [CLSCompliant (false)]
199 public enum FcntlCommand : int {
200 // Form /usr/include/bits/fcntl.h
201 F_DUPFD = 0, // Duplicate file descriptor.
202 F_GETFD = 1, // Get file descriptor flags.
203 F_SETFD = 2, // Set file descriptor flags.
204 F_GETFL = 3, // Get file status flags.
205 F_SETFL = 4, // Set file status flags.
206 F_GETLK = 12, // Get record locking info. [64]
207 F_SETLK = 13, // Set record locking info (non-blocking). [64]
208 F_SETLKW = 14, // Set record locking info (blocking). [64]
209 F_SETOWN = 8, // Set owner of socket (receiver of SIGIO).
210 F_GETOWN = 9, // Get owner of socket (receiver of SIGIO).
211 F_SETSIG = 10, // Set number of signal to be sent.
212 F_GETSIG = 11, // Get number of signal to be sent.
213 F_SETLEASE = 1024, // Set a lease.
214 F_GETLEASE = 1025, // Enquire what lease is active.
215 F_NOTIFY = 1026, // Required notifications on a directory
219 [CLSCompliant (false)]
220 public enum LockType : short {
221 F_RDLCK = 0, // Read lock.
222 F_WRLCK = 1, // Write lock.
223 F_UNLCK = 2, // Remove lock.
227 [CLSCompliant (false)]
228 public enum SeekFlags : short {
229 // values liberally copied from /usr/include/unistd.h
230 SEEK_SET = 0, // Seek from beginning of file.
231 SEEK_CUR = 1, // Seek from current position.
232 SEEK_END = 2, // Seek from end of file.
234 L_SET = SEEK_SET, // BSD alias for SEEK_SET
235 L_INCR = SEEK_CUR, // BSD alias for SEEK_CUR
236 L_XTND = SEEK_END, // BSD alias for SEEK_END
240 [CLSCompliant (false)]
241 public enum DirectoryNotifyFlags : int {
242 // from /usr/include/bits/fcntl.h
243 DN_ACCESS = 0x00000001, // File accessed.
244 DN_MODIFY = 0x00000002, // File modified.
245 DN_CREATE = 0x00000004, // File created.
246 DN_DELETE = 0x00000008, // File removed.
247 DN_RENAME = 0x00000010, // File renamed.
248 DN_ATTRIB = 0x00000020, // File changed attributes.
249 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
253 [CLSCompliant (false)]
254 public enum PosixFadviseAdvice : int {
255 POSIX_FADV_NORMAL = 0, // No further special treatment.
256 POSIX_FADV_RANDOM = 1, // Expect random page references.
257 POSIX_FADV_SEQUENTIAL = 2, // Expect sequential page references.
258 POSIX_FADV_WILLNEED = 3, // Will need these pages.
259 POSIX_FADV_DONTNEED = 4, // Don't need these pages.
260 POSIX_FADV_NOREUSE = 5, // Data will be accessed once.
264 [CLSCompliant (false)]
265 public enum PosixMadviseAdvice : int {
266 POSIX_MADV_NORMAL = 0, // No further special treatment.
267 POSIX_MADV_RANDOM = 1, // Expect random page references.
268 POSIX_MADV_SEQUENTIAL = 2, // Expect sequential page references.
269 POSIX_MADV_WILLNEED = 3, // Will need these pages.
270 POSIX_MADV_DONTNEED = 4, // Don't need these pages.
274 public enum Signum : int {
275 SIGHUP = 1, // Hangup (POSIX).
276 SIGINT = 2, // Interrupt (ANSI).
277 SIGQUIT = 3, // Quit (POSIX).
278 SIGILL = 4, // Illegal instruction (ANSI).
279 SIGTRAP = 5, // Trace trap (POSIX).
280 SIGABRT = 6, // Abort (ANSI).
281 SIGIOT = 6, // IOT trap (4.2 BSD).
282 SIGBUS = 7, // BUS error (4.2 BSD).
283 SIGFPE = 8, // Floating-point exception (ANSI).
284 SIGKILL = 9, // Kill, unblockable (POSIX).
285 SIGUSR1 = 10, // User-defined signal 1 (POSIX).
286 SIGSEGV = 11, // Segmentation violation (ANSI).
287 SIGUSR2 = 12, // User-defined signal 2 (POSIX).
288 SIGPIPE = 13, // Broken pipe (POSIX).
289 SIGALRM = 14, // Alarm clock (POSIX).
290 SIGTERM = 15, // Termination (ANSI).
291 SIGSTKFLT = 16, // Stack fault.
292 SIGCLD = SIGCHLD, // Same as SIGCHLD (System V).
293 SIGCHLD = 17, // Child status has changed (POSIX).
294 SIGCONT = 18, // Continue (POSIX).
295 SIGSTOP = 19, // Stop, unblockable (POSIX).
296 SIGTSTP = 20, // Keyboard stop (POSIX).
297 SIGTTIN = 21, // Background read from tty (POSIX).
298 SIGTTOU = 22, // Background write to tty (POSIX).
299 SIGURG = 23, // Urgent condition on socket (4.2 BSD).
300 SIGXCPU = 24, // CPU limit exceeded (4.2 BSD).
301 SIGXFSZ = 25, // File size limit exceeded (4.2 BSD).
302 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
303 SIGPROF = 27, // Profiling alarm clock (4.2 BSD).
304 SIGWINCH = 28, // Window size change (4.3 BSD, Sun).
305 SIGPOLL = SIGIO, // Pollable event occurred (System V).
306 SIGIO = 29, // I/O now possible (4.2 BSD).
307 SIGPWR = 30, // Power failure restart (System V).
308 SIGSYS = 31, // Bad system call.
313 public enum WaitOptions : int {
314 WNOHANG = 1, // Don't block waiting
315 WUNTRACED = 2, // Report status of stopped children
319 [CLSCompliant (false)]
320 public enum AccessModes : int {
328 [CLSCompliant (false)]
329 public enum PathconfName : int {
336 _PC_CHOWN_RESTRICTED,
344 _PC_REC_INCR_XFER_SIZE,
345 _PC_REC_MAX_XFER_SIZE,
346 _PC_REC_MIN_XFER_SIZE,
354 [CLSCompliant (false)]
355 public enum SysconfName : int {
365 _SC_REALTIME_SIGNALS,
366 _SC_PRIORITY_SCHEDULING,
375 _SC_MEMORY_PROTECTION,
378 _SC_SHARED_MEMORY_OBJECTS,
381 _SC_AIO_PRIO_DELTA_MAX,
392 /* Values for the argument to `sysconf'
393 corresponding to _POSIX2_* symbols. */
398 _SC_COLL_WEIGHTS_MAX,
403 _SC_CHARCLASS_NAME_MAX,
419 _SC_IOV_MAX = _SC_UIO_MAXIOV,
420 _SC_PII_INTERNET_STREAM,
421 _SC_PII_INTERNET_DGRAM,
426 /* Values according to POSIX 1003.1c (POSIX threads). */
428 _SC_THREAD_SAFE_FUNCTIONS,
429 _SC_GETGR_R_SIZE_MAX,
430 _SC_GETPW_R_SIZE_MAX,
433 _SC_THREAD_DESTRUCTOR_ITERATIONS,
435 _SC_THREAD_STACK_MIN,
436 _SC_THREAD_THREADS_MAX,
437 _SC_THREAD_ATTR_STACKADDR,
438 _SC_THREAD_ATTR_STACKSIZE,
439 _SC_THREAD_PRIORITY_SCHEDULING,
440 _SC_THREAD_PRIO_INHERIT,
441 _SC_THREAD_PRIO_PROTECT,
442 _SC_THREAD_PROCESS_SHARED,
443 _SC_NPROCESSORS_CONF,
444 _SC_NPROCESSORS_ONLN,
450 _SC_XOPEN_XCU_VERSION,
485 _SC_XBS5_ILP32_OFF32,
486 _SC_XBS5_ILP32_OFFBIG,
488 _SC_XBS5_LPBIG_OFFBIG,
491 _SC_XOPEN_REALTIME_THREADS,
496 _SC_C_LANG_SUPPORT_R,
502 _SC_DEVICE_SPECIFIC_R,
513 _SC_READER_WRITER_LOCKS,
521 _SC_THREAD_SPORADIC_SERVER,
523 _SC_SYSTEM_DATABASE_R,
525 _SC_TYPED_MEMORY_OBJECTS,
529 _SC_2_PBS_ACCOUNTING,
535 _SC_2_PBS_CHECKPOINT,
542 _SC_TRACE_EVENT_FILTER,
545 _SC_LEVEL1_ICACHE_SIZE,
546 _SC_LEVEL1_ICACHE_ASSOC,
547 _SC_LEVEL1_ICACHE_LINESIZE,
548 _SC_LEVEL1_DCACHE_SIZE,
549 _SC_LEVEL1_DCACHE_ASSOC,
550 _SC_LEVEL1_DCACHE_LINESIZE,
551 _SC_LEVEL2_CACHE_SIZE,
552 _SC_LEVEL2_CACHE_ASSOC,
553 _SC_LEVEL2_CACHE_LINESIZE,
554 _SC_LEVEL3_CACHE_SIZE,
555 _SC_LEVEL3_CACHE_ASSOC,
556 _SC_LEVEL3_CACHE_LINESIZE,
557 _SC_LEVEL4_CACHE_SIZE,
558 _SC_LEVEL4_CACHE_ASSOC,
559 _SC_LEVEL4_CACHE_LINESIZE
563 [CLSCompliant (false)]
564 public enum ConfstrName : int {
565 _CS_PATH, /* The default search path. */
566 _CS_V6_WIDTH_RESTRICTED_ENVS,
567 _CS_GNU_LIBC_VERSION,
568 _CS_GNU_LIBPTHREAD_VERSION,
569 _CS_LFS_CFLAGS = 1000,
577 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
578 _CS_XBS5_ILP32_OFF32_LDFLAGS,
579 _CS_XBS5_ILP32_OFF32_LIBS,
580 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
581 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
582 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
583 _CS_XBS5_ILP32_OFFBIG_LIBS,
584 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
585 _CS_XBS5_LP64_OFF64_CFLAGS,
586 _CS_XBS5_LP64_OFF64_LDFLAGS,
587 _CS_XBS5_LP64_OFF64_LIBS,
588 _CS_XBS5_LP64_OFF64_LINTFLAGS,
589 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
590 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
591 _CS_XBS5_LPBIG_OFFBIG_LIBS,
592 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
593 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
594 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
595 _CS_POSIX_V6_ILP32_OFF32_LIBS,
596 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
597 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
598 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
599 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
600 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
601 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
602 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
603 _CS_POSIX_V6_LP64_OFF64_LIBS,
604 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
605 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
606 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
607 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
608 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
612 [CLSCompliant (false)]
613 public enum LockfCommand : int {
614 F_ULOCK = 0, // Unlock a previously locked region.
615 F_LOCK = 1, // Lock a region for exclusive use.
616 F_TLOCK = 2, // Test and lock a region for exclusive use.
617 F_TEST = 3, // Test a region for other process locks.
621 public enum PollEvents : short {
622 POLLIN = 0x0001, // There is data to read
623 POLLPRI = 0x0002, // There is urgent data to read
624 POLLOUT = 0x0004, // Writing now will not block
625 POLLERR = 0x0008, // Error condition
626 POLLHUP = 0x0010, // Hung up
627 POLLNVAL = 0x0020, // Invalid request; fd not open
628 // XPG4.2 definitions (via _XOPEN_SOURCE)
629 POLLRDNORM = 0x0040, // Normal data may be read
630 POLLRDBAND = 0x0080, // Priority data may be read
631 POLLWRNORM = 0x0100, // Writing now will not block
632 POLLWRBAND = 0x0200, // Priority data may be written
636 [CLSCompliant (false)]
637 public enum XattrFlags : int {
644 [CLSCompliant (false)]
645 public enum MountFlags : ulong {
646 ST_RDONLY = 1, // Mount read-only
647 ST_NOSUID = 2, // Ignore suid and sgid bits
648 ST_NODEV = 4, // Disallow access to device special files
649 ST_SYNCHRONOUS = 16, // Writes are synced at once
650 ST_MANDLOCK = 64, // Allow mandatory locks on an FS
651 ST_WRITE = 128, // Write on file/directory/symlink
652 ST_APPEND = 256, // Append-only file
653 ST_IMMUTABLE = 512, // Immutable file
654 ST_NOATIME = 1024, // Do not update access times
655 ST_NODIRATIME = 2048, // Do not update directory access times
659 [CLSCompliant (false)]
660 public enum MmapFlags : int {
661 MAP_SHARED = 0x01, // Share changes.
662 MAP_PRIVATE = 0x02, // Changes are private.
663 MAP_TYPE = 0x0f, // Mask for type of mapping.
664 MAP_FIXED = 0x10, // Interpret addr exactly.
666 MAP_ANONYMOUS = 0x20, // Don't use a file.
667 MAP_ANON = MAP_ANONYMOUS,
669 // These are Linux-specific.
670 MAP_GROWSDOWN = 0x00100, // Stack-like segment.
671 MAP_DENYWRITE = 0x00800, // ETXTBSY
672 MAP_EXECUTABLE = 0x01000, // Mark it as an executable.
673 MAP_LOCKED = 0x02000, // Lock the mapping.
674 MAP_NORESERVE = 0x04000, // Don't check for reservations.
675 MAP_POPULATE = 0x08000, // Populate (prefault) pagetables.
676 MAP_NONBLOCK = 0x10000, // Do not block on IO.
680 [CLSCompliant (false)]
681 public enum MmapProts : int {
682 PROT_READ = 0x1, // Page can be read.
683 PROT_WRITE = 0x2, // Page can be written.
684 PROT_EXEC = 0x4, // Page can be executed.
685 PROT_NONE = 0x0, // Page can not be accessed.
686 PROT_GROWSDOWN = 0x01000000, // Extend change to start of
687 // growsdown vma (mprotect only).
688 PROT_GROWSUP = 0x02000000, // Extend change to start of
689 // growsup vma (mprotect only).
693 [CLSCompliant (false)]
694 public enum MsyncFlags : int {
695 MS_ASYNC = 0x1, // Sync memory asynchronously.
696 MS_SYNC = 0x4, // Synchronous memory sync.
697 MS_INVALIDATE = 0x2, // Invalidate the caches.
701 [CLSCompliant (false)]
702 public enum MlockallFlags : int {
703 MCL_CURRENT = 0x1, // Lock all currently mapped pages.
704 MCL_FUTURE = 0x2, // Lock all additions to address
708 [CLSCompliant (false)]
709 public enum MremapFlags : ulong {
710 MREMAP_MAYMOVE = 0x1,
722 [CLSCompliant (false)]
723 public LockType l_type; // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
724 [CLSCompliant (false)]
725 public SeekFlags l_whence; // How to interpret l_start
726 [off_t] public long l_start; // Starting offset for lock
727 [off_t] public long l_len; // Number of bytes to lock
728 [pid_t] public int l_pid; // PID of process blocking our lock (F_GETLK only)
730 public override int GetHashCode ()
732 return l_type.GetHashCode () ^ l_whence.GetHashCode () ^
733 l_start.GetHashCode () ^ l_len.GetHashCode () ^
734 l_pid.GetHashCode ();
737 public override bool Equals (object obj)
739 if (obj.GetType() != GetType())
741 Flock value = (Flock) obj;
742 return l_type == value.l_type && l_whence == value.l_whence &&
743 l_start == value.l_start && l_len == value.l_len &&
744 l_pid == value.l_pid;
747 public bool Equals (Flock value)
749 return l_type == value.l_type && l_whence == value.l_whence &&
750 l_start == value.l_start && l_len == value.l_len &&
751 l_pid == value.l_pid;
754 public static bool operator== (Flock lhs, Flock rhs)
756 return lhs.Equals (rhs);
759 public static bool operator!= (Flock lhs, Flock rhs)
761 return !lhs.Equals (rhs);
765 [Map ("struct pollfd")]
768 : IEquatable <Pollfd>
772 [CLSCompliant (false)]
773 public PollEvents events;
774 [CLSCompliant (false)]
775 public PollEvents revents;
777 public override int GetHashCode ()
779 return events.GetHashCode () ^ revents.GetHashCode ();
782 public override bool Equals (object obj)
784 if (obj == null || obj.GetType () != GetType ())
786 Pollfd value = (Pollfd) obj;
787 return value.events == events && value.revents == revents;
790 public bool Equals (Pollfd value)
792 return value.events == events && value.revents == revents;
795 public static bool operator== (Pollfd lhs, Pollfd rhs)
797 return lhs.Equals (rhs);
800 public static bool operator!= (Pollfd lhs, Pollfd rhs)
802 return !lhs.Equals (rhs);
806 [Map ("struct stat")]
812 [CLSCompliant (false)]
813 [dev_t] public ulong st_dev; // device
814 [CLSCompliant (false)]
815 [ino_t] public ulong st_ino; // inode
816 [CLSCompliant (false)]
817 public FilePermissions st_mode; // protection
818 [CLSCompliant (false)]
820 private uint _padding_; // padding for structure alignment
821 [CLSCompliant (false)]
822 [nlink_t] public ulong st_nlink; // number of hard links
823 [CLSCompliant (false)]
824 [uid_t] public uint st_uid; // user ID of owner
825 [CLSCompliant (false)]
826 [gid_t] public uint st_gid; // group ID of owner
827 [CLSCompliant (false)]
828 [dev_t] public ulong st_rdev; // device type (if inode device)
829 [off_t] public long st_size; // total size, in bytes
830 [blksize_t] public long st_blksize; // blocksize for filesystem I/O
831 [blkcnt_t] public long st_blocks; // number of blocks allocated
832 [time_t] public long st_atime; // time of last access
833 [time_t] public long st_mtime; // time of last modification
834 [time_t] public long st_ctime; // time of last status change
836 public override int GetHashCode ()
838 return st_dev.GetHashCode () ^
839 st_ino.GetHashCode () ^
840 st_mode.GetHashCode () ^
841 st_nlink.GetHashCode () ^
842 st_uid.GetHashCode () ^
843 st_gid.GetHashCode () ^
844 st_rdev.GetHashCode () ^
845 st_size.GetHashCode () ^
846 st_blksize.GetHashCode () ^
847 st_blocks.GetHashCode () ^
848 st_atime.GetHashCode () ^
849 st_mtime.GetHashCode () ^
850 st_ctime.GetHashCode ();
853 public override bool Equals (object obj)
855 if (obj == null || obj.GetType() != GetType ())
857 Stat value = (Stat) obj;
858 return value.st_dev == st_dev &&
859 value.st_ino == st_ino &&
860 value.st_mode == st_mode &&
861 value.st_nlink == st_nlink &&
862 value.st_uid == st_uid &&
863 value.st_gid == st_gid &&
864 value.st_rdev == st_rdev &&
865 value.st_size == st_size &&
866 value.st_blksize == st_blksize &&
867 value.st_blocks == st_blocks &&
868 value.st_atime == st_atime &&
869 value.st_mtime == st_mtime &&
870 value.st_ctime == st_ctime;
873 public bool Equals (Stat value)
875 return value.st_dev == st_dev &&
876 value.st_ino == st_ino &&
877 value.st_mode == st_mode &&
878 value.st_nlink == st_nlink &&
879 value.st_uid == st_uid &&
880 value.st_gid == st_gid &&
881 value.st_rdev == st_rdev &&
882 value.st_size == st_size &&
883 value.st_blksize == st_blksize &&
884 value.st_blocks == st_blocks &&
885 value.st_atime == st_atime &&
886 value.st_mtime == st_mtime &&
887 value.st_ctime == st_ctime;
890 public static bool operator== (Stat lhs, Stat rhs)
892 return lhs.Equals (rhs);
895 public static bool operator!= (Stat lhs, Stat rhs)
897 return !lhs.Equals (rhs);
901 // `struct statvfs' isn't portable, so don't generate To/From methods.
903 [CLSCompliant (false)]
904 public struct Statvfs
906 : IEquatable <Statvfs>
909 public ulong f_bsize; // file system block size
910 public ulong f_frsize; // fragment size
911 [fsblkcnt_t] public ulong f_blocks; // size of fs in f_frsize units
912 [fsblkcnt_t] public ulong f_bfree; // # free blocks
913 [fsblkcnt_t] public ulong f_bavail; // # free blocks for non-root
914 [fsfilcnt_t] public ulong f_files; // # inodes
915 [fsfilcnt_t] public ulong f_ffree; // # free inodes
916 [fsfilcnt_t] public ulong f_favail; // # free inodes for non-root
917 public ulong f_fsid; // file system id
918 public MountFlags f_flag; // mount flags
919 public ulong f_namemax; // maximum filename length
921 public override int GetHashCode ()
923 return f_bsize.GetHashCode () ^
924 f_frsize.GetHashCode () ^
925 f_blocks.GetHashCode () ^
926 f_bfree.GetHashCode () ^
927 f_bavail.GetHashCode () ^
928 f_files.GetHashCode () ^
929 f_ffree.GetHashCode () ^
930 f_favail.GetHashCode () ^
931 f_fsid.GetHashCode () ^
932 f_flag.GetHashCode () ^
933 f_namemax.GetHashCode ();
936 public override bool Equals (object obj)
938 if (obj == null || obj.GetType() != GetType ())
940 Statvfs value = (Statvfs) obj;
941 return value.f_bsize == f_bsize &&
942 value.f_frsize == f_frsize &&
943 value.f_blocks == f_blocks &&
944 value.f_bfree == f_bfree &&
945 value.f_bavail == f_bavail &&
946 value.f_files == f_files &&
947 value.f_ffree == f_ffree &&
948 value.f_favail == f_favail &&
949 value.f_fsid == f_fsid &&
950 value.f_flag == f_flag &&
951 value.f_namemax == f_namemax;
954 public bool Equals (Statvfs value)
956 return value.f_bsize == f_bsize &&
957 value.f_frsize == f_frsize &&
958 value.f_blocks == f_blocks &&
959 value.f_bfree == f_bfree &&
960 value.f_bavail == f_bavail &&
961 value.f_files == f_files &&
962 value.f_ffree == f_ffree &&
963 value.f_favail == f_favail &&
964 value.f_fsid == f_fsid &&
965 value.f_flag == f_flag &&
966 value.f_namemax == f_namemax;
969 public static bool operator== (Statvfs lhs, Statvfs rhs)
971 return lhs.Equals (rhs);
974 public static bool operator!= (Statvfs lhs, Statvfs rhs)
976 return !lhs.Equals (rhs);
980 [Map ("struct timeval")]
981 public struct Timeval
983 : IEquatable <Timeval>
986 [time_t] public long tv_sec; // seconds
987 [suseconds_t] public long tv_usec; // microseconds
989 public override int GetHashCode ()
991 return tv_sec.GetHashCode () ^ tv_usec.GetHashCode ();
994 public override bool Equals (object obj)
996 if (obj == null || obj.GetType () != GetType ())
998 Timeval value = (Timeval) obj;
999 return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1002 public bool Equals (Timeval value)
1004 return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1007 public static bool operator== (Timeval lhs, Timeval rhs)
1009 return lhs.Equals (rhs);
1012 public static bool operator!= (Timeval lhs, Timeval rhs)
1014 return !lhs.Equals (rhs);
1018 [Map ("struct timezone")]
1019 public struct Timezone
1021 : IEquatable <Timezone>
1024 public int tz_minuteswest; // minutes W of Greenwich
1025 private int tz_dsttime; // type of dst correction (OBSOLETE)
1027 public override int GetHashCode ()
1029 return tz_minuteswest.GetHashCode ();
1032 public override bool Equals (object obj)
1034 if (obj == null || obj.GetType () != GetType ())
1036 Timezone value = (Timezone) obj;
1037 return value.tz_minuteswest == tz_minuteswest;
1040 public bool Equals (Timezone value)
1042 return value.tz_minuteswest == tz_minuteswest;
1045 public static bool operator== (Timezone lhs, Timezone rhs)
1047 return lhs.Equals (rhs);
1050 public static bool operator!= (Timezone lhs, Timezone rhs)
1052 return !lhs.Equals (rhs);
1056 public struct Utimbuf
1058 : IEquatable <Utimbuf>
1061 [time_t] public long actime; // access time
1062 [time_t] public long modtime; // modification time
1064 public override int GetHashCode ()
1066 return actime.GetHashCode () ^ modtime.GetHashCode ();
1069 public override bool Equals (object obj)
1071 if (obj == null || obj.GetType () != GetType ())
1073 Utimbuf value = (Utimbuf) obj;
1074 return value.actime == actime && value.modtime == modtime;
1077 public bool Equals (Utimbuf value)
1079 return value.actime == actime && value.modtime == modtime;
1082 public static bool operator== (Utimbuf lhs, Utimbuf rhs)
1084 return lhs.Equals (rhs);
1087 public static bool operator!= (Utimbuf lhs, Utimbuf rhs)
1089 return !lhs.Equals (rhs);
1097 public sealed class Dirent
1099 : IEquatable <Dirent>
1102 [CLSCompliant (false)]
1103 public /* ino_t */ ulong d_ino;
1104 public /* off_t */ long d_off;
1105 [CLSCompliant (false)]
1106 public ushort d_reclen;
1108 public string d_name;
1110 public override int GetHashCode ()
1112 return d_ino.GetHashCode () ^ d_off.GetHashCode () ^
1113 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
1114 d_name.GetHashCode ();
1117 public override bool Equals (object obj)
1119 if (obj == null || GetType() != obj.GetType())
1121 Dirent d = (Dirent) obj;
1125 public bool Equals (Dirent value)
1129 return value.d_ino == d_ino && value.d_off == d_off &&
1130 value.d_reclen == d_reclen && value.d_type == d_type &&
1131 value.d_name == d_name;
1134 public override string ToString ()
1139 public static bool operator== (Dirent lhs, Dirent rhs)
1141 return Object.Equals (lhs, rhs);
1144 public static bool operator!= (Dirent lhs, Dirent rhs)
1146 return !Object.Equals (lhs, rhs);
1150 public sealed class Fstab
1152 : IEquatable <Fstab>
1155 public string fs_spec;
1156 public string fs_file;
1157 public string fs_vfstype;
1158 public string fs_mntops;
1159 public string fs_type;
1161 public int fs_passno;
1163 public override int GetHashCode ()
1165 return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
1166 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
1167 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
1170 public override bool Equals (object obj)
1172 if (obj == null || GetType() != obj.GetType())
1174 Fstab f = (Fstab) obj;
1178 public bool Equals (Fstab value)
1182 return value.fs_spec == fs_spec && value.fs_file == fs_file &&
1183 value.fs_vfstype == fs_vfstype && value.fs_mntops == fs_mntops &&
1184 value.fs_type == fs_type && value.fs_freq == fs_freq &&
1185 value.fs_passno == fs_passno;
1188 public override string ToString ()
1193 public static bool operator== (Fstab lhs, Fstab rhs)
1195 return Object.Equals (lhs, rhs);
1198 public static bool operator!= (Fstab lhs, Fstab rhs)
1200 return !Object.Equals (lhs, rhs);
1204 public sealed class Group
1206 : IEquatable <Group>
1209 public string gr_name;
1210 public string gr_passwd;
1211 [CLSCompliant (false)]
1212 public /* gid_t */ uint gr_gid;
1213 public string[] gr_mem;
1215 public override int GetHashCode ()
1218 for (int i = 0; i < gr_mem.Length; ++i)
1219 memhc ^= gr_mem[i].GetHashCode ();
1221 return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^
1222 gr_gid.GetHashCode () ^ memhc;
1225 public override bool Equals (object obj)
1227 if (obj == null || GetType() != obj.GetType())
1229 Group g = (Group) obj;
1233 public bool Equals (Group value)
1237 if (value.gr_gid != gr_gid)
1239 if (value.gr_gid == gr_gid && value.gr_name == gr_name &&
1240 value.gr_passwd == gr_passwd) {
1241 if (value.gr_mem == gr_mem)
1243 if (value.gr_mem == null || gr_mem == null)
1245 if (value.gr_mem.Length != gr_mem.Length)
1247 for (int i = 0; i < gr_mem.Length; ++i)
1248 if (gr_mem[i] != value.gr_mem[i])
1255 // Generate string in /etc/group format
1256 public override string ToString ()
1258 StringBuilder sb = new StringBuilder ();
1259 sb.Append (gr_name).Append (":").Append (gr_passwd).Append (":");
1260 sb.Append (gr_gid).Append (":");
1261 GetMembers (sb, gr_mem);
1262 return sb.ToString ();
1265 private static void GetMembers (StringBuilder sb, string[] members)
1267 if (members.Length > 0)
1268 sb.Append (members[0]);
1269 for (int i = 1; i < members.Length; ++i) {
1271 sb.Append (members[i]);
1275 public static bool operator== (Group lhs, Group rhs)
1277 return Object.Equals (lhs, rhs);
1280 public static bool operator!= (Group lhs, Group rhs)
1282 return !Object.Equals (lhs, rhs);
1286 public sealed class Passwd
1288 : IEquatable <Passwd>
1291 public string pw_name;
1292 public string pw_passwd;
1293 [CLSCompliant (false)]
1294 public /* uid_t */ uint pw_uid;
1295 [CLSCompliant (false)]
1296 public /* gid_t */ uint pw_gid;
1297 public string pw_gecos;
1298 public string pw_dir;
1299 public string pw_shell;
1301 public override int GetHashCode ()
1303 return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^
1304 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
1305 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
1306 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
1309 public override bool Equals (object obj)
1311 if (obj == null || GetType() != obj.GetType())
1313 Passwd p = (Passwd) obj;
1317 public bool Equals (Passwd value)
1321 return value.pw_uid == pw_uid && value.pw_gid == pw_gid &&
1322 value.pw_name == pw_name && value.pw_passwd == pw_passwd &&
1323 value.pw_gecos == pw_gecos && value.pw_dir == pw_dir &&
1324 value.pw_shell == pw_shell;
1327 // Generate string in /etc/passwd format
1328 public override string ToString ()
1330 return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
1331 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
1334 public static bool operator== (Passwd lhs, Passwd rhs)
1336 return Object.Equals (lhs, rhs);
1339 public static bool operator!= (Passwd lhs, Passwd rhs)
1341 return !Object.Equals (lhs, rhs);
1346 // Convention: Functions *not* part of the standard C library AND part of
1347 // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
1349 // For example, the man page should be similar to:
1351 // CONFORMING TO (or CONFORMS TO)
1352 // XPG2, SUSv2, POSIX, etc.
1354 // BSD- and GNU-specific exports can also be placed here.
1356 // Non-POSIX/XPG/etc. functions can also be placed here if:
1357 // (a) They'd be likely to be covered in a Steven's-like book
1358 // (b) The functions would be present in libc.so (or equivalent).
1360 // If a function has its own library, that's a STRONG indicator that the
1361 // function should get a different binding, probably in its own assembly,
1362 // so that package management can work sanely. (That is, we'd like to avoid
1363 // scenarios where FooLib.dll is installed, but it requires libFooLib.so to
1364 // run, and libFooLib.so doesn't exist. That would be confusing.)
1366 // The only methods in here should be:
1367 // (1) low-level functions
1368 // (2) "Trivial" function overloads. For example, if the parameters to a
1369 // function are related (e.g. getgroups(2))
1370 // (3) The return type SHOULD NOT be changed. If you want to provide a
1371 // convenience function with a nicer return type, place it into one of
1372 // the Mono.Unix.Unix* wrapper classes, and give it a .NET-styled name.
1373 // - EXCEPTION: No public functions should have a `void' return type.
1374 // `void' return types should be replaced with `int'.
1375 // Rationality: `void'-return functions typically require a
1376 // complicated call sequence, such as clear errno, then call, then
1377 // check errno to see if any errors occurred. This sequence can't
1378 // be done safely in managed code, as errno may change as part of
1379 // the P/Invoke mechanism.
1380 // Instead, add a MonoPosixHelper export which does:
1383 // return errno == 0 ? 0 : -1;
1384 // This lets managed code check the return value in the usual manner.
1385 // (4) Exceptions SHOULD NOT be thrown. EXCEPTIONS:
1386 // - If you're wrapping *broken* methods which make assumptions about
1387 // input data, such as that an argument refers to N bytes of data.
1388 // This is currently limited to cuserid(3) and encrypt(3).
1389 // - If you call functions which themselves generate exceptions.
1390 // This is the case for using NativeConvert, which will throw an
1391 // exception if an invalid/unsupported value is used.
1393 // Naming Conventions:
1394 // - Syscall method names should have the same name as the function being
1395 // wrapped (e.g. Syscall.read ==> read(2)). This allows people to
1396 // consult the appropriate man page if necessary.
1397 // - Methods need not have the same arguments IF this simplifies or
1398 // permits correct usage. The current example is syslog, in which
1399 // syslog(3)'s single `priority' argument is split into SyslogFacility
1400 // and SyslogLevel arguments.
1401 // - Type names (structures, classes, enumerations) are always PascalCased.
1402 // - Enumerations are named as <MethodName><ArgumentName>, and are located
1403 // in the Mono.Unix.Native namespace. For readability, if ArgumentName
1404 // is "cmd", use Command instead. For example, fcntl(2) takes a
1405 // FcntlCommand argument. This naming convention is to provide an
1406 // assocation between an enumeration and where it should be used, and
1407 // allows a single method to accept multiple different enumerations
1408 // (see mmap(2), which takes MmapProts and MmapFlags).
1409 // - EXCEPTION: if an enumeration is shared between multiple different
1410 // methods, AND/OR the "obvious" enumeration name conflicts with an
1411 // existing .NET type, a more appropriate name should be used.
1412 // Example: FilePermissions
1413 // - EXCEPTION: [Flags] enumerations should get plural names to follow
1414 // .NET name guidelines. Usually this doesn't result in a change
1415 // (OpenFlags is the `flags' parameter for open(2)), but it can
1416 // (mmap(2) prot ==> MmapProts, access(2) mode ==> AccessModes).
1417 // - Enumerations should have the [Map] and (optional) [Flags] attributes.
1418 // [Map] is required for make-map to find the type and generate the
1419 // appropriate NativeConvert conversion functions.
1420 // - Enumeration contents should match the original Unix names. This helps
1421 // with documentation (the existing man pages are still useful), and is
1422 // required for use with the make-map generation program.
1423 // - Structure names should be the PascalCased version of the actual
1424 // structure name (struct flock ==> Flock). Structure members should
1425 // have the same names, or a (reasonably) portable subset (Dirent being
1426 // the poster child for questionable members).
1427 // - Whether the managed type should be a reference type (class) or a
1428 // value type (struct) should be determined on a case-by-case basis:
1429 // if you ever need to be able to use NULL for it (such as with Dirent,
1430 // Group, Passwd, as these are method return types and `null' is used
1431 // to signify the end), it should be a reference type; otherwise, use
1432 // your discretion, and keep any expected usage patterns in mind.
1433 // - Syscall should be a Single Point Of Truth (SPOT). There should be
1434 // only ONE way to do anything. By convention, the Linux function names
1435 // are used, but that need not always be the case (use your discretion).
1436 // It SHOULD NOT be required that developers know what platform they're
1437 // on, and choose among a set of similar functions. In short, anything
1438 // that requires a platform check is BAD -- Mono.Unix is a wrapper, and
1439 // we can afford to clean things up whenever possible.
1441 // - Syscall.statfs: Solaris/Mac OS X provide statfs(2), Linux provides
1442 // statvfs(2). MonoPosixHelper will "thunk" between the two,
1443 // exporting a statvfs that works across platforms.
1444 // - Syscall.getfsent: Glibc export which Solaris lacks, while Solaris
1445 // instead provides getvfsent(3). MonoPosixHelper provides wrappers
1446 // to convert getvfsent(3) into Fstab data.
1447 // - Exception: If it isn't possible to cleanly wrap platforms, then the
1448 // method shouldn't be exported. The user will be expected to do their
1449 // own platform check and their own DllImports.
1450 // Examples: mount(2), umount(2), etc.
1451 // - Note: if a platform doesn't support a function AT ALL, the
1452 // MonoPosixHelper wrapper won't be compiled, resulting in a
1453 // EntryPointNotFoundException. This is also consistent with a missing
1454 // P/Invoke into libc.so.
1456 [CLSCompliant (false)]
1457 public sealed class Syscall : Stdlib
1459 new internal const string LIBC = "libc";
1461 private Syscall () {}
1467 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3),
1468 // aio_return(3), aio_suspend(3), aio_write(3)
1470 // Then update UnixStream.BeginRead to use the aio* functions.
1473 #region <attr/xattr.h> Declarations
1475 // <attr/xattr.h> -- COMPLETE
1479 // int setxattr (const char *path, const char *name,
1480 // const void *value, size_t size, int flags);
1481 [DllImport (MPH, SetLastError=true,
1482 EntryPoint="Mono_Posix_Syscall_setxattr")]
1483 public static extern int setxattr (
1484 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1486 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1487 string name, byte[] value, ulong size, XattrFlags flags);
1489 public static int setxattr (string path, string name, byte [] value, ulong size)
1491 return setxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1494 public static int setxattr (string path, string name, byte [] value, XattrFlags flags)
1496 return setxattr (path, name, value, (ulong) value.Length, flags);
1499 public static int setxattr (string path, string name, byte [] value)
1501 return setxattr (path, name, value, (ulong) value.Length);
1505 // int lsetxattr (const char *path, const char *name,
1506 // const void *value, size_t size, int flags);
1507 [DllImport (MPH, SetLastError=true,
1508 EntryPoint="Mono_Posix_Syscall_lsetxattr")]
1509 public static extern int lsetxattr (
1510 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1512 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1513 string name, byte[] value, ulong size, XattrFlags flags);
1515 public static int lsetxattr (string path, string name, byte [] value, ulong size)
1517 return lsetxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1520 public static int lsetxattr (string path, string name, byte [] value, XattrFlags flags)
1522 return lsetxattr (path, name, value, (ulong) value.Length, flags);
1525 public static int lsetxattr (string path, string name, byte [] value)
1527 return lsetxattr (path, name, value, (ulong) value.Length);
1531 // int fsetxattr (int fd, const char *name,
1532 // const void *value, size_t size, int flags);
1533 [DllImport (MPH, SetLastError=true,
1534 EntryPoint="Mono_Posix_Syscall_fsetxattr")]
1535 public static extern int fsetxattr (int fd,
1536 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1537 string name, byte[] value, ulong size, XattrFlags flags);
1539 public static int fsetxattr (int fd, string name, byte [] value, ulong size)
1541 return fsetxattr (fd, name, value, size, XattrFlags.XATTR_AUTO);
1544 public static int fsetxattr (int fd, string name, byte [] value, XattrFlags flags)
1546 return fsetxattr (fd, name, value, (ulong) value.Length, flags);
1549 public static int fsetxattr (int fd, string name, byte [] value)
1551 return fsetxattr (fd, name, value, (ulong) value.Length);
1555 // ssize_t getxattr (const char *path, const char *name,
1556 // void *value, size_t size);
1557 [DllImport (MPH, SetLastError=true,
1558 EntryPoint="Mono_Posix_Syscall_getxattr")]
1559 public static extern long getxattr (
1560 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1562 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1563 string name, byte[] value, ulong size);
1565 public static long getxattr (string path, string name, byte [] value)
1567 return getxattr (path, name, value, (ulong) value.Length);
1570 public static long getxattr (string path, string name, out byte [] value)
1573 long size = getxattr (path, name, value, 0);
1577 value = new byte [size];
1578 return getxattr (path, name, value, (ulong) size);
1582 // ssize_t lgetxattr (const char *path, const char *name,
1583 // void *value, size_t size);
1584 [DllImport (MPH, SetLastError=true,
1585 EntryPoint="Mono_Posix_Syscall_lgetxattr")]
1586 public static extern long lgetxattr (
1587 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1589 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1590 string name, byte[] value, ulong size);
1592 public static long lgetxattr (string path, string name, byte [] value)
1594 return lgetxattr (path, name, value, (ulong) value.Length);
1597 public static long lgetxattr (string path, string name, out byte [] value)
1600 long size = lgetxattr (path, name, value, 0);
1604 value = new byte [size];
1605 return lgetxattr (path, name, value, (ulong) size);
1609 // ssize_t fgetxattr (int fd, const char *name, void *value, size_t size);
1610 [DllImport (MPH, SetLastError=true,
1611 EntryPoint="Mono_Posix_Syscall_fgetxattr")]
1612 public static extern long fgetxattr (int fd,
1613 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1614 string name, byte[] value, ulong size);
1616 public static long fgetxattr (int fd, string name, byte [] value)
1618 return fgetxattr (fd, name, value, (ulong) value.Length);
1621 public static long fgetxattr (int fd, string name, out byte [] value)
1624 long size = fgetxattr (fd, name, value, 0);
1628 value = new byte [size];
1629 return fgetxattr (fd, name, value, (ulong) size);
1633 // ssize_t listxattr (const char *path, char *list, size_t size);
1634 [DllImport (MPH, SetLastError=true,
1635 EntryPoint="Mono_Posix_Syscall_listxattr")]
1636 public static extern long listxattr (
1637 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1638 string path, byte[] list, ulong size);
1640 // Slight modification: returns 0 on success, negative on error
1641 public static long listxattr (string path, Encoding encoding, out string [] values)
1644 long size = listxattr (path, null, 0);
1646 values = new string [0];
1650 byte[] list = new byte [size];
1651 long ret = listxattr (path, list, (ulong) size);
1655 GetValues (list, encoding, out values);
1659 public static long listxattr (string path, out string[] values)
1661 return listxattr (path, UnixEncoding.Instance, out values);
1664 private static void GetValues (byte[] list, Encoding encoding, out string[] values)
1667 for (int i = 0; i < list.Length; ++i)
1671 values = new string [num_values];
1674 for (int i = 0; i < list.Length; ++i) {
1675 if (list [i] == 0) {
1676 values [num_values++] = encoding.GetString (list, str_start, i - str_start);
1683 // ssize_t llistxattr (const char *path, char *list, size_t size);
1684 [DllImport (MPH, SetLastError=true,
1685 EntryPoint="Mono_Posix_Syscall_llistxattr")]
1686 public static extern long llistxattr (
1687 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1688 string path, byte[] list, ulong size);
1690 // Slight modification: returns 0 on success, negative on error
1691 public static long llistxattr (string path, Encoding encoding, out string [] values)
1694 long size = llistxattr (path, null, 0);
1696 values = new string [0];
1700 byte[] list = new byte [size];
1701 long ret = llistxattr (path, list, (ulong) size);
1705 GetValues (list, encoding, out values);
1709 public static long llistxattr (string path, out string[] values)
1711 return llistxattr (path, UnixEncoding.Instance, out values);
1715 // ssize_t flistxattr (int fd, char *list, size_t size);
1716 [DllImport (MPH, SetLastError=true,
1717 EntryPoint="Mono_Posix_Syscall_flistxattr")]
1718 public static extern long flistxattr (int fd, byte[] list, ulong size);
1720 // Slight modification: returns 0 on success, negative on error
1721 public static long flistxattr (int fd, Encoding encoding, out string [] values)
1724 long size = flistxattr (fd, null, 0);
1726 values = new string [0];
1730 byte[] list = new byte [size];
1731 long ret = flistxattr (fd, list, (ulong) size);
1735 GetValues (list, encoding, out values);
1739 public static long flistxattr (int fd, out string[] values)
1741 return flistxattr (fd, UnixEncoding.Instance, out values);
1744 [DllImport (MPH, SetLastError=true,
1745 EntryPoint="Mono_Posix_Syscall_removexattr")]
1746 public static extern int removexattr (
1747 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1749 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1752 [DllImport (MPH, SetLastError=true,
1753 EntryPoint="Mono_Posix_Syscall_lremovexattr")]
1754 public static extern int lremovexattr (
1755 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1757 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1760 [DllImport (MPH, SetLastError=true,
1761 EntryPoint="Mono_Posix_Syscall_fremovexattr")]
1762 public static extern int fremovexattr (int fd,
1763 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1767 #region <dirent.h> Declarations
1771 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1773 [DllImport (LIBC, SetLastError=true)]
1774 public static extern IntPtr opendir (
1775 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1778 [DllImport (LIBC, SetLastError=true)]
1779 public static extern int closedir (IntPtr dir);
1782 // void seekdir (DIR *dir, off_t offset);
1783 // Slight modification. Returns -1 on error, 0 on success.
1784 [DllImport (MPH, SetLastError=true,
1785 EntryPoint="Mono_Posix_Syscall_seekdir")]
1786 public static extern int seekdir (IntPtr dir, long offset);
1789 // off_t telldir(DIR *dir);
1790 [DllImport (MPH, SetLastError=true,
1791 EntryPoint="Mono_Posix_Syscall_telldir")]
1792 public static extern long telldir (IntPtr dir);
1794 [DllImport (MPH, SetLastError=true,
1795 EntryPoint="Mono_Posix_Syscall_rewinddir")]
1796 public static extern int rewinddir (IntPtr dir);
1798 private struct _Dirent {
1799 [ino_t] public ulong d_ino;
1800 [off_t] public long d_off;
1801 public ushort d_reclen;
1803 public IntPtr d_name;
1806 private static void CopyDirent (Dirent to, ref _Dirent from)
1809 to.d_ino = from.d_ino;
1810 to.d_off = from.d_off;
1811 to.d_reclen = from.d_reclen;
1812 to.d_type = from.d_type;
1813 to.d_name = UnixMarshal.PtrToString (from.d_name);
1816 Stdlib.free (from.d_name);
1817 from.d_name = IntPtr.Zero;
1821 internal static object readdir_lock = new object ();
1823 [DllImport (MPH, SetLastError=true,
1824 EntryPoint="Mono_Posix_Syscall_readdir")]
1825 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
1827 public static Dirent readdir (IntPtr dir)
1831 lock (readdir_lock) {
1832 r = sys_readdir (dir, out dentry);
1836 Dirent d = new Dirent ();
1837 CopyDirent (d, ref dentry);
1841 [DllImport (MPH, SetLastError=true,
1842 EntryPoint="Mono_Posix_Syscall_readdir_r")]
1843 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
1845 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
1851 entry.d_name = null;
1854 int r = sys_readdir_r (dirp, out _d, out result);
1856 if (r == 0 && result != IntPtr.Zero) {
1857 CopyDirent (entry, ref _d);
1863 [DllImport (LIBC, SetLastError=true)]
1864 public static extern int dirfd (IntPtr dir);
1867 #region <fcntl.h> Declarations
1869 // <fcntl.h> -- COMPLETE
1872 [DllImport (MPH, SetLastError=true,
1873 EntryPoint="Mono_Posix_Syscall_fcntl")]
1874 public static extern int fcntl (int fd, FcntlCommand cmd);
1876 [DllImport (MPH, SetLastError=true,
1877 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
1878 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
1880 public static int fcntl (int fd, FcntlCommand cmd, DirectoryNotifyFlags arg)
1882 if (cmd != FcntlCommand.F_NOTIFY) {
1883 SetLastError (Errno.EINVAL);
1886 long _arg = NativeConvert.FromDirectoryNotifyFlags (arg);
1887 return fcntl (fd, FcntlCommand.F_NOTIFY, _arg);
1890 [DllImport (MPH, SetLastError=true,
1891 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
1892 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
1894 [DllImport (MPH, SetLastError=true,
1895 EntryPoint="Mono_Posix_Syscall_open")]
1896 public static extern int open (
1897 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1898 string pathname, OpenFlags flags);
1901 // int open(const char *pathname, int flags, mode_t mode);
1902 [DllImport (MPH, SetLastError=true,
1903 EntryPoint="Mono_Posix_Syscall_open_mode")]
1904 public static extern int open (
1905 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1906 string pathname, OpenFlags flags, FilePermissions mode);
1909 // int creat(const char *pathname, mode_t mode);
1910 [DllImport (MPH, SetLastError=true,
1911 EntryPoint="Mono_Posix_Syscall_creat")]
1912 public static extern int creat (
1913 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1914 string pathname, FilePermissions mode);
1917 // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
1918 [DllImport (MPH, SetLastError=true,
1919 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
1920 public static extern int posix_fadvise (int fd, long offset,
1921 long len, PosixFadviseAdvice advice);
1923 // posix_fallocate(P)
1924 // int posix_fallocate(int fd, off_t offset, size_t len);
1925 [DllImport (MPH, SetLastError=true,
1926 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
1927 public static extern int posix_fallocate (int fd, long offset, ulong len);
1930 #region <fstab.h> Declarations
1932 // <fstab.h> -- COMPLETE
1935 private struct _Fstab {
1936 public IntPtr fs_spec;
1937 public IntPtr fs_file;
1938 public IntPtr fs_vfstype;
1939 public IntPtr fs_mntops;
1940 public IntPtr fs_type;
1942 public int fs_passno;
1943 public IntPtr _fs_buf_;
1946 private static void CopyFstab (Fstab to, ref _Fstab from)
1949 to.fs_spec = UnixMarshal.PtrToString (from.fs_spec);
1950 to.fs_file = UnixMarshal.PtrToString (from.fs_file);
1951 to.fs_vfstype = UnixMarshal.PtrToString (from.fs_vfstype);
1952 to.fs_mntops = UnixMarshal.PtrToString (from.fs_mntops);
1953 to.fs_type = UnixMarshal.PtrToString (from.fs_type);
1954 to.fs_freq = from.fs_freq;
1955 to.fs_passno = from.fs_passno;
1958 Stdlib.free (from._fs_buf_);
1959 from._fs_buf_ = IntPtr.Zero;
1963 internal static object fstab_lock = new object ();
1965 [DllImport (MPH, SetLastError=true,
1966 EntryPoint="Mono_Posix_Syscall_endfsent")]
1967 private static extern int sys_endfsent ();
1969 public static int endfsent ()
1972 return sys_endfsent ();
1976 [DllImport (MPH, SetLastError=true,
1977 EntryPoint="Mono_Posix_Syscall_getfsent")]
1978 private static extern int sys_getfsent (out _Fstab fs);
1980 public static Fstab getfsent ()
1985 r = sys_getfsent (out fsbuf);
1989 Fstab fs = new Fstab ();
1990 CopyFstab (fs, ref fsbuf);
1994 [DllImport (MPH, SetLastError=true,
1995 EntryPoint="Mono_Posix_Syscall_getfsfile")]
1996 private static extern int sys_getfsfile (
1997 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1998 string mount_point, out _Fstab fs);
2000 public static Fstab getfsfile (string mount_point)
2005 r = sys_getfsfile (mount_point, out fsbuf);
2009 Fstab fs = new Fstab ();
2010 CopyFstab (fs, ref fsbuf);
2014 [DllImport (MPH, SetLastError=true,
2015 EntryPoint="Mono_Posix_Syscall_getfsspec")]
2016 private static extern int sys_getfsspec (
2017 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2018 string special_file, out _Fstab fs);
2020 public static Fstab getfsspec (string special_file)
2025 r = sys_getfsspec (special_file, out fsbuf);
2029 Fstab fs = new Fstab ();
2030 CopyFstab (fs, ref fsbuf);
2034 [DllImport (MPH, SetLastError=true,
2035 EntryPoint="Mono_Posix_Syscall_setfsent")]
2036 private static extern int sys_setfsent ();
2038 public static int setfsent ()
2041 return sys_setfsent ();
2047 #region <grp.h> Declarations
2051 // TODO: putgrent(3), fgetgrent_r(), getgrouplist(2), initgroups(3)
2054 // int setgroups (size_t size, const gid_t *list);
2055 [DllImport (MPH, SetLastError=true,
2056 EntryPoint="Mono_Posix_Syscall_setgroups")]
2057 public static extern int setgroups (ulong size, uint[] list);
2059 public static int setgroups (uint [] list)
2061 return setgroups ((ulong) list.Length, list);
2065 private struct _Group
2067 public IntPtr gr_name;
2068 public IntPtr gr_passwd;
2069 [gid_t] public uint gr_gid;
2070 public int _gr_nmem_;
2071 public IntPtr gr_mem;
2072 public IntPtr _gr_buf_;
2075 private static void CopyGroup (Group to, ref _Group from)
2078 to.gr_gid = from.gr_gid;
2079 to.gr_name = UnixMarshal.PtrToString (from.gr_name);
2080 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
2081 to.gr_mem = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
2084 Stdlib.free (from.gr_mem);
2085 Stdlib.free (from._gr_buf_);
2086 from.gr_mem = IntPtr.Zero;
2087 from._gr_buf_ = IntPtr.Zero;
2091 internal static object grp_lock = new object ();
2093 [DllImport (MPH, SetLastError=true,
2094 EntryPoint="Mono_Posix_Syscall_getgrnam")]
2095 private static extern int sys_getgrnam (string name, out _Group group);
2097 public static Group getgrnam (string name)
2102 r = sys_getgrnam (name, out group);
2106 Group gr = new Group ();
2107 CopyGroup (gr, ref group);
2112 // struct group *getgrgid(gid_t gid);
2113 [DllImport (MPH, SetLastError=true,
2114 EntryPoint="Mono_Posix_Syscall_getgrgid")]
2115 private static extern int sys_getgrgid (uint uid, out _Group group);
2117 public static Group getgrgid (uint uid)
2122 r = sys_getgrgid (uid, out group);
2126 Group gr = new Group ();
2127 CopyGroup (gr, ref group);
2131 [DllImport (MPH, SetLastError=true,
2132 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
2133 private static extern int sys_getgrnam_r (
2134 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2135 string name, out _Group grbuf, out IntPtr grbufp);
2137 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
2142 int r = sys_getgrnam_r (name, out group, out _grbufp);
2143 if (r == 0 && _grbufp != IntPtr.Zero) {
2144 CopyGroup (grbuf, ref group);
2151 // int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
2152 // size_t buflen, struct group **gbufp);
2153 [DllImport (MPH, SetLastError=true,
2154 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
2155 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
2157 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
2162 int r = sys_getgrgid_r (uid, out group, out _grbufp);
2163 if (r == 0 && _grbufp != IntPtr.Zero) {
2164 CopyGroup (grbuf, ref group);
2170 [DllImport (MPH, SetLastError=true,
2171 EntryPoint="Mono_Posix_Syscall_getgrent")]
2172 private static extern int sys_getgrent (out _Group grbuf);
2174 public static Group getgrent ()
2179 r = sys_getgrent (out group);
2183 Group gr = new Group();
2184 CopyGroup (gr, ref group);
2188 [DllImport (MPH, SetLastError=true,
2189 EntryPoint="Mono_Posix_Syscall_setgrent")]
2190 private static extern int sys_setgrent ();
2192 public static int setgrent ()
2195 return sys_setgrent ();
2199 [DllImport (MPH, SetLastError=true,
2200 EntryPoint="Mono_Posix_Syscall_endgrent")]
2201 private static extern int sys_endgrent ();
2203 public static int endgrent ()
2206 return sys_endgrent ();
2210 [DllImport (MPH, SetLastError=true,
2211 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
2212 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
2214 public static Group fgetgrent (IntPtr stream)
2219 r = sys_fgetgrent (stream, out group);
2223 Group gr = new Group ();
2224 CopyGroup (gr, ref group);
2229 #region <pwd.h> Declarations
2233 // TODO: putpwent(3), fgetpwent_r()
2235 // SKIPPING: getpw(3): it's dangerous. Use getpwuid(3) instead.
2238 private struct _Passwd
2240 public IntPtr pw_name;
2241 public IntPtr pw_passwd;
2242 [uid_t] public uint pw_uid;
2243 [gid_t] public uint pw_gid;
2244 public IntPtr pw_gecos;
2245 public IntPtr pw_dir;
2246 public IntPtr pw_shell;
2247 public IntPtr _pw_buf_;
2250 private static void CopyPasswd (Passwd to, ref _Passwd from)
2253 to.pw_name = UnixMarshal.PtrToString (from.pw_name);
2254 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
2255 to.pw_uid = from.pw_uid;
2256 to.pw_gid = from.pw_gid;
2257 to.pw_gecos = UnixMarshal.PtrToString (from.pw_gecos);
2258 to.pw_dir = UnixMarshal.PtrToString (from.pw_dir);
2259 to.pw_shell = UnixMarshal.PtrToString (from.pw_shell);
2262 Stdlib.free (from._pw_buf_);
2263 from._pw_buf_ = IntPtr.Zero;
2267 internal static object pwd_lock = new object ();
2269 [DllImport (MPH, SetLastError=true,
2270 EntryPoint="Mono_Posix_Syscall_getpwnam")]
2271 private static extern int sys_getpwnam (string name, out _Passwd passwd);
2273 public static Passwd getpwnam (string name)
2278 r = sys_getpwnam (name, out passwd);
2282 Passwd pw = new Passwd ();
2283 CopyPasswd (pw, ref passwd);
2288 // struct passwd *getpwnuid(uid_t uid);
2289 [DllImport (MPH, SetLastError=true,
2290 EntryPoint="Mono_Posix_Syscall_getpwuid")]
2291 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
2293 public static Passwd getpwuid (uint uid)
2298 r = sys_getpwuid (uid, out passwd);
2302 Passwd pw = new Passwd ();
2303 CopyPasswd (pw, ref passwd);
2307 [DllImport (MPH, SetLastError=true,
2308 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
2309 private static extern int sys_getpwnam_r (
2310 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2311 string name, out _Passwd pwbuf, out IntPtr pwbufp);
2313 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
2318 int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
2319 if (r == 0 && _pwbufp != IntPtr.Zero) {
2320 CopyPasswd (pwbuf, ref passwd);
2327 // int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
2328 // buflen, struct passwd **pwbufp);
2329 [DllImport (MPH, SetLastError=true,
2330 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
2331 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
2333 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
2338 int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
2339 if (r == 0 && _pwbufp != IntPtr.Zero) {
2340 CopyPasswd (pwbuf, ref passwd);
2346 [DllImport (MPH, SetLastError=true,
2347 EntryPoint="Mono_Posix_Syscall_getpwent")]
2348 private static extern int sys_getpwent (out _Passwd pwbuf);
2350 public static Passwd getpwent ()
2355 r = sys_getpwent (out passwd);
2359 Passwd pw = new Passwd ();
2360 CopyPasswd (pw, ref passwd);
2364 [DllImport (MPH, SetLastError=true,
2365 EntryPoint="Mono_Posix_Syscall_setpwent")]
2366 private static extern int sys_setpwent ();
2368 public static int setpwent ()
2371 return sys_setpwent ();
2375 [DllImport (MPH, SetLastError=true,
2376 EntryPoint="Mono_Posix_Syscall_endpwent")]
2377 private static extern int sys_endpwent ();
2379 public static int endpwent ()
2382 return sys_endpwent ();
2386 [DllImport (MPH, SetLastError=true,
2387 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
2388 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
2390 public static Passwd fgetpwent (IntPtr stream)
2395 r = sys_fgetpwent (stream, out passwd);
2399 Passwd pw = new Passwd ();
2400 CopyPasswd (pw, ref passwd);
2405 #region <signal.h> Declarations
2409 [DllImport (MPH, SetLastError=true,
2410 EntryPoint="Mono_Posix_Syscall_psignal")]
2411 private static extern int psignal (int sig, string s);
2413 public static int psignal (Signum sig, string s)
2415 int signum = NativeConvert.FromSignum (sig);
2416 return psignal (signum, s);
2420 // int kill(pid_t pid, int sig);
2421 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
2422 private static extern int sys_kill (int pid, int sig);
2424 public static int kill (int pid, Signum sig)
2426 int _sig = NativeConvert.FromSignum (sig);
2427 return sys_kill (pid, _sig);
2430 private static object signal_lock = new object ();
2432 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
2433 private static extern IntPtr sys_strsignal (int sig);
2435 public static string strsignal (Signum sig)
2437 int s = NativeConvert.FromSignum (sig);
2438 lock (signal_lock) {
2439 IntPtr r = sys_strsignal (s);
2440 return UnixMarshal.PtrToString (r);
2444 // TODO: sigaction(2)
2445 // TODO: sigsuspend(2)
2446 // TODO: sigpending(2)
2450 #region <stdio.h> Declarations
2454 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
2455 private static extern int _L_ctermid ();
2457 public static readonly int L_ctermid = _L_ctermid ();
2459 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
2460 private static extern int _L_cuserid ();
2462 public static readonly int L_cuserid = _L_cuserid ();
2464 internal static object getlogin_lock = new object ();
2466 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
2467 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
2469 [Obsolete ("\"Nobody knows precisely what cuserid() does... " +
2470 "DO NOT USE cuserid().\n" +
2471 "`string' must hold L_cuserid characters. Use getlogin_r instead.")]
2472 public static string cuserid (StringBuilder @string)
2474 if (@string.Capacity < L_cuserid) {
2475 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
2477 lock (getlogin_lock) {
2478 IntPtr r = sys_cuserid (@string);
2479 return UnixMarshal.PtrToString (r);
2485 #region <stdlib.h> Declarations
2489 [DllImport (LIBC, SetLastError=true)]
2490 public static extern int mkstemp (StringBuilder template);
2492 [DllImport (LIBC, SetLastError=true)]
2493 public static extern int ttyslot ();
2495 [Obsolete ("This is insecure and should not be used", true)]
2496 public static int setkey (string key)
2498 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
2503 #region <string.h> Declarations
2509 // int strerror_r(int errnum, char *buf, size_t n);
2510 [DllImport (MPH, SetLastError=true,
2511 EntryPoint="Mono_Posix_Syscall_strerror_r")]
2512 private static extern int sys_strerror_r (int errnum,
2513 [Out] StringBuilder buf, ulong n);
2515 public static int strerror_r (Errno errnum, StringBuilder buf, ulong n)
2517 int e = NativeConvert.FromErrno (errnum);
2518 return sys_strerror_r (e, buf, n);
2521 public static int strerror_r (Errno errnum, StringBuilder buf)
2523 return strerror_r (errnum, buf, (ulong) buf.Capacity);
2528 #region <sys/mman.h> Declarations
2534 // int posix_madvise(void *addr, size_t len, int advice);
2535 [DllImport (MPH, SetLastError=true,
2536 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
2537 public static extern int posix_madvise (IntPtr addr, ulong len,
2538 PosixMadviseAdvice advice);
2540 public static readonly IntPtr MAP_FAILED = unchecked((IntPtr)(-1));
2542 [DllImport (MPH, SetLastError=true,
2543 EntryPoint="Mono_Posix_Syscall_mmap")]
2544 public static extern IntPtr mmap (IntPtr start, ulong length,
2545 MmapProts prot, MmapFlags flags, int fd, long offset);
2547 [DllImport (MPH, SetLastError=true,
2548 EntryPoint="Mono_Posix_Syscall_munmap")]
2549 public static extern int munmap (IntPtr start, ulong length);
2551 [DllImport (MPH, SetLastError=true,
2552 EntryPoint="Mono_Posix_Syscall_mprotect")]
2553 public static extern int mprotect (IntPtr start, ulong len, MmapProts prot);
2555 [DllImport (MPH, SetLastError=true,
2556 EntryPoint="Mono_Posix_Syscall_msync")]
2557 public static extern int msync (IntPtr start, ulong len, MsyncFlags flags);
2559 [DllImport (MPH, SetLastError=true,
2560 EntryPoint="Mono_Posix_Syscall_mlock")]
2561 public static extern int mlock (IntPtr start, ulong len);
2563 [DllImport (MPH, SetLastError=true,
2564 EntryPoint="Mono_Posix_Syscall_munlock")]
2565 public static extern int munlock (IntPtr start, ulong len);
2567 [DllImport (LIBC, SetLastError=true, EntryPoint="mlockall")]
2568 private static extern int sys_mlockall (int flags);
2570 public static int mlockall (MlockallFlags flags)
2572 int _flags = NativeConvert.FromMlockallFlags (flags);
2573 return sys_mlockall (_flags);
2576 [DllImport (LIBC, SetLastError=true)]
2577 public static extern int munlockall ();
2579 [DllImport (MPH, SetLastError=true,
2580 EntryPoint="Mono_Posix_Syscall_mremap")]
2581 public static extern IntPtr mremap (IntPtr old_address, ulong old_size,
2582 ulong new_size, MremapFlags flags);
2584 [DllImport (MPH, SetLastError=true,
2585 EntryPoint="Mono_Posix_Syscall_mincore")]
2586 public static extern int mincore (IntPtr start, ulong length, byte[] vec);
2588 [DllImport (MPH, SetLastError=true,
2589 EntryPoint="Mono_Posix_Syscall_remap_file_pages")]
2590 public static extern int remap_file_pages (IntPtr start, ulong size,
2591 MmapProts prot, long pgoff, MmapFlags flags);
2595 #region <sys/poll.h> Declarations
2597 // <sys/poll.h> -- COMPLETE
2600 private struct _pollfd {
2602 public short events;
2603 public short revents;
2606 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
2607 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
2609 public static int poll (Pollfd [] fds, uint nfds, int timeout)
2611 if (fds.Length < nfds)
2612 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
2614 _pollfd[] send = new _pollfd[nfds];
2616 for (int i = 0; i < send.Length; i++) {
2617 send [i].fd = fds [i].fd;
2618 send [i].events = NativeConvert.FromPollEvents (fds [i].events);
2621 int r = sys_poll (send, nfds, timeout);
2623 for (int i = 0; i < send.Length; i++) {
2624 fds [i].revents = NativeConvert.ToPollEvents (send [i].revents);
2630 public static int poll (Pollfd [] fds, int timeout)
2632 return poll (fds, (uint) fds.Length, timeout);
2645 // TODO: setrlimit(2)
2646 // TODO: getrlimit(2)
2647 // TODO: getrusage(2)
2651 #region <sys/sendfile.h> Declarations
2653 // <sys/sendfile.h> -- COMPLETE
2656 [DllImport (MPH, SetLastError=true,
2657 EntryPoint="Mono_Posix_Syscall_sendfile")]
2658 public static extern long sendfile (int out_fd, int in_fd,
2659 ref long offset, ulong count);
2663 #region <sys/stat.h> Declarations
2665 // <sys/stat.h> -- COMPLETE
2667 [DllImport (MPH, SetLastError=true,
2668 EntryPoint="Mono_Posix_Syscall_stat")]
2669 public static extern int stat (
2670 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2671 string file_name, out Stat buf);
2673 [DllImport (MPH, SetLastError=true,
2674 EntryPoint="Mono_Posix_Syscall_fstat")]
2675 public static extern int fstat (int filedes, out Stat buf);
2677 [DllImport (MPH, SetLastError=true,
2678 EntryPoint="Mono_Posix_Syscall_lstat")]
2679 public static extern int lstat (
2680 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2681 string file_name, out Stat buf);
2684 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
2685 // All take FilePermissions
2688 // int chmod(const char *path, mode_t mode);
2689 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
2690 private static extern int sys_chmod (
2691 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2692 string path, uint mode);
2694 public static int chmod (string path, FilePermissions mode)
2696 uint _mode = NativeConvert.FromFilePermissions (mode);
2697 return sys_chmod (path, _mode);
2701 // int chmod(int filedes, mode_t mode);
2702 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
2703 private static extern int sys_fchmod (int filedes, uint mode);
2705 public static int fchmod (int filedes, FilePermissions mode)
2707 uint _mode = NativeConvert.FromFilePermissions (mode);
2708 return sys_fchmod (filedes, _mode);
2712 // mode_t umask(mode_t mask);
2713 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
2714 private static extern uint sys_umask (uint mask);
2716 public static FilePermissions umask (FilePermissions mask)
2718 uint _mask = NativeConvert.FromFilePermissions (mask);
2719 uint r = sys_umask (_mask);
2720 return NativeConvert.ToFilePermissions (r);
2724 // int mkdir(const char *pathname, mode_t mode);
2725 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
2726 private static extern int sys_mkdir (
2727 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2728 string oldpath, uint mode);
2730 public static int mkdir (string oldpath, FilePermissions mode)
2732 uint _mode = NativeConvert.FromFilePermissions (mode);
2733 return sys_mkdir (oldpath, _mode);
2737 // int mknod (const char *pathname, mode_t mode, dev_t dev);
2738 [DllImport (MPH, SetLastError=true,
2739 EntryPoint="Mono_Posix_Syscall_mknod")]
2740 public static extern int mknod (
2741 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2742 string pathname, FilePermissions mode, ulong dev);
2745 // int mkfifo(const char *pathname, mode_t mode);
2746 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
2747 private static extern int sys_mkfifo (
2748 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2749 string pathname, uint mode);
2751 public static int mkfifo (string pathname, FilePermissions mode)
2753 uint _mode = NativeConvert.FromFilePermissions (mode);
2754 return sys_mkfifo (pathname, _mode);
2759 #region <sys/stat.h> Declarations
2764 [DllImport (MPH, SetLastError=true,
2765 EntryPoint="Mono_Posix_Syscall_statvfs")]
2766 public static extern int statvfs (
2767 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2768 string path, out Statvfs buf);
2770 [DllImport (MPH, SetLastError=true,
2771 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
2772 public static extern int fstatvfs (int fd, out Statvfs buf);
2776 #region <sys/time.h> Declarations
2780 // TODO: adjtime(), getitimer(2), setitimer(2)
2782 [DllImport (MPH, SetLastError=true,
2783 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2784 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
2786 [DllImport (MPH, SetLastError=true,
2787 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2788 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
2790 public static int gettimeofday (out Timeval tv)
2792 return gettimeofday (out tv, IntPtr.Zero);
2795 [DllImport (MPH, SetLastError=true,
2796 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2797 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
2799 public static int gettimeofday (out Timezone tz)
2801 return gettimeofday (IntPtr.Zero, out tz);
2804 [DllImport (MPH, SetLastError=true,
2805 EntryPoint="Mono_Posix_Syscall_settimeofday")]
2806 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
2808 [DllImport (MPH, SetLastError=true,
2809 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2810 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
2812 public static int settimeofday (ref Timeval tv)
2814 return settimeofday (ref tv, IntPtr.Zero);
2817 [DllImport (MPH, SetLastError=true,
2818 EntryPoint="Mono_Posix_Syscall_utimes")]
2819 private static extern int sys_utimes (
2820 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2821 string filename, Timeval[] tvp);
2823 public static int utimes (string filename, Timeval[] tvp)
2825 if (tvp != null && tvp.Length != 2) {
2826 SetLastError (Errno.EINVAL);
2829 return sys_utimes (filename, tvp);
2832 [DllImport (MPH, SetLastError=true,
2833 EntryPoint="Mono_Posix_Syscall_lutimes")]
2834 private static extern int sys_lutimes (
2835 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2836 string filename, Timeval[] tvp);
2838 public static int lutimes (string filename, Timeval[] tvp)
2840 if (tvp != null && tvp.Length != 2) {
2841 SetLastError (Errno.EINVAL);
2844 return sys_lutimes (filename, tvp);
2847 [DllImport (MPH, SetLastError=true,
2848 EntryPoint="Mono_Posix_Syscall_futimes")]
2849 private static extern int sys_futimes (int fd, Timeval[] tvp);
2851 public static int futimes (int fd, Timeval[] tvp)
2853 if (tvp != null && tvp.Length != 2) {
2854 SetLastError (Errno.EINVAL);
2857 return sys_futimes (fd, tvp);
2880 #region <sys/wait.h> Declarations
2886 // pid_t wait(int *status);
2887 [DllImport (LIBC, SetLastError=true)]
2888 public static extern int wait (out int status);
2891 // pid_t waitpid(pid_t pid, int *status, int options);
2892 [DllImport (LIBC, SetLastError=true)]
2893 private static extern int waitpid (int pid, out int status, int options);
2895 public static int waitpid (int pid, out int status, WaitOptions options)
2897 int _options = NativeConvert.FromWaitOptions (options);
2898 return waitpid (pid, out status, _options);
2901 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
2902 private static extern int _WIFEXITED (int status);
2904 public static bool WIFEXITED (int status)
2906 return _WIFEXITED (status) != 0;
2909 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
2910 public static extern int WEXITSTATUS (int status);
2912 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
2913 private static extern int _WIFSIGNALED (int status);
2915 public static bool WIFSIGNALED (int status)
2917 return _WIFSIGNALED (status) != 0;
2920 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
2921 private static extern int _WTERMSIG (int status);
2923 public static Signum WTERMSIG (int status)
2925 int r = _WTERMSIG (status);
2926 return NativeConvert.ToSignum (r);
2929 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
2930 private static extern int _WIFSTOPPED (int status);
2932 public static bool WIFSTOPPED (int status)
2934 return _WIFSTOPPED (status) != 0;
2937 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
2938 private static extern int _WSTOPSIG (int status);
2940 public static Signum WSTOPSIG (int status)
2942 int r = _WSTOPSIG (status);
2943 return NativeConvert.ToSignum (r);
2952 #region <syslog.h> Declarations
2957 [DllImport (MPH, SetLastError=true,
2958 EntryPoint="Mono_Posix_Syscall_openlog")]
2959 private static extern int sys_openlog (IntPtr ident, int option, int facility);
2961 public static int openlog (IntPtr ident, SyslogOptions option,
2962 SyslogFacility defaultFacility)
2964 int _option = NativeConvert.FromSyslogOptions (option);
2965 int _facility = NativeConvert.FromSyslogFacility (defaultFacility);
2967 return sys_openlog (ident, _option, _facility);
2970 [DllImport (MPH, SetLastError=true,
2971 EntryPoint="Mono_Posix_Syscall_syslog")]
2972 private static extern int sys_syslog (int priority, string message);
2974 public static int syslog (SyslogFacility facility, SyslogLevel level, string message)
2976 int _facility = NativeConvert.FromSyslogFacility (facility);
2977 int _level = NativeConvert.FromSyslogLevel (level);
2978 return sys_syslog (_facility | _level, GetSyslogMessage (message));
2981 public static int syslog (SyslogLevel level, string message)
2983 int _level = NativeConvert.FromSyslogLevel (level);
2984 return sys_syslog (_level, GetSyslogMessage (message));
2987 private static string GetSyslogMessage (string message)
2989 return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
2992 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2993 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
2994 public static int syslog (SyslogFacility facility, SyslogLevel level,
2995 string format, params object[] parameters)
2997 int _facility = NativeConvert.FromSyslogFacility (facility);
2998 int _level = NativeConvert.FromSyslogLevel (level);
3000 object[] _parameters = new object[checked(parameters.Length+2)];
3001 _parameters [0] = _facility | _level;
3002 _parameters [1] = format;
3003 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3004 return (int) XPrintfFunctions.syslog (_parameters);
3007 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
3008 "Use syslog(SyslogLevel, string) instead.")]
3009 public static int syslog (SyslogLevel level, string format,
3010 params object[] parameters)
3012 int _level = NativeConvert.FromSyslogLevel (level);
3014 object[] _parameters = new object[checked(parameters.Length+2)];
3015 _parameters [0] = _level;
3016 _parameters [1] = format;
3017 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3018 return (int) XPrintfFunctions.syslog (_parameters);
3021 [DllImport (MPH, SetLastError=true,
3022 EntryPoint="Mono_Posix_Syscall_closelog")]
3023 public static extern int closelog ();
3025 [DllImport (LIBC, SetLastError=true, EntryPoint="setlogmask")]
3026 private static extern int sys_setlogmask (int mask);
3028 public static int setlogmask (SyslogLevel mask)
3030 int _mask = NativeConvert.FromSyslogLevel (mask);
3031 return sys_setlogmask (_mask);
3036 #region <time.h> Declarations
3043 // int stime(time_t *t);
3044 [DllImport (MPH, SetLastError=true,
3045 EntryPoint="Mono_Posix_Syscall_stime")]
3046 public static extern int stime (ref long t);
3049 // time_t time(time_t *t);
3050 [DllImport (MPH, SetLastError=true,
3051 EntryPoint="Mono_Posix_Syscall_time")]
3052 public static extern long time (out long t);
3062 #region <unistd.h> Declarations
3066 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
3067 // other TODOs listed below.
3069 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
3070 private static extern int sys_access (
3071 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3072 string pathname, int mode);
3074 public static int access (string pathname, AccessModes mode)
3076 int _mode = NativeConvert.FromAccessModes (mode);
3077 return sys_access (pathname, _mode);
3081 // off_t lseek(int filedes, off_t offset, int whence);
3082 [DllImport (MPH, SetLastError=true,
3083 EntryPoint="Mono_Posix_Syscall_lseek")]
3084 private static extern long sys_lseek (int fd, long offset, int whence);
3086 public static long lseek (int fd, long offset, SeekFlags whence)
3088 short _whence = NativeConvert.FromSeekFlags (whence);
3089 return sys_lseek (fd, offset, _whence);
3092 [DllImport (LIBC, SetLastError=true)]
3093 public static extern int close (int fd);
3096 // ssize_t read(int fd, void *buf, size_t count);
3097 [DllImport (MPH, SetLastError=true,
3098 EntryPoint="Mono_Posix_Syscall_read")]
3099 public static extern long read (int fd, IntPtr buf, ulong count);
3101 public static unsafe long read (int fd, void *buf, ulong count)
3103 return read (fd, (IntPtr) buf, count);
3107 // ssize_t write(int fd, const void *buf, size_t count);
3108 [DllImport (MPH, SetLastError=true,
3109 EntryPoint="Mono_Posix_Syscall_write")]
3110 public static extern long write (int fd, IntPtr buf, ulong count);
3112 public static unsafe long write (int fd, void *buf, ulong count)
3114 return write (fd, (IntPtr) buf, count);
3118 // ssize_t pread(int fd, void *buf, size_t count, off_t offset);
3119 [DllImport (MPH, SetLastError=true,
3120 EntryPoint="Mono_Posix_Syscall_pread")]
3121 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
3123 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
3125 return pread (fd, (IntPtr) buf, count, offset);
3129 // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
3130 [DllImport (MPH, SetLastError=true,
3131 EntryPoint="Mono_Posix_Syscall_pwrite")]
3132 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
3134 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
3136 return pwrite (fd, (IntPtr) buf, count, offset);
3139 [DllImport (MPH, SetLastError=true,
3140 EntryPoint="Mono_Posix_Syscall_pipe")]
3141 public static extern int pipe (out int reading, out int writing);
3143 public static int pipe (int[] filedes)
3145 if (filedes == null || filedes.Length != 2) {
3149 int reading, writing;
3150 int r = pipe (out reading, out writing);
3151 filedes[0] = reading;
3152 filedes[1] = writing;
3156 [DllImport (LIBC, SetLastError=true)]
3157 public static extern uint alarm (uint seconds);
3159 [DllImport (LIBC, SetLastError=true)]
3160 public static extern uint sleep (uint seconds);
3162 [DllImport (LIBC, SetLastError=true)]
3163 public static extern uint ualarm (uint usecs, uint interval);
3165 [DllImport (LIBC, SetLastError=true)]
3166 public static extern int pause ();
3169 // int chown(const char *path, uid_t owner, gid_t group);
3170 [DllImport (LIBC, SetLastError=true)]
3171 public static extern int chown (
3172 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3173 string path, uint owner, uint group);
3176 // int fchown(int fd, uid_t owner, gid_t group);
3177 [DllImport (LIBC, SetLastError=true)]
3178 public static extern int fchown (int fd, uint owner, uint group);
3181 // int lchown(const char *path, uid_t owner, gid_t group);
3182 [DllImport (LIBC, SetLastError=true)]
3183 public static extern int lchown (
3184 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3185 string path, uint owner, uint group);
3187 [DllImport (LIBC, SetLastError=true)]
3188 public static extern int chdir (
3189 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3192 [DllImport (LIBC, SetLastError=true)]
3193 public static extern int fchdir (int fd);
3196 // char *getcwd(char *buf, size_t size);
3197 [DllImport (MPH, SetLastError=true,
3198 EntryPoint="Mono_Posix_Syscall_getcwd")]
3199 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
3201 public static StringBuilder getcwd (StringBuilder buf)
3203 getcwd (buf, (ulong) buf.Capacity);
3207 // getwd(2) is deprecated; don't expose it.
3209 [DllImport (LIBC, SetLastError=true)]
3210 public static extern int dup (int fd);
3212 [DllImport (LIBC, SetLastError=true)]
3213 public static extern int dup2 (int fd, int fd2);
3215 // TODO: does Mono marshal arrays properly?
3216 [DllImport (LIBC, SetLastError=true)]
3217 public static extern int execve (
3218 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3219 string path, string[] argv, string[] envp);
3221 [DllImport (LIBC, SetLastError=true)]
3222 public static extern int fexecve (int fd, string[] argv, string[] envp);
3224 [DllImport (LIBC, SetLastError=true)]
3225 public static extern int execv (
3226 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3227 string path, string[] argv);
3229 // TODO: execle, execl, execlp
3230 [DllImport (LIBC, SetLastError=true)]
3231 public static extern int execvp (
3232 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3233 string path, string[] argv);
3235 [DllImport (LIBC, SetLastError=true)]
3236 public static extern int nice (int inc);
3238 [DllImport (LIBC, SetLastError=true)]
3239 [CLSCompliant (false)]
3240 public static extern int _exit (int status);
3242 [DllImport (MPH, SetLastError=true,
3243 EntryPoint="Mono_Posix_Syscall_fpathconf")]
3244 public static extern long fpathconf (int filedes, PathconfName name, Errno defaultError);
3246 public static long fpathconf (int filedes, PathconfName name)
3248 return fpathconf (filedes, name, (Errno) 0);
3251 [DllImport (MPH, SetLastError=true,
3252 EntryPoint="Mono_Posix_Syscall_pathconf")]
3253 public static extern long pathconf (
3254 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3255 string path, PathconfName name, Errno defaultError);
3257 public static long pathconf (string path, PathconfName name)
3259 return pathconf (path, name, (Errno) 0);
3262 [DllImport (MPH, SetLastError=true,
3263 EntryPoint="Mono_Posix_Syscall_sysconf")]
3264 public static extern long sysconf (SysconfName name, Errno defaultError);
3266 public static long sysconf (SysconfName name)
3268 return sysconf (name, (Errno) 0);
3272 // size_t confstr(int name, char *buf, size_t len);
3273 [DllImport (MPH, SetLastError=true,
3274 EntryPoint="Mono_Posix_Syscall_confstr")]
3275 public static extern ulong confstr (ConfstrName name, [Out] StringBuilder buf, ulong len);
3278 // pid_t getpid(void);
3279 [DllImport (LIBC, SetLastError=true)]
3280 public static extern int getpid ();
3283 // pid_t getppid(void);
3284 [DllImport (LIBC, SetLastError=true)]
3285 public static extern int getppid ();
3288 // int setpgid(pid_t pid, pid_t pgid);
3289 [DllImport (LIBC, SetLastError=true)]
3290 public static extern int setpgid (int pid, int pgid);
3293 // pid_t getpgid(pid_t pid);
3294 [DllImport (LIBC, SetLastError=true)]
3295 public static extern int getpgid (int pid);
3297 [DllImport (LIBC, SetLastError=true)]
3298 public static extern int setpgrp ();
3301 // pid_t getpgrp(void);
3302 [DllImport (LIBC, SetLastError=true)]
3303 public static extern int getpgrp ();
3306 // pid_t setsid(void);
3307 [DllImport (LIBC, SetLastError=true)]
3308 public static extern int setsid ();
3311 // pid_t getsid(pid_t pid);
3312 [DllImport (LIBC, SetLastError=true)]
3313 public static extern int getsid (int pid);
3316 // uid_t getuid(void);
3317 [DllImport (LIBC, SetLastError=true)]
3318 public static extern uint getuid ();
3321 // uid_t geteuid(void);
3322 [DllImport (LIBC, SetLastError=true)]
3323 public static extern uint geteuid ();
3326 // gid_t getgid(void);
3327 [DllImport (LIBC, SetLastError=true)]
3328 public static extern uint getgid ();
3331 // gid_t getgid(void);
3332 [DllImport (LIBC, SetLastError=true)]
3333 public static extern uint getegid ();
3336 // int getgroups(int size, gid_t list[]);
3337 [DllImport (LIBC, SetLastError=true)]
3338 public static extern int getgroups (int size, uint[] list);
3340 public static int getgroups (uint[] list)
3342 return getgroups (list.Length, list);
3346 // int setuid(uid_t uid);
3347 [DllImport (LIBC, SetLastError=true)]
3348 public static extern int setuid (uint uid);
3351 // int setreuid(uid_t ruid, uid_t euid);
3352 [DllImport (LIBC, SetLastError=true)]
3353 public static extern int setreuid (uint ruid, uint euid);
3356 // int setregid(gid_t ruid, gid_t euid);
3357 [DllImport (LIBC, SetLastError=true)]
3358 public static extern int setregid (uint rgid, uint egid);
3361 // int seteuid(uid_t euid);
3362 [DllImport (LIBC, SetLastError=true)]
3363 public static extern int seteuid (uint euid);
3366 // int setegid(gid_t euid);
3367 [DllImport (LIBC, SetLastError=true)]
3368 public static extern int setegid (uint uid);
3371 // int setgid(gid_t gid);
3372 [DllImport (LIBC, SetLastError=true)]
3373 public static extern int setgid (uint gid);
3376 // int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
3377 [DllImport (LIBC, SetLastError=true)]
3378 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
3381 // int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
3382 [DllImport (LIBC, SetLastError=true)]
3383 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
3386 // int setresuid(uid_t ruid, uid_t euid, uid_t suid);
3387 [DllImport (LIBC, SetLastError=true)]
3388 public static extern int setresuid (uint ruid, uint euid, uint suid);
3391 // int setresgid(gid_t ruid, gid_t euid, gid_t suid);
3392 [DllImport (LIBC, SetLastError=true)]
3393 public static extern int setresgid (uint rgid, uint egid, uint sgid);
3397 // pid_t fork(void);
3398 [DllImport (LIBC, SetLastError=true)]
3399 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " +
3400 "shutdown code.\nUse System.Diagnostics.Process instead")]
3401 private static extern int fork ();
3404 // pid_t vfork(void);
3405 [DllImport (LIBC, SetLastError=true)]
3406 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " +
3407 "shutdown code.\nUse System.Diagnostics.Process instead")]
3408 private static extern int vfork ();
3411 private static object tty_lock = new object ();
3413 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
3414 private static extern IntPtr sys_ttyname (int fd);
3416 public static string ttyname (int fd)
3419 IntPtr r = sys_ttyname (fd);
3420 return UnixMarshal.PtrToString (r);
3425 // int ttyname_r(int fd, char *buf, size_t buflen);
3426 [DllImport (MPH, SetLastError=true,
3427 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
3428 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
3430 public static int ttyname_r (int fd, StringBuilder buf)
3432 return ttyname_r (fd, buf, (ulong) buf.Capacity);
3435 [DllImport (LIBC, EntryPoint="isatty")]
3436 private static extern int sys_isatty (int fd);
3438 public static bool isatty (int fd)
3440 return sys_isatty (fd) == 1;
3443 [DllImport (LIBC, SetLastError=true)]
3444 public static extern int link (
3445 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3447 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3450 [DllImport (LIBC, SetLastError=true)]
3451 public static extern int symlink (
3452 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3454 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3458 // int readlink(const char *path, char *buf, size_t bufsize);
3459 [DllImport (MPH, SetLastError=true,
3460 EntryPoint="Mono_Posix_Syscall_readlink")]
3461 public static extern int readlink (
3462 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3463 string path, [Out] StringBuilder buf, ulong bufsiz);
3465 public static int readlink (string path, [Out] StringBuilder buf)
3467 return readlink (path, buf, (ulong) buf.Capacity);
3470 [DllImport (LIBC, SetLastError=true)]
3471 public static extern int unlink (
3472 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3475 [DllImport (LIBC, SetLastError=true)]
3476 public static extern int rmdir (
3477 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3481 // pid_t tcgetpgrp(int fd);
3482 [DllImport (LIBC, SetLastError=true)]
3483 public static extern int tcgetpgrp (int fd);
3486 // int tcsetpgrp(int fd, pid_t pgrp);
3487 [DllImport (LIBC, SetLastError=true)]
3488 public static extern int tcsetpgrp (int fd, int pgrp);
3490 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
3491 private static extern IntPtr sys_getlogin ();
3493 public static string getlogin ()
3495 lock (getlogin_lock) {
3496 IntPtr r = sys_getlogin ();
3497 return UnixMarshal.PtrToString (r);
3502 // int getlogin_r(char *buf, size_t bufsize);
3503 [DllImport (MPH, SetLastError=true,
3504 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
3505 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
3507 public static int getlogin_r (StringBuilder name)
3509 return getlogin_r (name, (ulong) name.Capacity);
3512 [DllImport (LIBC, SetLastError=true)]
3513 public static extern int setlogin (string name);
3516 // int gethostname(char *name, size_t len);
3517 [DllImport (MPH, SetLastError=true,
3518 EntryPoint="Mono_Posix_Syscall_gethostname")]
3519 public static extern int gethostname ([Out] StringBuilder name, ulong len);
3521 public static int gethostname (StringBuilder name)
3523 return gethostname (name, (ulong) name.Capacity);
3527 // int gethostname(const char *name, size_t len);
3528 [DllImport (MPH, SetLastError=true,
3529 EntryPoint="Mono_Posix_Syscall_sethostname")]
3530 public static extern int sethostname (string name, ulong len);
3532 public static int sethostname (string name)
3534 return sethostname (name, (ulong) name.Length);
3537 [DllImport (MPH, SetLastError=true,
3538 EntryPoint="Mono_Posix_Syscall_gethostid")]
3539 public static extern long gethostid ();
3541 [DllImport (MPH, SetLastError=true,
3542 EntryPoint="Mono_Posix_Syscall_sethostid")]
3543 public static extern int sethostid (long hostid);
3546 // int getdomainname(char *name, size_t len);
3547 [DllImport (MPH, SetLastError=true,
3548 EntryPoint="Mono_Posix_Syscall_getdomainname")]
3549 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
3551 public static int getdomainname (StringBuilder name)
3553 return getdomainname (name, (ulong) name.Capacity);
3557 // int setdomainname(const char *name, size_t len);
3558 [DllImport (MPH, SetLastError=true,
3559 EntryPoint="Mono_Posix_Syscall_setdomainname")]
3560 public static extern int setdomainname (string name, ulong len);
3562 public static int setdomainname (string name)
3564 return setdomainname (name, (ulong) name.Length);
3567 [DllImport (LIBC, SetLastError=true)]
3568 public static extern int vhangup ();
3570 // Revoke doesn't appear to be POSIX. Include it?
3571 [DllImport (LIBC, SetLastError=true)]
3572 public static extern int revoke (
3573 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3576 // TODO: profil? It's not POSIX.
3578 [DllImport (LIBC, SetLastError=true)]
3579 public static extern int acct (
3580 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3583 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
3584 private static extern IntPtr sys_getusershell ();
3586 internal static object usershell_lock = new object ();
3588 public static string getusershell ()
3590 lock (usershell_lock) {
3591 IntPtr r = sys_getusershell ();
3592 return UnixMarshal.PtrToString (r);
3596 [DllImport (MPH, SetLastError=true,
3597 EntryPoint="Mono_Posix_Syscall_setusershell")]
3598 private static extern int sys_setusershell ();
3600 public static int setusershell ()
3602 lock (usershell_lock) {
3603 return sys_setusershell ();
3607 [DllImport (MPH, SetLastError=true,
3608 EntryPoint="Mono_Posix_Syscall_endusershell")]
3609 private static extern int sys_endusershell ();
3611 public static int endusershell ()
3613 lock (usershell_lock) {
3614 return sys_endusershell ();
3619 [DllImport (LIBC, SetLastError=true)]
3620 private static extern int daemon (int nochdir, int noclose);
3622 // this implicitly forks, and thus isn't safe.
3623 private static int daemon (bool nochdir, bool noclose)
3625 return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
3629 [DllImport (LIBC, SetLastError=true)]
3630 public static extern int chroot (
3631 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3634 // skipping getpass(3) as the man page states:
3635 // This function is obsolete. Do not use it.
3637 [DllImport (LIBC, SetLastError=true)]
3638 public static extern int fsync (int fd);
3640 [DllImport (LIBC, SetLastError=true)]
3641 public static extern int fdatasync (int fd);
3643 [DllImport (MPH, SetLastError=true,
3644 EntryPoint="Mono_Posix_Syscall_sync")]
3645 public static extern int sync ();
3647 [DllImport (LIBC, SetLastError=true)]
3648 [Obsolete ("Dropped in POSIX 1003.1-2001. " +
3649 "Use Syscall.sysconf (SysconfName._SC_PAGESIZE).")]
3650 public static extern int getpagesize ();
3653 // int truncate(const char *path, off_t length);
3654 [DllImport (MPH, SetLastError=true,
3655 EntryPoint="Mono_Posix_Syscall_truncate")]
3656 public static extern int truncate (
3657 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3658 string path, long length);
3661 // int ftruncate(int fd, off_t length);
3662 [DllImport (MPH, SetLastError=true,
3663 EntryPoint="Mono_Posix_Syscall_ftruncate")]
3664 public static extern int ftruncate (int fd, long length);
3666 [DllImport (LIBC, SetLastError=true)]
3667 public static extern int getdtablesize ();
3669 [DllImport (LIBC, SetLastError=true)]
3670 public static extern int brk (IntPtr end_data_segment);
3672 [DllImport (LIBC, SetLastError=true)]
3673 public static extern IntPtr sbrk (IntPtr increment);
3675 // TODO: syscall(2)?
3676 // Probably safer to skip entirely.
3679 // int lockf(int fd, int cmd, off_t len);
3680 [DllImport (MPH, SetLastError=true,
3681 EntryPoint="Mono_Posix_Syscall_lockf")]
3682 public static extern int lockf (int fd, LockfCommand cmd, long len);
3684 [Obsolete ("This is insecure and should not be used", true)]
3685 public static string crypt (string key, string salt)
3687 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
3690 [Obsolete ("This is insecure and should not be used", true)]
3691 public static int encrypt (byte[] block, bool decode)
3693 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
3697 // void swab(const void *from, void *to, ssize_t n);
3698 [DllImport (MPH, SetLastError=true,
3699 EntryPoint="Mono_Posix_Syscall_swab")]
3700 public static extern int swab (IntPtr from, IntPtr to, long n);
3702 public static unsafe void swab (void* from, void* to, long n)
3704 swab ((IntPtr) from, (IntPtr) to, n);
3709 #region <utime.h> Declarations
3711 // <utime.h> -- COMPLETE
3714 [DllImport (MPH, SetLastError=true,
3715 EntryPoint="Mono_Posix_Syscall_utime")]
3716 private static extern int sys_utime (
3717 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3718 string filename, ref Utimbuf buf, int use_buf);
3720 public static int utime (string filename, ref Utimbuf buf)
3722 return sys_utime (filename, ref buf, 1);
3725 public static int utime (string filename)
3727 Utimbuf buf = new Utimbuf ();
3728 return sys_utime (filename, ref buf, 0);