2 // Mono.Unix/Syscall.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Jonathan Pryor (jonpryor@vt.edu)
8 // (C) 2003 Novell, Inc.
9 // (C) 2004-2006 Jonathan Pryor
11 // This file implements the low-level syscall interface to the POSIX
14 // This file tries to stay close to the low-level API as much as possible
15 // using enumerations, structures and in a few cases, using existing .NET
18 // Implementation notes:
20 // Since the values for the various constants on the API changes
21 // from system to system (even Linux on different architectures will
22 // have different values), we define our own set of values, and we
23 // use a set of C helper routines to map from the constants we define
24 // to the values of the native OS.
26 // Bitfields are flagged with the [Map] attribute, and a helper program
27 // generates a set of routines that we can call to convert from our value
28 // definitions to the value definitions expected by the OS; see
29 // NativeConvert for the conversion routines.
31 // Methods that require tuning are bound as `private sys_NAME' methods
32 // and then a `NAME' method is exposed.
36 // Permission is hereby granted, free of charge, to any person obtaining
37 // a copy of this software and associated documentation files (the
38 // "Software"), to deal in the Software without restriction, including
39 // without limitation the rights to use, copy, modify, merge, publish,
40 // distribute, sublicense, and/or sell copies of the Software, and to
41 // permit persons to whom the Software is furnished to do so, subject to
42 // the following conditions:
44 // The above copyright notice and this permission notice shall be
45 // included in all copies or substantial portions of the Software.
47 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
51 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
52 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
53 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57 using System.Collections;
58 using System.Runtime.InteropServices;
59 using System.Security;
61 using Mono.Unix.Native;
63 namespace Mono.Unix.Native {
68 [CLSCompliant (false)]
69 public enum SyslogOptions {
70 LOG_PID = 0x01, // log the pid with each message
71 LOG_CONS = 0x02, // log on the console if errors in sending
72 LOG_ODELAY = 0x04, // delay open until first syslog (default)
73 LOG_NDELAY = 0x08, // don't delay open
74 LOG_NOWAIT = 0x10, // don't wait for console forks; DEPRECATED
75 LOG_PERROR = 0x20 // log to stderr as well
79 [CLSCompliant (false)]
80 public enum SyslogFacility {
91 LOG_AUTHPRIV = 10 << 3,
100 LOG_LOCAL7 = 23 << 3,
104 [CLSCompliant (false)]
105 public enum SyslogLevel {
106 LOG_EMERG = 0, // system is unusable
107 LOG_ALERT = 1, // action must be taken immediately
108 LOG_CRIT = 2, // critical conditions
109 LOG_ERR = 3, // warning conditions
110 LOG_WARNING = 4, // warning conditions
111 LOG_NOTICE = 5, // normal but significant condition
112 LOG_INFO = 6, // informational
113 LOG_DEBUG = 7 // debug-level messages
117 [CLSCompliant (false)]
118 public enum OpenFlags : int {
122 O_RDONLY = 0x00000000,
123 O_WRONLY = 0x00000001,
127 // Or-ed with zero or more of these
129 O_CREAT = 0x00000040,
131 O_NOCTTY = 0x00000100,
132 O_TRUNC = 0x00000200,
133 O_APPEND = 0x00000400,
134 O_NONBLOCK = 0x00000800,
138 // These are non-Posix. Using them will result in errors/exceptions on
139 // non-supported platforms.
141 // (For example, "C-wrapped" system calls -- calls with implementation in
142 // MonoPosixHelper -- will return -1 with errno=EINVAL. C#-wrapped system
143 // calls will generate an exception in NativeConvert, as the value can't be
144 // converted on the target platform.)
147 O_NOFOLLOW = 0x00020000,
148 O_DIRECTORY = 0x00010000,
149 O_DIRECT = 0x00004000,
150 O_ASYNC = 0x00002000,
151 O_LARGEFILE = 0x00008000
156 [CLSCompliant (false)]
157 public enum FilePermissions : uint {
158 S_ISUID = 0x0800, // Set user ID on execution
159 S_ISGID = 0x0400, // Set group ID on execution
160 S_ISVTX = 0x0200, // Save swapped text after use (sticky).
161 S_IRUSR = 0x0100, // Read by owner
162 S_IWUSR = 0x0080, // Write by owner
163 S_IXUSR = 0x0040, // Execute by owner
164 S_IRGRP = 0x0020, // Read by group
165 S_IWGRP = 0x0010, // Write by group
166 S_IXGRP = 0x0008, // Execute by group
167 S_IROTH = 0x0004, // Read by other
168 S_IWOTH = 0x0002, // Write by other
169 S_IXOTH = 0x0001, // Execute by other
171 S_IRWXG = (S_IRGRP | S_IWGRP | S_IXGRP),
172 S_IRWXU = (S_IRUSR | S_IWUSR | S_IXUSR),
173 S_IRWXO = (S_IROTH | S_IWOTH | S_IXOTH),
174 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
175 ALLPERMS = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
176 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
179 // Why these are held in "mode_t" is beyond me...
180 S_IFMT = 0xF000, // Bits which determine file type
181 [Map(SuppressFlags="S_IFMT")]
182 S_IFDIR = 0x4000, // Directory
183 [Map(SuppressFlags="S_IFMT")]
184 S_IFCHR = 0x2000, // Character device
185 [Map(SuppressFlags="S_IFMT")]
186 S_IFBLK = 0x6000, // Block device
187 [Map(SuppressFlags="S_IFMT")]
188 S_IFREG = 0x8000, // Regular file
189 [Map(SuppressFlags="S_IFMT")]
190 S_IFIFO = 0x1000, // FIFO
191 [Map(SuppressFlags="S_IFMT")]
192 S_IFLNK = 0xA000, // Symbolic link
193 [Map(SuppressFlags="S_IFMT")]
194 S_IFSOCK = 0xC000, // Socket
198 [CLSCompliant (false)]
199 public enum FcntlCommand : int {
200 // Form /usr/include/bits/fcntl.h
201 F_DUPFD = 0, // Duplicate file descriptor.
202 F_GETFD = 1, // Get file descriptor flags.
203 F_SETFD = 2, // Set file descriptor flags.
204 F_GETFL = 3, // Get file status flags.
205 F_SETFL = 4, // Set file status flags.
206 F_GETLK = 12, // Get record locking info. [64]
207 F_SETLK = 13, // Set record locking info (non-blocking). [64]
208 F_SETLKW = 14, // Set record locking info (blocking). [64]
209 F_SETOWN = 8, // Set owner of socket (receiver of SIGIO).
210 F_GETOWN = 9, // Get owner of socket (receiver of SIGIO).
211 F_SETSIG = 10, // Set number of signal to be sent.
212 F_GETSIG = 11, // Get number of signal to be sent.
213 F_SETLEASE = 1024, // Set a lease.
214 F_GETLEASE = 1025, // Enquire what lease is active.
215 F_NOTIFY = 1026, // Required notifications on a directory
219 [CLSCompliant (false)]
220 public enum LockType : short {
221 F_RDLCK = 0, // Read lock.
222 F_WRLCK = 1, // Write lock.
223 F_UNLCK = 2, // Remove lock.
227 [CLSCompliant (false)]
228 public enum SeekFlags : short {
229 // values liberally copied from /usr/include/unistd.h
230 SEEK_SET = 0, // Seek from beginning of file.
231 SEEK_CUR = 1, // Seek from current position.
232 SEEK_END = 2, // Seek from end of file.
234 L_SET = SEEK_SET, // BSD alias for SEEK_SET
235 L_INCR = SEEK_CUR, // BSD alias for SEEK_CUR
236 L_XTND = SEEK_END, // BSD alias for SEEK_END
240 [CLSCompliant (false)]
241 public enum DirectoryNotifyFlags : int {
242 // from /usr/include/bits/fcntl.h
243 DN_ACCESS = 0x00000001, // File accessed.
244 DN_MODIFY = 0x00000002, // File modified.
245 DN_CREATE = 0x00000004, // File created.
246 DN_DELETE = 0x00000008, // File removed.
247 DN_RENAME = 0x00000010, // File renamed.
248 DN_ATTRIB = 0x00000020, // File changed attributes.
249 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
253 [CLSCompliant (false)]
254 public enum PosixFadviseAdvice : int {
255 POSIX_FADV_NORMAL = 0, // No further special treatment.
256 POSIX_FADV_RANDOM = 1, // Expect random page references.
257 POSIX_FADV_SEQUENTIAL = 2, // Expect sequential page references.
258 POSIX_FADV_WILLNEED = 3, // Will need these pages.
259 POSIX_FADV_DONTNEED = 4, // Don't need these pages.
260 POSIX_FADV_NOREUSE = 5, // Data will be accessed once.
264 [CLSCompliant (false)]
265 public enum PosixMadviseAdvice : int {
266 POSIX_MADV_NORMAL = 0, // No further special treatment.
267 POSIX_MADV_RANDOM = 1, // Expect random page references.
268 POSIX_MADV_SEQUENTIAL = 2, // Expect sequential page references.
269 POSIX_MADV_WILLNEED = 3, // Will need these pages.
270 POSIX_MADV_DONTNEED = 4, // Don't need these pages.
274 public enum Signum : int {
275 SIGHUP = 1, // Hangup (POSIX).
276 SIGINT = 2, // Interrupt (ANSI).
277 SIGQUIT = 3, // Quit (POSIX).
278 SIGILL = 4, // Illegal instruction (ANSI).
279 SIGTRAP = 5, // Trace trap (POSIX).
280 SIGABRT = 6, // Abort (ANSI).
281 SIGIOT = 6, // IOT trap (4.2 BSD).
282 SIGBUS = 7, // BUS error (4.2 BSD).
283 SIGFPE = 8, // Floating-point exception (ANSI).
284 SIGKILL = 9, // Kill, unblockable (POSIX).
285 SIGUSR1 = 10, // User-defined signal 1 (POSIX).
286 SIGSEGV = 11, // Segmentation violation (ANSI).
287 SIGUSR2 = 12, // User-defined signal 2 (POSIX).
288 SIGPIPE = 13, // Broken pipe (POSIX).
289 SIGALRM = 14, // Alarm clock (POSIX).
290 SIGTERM = 15, // Termination (ANSI).
291 SIGSTKFLT = 16, // Stack fault.
292 SIGCLD = SIGCHLD, // Same as SIGCHLD (System V).
293 SIGCHLD = 17, // Child status has changed (POSIX).
294 SIGCONT = 18, // Continue (POSIX).
295 SIGSTOP = 19, // Stop, unblockable (POSIX).
296 SIGTSTP = 20, // Keyboard stop (POSIX).
297 SIGTTIN = 21, // Background read from tty (POSIX).
298 SIGTTOU = 22, // Background write to tty (POSIX).
299 SIGURG = 23, // Urgent condition on socket (4.2 BSD).
300 SIGXCPU = 24, // CPU limit exceeded (4.2 BSD).
301 SIGXFSZ = 25, // File size limit exceeded (4.2 BSD).
302 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
303 SIGPROF = 27, // Profiling alarm clock (4.2 BSD).
304 SIGWINCH = 28, // Window size change (4.3 BSD, Sun).
305 SIGPOLL = SIGIO, // Pollable event occurred (System V).
306 SIGIO = 29, // I/O now possible (4.2 BSD).
307 SIGPWR = 30, // Power failure restart (System V).
308 SIGSYS = 31, // Bad system call.
313 public enum WaitOptions : int {
314 WNOHANG = 1, // Don't block waiting
315 WUNTRACED = 2, // Report status of stopped children
319 [CLSCompliant (false)]
320 public enum AccessModes : int {
328 [CLSCompliant (false)]
329 public enum PathconfName : int {
336 _PC_CHOWN_RESTRICTED,
344 _PC_REC_INCR_XFER_SIZE,
345 _PC_REC_MAX_XFER_SIZE,
346 _PC_REC_MIN_XFER_SIZE,
354 [CLSCompliant (false)]
355 public enum SysconfName : int {
365 _SC_REALTIME_SIGNALS,
366 _SC_PRIORITY_SCHEDULING,
375 _SC_MEMORY_PROTECTION,
378 _SC_SHARED_MEMORY_OBJECTS,
381 _SC_AIO_PRIO_DELTA_MAX,
392 /* Values for the argument to `sysconf'
393 corresponding to _POSIX2_* symbols. */
398 _SC_COLL_WEIGHTS_MAX,
403 _SC_CHARCLASS_NAME_MAX,
419 _SC_IOV_MAX = _SC_UIO_MAXIOV,
420 _SC_PII_INTERNET_STREAM,
421 _SC_PII_INTERNET_DGRAM,
426 /* Values according to POSIX 1003.1c (POSIX threads). */
428 _SC_THREAD_SAFE_FUNCTIONS,
429 _SC_GETGR_R_SIZE_MAX,
430 _SC_GETPW_R_SIZE_MAX,
433 _SC_THREAD_DESTRUCTOR_ITERATIONS,
435 _SC_THREAD_STACK_MIN,
436 _SC_THREAD_THREADS_MAX,
437 _SC_THREAD_ATTR_STACKADDR,
438 _SC_THREAD_ATTR_STACKSIZE,
439 _SC_THREAD_PRIORITY_SCHEDULING,
440 _SC_THREAD_PRIO_INHERIT,
441 _SC_THREAD_PRIO_PROTECT,
442 _SC_THREAD_PROCESS_SHARED,
443 _SC_NPROCESSORS_CONF,
444 _SC_NPROCESSORS_ONLN,
450 _SC_XOPEN_XCU_VERSION,
485 _SC_XBS5_ILP32_OFF32,
486 _SC_XBS5_ILP32_OFFBIG,
488 _SC_XBS5_LPBIG_OFFBIG,
491 _SC_XOPEN_REALTIME_THREADS,
496 _SC_C_LANG_SUPPORT_R,
502 _SC_DEVICE_SPECIFIC_R,
513 _SC_READER_WRITER_LOCKS,
521 _SC_THREAD_SPORADIC_SERVER,
523 _SC_SYSTEM_DATABASE_R,
525 _SC_TYPED_MEMORY_OBJECTS,
529 _SC_2_PBS_ACCOUNTING,
535 _SC_2_PBS_CHECKPOINT,
542 _SC_TRACE_EVENT_FILTER,
545 _SC_LEVEL1_ICACHE_SIZE,
546 _SC_LEVEL1_ICACHE_ASSOC,
547 _SC_LEVEL1_ICACHE_LINESIZE,
548 _SC_LEVEL1_DCACHE_SIZE,
549 _SC_LEVEL1_DCACHE_ASSOC,
550 _SC_LEVEL1_DCACHE_LINESIZE,
551 _SC_LEVEL2_CACHE_SIZE,
552 _SC_LEVEL2_CACHE_ASSOC,
553 _SC_LEVEL2_CACHE_LINESIZE,
554 _SC_LEVEL3_CACHE_SIZE,
555 _SC_LEVEL3_CACHE_ASSOC,
556 _SC_LEVEL3_CACHE_LINESIZE,
557 _SC_LEVEL4_CACHE_SIZE,
558 _SC_LEVEL4_CACHE_ASSOC,
559 _SC_LEVEL4_CACHE_LINESIZE
563 [CLSCompliant (false)]
564 public enum ConfstrName : int {
565 _CS_PATH, /* The default search path. */
566 _CS_V6_WIDTH_RESTRICTED_ENVS,
567 _CS_GNU_LIBC_VERSION,
568 _CS_GNU_LIBPTHREAD_VERSION,
569 _CS_LFS_CFLAGS = 1000,
577 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
578 _CS_XBS5_ILP32_OFF32_LDFLAGS,
579 _CS_XBS5_ILP32_OFF32_LIBS,
580 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
581 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
582 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
583 _CS_XBS5_ILP32_OFFBIG_LIBS,
584 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
585 _CS_XBS5_LP64_OFF64_CFLAGS,
586 _CS_XBS5_LP64_OFF64_LDFLAGS,
587 _CS_XBS5_LP64_OFF64_LIBS,
588 _CS_XBS5_LP64_OFF64_LINTFLAGS,
589 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
590 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
591 _CS_XBS5_LPBIG_OFFBIG_LIBS,
592 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
593 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
594 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
595 _CS_POSIX_V6_ILP32_OFF32_LIBS,
596 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
597 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
598 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
599 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
600 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
601 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
602 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
603 _CS_POSIX_V6_LP64_OFF64_LIBS,
604 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
605 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
606 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
607 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
608 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
612 [CLSCompliant (false)]
613 public enum LockfCommand : int {
614 F_ULOCK = 0, // Unlock a previously locked region.
615 F_LOCK = 1, // Lock a region for exclusive use.
616 F_TLOCK = 2, // Test and lock a region for exclusive use.
617 F_TEST = 3, // Test a region for other process locks.
621 public enum PollEvents : short {
622 POLLIN = 0x0001, // There is data to read
623 POLLPRI = 0x0002, // There is urgent data to read
624 POLLOUT = 0x0004, // Writing now will not block
625 POLLERR = 0x0008, // Error condition
626 POLLHUP = 0x0010, // Hung up
627 POLLNVAL = 0x0020, // Invalid request; fd not open
628 // XPG4.2 definitions (via _XOPEN_SOURCE)
629 POLLRDNORM = 0x0040, // Normal data may be read
630 POLLRDBAND = 0x0080, // Priority data may be read
631 POLLWRNORM = 0x0100, // Writing now will not block
632 POLLWRBAND = 0x0200, // Priority data may be written
636 [CLSCompliant (false)]
637 public enum XattrFlags : int {
644 [CLSCompliant (false)]
645 public enum MountFlags : ulong {
646 ST_RDONLY = 1, // Mount read-only
647 ST_NOSUID = 2, // Ignore suid and sgid bits
648 ST_NODEV = 4, // Disallow access to device special files
649 ST_NOEXEC = 8, // Disallow program execution
650 ST_SYNCHRONOUS = 16, // Writes are synced at once
651 ST_REMOUNT = 32, // Alter flags of a mounted FS
652 ST_MANDLOCK = 64, // Allow mandatory locks on an FS
653 ST_WRITE = 128, // Write on file/directory/symlink
654 ST_APPEND = 256, // Append-only file
655 ST_IMMUTABLE = 512, // Immutable file
656 ST_NOATIME = 1024, // Do not update access times
657 ST_NODIRATIME = 2048, // Do not update directory access times
658 ST_BIND = 4096, // Bind directory at different place
662 [CLSCompliant (false)]
663 public enum MmapFlags : int {
664 MAP_SHARED = 0x01, // Share changes.
665 MAP_PRIVATE = 0x02, // Changes are private.
666 MAP_TYPE = 0x0f, // Mask for type of mapping.
667 MAP_FIXED = 0x10, // Interpret addr exactly.
669 MAP_ANONYMOUS = 0x20, // Don't use a file.
670 MAP_ANON = MAP_ANONYMOUS,
672 // These are Linux-specific.
673 MAP_GROWSDOWN = 0x00100, // Stack-like segment.
674 MAP_DENYWRITE = 0x00800, // ETXTBSY
675 MAP_EXECUTABLE = 0x01000, // Mark it as an executable.
676 MAP_LOCKED = 0x02000, // Lock the mapping.
677 MAP_NORESERVE = 0x04000, // Don't check for reservations.
678 MAP_POPULATE = 0x08000, // Populate (prefault) pagetables.
679 MAP_NONBLOCK = 0x10000, // Do not block on IO.
683 [CLSCompliant (false)]
684 public enum MmapProts : int {
685 PROT_READ = 0x1, // Page can be read.
686 PROT_WRITE = 0x2, // Page can be written.
687 PROT_EXEC = 0x4, // Page can be executed.
688 PROT_NONE = 0x0, // Page can not be accessed.
689 PROT_GROWSDOWN = 0x01000000, // Extend change to start of
690 // growsdown vma (mprotect only).
691 PROT_GROWSUP = 0x02000000, // Extend change to start of
692 // growsup vma (mprotect only).
696 [CLSCompliant (false)]
697 public enum MsyncFlags : int {
698 MS_ASYNC = 0x1, // Sync memory asynchronously.
699 MS_SYNC = 0x4, // Synchronous memory sync.
700 MS_INVALIDATE = 0x2, // Invalidate the caches.
704 [CLSCompliant (false)]
705 public enum MlockallFlags : int {
706 MCL_CURRENT = 0x1, // Lock all currently mapped pages.
707 MCL_FUTURE = 0x2, // Lock all additions to address
711 [CLSCompliant (false)]
712 public enum MremapFlags : ulong {
713 MREMAP_MAYMOVE = 0x1,
725 [CLSCompliant (false)]
726 public LockType l_type; // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
727 [CLSCompliant (false)]
728 public SeekFlags l_whence; // How to interpret l_start
729 [off_t] public long l_start; // Starting offset for lock
730 [off_t] public long l_len; // Number of bytes to lock
731 [pid_t] public int l_pid; // PID of process blocking our lock (F_GETLK only)
733 public override int GetHashCode ()
735 return l_type.GetHashCode () ^ l_whence.GetHashCode () ^
736 l_start.GetHashCode () ^ l_len.GetHashCode () ^
737 l_pid.GetHashCode ();
740 public override bool Equals (object obj)
742 if ((obj == null) || (obj.GetType () != GetType ()))
744 Flock value = (Flock) obj;
745 return l_type == value.l_type && l_whence == value.l_whence &&
746 l_start == value.l_start && l_len == value.l_len &&
747 l_pid == value.l_pid;
750 public bool Equals (Flock value)
752 return l_type == value.l_type && l_whence == value.l_whence &&
753 l_start == value.l_start && l_len == value.l_len &&
754 l_pid == value.l_pid;
757 public static bool operator== (Flock lhs, Flock rhs)
759 return lhs.Equals (rhs);
762 public static bool operator!= (Flock lhs, Flock rhs)
764 return !lhs.Equals (rhs);
768 [Map ("struct pollfd")]
771 : IEquatable <Pollfd>
775 [CLSCompliant (false)]
776 public PollEvents events;
777 [CLSCompliant (false)]
778 public PollEvents revents;
780 public override int GetHashCode ()
782 return events.GetHashCode () ^ revents.GetHashCode ();
785 public override bool Equals (object obj)
787 if (obj == null || obj.GetType () != GetType ())
789 Pollfd value = (Pollfd) obj;
790 return value.events == events && value.revents == revents;
793 public bool Equals (Pollfd value)
795 return value.events == events && value.revents == revents;
798 public static bool operator== (Pollfd lhs, Pollfd rhs)
800 return lhs.Equals (rhs);
803 public static bool operator!= (Pollfd lhs, Pollfd rhs)
805 return !lhs.Equals (rhs);
809 [Map ("struct stat")]
815 [CLSCompliant (false)]
816 [dev_t] public ulong st_dev; // device
817 [CLSCompliant (false)]
818 [ino_t] public ulong st_ino; // inode
819 [CLSCompliant (false)]
820 public FilePermissions st_mode; // protection
821 [CLSCompliant (false)]
823 private uint _padding_; // padding for structure alignment
824 [CLSCompliant (false)]
825 [nlink_t] public ulong st_nlink; // number of hard links
826 [CLSCompliant (false)]
827 [uid_t] public uint st_uid; // user ID of owner
828 [CLSCompliant (false)]
829 [gid_t] public uint st_gid; // group ID of owner
830 [CLSCompliant (false)]
831 [dev_t] public ulong st_rdev; // device type (if inode device)
832 [off_t] public long st_size; // total size, in bytes
833 [blksize_t] public long st_blksize; // blocksize for filesystem I/O
834 [blkcnt_t] public long st_blocks; // number of blocks allocated
835 [time_t] public long st_atime; // time of last access
836 [time_t] public long st_mtime; // time of last modification
837 [time_t] public long st_ctime; // time of last status change
839 public override int GetHashCode ()
841 return st_dev.GetHashCode () ^
842 st_ino.GetHashCode () ^
843 st_mode.GetHashCode () ^
844 st_nlink.GetHashCode () ^
845 st_uid.GetHashCode () ^
846 st_gid.GetHashCode () ^
847 st_rdev.GetHashCode () ^
848 st_size.GetHashCode () ^
849 st_blksize.GetHashCode () ^
850 st_blocks.GetHashCode () ^
851 st_atime.GetHashCode () ^
852 st_mtime.GetHashCode () ^
853 st_ctime.GetHashCode ();
856 public override bool Equals (object obj)
858 if (obj == null || obj.GetType() != GetType ())
860 Stat value = (Stat) obj;
861 return value.st_dev == st_dev &&
862 value.st_ino == st_ino &&
863 value.st_mode == st_mode &&
864 value.st_nlink == st_nlink &&
865 value.st_uid == st_uid &&
866 value.st_gid == st_gid &&
867 value.st_rdev == st_rdev &&
868 value.st_size == st_size &&
869 value.st_blksize == st_blksize &&
870 value.st_blocks == st_blocks &&
871 value.st_atime == st_atime &&
872 value.st_mtime == st_mtime &&
873 value.st_ctime == st_ctime;
876 public bool Equals (Stat value)
878 return value.st_dev == st_dev &&
879 value.st_ino == st_ino &&
880 value.st_mode == st_mode &&
881 value.st_nlink == st_nlink &&
882 value.st_uid == st_uid &&
883 value.st_gid == st_gid &&
884 value.st_rdev == st_rdev &&
885 value.st_size == st_size &&
886 value.st_blksize == st_blksize &&
887 value.st_blocks == st_blocks &&
888 value.st_atime == st_atime &&
889 value.st_mtime == st_mtime &&
890 value.st_ctime == st_ctime;
893 public static bool operator== (Stat lhs, Stat rhs)
895 return lhs.Equals (rhs);
898 public static bool operator!= (Stat lhs, Stat rhs)
900 return !lhs.Equals (rhs);
904 // `struct statvfs' isn't portable, so don't generate To/From methods.
906 [CLSCompliant (false)]
907 public struct Statvfs
909 : IEquatable <Statvfs>
912 public ulong f_bsize; // file system block size
913 public ulong f_frsize; // fragment size
914 [fsblkcnt_t] public ulong f_blocks; // size of fs in f_frsize units
915 [fsblkcnt_t] public ulong f_bfree; // # free blocks
916 [fsblkcnt_t] public ulong f_bavail; // # free blocks for non-root
917 [fsfilcnt_t] public ulong f_files; // # inodes
918 [fsfilcnt_t] public ulong f_ffree; // # free inodes
919 [fsfilcnt_t] public ulong f_favail; // # free inodes for non-root
920 public ulong f_fsid; // file system id
921 public MountFlags f_flag; // mount flags
922 public ulong f_namemax; // maximum filename length
924 public override int GetHashCode ()
926 return f_bsize.GetHashCode () ^
927 f_frsize.GetHashCode () ^
928 f_blocks.GetHashCode () ^
929 f_bfree.GetHashCode () ^
930 f_bavail.GetHashCode () ^
931 f_files.GetHashCode () ^
932 f_ffree.GetHashCode () ^
933 f_favail.GetHashCode () ^
934 f_fsid.GetHashCode () ^
935 f_flag.GetHashCode () ^
936 f_namemax.GetHashCode ();
939 public override bool Equals (object obj)
941 if (obj == null || obj.GetType() != GetType ())
943 Statvfs value = (Statvfs) obj;
944 return value.f_bsize == f_bsize &&
945 value.f_frsize == f_frsize &&
946 value.f_blocks == f_blocks &&
947 value.f_bfree == f_bfree &&
948 value.f_bavail == f_bavail &&
949 value.f_files == f_files &&
950 value.f_ffree == f_ffree &&
951 value.f_favail == f_favail &&
952 value.f_fsid == f_fsid &&
953 value.f_flag == f_flag &&
954 value.f_namemax == f_namemax;
957 public bool Equals (Statvfs value)
959 return value.f_bsize == f_bsize &&
960 value.f_frsize == f_frsize &&
961 value.f_blocks == f_blocks &&
962 value.f_bfree == f_bfree &&
963 value.f_bavail == f_bavail &&
964 value.f_files == f_files &&
965 value.f_ffree == f_ffree &&
966 value.f_favail == f_favail &&
967 value.f_fsid == f_fsid &&
968 value.f_flag == f_flag &&
969 value.f_namemax == f_namemax;
972 public static bool operator== (Statvfs lhs, Statvfs rhs)
974 return lhs.Equals (rhs);
977 public static bool operator!= (Statvfs lhs, Statvfs rhs)
979 return !lhs.Equals (rhs);
983 [Map ("struct timeval")]
984 public struct Timeval
986 : IEquatable <Timeval>
989 [time_t] public long tv_sec; // seconds
990 [suseconds_t] public long tv_usec; // microseconds
992 public override int GetHashCode ()
994 return tv_sec.GetHashCode () ^ tv_usec.GetHashCode ();
997 public override bool Equals (object obj)
999 if (obj == null || obj.GetType () != GetType ())
1001 Timeval value = (Timeval) obj;
1002 return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1005 public bool Equals (Timeval value)
1007 return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1010 public static bool operator== (Timeval lhs, Timeval rhs)
1012 return lhs.Equals (rhs);
1015 public static bool operator!= (Timeval lhs, Timeval rhs)
1017 return !lhs.Equals (rhs);
1021 [Map ("struct timezone")]
1022 public struct Timezone
1024 : IEquatable <Timezone>
1027 public int tz_minuteswest; // minutes W of Greenwich
1028 private int tz_dsttime; // type of dst correction (OBSOLETE)
1030 public override int GetHashCode ()
1032 return tz_minuteswest.GetHashCode ();
1035 public override bool Equals (object obj)
1037 if (obj == null || obj.GetType () != GetType ())
1039 Timezone value = (Timezone) obj;
1040 return value.tz_minuteswest == tz_minuteswest;
1043 public bool Equals (Timezone value)
1045 return value.tz_minuteswest == tz_minuteswest;
1048 public static bool operator== (Timezone lhs, Timezone rhs)
1050 return lhs.Equals (rhs);
1053 public static bool operator!= (Timezone lhs, Timezone rhs)
1055 return !lhs.Equals (rhs);
1059 [Map ("struct utimbuf")]
1060 public struct Utimbuf
1062 : IEquatable <Utimbuf>
1065 [time_t] public long actime; // access time
1066 [time_t] public long modtime; // modification time
1068 public override int GetHashCode ()
1070 return actime.GetHashCode () ^ modtime.GetHashCode ();
1073 public override bool Equals (object obj)
1075 if (obj == null || obj.GetType () != GetType ())
1077 Utimbuf value = (Utimbuf) obj;
1078 return value.actime == actime && value.modtime == modtime;
1081 public bool Equals (Utimbuf value)
1083 return value.actime == actime && value.modtime == modtime;
1086 public static bool operator== (Utimbuf lhs, Utimbuf rhs)
1088 return lhs.Equals (rhs);
1091 public static bool operator!= (Utimbuf lhs, Utimbuf rhs)
1093 return !lhs.Equals (rhs);
1101 public sealed class Dirent
1103 : IEquatable <Dirent>
1106 [CLSCompliant (false)]
1107 public /* ino_t */ ulong d_ino;
1108 public /* off_t */ long d_off;
1109 [CLSCompliant (false)]
1110 public ushort d_reclen;
1112 public string d_name;
1114 public override int GetHashCode ()
1116 return d_ino.GetHashCode () ^ d_off.GetHashCode () ^
1117 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
1118 d_name.GetHashCode ();
1121 public override bool Equals (object obj)
1123 if (obj == null || GetType() != obj.GetType())
1125 Dirent d = (Dirent) obj;
1129 public bool Equals (Dirent value)
1133 return value.d_ino == d_ino && value.d_off == d_off &&
1134 value.d_reclen == d_reclen && value.d_type == d_type &&
1135 value.d_name == d_name;
1138 public override string ToString ()
1143 public static bool operator== (Dirent lhs, Dirent rhs)
1145 return Object.Equals (lhs, rhs);
1148 public static bool operator!= (Dirent lhs, Dirent rhs)
1150 return !Object.Equals (lhs, rhs);
1154 public sealed class Fstab
1156 : IEquatable <Fstab>
1159 public string fs_spec;
1160 public string fs_file;
1161 public string fs_vfstype;
1162 public string fs_mntops;
1163 public string fs_type;
1165 public int fs_passno;
1167 public override int GetHashCode ()
1169 return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
1170 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
1171 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
1174 public override bool Equals (object obj)
1176 if (obj == null || GetType() != obj.GetType())
1178 Fstab f = (Fstab) obj;
1182 public bool Equals (Fstab value)
1186 return value.fs_spec == fs_spec && value.fs_file == fs_file &&
1187 value.fs_vfstype == fs_vfstype && value.fs_mntops == fs_mntops &&
1188 value.fs_type == fs_type && value.fs_freq == fs_freq &&
1189 value.fs_passno == fs_passno;
1192 public override string ToString ()
1197 public static bool operator== (Fstab lhs, Fstab rhs)
1199 return Object.Equals (lhs, rhs);
1202 public static bool operator!= (Fstab lhs, Fstab rhs)
1204 return !Object.Equals (lhs, rhs);
1208 public sealed class Group
1210 : IEquatable <Group>
1213 public string gr_name;
1214 public string gr_passwd;
1215 [CLSCompliant (false)]
1216 public /* gid_t */ uint gr_gid;
1217 public string[] gr_mem;
1219 public override int GetHashCode ()
1222 for (int i = 0; i < gr_mem.Length; ++i)
1223 memhc ^= gr_mem[i].GetHashCode ();
1225 return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^
1226 gr_gid.GetHashCode () ^ memhc;
1229 public override bool Equals (object obj)
1231 if (obj == null || GetType() != obj.GetType())
1233 Group g = (Group) obj;
1237 public bool Equals (Group value)
1241 if (value.gr_gid != gr_gid)
1243 if (value.gr_gid == gr_gid && value.gr_name == gr_name &&
1244 value.gr_passwd == gr_passwd) {
1245 if (value.gr_mem == gr_mem)
1247 if (value.gr_mem == null || gr_mem == null)
1249 if (value.gr_mem.Length != gr_mem.Length)
1251 for (int i = 0; i < gr_mem.Length; ++i)
1252 if (gr_mem[i] != value.gr_mem[i])
1259 // Generate string in /etc/group format
1260 public override string ToString ()
1262 StringBuilder sb = new StringBuilder ();
1263 sb.Append (gr_name).Append (":").Append (gr_passwd).Append (":");
1264 sb.Append (gr_gid).Append (":");
1265 GetMembers (sb, gr_mem);
1266 return sb.ToString ();
1269 private static void GetMembers (StringBuilder sb, string[] members)
1271 if (members.Length > 0)
1272 sb.Append (members[0]);
1273 for (int i = 1; i < members.Length; ++i) {
1275 sb.Append (members[i]);
1279 public static bool operator== (Group lhs, Group rhs)
1281 return Object.Equals (lhs, rhs);
1284 public static bool operator!= (Group lhs, Group rhs)
1286 return !Object.Equals (lhs, rhs);
1290 public sealed class Passwd
1292 : IEquatable <Passwd>
1295 public string pw_name;
1296 public string pw_passwd;
1297 [CLSCompliant (false)]
1298 public /* uid_t */ uint pw_uid;
1299 [CLSCompliant (false)]
1300 public /* gid_t */ uint pw_gid;
1301 public string pw_gecos;
1302 public string pw_dir;
1303 public string pw_shell;
1305 public override int GetHashCode ()
1307 return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^
1308 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
1309 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
1310 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
1313 public override bool Equals (object obj)
1315 if (obj == null || GetType() != obj.GetType())
1317 Passwd p = (Passwd) obj;
1321 public bool Equals (Passwd value)
1325 return value.pw_uid == pw_uid && value.pw_gid == pw_gid &&
1326 value.pw_name == pw_name && value.pw_passwd == pw_passwd &&
1327 value.pw_gecos == pw_gecos && value.pw_dir == pw_dir &&
1328 value.pw_shell == pw_shell;
1331 // Generate string in /etc/passwd format
1332 public override string ToString ()
1334 return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
1335 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
1338 public static bool operator== (Passwd lhs, Passwd rhs)
1340 return Object.Equals (lhs, rhs);
1343 public static bool operator!= (Passwd lhs, Passwd rhs)
1345 return !Object.Equals (lhs, rhs);
1349 public sealed class Utsname
1351 : IEquatable <Utsname>
1354 public string sysname;
1355 public string nodename;
1356 public string release;
1357 public string version;
1358 public string machine;
1359 public string domainname;
1361 public override int GetHashCode ()
1363 return sysname.GetHashCode () ^ nodename.GetHashCode () ^
1364 release.GetHashCode () ^ version.GetHashCode () ^
1365 machine.GetHashCode () ^ domainname.GetHashCode ();
1368 public override bool Equals (object obj)
1370 if (obj == null || GetType() != obj.GetType())
1372 Utsname u = (Utsname) obj;
1376 public bool Equals (Utsname value)
1378 return value.sysname == sysname && value.nodename == nodename &&
1379 value.release == release && value.version == version &&
1380 value.machine == machine && value.domainname == domainname;
1383 // Generate string in /etc/passwd format
1384 public override string ToString ()
1386 return string.Format ("{0} {1} {2} {3} {4}",
1387 sysname, nodename, release, version, machine);
1390 public static bool operator== (Utsname lhs, Utsname rhs)
1392 return Object.Equals (lhs, rhs);
1395 public static bool operator!= (Utsname lhs, Utsname rhs)
1397 return !Object.Equals (lhs, rhs);
1402 // Convention: Functions *not* part of the standard C library AND part of
1403 // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
1405 // For example, the man page should be similar to:
1407 // CONFORMING TO (or CONFORMS TO)
1408 // XPG2, SUSv2, POSIX, etc.
1410 // BSD- and GNU-specific exports can also be placed here.
1412 // Non-POSIX/XPG/etc. functions can also be placed here if:
1413 // (a) They'd be likely to be covered in a Steven's-like book
1414 // (b) The functions would be present in libc.so (or equivalent).
1416 // If a function has its own library, that's a STRONG indicator that the
1417 // function should get a different binding, probably in its own assembly,
1418 // so that package management can work sanely. (That is, we'd like to avoid
1419 // scenarios where FooLib.dll is installed, but it requires libFooLib.so to
1420 // run, and libFooLib.so doesn't exist. That would be confusing.)
1422 // The only methods in here should be:
1423 // (1) low-level functions
1424 // (2) "Trivial" function overloads. For example, if the parameters to a
1425 // function are related (e.g. getgroups(2))
1426 // (3) The return type SHOULD NOT be changed. If you want to provide a
1427 // convenience function with a nicer return type, place it into one of
1428 // the Mono.Unix.Unix* wrapper classes, and give it a .NET-styled name.
1429 // - EXCEPTION: No public functions should have a `void' return type.
1430 // `void' return types should be replaced with `int'.
1431 // Rationality: `void'-return functions typically require a
1432 // complicated call sequence, such as clear errno, then call, then
1433 // check errno to see if any errors occurred. This sequence can't
1434 // be done safely in managed code, as errno may change as part of
1435 // the P/Invoke mechanism.
1436 // Instead, add a MonoPosixHelper export which does:
1439 // return errno == 0 ? 0 : -1;
1440 // This lets managed code check the return value in the usual manner.
1441 // (4) Exceptions SHOULD NOT be thrown. EXCEPTIONS:
1442 // - If you're wrapping *broken* methods which make assumptions about
1443 // input data, such as that an argument refers to N bytes of data.
1444 // This is currently limited to cuserid(3) and encrypt(3).
1445 // - If you call functions which themselves generate exceptions.
1446 // This is the case for using NativeConvert, which will throw an
1447 // exception if an invalid/unsupported value is used.
1449 // Naming Conventions:
1450 // - Syscall method names should have the same name as the function being
1451 // wrapped (e.g. Syscall.read ==> read(2)). This allows people to
1452 // consult the appropriate man page if necessary.
1453 // - Methods need not have the same arguments IF this simplifies or
1454 // permits correct usage. The current example is syslog, in which
1455 // syslog(3)'s single `priority' argument is split into SyslogFacility
1456 // and SyslogLevel arguments.
1457 // - Type names (structures, classes, enumerations) are always PascalCased.
1458 // - Enumerations are named as <MethodName><ArgumentName>, and are located
1459 // in the Mono.Unix.Native namespace. For readability, if ArgumentName
1460 // is "cmd", use Command instead. For example, fcntl(2) takes a
1461 // FcntlCommand argument. This naming convention is to provide an
1462 // assocation between an enumeration and where it should be used, and
1463 // allows a single method to accept multiple different enumerations
1464 // (see mmap(2), which takes MmapProts and MmapFlags).
1465 // - EXCEPTION: if an enumeration is shared between multiple different
1466 // methods, AND/OR the "obvious" enumeration name conflicts with an
1467 // existing .NET type, a more appropriate name should be used.
1468 // Example: FilePermissions
1469 // - EXCEPTION: [Flags] enumerations should get plural names to follow
1470 // .NET name guidelines. Usually this doesn't result in a change
1471 // (OpenFlags is the `flags' parameter for open(2)), but it can
1472 // (mmap(2) prot ==> MmapProts, access(2) mode ==> AccessModes).
1473 // - Enumerations should have the [Map] and (optional) [Flags] attributes.
1474 // [Map] is required for make-map to find the type and generate the
1475 // appropriate NativeConvert conversion functions.
1476 // - Enumeration contents should match the original Unix names. This helps
1477 // with documentation (the existing man pages are still useful), and is
1478 // required for use with the make-map generation program.
1479 // - Structure names should be the PascalCased version of the actual
1480 // structure name (struct flock ==> Flock). Structure members should
1481 // have the same names, or a (reasonably) portable subset (Dirent being
1482 // the poster child for questionable members).
1483 // - Whether the managed type should be a reference type (class) or a
1484 // value type (struct) should be determined on a case-by-case basis:
1485 // if you ever need to be able to use NULL for it (such as with Dirent,
1486 // Group, Passwd, as these are method return types and `null' is used
1487 // to signify the end), it should be a reference type; otherwise, use
1488 // your discretion, and keep any expected usage patterns in mind.
1489 // - Syscall should be a Single Point Of Truth (SPOT). There should be
1490 // only ONE way to do anything. By convention, the Linux function names
1491 // are used, but that need not always be the case (use your discretion).
1492 // It SHOULD NOT be required that developers know what platform they're
1493 // on, and choose among a set of similar functions. In short, anything
1494 // that requires a platform check is BAD -- Mono.Unix is a wrapper, and
1495 // we can afford to clean things up whenever possible.
1497 // - Syscall.statfs: Solaris/Mac OS X provide statfs(2), Linux provides
1498 // statvfs(2). MonoPosixHelper will "thunk" between the two,
1499 // exporting a statvfs that works across platforms.
1500 // - Syscall.getfsent: Glibc export which Solaris lacks, while Solaris
1501 // instead provides getvfsent(3). MonoPosixHelper provides wrappers
1502 // to convert getvfsent(3) into Fstab data.
1503 // - Exception: If it isn't possible to cleanly wrap platforms, then the
1504 // method shouldn't be exported. The user will be expected to do their
1505 // own platform check and their own DllImports.
1506 // Examples: mount(2), umount(2), etc.
1507 // - Note: if a platform doesn't support a function AT ALL, the
1508 // MonoPosixHelper wrapper won't be compiled, resulting in a
1509 // EntryPointNotFoundException. This is also consistent with a missing
1510 // P/Invoke into libc.so.
1512 [CLSCompliant (false)]
1513 public sealed class Syscall : Stdlib
1515 new internal const string LIBC = "libc";
1517 private Syscall () {}
1523 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3),
1524 // aio_return(3), aio_suspend(3), aio_write(3)
1526 // Then update UnixStream.BeginRead to use the aio* functions.
1529 #region <attr/xattr.h> Declarations
1531 // <attr/xattr.h> -- COMPLETE
1535 // int setxattr (const char *path, const char *name,
1536 // const void *value, size_t size, int flags);
1537 [DllImport (MPH, SetLastError=true,
1538 EntryPoint="Mono_Posix_Syscall_setxattr")]
1539 public static extern int setxattr (
1540 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1542 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1543 string name, byte[] value, ulong size, XattrFlags flags);
1545 public static int setxattr (string path, string name, byte [] value, ulong size)
1547 return setxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1550 public static int setxattr (string path, string name, byte [] value, XattrFlags flags)
1552 return setxattr (path, name, value, (ulong) value.Length, flags);
1555 public static int setxattr (string path, string name, byte [] value)
1557 return setxattr (path, name, value, (ulong) value.Length);
1561 // int lsetxattr (const char *path, const char *name,
1562 // const void *value, size_t size, int flags);
1563 [DllImport (MPH, SetLastError=true,
1564 EntryPoint="Mono_Posix_Syscall_lsetxattr")]
1565 public static extern int lsetxattr (
1566 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1568 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1569 string name, byte[] value, ulong size, XattrFlags flags);
1571 public static int lsetxattr (string path, string name, byte [] value, ulong size)
1573 return lsetxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1576 public static int lsetxattr (string path, string name, byte [] value, XattrFlags flags)
1578 return lsetxattr (path, name, value, (ulong) value.Length, flags);
1581 public static int lsetxattr (string path, string name, byte [] value)
1583 return lsetxattr (path, name, value, (ulong) value.Length);
1587 // int fsetxattr (int fd, const char *name,
1588 // const void *value, size_t size, int flags);
1589 [DllImport (MPH, SetLastError=true,
1590 EntryPoint="Mono_Posix_Syscall_fsetxattr")]
1591 public static extern int fsetxattr (int fd,
1592 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1593 string name, byte[] value, ulong size, XattrFlags flags);
1595 public static int fsetxattr (int fd, string name, byte [] value, ulong size)
1597 return fsetxattr (fd, name, value, size, XattrFlags.XATTR_AUTO);
1600 public static int fsetxattr (int fd, string name, byte [] value, XattrFlags flags)
1602 return fsetxattr (fd, name, value, (ulong) value.Length, flags);
1605 public static int fsetxattr (int fd, string name, byte [] value)
1607 return fsetxattr (fd, name, value, (ulong) value.Length);
1611 // ssize_t getxattr (const char *path, const char *name,
1612 // void *value, size_t size);
1613 [DllImport (MPH, SetLastError=true,
1614 EntryPoint="Mono_Posix_Syscall_getxattr")]
1615 public static extern long getxattr (
1616 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1618 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1619 string name, byte[] value, ulong size);
1621 public static long getxattr (string path, string name, byte [] value)
1623 return getxattr (path, name, value, (ulong) value.Length);
1626 public static long getxattr (string path, string name, out byte [] value)
1629 long size = getxattr (path, name, value, 0);
1633 value = new byte [size];
1634 return getxattr (path, name, value, (ulong) size);
1638 // ssize_t lgetxattr (const char *path, const char *name,
1639 // void *value, size_t size);
1640 [DllImport (MPH, SetLastError=true,
1641 EntryPoint="Mono_Posix_Syscall_lgetxattr")]
1642 public static extern long lgetxattr (
1643 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1645 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1646 string name, byte[] value, ulong size);
1648 public static long lgetxattr (string path, string name, byte [] value)
1650 return lgetxattr (path, name, value, (ulong) value.Length);
1653 public static long lgetxattr (string path, string name, out byte [] value)
1656 long size = lgetxattr (path, name, value, 0);
1660 value = new byte [size];
1661 return lgetxattr (path, name, value, (ulong) size);
1665 // ssize_t fgetxattr (int fd, const char *name, void *value, size_t size);
1666 [DllImport (MPH, SetLastError=true,
1667 EntryPoint="Mono_Posix_Syscall_fgetxattr")]
1668 public static extern long fgetxattr (int fd,
1669 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1670 string name, byte[] value, ulong size);
1672 public static long fgetxattr (int fd, string name, byte [] value)
1674 return fgetxattr (fd, name, value, (ulong) value.Length);
1677 public static long fgetxattr (int fd, string name, out byte [] value)
1680 long size = fgetxattr (fd, name, value, 0);
1684 value = new byte [size];
1685 return fgetxattr (fd, name, value, (ulong) size);
1689 // ssize_t listxattr (const char *path, char *list, size_t size);
1690 [DllImport (MPH, SetLastError=true,
1691 EntryPoint="Mono_Posix_Syscall_listxattr")]
1692 public static extern long listxattr (
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 listxattr (string path, Encoding encoding, out string [] values)
1700 long size = listxattr (path, null, 0);
1702 values = new string [0];
1706 byte[] list = new byte [size];
1707 long ret = listxattr (path, list, (ulong) size);
1711 GetValues (list, encoding, out values);
1715 public static long listxattr (string path, out string[] values)
1717 return listxattr (path, UnixEncoding.Instance, out values);
1720 private static void GetValues (byte[] list, Encoding encoding, out string[] values)
1723 for (int i = 0; i < list.Length; ++i)
1727 values = new string [num_values];
1730 for (int i = 0; i < list.Length; ++i) {
1731 if (list [i] == 0) {
1732 values [num_values++] = encoding.GetString (list, str_start, i - str_start);
1739 // ssize_t llistxattr (const char *path, char *list, size_t size);
1740 [DllImport (MPH, SetLastError=true,
1741 EntryPoint="Mono_Posix_Syscall_llistxattr")]
1742 public static extern long llistxattr (
1743 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1744 string path, byte[] list, ulong size);
1746 // Slight modification: returns 0 on success, negative on error
1747 public static long llistxattr (string path, Encoding encoding, out string [] values)
1750 long size = llistxattr (path, null, 0);
1752 values = new string [0];
1756 byte[] list = new byte [size];
1757 long ret = llistxattr (path, list, (ulong) size);
1761 GetValues (list, encoding, out values);
1765 public static long llistxattr (string path, out string[] values)
1767 return llistxattr (path, UnixEncoding.Instance, out values);
1771 // ssize_t flistxattr (int fd, char *list, size_t size);
1772 [DllImport (MPH, SetLastError=true,
1773 EntryPoint="Mono_Posix_Syscall_flistxattr")]
1774 public static extern long flistxattr (int fd, byte[] list, ulong size);
1776 // Slight modification: returns 0 on success, negative on error
1777 public static long flistxattr (int fd, Encoding encoding, out string [] values)
1780 long size = flistxattr (fd, null, 0);
1782 values = new string [0];
1786 byte[] list = new byte [size];
1787 long ret = flistxattr (fd, list, (ulong) size);
1791 GetValues (list, encoding, out values);
1795 public static long flistxattr (int fd, out string[] values)
1797 return flistxattr (fd, UnixEncoding.Instance, out values);
1800 [DllImport (MPH, SetLastError=true,
1801 EntryPoint="Mono_Posix_Syscall_removexattr")]
1802 public static extern int removexattr (
1803 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1805 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1808 [DllImport (MPH, SetLastError=true,
1809 EntryPoint="Mono_Posix_Syscall_lremovexattr")]
1810 public static extern int lremovexattr (
1811 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1813 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1816 [DllImport (MPH, SetLastError=true,
1817 EntryPoint="Mono_Posix_Syscall_fremovexattr")]
1818 public static extern int fremovexattr (int fd,
1819 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1823 #region <dirent.h> Declarations
1827 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1829 [DllImport (LIBC, SetLastError=true)]
1830 public static extern IntPtr opendir (
1831 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1834 [DllImport (LIBC, SetLastError=true)]
1835 public static extern int closedir (IntPtr dir);
1838 // void seekdir (DIR *dir, off_t offset);
1839 // Slight modification. Returns -1 on error, 0 on success.
1840 [DllImport (MPH, SetLastError=true,
1841 EntryPoint="Mono_Posix_Syscall_seekdir")]
1842 public static extern int seekdir (IntPtr dir, long offset);
1845 // off_t telldir(DIR *dir);
1846 [DllImport (MPH, SetLastError=true,
1847 EntryPoint="Mono_Posix_Syscall_telldir")]
1848 public static extern long telldir (IntPtr dir);
1850 [DllImport (MPH, SetLastError=true,
1851 EntryPoint="Mono_Posix_Syscall_rewinddir")]
1852 public static extern int rewinddir (IntPtr dir);
1854 private struct _Dirent {
1855 [ino_t] public ulong d_ino;
1856 [off_t] public long d_off;
1857 public ushort d_reclen;
1859 public IntPtr d_name;
1862 private static void CopyDirent (Dirent to, ref _Dirent from)
1865 to.d_ino = from.d_ino;
1866 to.d_off = from.d_off;
1867 to.d_reclen = from.d_reclen;
1868 to.d_type = from.d_type;
1869 to.d_name = UnixMarshal.PtrToString (from.d_name);
1872 Stdlib.free (from.d_name);
1873 from.d_name = IntPtr.Zero;
1877 internal static object readdir_lock = new object ();
1879 [DllImport (MPH, SetLastError=true,
1880 EntryPoint="Mono_Posix_Syscall_readdir")]
1881 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
1883 public static Dirent readdir (IntPtr dir)
1887 lock (readdir_lock) {
1888 r = sys_readdir (dir, out dentry);
1892 Dirent d = new Dirent ();
1893 CopyDirent (d, ref dentry);
1897 [DllImport (MPH, SetLastError=true,
1898 EntryPoint="Mono_Posix_Syscall_readdir_r")]
1899 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
1901 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
1907 entry.d_name = null;
1910 int r = sys_readdir_r (dirp, out _d, out result);
1912 if (r == 0 && result != IntPtr.Zero) {
1913 CopyDirent (entry, ref _d);
1919 [DllImport (LIBC, SetLastError=true)]
1920 public static extern int dirfd (IntPtr dir);
1923 #region <fcntl.h> Declarations
1925 // <fcntl.h> -- COMPLETE
1928 [DllImport (MPH, SetLastError=true,
1929 EntryPoint="Mono_Posix_Syscall_fcntl")]
1930 public static extern int fcntl (int fd, FcntlCommand cmd);
1932 [DllImport (MPH, SetLastError=true,
1933 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
1934 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
1936 public static int fcntl (int fd, FcntlCommand cmd, DirectoryNotifyFlags arg)
1938 if (cmd != FcntlCommand.F_NOTIFY) {
1939 SetLastError (Errno.EINVAL);
1942 long _arg = NativeConvert.FromDirectoryNotifyFlags (arg);
1943 return fcntl (fd, FcntlCommand.F_NOTIFY, _arg);
1946 [DllImport (MPH, SetLastError=true,
1947 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
1948 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
1950 [DllImport (MPH, SetLastError=true,
1951 EntryPoint="Mono_Posix_Syscall_open")]
1952 public static extern int open (
1953 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1954 string pathname, OpenFlags flags);
1957 // int open(const char *pathname, int flags, mode_t mode);
1958 [DllImport (MPH, SetLastError=true,
1959 EntryPoint="Mono_Posix_Syscall_open_mode")]
1960 public static extern int open (
1961 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1962 string pathname, OpenFlags flags, FilePermissions mode);
1965 // int creat(const char *pathname, mode_t mode);
1966 [DllImport (MPH, SetLastError=true,
1967 EntryPoint="Mono_Posix_Syscall_creat")]
1968 public static extern int creat (
1969 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1970 string pathname, FilePermissions mode);
1973 // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
1974 [DllImport (MPH, SetLastError=true,
1975 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
1976 public static extern int posix_fadvise (int fd, long offset,
1977 long len, PosixFadviseAdvice advice);
1979 // posix_fallocate(P)
1980 // int posix_fallocate(int fd, off_t offset, size_t len);
1981 [DllImport (MPH, SetLastError=true,
1982 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
1983 public static extern int posix_fallocate (int fd, long offset, ulong len);
1986 #region <fstab.h> Declarations
1988 // <fstab.h> -- COMPLETE
1991 private struct _Fstab {
1992 public IntPtr fs_spec;
1993 public IntPtr fs_file;
1994 public IntPtr fs_vfstype;
1995 public IntPtr fs_mntops;
1996 public IntPtr fs_type;
1998 public int fs_passno;
1999 public IntPtr _fs_buf_;
2002 private static void CopyFstab (Fstab to, ref _Fstab from)
2005 to.fs_spec = UnixMarshal.PtrToString (from.fs_spec);
2006 to.fs_file = UnixMarshal.PtrToString (from.fs_file);
2007 to.fs_vfstype = UnixMarshal.PtrToString (from.fs_vfstype);
2008 to.fs_mntops = UnixMarshal.PtrToString (from.fs_mntops);
2009 to.fs_type = UnixMarshal.PtrToString (from.fs_type);
2010 to.fs_freq = from.fs_freq;
2011 to.fs_passno = from.fs_passno;
2014 Stdlib.free (from._fs_buf_);
2015 from._fs_buf_ = IntPtr.Zero;
2019 internal static object fstab_lock = new object ();
2021 [DllImport (MPH, SetLastError=true,
2022 EntryPoint="Mono_Posix_Syscall_endfsent")]
2023 private static extern int sys_endfsent ();
2025 public static int endfsent ()
2028 return sys_endfsent ();
2032 [DllImport (MPH, SetLastError=true,
2033 EntryPoint="Mono_Posix_Syscall_getfsent")]
2034 private static extern int sys_getfsent (out _Fstab fs);
2036 public static Fstab getfsent ()
2041 r = sys_getfsent (out fsbuf);
2045 Fstab fs = new Fstab ();
2046 CopyFstab (fs, ref fsbuf);
2050 [DllImport (MPH, SetLastError=true,
2051 EntryPoint="Mono_Posix_Syscall_getfsfile")]
2052 private static extern int sys_getfsfile (
2053 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2054 string mount_point, out _Fstab fs);
2056 public static Fstab getfsfile (string mount_point)
2061 r = sys_getfsfile (mount_point, out fsbuf);
2065 Fstab fs = new Fstab ();
2066 CopyFstab (fs, ref fsbuf);
2070 [DllImport (MPH, SetLastError=true,
2071 EntryPoint="Mono_Posix_Syscall_getfsspec")]
2072 private static extern int sys_getfsspec (
2073 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2074 string special_file, out _Fstab fs);
2076 public static Fstab getfsspec (string special_file)
2081 r = sys_getfsspec (special_file, out fsbuf);
2085 Fstab fs = new Fstab ();
2086 CopyFstab (fs, ref fsbuf);
2090 [DllImport (MPH, SetLastError=true,
2091 EntryPoint="Mono_Posix_Syscall_setfsent")]
2092 private static extern int sys_setfsent ();
2094 public static int setfsent ()
2097 return sys_setfsent ();
2103 #region <grp.h> Declarations
2107 // TODO: putgrent(3), fgetgrent_r(), getgrouplist(2), initgroups(3)
2110 // int setgroups (size_t size, const gid_t *list);
2111 [DllImport (MPH, SetLastError=true,
2112 EntryPoint="Mono_Posix_Syscall_setgroups")]
2113 public static extern int setgroups (ulong size, uint[] list);
2115 public static int setgroups (uint [] list)
2117 return setgroups ((ulong) list.Length, list);
2121 private struct _Group
2123 public IntPtr gr_name;
2124 public IntPtr gr_passwd;
2125 [gid_t] public uint gr_gid;
2126 public int _gr_nmem_;
2127 public IntPtr gr_mem;
2128 public IntPtr _gr_buf_;
2131 private static void CopyGroup (Group to, ref _Group from)
2134 to.gr_gid = from.gr_gid;
2135 to.gr_name = UnixMarshal.PtrToString (from.gr_name);
2136 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
2137 to.gr_mem = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
2140 Stdlib.free (from.gr_mem);
2141 Stdlib.free (from._gr_buf_);
2142 from.gr_mem = IntPtr.Zero;
2143 from._gr_buf_ = IntPtr.Zero;
2147 internal static object grp_lock = new object ();
2149 [DllImport (MPH, SetLastError=true,
2150 EntryPoint="Mono_Posix_Syscall_getgrnam")]
2151 private static extern int sys_getgrnam (string name, out _Group group);
2153 public static Group getgrnam (string name)
2158 r = sys_getgrnam (name, out group);
2162 Group gr = new Group ();
2163 CopyGroup (gr, ref group);
2168 // struct group *getgrgid(gid_t gid);
2169 [DllImport (MPH, SetLastError=true,
2170 EntryPoint="Mono_Posix_Syscall_getgrgid")]
2171 private static extern int sys_getgrgid (uint uid, out _Group group);
2173 public static Group getgrgid (uint uid)
2178 r = sys_getgrgid (uid, out group);
2182 Group gr = new Group ();
2183 CopyGroup (gr, ref group);
2187 [DllImport (MPH, SetLastError=true,
2188 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
2189 private static extern int sys_getgrnam_r (
2190 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2191 string name, out _Group grbuf, out IntPtr grbufp);
2193 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
2198 int r = sys_getgrnam_r (name, out group, out _grbufp);
2199 if (r == 0 && _grbufp != IntPtr.Zero) {
2200 CopyGroup (grbuf, ref group);
2207 // int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
2208 // size_t buflen, struct group **gbufp);
2209 [DllImport (MPH, SetLastError=true,
2210 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
2211 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
2213 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
2218 int r = sys_getgrgid_r (uid, out group, out _grbufp);
2219 if (r == 0 && _grbufp != IntPtr.Zero) {
2220 CopyGroup (grbuf, ref group);
2226 [DllImport (MPH, SetLastError=true,
2227 EntryPoint="Mono_Posix_Syscall_getgrent")]
2228 private static extern int sys_getgrent (out _Group grbuf);
2230 public static Group getgrent ()
2235 r = sys_getgrent (out group);
2239 Group gr = new Group();
2240 CopyGroup (gr, ref group);
2244 [DllImport (MPH, SetLastError=true,
2245 EntryPoint="Mono_Posix_Syscall_setgrent")]
2246 private static extern int sys_setgrent ();
2248 public static int setgrent ()
2251 return sys_setgrent ();
2255 [DllImport (MPH, SetLastError=true,
2256 EntryPoint="Mono_Posix_Syscall_endgrent")]
2257 private static extern int sys_endgrent ();
2259 public static int endgrent ()
2262 return sys_endgrent ();
2266 [DllImport (MPH, SetLastError=true,
2267 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
2268 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
2270 public static Group fgetgrent (IntPtr stream)
2275 r = sys_fgetgrent (stream, out group);
2279 Group gr = new Group ();
2280 CopyGroup (gr, ref group);
2285 #region <pwd.h> Declarations
2289 // TODO: putpwent(3), fgetpwent_r()
2291 // SKIPPING: getpw(3): it's dangerous. Use getpwuid(3) instead.
2294 private struct _Passwd
2296 public IntPtr pw_name;
2297 public IntPtr pw_passwd;
2298 [uid_t] public uint pw_uid;
2299 [gid_t] public uint pw_gid;
2300 public IntPtr pw_gecos;
2301 public IntPtr pw_dir;
2302 public IntPtr pw_shell;
2303 public IntPtr _pw_buf_;
2306 private static void CopyPasswd (Passwd to, ref _Passwd from)
2309 to.pw_name = UnixMarshal.PtrToString (from.pw_name);
2310 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
2311 to.pw_uid = from.pw_uid;
2312 to.pw_gid = from.pw_gid;
2313 to.pw_gecos = UnixMarshal.PtrToString (from.pw_gecos);
2314 to.pw_dir = UnixMarshal.PtrToString (from.pw_dir);
2315 to.pw_shell = UnixMarshal.PtrToString (from.pw_shell);
2318 Stdlib.free (from._pw_buf_);
2319 from._pw_buf_ = IntPtr.Zero;
2323 internal static object pwd_lock = new object ();
2325 [DllImport (MPH, SetLastError=true,
2326 EntryPoint="Mono_Posix_Syscall_getpwnam")]
2327 private static extern int sys_getpwnam (string name, out _Passwd passwd);
2329 public static Passwd getpwnam (string name)
2334 r = sys_getpwnam (name, out passwd);
2338 Passwd pw = new Passwd ();
2339 CopyPasswd (pw, ref passwd);
2344 // struct passwd *getpwnuid(uid_t uid);
2345 [DllImport (MPH, SetLastError=true,
2346 EntryPoint="Mono_Posix_Syscall_getpwuid")]
2347 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
2349 public static Passwd getpwuid (uint uid)
2354 r = sys_getpwuid (uid, out passwd);
2358 Passwd pw = new Passwd ();
2359 CopyPasswd (pw, ref passwd);
2363 [DllImport (MPH, SetLastError=true,
2364 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
2365 private static extern int sys_getpwnam_r (
2366 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2367 string name, out _Passwd pwbuf, out IntPtr pwbufp);
2369 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
2374 int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
2375 if (r == 0 && _pwbufp != IntPtr.Zero) {
2376 CopyPasswd (pwbuf, ref passwd);
2383 // int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
2384 // buflen, struct passwd **pwbufp);
2385 [DllImport (MPH, SetLastError=true,
2386 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
2387 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
2389 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
2394 int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
2395 if (r == 0 && _pwbufp != IntPtr.Zero) {
2396 CopyPasswd (pwbuf, ref passwd);
2402 [DllImport (MPH, SetLastError=true,
2403 EntryPoint="Mono_Posix_Syscall_getpwent")]
2404 private static extern int sys_getpwent (out _Passwd pwbuf);
2406 public static Passwd getpwent ()
2411 r = sys_getpwent (out passwd);
2415 Passwd pw = new Passwd ();
2416 CopyPasswd (pw, ref passwd);
2420 [DllImport (MPH, SetLastError=true,
2421 EntryPoint="Mono_Posix_Syscall_setpwent")]
2422 private static extern int sys_setpwent ();
2424 public static int setpwent ()
2427 return sys_setpwent ();
2431 [DllImport (MPH, SetLastError=true,
2432 EntryPoint="Mono_Posix_Syscall_endpwent")]
2433 private static extern int sys_endpwent ();
2435 public static int endpwent ()
2438 return sys_endpwent ();
2442 [DllImport (MPH, SetLastError=true,
2443 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
2444 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
2446 public static Passwd fgetpwent (IntPtr stream)
2451 r = sys_fgetpwent (stream, out passwd);
2455 Passwd pw = new Passwd ();
2456 CopyPasswd (pw, ref passwd);
2461 #region <signal.h> Declarations
2465 [DllImport (MPH, SetLastError=true,
2466 EntryPoint="Mono_Posix_Syscall_psignal")]
2467 private static extern int psignal (int sig, string s);
2469 public static int psignal (Signum sig, string s)
2471 int signum = NativeConvert.FromSignum (sig);
2472 return psignal (signum, s);
2476 // int kill(pid_t pid, int sig);
2477 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
2478 private static extern int sys_kill (int pid, int sig);
2480 public static int kill (int pid, Signum sig)
2482 int _sig = NativeConvert.FromSignum (sig);
2483 return sys_kill (pid, _sig);
2486 private static object signal_lock = new object ();
2488 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
2489 private static extern IntPtr sys_strsignal (int sig);
2491 public static string strsignal (Signum sig)
2493 int s = NativeConvert.FromSignum (sig);
2494 lock (signal_lock) {
2495 IntPtr r = sys_strsignal (s);
2496 return UnixMarshal.PtrToString (r);
2500 // TODO: sigaction(2)
2501 // TODO: sigsuspend(2)
2502 // TODO: sigpending(2)
2506 #region <stdio.h> Declarations
2510 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
2511 private static extern int _L_ctermid ();
2513 public static readonly int L_ctermid = _L_ctermid ();
2515 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
2516 private static extern int _L_cuserid ();
2518 public static readonly int L_cuserid = _L_cuserid ();
2520 internal static object getlogin_lock = new object ();
2522 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
2523 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
2525 [Obsolete ("\"Nobody knows precisely what cuserid() does... " +
2526 "DO NOT USE cuserid().\n" +
2527 "`string' must hold L_cuserid characters. Use getlogin_r instead.")]
2528 public static string cuserid (StringBuilder @string)
2530 if (@string.Capacity < L_cuserid) {
2531 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
2533 lock (getlogin_lock) {
2534 IntPtr r = sys_cuserid (@string);
2535 return UnixMarshal.PtrToString (r);
2541 #region <stdlib.h> Declarations
2545 [DllImport (LIBC, SetLastError=true)]
2546 public static extern int mkstemp (StringBuilder template);
2548 [DllImport (LIBC, SetLastError=true)]
2549 public static extern int ttyslot ();
2551 [Obsolete ("This is insecure and should not be used", true)]
2552 public static int setkey (string key)
2554 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
2559 #region <string.h> Declarations
2565 // int strerror_r(int errnum, char *buf, size_t n);
2566 [DllImport (MPH, SetLastError=true,
2567 EntryPoint="Mono_Posix_Syscall_strerror_r")]
2568 private static extern int sys_strerror_r (int errnum,
2569 [Out] StringBuilder buf, ulong n);
2571 public static int strerror_r (Errno errnum, StringBuilder buf, ulong n)
2573 int e = NativeConvert.FromErrno (errnum);
2574 return sys_strerror_r (e, buf, n);
2577 public static int strerror_r (Errno errnum, StringBuilder buf)
2579 return strerror_r (errnum, buf, (ulong) buf.Capacity);
2584 #region <sys/mman.h> Declarations
2590 // int posix_madvise(void *addr, size_t len, int advice);
2591 [DllImport (MPH, SetLastError=true,
2592 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
2593 public static extern int posix_madvise (IntPtr addr, ulong len,
2594 PosixMadviseAdvice advice);
2596 public static readonly IntPtr MAP_FAILED = unchecked((IntPtr)(-1));
2598 [DllImport (MPH, SetLastError=true,
2599 EntryPoint="Mono_Posix_Syscall_mmap")]
2600 public static extern IntPtr mmap (IntPtr start, ulong length,
2601 MmapProts prot, MmapFlags flags, int fd, long offset);
2603 [DllImport (MPH, SetLastError=true,
2604 EntryPoint="Mono_Posix_Syscall_munmap")]
2605 public static extern int munmap (IntPtr start, ulong length);
2607 [DllImport (MPH, SetLastError=true,
2608 EntryPoint="Mono_Posix_Syscall_mprotect")]
2609 public static extern int mprotect (IntPtr start, ulong len, MmapProts prot);
2611 [DllImport (MPH, SetLastError=true,
2612 EntryPoint="Mono_Posix_Syscall_msync")]
2613 public static extern int msync (IntPtr start, ulong len, MsyncFlags flags);
2615 [DllImport (MPH, SetLastError=true,
2616 EntryPoint="Mono_Posix_Syscall_mlock")]
2617 public static extern int mlock (IntPtr start, ulong len);
2619 [DllImport (MPH, SetLastError=true,
2620 EntryPoint="Mono_Posix_Syscall_munlock")]
2621 public static extern int munlock (IntPtr start, ulong len);
2623 [DllImport (LIBC, SetLastError=true, EntryPoint="mlockall")]
2624 private static extern int sys_mlockall (int flags);
2626 public static int mlockall (MlockallFlags flags)
2628 int _flags = NativeConvert.FromMlockallFlags (flags);
2629 return sys_mlockall (_flags);
2632 [DllImport (LIBC, SetLastError=true)]
2633 public static extern int munlockall ();
2635 [DllImport (MPH, SetLastError=true,
2636 EntryPoint="Mono_Posix_Syscall_mremap")]
2637 public static extern IntPtr mremap (IntPtr old_address, ulong old_size,
2638 ulong new_size, MremapFlags flags);
2640 [DllImport (MPH, SetLastError=true,
2641 EntryPoint="Mono_Posix_Syscall_mincore")]
2642 public static extern int mincore (IntPtr start, ulong length, byte[] vec);
2644 [DllImport (MPH, SetLastError=true,
2645 EntryPoint="Mono_Posix_Syscall_remap_file_pages")]
2646 public static extern int remap_file_pages (IntPtr start, ulong size,
2647 MmapProts prot, long pgoff, MmapFlags flags);
2651 #region <sys/poll.h> Declarations
2653 // <sys/poll.h> -- COMPLETE
2656 private struct _pollfd {
2658 public short events;
2659 public short revents;
2662 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
2663 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
2665 public static int poll (Pollfd [] fds, uint nfds, int timeout)
2667 if (fds.Length < nfds)
2668 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
2670 _pollfd[] send = new _pollfd[nfds];
2672 for (int i = 0; i < send.Length; i++) {
2673 send [i].fd = fds [i].fd;
2674 send [i].events = NativeConvert.FromPollEvents (fds [i].events);
2677 int r = sys_poll (send, nfds, timeout);
2679 for (int i = 0; i < send.Length; i++) {
2680 fds [i].revents = NativeConvert.ToPollEvents (send [i].revents);
2686 public static int poll (Pollfd [] fds, int timeout)
2688 return poll (fds, (uint) fds.Length, timeout);
2701 // TODO: setrlimit(2)
2702 // TODO: getrlimit(2)
2703 // TODO: getrusage(2)
2707 #region <sys/sendfile.h> Declarations
2709 // <sys/sendfile.h> -- COMPLETE
2712 [DllImport (MPH, SetLastError=true,
2713 EntryPoint="Mono_Posix_Syscall_sendfile")]
2714 public static extern long sendfile (int out_fd, int in_fd,
2715 ref long offset, ulong count);
2719 #region <sys/stat.h> Declarations
2721 // <sys/stat.h> -- COMPLETE
2723 [DllImport (MPH, SetLastError=true,
2724 EntryPoint="Mono_Posix_Syscall_stat")]
2725 public static extern int stat (
2726 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2727 string file_name, out Stat buf);
2729 [DllImport (MPH, SetLastError=true,
2730 EntryPoint="Mono_Posix_Syscall_fstat")]
2731 public static extern int fstat (int filedes, out Stat buf);
2733 [DllImport (MPH, SetLastError=true,
2734 EntryPoint="Mono_Posix_Syscall_lstat")]
2735 public static extern int lstat (
2736 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2737 string file_name, out Stat buf);
2740 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
2741 // All take FilePermissions
2744 // int chmod(const char *path, mode_t mode);
2745 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
2746 private static extern int sys_chmod (
2747 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2748 string path, uint mode);
2750 public static int chmod (string path, FilePermissions mode)
2752 uint _mode = NativeConvert.FromFilePermissions (mode);
2753 return sys_chmod (path, _mode);
2757 // int chmod(int filedes, mode_t mode);
2758 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
2759 private static extern int sys_fchmod (int filedes, uint mode);
2761 public static int fchmod (int filedes, FilePermissions mode)
2763 uint _mode = NativeConvert.FromFilePermissions (mode);
2764 return sys_fchmod (filedes, _mode);
2768 // mode_t umask(mode_t mask);
2769 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
2770 private static extern uint sys_umask (uint mask);
2772 public static FilePermissions umask (FilePermissions mask)
2774 uint _mask = NativeConvert.FromFilePermissions (mask);
2775 uint r = sys_umask (_mask);
2776 return NativeConvert.ToFilePermissions (r);
2780 // int mkdir(const char *pathname, mode_t mode);
2781 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
2782 private static extern int sys_mkdir (
2783 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2784 string oldpath, uint mode);
2786 public static int mkdir (string oldpath, FilePermissions mode)
2788 uint _mode = NativeConvert.FromFilePermissions (mode);
2789 return sys_mkdir (oldpath, _mode);
2793 // int mknod (const char *pathname, mode_t mode, dev_t dev);
2794 [DllImport (MPH, SetLastError=true,
2795 EntryPoint="Mono_Posix_Syscall_mknod")]
2796 public static extern int mknod (
2797 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2798 string pathname, FilePermissions mode, ulong dev);
2801 // int mkfifo(const char *pathname, mode_t mode);
2802 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
2803 private static extern int sys_mkfifo (
2804 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2805 string pathname, uint mode);
2807 public static int mkfifo (string pathname, FilePermissions mode)
2809 uint _mode = NativeConvert.FromFilePermissions (mode);
2810 return sys_mkfifo (pathname, _mode);
2815 #region <sys/stat.h> Declarations
2820 [DllImport (MPH, SetLastError=true,
2821 EntryPoint="Mono_Posix_Syscall_statvfs")]
2822 public static extern int statvfs (
2823 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2824 string path, out Statvfs buf);
2826 [DllImport (MPH, SetLastError=true,
2827 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
2828 public static extern int fstatvfs (int fd, out Statvfs buf);
2832 #region <sys/time.h> Declarations
2836 // TODO: adjtime(), getitimer(2), setitimer(2)
2838 [DllImport (MPH, SetLastError=true,
2839 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2840 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
2842 [DllImport (MPH, SetLastError=true,
2843 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2844 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
2846 public static int gettimeofday (out Timeval tv)
2848 return gettimeofday (out tv, IntPtr.Zero);
2851 [DllImport (MPH, SetLastError=true,
2852 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2853 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
2855 public static int gettimeofday (out Timezone tz)
2857 return gettimeofday (IntPtr.Zero, out tz);
2860 [DllImport (MPH, SetLastError=true,
2861 EntryPoint="Mono_Posix_Syscall_settimeofday")]
2862 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
2864 [DllImport (MPH, SetLastError=true,
2865 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2866 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
2868 public static int settimeofday (ref Timeval tv)
2870 return settimeofday (ref tv, IntPtr.Zero);
2873 [DllImport (MPH, SetLastError=true,
2874 EntryPoint="Mono_Posix_Syscall_utimes")]
2875 private static extern int sys_utimes (
2876 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2877 string filename, Timeval[] tvp);
2879 public static int utimes (string filename, Timeval[] tvp)
2881 if (tvp != null && tvp.Length != 2) {
2882 SetLastError (Errno.EINVAL);
2885 return sys_utimes (filename, tvp);
2888 [DllImport (MPH, SetLastError=true,
2889 EntryPoint="Mono_Posix_Syscall_lutimes")]
2890 private static extern int sys_lutimes (
2891 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2892 string filename, Timeval[] tvp);
2894 public static int lutimes (string filename, Timeval[] tvp)
2896 if (tvp != null && tvp.Length != 2) {
2897 SetLastError (Errno.EINVAL);
2900 return sys_lutimes (filename, tvp);
2903 [DllImport (MPH, SetLastError=true,
2904 EntryPoint="Mono_Posix_Syscall_futimes")]
2905 private static extern int sys_futimes (int fd, Timeval[] tvp);
2907 public static int futimes (int fd, Timeval[] tvp)
2909 if (tvp != null && tvp.Length != 2) {
2910 SetLastError (Errno.EINVAL);
2913 return sys_futimes (fd, tvp);
2935 private struct _Utsname
2937 public IntPtr sysname;
2938 public IntPtr nodename;
2939 public IntPtr release;
2940 public IntPtr version;
2941 public IntPtr machine;
2942 public IntPtr domainname;
2943 public IntPtr _buf_;
2946 private static void CopyUtsname (ref Utsname to, ref _Utsname from)
2949 to = new Utsname ();
2950 to.sysname = UnixMarshal.PtrToString (from.sysname);
2951 to.nodename = UnixMarshal.PtrToString (from.nodename);
2952 to.release = UnixMarshal.PtrToString (from.release);
2953 to.version = UnixMarshal.PtrToString (from.version);
2954 to.machine = UnixMarshal.PtrToString (from.machine);
2955 to.domainname = UnixMarshal.PtrToString (from.domainname);
2958 Stdlib.free (from._buf_);
2959 from._buf_ = IntPtr.Zero;
2963 [DllImport (MPH, SetLastError=true,
2964 EntryPoint="Mono_Posix_Syscall_uname")]
2965 private static extern int sys_uname (out _Utsname buf);
2967 public static int uname (out Utsname buf)
2970 int r = sys_uname (out _buf);
2971 buf = new Utsname ();
2973 CopyUtsname (ref buf, ref _buf);
2978 #region <sys/wait.h> Declarations
2984 // pid_t wait(int *status);
2985 [DllImport (LIBC, SetLastError=true)]
2986 public static extern int wait (out int status);
2989 // pid_t waitpid(pid_t pid, int *status, int options);
2990 [DllImport (LIBC, SetLastError=true)]
2991 private static extern int waitpid (int pid, out int status, int options);
2993 public static int waitpid (int pid, out int status, WaitOptions options)
2995 int _options = NativeConvert.FromWaitOptions (options);
2996 return waitpid (pid, out status, _options);
2999 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
3000 private static extern int _WIFEXITED (int status);
3002 public static bool WIFEXITED (int status)
3004 return _WIFEXITED (status) != 0;
3007 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
3008 public static extern int WEXITSTATUS (int status);
3010 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
3011 private static extern int _WIFSIGNALED (int status);
3013 public static bool WIFSIGNALED (int status)
3015 return _WIFSIGNALED (status) != 0;
3018 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
3019 private static extern int _WTERMSIG (int status);
3021 public static Signum WTERMSIG (int status)
3023 int r = _WTERMSIG (status);
3024 return NativeConvert.ToSignum (r);
3027 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
3028 private static extern int _WIFSTOPPED (int status);
3030 public static bool WIFSTOPPED (int status)
3032 return _WIFSTOPPED (status) != 0;
3035 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
3036 private static extern int _WSTOPSIG (int status);
3038 public static Signum WSTOPSIG (int status)
3040 int r = _WSTOPSIG (status);
3041 return NativeConvert.ToSignum (r);
3050 #region <syslog.h> Declarations
3055 [DllImport (MPH, SetLastError=true,
3056 EntryPoint="Mono_Posix_Syscall_openlog")]
3057 private static extern int sys_openlog (IntPtr ident, int option, int facility);
3059 public static int openlog (IntPtr ident, SyslogOptions option,
3060 SyslogFacility defaultFacility)
3062 int _option = NativeConvert.FromSyslogOptions (option);
3063 int _facility = NativeConvert.FromSyslogFacility (defaultFacility);
3065 return sys_openlog (ident, _option, _facility);
3068 [DllImport (MPH, SetLastError=true,
3069 EntryPoint="Mono_Posix_Syscall_syslog")]
3070 private static extern int sys_syslog (int priority, string message);
3072 public static int syslog (SyslogFacility facility, SyslogLevel level, string message)
3074 int _facility = NativeConvert.FromSyslogFacility (facility);
3075 int _level = NativeConvert.FromSyslogLevel (level);
3076 return sys_syslog (_facility | _level, GetSyslogMessage (message));
3079 public static int syslog (SyslogLevel level, string message)
3081 int _level = NativeConvert.FromSyslogLevel (level);
3082 return sys_syslog (_level, GetSyslogMessage (message));
3085 private static string GetSyslogMessage (string message)
3087 return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
3090 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
3091 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
3092 public static int syslog (SyslogFacility facility, SyslogLevel level,
3093 string format, params object[] parameters)
3095 int _facility = NativeConvert.FromSyslogFacility (facility);
3096 int _level = NativeConvert.FromSyslogLevel (level);
3098 object[] _parameters = new object[checked(parameters.Length+2)];
3099 _parameters [0] = _facility | _level;
3100 _parameters [1] = format;
3101 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3102 return (int) XPrintfFunctions.syslog (_parameters);
3105 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
3106 "Use syslog(SyslogLevel, string) instead.")]
3107 public static int syslog (SyslogLevel level, string format,
3108 params object[] parameters)
3110 int _level = NativeConvert.FromSyslogLevel (level);
3112 object[] _parameters = new object[checked(parameters.Length+2)];
3113 _parameters [0] = _level;
3114 _parameters [1] = format;
3115 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3116 return (int) XPrintfFunctions.syslog (_parameters);
3119 [DllImport (MPH, SetLastError=true,
3120 EntryPoint="Mono_Posix_Syscall_closelog")]
3121 public static extern int closelog ();
3123 [DllImport (LIBC, SetLastError=true, EntryPoint="setlogmask")]
3124 private static extern int sys_setlogmask (int mask);
3126 public static int setlogmask (SyslogLevel mask)
3128 int _mask = NativeConvert.FromSyslogLevel (mask);
3129 return sys_setlogmask (_mask);
3134 #region <time.h> Declarations
3141 // int stime(time_t *t);
3142 [DllImport (MPH, SetLastError=true,
3143 EntryPoint="Mono_Posix_Syscall_stime")]
3144 public static extern int stime (ref long t);
3147 // time_t time(time_t *t);
3148 [DllImport (MPH, SetLastError=true,
3149 EntryPoint="Mono_Posix_Syscall_time")]
3150 public static extern long time (out long t);
3160 #region <unistd.h> Declarations
3164 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
3165 // other TODOs listed below.
3167 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
3168 private static extern int sys_access (
3169 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3170 string pathname, int mode);
3172 public static int access (string pathname, AccessModes mode)
3174 int _mode = NativeConvert.FromAccessModes (mode);
3175 return sys_access (pathname, _mode);
3179 // off_t lseek(int filedes, off_t offset, int whence);
3180 [DllImport (MPH, SetLastError=true,
3181 EntryPoint="Mono_Posix_Syscall_lseek")]
3182 private static extern long sys_lseek (int fd, long offset, int whence);
3184 public static long lseek (int fd, long offset, SeekFlags whence)
3186 short _whence = NativeConvert.FromSeekFlags (whence);
3187 return sys_lseek (fd, offset, _whence);
3190 [DllImport (LIBC, SetLastError=true)]
3191 public static extern int close (int fd);
3194 // ssize_t read(int fd, void *buf, size_t count);
3195 [DllImport (MPH, SetLastError=true,
3196 EntryPoint="Mono_Posix_Syscall_read")]
3197 public static extern long read (int fd, IntPtr buf, ulong count);
3199 public static unsafe long read (int fd, void *buf, ulong count)
3201 return read (fd, (IntPtr) buf, count);
3205 // ssize_t write(int fd, const void *buf, size_t count);
3206 [DllImport (MPH, SetLastError=true,
3207 EntryPoint="Mono_Posix_Syscall_write")]
3208 public static extern long write (int fd, IntPtr buf, ulong count);
3210 public static unsafe long write (int fd, void *buf, ulong count)
3212 return write (fd, (IntPtr) buf, count);
3216 // ssize_t pread(int fd, void *buf, size_t count, off_t offset);
3217 [DllImport (MPH, SetLastError=true,
3218 EntryPoint="Mono_Posix_Syscall_pread")]
3219 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
3221 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
3223 return pread (fd, (IntPtr) buf, count, offset);
3227 // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
3228 [DllImport (MPH, SetLastError=true,
3229 EntryPoint="Mono_Posix_Syscall_pwrite")]
3230 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
3232 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
3234 return pwrite (fd, (IntPtr) buf, count, offset);
3237 [DllImport (MPH, SetLastError=true,
3238 EntryPoint="Mono_Posix_Syscall_pipe")]
3239 public static extern int pipe (out int reading, out int writing);
3241 public static int pipe (int[] filedes)
3243 if (filedes == null || filedes.Length != 2) {
3247 int reading, writing;
3248 int r = pipe (out reading, out writing);
3249 filedes[0] = reading;
3250 filedes[1] = writing;
3254 [DllImport (LIBC, SetLastError=true)]
3255 public static extern uint alarm (uint seconds);
3257 [DllImport (LIBC, SetLastError=true)]
3258 public static extern uint sleep (uint seconds);
3260 [DllImport (LIBC, SetLastError=true)]
3261 public static extern uint ualarm (uint usecs, uint interval);
3263 [DllImport (LIBC, SetLastError=true)]
3264 public static extern int pause ();
3267 // int chown(const char *path, uid_t owner, gid_t group);
3268 [DllImport (LIBC, SetLastError=true)]
3269 public static extern int chown (
3270 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3271 string path, uint owner, uint group);
3274 // int fchown(int fd, uid_t owner, gid_t group);
3275 [DllImport (LIBC, SetLastError=true)]
3276 public static extern int fchown (int fd, uint owner, uint group);
3279 // int lchown(const char *path, uid_t owner, gid_t group);
3280 [DllImport (LIBC, SetLastError=true)]
3281 public static extern int lchown (
3282 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3283 string path, uint owner, uint group);
3285 [DllImport (LIBC, SetLastError=true)]
3286 public static extern int chdir (
3287 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3290 [DllImport (LIBC, SetLastError=true)]
3291 public static extern int fchdir (int fd);
3294 // char *getcwd(char *buf, size_t size);
3295 [DllImport (MPH, SetLastError=true,
3296 EntryPoint="Mono_Posix_Syscall_getcwd")]
3297 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
3299 public static StringBuilder getcwd (StringBuilder buf)
3301 getcwd (buf, (ulong) buf.Capacity);
3305 // getwd(2) is deprecated; don't expose it.
3307 [DllImport (LIBC, SetLastError=true)]
3308 public static extern int dup (int fd);
3310 [DllImport (LIBC, SetLastError=true)]
3311 public static extern int dup2 (int fd, int fd2);
3313 // TODO: does Mono marshal arrays properly?
3314 [DllImport (LIBC, SetLastError=true)]
3315 public static extern int execve (
3316 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3317 string path, string[] argv, string[] envp);
3319 [DllImport (LIBC, SetLastError=true)]
3320 public static extern int fexecve (int fd, string[] argv, string[] envp);
3322 [DllImport (LIBC, SetLastError=true)]
3323 public static extern int execv (
3324 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3325 string path, string[] argv);
3327 // TODO: execle, execl, execlp
3328 [DllImport (LIBC, SetLastError=true)]
3329 public static extern int execvp (
3330 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3331 string path, string[] argv);
3333 [DllImport (LIBC, SetLastError=true)]
3334 public static extern int nice (int inc);
3336 [DllImport (LIBC, SetLastError=true)]
3337 [CLSCompliant (false)]
3338 public static extern int _exit (int status);
3340 [DllImport (MPH, SetLastError=true,
3341 EntryPoint="Mono_Posix_Syscall_fpathconf")]
3342 public static extern long fpathconf (int filedes, PathconfName name, Errno defaultError);
3344 public static long fpathconf (int filedes, PathconfName name)
3346 return fpathconf (filedes, name, (Errno) 0);
3349 [DllImport (MPH, SetLastError=true,
3350 EntryPoint="Mono_Posix_Syscall_pathconf")]
3351 public static extern long pathconf (
3352 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3353 string path, PathconfName name, Errno defaultError);
3355 public static long pathconf (string path, PathconfName name)
3357 return pathconf (path, name, (Errno) 0);
3360 [DllImport (MPH, SetLastError=true,
3361 EntryPoint="Mono_Posix_Syscall_sysconf")]
3362 public static extern long sysconf (SysconfName name, Errno defaultError);
3364 public static long sysconf (SysconfName name)
3366 return sysconf (name, (Errno) 0);
3370 // size_t confstr(int name, char *buf, size_t len);
3371 [DllImport (MPH, SetLastError=true,
3372 EntryPoint="Mono_Posix_Syscall_confstr")]
3373 public static extern ulong confstr (ConfstrName name, [Out] StringBuilder buf, ulong len);
3376 // pid_t getpid(void);
3377 [DllImport (LIBC, SetLastError=true)]
3378 public static extern int getpid ();
3381 // pid_t getppid(void);
3382 [DllImport (LIBC, SetLastError=true)]
3383 public static extern int getppid ();
3386 // int setpgid(pid_t pid, pid_t pgid);
3387 [DllImport (LIBC, SetLastError=true)]
3388 public static extern int setpgid (int pid, int pgid);
3391 // pid_t getpgid(pid_t pid);
3392 [DllImport (LIBC, SetLastError=true)]
3393 public static extern int getpgid (int pid);
3395 [DllImport (LIBC, SetLastError=true)]
3396 public static extern int setpgrp ();
3399 // pid_t getpgrp(void);
3400 [DllImport (LIBC, SetLastError=true)]
3401 public static extern int getpgrp ();
3404 // pid_t setsid(void);
3405 [DllImport (LIBC, SetLastError=true)]
3406 public static extern int setsid ();
3409 // pid_t getsid(pid_t pid);
3410 [DllImport (LIBC, SetLastError=true)]
3411 public static extern int getsid (int pid);
3414 // uid_t getuid(void);
3415 [DllImport (LIBC, SetLastError=true)]
3416 public static extern uint getuid ();
3419 // uid_t geteuid(void);
3420 [DllImport (LIBC, SetLastError=true)]
3421 public static extern uint geteuid ();
3424 // gid_t getgid(void);
3425 [DllImport (LIBC, SetLastError=true)]
3426 public static extern uint getgid ();
3429 // gid_t getgid(void);
3430 [DllImport (LIBC, SetLastError=true)]
3431 public static extern uint getegid ();
3434 // int getgroups(int size, gid_t list[]);
3435 [DllImport (LIBC, SetLastError=true)]
3436 public static extern int getgroups (int size, uint[] list);
3438 public static int getgroups (uint[] list)
3440 return getgroups (list.Length, list);
3444 // int setuid(uid_t uid);
3445 [DllImport (LIBC, SetLastError=true)]
3446 public static extern int setuid (uint uid);
3449 // int setreuid(uid_t ruid, uid_t euid);
3450 [DllImport (LIBC, SetLastError=true)]
3451 public static extern int setreuid (uint ruid, uint euid);
3454 // int setregid(gid_t ruid, gid_t euid);
3455 [DllImport (LIBC, SetLastError=true)]
3456 public static extern int setregid (uint rgid, uint egid);
3459 // int seteuid(uid_t euid);
3460 [DllImport (LIBC, SetLastError=true)]
3461 public static extern int seteuid (uint euid);
3464 // int setegid(gid_t euid);
3465 [DllImport (LIBC, SetLastError=true)]
3466 public static extern int setegid (uint uid);
3469 // int setgid(gid_t gid);
3470 [DllImport (LIBC, SetLastError=true)]
3471 public static extern int setgid (uint gid);
3474 // int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
3475 [DllImport (LIBC, SetLastError=true)]
3476 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
3479 // int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
3480 [DllImport (LIBC, SetLastError=true)]
3481 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
3484 // int setresuid(uid_t ruid, uid_t euid, uid_t suid);
3485 [DllImport (LIBC, SetLastError=true)]
3486 public static extern int setresuid (uint ruid, uint euid, uint suid);
3489 // int setresgid(gid_t ruid, gid_t euid, gid_t suid);
3490 [DllImport (LIBC, SetLastError=true)]
3491 public static extern int setresgid (uint rgid, uint egid, uint sgid);
3495 // pid_t fork(void);
3496 [DllImport (LIBC, SetLastError=true)]
3497 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " +
3498 "shutdown code.\nUse System.Diagnostics.Process instead")]
3499 private static extern int fork ();
3502 // pid_t vfork(void);
3503 [DllImport (LIBC, SetLastError=true)]
3504 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " +
3505 "shutdown code.\nUse System.Diagnostics.Process instead")]
3506 private static extern int vfork ();
3509 private static object tty_lock = new object ();
3511 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
3512 private static extern IntPtr sys_ttyname (int fd);
3514 public static string ttyname (int fd)
3517 IntPtr r = sys_ttyname (fd);
3518 return UnixMarshal.PtrToString (r);
3523 // int ttyname_r(int fd, char *buf, size_t buflen);
3524 [DllImport (MPH, SetLastError=true,
3525 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
3526 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
3528 public static int ttyname_r (int fd, StringBuilder buf)
3530 return ttyname_r (fd, buf, (ulong) buf.Capacity);
3533 [DllImport (LIBC, EntryPoint="isatty")]
3534 private static extern int sys_isatty (int fd);
3536 public static bool isatty (int fd)
3538 return sys_isatty (fd) == 1;
3541 [DllImport (LIBC, SetLastError=true)]
3542 public static extern int link (
3543 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3545 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3548 [DllImport (LIBC, SetLastError=true)]
3549 public static extern int symlink (
3550 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3552 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3556 // int readlink(const char *path, char *buf, size_t bufsize);
3557 [DllImport (MPH, SetLastError=true,
3558 EntryPoint="Mono_Posix_Syscall_readlink")]
3559 public static extern int readlink (
3560 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3561 string path, [Out] StringBuilder buf, ulong bufsiz);
3563 public static int readlink (string path, [Out] StringBuilder buf)
3565 return readlink (path, buf, (ulong) buf.Capacity);
3568 [DllImport (LIBC, SetLastError=true)]
3569 public static extern int unlink (
3570 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3573 [DllImport (LIBC, SetLastError=true)]
3574 public static extern int rmdir (
3575 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3579 // pid_t tcgetpgrp(int fd);
3580 [DllImport (LIBC, SetLastError=true)]
3581 public static extern int tcgetpgrp (int fd);
3584 // int tcsetpgrp(int fd, pid_t pgrp);
3585 [DllImport (LIBC, SetLastError=true)]
3586 public static extern int tcsetpgrp (int fd, int pgrp);
3588 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
3589 private static extern IntPtr sys_getlogin ();
3591 public static string getlogin ()
3593 lock (getlogin_lock) {
3594 IntPtr r = sys_getlogin ();
3595 return UnixMarshal.PtrToString (r);
3600 // int getlogin_r(char *buf, size_t bufsize);
3601 [DllImport (MPH, SetLastError=true,
3602 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
3603 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
3605 public static int getlogin_r (StringBuilder name)
3607 return getlogin_r (name, (ulong) name.Capacity);
3610 [DllImport (LIBC, SetLastError=true)]
3611 public static extern int setlogin (string name);
3614 // int gethostname(char *name, size_t len);
3615 [DllImport (MPH, SetLastError=true,
3616 EntryPoint="Mono_Posix_Syscall_gethostname")]
3617 public static extern int gethostname ([Out] StringBuilder name, ulong len);
3619 public static int gethostname (StringBuilder name)
3621 return gethostname (name, (ulong) name.Capacity);
3625 // int gethostname(const char *name, size_t len);
3626 [DllImport (MPH, SetLastError=true,
3627 EntryPoint="Mono_Posix_Syscall_sethostname")]
3628 public static extern int sethostname (string name, ulong len);
3630 public static int sethostname (string name)
3632 return sethostname (name, (ulong) name.Length);
3635 [DllImport (MPH, SetLastError=true,
3636 EntryPoint="Mono_Posix_Syscall_gethostid")]
3637 public static extern long gethostid ();
3639 [DllImport (MPH, SetLastError=true,
3640 EntryPoint="Mono_Posix_Syscall_sethostid")]
3641 public static extern int sethostid (long hostid);
3644 // int getdomainname(char *name, size_t len);
3645 [DllImport (MPH, SetLastError=true,
3646 EntryPoint="Mono_Posix_Syscall_getdomainname")]
3647 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
3649 public static int getdomainname (StringBuilder name)
3651 return getdomainname (name, (ulong) name.Capacity);
3655 // int setdomainname(const char *name, size_t len);
3656 [DllImport (MPH, SetLastError=true,
3657 EntryPoint="Mono_Posix_Syscall_setdomainname")]
3658 public static extern int setdomainname (string name, ulong len);
3660 public static int setdomainname (string name)
3662 return setdomainname (name, (ulong) name.Length);
3665 [DllImport (LIBC, SetLastError=true)]
3666 public static extern int vhangup ();
3668 // Revoke doesn't appear to be POSIX. Include it?
3669 [DllImport (LIBC, SetLastError=true)]
3670 public static extern int revoke (
3671 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3674 // TODO: profil? It's not POSIX.
3676 [DllImport (LIBC, SetLastError=true)]
3677 public static extern int acct (
3678 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3681 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
3682 private static extern IntPtr sys_getusershell ();
3684 internal static object usershell_lock = new object ();
3686 public static string getusershell ()
3688 lock (usershell_lock) {
3689 IntPtr r = sys_getusershell ();
3690 return UnixMarshal.PtrToString (r);
3694 [DllImport (MPH, SetLastError=true,
3695 EntryPoint="Mono_Posix_Syscall_setusershell")]
3696 private static extern int sys_setusershell ();
3698 public static int setusershell ()
3700 lock (usershell_lock) {
3701 return sys_setusershell ();
3705 [DllImport (MPH, SetLastError=true,
3706 EntryPoint="Mono_Posix_Syscall_endusershell")]
3707 private static extern int sys_endusershell ();
3709 public static int endusershell ()
3711 lock (usershell_lock) {
3712 return sys_endusershell ();
3717 [DllImport (LIBC, SetLastError=true)]
3718 private static extern int daemon (int nochdir, int noclose);
3720 // this implicitly forks, and thus isn't safe.
3721 private static int daemon (bool nochdir, bool noclose)
3723 return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
3727 [DllImport (LIBC, SetLastError=true)]
3728 public static extern int chroot (
3729 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3732 // skipping getpass(3) as the man page states:
3733 // This function is obsolete. Do not use it.
3735 [DllImport (LIBC, SetLastError=true)]
3736 public static extern int fsync (int fd);
3738 [DllImport (LIBC, SetLastError=true)]
3739 public static extern int fdatasync (int fd);
3741 [DllImport (MPH, SetLastError=true,
3742 EntryPoint="Mono_Posix_Syscall_sync")]
3743 public static extern int sync ();
3745 [DllImport (LIBC, SetLastError=true)]
3746 [Obsolete ("Dropped in POSIX 1003.1-2001. " +
3747 "Use Syscall.sysconf (SysconfName._SC_PAGESIZE).")]
3748 public static extern int getpagesize ();
3751 // int truncate(const char *path, off_t length);
3752 [DllImport (MPH, SetLastError=true,
3753 EntryPoint="Mono_Posix_Syscall_truncate")]
3754 public static extern int truncate (
3755 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3756 string path, long length);
3759 // int ftruncate(int fd, off_t length);
3760 [DllImport (MPH, SetLastError=true,
3761 EntryPoint="Mono_Posix_Syscall_ftruncate")]
3762 public static extern int ftruncate (int fd, long length);
3764 [DllImport (LIBC, SetLastError=true)]
3765 public static extern int getdtablesize ();
3767 [DllImport (LIBC, SetLastError=true)]
3768 public static extern int brk (IntPtr end_data_segment);
3770 [DllImport (LIBC, SetLastError=true)]
3771 public static extern IntPtr sbrk (IntPtr increment);
3773 // TODO: syscall(2)?
3774 // Probably safer to skip entirely.
3777 // int lockf(int fd, int cmd, off_t len);
3778 [DllImport (MPH, SetLastError=true,
3779 EntryPoint="Mono_Posix_Syscall_lockf")]
3780 public static extern int lockf (int fd, LockfCommand cmd, long len);
3782 [Obsolete ("This is insecure and should not be used", true)]
3783 public static string crypt (string key, string salt)
3785 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
3788 [Obsolete ("This is insecure and should not be used", true)]
3789 public static int encrypt (byte[] block, bool decode)
3791 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
3795 // void swab(const void *from, void *to, ssize_t n);
3796 [DllImport (MPH, SetLastError=true,
3797 EntryPoint="Mono_Posix_Syscall_swab")]
3798 public static extern int swab (IntPtr from, IntPtr to, long n);
3800 public static unsafe void swab (void* from, void* to, long n)
3802 swab ((IntPtr) from, (IntPtr) to, n);
3807 #region <utime.h> Declarations
3809 // <utime.h> -- COMPLETE
3812 [DllImport (MPH, SetLastError=true,
3813 EntryPoint="Mono_Posix_Syscall_utime")]
3814 private static extern int sys_utime (
3815 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3816 string filename, ref Utimbuf buf, int use_buf);
3818 public static int utime (string filename, ref Utimbuf buf)
3820 return sys_utime (filename, ref buf, 1);
3823 public static int utime (string filename)
3825 Utimbuf buf = new Utimbuf ();
3826 return sys_utime (filename, ref buf, 0);