2 // Mono.Unix/Syscall.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Jonathan Pryor (jonpryor@vt.edu)
8 // (C) 2003 Novell, Inc.
9 // (C) 2004-2005 Jonathan Pryor
11 // This file implements the low-level syscall interface to the POSIX
14 // This file tries to stay close to the low-level API as much as possible
15 // using enumerations, structures and in a few cases, using existing .NET
18 // Implementation notes:
20 // Since the values for the various constants on the API changes
21 // from system to system (even Linux on different architectures will
22 // have different values), we define our own set of values, and we
23 // use a set of C helper routines to map from the constants we define
24 // to the values of the native OS.
26 // Bitfields are flagged with the [Map] attribute, and a helper program
27 // generates a set of routines that we can call to convert from our value
28 // definitions to the value definitions expected by the OS; see
29 // UnixConvert for the conversion routines.
31 // Methods that require tuning are bound as `private sys_NAME' methods
32 // and then a `NAME' method is exposed.
36 // Permission is hereby granted, free of charge, to any person obtaining
37 // a copy of this software and associated documentation files (the
38 // "Software"), to deal in the Software without restriction, including
39 // without limitation the rights to use, copy, modify, merge, publish,
40 // distribute, sublicense, and/or sell copies of the Software, and to
41 // permit persons to whom the Software is furnished to do so, subject to
42 // the following conditions:
44 // The above copyright notice and this permission notice shall be
45 // included in all copies or substantial portions of the Software.
47 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
51 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
52 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
53 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57 using System.Collections;
58 using System.Runtime.InteropServices;
62 [assembly:Mono.Unix.IncludeAttribute (
88 public enum SyslogOptions {
89 LOG_PID = 0x01, // log the pid with each message
90 LOG_CONS = 0x02, // log on the console if errors in sending
91 LOG_ODELAY = 0x04, // delay open until first syslog (default)
92 LOG_NDELAY = 0x08, // don't delay open
93 LOG_NOWAIT = 0x10, // don't wait for console forks; DEPRECATED
94 LOG_PERROR = 0x20 // log to stderr as well
98 public enum SyslogFacility {
101 [Obsolete ("use SyslogFacility.LOG_USER")]
111 LOG_AUTHPRIV = 10 << 3,
113 LOG_LOCAL0 = 16 << 3,
114 LOG_LOCAL1 = 17 << 3,
115 LOG_LOCAL2 = 18 << 3,
116 LOG_LOCAL3 = 19 << 3,
117 LOG_LOCAL4 = 20 << 3,
118 LOG_LOCAL5 = 21 << 3,
119 LOG_LOCAL6 = 22 << 3,
120 LOG_LOCAL7 = 23 << 3,
124 public enum SyslogLevel {
125 LOG_EMERG = 0, // system is unusable
126 LOG_ALERT = 1, // action must be taken immediately
127 LOG_CRIT = 2, // critical conditions
128 LOG_ERR = 3, // warning conditions
129 LOG_WARNING = 4, // warning conditions
130 LOG_NOTICE = 5, // normal but significant condition
131 LOG_INFO = 6, // informational
132 LOG_DEBUG = 7 // debug-level messages
136 public enum OpenFlags : int {
140 O_RDONLY = 0x00000000,
141 O_WRONLY = 0x00000001,
145 // Or-ed with zero or more of these
147 O_CREAT = 0x00000040,
149 O_NOCTTY = 0x00000100,
150 O_TRUNC = 0x00000200,
151 O_APPEND = 0x00000400,
152 O_NONBLOCK = 0x00000800,
156 // These are non-Posix. Using them will result in errors/exceptions on
157 // non-supported platforms.
159 // (For example, "C-wrapped" system calls -- calls with implementation in
160 // MonoPosixHelper -- will return -1 with errno=EINVAL. C#-wrapped system
161 // calls will generate an exception in UnixConvert, as the value can't be
162 // converted on the target platform.)
165 O_NOFOLLOW = 0x00020000,
166 O_DIRECTORY = 0x00010000,
167 O_DIRECT = 0x00004000,
168 O_ASYNC = 0x00002000,
169 O_LARGEFILE = 0x00008000
174 public enum FilePermissions : uint {
175 S_ISUID = 0x0800, // Set user ID on execution
176 S_ISGID = 0x0400, // Set gorup ID on execution
177 S_ISVTX = 0x0200, // Save swapped text after use (sticky).
178 S_IRUSR = 0x0100, // Read by owner
179 S_IWUSR = 0x0080, // Write by owner
180 S_IXUSR = 0x0040, // Execute by owner
181 S_IRGRP = 0x0020, // Read by group
182 S_IWGRP = 0x0010, // Write by group
183 S_IXGRP = 0x0008, // Execute by group
184 S_IROTH = 0x0004, // Read by other
185 S_IWOTH = 0x0002, // Write by other
186 S_IXOTH = 0x0001, // Execute by other
188 S_IRWXG = (S_IRGRP | S_IWGRP | S_IXGRP),
189 S_IRWXU = (S_IRUSR | S_IWUSR | S_IXUSR),
190 S_IRWXO = (S_IROTH | S_IWOTH | S_IXOTH),
191 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
192 ALLPERMS = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
193 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
196 // Why these are held in "mode_t" is beyond me...
197 S_IFMT = 0xF000, // Bits which determine file type
198 S_IFDIR = 0x4000, // Directory
199 S_IFCHR = 0x2000, // Character device
200 S_IFBLK = 0x6000, // Block device
201 S_IFREG = 0x8000, // Regular file
202 S_IFIFO = 0x1000, // FIFO
203 S_IFLNK = 0xA000, // Symbolic link
204 S_IFSOCK = 0xC000, // Socket
208 public enum FcntlCommand : int {
209 // Form /usr/include/bits/fcntl.h
210 F_DUPFD = 0, // Duplicate file descriptor.
211 F_GETFD = 1, // Get file descriptor flags.
212 F_SETFD = 2, // Set file descriptor flags.
213 F_GETFL = 3, // Get file status flags.
214 F_SETFL = 4, // Set file status flags.
215 F_GETLK = 12, // Get record locking info. [64]
216 F_SETLK = 13, // Set record locking info (non-blocking). [64]
217 F_SETLKW = 14, // Set record locking info (blocking). [64]
218 F_SETOWN = 8, // Set owner of socket (receiver of SIGIO).
219 F_GETOWN = 9, // Get owner of socket (receiver of SIGIO).
220 F_SETSIG = 10, // Set number of signal to be sent.
221 F_GETSIG = 11, // Get number of signal to be sent.
222 F_SETLEASE = 1024, // Set a lease.
223 F_GETLEASE = 1025, // Enquire what lease is active.
224 F_NOTIFY = 1026, // Required notifications on a directory
228 public enum LockType : short {
229 F_RDLCK = 0, // Read lock.
230 F_WRLCK = 1, // Write lock.
231 F_UNLCK = 2, // Remove lock.
235 public enum SeekFlags : short {
236 // values liberally copied from /usr/include/unistd.h
237 SEEK_SET = 0, // Seek from beginning of file.
238 SEEK_CUR = 1, // Seek from current position.
239 SEEK_END = 2, // Seek from end of file.
241 L_SET = SEEK_SET, // BSD alias for SEEK_SET
242 L_INCR = SEEK_CUR, // BSD alias for SEEK_CUR
243 L_XTND = SEEK_END, // BSD alias for SEEK_END
247 public enum DirectoryNotifyFlags : int {
248 // from /usr/include/bits/fcntl.h
249 DN_ACCESS = 0x00000001, // File accessed.
250 DN_MODIFY = 0x00000002, // File modified.
251 DN_CREATE = 0x00000004, // File created.
252 DN_DELETE = 0x00000008, // File removed.
253 DN_RENAME = 0x00000010, // File renamed.
254 DN_ATTRIB = 0x00000020, // File changed attributes.
255 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
259 public enum PosixFadviseAdvice : int {
260 POSIX_FADV_NORMAL = 0, // No further special treatment.
261 POSIX_FADV_RANDOM = 1, // Expect random page references.
262 POSIX_FADV_SEQUENTIAL = 2, // Expect sequential page references.
263 POSIX_FADV_WILLNEED = 3, // Will need these pages.
264 POSIX_FADV_DONTNEED = 4, // Don't need these pages.
265 POSIX_FADV_NOREUSE = 5, // Data will be accessed once.
269 public enum PosixMadviseAdvice : int {
270 POSIX_MADV_NORMAL = 0, // No further special treatment.
271 POSIX_MADV_RANDOM = 1, // Expect random page references.
272 POSIX_MADV_SEQUENTIAL = 2, // Expect sequential page references.
273 POSIX_MADV_WILLNEED = 3, // Will need these pages.
274 POSIX_MADV_DONTNEED = 4, // Don't need these pages.
278 public enum Signum : int {
279 SIGHUP = 1, // Hangup (POSIX).
280 SIGINT = 2, // Interrupt (ANSI).
281 SIGQUIT = 3, // Quit (POSIX).
282 SIGILL = 4, // Illegal instruction (ANSI).
283 SIGTRAP = 5, // Trace trap (POSIX).
284 SIGABRT = 6, // Abort (ANSI).
285 SIGIOT = 6, // IOT trap (4.2 BSD).
286 SIGBUS = 7, // BUS error (4.2 BSD).
287 SIGFPE = 8, // Floating-point exception (ANSI).
288 SIGKILL = 9, // Kill, unblockable (POSIX).
289 SIGUSR1 = 10, // User-defined signal 1 (POSIX).
290 SIGSEGV = 11, // Segmentation violation (ANSI).
291 SIGUSR2 = 12, // User-defined signal 2 (POSIX).
292 SIGPIPE = 13, // Broken pipe (POSIX).
293 SIGALRM = 14, // Alarm clock (POSIX).
294 SIGTERM = 15, // Termination (ANSI).
295 SIGSTKFLT = 16, // Stack fault.
296 SIGCLD = SIGCHLD, // Same as SIGCHLD (System V).
297 SIGCHLD = 17, // Child status has changed (POSIX).
298 SIGCONT = 18, // Continue (POSIX).
299 SIGSTOP = 19, // Stop, unblockable (POSIX).
300 SIGTSTP = 20, // Keyboard stop (POSIX).
301 SIGTTIN = 21, // Background read from tty (POSIX).
302 SIGTTOU = 22, // Background write to tty (POSIX).
303 SIGURG = 23, // Urgent condition on socket (4.2 BSD).
304 SIGXCPU = 24, // CPU limit exceeded (4.2 BSD).
305 SIGXFSZ = 25, // File size limit exceeded (4.2 BSD).
306 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
307 SIGPROF = 27, // Profiling alarm clock (4.2 BSD).
308 SIGWINCH = 28, // Window size change (4.3 BSD, Sun).
309 SIGPOLL = SIGIO, // Pollable event occurred (System V).
310 SIGIO = 29, // I/O now possible (4.2 BSD).
311 SIGPWR = 30, // Power failure restart (System V).
312 SIGSYS = 31, // Bad system call.
317 public enum WaitOptions : int {
318 WNOHANG = 1, // Don't block waiting
319 WUNTRACED = 2, // Report status of stopped children
323 public enum AccessMode : int {
331 public enum PathConf : int {
338 _PC_CHOWN_RESTRICTED,
346 _PC_REC_INCR_XFER_SIZE,
347 _PC_REC_MAX_XFER_SIZE,
348 _PC_REC_MIN_XFER_SIZE,
356 public enum SysConf : int {
366 _SC_REALTIME_SIGNALS,
367 _SC_PRIORITY_SCHEDULING,
376 _SC_MEMORY_PROTECTION,
379 _SC_SHARED_MEMORY_OBJECTS,
382 _SC_AIO_PRIO_DELTA_MAX,
393 /* Values for the argument to `sysconf'
394 corresponding to _POSIX2_* symbols. */
399 _SC_COLL_WEIGHTS_MAX,
404 _SC_CHARCLASS_NAME_MAX,
420 _SC_IOV_MAX = _SC_UIO_MAXIOV,
421 _SC_PII_INTERNET_STREAM,
422 _SC_PII_INTERNET_DGRAM,
427 /* Values according to POSIX 1003.1c (POSIX threads). */
429 _SC_THREAD_SAFE_FUNCTIONS,
430 _SC_GETGR_R_SIZE_MAX,
431 _SC_GETPW_R_SIZE_MAX,
434 _SC_THREAD_DESTRUCTOR_ITERATIONS,
436 _SC_THREAD_STACK_MIN,
437 _SC_THREAD_THREADS_MAX,
438 _SC_THREAD_ATTR_STACKADDR,
439 _SC_THREAD_ATTR_STACKSIZE,
440 _SC_THREAD_PRIORITY_SCHEDULING,
441 _SC_THREAD_PRIO_INHERIT,
442 _SC_THREAD_PRIO_PROTECT,
443 _SC_THREAD_PROCESS_SHARED,
444 _SC_NPROCESSORS_CONF,
445 _SC_NPROCESSORS_ONLN,
451 _SC_XOPEN_XCU_VERSION,
486 _SC_XBS5_ILP32_OFF32,
487 _SC_XBS5_ILP32_OFFBIG,
489 _SC_XBS5_LPBIG_OFFBIG,
492 _SC_XOPEN_REALTIME_THREADS,
497 _SC_C_LANG_SUPPORT_R,
503 _SC_DEVICE_SPECIFIC_R,
514 _SC_READER_WRITER_LOCKS,
522 _SC_THREAD_SPORADIC_SERVER,
524 _SC_SYSTEM_DATABASE_R,
526 _SC_TYPED_MEMORY_OBJECTS,
530 _SC_2_PBS_ACCOUNTING,
536 _SC_2_PBS_CHECKPOINT,
543 _SC_TRACE_EVENT_FILTER,
546 _SC_LEVEL1_ICACHE_SIZE,
547 _SC_LEVEL1_ICACHE_ASSOC,
548 _SC_LEVEL1_ICACHE_LINESIZE,
549 _SC_LEVEL1_DCACHE_SIZE,
550 _SC_LEVEL1_DCACHE_ASSOC,
551 _SC_LEVEL1_DCACHE_LINESIZE,
552 _SC_LEVEL2_CACHE_SIZE,
553 _SC_LEVEL2_CACHE_ASSOC,
554 _SC_LEVEL2_CACHE_LINESIZE,
555 _SC_LEVEL3_CACHE_SIZE,
556 _SC_LEVEL3_CACHE_ASSOC,
557 _SC_LEVEL3_CACHE_LINESIZE,
558 _SC_LEVEL4_CACHE_SIZE,
559 _SC_LEVEL4_CACHE_ASSOC,
560 _SC_LEVEL4_CACHE_LINESIZE
564 public enum ConfStr : 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 public enum LockfCommand : int {
613 F_ULOCK = 0, // Unlock a previously locked region.
614 F_LOCK = 1, // Lock a region for exclusive use.
615 F_TLOCK = 2, // Test and lock a region for exclusive use.
616 F_TEST = 3, // Test a region for other process locks.
620 public enum PollEvents : short {
621 POLLIN = 0x0001, // There is data to read
622 POLLPRI = 0x0002, // There is urgent data to read
623 POLLOUT = 0x0004, // Writing now will not block
624 POLLERR = 0x0008, // Error condition
625 POLLHUP = 0x0010, // Hung up
626 POLLNVAL = 0x0020, // Invalid request; fd not open
627 // XPG4.2 definitions (via _XOPEN_SOURCE)
628 POLLRDNORM = 0x0040, // Normal data bay be read
629 POLLRDBAND = 0x0080, // Priority data may be read
630 POLLWRNORM = 0x0100, // Writing now will not block
631 POLLWRBAND = 0x0200, // Priority data may be written
635 public enum XattrFlags : int {
642 public enum MountFlags : ulong {
643 ST_RDONLY = 1, // Mount read-only
644 ST_NOSUID = 2, // Ignore suid and sgid bits
645 ST_NODEV = 4, // Disallow access to device special files
646 ST_SYNCHRONOUS = 16, // Writes are synced at once
647 ST_MANDLOCK = 64, // Allow mandatory locks on an FS
648 ST_WRITE = 128, // Write on file/directory/symlink
649 ST_APPEND = 256, // Append-only file
650 ST_IMMUTABLE = 512, // Immutable file
651 ST_NOATIME = 1024, // Do not update access times
652 ST_NODIRATIME = 2048, // Do not update directory access times
656 public enum MmapFlags : int {
657 MAP_SHARED = 0x01, // Share changes.
658 MAP_PRIVATE = 0x02, // Changes are private.
659 MAP_TYPE = 0x0f, // Mask for type of mapping.
660 MAP_FIXED = 0x10, // Interpret addr exactly.
662 MAP_ANONYMOUS = 0x20, // Don't use a file.
663 MAP_ANON = MAP_ANONYMOUS,
665 // These are Linux-specific.
666 MAP_GROWSDOWN = 0x00100, // Stack-like segment.
667 MAP_DENYWRITE = 0x00800, // ETXTBSY
668 MAP_EXECUTABLE = 0x01000, // Mark it as an executable.
669 MAP_LOCKED = 0x02000, // Lock the mapping.
670 MAP_NORESERVE = 0x04000, // Don't check for reservations.
671 MAP_POPULATE = 0x08000, // Populate (prefault) pagetables.
672 MAP_NONBLOCK = 0x10000, // Do not block on IO.
676 public enum MmapProt : int {
677 PROT_READ = 0x1, // Page can be read.
678 PROT_WRITE = 0x2, // Page can be written.
679 PROT_EXEC = 0x4, // Page can be executed.
680 PROT_NONE = 0x0, // Page can not be accessed.
681 PROT_GROWSDOWN = 0x01000000, // Extend change to start of
682 // growsdown vma (mprotect only).
683 PROT_GROWSUP = 0x02000000, // Extend change to start of
684 // growsup vma (mprotect only).
688 public enum MsyncFlags : int {
689 MS_ASYNC = 0x1, // Sync memory asynchronously.
690 MS_SYNC = 0x4, // Synchronous memory sync.
691 MS_INVALIDATE = 0x2, // Invalidate the caches.
695 public enum MlockallFlags : int {
696 MCL_CURRENT = 0x1, // Lock all currently mapped pages.
697 MCL_FUTURE = 0x2, // Lock all additions to address
701 public enum MremapFlags : ulong {
702 MREMAP_MAYMOVE = 0x1,
709 public struct Flock {
710 public LockType l_type; // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
711 public SeekFlags l_whence; // How to interpret l_start
712 public /* off_t */ long l_start; // Starting offset for lock
713 public /* off_t */ long l_len; // Number of bytes to lock
714 public /* pid_t */ int l_pid; // PID of process blocking our lock (F_GETLK only)
717 [StructLayout(LayoutKind.Sequential)]
718 public struct Pollfd {
720 public PollEvents events;
721 public PollEvents revents;
725 public /* dev_t */ ulong st_dev; // device
726 public /* ino_t */ ulong st_ino; // inode
727 public FilePermissions st_mode; // protection
728 private uint _padding_; // padding for structure alignment
729 public /* nlink_t */ ulong st_nlink; // number of hard links
730 public /* uid_t */ uint st_uid; // user ID of owner
731 public /* gid_t */ uint st_gid; // group ID of owner
732 public /* dev_t */ ulong st_rdev; // device type (if inode device)
733 public /* off_t */ long st_size; // total size, in bytes
734 public /* blksize_t */ long st_blksize; // blocksize for filesystem I/O
735 public /* blkcnt_t */ long st_blocks; // number of blocks allocated
736 public /* time_t */ long st_atime; // time of last access
737 public /* time_t */ long st_mtime; // time of last modification
738 public /* time_t */ long st_ctime; // time of last status change
741 public struct Statvfs {
742 public ulong f_bsize; // file system block size
743 public ulong f_frsize; // fragment size
744 public /* fsblkcnt_t */ ulong f_blocks; // size of fs in f_frsize units
745 public /* fsblkcnt_t */ ulong f_bfree; // # free blocks
746 public /* fsblkcnt_t */ ulong f_bavail; // # free blocks for non-root
747 public /* fsfilcnt_t */ ulong f_files; // # inodes
748 public /* fsfilcnt_t */ ulong f_ffree; // # free inodes
749 public /* fsfilcnt_t */ ulong f_favail; // # free inodes for non-root
750 public ulong f_fsid; // file system id
751 public MountFlags f_flag; // mount flags
752 public ulong f_namemax; // maximum filename length
755 public struct Timeval {
756 public /* time_t */ long tv_sec; // seconds
757 public /* suseconds_t */ long tv_usec; // microseconds
760 public struct Timezone {
761 public int tz_minuteswest; // minutes W of Greenwich
762 private int tz_dsttime; // type of dst correction (OBSOLETE)
765 public struct Utimbuf {
766 public /* time_t */ long actime; // access time
767 public /* time_t */ long modtime; // modification time
774 public sealed class Dirent
776 public /* ino_t */ ulong d_ino;
777 public /* off_t */ long d_off;
778 public ushort d_reclen;
780 public string d_name;
782 public override int GetHashCode ()
784 return d_ino.GetHashCode () ^ d_off.GetHashCode () ^
785 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
786 d_name.GetHashCode ();
789 public override bool Equals (object obj)
791 if (obj == null || GetType() != obj.GetType())
793 Dirent d = (Dirent) obj;
794 return d.d_ino == d_ino && d.d_off == d_off &&
795 d.d_reclen == d_reclen && d.d_type == d_type &&
799 public override string ToString ()
804 public static bool operator== (Dirent lhs, Dirent rhs)
806 return Object.Equals (lhs, rhs);
809 public static bool operator!= (Dirent lhs, Dirent rhs)
811 return !Object.Equals (lhs, rhs);
815 public sealed class Fstab
817 public string fs_spec;
818 public string fs_file;
819 public string fs_vfstype;
820 public string fs_mntops;
821 public string fs_type;
823 public int fs_passno;
825 public override int GetHashCode ()
827 return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
828 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
829 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
832 public override bool Equals (object obj)
834 if (obj == null || GetType() != obj.GetType())
836 Fstab f = (Fstab) obj;
837 return f.fs_spec == fs_spec && f.fs_file == fs_file &&
838 f.fs_vfstype == fs_vfstype && f.fs_mntops == fs_mntops &&
839 f.fs_type == fs_type && f.fs_freq == fs_freq &&
840 f.fs_passno == fs_passno;
843 public override string ToString ()
848 public static bool operator== (Fstab lhs, Fstab rhs)
850 return Object.Equals (lhs, rhs);
853 public static bool operator!= (Fstab lhs, Fstab rhs)
855 return !Object.Equals (lhs, rhs);
859 public sealed class Group
861 public string gr_name;
862 public string gr_passwd;
863 public /* gid_t */ uint gr_gid;
864 public string[] gr_mem;
866 public override int GetHashCode ()
869 for (int i = 0; i < gr_mem.Length; ++i)
870 memhc ^= gr_mem[i].GetHashCode ();
872 return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^
873 gr_gid.GetHashCode () ^ memhc;
876 public override bool Equals (object obj)
878 if (obj == null || GetType() != obj.GetType())
880 Group g = (Group) obj;
881 if (g.gr_gid != gr_gid)
883 if (g.gr_gid == gr_gid && g.gr_name == gr_name &&
884 g.gr_passwd == gr_passwd) {
885 if (g.gr_mem == gr_mem)
887 if (g.gr_mem == null || gr_mem == null)
889 if (g.gr_mem.Length != gr_mem.Length)
891 for (int i = 0; i < gr_mem.Length; ++i)
892 if (gr_mem[i] != g.gr_mem[i])
899 // Generate string in /etc/group format
900 public override string ToString ()
902 StringBuilder sb = new StringBuilder ();
903 sb.AppendFormat ("{0}:{1}:{2}:", gr_name, gr_passwd, gr_gid);
904 GetMembers (sb, gr_mem);
905 return sb.ToString ();
908 private static void GetMembers (StringBuilder sb, string[] members)
910 if (members.Length > 0)
911 sb.Append (members[0]);
912 for (int i = 1; i < members.Length; ++i) {
914 sb.Append (members[i]);
918 public static bool operator== (Group lhs, Group rhs)
920 return Object.Equals (lhs, rhs);
923 public static bool operator!= (Group lhs, Group rhs)
925 return !Object.Equals (lhs, rhs);
929 public sealed class Passwd
931 public string pw_name;
932 public string pw_passwd;
933 public /* uid_t */ uint pw_uid;
934 public /* gid_t */ uint pw_gid;
935 public string pw_gecos;
936 public string pw_dir;
937 public string pw_shell;
939 public override int GetHashCode ()
941 return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^
942 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
943 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
944 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
947 public override bool Equals (object obj)
949 if (obj == null || GetType() != obj.GetType())
951 Passwd p = (Passwd) obj;
952 return p.pw_uid == pw_uid && p.pw_gid == pw_gid && p.pw_name == pw_name &&
953 p.pw_passwd == pw_passwd && p.pw_gecos == pw_gecos &&
954 p.pw_dir == pw_dir && p.pw_shell == pw_shell;
957 // Generate string in /etc/passwd format
958 public override string ToString ()
960 return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
961 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
964 public static bool operator== (Passwd lhs, Passwd rhs)
966 return Object.Equals (lhs, rhs);
969 public static bool operator!= (Passwd lhs, Passwd rhs)
971 return !Object.Equals (lhs, rhs);
976 // Convention: Functions *not* part of the standard C library AND part of
977 // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
979 // For example, the man page should be similar to:
981 // CONFORMING TO (or CONFORMS TO)
982 // XPG2, SUSv2, POSIX, etc.
984 // BSD- and GNU-specific exports can also be placed here.
986 // Non-POSIX/XPG/etc. functions can also be placed here if:
987 // (a) They'd be likely to be covered in a Steven's-like book
988 // (b) The functions would be present in libc.so (or equivalent).
990 // If a function has its own library, that's a STRONG indicator that the
991 // function should get a different binding, probably in its own assembly,
992 // so that package management can work sanely. (That is, we'd like to avoid
993 // scenarios where FooLib.dll is installed, but it requires libFooLib.so to
994 // run, and libFooLib.so doesn't exist. That would be confusing.)
996 // The only methods in here should be:
997 // (1) low-level functions
998 // (2) "Trivial" function overloads. For example, if the parameters to a
999 // function are related (e.g. getgroups(2))
1000 // (3) The return type SHOULD NOT be changed. If you want to provide a
1001 // convenience function with a nicer return type, place it into one of
1002 // the Unix* wrapper classes, and give it a .NET-styled name.
1003 // (4) Exceptions SHOULD NOT be thrown. EXCEPTIONS:
1004 // - If you're wrapping *broken* methods which make assumptions about
1005 // input data, such as that an argument refers to N bytes of data.
1006 // This is currently limited to cuserid(3) and encrypt(3).
1007 // - If you call functions which themselves generate exceptions.
1008 // This is the case for using UnixConvert, which will throw an
1009 // exception if an invalid/unsupported value is used.
1011 // Naming Conventions:
1012 // - Syscall method names should have the same name as the function being
1013 // wrapped (e.g. Syscall.read ==> read(2)). This allows people to
1014 // consult the appropriate man page if necessary.
1015 // - Methods need not have the same arguments IF this simplifies or
1016 // permits correct usage. The current example is syslog, in which
1017 // syslog(3)'s single `priority' argument is split into SyslogFacility
1018 // and SyslogLevel arguments.
1019 // - Type names (structures, classes, enumerations) are always PascalCased.
1020 // - Enumerations are named as <MethodName><ArgumentName>, and are located
1021 // in the Mono.Unix namespace. For readability, if ArgumentName is
1022 // "cmd", use Command instead. For example, fcntl(2) takes a
1023 // FcntlCommand argument. This naming convention is to provide an
1024 // assocation between an enumeration and where it should be used, and
1025 // allows a single method to accept multiple different enumerations
1026 // (see mmap(2), which takes MmapProt and MmapFlags).
1027 // - EXCEPTION: if an enumeration is shared between multiple different
1028 // methods, AND/OR the "obvious" enumeration name conflicts with an
1029 // existing .NET type, a more appropriate name should be used.
1030 // Example: FilePermissions
1031 // - Enumerations should have the [Map] and (optional) [Flgas] attributes.
1032 // [Map] is required for make-map to find the type and generate the
1033 // appropriate UnixConvert conversion functions.
1034 // - Enumeration contents should match the original Unix names. This helps
1035 // with documentation (the existing man pages are still useful), and is
1036 // required for use with the make-map generation program.
1037 // - Structure names should be the PascalCased version of the actual
1038 // structure name (struct flock ==> Flock). Structure members should
1039 // have the same names, or a (reasonably) portable subset (Dirent being
1040 // the poster child for questionable members).
1041 // - Whether the managed type should be a reference type (class) or a
1042 // value type (struct) should be determined on a case-by-case basis:
1043 // if you ever need to be able to use NULL for it (such as with Dirent,
1044 // Group, Passwd, as these are method return types and `null' is used
1045 // to signify the end), it should be a reference type; otherwise, use
1046 // your discretion, and keep any expected usage patterns in mind.
1047 // - Syscall should be a Single Point Of Truth (SPOT). There should be
1048 // only ONE way to do anything. By convention, the Linux function names
1049 // are used, but that need not always be the case (use your discretion).
1050 // It SHOULD NOT be required that developers know what platform they're
1051 // on, and choose among a set of similar functions. In short, anything
1052 // that requires a platform check is BAD -- Mono.Unix is a wrapper, and
1053 // we can afford to clean things up whenever possible.
1055 // - Syscall.statfs: Solaris/Mac OS X provide statfs(2), Linux provides
1056 // statvfs(2). MonoPosixHelper will "thunk" between the two,
1057 // exporting a statvfs that works across platforms.
1058 // - Syscall.getfsent: Glibc export which Solaris lacks, while Solaris
1059 // instead provides getvfsent(3). MonoPosixHelper provides wrappers
1060 // to convert getvfsent(3) into Fstab data.
1061 // - Exception: If it isn't possible to cleanly wrap platforms, then the
1062 // method shouldn't be exported. The user will be expected to do their
1063 // own platform check and their own DllImports.
1064 // Examples: mount(2), umount(2), etc.
1065 // - Note: if a platform doesn't support a function AT ALL, the
1066 // MonoPosixHelper wrapper won't be compiled, resulting in a
1067 // EntryPointNotFoundException. This is also consistent with a missing
1068 // P/Invoke into libc.so.
1070 public sealed class Syscall : Stdlib
1072 new internal const string LIBC = "libc";
1073 private const string CRYPT = "crypt";
1075 private Syscall () {}
1081 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3),
1082 // aio_return(3), aio_suspend(3), aio_write(3)
1084 // Then update UnixStream.BeginRead to use the aio* functions.
1087 #region <attr/xattr.h> Declarations
1089 // <attr/xattr.h> -- COMPLETE
1093 // int setxattr (const char *path, const char *name,
1094 // const void *value, size_t size, int flags);
1095 [DllImport (MPH, SetLastError=true,
1096 EntryPoint="Mono_Posix_Syscall_setxattr")]
1097 public static extern int setxattr (string path, string name, byte[] value, ulong size, XattrFlags flags);
1099 public static int setxattr (string path, string name, byte [] value, ulong size)
1101 return setxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1104 public static int setxattr (string path, string name, byte [] value, XattrFlags flags)
1106 return setxattr (path, name, value, (ulong) value.Length, flags);
1109 public static int setxattr (string path, string name, byte [] value)
1111 return setxattr (path, name, value, (ulong) value.Length);
1115 // int lsetxattr (const char *path, const char *name,
1116 // const void *value, size_t size, int flags);
1117 [DllImport (MPH, SetLastError=true,
1118 EntryPoint="Mono_Posix_Syscall_lsetxattr")]
1119 public static extern int lsetxattr (string path, string name, byte[] value, ulong size, XattrFlags flags);
1121 public static int lsetxattr (string path, string name, byte [] value, ulong size)
1123 return lsetxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1126 public static int lsetxattr (string path, string name, byte [] value, XattrFlags flags)
1128 return lsetxattr (path, name, value, (ulong) value.Length, flags);
1131 public static int lsetxattr (string path, string name, byte [] value)
1133 return lsetxattr (path, name, value, (ulong) value.Length);
1137 // int fsetxattr (int fd, const char *name,
1138 // const void *value, size_t size, int flags);
1139 [DllImport (MPH, SetLastError=true,
1140 EntryPoint="Mono_Posix_Syscall_fsetxattr")]
1141 public static extern int fsetxattr (int fd, string name, byte[] value, ulong size, XattrFlags flags);
1143 public static int fsetxattr (int fd, string name, byte [] value, ulong size)
1145 return fsetxattr (fd, name, value, size, XattrFlags.XATTR_AUTO);
1148 public static int fsetxattr (int fd, string name, byte [] value, XattrFlags flags)
1150 return fsetxattr (fd, name, value, (ulong) value.Length, flags);
1153 public static int fsetxattr (int fd, string name, byte [] value)
1155 return fsetxattr (fd, name, value, (ulong) value.Length);
1159 // ssize_t getxattr (const char *path, const char *name,
1160 // void *value, size_t size);
1161 [DllImport (MPH, SetLastError=true,
1162 EntryPoint="Mono_Posix_Syscall_getxattr")]
1163 public static extern long getxattr (string path, string name, byte[] value, ulong size);
1165 public static long getxattr (string path, string name, byte [] value)
1167 return getxattr (path, name, value, (ulong) value.Length);
1170 public static long getxattr (string path, string name, out byte [] value)
1173 long size = getxattr (path, name, value, 0);
1177 value = new byte [size];
1178 return getxattr (path, name, value, (ulong) size);
1182 // ssize_t lgetxattr (const char *path, const char *name,
1183 // void *value, size_t size);
1184 [DllImport (MPH, SetLastError=true,
1185 EntryPoint="Mono_Posix_Syscall_lgetxattr")]
1186 public static extern long lgetxattr (string path, string name, byte[] value, ulong size);
1188 public static long lgetxattr (string path, string name, byte [] value)
1190 return lgetxattr (path, name, value, (ulong) value.Length);
1193 public static long lgetxattr (string path, string name, out byte [] value)
1196 long size = lgetxattr (path, name, value, 0);
1200 value = new byte [size];
1201 return lgetxattr (path, name, value, (ulong) size);
1205 // ssize_t fgetxattr (int fd, const char *name, void *value, size_t size);
1206 [DllImport (MPH, SetLastError=true,
1207 EntryPoint="Mono_Posix_Syscall_fgetxattr")]
1208 public static extern long fgetxattr (int fd, string name, byte[] value, ulong size);
1210 public static long fgetxattr (int fd, string name, byte [] value)
1212 return fgetxattr (fd, name, value, (ulong) value.Length);
1215 public static long fgetxattr (int fd, string name, out byte [] value)
1218 long size = fgetxattr (fd, name, value, 0);
1222 value = new byte [size];
1223 return fgetxattr (fd, name, value, (ulong) size);
1227 // ssize_t listxattr (const char *path, char *list, size_t size);
1228 [DllImport (MPH, SetLastError=true,
1229 EntryPoint="Mono_Posix_Syscall_listxattr")]
1230 public static extern long listxattr (string path, byte[] list, ulong size);
1232 // Slight modification: returns 0 on success, negative on error
1233 public static long listxattr (string path, Encoding encoding, out string [] values)
1236 long size = listxattr (path, null, 0);
1238 values = new string [0];
1242 byte[] list = new byte [size];
1243 long ret = listxattr (path, list, (ulong) size);
1247 string [] output = encoding.GetString (list).Split((char) 0);
1248 values = new string [output.Length - 1];
1249 Array.Copy (output, 0, values, 0, output.Length - 1);
1254 // ssize_t llistxattr (const char *path, char *list, size_t size);
1255 [DllImport (MPH, SetLastError=true,
1256 EntryPoint="Mono_Posix_Syscall_llistxattr")]
1257 public static extern long llistxattr (string path, byte[] list, ulong size);
1259 // Slight modification: returns 0 on success, negative on error
1260 public static long llistxattr (string path, Encoding encoding, out string [] values)
1263 long size = llistxattr (path, null, 0);
1265 values = new string [0];
1269 byte[] list = new byte [size];
1270 long ret = llistxattr (path, list, (ulong) size);
1274 string [] output = encoding.GetString (list).Split((char) 0);
1275 values = new string [output.Length - 1];
1276 Array.Copy (output, 0, values, 0, output.Length - 1);
1281 // ssize_t flistxattr (int fd, char *list, size_t size);
1282 [DllImport (MPH, SetLastError=true,
1283 EntryPoint="Mono_Posix_Syscall_flistxattr")]
1284 public static extern long flistxattr (int fd, byte[] list, ulong size);
1286 // Slight modification: returns 0 on success, negative on error
1287 public static long flistxattr (int fd, Encoding encoding, out string [] values)
1290 long size = flistxattr (fd, null, 0);
1292 values = new string [0];
1296 byte[] list = new byte [size];
1297 long ret = flistxattr (fd, list, (ulong) size);
1301 string [] output = encoding.GetString (list).Split((char) 0);
1302 values = new string [output.Length - 1];
1303 Array.Copy (output, 0, values, 0, output.Length - 1);
1307 [DllImport (MPH, SetLastError=true,
1308 EntryPoint="Mono_Posix_Syscall_removexattr")]
1309 public static extern int removexattr (string path, string name);
1311 [DllImport (MPH, SetLastError=true,
1312 EntryPoint="Mono_Posix_Syscall_lremovexattr")]
1313 public static extern int lremovexattr (string path, string name);
1315 [DllImport (MPH, SetLastError=true,
1316 EntryPoint="Mono_Posix_Syscall_fremovexattr")]
1317 public static extern int fremovexattr (int fd, string name);
1320 #region <dirent.h> Declarations
1324 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1326 [DllImport (LIBC, SetLastError=true)]
1327 public static extern IntPtr opendir (string name);
1329 [DllImport (LIBC, SetLastError=true)]
1330 public static extern int closedir (IntPtr dir);
1333 // void seekdir (DIR *dir, off_t offset);
1334 // Slight modification. Returns -1 on error, 0 on success.
1335 [DllImport (MPH, SetLastError=true,
1336 EntryPoint="Mono_Posix_Syscall_seekdir")]
1337 public static extern int seekdir (IntPtr dir, long offset);
1340 // off_t telldir(DIR *dir);
1341 [DllImport (MPH, SetLastError=true,
1342 EntryPoint="Mono_Posix_Syscall_telldir")]
1343 public static extern long telldir (IntPtr dir);
1345 [DllImport (LIBC, SetLastError=true)]
1346 public static extern void rewinddir (IntPtr dir);
1348 private struct _Dirent {
1349 public /* ino_t */ ulong d_ino;
1350 public /* off_t */ long d_off;
1351 public ushort d_reclen;
1353 public IntPtr d_name;
1356 private static void CopyDirent (Dirent to, ref _Dirent from)
1359 to.d_ino = from.d_ino;
1360 to.d_off = from.d_off;
1361 to.d_reclen = from.d_reclen;
1362 to.d_type = from.d_type;
1363 to.d_name = UnixMarshal.PtrToString (from.d_name);
1366 Stdlib.free (from.d_name);
1367 from.d_name = IntPtr.Zero;
1371 [DllImport (MPH, SetLastError=true,
1372 EntryPoint="Mono_Posix_Syscall_readdir")]
1373 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
1375 public static Dirent readdir (IntPtr dir)
1378 int r = sys_readdir (dir, out dentry);
1381 Dirent d = new Dirent ();
1382 CopyDirent (d, ref dentry);
1386 [DllImport (MPH, SetLastError=true,
1387 EntryPoint="Mono_Posix_Syscall_readdir_r")]
1388 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
1390 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
1396 entry.d_name = null;
1399 int r = sys_readdir_r (dirp, out _d, out result);
1401 if (r == 0 && result != IntPtr.Zero) {
1402 CopyDirent (entry, ref _d);
1408 [DllImport (LIBC, SetLastError=true)]
1409 public static extern int dirfd (IntPtr dir);
1412 #region <fcntl.h> Declarations
1414 // <fcntl.h> -- COMPLETE
1417 [DllImport (MPH, SetLastError=true,
1418 EntryPoint="Mono_Posix_Syscall_fcntl")]
1419 public static extern int fcntl (int fd, FcntlCommand cmd);
1421 [DllImport (MPH, SetLastError=true,
1422 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
1423 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
1425 public static int fcntl (int fd, FcntlCommand cmd, DirectoryNotifyFlags arg)
1427 if (cmd != FcntlCommand.F_NOTIFY) {
1428 SetLastError (Error.EINVAL);
1431 long _arg = UnixConvert.FromDirectoryNotifyFlags (arg);
1432 return fcntl (fd, FcntlCommand.F_NOTIFY, _arg);
1435 [DllImport (MPH, SetLastError=true,
1436 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
1437 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
1439 [DllImport (MPH, SetLastError=true,
1440 EntryPoint="Mono_Posix_Syscall_open")]
1441 public static extern int open (string pathname, OpenFlags flags);
1444 // int open(const char *pathname, int flags, mode_t mode);
1445 [DllImport (MPH, SetLastError=true,
1446 EntryPoint="Mono_Posix_Syscall_open_mode")]
1447 public static extern int open (string pathname, OpenFlags flags, FilePermissions mode);
1450 // int creat(const char *pathname, mode_t mode);
1451 [DllImport (MPH, SetLastError=true,
1452 EntryPoint="Mono_Posix_Syscall_creat")]
1453 public static extern int creat (string pathname, FilePermissions mode);
1456 // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
1457 [DllImport (MPH, SetLastError=true,
1458 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
1459 public static extern int posix_fadvise (int fd, long offset,
1460 long len, PosixFadviseAdvice advice);
1462 // posix_fallocate(P)
1463 // int posix_fallocate(int fd, off_t offset, size_t len);
1464 [DllImport (MPH, SetLastError=true,
1465 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
1466 public static extern int posix_fallocate (int fd, long offset, ulong len);
1469 #region <fstab.h> Declarations
1471 // <fstab.h> -- COMPLETE
1473 private struct _Fstab {
1474 public IntPtr fs_spec;
1475 public IntPtr fs_file;
1476 public IntPtr fs_vfstype;
1477 public IntPtr fs_mntops;
1478 public IntPtr fs_type;
1480 public int fs_passno;
1481 public IntPtr _fs_buf_;
1484 private static void CopyFstab (Fstab to, ref _Fstab from)
1487 to.fs_spec = UnixMarshal.PtrToString (from.fs_spec);
1488 to.fs_file = UnixMarshal.PtrToString (from.fs_file);
1489 to.fs_vfstype = UnixMarshal.PtrToString (from.fs_vfstype);
1490 to.fs_mntops = UnixMarshal.PtrToString (from.fs_mntops);
1491 to.fs_type = UnixMarshal.PtrToString (from.fs_type);
1492 to.fs_freq = from.fs_freq;
1493 to.fs_passno = from.fs_passno;
1496 Stdlib.free (from._fs_buf_);
1497 from._fs_buf_ = IntPtr.Zero;
1501 internal static object fstab_lock = new object ();
1503 [DllImport (MPH, SetLastError=true,
1504 EntryPoint="Mono_Posix_Syscall_endfsent")]
1505 private static extern void sys_endfsent ();
1507 public static void endfsent ()
1514 [DllImport (MPH, SetLastError=true,
1515 EntryPoint="Mono_Posix_Syscall_getfsent")]
1516 private static extern int sys_getfsent (out _Fstab fs);
1518 public static Fstab getfsent ()
1523 r = sys_getfsent (out fsbuf);
1527 Fstab fs = new Fstab ();
1528 CopyFstab (fs, ref fsbuf);
1532 [DllImport (MPH, SetLastError=true,
1533 EntryPoint="Mono_Posix_Syscall_getfsfile")]
1534 private static extern int sys_getfsfile (string mount_point, out _Fstab fs);
1536 public static Fstab getfsfile (string mount_point)
1541 r = sys_getfsfile (mount_point, out fsbuf);
1545 Fstab fs = new Fstab ();
1546 CopyFstab (fs, ref fsbuf);
1550 [DllImport (MPH, SetLastError=true,
1551 EntryPoint="Mono_Posix_Syscall_getfsspec")]
1552 private static extern int sys_getfsspec (string special_file, out _Fstab fs);
1554 public static Fstab getfsspec (string special_file)
1559 r = sys_getfsspec (special_file, out fsbuf);
1563 Fstab fs = new Fstab ();
1564 CopyFstab (fs, ref fsbuf);
1568 [DllImport (MPH, SetLastError=true,
1569 EntryPoint="Mono_Posix_Syscall_setfsent")]
1570 private static extern int sys_setfsent ();
1572 public static int setfsent ()
1575 return sys_setfsent ();
1581 #region <grp.h> Declarations
1585 // TODO: putgrent(3), fgetgrent_r(), getgrouplist(2), initgroups(3)
1588 // int setgroups (size_t size, const gid_t *list);
1589 [DllImport (MPH, SetLastError=true,
1590 EntryPoint="Mono_Posix_Syscall_setgroups")]
1591 public static extern int setgroups (ulong size, uint[] list);
1593 public static int setgroups (uint [] list)
1595 return setgroups ((ulong) list.Length, list);
1598 private struct _Group
1600 public IntPtr gr_name;
1601 public IntPtr gr_passwd;
1602 public /* gid_t */ uint gr_gid;
1603 public int _gr_nmem_;
1604 public IntPtr gr_mem;
1605 public IntPtr _gr_buf_;
1608 private static void CopyGroup (Group to, ref _Group from)
1611 to.gr_gid = from.gr_gid;
1612 to.gr_name = UnixMarshal.PtrToString (from.gr_name);
1613 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
1614 to.gr_mem = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
1617 Stdlib.free (from.gr_mem);
1618 Stdlib.free (from._gr_buf_);
1619 from.gr_mem = IntPtr.Zero;
1620 from._gr_buf_ = IntPtr.Zero;
1624 internal static object grp_lock = new object ();
1626 [DllImport (MPH, SetLastError=true,
1627 EntryPoint="Mono_Posix_Syscall_getgrnam")]
1628 private static extern int sys_getgrnam (string name, out _Group group);
1630 public static Group getgrnam (string name)
1635 r = sys_getgrnam (name, out group);
1639 Group gr = new Group ();
1640 CopyGroup (gr, ref group);
1645 // struct group *getgrgid(gid_t gid);
1646 [DllImport (MPH, SetLastError=true,
1647 EntryPoint="Mono_Posix_Syscall_getgrgid")]
1648 private static extern int sys_getgrgid (uint uid, out _Group group);
1650 public static Group getgrgid (uint uid)
1655 r = sys_getgrgid (uid, out group);
1659 Group gr = new Group ();
1660 CopyGroup (gr, ref group);
1664 [DllImport (MPH, SetLastError=true,
1665 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
1666 private static extern int sys_getgrnam_r (string name, out _Group grbuf, out IntPtr grbufp);
1668 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
1673 int r = sys_getgrnam_r (name, out group, out _grbufp);
1674 if (r == 0 && _grbufp != IntPtr.Zero) {
1675 CopyGroup (grbuf, ref group);
1682 // int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
1683 // size_t buflen, struct group **gbufp);
1684 [DllImport (MPH, SetLastError=true,
1685 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
1686 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
1688 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
1693 int r = sys_getgrgid_r (uid, out group, out _grbufp);
1694 if (r == 0 && _grbufp != IntPtr.Zero) {
1695 CopyGroup (grbuf, ref group);
1701 [DllImport (MPH, SetLastError=true,
1702 EntryPoint="Mono_Posix_Syscall_getgrent")]
1703 private static extern int sys_getgrent (out _Group grbuf);
1705 public static Group getgrent ()
1710 r = sys_getgrent (out group);
1714 Group gr = new Group();
1715 CopyGroup (gr, ref group);
1719 [DllImport (LIBC, SetLastError=true, EntryPoint="setgrent")]
1720 private static extern void sys_setgrent ();
1722 public static void setgrent ()
1729 [DllImport (LIBC, SetLastError=true, EntryPoint="endgrent")]
1730 private static extern void sys_endgrent ();
1732 public static void endgrent ()
1739 [DllImport (MPH, SetLastError=true,
1740 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
1741 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
1743 public static Group fgetgrent (IntPtr stream)
1748 r = sys_fgetgrent (stream, out group);
1752 Group gr = new Group ();
1753 CopyGroup (gr, ref group);
1758 #region <pwd.h> Declarations
1762 // TODO: putpwent(3), fgetpwent_r()
1764 // SKIPPING: getpw(3): it's dangerous. Use getpwuid(3) instead.
1766 private struct _Passwd
1768 public IntPtr pw_name;
1769 public IntPtr pw_passwd;
1770 public /* uid_t */ uint pw_uid;
1771 public /* gid_t */ uint pw_gid;
1772 public IntPtr pw_gecos;
1773 public IntPtr pw_dir;
1774 public IntPtr pw_shell;
1775 public IntPtr _pw_buf_;
1778 private static void CopyPasswd (Passwd to, ref _Passwd from)
1781 to.pw_name = UnixMarshal.PtrToString (from.pw_name);
1782 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
1783 to.pw_uid = from.pw_uid;
1784 to.pw_gid = from.pw_gid;
1785 to.pw_gecos = UnixMarshal.PtrToString (from.pw_gecos);
1786 to.pw_dir = UnixMarshal.PtrToString (from.pw_dir);
1787 to.pw_shell = UnixMarshal.PtrToString (from.pw_shell);
1790 Stdlib.free (from._pw_buf_);
1791 from._pw_buf_ = IntPtr.Zero;
1795 internal static object pwd_lock = new object ();
1797 [DllImport (MPH, SetLastError=true,
1798 EntryPoint="Mono_Posix_Syscall_getpwnam")]
1799 private static extern int sys_getpwnam (string name, out _Passwd passwd);
1801 public static Passwd getpwnam (string name)
1806 r = sys_getpwnam (name, out passwd);
1810 Passwd pw = new Passwd ();
1811 CopyPasswd (pw, ref passwd);
1816 // struct passwd *getpwnuid(uid_t uid);
1817 [DllImport (MPH, SetLastError=true,
1818 EntryPoint="Mono_Posix_Syscall_getpwuid")]
1819 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
1821 public static Passwd getpwuid (uint uid)
1826 r = sys_getpwuid (uid, out passwd);
1830 Passwd pw = new Passwd ();
1831 CopyPasswd (pw, ref passwd);
1835 [DllImport (MPH, SetLastError=true,
1836 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
1837 private static extern int sys_getpwnam_r (string name, out _Passwd pwbuf, out IntPtr pwbufp);
1839 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
1844 int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
1845 if (r == 0 && _pwbufp != IntPtr.Zero) {
1846 CopyPasswd (pwbuf, ref passwd);
1853 // int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
1854 // buflen, struct passwd **pwbufp);
1855 [DllImport (MPH, SetLastError=true,
1856 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
1857 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
1859 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
1864 int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
1865 if (r == 0 && _pwbufp != IntPtr.Zero) {
1866 CopyPasswd (pwbuf, ref passwd);
1872 [DllImport (MPH, SetLastError=true,
1873 EntryPoint="Mono_Posix_Syscall_getpwent")]
1874 private static extern int sys_getpwent (out _Passwd pwbuf);
1876 public static Passwd getpwent ()
1881 r = sys_getpwent (out passwd);
1885 Passwd pw = new Passwd ();
1886 CopyPasswd (pw, ref passwd);
1890 [DllImport (LIBC, SetLastError=true, EntryPoint="setpwent")]
1891 private static extern void sys_setpwent ();
1893 public static void setpwent ()
1900 [DllImport (LIBC, SetLastError=true, EntryPoint="endpwent")]
1901 private static extern void sys_endpwent ();
1903 public static void endpwent ()
1910 [DllImport (MPH, SetLastError=true,
1911 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
1912 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
1914 public static Passwd fgetpwent (IntPtr stream)
1919 r = sys_fgetpwent (stream, out passwd);
1923 Passwd pw = new Passwd ();
1924 CopyPasswd (pw, ref passwd);
1929 #region <signal.h> Declarations
1933 [DllImport (LIBC, SetLastError=true)]
1934 private static extern void psignal (int sig, string s);
1936 public static void psignal (Signum sig, string s)
1938 int signum = UnixConvert.FromSignum (sig);
1939 psignal (signum, s);
1943 // int kill(pid_t pid, int sig);
1944 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
1945 private static extern int sys_kill (int pid, int sig);
1947 public static int kill (int pid, Signum sig)
1949 int _sig = UnixConvert.FromSignum (sig);
1950 return sys_kill (pid, _sig);
1953 private static object signal_lock = new object ();
1955 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
1956 private static extern IntPtr sys_strsignal (int sig);
1958 public static string strsignal (Signum sig)
1960 int s = UnixConvert.FromSignum (sig);
1961 lock (signal_lock) {
1962 IntPtr r = sys_strsignal (s);
1963 return UnixMarshal.PtrToString (r);
1967 // TODO: sigaction(2)
1968 // TODO: sigsuspend(2)
1969 // TODO: sigpending(2)
1973 #region <stdio.h> Declarations
1977 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
1978 private static extern int _L_ctermid ();
1980 public static readonly int L_ctermid = _L_ctermid ();
1982 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
1983 private static extern int _L_cuserid ();
1985 public static readonly int L_cuserid = _L_cuserid ();
1987 internal static object getlogin_lock = new object ();
1989 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
1990 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
1992 [Obsolete ("\"Nobody knows precisely what cuserid() does... " +
1993 "DO NOT USE cuserid().\n" +
1994 "`string' must hold L_cuserid characters. Use getlogin_r instead.")]
1995 public static string cuserid (StringBuilder @string)
1997 if (@string.Capacity < L_cuserid) {
1998 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
2000 lock (getlogin_lock) {
2001 IntPtr r = sys_cuserid (@string);
2002 return UnixMarshal.PtrToString (r);
2008 #region <stdlib.h> Declarations
2012 [DllImport (LIBC, SetLastError=true)]
2013 public static extern int mkstemp (StringBuilder template);
2015 [DllImport (LIBC, SetLastError=true)]
2016 public static extern int ttyslot ();
2018 [DllImport (CRYPT, SetLastError=true)]
2019 public static extern void setkey (string key);
2023 #region <string.h> Declarations
2029 // int strerror_r(int errnum, char *buf, size_t n);
2030 [DllImport (MPH, SetLastError=true,
2031 EntryPoint="Mono_Posix_Syscall_strerror_r")]
2032 private static extern int sys_strerror_r (int errnum,
2033 [Out] StringBuilder buf, ulong n);
2035 public static int strerror_r (Error errnum, StringBuilder buf, ulong n)
2037 int e = UnixConvert.FromError (errnum);
2038 return sys_strerror_r (e, buf, n);
2041 public static int strerror_r (Error errnum, StringBuilder buf)
2043 return strerror_r (errnum, buf, (ulong) buf.Capacity);
2048 #region <sys/mman.h> Declarations
2054 // int posix_madvise(void *addr, size_t len, int advice);
2055 [DllImport (MPH, SetLastError=true,
2056 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
2057 public static extern int posix_madvise (IntPtr addr, ulong len,
2058 PosixMadviseAdvice advice);
2060 public static readonly IntPtr MAP_FAILED = unchecked((IntPtr)(-1));
2062 [DllImport (MPH, SetLastError=true,
2063 EntryPoint="Mono_Posix_Syscall_mmap")]
2064 public static extern IntPtr mmap (IntPtr start, ulong length,
2065 MmapProt prot, MmapFlags flags, int fd, long offset);
2067 [DllImport (MPH, SetLastError=true,
2068 EntryPoint="Mono_Posix_Syscall_munmap")]
2069 public static extern int munmap (IntPtr start, ulong length);
2071 [DllImport (MPH, SetLastError=true,
2072 EntryPoint="Mono_Posix_Syscall_mprotect")]
2073 public static extern int mprotect (IntPtr start, ulong len, MmapProt prot);
2075 [DllImport (MPH, SetLastError=true,
2076 EntryPoint="Mono_Posix_Syscall_msync")]
2077 public static extern int msync (IntPtr start, ulong len, MsyncFlags flags);
2079 [DllImport (MPH, SetLastError=true,
2080 EntryPoint="Mono_Posix_Syscall_mlock")]
2081 public static extern int mlock (IntPtr start, ulong len);
2083 [DllImport (MPH, SetLastError=true,
2084 EntryPoint="Mono_Posix_Syscall_munlock")]
2085 public static extern int munlock (IntPtr start, ulong len);
2087 [DllImport (LIBC, SetLastError=true, EntryPoint="mlockall")]
2088 private static extern int sys_mlockall (int flags);
2090 public static int mlockall (MlockallFlags flags)
2092 int _flags = UnixConvert.FromMlockallFlags (flags);
2093 return sys_mlockall (_flags);
2096 [DllImport (LIBC, SetLastError=true)]
2097 public static extern int munlockall ();
2099 [DllImport (MPH, SetLastError=true,
2100 EntryPoint="Mono_Posix_Syscall_mremap")]
2101 public static extern IntPtr mremap (IntPtr old_address, ulong old_size,
2102 ulong new_size, MremapFlags flags);
2104 [DllImport (MPH, SetLastError=true,
2105 EntryPoint="Mono_Posix_Syscall_mincore")]
2106 public static extern int mincore (IntPtr start, ulong length, byte[] vec);
2108 [DllImport (MPH, SetLastError=true,
2109 EntryPoint="Mono_Posix_Syscall_remap_file_pages")]
2110 public static extern int remap_file_pages (IntPtr start, ulong size,
2111 MmapProt prot, long pgoff, MmapFlags flags);
2115 #region <sys/poll.h> Declarations
2117 // <sys/poll.h> -- COMPLETE
2120 private struct _pollfd {
2122 public short events;
2123 public short revents;
2126 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
2127 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
2129 public static int poll (Pollfd [] fds, uint nfds, int timeout)
2131 if (fds.Length < nfds)
2132 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
2134 _pollfd[] send = new _pollfd[nfds];
2136 for (int i = 0; i < send.Length; i++) {
2137 send [i].fd = fds [i].fd;
2138 send [i].events = UnixConvert.FromPollEvents (fds [i].events);
2141 int r = sys_poll (send, nfds, timeout);
2143 for (int i = 0; i < send.Length; i++) {
2144 fds [i].revents = UnixConvert.ToPollEvents (send [i].revents);
2150 public static int poll (Pollfd [] fds, int timeout)
2152 return poll (fds, (uint) fds.Length, timeout);
2165 // TODO: setrlimit(2)
2166 // TODO: getrlimit(2)
2167 // TODO: getrusage(2)
2171 #region <sys/sendfile.h> Declarations
2173 // <sys/sendfile.h> -- COMPLETE
2176 [DllImport (MPH, SetLastError=true,
2177 EntryPoint="Mono_Posix_Syscall_sendfile")]
2178 public static extern long sendfile (int out_fd, int in_fd,
2179 ref long offset, ulong count);
2183 #region <sys/stat.h> Declarations
2185 // <sys/stat.h> -- COMPLETE
2187 [DllImport (MPH, SetLastError=true,
2188 EntryPoint="Mono_Posix_Syscall_stat")]
2189 public static extern int stat (string file_name, out Stat buf);
2191 [DllImport (MPH, SetLastError=true,
2192 EntryPoint="Mono_Posix_Syscall_fstat")]
2193 public static extern int fstat (int filedes, out Stat buf);
2195 [DllImport (MPH, SetLastError=true,
2196 EntryPoint="Mono_Posix_Syscall_lstat")]
2197 public static extern int lstat (string file_name, out Stat buf);
2200 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
2201 // All take FilePermissions
2204 // int chmod(const char *path, mode_t mode);
2205 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
2206 private static extern int sys_chmod (string path, uint mode);
2208 public static int chmod (string path, FilePermissions mode)
2210 uint _mode = UnixConvert.FromFilePermissions (mode);
2211 return sys_chmod (path, _mode);
2215 // int chmod(int filedes, mode_t mode);
2216 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
2217 private static extern int sys_fchmod (int filedes, uint mode);
2219 public static int fchmod (int filedes, FilePermissions mode)
2221 uint _mode = UnixConvert.FromFilePermissions (mode);
2222 return sys_fchmod (filedes, _mode);
2226 // mode_t umask(mode_t mask);
2227 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
2228 private static extern uint sys_umask (uint mask);
2230 public static FilePermissions umask (FilePermissions mask)
2232 uint _mask = UnixConvert.FromFilePermissions (mask);
2233 uint r = sys_umask (_mask);
2234 return UnixConvert.ToFilePermissions (r);
2238 // int mkdir(const char *pathname, mode_t mode);
2239 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
2240 private static extern int sys_mkdir (string oldpath, uint mode);
2242 public static int mkdir (string oldpath, FilePermissions mode)
2244 uint _mode = UnixConvert.FromFilePermissions (mode);
2245 return sys_mkdir (oldpath, _mode);
2249 // int mknod (const char *pathname, mode_t mode, dev_t dev);
2250 [DllImport (MPH, SetLastError=true,
2251 EntryPoint="Mono_Posix_Syscall_mknod")]
2252 public static extern int mknod (string pathname, FilePermissions mode, ulong dev);
2255 // int mkfifo(const char *pathname, mode_t mode);
2256 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
2257 private static extern int sys_mkfifo (string pathname, uint mode);
2259 public static int mkfifo (string pathname, FilePermissions mode)
2261 uint _mode = UnixConvert.FromFilePermissions (mode);
2262 return sys_mkfifo (pathname, _mode);
2267 #region <sys/stat.h> Declarations
2272 [DllImport (MPH, SetLastError=true,
2273 EntryPoint="Mono_Posix_Syscall_statvfs")]
2274 public static extern int statvfs (string path, out Statvfs buf);
2276 [DllImport (MPH, SetLastError=true,
2277 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
2278 public static extern int fstatvfs (int fd, out Statvfs buf);
2282 #region <sys/time.h> Declarations
2286 // TODO: adjtime(), getitimer(2), setitimer(2), lutimes(), futimes()
2288 [DllImport (MPH, SetLastError=true,
2289 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2290 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
2292 [DllImport (MPH, SetLastError=true,
2293 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2294 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
2296 public static int gettimeofday (out Timeval tv)
2298 return gettimeofday (out tv, IntPtr.Zero);
2301 [DllImport (MPH, SetLastError=true,
2302 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2303 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
2305 public static int gettimeofday (out Timezone tz)
2307 return gettimeofday (IntPtr.Zero, out tz);
2310 [DllImport (MPH, SetLastError=true,
2311 EntryPoint="Mono_Posix_Syscall_settimeofday")]
2312 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
2314 [DllImport (MPH, SetLastError=true,
2315 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2316 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
2318 public static int settimeofday (ref Timeval tv)
2320 return settimeofday (ref tv, IntPtr.Zero);
2323 [DllImport (MPH, SetLastError=true,
2324 EntryPoint="Mono_Posix_Syscall_utimes")]
2325 public static extern int utimes (string filename, ref Timeval tvp);
2341 #region <sys/wait.h> Declarations
2347 // pid_t wait(int *status);
2348 [DllImport (LIBC, SetLastError=true)]
2349 public static extern int wait (out int status);
2352 // pid_t waitpid(pid_t pid, int *status, int options);
2353 [DllImport (LIBC, SetLastError=true)]
2354 private static extern int waitpid (int pid, out int status, int options);
2356 public static int waitpid (int pid, out int status, WaitOptions options)
2358 int _options = UnixConvert.FromWaitOptions (options);
2359 return waitpid (pid, out status, _options);
2362 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
2363 private static extern int _WIFEXITED (int status);
2365 public static bool WIFEXITED (int status)
2367 return _WIFEXITED (status) != 0;
2370 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
2371 public static extern int WEXITSTATUS (int status);
2373 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
2374 private static extern int _WIFSIGNALED (int status);
2376 public static bool WIFSIGNALED (int status)
2378 return _WIFSIGNALED (status) != 0;
2381 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
2382 private static extern int _WTERMSIG (int status);
2384 public static Signum WTERMSIG (int status)
2386 int r = _WTERMSIG (status);
2387 return UnixConvert.ToSignum (r);
2390 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
2391 private static extern int _WIFSTOPPED (int status);
2393 public static bool WIFSTOPPED (int status)
2395 return _WIFSTOPPED (status) != 0;
2398 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
2399 private static extern int _WSTOPSIG (int status);
2401 public static Signum WSTOPSIG (int status)
2403 int r = _WSTOPSIG (status);
2404 return UnixConvert.ToSignum (r);
2413 #region <syslog.h> Declarations
2418 [DllImport (LIBC, EntryPoint="openlog")]
2419 private static extern void sys_openlog (IntPtr ident, int option, int facility);
2421 public static void openlog (IntPtr ident, SyslogOptions option,
2422 SyslogFacility defaultFacility)
2424 int _option = UnixConvert.FromSyslogOptions (option);
2425 int _facility = UnixConvert.FromSyslogFacility (defaultFacility);
2427 sys_openlog (ident, _option, _facility);
2430 [DllImport (LIBC, EntryPoint="syslog")]
2431 private static extern void sys_syslog (int priority, string message);
2433 public static void syslog (SyslogFacility facility, SyslogLevel level, string message)
2435 int _facility = UnixConvert.FromSyslogFacility (facility);
2436 int _level = UnixConvert.FromSyslogLevel (level);
2437 sys_syslog (_facility | _level, GetSyslogMessage (message));
2440 public static void syslog (SyslogLevel level, string message)
2442 int _level = UnixConvert.FromSyslogLevel (level);
2443 sys_syslog (_level, GetSyslogMessage (message));
2446 private static string GetSyslogMessage (string message)
2448 return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
2451 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2452 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
2453 public static void syslog (SyslogFacility facility, SyslogLevel level,
2454 string format, params object[] parameters)
2456 int _facility = UnixConvert.FromSyslogFacility (facility);
2457 int _level = UnixConvert.FromSyslogLevel (level);
2459 object[] _parameters = new object[checked(parameters.Length+2)];
2460 _parameters [0] = _facility | _level;
2461 _parameters [1] = format;
2462 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2463 XPrintfFunctions.syslog (_parameters);
2466 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2467 "Use syslog(SyslogLevel, string) instead.")]
2468 public static void syslog (SyslogLevel level, string format,
2469 params object[] parameters)
2471 int _level = UnixConvert.FromSyslogLevel (level);
2473 object[] _parameters = new object[checked(parameters.Length+2)];
2474 _parameters [0] = _level;
2475 _parameters [1] = format;
2476 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2477 XPrintfFunctions.syslog (_parameters);
2481 public static extern void closelog ();
2483 [DllImport (LIBC, EntryPoint="setlogmask")]
2484 private static extern int sys_setlogmask (int mask);
2486 public static int setlogmask (SyslogLevel mask)
2488 int _mask = UnixConvert.FromSyslogLevel (mask);
2489 return sys_setlogmask (_mask);
2494 #region <time.h> Declarations
2501 // int stime(time_t *t);
2502 [DllImport (MPH, SetLastError=true,
2503 EntryPoint="Mono_Posix_Syscall_stime")]
2504 public static extern int stime (ref long t);
2507 // time_t time(time_t *t);
2508 [DllImport (MPH, SetLastError=true,
2509 EntryPoint="Mono_Posix_Syscall_time")]
2510 public static extern long time (out long t);
2520 #region <unistd.h> Declarations
2524 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
2525 // other TODOs listed below.
2527 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
2528 private static extern int sys_access (string pathname, int mode);
2530 public static int access (string pathname, AccessMode mode)
2532 int _mode = UnixConvert.FromAccessMode (mode);
2533 return sys_access (pathname, _mode);
2537 // off_t lseek(int filedes, off_t offset, int whence);
2538 [DllImport (MPH, SetLastError=true,
2539 EntryPoint="Mono_Posix_Syscall_lseek")]
2540 private static extern long sys_lseek (int fd, long offset, int whence);
2542 public static long lseek (int fd, long offset, SeekFlags whence)
2544 short _whence = UnixConvert.FromSeekFlags (whence);
2545 return sys_lseek (fd, offset, _whence);
2548 [DllImport (LIBC, SetLastError=true)]
2549 public static extern int close (int fd);
2552 // ssize_t read(int fd, void *buf, size_t count);
2553 [DllImport (MPH, SetLastError=true,
2554 EntryPoint="Mono_Posix_Syscall_read")]
2555 public static extern long read (int fd, IntPtr buf, ulong count);
2557 public static unsafe long read (int fd, void *buf, ulong count)
2559 return read (fd, (IntPtr) buf, count);
2563 // ssize_t write(int fd, const void *buf, size_t count);
2564 [DllImport (MPH, SetLastError=true,
2565 EntryPoint="Mono_Posix_Syscall_write")]
2566 public static extern long write (int fd, IntPtr buf, ulong count);
2568 public static unsafe long write (int fd, void *buf, ulong count)
2570 return write (fd, (IntPtr) buf, count);
2574 // ssize_t pread(int fd, void *buf, size_t count, off_t offset);
2575 [DllImport (MPH, SetLastError=true,
2576 EntryPoint="Mono_Posix_Syscall_pread")]
2577 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
2579 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
2581 return pread (fd, (IntPtr) buf, count, offset);
2585 // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
2586 [DllImport (MPH, SetLastError=true,
2587 EntryPoint="Mono_Posix_Syscall_pwrite")]
2588 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
2590 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
2592 return pwrite (fd, (IntPtr) buf, count, offset);
2595 [DllImport (MPH, SetLastError=true,
2596 EntryPoint="Mono_Posix_Syscall_pipe")]
2597 public static extern int pipe (out int reading, out int writing);
2599 public static int pipe (int[] filedes)
2601 if (filedes == null || filedes.Length != 2) {
2605 int reading, writing;
2606 int r = pipe (out reading, out writing);
2607 filedes[0] = reading;
2608 filedes[1] = writing;
2612 [DllImport (LIBC, SetLastError=true)]
2613 public static extern uint alarm (uint seconds);
2615 [DllImport (LIBC, SetLastError=true)]
2616 public static extern uint sleep (uint seconds);
2618 [DllImport (LIBC, SetLastError=true)]
2619 public static extern uint ualarm (uint usecs, uint interval);
2621 [DllImport (LIBC, SetLastError=true)]
2622 public static extern int pause ();
2625 // int chown(const char *path, uid_t owner, gid_t group);
2626 [DllImport (LIBC, SetLastError=true)]
2627 public static extern int chown (string path, uint owner, uint group);
2630 // int fchown(int fd, uid_t owner, gid_t group);
2631 [DllImport (LIBC, SetLastError=true)]
2632 public static extern int fchown (int fd, uint owner, uint group);
2635 // int lchown(const char *path, uid_t owner, gid_t group);
2636 [DllImport (LIBC, SetLastError=true)]
2637 public static extern int lchown (string path, uint owner, uint group);
2639 [DllImport (LIBC, SetLastError=true)]
2640 public static extern int chdir (string path);
2642 [DllImport (LIBC, SetLastError=true)]
2643 public static extern int fchdir (int fd);
2646 // char *getcwd(char *buf, size_t size);
2647 [DllImport (MPH, SetLastError=true,
2648 EntryPoint="Mono_Posix_Syscall_getcwd")]
2649 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
2651 public static StringBuilder getcwd (StringBuilder buf)
2653 getcwd (buf, (ulong) buf.Capacity);
2657 // getwd(2) is deprecated; don't expose it.
2659 [DllImport (LIBC, SetLastError=true)]
2660 public static extern int dup (int fd);
2662 [DllImport (LIBC, SetLastError=true)]
2663 public static extern int dup2 (int fd, int fd2);
2665 // TODO: does Mono marshal arrays properly?
2666 [DllImport (LIBC, SetLastError=true)]
2667 public static extern int execve (string path, string[] argv, string[] envp);
2669 [DllImport (LIBC, SetLastError=true)]
2670 public static extern int fexecve (int fd, string[] argv, string[] envp);
2672 [DllImport (LIBC, SetLastError=true)]
2673 public static extern int execv (string path, string[] argv);
2675 // TODO: execle, execl, execlp
2676 [DllImport (LIBC, SetLastError=true)]
2677 public static extern int execvp (string path, string[] argv);
2679 [DllImport (LIBC, SetLastError=true)]
2680 public static extern int nice (int inc);
2682 [DllImport (LIBC, SetLastError=true)]
2683 public static extern int _exit (int status);
2685 [DllImport (MPH, SetLastError=true,
2686 EntryPoint="Mono_Posix_Syscall_fpathconf")]
2687 public static extern long fpathconf (int filedes, PathConf name);
2689 [DllImport (MPH, SetLastError=true,
2690 EntryPoint="Mono_Posix_Syscall_pathconf")]
2691 public static extern long pathconf (string path, PathConf name);
2693 [DllImport (MPH, SetLastError=true,
2694 EntryPoint="Mono_Posix_Syscall_sysconf")]
2695 public static extern long sysconf (SysConf name);
2698 // size_t confstr(int name, char *buf, size_t len);
2699 [DllImport (MPH, SetLastError=true,
2700 EntryPoint="Mono_Posix_Syscall_confstr")]
2701 public static extern ulong confstr (ConfStr name, [Out] StringBuilder buf, ulong len);
2704 // pid_t getpid(void);
2705 [DllImport (LIBC, SetLastError=true)]
2706 public static extern int getpid ();
2709 // pid_t getppid(void);
2710 [DllImport (LIBC, SetLastError=true)]
2711 public static extern int getppid ();
2714 // int setpgid(pid_t pid, pid_t pgid);
2715 [DllImport (LIBC, SetLastError=true)]
2716 public static extern int setpgid (int pid, int pgid);
2719 // pid_t getpgid(pid_t pid);
2720 [DllImport (LIBC, SetLastError=true)]
2721 public static extern int getpgid (int pid);
2723 [DllImport (LIBC, SetLastError=true)]
2724 public static extern int setpgrp ();
2727 // pid_t getpgrp(void);
2728 [DllImport (LIBC, SetLastError=true)]
2729 public static extern int getpgrp ();
2732 // pid_t setsid(void);
2733 [DllImport (LIBC, SetLastError=true)]
2734 public static extern int setsid ();
2737 // pid_t getsid(pid_t pid);
2738 [DllImport (LIBC, SetLastError=true)]
2739 public static extern int getsid (int pid);
2742 // uid_t getuid(void);
2743 [DllImport (LIBC, SetLastError=true)]
2744 public static extern uint getuid ();
2747 // uid_t geteuid(void);
2748 [DllImport (LIBC, SetLastError=true)]
2749 public static extern uint geteuid ();
2752 // gid_t getgid(void);
2753 [DllImport (LIBC, SetLastError=true)]
2754 public static extern uint getgid ();
2757 // gid_t getgid(void);
2758 [DllImport (LIBC, SetLastError=true)]
2759 public static extern uint getegid ();
2762 // int getgroups(int size, gid_t list[]);
2763 [DllImport (LIBC, SetLastError=true)]
2764 public static extern int getgroups (int size, uint[] list);
2766 public static int getgroups (uint[] list)
2768 return getgroups (list.Length, list);
2772 // int setuid(uid_t uid);
2773 [DllImport (LIBC, SetLastError=true)]
2774 public static extern int setuid (uint uid);
2777 // int setreuid(uid_t ruid, uid_t euid);
2778 [DllImport (LIBC, SetLastError=true)]
2779 public static extern int setreuid (uint ruid, uint euid);
2782 // int setregid(gid_t ruid, gid_t euid);
2783 [DllImport (LIBC, SetLastError=true)]
2784 public static extern int setregid (uint rgid, uint egid);
2787 // int seteuid(uid_t euid);
2788 [DllImport (LIBC, SetLastError=true)]
2789 public static extern int seteuid (uint euid);
2792 // int setegid(gid_t euid);
2793 [DllImport (LIBC, SetLastError=true)]
2794 public static extern int setegid (uint uid);
2797 // int setgid(gid_t gid);
2798 [DllImport (LIBC, SetLastError=true)]
2799 public static extern int setgid (uint gid);
2802 // int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
2803 [DllImport (LIBC, SetLastError=true)]
2804 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
2807 // int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
2808 [DllImport (LIBC, SetLastError=true)]
2809 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
2812 // int setresuid(uid_t ruid, uid_t euid, uid_t suid);
2813 [DllImport (LIBC, SetLastError=true)]
2814 public static extern int setresuid (uint ruid, uint euid, uint suid);
2817 // int setresgid(gid_t ruid, gid_t euid, gid_t suid);
2818 [DllImport (LIBC, SetLastError=true)]
2819 public static extern int setresgid (uint rgid, uint egid, uint sgid);
2822 // pid_t fork(void);
2823 [DllImport (LIBC, SetLastError=true)]
2824 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " +
2825 "shutdown code.\nUse System.Diagnostics.Process instead")]
2826 private static extern int fork ();
2829 // pid_t vfork(void);
2830 [DllImport (LIBC, SetLastError=true)]
2831 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " +
2832 "shutdown code.\nUse System.Diagnostics.Process instead")]
2833 private static extern int vfork ();
2835 private static object tty_lock = new object ();
2837 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
2838 private static extern IntPtr sys_ttyname (int fd);
2840 public static string ttyname (int fd)
2843 IntPtr r = sys_ttyname (fd);
2844 return UnixMarshal.PtrToString (r);
2849 // int ttyname_r(int fd, char *buf, size_t buflen);
2850 [DllImport (MPH, SetLastError=true,
2851 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
2852 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
2854 public static int ttyname_r (int fd, StringBuilder buf)
2856 return ttyname_r (fd, buf, (ulong) buf.Capacity);
2859 [DllImport (LIBC, EntryPoint="isatty")]
2860 private static extern int sys_isatty (int fd);
2862 public static bool isatty (int fd)
2864 return sys_isatty (fd) == 1;
2867 [DllImport (LIBC, SetLastError=true)]
2868 public static extern int link (string oldpath, string newpath);
2870 [DllImport (LIBC, SetLastError=true)]
2871 public static extern int symlink (string oldpath, string newpath);
2874 // int readlink(const char *path, char *buf, size_t bufsize);
2875 [DllImport (MPH, SetLastError=true,
2876 EntryPoint="Mono_Posix_Syscall_readlink")]
2877 public static extern int readlink (string path, [Out] StringBuilder buf, ulong bufsiz);
2879 public static int readlink (string path, [Out] StringBuilder buf)
2881 return readlink (path, buf, (ulong) buf.Capacity);
2884 [DllImport (LIBC, SetLastError=true)]
2885 public static extern int unlink (string pathname);
2887 [DllImport (LIBC, SetLastError=true)]
2888 public static extern int rmdir (string pathname);
2891 // pid_t tcgetpgrp(int fd);
2892 [DllImport (LIBC, SetLastError=true)]
2893 public static extern int tcgetpgrp (int fd);
2896 // int tcsetpgrp(int fd, pid_t pgrp);
2897 [DllImport (LIBC, SetLastError=true)]
2898 public static extern int tcsetpgrp (int fd, int pgrp);
2900 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
2901 private static extern IntPtr sys_getlogin ();
2903 public static string getlogin ()
2905 lock (getlogin_lock) {
2906 IntPtr r = sys_getlogin ();
2907 return UnixMarshal.PtrToString (r);
2912 // int getlogin_r(char *buf, size_t bufsize);
2913 [DllImport (MPH, SetLastError=true,
2914 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
2915 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
2917 public static int getlogin_r (StringBuilder name)
2919 return getlogin_r (name, (ulong) name.Capacity);
2922 [DllImport (LIBC, SetLastError=true)]
2923 public static extern int setlogin (string name);
2926 // int gethostname(char *name, size_t len);
2927 [DllImport (MPH, SetLastError=true,
2928 EntryPoint="Mono_Posix_Syscall_gethostname")]
2929 public static extern int gethostname ([Out] StringBuilder name, ulong len);
2931 public static int gethostname (StringBuilder name)
2933 return gethostname (name, (ulong) name.Capacity);
2937 // int gethostname(const char *name, size_t len);
2938 [DllImport (MPH, SetLastError=true,
2939 EntryPoint="Mono_Posix_Syscall_sethostname")]
2940 public static extern int sethostname (string name, ulong len);
2942 public static int sethostname (string name)
2944 return sethostname (name, (ulong) name.Length);
2947 [DllImport (MPH, SetLastError=true,
2948 EntryPoint="Mono_Posix_Syscall_gethostid")]
2949 public static extern long gethostid ();
2951 [DllImport (MPH, SetLastError=true,
2952 EntryPoint="Mono_Posix_Syscall_sethostid")]
2953 public static extern int sethostid (long hostid);
2956 // int getdomainname(char *name, size_t len);
2957 [DllImport (MPH, SetLastError=true,
2958 EntryPoint="Mono_Posix_Syscall_getdomainname")]
2959 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
2961 public static int getdomainname (StringBuilder name)
2963 return getdomainname (name, (ulong) name.Capacity);
2967 // int setdomainname(const char *name, size_t len);
2968 [DllImport (MPH, SetLastError=true,
2969 EntryPoint="Mono_Posix_Syscall_setdomainname")]
2970 public static extern int setdomainname (string name, ulong len);
2972 public static int setdomainname (string name)
2974 return setdomainname (name, (ulong) name.Length);
2977 [DllImport (LIBC, SetLastError=true)]
2978 public static extern int vhangup ();
2980 // Revoke doesn't appear to be POSIX. Include it?
2981 [DllImport (LIBC, SetLastError=true)]
2982 public static extern int revoke (string file);
2984 // TODO: profil? It's not POSIX.
2986 [DllImport (LIBC, SetLastError=true)]
2987 public static extern int acct (string filename);
2989 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
2990 private static extern IntPtr sys_getusershell ();
2992 internal static object usershell_lock = new object ();
2994 public static string getusershell ()
2996 lock (usershell_lock) {
2997 IntPtr r = sys_getusershell ();
2998 return UnixMarshal.PtrToString (r);
3002 [DllImport (LIBC, SetLastError=true, EntryPoint="setusershell")]
3003 private static extern void sys_setusershell ();
3005 public static void setusershell ()
3007 lock (usershell_lock) {
3008 sys_setusershell ();
3012 [DllImport (LIBC, SetLastError=true, EntryPoint="endusershell")]
3013 private static extern void sys_endusershell ();
3015 public static void endusershell ()
3017 lock (usershell_lock) {
3018 sys_endusershell ();
3022 [DllImport (LIBC, SetLastError=true)]
3023 private static extern int daemon (int nochdir, int noclose);
3025 public static int daemon (bool nochdir, bool noclose)
3027 return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
3030 [DllImport (LIBC, SetLastError=true)]
3031 public static extern int chroot (string path);
3033 // skipping getpass(3) as the man page states:
3034 // This function is obsolete. Do not use it.
3036 [DllImport (LIBC, SetLastError=true)]
3037 public static extern int fsync (int fd);
3039 [DllImport (LIBC, SetLastError=true)]
3040 public static extern int fdatasync (int fd);
3042 [DllImport (LIBC, SetLastError=true)]
3043 public static extern void sync ();
3045 [DllImport (LIBC, SetLastError=true)]
3046 [Obsolete ("Dropped in POSIX 1003.1-2001. " +
3047 "Use Unistd.sysconf (SysConf._SC_PAGESIZE).")]
3048 public static extern int getpagesize ();
3051 // int truncate(const char *path, off_t length);
3052 [DllImport (MPH, SetLastError=true,
3053 EntryPoint="Mono_Posix_Syscall_truncate")]
3054 public static extern int truncate (string path, long length);
3057 // int ftruncate(int fd, off_t length);
3058 [DllImport (MPH, SetLastError=true,
3059 EntryPoint="Mono_Posix_Syscall_ftruncate")]
3060 public static extern int ftruncate (int fd, long length);
3062 [DllImport (LIBC, SetLastError=true)]
3063 public static extern int getdtablesize ();
3065 [DllImport (LIBC, SetLastError=true)]
3066 public static extern int brk (IntPtr end_data_segment);
3068 [DllImport (LIBC, SetLastError=true)]
3069 public static extern IntPtr sbrk (IntPtr increment);
3071 // TODO: syscall(2)?
3072 // Probably safer to skip entirely.
3075 // int lockf(int fd, int cmd, off_t len);
3076 [DllImport (MPH, SetLastError=true,
3077 EntryPoint="Mono_Posix_Syscall_lockf")]
3078 public static extern int lockf (int fd, LockfCommand cmd, long len);
3080 internal static object crypt_lock = new object ();
3082 [DllImport (CRYPT, SetLastError=true, EntryPoint="crypt")]
3083 private static extern IntPtr sys_crypt (string key, string salt);
3085 public static string crypt (string key, string salt)
3088 IntPtr r = sys_crypt (key, salt);
3089 return UnixMarshal.PtrToString (r);
3093 internal static object encrypt_lock = new object ();
3095 [DllImport (CRYPT, SetLastError=true, EntryPoint="encrypt")]
3096 private static extern void sys_encrypt ([In, Out] byte[] block, int edflag);
3098 public static void encrypt (byte[] block, bool decode)
3100 if (block.Length < 64)
3101 throw new ArgumentOutOfRangeException ("block", "Must refer to at least 64 bytes");
3102 lock (encrypt_lock) {
3103 sys_encrypt (block, decode ? 1 : 0);
3108 // void swab(const void *from, void *to, ssize_t n);
3109 [DllImport (MPH, SetLastError=true,
3110 EntryPoint="Mono_Posix_Syscall_swab")]
3111 public static extern void swab (IntPtr from, IntPtr to, long n);
3113 public static unsafe void swab (void* from, void* to, long n)
3115 swab ((IntPtr) from, (IntPtr) to, n);
3120 #region <utime.h> Declarations
3122 // <utime.h> -- COMPLETE
3125 [DllImport (MPH, SetLastError=true,
3126 EntryPoint="Mono_Posix_Syscall_utime")]
3127 private static extern int sys_utime (string filename, ref Utimbuf buf, int use_buf);
3129 public static int utime (string filename, ref Utimbuf buf)
3131 return sys_utime (filename, ref buf, 1);
3134 public static int utime (string filename)
3136 Utimbuf buf = new Utimbuf ();
3137 return sys_utime (filename, ref buf, 0);