2 // Mono.Unix/Syscall.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Jonathan Pryor (jonpryor@vt.edu)
8 // (C) 2003 Novell, Inc.
9 // (C) 2004-2005 Jonathan Pryor
11 // This file implements the low-level syscall interface to the POSIX
14 // This file tries to stay close to the low-level API as much as possible
15 // using enumerations, structures and in a few cases, using existing .NET
18 // Implementation notes:
20 // Since the values for the various constants on the API changes
21 // from system to system (even Linux on different architectures will
22 // have different values), we define our own set of values, and we
23 // use a set of C helper routines to map from the constants we define
24 // to the values of the native OS.
26 // Bitfields are flagged with the [Map] attribute, and a helper program
27 // generates a set of routines that we can call to convert from our value
28 // definitions to the value definitions expected by the OS; see
29 // 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;
60 using Mono.Unix.Native;
62 [assembly:Mono.Unix.Native.HeaderAttribute (
82 namespace Mono.Unix.Native {
87 [CLSCompliant (false)]
88 public enum SyslogOptions {
89 LOG_PID = 0x01, // log the pid with each message
90 LOG_CONS = 0x02, // log on the console if errors in sending
91 LOG_ODELAY = 0x04, // delay open until first syslog (default)
92 LOG_NDELAY = 0x08, // don't delay open
93 LOG_NOWAIT = 0x10, // don't wait for console forks; DEPRECATED
94 LOG_PERROR = 0x20 // log to stderr as well
98 [CLSCompliant (false)]
99 public enum SyslogFacility {
110 LOG_AUTHPRIV = 10 << 3,
112 LOG_LOCAL0 = 16 << 3,
113 LOG_LOCAL1 = 17 << 3,
114 LOG_LOCAL2 = 18 << 3,
115 LOG_LOCAL3 = 19 << 3,
116 LOG_LOCAL4 = 20 << 3,
117 LOG_LOCAL5 = 21 << 3,
118 LOG_LOCAL6 = 22 << 3,
119 LOG_LOCAL7 = 23 << 3,
123 [CLSCompliant (false)]
124 public enum SyslogLevel {
125 LOG_EMERG = 0, // system is unusable
126 LOG_ALERT = 1, // action must be taken immediately
127 LOG_CRIT = 2, // critical conditions
128 LOG_ERR = 3, // warning conditions
129 LOG_WARNING = 4, // warning conditions
130 LOG_NOTICE = 5, // normal but significant condition
131 LOG_INFO = 6, // informational
132 LOG_DEBUG = 7 // debug-level messages
136 [CLSCompliant (false)]
137 public enum OpenFlags : int {
141 O_RDONLY = 0x00000000,
142 O_WRONLY = 0x00000001,
146 // Or-ed with zero or more of these
148 O_CREAT = 0x00000040,
150 O_NOCTTY = 0x00000100,
151 O_TRUNC = 0x00000200,
152 O_APPEND = 0x00000400,
153 O_NONBLOCK = 0x00000800,
157 // These are non-Posix. Using them will result in errors/exceptions on
158 // non-supported platforms.
160 // (For example, "C-wrapped" system calls -- calls with implementation in
161 // MonoPosixHelper -- will return -1 with errno=EINVAL. C#-wrapped system
162 // calls will generate an exception in NativeConvert, as the value can't be
163 // converted on the target platform.)
166 O_NOFOLLOW = 0x00020000,
167 O_DIRECTORY = 0x00010000,
168 O_DIRECT = 0x00004000,
169 O_ASYNC = 0x00002000,
170 O_LARGEFILE = 0x00008000
175 [CLSCompliant (false)]
176 public enum FilePermissions : uint {
177 S_ISUID = 0x0800, // Set user ID on execution
178 S_ISGID = 0x0400, // Set group ID on execution
179 S_ISVTX = 0x0200, // Save swapped text after use (sticky).
180 S_IRUSR = 0x0100, // Read by owner
181 S_IWUSR = 0x0080, // Write by owner
182 S_IXUSR = 0x0040, // Execute by owner
183 S_IRGRP = 0x0020, // Read by group
184 S_IWGRP = 0x0010, // Write by group
185 S_IXGRP = 0x0008, // Execute by group
186 S_IROTH = 0x0004, // Read by other
187 S_IWOTH = 0x0002, // Write by other
188 S_IXOTH = 0x0001, // Execute by other
190 S_IRWXG = (S_IRGRP | S_IWGRP | S_IXGRP),
191 S_IRWXU = (S_IRUSR | S_IWUSR | S_IXUSR),
192 S_IRWXO = (S_IROTH | S_IWOTH | S_IXOTH),
193 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
194 ALLPERMS = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
195 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
198 // Why these are held in "mode_t" is beyond me...
199 S_IFMT = 0xF000, // Bits which determine file type
200 S_IFDIR = 0x4000, // Directory
201 S_IFCHR = 0x2000, // Character device
202 S_IFBLK = 0x6000, // Block device
203 S_IFREG = 0x8000, // Regular file
204 S_IFIFO = 0x1000, // FIFO
205 S_IFLNK = 0xA000, // Symbolic link
206 S_IFSOCK = 0xC000, // Socket
210 [CLSCompliant (false)]
211 public enum FcntlCommand : int {
212 // Form /usr/include/bits/fcntl.h
213 F_DUPFD = 0, // Duplicate file descriptor.
214 F_GETFD = 1, // Get file descriptor flags.
215 F_SETFD = 2, // Set file descriptor flags.
216 F_GETFL = 3, // Get file status flags.
217 F_SETFL = 4, // Set file status flags.
218 F_GETLK = 12, // Get record locking info. [64]
219 F_SETLK = 13, // Set record locking info (non-blocking). [64]
220 F_SETLKW = 14, // Set record locking info (blocking). [64]
221 F_SETOWN = 8, // Set owner of socket (receiver of SIGIO).
222 F_GETOWN = 9, // Get owner of socket (receiver of SIGIO).
223 F_SETSIG = 10, // Set number of signal to be sent.
224 F_GETSIG = 11, // Get number of signal to be sent.
225 F_SETLEASE = 1024, // Set a lease.
226 F_GETLEASE = 1025, // Enquire what lease is active.
227 F_NOTIFY = 1026, // Required notifications on a directory
231 [CLSCompliant (false)]
232 public enum LockType : short {
233 F_RDLCK = 0, // Read lock.
234 F_WRLCK = 1, // Write lock.
235 F_UNLCK = 2, // Remove lock.
239 [CLSCompliant (false)]
240 public enum SeekFlags : short {
241 // values liberally copied from /usr/include/unistd.h
242 SEEK_SET = 0, // Seek from beginning of file.
243 SEEK_CUR = 1, // Seek from current position.
244 SEEK_END = 2, // Seek from end of file.
246 L_SET = SEEK_SET, // BSD alias for SEEK_SET
247 L_INCR = SEEK_CUR, // BSD alias for SEEK_CUR
248 L_XTND = SEEK_END, // BSD alias for SEEK_END
252 [CLSCompliant (false)]
253 public enum DirectoryNotifyFlags : int {
254 // from /usr/include/bits/fcntl.h
255 DN_ACCESS = 0x00000001, // File accessed.
256 DN_MODIFY = 0x00000002, // File modified.
257 DN_CREATE = 0x00000004, // File created.
258 DN_DELETE = 0x00000008, // File removed.
259 DN_RENAME = 0x00000010, // File renamed.
260 DN_ATTRIB = 0x00000020, // File changed attributes.
261 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
265 [CLSCompliant (false)]
266 public enum PosixFadviseAdvice : int {
267 POSIX_FADV_NORMAL = 0, // No further special treatment.
268 POSIX_FADV_RANDOM = 1, // Expect random page references.
269 POSIX_FADV_SEQUENTIAL = 2, // Expect sequential page references.
270 POSIX_FADV_WILLNEED = 3, // Will need these pages.
271 POSIX_FADV_DONTNEED = 4, // Don't need these pages.
272 POSIX_FADV_NOREUSE = 5, // Data will be accessed once.
276 [CLSCompliant (false)]
277 public enum PosixMadviseAdvice : int {
278 POSIX_MADV_NORMAL = 0, // No further special treatment.
279 POSIX_MADV_RANDOM = 1, // Expect random page references.
280 POSIX_MADV_SEQUENTIAL = 2, // Expect sequential page references.
281 POSIX_MADV_WILLNEED = 3, // Will need these pages.
282 POSIX_MADV_DONTNEED = 4, // Don't need these pages.
286 public enum Signum : int {
287 SIGHUP = 1, // Hangup (POSIX).
288 SIGINT = 2, // Interrupt (ANSI).
289 SIGQUIT = 3, // Quit (POSIX).
290 SIGILL = 4, // Illegal instruction (ANSI).
291 SIGTRAP = 5, // Trace trap (POSIX).
292 SIGABRT = 6, // Abort (ANSI).
293 SIGIOT = 6, // IOT trap (4.2 BSD).
294 SIGBUS = 7, // BUS error (4.2 BSD).
295 SIGFPE = 8, // Floating-point exception (ANSI).
296 SIGKILL = 9, // Kill, unblockable (POSIX).
297 SIGUSR1 = 10, // User-defined signal 1 (POSIX).
298 SIGSEGV = 11, // Segmentation violation (ANSI).
299 SIGUSR2 = 12, // User-defined signal 2 (POSIX).
300 SIGPIPE = 13, // Broken pipe (POSIX).
301 SIGALRM = 14, // Alarm clock (POSIX).
302 SIGTERM = 15, // Termination (ANSI).
303 SIGSTKFLT = 16, // Stack fault.
304 SIGCLD = SIGCHLD, // Same as SIGCHLD (System V).
305 SIGCHLD = 17, // Child status has changed (POSIX).
306 SIGCONT = 18, // Continue (POSIX).
307 SIGSTOP = 19, // Stop, unblockable (POSIX).
308 SIGTSTP = 20, // Keyboard stop (POSIX).
309 SIGTTIN = 21, // Background read from tty (POSIX).
310 SIGTTOU = 22, // Background write to tty (POSIX).
311 SIGURG = 23, // Urgent condition on socket (4.2 BSD).
312 SIGXCPU = 24, // CPU limit exceeded (4.2 BSD).
313 SIGXFSZ = 25, // File size limit exceeded (4.2 BSD).
314 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
315 SIGPROF = 27, // Profiling alarm clock (4.2 BSD).
316 SIGWINCH = 28, // Window size change (4.3 BSD, Sun).
317 SIGPOLL = SIGIO, // Pollable event occurred (System V).
318 SIGIO = 29, // I/O now possible (4.2 BSD).
319 SIGPWR = 30, // Power failure restart (System V).
320 SIGSYS = 31, // Bad system call.
325 public enum WaitOptions : int {
326 WNOHANG = 1, // Don't block waiting
327 WUNTRACED = 2, // Report status of stopped children
331 [CLSCompliant (false)]
332 public enum AccessModes : int {
340 [CLSCompliant (false)]
341 public enum PathconfName : int {
348 _PC_CHOWN_RESTRICTED,
356 _PC_REC_INCR_XFER_SIZE,
357 _PC_REC_MAX_XFER_SIZE,
358 _PC_REC_MIN_XFER_SIZE,
366 [CLSCompliant (false)]
367 public enum SysconfName : int {
377 _SC_REALTIME_SIGNALS,
378 _SC_PRIORITY_SCHEDULING,
387 _SC_MEMORY_PROTECTION,
390 _SC_SHARED_MEMORY_OBJECTS,
393 _SC_AIO_PRIO_DELTA_MAX,
404 /* Values for the argument to `sysconf'
405 corresponding to _POSIX2_* symbols. */
410 _SC_COLL_WEIGHTS_MAX,
415 _SC_CHARCLASS_NAME_MAX,
431 _SC_IOV_MAX = _SC_UIO_MAXIOV,
432 _SC_PII_INTERNET_STREAM,
433 _SC_PII_INTERNET_DGRAM,
438 /* Values according to POSIX 1003.1c (POSIX threads). */
440 _SC_THREAD_SAFE_FUNCTIONS,
441 _SC_GETGR_R_SIZE_MAX,
442 _SC_GETPW_R_SIZE_MAX,
445 _SC_THREAD_DESTRUCTOR_ITERATIONS,
447 _SC_THREAD_STACK_MIN,
448 _SC_THREAD_THREADS_MAX,
449 _SC_THREAD_ATTR_STACKADDR,
450 _SC_THREAD_ATTR_STACKSIZE,
451 _SC_THREAD_PRIORITY_SCHEDULING,
452 _SC_THREAD_PRIO_INHERIT,
453 _SC_THREAD_PRIO_PROTECT,
454 _SC_THREAD_PROCESS_SHARED,
455 _SC_NPROCESSORS_CONF,
456 _SC_NPROCESSORS_ONLN,
462 _SC_XOPEN_XCU_VERSION,
497 _SC_XBS5_ILP32_OFF32,
498 _SC_XBS5_ILP32_OFFBIG,
500 _SC_XBS5_LPBIG_OFFBIG,
503 _SC_XOPEN_REALTIME_THREADS,
508 _SC_C_LANG_SUPPORT_R,
514 _SC_DEVICE_SPECIFIC_R,
525 _SC_READER_WRITER_LOCKS,
533 _SC_THREAD_SPORADIC_SERVER,
535 _SC_SYSTEM_DATABASE_R,
537 _SC_TYPED_MEMORY_OBJECTS,
541 _SC_2_PBS_ACCOUNTING,
547 _SC_2_PBS_CHECKPOINT,
554 _SC_TRACE_EVENT_FILTER,
557 _SC_LEVEL1_ICACHE_SIZE,
558 _SC_LEVEL1_ICACHE_ASSOC,
559 _SC_LEVEL1_ICACHE_LINESIZE,
560 _SC_LEVEL1_DCACHE_SIZE,
561 _SC_LEVEL1_DCACHE_ASSOC,
562 _SC_LEVEL1_DCACHE_LINESIZE,
563 _SC_LEVEL2_CACHE_SIZE,
564 _SC_LEVEL2_CACHE_ASSOC,
565 _SC_LEVEL2_CACHE_LINESIZE,
566 _SC_LEVEL3_CACHE_SIZE,
567 _SC_LEVEL3_CACHE_ASSOC,
568 _SC_LEVEL3_CACHE_LINESIZE,
569 _SC_LEVEL4_CACHE_SIZE,
570 _SC_LEVEL4_CACHE_ASSOC,
571 _SC_LEVEL4_CACHE_LINESIZE
575 [CLSCompliant (false)]
576 public enum ConfstrName : int {
577 _CS_PATH, /* The default search path. */
578 _CS_V6_WIDTH_RESTRICTED_ENVS,
579 _CS_GNU_LIBC_VERSION,
580 _CS_GNU_LIBPTHREAD_VERSION,
581 _CS_LFS_CFLAGS = 1000,
589 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
590 _CS_XBS5_ILP32_OFF32_LDFLAGS,
591 _CS_XBS5_ILP32_OFF32_LIBS,
592 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
593 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
594 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
595 _CS_XBS5_ILP32_OFFBIG_LIBS,
596 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
597 _CS_XBS5_LP64_OFF64_CFLAGS,
598 _CS_XBS5_LP64_OFF64_LDFLAGS,
599 _CS_XBS5_LP64_OFF64_LIBS,
600 _CS_XBS5_LP64_OFF64_LINTFLAGS,
601 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
602 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
603 _CS_XBS5_LPBIG_OFFBIG_LIBS,
604 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
605 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
606 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
607 _CS_POSIX_V6_ILP32_OFF32_LIBS,
608 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
609 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
610 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
611 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
612 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
613 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
614 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
615 _CS_POSIX_V6_LP64_OFF64_LIBS,
616 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
617 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
618 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
619 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
620 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
624 [CLSCompliant (false)]
625 public enum LockfCommand : int {
626 F_ULOCK = 0, // Unlock a previously locked region.
627 F_LOCK = 1, // Lock a region for exclusive use.
628 F_TLOCK = 2, // Test and lock a region for exclusive use.
629 F_TEST = 3, // Test a region for other process locks.
633 public enum PollEvents : short {
634 POLLIN = 0x0001, // There is data to read
635 POLLPRI = 0x0002, // There is urgent data to read
636 POLLOUT = 0x0004, // Writing now will not block
637 POLLERR = 0x0008, // Error condition
638 POLLHUP = 0x0010, // Hung up
639 POLLNVAL = 0x0020, // Invalid request; fd not open
640 // XPG4.2 definitions (via _XOPEN_SOURCE)
641 POLLRDNORM = 0x0040, // Normal data bay be read
642 POLLRDBAND = 0x0080, // Priority data may be read
643 POLLWRNORM = 0x0100, // Writing now will not block
644 POLLWRBAND = 0x0200, // Priority data may be written
648 [CLSCompliant (false)]
649 public enum XattrFlags : int {
656 [CLSCompliant (false)]
657 public enum MountFlags : ulong {
658 ST_RDONLY = 1, // Mount read-only
659 ST_NOSUID = 2, // Ignore suid and sgid bits
660 ST_NODEV = 4, // Disallow access to device special files
661 ST_SYNCHRONOUS = 16, // Writes are synced at once
662 ST_MANDLOCK = 64, // Allow mandatory locks on an FS
663 ST_WRITE = 128, // Write on file/directory/symlink
664 ST_APPEND = 256, // Append-only file
665 ST_IMMUTABLE = 512, // Immutable file
666 ST_NOATIME = 1024, // Do not update access times
667 ST_NODIRATIME = 2048, // Do not update directory access times
671 [CLSCompliant (false)]
672 public enum MmapFlags : int {
673 MAP_SHARED = 0x01, // Share changes.
674 MAP_PRIVATE = 0x02, // Changes are private.
675 MAP_TYPE = 0x0f, // Mask for type of mapping.
676 MAP_FIXED = 0x10, // Interpret addr exactly.
678 MAP_ANONYMOUS = 0x20, // Don't use a file.
679 MAP_ANON = MAP_ANONYMOUS,
681 // These are Linux-specific.
682 MAP_GROWSDOWN = 0x00100, // Stack-like segment.
683 MAP_DENYWRITE = 0x00800, // ETXTBSY
684 MAP_EXECUTABLE = 0x01000, // Mark it as an executable.
685 MAP_LOCKED = 0x02000, // Lock the mapping.
686 MAP_NORESERVE = 0x04000, // Don't check for reservations.
687 MAP_POPULATE = 0x08000, // Populate (prefault) pagetables.
688 MAP_NONBLOCK = 0x10000, // Do not block on IO.
692 [CLSCompliant (false)]
693 public enum MmapProts : int {
694 PROT_READ = 0x1, // Page can be read.
695 PROT_WRITE = 0x2, // Page can be written.
696 PROT_EXEC = 0x4, // Page can be executed.
697 PROT_NONE = 0x0, // Page can not be accessed.
698 PROT_GROWSDOWN = 0x01000000, // Extend change to start of
699 // growsdown vma (mprotect only).
700 PROT_GROWSUP = 0x02000000, // Extend change to start of
701 // growsup vma (mprotect only).
705 [CLSCompliant (false)]
706 public enum MsyncFlags : int {
707 MS_ASYNC = 0x1, // Sync memory asynchronously.
708 MS_SYNC = 0x4, // Synchronous memory sync.
709 MS_INVALIDATE = 0x2, // Invalidate the caches.
713 [CLSCompliant (false)]
714 public enum MlockallFlags : int {
715 MCL_CURRENT = 0x1, // Lock all currently mapped pages.
716 MCL_FUTURE = 0x2, // Lock all additions to address
720 [CLSCompliant (false)]
721 public enum MremapFlags : ulong {
722 MREMAP_MAYMOVE = 0x1,
734 [CLSCompliant (false)]
735 public LockType l_type; // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
736 [CLSCompliant (false)]
737 public SeekFlags l_whence; // How to interpret l_start
738 public /* off_t */ long l_start; // Starting offset for lock
739 public /* off_t */ long l_len; // Number of bytes to lock
740 public /* pid_t */ int l_pid; // PID of process blocking our lock (F_GETLK only)
742 public override int GetHashCode ()
744 return l_type.GetHashCode () ^ l_whence.GetHashCode () ^
745 l_start.GetHashCode () ^ l_len.GetHashCode () ^
746 l_pid.GetHashCode ();
749 public override bool Equals (object obj)
751 if (obj.GetType() != GetType())
753 Flock value = (Flock) obj;
754 return l_type == value.l_type && l_whence == value.l_whence &&
755 l_start == value.l_start && l_len == value.l_len &&
756 l_pid == value.l_pid;
759 public bool Equals (Flock value)
761 return l_type == value.l_type && l_whence == value.l_whence &&
762 l_start == value.l_start && l_len == value.l_len &&
763 l_pid == value.l_pid;
766 public static bool operator== (Flock lhs, Flock rhs)
768 return lhs.Equals (rhs);
771 public static bool operator!= (Flock lhs, Flock rhs)
773 return !lhs.Equals (rhs);
779 : IEquatable <Pollfd>
783 [CLSCompliant (false)]
784 public PollEvents events;
785 [CLSCompliant (false)]
786 public PollEvents revents;
788 public override int GetHashCode ()
790 return events.GetHashCode () ^ revents.GetHashCode ();
793 public override bool Equals (object obj)
795 if (obj == null || obj.GetType () != GetType ())
797 Pollfd value = (Pollfd) obj;
798 return value.events == events && value.revents == revents;
801 public bool Equals (Pollfd value)
803 return value.events == events && value.revents == revents;
806 public static bool operator== (Pollfd lhs, Pollfd rhs)
808 return lhs.Equals (rhs);
811 public static bool operator!= (Pollfd lhs, Pollfd rhs)
813 return !lhs.Equals (rhs);
822 [CLSCompliant (false)]
823 public /* dev_t */ ulong st_dev; // device
824 [CLSCompliant (false)]
825 public /* ino_t */ ulong st_ino; // inode
826 [CLSCompliant (false)]
827 public FilePermissions st_mode; // protection
828 [CLSCompliant (false)]
829 private uint _padding_; // padding for structure alignment
830 [CLSCompliant (false)]
831 public /* nlink_t */ ulong st_nlink; // number of hard links
832 [CLSCompliant (false)]
833 public /* uid_t */ uint st_uid; // user ID of owner
834 [CLSCompliant (false)]
835 public /* gid_t */ uint st_gid; // group ID of owner
836 [CLSCompliant (false)]
837 public /* dev_t */ ulong st_rdev; // device type (if inode device)
838 public /* off_t */ long st_size; // total size, in bytes
839 public /* blksize_t */ long st_blksize; // blocksize for filesystem I/O
840 public /* blkcnt_t */ long st_blocks; // number of blocks allocated
841 public /* time_t */ long st_atime; // time of last access
842 public /* time_t */ long st_mtime; // time of last modification
843 public /* time_t */ long st_ctime; // time of last status change
845 public override int GetHashCode ()
847 return st_dev.GetHashCode () ^
848 st_ino.GetHashCode () ^
849 st_mode.GetHashCode () ^
850 st_nlink.GetHashCode () ^
851 st_uid.GetHashCode () ^
852 st_gid.GetHashCode () ^
853 st_rdev.GetHashCode () ^
854 st_size.GetHashCode () ^
855 st_blksize.GetHashCode () ^
856 st_blocks.GetHashCode () ^
857 st_atime.GetHashCode () ^
858 st_mtime.GetHashCode () ^
859 st_ctime.GetHashCode ();
862 public override bool Equals (object obj)
864 if (obj == null || obj.GetType() != GetType ())
866 Stat value = (Stat) obj;
867 return value.st_dev == st_dev &&
868 value.st_ino == st_ino &&
869 value.st_mode == st_mode &&
870 value.st_nlink == st_nlink &&
871 value.st_uid == st_uid &&
872 value.st_gid == st_gid &&
873 value.st_rdev == st_rdev &&
874 value.st_size == st_size &&
875 value.st_blksize == st_blksize &&
876 value.st_blocks == st_blocks &&
877 value.st_atime == st_atime &&
878 value.st_mtime == st_mtime &&
879 value.st_ctime == st_ctime;
882 public bool Equals (Stat value)
884 return value.st_dev == st_dev &&
885 value.st_ino == st_ino &&
886 value.st_mode == st_mode &&
887 value.st_nlink == st_nlink &&
888 value.st_uid == st_uid &&
889 value.st_gid == st_gid &&
890 value.st_rdev == st_rdev &&
891 value.st_size == st_size &&
892 value.st_blksize == st_blksize &&
893 value.st_blocks == st_blocks &&
894 value.st_atime == st_atime &&
895 value.st_mtime == st_mtime &&
896 value.st_ctime == st_ctime;
899 public static bool operator== (Stat lhs, Stat rhs)
901 return lhs.Equals (rhs);
904 public static bool operator!= (Stat lhs, Stat rhs)
906 return !lhs.Equals (rhs);
910 [CLSCompliant (false)]
911 public struct Statvfs
913 : IEquatable <Statvfs>
916 public ulong f_bsize; // file system block size
917 public ulong f_frsize; // fragment size
918 public /* fsblkcnt_t */ ulong f_blocks; // size of fs in f_frsize units
919 public /* fsblkcnt_t */ ulong f_bfree; // # free blocks
920 public /* fsblkcnt_t */ ulong f_bavail; // # free blocks for non-root
921 public /* fsfilcnt_t */ ulong f_files; // # inodes
922 public /* fsfilcnt_t */ ulong f_ffree; // # free inodes
923 public /* fsfilcnt_t */ ulong f_favail; // # free inodes for non-root
924 public ulong f_fsid; // file system id
925 public MountFlags f_flag; // mount flags
926 public ulong f_namemax; // maximum filename length
928 public override int GetHashCode ()
930 return f_bsize.GetHashCode () ^
931 f_frsize.GetHashCode () ^
932 f_blocks.GetHashCode () ^
933 f_bfree.GetHashCode () ^
934 f_bavail.GetHashCode () ^
935 f_files.GetHashCode () ^
936 f_ffree.GetHashCode () ^
937 f_favail.GetHashCode () ^
938 f_fsid.GetHashCode () ^
939 f_flag.GetHashCode () ^
940 f_namemax.GetHashCode ();
943 public override bool Equals (object obj)
945 if (obj == null || obj.GetType() != GetType ())
947 Statvfs value = (Statvfs) obj;
948 return value.f_bsize == f_bsize &&
949 value.f_frsize == f_frsize &&
950 value.f_blocks == f_blocks &&
951 value.f_bfree == f_bfree &&
952 value.f_bavail == f_bavail &&
953 value.f_files == f_files &&
954 value.f_ffree == f_ffree &&
955 value.f_favail == f_favail &&
956 value.f_fsid == f_fsid &&
957 value.f_flag == f_flag &&
958 value.f_namemax == f_namemax;
961 public bool Equals (Statvfs value)
963 return value.f_bsize == f_bsize &&
964 value.f_frsize == f_frsize &&
965 value.f_blocks == f_blocks &&
966 value.f_bfree == f_bfree &&
967 value.f_bavail == f_bavail &&
968 value.f_files == f_files &&
969 value.f_ffree == f_ffree &&
970 value.f_favail == f_favail &&
971 value.f_fsid == f_fsid &&
972 value.f_flag == f_flag &&
973 value.f_namemax == f_namemax;
976 public static bool operator== (Statvfs lhs, Statvfs rhs)
978 return lhs.Equals (rhs);
981 public static bool operator!= (Statvfs lhs, Statvfs rhs)
983 return !lhs.Equals (rhs);
987 public struct Timeval
989 : IEquatable <Timeval>
992 public /* time_t */ long tv_sec; // seconds
993 public /* suseconds_t */ long tv_usec; // microseconds
995 public override int GetHashCode ()
997 return tv_sec.GetHashCode () ^ tv_usec.GetHashCode ();
1000 public override bool Equals (object obj)
1002 if (obj == null || obj.GetType () != GetType ())
1004 Timeval value = (Timeval) obj;
1005 return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1008 public bool Equals (Timeval value)
1010 return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1013 public static bool operator== (Timeval lhs, Timeval rhs)
1015 return lhs.Equals (rhs);
1018 public static bool operator!= (Timeval lhs, Timeval rhs)
1020 return !lhs.Equals (rhs);
1024 public struct Timezone
1026 : IEquatable <Timezone>
1029 public int tz_minuteswest; // minutes W of Greenwich
1030 private int tz_dsttime; // type of dst correction (OBSOLETE)
1032 public override int GetHashCode ()
1034 return tz_minuteswest.GetHashCode ();
1037 public override bool Equals (object obj)
1039 if (obj == null || obj.GetType () != GetType ())
1041 Timezone value = (Timezone) obj;
1042 return value.tz_minuteswest == tz_minuteswest;
1045 public bool Equals (Timezone value)
1047 return value.tz_minuteswest == tz_minuteswest;
1050 public static bool operator== (Timezone lhs, Timezone rhs)
1052 return lhs.Equals (rhs);
1055 public static bool operator!= (Timezone lhs, Timezone rhs)
1057 return !lhs.Equals (rhs);
1061 public struct Utimbuf
1063 : IEquatable <Utimbuf>
1066 public /* time_t */ long actime; // access time
1067 public /* time_t */ long modtime; // modification time
1069 public override int GetHashCode ()
1071 return actime.GetHashCode () ^ modtime.GetHashCode ();
1074 public override bool Equals (object obj)
1076 if (obj == null || obj.GetType () != GetType ())
1078 Utimbuf value = (Utimbuf) obj;
1079 return value.actime == actime && value.modtime == modtime;
1082 public bool Equals (Utimbuf value)
1084 return value.actime == actime && value.modtime == modtime;
1087 public static bool operator== (Utimbuf lhs, Utimbuf rhs)
1089 return lhs.Equals (rhs);
1092 public static bool operator!= (Utimbuf lhs, Utimbuf rhs)
1094 return !lhs.Equals (rhs);
1102 public sealed class Dirent
1104 : IEquatable <Dirent>
1107 [CLSCompliant (false)]
1108 public /* ino_t */ ulong d_ino;
1109 public /* off_t */ long d_off;
1110 [CLSCompliant (false)]
1111 public ushort d_reclen;
1113 public string d_name;
1115 public override int GetHashCode ()
1117 return d_ino.GetHashCode () ^ d_off.GetHashCode () ^
1118 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
1119 d_name.GetHashCode ();
1122 public override bool Equals (object obj)
1124 if (obj == null || GetType() != obj.GetType())
1126 Dirent d = (Dirent) obj;
1130 public bool Equals (Dirent value)
1134 return value.d_ino == d_ino && value.d_off == d_off &&
1135 value.d_reclen == d_reclen && value.d_type == d_type &&
1136 value.d_name == d_name;
1139 public override string ToString ()
1144 public static bool operator== (Dirent lhs, Dirent rhs)
1146 return Object.Equals (lhs, rhs);
1149 public static bool operator!= (Dirent lhs, Dirent rhs)
1151 return !Object.Equals (lhs, rhs);
1155 public sealed class Fstab
1157 : IEquatable <Fstab>
1160 public string fs_spec;
1161 public string fs_file;
1162 public string fs_vfstype;
1163 public string fs_mntops;
1164 public string fs_type;
1166 public int fs_passno;
1168 public override int GetHashCode ()
1170 return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
1171 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
1172 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
1175 public override bool Equals (object obj)
1177 if (obj == null || GetType() != obj.GetType())
1179 Fstab f = (Fstab) obj;
1183 public bool Equals (Fstab value)
1187 return value.fs_spec == fs_spec && value.fs_file == fs_file &&
1188 value.fs_vfstype == fs_vfstype && value.fs_mntops == fs_mntops &&
1189 value.fs_type == fs_type && value.fs_freq == fs_freq &&
1190 value.fs_passno == fs_passno;
1193 public override string ToString ()
1198 public static bool operator== (Fstab lhs, Fstab rhs)
1200 return Object.Equals (lhs, rhs);
1203 public static bool operator!= (Fstab lhs, Fstab rhs)
1205 return !Object.Equals (lhs, rhs);
1209 public sealed class Group
1211 : IEquatable <Group>
1214 public string gr_name;
1215 public string gr_passwd;
1216 [CLSCompliant (false)]
1217 public /* gid_t */ uint gr_gid;
1218 public string[] gr_mem;
1220 public override int GetHashCode ()
1223 for (int i = 0; i < gr_mem.Length; ++i)
1224 memhc ^= gr_mem[i].GetHashCode ();
1226 return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^
1227 gr_gid.GetHashCode () ^ memhc;
1230 public override bool Equals (object obj)
1232 if (obj == null || GetType() != obj.GetType())
1234 Group g = (Group) obj;
1238 public bool Equals (Group value)
1242 if (value.gr_gid != gr_gid)
1244 if (value.gr_gid == gr_gid && value.gr_name == gr_name &&
1245 value.gr_passwd == gr_passwd) {
1246 if (value.gr_mem == gr_mem)
1248 if (value.gr_mem == null || gr_mem == null)
1250 if (value.gr_mem.Length != gr_mem.Length)
1252 for (int i = 0; i < gr_mem.Length; ++i)
1253 if (gr_mem[i] != value.gr_mem[i])
1260 // Generate string in /etc/group format
1261 public override string ToString ()
1263 StringBuilder sb = new StringBuilder ();
1264 sb.Append (gr_name).Append (":").Append (gr_passwd).Append (":");
1265 sb.Append (gr_gid).Append (":");
1266 GetMembers (sb, gr_mem);
1267 return sb.ToString ();
1270 private static void GetMembers (StringBuilder sb, string[] members)
1272 if (members.Length > 0)
1273 sb.Append (members[0]);
1274 for (int i = 1; i < members.Length; ++i) {
1276 sb.Append (members[i]);
1280 public static bool operator== (Group lhs, Group rhs)
1282 return Object.Equals (lhs, rhs);
1285 public static bool operator!= (Group lhs, Group rhs)
1287 return !Object.Equals (lhs, rhs);
1291 public sealed class Passwd
1293 : IEquatable <Passwd>
1296 public string pw_name;
1297 public string pw_passwd;
1298 [CLSCompliant (false)]
1299 public /* uid_t */ uint pw_uid;
1300 [CLSCompliant (false)]
1301 public /* gid_t */ uint pw_gid;
1302 public string pw_gecos;
1303 public string pw_dir;
1304 public string pw_shell;
1306 public override int GetHashCode ()
1308 return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^
1309 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
1310 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
1311 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
1314 public override bool Equals (object obj)
1316 if (obj == null || GetType() != obj.GetType())
1318 Passwd p = (Passwd) obj;
1322 public bool Equals (Passwd value)
1326 return value.pw_uid == pw_uid && value.pw_gid == pw_gid &&
1327 value.pw_name == pw_name && value.pw_passwd == pw_passwd &&
1328 value.pw_gecos == pw_gecos && value.pw_dir == pw_dir &&
1329 value.pw_shell == pw_shell;
1332 // Generate string in /etc/passwd format
1333 public override string ToString ()
1335 return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
1336 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
1339 public static bool operator== (Passwd lhs, Passwd rhs)
1341 return Object.Equals (lhs, rhs);
1344 public static bool operator!= (Passwd lhs, Passwd rhs)
1346 return !Object.Equals (lhs, rhs);
1351 // Convention: Functions *not* part of the standard C library AND part of
1352 // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
1354 // For example, the man page should be similar to:
1356 // CONFORMING TO (or CONFORMS TO)
1357 // XPG2, SUSv2, POSIX, etc.
1359 // BSD- and GNU-specific exports can also be placed here.
1361 // Non-POSIX/XPG/etc. functions can also be placed here if:
1362 // (a) They'd be likely to be covered in a Steven's-like book
1363 // (b) The functions would be present in libc.so (or equivalent).
1365 // If a function has its own library, that's a STRONG indicator that the
1366 // function should get a different binding, probably in its own assembly,
1367 // so that package management can work sanely. (That is, we'd like to avoid
1368 // scenarios where FooLib.dll is installed, but it requires libFooLib.so to
1369 // run, and libFooLib.so doesn't exist. That would be confusing.)
1371 // The only methods in here should be:
1372 // (1) low-level functions
1373 // (2) "Trivial" function overloads. For example, if the parameters to a
1374 // function are related (e.g. getgroups(2))
1375 // (3) The return type SHOULD NOT be changed. If you want to provide a
1376 // convenience function with a nicer return type, place it into one of
1377 // the Mono.Unix.Unix* wrapper classes, and give it a .NET-styled name.
1378 // - EXCEPTION: No public functions should have a `void' return type.
1379 // `void' return types should be replaced with `int'.
1380 // Rationality: `void'-return functions typically require a
1381 // complicated call sequence, such as clear errno, then call, then
1382 // check errno to see if any errors occurred. This sequence can't
1383 // be done safely in managed code, as errno may change as part of
1384 // the P/Invoke mechanism.
1385 // Instead, add a MonoPosixHelper export which does:
1388 // return errno == 0 ? 0 : -1;
1389 // This lets managed code check the return value in the usual manner.
1390 // (4) Exceptions SHOULD NOT be thrown. EXCEPTIONS:
1391 // - If you're wrapping *broken* methods which make assumptions about
1392 // input data, such as that an argument refers to N bytes of data.
1393 // This is currently limited to cuserid(3) and encrypt(3).
1394 // - If you call functions which themselves generate exceptions.
1395 // This is the case for using NativeConvert, which will throw an
1396 // exception if an invalid/unsupported value is used.
1398 // Naming Conventions:
1399 // - Syscall method names should have the same name as the function being
1400 // wrapped (e.g. Syscall.read ==> read(2)). This allows people to
1401 // consult the appropriate man page if necessary.
1402 // - Methods need not have the same arguments IF this simplifies or
1403 // permits correct usage. The current example is syslog, in which
1404 // syslog(3)'s single `priority' argument is split into SyslogFacility
1405 // and SyslogLevel arguments.
1406 // - Type names (structures, classes, enumerations) are always PascalCased.
1407 // - Enumerations are named as <MethodName><ArgumentName>, and are located
1408 // in the Mono.Unix.Native namespace. For readability, if ArgumentName
1409 // is "cmd", use Command instead. For example, fcntl(2) takes a
1410 // FcntlCommand argument. This naming convention is to provide an
1411 // assocation between an enumeration and where it should be used, and
1412 // allows a single method to accept multiple different enumerations
1413 // (see mmap(2), which takes MmapProts and MmapFlags).
1414 // - EXCEPTION: if an enumeration is shared between multiple different
1415 // methods, AND/OR the "obvious" enumeration name conflicts with an
1416 // existing .NET type, a more appropriate name should be used.
1417 // Example: FilePermissions
1418 // - EXCEPTION: [Flags] enumerations should get plural names to follow
1419 // .NET name guidelines. Usually this doesn't result in a change
1420 // (OpenFlags is the `flags' parameter for open(2)), but it can
1421 // (mmap(2) prot ==> MmapProts, access(2) mode ==> AccessModes).
1422 // - Enumerations should have the [Map] and (optional) [Flags] attributes.
1423 // [Map] is required for make-map to find the type and generate the
1424 // appropriate NativeConvert conversion functions.
1425 // - Enumeration contents should match the original Unix names. This helps
1426 // with documentation (the existing man pages are still useful), and is
1427 // required for use with the make-map generation program.
1428 // - Structure names should be the PascalCased version of the actual
1429 // structure name (struct flock ==> Flock). Structure members should
1430 // have the same names, or a (reasonably) portable subset (Dirent being
1431 // the poster child for questionable members).
1432 // - Whether the managed type should be a reference type (class) or a
1433 // value type (struct) should be determined on a case-by-case basis:
1434 // if you ever need to be able to use NULL for it (such as with Dirent,
1435 // Group, Passwd, as these are method return types and `null' is used
1436 // to signify the end), it should be a reference type; otherwise, use
1437 // your discretion, and keep any expected usage patterns in mind.
1438 // - Syscall should be a Single Point Of Truth (SPOT). There should be
1439 // only ONE way to do anything. By convention, the Linux function names
1440 // are used, but that need not always be the case (use your discretion).
1441 // It SHOULD NOT be required that developers know what platform they're
1442 // on, and choose among a set of similar functions. In short, anything
1443 // that requires a platform check is BAD -- Mono.Unix is a wrapper, and
1444 // we can afford to clean things up whenever possible.
1446 // - Syscall.statfs: Solaris/Mac OS X provide statfs(2), Linux provides
1447 // statvfs(2). MonoPosixHelper will "thunk" between the two,
1448 // exporting a statvfs that works across platforms.
1449 // - Syscall.getfsent: Glibc export which Solaris lacks, while Solaris
1450 // instead provides getvfsent(3). MonoPosixHelper provides wrappers
1451 // to convert getvfsent(3) into Fstab data.
1452 // - Exception: If it isn't possible to cleanly wrap platforms, then the
1453 // method shouldn't be exported. The user will be expected to do their
1454 // own platform check and their own DllImports.
1455 // Examples: mount(2), umount(2), etc.
1456 // - Note: if a platform doesn't support a function AT ALL, the
1457 // MonoPosixHelper wrapper won't be compiled, resulting in a
1458 // EntryPointNotFoundException. This is also consistent with a missing
1459 // P/Invoke into libc.so.
1461 [CLSCompliant (false)]
1462 public sealed class Syscall : Stdlib
1464 new internal const string LIBC = "libc";
1465 private const string CRYPT = "crypt";
1467 private Syscall () {}
1473 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3),
1474 // aio_return(3), aio_suspend(3), aio_write(3)
1476 // Then update UnixStream.BeginRead to use the aio* functions.
1479 #region <attr/xattr.h> Declarations
1481 // <attr/xattr.h> -- COMPLETE
1485 // int setxattr (const char *path, const char *name,
1486 // const void *value, size_t size, int flags);
1487 [DllImport (MPH, SetLastError=true,
1488 EntryPoint="Mono_Posix_Syscall_setxattr")]
1489 public static extern int setxattr (
1490 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1492 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1493 string name, byte[] value, ulong size, XattrFlags flags);
1495 public static int setxattr (string path, string name, byte [] value, ulong size)
1497 return setxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1500 public static int setxattr (string path, string name, byte [] value, XattrFlags flags)
1502 return setxattr (path, name, value, (ulong) value.Length, flags);
1505 public static int setxattr (string path, string name, byte [] value)
1507 return setxattr (path, name, value, (ulong) value.Length);
1511 // int lsetxattr (const char *path, const char *name,
1512 // const void *value, size_t size, int flags);
1513 [DllImport (MPH, SetLastError=true,
1514 EntryPoint="Mono_Posix_Syscall_lsetxattr")]
1515 public static extern int lsetxattr (
1516 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1518 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1519 string name, byte[] value, ulong size, XattrFlags flags);
1521 public static int lsetxattr (string path, string name, byte [] value, ulong size)
1523 return lsetxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1526 public static int lsetxattr (string path, string name, byte [] value, XattrFlags flags)
1528 return lsetxattr (path, name, value, (ulong) value.Length, flags);
1531 public static int lsetxattr (string path, string name, byte [] value)
1533 return lsetxattr (path, name, value, (ulong) value.Length);
1537 // int fsetxattr (int fd, const char *name,
1538 // const void *value, size_t size, int flags);
1539 [DllImport (MPH, SetLastError=true,
1540 EntryPoint="Mono_Posix_Syscall_fsetxattr")]
1541 public static extern int fsetxattr (int fd,
1542 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1543 string name, byte[] value, ulong size, XattrFlags flags);
1545 public static int fsetxattr (int fd, string name, byte [] value, ulong size)
1547 return fsetxattr (fd, name, value, size, XattrFlags.XATTR_AUTO);
1550 public static int fsetxattr (int fd, string name, byte [] value, XattrFlags flags)
1552 return fsetxattr (fd, name, value, (ulong) value.Length, flags);
1555 public static int fsetxattr (int fd, string name, byte [] value)
1557 return fsetxattr (fd, name, value, (ulong) value.Length);
1561 // ssize_t getxattr (const char *path, const char *name,
1562 // void *value, size_t size);
1563 [DllImport (MPH, SetLastError=true,
1564 EntryPoint="Mono_Posix_Syscall_getxattr")]
1565 public static extern long getxattr (
1566 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1568 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1569 string name, byte[] value, ulong size);
1571 public static long getxattr (string path, string name, byte [] value)
1573 return getxattr (path, name, value, (ulong) value.Length);
1576 public static long getxattr (string path, string name, out byte [] value)
1579 long size = getxattr (path, name, value, 0);
1583 value = new byte [size];
1584 return getxattr (path, name, value, (ulong) size);
1588 // ssize_t lgetxattr (const char *path, const char *name,
1589 // void *value, size_t size);
1590 [DllImport (MPH, SetLastError=true,
1591 EntryPoint="Mono_Posix_Syscall_lgetxattr")]
1592 public static extern long lgetxattr (
1593 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1595 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1596 string name, byte[] value, ulong size);
1598 public static long lgetxattr (string path, string name, byte [] value)
1600 return lgetxattr (path, name, value, (ulong) value.Length);
1603 public static long lgetxattr (string path, string name, out byte [] value)
1606 long size = lgetxattr (path, name, value, 0);
1610 value = new byte [size];
1611 return lgetxattr (path, name, value, (ulong) size);
1615 // ssize_t fgetxattr (int fd, const char *name, void *value, size_t size);
1616 [DllImport (MPH, SetLastError=true,
1617 EntryPoint="Mono_Posix_Syscall_fgetxattr")]
1618 public static extern long fgetxattr (int fd,
1619 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1620 string name, byte[] value, ulong size);
1622 public static long fgetxattr (int fd, string name, byte [] value)
1624 return fgetxattr (fd, name, value, (ulong) value.Length);
1627 public static long fgetxattr (int fd, string name, out byte [] value)
1630 long size = fgetxattr (fd, name, value, 0);
1634 value = new byte [size];
1635 return fgetxattr (fd, name, value, (ulong) size);
1639 // ssize_t listxattr (const char *path, char *list, size_t size);
1640 [DllImport (MPH, SetLastError=true,
1641 EntryPoint="Mono_Posix_Syscall_listxattr")]
1642 public static extern long listxattr (
1643 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1644 string path, byte[] list, ulong size);
1646 // Slight modification: returns 0 on success, negative on error
1647 public static long listxattr (string path, Encoding encoding, out string [] values)
1650 long size = listxattr (path, null, 0);
1652 values = new string [0];
1656 byte[] list = new byte [size];
1657 long ret = listxattr (path, list, (ulong) size);
1661 GetValues (list, encoding, out values);
1665 public static long listxattr (string path, out string[] values)
1667 return listxattr (path, UnixEncoding.Instance, out values);
1670 private static void GetValues (byte[] list, Encoding encoding, out string[] values)
1673 for (int i = 0; i < list.Length; ++i)
1677 values = new string [num_values];
1680 for (int i = 0; i < list.Length; ++i) {
1681 if (list [i] == 0) {
1682 values [num_values++] = encoding.GetString (list, str_start, i - str_start);
1689 // ssize_t llistxattr (const char *path, char *list, size_t size);
1690 [DllImport (MPH, SetLastError=true,
1691 EntryPoint="Mono_Posix_Syscall_llistxattr")]
1692 public static extern long llistxattr (
1693 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1694 string path, byte[] list, ulong size);
1696 // Slight modification: returns 0 on success, negative on error
1697 public static long llistxattr (string path, Encoding encoding, out string [] values)
1700 long size = llistxattr (path, null, 0);
1702 values = new string [0];
1706 byte[] list = new byte [size];
1707 long ret = llistxattr (path, list, (ulong) size);
1711 GetValues (list, encoding, out values);
1715 public static long llistxattr (string path, out string[] values)
1717 return llistxattr (path, UnixEncoding.Instance, out values);
1721 // ssize_t flistxattr (int fd, char *list, size_t size);
1722 [DllImport (MPH, SetLastError=true,
1723 EntryPoint="Mono_Posix_Syscall_flistxattr")]
1724 public static extern long flistxattr (int fd, byte[] list, ulong size);
1726 // Slight modification: returns 0 on success, negative on error
1727 public static long flistxattr (int fd, Encoding encoding, out string [] values)
1730 long size = flistxattr (fd, null, 0);
1732 values = new string [0];
1736 byte[] list = new byte [size];
1737 long ret = flistxattr (fd, list, (ulong) size);
1741 GetValues (list, encoding, out values);
1745 public static long flistxattr (int fd, out string[] values)
1747 return flistxattr (fd, UnixEncoding.Instance, out values);
1750 [DllImport (MPH, SetLastError=true,
1751 EntryPoint="Mono_Posix_Syscall_removexattr")]
1752 public static extern int removexattr (
1753 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1755 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1758 [DllImport (MPH, SetLastError=true,
1759 EntryPoint="Mono_Posix_Syscall_lremovexattr")]
1760 public static extern int lremovexattr (
1761 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1763 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1766 [DllImport (MPH, SetLastError=true,
1767 EntryPoint="Mono_Posix_Syscall_fremovexattr")]
1768 public static extern int fremovexattr (int fd,
1769 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1773 #region <dirent.h> Declarations
1777 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1779 [DllImport (LIBC, SetLastError=true)]
1780 public static extern IntPtr opendir (
1781 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1784 [DllImport (LIBC, SetLastError=true)]
1785 public static extern int closedir (IntPtr dir);
1788 // void seekdir (DIR *dir, off_t offset);
1789 // Slight modification. Returns -1 on error, 0 on success.
1790 [DllImport (MPH, SetLastError=true,
1791 EntryPoint="Mono_Posix_Syscall_seekdir")]
1792 public static extern int seekdir (IntPtr dir, long offset);
1795 // off_t telldir(DIR *dir);
1796 [DllImport (MPH, SetLastError=true,
1797 EntryPoint="Mono_Posix_Syscall_telldir")]
1798 public static extern long telldir (IntPtr dir);
1800 [DllImport (MPH, SetLastError=true,
1801 EntryPoint="Mono_Posix_Syscall_rewinddir")]
1802 public static extern int rewinddir (IntPtr dir);
1804 private struct _Dirent {
1805 public /* ino_t */ ulong d_ino;
1806 public /* off_t */ long d_off;
1807 public ushort d_reclen;
1809 public IntPtr d_name;
1812 private static void CopyDirent (Dirent to, ref _Dirent from)
1815 to.d_ino = from.d_ino;
1816 to.d_off = from.d_off;
1817 to.d_reclen = from.d_reclen;
1818 to.d_type = from.d_type;
1819 to.d_name = UnixMarshal.PtrToString (from.d_name);
1822 Stdlib.free (from.d_name);
1823 from.d_name = IntPtr.Zero;
1827 [DllImport (MPH, SetLastError=true,
1828 EntryPoint="Mono_Posix_Syscall_readdir")]
1829 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
1831 public static Dirent readdir (IntPtr dir)
1834 int r = sys_readdir (dir, out dentry);
1837 Dirent d = new Dirent ();
1838 CopyDirent (d, ref dentry);
1842 [DllImport (MPH, SetLastError=true,
1843 EntryPoint="Mono_Posix_Syscall_readdir_r")]
1844 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
1846 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
1852 entry.d_name = null;
1855 int r = sys_readdir_r (dirp, out _d, out result);
1857 if (r == 0 && result != IntPtr.Zero) {
1858 CopyDirent (entry, ref _d);
1864 [DllImport (LIBC, SetLastError=true)]
1865 public static extern int dirfd (IntPtr dir);
1868 #region <fcntl.h> Declarations
1870 // <fcntl.h> -- COMPLETE
1873 [DllImport (MPH, SetLastError=true,
1874 EntryPoint="Mono_Posix_Syscall_fcntl")]
1875 public static extern int fcntl (int fd, FcntlCommand cmd);
1877 [DllImport (MPH, SetLastError=true,
1878 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
1879 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
1881 public static int fcntl (int fd, FcntlCommand cmd, DirectoryNotifyFlags arg)
1883 if (cmd != FcntlCommand.F_NOTIFY) {
1884 SetLastError (Errno.EINVAL);
1887 long _arg = NativeConvert.FromDirectoryNotifyFlags (arg);
1888 return fcntl (fd, FcntlCommand.F_NOTIFY, _arg);
1891 [DllImport (MPH, SetLastError=true,
1892 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
1893 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
1895 [DllImport (MPH, SetLastError=true,
1896 EntryPoint="Mono_Posix_Syscall_open")]
1897 public static extern int open (
1898 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1899 string pathname, OpenFlags flags);
1902 // int open(const char *pathname, int flags, mode_t mode);
1903 [DllImport (MPH, SetLastError=true,
1904 EntryPoint="Mono_Posix_Syscall_open_mode")]
1905 public static extern int open (
1906 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1907 string pathname, OpenFlags flags, FilePermissions mode);
1910 // int creat(const char *pathname, mode_t mode);
1911 [DllImport (MPH, SetLastError=true,
1912 EntryPoint="Mono_Posix_Syscall_creat")]
1913 public static extern int creat (
1914 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1915 string pathname, FilePermissions mode);
1918 // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
1919 [DllImport (MPH, SetLastError=true,
1920 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
1921 public static extern int posix_fadvise (int fd, long offset,
1922 long len, PosixFadviseAdvice advice);
1924 // posix_fallocate(P)
1925 // int posix_fallocate(int fd, off_t offset, size_t len);
1926 [DllImport (MPH, SetLastError=true,
1927 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
1928 public static extern int posix_fallocate (int fd, long offset, ulong len);
1931 #region <fstab.h> Declarations
1933 // <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);
2064 private struct _Group
2066 public IntPtr gr_name;
2067 public IntPtr gr_passwd;
2068 public /* gid_t */ uint gr_gid;
2069 public int _gr_nmem_;
2070 public IntPtr gr_mem;
2071 public IntPtr _gr_buf_;
2074 private static void CopyGroup (Group to, ref _Group from)
2077 to.gr_gid = from.gr_gid;
2078 to.gr_name = UnixMarshal.PtrToString (from.gr_name);
2079 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
2080 to.gr_mem = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
2083 Stdlib.free (from.gr_mem);
2084 Stdlib.free (from._gr_buf_);
2085 from.gr_mem = IntPtr.Zero;
2086 from._gr_buf_ = IntPtr.Zero;
2090 internal static object grp_lock = new object ();
2092 [DllImport (MPH, SetLastError=true,
2093 EntryPoint="Mono_Posix_Syscall_getgrnam")]
2094 private static extern int sys_getgrnam (string name, out _Group group);
2096 public static Group getgrnam (string name)
2101 r = sys_getgrnam (name, out group);
2105 Group gr = new Group ();
2106 CopyGroup (gr, ref group);
2111 // struct group *getgrgid(gid_t gid);
2112 [DllImport (MPH, SetLastError=true,
2113 EntryPoint="Mono_Posix_Syscall_getgrgid")]
2114 private static extern int sys_getgrgid (uint uid, out _Group group);
2116 public static Group getgrgid (uint uid)
2121 r = sys_getgrgid (uid, out group);
2125 Group gr = new Group ();
2126 CopyGroup (gr, ref group);
2130 [DllImport (MPH, SetLastError=true,
2131 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
2132 private static extern int sys_getgrnam_r (
2133 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2134 string name, out _Group grbuf, out IntPtr grbufp);
2136 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
2141 int r = sys_getgrnam_r (name, out group, out _grbufp);
2142 if (r == 0 && _grbufp != IntPtr.Zero) {
2143 CopyGroup (grbuf, ref group);
2150 // int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
2151 // size_t buflen, struct group **gbufp);
2152 [DllImport (MPH, SetLastError=true,
2153 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
2154 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
2156 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
2161 int r = sys_getgrgid_r (uid, out group, out _grbufp);
2162 if (r == 0 && _grbufp != IntPtr.Zero) {
2163 CopyGroup (grbuf, ref group);
2169 [DllImport (MPH, SetLastError=true,
2170 EntryPoint="Mono_Posix_Syscall_getgrent")]
2171 private static extern int sys_getgrent (out _Group grbuf);
2173 public static Group getgrent ()
2178 r = sys_getgrent (out group);
2182 Group gr = new Group();
2183 CopyGroup (gr, ref group);
2187 [DllImport (MPH, SetLastError=true,
2188 EntryPoint="Mono_Posix_Syscall_setgrent")]
2189 private static extern int sys_setgrent ();
2191 public static int setgrent ()
2194 return sys_setgrent ();
2198 [DllImport (MPH, SetLastError=true,
2199 EntryPoint="Mono_Posix_Syscall_endgrent")]
2200 private static extern int sys_endgrent ();
2202 public static int endgrent ()
2205 return sys_endgrent ();
2209 [DllImport (MPH, SetLastError=true,
2210 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
2211 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
2213 public static Group fgetgrent (IntPtr stream)
2218 r = sys_fgetgrent (stream, out group);
2222 Group gr = new Group ();
2223 CopyGroup (gr, ref group);
2228 #region <pwd.h> Declarations
2232 // TODO: putpwent(3), fgetpwent_r()
2234 // SKIPPING: getpw(3): it's dangerous. Use getpwuid(3) instead.
2236 private struct _Passwd
2238 public IntPtr pw_name;
2239 public IntPtr pw_passwd;
2240 public /* uid_t */ uint pw_uid;
2241 public /* gid_t */ uint pw_gid;
2242 public IntPtr pw_gecos;
2243 public IntPtr pw_dir;
2244 public IntPtr pw_shell;
2245 public IntPtr _pw_buf_;
2248 private static void CopyPasswd (Passwd to, ref _Passwd from)
2251 to.pw_name = UnixMarshal.PtrToString (from.pw_name);
2252 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
2253 to.pw_uid = from.pw_uid;
2254 to.pw_gid = from.pw_gid;
2255 to.pw_gecos = UnixMarshal.PtrToString (from.pw_gecos);
2256 to.pw_dir = UnixMarshal.PtrToString (from.pw_dir);
2257 to.pw_shell = UnixMarshal.PtrToString (from.pw_shell);
2260 Stdlib.free (from._pw_buf_);
2261 from._pw_buf_ = IntPtr.Zero;
2265 internal static object pwd_lock = new object ();
2267 [DllImport (MPH, SetLastError=true,
2268 EntryPoint="Mono_Posix_Syscall_getpwnam")]
2269 private static extern int sys_getpwnam (string name, out _Passwd passwd);
2271 public static Passwd getpwnam (string name)
2276 r = sys_getpwnam (name, out passwd);
2280 Passwd pw = new Passwd ();
2281 CopyPasswd (pw, ref passwd);
2286 // struct passwd *getpwnuid(uid_t uid);
2287 [DllImport (MPH, SetLastError=true,
2288 EntryPoint="Mono_Posix_Syscall_getpwuid")]
2289 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
2291 public static Passwd getpwuid (uint uid)
2296 r = sys_getpwuid (uid, out passwd);
2300 Passwd pw = new Passwd ();
2301 CopyPasswd (pw, ref passwd);
2305 [DllImport (MPH, SetLastError=true,
2306 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
2307 private static extern int sys_getpwnam_r (
2308 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2309 string name, out _Passwd pwbuf, out IntPtr pwbufp);
2311 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
2316 int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
2317 if (r == 0 && _pwbufp != IntPtr.Zero) {
2318 CopyPasswd (pwbuf, ref passwd);
2325 // int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
2326 // buflen, struct passwd **pwbufp);
2327 [DllImport (MPH, SetLastError=true,
2328 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
2329 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
2331 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
2336 int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
2337 if (r == 0 && _pwbufp != IntPtr.Zero) {
2338 CopyPasswd (pwbuf, ref passwd);
2344 [DllImport (MPH, SetLastError=true,
2345 EntryPoint="Mono_Posix_Syscall_getpwent")]
2346 private static extern int sys_getpwent (out _Passwd pwbuf);
2348 public static Passwd getpwent ()
2353 r = sys_getpwent (out passwd);
2357 Passwd pw = new Passwd ();
2358 CopyPasswd (pw, ref passwd);
2362 [DllImport (MPH, SetLastError=true,
2363 EntryPoint="Mono_Posix_Syscall_setpwent")]
2364 private static extern int sys_setpwent ();
2366 public static int setpwent ()
2369 return sys_setpwent ();
2373 [DllImport (MPH, SetLastError=true,
2374 EntryPoint="Mono_Posix_Syscall_endpwent")]
2375 private static extern int sys_endpwent ();
2377 public static int endpwent ()
2380 return sys_endpwent ();
2384 [DllImport (MPH, SetLastError=true,
2385 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
2386 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
2388 public static Passwd fgetpwent (IntPtr stream)
2393 r = sys_fgetpwent (stream, out passwd);
2397 Passwd pw = new Passwd ();
2398 CopyPasswd (pw, ref passwd);
2403 #region <signal.h> Declarations
2407 [DllImport (MPH, SetLastError=true,
2408 EntryPoint="Mono_Posix_Syscall_psignal")]
2409 private static extern int psignal (int sig, string s);
2411 public static int psignal (Signum sig, string s)
2413 int signum = NativeConvert.FromSignum (sig);
2414 return psignal (signum, s);
2418 // int kill(pid_t pid, int sig);
2419 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
2420 private static extern int sys_kill (int pid, int sig);
2422 public static int kill (int pid, Signum sig)
2424 int _sig = NativeConvert.FromSignum (sig);
2425 return sys_kill (pid, _sig);
2428 private static object signal_lock = new object ();
2430 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
2431 private static extern IntPtr sys_strsignal (int sig);
2433 public static string strsignal (Signum sig)
2435 int s = NativeConvert.FromSignum (sig);
2436 lock (signal_lock) {
2437 IntPtr r = sys_strsignal (s);
2438 return UnixMarshal.PtrToString (r);
2442 // TODO: sigaction(2)
2443 // TODO: sigsuspend(2)
2444 // TODO: sigpending(2)
2448 #region <stdio.h> Declarations
2452 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
2453 private static extern int _L_ctermid ();
2455 public static readonly int L_ctermid = _L_ctermid ();
2457 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
2458 private static extern int _L_cuserid ();
2460 public static readonly int L_cuserid = _L_cuserid ();
2462 internal static object getlogin_lock = new object ();
2464 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
2465 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
2467 [Obsolete ("\"Nobody knows precisely what cuserid() does... " +
2468 "DO NOT USE cuserid().\n" +
2469 "`string' must hold L_cuserid characters. Use getlogin_r instead.")]
2470 public static string cuserid (StringBuilder @string)
2472 if (@string.Capacity < L_cuserid) {
2473 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
2475 lock (getlogin_lock) {
2476 IntPtr r = sys_cuserid (@string);
2477 return UnixMarshal.PtrToString (r);
2483 #region <stdlib.h> Declarations
2487 [DllImport (LIBC, SetLastError=true)]
2488 public static extern int mkstemp (StringBuilder template);
2490 [DllImport (LIBC, SetLastError=true)]
2491 public static extern int ttyslot ();
2493 [DllImport (MPH, SetLastError=true,
2494 EntryPoint="Mono_Posix_Syscall_setkey")]
2495 public static extern int setkey (string key);
2499 #region <string.h> Declarations
2505 // int strerror_r(int errnum, char *buf, size_t n);
2506 [DllImport (MPH, SetLastError=true,
2507 EntryPoint="Mono_Posix_Syscall_strerror_r")]
2508 private static extern int sys_strerror_r (int errnum,
2509 [Out] StringBuilder buf, ulong n);
2511 public static int strerror_r (Errno errnum, StringBuilder buf, ulong n)
2513 int e = NativeConvert.FromErrno (errnum);
2514 return sys_strerror_r (e, buf, n);
2517 public static int strerror_r (Errno errnum, StringBuilder buf)
2519 return strerror_r (errnum, buf, (ulong) buf.Capacity);
2524 #region <sys/mman.h> Declarations
2530 // int posix_madvise(void *addr, size_t len, int advice);
2531 [DllImport (MPH, SetLastError=true,
2532 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
2533 public static extern int posix_madvise (IntPtr addr, ulong len,
2534 PosixMadviseAdvice advice);
2536 public static readonly IntPtr MAP_FAILED = unchecked((IntPtr)(-1));
2538 [DllImport (MPH, SetLastError=true,
2539 EntryPoint="Mono_Posix_Syscall_mmap")]
2540 public static extern IntPtr mmap (IntPtr start, ulong length,
2541 MmapProts prot, MmapFlags flags, int fd, long offset);
2543 [DllImport (MPH, SetLastError=true,
2544 EntryPoint="Mono_Posix_Syscall_munmap")]
2545 public static extern int munmap (IntPtr start, ulong length);
2547 [DllImport (MPH, SetLastError=true,
2548 EntryPoint="Mono_Posix_Syscall_mprotect")]
2549 public static extern int mprotect (IntPtr start, ulong len, MmapProts prot);
2551 [DllImport (MPH, SetLastError=true,
2552 EntryPoint="Mono_Posix_Syscall_msync")]
2553 public static extern int msync (IntPtr start, ulong len, MsyncFlags flags);
2555 [DllImport (MPH, SetLastError=true,
2556 EntryPoint="Mono_Posix_Syscall_mlock")]
2557 public static extern int mlock (IntPtr start, ulong len);
2559 [DllImport (MPH, SetLastError=true,
2560 EntryPoint="Mono_Posix_Syscall_munlock")]
2561 public static extern int munlock (IntPtr start, ulong len);
2563 [DllImport (LIBC, SetLastError=true, EntryPoint="mlockall")]
2564 private static extern int sys_mlockall (int flags);
2566 public static int mlockall (MlockallFlags flags)
2568 int _flags = NativeConvert.FromMlockallFlags (flags);
2569 return sys_mlockall (_flags);
2572 [DllImport (LIBC, SetLastError=true)]
2573 public static extern int munlockall ();
2575 [DllImport (MPH, SetLastError=true,
2576 EntryPoint="Mono_Posix_Syscall_mremap")]
2577 public static extern IntPtr mremap (IntPtr old_address, ulong old_size,
2578 ulong new_size, MremapFlags flags);
2580 [DllImport (MPH, SetLastError=true,
2581 EntryPoint="Mono_Posix_Syscall_mincore")]
2582 public static extern int mincore (IntPtr start, ulong length, byte[] vec);
2584 [DllImport (MPH, SetLastError=true,
2585 EntryPoint="Mono_Posix_Syscall_remap_file_pages")]
2586 public static extern int remap_file_pages (IntPtr start, ulong size,
2587 MmapProts prot, long pgoff, MmapFlags flags);
2591 #region <sys/poll.h> Declarations
2593 // <sys/poll.h> -- COMPLETE
2596 private struct _pollfd {
2598 public short events;
2599 public short revents;
2602 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
2603 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
2605 public static int poll (Pollfd [] fds, uint nfds, int timeout)
2607 if (fds.Length < nfds)
2608 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
2610 _pollfd[] send = new _pollfd[nfds];
2612 for (int i = 0; i < send.Length; i++) {
2613 send [i].fd = fds [i].fd;
2614 send [i].events = NativeConvert.FromPollEvents (fds [i].events);
2617 int r = sys_poll (send, nfds, timeout);
2619 for (int i = 0; i < send.Length; i++) {
2620 fds [i].revents = NativeConvert.ToPollEvents (send [i].revents);
2626 public static int poll (Pollfd [] fds, int timeout)
2628 return poll (fds, (uint) fds.Length, timeout);
2641 // TODO: setrlimit(2)
2642 // TODO: getrlimit(2)
2643 // TODO: getrusage(2)
2647 #region <sys/sendfile.h> Declarations
2649 // <sys/sendfile.h> -- COMPLETE
2652 [DllImport (MPH, SetLastError=true,
2653 EntryPoint="Mono_Posix_Syscall_sendfile")]
2654 public static extern long sendfile (int out_fd, int in_fd,
2655 ref long offset, ulong count);
2659 #region <sys/stat.h> Declarations
2661 // <sys/stat.h> -- COMPLETE
2663 [DllImport (MPH, SetLastError=true,
2664 EntryPoint="Mono_Posix_Syscall_stat")]
2665 public static extern int stat (
2666 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2667 string file_name, out Stat buf);
2669 [DllImport (MPH, SetLastError=true,
2670 EntryPoint="Mono_Posix_Syscall_fstat")]
2671 public static extern int fstat (int filedes, out Stat buf);
2673 [DllImport (MPH, SetLastError=true,
2674 EntryPoint="Mono_Posix_Syscall_lstat")]
2675 public static extern int lstat (
2676 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2677 string file_name, out Stat buf);
2680 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
2681 // All take FilePermissions
2684 // int chmod(const char *path, mode_t mode);
2685 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
2686 private static extern int sys_chmod (
2687 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2688 string path, uint mode);
2690 public static int chmod (string path, FilePermissions mode)
2692 uint _mode = NativeConvert.FromFilePermissions (mode);
2693 return sys_chmod (path, _mode);
2697 // int chmod(int filedes, mode_t mode);
2698 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
2699 private static extern int sys_fchmod (int filedes, uint mode);
2701 public static int fchmod (int filedes, FilePermissions mode)
2703 uint _mode = NativeConvert.FromFilePermissions (mode);
2704 return sys_fchmod (filedes, _mode);
2708 // mode_t umask(mode_t mask);
2709 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
2710 private static extern uint sys_umask (uint mask);
2712 public static FilePermissions umask (FilePermissions mask)
2714 uint _mask = NativeConvert.FromFilePermissions (mask);
2715 uint r = sys_umask (_mask);
2716 return NativeConvert.ToFilePermissions (r);
2720 // int mkdir(const char *pathname, mode_t mode);
2721 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
2722 private static extern int sys_mkdir (
2723 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2724 string oldpath, uint mode);
2726 public static int mkdir (string oldpath, FilePermissions mode)
2728 uint _mode = NativeConvert.FromFilePermissions (mode);
2729 return sys_mkdir (oldpath, _mode);
2733 // int mknod (const char *pathname, mode_t mode, dev_t dev);
2734 [DllImport (MPH, SetLastError=true,
2735 EntryPoint="Mono_Posix_Syscall_mknod")]
2736 public static extern int mknod (
2737 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2738 string pathname, FilePermissions mode, ulong dev);
2741 // int mkfifo(const char *pathname, mode_t mode);
2742 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
2743 private static extern int sys_mkfifo (
2744 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2745 string pathname, uint mode);
2747 public static int mkfifo (string pathname, FilePermissions mode)
2749 uint _mode = NativeConvert.FromFilePermissions (mode);
2750 return sys_mkfifo (pathname, _mode);
2755 #region <sys/stat.h> Declarations
2760 [DllImport (MPH, SetLastError=true,
2761 EntryPoint="Mono_Posix_Syscall_statvfs")]
2762 public static extern int statvfs (
2763 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2764 string path, out Statvfs buf);
2766 [DllImport (MPH, SetLastError=true,
2767 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
2768 public static extern int fstatvfs (int fd, out Statvfs buf);
2772 #region <sys/time.h> Declarations
2776 // TODO: adjtime(), getitimer(2), setitimer(2), lutimes(), futimes()
2778 [DllImport (MPH, SetLastError=true,
2779 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2780 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
2782 [DllImport (MPH, SetLastError=true,
2783 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2784 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
2786 public static int gettimeofday (out Timeval tv)
2788 return gettimeofday (out tv, IntPtr.Zero);
2791 [DllImport (MPH, SetLastError=true,
2792 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2793 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
2795 public static int gettimeofday (out Timezone tz)
2797 return gettimeofday (IntPtr.Zero, out tz);
2800 [DllImport (MPH, SetLastError=true,
2801 EntryPoint="Mono_Posix_Syscall_settimeofday")]
2802 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
2804 [DllImport (MPH, SetLastError=true,
2805 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2806 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
2808 public static int settimeofday (ref Timeval tv)
2810 return settimeofday (ref tv, IntPtr.Zero);
2813 [DllImport (MPH, SetLastError=true,
2814 EntryPoint="Mono_Posix_Syscall_utimes")]
2815 public static extern int utimes (
2816 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2817 string filename, ref Timeval tvp);
2839 #region <sys/wait.h> Declarations
2845 // pid_t wait(int *status);
2846 [DllImport (LIBC, SetLastError=true)]
2847 public static extern int wait (out int status);
2850 // pid_t waitpid(pid_t pid, int *status, int options);
2851 [DllImport (LIBC, SetLastError=true)]
2852 private static extern int waitpid (int pid, out int status, int options);
2854 public static int waitpid (int pid, out int status, WaitOptions options)
2856 int _options = NativeConvert.FromWaitOptions (options);
2857 return waitpid (pid, out status, _options);
2860 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
2861 private static extern int _WIFEXITED (int status);
2863 public static bool WIFEXITED (int status)
2865 return _WIFEXITED (status) != 0;
2868 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
2869 public static extern int WEXITSTATUS (int status);
2871 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
2872 private static extern int _WIFSIGNALED (int status);
2874 public static bool WIFSIGNALED (int status)
2876 return _WIFSIGNALED (status) != 0;
2879 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
2880 private static extern int _WTERMSIG (int status);
2882 public static Signum WTERMSIG (int status)
2884 int r = _WTERMSIG (status);
2885 return NativeConvert.ToSignum (r);
2888 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
2889 private static extern int _WIFSTOPPED (int status);
2891 public static bool WIFSTOPPED (int status)
2893 return _WIFSTOPPED (status) != 0;
2896 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
2897 private static extern int _WSTOPSIG (int status);
2899 public static Signum WSTOPSIG (int status)
2901 int r = _WSTOPSIG (status);
2902 return NativeConvert.ToSignum (r);
2911 #region <syslog.h> Declarations
2916 [DllImport (MPH, SetLastError=true,
2917 EntryPoint="Mono_Posix_Syscall_openlog")]
2918 private static extern int sys_openlog (IntPtr ident, int option, int facility);
2920 public static int openlog (IntPtr ident, SyslogOptions option,
2921 SyslogFacility defaultFacility)
2923 int _option = NativeConvert.FromSyslogOptions (option);
2924 int _facility = NativeConvert.FromSyslogFacility (defaultFacility);
2926 return sys_openlog (ident, _option, _facility);
2929 [DllImport (MPH, SetLastError=true,
2930 EntryPoint="Mono_Posix_Syscall_syslog")]
2931 private static extern int sys_syslog (int priority, string message);
2933 public static int syslog (SyslogFacility facility, SyslogLevel level, string message)
2935 int _facility = NativeConvert.FromSyslogFacility (facility);
2936 int _level = NativeConvert.FromSyslogLevel (level);
2937 return sys_syslog (_facility | _level, GetSyslogMessage (message));
2940 public static int syslog (SyslogLevel level, string message)
2942 int _level = NativeConvert.FromSyslogLevel (level);
2943 return sys_syslog (_level, GetSyslogMessage (message));
2946 private static string GetSyslogMessage (string message)
2948 return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
2951 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2952 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
2953 public static int syslog (SyslogFacility facility, SyslogLevel level,
2954 string format, params object[] parameters)
2956 int _facility = NativeConvert.FromSyslogFacility (facility);
2957 int _level = NativeConvert.FromSyslogLevel (level);
2959 object[] _parameters = new object[checked(parameters.Length+2)];
2960 _parameters [0] = _facility | _level;
2961 _parameters [1] = format;
2962 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2963 return (int) XPrintfFunctions.syslog (_parameters);
2966 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2967 "Use syslog(SyslogLevel, string) instead.")]
2968 public static int syslog (SyslogLevel level, string format,
2969 params object[] parameters)
2971 int _level = NativeConvert.FromSyslogLevel (level);
2973 object[] _parameters = new object[checked(parameters.Length+2)];
2974 _parameters [0] = _level;
2975 _parameters [1] = format;
2976 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2977 return (int) XPrintfFunctions.syslog (_parameters);
2980 [DllImport (MPH, SetLastError=true,
2981 EntryPoint="Mono_Posix_Syscall_closelog")]
2982 public static extern int closelog ();
2984 [DllImport (LIBC, SetLastError=true, EntryPoint="setlogmask")]
2985 private static extern int sys_setlogmask (int mask);
2987 public static int setlogmask (SyslogLevel mask)
2989 int _mask = NativeConvert.FromSyslogLevel (mask);
2990 return sys_setlogmask (_mask);
2995 #region <time.h> Declarations
3002 // int stime(time_t *t);
3003 [DllImport (MPH, SetLastError=true,
3004 EntryPoint="Mono_Posix_Syscall_stime")]
3005 public static extern int stime (ref long t);
3008 // time_t time(time_t *t);
3009 [DllImport (MPH, SetLastError=true,
3010 EntryPoint="Mono_Posix_Syscall_time")]
3011 public static extern long time (out long t);
3021 #region <unistd.h> Declarations
3025 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
3026 // other TODOs listed below.
3028 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
3029 private static extern int sys_access (
3030 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3031 string pathname, int mode);
3033 public static int access (string pathname, AccessModes mode)
3035 int _mode = NativeConvert.FromAccessModes (mode);
3036 return sys_access (pathname, _mode);
3040 // off_t lseek(int filedes, off_t offset, int whence);
3041 [DllImport (MPH, SetLastError=true,
3042 EntryPoint="Mono_Posix_Syscall_lseek")]
3043 private static extern long sys_lseek (int fd, long offset, int whence);
3045 public static long lseek (int fd, long offset, SeekFlags whence)
3047 short _whence = NativeConvert.FromSeekFlags (whence);
3048 return sys_lseek (fd, offset, _whence);
3051 [DllImport (LIBC, SetLastError=true)]
3052 public static extern int close (int fd);
3055 // ssize_t read(int fd, void *buf, size_t count);
3056 [DllImport (MPH, SetLastError=true,
3057 EntryPoint="Mono_Posix_Syscall_read")]
3058 public static extern long read (int fd, IntPtr buf, ulong count);
3060 public static unsafe long read (int fd, void *buf, ulong count)
3062 return read (fd, (IntPtr) buf, count);
3066 // ssize_t write(int fd, const void *buf, size_t count);
3067 [DllImport (MPH, SetLastError=true,
3068 EntryPoint="Mono_Posix_Syscall_write")]
3069 public static extern long write (int fd, IntPtr buf, ulong count);
3071 public static unsafe long write (int fd, void *buf, ulong count)
3073 return write (fd, (IntPtr) buf, count);
3077 // ssize_t pread(int fd, void *buf, size_t count, off_t offset);
3078 [DllImport (MPH, SetLastError=true,
3079 EntryPoint="Mono_Posix_Syscall_pread")]
3080 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
3082 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
3084 return pread (fd, (IntPtr) buf, count, offset);
3088 // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
3089 [DllImport (MPH, SetLastError=true,
3090 EntryPoint="Mono_Posix_Syscall_pwrite")]
3091 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
3093 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
3095 return pwrite (fd, (IntPtr) buf, count, offset);
3098 [DllImport (MPH, SetLastError=true,
3099 EntryPoint="Mono_Posix_Syscall_pipe")]
3100 public static extern int pipe (out int reading, out int writing);
3102 public static int pipe (int[] filedes)
3104 if (filedes == null || filedes.Length != 2) {
3108 int reading, writing;
3109 int r = pipe (out reading, out writing);
3110 filedes[0] = reading;
3111 filedes[1] = writing;
3115 [DllImport (LIBC, SetLastError=true)]
3116 public static extern uint alarm (uint seconds);
3118 [DllImport (LIBC, SetLastError=true)]
3119 public static extern uint sleep (uint seconds);
3121 [DllImport (LIBC, SetLastError=true)]
3122 public static extern uint ualarm (uint usecs, uint interval);
3124 [DllImport (LIBC, SetLastError=true)]
3125 public static extern int pause ();
3128 // int chown(const char *path, uid_t owner, gid_t group);
3129 [DllImport (LIBC, SetLastError=true)]
3130 public static extern int chown (
3131 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3132 string path, uint owner, uint group);
3135 // int fchown(int fd, uid_t owner, gid_t group);
3136 [DllImport (LIBC, SetLastError=true)]
3137 public static extern int fchown (int fd, uint owner, uint group);
3140 // int lchown(const char *path, uid_t owner, gid_t group);
3141 [DllImport (LIBC, SetLastError=true)]
3142 public static extern int lchown (
3143 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3144 string path, uint owner, uint group);
3146 [DllImport (LIBC, SetLastError=true)]
3147 public static extern int chdir (
3148 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3151 [DllImport (LIBC, SetLastError=true)]
3152 public static extern int fchdir (int fd);
3155 // char *getcwd(char *buf, size_t size);
3156 [DllImport (MPH, SetLastError=true,
3157 EntryPoint="Mono_Posix_Syscall_getcwd")]
3158 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
3160 public static StringBuilder getcwd (StringBuilder buf)
3162 getcwd (buf, (ulong) buf.Capacity);
3166 // getwd(2) is deprecated; don't expose it.
3168 [DllImport (LIBC, SetLastError=true)]
3169 public static extern int dup (int fd);
3171 [DllImport (LIBC, SetLastError=true)]
3172 public static extern int dup2 (int fd, int fd2);
3174 // TODO: does Mono marshal arrays properly?
3175 [DllImport (LIBC, SetLastError=true)]
3176 public static extern int execve (
3177 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3178 string path, string[] argv, string[] envp);
3180 [DllImport (LIBC, SetLastError=true)]
3181 public static extern int fexecve (int fd, string[] argv, string[] envp);
3183 [DllImport (LIBC, SetLastError=true)]
3184 public static extern int execv (
3185 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3186 string path, string[] argv);
3188 // TODO: execle, execl, execlp
3189 [DllImport (LIBC, SetLastError=true)]
3190 public static extern int execvp (
3191 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3192 string path, string[] argv);
3194 [DllImport (LIBC, SetLastError=true)]
3195 public static extern int nice (int inc);
3197 [DllImport (LIBC, SetLastError=true)]
3198 [CLSCompliant (false)]
3199 public static extern int _exit (int status);
3201 [DllImport (MPH, SetLastError=true,
3202 EntryPoint="Mono_Posix_Syscall_fpathconf")]
3203 public static extern long fpathconf (int filedes, PathconfName name, Errno defaultError);
3205 public static long fpathconf (int filedes, PathconfName name)
3207 return fpathconf (filedes, name, (Errno) 0);
3210 [DllImport (MPH, SetLastError=true,
3211 EntryPoint="Mono_Posix_Syscall_pathconf")]
3212 public static extern long pathconf (
3213 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3214 string path, PathconfName name, Errno defaultError);
3216 public static long pathconf (string path, PathconfName name)
3218 return pathconf (path, name, (Errno) 0);
3221 [DllImport (MPH, SetLastError=true,
3222 EntryPoint="Mono_Posix_Syscall_sysconf")]
3223 public static extern long sysconf (SysconfName name, Errno defaultError);
3225 public static long sysconf (SysconfName name)
3227 return sysconf (name, (Errno) 0);
3231 // size_t confstr(int name, char *buf, size_t len);
3232 [DllImport (MPH, SetLastError=true,
3233 EntryPoint="Mono_Posix_Syscall_confstr")]
3234 public static extern ulong confstr (ConfstrName name, [Out] StringBuilder buf, ulong len);
3237 // pid_t getpid(void);
3238 [DllImport (LIBC, SetLastError=true)]
3239 public static extern int getpid ();
3242 // pid_t getppid(void);
3243 [DllImport (LIBC, SetLastError=true)]
3244 public static extern int getppid ();
3247 // int setpgid(pid_t pid, pid_t pgid);
3248 [DllImport (LIBC, SetLastError=true)]
3249 public static extern int setpgid (int pid, int pgid);
3252 // pid_t getpgid(pid_t pid);
3253 [DllImport (LIBC, SetLastError=true)]
3254 public static extern int getpgid (int pid);
3256 [DllImport (LIBC, SetLastError=true)]
3257 public static extern int setpgrp ();
3260 // pid_t getpgrp(void);
3261 [DllImport (LIBC, SetLastError=true)]
3262 public static extern int getpgrp ();
3265 // pid_t setsid(void);
3266 [DllImport (LIBC, SetLastError=true)]
3267 public static extern int setsid ();
3270 // pid_t getsid(pid_t pid);
3271 [DllImport (LIBC, SetLastError=true)]
3272 public static extern int getsid (int pid);
3275 // uid_t getuid(void);
3276 [DllImport (LIBC, SetLastError=true)]
3277 public static extern uint getuid ();
3280 // uid_t geteuid(void);
3281 [DllImport (LIBC, SetLastError=true)]
3282 public static extern uint geteuid ();
3285 // gid_t getgid(void);
3286 [DllImport (LIBC, SetLastError=true)]
3287 public static extern uint getgid ();
3290 // gid_t getgid(void);
3291 [DllImport (LIBC, SetLastError=true)]
3292 public static extern uint getegid ();
3295 // int getgroups(int size, gid_t list[]);
3296 [DllImport (LIBC, SetLastError=true)]
3297 public static extern int getgroups (int size, uint[] list);
3299 public static int getgroups (uint[] list)
3301 return getgroups (list.Length, list);
3305 // int setuid(uid_t uid);
3306 [DllImport (LIBC, SetLastError=true)]
3307 public static extern int setuid (uint uid);
3310 // int setreuid(uid_t ruid, uid_t euid);
3311 [DllImport (LIBC, SetLastError=true)]
3312 public static extern int setreuid (uint ruid, uint euid);
3315 // int setregid(gid_t ruid, gid_t euid);
3316 [DllImport (LIBC, SetLastError=true)]
3317 public static extern int setregid (uint rgid, uint egid);
3320 // int seteuid(uid_t euid);
3321 [DllImport (LIBC, SetLastError=true)]
3322 public static extern int seteuid (uint euid);
3325 // int setegid(gid_t euid);
3326 [DllImport (LIBC, SetLastError=true)]
3327 public static extern int setegid (uint uid);
3330 // int setgid(gid_t gid);
3331 [DllImport (LIBC, SetLastError=true)]
3332 public static extern int setgid (uint gid);
3335 // int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
3336 [DllImport (LIBC, SetLastError=true)]
3337 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
3340 // int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
3341 [DllImport (LIBC, SetLastError=true)]
3342 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
3345 // int setresuid(uid_t ruid, uid_t euid, uid_t suid);
3346 [DllImport (LIBC, SetLastError=true)]
3347 public static extern int setresuid (uint ruid, uint euid, uint suid);
3350 // int setresgid(gid_t ruid, gid_t euid, gid_t suid);
3351 [DllImport (LIBC, SetLastError=true)]
3352 public static extern int setresgid (uint rgid, uint egid, uint sgid);
3356 // pid_t fork(void);
3357 [DllImport (LIBC, SetLastError=true)]
3358 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " +
3359 "shutdown code.\nUse System.Diagnostics.Process instead")]
3360 private static extern int fork ();
3363 // pid_t vfork(void);
3364 [DllImport (LIBC, SetLastError=true)]
3365 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " +
3366 "shutdown code.\nUse System.Diagnostics.Process instead")]
3367 private static extern int vfork ();
3370 private static object tty_lock = new object ();
3372 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
3373 private static extern IntPtr sys_ttyname (int fd);
3375 public static string ttyname (int fd)
3378 IntPtr r = sys_ttyname (fd);
3379 return UnixMarshal.PtrToString (r);
3384 // int ttyname_r(int fd, char *buf, size_t buflen);
3385 [DllImport (MPH, SetLastError=true,
3386 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
3387 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
3389 public static int ttyname_r (int fd, StringBuilder buf)
3391 return ttyname_r (fd, buf, (ulong) buf.Capacity);
3394 [DllImport (LIBC, EntryPoint="isatty")]
3395 private static extern int sys_isatty (int fd);
3397 public static bool isatty (int fd)
3399 return sys_isatty (fd) == 1;
3402 [DllImport (LIBC, SetLastError=true)]
3403 public static extern int link (
3404 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3406 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3409 [DllImport (LIBC, SetLastError=true)]
3410 public static extern int symlink (
3411 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3413 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3417 // int readlink(const char *path, char *buf, size_t bufsize);
3418 [DllImport (MPH, SetLastError=true,
3419 EntryPoint="Mono_Posix_Syscall_readlink")]
3420 public static extern int readlink (
3421 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3422 string path, [Out] StringBuilder buf, ulong bufsiz);
3424 public static int readlink (string path, [Out] StringBuilder buf)
3426 return readlink (path, buf, (ulong) buf.Capacity);
3429 [DllImport (LIBC, SetLastError=true)]
3430 public static extern int unlink (
3431 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3434 [DllImport (LIBC, SetLastError=true)]
3435 public static extern int rmdir (
3436 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3440 // pid_t tcgetpgrp(int fd);
3441 [DllImport (LIBC, SetLastError=true)]
3442 public static extern int tcgetpgrp (int fd);
3445 // int tcsetpgrp(int fd, pid_t pgrp);
3446 [DllImport (LIBC, SetLastError=true)]
3447 public static extern int tcsetpgrp (int fd, int pgrp);
3449 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
3450 private static extern IntPtr sys_getlogin ();
3452 public static string getlogin ()
3454 lock (getlogin_lock) {
3455 IntPtr r = sys_getlogin ();
3456 return UnixMarshal.PtrToString (r);
3461 // int getlogin_r(char *buf, size_t bufsize);
3462 [DllImport (MPH, SetLastError=true,
3463 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
3464 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
3466 public static int getlogin_r (StringBuilder name)
3468 return getlogin_r (name, (ulong) name.Capacity);
3471 [DllImport (LIBC, SetLastError=true)]
3472 public static extern int setlogin (string name);
3475 // int gethostname(char *name, size_t len);
3476 [DllImport (MPH, SetLastError=true,
3477 EntryPoint="Mono_Posix_Syscall_gethostname")]
3478 public static extern int gethostname ([Out] StringBuilder name, ulong len);
3480 public static int gethostname (StringBuilder name)
3482 return gethostname (name, (ulong) name.Capacity);
3486 // int gethostname(const char *name, size_t len);
3487 [DllImport (MPH, SetLastError=true,
3488 EntryPoint="Mono_Posix_Syscall_sethostname")]
3489 public static extern int sethostname (string name, ulong len);
3491 public static int sethostname (string name)
3493 return sethostname (name, (ulong) name.Length);
3496 [DllImport (MPH, SetLastError=true,
3497 EntryPoint="Mono_Posix_Syscall_gethostid")]
3498 public static extern long gethostid ();
3500 [DllImport (MPH, SetLastError=true,
3501 EntryPoint="Mono_Posix_Syscall_sethostid")]
3502 public static extern int sethostid (long hostid);
3505 // int getdomainname(char *name, size_t len);
3506 [DllImport (MPH, SetLastError=true,
3507 EntryPoint="Mono_Posix_Syscall_getdomainname")]
3508 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
3510 public static int getdomainname (StringBuilder name)
3512 return getdomainname (name, (ulong) name.Capacity);
3516 // int setdomainname(const char *name, size_t len);
3517 [DllImport (MPH, SetLastError=true,
3518 EntryPoint="Mono_Posix_Syscall_setdomainname")]
3519 public static extern int setdomainname (string name, ulong len);
3521 public static int setdomainname (string name)
3523 return setdomainname (name, (ulong) name.Length);
3526 [DllImport (LIBC, SetLastError=true)]
3527 public static extern int vhangup ();
3529 // Revoke doesn't appear to be POSIX. Include it?
3530 [DllImport (LIBC, SetLastError=true)]
3531 public static extern int revoke (
3532 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3535 // TODO: profil? It's not POSIX.
3537 [DllImport (LIBC, SetLastError=true)]
3538 public static extern int acct (
3539 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3542 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
3543 private static extern IntPtr sys_getusershell ();
3545 internal static object usershell_lock = new object ();
3547 public static string getusershell ()
3549 lock (usershell_lock) {
3550 IntPtr r = sys_getusershell ();
3551 return UnixMarshal.PtrToString (r);
3555 [DllImport (MPH, SetLastError=true,
3556 EntryPoint="Mono_Posix_Syscall_setusershell")]
3557 private static extern int sys_setusershell ();
3559 public static int setusershell ()
3561 lock (usershell_lock) {
3562 return sys_setusershell ();
3566 [DllImport (MPH, SetLastError=true,
3567 EntryPoint="Mono_Posix_Syscall_endusershell")]
3568 private static extern int sys_endusershell ();
3570 public static int endusershell ()
3572 lock (usershell_lock) {
3573 return sys_endusershell ();
3578 [DllImport (LIBC, SetLastError=true)]
3579 private static extern int daemon (int nochdir, int noclose);
3581 // this implicitly forks, and thus isn't safe.
3582 private static int daemon (bool nochdir, bool noclose)
3584 return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
3588 [DllImport (LIBC, SetLastError=true)]
3589 public static extern int chroot (
3590 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3593 // skipping getpass(3) as the man page states:
3594 // This function is obsolete. Do not use it.
3596 [DllImport (LIBC, SetLastError=true)]
3597 public static extern int fsync (int fd);
3599 [DllImport (LIBC, SetLastError=true)]
3600 public static extern int fdatasync (int fd);
3602 [DllImport (MPH, SetLastError=true,
3603 EntryPoint="Mono_Posix_Syscall_sync")]
3604 public static extern int sync ();
3606 [DllImport (LIBC, SetLastError=true)]
3607 [Obsolete ("Dropped in POSIX 1003.1-2001. " +
3608 "Use Syscall.sysconf (SysconfName._SC_PAGESIZE).")]
3609 public static extern int getpagesize ();
3612 // int truncate(const char *path, off_t length);
3613 [DllImport (MPH, SetLastError=true,
3614 EntryPoint="Mono_Posix_Syscall_truncate")]
3615 public static extern int truncate (
3616 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3617 string path, long length);
3620 // int ftruncate(int fd, off_t length);
3621 [DllImport (MPH, SetLastError=true,
3622 EntryPoint="Mono_Posix_Syscall_ftruncate")]
3623 public static extern int ftruncate (int fd, long length);
3625 [DllImport (LIBC, SetLastError=true)]
3626 public static extern int getdtablesize ();
3628 [DllImport (LIBC, SetLastError=true)]
3629 public static extern int brk (IntPtr end_data_segment);
3631 [DllImport (LIBC, SetLastError=true)]
3632 public static extern IntPtr sbrk (IntPtr increment);
3634 // TODO: syscall(2)?
3635 // Probably safer to skip entirely.
3638 // int lockf(int fd, int cmd, off_t len);
3639 [DllImport (MPH, SetLastError=true,
3640 EntryPoint="Mono_Posix_Syscall_lockf")]
3641 public static extern int lockf (int fd, LockfCommand cmd, long len);
3643 internal static object crypt_lock = new object ();
3645 [DllImport (CRYPT, SetLastError=true, EntryPoint="crypt")]
3646 private static extern IntPtr sys_crypt (string key, string salt);
3648 public static string crypt (string key, string salt)
3651 IntPtr r = sys_crypt (key, salt);
3652 return UnixMarshal.PtrToString (r);
3656 internal static object encrypt_lock = new object ();
3658 [DllImport (MPH, SetLastError=true,
3659 EntryPoint="Mono_Posix_Syscall_encrypt")]
3660 private static extern int sys_encrypt ([In, Out] byte[] block, int edflag);
3662 public static int encrypt (byte[] block, bool decode)
3664 if (block.Length < 64)
3665 throw new ArgumentOutOfRangeException ("block", "Must refer to at least 64 bytes");
3666 lock (encrypt_lock) {
3667 return sys_encrypt (block, decode ? 1 : 0);
3672 // void swab(const void *from, void *to, ssize_t n);
3673 [DllImport (MPH, SetLastError=true,
3674 EntryPoint="Mono_Posix_Syscall_swab")]
3675 public static extern int swab (IntPtr from, IntPtr to, long n);
3677 public static unsafe void swab (void* from, void* to, long n)
3679 swab ((IntPtr) from, (IntPtr) to, n);
3684 #region <utime.h> Declarations
3686 // <utime.h> -- COMPLETE
3689 [DllImport (MPH, SetLastError=true,
3690 EntryPoint="Mono_Posix_Syscall_utime")]
3691 private static extern int sys_utime (
3692 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3693 string filename, ref Utimbuf buf, int use_buf);
3695 public static int utime (string filename, ref Utimbuf buf)
3697 return sys_utime (filename, ref buf, 1);
3700 public static int utime (string filename)
3702 Utimbuf buf = new Utimbuf ();
3703 return sys_utime (filename, ref buf, 0);