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 {
109 LOG_AUTHPRIV = 10 << 3,
111 LOG_LOCAL0 = 16 << 3,
112 LOG_LOCAL1 = 17 << 3,
113 LOG_LOCAL2 = 18 << 3,
114 LOG_LOCAL3 = 19 << 3,
115 LOG_LOCAL4 = 20 << 3,
116 LOG_LOCAL5 = 21 << 3,
117 LOG_LOCAL6 = 22 << 3,
118 LOG_LOCAL7 = 23 << 3,
122 public enum SyslogLevel {
123 LOG_EMERG = 0, // system is unusable
124 LOG_ALERT = 1, // action must be taken immediately
125 LOG_CRIT = 2, // critical conditions
126 LOG_ERR = 3, // warning conditions
127 LOG_WARNING = 4, // warning conditions
128 LOG_NOTICE = 5, // normal but significant condition
129 LOG_INFO = 6, // informational
130 LOG_DEBUG = 7 // debug-level messages
134 public enum OpenFlags : int {
138 O_RDONLY = 0x00000000,
139 O_WRONLY = 0x00000001,
143 // Or-ed with zero or more of these
145 O_CREAT = 0x00000040,
147 O_NOCTTY = 0x00000100,
148 O_TRUNC = 0x00000200,
149 O_APPEND = 0x00000400,
150 O_NONBLOCK = 0x00000800,
154 // These are non-Posix. Using them will result in errors/exceptions on
155 // non-supported platforms.
157 // (For example, "C-wrapped" system calls -- calls with implementation in
158 // MonoPosixHelper -- will return -1 with errno=EINVAL. C#-wrapped system
159 // calls will generate an exception in UnixConvert, as the value can't be
160 // converted on the target platform.)
163 O_NOFOLLOW = 0x00020000,
164 O_DIRECTORY = 0x00010000,
165 O_DIRECT = 0x00004000,
166 O_ASYNC = 0x00002000,
167 O_LARGEFILE = 0x00008000
172 public enum FilePermissions : uint {
173 S_ISUID = 0x0800, // Set user ID on execution
174 S_ISGID = 0x0400, // Set gorup ID on execution
175 S_ISVTX = 0x0200, // Save swapped text after use (sticky).
176 S_IRUSR = 0x0100, // Read by owner
177 S_IWUSR = 0x0080, // Write by owner
178 S_IXUSR = 0x0040, // Execute by owner
179 S_IRGRP = 0x0020, // Read by group
180 S_IWGRP = 0x0010, // Write by group
181 S_IXGRP = 0x0008, // Execute by group
182 S_IROTH = 0x0004, // Read by other
183 S_IWOTH = 0x0002, // Write by other
184 S_IXOTH = 0x0001, // Execute by other
186 S_IRWXG = (S_IRGRP | S_IWGRP | S_IXGRP),
187 S_IRWXU = (S_IRUSR | S_IWUSR | S_IXUSR),
188 S_IRWXO = (S_IROTH | S_IWOTH | S_IXOTH),
189 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
190 ALLPERMS = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
191 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
194 // Why these are held in "mode_t" is beyond me...
195 S_IFMT = 0xF000, // Bits which determine file type
196 S_IFDIR = 0x4000, // Directory
197 S_IFCHR = 0x2000, // Character device
198 S_IFBLK = 0x6000, // Block device
199 S_IFREG = 0x8000, // Regular file
200 S_IFIFO = 0x1000, // FIFO
201 S_IFLNK = 0xA000, // Symbolic link
202 S_IFSOCK = 0xC000, // Socket
206 public enum FcntlCommand : int {
207 // Form /usr/include/bits/fcntl.h
208 F_DUPFD = 0, // Duplicate file descriptor.
209 F_GETFD = 1, // Get file descriptor flags.
210 F_SETFD = 2, // Set file descriptor flags.
211 F_GETFL = 3, // Get file status flags.
212 F_SETFL = 4, // Set file status flags.
213 F_GETLK = 12, // Get record locking info. [64]
214 F_SETLK = 13, // Set record locking info (non-blocking). [64]
215 F_SETLKW = 14, // Set record locking info (blocking). [64]
216 F_SETOWN = 8, // Set owner of socket (receiver of SIGIO).
217 F_GETOWN = 9, // Get owner of socket (receiver of SIGIO).
218 F_SETSIG = 10, // Set number of signal to be sent.
219 F_GETSIG = 11, // Get number of signal to be sent.
220 F_SETLEASE = 1024, // Set a lease.
221 F_GETLEASE = 1025, // Enquire what lease is active.
222 F_NOTIFY = 1026, // Required notifications on a directory
226 public enum LockType : short {
227 F_RDLCK = 0, // Read lock.
228 F_WRLCK = 1, // Write lock.
229 F_UNLCK = 2, // Remove lock.
233 public enum SeekFlags : short {
234 // values liberally copied from /usr/include/unistd.h
235 SEEK_SET = 0, // Seek from beginning of file.
236 SEEK_CUR = 1, // Seek from current position.
237 SEEK_END = 2, // Seek from end of file.
239 L_SET = SEEK_SET, // BSD alias for SEEK_SET
240 L_INCR = SEEK_CUR, // BSD alias for SEEK_CUR
241 L_XTND = SEEK_END, // BSD alias for SEEK_END
245 public enum DirectoryNotifyFlags : int {
246 // from /usr/include/bits/fcntl.h
247 DN_ACCESS = 0x00000001, // File accessed.
248 DN_MODIFY = 0x00000002, // File modified.
249 DN_CREATE = 0x00000004, // File created.
250 DN_DELETE = 0x00000008, // File removed.
251 DN_RENAME = 0x00000010, // File renamed.
252 DN_ATTRIB = 0x00000020, // File changed attributes.
253 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
257 public enum PosixFadviseAdvice : int {
258 POSIX_FADV_NORMAL = 0, // No further special treatment.
259 POSIX_FADV_RANDOM = 1, // Expect random page references.
260 POSIX_FADV_SEQUENTIAL = 2, // Expect sequential page references.
261 POSIX_FADV_WILLNEED = 3, // Will need these pages.
262 POSIX_FADV_DONTNEED = 4, // Don't need these pages.
263 POSIX_FADV_NOREUSE = 5, // Data will be accessed once.
267 public enum PosixMadviseAdvice : int {
268 POSIX_MADV_NORMAL = 0, // No further special treatment.
269 POSIX_MADV_RANDOM = 1, // Expect random page references.
270 POSIX_MADV_SEQUENTIAL = 2, // Expect sequential page references.
271 POSIX_MADV_WILLNEED = 3, // Will need these pages.
272 POSIX_MADV_DONTNEED = 4, // Don't need these pages.
276 public enum Signum : int {
277 SIGHUP = 1, // Hangup (POSIX).
278 SIGINT = 2, // Interrupt (ANSI).
279 SIGQUIT = 3, // Quit (POSIX).
280 SIGILL = 4, // Illegal instruction (ANSI).
281 SIGTRAP = 5, // Trace trap (POSIX).
282 SIGABRT = 6, // Abort (ANSI).
283 SIGIOT = 6, // IOT trap (4.2 BSD).
284 SIGBUS = 7, // BUS error (4.2 BSD).
285 SIGFPE = 8, // Floating-point exception (ANSI).
286 SIGKILL = 9, // Kill, unblockable (POSIX).
287 SIGUSR1 = 10, // User-defined signal 1 (POSIX).
288 SIGSEGV = 11, // Segmentation violation (ANSI).
289 SIGUSR2 = 12, // User-defined signal 2 (POSIX).
290 SIGPIPE = 13, // Broken pipe (POSIX).
291 SIGALRM = 14, // Alarm clock (POSIX).
292 SIGTERM = 15, // Termination (ANSI).
293 SIGSTKFLT = 16, // Stack fault.
294 SIGCLD = SIGCHLD, // Same as SIGCHLD (System V).
295 SIGCHLD = 17, // Child status has changed (POSIX).
296 SIGCONT = 18, // Continue (POSIX).
297 SIGSTOP = 19, // Stop, unblockable (POSIX).
298 SIGTSTP = 20, // Keyboard stop (POSIX).
299 SIGTTIN = 21, // Background read from tty (POSIX).
300 SIGTTOU = 22, // Background write to tty (POSIX).
301 SIGURG = 23, // Urgent condition on socket (4.2 BSD).
302 SIGXCPU = 24, // CPU limit exceeded (4.2 BSD).
303 SIGXFSZ = 25, // File size limit exceeded (4.2 BSD).
304 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
305 SIGPROF = 27, // Profiling alarm clock (4.2 BSD).
306 SIGWINCH = 28, // Window size change (4.3 BSD, Sun).
307 SIGPOLL = SIGIO, // Pollable event occurred (System V).
308 SIGIO = 29, // I/O now possible (4.2 BSD).
309 SIGPWR = 30, // Power failure restart (System V).
310 SIGSYS = 31, // Bad system call.
315 public enum WaitOptions : int {
316 WNOHANG = 1, // Don't block waiting
317 WUNTRACED = 2, // Report status of stopped children
321 public enum AccessMode : int {
329 public enum PathConf : int {
336 _PC_CHOWN_RESTRICTED,
344 _PC_REC_INCR_XFER_SIZE,
345 _PC_REC_MAX_XFER_SIZE,
346 _PC_REC_MIN_XFER_SIZE,
354 public enum SysConf : int {
364 _SC_REALTIME_SIGNALS,
365 _SC_PRIORITY_SCHEDULING,
374 _SC_MEMORY_PROTECTION,
377 _SC_SHARED_MEMORY_OBJECTS,
380 _SC_AIO_PRIO_DELTA_MAX,
391 /* Values for the argument to `sysconf'
392 corresponding to _POSIX2_* symbols. */
397 _SC_COLL_WEIGHTS_MAX,
402 _SC_CHARCLASS_NAME_MAX,
418 _SC_IOV_MAX = _SC_UIO_MAXIOV,
419 _SC_PII_INTERNET_STREAM,
420 _SC_PII_INTERNET_DGRAM,
425 /* Values according to POSIX 1003.1c (POSIX threads). */
427 _SC_THREAD_SAFE_FUNCTIONS,
428 _SC_GETGR_R_SIZE_MAX,
429 _SC_GETPW_R_SIZE_MAX,
432 _SC_THREAD_DESTRUCTOR_ITERATIONS,
434 _SC_THREAD_STACK_MIN,
435 _SC_THREAD_THREADS_MAX,
436 _SC_THREAD_ATTR_STACKADDR,
437 _SC_THREAD_ATTR_STACKSIZE,
438 _SC_THREAD_PRIORITY_SCHEDULING,
439 _SC_THREAD_PRIO_INHERIT,
440 _SC_THREAD_PRIO_PROTECT,
441 _SC_THREAD_PROCESS_SHARED,
442 _SC_NPROCESSORS_CONF,
443 _SC_NPROCESSORS_ONLN,
449 _SC_XOPEN_XCU_VERSION,
484 _SC_XBS5_ILP32_OFF32,
485 _SC_XBS5_ILP32_OFFBIG,
487 _SC_XBS5_LPBIG_OFFBIG,
490 _SC_XOPEN_REALTIME_THREADS,
495 _SC_C_LANG_SUPPORT_R,
501 _SC_DEVICE_SPECIFIC_R,
512 _SC_READER_WRITER_LOCKS,
520 _SC_THREAD_SPORADIC_SERVER,
522 _SC_SYSTEM_DATABASE_R,
524 _SC_TYPED_MEMORY_OBJECTS,
528 _SC_2_PBS_ACCOUNTING,
534 _SC_2_PBS_CHECKPOINT,
541 _SC_TRACE_EVENT_FILTER,
544 _SC_LEVEL1_ICACHE_SIZE,
545 _SC_LEVEL1_ICACHE_ASSOC,
546 _SC_LEVEL1_ICACHE_LINESIZE,
547 _SC_LEVEL1_DCACHE_SIZE,
548 _SC_LEVEL1_DCACHE_ASSOC,
549 _SC_LEVEL1_DCACHE_LINESIZE,
550 _SC_LEVEL2_CACHE_SIZE,
551 _SC_LEVEL2_CACHE_ASSOC,
552 _SC_LEVEL2_CACHE_LINESIZE,
553 _SC_LEVEL3_CACHE_SIZE,
554 _SC_LEVEL3_CACHE_ASSOC,
555 _SC_LEVEL3_CACHE_LINESIZE,
556 _SC_LEVEL4_CACHE_SIZE,
557 _SC_LEVEL4_CACHE_ASSOC,
558 _SC_LEVEL4_CACHE_LINESIZE
562 public enum ConfStr : int {
563 _CS_PATH, /* The default search path. */
564 _CS_V6_WIDTH_RESTRICTED_ENVS,
565 _CS_GNU_LIBC_VERSION,
566 _CS_GNU_LIBPTHREAD_VERSION,
567 _CS_LFS_CFLAGS = 1000,
575 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
576 _CS_XBS5_ILP32_OFF32_LDFLAGS,
577 _CS_XBS5_ILP32_OFF32_LIBS,
578 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
579 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
580 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
581 _CS_XBS5_ILP32_OFFBIG_LIBS,
582 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
583 _CS_XBS5_LP64_OFF64_CFLAGS,
584 _CS_XBS5_LP64_OFF64_LDFLAGS,
585 _CS_XBS5_LP64_OFF64_LIBS,
586 _CS_XBS5_LP64_OFF64_LINTFLAGS,
587 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
588 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
589 _CS_XBS5_LPBIG_OFFBIG_LIBS,
590 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
591 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
592 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
593 _CS_POSIX_V6_ILP32_OFF32_LIBS,
594 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
595 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
596 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
597 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
598 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
599 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
600 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
601 _CS_POSIX_V6_LP64_OFF64_LIBS,
602 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
603 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
604 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
605 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
606 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
610 public enum LockfCommand : int {
611 F_ULOCK = 0, // Unlock a previously locked region.
612 F_LOCK = 1, // Lock a region for exclusive use.
613 F_TLOCK = 2, // Test and lock a region for exclusive use.
614 F_TEST = 3, // Test a region for other process locks.
618 public enum PollEvents : short {
619 POLLIN = 0x0001, // There is data to read
620 POLLPRI = 0x0002, // There is urgent data to read
621 POLLOUT = 0x0004, // Writing now will not block
622 POLLERR = 0x0008, // Error condition
623 POLLHUP = 0x0010, // Hung up
624 POLLNVAL = 0x0020, // Invalid request; fd not open
625 // XPG4.2 definitions (via _XOPEN_SOURCE)
626 POLLRDNORM = 0x0040, // Normal data bay be read
627 POLLRDBAND = 0x0080, // Priority data may be read
628 POLLWRNORM = 0x0100, // Writing now will not block
629 POLLWRBAND = 0x0200, // Priority data may be written
633 public enum XattrFlags : int {
640 public enum MountFlags : ulong {
641 ST_RDONLY = 1, // Mount read-only
642 ST_NOSUID = 2, // Ignore suid and sgid bits
643 ST_NODEV = 4, // Disallow access to device special files
644 ST_SYNCHRONOUS = 16, // Writes are synced at once
645 ST_MANDLOCK = 64, // Allow mandatory locks on an FS
646 ST_WRITE = 128, // Write on file/directory/symlink
647 ST_APPEND = 256, // Append-only file
648 ST_IMMUTABLE = 512, // Immutable file
649 ST_NOATIME = 1024, // Do not update access times
650 ST_NODIRATIME = 2048, // Do not update directory access times
654 public enum MmapFlags : int {
655 MAP_SHARED = 0x01, // Share changes.
656 MAP_PRIVATE = 0x02, // Changes are private.
657 MAP_TYPE = 0x0f, // Mask for type of mapping.
658 MAP_FIXED = 0x10, // Interpret addr exactly.
660 MAP_ANONYMOUS = 0x20, // Don't use a file.
661 MAP_ANON = MAP_ANONYMOUS,
663 // These are Linux-specific.
664 MAP_GROWSDOWN = 0x00100, // Stack-like segment.
665 MAP_DENYWRITE = 0x00800, // ETXTBSY
666 MAP_EXECUTABLE = 0x01000, // Mark it as an executable.
667 MAP_LOCKED = 0x02000, // Lock the mapping.
668 MAP_NORESERVE = 0x04000, // Don't check for reservations.
669 MAP_POPULATE = 0x08000, // Populate (prefault) pagetables.
670 MAP_NONBLOCK = 0x10000, // Do not block on IO.
674 public enum MmapProt : int {
675 PROT_READ = 0x1, // Page can be read.
676 PROT_WRITE = 0x2, // Page can be written.
677 PROT_EXEC = 0x4, // Page can be executed.
678 PROT_NONE = 0x0, // Page can not be accessed.
679 PROT_GROWSDOWN = 0x01000000, // Extend change to start of
680 // growsdown vma (mprotect only).
681 PROT_GROWSUP = 0x02000000, // Extend change to start of
682 // growsup vma (mprotect only).
686 public enum MsyncFlags : int {
687 MS_ASYNC = 0x1, // Sync memory asynchronously.
688 MS_SYNC = 0x4, // Synchronous memory sync.
689 MS_INVALIDATE = 0x2, // Invalidate the caches.
693 public enum MlockallFlags : int {
694 MCL_CURRENT = 0x1, // Lock all currently mapped pages.
695 MCL_FUTURE = 0x2, // Lock all additions to address
699 public enum MremapFlags : ulong {
700 MREMAP_MAYMOVE = 0x1,
707 public struct Flock {
708 public LockType l_type; // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
709 public SeekFlags l_whence; // How to interpret l_start
710 public /* off_t */ long l_start; // Starting offset for lock
711 public /* off_t */ long l_len; // Number of bytes to lock
712 public /* pid_t */ int l_pid; // PID of process blocking our lock (F_GETLK only)
715 [StructLayout(LayoutKind.Sequential)]
716 public struct Pollfd {
718 public PollEvents events;
719 public PollEvents revents;
723 public /* dev_t */ ulong st_dev; // device
724 public /* ino_t */ ulong st_ino; // inode
725 public FilePermissions st_mode; // protection
726 private uint _padding_; // padding for structure alignment
727 public /* nlink_t */ ulong st_nlink; // number of hard links
728 public /* uid_t */ uint st_uid; // user ID of owner
729 public /* gid_t */ uint st_gid; // group ID of owner
730 public /* dev_t */ ulong st_rdev; // device type (if inode device)
731 public /* off_t */ long st_size; // total size, in bytes
732 public /* blksize_t */ long st_blksize; // blocksize for filesystem I/O
733 public /* blkcnt_t */ long st_blocks; // number of blocks allocated
734 public /* time_t */ long st_atime; // time of last access
735 public /* time_t */ long st_mtime; // time of last modification
736 public /* time_t */ long st_ctime; // time of last status change
739 public struct Statvfs {
740 public ulong f_bsize; // file system block size
741 public ulong f_frsize; // fragment size
742 public /* fsblkcnt_t */ ulong f_blocks; // size of fs in f_frsize units
743 public /* fsblkcnt_t */ ulong f_bfree; // # free blocks
744 public /* fsblkcnt_t */ ulong f_bavail; // # free blocks for non-root
745 public /* fsfilcnt_t */ ulong f_files; // # inodes
746 public /* fsfilcnt_t */ ulong f_ffree; // # free inodes
747 public /* fsfilcnt_t */ ulong f_favail; // # free inodes for non-root
748 public ulong f_fsid; // file system id
749 public MountFlags f_flag; // mount flags
750 public ulong f_namemax; // maximum filename length
753 public struct Timeval {
754 public /* time_t */ long tv_sec; // seconds
755 public /* suseconds_t */ long tv_usec; // microseconds
758 public struct Timezone {
759 public int tz_minuteswest; // minutes W of Greenwich
760 private int tz_dsttime; // type of dst correction (OBSOLETE)
763 public struct Utimbuf {
764 public /* time_t */ long actime; // access time
765 public /* time_t */ long modtime; // modification time
772 public sealed class Dirent
774 public /* ino_t */ ulong d_ino;
775 public /* off_t */ long d_off;
776 public ushort d_reclen;
778 public string d_name;
780 public override int GetHashCode ()
782 return d_ino.GetHashCode () ^ d_off.GetHashCode () ^
783 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
784 d_name.GetHashCode ();
787 public override bool Equals (object obj)
789 if (obj == null || GetType() != obj.GetType())
791 Dirent d = (Dirent) obj;
792 return d.d_ino == d_ino && d.d_off == d_off &&
793 d.d_reclen == d_reclen && d.d_type == d_type &&
797 public override string ToString ()
802 public static bool operator== (Dirent lhs, Dirent rhs)
804 return Object.Equals (lhs, rhs);
807 public static bool operator!= (Dirent lhs, Dirent rhs)
809 return !Object.Equals (lhs, rhs);
813 public sealed class Fstab
815 public string fs_spec;
816 public string fs_file;
817 public string fs_vfstype;
818 public string fs_mntops;
819 public string fs_type;
821 public int fs_passno;
823 public override int GetHashCode ()
825 return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
826 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
827 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
830 public override bool Equals (object obj)
832 if (obj == null || GetType() != obj.GetType())
834 Fstab f = (Fstab) obj;
835 return f.fs_spec == fs_spec && f.fs_file == fs_file &&
836 f.fs_vfstype == fs_vfstype && f.fs_mntops == fs_mntops &&
837 f.fs_type == fs_type && f.fs_freq == fs_freq &&
838 f.fs_passno == fs_passno;
841 public override string ToString ()
846 public static bool operator== (Fstab lhs, Fstab rhs)
848 return Object.Equals (lhs, rhs);
851 public static bool operator!= (Fstab lhs, Fstab rhs)
853 return !Object.Equals (lhs, rhs);
857 public sealed class Group
859 public string gr_name;
860 public string gr_passwd;
861 public /* gid_t */ uint gr_gid;
862 public string[] gr_mem;
864 public override int GetHashCode ()
867 for (int i = 0; i < gr_mem.Length; ++i)
868 memhc ^= gr_mem[i].GetHashCode ();
870 return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^
871 gr_gid.GetHashCode () ^ memhc;
874 public override bool Equals (object obj)
876 if (obj == null || GetType() != obj.GetType())
878 Group g = (Group) obj;
879 if (g.gr_gid != gr_gid)
881 if (g.gr_gid == gr_gid && g.gr_name == gr_name &&
882 g.gr_passwd == gr_passwd) {
883 if (g.gr_mem == gr_mem)
885 if (g.gr_mem == null || gr_mem == null)
887 if (g.gr_mem.Length != gr_mem.Length)
889 for (int i = 0; i < gr_mem.Length; ++i)
890 if (gr_mem[i] != g.gr_mem[i])
897 // Generate string in /etc/group format
898 public override string ToString ()
900 StringBuilder sb = new StringBuilder ();
901 sb.AppendFormat ("{0}:{1}:{2}:", gr_name, gr_passwd, gr_gid);
902 GetMembers (sb, gr_mem);
903 return sb.ToString ();
906 private static void GetMembers (StringBuilder sb, string[] members)
908 if (members.Length > 0)
909 sb.Append (members[0]);
910 for (int i = 1; i < members.Length; ++i) {
912 sb.Append (members[i]);
916 public static bool operator== (Group lhs, Group rhs)
918 return Object.Equals (lhs, rhs);
921 public static bool operator!= (Group lhs, Group rhs)
923 return !Object.Equals (lhs, rhs);
927 public sealed class Passwd
929 public string pw_name;
930 public string pw_passwd;
931 public /* uid_t */ uint pw_uid;
932 public /* gid_t */ uint pw_gid;
933 public string pw_gecos;
934 public string pw_dir;
935 public string pw_shell;
937 public override int GetHashCode ()
939 return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^
940 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
941 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
942 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
945 public override bool Equals (object obj)
947 if (obj == null || GetType() != obj.GetType())
949 Passwd p = (Passwd) obj;
950 return p.pw_uid == pw_uid && p.pw_gid == pw_gid && p.pw_name == pw_name &&
951 p.pw_passwd == pw_passwd && p.pw_gecos == pw_gecos &&
952 p.pw_dir == pw_dir && p.pw_shell == pw_shell;
955 // Generate string in /etc/passwd format
956 public override string ToString ()
958 return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
959 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
962 public static bool operator== (Passwd lhs, Passwd rhs)
964 return Object.Equals (lhs, rhs);
967 public static bool operator!= (Passwd lhs, Passwd rhs)
969 return !Object.Equals (lhs, rhs);
974 // Convention: Functions *not* part of the standard C library AND part of
975 // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
977 // For example, the man page should be similar to:
979 // CONFORMING TO (or CONFORMS TO)
980 // XPG2, SUSv2, POSIX, etc.
982 // BSD- and GNU-specific exports can also be placed here.
984 // Non-POSIX/XPG/etc. functions can also be placed here if:
985 // (a) They'd be likely to be covered in a Steven's-like book
986 // (b) The functions would be present in libc.so (or equivalent).
988 // If a function has its own library, that's a STRONG indicator that the
989 // function should get a different binding, probably in its own assembly,
990 // so that package management can work sanely. (That is, we'd like to avoid
991 // scenarios where FooLib.dll is installed, but it requires libFooLib.so to
992 // run, and libFooLib.so doesn't exist. That would be confusing.)
994 // The only methods in here should be:
995 // (1) low-level functions
996 // (2) "Trivial" function overloads. For example, if the parameters to a
997 // function are related (e.g. getgroups(2))
998 // (3) The return type SHOULD NOT be changed. If you want to provide a
999 // convenience function with a nicer return type, place it into one of
1000 // the Unix* wrapper classes, and give it a .NET-styled name.
1001 // (4) Exceptions SHOULD NOT be thrown. EXCEPTIONS:
1002 // - If you're wrapping *broken* methods which make assumptions about
1003 // input data, such as that an argument refers to N bytes of data.
1004 // This is currently limited to cuserid(3) and encrypt(3).
1005 // - If you call functions which themselves generate exceptions.
1006 // This is the case for using UnixConvert, which will throw an
1007 // exception if an invalid/unsupported value is used.
1009 public sealed class Syscall : Stdlib
1011 new internal const string LIBC = "libc";
1012 private const string CRYPT = "crypt";
1014 private Syscall () {}
1020 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3),
1021 // aio_return(3), aio_suspend(3), aio_write(3)
1023 // Then update UnixStream.BeginRead to use the aio* functions.
1026 #region <attr/xattr.h> Declarations
1028 // <attr/xattr.h> -- COMPLETE
1032 // int setxattr (const char *path, const char *name,
1033 // const void *value, size_t size, int flags);
1034 [DllImport (MPH, SetLastError=true,
1035 EntryPoint="Mono_Posix_Syscall_setxattr")]
1036 public static extern int setxattr (string path, string name, byte[] value, ulong size, XattrFlags flags);
1038 public static int setxattr (string path, string name, byte [] value, XattrFlags flags)
1040 return setxattr (path, name, value, (ulong) value.Length, flags);
1044 // int lsetxattr (const char *path, const char *name,
1045 // const void *value, size_t size, int flags);
1046 [DllImport (MPH, SetLastError=true,
1047 EntryPoint="Mono_Posix_Syscall_lsetxattr")]
1048 public static extern int lsetxattr (string path, string name, byte[] value, ulong size, XattrFlags flags);
1050 public static int lsetxattr (string path, string name, byte [] value, XattrFlags flags)
1052 return lsetxattr (path, name, value, (ulong) value.Length, flags);
1056 // int fsetxattr (int fd, const char *name,
1057 // const void *value, size_t size, int flags);
1058 [DllImport (MPH, SetLastError=true,
1059 EntryPoint="Mono_Posix_Syscall_fsetxattr")]
1060 public static extern int fsetxattr (int fd, string name, byte[] value, ulong size, XattrFlags flags);
1062 public static int fsetxattr (int fd, string name, byte [] value, XattrFlags flags)
1064 return fsetxattr (fd, name, value, (ulong) value.Length, flags);
1068 // ssize_t getxattr (const char *path, const char *name,
1069 // void *value, size_t size);
1070 [DllImport (MPH, SetLastError=true,
1071 EntryPoint="Mono_Posix_Syscall_getxattr")]
1072 public static extern long getxattr (string path, string name, byte[] value, ulong size);
1074 public static long getxattr (string path, string name, byte [] value)
1076 return getxattr (path, name, value, (ulong) value.Length);
1079 public static long getxattr (string path, string name, out byte [] value)
1082 long size = getxattr (path, name, value, 0);
1086 value = new byte [size];
1087 return getxattr (path, name, value, (ulong) size);
1091 // ssize_t lgetxattr (const char *path, const char *name,
1092 // void *value, size_t size);
1093 [DllImport (MPH, SetLastError=true,
1094 EntryPoint="Mono_Posix_Syscall_lgetxattr")]
1095 public static extern long lgetxattr (string path, string name, byte[] value, ulong size);
1097 public static long lgetxattr (string path, string name, byte [] value)
1099 return lgetxattr (path, name, value, (ulong) value.Length);
1102 public static long lgetxattr (string path, string name, out byte [] value)
1105 long size = lgetxattr (path, name, value, 0);
1109 value = new byte [size];
1110 return lgetxattr (path, name, value, (ulong) size);
1114 // ssize_t fgetxattr (int fd, const char *name, void *value, size_t size);
1115 [DllImport (MPH, SetLastError=true,
1116 EntryPoint="Mono_Posix_Syscall_fgetxattr")]
1117 public static extern long fgetxattr (int fd, string name, byte[] value, ulong size);
1119 public static long fgetxattr (int fd, string name, byte [] value)
1121 return fgetxattr (fd, name, value, (ulong) value.Length);
1124 public static long fgetxattr (int fd, string name, out byte [] value)
1127 long size = fgetxattr (fd, name, value, 0);
1131 value = new byte [size];
1132 return fgetxattr (fd, name, value, (ulong) size);
1136 // ssize_t listxattr (const char *path, char *list, size_t size);
1137 [DllImport (MPH, SetLastError=true,
1138 EntryPoint="Mono_Posix_Syscall_listxattr")]
1139 public static extern long listxattr (string path, byte[] list, ulong size);
1141 // Slight modification: returns 0 on success, negative on error
1142 public static long listxattr (string path, Encoding encoding, out string [] values)
1145 long size = listxattr (path, null, 0);
1147 values = new string [0];
1151 byte[] list = new byte [size];
1152 long ret = listxattr (path, list, (ulong) size);
1156 string [] output = encoding.GetString (list).Split((char) 0);
1157 values = new string [output.Length - 1];
1158 Array.Copy (output, 0, values, 0, output.Length - 1);
1163 // ssize_t llistxattr (const char *path, char *list, size_t size);
1164 [DllImport (MPH, SetLastError=true,
1165 EntryPoint="Mono_Posix_Syscall_llistxattr")]
1166 public static extern long llistxattr (string path, byte[] list, ulong size);
1168 // Slight modification: returns 0 on success, negative on error
1169 public static long llistxattr (string path, Encoding encoding, out string [] values)
1172 long size = llistxattr (path, null, 0);
1174 values = new string [0];
1178 byte[] list = new byte [size];
1179 long ret = llistxattr (path, list, (ulong) size);
1183 string [] output = encoding.GetString (list).Split((char) 0);
1184 values = new string [output.Length - 1];
1185 Array.Copy (output, 0, values, 0, output.Length - 1);
1190 // ssize_t flistxattr (int fd, char *list, size_t size);
1191 [DllImport (MPH, SetLastError=true,
1192 EntryPoint="Mono_Posix_Syscall_flistxattr")]
1193 public static extern long flistxattr (int fd, byte[] list, ulong size);
1195 // Slight modification: returns 0 on success, negative on error
1196 public static long flistxattr (int fd, Encoding encoding, out string [] values)
1199 long size = flistxattr (fd, null, 0);
1201 values = new string [0];
1205 byte[] list = new byte [size];
1206 long ret = flistxattr (fd, list, (ulong) size);
1210 string [] output = encoding.GetString (list).Split((char) 0);
1211 values = new string [output.Length - 1];
1212 Array.Copy (output, 0, values, 0, output.Length - 1);
1216 [DllImport (LIBC, SetLastError=true)]
1217 public static extern int removexattr (string path, string name);
1219 [DllImport (LIBC, SetLastError=true)]
1220 public static extern int lremovexattr (string path, string name);
1222 [DllImport (LIBC, SetLastError=true)]
1223 public static extern int fremovexattr (int fd, string name);
1226 #region <dirent.h> Declarations
1230 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1232 [DllImport (LIBC, SetLastError=true)]
1233 public static extern IntPtr opendir (string name);
1235 [DllImport (LIBC, SetLastError=true)]
1236 public static extern int closedir (IntPtr dir);
1239 // void seekdir (DIR *dir, off_t offset);
1240 // Slight modification. Returns -1 on error, 0 on success.
1241 [DllImport (MPH, SetLastError=true,
1242 EntryPoint="Mono_Posix_Syscall_seekdir")]
1243 public static extern int seekdir (IntPtr dir, long offset);
1246 // off_t telldir(DIR *dir);
1247 [DllImport (MPH, SetLastError=true,
1248 EntryPoint="Mono_Posix_Syscall_telldir")]
1249 public static extern long telldir (IntPtr dir);
1251 [DllImport (LIBC, SetLastError=true)]
1252 public static extern void rewinddir (IntPtr dir);
1254 private struct _Dirent {
1255 public /* ino_t */ ulong d_ino;
1256 public /* off_t */ long d_off;
1257 public ushort d_reclen;
1259 public IntPtr d_name;
1262 private static void CopyDirent (Dirent to, ref _Dirent from)
1265 to.d_ino = from.d_ino;
1266 to.d_off = from.d_off;
1267 to.d_reclen = from.d_reclen;
1268 to.d_type = from.d_type;
1269 to.d_name = UnixMarshal.PtrToString (from.d_name);
1272 Stdlib.free (from.d_name);
1273 from.d_name = IntPtr.Zero;
1277 [DllImport (MPH, SetLastError=true,
1278 EntryPoint="Mono_Posix_Syscall_readdir")]
1279 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
1281 public static Dirent readdir (IntPtr dir)
1284 int r = sys_readdir (dir, out dentry);
1287 Dirent d = new Dirent ();
1288 CopyDirent (d, ref dentry);
1292 [DllImport (MPH, SetLastError=true,
1293 EntryPoint="Mono_Posix_Syscall_readdir_r")]
1294 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
1296 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
1302 entry.d_name = null;
1305 int r = sys_readdir_r (dirp, out _d, out result);
1307 if (r == 0 && result != IntPtr.Zero) {
1308 CopyDirent (entry, ref _d);
1314 [DllImport (LIBC, SetLastError=true)]
1315 public static extern int dirfd (IntPtr dir);
1318 #region <fcntl.h> Declarations
1320 // <fcntl.h> -- COMPLETE
1323 [DllImport (MPH, SetLastError=true,
1324 EntryPoint="Mono_Posix_Syscall_fcntl")]
1325 public static extern int fcntl (int fd, FcntlCommand cmd);
1327 [DllImport (MPH, SetLastError=true,
1328 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
1329 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
1331 public static int fcntl (int fd, FcntlCommand cmd, DirectoryNotifyFlags arg)
1333 if (cmd != FcntlCommand.F_NOTIFY) {
1334 SetLastError (Error.EINVAL);
1337 long _arg = UnixConvert.FromDirectoryNotifyFlags (arg);
1338 return fcntl (fd, FcntlCommand.F_NOTIFY, _arg);
1341 [DllImport (MPH, SetLastError=true,
1342 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
1343 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
1345 [DllImport (MPH, SetLastError=true,
1346 EntryPoint="Mono_Posix_Syscall_open")]
1347 public static extern int open (string pathname, OpenFlags flags);
1350 // int open(const char *pathname, int flags, mode_t mode);
1351 [DllImport (MPH, SetLastError=true,
1352 EntryPoint="Mono_Posix_Syscall_open_mode")]
1353 public static extern int open (string pathname, OpenFlags flags, FilePermissions mode);
1356 // int creat(const char *pathname, mode_t mode);
1357 [DllImport (MPH, SetLastError=true,
1358 EntryPoint="Mono_Posix_Syscall_creat")]
1359 public static extern int creat (string pathname, FilePermissions mode);
1362 // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
1363 [DllImport (MPH, SetLastError=true,
1364 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
1365 public static extern int posix_fadvise (int fd, long offset,
1366 long len, PosixFadviseAdvice advice);
1368 // posix_fallocate(P)
1369 // int posix_fallocate(int fd, off_t offset, size_t len);
1370 [DllImport (MPH, SetLastError=true,
1371 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
1372 public static extern int posix_fallocate (int fd, long offset, ulong len);
1375 #region <fstab.h> Declarations
1377 // <fstab.h> -- COMPLETE
1379 private struct _Fstab {
1380 public IntPtr fs_spec;
1381 public IntPtr fs_file;
1382 public IntPtr fs_vfstype;
1383 public IntPtr fs_mntops;
1384 public IntPtr fs_type;
1386 public int fs_passno;
1387 public IntPtr _fs_buf_;
1390 private static void CopyFstab (Fstab to, ref _Fstab from)
1393 to.fs_spec = UnixMarshal.PtrToString (from.fs_spec);
1394 to.fs_file = UnixMarshal.PtrToString (from.fs_file);
1395 to.fs_vfstype = UnixMarshal.PtrToString (from.fs_vfstype);
1396 to.fs_mntops = UnixMarshal.PtrToString (from.fs_mntops);
1397 to.fs_type = UnixMarshal.PtrToString (from.fs_type);
1398 to.fs_freq = from.fs_freq;
1399 to.fs_passno = from.fs_passno;
1402 Stdlib.free (from._fs_buf_);
1403 from._fs_buf_ = IntPtr.Zero;
1407 internal static object fstab_lock = new object ();
1409 [DllImport (MPH, SetLastError=true,
1410 EntryPoint="Mono_Posix_Syscall_endfsent")]
1411 private static extern void sys_endfsent ();
1413 public static void endfsent ()
1420 [DllImport (MPH, SetLastError=true,
1421 EntryPoint="Mono_Posix_Syscall_getfsent")]
1422 private static extern int sys_getfsent (out _Fstab fs);
1424 public static Fstab getfsent ()
1429 r = sys_getfsent (out fsbuf);
1433 Fstab fs = new Fstab ();
1434 CopyFstab (fs, ref fsbuf);
1438 [DllImport (MPH, SetLastError=true,
1439 EntryPoint="Mono_Posix_Syscall_getfsfile")]
1440 private static extern int sys_getfsfile (string mount_point, out _Fstab fs);
1442 public static Fstab getfsfile (string mount_point)
1447 r = sys_getfsfile (mount_point, out fsbuf);
1451 Fstab fs = new Fstab ();
1452 CopyFstab (fs, ref fsbuf);
1456 [DllImport (MPH, SetLastError=true,
1457 EntryPoint="Mono_Posix_Syscall_getfsspec")]
1458 private static extern int sys_getfsspec (string special_file, out _Fstab fs);
1460 public static Fstab getfsspec (string special_file)
1465 r = sys_getfsspec (special_file, out fsbuf);
1469 Fstab fs = new Fstab ();
1470 CopyFstab (fs, ref fsbuf);
1474 [DllImport (MPH, SetLastError=true,
1475 EntryPoint="Mono_Posix_Syscall_setfsent")]
1476 private static extern int sys_setfsent ();
1478 public static int setfsent ()
1481 return sys_setfsent ();
1487 #region <grp.h> Declarations
1491 // TODO: putgrent(3), fgetgrent_r(), getgrouplist(2), initgroups(3)
1494 // int setgroups (size_t size, const gid_t *list);
1495 [DllImport (MPH, SetLastError=true,
1496 EntryPoint="Mono_Posix_Syscall_setgroups")]
1497 public static extern int setgroups (ulong size, uint[] list);
1499 public static int setgroups (uint [] list)
1501 return setgroups ((ulong) list.Length, list);
1504 private struct _Group
1506 public IntPtr gr_name;
1507 public IntPtr gr_passwd;
1508 public /* gid_t */ uint gr_gid;
1509 public int _gr_nmem_;
1510 public IntPtr gr_mem;
1511 public IntPtr _gr_buf_;
1514 private static void CopyGroup (Group to, ref _Group from)
1517 to.gr_gid = from.gr_gid;
1518 to.gr_name = UnixMarshal.PtrToString (from.gr_name);
1519 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
1520 to.gr_mem = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
1523 Stdlib.free (from.gr_mem);
1524 Stdlib.free (from._gr_buf_);
1525 from.gr_mem = IntPtr.Zero;
1526 from._gr_buf_ = IntPtr.Zero;
1530 internal static object grp_lock = new object ();
1532 [DllImport (MPH, SetLastError=true,
1533 EntryPoint="Mono_Posix_Syscall_getgrnam")]
1534 private static extern int sys_getgrnam (string name, out _Group group);
1536 public static Group getgrnam (string name)
1541 r = sys_getgrnam (name, out group);
1545 Group gr = new Group ();
1546 CopyGroup (gr, ref group);
1551 // struct group *getgrgid(gid_t gid);
1552 [DllImport (MPH, SetLastError=true,
1553 EntryPoint="Mono_Posix_Syscall_getgrgid")]
1554 private static extern int sys_getgrgid (uint uid, out _Group group);
1556 public static Group getgrgid (uint uid)
1561 r = sys_getgrgid (uid, out group);
1565 Group gr = new Group ();
1566 CopyGroup (gr, ref group);
1570 [DllImport (MPH, SetLastError=true,
1571 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
1572 private static extern int sys_getgrnam_r (string name, out _Group grbuf, out IntPtr grbufp);
1574 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
1579 int r = sys_getgrnam_r (name, out group, out _grbufp);
1580 if (r == 0 && _grbufp != IntPtr.Zero) {
1581 CopyGroup (grbuf, ref group);
1588 // int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
1589 // size_t buflen, struct group **gbufp);
1590 [DllImport (MPH, SetLastError=true,
1591 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
1592 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
1594 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
1599 int r = sys_getgrgid_r (uid, out group, out _grbufp);
1600 if (r == 0 && _grbufp != IntPtr.Zero) {
1601 CopyGroup (grbuf, ref group);
1607 [DllImport (MPH, SetLastError=true,
1608 EntryPoint="Mono_Posix_Syscall_getgrent")]
1609 private static extern int sys_getgrent (out _Group grbuf);
1611 public static Group getgrent ()
1616 r = sys_getgrent (out group);
1620 Group gr = new Group();
1621 CopyGroup (gr, ref group);
1625 [DllImport (LIBC, SetLastError=true, EntryPoint="setgrent")]
1626 private static extern void sys_setgrent ();
1628 public static void setgrent ()
1635 [DllImport (LIBC, SetLastError=true, EntryPoint="endgrent")]
1636 private static extern void sys_endgrent ();
1638 public static void endgrent ()
1645 [DllImport (MPH, SetLastError=true,
1646 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
1647 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
1649 public static Group fgetgrent (IntPtr stream)
1654 r = sys_fgetgrent (stream, out group);
1658 Group gr = new Group ();
1659 CopyGroup (gr, ref group);
1664 #region <pwd.h> Declarations
1668 // TODO: putpwent(3), fgetpwent_r()
1670 // SKIPPING: getpw(3): it's dangerous. Use getpwuid(3) instead.
1672 private struct _Passwd
1674 public IntPtr pw_name;
1675 public IntPtr pw_passwd;
1676 public /* uid_t */ uint pw_uid;
1677 public /* gid_t */ uint pw_gid;
1678 public IntPtr pw_gecos;
1679 public IntPtr pw_dir;
1680 public IntPtr pw_shell;
1681 public IntPtr _pw_buf_;
1684 private static void CopyPasswd (Passwd to, ref _Passwd from)
1687 to.pw_name = UnixMarshal.PtrToString (from.pw_name);
1688 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
1689 to.pw_uid = from.pw_uid;
1690 to.pw_gid = from.pw_gid;
1691 to.pw_gecos = UnixMarshal.PtrToString (from.pw_gecos);
1692 to.pw_dir = UnixMarshal.PtrToString (from.pw_dir);
1693 to.pw_shell = UnixMarshal.PtrToString (from.pw_shell);
1696 Stdlib.free (from._pw_buf_);
1697 from._pw_buf_ = IntPtr.Zero;
1701 internal static object pwd_lock = new object ();
1703 [DllImport (MPH, SetLastError=true,
1704 EntryPoint="Mono_Posix_Syscall_getpwnam")]
1705 private static extern int sys_getpwnam (string name, out _Passwd passwd);
1707 public static Passwd getpwnam (string name)
1712 r = sys_getpwnam (name, out passwd);
1716 Passwd pw = new Passwd ();
1717 CopyPasswd (pw, ref passwd);
1722 // struct passwd *getpwnuid(uid_t uid);
1723 [DllImport (MPH, SetLastError=true,
1724 EntryPoint="Mono_Posix_Syscall_getpwuid")]
1725 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
1727 public static Passwd getpwuid (uint uid)
1732 r = sys_getpwuid (uid, out passwd);
1736 Passwd pw = new Passwd ();
1737 CopyPasswd (pw, ref passwd);
1741 [DllImport (MPH, SetLastError=true,
1742 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
1743 private static extern int sys_getpwnam_r (string name, out _Passwd pwbuf, out IntPtr pwbufp);
1745 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
1750 int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
1751 if (r == 0 && _pwbufp != IntPtr.Zero) {
1752 CopyPasswd (pwbuf, ref passwd);
1759 // int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
1760 // buflen, struct passwd **pwbufp);
1761 [DllImport (MPH, SetLastError=true,
1762 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
1763 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
1765 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
1770 int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
1771 if (r == 0 && _pwbufp != IntPtr.Zero) {
1772 CopyPasswd (pwbuf, ref passwd);
1778 [DllImport (MPH, SetLastError=true,
1779 EntryPoint="Mono_Posix_Syscall_getpwent")]
1780 private static extern int sys_getpwent (out _Passwd pwbuf);
1782 public static Passwd getpwent ()
1787 r = sys_getpwent (out passwd);
1791 Passwd pw = new Passwd ();
1792 CopyPasswd (pw, ref passwd);
1796 [DllImport (LIBC, SetLastError=true, EntryPoint="setpwent")]
1797 private static extern void sys_setpwent ();
1799 public static void setpwent ()
1806 [DllImport (LIBC, SetLastError=true, EntryPoint="endpwent")]
1807 private static extern void sys_endpwent ();
1809 public static void endpwent ()
1816 [DllImport (MPH, SetLastError=true,
1817 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
1818 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
1820 public static Passwd fgetpwent (IntPtr stream)
1825 r = sys_fgetpwent (stream, out passwd);
1829 Passwd pw = new Passwd ();
1830 CopyPasswd (pw, ref passwd);
1835 #region <signal.h> Declarations
1839 [DllImport (LIBC, SetLastError=true)]
1840 private static extern void psignal (int sig, string s);
1842 public static void psignal (Signum sig, string s)
1844 int signum = UnixConvert.FromSignum (sig);
1845 psignal (signum, s);
1849 // int kill(pid_t pid, int sig);
1850 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
1851 private static extern int sys_kill (int pid, int sig);
1853 public static int kill (int pid, Signum sig)
1855 int _sig = UnixConvert.FromSignum (sig);
1856 return sys_kill (pid, _sig);
1859 private static object signal_lock = new object ();
1861 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
1862 private static extern IntPtr sys_strsignal (int sig);
1864 public static string strsignal (Signum sig)
1866 int s = UnixConvert.FromSignum (sig);
1867 lock (signal_lock) {
1868 IntPtr r = sys_strsignal (s);
1869 return UnixMarshal.PtrToString (r);
1873 // TODO: sigaction(2)
1874 // TODO: sigsuspend(2)
1875 // TODO: sigpending(2)
1879 #region <stdio.h> Declarations
1883 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
1884 private static extern int _L_ctermid ();
1886 public static readonly int L_ctermid = _L_ctermid ();
1888 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
1889 private static extern int _L_cuserid ();
1891 public static readonly int L_cuserid = _L_cuserid ();
1893 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
1894 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
1896 [Obsolete ("\"Nobody knows precisely what cuserid() does... " +
1897 "DO NOT USE cuserid().\n" +
1898 "`string' must hold L_cuserid characters. Use getlogin_r instead.")]
1899 public static string cuserid (StringBuilder @string)
1901 if (@string.Capacity < L_cuserid) {
1902 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
1904 IntPtr r = sys_cuserid (@string);
1905 return UnixMarshal.PtrToString (r);
1910 #region <stdlib.h> Declarations
1914 [DllImport (LIBC, SetLastError=true)]
1915 public static extern int mkstemp (StringBuilder template);
1917 [DllImport (LIBC, SetLastError=true)]
1918 public static extern int ttyslot ();
1920 [DllImport (CRYPT, SetLastError=true)]
1921 public static extern void setkey (string key);
1925 #region <string.h> Declarations
1931 // int strerror_r(int errnum, char *buf, size_t n);
1932 [DllImport (MPH, SetLastError=true,
1933 EntryPoint="Mono_Posix_Syscall_strerror_r")]
1934 private static extern int sys_strerror_r (int errnum,
1935 [Out] StringBuilder buf, ulong n);
1937 public static int strerror_r (Error errnum, StringBuilder buf, ulong n)
1939 int e = UnixConvert.FromError (errnum);
1940 return sys_strerror_r (e, buf, n);
1943 public static int strerror_r (Error errnum, StringBuilder buf)
1945 return strerror_r (errnum, buf, (ulong) buf.Capacity);
1950 #region <sys/mman.h> Declarations
1956 // int posix_madvise(void *addr, size_t len, int advice);
1957 [DllImport (MPH, SetLastError=true,
1958 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
1959 public static extern int posix_madvise (IntPtr addr, ulong len,
1960 PosixMadviseAdvice advice);
1962 public static readonly IntPtr MAP_FAILED = unchecked((IntPtr)(-1));
1964 [DllImport (MPH, SetLastError=true,
1965 EntryPoint="Mono_Posix_Syscall_mmap")]
1966 public static extern IntPtr mmap (IntPtr start, ulong length,
1967 MmapProt prot, MmapFlags flags, int fd, long offset);
1969 [DllImport (MPH, SetLastError=true,
1970 EntryPoint="Mono_Posix_Syscall_munmap")]
1971 public static extern int munmap (IntPtr start, ulong length);
1973 [DllImport (MPH, SetLastError=true,
1974 EntryPoint="Mono_Posix_Syscall_mprotect")]
1975 public static extern int mprotect (IntPtr start, ulong len, MmapProt prot);
1977 [DllImport (MPH, SetLastError=true,
1978 EntryPoint="Mono_Posix_Syscall_msync")]
1979 public static extern int msync (IntPtr start, ulong len, MsyncFlags flags);
1981 [DllImport (MPH, SetLastError=true,
1982 EntryPoint="Mono_Posix_Syscall_mlock")]
1983 public static extern int mlock (IntPtr start, ulong len);
1985 [DllImport (MPH, SetLastError=true,
1986 EntryPoint="Mono_Posix_Syscall_munlock")]
1987 public static extern int munlock (IntPtr start, ulong len);
1989 [DllImport (LIBC, SetLastError=true, EntryPoint="mlockall")]
1990 private static extern int sys_mlockall (int flags);
1992 public static int mlockall (MlockallFlags flags)
1994 int _flags = UnixConvert.FromMlockallFlags (flags);
1995 return sys_mlockall (_flags);
1998 [DllImport (LIBC, SetLastError=true)]
1999 public static extern int munlockall ();
2001 [DllImport (MPH, SetLastError=true,
2002 EntryPoint="Mono_Posix_Syscall_mremap")]
2003 public static extern IntPtr mremap (IntPtr old_address, ulong old_size,
2004 ulong new_size, MremapFlags flags);
2006 [DllImport (MPH, SetLastError=true,
2007 EntryPoint="Mono_Posix_Syscall_mincore")]
2008 public static extern int mincore (IntPtr start, ulong length, byte[] vec);
2010 [DllImport (MPH, SetLastError=true,
2011 EntryPoint="Mono_Posix_Syscall_remap_file_pages")]
2012 public static extern int remap_file_pages (IntPtr start, ulong size,
2013 MmapProt prot, long pgoff, MmapFlags flags);
2017 #region <sys/poll.h> Declarations
2019 // <sys/poll.h> -- COMPLETE
2022 private struct _pollfd {
2024 public short events;
2025 public short revents;
2028 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
2029 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
2031 public static int poll (Pollfd [] fds, uint nfds, int timeout)
2033 if (fds.Length < nfds)
2034 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
2036 _pollfd[] send = new _pollfd[nfds];
2038 for (int i = 0; i < send.Length; i++) {
2039 send [i].fd = fds [i].fd;
2040 send [i].events = UnixConvert.FromPollEvents (fds [i].events);
2043 int r = sys_poll (send, nfds, timeout);
2045 for (int i = 0; i < send.Length; i++) {
2046 fds [i].revents = UnixConvert.ToPollEvents (send [i].revents);
2052 public static int poll (Pollfd [] fds, int timeout)
2054 return poll (fds, (uint) fds.Length, timeout);
2067 // TODO: setrlimit(2)
2068 // TODO: getrlimit(2)
2069 // TODO: getrusage(2)
2073 #region <sys/sendfile.h> Declarations
2075 // <sys/sendfile.h> -- COMPLETE
2078 [DllImport (MPH, SetLastError=true,
2079 EntryPoint="Mono_Posix_Syscall_sendfile")]
2080 public static extern long sendfile (int out_fd, int in_fd,
2081 ref long offset, ulong count);
2085 #region <sys/stat.h> Declarations
2087 // <sys/stat.h> -- COMPLETE
2089 [DllImport (MPH, SetLastError=true,
2090 EntryPoint="Mono_Posix_Syscall_stat")]
2091 public static extern int stat (string file_name, out Stat buf);
2093 [DllImport (MPH, SetLastError=true,
2094 EntryPoint="Mono_Posix_Syscall_fstat")]
2095 public static extern int fstat (int filedes, out Stat buf);
2097 [DllImport (MPH, SetLastError=true,
2098 EntryPoint="Mono_Posix_Syscall_lstat")]
2099 public static extern int lstat (string file_name, out Stat buf);
2102 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
2103 // All take FilePermissions
2106 // int chmod(const char *path, mode_t mode);
2107 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
2108 private static extern int sys_chmod (string path, uint mode);
2110 public static int chmod (string path, FilePermissions mode)
2112 uint _mode = UnixConvert.FromFilePermissions (mode);
2113 return sys_chmod (path, _mode);
2117 // int chmod(int filedes, mode_t mode);
2118 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
2119 private static extern int sys_fchmod (int filedes, uint mode);
2121 public static int fchmod (int filedes, FilePermissions mode)
2123 uint _mode = UnixConvert.FromFilePermissions (mode);
2124 return sys_fchmod (filedes, _mode);
2128 // mode_t umask(mode_t mask);
2129 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
2130 private static extern uint sys_umask (uint mask);
2132 public static FilePermissions umask (FilePermissions mask)
2134 uint _mask = UnixConvert.FromFilePermissions (mask);
2135 uint r = sys_umask (_mask);
2136 return UnixConvert.ToFilePermissions (r);
2140 // int mkdir(const char *pathname, mode_t mode);
2141 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
2142 private static extern int sys_mkdir (string oldpath, uint mode);
2144 public static int mkdir (string oldpath, FilePermissions mode)
2146 uint _mode = UnixConvert.FromFilePermissions (mode);
2147 return sys_mkdir (oldpath, _mode);
2151 // int mknod (const char *pathname, mode_t mode, dev_t dev);
2152 [DllImport (MPH, SetLastError=true,
2153 EntryPoint="Mono_Posix_Syscall_mknod")]
2154 public static extern int mknod (string pathname, FilePermissions mode, ulong dev);
2157 // int mkfifo(const char *pathname, mode_t mode);
2158 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
2159 private static extern int sys_mkfifo (string pathname, uint mode);
2161 public static int mkfifo (string pathname, FilePermissions mode)
2163 uint _mode = UnixConvert.FromFilePermissions (mode);
2164 return sys_mkfifo (pathname, _mode);
2169 #region <sys/stat.h> Declarations
2174 [DllImport (MPH, SetLastError=true,
2175 EntryPoint="Mono_Posix_Syscall_statvfs")]
2176 public static extern int statvfs (string path, out Statvfs buf);
2178 [DllImport (MPH, SetLastError=true,
2179 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
2180 public static extern int fstatvfs (int fd, out Statvfs buf);
2184 #region <sys/time.h> Declarations
2188 // TODO: adjtime(), getitimer(2), setitimer(2), lutimes(), futimes()
2190 [DllImport (MPH, SetLastError=true,
2191 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2192 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
2194 [DllImport (MPH, SetLastError=true,
2195 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2196 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
2198 public static int gettimeofday (out Timeval tv)
2200 return gettimeofday (out tv, IntPtr.Zero);
2203 [DllImport (MPH, SetLastError=true,
2204 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2205 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
2207 public static int gettimeofday (out Timezone tz)
2209 return gettimeofday (IntPtr.Zero, out tz);
2212 [DllImport (MPH, SetLastError=true,
2213 EntryPoint="Mono_Posix_Syscall_settimeofday")]
2214 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
2216 [DllImport (MPH, SetLastError=true,
2217 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2218 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
2220 public static int settimeofday (ref Timeval tv)
2222 return settimeofday (ref tv, IntPtr.Zero);
2225 [DllImport (MPH, SetLastError=true,
2226 EntryPoint="Mono_Posix_Syscall_utimes")]
2227 public static extern int utimes (string filename, ref Timeval tvp);
2243 #region <sys/wait.h> Declarations
2249 // pid_t wait(int *status);
2250 [DllImport (LIBC, SetLastError=true)]
2251 public static extern int wait (out int status);
2254 // pid_t waitpid(pid_t pid, int *status, int options);
2255 [DllImport (LIBC, SetLastError=true)]
2256 private static extern int waitpid (int pid, out int status, int options);
2258 public static int waitpid (int pid, out int status, WaitOptions options)
2260 int _options = UnixConvert.FromWaitOptions (options);
2261 return waitpid (pid, out status, _options);
2264 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
2265 private static extern int _WIFEXITED (int status);
2267 public static bool WIFEXITED (int status)
2269 return _WIFEXITED (status) != 0;
2272 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
2273 public static extern int WEXITSTATUS (int status);
2275 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
2276 private static extern int _WIFSIGNALED (int status);
2278 public static bool WIFSIGNALED (int status)
2280 return _WIFSIGNALED (status) != 0;
2283 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
2284 private static extern int _WTERMSIG (int status);
2286 public static Signum WTERMSIG (int status)
2288 int r = _WTERMSIG (status);
2289 return UnixConvert.ToSignum (r);
2292 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
2293 private static extern int _WIFSTOPPED (int status);
2295 public static bool WIFSTOPPED (int status)
2297 return _WIFSTOPPED (status) != 0;
2300 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
2301 private static extern int _WSTOPSIG (int status);
2303 public static Signum WSTOPSIG (int status)
2305 int r = _WSTOPSIG (status);
2306 return UnixConvert.ToSignum (r);
2315 #region <syslog.h> Declarations
2320 [DllImport (LIBC, EntryPoint="openlog")]
2321 private static extern void sys_openlog (IntPtr ident, int option, int facility);
2323 public static void openlog (IntPtr ident, SyslogOptions option,
2324 SyslogFacility defaultFacility)
2326 int _option = UnixConvert.FromSyslogOptions (option);
2327 int _facility = UnixConvert.FromSyslogFacility (defaultFacility);
2329 sys_openlog (ident, _option, _facility);
2332 [DllImport (LIBC, EntryPoint="syslog")]
2333 private static extern void sys_syslog (int priority, string message);
2335 public static void syslog (SyslogFacility facility, SyslogLevel level, string message)
2337 int _facility = UnixConvert.FromSyslogFacility (facility);
2338 int _level = UnixConvert.FromSyslogLevel (level);
2339 sys_syslog (_facility | _level, GetSyslogMessage (message));
2342 public static void syslog (SyslogLevel level, string message)
2344 int _level = UnixConvert.FromSyslogLevel (level);
2345 sys_syslog (_level, GetSyslogMessage (message));
2348 private static string GetSyslogMessage (string message)
2350 return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
2353 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2354 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
2355 public static void syslog (SyslogFacility facility, SyslogLevel level,
2356 string format, params object[] parameters)
2358 int _facility = UnixConvert.FromSyslogFacility (facility);
2359 int _level = UnixConvert.FromSyslogLevel (level);
2361 object[] _parameters = new object[checked(parameters.Length+2)];
2362 _parameters [0] = _facility | _level;
2363 _parameters [1] = format;
2364 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2365 XPrintfFunctions.syslog (_parameters);
2368 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2369 "Use syslog(SyslogLevel, string) instead.")]
2370 public static void syslog (SyslogLevel level, string format,
2371 params object[] parameters)
2373 int _level = UnixConvert.FromSyslogLevel (level);
2375 object[] _parameters = new object[checked(parameters.Length+2)];
2376 _parameters [0] = _level;
2377 _parameters [1] = format;
2378 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2379 XPrintfFunctions.syslog (_parameters);
2383 public static extern void closelog ();
2385 [DllImport (LIBC, EntryPoint="setlogmask")]
2386 private static extern int sys_setlogmask (int mask);
2388 public static int setlogmask (SyslogLevel mask)
2390 int _mask = UnixConvert.FromSyslogLevel (mask);
2391 return sys_setlogmask (_mask);
2396 #region <time.h> Declarations
2403 // int stime(time_t *t);
2404 [DllImport (MPH, SetLastError=true,
2405 EntryPoint="Mono_Posix_Syscall_stime")]
2406 public static extern int stime (ref long t);
2409 // time_t time(time_t *t);
2410 [DllImport (MPH, SetLastError=true,
2411 EntryPoint="Mono_Posix_Syscall_time")]
2412 public static extern long time (out long t);
2422 #region <unistd.h> Declarations
2426 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
2427 // other TODOs listed below.
2429 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
2430 private static extern int sys_access (string pathname, int mode);
2432 public static int access (string pathname, AccessMode mode)
2434 int _mode = UnixConvert.FromAccessMode (mode);
2435 return sys_access (pathname, _mode);
2439 // off_t lseek(int filedes, off_t offset, int whence);
2440 [DllImport (MPH, SetLastError=true,
2441 EntryPoint="Mono_Posix_Syscall_lseek")]
2442 private static extern long sys_lseek (int fd, long offset, int whence);
2444 public static long lseek (int fd, long offset, SeekFlags whence)
2446 short _whence = UnixConvert.FromSeekFlags (whence);
2447 return sys_lseek (fd, offset, _whence);
2450 [DllImport (LIBC, SetLastError=true)]
2451 public static extern int close (int fd);
2454 // ssize_t read(int fd, void *buf, size_t count);
2455 [DllImport (MPH, SetLastError=true,
2456 EntryPoint="Mono_Posix_Syscall_read")]
2457 public static extern long read (int fd, IntPtr buf, ulong count);
2459 public static unsafe long read (int fd, void *buf, ulong count)
2461 return read (fd, (IntPtr) buf, count);
2465 // ssize_t write(int fd, const void *buf, size_t count);
2466 [DllImport (MPH, SetLastError=true,
2467 EntryPoint="Mono_Posix_Syscall_write")]
2468 public static extern long write (int fd, IntPtr buf, ulong count);
2470 public static unsafe long write (int fd, void *buf, ulong count)
2472 return write (fd, (IntPtr) buf, count);
2476 // ssize_t pread(int fd, void *buf, size_t count, off_t offset);
2477 [DllImport (MPH, SetLastError=true,
2478 EntryPoint="Mono_Posix_Syscall_pread")]
2479 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
2481 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
2483 return pread (fd, (IntPtr) buf, count, offset);
2487 // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
2488 [DllImport (MPH, SetLastError=true,
2489 EntryPoint="Mono_Posix_Syscall_pwrite")]
2490 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
2492 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
2494 return pwrite (fd, (IntPtr) buf, count, offset);
2497 [DllImport (MPH, SetLastError=true,
2498 EntryPoint="Mono_Posix_Syscall_pipe")]
2499 public static extern int pipe (out int reading, out int writing);
2501 public static int pipe (int[] filedes)
2503 if (filedes == null || filedes.Length != 2) {
2507 int reading, writing;
2508 int r = pipe (out reading, out writing);
2509 filedes[0] = reading;
2510 filedes[1] = writing;
2514 [DllImport (LIBC, SetLastError=true)]
2515 public static extern uint alarm (uint seconds);
2517 [DllImport (LIBC, SetLastError=true)]
2518 public static extern uint sleep (uint seconds);
2520 [DllImport (LIBC, SetLastError=true)]
2521 public static extern uint ualarm (uint usecs, uint interval);
2523 [DllImport (LIBC, SetLastError=true)]
2524 public static extern int pause ();
2527 // int chown(const char *path, uid_t owner, gid_t group);
2528 [DllImport (LIBC, SetLastError=true)]
2529 public static extern int chown (string path, uint owner, uint group);
2532 // int fchown(int fd, uid_t owner, gid_t group);
2533 [DllImport (LIBC, SetLastError=true)]
2534 public static extern int fchown (int fd, uint owner, uint group);
2537 // int lchown(const char *path, uid_t owner, gid_t group);
2538 [DllImport (LIBC, SetLastError=true)]
2539 public static extern int lchown (string path, uint owner, uint group);
2541 [DllImport (LIBC, SetLastError=true)]
2542 public static extern int chdir (string path);
2544 [DllImport (LIBC, SetLastError=true)]
2545 public static extern int fchdir (int fd);
2548 // char *getcwd(char *buf, size_t size);
2549 [DllImport (MPH, SetLastError=true,
2550 EntryPoint="Mono_Posix_Syscall_getcwd")]
2551 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
2553 public static StringBuilder getcwd (StringBuilder buf)
2555 getcwd (buf, (ulong) buf.Capacity);
2559 // getwd(2) is deprecated; don't expose it.
2561 [DllImport (LIBC, SetLastError=true)]
2562 public static extern int dup (int fd);
2564 [DllImport (LIBC, SetLastError=true)]
2565 public static extern int dup2 (int fd, int fd2);
2567 // TODO: does Mono marshal arrays properly?
2568 [DllImport (LIBC, SetLastError=true)]
2569 public static extern int execve (string path, string[] argv, string[] envp);
2571 [DllImport (LIBC, SetLastError=true)]
2572 public static extern int fexecve (int fd, string[] argv, string[] envp);
2574 [DllImport (LIBC, SetLastError=true)]
2575 public static extern int execv (string path, string[] argv);
2577 // TODO: execle, execl, execlp
2578 [DllImport (LIBC, SetLastError=true)]
2579 private static extern int execvp (string path, string[] argv);
2581 [DllImport (LIBC, SetLastError=true)]
2582 public static extern int nice (int inc);
2584 [DllImport (LIBC, SetLastError=true)]
2585 public static extern int _exit (int status);
2587 [DllImport (MPH, SetLastError=true,
2588 EntryPoint="Mono_Posix_Syscall_fpathconf")]
2589 public static extern long fpathconf (int filedes, PathConf name);
2591 [DllImport (MPH, SetLastError=true,
2592 EntryPoint="Mono_Posix_Syscall_pathconf")]
2593 public static extern long pathconf (string path, PathConf name);
2595 [DllImport (MPH, SetLastError=true,
2596 EntryPoint="Mono_Posix_Syscall_sysconf")]
2597 public static extern long sysconf (SysConf name);
2600 // size_t confstr(int name, char *buf, size_t len);
2601 [DllImport (MPH, SetLastError=true,
2602 EntryPoint="Mono_Posix_Syscall_confstr")]
2603 public static extern ulong confstr (ConfStr name, [Out] StringBuilder buf, ulong len);
2606 // pid_t getpid(void);
2607 [DllImport (LIBC, SetLastError=true)]
2608 public static extern int getpid ();
2611 // pid_t getppid(void);
2612 [DllImport (LIBC, SetLastError=true)]
2613 public static extern int getppid ();
2616 // int setpgid(pid_t pid, pid_t pgid);
2617 [DllImport (LIBC, SetLastError=true)]
2618 public static extern int setpgid (int pid, int pgid);
2621 // pid_t getpgid(pid_t pid);
2622 [DllImport (LIBC, SetLastError=true)]
2623 public static extern int getpgid (int pid);
2625 [DllImport (LIBC, SetLastError=true)]
2626 public static extern int setpgrp ();
2629 // pid_t getpgrp(void);
2630 [DllImport (LIBC, SetLastError=true)]
2631 public static extern int getpgrp ();
2634 // pid_t setsid(void);
2635 [DllImport (LIBC, SetLastError=true)]
2636 public static extern int setsid ();
2639 // pid_t getsid(pid_t pid);
2640 [DllImport (LIBC, SetLastError=true)]
2641 public static extern int getsid (int pid);
2644 // uid_t getuid(void);
2645 [DllImport (LIBC, SetLastError=true)]
2646 public static extern uint getuid ();
2649 // uid_t geteuid(void);
2650 [DllImport (LIBC, SetLastError=true)]
2651 public static extern uint geteuid ();
2654 // gid_t getgid(void);
2655 [DllImport (LIBC, SetLastError=true)]
2656 public static extern uint getgid ();
2659 // gid_t getgid(void);
2660 [DllImport (LIBC, SetLastError=true)]
2661 public static extern uint getegid ();
2664 // int getgroups(int size, gid_t list[]);
2665 [DllImport (LIBC, SetLastError=true)]
2666 public static extern int getgroups (int size, uint[] list);
2668 public static int getgroups (uint[] list)
2670 return getgroups (list.Length, list);
2674 // int setuid(uid_t uid);
2675 [DllImport (LIBC, SetLastError=true)]
2676 public static extern int setuid (uint uid);
2679 // int setreuid(uid_t ruid, uid_t euid);
2680 [DllImport (LIBC, SetLastError=true)]
2681 public static extern int setreuid (uint ruid, uint euid);
2684 // int setregid(gid_t ruid, gid_t euid);
2685 [DllImport (LIBC, SetLastError=true)]
2686 public static extern int setregid (uint rgid, uint egid);
2689 // int seteuid(uid_t euid);
2690 [DllImport (LIBC, SetLastError=true)]
2691 public static extern int seteuid (uint euid);
2694 // int setegid(gid_t euid);
2695 [DllImport (LIBC, SetLastError=true)]
2696 public static extern int setegid (uint uid);
2699 // int setgid(gid_t gid);
2700 [DllImport (LIBC, SetLastError=true)]
2701 public static extern int setgid (uint gid);
2704 // int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
2705 [DllImport (LIBC, SetLastError=true)]
2706 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
2709 // int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
2710 [DllImport (LIBC, SetLastError=true)]
2711 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
2714 // int setresuid(uid_t ruid, uid_t euid, uid_t suid);
2715 [DllImport (LIBC, SetLastError=true)]
2716 public static extern int setresuid (uint ruid, uint euid, uint suid);
2719 // int setresgid(gid_t ruid, gid_t euid, gid_t suid);
2720 [DllImport (LIBC, SetLastError=true)]
2721 public static extern int setresgid (uint rgid, uint egid, uint sgid);
2724 // pid_t fork(void);
2725 [DllImport (LIBC, SetLastError=true)]
2726 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " +
2727 "shutdown code.\nUse System.Diagnostics.Process instead")]
2728 private static extern int fork ();
2731 // pid_t vfork(void);
2732 [DllImport (LIBC, SetLastError=true)]
2733 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " +
2734 "shutdown code.\nUse System.Diagnostics.Process instead")]
2735 private static extern int vfork ();
2737 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
2738 private static extern IntPtr sys_ttyname (int fd);
2740 private static object tty_lock = new object ();
2742 public static string ttyname (int fd)
2745 IntPtr r = sys_ttyname (fd);
2746 return UnixMarshal.PtrToString (r);
2751 // int ttyname_r(int fd, char *buf, size_t buflen);
2752 [DllImport (MPH, SetLastError=true,
2753 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
2754 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
2756 public static int ttyname_r (int fd, StringBuilder buf)
2758 return ttyname_r (fd, buf, (ulong) buf.Capacity);
2761 [DllImport (LIBC, EntryPoint="isatty")]
2762 private static extern int sys_isatty (int fd);
2764 public static bool isatty (int fd)
2766 return sys_isatty (fd) == 1;
2769 [DllImport (LIBC, SetLastError=true)]
2770 public static extern int link (string oldpath, string newpath);
2772 [DllImport (LIBC, SetLastError=true)]
2773 public static extern int symlink (string oldpath, string newpath);
2776 // int readlink(const char *path, char *buf, size_t bufsize);
2777 [DllImport (MPH, SetLastError=true,
2778 EntryPoint="Mono_Posix_Syscall_readlink")]
2779 public static extern int readlink (string path, [Out] StringBuilder buf, ulong bufsiz);
2781 public static int readlink (string path, [Out] StringBuilder buf)
2783 return readlink (path, buf, (ulong) buf.Capacity);
2786 [DllImport (LIBC, SetLastError=true)]
2787 public static extern int unlink (string pathname);
2789 [DllImport (LIBC, SetLastError=true)]
2790 public static extern int rmdir (string pathname);
2793 // pid_t tcgetpgrp(int fd);
2794 [DllImport (LIBC, SetLastError=true)]
2795 public static extern int tcgetpgrp (int fd);
2798 // int tcsetpgrp(int fd, pid_t pgrp);
2799 [DllImport (LIBC, SetLastError=true)]
2800 public static extern int tcsetpgrp (int fd, int pgrp);
2802 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
2803 private static extern IntPtr sys_getlogin ();
2805 [Obsolete ("Not re-entrant. Use getlogin_r instead.")]
2806 public static string getlogin ()
2808 IntPtr r = sys_getlogin ();
2809 return UnixMarshal.PtrToString (r);
2813 // int getlogin_r(char *buf, size_t bufsize);
2814 [DllImport (MPH, SetLastError=true,
2815 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
2816 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
2818 public static int getlogin_r (StringBuilder name)
2820 return getlogin_r (name, (ulong) name.Capacity);
2823 [DllImport (LIBC, SetLastError=true)]
2824 public static extern int setlogin (string name);
2827 // int gethostname(char *name, size_t len);
2828 [DllImport (MPH, SetLastError=true,
2829 EntryPoint="Mono_Posix_Syscall_gethostname")]
2830 public static extern int gethostname ([Out] StringBuilder name, ulong len);
2832 public static int gethostname (StringBuilder name)
2834 return gethostname (name, (ulong) name.Capacity);
2838 // int gethostname(const char *name, size_t len);
2839 [DllImport (MPH, SetLastError=true,
2840 EntryPoint="Mono_Posix_Syscall_sethostname")]
2841 public static extern int sethostname (string name, ulong len);
2843 public static int sethostname (string name)
2845 return sethostname (name, (ulong) name.Length);
2848 [DllImport (MPH, SetLastError=true,
2849 EntryPoint="Mono_Posix_Syscall_gethostid")]
2850 public static extern long gethostid ();
2852 [DllImport (MPH, SetLastError=true,
2853 EntryPoint="Mono_Posix_Syscall_sethostid")]
2854 public static extern int sethostid (long hostid);
2857 // int getdomainname(char *name, size_t len);
2858 [DllImport (MPH, SetLastError=true,
2859 EntryPoint="Mono_Posix_Syscall_getdomainname")]
2860 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
2862 public static int getdomainname (StringBuilder name)
2864 return getdomainname (name, (ulong) name.Capacity);
2868 // int setdomainname(const char *name, size_t len);
2869 [DllImport (MPH, SetLastError=true,
2870 EntryPoint="Mono_Posix_Syscall_setdomainname")]
2871 public static extern int setdomainname (string name, ulong len);
2873 public static int setdomainname (string name)
2875 return setdomainname (name, (ulong) name.Length);
2878 [DllImport (LIBC, SetLastError=true)]
2879 public static extern int vhangup ();
2881 // Revoke doesn't appear to be POSIX. Include it?
2882 [DllImport (LIBC, SetLastError=true)]
2883 public static extern int revoke (string file);
2885 // TODO: profil? It's not POSIX.
2887 [DllImport (LIBC, SetLastError=true)]
2888 public static extern int acct (string filename);
2890 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
2891 private static extern IntPtr sys_getusershell ();
2893 internal static object usershell_lock = new object ();
2895 public static string getusershell ()
2897 lock (usershell_lock) {
2898 IntPtr r = sys_getusershell ();
2899 return UnixMarshal.PtrToString (r);
2903 [DllImport (LIBC, SetLastError=true, EntryPoint="setusershell")]
2904 private static extern void sys_setusershell ();
2906 public static void setusershell ()
2908 lock (usershell_lock) {
2909 sys_setusershell ();
2913 [DllImport (LIBC, SetLastError=true, EntryPoint="endusershell")]
2914 private static extern void sys_endusershell ();
2916 public static void endusershell ()
2918 lock (usershell_lock) {
2919 sys_endusershell ();
2923 [DllImport (LIBC, SetLastError=true)]
2924 private static extern int daemon (int nochdir, int noclose);
2926 public static int daemon (bool nochdir, bool noclose)
2928 return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
2931 [DllImport (LIBC, SetLastError=true)]
2932 public static extern int chroot (string path);
2934 // skipping getpass(3) as the man page states:
2935 // This function is obsolete. Do not use it.
2937 [DllImport (LIBC, SetLastError=true)]
2938 public static extern int fsync (int fd);
2940 [DllImport (LIBC, SetLastError=true)]
2941 public static extern int fdatasync (int fd);
2943 [DllImport (LIBC, SetLastError=true)]
2944 public static extern void sync ();
2946 [DllImport (LIBC, SetLastError=true)]
2947 [Obsolete ("Dropped in POSIX 1003.1-2001. " +
2948 "Use Unistd.sysconf (SysConf._SC_PAGESIZE).")]
2949 public static extern int getpagesize ();
2952 // int truncate(const char *path, off_t length);
2953 [DllImport (MPH, SetLastError=true,
2954 EntryPoint="Mono_Posix_Syscall_truncate")]
2955 public static extern int truncate (string path, long length);
2958 // int ftruncate(int fd, off_t length);
2959 [DllImport (MPH, SetLastError=true,
2960 EntryPoint="Mono_Posix_Syscall_ftruncate")]
2961 public static extern int ftruncate (int fd, long length);
2963 [DllImport (LIBC, SetLastError=true)]
2964 public static extern int getdtablesize ();
2966 [DllImport (LIBC, SetLastError=true)]
2967 public static extern int brk (IntPtr end_data_segment);
2969 [DllImport (LIBC, SetLastError=true)]
2970 public static extern IntPtr sbrk (IntPtr increment);
2972 // TODO: syscall(2)?
2973 // Probably safer to skip entirely.
2976 // int lockf(int fd, int cmd, off_t len);
2977 [DllImport (MPH, SetLastError=true,
2978 EntryPoint="Mono_Posix_Syscall_lockf")]
2979 public static extern int lockf (int fd, LockfCommand cmd, long len);
2981 [DllImport (CRYPT, SetLastError=true, EntryPoint="crypt")]
2982 private static extern IntPtr sys_crypt (string key, string salt);
2984 public static string crypt (string key, string salt)
2986 IntPtr r = sys_crypt (key, salt);
2987 return UnixMarshal.PtrToString (r);
2990 [DllImport (CRYPT, SetLastError=true, EntryPoint="encrypt")]
2991 private static extern void sys_encrypt ([In, Out] byte[] block, int edflag);
2993 public static void encrypt (byte[] block, bool decode)
2995 if (block.Length < 64)
2996 throw new ArgumentOutOfRangeException ("block", "Must refer to at least 64 bytes");
2997 sys_encrypt (block, decode ? 1 : 0);
3001 // void swab(const void *from, void *to, ssize_t n);
3002 [DllImport (MPH, SetLastError=true,
3003 EntryPoint="Mono_Posix_Syscall_swab")]
3004 public static extern void swab (IntPtr from, IntPtr to, long n);
3006 public static unsafe void swab (void* from, void* to, long n)
3008 swab ((IntPtr) from, (IntPtr) to, n);
3013 #region <utime.h> Declarations
3015 // <utime.h> -- COMPLETE
3018 [DllImport (MPH, SetLastError=true,
3019 EntryPoint="Mono_Posix_Syscall_utime")]
3020 private static extern int sys_utime (string filename, ref Utimbuf buf, int use_buf);
3022 public static int utime (string filename, ref Utimbuf buf)
3024 return sys_utime (filename, ref buf, 1);
3027 public static int utime (string filename)
3029 Utimbuf buf = new Utimbuf ();
3030 return sys_utime (filename, ref buf, 0);