2 // Mono.Unix/Syscall.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Jonathan Pryor (jonpryor@vt.edu)
8 // (C) 2003 Novell, Inc.
9 // (C) 2004-2005 Jonathan Pryor
11 // This file implements the low-level syscall interface to the POSIX
14 // This file tries to stay close to the low-level API as much as possible
15 // using enumerations, structures and in a few cases, using existing .NET
18 // Implementation notes:
20 // Since the values for the various constants on the API changes
21 // from system to system (even Linux on different architectures will
22 // have different values), we define our own set of values, and we
23 // use a set of C helper routines to map from the constants we define
24 // to the values of the native OS.
26 // Bitfields are flagged with the [Map] attribute, and a helper program
27 // generates a set of routines that we can call to convert from our value
28 // definitions to the value definitions expected by the OS; see
29 // NativeConvert for the conversion routines.
31 // Methods that require tuning are bound as `private sys_NAME' methods
32 // and then a `NAME' method is exposed.
36 // Permission is hereby granted, free of charge, to any person obtaining
37 // a copy of this software and associated documentation files (the
38 // "Software"), to deal in the Software without restriction, including
39 // without limitation the rights to use, copy, modify, merge, publish,
40 // distribute, sublicense, and/or sell copies of the Software, and to
41 // permit persons to whom the Software is furnished to do so, subject to
42 // the following conditions:
44 // The above copyright notice and this permission notice shall be
45 // included in all copies or substantial portions of the Software.
47 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
51 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
52 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
53 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57 using System.Collections;
58 using System.Runtime.InteropServices;
60 using Mono.Unix.Native;
62 [assembly:Mono.Unix.Native.HeaderAttribute (
82 namespace Mono.Unix.Native {
87 [CLSCompliant (false)]
88 public enum SyslogOptions {
89 LOG_PID = 0x01, // log the pid with each message
90 LOG_CONS = 0x02, // log on the console if errors in sending
91 LOG_ODELAY = 0x04, // delay open until first syslog (default)
92 LOG_NDELAY = 0x08, // don't delay open
93 LOG_NOWAIT = 0x10, // don't wait for console forks; DEPRECATED
94 LOG_PERROR = 0x20 // log to stderr as well
98 [CLSCompliant (false)]
99 public enum SyslogFacility {
110 LOG_AUTHPRIV = 10 << 3,
112 LOG_LOCAL0 = 16 << 3,
113 LOG_LOCAL1 = 17 << 3,
114 LOG_LOCAL2 = 18 << 3,
115 LOG_LOCAL3 = 19 << 3,
116 LOG_LOCAL4 = 20 << 3,
117 LOG_LOCAL5 = 21 << 3,
118 LOG_LOCAL6 = 22 << 3,
119 LOG_LOCAL7 = 23 << 3,
123 [CLSCompliant (false)]
124 public enum SyslogLevel {
125 LOG_EMERG = 0, // system is unusable
126 LOG_ALERT = 1, // action must be taken immediately
127 LOG_CRIT = 2, // critical conditions
128 LOG_ERR = 3, // warning conditions
129 LOG_WARNING = 4, // warning conditions
130 LOG_NOTICE = 5, // normal but significant condition
131 LOG_INFO = 6, // informational
132 LOG_DEBUG = 7 // debug-level messages
136 [CLSCompliant (false)]
137 public enum OpenFlags : int {
141 O_RDONLY = 0x00000000,
142 O_WRONLY = 0x00000001,
146 // Or-ed with zero or more of these
148 O_CREAT = 0x00000040,
150 O_NOCTTY = 0x00000100,
151 O_TRUNC = 0x00000200,
152 O_APPEND = 0x00000400,
153 O_NONBLOCK = 0x00000800,
157 // These are non-Posix. Using them will result in errors/exceptions on
158 // non-supported platforms.
160 // (For example, "C-wrapped" system calls -- calls with implementation in
161 // MonoPosixHelper -- will return -1 with errno=EINVAL. C#-wrapped system
162 // calls will generate an exception in NativeConvert, as the value can't be
163 // converted on the target platform.)
166 O_NOFOLLOW = 0x00020000,
167 O_DIRECTORY = 0x00010000,
168 O_DIRECT = 0x00004000,
169 O_ASYNC = 0x00002000,
170 O_LARGEFILE = 0x00008000
175 [CLSCompliant (false)]
176 public enum FilePermissions : uint {
177 S_ISUID = 0x0800, // Set user ID on execution
178 S_ISGID = 0x0400, // Set group ID on execution
179 S_ISVTX = 0x0200, // Save swapped text after use (sticky).
180 S_IRUSR = 0x0100, // Read by owner
181 S_IWUSR = 0x0080, // Write by owner
182 S_IXUSR = 0x0040, // Execute by owner
183 S_IRGRP = 0x0020, // Read by group
184 S_IWGRP = 0x0010, // Write by group
185 S_IXGRP = 0x0008, // Execute by group
186 S_IROTH = 0x0004, // Read by other
187 S_IWOTH = 0x0002, // Write by other
188 S_IXOTH = 0x0001, // Execute by other
190 S_IRWXG = (S_IRGRP | S_IWGRP | S_IXGRP),
191 S_IRWXU = (S_IRUSR | S_IWUSR | S_IXUSR),
192 S_IRWXO = (S_IROTH | S_IWOTH | S_IXOTH),
193 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
194 ALLPERMS = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
195 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
198 // Why these are held in "mode_t" is beyond me...
199 S_IFMT = 0xF000, // Bits which determine file type
200 S_IFDIR = 0x4000, // Directory
201 S_IFCHR = 0x2000, // Character device
202 S_IFBLK = 0x6000, // Block device
203 S_IFREG = 0x8000, // Regular file
204 S_IFIFO = 0x1000, // FIFO
205 S_IFLNK = 0xA000, // Symbolic link
206 S_IFSOCK = 0xC000, // Socket
210 [CLSCompliant (false)]
211 public enum FcntlCommand : int {
212 // Form /usr/include/bits/fcntl.h
213 F_DUPFD = 0, // Duplicate file descriptor.
214 F_GETFD = 1, // Get file descriptor flags.
215 F_SETFD = 2, // Set file descriptor flags.
216 F_GETFL = 3, // Get file status flags.
217 F_SETFL = 4, // Set file status flags.
218 F_GETLK = 12, // Get record locking info. [64]
219 F_SETLK = 13, // Set record locking info (non-blocking). [64]
220 F_SETLKW = 14, // Set record locking info (blocking). [64]
221 F_SETOWN = 8, // Set owner of socket (receiver of SIGIO).
222 F_GETOWN = 9, // Get owner of socket (receiver of SIGIO).
223 F_SETSIG = 10, // Set number of signal to be sent.
224 F_GETSIG = 11, // Get number of signal to be sent.
225 F_SETLEASE = 1024, // Set a lease.
226 F_GETLEASE = 1025, // Enquire what lease is active.
227 F_NOTIFY = 1026, // Required notifications on a directory
231 [CLSCompliant (false)]
232 public enum LockType : short {
233 F_RDLCK = 0, // Read lock.
234 F_WRLCK = 1, // Write lock.
235 F_UNLCK = 2, // Remove lock.
239 [CLSCompliant (false)]
240 public enum SeekFlags : short {
241 // values liberally copied from /usr/include/unistd.h
242 SEEK_SET = 0, // Seek from beginning of file.
243 SEEK_CUR = 1, // Seek from current position.
244 SEEK_END = 2, // Seek from end of file.
246 L_SET = SEEK_SET, // BSD alias for SEEK_SET
247 L_INCR = SEEK_CUR, // BSD alias for SEEK_CUR
248 L_XTND = SEEK_END, // BSD alias for SEEK_END
252 [CLSCompliant (false)]
253 public enum DirectoryNotifyFlags : int {
254 // from /usr/include/bits/fcntl.h
255 DN_ACCESS = 0x00000001, // File accessed.
256 DN_MODIFY = 0x00000002, // File modified.
257 DN_CREATE = 0x00000004, // File created.
258 DN_DELETE = 0x00000008, // File removed.
259 DN_RENAME = 0x00000010, // File renamed.
260 DN_ATTRIB = 0x00000020, // File changed attributes.
261 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
265 [CLSCompliant (false)]
266 public enum PosixFadviseAdvice : int {
267 POSIX_FADV_NORMAL = 0, // No further special treatment.
268 POSIX_FADV_RANDOM = 1, // Expect random page references.
269 POSIX_FADV_SEQUENTIAL = 2, // Expect sequential page references.
270 POSIX_FADV_WILLNEED = 3, // Will need these pages.
271 POSIX_FADV_DONTNEED = 4, // Don't need these pages.
272 POSIX_FADV_NOREUSE = 5, // Data will be accessed once.
276 [CLSCompliant (false)]
277 public enum PosixMadviseAdvice : int {
278 POSIX_MADV_NORMAL = 0, // No further special treatment.
279 POSIX_MADV_RANDOM = 1, // Expect random page references.
280 POSIX_MADV_SEQUENTIAL = 2, // Expect sequential page references.
281 POSIX_MADV_WILLNEED = 3, // Will need these pages.
282 POSIX_MADV_DONTNEED = 4, // Don't need these pages.
286 [CLSCompliant (false)]
287 public enum Signum : int {
288 SIGHUP = 1, // Hangup (POSIX).
289 SIGINT = 2, // Interrupt (ANSI).
290 SIGQUIT = 3, // Quit (POSIX).
291 SIGILL = 4, // Illegal instruction (ANSI).
292 SIGTRAP = 5, // Trace trap (POSIX).
293 SIGABRT = 6, // Abort (ANSI).
294 SIGIOT = 6, // IOT trap (4.2 BSD).
295 SIGBUS = 7, // BUS error (4.2 BSD).
296 SIGFPE = 8, // Floating-point exception (ANSI).
297 SIGKILL = 9, // Kill, unblockable (POSIX).
298 SIGUSR1 = 10, // User-defined signal 1 (POSIX).
299 SIGSEGV = 11, // Segmentation violation (ANSI).
300 SIGUSR2 = 12, // User-defined signal 2 (POSIX).
301 SIGPIPE = 13, // Broken pipe (POSIX).
302 SIGALRM = 14, // Alarm clock (POSIX).
303 SIGTERM = 15, // Termination (ANSI).
304 SIGSTKFLT = 16, // Stack fault.
305 SIGCLD = SIGCHLD, // Same as SIGCHLD (System V).
306 SIGCHLD = 17, // Child status has changed (POSIX).
307 SIGCONT = 18, // Continue (POSIX).
308 SIGSTOP = 19, // Stop, unblockable (POSIX).
309 SIGTSTP = 20, // Keyboard stop (POSIX).
310 SIGTTIN = 21, // Background read from tty (POSIX).
311 SIGTTOU = 22, // Background write to tty (POSIX).
312 SIGURG = 23, // Urgent condition on socket (4.2 BSD).
313 SIGXCPU = 24, // CPU limit exceeded (4.2 BSD).
314 SIGXFSZ = 25, // File size limit exceeded (4.2 BSD).
315 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
316 SIGPROF = 27, // Profiling alarm clock (4.2 BSD).
317 SIGWINCH = 28, // Window size change (4.3 BSD, Sun).
318 SIGPOLL = SIGIO, // Pollable event occurred (System V).
319 SIGIO = 29, // I/O now possible (4.2 BSD).
320 SIGPWR = 30, // Power failure restart (System V).
321 SIGSYS = 31, // Bad system call.
326 [CLSCompliant (false)]
327 public enum WaitOptions : int {
328 WNOHANG = 1, // Don't block waiting
329 WUNTRACED = 2, // Report status of stopped children
333 [CLSCompliant (false)]
334 public enum AccessModes : int {
342 [CLSCompliant (false)]
343 public enum PathconfName : int {
350 _PC_CHOWN_RESTRICTED,
358 _PC_REC_INCR_XFER_SIZE,
359 _PC_REC_MAX_XFER_SIZE,
360 _PC_REC_MIN_XFER_SIZE,
368 [CLSCompliant (false)]
369 public enum SysconfName : int {
379 _SC_REALTIME_SIGNALS,
380 _SC_PRIORITY_SCHEDULING,
389 _SC_MEMORY_PROTECTION,
392 _SC_SHARED_MEMORY_OBJECTS,
395 _SC_AIO_PRIO_DELTA_MAX,
406 /* Values for the argument to `sysconf'
407 corresponding to _POSIX2_* symbols. */
412 _SC_COLL_WEIGHTS_MAX,
417 _SC_CHARCLASS_NAME_MAX,
433 _SC_IOV_MAX = _SC_UIO_MAXIOV,
434 _SC_PII_INTERNET_STREAM,
435 _SC_PII_INTERNET_DGRAM,
440 /* Values according to POSIX 1003.1c (POSIX threads). */
442 _SC_THREAD_SAFE_FUNCTIONS,
443 _SC_GETGR_R_SIZE_MAX,
444 _SC_GETPW_R_SIZE_MAX,
447 _SC_THREAD_DESTRUCTOR_ITERATIONS,
449 _SC_THREAD_STACK_MIN,
450 _SC_THREAD_THREADS_MAX,
451 _SC_THREAD_ATTR_STACKADDR,
452 _SC_THREAD_ATTR_STACKSIZE,
453 _SC_THREAD_PRIORITY_SCHEDULING,
454 _SC_THREAD_PRIO_INHERIT,
455 _SC_THREAD_PRIO_PROTECT,
456 _SC_THREAD_PROCESS_SHARED,
457 _SC_NPROCESSORS_CONF,
458 _SC_NPROCESSORS_ONLN,
464 _SC_XOPEN_XCU_VERSION,
499 _SC_XBS5_ILP32_OFF32,
500 _SC_XBS5_ILP32_OFFBIG,
502 _SC_XBS5_LPBIG_OFFBIG,
505 _SC_XOPEN_REALTIME_THREADS,
510 _SC_C_LANG_SUPPORT_R,
516 _SC_DEVICE_SPECIFIC_R,
527 _SC_READER_WRITER_LOCKS,
535 _SC_THREAD_SPORADIC_SERVER,
537 _SC_SYSTEM_DATABASE_R,
539 _SC_TYPED_MEMORY_OBJECTS,
543 _SC_2_PBS_ACCOUNTING,
549 _SC_2_PBS_CHECKPOINT,
556 _SC_TRACE_EVENT_FILTER,
559 _SC_LEVEL1_ICACHE_SIZE,
560 _SC_LEVEL1_ICACHE_ASSOC,
561 _SC_LEVEL1_ICACHE_LINESIZE,
562 _SC_LEVEL1_DCACHE_SIZE,
563 _SC_LEVEL1_DCACHE_ASSOC,
564 _SC_LEVEL1_DCACHE_LINESIZE,
565 _SC_LEVEL2_CACHE_SIZE,
566 _SC_LEVEL2_CACHE_ASSOC,
567 _SC_LEVEL2_CACHE_LINESIZE,
568 _SC_LEVEL3_CACHE_SIZE,
569 _SC_LEVEL3_CACHE_ASSOC,
570 _SC_LEVEL3_CACHE_LINESIZE,
571 _SC_LEVEL4_CACHE_SIZE,
572 _SC_LEVEL4_CACHE_ASSOC,
573 _SC_LEVEL4_CACHE_LINESIZE
577 [CLSCompliant (false)]
578 public enum ConfstrName : int {
579 _CS_PATH, /* The default search path. */
580 _CS_V6_WIDTH_RESTRICTED_ENVS,
581 _CS_GNU_LIBC_VERSION,
582 _CS_GNU_LIBPTHREAD_VERSION,
583 _CS_LFS_CFLAGS = 1000,
591 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
592 _CS_XBS5_ILP32_OFF32_LDFLAGS,
593 _CS_XBS5_ILP32_OFF32_LIBS,
594 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
595 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
596 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
597 _CS_XBS5_ILP32_OFFBIG_LIBS,
598 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
599 _CS_XBS5_LP64_OFF64_CFLAGS,
600 _CS_XBS5_LP64_OFF64_LDFLAGS,
601 _CS_XBS5_LP64_OFF64_LIBS,
602 _CS_XBS5_LP64_OFF64_LINTFLAGS,
603 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
604 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
605 _CS_XBS5_LPBIG_OFFBIG_LIBS,
606 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
607 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
608 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
609 _CS_POSIX_V6_ILP32_OFF32_LIBS,
610 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
611 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
612 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
613 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
614 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
615 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
616 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
617 _CS_POSIX_V6_LP64_OFF64_LIBS,
618 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
619 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
620 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
621 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
622 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
626 [CLSCompliant (false)]
627 public enum LockfCommand : int {
628 F_ULOCK = 0, // Unlock a previously locked region.
629 F_LOCK = 1, // Lock a region for exclusive use.
630 F_TLOCK = 2, // Test and lock a region for exclusive use.
631 F_TEST = 3, // Test a region for other process locks.
635 [CLSCompliant (false)]
636 public enum PollEvents : short {
637 POLLIN = 0x0001, // There is data to read
638 POLLPRI = 0x0002, // There is urgent data to read
639 POLLOUT = 0x0004, // Writing now will not block
640 POLLERR = 0x0008, // Error condition
641 POLLHUP = 0x0010, // Hung up
642 POLLNVAL = 0x0020, // Invalid request; fd not open
643 // XPG4.2 definitions (via _XOPEN_SOURCE)
644 POLLRDNORM = 0x0040, // Normal data bay be read
645 POLLRDBAND = 0x0080, // Priority data may be read
646 POLLWRNORM = 0x0100, // Writing now will not block
647 POLLWRBAND = 0x0200, // Priority data may be written
651 [CLSCompliant (false)]
652 public enum XattrFlags : int {
659 [CLSCompliant (false)]
660 public enum MountFlags : ulong {
661 ST_RDONLY = 1, // Mount read-only
662 ST_NOSUID = 2, // Ignore suid and sgid bits
663 ST_NODEV = 4, // Disallow access to device special files
664 ST_SYNCHRONOUS = 16, // Writes are synced at once
665 ST_MANDLOCK = 64, // Allow mandatory locks on an FS
666 ST_WRITE = 128, // Write on file/directory/symlink
667 ST_APPEND = 256, // Append-only file
668 ST_IMMUTABLE = 512, // Immutable file
669 ST_NOATIME = 1024, // Do not update access times
670 ST_NODIRATIME = 2048, // Do not update directory access times
674 [CLSCompliant (false)]
675 public enum MmapFlags : int {
676 MAP_SHARED = 0x01, // Share changes.
677 MAP_PRIVATE = 0x02, // Changes are private.
678 MAP_TYPE = 0x0f, // Mask for type of mapping.
679 MAP_FIXED = 0x10, // Interpret addr exactly.
681 MAP_ANONYMOUS = 0x20, // Don't use a file.
682 MAP_ANON = MAP_ANONYMOUS,
684 // These are Linux-specific.
685 MAP_GROWSDOWN = 0x00100, // Stack-like segment.
686 MAP_DENYWRITE = 0x00800, // ETXTBSY
687 MAP_EXECUTABLE = 0x01000, // Mark it as an executable.
688 MAP_LOCKED = 0x02000, // Lock the mapping.
689 MAP_NORESERVE = 0x04000, // Don't check for reservations.
690 MAP_POPULATE = 0x08000, // Populate (prefault) pagetables.
691 MAP_NONBLOCK = 0x10000, // Do not block on IO.
695 [CLSCompliant (false)]
696 public enum MmapProts : int {
697 PROT_READ = 0x1, // Page can be read.
698 PROT_WRITE = 0x2, // Page can be written.
699 PROT_EXEC = 0x4, // Page can be executed.
700 PROT_NONE = 0x0, // Page can not be accessed.
701 PROT_GROWSDOWN = 0x01000000, // Extend change to start of
702 // growsdown vma (mprotect only).
703 PROT_GROWSUP = 0x02000000, // Extend change to start of
704 // growsup vma (mprotect only).
708 [CLSCompliant (false)]
709 public enum MsyncFlags : int {
710 MS_ASYNC = 0x1, // Sync memory asynchronously.
711 MS_SYNC = 0x4, // Synchronous memory sync.
712 MS_INVALIDATE = 0x2, // Invalidate the caches.
716 [CLSCompliant (false)]
717 public enum MlockallFlags : int {
718 MCL_CURRENT = 0x1, // Lock all currently mapped pages.
719 MCL_FUTURE = 0x2, // Lock all additions to address
723 [CLSCompliant (false)]
724 public enum MremapFlags : ulong {
725 MREMAP_MAYMOVE = 0x1,
732 public struct Flock {
733 [CLSCompliant (false)]
734 public LockType l_type; // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
735 [CLSCompliant (false)]
736 public SeekFlags l_whence; // How to interpret l_start
737 public /* off_t */ long l_start; // Starting offset for lock
738 public /* off_t */ long l_len; // Number of bytes to lock
739 public /* pid_t */ int l_pid; // PID of process blocking our lock (F_GETLK only)
742 [StructLayout(LayoutKind.Sequential)]
743 public struct Pollfd {
745 [CLSCompliant (false)]
746 public PollEvents events;
747 [CLSCompliant (false)]
748 public PollEvents revents;
752 [CLSCompliant (false)]
753 public /* dev_t */ ulong st_dev; // device
754 [CLSCompliant (false)]
755 public /* ino_t */ ulong st_ino; // inode
756 [CLSCompliant (false)]
757 public FilePermissions st_mode; // protection
758 [CLSCompliant (false)]
759 private uint _padding_; // padding for structure alignment
760 [CLSCompliant (false)]
761 public /* nlink_t */ ulong st_nlink; // number of hard links
762 [CLSCompliant (false)]
763 public /* uid_t */ uint st_uid; // user ID of owner
764 [CLSCompliant (false)]
765 public /* gid_t */ uint st_gid; // group ID of owner
766 [CLSCompliant (false)]
767 public /* dev_t */ ulong st_rdev; // device type (if inode device)
768 public /* off_t */ long st_size; // total size, in bytes
769 public /* blksize_t */ long st_blksize; // blocksize for filesystem I/O
770 public /* blkcnt_t */ long st_blocks; // number of blocks allocated
771 public /* time_t */ long st_atime; // time of last access
772 public /* time_t */ long st_mtime; // time of last modification
773 public /* time_t */ long st_ctime; // time of last status change
776 [CLSCompliant (false)]
777 public struct Statvfs {
778 public ulong f_bsize; // file system block size
779 public ulong f_frsize; // fragment size
780 public /* fsblkcnt_t */ ulong f_blocks; // size of fs in f_frsize units
781 public /* fsblkcnt_t */ ulong f_bfree; // # free blocks
782 public /* fsblkcnt_t */ ulong f_bavail; // # free blocks for non-root
783 public /* fsfilcnt_t */ ulong f_files; // # inodes
784 public /* fsfilcnt_t */ ulong f_ffree; // # free inodes
785 public /* fsfilcnt_t */ ulong f_favail; // # free inodes for non-root
786 public ulong f_fsid; // file system id
787 public MountFlags f_flag; // mount flags
788 public ulong f_namemax; // maximum filename length
791 public struct Timeval {
792 public /* time_t */ long tv_sec; // seconds
793 public /* suseconds_t */ long tv_usec; // microseconds
796 public struct Timezone {
797 public int tz_minuteswest; // minutes W of Greenwich
798 private int tz_dsttime; // type of dst correction (OBSOLETE)
801 public struct Utimbuf {
802 public /* time_t */ long actime; // access time
803 public /* time_t */ long modtime; // modification time
810 [CLSCompliant (false)]
811 public sealed class Dirent
813 [CLSCompliant (false)]
814 public /* ino_t */ ulong d_ino;
815 public /* off_t */ long d_off;
816 [CLSCompliant (false)]
817 public ushort d_reclen;
819 public string d_name;
821 public override int GetHashCode ()
823 return d_ino.GetHashCode () ^ d_off.GetHashCode () ^
824 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
825 d_name.GetHashCode ();
828 public override bool Equals (object obj)
830 if (obj == null || GetType() != obj.GetType())
832 Dirent d = (Dirent) obj;
833 return d.d_ino == d_ino && d.d_off == d_off &&
834 d.d_reclen == d_reclen && d.d_type == d_type &&
838 public override string ToString ()
843 public static bool operator== (Dirent lhs, Dirent rhs)
845 return Object.Equals (lhs, rhs);
848 public static bool operator!= (Dirent lhs, Dirent rhs)
850 return !Object.Equals (lhs, rhs);
854 public sealed class Fstab
856 public string fs_spec;
857 public string fs_file;
858 public string fs_vfstype;
859 public string fs_mntops;
860 public string fs_type;
862 public int fs_passno;
864 public override int GetHashCode ()
866 return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
867 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
868 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
871 public override bool Equals (object obj)
873 if (obj == null || GetType() != obj.GetType())
875 Fstab f = (Fstab) obj;
876 return f.fs_spec == fs_spec && f.fs_file == fs_file &&
877 f.fs_vfstype == fs_vfstype && f.fs_mntops == fs_mntops &&
878 f.fs_type == fs_type && f.fs_freq == fs_freq &&
879 f.fs_passno == fs_passno;
882 public override string ToString ()
887 public static bool operator== (Fstab lhs, Fstab rhs)
889 return Object.Equals (lhs, rhs);
892 public static bool operator!= (Fstab lhs, Fstab rhs)
894 return !Object.Equals (lhs, rhs);
898 public sealed class Group
900 public string gr_name;
901 public string gr_passwd;
902 [CLSCompliant (false)]
903 public /* gid_t */ uint gr_gid;
904 public string[] gr_mem;
906 public override int GetHashCode ()
909 for (int i = 0; i < gr_mem.Length; ++i)
910 memhc ^= gr_mem[i].GetHashCode ();
912 return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^
913 gr_gid.GetHashCode () ^ memhc;
916 public override bool Equals (object obj)
918 if (obj == null || GetType() != obj.GetType())
920 Group g = (Group) obj;
921 if (g.gr_gid != gr_gid)
923 if (g.gr_gid == gr_gid && g.gr_name == gr_name &&
924 g.gr_passwd == gr_passwd) {
925 if (g.gr_mem == gr_mem)
927 if (g.gr_mem == null || gr_mem == null)
929 if (g.gr_mem.Length != gr_mem.Length)
931 for (int i = 0; i < gr_mem.Length; ++i)
932 if (gr_mem[i] != g.gr_mem[i])
939 // Generate string in /etc/group format
940 public override string ToString ()
942 StringBuilder sb = new StringBuilder ();
943 sb.AppendFormat ("{0}:{1}:{2}:", gr_name, gr_passwd, gr_gid);
944 GetMembers (sb, gr_mem);
945 return sb.ToString ();
948 private static void GetMembers (StringBuilder sb, string[] members)
950 if (members.Length > 0)
951 sb.Append (members[0]);
952 for (int i = 1; i < members.Length; ++i) {
954 sb.Append (members[i]);
958 public static bool operator== (Group lhs, Group rhs)
960 return Object.Equals (lhs, rhs);
963 public static bool operator!= (Group lhs, Group rhs)
965 return !Object.Equals (lhs, rhs);
969 public sealed class Passwd
971 public string pw_name;
972 public string pw_passwd;
973 [CLSCompliant (false)]
974 public /* uid_t */ uint pw_uid;
975 [CLSCompliant (false)]
976 public /* gid_t */ uint pw_gid;
977 public string pw_gecos;
978 public string pw_dir;
979 public string pw_shell;
981 public override int GetHashCode ()
983 return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^
984 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
985 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
986 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
989 public override bool Equals (object obj)
991 if (obj == null || GetType() != obj.GetType())
993 Passwd p = (Passwd) obj;
994 return p.pw_uid == pw_uid && p.pw_gid == pw_gid && p.pw_name == pw_name &&
995 p.pw_passwd == pw_passwd && p.pw_gecos == pw_gecos &&
996 p.pw_dir == pw_dir && p.pw_shell == pw_shell;
999 // Generate string in /etc/passwd format
1000 public override string ToString ()
1002 return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
1003 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
1006 public static bool operator== (Passwd lhs, Passwd rhs)
1008 return Object.Equals (lhs, rhs);
1011 public static bool operator!= (Passwd lhs, Passwd rhs)
1013 return !Object.Equals (lhs, rhs);
1018 // Convention: Functions *not* part of the standard C library AND part of
1019 // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
1021 // For example, the man page should be similar to:
1023 // CONFORMING TO (or CONFORMS TO)
1024 // XPG2, SUSv2, POSIX, etc.
1026 // BSD- and GNU-specific exports can also be placed here.
1028 // Non-POSIX/XPG/etc. functions can also be placed here if:
1029 // (a) They'd be likely to be covered in a Steven's-like book
1030 // (b) The functions would be present in libc.so (or equivalent).
1032 // If a function has its own library, that's a STRONG indicator that the
1033 // function should get a different binding, probably in its own assembly,
1034 // so that package management can work sanely. (That is, we'd like to avoid
1035 // scenarios where FooLib.dll is installed, but it requires libFooLib.so to
1036 // run, and libFooLib.so doesn't exist. That would be confusing.)
1038 // The only methods in here should be:
1039 // (1) low-level functions
1040 // (2) "Trivial" function overloads. For example, if the parameters to a
1041 // function are related (e.g. getgroups(2))
1042 // (3) The return type SHOULD NOT be changed. If you want to provide a
1043 // convenience function with a nicer return type, place it into one of
1044 // the Mono.Unix.Unix* wrapper classes, and give it a .NET-styled name.
1045 // - EXCEPTION: No public functions should have a `void' return type.
1046 // `void' return types should be replaced with `int'.
1047 // Rationality: `void'-return functions typically require a
1048 // complicated call sequence, such as clear errno, then call, then
1049 // check errno to see if any errors occurred. This sequence can't
1050 // be done safely in managed code, as errno may change as part of
1051 // the P/Invoke mechanism.
1052 // Instead, add a MonoPosixHelper export which does:
1055 // return errno == 0 ? 0 : -1;
1056 // This lets managed code check the return value in the usual manner.
1057 // (4) Exceptions SHOULD NOT be thrown. EXCEPTIONS:
1058 // - If you're wrapping *broken* methods which make assumptions about
1059 // input data, such as that an argument refers to N bytes of data.
1060 // This is currently limited to cuserid(3) and encrypt(3).
1061 // - If you call functions which themselves generate exceptions.
1062 // This is the case for using NativeConvert, which will throw an
1063 // exception if an invalid/unsupported value is used.
1065 // Naming Conventions:
1066 // - Syscall method names should have the same name as the function being
1067 // wrapped (e.g. Syscall.read ==> read(2)). This allows people to
1068 // consult the appropriate man page if necessary.
1069 // - Methods need not have the same arguments IF this simplifies or
1070 // permits correct usage. The current example is syslog, in which
1071 // syslog(3)'s single `priority' argument is split into SyslogFacility
1072 // and SyslogLevel arguments.
1073 // - Type names (structures, classes, enumerations) are always PascalCased.
1074 // - Enumerations are named as <MethodName><ArgumentName>, and are located
1075 // in the Mono.Unix.Native namespace. For readability, if ArgumentName
1076 // is "cmd", use Command instead. For example, fcntl(2) takes a
1077 // FcntlCommand argument. This naming convention is to provide an
1078 // assocation between an enumeration and where it should be used, and
1079 // allows a single method to accept multiple different enumerations
1080 // (see mmap(2), which takes MmapProts and MmapFlags).
1081 // - EXCEPTION: if an enumeration is shared between multiple different
1082 // methods, AND/OR the "obvious" enumeration name conflicts with an
1083 // existing .NET type, a more appropriate name should be used.
1084 // Example: FilePermissions
1085 // - EXCEPTION: [Flags] enumerations should get plural names to follow
1086 // .NET name guidelines. Usually this doesn't result in a change
1087 // (OpenFlags is the `flags' parameter for open(2)), but it can
1088 // (mmap(2) prot ==> MmapProts, access(2) mode ==> AccessModes).
1089 // - Enumerations should have the [Map] and (optional) [Flags] attributes.
1090 // [Map] is required for make-map to find the type and generate the
1091 // appropriate NativeConvert conversion functions.
1092 // - Enumeration contents should match the original Unix names. This helps
1093 // with documentation (the existing man pages are still useful), and is
1094 // required for use with the make-map generation program.
1095 // - Structure names should be the PascalCased version of the actual
1096 // structure name (struct flock ==> Flock). Structure members should
1097 // have the same names, or a (reasonably) portable subset (Dirent being
1098 // the poster child for questionable members).
1099 // - Whether the managed type should be a reference type (class) or a
1100 // value type (struct) should be determined on a case-by-case basis:
1101 // if you ever need to be able to use NULL for it (such as with Dirent,
1102 // Group, Passwd, as these are method return types and `null' is used
1103 // to signify the end), it should be a reference type; otherwise, use
1104 // your discretion, and keep any expected usage patterns in mind.
1105 // - Syscall should be a Single Point Of Truth (SPOT). There should be
1106 // only ONE way to do anything. By convention, the Linux function names
1107 // are used, but that need not always be the case (use your discretion).
1108 // It SHOULD NOT be required that developers know what platform they're
1109 // on, and choose among a set of similar functions. In short, anything
1110 // that requires a platform check is BAD -- Mono.Unix is a wrapper, and
1111 // we can afford to clean things up whenever possible.
1113 // - Syscall.statfs: Solaris/Mac OS X provide statfs(2), Linux provides
1114 // statvfs(2). MonoPosixHelper will "thunk" between the two,
1115 // exporting a statvfs that works across platforms.
1116 // - Syscall.getfsent: Glibc export which Solaris lacks, while Solaris
1117 // instead provides getvfsent(3). MonoPosixHelper provides wrappers
1118 // to convert getvfsent(3) into Fstab data.
1119 // - Exception: If it isn't possible to cleanly wrap platforms, then the
1120 // method shouldn't be exported. The user will be expected to do their
1121 // own platform check and their own DllImports.
1122 // Examples: mount(2), umount(2), etc.
1123 // - Note: if a platform doesn't support a function AT ALL, the
1124 // MonoPosixHelper wrapper won't be compiled, resulting in a
1125 // EntryPointNotFoundException. This is also consistent with a missing
1126 // P/Invoke into libc.so.
1128 [CLSCompliant (false)]
1129 public sealed class Syscall : Stdlib
1131 new internal const string LIBC = "libc";
1132 private const string CRYPT = "crypt";
1134 private Syscall () {}
1140 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3),
1141 // aio_return(3), aio_suspend(3), aio_write(3)
1143 // Then update UnixStream.BeginRead to use the aio* functions.
1146 #region <attr/xattr.h> Declarations
1148 // <attr/xattr.h> -- COMPLETE
1152 // int setxattr (const char *path, const char *name,
1153 // const void *value, size_t size, int flags);
1154 [DllImport (MPH, SetLastError=true,
1155 EntryPoint="Mono_Posix_Syscall_setxattr")]
1156 public static extern int setxattr (string path, string name, byte[] value, ulong size, XattrFlags flags);
1158 public static int setxattr (string path, string name, byte [] value, ulong size)
1160 return setxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1163 public static int setxattr (string path, string name, byte [] value, XattrFlags flags)
1165 return setxattr (path, name, value, (ulong) value.Length, flags);
1168 public static int setxattr (string path, string name, byte [] value)
1170 return setxattr (path, name, value, (ulong) value.Length);
1174 // int lsetxattr (const char *path, const char *name,
1175 // const void *value, size_t size, int flags);
1176 [DllImport (MPH, SetLastError=true,
1177 EntryPoint="Mono_Posix_Syscall_lsetxattr")]
1178 public static extern int lsetxattr (string path, string name, byte[] value, ulong size, XattrFlags flags);
1180 public static int lsetxattr (string path, string name, byte [] value, ulong size)
1182 return lsetxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1185 public static int lsetxattr (string path, string name, byte [] value, XattrFlags flags)
1187 return lsetxattr (path, name, value, (ulong) value.Length, flags);
1190 public static int lsetxattr (string path, string name, byte [] value)
1192 return lsetxattr (path, name, value, (ulong) value.Length);
1196 // int fsetxattr (int fd, const char *name,
1197 // const void *value, size_t size, int flags);
1198 [DllImport (MPH, SetLastError=true,
1199 EntryPoint="Mono_Posix_Syscall_fsetxattr")]
1200 public static extern int fsetxattr (int fd, string name, byte[] value, ulong size, XattrFlags flags);
1202 public static int fsetxattr (int fd, string name, byte [] value, ulong size)
1204 return fsetxattr (fd, name, value, size, XattrFlags.XATTR_AUTO);
1207 public static int fsetxattr (int fd, string name, byte [] value, XattrFlags flags)
1209 return fsetxattr (fd, name, value, (ulong) value.Length, flags);
1212 public static int fsetxattr (int fd, string name, byte [] value)
1214 return fsetxattr (fd, name, value, (ulong) value.Length);
1218 // ssize_t getxattr (const char *path, const char *name,
1219 // void *value, size_t size);
1220 [DllImport (MPH, SetLastError=true,
1221 EntryPoint="Mono_Posix_Syscall_getxattr")]
1222 public static extern long getxattr (string path, string name, byte[] value, ulong size);
1224 public static long getxattr (string path, string name, byte [] value)
1226 return getxattr (path, name, value, (ulong) value.Length);
1229 public static long getxattr (string path, string name, out byte [] value)
1232 long size = getxattr (path, name, value, 0);
1236 value = new byte [size];
1237 return getxattr (path, name, value, (ulong) size);
1241 // ssize_t lgetxattr (const char *path, const char *name,
1242 // void *value, size_t size);
1243 [DllImport (MPH, SetLastError=true,
1244 EntryPoint="Mono_Posix_Syscall_lgetxattr")]
1245 public static extern long lgetxattr (string path, string name, byte[] value, ulong size);
1247 public static long lgetxattr (string path, string name, byte [] value)
1249 return lgetxattr (path, name, value, (ulong) value.Length);
1252 public static long lgetxattr (string path, string name, out byte [] value)
1255 long size = lgetxattr (path, name, value, 0);
1259 value = new byte [size];
1260 return lgetxattr (path, name, value, (ulong) size);
1264 // ssize_t fgetxattr (int fd, const char *name, void *value, size_t size);
1265 [DllImport (MPH, SetLastError=true,
1266 EntryPoint="Mono_Posix_Syscall_fgetxattr")]
1267 public static extern long fgetxattr (int fd, string name, byte[] value, ulong size);
1269 public static long fgetxattr (int fd, string name, byte [] value)
1271 return fgetxattr (fd, name, value, (ulong) value.Length);
1274 public static long fgetxattr (int fd, string name, out byte [] value)
1277 long size = fgetxattr (fd, name, value, 0);
1281 value = new byte [size];
1282 return fgetxattr (fd, name, value, (ulong) size);
1286 // ssize_t listxattr (const char *path, char *list, size_t size);
1287 [DllImport (MPH, SetLastError=true,
1288 EntryPoint="Mono_Posix_Syscall_listxattr")]
1289 public static extern long listxattr (string path, byte[] list, ulong size);
1291 // Slight modification: returns 0 on success, negative on error
1292 public static long listxattr (string path, Encoding encoding, out string [] values)
1295 long size = listxattr (path, null, 0);
1297 values = new string [0];
1301 byte[] list = new byte [size];
1302 long ret = listxattr (path, list, (ulong) size);
1306 string [] output = encoding.GetString (list).Split((char) 0);
1307 values = new string [output.Length - 1];
1308 Array.Copy (output, 0, values, 0, output.Length - 1);
1313 // ssize_t llistxattr (const char *path, char *list, size_t size);
1314 [DllImport (MPH, SetLastError=true,
1315 EntryPoint="Mono_Posix_Syscall_llistxattr")]
1316 public static extern long llistxattr (string path, byte[] list, ulong size);
1318 // Slight modification: returns 0 on success, negative on error
1319 public static long llistxattr (string path, Encoding encoding, out string [] values)
1322 long size = llistxattr (path, null, 0);
1324 values = new string [0];
1328 byte[] list = new byte [size];
1329 long ret = llistxattr (path, list, (ulong) size);
1333 string [] output = encoding.GetString (list).Split((char) 0);
1334 values = new string [output.Length - 1];
1335 Array.Copy (output, 0, values, 0, output.Length - 1);
1340 // ssize_t flistxattr (int fd, char *list, size_t size);
1341 [DllImport (MPH, SetLastError=true,
1342 EntryPoint="Mono_Posix_Syscall_flistxattr")]
1343 public static extern long flistxattr (int fd, byte[] list, ulong size);
1345 // Slight modification: returns 0 on success, negative on error
1346 public static long flistxattr (int fd, Encoding encoding, out string [] values)
1349 long size = flistxattr (fd, null, 0);
1351 values = new string [0];
1355 byte[] list = new byte [size];
1356 long ret = flistxattr (fd, list, (ulong) size);
1360 string [] output = encoding.GetString (list).Split((char) 0);
1361 values = new string [output.Length - 1];
1362 Array.Copy (output, 0, values, 0, output.Length - 1);
1366 [DllImport (MPH, SetLastError=true,
1367 EntryPoint="Mono_Posix_Syscall_removexattr")]
1368 public static extern int removexattr (string path, string name);
1370 [DllImport (MPH, SetLastError=true,
1371 EntryPoint="Mono_Posix_Syscall_lremovexattr")]
1372 public static extern int lremovexattr (string path, string name);
1374 [DllImport (MPH, SetLastError=true,
1375 EntryPoint="Mono_Posix_Syscall_fremovexattr")]
1376 public static extern int fremovexattr (int fd, string name);
1379 #region <dirent.h> Declarations
1383 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1385 [DllImport (LIBC, SetLastError=true)]
1386 public static extern IntPtr opendir (
1387 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1390 [DllImport (LIBC, SetLastError=true)]
1391 public static extern int closedir (IntPtr dir);
1394 // void seekdir (DIR *dir, off_t offset);
1395 // Slight modification. Returns -1 on error, 0 on success.
1396 [DllImport (MPH, SetLastError=true,
1397 EntryPoint="Mono_Posix_Syscall_seekdir")]
1398 public static extern int seekdir (IntPtr dir, long offset);
1401 // off_t telldir(DIR *dir);
1402 [DllImport (MPH, SetLastError=true,
1403 EntryPoint="Mono_Posix_Syscall_telldir")]
1404 public static extern long telldir (IntPtr dir);
1406 [DllImport (MPH, SetLastError=true,
1407 EntryPoint="Mono_Posix_Syscall_rewinddir")]
1408 public static extern int rewinddir (IntPtr dir);
1410 private struct _Dirent {
1411 public /* ino_t */ ulong d_ino;
1412 public /* off_t */ long d_off;
1413 public ushort d_reclen;
1415 public IntPtr d_name;
1418 private static void CopyDirent (Dirent to, ref _Dirent from)
1421 to.d_ino = from.d_ino;
1422 to.d_off = from.d_off;
1423 to.d_reclen = from.d_reclen;
1424 to.d_type = from.d_type;
1425 to.d_name = UnixMarshal.PtrToString (from.d_name);
1428 Stdlib.free (from.d_name);
1429 from.d_name = IntPtr.Zero;
1433 [DllImport (MPH, SetLastError=true,
1434 EntryPoint="Mono_Posix_Syscall_readdir")]
1435 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
1437 public static Dirent readdir (IntPtr dir)
1440 int r = sys_readdir (dir, out dentry);
1443 Dirent d = new Dirent ();
1444 CopyDirent (d, ref dentry);
1448 [DllImport (MPH, SetLastError=true,
1449 EntryPoint="Mono_Posix_Syscall_readdir_r")]
1450 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
1452 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
1458 entry.d_name = null;
1461 int r = sys_readdir_r (dirp, out _d, out result);
1463 if (r == 0 && result != IntPtr.Zero) {
1464 CopyDirent (entry, ref _d);
1470 [DllImport (LIBC, SetLastError=true)]
1471 public static extern int dirfd (IntPtr dir);
1474 #region <fcntl.h> Declarations
1476 // <fcntl.h> -- COMPLETE
1479 [DllImport (MPH, SetLastError=true,
1480 EntryPoint="Mono_Posix_Syscall_fcntl")]
1481 public static extern int fcntl (int fd, FcntlCommand cmd);
1483 [DllImport (MPH, SetLastError=true,
1484 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
1485 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
1487 public static int fcntl (int fd, FcntlCommand cmd, DirectoryNotifyFlags arg)
1489 if (cmd != FcntlCommand.F_NOTIFY) {
1490 SetLastError (Errno.EINVAL);
1493 long _arg = NativeConvert.FromDirectoryNotifyFlags (arg);
1494 return fcntl (fd, FcntlCommand.F_NOTIFY, _arg);
1497 [DllImport (MPH, SetLastError=true,
1498 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
1499 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
1501 [DllImport (MPH, SetLastError=true,
1502 EntryPoint="Mono_Posix_Syscall_open")]
1503 public static extern int open (
1504 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1505 string pathname, OpenFlags flags);
1508 // int open(const char *pathname, int flags, mode_t mode);
1509 [DllImport (MPH, SetLastError=true,
1510 EntryPoint="Mono_Posix_Syscall_open_mode")]
1511 public static extern int open (
1512 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1513 string pathname, OpenFlags flags, FilePermissions mode);
1516 // int creat(const char *pathname, mode_t mode);
1517 [DllImport (MPH, SetLastError=true,
1518 EntryPoint="Mono_Posix_Syscall_creat")]
1519 public static extern int creat (
1520 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1521 string pathname, FilePermissions mode);
1524 // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
1525 [DllImport (MPH, SetLastError=true,
1526 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
1527 public static extern int posix_fadvise (int fd, long offset,
1528 long len, PosixFadviseAdvice advice);
1530 // posix_fallocate(P)
1531 // int posix_fallocate(int fd, off_t offset, size_t len);
1532 [DllImport (MPH, SetLastError=true,
1533 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
1534 public static extern int posix_fallocate (int fd, long offset, ulong len);
1537 #region <fstab.h> Declarations
1539 // <fstab.h> -- COMPLETE
1541 private struct _Fstab {
1542 public IntPtr fs_spec;
1543 public IntPtr fs_file;
1544 public IntPtr fs_vfstype;
1545 public IntPtr fs_mntops;
1546 public IntPtr fs_type;
1548 public int fs_passno;
1549 public IntPtr _fs_buf_;
1552 private static void CopyFstab (Fstab to, ref _Fstab from)
1555 to.fs_spec = UnixMarshal.PtrToString (from.fs_spec);
1556 to.fs_file = UnixMarshal.PtrToString (from.fs_file);
1557 to.fs_vfstype = UnixMarshal.PtrToString (from.fs_vfstype);
1558 to.fs_mntops = UnixMarshal.PtrToString (from.fs_mntops);
1559 to.fs_type = UnixMarshal.PtrToString (from.fs_type);
1560 to.fs_freq = from.fs_freq;
1561 to.fs_passno = from.fs_passno;
1564 Stdlib.free (from._fs_buf_);
1565 from._fs_buf_ = IntPtr.Zero;
1569 internal static object fstab_lock = new object ();
1571 [DllImport (MPH, SetLastError=true,
1572 EntryPoint="Mono_Posix_Syscall_endfsent")]
1573 private static extern int sys_endfsent ();
1575 public static int endfsent ()
1578 return sys_endfsent ();
1582 [DllImport (MPH, SetLastError=true,
1583 EntryPoint="Mono_Posix_Syscall_getfsent")]
1584 private static extern int sys_getfsent (out _Fstab fs);
1586 public static Fstab getfsent ()
1591 r = sys_getfsent (out fsbuf);
1595 Fstab fs = new Fstab ();
1596 CopyFstab (fs, ref fsbuf);
1600 [DllImport (MPH, SetLastError=true,
1601 EntryPoint="Mono_Posix_Syscall_getfsfile")]
1602 private static extern int sys_getfsfile (
1603 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1604 string mount_point, out _Fstab fs);
1606 public static Fstab getfsfile (string mount_point)
1611 r = sys_getfsfile (mount_point, out fsbuf);
1615 Fstab fs = new Fstab ();
1616 CopyFstab (fs, ref fsbuf);
1620 [DllImport (MPH, SetLastError=true,
1621 EntryPoint="Mono_Posix_Syscall_getfsspec")]
1622 private static extern int sys_getfsspec (
1623 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1624 string special_file, out _Fstab fs);
1626 public static Fstab getfsspec (string special_file)
1631 r = sys_getfsspec (special_file, out fsbuf);
1635 Fstab fs = new Fstab ();
1636 CopyFstab (fs, ref fsbuf);
1640 [DllImport (MPH, SetLastError=true,
1641 EntryPoint="Mono_Posix_Syscall_setfsent")]
1642 private static extern int sys_setfsent ();
1644 public static int setfsent ()
1647 return sys_setfsent ();
1653 #region <grp.h> Declarations
1657 // TODO: putgrent(3), fgetgrent_r(), getgrouplist(2), initgroups(3)
1660 // int setgroups (size_t size, const gid_t *list);
1661 [DllImport (MPH, SetLastError=true,
1662 EntryPoint="Mono_Posix_Syscall_setgroups")]
1663 public static extern int setgroups (ulong size, uint[] list);
1665 public static int setgroups (uint [] list)
1667 return setgroups ((ulong) list.Length, list);
1670 private struct _Group
1672 public IntPtr gr_name;
1673 public IntPtr gr_passwd;
1674 public /* gid_t */ uint gr_gid;
1675 public int _gr_nmem_;
1676 public IntPtr gr_mem;
1677 public IntPtr _gr_buf_;
1680 private static void CopyGroup (Group to, ref _Group from)
1683 to.gr_gid = from.gr_gid;
1684 to.gr_name = UnixMarshal.PtrToString (from.gr_name);
1685 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
1686 to.gr_mem = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
1689 Stdlib.free (from.gr_mem);
1690 Stdlib.free (from._gr_buf_);
1691 from.gr_mem = IntPtr.Zero;
1692 from._gr_buf_ = IntPtr.Zero;
1696 internal static object grp_lock = new object ();
1698 [DllImport (MPH, SetLastError=true,
1699 EntryPoint="Mono_Posix_Syscall_getgrnam")]
1700 private static extern int sys_getgrnam (string name, out _Group group);
1702 public static Group getgrnam (string name)
1707 r = sys_getgrnam (name, out group);
1711 Group gr = new Group ();
1712 CopyGroup (gr, ref group);
1717 // struct group *getgrgid(gid_t gid);
1718 [DllImport (MPH, SetLastError=true,
1719 EntryPoint="Mono_Posix_Syscall_getgrgid")]
1720 private static extern int sys_getgrgid (uint uid, out _Group group);
1722 public static Group getgrgid (uint uid)
1727 r = sys_getgrgid (uid, out group);
1731 Group gr = new Group ();
1732 CopyGroup (gr, ref group);
1736 [DllImport (MPH, SetLastError=true,
1737 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
1738 private static extern int sys_getgrnam_r (
1739 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1740 string name, out _Group grbuf, out IntPtr grbufp);
1742 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
1747 int r = sys_getgrnam_r (name, out group, out _grbufp);
1748 if (r == 0 && _grbufp != IntPtr.Zero) {
1749 CopyGroup (grbuf, ref group);
1756 // int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
1757 // size_t buflen, struct group **gbufp);
1758 [DllImport (MPH, SetLastError=true,
1759 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
1760 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
1762 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
1767 int r = sys_getgrgid_r (uid, out group, out _grbufp);
1768 if (r == 0 && _grbufp != IntPtr.Zero) {
1769 CopyGroup (grbuf, ref group);
1775 [DllImport (MPH, SetLastError=true,
1776 EntryPoint="Mono_Posix_Syscall_getgrent")]
1777 private static extern int sys_getgrent (out _Group grbuf);
1779 public static Group getgrent ()
1784 r = sys_getgrent (out group);
1788 Group gr = new Group();
1789 CopyGroup (gr, ref group);
1793 [DllImport (MPH, SetLastError=true,
1794 EntryPoint="Mono_Posix_Syscall_setgrent")]
1795 private static extern int sys_setgrent ();
1797 public static int setgrent ()
1800 return sys_setgrent ();
1804 [DllImport (MPH, SetLastError=true,
1805 EntryPoint="Mono_Posix_Syscall_endgrent")]
1806 private static extern int sys_endgrent ();
1808 public static int endgrent ()
1811 return sys_endgrent ();
1815 [DllImport (MPH, SetLastError=true,
1816 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
1817 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
1819 public static Group fgetgrent (IntPtr stream)
1824 r = sys_fgetgrent (stream, out group);
1828 Group gr = new Group ();
1829 CopyGroup (gr, ref group);
1834 #region <pwd.h> Declarations
1838 // TODO: putpwent(3), fgetpwent_r()
1840 // SKIPPING: getpw(3): it's dangerous. Use getpwuid(3) instead.
1842 private struct _Passwd
1844 public IntPtr pw_name;
1845 public IntPtr pw_passwd;
1846 public /* uid_t */ uint pw_uid;
1847 public /* gid_t */ uint pw_gid;
1848 public IntPtr pw_gecos;
1849 public IntPtr pw_dir;
1850 public IntPtr pw_shell;
1851 public IntPtr _pw_buf_;
1854 private static void CopyPasswd (Passwd to, ref _Passwd from)
1857 to.pw_name = UnixMarshal.PtrToString (from.pw_name);
1858 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
1859 to.pw_uid = from.pw_uid;
1860 to.pw_gid = from.pw_gid;
1861 to.pw_gecos = UnixMarshal.PtrToString (from.pw_gecos);
1862 to.pw_dir = UnixMarshal.PtrToString (from.pw_dir);
1863 to.pw_shell = UnixMarshal.PtrToString (from.pw_shell);
1866 Stdlib.free (from._pw_buf_);
1867 from._pw_buf_ = IntPtr.Zero;
1871 internal static object pwd_lock = new object ();
1873 [DllImport (MPH, SetLastError=true,
1874 EntryPoint="Mono_Posix_Syscall_getpwnam")]
1875 private static extern int sys_getpwnam (string name, out _Passwd passwd);
1877 public static Passwd getpwnam (string name)
1882 r = sys_getpwnam (name, out passwd);
1886 Passwd pw = new Passwd ();
1887 CopyPasswd (pw, ref passwd);
1892 // struct passwd *getpwnuid(uid_t uid);
1893 [DllImport (MPH, SetLastError=true,
1894 EntryPoint="Mono_Posix_Syscall_getpwuid")]
1895 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
1897 public static Passwd getpwuid (uint uid)
1902 r = sys_getpwuid (uid, out passwd);
1906 Passwd pw = new Passwd ();
1907 CopyPasswd (pw, ref passwd);
1911 [DllImport (MPH, SetLastError=true,
1912 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
1913 private static extern int sys_getpwnam_r (
1914 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1915 string name, out _Passwd pwbuf, out IntPtr pwbufp);
1917 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
1922 int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
1923 if (r == 0 && _pwbufp != IntPtr.Zero) {
1924 CopyPasswd (pwbuf, ref passwd);
1931 // int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
1932 // buflen, struct passwd **pwbufp);
1933 [DllImport (MPH, SetLastError=true,
1934 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
1935 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
1937 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
1942 int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
1943 if (r == 0 && _pwbufp != IntPtr.Zero) {
1944 CopyPasswd (pwbuf, ref passwd);
1950 [DllImport (MPH, SetLastError=true,
1951 EntryPoint="Mono_Posix_Syscall_getpwent")]
1952 private static extern int sys_getpwent (out _Passwd pwbuf);
1954 public static Passwd getpwent ()
1959 r = sys_getpwent (out passwd);
1963 Passwd pw = new Passwd ();
1964 CopyPasswd (pw, ref passwd);
1968 [DllImport (MPH, SetLastError=true,
1969 EntryPoint="Mono_Posix_Syscall_setpwent")]
1970 private static extern int sys_setpwent ();
1972 public static int setpwent ()
1975 return sys_setpwent ();
1979 [DllImport (MPH, SetLastError=true,
1980 EntryPoint="Mono_Posix_Syscall_endpwent")]
1981 private static extern int sys_endpwent ();
1983 public static int endpwent ()
1986 return sys_endpwent ();
1990 [DllImport (MPH, SetLastError=true,
1991 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
1992 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
1994 public static Passwd fgetpwent (IntPtr stream)
1999 r = sys_fgetpwent (stream, out passwd);
2003 Passwd pw = new Passwd ();
2004 CopyPasswd (pw, ref passwd);
2009 #region <signal.h> Declarations
2013 [DllImport (MPH, SetLastError=true,
2014 EntryPoint="Mono_Posix_Syscall_psignal")]
2015 private static extern int psignal (int sig, string s);
2017 public static int psignal (Signum sig, string s)
2019 int signum = NativeConvert.FromSignum (sig);
2020 return psignal (signum, s);
2024 // int kill(pid_t pid, int sig);
2025 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
2026 private static extern int sys_kill (int pid, int sig);
2028 public static int kill (int pid, Signum sig)
2030 int _sig = NativeConvert.FromSignum (sig);
2031 return sys_kill (pid, _sig);
2034 private static object signal_lock = new object ();
2036 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
2037 private static extern IntPtr sys_strsignal (int sig);
2039 public static string strsignal (Signum sig)
2041 int s = NativeConvert.FromSignum (sig);
2042 lock (signal_lock) {
2043 IntPtr r = sys_strsignal (s);
2044 return UnixMarshal.PtrToString (r);
2048 // TODO: sigaction(2)
2049 // TODO: sigsuspend(2)
2050 // TODO: sigpending(2)
2054 #region <stdio.h> Declarations
2058 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
2059 private static extern int _L_ctermid ();
2061 public static readonly int L_ctermid = _L_ctermid ();
2063 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
2064 private static extern int _L_cuserid ();
2066 public static readonly int L_cuserid = _L_cuserid ();
2068 internal static object getlogin_lock = new object ();
2070 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
2071 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
2073 [Obsolete ("\"Nobody knows precisely what cuserid() does... " +
2074 "DO NOT USE cuserid().\n" +
2075 "`string' must hold L_cuserid characters. Use getlogin_r instead.")]
2076 public static string cuserid (StringBuilder @string)
2078 if (@string.Capacity < L_cuserid) {
2079 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
2081 lock (getlogin_lock) {
2082 IntPtr r = sys_cuserid (@string);
2083 return UnixMarshal.PtrToString (r);
2089 #region <stdlib.h> Declarations
2093 [DllImport (LIBC, SetLastError=true)]
2094 public static extern int mkstemp (StringBuilder template);
2096 [DllImport (LIBC, SetLastError=true)]
2097 public static extern int ttyslot ();
2099 [DllImport (MPH, SetLastError=true,
2100 EntryPoint="Mono_Posix_Syscall_setkey")]
2101 public static extern int setkey (string key);
2105 #region <string.h> Declarations
2111 // int strerror_r(int errnum, char *buf, size_t n);
2112 [DllImport (MPH, SetLastError=true,
2113 EntryPoint="Mono_Posix_Syscall_strerror_r")]
2114 private static extern int sys_strerror_r (int errnum,
2115 [Out] StringBuilder buf, ulong n);
2117 public static int strerror_r (Errno errnum, StringBuilder buf, ulong n)
2119 int e = NativeConvert.FromErrno (errnum);
2120 return sys_strerror_r (e, buf, n);
2123 public static int strerror_r (Errno errnum, StringBuilder buf)
2125 return strerror_r (errnum, buf, (ulong) buf.Capacity);
2130 #region <sys/mman.h> Declarations
2136 // int posix_madvise(void *addr, size_t len, int advice);
2137 [DllImport (MPH, SetLastError=true,
2138 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
2139 public static extern int posix_madvise (IntPtr addr, ulong len,
2140 PosixMadviseAdvice advice);
2142 public static readonly IntPtr MAP_FAILED = unchecked((IntPtr)(-1));
2144 [DllImport (MPH, SetLastError=true,
2145 EntryPoint="Mono_Posix_Syscall_mmap")]
2146 public static extern IntPtr mmap (IntPtr start, ulong length,
2147 MmapProts prot, MmapFlags flags, int fd, long offset);
2149 [DllImport (MPH, SetLastError=true,
2150 EntryPoint="Mono_Posix_Syscall_munmap")]
2151 public static extern int munmap (IntPtr start, ulong length);
2153 [DllImport (MPH, SetLastError=true,
2154 EntryPoint="Mono_Posix_Syscall_mprotect")]
2155 public static extern int mprotect (IntPtr start, ulong len, MmapProts prot);
2157 [DllImport (MPH, SetLastError=true,
2158 EntryPoint="Mono_Posix_Syscall_msync")]
2159 public static extern int msync (IntPtr start, ulong len, MsyncFlags flags);
2161 [DllImport (MPH, SetLastError=true,
2162 EntryPoint="Mono_Posix_Syscall_mlock")]
2163 public static extern int mlock (IntPtr start, ulong len);
2165 [DllImport (MPH, SetLastError=true,
2166 EntryPoint="Mono_Posix_Syscall_munlock")]
2167 public static extern int munlock (IntPtr start, ulong len);
2169 [DllImport (LIBC, SetLastError=true, EntryPoint="mlockall")]
2170 private static extern int sys_mlockall (int flags);
2172 public static int mlockall (MlockallFlags flags)
2174 int _flags = NativeConvert.FromMlockallFlags (flags);
2175 return sys_mlockall (_flags);
2178 [DllImport (LIBC, SetLastError=true)]
2179 public static extern int munlockall ();
2181 [DllImport (MPH, SetLastError=true,
2182 EntryPoint="Mono_Posix_Syscall_mremap")]
2183 public static extern IntPtr mremap (IntPtr old_address, ulong old_size,
2184 ulong new_size, MremapFlags flags);
2186 [DllImport (MPH, SetLastError=true,
2187 EntryPoint="Mono_Posix_Syscall_mincore")]
2188 public static extern int mincore (IntPtr start, ulong length, byte[] vec);
2190 [DllImport (MPH, SetLastError=true,
2191 EntryPoint="Mono_Posix_Syscall_remap_file_pages")]
2192 public static extern int remap_file_pages (IntPtr start, ulong size,
2193 MmapProts prot, long pgoff, MmapFlags flags);
2197 #region <sys/poll.h> Declarations
2199 // <sys/poll.h> -- COMPLETE
2202 private struct _pollfd {
2204 public short events;
2205 public short revents;
2208 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
2209 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
2211 public static int poll (Pollfd [] fds, uint nfds, int timeout)
2213 if (fds.Length < nfds)
2214 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
2216 _pollfd[] send = new _pollfd[nfds];
2218 for (int i = 0; i < send.Length; i++) {
2219 send [i].fd = fds [i].fd;
2220 send [i].events = NativeConvert.FromPollEvents (fds [i].events);
2223 int r = sys_poll (send, nfds, timeout);
2225 for (int i = 0; i < send.Length; i++) {
2226 fds [i].revents = NativeConvert.ToPollEvents (send [i].revents);
2232 public static int poll (Pollfd [] fds, int timeout)
2234 return poll (fds, (uint) fds.Length, timeout);
2247 // TODO: setrlimit(2)
2248 // TODO: getrlimit(2)
2249 // TODO: getrusage(2)
2253 #region <sys/sendfile.h> Declarations
2255 // <sys/sendfile.h> -- COMPLETE
2258 [DllImport (MPH, SetLastError=true,
2259 EntryPoint="Mono_Posix_Syscall_sendfile")]
2260 public static extern long sendfile (int out_fd, int in_fd,
2261 ref long offset, ulong count);
2265 #region <sys/stat.h> Declarations
2267 // <sys/stat.h> -- COMPLETE
2269 [DllImport (MPH, SetLastError=true,
2270 EntryPoint="Mono_Posix_Syscall_stat")]
2271 public static extern int stat (
2272 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2273 string file_name, out Stat buf);
2275 [DllImport (MPH, SetLastError=true,
2276 EntryPoint="Mono_Posix_Syscall_fstat")]
2277 public static extern int fstat (int filedes, out Stat buf);
2279 [DllImport (MPH, SetLastError=true,
2280 EntryPoint="Mono_Posix_Syscall_lstat")]
2281 public static extern int lstat (
2282 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2283 string file_name, out Stat buf);
2286 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
2287 // All take FilePermissions
2290 // int chmod(const char *path, mode_t mode);
2291 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
2292 private static extern int sys_chmod (
2293 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2294 string path, uint mode);
2296 public static int chmod (string path, FilePermissions mode)
2298 uint _mode = NativeConvert.FromFilePermissions (mode);
2299 return sys_chmod (path, _mode);
2303 // int chmod(int filedes, mode_t mode);
2304 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
2305 private static extern int sys_fchmod (int filedes, uint mode);
2307 public static int fchmod (int filedes, FilePermissions mode)
2309 uint _mode = NativeConvert.FromFilePermissions (mode);
2310 return sys_fchmod (filedes, _mode);
2314 // mode_t umask(mode_t mask);
2315 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
2316 private static extern uint sys_umask (uint mask);
2318 public static FilePermissions umask (FilePermissions mask)
2320 uint _mask = NativeConvert.FromFilePermissions (mask);
2321 uint r = sys_umask (_mask);
2322 return NativeConvert.ToFilePermissions (r);
2326 // int mkdir(const char *pathname, mode_t mode);
2327 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
2328 private static extern int sys_mkdir (
2329 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2330 string oldpath, uint mode);
2332 public static int mkdir (string oldpath, FilePermissions mode)
2334 uint _mode = NativeConvert.FromFilePermissions (mode);
2335 return sys_mkdir (oldpath, _mode);
2339 // int mknod (const char *pathname, mode_t mode, dev_t dev);
2340 [DllImport (MPH, SetLastError=true,
2341 EntryPoint="Mono_Posix_Syscall_mknod")]
2342 public static extern int mknod (
2343 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2344 string pathname, FilePermissions mode, ulong dev);
2347 // int mkfifo(const char *pathname, mode_t mode);
2348 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
2349 private static extern int sys_mkfifo (
2350 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2351 string pathname, uint mode);
2353 public static int mkfifo (string pathname, FilePermissions mode)
2355 uint _mode = NativeConvert.FromFilePermissions (mode);
2356 return sys_mkfifo (pathname, _mode);
2361 #region <sys/stat.h> Declarations
2366 [DllImport (MPH, SetLastError=true,
2367 EntryPoint="Mono_Posix_Syscall_statvfs")]
2368 public static extern int statvfs (
2369 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2370 string path, out Statvfs buf);
2372 [DllImport (MPH, SetLastError=true,
2373 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
2374 public static extern int fstatvfs (int fd, out Statvfs buf);
2378 #region <sys/time.h> Declarations
2382 // TODO: adjtime(), getitimer(2), setitimer(2), lutimes(), futimes()
2384 [DllImport (MPH, SetLastError=true,
2385 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2386 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
2388 [DllImport (MPH, SetLastError=true,
2389 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2390 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
2392 public static int gettimeofday (out Timeval tv)
2394 return gettimeofday (out tv, IntPtr.Zero);
2397 [DllImport (MPH, SetLastError=true,
2398 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2399 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
2401 public static int gettimeofday (out Timezone tz)
2403 return gettimeofday (IntPtr.Zero, out tz);
2406 [DllImport (MPH, SetLastError=true,
2407 EntryPoint="Mono_Posix_Syscall_settimeofday")]
2408 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
2410 [DllImport (MPH, SetLastError=true,
2411 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2412 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
2414 public static int settimeofday (ref Timeval tv)
2416 return settimeofday (ref tv, IntPtr.Zero);
2419 [DllImport (MPH, SetLastError=true,
2420 EntryPoint="Mono_Posix_Syscall_utimes")]
2421 public static extern int utimes (
2422 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2423 string filename, ref Timeval tvp);
2445 #region <sys/wait.h> Declarations
2451 // pid_t wait(int *status);
2452 [DllImport (LIBC, SetLastError=true)]
2453 public static extern int wait (out int status);
2456 // pid_t waitpid(pid_t pid, int *status, int options);
2457 [DllImport (LIBC, SetLastError=true)]
2458 private static extern int waitpid (int pid, out int status, int options);
2460 public static int waitpid (int pid, out int status, WaitOptions options)
2462 int _options = NativeConvert.FromWaitOptions (options);
2463 return waitpid (pid, out status, _options);
2466 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
2467 private static extern int _WIFEXITED (int status);
2469 public static bool WIFEXITED (int status)
2471 return _WIFEXITED (status) != 0;
2474 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
2475 public static extern int WEXITSTATUS (int status);
2477 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
2478 private static extern int _WIFSIGNALED (int status);
2480 public static bool WIFSIGNALED (int status)
2482 return _WIFSIGNALED (status) != 0;
2485 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
2486 private static extern int _WTERMSIG (int status);
2488 public static Signum WTERMSIG (int status)
2490 int r = _WTERMSIG (status);
2491 return NativeConvert.ToSignum (r);
2494 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
2495 private static extern int _WIFSTOPPED (int status);
2497 public static bool WIFSTOPPED (int status)
2499 return _WIFSTOPPED (status) != 0;
2502 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
2503 private static extern int _WSTOPSIG (int status);
2505 public static Signum WSTOPSIG (int status)
2507 int r = _WSTOPSIG (status);
2508 return NativeConvert.ToSignum (r);
2517 #region <syslog.h> Declarations
2522 [DllImport (MPH, SetLastError=true,
2523 EntryPoint="Mono_Posix_Syscall_openlog")]
2524 private static extern int sys_openlog (IntPtr ident, int option, int facility);
2526 public static int openlog (IntPtr ident, SyslogOptions option,
2527 SyslogFacility defaultFacility)
2529 int _option = NativeConvert.FromSyslogOptions (option);
2530 int _facility = NativeConvert.FromSyslogFacility (defaultFacility);
2532 return sys_openlog (ident, _option, _facility);
2535 [DllImport (MPH, SetLastError=true,
2536 EntryPoint="Mono_Posix_Syscall_syslog")]
2537 private static extern int sys_syslog (int priority, string message);
2539 public static int syslog (SyslogFacility facility, SyslogLevel level, string message)
2541 int _facility = NativeConvert.FromSyslogFacility (facility);
2542 int _level = NativeConvert.FromSyslogLevel (level);
2543 return sys_syslog (_facility | _level, GetSyslogMessage (message));
2546 public static int syslog (SyslogLevel level, string message)
2548 int _level = NativeConvert.FromSyslogLevel (level);
2549 return sys_syslog (_level, GetSyslogMessage (message));
2552 private static string GetSyslogMessage (string message)
2554 return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
2557 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2558 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
2559 public static int syslog (SyslogFacility facility, SyslogLevel level,
2560 string format, params object[] parameters)
2562 int _facility = NativeConvert.FromSyslogFacility (facility);
2563 int _level = NativeConvert.FromSyslogLevel (level);
2565 object[] _parameters = new object[checked(parameters.Length+2)];
2566 _parameters [0] = _facility | _level;
2567 _parameters [1] = format;
2568 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2569 return (int) XPrintfFunctions.syslog (_parameters);
2572 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2573 "Use syslog(SyslogLevel, string) instead.")]
2574 public static int syslog (SyslogLevel level, string format,
2575 params object[] parameters)
2577 int _level = NativeConvert.FromSyslogLevel (level);
2579 object[] _parameters = new object[checked(parameters.Length+2)];
2580 _parameters [0] = _level;
2581 _parameters [1] = format;
2582 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2583 return (int) XPrintfFunctions.syslog (_parameters);
2586 [DllImport (MPH, SetLastError=true,
2587 EntryPoint="Mono_Posix_Syscall_closelog")]
2588 public static extern int closelog ();
2590 [DllImport (LIBC, SetLastError=true, EntryPoint="setlogmask")]
2591 private static extern int sys_setlogmask (int mask);
2593 public static int setlogmask (SyslogLevel mask)
2595 int _mask = NativeConvert.FromSyslogLevel (mask);
2596 return sys_setlogmask (_mask);
2601 #region <time.h> Declarations
2608 // int stime(time_t *t);
2609 [DllImport (MPH, SetLastError=true,
2610 EntryPoint="Mono_Posix_Syscall_stime")]
2611 public static extern int stime (ref long t);
2614 // time_t time(time_t *t);
2615 [DllImport (MPH, SetLastError=true,
2616 EntryPoint="Mono_Posix_Syscall_time")]
2617 public static extern long time (out long t);
2627 #region <unistd.h> Declarations
2631 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
2632 // other TODOs listed below.
2634 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
2635 private static extern int sys_access (
2636 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2637 string pathname, int mode);
2639 public static int access (string pathname, AccessModes mode)
2641 int _mode = NativeConvert.FromAccessModes (mode);
2642 return sys_access (pathname, _mode);
2646 // off_t lseek(int filedes, off_t offset, int whence);
2647 [DllImport (MPH, SetLastError=true,
2648 EntryPoint="Mono_Posix_Syscall_lseek")]
2649 private static extern long sys_lseek (int fd, long offset, int whence);
2651 public static long lseek (int fd, long offset, SeekFlags whence)
2653 short _whence = NativeConvert.FromSeekFlags (whence);
2654 return sys_lseek (fd, offset, _whence);
2657 [DllImport (LIBC, SetLastError=true)]
2658 public static extern int close (int fd);
2661 // ssize_t read(int fd, void *buf, size_t count);
2662 [DllImport (MPH, SetLastError=true,
2663 EntryPoint="Mono_Posix_Syscall_read")]
2664 public static extern long read (int fd, IntPtr buf, ulong count);
2666 public static unsafe long read (int fd, void *buf, ulong count)
2668 return read (fd, (IntPtr) buf, count);
2672 // ssize_t write(int fd, const void *buf, size_t count);
2673 [DllImport (MPH, SetLastError=true,
2674 EntryPoint="Mono_Posix_Syscall_write")]
2675 public static extern long write (int fd, IntPtr buf, ulong count);
2677 public static unsafe long write (int fd, void *buf, ulong count)
2679 return write (fd, (IntPtr) buf, count);
2683 // ssize_t pread(int fd, void *buf, size_t count, off_t offset);
2684 [DllImport (MPH, SetLastError=true,
2685 EntryPoint="Mono_Posix_Syscall_pread")]
2686 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
2688 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
2690 return pread (fd, (IntPtr) buf, count, offset);
2694 // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
2695 [DllImport (MPH, SetLastError=true,
2696 EntryPoint="Mono_Posix_Syscall_pwrite")]
2697 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
2699 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
2701 return pwrite (fd, (IntPtr) buf, count, offset);
2704 [DllImport (MPH, SetLastError=true,
2705 EntryPoint="Mono_Posix_Syscall_pipe")]
2706 public static extern int pipe (out int reading, out int writing);
2708 public static int pipe (int[] filedes)
2710 if (filedes == null || filedes.Length != 2) {
2714 int reading, writing;
2715 int r = pipe (out reading, out writing);
2716 filedes[0] = reading;
2717 filedes[1] = writing;
2721 [DllImport (LIBC, SetLastError=true)]
2722 public static extern uint alarm (uint seconds);
2724 [DllImport (LIBC, SetLastError=true)]
2725 public static extern uint sleep (uint seconds);
2727 [DllImport (LIBC, SetLastError=true)]
2728 public static extern uint ualarm (uint usecs, uint interval);
2730 [DllImport (LIBC, SetLastError=true)]
2731 public static extern int pause ();
2734 // int chown(const char *path, uid_t owner, gid_t group);
2735 [DllImport (LIBC, SetLastError=true)]
2736 public static extern int chown (
2737 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2738 string path, uint owner, uint group);
2741 // int fchown(int fd, uid_t owner, gid_t group);
2742 [DllImport (LIBC, SetLastError=true)]
2743 public static extern int fchown (int fd, uint owner, uint group);
2746 // int lchown(const char *path, uid_t owner, gid_t group);
2747 [DllImport (LIBC, SetLastError=true)]
2748 public static extern int lchown (
2749 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2750 string path, uint owner, uint group);
2752 [DllImport (LIBC, SetLastError=true)]
2753 public static extern int chdir (
2754 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2757 [DllImport (LIBC, SetLastError=true)]
2758 public static extern int fchdir (int fd);
2761 // char *getcwd(char *buf, size_t size);
2762 [DllImport (MPH, SetLastError=true,
2763 EntryPoint="Mono_Posix_Syscall_getcwd")]
2764 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
2766 public static StringBuilder getcwd (StringBuilder buf)
2768 getcwd (buf, (ulong) buf.Capacity);
2772 // getwd(2) is deprecated; don't expose it.
2774 [DllImport (LIBC, SetLastError=true)]
2775 public static extern int dup (int fd);
2777 [DllImport (LIBC, SetLastError=true)]
2778 public static extern int dup2 (int fd, int fd2);
2780 // TODO: does Mono marshal arrays properly?
2781 [DllImport (LIBC, SetLastError=true)]
2782 public static extern int execve (
2783 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2784 string path, string[] argv, string[] envp);
2786 [DllImport (LIBC, SetLastError=true)]
2787 public static extern int fexecve (int fd, string[] argv, string[] envp);
2789 [DllImport (LIBC, SetLastError=true)]
2790 public static extern int execv (
2791 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2792 string path, string[] argv);
2794 // TODO: execle, execl, execlp
2795 [DllImport (LIBC, SetLastError=true)]
2796 public static extern int execvp (
2797 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2798 string path, string[] argv);
2800 [DllImport (LIBC, SetLastError=true)]
2801 public static extern int nice (int inc);
2803 [DllImport (LIBC, SetLastError=true)]
2804 [CLSCompliant (false)]
2805 public static extern int _exit (int status);
2807 [DllImport (MPH, SetLastError=true,
2808 EntryPoint="Mono_Posix_Syscall_fpathconf")]
2809 public static extern long fpathconf (int filedes, PathconfName name, Errno defaultError);
2811 public static long fpathconf (int filedes, PathconfName name)
2813 return fpathconf (filedes, name, (Errno) 0);
2816 [DllImport (MPH, SetLastError=true,
2817 EntryPoint="Mono_Posix_Syscall_pathconf")]
2818 public static extern long pathconf (
2819 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2820 string path, PathconfName name, Errno defaultError);
2822 public static long pathconf (string path, PathconfName name)
2824 return pathconf (path, name, (Errno) 0);
2827 [DllImport (MPH, SetLastError=true,
2828 EntryPoint="Mono_Posix_Syscall_sysconf")]
2829 public static extern long sysconf (SysconfName name, Errno defaultError);
2831 public static long sysconf (SysconfName name)
2833 return sysconf (name, (Errno) 0);
2837 // size_t confstr(int name, char *buf, size_t len);
2838 [DllImport (MPH, SetLastError=true,
2839 EntryPoint="Mono_Posix_Syscall_confstr")]
2840 public static extern ulong confstr (ConfstrName name, [Out] StringBuilder buf, ulong len);
2843 // pid_t getpid(void);
2844 [DllImport (LIBC, SetLastError=true)]
2845 public static extern int getpid ();
2848 // pid_t getppid(void);
2849 [DllImport (LIBC, SetLastError=true)]
2850 public static extern int getppid ();
2853 // int setpgid(pid_t pid, pid_t pgid);
2854 [DllImport (LIBC, SetLastError=true)]
2855 public static extern int setpgid (int pid, int pgid);
2858 // pid_t getpgid(pid_t pid);
2859 [DllImport (LIBC, SetLastError=true)]
2860 public static extern int getpgid (int pid);
2862 [DllImport (LIBC, SetLastError=true)]
2863 public static extern int setpgrp ();
2866 // pid_t getpgrp(void);
2867 [DllImport (LIBC, SetLastError=true)]
2868 public static extern int getpgrp ();
2871 // pid_t setsid(void);
2872 [DllImport (LIBC, SetLastError=true)]
2873 public static extern int setsid ();
2876 // pid_t getsid(pid_t pid);
2877 [DllImport (LIBC, SetLastError=true)]
2878 public static extern int getsid (int pid);
2881 // uid_t getuid(void);
2882 [DllImport (LIBC, SetLastError=true)]
2883 public static extern uint getuid ();
2886 // uid_t geteuid(void);
2887 [DllImport (LIBC, SetLastError=true)]
2888 public static extern uint geteuid ();
2891 // gid_t getgid(void);
2892 [DllImport (LIBC, SetLastError=true)]
2893 public static extern uint getgid ();
2896 // gid_t getgid(void);
2897 [DllImport (LIBC, SetLastError=true)]
2898 public static extern uint getegid ();
2901 // int getgroups(int size, gid_t list[]);
2902 [DllImport (LIBC, SetLastError=true)]
2903 public static extern int getgroups (int size, uint[] list);
2905 public static int getgroups (uint[] list)
2907 return getgroups (list.Length, list);
2911 // int setuid(uid_t uid);
2912 [DllImport (LIBC, SetLastError=true)]
2913 public static extern int setuid (uint uid);
2916 // int setreuid(uid_t ruid, uid_t euid);
2917 [DllImport (LIBC, SetLastError=true)]
2918 public static extern int setreuid (uint ruid, uint euid);
2921 // int setregid(gid_t ruid, gid_t euid);
2922 [DllImport (LIBC, SetLastError=true)]
2923 public static extern int setregid (uint rgid, uint egid);
2926 // int seteuid(uid_t euid);
2927 [DllImport (LIBC, SetLastError=true)]
2928 public static extern int seteuid (uint euid);
2931 // int setegid(gid_t euid);
2932 [DllImport (LIBC, SetLastError=true)]
2933 public static extern int setegid (uint uid);
2936 // int setgid(gid_t gid);
2937 [DllImport (LIBC, SetLastError=true)]
2938 public static extern int setgid (uint gid);
2941 // int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
2942 [DllImport (LIBC, SetLastError=true)]
2943 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
2946 // int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
2947 [DllImport (LIBC, SetLastError=true)]
2948 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
2951 // int setresuid(uid_t ruid, uid_t euid, uid_t suid);
2952 [DllImport (LIBC, SetLastError=true)]
2953 public static extern int setresuid (uint ruid, uint euid, uint suid);
2956 // int setresgid(gid_t ruid, gid_t euid, gid_t suid);
2957 [DllImport (LIBC, SetLastError=true)]
2958 public static extern int setresgid (uint rgid, uint egid, uint sgid);
2962 // pid_t fork(void);
2963 [DllImport (LIBC, SetLastError=true)]
2964 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " +
2965 "shutdown code.\nUse System.Diagnostics.Process instead")]
2966 private static extern int fork ();
2969 // pid_t vfork(void);
2970 [DllImport (LIBC, SetLastError=true)]
2971 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " +
2972 "shutdown code.\nUse System.Diagnostics.Process instead")]
2973 private static extern int vfork ();
2976 private static object tty_lock = new object ();
2978 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
2979 private static extern IntPtr sys_ttyname (int fd);
2981 public static string ttyname (int fd)
2984 IntPtr r = sys_ttyname (fd);
2985 return UnixMarshal.PtrToString (r);
2990 // int ttyname_r(int fd, char *buf, size_t buflen);
2991 [DllImport (MPH, SetLastError=true,
2992 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
2993 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
2995 public static int ttyname_r (int fd, StringBuilder buf)
2997 return ttyname_r (fd, buf, (ulong) buf.Capacity);
3000 [DllImport (LIBC, EntryPoint="isatty")]
3001 private static extern int sys_isatty (int fd);
3003 public static bool isatty (int fd)
3005 return sys_isatty (fd) == 1;
3008 [DllImport (LIBC, SetLastError=true)]
3009 public static extern int link (
3010 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3012 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3015 [DllImport (LIBC, SetLastError=true)]
3016 public static extern int symlink (
3017 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3019 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3023 // int readlink(const char *path, char *buf, size_t bufsize);
3024 [DllImport (MPH, SetLastError=true,
3025 EntryPoint="Mono_Posix_Syscall_readlink")]
3026 public static extern int readlink (
3027 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3028 string path, [Out] StringBuilder buf, ulong bufsiz);
3030 public static int readlink (string path, [Out] StringBuilder buf)
3032 return readlink (path, buf, (ulong) buf.Capacity);
3035 [DllImport (LIBC, SetLastError=true)]
3036 public static extern int unlink (
3037 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3040 [DllImport (LIBC, SetLastError=true)]
3041 public static extern int rmdir (
3042 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3046 // pid_t tcgetpgrp(int fd);
3047 [DllImport (LIBC, SetLastError=true)]
3048 public static extern int tcgetpgrp (int fd);
3051 // int tcsetpgrp(int fd, pid_t pgrp);
3052 [DllImport (LIBC, SetLastError=true)]
3053 public static extern int tcsetpgrp (int fd, int pgrp);
3055 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
3056 private static extern IntPtr sys_getlogin ();
3058 public static string getlogin ()
3060 lock (getlogin_lock) {
3061 IntPtr r = sys_getlogin ();
3062 return UnixMarshal.PtrToString (r);
3067 // int getlogin_r(char *buf, size_t bufsize);
3068 [DllImport (MPH, SetLastError=true,
3069 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
3070 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
3072 public static int getlogin_r (StringBuilder name)
3074 return getlogin_r (name, (ulong) name.Capacity);
3077 [DllImport (LIBC, SetLastError=true)]
3078 public static extern int setlogin (string name);
3081 // int gethostname(char *name, size_t len);
3082 [DllImport (MPH, SetLastError=true,
3083 EntryPoint="Mono_Posix_Syscall_gethostname")]
3084 public static extern int gethostname ([Out] StringBuilder name, ulong len);
3086 public static int gethostname (StringBuilder name)
3088 return gethostname (name, (ulong) name.Capacity);
3092 // int gethostname(const char *name, size_t len);
3093 [DllImport (MPH, SetLastError=true,
3094 EntryPoint="Mono_Posix_Syscall_sethostname")]
3095 public static extern int sethostname (string name, ulong len);
3097 public static int sethostname (string name)
3099 return sethostname (name, (ulong) name.Length);
3102 [DllImport (MPH, SetLastError=true,
3103 EntryPoint="Mono_Posix_Syscall_gethostid")]
3104 public static extern long gethostid ();
3106 [DllImport (MPH, SetLastError=true,
3107 EntryPoint="Mono_Posix_Syscall_sethostid")]
3108 public static extern int sethostid (long hostid);
3111 // int getdomainname(char *name, size_t len);
3112 [DllImport (MPH, SetLastError=true,
3113 EntryPoint="Mono_Posix_Syscall_getdomainname")]
3114 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
3116 public static int getdomainname (StringBuilder name)
3118 return getdomainname (name, (ulong) name.Capacity);
3122 // int setdomainname(const char *name, size_t len);
3123 [DllImport (MPH, SetLastError=true,
3124 EntryPoint="Mono_Posix_Syscall_setdomainname")]
3125 public static extern int setdomainname (string name, ulong len);
3127 public static int setdomainname (string name)
3129 return setdomainname (name, (ulong) name.Length);
3132 [DllImport (LIBC, SetLastError=true)]
3133 public static extern int vhangup ();
3135 // Revoke doesn't appear to be POSIX. Include it?
3136 [DllImport (LIBC, SetLastError=true)]
3137 public static extern int revoke (
3138 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3141 // TODO: profil? It's not POSIX.
3143 [DllImport (LIBC, SetLastError=true)]
3144 public static extern int acct (
3145 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3148 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
3149 private static extern IntPtr sys_getusershell ();
3151 internal static object usershell_lock = new object ();
3153 public static string getusershell ()
3155 lock (usershell_lock) {
3156 IntPtr r = sys_getusershell ();
3157 return UnixMarshal.PtrToString (r);
3161 [DllImport (MPH, SetLastError=true,
3162 EntryPoint="Mono_Posix_Syscall_setusershell")]
3163 private static extern int sys_setusershell ();
3165 public static int setusershell ()
3167 lock (usershell_lock) {
3168 return sys_setusershell ();
3172 [DllImport (MPH, SetLastError=true,
3173 EntryPoint="Mono_Posix_Syscall_endusershell")]
3174 private static extern int sys_endusershell ();
3176 public static int endusershell ()
3178 lock (usershell_lock) {
3179 return sys_endusershell ();
3184 [DllImport (LIBC, SetLastError=true)]
3185 private static extern int daemon (int nochdir, int noclose);
3187 // this implicitly forks, and thus isn't safe.
3188 private static int daemon (bool nochdir, bool noclose)
3190 return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
3194 [DllImport (LIBC, SetLastError=true)]
3195 public static extern int chroot (
3196 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3199 // skipping getpass(3) as the man page states:
3200 // This function is obsolete. Do not use it.
3202 [DllImport (LIBC, SetLastError=true)]
3203 public static extern int fsync (int fd);
3205 [DllImport (LIBC, SetLastError=true)]
3206 public static extern int fdatasync (int fd);
3208 [DllImport (MPH, SetLastError=true,
3209 EntryPoint="Mono_Posix_Syscall_sync")]
3210 public static extern int sync ();
3212 [DllImport (LIBC, SetLastError=true)]
3213 [Obsolete ("Dropped in POSIX 1003.1-2001. " +
3214 "Use Syscall.sysconf (SysconfName._SC_PAGESIZE).")]
3215 public static extern int getpagesize ();
3218 // int truncate(const char *path, off_t length);
3219 [DllImport (MPH, SetLastError=true,
3220 EntryPoint="Mono_Posix_Syscall_truncate")]
3221 public static extern int truncate (
3222 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3223 string path, long length);
3226 // int ftruncate(int fd, off_t length);
3227 [DllImport (MPH, SetLastError=true,
3228 EntryPoint="Mono_Posix_Syscall_ftruncate")]
3229 public static extern int ftruncate (int fd, long length);
3231 [DllImport (LIBC, SetLastError=true)]
3232 public static extern int getdtablesize ();
3234 [DllImport (LIBC, SetLastError=true)]
3235 public static extern int brk (IntPtr end_data_segment);
3237 [DllImport (LIBC, SetLastError=true)]
3238 public static extern IntPtr sbrk (IntPtr increment);
3240 // TODO: syscall(2)?
3241 // Probably safer to skip entirely.
3244 // int lockf(int fd, int cmd, off_t len);
3245 [DllImport (MPH, SetLastError=true,
3246 EntryPoint="Mono_Posix_Syscall_lockf")]
3247 public static extern int lockf (int fd, LockfCommand cmd, long len);
3249 internal static object crypt_lock = new object ();
3251 [DllImport (CRYPT, SetLastError=true, EntryPoint="crypt")]
3252 private static extern IntPtr sys_crypt (string key, string salt);
3254 public static string crypt (string key, string salt)
3257 IntPtr r = sys_crypt (key, salt);
3258 return UnixMarshal.PtrToString (r);
3262 internal static object encrypt_lock = new object ();
3264 [DllImport (MPH, SetLastError=true,
3265 EntryPoint="Mono_Posix_Syscall_encrypt")]
3266 private static extern int sys_encrypt ([In, Out] byte[] block, int edflag);
3268 public static int encrypt (byte[] block, bool decode)
3270 if (block.Length < 64)
3271 throw new ArgumentOutOfRangeException ("block", "Must refer to at least 64 bytes");
3272 lock (encrypt_lock) {
3273 return sys_encrypt (block, decode ? 1 : 0);
3278 // void swab(const void *from, void *to, ssize_t n);
3279 [DllImport (MPH, SetLastError=true,
3280 EntryPoint="Mono_Posix_Syscall_swab")]
3281 public static extern int swab (IntPtr from, IntPtr to, long n);
3283 public static unsafe void swab (void* from, void* to, long n)
3285 swab ((IntPtr) from, (IntPtr) to, n);
3290 #region <utime.h> Declarations
3292 // <utime.h> -- COMPLETE
3295 [DllImport (MPH, SetLastError=true,
3296 EntryPoint="Mono_Posix_Syscall_utime")]
3297 private static extern int sys_utime (
3298 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3299 string filename, ref Utimbuf buf, int use_buf);
3301 public static int utime (string filename, ref Utimbuf buf)
3303 return sys_utime (filename, ref buf, 1);
3306 public static int utime (string filename)
3308 Utimbuf buf = new Utimbuf ();
3309 return sys_utime (filename, ref buf, 0);