2 // Mono.Unix/Syscall.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Jonathan Pryor (jonpryor@vt.edu)
8 // (C) 2003 Novell, Inc.
9 // (C) 2004-2006 Jonathan Pryor
11 // This file implements the low-level syscall interface to the POSIX
14 // This file tries to stay close to the low-level API as much as possible
15 // using enumerations, structures and in a few cases, using existing .NET
18 // Implementation notes:
20 // Since the values for the various constants on the API changes
21 // from system to system (even Linux on different architectures will
22 // have different values), we define our own set of values, and we
23 // use a set of C helper routines to map from the constants we define
24 // to the values of the native OS.
26 // Bitfields are flagged with the [Map] attribute, and a helper program
27 // generates a set of routines that we can call to convert from our value
28 // definitions to the value definitions expected by the OS; see
29 // NativeConvert for the conversion routines.
31 // Methods that require tuning are bound as `private sys_NAME' methods
32 // and then a `NAME' method is exposed.
36 // Permission is hereby granted, free of charge, to any person obtaining
37 // a copy of this software and associated documentation files (the
38 // "Software"), to deal in the Software without restriction, including
39 // without limitation the rights to use, copy, modify, merge, publish,
40 // distribute, sublicense, and/or sell copies of the Software, and to
41 // permit persons to whom the Software is furnished to do so, subject to
42 // the following conditions:
44 // The above copyright notice and this permission notice shall be
45 // included in all copies or substantial portions of the Software.
47 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
51 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
52 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
53 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57 using System.Collections;
58 using System.Collections.Generic;
59 using System.Runtime.InteropServices;
60 using System.Security;
62 using Mono.Unix.Native;
64 namespace Mono.Unix.Native {
69 [CLSCompliant (false)]
70 public enum SyslogOptions {
71 LOG_PID = 0x01, // log the pid with each message
72 LOG_CONS = 0x02, // log on the console if errors in sending
73 LOG_ODELAY = 0x04, // delay open until first syslog (default)
74 LOG_NDELAY = 0x08, // don't delay open
75 LOG_NOWAIT = 0x10, // don't wait for console forks; DEPRECATED
76 LOG_PERROR = 0x20 // log to stderr as well
80 [CLSCompliant (false)]
81 public enum SyslogFacility {
92 LOG_AUTHPRIV = 10 << 3,
100 LOG_LOCAL6 = 22 << 3,
101 LOG_LOCAL7 = 23 << 3,
105 [CLSCompliant (false)]
106 public enum SyslogLevel {
107 LOG_EMERG = 0, // system is unusable
108 LOG_ALERT = 1, // action must be taken immediately
109 LOG_CRIT = 2, // critical conditions
110 LOG_ERR = 3, // warning conditions
111 LOG_WARNING = 4, // warning conditions
112 LOG_NOTICE = 5, // normal but significant condition
113 LOG_INFO = 6, // informational
114 LOG_DEBUG = 7 // debug-level messages
118 [CLSCompliant (false)]
119 public enum OpenFlags : int {
123 O_RDONLY = 0x00000000,
124 O_WRONLY = 0x00000001,
128 // Or-ed with zero or more of these
130 O_CREAT = 0x00000040,
132 O_NOCTTY = 0x00000100,
133 O_TRUNC = 0x00000200,
134 O_APPEND = 0x00000400,
135 O_NONBLOCK = 0x00000800,
139 // These are non-Posix. Using them will result in errors/exceptions on
140 // non-supported platforms.
142 // (For example, "C-wrapped" system calls -- calls with implementation in
143 // MonoPosixHelper -- will return -1 with errno=EINVAL. C#-wrapped system
144 // calls will generate an exception in NativeConvert, as the value can't be
145 // converted on the target platform.)
148 O_NOFOLLOW = 0x00020000,
149 O_DIRECTORY = 0x00010000,
150 O_DIRECT = 0x00004000,
151 O_ASYNC = 0x00002000,
152 O_LARGEFILE = 0x00008000,
153 O_CLOEXEC = 0x00080000,
158 [CLSCompliant (false)]
159 public enum AtFlags : int {
160 AT_SYMLINK_NOFOLLOW = 0x00000100,
161 AT_REMOVEDIR = 0x00000200,
162 AT_SYMLINK_FOLLOW = 0x00000400,
163 AT_NO_AUTOMOUNT = 0x00000800,
164 AT_EMPTY_PATH = 0x00001000
169 [CLSCompliant (false)]
170 public enum FilePermissions : uint {
171 S_ISUID = 0x0800, // Set user ID on execution
172 S_ISGID = 0x0400, // Set group ID on execution
173 S_ISVTX = 0x0200, // Save swapped text after use (sticky).
174 S_IRUSR = 0x0100, // Read by owner
175 S_IWUSR = 0x0080, // Write by owner
176 S_IXUSR = 0x0040, // Execute by owner
177 S_IRGRP = 0x0020, // Read by group
178 S_IWGRP = 0x0010, // Write by group
179 S_IXGRP = 0x0008, // Execute by group
180 S_IROTH = 0x0004, // Read by other
181 S_IWOTH = 0x0002, // Write by other
182 S_IXOTH = 0x0001, // Execute by other
184 S_IRWXG = (S_IRGRP | S_IWGRP | S_IXGRP),
185 S_IRWXU = (S_IRUSR | S_IWUSR | S_IXUSR),
186 S_IRWXO = (S_IROTH | S_IWOTH | S_IXOTH),
187 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
188 ALLPERMS = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
189 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
192 // Why these are held in "mode_t" is beyond me...
193 S_IFMT = 0xF000, // Bits which determine file type
194 [Map(SuppressFlags="S_IFMT")]
195 S_IFDIR = 0x4000, // Directory
196 [Map(SuppressFlags="S_IFMT")]
197 S_IFCHR = 0x2000, // Character device
198 [Map(SuppressFlags="S_IFMT")]
199 S_IFBLK = 0x6000, // Block device
200 [Map(SuppressFlags="S_IFMT")]
201 S_IFREG = 0x8000, // Regular file
202 [Map(SuppressFlags="S_IFMT")]
203 S_IFIFO = 0x1000, // FIFO
204 [Map(SuppressFlags="S_IFMT")]
205 S_IFLNK = 0xA000, // Symbolic link
206 [Map(SuppressFlags="S_IFMT")]
207 S_IFSOCK = 0xC000, // Socket
211 [CLSCompliant (false)]
212 public enum FcntlCommand : int {
213 // Form /usr/include/bits/fcntl.h
214 F_DUPFD = 0, // Duplicate file descriptor.
215 F_GETFD = 1, // Get file descriptor flags.
216 F_SETFD = 2, // Set file descriptor flags.
217 F_GETFL = 3, // Get file status flags.
218 F_SETFL = 4, // Set file status flags.
219 F_GETLK = 12, // Get record locking info. [64]
220 F_SETLK = 13, // Set record locking info (non-blocking). [64]
221 F_SETLKW = 14, // Set record locking info (blocking). [64]
222 F_SETOWN = 8, // Set owner of socket (receiver of SIGIO).
223 F_GETOWN = 9, // Get owner of socket (receiver of SIGIO).
224 F_SETSIG = 10, // Set number of signal to be sent.
225 F_GETSIG = 11, // Get number of signal to be sent.
226 F_SETLEASE = 1024, // Set a lease.
227 F_GETLEASE = 1025, // Enquire what lease is active.
228 F_NOTIFY = 1026, // Required notifications on a directory
232 [CLSCompliant (false)]
233 public enum LockType : short {
234 F_RDLCK = 0, // Read lock.
235 F_WRLCK = 1, // Write lock.
236 F_UNLCK = 2, // Remove lock.
240 [CLSCompliant (false)]
241 public enum SeekFlags : short {
242 // values liberally copied from /usr/include/unistd.h
243 SEEK_SET = 0, // Seek from beginning of file.
244 SEEK_CUR = 1, // Seek from current position.
245 SEEK_END = 2, // Seek from end of file.
247 L_SET = SEEK_SET, // BSD alias for SEEK_SET
248 L_INCR = SEEK_CUR, // BSD alias for SEEK_CUR
249 L_XTND = SEEK_END, // BSD alias for SEEK_END
253 [CLSCompliant (false)]
254 public enum DirectoryNotifyFlags : int {
255 // from /usr/include/bits/fcntl.h
256 DN_ACCESS = 0x00000001, // File accessed.
257 DN_MODIFY = 0x00000002, // File modified.
258 DN_CREATE = 0x00000004, // File created.
259 DN_DELETE = 0x00000008, // File removed.
260 DN_RENAME = 0x00000010, // File renamed.
261 DN_ATTRIB = 0x00000020, // File changed attributes.
262 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
266 [CLSCompliant (false)]
267 public enum PosixFadviseAdvice : int {
268 POSIX_FADV_NORMAL = 0, // No further special treatment.
269 POSIX_FADV_RANDOM = 1, // Expect random page references.
270 POSIX_FADV_SEQUENTIAL = 2, // Expect sequential page references.
271 POSIX_FADV_WILLNEED = 3, // Will need these pages.
272 POSIX_FADV_DONTNEED = 4, // Don't need these pages.
273 POSIX_FADV_NOREUSE = 5, // Data will be accessed once.
277 [CLSCompliant (false)]
278 public enum PosixMadviseAdvice : int {
279 POSIX_MADV_NORMAL = 0, // No further special treatment.
280 POSIX_MADV_RANDOM = 1, // Expect random page references.
281 POSIX_MADV_SEQUENTIAL = 2, // Expect sequential page references.
282 POSIX_MADV_WILLNEED = 3, // Will need these pages.
283 POSIX_MADV_DONTNEED = 4, // Don't need these pages.
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 public enum WaitOptions : int {
327 WNOHANG = 1, // Don't block waiting
328 WUNTRACED = 2, // Report status of stopped children
332 [CLSCompliant (false)]
333 public enum AccessModes : int {
341 [CLSCompliant (false)]
342 public enum PathconfName : int {
349 _PC_CHOWN_RESTRICTED,
357 _PC_REC_INCR_XFER_SIZE,
358 _PC_REC_MAX_XFER_SIZE,
359 _PC_REC_MIN_XFER_SIZE,
367 [CLSCompliant (false)]
368 public enum SysconfName : int {
378 _SC_REALTIME_SIGNALS,
379 _SC_PRIORITY_SCHEDULING,
388 _SC_MEMORY_PROTECTION,
391 _SC_SHARED_MEMORY_OBJECTS,
394 _SC_AIO_PRIO_DELTA_MAX,
405 /* Values for the argument to `sysconf'
406 corresponding to _POSIX2_* symbols. */
411 _SC_COLL_WEIGHTS_MAX,
416 _SC_CHARCLASS_NAME_MAX,
432 _SC_IOV_MAX = _SC_UIO_MAXIOV,
433 _SC_PII_INTERNET_STREAM,
434 _SC_PII_INTERNET_DGRAM,
439 /* Values according to POSIX 1003.1c (POSIX threads). */
441 _SC_THREAD_SAFE_FUNCTIONS,
442 _SC_GETGR_R_SIZE_MAX,
443 _SC_GETPW_R_SIZE_MAX,
446 _SC_THREAD_DESTRUCTOR_ITERATIONS,
448 _SC_THREAD_STACK_MIN,
449 _SC_THREAD_THREADS_MAX,
450 _SC_THREAD_ATTR_STACKADDR,
451 _SC_THREAD_ATTR_STACKSIZE,
452 _SC_THREAD_PRIORITY_SCHEDULING,
453 _SC_THREAD_PRIO_INHERIT,
454 _SC_THREAD_PRIO_PROTECT,
455 _SC_THREAD_PROCESS_SHARED,
456 _SC_NPROCESSORS_CONF,
457 _SC_NPROCESSORS_ONLN,
463 _SC_XOPEN_XCU_VERSION,
498 _SC_XBS5_ILP32_OFF32,
499 _SC_XBS5_ILP32_OFFBIG,
501 _SC_XBS5_LPBIG_OFFBIG,
504 _SC_XOPEN_REALTIME_THREADS,
509 _SC_C_LANG_SUPPORT_R,
515 _SC_DEVICE_SPECIFIC_R,
526 _SC_READER_WRITER_LOCKS,
534 _SC_THREAD_SPORADIC_SERVER,
536 _SC_SYSTEM_DATABASE_R,
538 _SC_TYPED_MEMORY_OBJECTS,
542 _SC_2_PBS_ACCOUNTING,
548 _SC_2_PBS_CHECKPOINT,
555 _SC_TRACE_EVENT_FILTER,
558 _SC_LEVEL1_ICACHE_SIZE,
559 _SC_LEVEL1_ICACHE_ASSOC,
560 _SC_LEVEL1_ICACHE_LINESIZE,
561 _SC_LEVEL1_DCACHE_SIZE,
562 _SC_LEVEL1_DCACHE_ASSOC,
563 _SC_LEVEL1_DCACHE_LINESIZE,
564 _SC_LEVEL2_CACHE_SIZE,
565 _SC_LEVEL2_CACHE_ASSOC,
566 _SC_LEVEL2_CACHE_LINESIZE,
567 _SC_LEVEL3_CACHE_SIZE,
568 _SC_LEVEL3_CACHE_ASSOC,
569 _SC_LEVEL3_CACHE_LINESIZE,
570 _SC_LEVEL4_CACHE_SIZE,
571 _SC_LEVEL4_CACHE_ASSOC,
572 _SC_LEVEL4_CACHE_LINESIZE
576 [CLSCompliant (false)]
577 public enum ConfstrName : int {
578 _CS_PATH, /* The default search path. */
579 _CS_V6_WIDTH_RESTRICTED_ENVS,
580 _CS_GNU_LIBC_VERSION,
581 _CS_GNU_LIBPTHREAD_VERSION,
582 _CS_LFS_CFLAGS = 1000,
590 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
591 _CS_XBS5_ILP32_OFF32_LDFLAGS,
592 _CS_XBS5_ILP32_OFF32_LIBS,
593 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
594 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
595 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
596 _CS_XBS5_ILP32_OFFBIG_LIBS,
597 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
598 _CS_XBS5_LP64_OFF64_CFLAGS,
599 _CS_XBS5_LP64_OFF64_LDFLAGS,
600 _CS_XBS5_LP64_OFF64_LIBS,
601 _CS_XBS5_LP64_OFF64_LINTFLAGS,
602 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
603 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
604 _CS_XBS5_LPBIG_OFFBIG_LIBS,
605 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
606 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
607 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
608 _CS_POSIX_V6_ILP32_OFF32_LIBS,
609 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
610 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
611 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
612 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
613 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
614 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
615 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
616 _CS_POSIX_V6_LP64_OFF64_LIBS,
617 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
618 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
619 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
620 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
621 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
625 [CLSCompliant (false)]
626 public enum LockfCommand : int {
627 F_ULOCK = 0, // Unlock a previously locked region.
628 F_LOCK = 1, // Lock a region for exclusive use.
629 F_TLOCK = 2, // Test and lock a region for exclusive use.
630 F_TEST = 3, // Test a region for other process locks.
634 public enum PollEvents : short {
635 POLLIN = 0x0001, // There is data to read
636 POLLPRI = 0x0002, // There is urgent data to read
637 POLLOUT = 0x0004, // Writing now will not block
638 POLLERR = 0x0008, // Error condition
639 POLLHUP = 0x0010, // Hung up
640 POLLNVAL = 0x0020, // Invalid request; fd not open
641 // XPG4.2 definitions (via _XOPEN_SOURCE)
642 POLLRDNORM = 0x0040, // Normal data may be read
643 POLLRDBAND = 0x0080, // Priority data may be read
644 POLLWRNORM = 0x0100, // Writing now will not block
645 POLLWRBAND = 0x0200, // Priority data may be written
649 [CLSCompliant (false)]
650 public enum XattrFlags : int {
657 [CLSCompliant (false)]
658 public enum MountFlags : ulong {
659 ST_RDONLY = 1, // Mount read-only
660 ST_NOSUID = 2, // Ignore suid and sgid bits
661 ST_NODEV = 4, // Disallow access to device special files
662 ST_NOEXEC = 8, // Disallow program execution
663 ST_SYNCHRONOUS = 16, // Writes are synced at once
664 ST_REMOUNT = 32, // Alter flags of a mounted FS
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
671 ST_BIND = 4096, // Bind directory at different place
675 [CLSCompliant (false)]
676 public enum MmapFlags : int {
677 MAP_SHARED = 0x01, // Share changes.
678 MAP_PRIVATE = 0x02, // Changes are private.
679 MAP_TYPE = 0x0f, // Mask for type of mapping.
680 MAP_FIXED = 0x10, // Interpret addr exactly.
682 MAP_ANONYMOUS = 0x20, // Don't use a file.
683 MAP_ANON = MAP_ANONYMOUS,
685 // These are Linux-specific.
686 MAP_GROWSDOWN = 0x00100, // Stack-like segment.
687 MAP_DENYWRITE = 0x00800, // ETXTBSY
688 MAP_EXECUTABLE = 0x01000, // Mark it as an executable.
689 MAP_LOCKED = 0x02000, // Lock the mapping.
690 MAP_NORESERVE = 0x04000, // Don't check for reservations.
691 MAP_POPULATE = 0x08000, // Populate (prefault) pagetables.
692 MAP_NONBLOCK = 0x10000, // Do not block on IO.
696 [CLSCompliant (false)]
697 public enum MmapProts : int {
698 PROT_READ = 0x1, // Page can be read.
699 PROT_WRITE = 0x2, // Page can be written.
700 PROT_EXEC = 0x4, // Page can be executed.
701 PROT_NONE = 0x0, // Page can not be accessed.
702 PROT_GROWSDOWN = 0x01000000, // Extend change to start of
703 // growsdown vma (mprotect only).
704 PROT_GROWSUP = 0x02000000, // Extend change to start of
705 // growsup vma (mprotect only).
709 [CLSCompliant (false)]
710 public enum MsyncFlags : int {
711 MS_ASYNC = 0x1, // Sync memory asynchronously.
712 MS_SYNC = 0x4, // Synchronous memory sync.
713 MS_INVALIDATE = 0x2, // Invalidate the caches.
717 [CLSCompliant (false)]
718 public enum MlockallFlags : int {
719 MCL_CURRENT = 0x1, // Lock all currently mapped pages.
720 MCL_FUTURE = 0x2, // Lock all additions to address
724 [CLSCompliant (false)]
725 public enum MremapFlags : ulong {
726 MREMAP_MAYMOVE = 0x1,
733 [Map ("struct flock")]
739 [CLSCompliant (false)]
740 public LockType l_type; // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
741 [CLSCompliant (false)]
742 public SeekFlags l_whence; // How to interpret l_start
743 [off_t] public long l_start; // Starting offset for lock
744 [off_t] public long l_len; // Number of bytes to lock
745 [pid_t] public int l_pid; // PID of process blocking our lock (F_GETLK only)
747 public override int GetHashCode ()
749 return l_type.GetHashCode () ^ l_whence.GetHashCode () ^
750 l_start.GetHashCode () ^ l_len.GetHashCode () ^
751 l_pid.GetHashCode ();
754 public override bool Equals (object obj)
756 if ((obj == null) || (obj.GetType () != GetType ()))
758 Flock value = (Flock) obj;
759 return l_type == value.l_type && l_whence == value.l_whence &&
760 l_start == value.l_start && l_len == value.l_len &&
761 l_pid == value.l_pid;
764 public bool Equals (Flock value)
766 return l_type == value.l_type && l_whence == value.l_whence &&
767 l_start == value.l_start && l_len == value.l_len &&
768 l_pid == value.l_pid;
771 public static bool operator== (Flock lhs, Flock rhs)
773 return lhs.Equals (rhs);
776 public static bool operator!= (Flock lhs, Flock rhs)
778 return !lhs.Equals (rhs);
782 [Map ("struct pollfd")]
785 : IEquatable <Pollfd>
789 [CLSCompliant (false)]
790 public PollEvents events;
791 [CLSCompliant (false)]
792 public PollEvents revents;
794 public override int GetHashCode ()
796 return events.GetHashCode () ^ revents.GetHashCode ();
799 public override bool Equals (object obj)
801 if (obj == null || obj.GetType () != GetType ())
803 Pollfd value = (Pollfd) obj;
804 return value.events == events && value.revents == revents;
807 public bool Equals (Pollfd value)
809 return value.events == events && value.revents == revents;
812 public static bool operator== (Pollfd lhs, Pollfd rhs)
814 return lhs.Equals (rhs);
817 public static bool operator!= (Pollfd lhs, Pollfd rhs)
819 return !lhs.Equals (rhs);
823 // Use manually written To/From methods to handle fields st_atime_nsec etc.
829 [CLSCompliant (false)]
830 [dev_t] public ulong st_dev; // device
831 [CLSCompliant (false)]
832 [ino_t] public ulong st_ino; // inode
833 [CLSCompliant (false)]
834 public FilePermissions st_mode; // protection
836 #pragma warning disable 169
837 private uint _padding_; // padding for structure alignment
838 #pragma warning restore 169
839 [CLSCompliant (false)]
840 [nlink_t] public ulong st_nlink; // number of hard links
841 [CLSCompliant (false)]
842 [uid_t] public uint st_uid; // user ID of owner
843 [CLSCompliant (false)]
844 [gid_t] public uint st_gid; // group ID of owner
845 [CLSCompliant (false)]
846 [dev_t] public ulong st_rdev; // device type (if inode device)
847 [off_t] public long st_size; // total size, in bytes
848 [blksize_t] public long st_blksize; // blocksize for filesystem I/O
849 [blkcnt_t] public long st_blocks; // number of blocks allocated
850 [time_t] public long st_atime; // time of last access
851 [time_t] public long st_mtime; // time of last modification
852 [time_t] public long st_ctime; // time of last status change
853 public long st_atime_nsec; // Timespec.tv_nsec partner to st_atime
854 public long st_mtime_nsec; // Timespec.tv_nsec partner to st_mtime
855 public long st_ctime_nsec; // Timespec.tv_nsec partner to st_ctime
857 public Timespec st_atim {
859 return new Timespec { tv_sec = st_atime, tv_nsec = st_atime_nsec };
862 st_atime = value.tv_sec;
863 st_atime_nsec = value.tv_nsec;
867 public Timespec st_mtim {
869 return new Timespec { tv_sec = st_mtime, tv_nsec = st_mtime_nsec };
872 st_mtime = value.tv_sec;
873 st_mtime_nsec = value.tv_nsec;
877 public Timespec st_ctim {
879 return new Timespec { tv_sec = st_ctime, tv_nsec = st_ctime_nsec };
882 st_ctime = value.tv_sec;
883 st_ctime_nsec = value.tv_nsec;
887 public override int GetHashCode ()
889 return st_dev.GetHashCode () ^
890 st_ino.GetHashCode () ^
891 st_mode.GetHashCode () ^
892 st_nlink.GetHashCode () ^
893 st_uid.GetHashCode () ^
894 st_gid.GetHashCode () ^
895 st_rdev.GetHashCode () ^
896 st_size.GetHashCode () ^
897 st_blksize.GetHashCode () ^
898 st_blocks.GetHashCode () ^
899 st_atime.GetHashCode () ^
900 st_mtime.GetHashCode () ^
901 st_ctime.GetHashCode () ^
902 st_atime_nsec.GetHashCode () ^
903 st_mtime_nsec.GetHashCode () ^
904 st_ctime_nsec.GetHashCode ();
907 public override bool Equals (object obj)
909 if (obj == null || obj.GetType() != GetType ())
911 Stat value = (Stat) obj;
912 return value.st_dev == st_dev &&
913 value.st_ino == st_ino &&
914 value.st_mode == st_mode &&
915 value.st_nlink == st_nlink &&
916 value.st_uid == st_uid &&
917 value.st_gid == st_gid &&
918 value.st_rdev == st_rdev &&
919 value.st_size == st_size &&
920 value.st_blksize == st_blksize &&
921 value.st_blocks == st_blocks &&
922 value.st_atime == st_atime &&
923 value.st_mtime == st_mtime &&
924 value.st_ctime == st_ctime &&
925 value.st_atime_nsec == st_atime_nsec &&
926 value.st_mtime_nsec == st_mtime_nsec &&
927 value.st_ctime_nsec == st_ctime_nsec;
930 public bool Equals (Stat value)
932 return value.st_dev == st_dev &&
933 value.st_ino == st_ino &&
934 value.st_mode == st_mode &&
935 value.st_nlink == st_nlink &&
936 value.st_uid == st_uid &&
937 value.st_gid == st_gid &&
938 value.st_rdev == st_rdev &&
939 value.st_size == st_size &&
940 value.st_blksize == st_blksize &&
941 value.st_blocks == st_blocks &&
942 value.st_atime == st_atime &&
943 value.st_mtime == st_mtime &&
944 value.st_ctime == st_ctime &&
945 value.st_atime_nsec == st_atime_nsec &&
946 value.st_mtime_nsec == st_mtime_nsec &&
947 value.st_ctime_nsec == st_ctime_nsec;
950 public static bool operator== (Stat lhs, Stat rhs)
952 return lhs.Equals (rhs);
955 public static bool operator!= (Stat lhs, Stat rhs)
957 return !lhs.Equals (rhs);
961 // `struct statvfs' isn't portable, so don't generate To/From methods.
963 [CLSCompliant (false)]
964 public struct Statvfs
966 : IEquatable <Statvfs>
969 public ulong f_bsize; // file system block size
970 public ulong f_frsize; // fragment size
971 [fsblkcnt_t] public ulong f_blocks; // size of fs in f_frsize units
972 [fsblkcnt_t] public ulong f_bfree; // # free blocks
973 [fsblkcnt_t] public ulong f_bavail; // # free blocks for non-root
974 [fsfilcnt_t] public ulong f_files; // # inodes
975 [fsfilcnt_t] public ulong f_ffree; // # free inodes
976 [fsfilcnt_t] public ulong f_favail; // # free inodes for non-root
977 public ulong f_fsid; // file system id
978 public MountFlags f_flag; // mount flags
979 public ulong f_namemax; // maximum filename length
981 public override int GetHashCode ()
983 return f_bsize.GetHashCode () ^
984 f_frsize.GetHashCode () ^
985 f_blocks.GetHashCode () ^
986 f_bfree.GetHashCode () ^
987 f_bavail.GetHashCode () ^
988 f_files.GetHashCode () ^
989 f_ffree.GetHashCode () ^
990 f_favail.GetHashCode () ^
991 f_fsid.GetHashCode () ^
992 f_flag.GetHashCode () ^
993 f_namemax.GetHashCode ();
996 public override bool Equals (object obj)
998 if (obj == null || obj.GetType() != GetType ())
1000 Statvfs value = (Statvfs) obj;
1001 return value.f_bsize == f_bsize &&
1002 value.f_frsize == f_frsize &&
1003 value.f_blocks == f_blocks &&
1004 value.f_bfree == f_bfree &&
1005 value.f_bavail == f_bavail &&
1006 value.f_files == f_files &&
1007 value.f_ffree == f_ffree &&
1008 value.f_favail == f_favail &&
1009 value.f_fsid == f_fsid &&
1010 value.f_flag == f_flag &&
1011 value.f_namemax == f_namemax;
1014 public bool Equals (Statvfs value)
1016 return value.f_bsize == f_bsize &&
1017 value.f_frsize == f_frsize &&
1018 value.f_blocks == f_blocks &&
1019 value.f_bfree == f_bfree &&
1020 value.f_bavail == f_bavail &&
1021 value.f_files == f_files &&
1022 value.f_ffree == f_ffree &&
1023 value.f_favail == f_favail &&
1024 value.f_fsid == f_fsid &&
1025 value.f_flag == f_flag &&
1026 value.f_namemax == f_namemax;
1029 public static bool operator== (Statvfs lhs, Statvfs rhs)
1031 return lhs.Equals (rhs);
1034 public static bool operator!= (Statvfs lhs, Statvfs rhs)
1036 return !lhs.Equals (rhs);
1040 [Map ("struct timeval")]
1041 public struct Timeval
1043 : IEquatable <Timeval>
1046 [time_t] public long tv_sec; // seconds
1047 [suseconds_t] public long tv_usec; // microseconds
1049 public override int GetHashCode ()
1051 return tv_sec.GetHashCode () ^ tv_usec.GetHashCode ();
1054 public override bool Equals (object obj)
1056 if (obj == null || obj.GetType () != GetType ())
1058 Timeval value = (Timeval) obj;
1059 return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1062 public bool Equals (Timeval value)
1064 return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1067 public static bool operator== (Timeval lhs, Timeval rhs)
1069 return lhs.Equals (rhs);
1072 public static bool operator!= (Timeval lhs, Timeval rhs)
1074 return !lhs.Equals (rhs);
1078 [Map ("struct timezone")]
1079 public struct Timezone
1081 : IEquatable <Timezone>
1084 public int tz_minuteswest; // minutes W of Greenwich
1085 #pragma warning disable 169
1086 private int tz_dsttime; // type of dst correction (OBSOLETE)
1087 #pragma warning restore 169
1089 public override int GetHashCode ()
1091 return tz_minuteswest.GetHashCode ();
1094 public override bool Equals (object obj)
1096 if (obj == null || obj.GetType () != GetType ())
1098 Timezone value = (Timezone) obj;
1099 return value.tz_minuteswest == tz_minuteswest;
1102 public bool Equals (Timezone value)
1104 return value.tz_minuteswest == tz_minuteswest;
1107 public static bool operator== (Timezone lhs, Timezone rhs)
1109 return lhs.Equals (rhs);
1112 public static bool operator!= (Timezone lhs, Timezone rhs)
1114 return !lhs.Equals (rhs);
1118 [Map ("struct utimbuf")]
1119 public struct Utimbuf
1121 : IEquatable <Utimbuf>
1124 [time_t] public long actime; // access time
1125 [time_t] public long modtime; // modification time
1127 public override int GetHashCode ()
1129 return actime.GetHashCode () ^ modtime.GetHashCode ();
1132 public override bool Equals (object obj)
1134 if (obj == null || obj.GetType () != GetType ())
1136 Utimbuf value = (Utimbuf) obj;
1137 return value.actime == actime && value.modtime == modtime;
1140 public bool Equals (Utimbuf value)
1142 return value.actime == actime && value.modtime == modtime;
1145 public static bool operator== (Utimbuf lhs, Utimbuf rhs)
1147 return lhs.Equals (rhs);
1150 public static bool operator!= (Utimbuf lhs, Utimbuf rhs)
1152 return !lhs.Equals (rhs);
1156 [Map ("struct timespec")]
1157 public struct Timespec
1159 : IEquatable <Timespec>
1162 [time_t] public long tv_sec; // Seconds.
1163 public long tv_nsec; // Nanoseconds.
1165 public override int GetHashCode ()
1167 return tv_sec.GetHashCode () ^ tv_nsec.GetHashCode ();
1170 public override bool Equals (object obj)
1172 if (obj == null || obj.GetType () != GetType ())
1174 Timespec value = (Timespec) obj;
1175 return value.tv_sec == tv_sec && value.tv_nsec == tv_nsec;
1178 public bool Equals (Timespec value)
1180 return value.tv_sec == tv_sec && value.tv_nsec == tv_nsec;
1183 public static bool operator== (Timespec lhs, Timespec rhs)
1185 return lhs.Equals (rhs);
1188 public static bool operator!= (Timespec lhs, Timespec rhs)
1190 return !lhs.Equals (rhs);
1194 [Map ("struct iovec")]
1197 public IntPtr iov_base; // Starting address
1198 [CLSCompliant (false)]
1199 public ulong iov_len; // Number of bytes to transfer
1203 public enum EpollFlags {
1204 EPOLL_CLOEXEC = 02000000,
1205 EPOLL_NONBLOCK = 04000,
1209 [CLSCompliant (false)]
1210 public enum EpollEvents : uint {
1214 EPOLLRDNORM = 0x040,
1215 EPOLLRDBAND = 0x080,
1216 EPOLLWRNORM = 0x100,
1217 EPOLLWRBAND = 0x200,
1221 EPOLLRDHUP = 0x2000,
1222 EPOLLONESHOT = 1 << 30,
1223 EPOLLET = unchecked ((uint) (1 << 31))
1226 public enum EpollOp {
1232 [StructLayout (LayoutKind.Explicit, Size=12, Pack=1)]
1233 [CLSCompliant (false)]
1234 public struct EpollEvent {
1236 public EpollEvents events;
1250 public sealed class Dirent
1252 : IEquatable <Dirent>
1255 [CLSCompliant (false)]
1256 public /* ino_t */ ulong d_ino;
1257 public /* off_t */ long d_off;
1258 [CLSCompliant (false)]
1259 public ushort d_reclen;
1261 public string d_name;
1263 public override int GetHashCode ()
1265 return d_ino.GetHashCode () ^ d_off.GetHashCode () ^
1266 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
1267 d_name.GetHashCode ();
1270 public override bool Equals (object obj)
1272 if (obj == null || GetType() != obj.GetType())
1274 Dirent d = (Dirent) obj;
1278 public bool Equals (Dirent value)
1282 return value.d_ino == d_ino && value.d_off == d_off &&
1283 value.d_reclen == d_reclen && value.d_type == d_type &&
1284 value.d_name == d_name;
1287 public override string ToString ()
1292 public static bool operator== (Dirent lhs, Dirent rhs)
1294 return Object.Equals (lhs, rhs);
1297 public static bool operator!= (Dirent lhs, Dirent rhs)
1299 return !Object.Equals (lhs, rhs);
1303 public sealed class Fstab
1305 : IEquatable <Fstab>
1308 public string fs_spec;
1309 public string fs_file;
1310 public string fs_vfstype;
1311 public string fs_mntops;
1312 public string fs_type;
1314 public int fs_passno;
1316 public override int GetHashCode ()
1318 return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
1319 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
1320 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
1323 public override bool Equals (object obj)
1325 if (obj == null || GetType() != obj.GetType())
1327 Fstab f = (Fstab) obj;
1331 public bool Equals (Fstab value)
1335 return value.fs_spec == fs_spec && value.fs_file == fs_file &&
1336 value.fs_vfstype == fs_vfstype && value.fs_mntops == fs_mntops &&
1337 value.fs_type == fs_type && value.fs_freq == fs_freq &&
1338 value.fs_passno == fs_passno;
1341 public override string ToString ()
1346 public static bool operator== (Fstab lhs, Fstab rhs)
1348 return Object.Equals (lhs, rhs);
1351 public static bool operator!= (Fstab lhs, Fstab rhs)
1353 return !Object.Equals (lhs, rhs);
1357 public sealed class Group
1359 : IEquatable <Group>
1362 public string gr_name;
1363 public string gr_passwd;
1364 [CLSCompliant (false)]
1365 public /* gid_t */ uint gr_gid;
1366 public string[] gr_mem;
1368 public override int GetHashCode ()
1371 for (int i = 0; i < gr_mem.Length; ++i)
1372 memhc ^= gr_mem[i].GetHashCode ();
1374 return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^
1375 gr_gid.GetHashCode () ^ memhc;
1378 public override bool Equals (object obj)
1380 if (obj == null || GetType() != obj.GetType())
1382 Group g = (Group) obj;
1386 public bool Equals (Group value)
1390 if (value.gr_gid != gr_gid)
1392 if (value.gr_gid == gr_gid && value.gr_name == gr_name &&
1393 value.gr_passwd == gr_passwd) {
1394 if (value.gr_mem == gr_mem)
1396 if (value.gr_mem == null || gr_mem == null)
1398 if (value.gr_mem.Length != gr_mem.Length)
1400 for (int i = 0; i < gr_mem.Length; ++i)
1401 if (gr_mem[i] != value.gr_mem[i])
1408 // Generate string in /etc/group format
1409 public override string ToString ()
1411 StringBuilder sb = new StringBuilder ();
1412 sb.Append (gr_name).Append (":").Append (gr_passwd).Append (":");
1413 sb.Append (gr_gid).Append (":");
1414 GetMembers (sb, gr_mem);
1415 return sb.ToString ();
1418 private static void GetMembers (StringBuilder sb, string[] members)
1420 if (members.Length > 0)
1421 sb.Append (members[0]);
1422 for (int i = 1; i < members.Length; ++i) {
1424 sb.Append (members[i]);
1428 public static bool operator== (Group lhs, Group rhs)
1430 return Object.Equals (lhs, rhs);
1433 public static bool operator!= (Group lhs, Group rhs)
1435 return !Object.Equals (lhs, rhs);
1439 public sealed class Passwd
1441 : IEquatable <Passwd>
1444 public string pw_name;
1445 public string pw_passwd;
1446 [CLSCompliant (false)]
1447 public /* uid_t */ uint pw_uid;
1448 [CLSCompliant (false)]
1449 public /* gid_t */ uint pw_gid;
1450 public string pw_gecos;
1451 public string pw_dir;
1452 public string pw_shell;
1454 public override int GetHashCode ()
1456 return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^
1457 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
1458 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
1459 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
1462 public override bool Equals (object obj)
1464 if (obj == null || GetType() != obj.GetType())
1466 Passwd p = (Passwd) obj;
1470 public bool Equals (Passwd value)
1474 return value.pw_uid == pw_uid && value.pw_gid == pw_gid &&
1475 value.pw_name == pw_name && value.pw_passwd == pw_passwd &&
1476 value.pw_gecos == pw_gecos && value.pw_dir == pw_dir &&
1477 value.pw_shell == pw_shell;
1480 // Generate string in /etc/passwd format
1481 public override string ToString ()
1483 return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
1484 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
1487 public static bool operator== (Passwd lhs, Passwd rhs)
1489 return Object.Equals (lhs, rhs);
1492 public static bool operator!= (Passwd lhs, Passwd rhs)
1494 return !Object.Equals (lhs, rhs);
1498 public sealed class Utsname
1500 : IEquatable <Utsname>
1503 public string sysname;
1504 public string nodename;
1505 public string release;
1506 public string version;
1507 public string machine;
1508 public string domainname;
1510 public override int GetHashCode ()
1512 return sysname.GetHashCode () ^ nodename.GetHashCode () ^
1513 release.GetHashCode () ^ version.GetHashCode () ^
1514 machine.GetHashCode () ^ domainname.GetHashCode ();
1517 public override bool Equals (object obj)
1519 if (obj == null || GetType() != obj.GetType())
1521 Utsname u = (Utsname) obj;
1525 public bool Equals (Utsname value)
1527 return value.sysname == sysname && value.nodename == nodename &&
1528 value.release == release && value.version == version &&
1529 value.machine == machine && value.domainname == domainname;
1532 // Generate string in /etc/passwd format
1533 public override string ToString ()
1535 return string.Format ("{0} {1} {2} {3} {4}",
1536 sysname, nodename, release, version, machine);
1539 public static bool operator== (Utsname lhs, Utsname rhs)
1541 return Object.Equals (lhs, rhs);
1544 public static bool operator!= (Utsname lhs, Utsname rhs)
1546 return !Object.Equals (lhs, rhs);
1551 // Convention: Functions *not* part of the standard C library AND part of
1552 // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
1554 // For example, the man page should be similar to:
1556 // CONFORMING TO (or CONFORMS TO)
1557 // XPG2, SUSv2, POSIX, etc.
1559 // BSD- and GNU-specific exports can also be placed here.
1561 // Non-POSIX/XPG/etc. functions can also be placed here if:
1562 // (a) They'd be likely to be covered in a Steven's-like book
1563 // (b) The functions would be present in libc.so (or equivalent).
1565 // If a function has its own library, that's a STRONG indicator that the
1566 // function should get a different binding, probably in its own assembly,
1567 // so that package management can work sanely. (That is, we'd like to avoid
1568 // scenarios where FooLib.dll is installed, but it requires libFooLib.so to
1569 // run, and libFooLib.so doesn't exist. That would be confusing.)
1571 // The only methods in here should be:
1572 // (1) low-level functions
1573 // (2) "Trivial" function overloads. For example, if the parameters to a
1574 // function are related (e.g. getgroups(2))
1575 // (3) The return type SHOULD NOT be changed. If you want to provide a
1576 // convenience function with a nicer return type, place it into one of
1577 // the Mono.Unix.Unix* wrapper classes, and give it a .NET-styled name.
1578 // - EXCEPTION: No public functions should have a `void' return type.
1579 // `void' return types should be replaced with `int'.
1580 // Rationality: `void'-return functions typically require a
1581 // complicated call sequence, such as clear errno, then call, then
1582 // check errno to see if any errors occurred. This sequence can't
1583 // be done safely in managed code, as errno may change as part of
1584 // the P/Invoke mechanism.
1585 // Instead, add a MonoPosixHelper export which does:
1588 // return errno == 0 ? 0 : -1;
1589 // This lets managed code check the return value in the usual manner.
1590 // (4) Exceptions SHOULD NOT be thrown. EXCEPTIONS:
1591 // - If you're wrapping *broken* methods which make assumptions about
1592 // input data, such as that an argument refers to N bytes of data.
1593 // This is currently limited to cuserid(3) and encrypt(3).
1594 // - If you call functions which themselves generate exceptions.
1595 // This is the case for using NativeConvert, which will throw an
1596 // exception if an invalid/unsupported value is used.
1598 // Naming Conventions:
1599 // - Syscall method names should have the same name as the function being
1600 // wrapped (e.g. Syscall.read ==> read(2)). This allows people to
1601 // consult the appropriate man page if necessary.
1602 // - Methods need not have the same arguments IF this simplifies or
1603 // permits correct usage. The current example is syslog, in which
1604 // syslog(3)'s single `priority' argument is split into SyslogFacility
1605 // and SyslogLevel arguments.
1606 // - Type names (structures, classes, enumerations) are always PascalCased.
1607 // - Enumerations are named as <MethodName><ArgumentName>, and are located
1608 // in the Mono.Unix.Native namespace. For readability, if ArgumentName
1609 // is "cmd", use Command instead. For example, fcntl(2) takes a
1610 // FcntlCommand argument. This naming convention is to provide an
1611 // assocation between an enumeration and where it should be used, and
1612 // allows a single method to accept multiple different enumerations
1613 // (see mmap(2), which takes MmapProts and MmapFlags).
1614 // - EXCEPTION: if an enumeration is shared between multiple different
1615 // methods, AND/OR the "obvious" enumeration name conflicts with an
1616 // existing .NET type, a more appropriate name should be used.
1617 // Example: FilePermissions
1618 // - EXCEPTION: [Flags] enumerations should get plural names to follow
1619 // .NET name guidelines. Usually this doesn't result in a change
1620 // (OpenFlags is the `flags' parameter for open(2)), but it can
1621 // (mmap(2) prot ==> MmapProts, access(2) mode ==> AccessModes).
1622 // - Enumerations should have the [Map] and (optional) [Flags] attributes.
1623 // [Map] is required for make-map to find the type and generate the
1624 // appropriate NativeConvert conversion functions.
1625 // - Enumeration contents should match the original Unix names. This helps
1626 // with documentation (the existing man pages are still useful), and is
1627 // required for use with the make-map generation program.
1628 // - Structure names should be the PascalCased version of the actual
1629 // structure name (struct flock ==> Flock). Structure members should
1630 // have the same names, or a (reasonably) portable subset (Dirent being
1631 // the poster child for questionable members).
1632 // - Whether the managed type should be a reference type (class) or a
1633 // value type (struct) should be determined on a case-by-case basis:
1634 // if you ever need to be able to use NULL for it (such as with Dirent,
1635 // Group, Passwd, as these are method return types and `null' is used
1636 // to signify the end), it should be a reference type; otherwise, use
1637 // your discretion, and keep any expected usage patterns in mind.
1638 // - Syscall should be a Single Point Of Truth (SPOT). There should be
1639 // only ONE way to do anything. By convention, the Linux function names
1640 // are used, but that need not always be the case (use your discretion).
1641 // It SHOULD NOT be required that developers know what platform they're
1642 // on, and choose among a set of similar functions. In short, anything
1643 // that requires a platform check is BAD -- Mono.Unix is a wrapper, and
1644 // we can afford to clean things up whenever possible.
1646 // - Syscall.statfs: Solaris/Mac OS X provide statfs(2), Linux provides
1647 // statvfs(2). MonoPosixHelper will "thunk" between the two,
1648 // exporting a statvfs that works across platforms.
1649 // - Syscall.getfsent: Glibc export which Solaris lacks, while Solaris
1650 // instead provides getvfsent(3). MonoPosixHelper provides wrappers
1651 // to convert getvfsent(3) into Fstab data.
1652 // - Exception: If it isn't possible to cleanly wrap platforms, then the
1653 // method shouldn't be exported. The user will be expected to do their
1654 // own platform check and their own DllImports.
1655 // Examples: mount(2), umount(2), etc.
1656 // - Note: if a platform doesn't support a function AT ALL, the
1657 // MonoPosixHelper wrapper won't be compiled, resulting in a
1658 // EntryPointNotFoundException. This is also consistent with a missing
1659 // P/Invoke into libc.so.
1661 [CLSCompliant (false)]
1662 public sealed class Syscall : Stdlib
1664 new internal const string LIBC = "libc";
1666 private Syscall () {}
1672 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3),
1673 // aio_return(3), aio_suspend(3), aio_write(3)
1675 // Then update UnixStream.BeginRead to use the aio* functions.
1678 #region <attr/xattr.h> Declarations
1680 // <attr/xattr.h> -- COMPLETE
1684 // int setxattr (const char *path, const char *name,
1685 // const void *value, size_t size, int flags);
1686 [DllImport (MPH, SetLastError=true,
1687 EntryPoint="Mono_Posix_Syscall_setxattr")]
1688 public static extern int setxattr (
1689 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1691 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1692 string name, byte[] value, ulong size, XattrFlags flags);
1694 public static int setxattr (string path, string name, byte [] value, ulong size)
1696 return setxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1699 public static int setxattr (string path, string name, byte [] value, XattrFlags flags)
1701 return setxattr (path, name, value, (ulong) value.Length, flags);
1704 public static int setxattr (string path, string name, byte [] value)
1706 return setxattr (path, name, value, (ulong) value.Length);
1710 // int lsetxattr (const char *path, const char *name,
1711 // const void *value, size_t size, int flags);
1712 [DllImport (MPH, SetLastError=true,
1713 EntryPoint="Mono_Posix_Syscall_lsetxattr")]
1714 public static extern int lsetxattr (
1715 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1717 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1718 string name, byte[] value, ulong size, XattrFlags flags);
1720 public static int lsetxattr (string path, string name, byte [] value, ulong size)
1722 return lsetxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1725 public static int lsetxattr (string path, string name, byte [] value, XattrFlags flags)
1727 return lsetxattr (path, name, value, (ulong) value.Length, flags);
1730 public static int lsetxattr (string path, string name, byte [] value)
1732 return lsetxattr (path, name, value, (ulong) value.Length);
1736 // int fsetxattr (int fd, const char *name,
1737 // const void *value, size_t size, int flags);
1738 [DllImport (MPH, SetLastError=true,
1739 EntryPoint="Mono_Posix_Syscall_fsetxattr")]
1740 public static extern int fsetxattr (int fd,
1741 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1742 string name, byte[] value, ulong size, XattrFlags flags);
1744 public static int fsetxattr (int fd, string name, byte [] value, ulong size)
1746 return fsetxattr (fd, name, value, size, XattrFlags.XATTR_AUTO);
1749 public static int fsetxattr (int fd, string name, byte [] value, XattrFlags flags)
1751 return fsetxattr (fd, name, value, (ulong) value.Length, flags);
1754 public static int fsetxattr (int fd, string name, byte [] value)
1756 return fsetxattr (fd, name, value, (ulong) value.Length);
1760 // ssize_t getxattr (const char *path, const char *name,
1761 // void *value, size_t size);
1762 [DllImport (MPH, SetLastError=true,
1763 EntryPoint="Mono_Posix_Syscall_getxattr")]
1764 public static extern long getxattr (
1765 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1767 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1768 string name, byte[] value, ulong size);
1770 public static long getxattr (string path, string name, byte [] value)
1772 return getxattr (path, name, value, (ulong) value.Length);
1775 public static long getxattr (string path, string name, out byte [] value)
1778 long size = getxattr (path, name, value, 0);
1782 value = new byte [size];
1783 return getxattr (path, name, value, (ulong) size);
1787 // ssize_t lgetxattr (const char *path, const char *name,
1788 // void *value, size_t size);
1789 [DllImport (MPH, SetLastError=true,
1790 EntryPoint="Mono_Posix_Syscall_lgetxattr")]
1791 public static extern long lgetxattr (
1792 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1794 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1795 string name, byte[] value, ulong size);
1797 public static long lgetxattr (string path, string name, byte [] value)
1799 return lgetxattr (path, name, value, (ulong) value.Length);
1802 public static long lgetxattr (string path, string name, out byte [] value)
1805 long size = lgetxattr (path, name, value, 0);
1809 value = new byte [size];
1810 return lgetxattr (path, name, value, (ulong) size);
1814 // ssize_t fgetxattr (int fd, const char *name, void *value, size_t size);
1815 [DllImport (MPH, SetLastError=true,
1816 EntryPoint="Mono_Posix_Syscall_fgetxattr")]
1817 public static extern long fgetxattr (int fd,
1818 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1819 string name, byte[] value, ulong size);
1821 public static long fgetxattr (int fd, string name, byte [] value)
1823 return fgetxattr (fd, name, value, (ulong) value.Length);
1826 public static long fgetxattr (int fd, string name, out byte [] value)
1829 long size = fgetxattr (fd, name, value, 0);
1833 value = new byte [size];
1834 return fgetxattr (fd, name, value, (ulong) size);
1838 // ssize_t listxattr (const char *path, char *list, size_t size);
1839 [DllImport (MPH, SetLastError=true,
1840 EntryPoint="Mono_Posix_Syscall_listxattr")]
1841 public static extern long listxattr (
1842 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1843 string path, byte[] list, ulong size);
1845 // Slight modification: returns 0 on success, negative on error
1846 public static long listxattr (string path, Encoding encoding, out string [] values)
1849 long size = listxattr (path, null, 0);
1851 values = new string [0];
1855 byte[] list = new byte [size];
1856 long ret = listxattr (path, list, (ulong) size);
1860 GetValues (list, encoding, out values);
1864 public static long listxattr (string path, out string[] values)
1866 return listxattr (path, UnixEncoding.Instance, out values);
1869 private static void GetValues (byte[] list, Encoding encoding, out string[] values)
1872 for (int i = 0; i < list.Length; ++i)
1876 values = new string [num_values];
1879 for (int i = 0; i < list.Length; ++i) {
1880 if (list [i] == 0) {
1881 values [num_values++] = encoding.GetString (list, str_start, i - str_start);
1888 // ssize_t llistxattr (const char *path, char *list, size_t size);
1889 [DllImport (MPH, SetLastError=true,
1890 EntryPoint="Mono_Posix_Syscall_llistxattr")]
1891 public static extern long llistxattr (
1892 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1893 string path, byte[] list, ulong size);
1895 // Slight modification: returns 0 on success, negative on error
1896 public static long llistxattr (string path, Encoding encoding, out string [] values)
1899 long size = llistxattr (path, null, 0);
1901 values = new string [0];
1905 byte[] list = new byte [size];
1906 long ret = llistxattr (path, list, (ulong) size);
1910 GetValues (list, encoding, out values);
1914 public static long llistxattr (string path, out string[] values)
1916 return llistxattr (path, UnixEncoding.Instance, out values);
1920 // ssize_t flistxattr (int fd, char *list, size_t size);
1921 [DllImport (MPH, SetLastError=true,
1922 EntryPoint="Mono_Posix_Syscall_flistxattr")]
1923 public static extern long flistxattr (int fd, byte[] list, ulong size);
1925 // Slight modification: returns 0 on success, negative on error
1926 public static long flistxattr (int fd, Encoding encoding, out string [] values)
1929 long size = flistxattr (fd, null, 0);
1931 values = new string [0];
1935 byte[] list = new byte [size];
1936 long ret = flistxattr (fd, list, (ulong) size);
1940 GetValues (list, encoding, out values);
1944 public static long flistxattr (int fd, out string[] values)
1946 return flistxattr (fd, UnixEncoding.Instance, out values);
1949 [DllImport (MPH, SetLastError=true,
1950 EntryPoint="Mono_Posix_Syscall_removexattr")]
1951 public static extern int removexattr (
1952 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1954 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1957 [DllImport (MPH, SetLastError=true,
1958 EntryPoint="Mono_Posix_Syscall_lremovexattr")]
1959 public static extern int lremovexattr (
1960 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1962 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1965 [DllImport (MPH, SetLastError=true,
1966 EntryPoint="Mono_Posix_Syscall_fremovexattr")]
1967 public static extern int fremovexattr (int fd,
1968 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1972 #region <dirent.h> Declarations
1976 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1978 [DllImport (LIBC, SetLastError=true)]
1979 public static extern IntPtr opendir (
1980 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1983 [DllImport (LIBC, SetLastError=true)]
1984 public static extern int closedir (IntPtr dir);
1987 // void seekdir (DIR *dir, off_t offset);
1988 // Slight modification. Returns -1 on error, 0 on success.
1989 [DllImport (MPH, SetLastError=true,
1990 EntryPoint="Mono_Posix_Syscall_seekdir")]
1991 public static extern int seekdir (IntPtr dir, long offset);
1994 // off_t telldir(DIR *dir);
1995 [DllImport (MPH, SetLastError=true,
1996 EntryPoint="Mono_Posix_Syscall_telldir")]
1997 public static extern long telldir (IntPtr dir);
1999 [DllImport (MPH, SetLastError=true,
2000 EntryPoint="Mono_Posix_Syscall_rewinddir")]
2001 public static extern int rewinddir (IntPtr dir);
2003 private struct _Dirent {
2004 [ino_t] public ulong d_ino;
2005 [off_t] public long d_off;
2006 public ushort d_reclen;
2008 public IntPtr d_name;
2011 private static void CopyDirent (Dirent to, ref _Dirent from)
2014 to.d_ino = from.d_ino;
2015 to.d_off = from.d_off;
2016 to.d_reclen = from.d_reclen;
2017 to.d_type = from.d_type;
2018 to.d_name = UnixMarshal.PtrToString (from.d_name);
2021 Stdlib.free (from.d_name);
2022 from.d_name = IntPtr.Zero;
2026 internal static object readdir_lock = new object ();
2028 [DllImport (MPH, SetLastError=true,
2029 EntryPoint="Mono_Posix_Syscall_readdir")]
2030 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
2032 public static Dirent readdir (IntPtr dir)
2036 lock (readdir_lock) {
2037 r = sys_readdir (dir, out dentry);
2041 Dirent d = new Dirent ();
2042 CopyDirent (d, ref dentry);
2046 [DllImport (MPH, SetLastError=true,
2047 EntryPoint="Mono_Posix_Syscall_readdir_r")]
2048 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
2050 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
2056 entry.d_name = null;
2059 int r = sys_readdir_r (dirp, out _d, out result);
2061 if (r == 0 && result != IntPtr.Zero) {
2062 CopyDirent (entry, ref _d);
2068 [DllImport (LIBC, SetLastError=true)]
2069 public static extern int dirfd (IntPtr dir);
2071 [DllImport (LIBC, SetLastError=true)]
2072 public static extern IntPtr fdopendir (int fd);
2075 #region <fcntl.h> Declarations
2077 // <fcntl.h> -- COMPLETE
2080 [DllImport (MPH, SetLastError=true,
2081 EntryPoint="Mono_Posix_Syscall_fcntl")]
2082 public static extern int fcntl (int fd, FcntlCommand cmd);
2084 [DllImport (MPH, SetLastError=true,
2085 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
2086 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
2088 public static int fcntl (int fd, FcntlCommand cmd, DirectoryNotifyFlags arg)
2090 if (cmd != FcntlCommand.F_NOTIFY) {
2091 SetLastError (Errno.EINVAL);
2094 long _arg = NativeConvert.FromDirectoryNotifyFlags (arg);
2095 return fcntl (fd, FcntlCommand.F_NOTIFY, _arg);
2098 [DllImport (MPH, SetLastError=true,
2099 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
2100 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
2102 [DllImport (MPH, SetLastError=true,
2103 EntryPoint="Mono_Posix_Syscall_open")]
2104 public static extern int open (
2105 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2106 string pathname, OpenFlags flags);
2109 // int open(const char *pathname, int flags, mode_t mode);
2110 [DllImport (MPH, SetLastError=true,
2111 EntryPoint="Mono_Posix_Syscall_open_mode")]
2112 public static extern int open (
2113 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2114 string pathname, OpenFlags flags, FilePermissions mode);
2117 // int creat(const char *pathname, mode_t mode);
2118 [DllImport (MPH, SetLastError=true,
2119 EntryPoint="Mono_Posix_Syscall_creat")]
2120 public static extern int creat (
2121 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2122 string pathname, FilePermissions mode);
2125 // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
2126 [DllImport (MPH, SetLastError=true,
2127 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
2128 public static extern int posix_fadvise (int fd, long offset,
2129 long len, PosixFadviseAdvice advice);
2131 // posix_fallocate(P)
2132 // int posix_fallocate(int fd, off_t offset, size_t len);
2133 [DllImport (MPH, SetLastError=true,
2134 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
2135 public static extern int posix_fallocate (int fd, long offset, ulong len);
2137 [DllImport (LIBC, SetLastError=true,
2138 EntryPoint="openat")]
2139 private static extern int sys_openat (int dirfd,
2140 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2141 string pathname, int flags);
2144 // int openat(int dirfd, const char *pathname, int flags, mode_t mode);
2145 [DllImport (LIBC, SetLastError=true,
2146 EntryPoint="openat")]
2147 private static extern int sys_openat (int dirfd,
2148 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2149 string pathname, int flags, uint mode);
2151 public static int openat (int dirfd, string pathname, OpenFlags flags)
2153 int _flags = NativeConvert.FromOpenFlags (flags);
2154 return sys_openat (dirfd, pathname, _flags);
2157 public static int openat (int dirfd, string pathname, OpenFlags flags, FilePermissions mode)
2159 int _flags = NativeConvert.FromOpenFlags (flags);
2160 uint _mode = NativeConvert.FromFilePermissions (mode);
2161 return sys_openat (dirfd, pathname, _flags, _mode);
2164 [DllImport (MPH, SetLastError=true,
2165 EntryPoint="Mono_Posix_Syscall_get_at_fdcwd")]
2166 private static extern int get_at_fdcwd ();
2168 public static readonly int AT_FDCWD = get_at_fdcwd ();
2172 #region <fstab.h> Declarations
2174 // <fstab.h> -- COMPLETE
2177 private struct _Fstab {
2178 public IntPtr fs_spec;
2179 public IntPtr fs_file;
2180 public IntPtr fs_vfstype;
2181 public IntPtr fs_mntops;
2182 public IntPtr fs_type;
2184 public int fs_passno;
2185 public IntPtr _fs_buf_;
2188 private static void CopyFstab (Fstab to, ref _Fstab from)
2191 to.fs_spec = UnixMarshal.PtrToString (from.fs_spec);
2192 to.fs_file = UnixMarshal.PtrToString (from.fs_file);
2193 to.fs_vfstype = UnixMarshal.PtrToString (from.fs_vfstype);
2194 to.fs_mntops = UnixMarshal.PtrToString (from.fs_mntops);
2195 to.fs_type = UnixMarshal.PtrToString (from.fs_type);
2196 to.fs_freq = from.fs_freq;
2197 to.fs_passno = from.fs_passno;
2200 Stdlib.free (from._fs_buf_);
2201 from._fs_buf_ = IntPtr.Zero;
2205 internal static object fstab_lock = new object ();
2207 [DllImport (MPH, SetLastError=true,
2208 EntryPoint="Mono_Posix_Syscall_endfsent")]
2209 private static extern int sys_endfsent ();
2211 public static int endfsent ()
2214 return sys_endfsent ();
2218 [DllImport (MPH, SetLastError=true,
2219 EntryPoint="Mono_Posix_Syscall_getfsent")]
2220 private static extern int sys_getfsent (out _Fstab fs);
2222 public static Fstab getfsent ()
2227 r = sys_getfsent (out fsbuf);
2231 Fstab fs = new Fstab ();
2232 CopyFstab (fs, ref fsbuf);
2236 [DllImport (MPH, SetLastError=true,
2237 EntryPoint="Mono_Posix_Syscall_getfsfile")]
2238 private static extern int sys_getfsfile (
2239 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2240 string mount_point, out _Fstab fs);
2242 public static Fstab getfsfile (string mount_point)
2247 r = sys_getfsfile (mount_point, out fsbuf);
2251 Fstab fs = new Fstab ();
2252 CopyFstab (fs, ref fsbuf);
2256 [DllImport (MPH, SetLastError=true,
2257 EntryPoint="Mono_Posix_Syscall_getfsspec")]
2258 private static extern int sys_getfsspec (
2259 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2260 string special_file, out _Fstab fs);
2262 public static Fstab getfsspec (string special_file)
2267 r = sys_getfsspec (special_file, out fsbuf);
2271 Fstab fs = new Fstab ();
2272 CopyFstab (fs, ref fsbuf);
2276 [DllImport (MPH, SetLastError=true,
2277 EntryPoint="Mono_Posix_Syscall_setfsent")]
2278 private static extern int sys_setfsent ();
2280 public static int setfsent ()
2283 return sys_setfsent ();
2289 #region <grp.h> Declarations
2293 // TODO: putgrent(3), fgetgrent_r(), initgroups(3)
2296 [DllImport (LIBC, SetLastError=true, EntryPoint="getgrouplist")]
2297 private static extern int sys_getgrouplist (string user, uint grp, uint [] groups,ref int ngroups);
2299 public static Group [] getgrouplist (string username)
2301 if (username == null)
2302 throw new ArgumentNullException ("username");
2303 if (username.Trim () == "")
2304 throw new ArgumentException ("Username cannot be empty", "username");
2305 // Syscall to getpwnam to retrieve user uid
2306 Passwd pw = Syscall.getpwnam (username);
2308 throw new ArgumentException (string.Format ("User {0} does not exists",username), "username");
2309 return getgrouplist (pw);
2312 public static Group [] getgrouplist (Passwd user)
2315 throw new ArgumentNullException ("user");
2316 // initializing ngroups by 16 to get the group count
2319 // allocating buffer to store group uid's
2320 uint [] groups=null;
2322 Array.Resize (ref groups, ngroups*=2);
2323 res = sys_getgrouplist (user.pw_name, user.pw_gid, groups, ref ngroups);
2326 List<Group> result = new List<Group> ();
2328 for (int i = 0; i < res; i++) {
2329 gr = Syscall.getgrgid (groups [i]);
2333 return result.ToArray ();
2337 // int setgroups (size_t size, const gid_t *list);
2338 [DllImport (MPH, SetLastError=true,
2339 EntryPoint="Mono_Posix_Syscall_setgroups")]
2340 public static extern int setgroups (ulong size, uint[] list);
2342 public static int setgroups (uint [] list)
2344 return setgroups ((ulong) list.Length, list);
2348 private struct _Group
2350 public IntPtr gr_name;
2351 public IntPtr gr_passwd;
2352 [gid_t] public uint gr_gid;
2353 public int _gr_nmem_;
2354 public IntPtr gr_mem;
2355 public IntPtr _gr_buf_;
2358 private static void CopyGroup (Group to, ref _Group from)
2361 to.gr_gid = from.gr_gid;
2362 to.gr_name = UnixMarshal.PtrToString (from.gr_name);
2363 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
2364 to.gr_mem = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
2367 Stdlib.free (from.gr_mem);
2368 Stdlib.free (from._gr_buf_);
2369 from.gr_mem = IntPtr.Zero;
2370 from._gr_buf_ = IntPtr.Zero;
2374 internal static object grp_lock = new object ();
2376 [DllImport (MPH, SetLastError=true,
2377 EntryPoint="Mono_Posix_Syscall_getgrnam")]
2378 private static extern int sys_getgrnam (string name, out _Group group);
2380 public static Group getgrnam (string name)
2385 r = sys_getgrnam (name, out group);
2389 Group gr = new Group ();
2390 CopyGroup (gr, ref group);
2395 // struct group *getgrgid(gid_t gid);
2396 [DllImport (MPH, SetLastError=true,
2397 EntryPoint="Mono_Posix_Syscall_getgrgid")]
2398 private static extern int sys_getgrgid (uint uid, out _Group group);
2400 public static Group getgrgid (uint uid)
2405 r = sys_getgrgid (uid, out group);
2409 Group gr = new Group ();
2410 CopyGroup (gr, ref group);
2414 [DllImport (MPH, SetLastError=true,
2415 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
2416 private static extern int sys_getgrnam_r (
2417 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2418 string name, out _Group grbuf, out IntPtr grbufp);
2420 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
2425 int r = sys_getgrnam_r (name, out group, out _grbufp);
2426 if (r == 0 && _grbufp != IntPtr.Zero) {
2427 CopyGroup (grbuf, ref group);
2434 // int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
2435 // size_t buflen, struct group **gbufp);
2436 [DllImport (MPH, SetLastError=true,
2437 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
2438 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
2440 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
2445 int r = sys_getgrgid_r (uid, out group, out _grbufp);
2446 if (r == 0 && _grbufp != IntPtr.Zero) {
2447 CopyGroup (grbuf, ref group);
2453 [DllImport (MPH, SetLastError=true,
2454 EntryPoint="Mono_Posix_Syscall_getgrent")]
2455 private static extern int sys_getgrent (out _Group grbuf);
2457 public static Group getgrent ()
2462 r = sys_getgrent (out group);
2466 Group gr = new Group();
2467 CopyGroup (gr, ref group);
2471 [DllImport (MPH, SetLastError=true,
2472 EntryPoint="Mono_Posix_Syscall_setgrent")]
2473 private static extern int sys_setgrent ();
2475 public static int setgrent ()
2478 return sys_setgrent ();
2482 [DllImport (MPH, SetLastError=true,
2483 EntryPoint="Mono_Posix_Syscall_endgrent")]
2484 private static extern int sys_endgrent ();
2486 public static int endgrent ()
2489 return sys_endgrent ();
2493 [DllImport (MPH, SetLastError=true,
2494 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
2495 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
2497 public static Group fgetgrent (IntPtr stream)
2502 r = sys_fgetgrent (stream, out group);
2506 Group gr = new Group ();
2507 CopyGroup (gr, ref group);
2512 #region <pwd.h> Declarations
2516 // TODO: putpwent(3), fgetpwent_r()
2518 // SKIPPING: getpw(3): it's dangerous. Use getpwuid(3) instead.
2521 private struct _Passwd
2523 public IntPtr pw_name;
2524 public IntPtr pw_passwd;
2525 [uid_t] public uint pw_uid;
2526 [gid_t] public uint pw_gid;
2527 public IntPtr pw_gecos;
2528 public IntPtr pw_dir;
2529 public IntPtr pw_shell;
2530 public IntPtr _pw_buf_;
2533 private static void CopyPasswd (Passwd to, ref _Passwd from)
2536 to.pw_name = UnixMarshal.PtrToString (from.pw_name);
2537 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
2538 to.pw_uid = from.pw_uid;
2539 to.pw_gid = from.pw_gid;
2540 to.pw_gecos = UnixMarshal.PtrToString (from.pw_gecos);
2541 to.pw_dir = UnixMarshal.PtrToString (from.pw_dir);
2542 to.pw_shell = UnixMarshal.PtrToString (from.pw_shell);
2545 Stdlib.free (from._pw_buf_);
2546 from._pw_buf_ = IntPtr.Zero;
2550 internal static object pwd_lock = new object ();
2552 [DllImport (MPH, SetLastError=true,
2553 EntryPoint="Mono_Posix_Syscall_getpwnam")]
2554 private static extern int sys_getpwnam (string name, out _Passwd passwd);
2556 public static Passwd getpwnam (string name)
2561 r = sys_getpwnam (name, out passwd);
2565 Passwd pw = new Passwd ();
2566 CopyPasswd (pw, ref passwd);
2571 // struct passwd *getpwnuid(uid_t uid);
2572 [DllImport (MPH, SetLastError=true,
2573 EntryPoint="Mono_Posix_Syscall_getpwuid")]
2574 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
2576 public static Passwd getpwuid (uint uid)
2581 r = sys_getpwuid (uid, out passwd);
2585 Passwd pw = new Passwd ();
2586 CopyPasswd (pw, ref passwd);
2590 [DllImport (MPH, SetLastError=true,
2591 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
2592 private static extern int sys_getpwnam_r (
2593 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2594 string name, out _Passwd pwbuf, out IntPtr pwbufp);
2596 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
2601 int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
2602 if (r == 0 && _pwbufp != IntPtr.Zero) {
2603 CopyPasswd (pwbuf, ref passwd);
2610 // int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
2611 // buflen, struct passwd **pwbufp);
2612 [DllImport (MPH, SetLastError=true,
2613 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
2614 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
2616 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
2621 int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
2622 if (r == 0 && _pwbufp != IntPtr.Zero) {
2623 CopyPasswd (pwbuf, ref passwd);
2629 [DllImport (MPH, SetLastError=true,
2630 EntryPoint="Mono_Posix_Syscall_getpwent")]
2631 private static extern int sys_getpwent (out _Passwd pwbuf);
2633 public static Passwd getpwent ()
2638 r = sys_getpwent (out passwd);
2642 Passwd pw = new Passwd ();
2643 CopyPasswd (pw, ref passwd);
2647 [DllImport (MPH, SetLastError=true,
2648 EntryPoint="Mono_Posix_Syscall_setpwent")]
2649 private static extern int sys_setpwent ();
2651 public static int setpwent ()
2654 return sys_setpwent ();
2658 [DllImport (MPH, SetLastError=true,
2659 EntryPoint="Mono_Posix_Syscall_endpwent")]
2660 private static extern int sys_endpwent ();
2662 public static int endpwent ()
2665 return sys_endpwent ();
2669 [DllImport (MPH, SetLastError=true,
2670 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
2671 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
2673 public static Passwd fgetpwent (IntPtr stream)
2678 r = sys_fgetpwent (stream, out passwd);
2682 Passwd pw = new Passwd ();
2683 CopyPasswd (pw, ref passwd);
2688 #region <signal.h> Declarations
2692 [DllImport (MPH, SetLastError=true,
2693 EntryPoint="Mono_Posix_Syscall_psignal")]
2694 private static extern int psignal (int sig, string s);
2696 public static int psignal (Signum sig, string s)
2698 int signum = NativeConvert.FromSignum (sig);
2699 return psignal (signum, s);
2703 // int kill(pid_t pid, int sig);
2704 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
2705 private static extern int sys_kill (int pid, int sig);
2707 public static int kill (int pid, Signum sig)
2709 int _sig = NativeConvert.FromSignum (sig);
2710 return sys_kill (pid, _sig);
2713 private static object signal_lock = new object ();
2715 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
2716 private static extern IntPtr sys_strsignal (int sig);
2718 public static string strsignal (Signum sig)
2720 int s = NativeConvert.FromSignum (sig);
2721 lock (signal_lock) {
2722 IntPtr r = sys_strsignal (s);
2723 return UnixMarshal.PtrToString (r);
2727 // TODO: sigaction(2)
2728 // TODO: sigsuspend(2)
2729 // TODO: sigpending(2)
2733 #region <stdio.h> Declarations
2737 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
2738 private static extern int _L_ctermid ();
2740 public static readonly int L_ctermid = _L_ctermid ();
2742 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
2743 private static extern int _L_cuserid ();
2745 public static readonly int L_cuserid = _L_cuserid ();
2747 internal static object getlogin_lock = new object ();
2749 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
2750 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
2752 [Obsolete ("\"Nobody knows precisely what cuserid() does... " +
2753 "DO NOT USE cuserid().\n" +
2754 "`string' must hold L_cuserid characters. Use getlogin_r instead.")]
2755 public static string cuserid (StringBuilder @string)
2757 if (@string.Capacity < L_cuserid) {
2758 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
2760 lock (getlogin_lock) {
2761 IntPtr r = sys_cuserid (@string);
2762 return UnixMarshal.PtrToString (r);
2766 [DllImport (LIBC, SetLastError=true)]
2767 public static extern int renameat (int olddirfd,
2768 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2769 string oldpath, int newdirfd,
2770 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2774 #region <stdlib.h> Declarations
2778 [DllImport (LIBC, SetLastError=true)]
2779 public static extern int mkstemp (StringBuilder template);
2781 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdtemp")]
2782 private static extern IntPtr sys_mkdtemp (StringBuilder template);
2784 public static StringBuilder mkdtemp (StringBuilder template)
2786 if (sys_mkdtemp (template) == IntPtr.Zero)
2791 [DllImport (LIBC, SetLastError=true)]
2792 public static extern int ttyslot ();
2794 [Obsolete ("This is insecure and should not be used", true)]
2795 public static int setkey (string key)
2797 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
2802 #region <string.h> Declarations
2808 // int strerror_r(int errnum, char *buf, size_t n);
2809 [DllImport (MPH, SetLastError=true,
2810 EntryPoint="Mono_Posix_Syscall_strerror_r")]
2811 private static extern int sys_strerror_r (int errnum,
2812 [Out] StringBuilder buf, ulong n);
2814 public static int strerror_r (Errno errnum, StringBuilder buf, ulong n)
2816 int e = NativeConvert.FromErrno (errnum);
2817 return sys_strerror_r (e, buf, n);
2820 public static int strerror_r (Errno errnum, StringBuilder buf)
2822 return strerror_r (errnum, buf, (ulong) buf.Capacity);
2827 #region <sys/epoll.h> Declarations
2829 public static int epoll_create (int size)
2831 return sys_epoll_create (size);
2834 public static int epoll_create (EpollFlags flags)
2836 return sys_epoll_create1 (flags);
2839 public static int epoll_ctl (int epfd, EpollOp op, int fd, EpollEvents events)
2841 EpollEvent ee = new EpollEvent ();
2845 return sys_epoll_ctl (epfd, op, fd, ref ee);
2848 public static int epoll_wait (int epfd, EpollEvent [] events, int max_events, int timeout)
2850 if (events.Length < max_events)
2851 throw new ArgumentOutOfRangeException ("events", "Must refer to at least 'max_events' elements.");
2853 return sys_epoll_wait (epfd, events, max_events, timeout);
2856 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_create")]
2857 private static extern int sys_epoll_create (int size);
2859 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_create1")]
2860 private static extern int sys_epoll_create1 (EpollFlags flags);
2862 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_ctl")]
2863 private static extern int sys_epoll_ctl (int epfd, EpollOp op, int fd, ref EpollEvent ee);
2865 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_wait")]
2866 private static extern int sys_epoll_wait (int epfd, EpollEvent [] ee, int maxevents, int timeout);
2869 #region <sys/mman.h> Declarations
2875 // int posix_madvise(void *addr, size_t len, int advice);
2876 [DllImport (MPH, SetLastError=true,
2877 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
2878 public static extern int posix_madvise (IntPtr addr, ulong len,
2879 PosixMadviseAdvice advice);
2881 public static readonly IntPtr MAP_FAILED = unchecked((IntPtr)(-1));
2883 [DllImport (MPH, SetLastError=true,
2884 EntryPoint="Mono_Posix_Syscall_mmap")]
2885 public static extern IntPtr mmap (IntPtr start, ulong length,
2886 MmapProts prot, MmapFlags flags, int fd, long offset);
2888 [DllImport (MPH, SetLastError=true,
2889 EntryPoint="Mono_Posix_Syscall_munmap")]
2890 public static extern int munmap (IntPtr start, ulong length);
2892 [DllImport (MPH, SetLastError=true,
2893 EntryPoint="Mono_Posix_Syscall_mprotect")]
2894 public static extern int mprotect (IntPtr start, ulong len, MmapProts prot);
2896 [DllImport (MPH, SetLastError=true,
2897 EntryPoint="Mono_Posix_Syscall_msync")]
2898 public static extern int msync (IntPtr start, ulong len, MsyncFlags flags);
2900 [DllImport (MPH, SetLastError=true,
2901 EntryPoint="Mono_Posix_Syscall_mlock")]
2902 public static extern int mlock (IntPtr start, ulong len);
2904 [DllImport (MPH, SetLastError=true,
2905 EntryPoint="Mono_Posix_Syscall_munlock")]
2906 public static extern int munlock (IntPtr start, ulong len);
2908 [DllImport (LIBC, SetLastError=true, EntryPoint="mlockall")]
2909 private static extern int sys_mlockall (int flags);
2911 public static int mlockall (MlockallFlags flags)
2913 int _flags = NativeConvert.FromMlockallFlags (flags);
2914 return sys_mlockall (_flags);
2917 [DllImport (LIBC, SetLastError=true)]
2918 public static extern int munlockall ();
2920 [DllImport (MPH, SetLastError=true,
2921 EntryPoint="Mono_Posix_Syscall_mremap")]
2922 public static extern IntPtr mremap (IntPtr old_address, ulong old_size,
2923 ulong new_size, MremapFlags flags);
2925 [DllImport (MPH, SetLastError=true,
2926 EntryPoint="Mono_Posix_Syscall_mincore")]
2927 public static extern int mincore (IntPtr start, ulong length, byte[] vec);
2929 [DllImport (MPH, SetLastError=true,
2930 EntryPoint="Mono_Posix_Syscall_remap_file_pages")]
2931 public static extern int remap_file_pages (IntPtr start, ulong size,
2932 MmapProts prot, long pgoff, MmapFlags flags);
2936 #region <sys/poll.h> Declarations
2938 // <sys/poll.h> -- COMPLETE
2941 private struct _pollfd {
2943 public short events;
2944 public short revents;
2947 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
2948 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
2950 public static int poll (Pollfd [] fds, uint nfds, int timeout)
2952 if (fds.Length < nfds)
2953 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
2955 _pollfd[] send = new _pollfd[nfds];
2957 for (int i = 0; i < send.Length; i++) {
2958 send [i].fd = fds [i].fd;
2959 send [i].events = NativeConvert.FromPollEvents (fds [i].events);
2962 int r = sys_poll (send, nfds, timeout);
2964 for (int i = 0; i < send.Length; i++) {
2965 fds [i].revents = NativeConvert.ToPollEvents (send [i].revents);
2971 public static int poll (Pollfd [] fds, int timeout)
2973 return poll (fds, (uint) fds.Length, timeout);
2986 // TODO: setrlimit(2)
2987 // TODO: getrlimit(2)
2988 // TODO: getrusage(2)
2992 #region <sys/sendfile.h> Declarations
2994 // <sys/sendfile.h> -- COMPLETE
2997 [DllImport (MPH, SetLastError=true,
2998 EntryPoint="Mono_Posix_Syscall_sendfile")]
2999 public static extern long sendfile (int out_fd, int in_fd,
3000 ref long offset, ulong count);
3004 #region <sys/stat.h> Declarations
3006 // <sys/stat.h> -- COMPLETE
3008 [DllImport (MPH, SetLastError=true,
3009 EntryPoint="Mono_Posix_Syscall_stat")]
3010 public static extern int stat (
3011 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3012 string file_name, out Stat buf);
3014 [DllImport (MPH, SetLastError=true,
3015 EntryPoint="Mono_Posix_Syscall_fstat")]
3016 public static extern int fstat (int filedes, out Stat buf);
3018 [DllImport (MPH, SetLastError=true,
3019 EntryPoint="Mono_Posix_Syscall_lstat")]
3020 public static extern int lstat (
3021 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3022 string file_name, out Stat buf);
3025 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
3026 // All take FilePermissions
3029 // int chmod(const char *path, mode_t mode);
3030 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
3031 private static extern int sys_chmod (
3032 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3033 string path, uint mode);
3035 public static int chmod (string path, FilePermissions mode)
3037 uint _mode = NativeConvert.FromFilePermissions (mode);
3038 return sys_chmod (path, _mode);
3042 // int chmod(int filedes, mode_t mode);
3043 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
3044 private static extern int sys_fchmod (int filedes, uint mode);
3046 public static int fchmod (int filedes, FilePermissions mode)
3048 uint _mode = NativeConvert.FromFilePermissions (mode);
3049 return sys_fchmod (filedes, _mode);
3053 // mode_t umask(mode_t mask);
3054 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
3055 private static extern uint sys_umask (uint mask);
3057 public static FilePermissions umask (FilePermissions mask)
3059 uint _mask = NativeConvert.FromFilePermissions (mask);
3060 uint r = sys_umask (_mask);
3061 return NativeConvert.ToFilePermissions (r);
3065 // int mkdir(const char *pathname, mode_t mode);
3066 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
3067 private static extern int sys_mkdir (
3068 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3069 string oldpath, uint mode);
3071 public static int mkdir (string oldpath, FilePermissions mode)
3073 uint _mode = NativeConvert.FromFilePermissions (mode);
3074 return sys_mkdir (oldpath, _mode);
3078 // int mknod (const char *pathname, mode_t mode, dev_t dev);
3079 [DllImport (MPH, SetLastError=true,
3080 EntryPoint="Mono_Posix_Syscall_mknod")]
3081 public static extern int mknod (
3082 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3083 string pathname, FilePermissions mode, ulong dev);
3086 // int mkfifo(const char *pathname, mode_t mode);
3087 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
3088 private static extern int sys_mkfifo (
3089 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3090 string pathname, uint mode);
3092 public static int mkfifo (string pathname, FilePermissions mode)
3094 uint _mode = NativeConvert.FromFilePermissions (mode);
3095 return sys_mkfifo (pathname, _mode);
3099 // int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
3100 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmodat")]
3101 private static extern int sys_fchmodat (int dirfd,
3102 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3103 string pathname, uint mode, int flags);
3105 public static int fchmodat (int dirfd, string pathname, FilePermissions mode, AtFlags flags)
3107 uint _mode = NativeConvert.FromFilePermissions (mode);
3108 int _flags = NativeConvert.FromAtFlags (flags);
3109 return sys_fchmodat (dirfd, pathname, _mode, _flags);
3112 [DllImport (MPH, SetLastError=true,
3113 EntryPoint="Mono_Posix_Syscall_fstatat")]
3114 public static extern int fstatat (int dirfd,
3115 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3116 string file_name, out Stat buf, AtFlags flags);
3118 [DllImport (MPH, SetLastError=true,
3119 EntryPoint="Mono_Posix_Syscall_get_utime_now")]
3120 private static extern long get_utime_now ();
3122 [DllImport (MPH, SetLastError=true,
3123 EntryPoint="Mono_Posix_Syscall_get_utime_omit")]
3124 private static extern long get_utime_omit ();
3126 public static readonly long UTIME_NOW = get_utime_now ();
3128 public static readonly long UTIME_OMIT = get_utime_omit ();
3130 [DllImport (MPH, SetLastError=true,
3131 EntryPoint="Mono_Posix_Syscall_futimens")]
3132 private static extern int sys_futimens (int fd, Timespec[] times);
3134 public static int futimens (int fd, Timespec[] times)
3136 if (times != null && times.Length != 2) {
3137 SetLastError (Errno.EINVAL);
3140 return sys_futimens (fd, times);
3143 [DllImport (MPH, SetLastError=true,
3144 EntryPoint="Mono_Posix_Syscall_utimensat")]
3145 private static extern int sys_utimensat (int dirfd,
3146 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3147 string pathname, Timespec[] times, int flags);
3149 public static int utimensat (int dirfd, string pathname, Timespec[] times, AtFlags flags)
3151 if (times != null && times.Length != 2) {
3152 SetLastError (Errno.EINVAL);
3155 int _flags = NativeConvert.FromAtFlags (flags);
3156 return sys_utimensat (dirfd, pathname, times, _flags);
3160 // int mkdirat(int dirfd, const char *pathname, mode_t mode);
3161 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdirat")]
3162 private static extern int sys_mkdirat (int dirfd,
3163 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3164 string oldpath, uint mode);
3166 public static int mkdirat (int dirfd, string oldpath, FilePermissions mode)
3168 uint _mode = NativeConvert.FromFilePermissions (mode);
3169 return sys_mkdirat (dirfd, oldpath, _mode);
3173 // int mknodat (int dirfd, const char *pathname, mode_t mode, dev_t dev);
3174 [DllImport (MPH, SetLastError=true,
3175 EntryPoint="Mono_Posix_Syscall_mknodat")]
3176 public static extern int mknodat (int dirfd,
3177 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3178 string pathname, FilePermissions mode, ulong dev);
3181 // int mkfifoat(int dirfd, const char *pathname, mode_t mode);
3182 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifoat")]
3183 private static extern int sys_mkfifoat (int dirfd,
3184 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3185 string pathname, uint mode);
3187 public static int mkfifoat (int dirfd, string pathname, FilePermissions mode)
3189 uint _mode = NativeConvert.FromFilePermissions (mode);
3190 return sys_mkfifoat (dirfd, pathname, _mode);
3194 #region <sys/stat.h> Declarations
3199 [DllImport (MPH, SetLastError=true,
3200 EntryPoint="Mono_Posix_Syscall_statvfs")]
3201 public static extern int statvfs (
3202 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3203 string path, out Statvfs buf);
3205 [DllImport (MPH, SetLastError=true,
3206 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
3207 public static extern int fstatvfs (int fd, out Statvfs buf);
3211 #region <sys/time.h> Declarations
3215 // TODO: adjtime(), getitimer(2), setitimer(2)
3217 [DllImport (MPH, SetLastError=true,
3218 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3219 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
3221 [DllImport (MPH, SetLastError=true,
3222 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3223 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
3225 public static int gettimeofday (out Timeval tv)
3227 return gettimeofday (out tv, IntPtr.Zero);
3230 [DllImport (MPH, SetLastError=true,
3231 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3232 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
3234 public static int gettimeofday (out Timezone tz)
3236 return gettimeofday (IntPtr.Zero, out tz);
3239 [DllImport (MPH, SetLastError=true,
3240 EntryPoint="Mono_Posix_Syscall_settimeofday")]
3241 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
3243 [DllImport (MPH, SetLastError=true,
3244 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3245 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
3247 public static int settimeofday (ref Timeval tv)
3249 return settimeofday (ref tv, IntPtr.Zero);
3252 [DllImport (MPH, SetLastError=true,
3253 EntryPoint="Mono_Posix_Syscall_utimes")]
3254 private static extern int sys_utimes (
3255 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3256 string filename, Timeval[] tvp);
3258 public static int utimes (string filename, Timeval[] tvp)
3260 if (tvp != null && tvp.Length != 2) {
3261 SetLastError (Errno.EINVAL);
3264 return sys_utimes (filename, tvp);
3267 [DllImport (MPH, SetLastError=true,
3268 EntryPoint="Mono_Posix_Syscall_lutimes")]
3269 private static extern int sys_lutimes (
3270 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3271 string filename, Timeval[] tvp);
3273 public static int lutimes (string filename, Timeval[] tvp)
3275 if (tvp != null && tvp.Length != 2) {
3276 SetLastError (Errno.EINVAL);
3279 return sys_lutimes (filename, tvp);
3282 [DllImport (MPH, SetLastError=true,
3283 EntryPoint="Mono_Posix_Syscall_futimes")]
3284 private static extern int sys_futimes (int fd, Timeval[] tvp);
3286 public static int futimes (int fd, Timeval[] tvp)
3288 if (tvp != null && tvp.Length != 2) {
3289 SetLastError (Errno.EINVAL);
3292 return sys_futimes (fd, tvp);
3314 private struct _Utsname
3316 public IntPtr sysname;
3317 public IntPtr nodename;
3318 public IntPtr release;
3319 public IntPtr version;
3320 public IntPtr machine;
3321 public IntPtr domainname;
3322 public IntPtr _buf_;
3325 private static void CopyUtsname (ref Utsname to, ref _Utsname from)
3328 to = new Utsname ();
3329 to.sysname = UnixMarshal.PtrToString (from.sysname);
3330 to.nodename = UnixMarshal.PtrToString (from.nodename);
3331 to.release = UnixMarshal.PtrToString (from.release);
3332 to.version = UnixMarshal.PtrToString (from.version);
3333 to.machine = UnixMarshal.PtrToString (from.machine);
3334 to.domainname = UnixMarshal.PtrToString (from.domainname);
3337 Stdlib.free (from._buf_);
3338 from._buf_ = IntPtr.Zero;
3342 [DllImport (MPH, SetLastError=true,
3343 EntryPoint="Mono_Posix_Syscall_uname")]
3344 private static extern int sys_uname (out _Utsname buf);
3346 public static int uname (out Utsname buf)
3349 int r = sys_uname (out _buf);
3350 buf = new Utsname ();
3352 CopyUtsname (ref buf, ref _buf);
3357 #region <sys/wait.h> Declarations
3363 // pid_t wait(int *status);
3364 [DllImport (LIBC, SetLastError=true)]
3365 public static extern int wait (out int status);
3368 // pid_t waitpid(pid_t pid, int *status, int options);
3369 [DllImport (LIBC, SetLastError=true)]
3370 private static extern int waitpid (int pid, out int status, int options);
3372 public static int waitpid (int pid, out int status, WaitOptions options)
3374 int _options = NativeConvert.FromWaitOptions (options);
3375 return waitpid (pid, out status, _options);
3378 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
3379 private static extern int _WIFEXITED (int status);
3381 public static bool WIFEXITED (int status)
3383 return _WIFEXITED (status) != 0;
3386 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
3387 public static extern int WEXITSTATUS (int status);
3389 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
3390 private static extern int _WIFSIGNALED (int status);
3392 public static bool WIFSIGNALED (int status)
3394 return _WIFSIGNALED (status) != 0;
3397 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
3398 private static extern int _WTERMSIG (int status);
3400 public static Signum WTERMSIG (int status)
3402 int r = _WTERMSIG (status);
3403 return NativeConvert.ToSignum (r);
3406 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
3407 private static extern int _WIFSTOPPED (int status);
3409 public static bool WIFSTOPPED (int status)
3411 return _WIFSTOPPED (status) != 0;
3414 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
3415 private static extern int _WSTOPSIG (int status);
3417 public static Signum WSTOPSIG (int status)
3419 int r = _WSTOPSIG (status);
3420 return NativeConvert.ToSignum (r);
3429 #region <syslog.h> Declarations
3434 [DllImport (MPH, SetLastError=true,
3435 EntryPoint="Mono_Posix_Syscall_openlog")]
3436 private static extern int sys_openlog (IntPtr ident, int option, int facility);
3438 public static int openlog (IntPtr ident, SyslogOptions option,
3439 SyslogFacility defaultFacility)
3441 int _option = NativeConvert.FromSyslogOptions (option);
3442 int _facility = NativeConvert.FromSyslogFacility (defaultFacility);
3444 return sys_openlog (ident, _option, _facility);
3447 [DllImport (MPH, SetLastError=true,
3448 EntryPoint="Mono_Posix_Syscall_syslog")]
3449 private static extern int sys_syslog (int priority, string message);
3451 public static int syslog (SyslogFacility facility, SyslogLevel level, string message)
3453 int _facility = NativeConvert.FromSyslogFacility (facility);
3454 int _level = NativeConvert.FromSyslogLevel (level);
3455 return sys_syslog (_facility | _level, GetSyslogMessage (message));
3458 public static int syslog (SyslogLevel level, string message)
3460 int _level = NativeConvert.FromSyslogLevel (level);
3461 return sys_syslog (_level, GetSyslogMessage (message));
3464 private static string GetSyslogMessage (string message)
3466 return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
3469 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
3470 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
3471 public static int syslog (SyslogFacility facility, SyslogLevel level,
3472 string format, params object[] parameters)
3474 int _facility = NativeConvert.FromSyslogFacility (facility);
3475 int _level = NativeConvert.FromSyslogLevel (level);
3477 object[] _parameters = new object[checked(parameters.Length+2)];
3478 _parameters [0] = _facility | _level;
3479 _parameters [1] = format;
3480 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3481 return (int) XPrintfFunctions.syslog (_parameters);
3484 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
3485 "Use syslog(SyslogLevel, string) instead.")]
3486 public static int syslog (SyslogLevel level, string format,
3487 params object[] parameters)
3489 int _level = NativeConvert.FromSyslogLevel (level);
3491 object[] _parameters = new object[checked(parameters.Length+2)];
3492 _parameters [0] = _level;
3493 _parameters [1] = format;
3494 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3495 return (int) XPrintfFunctions.syslog (_parameters);
3498 [DllImport (MPH, SetLastError=true,
3499 EntryPoint="Mono_Posix_Syscall_closelog")]
3500 public static extern int closelog ();
3502 [DllImport (LIBC, SetLastError=true, EntryPoint="setlogmask")]
3503 private static extern int sys_setlogmask (int mask);
3505 public static int setlogmask (SyslogLevel mask)
3507 int _mask = NativeConvert.FromSyslogLevel (mask);
3508 return sys_setlogmask (_mask);
3513 #region <time.h> Declarations
3520 // int nanosleep(const struct timespec *req, struct timespec *rem);
3521 [DllImport (MPH, SetLastError=true,
3522 EntryPoint="Mono_Posix_Syscall_nanosleep")]
3523 public static extern int nanosleep (ref Timespec req, ref Timespec rem);
3526 // int stime(time_t *t);
3527 [DllImport (MPH, SetLastError=true,
3528 EntryPoint="Mono_Posix_Syscall_stime")]
3529 public static extern int stime (ref long t);
3532 // time_t time(time_t *t);
3533 [DllImport (MPH, SetLastError=true,
3534 EntryPoint="Mono_Posix_Syscall_time")]
3535 public static extern long time (out long t);
3545 #region <unistd.h> Declarations
3549 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
3550 // other TODOs listed below.
3552 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
3553 private static extern int sys_access (
3554 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3555 string pathname, int mode);
3557 public static int access (string pathname, AccessModes mode)
3559 int _mode = NativeConvert.FromAccessModes (mode);
3560 return sys_access (pathname, _mode);
3564 // off_t lseek(int filedes, off_t offset, int whence);
3565 [DllImport (MPH, SetLastError=true,
3566 EntryPoint="Mono_Posix_Syscall_lseek")]
3567 private static extern long sys_lseek (int fd, long offset, int whence);
3569 public static long lseek (int fd, long offset, SeekFlags whence)
3571 short _whence = NativeConvert.FromSeekFlags (whence);
3572 return sys_lseek (fd, offset, _whence);
3575 [DllImport (LIBC, SetLastError=true)]
3576 public static extern int close (int fd);
3579 // ssize_t read(int fd, void *buf, size_t count);
3580 [DllImport (MPH, SetLastError=true,
3581 EntryPoint="Mono_Posix_Syscall_read")]
3582 public static extern long read (int fd, IntPtr buf, ulong count);
3584 public static unsafe long read (int fd, void *buf, ulong count)
3586 return read (fd, (IntPtr) buf, count);
3590 // ssize_t write(int fd, const void *buf, size_t count);
3591 [DllImport (MPH, SetLastError=true,
3592 EntryPoint="Mono_Posix_Syscall_write")]
3593 public static extern long write (int fd, IntPtr buf, ulong count);
3595 public static unsafe long write (int fd, void *buf, ulong count)
3597 return write (fd, (IntPtr) buf, count);
3601 // ssize_t pread(int fd, void *buf, size_t count, off_t offset);
3602 [DllImport (MPH, SetLastError=true,
3603 EntryPoint="Mono_Posix_Syscall_pread")]
3604 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
3606 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
3608 return pread (fd, (IntPtr) buf, count, offset);
3612 // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
3613 [DllImport (MPH, SetLastError=true,
3614 EntryPoint="Mono_Posix_Syscall_pwrite")]
3615 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
3617 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
3619 return pwrite (fd, (IntPtr) buf, count, offset);
3622 [DllImport (MPH, SetLastError=true,
3623 EntryPoint="Mono_Posix_Syscall_pipe")]
3624 public static extern int pipe (out int reading, out int writing);
3626 public static int pipe (int[] filedes)
3628 if (filedes == null || filedes.Length != 2) {
3632 int reading, writing;
3633 int r = pipe (out reading, out writing);
3634 filedes[0] = reading;
3635 filedes[1] = writing;
3639 [DllImport (LIBC, SetLastError=true)]
3640 public static extern uint alarm (uint seconds);
3642 [DllImport (LIBC, SetLastError=true)]
3643 public static extern uint sleep (uint seconds);
3645 [DllImport (LIBC, SetLastError=true)]
3646 public static extern uint ualarm (uint usecs, uint interval);
3648 [DllImport (LIBC, SetLastError=true)]
3649 public static extern int pause ();
3652 // int chown(const char *path, uid_t owner, gid_t group);
3653 [DllImport (LIBC, SetLastError=true)]
3654 public static extern int chown (
3655 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3656 string path, uint owner, uint group);
3659 // int fchown(int fd, uid_t owner, gid_t group);
3660 [DllImport (LIBC, SetLastError=true)]
3661 public static extern int fchown (int fd, uint owner, uint group);
3664 // int lchown(const char *path, uid_t owner, gid_t group);
3665 [DllImport (LIBC, SetLastError=true)]
3666 public static extern int lchown (
3667 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3668 string path, uint owner, uint group);
3670 [DllImport (LIBC, SetLastError=true)]
3671 public static extern int chdir (
3672 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3675 [DllImport (LIBC, SetLastError=true)]
3676 public static extern int fchdir (int fd);
3679 // char *getcwd(char *buf, size_t size);
3680 [DllImport (MPH, SetLastError=true,
3681 EntryPoint="Mono_Posix_Syscall_getcwd")]
3682 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
3684 public static StringBuilder getcwd (StringBuilder buf)
3686 getcwd (buf, (ulong) buf.Capacity);
3690 // getwd(2) is deprecated; don't expose it.
3692 [DllImport (LIBC, SetLastError=true)]
3693 public static extern int dup (int fd);
3695 [DllImport (LIBC, SetLastError=true)]
3696 public static extern int dup2 (int fd, int fd2);
3698 // TODO: does Mono marshal arrays properly?
3699 [DllImport (LIBC, SetLastError=true)]
3700 public static extern int execve (
3701 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3702 string path, string[] argv, string[] envp);
3704 [DllImport (LIBC, SetLastError=true)]
3705 public static extern int fexecve (int fd, string[] argv, string[] envp);
3707 [DllImport (LIBC, SetLastError=true)]
3708 public static extern int execv (
3709 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3710 string path, string[] argv);
3712 // TODO: execle, execl, execlp
3713 [DllImport (LIBC, SetLastError=true)]
3714 public static extern int execvp (
3715 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3716 string path, string[] argv);
3718 [DllImport (LIBC, SetLastError=true)]
3719 public static extern int nice (int inc);
3721 [DllImport (LIBC, SetLastError=true)]
3722 [CLSCompliant (false)]
3723 public static extern int _exit (int status);
3725 [DllImport (MPH, SetLastError=true,
3726 EntryPoint="Mono_Posix_Syscall_fpathconf")]
3727 public static extern long fpathconf (int filedes, PathconfName name, Errno defaultError);
3729 public static long fpathconf (int filedes, PathconfName name)
3731 return fpathconf (filedes, name, (Errno) 0);
3734 [DllImport (MPH, SetLastError=true,
3735 EntryPoint="Mono_Posix_Syscall_pathconf")]
3736 public static extern long pathconf (
3737 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3738 string path, PathconfName name, Errno defaultError);
3740 public static long pathconf (string path, PathconfName name)
3742 return pathconf (path, name, (Errno) 0);
3745 [DllImport (MPH, SetLastError=true,
3746 EntryPoint="Mono_Posix_Syscall_sysconf")]
3747 public static extern long sysconf (SysconfName name, Errno defaultError);
3749 public static long sysconf (SysconfName name)
3751 return sysconf (name, (Errno) 0);
3755 // size_t confstr(int name, char *buf, size_t len);
3756 [DllImport (MPH, SetLastError=true,
3757 EntryPoint="Mono_Posix_Syscall_confstr")]
3758 public static extern ulong confstr (ConfstrName name, [Out] StringBuilder buf, ulong len);
3761 // pid_t getpid(void);
3762 [DllImport (LIBC, SetLastError=true)]
3763 public static extern int getpid ();
3766 // pid_t getppid(void);
3767 [DllImport (LIBC, SetLastError=true)]
3768 public static extern int getppid ();
3771 // int setpgid(pid_t pid, pid_t pgid);
3772 [DllImport (LIBC, SetLastError=true)]
3773 public static extern int setpgid (int pid, int pgid);
3776 // pid_t getpgid(pid_t pid);
3777 [DllImport (LIBC, SetLastError=true)]
3778 public static extern int getpgid (int pid);
3780 [DllImport (LIBC, SetLastError=true)]
3781 public static extern int setpgrp ();
3784 // pid_t getpgrp(void);
3785 [DllImport (LIBC, SetLastError=true)]
3786 public static extern int getpgrp ();
3789 // pid_t setsid(void);
3790 [DllImport (LIBC, SetLastError=true)]
3791 public static extern int setsid ();
3794 // pid_t getsid(pid_t pid);
3795 [DllImport (LIBC, SetLastError=true)]
3796 public static extern int getsid (int pid);
3799 // uid_t getuid(void);
3800 [DllImport (LIBC, SetLastError=true)]
3801 public static extern uint getuid ();
3804 // uid_t geteuid(void);
3805 [DllImport (LIBC, SetLastError=true)]
3806 public static extern uint geteuid ();
3809 // gid_t getgid(void);
3810 [DllImport (LIBC, SetLastError=true)]
3811 public static extern uint getgid ();
3814 // gid_t getgid(void);
3815 [DllImport (LIBC, SetLastError=true)]
3816 public static extern uint getegid ();
3819 // int getgroups(int size, gid_t list[]);
3820 [DllImport (LIBC, SetLastError=true)]
3821 public static extern int getgroups (int size, uint[] list);
3823 public static int getgroups (uint[] list)
3825 return getgroups (list.Length, list);
3829 // int setuid(uid_t uid);
3830 [DllImport (LIBC, SetLastError=true)]
3831 public static extern int setuid (uint uid);
3834 // int setreuid(uid_t ruid, uid_t euid);
3835 [DllImport (LIBC, SetLastError=true)]
3836 public static extern int setreuid (uint ruid, uint euid);
3839 // int setregid(gid_t ruid, gid_t euid);
3840 [DllImport (LIBC, SetLastError=true)]
3841 public static extern int setregid (uint rgid, uint egid);
3844 // int seteuid(uid_t euid);
3845 [DllImport (LIBC, SetLastError=true)]
3846 public static extern int seteuid (uint euid);
3849 // int setegid(gid_t euid);
3850 [DllImport (LIBC, SetLastError=true)]
3851 public static extern int setegid (uint uid);
3854 // int setgid(gid_t gid);
3855 [DllImport (LIBC, SetLastError=true)]
3856 public static extern int setgid (uint gid);
3859 // int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
3860 [DllImport (LIBC, SetLastError=true)]
3861 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
3864 // int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
3865 [DllImport (LIBC, SetLastError=true)]
3866 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
3869 // int setresuid(uid_t ruid, uid_t euid, uid_t suid);
3870 [DllImport (LIBC, SetLastError=true)]
3871 public static extern int setresuid (uint ruid, uint euid, uint suid);
3874 // int setresgid(gid_t ruid, gid_t euid, gid_t suid);
3875 [DllImport (LIBC, SetLastError=true)]
3876 public static extern int setresgid (uint rgid, uint egid, uint sgid);
3880 // pid_t fork(void);
3881 [DllImport (LIBC, SetLastError=true)]
3882 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " +
3883 "shutdown code.\nUse System.Diagnostics.Process instead")]
3884 private static extern int fork ();
3887 // pid_t vfork(void);
3888 [DllImport (LIBC, SetLastError=true)]
3889 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " +
3890 "shutdown code.\nUse System.Diagnostics.Process instead")]
3891 private static extern int vfork ();
3894 private static object tty_lock = new object ();
3896 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
3897 private static extern IntPtr sys_ttyname (int fd);
3899 public static string ttyname (int fd)
3902 IntPtr r = sys_ttyname (fd);
3903 return UnixMarshal.PtrToString (r);
3908 // int ttyname_r(int fd, char *buf, size_t buflen);
3909 [DllImport (MPH, SetLastError=true,
3910 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
3911 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
3913 public static int ttyname_r (int fd, StringBuilder buf)
3915 return ttyname_r (fd, buf, (ulong) buf.Capacity);
3918 [DllImport (LIBC, EntryPoint="isatty")]
3919 private static extern int sys_isatty (int fd);
3921 public static bool isatty (int fd)
3923 return sys_isatty (fd) == 1;
3926 [DllImport (LIBC, SetLastError=true)]
3927 public static extern int link (
3928 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3930 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3933 [DllImport (LIBC, SetLastError=true)]
3934 public static extern int symlink (
3935 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3937 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3940 delegate long DoReadlinkFun (byte[] target);
3942 // Helper function for readlink(string, StringBuilder) and readlinkat (int, string, StringBuilder)
3943 static int ReadlinkIntoStringBuilder (DoReadlinkFun doReadlink, [Out] StringBuilder buf, ulong bufsiz)
3945 // bufsiz > int.MaxValue can't work because StringBuilder can store only int.MaxValue chars
3946 int bufsizInt = checked ((int) bufsiz);
3947 var target = new byte [bufsizInt];
3949 var r = doReadlink (target);
3951 return checked ((int) r);
3954 var chars = UnixEncoding.Instance.GetChars (target, 0, checked ((int) r));
3955 // Make sure that at more bufsiz chars are written
3956 buf.Append (chars, 0, System.Math.Min (bufsizInt, chars.Length));
3957 if (r == bufsizInt) {
3958 // may not have read full contents; fill 'buf' so that caller can properly check
3959 buf.Append (new string ('\x00', bufsizInt - buf.Length));
3965 // ssize_t readlink(const char *path, char *buf, size_t bufsize);
3966 public static int readlink (string path, [Out] StringBuilder buf, ulong bufsiz)
3968 return ReadlinkIntoStringBuilder (target => readlink (path, target), buf, bufsiz);
3971 public static int readlink (string path, [Out] StringBuilder buf)
3973 return readlink (path, buf, (ulong) buf.Capacity);
3976 [DllImport (MPH, SetLastError=true,
3977 EntryPoint="Mono_Posix_Syscall_readlink")]
3978 private static extern long readlink (
3979 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3980 string path, byte[] buf, ulong bufsiz);
3982 public static long readlink (string path, byte[] buf)
3984 return readlink (path, buf, (ulong) buf.LongLength);
3987 [DllImport (LIBC, SetLastError=true)]
3988 public static extern int unlink (
3989 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3992 [DllImport (LIBC, SetLastError=true)]
3993 public static extern int rmdir (
3994 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3998 // pid_t tcgetpgrp(int fd);
3999 [DllImport (LIBC, SetLastError=true)]
4000 public static extern int tcgetpgrp (int fd);
4003 // int tcsetpgrp(int fd, pid_t pgrp);
4004 [DllImport (LIBC, SetLastError=true)]
4005 public static extern int tcsetpgrp (int fd, int pgrp);
4007 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
4008 private static extern IntPtr sys_getlogin ();
4010 public static string getlogin ()
4012 lock (getlogin_lock) {
4013 IntPtr r = sys_getlogin ();
4014 return UnixMarshal.PtrToString (r);
4019 // int getlogin_r(char *buf, size_t bufsize);
4020 [DllImport (MPH, SetLastError=true,
4021 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
4022 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
4024 public static int getlogin_r (StringBuilder name)
4026 return getlogin_r (name, (ulong) name.Capacity);
4029 [DllImport (LIBC, SetLastError=true)]
4030 public static extern int setlogin (string name);
4033 // int gethostname(char *name, size_t len);
4034 [DllImport (MPH, SetLastError=true,
4035 EntryPoint="Mono_Posix_Syscall_gethostname")]
4036 public static extern int gethostname ([Out] StringBuilder name, ulong len);
4038 public static int gethostname (StringBuilder name)
4040 return gethostname (name, (ulong) name.Capacity);
4044 // int gethostname(const char *name, size_t len);
4045 [DllImport (MPH, SetLastError=true,
4046 EntryPoint="Mono_Posix_Syscall_sethostname")]
4047 public static extern int sethostname (string name, ulong len);
4049 public static int sethostname (string name)
4051 return sethostname (name, (ulong) name.Length);
4054 [DllImport (MPH, SetLastError=true,
4055 EntryPoint="Mono_Posix_Syscall_gethostid")]
4056 public static extern long gethostid ();
4058 [DllImport (MPH, SetLastError=true,
4059 EntryPoint="Mono_Posix_Syscall_sethostid")]
4060 public static extern int sethostid (long hostid);
4063 // int getdomainname(char *name, size_t len);
4064 [DllImport (MPH, SetLastError=true,
4065 EntryPoint="Mono_Posix_Syscall_getdomainname")]
4066 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
4068 public static int getdomainname (StringBuilder name)
4070 return getdomainname (name, (ulong) name.Capacity);
4074 // int setdomainname(const char *name, size_t len);
4075 [DllImport (MPH, SetLastError=true,
4076 EntryPoint="Mono_Posix_Syscall_setdomainname")]
4077 public static extern int setdomainname (string name, ulong len);
4079 public static int setdomainname (string name)
4081 return setdomainname (name, (ulong) name.Length);
4084 [DllImport (LIBC, SetLastError=true)]
4085 public static extern int vhangup ();
4087 // Revoke doesn't appear to be POSIX. Include it?
4088 [DllImport (LIBC, SetLastError=true)]
4089 public static extern int revoke (
4090 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4093 // TODO: profil? It's not POSIX.
4095 [DllImport (LIBC, SetLastError=true)]
4096 public static extern int acct (
4097 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4100 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
4101 private static extern IntPtr sys_getusershell ();
4103 internal static object usershell_lock = new object ();
4105 public static string getusershell ()
4107 lock (usershell_lock) {
4108 IntPtr r = sys_getusershell ();
4109 return UnixMarshal.PtrToString (r);
4113 [DllImport (MPH, SetLastError=true,
4114 EntryPoint="Mono_Posix_Syscall_setusershell")]
4115 private static extern int sys_setusershell ();
4117 public static int setusershell ()
4119 lock (usershell_lock) {
4120 return sys_setusershell ();
4124 [DllImport (MPH, SetLastError=true,
4125 EntryPoint="Mono_Posix_Syscall_endusershell")]
4126 private static extern int sys_endusershell ();
4128 public static int endusershell ()
4130 lock (usershell_lock) {
4131 return sys_endusershell ();
4136 [DllImport (LIBC, SetLastError=true)]
4137 private static extern int daemon (int nochdir, int noclose);
4139 // this implicitly forks, and thus isn't safe.
4140 private static int daemon (bool nochdir, bool noclose)
4142 return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
4146 [DllImport (LIBC, SetLastError=true)]
4147 public static extern int chroot (
4148 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4151 // skipping getpass(3) as the man page states:
4152 // This function is obsolete. Do not use it.
4154 [DllImport (LIBC, SetLastError=true)]
4155 public static extern int fsync (int fd);
4157 [DllImport (LIBC, SetLastError=true)]
4158 public static extern int fdatasync (int fd);
4160 [DllImport (MPH, SetLastError=true,
4161 EntryPoint="Mono_Posix_Syscall_sync")]
4162 public static extern int sync ();
4164 [DllImport (LIBC, SetLastError=true)]
4165 [Obsolete ("Dropped in POSIX 1003.1-2001. " +
4166 "Use Syscall.sysconf (SysconfName._SC_PAGESIZE).")]
4167 public static extern int getpagesize ();
4170 // int truncate(const char *path, off_t length);
4171 [DllImport (MPH, SetLastError=true,
4172 EntryPoint="Mono_Posix_Syscall_truncate")]
4173 public static extern int truncate (
4174 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4175 string path, long length);
4178 // int ftruncate(int fd, off_t length);
4179 [DllImport (MPH, SetLastError=true,
4180 EntryPoint="Mono_Posix_Syscall_ftruncate")]
4181 public static extern int ftruncate (int fd, long length);
4183 [DllImport (LIBC, SetLastError=true)]
4184 public static extern int getdtablesize ();
4186 [DllImport (LIBC, SetLastError=true)]
4187 public static extern int brk (IntPtr end_data_segment);
4189 [DllImport (LIBC, SetLastError=true)]
4190 public static extern IntPtr sbrk (IntPtr increment);
4192 // TODO: syscall(2)?
4193 // Probably safer to skip entirely.
4196 // int lockf(int fd, int cmd, off_t len);
4197 [DllImport (MPH, SetLastError=true,
4198 EntryPoint="Mono_Posix_Syscall_lockf")]
4199 public static extern int lockf (int fd, LockfCommand cmd, long len);
4201 [Obsolete ("This is insecure and should not be used", true)]
4202 public static string crypt (string key, string salt)
4204 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
4207 [Obsolete ("This is insecure and should not be used", true)]
4208 public static int encrypt (byte[] block, bool decode)
4210 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
4214 // void swab(const void *from, void *to, ssize_t n);
4215 [DllImport (MPH, SetLastError=true,
4216 EntryPoint="Mono_Posix_Syscall_swab")]
4217 public static extern int swab (IntPtr from, IntPtr to, long n);
4219 public static unsafe void swab (void* from, void* to, long n)
4221 swab ((IntPtr) from, (IntPtr) to, n);
4224 [DllImport (LIBC, SetLastError=true, EntryPoint="faccessat")]
4225 private static extern int sys_faccessat (int dirfd,
4226 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4227 string pathname, int mode, int flags);
4229 public static int faccessat (int dirfd, string pathname, AccessModes mode, AtFlags flags)
4231 int _mode = NativeConvert.FromAccessModes (mode);
4232 int _flags = NativeConvert.FromAtFlags (flags);
4233 return sys_faccessat (dirfd, pathname, _mode, _flags);
4237 // int fchownat(int dirfd, const char *path, uid_t owner, gid_t group, int flags);
4238 [DllImport (LIBC, SetLastError=true, EntryPoint="fchownat")]
4239 private static extern int sys_fchownat (int dirfd,
4240 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4241 string pathname, uint owner, uint group, int flags);
4243 public static int fchownat (int dirfd, string pathname, uint owner, uint group, AtFlags flags)
4245 int _flags = NativeConvert.FromAtFlags (flags);
4246 return sys_fchownat (dirfd, pathname, owner, group, _flags);
4249 [DllImport (LIBC, SetLastError=true, EntryPoint="linkat")]
4250 private static extern int sys_linkat (int olddirfd,
4251 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4252 string oldpath, int newdirfd,
4253 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4254 string newpath, int flags);
4256 public static int linkat (int olddirfd, string oldpath, int newdirfd, string newpath, AtFlags flags)
4258 int _flags = NativeConvert.FromAtFlags (flags);
4259 return sys_linkat (olddirfd, oldpath, newdirfd, newpath, _flags);
4263 // ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsize);
4264 public static int readlinkat (int dirfd, string pathname, [Out] StringBuilder buf, ulong bufsiz)
4266 return ReadlinkIntoStringBuilder (target => readlinkat (dirfd, pathname, target), buf, bufsiz);
4269 public static int readlinkat (int dirfd, string pathname, [Out] StringBuilder buf)
4271 return readlinkat (dirfd, pathname, buf, (ulong) buf.Capacity);
4274 [DllImport (MPH, SetLastError=true,
4275 EntryPoint="Mono_Posix_Syscall_readlinkat")]
4276 private static extern long readlinkat (int dirfd,
4277 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4278 string pathname, byte[] buf, ulong bufsiz);
4280 public static long readlinkat (int dirfd, string pathname, byte[] buf)
4282 return readlinkat (dirfd, pathname, buf, (ulong) buf.LongLength);
4285 [DllImport (LIBC, SetLastError=true)]
4286 public static extern int symlinkat (
4287 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4288 string oldpath, int dirfd,
4289 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4292 [DllImport (LIBC, SetLastError=true, EntryPoint="unlinkat")]
4293 private static extern int sys_unlinkat (int dirfd,
4294 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4295 string pathname, int flags);
4297 public static int unlinkat (int dirfd, string pathname, AtFlags flags)
4299 int _flags = NativeConvert.FromAtFlags (flags);
4300 return sys_unlinkat (dirfd, pathname, _flags);
4304 #region <utime.h> Declarations
4306 // <utime.h> -- COMPLETE
4309 [DllImport (MPH, SetLastError=true,
4310 EntryPoint="Mono_Posix_Syscall_utime")]
4311 private static extern int sys_utime (
4312 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4313 string filename, ref Utimbuf buf, int use_buf);
4315 public static int utime (string filename, ref Utimbuf buf)
4317 return sys_utime (filename, ref buf, 1);
4320 public static int utime (string filename)
4322 Utimbuf buf = new Utimbuf ();
4323 return sys_utime (filename, ref buf, 0);
4327 #region <sys/uio.h> Declarations
4329 // <sys/uio.h> -- COMPLETE
4333 // ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
4334 [DllImport (MPH, SetLastError=true,
4335 EntryPoint="Mono_Posix_Syscall_readv")]
4336 private static extern long sys_readv (int fd, Iovec[] iov, int iovcnt);
4338 public static long readv (int fd, Iovec[] iov)
4340 return sys_readv (fd, iov, iov.Length);
4344 // ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
4345 [DllImport (MPH, SetLastError=true,
4346 EntryPoint="Mono_Posix_Syscall_writev")]
4347 private static extern long sys_writev (int fd, Iovec[] iov, int iovcnt);
4349 public static long writev (int fd, Iovec[] iov)
4351 return sys_writev (fd, iov, iov.Length);
4355 // ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
4356 [DllImport (MPH, SetLastError=true,
4357 EntryPoint="Mono_Posix_Syscall_preadv")]
4358 private static extern long sys_preadv (int fd, Iovec[] iov, int iovcnt, long offset);
4360 public static long preadv (int fd, Iovec[] iov, long offset)
4362 return sys_preadv (fd, iov, iov.Length, offset);
4366 // ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
4367 [DllImport (MPH, SetLastError=true,
4368 EntryPoint="Mono_Posix_Syscall_pwritev")]
4369 private static extern long sys_pwritev (int fd, Iovec[] iov, int iovcnt, long offset);
4371 public static long pwritev (int fd, Iovec[] iov, long offset)
4373 return sys_pwritev (fd, iov, iov.Length, offset);