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_NOCACHE = 48, // OSX: turn data caching off/on for this fd.
227 F_SETLEASE = 1024, // Set a lease.
228 F_GETLEASE = 1025, // Enquire what lease is active.
229 F_NOTIFY = 1026, // Required notifications on a directory
233 [CLSCompliant (false)]
234 public enum LockType : short {
235 F_RDLCK = 0, // Read lock.
236 F_WRLCK = 1, // Write lock.
237 F_UNLCK = 2, // Remove lock.
241 [CLSCompliant (false)]
242 public enum SeekFlags : short {
243 // values liberally copied from /usr/include/unistd.h
244 SEEK_SET = 0, // Seek from beginning of file.
245 SEEK_CUR = 1, // Seek from current position.
246 SEEK_END = 2, // Seek from end of file.
248 L_SET = SEEK_SET, // BSD alias for SEEK_SET
249 L_INCR = SEEK_CUR, // BSD alias for SEEK_CUR
250 L_XTND = SEEK_END, // BSD alias for SEEK_END
254 [CLSCompliant (false)]
255 public enum DirectoryNotifyFlags : int {
256 // from /usr/include/bits/fcntl.h
257 DN_ACCESS = 0x00000001, // File accessed.
258 DN_MODIFY = 0x00000002, // File modified.
259 DN_CREATE = 0x00000004, // File created.
260 DN_DELETE = 0x00000008, // File removed.
261 DN_RENAME = 0x00000010, // File renamed.
262 DN_ATTRIB = 0x00000020, // File changed attributes.
263 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
267 [CLSCompliant (false)]
268 public enum PosixFadviseAdvice : int {
269 POSIX_FADV_NORMAL = 0, // No further special treatment.
270 POSIX_FADV_RANDOM = 1, // Expect random page references.
271 POSIX_FADV_SEQUENTIAL = 2, // Expect sequential page references.
272 POSIX_FADV_WILLNEED = 3, // Will need these pages.
273 POSIX_FADV_DONTNEED = 4, // Don't need these pages.
274 POSIX_FADV_NOREUSE = 5, // Data will be accessed once.
278 [CLSCompliant (false)]
279 public enum PosixMadviseAdvice : int {
280 POSIX_MADV_NORMAL = 0, // No further special treatment.
281 POSIX_MADV_RANDOM = 1, // Expect random page references.
282 POSIX_MADV_SEQUENTIAL = 2, // Expect sequential page references.
283 POSIX_MADV_WILLNEED = 3, // Will need these pages.
284 POSIX_MADV_DONTNEED = 4, // Don't need these pages.
288 public enum Signum : int {
289 SIGHUP = 1, // Hangup (POSIX).
290 SIGINT = 2, // Interrupt (ANSI).
291 SIGQUIT = 3, // Quit (POSIX).
292 SIGILL = 4, // Illegal instruction (ANSI).
293 SIGTRAP = 5, // Trace trap (POSIX).
294 SIGABRT = 6, // Abort (ANSI).
295 SIGIOT = 6, // IOT trap (4.2 BSD).
296 SIGBUS = 7, // BUS error (4.2 BSD).
297 SIGFPE = 8, // Floating-point exception (ANSI).
298 SIGKILL = 9, // Kill, unblockable (POSIX).
299 SIGUSR1 = 10, // User-defined signal 1 (POSIX).
300 SIGSEGV = 11, // Segmentation violation (ANSI).
301 SIGUSR2 = 12, // User-defined signal 2 (POSIX).
302 SIGPIPE = 13, // Broken pipe (POSIX).
303 SIGALRM = 14, // Alarm clock (POSIX).
304 SIGTERM = 15, // Termination (ANSI).
305 SIGSTKFLT = 16, // Stack fault.
306 SIGCLD = SIGCHLD, // Same as SIGCHLD (System V).
307 SIGCHLD = 17, // Child status has changed (POSIX).
308 SIGCONT = 18, // Continue (POSIX).
309 SIGSTOP = 19, // Stop, unblockable (POSIX).
310 SIGTSTP = 20, // Keyboard stop (POSIX).
311 SIGTTIN = 21, // Background read from tty (POSIX).
312 SIGTTOU = 22, // Background write to tty (POSIX).
313 SIGURG = 23, // Urgent condition on socket (4.2 BSD).
314 SIGXCPU = 24, // CPU limit exceeded (4.2 BSD).
315 SIGXFSZ = 25, // File size limit exceeded (4.2 BSD).
316 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
317 SIGPROF = 27, // Profiling alarm clock (4.2 BSD).
318 SIGWINCH = 28, // Window size change (4.3 BSD, Sun).
319 SIGPOLL = SIGIO, // Pollable event occurred (System V).
320 SIGIO = 29, // I/O now possible (4.2 BSD).
321 SIGPWR = 30, // Power failure restart (System V).
322 SIGSYS = 31, // Bad system call.
327 public enum WaitOptions : int {
328 WNOHANG = 1, // Don't block waiting
329 WUNTRACED = 2, // Report status of stopped children
333 [CLSCompliant (false)]
334 public enum AccessModes : int {
342 [CLSCompliant (false)]
343 public enum PathconfName : int {
350 _PC_CHOWN_RESTRICTED,
358 _PC_REC_INCR_XFER_SIZE,
359 _PC_REC_MAX_XFER_SIZE,
360 _PC_REC_MIN_XFER_SIZE,
368 [CLSCompliant (false)]
369 public enum SysconfName : int {
379 _SC_REALTIME_SIGNALS,
380 _SC_PRIORITY_SCHEDULING,
389 _SC_MEMORY_PROTECTION,
392 _SC_SHARED_MEMORY_OBJECTS,
395 _SC_AIO_PRIO_DELTA_MAX,
406 /* Values for the argument to `sysconf'
407 corresponding to _POSIX2_* symbols. */
412 _SC_COLL_WEIGHTS_MAX,
417 _SC_CHARCLASS_NAME_MAX,
433 _SC_IOV_MAX = _SC_UIO_MAXIOV,
434 _SC_PII_INTERNET_STREAM,
435 _SC_PII_INTERNET_DGRAM,
440 /* Values according to POSIX 1003.1c (POSIX threads). */
442 _SC_THREAD_SAFE_FUNCTIONS,
443 _SC_GETGR_R_SIZE_MAX,
444 _SC_GETPW_R_SIZE_MAX,
447 _SC_THREAD_DESTRUCTOR_ITERATIONS,
449 _SC_THREAD_STACK_MIN,
450 _SC_THREAD_THREADS_MAX,
451 _SC_THREAD_ATTR_STACKADDR,
452 _SC_THREAD_ATTR_STACKSIZE,
453 _SC_THREAD_PRIORITY_SCHEDULING,
454 _SC_THREAD_PRIO_INHERIT,
455 _SC_THREAD_PRIO_PROTECT,
456 _SC_THREAD_PROCESS_SHARED,
457 _SC_NPROCESSORS_CONF,
458 _SC_NPROCESSORS_ONLN,
464 _SC_XOPEN_XCU_VERSION,
499 _SC_XBS5_ILP32_OFF32,
500 _SC_XBS5_ILP32_OFFBIG,
502 _SC_XBS5_LPBIG_OFFBIG,
505 _SC_XOPEN_REALTIME_THREADS,
510 _SC_C_LANG_SUPPORT_R,
516 _SC_DEVICE_SPECIFIC_R,
527 _SC_READER_WRITER_LOCKS,
535 _SC_THREAD_SPORADIC_SERVER,
537 _SC_SYSTEM_DATABASE_R,
539 _SC_TYPED_MEMORY_OBJECTS,
543 _SC_2_PBS_ACCOUNTING,
549 _SC_2_PBS_CHECKPOINT,
556 _SC_TRACE_EVENT_FILTER,
559 _SC_LEVEL1_ICACHE_SIZE,
560 _SC_LEVEL1_ICACHE_ASSOC,
561 _SC_LEVEL1_ICACHE_LINESIZE,
562 _SC_LEVEL1_DCACHE_SIZE,
563 _SC_LEVEL1_DCACHE_ASSOC,
564 _SC_LEVEL1_DCACHE_LINESIZE,
565 _SC_LEVEL2_CACHE_SIZE,
566 _SC_LEVEL2_CACHE_ASSOC,
567 _SC_LEVEL2_CACHE_LINESIZE,
568 _SC_LEVEL3_CACHE_SIZE,
569 _SC_LEVEL3_CACHE_ASSOC,
570 _SC_LEVEL3_CACHE_LINESIZE,
571 _SC_LEVEL4_CACHE_SIZE,
572 _SC_LEVEL4_CACHE_ASSOC,
573 _SC_LEVEL4_CACHE_LINESIZE
577 [CLSCompliant (false)]
578 public enum ConfstrName : int {
579 _CS_PATH, /* The default search path. */
580 _CS_V6_WIDTH_RESTRICTED_ENVS,
581 _CS_GNU_LIBC_VERSION,
582 _CS_GNU_LIBPTHREAD_VERSION,
583 _CS_LFS_CFLAGS = 1000,
591 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
592 _CS_XBS5_ILP32_OFF32_LDFLAGS,
593 _CS_XBS5_ILP32_OFF32_LIBS,
594 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
595 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
596 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
597 _CS_XBS5_ILP32_OFFBIG_LIBS,
598 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
599 _CS_XBS5_LP64_OFF64_CFLAGS,
600 _CS_XBS5_LP64_OFF64_LDFLAGS,
601 _CS_XBS5_LP64_OFF64_LIBS,
602 _CS_XBS5_LP64_OFF64_LINTFLAGS,
603 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
604 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
605 _CS_XBS5_LPBIG_OFFBIG_LIBS,
606 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
607 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
608 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
609 _CS_POSIX_V6_ILP32_OFF32_LIBS,
610 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
611 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
612 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
613 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
614 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
615 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
616 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
617 _CS_POSIX_V6_LP64_OFF64_LIBS,
618 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
619 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
620 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
621 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
622 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
626 [CLSCompliant (false)]
627 public enum LockfCommand : int {
628 F_ULOCK = 0, // Unlock a previously locked region.
629 F_LOCK = 1, // Lock a region for exclusive use.
630 F_TLOCK = 2, // Test and lock a region for exclusive use.
631 F_TEST = 3, // Test a region for other process locks.
635 public enum PollEvents : short {
636 POLLIN = 0x0001, // There is data to read
637 POLLPRI = 0x0002, // There is urgent data to read
638 POLLOUT = 0x0004, // Writing now will not block
639 POLLERR = 0x0008, // Error condition
640 POLLHUP = 0x0010, // Hung up
641 POLLNVAL = 0x0020, // Invalid request; fd not open
642 // XPG4.2 definitions (via _XOPEN_SOURCE)
643 POLLRDNORM = 0x0040, // Normal data may be read
644 POLLRDBAND = 0x0080, // Priority data may be read
645 POLLWRNORM = 0x0100, // Writing now will not block
646 POLLWRBAND = 0x0200, // Priority data may be written
650 [CLSCompliant (false)]
651 public enum XattrFlags : int {
658 [CLSCompliant (false)]
659 public enum MountFlags : ulong {
660 ST_RDONLY = 1, // Mount read-only
661 ST_NOSUID = 2, // Ignore suid and sgid bits
662 ST_NODEV = 4, // Disallow access to device special files
663 ST_NOEXEC = 8, // Disallow program execution
664 ST_SYNCHRONOUS = 16, // Writes are synced at once
665 ST_REMOUNT = 32, // Alter flags of a mounted FS
666 ST_MANDLOCK = 64, // Allow mandatory locks on an FS
667 ST_WRITE = 128, // Write on file/directory/symlink
668 ST_APPEND = 256, // Append-only file
669 ST_IMMUTABLE = 512, // Immutable file
670 ST_NOATIME = 1024, // Do not update access times
671 ST_NODIRATIME = 2048, // Do not update directory access times
672 ST_BIND = 4096, // Bind directory at different place
676 [CLSCompliant (false)]
677 public enum MmapFlags : int {
678 MAP_SHARED = 0x01, // Share changes.
679 MAP_PRIVATE = 0x02, // Changes are private.
680 MAP_TYPE = 0x0f, // Mask for type of mapping.
681 MAP_FIXED = 0x10, // Interpret addr exactly.
683 MAP_ANONYMOUS = 0x20, // Don't use a file.
684 MAP_ANON = MAP_ANONYMOUS,
686 // These are Linux-specific.
687 MAP_GROWSDOWN = 0x00100, // Stack-like segment.
688 MAP_DENYWRITE = 0x00800, // ETXTBSY
689 MAP_EXECUTABLE = 0x01000, // Mark it as an executable.
690 MAP_LOCKED = 0x02000, // Lock the mapping.
691 MAP_NORESERVE = 0x04000, // Don't check for reservations.
692 MAP_POPULATE = 0x08000, // Populate (prefault) pagetables.
693 MAP_NONBLOCK = 0x10000, // Do not block on IO.
697 [CLSCompliant (false)]
698 public enum MmapProts : int {
699 PROT_READ = 0x1, // Page can be read.
700 PROT_WRITE = 0x2, // Page can be written.
701 PROT_EXEC = 0x4, // Page can be executed.
702 PROT_NONE = 0x0, // Page can not be accessed.
703 PROT_GROWSDOWN = 0x01000000, // Extend change to start of
704 // growsdown vma (mprotect only).
705 PROT_GROWSUP = 0x02000000, // Extend change to start of
706 // growsup vma (mprotect only).
710 [CLSCompliant (false)]
711 public enum MsyncFlags : int {
712 MS_ASYNC = 0x1, // Sync memory asynchronously.
713 MS_SYNC = 0x4, // Synchronous memory sync.
714 MS_INVALIDATE = 0x2, // Invalidate the caches.
718 [CLSCompliant (false)]
719 public enum MlockallFlags : int {
720 MCL_CURRENT = 0x1, // Lock all currently mapped pages.
721 MCL_FUTURE = 0x2, // Lock all additions to address
725 [CLSCompliant (false)]
726 public enum MremapFlags : ulong {
727 MREMAP_MAYMOVE = 0x1,
734 [Map ("struct flock")]
738 [CLSCompliant (false)]
739 public LockType l_type; // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
740 [CLSCompliant (false)]
741 public SeekFlags l_whence; // How to interpret l_start
742 [off_t] public long l_start; // Starting offset for lock
743 [off_t] public long l_len; // Number of bytes to lock
744 [pid_t] public int l_pid; // PID of process blocking our lock (F_GETLK only)
746 public override int GetHashCode ()
748 return l_type.GetHashCode () ^ l_whence.GetHashCode () ^
749 l_start.GetHashCode () ^ l_len.GetHashCode () ^
750 l_pid.GetHashCode ();
753 public override bool Equals (object obj)
755 if ((obj == null) || (obj.GetType () != GetType ()))
757 Flock value = (Flock) obj;
758 return l_type == value.l_type && l_whence == value.l_whence &&
759 l_start == value.l_start && l_len == value.l_len &&
760 l_pid == value.l_pid;
763 public bool Equals (Flock value)
765 return l_type == value.l_type && l_whence == value.l_whence &&
766 l_start == value.l_start && l_len == value.l_len &&
767 l_pid == value.l_pid;
770 public static bool operator== (Flock lhs, Flock rhs)
772 return lhs.Equals (rhs);
775 public static bool operator!= (Flock lhs, Flock rhs)
777 return !lhs.Equals (rhs);
781 [Map ("struct pollfd")]
783 : IEquatable <Pollfd>
786 [CLSCompliant (false)]
787 public PollEvents events;
788 [CLSCompliant (false)]
789 public PollEvents revents;
791 public override int GetHashCode ()
793 return events.GetHashCode () ^ revents.GetHashCode ();
796 public override bool Equals (object obj)
798 if (obj == null || obj.GetType () != GetType ())
800 Pollfd value = (Pollfd) obj;
801 return value.events == events && value.revents == revents;
804 public bool Equals (Pollfd value)
806 return value.events == events && value.revents == revents;
809 public static bool operator== (Pollfd lhs, Pollfd rhs)
811 return lhs.Equals (rhs);
814 public static bool operator!= (Pollfd lhs, Pollfd rhs)
816 return !lhs.Equals (rhs);
820 // Use manually written To/From methods to handle fields st_atime_nsec etc.
824 [CLSCompliant (false)]
825 [dev_t] public ulong st_dev; // device
826 [CLSCompliant (false)]
827 [ino_t] public ulong st_ino; // inode
828 [CLSCompliant (false)]
829 public FilePermissions st_mode; // protection
831 #pragma warning disable 169
832 private uint _padding_; // padding for structure alignment
833 #pragma warning restore 169
834 [CLSCompliant (false)]
835 [nlink_t] public ulong st_nlink; // number of hard links
836 [CLSCompliant (false)]
837 [uid_t] public uint st_uid; // user ID of owner
838 [CLSCompliant (false)]
839 [gid_t] public uint st_gid; // group ID of owner
840 [CLSCompliant (false)]
841 [dev_t] public ulong st_rdev; // device type (if inode device)
842 [off_t] public long st_size; // total size, in bytes
843 [blksize_t] public long st_blksize; // blocksize for filesystem I/O
844 [blkcnt_t] public long st_blocks; // number of blocks allocated
845 [time_t] public long st_atime; // time of last access
846 [time_t] public long st_mtime; // time of last modification
847 [time_t] public long st_ctime; // time of last status change
848 public long st_atime_nsec; // Timespec.tv_nsec partner to st_atime
849 public long st_mtime_nsec; // Timespec.tv_nsec partner to st_mtime
850 public long st_ctime_nsec; // Timespec.tv_nsec partner to st_ctime
852 public Timespec st_atim {
854 return new Timespec { tv_sec = st_atime, tv_nsec = st_atime_nsec };
857 st_atime = value.tv_sec;
858 st_atime_nsec = value.tv_nsec;
862 public Timespec st_mtim {
864 return new Timespec { tv_sec = st_mtime, tv_nsec = st_mtime_nsec };
867 st_mtime = value.tv_sec;
868 st_mtime_nsec = value.tv_nsec;
872 public Timespec st_ctim {
874 return new Timespec { tv_sec = st_ctime, tv_nsec = st_ctime_nsec };
877 st_ctime = value.tv_sec;
878 st_ctime_nsec = value.tv_nsec;
882 public override int GetHashCode ()
884 return st_dev.GetHashCode () ^
885 st_ino.GetHashCode () ^
886 st_mode.GetHashCode () ^
887 st_nlink.GetHashCode () ^
888 st_uid.GetHashCode () ^
889 st_gid.GetHashCode () ^
890 st_rdev.GetHashCode () ^
891 st_size.GetHashCode () ^
892 st_blksize.GetHashCode () ^
893 st_blocks.GetHashCode () ^
894 st_atime.GetHashCode () ^
895 st_mtime.GetHashCode () ^
896 st_ctime.GetHashCode () ^
897 st_atime_nsec.GetHashCode () ^
898 st_mtime_nsec.GetHashCode () ^
899 st_ctime_nsec.GetHashCode ();
902 public override bool Equals (object obj)
904 if (obj == null || obj.GetType() != GetType ())
906 Stat value = (Stat) obj;
907 return value.st_dev == st_dev &&
908 value.st_ino == st_ino &&
909 value.st_mode == st_mode &&
910 value.st_nlink == st_nlink &&
911 value.st_uid == st_uid &&
912 value.st_gid == st_gid &&
913 value.st_rdev == st_rdev &&
914 value.st_size == st_size &&
915 value.st_blksize == st_blksize &&
916 value.st_blocks == st_blocks &&
917 value.st_atime == st_atime &&
918 value.st_mtime == st_mtime &&
919 value.st_ctime == st_ctime &&
920 value.st_atime_nsec == st_atime_nsec &&
921 value.st_mtime_nsec == st_mtime_nsec &&
922 value.st_ctime_nsec == st_ctime_nsec;
925 public bool Equals (Stat value)
927 return value.st_dev == st_dev &&
928 value.st_ino == st_ino &&
929 value.st_mode == st_mode &&
930 value.st_nlink == st_nlink &&
931 value.st_uid == st_uid &&
932 value.st_gid == st_gid &&
933 value.st_rdev == st_rdev &&
934 value.st_size == st_size &&
935 value.st_blksize == st_blksize &&
936 value.st_blocks == st_blocks &&
937 value.st_atime == st_atime &&
938 value.st_mtime == st_mtime &&
939 value.st_ctime == st_ctime &&
940 value.st_atime_nsec == st_atime_nsec &&
941 value.st_mtime_nsec == st_mtime_nsec &&
942 value.st_ctime_nsec == st_ctime_nsec;
945 public static bool operator== (Stat lhs, Stat rhs)
947 return lhs.Equals (rhs);
950 public static bool operator!= (Stat lhs, Stat rhs)
952 return !lhs.Equals (rhs);
956 // `struct statvfs' isn't portable, so don't generate To/From methods.
958 [CLSCompliant (false)]
959 public struct Statvfs
960 : IEquatable <Statvfs>
962 public ulong f_bsize; // file system block size
963 public ulong f_frsize; // fragment size
964 [fsblkcnt_t] public ulong f_blocks; // size of fs in f_frsize units
965 [fsblkcnt_t] public ulong f_bfree; // # free blocks
966 [fsblkcnt_t] public ulong f_bavail; // # free blocks for non-root
967 [fsfilcnt_t] public ulong f_files; // # inodes
968 [fsfilcnt_t] public ulong f_ffree; // # free inodes
969 [fsfilcnt_t] public ulong f_favail; // # free inodes for non-root
970 public ulong f_fsid; // file system id
971 public MountFlags f_flag; // mount flags
972 public ulong f_namemax; // maximum filename length
974 public override int GetHashCode ()
976 return f_bsize.GetHashCode () ^
977 f_frsize.GetHashCode () ^
978 f_blocks.GetHashCode () ^
979 f_bfree.GetHashCode () ^
980 f_bavail.GetHashCode () ^
981 f_files.GetHashCode () ^
982 f_ffree.GetHashCode () ^
983 f_favail.GetHashCode () ^
984 f_fsid.GetHashCode () ^
985 f_flag.GetHashCode () ^
986 f_namemax.GetHashCode ();
989 public override bool Equals (object obj)
991 if (obj == null || obj.GetType() != GetType ())
993 Statvfs value = (Statvfs) obj;
994 return value.f_bsize == f_bsize &&
995 value.f_frsize == f_frsize &&
996 value.f_blocks == f_blocks &&
997 value.f_bfree == f_bfree &&
998 value.f_bavail == f_bavail &&
999 value.f_files == f_files &&
1000 value.f_ffree == f_ffree &&
1001 value.f_favail == f_favail &&
1002 value.f_fsid == f_fsid &&
1003 value.f_flag == f_flag &&
1004 value.f_namemax == f_namemax;
1007 public bool Equals (Statvfs value)
1009 return value.f_bsize == f_bsize &&
1010 value.f_frsize == f_frsize &&
1011 value.f_blocks == f_blocks &&
1012 value.f_bfree == f_bfree &&
1013 value.f_bavail == f_bavail &&
1014 value.f_files == f_files &&
1015 value.f_ffree == f_ffree &&
1016 value.f_favail == f_favail &&
1017 value.f_fsid == f_fsid &&
1018 value.f_flag == f_flag &&
1019 value.f_namemax == f_namemax;
1022 public static bool operator== (Statvfs lhs, Statvfs rhs)
1024 return lhs.Equals (rhs);
1027 public static bool operator!= (Statvfs lhs, Statvfs rhs)
1029 return !lhs.Equals (rhs);
1033 [Map ("struct timeval")]
1034 public struct Timeval
1035 : IEquatable <Timeval>
1037 [time_t] public long tv_sec; // seconds
1038 [suseconds_t] public long tv_usec; // microseconds
1040 public override int GetHashCode ()
1042 return tv_sec.GetHashCode () ^ tv_usec.GetHashCode ();
1045 public override bool Equals (object obj)
1047 if (obj == null || obj.GetType () != GetType ())
1049 Timeval value = (Timeval) obj;
1050 return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1053 public bool Equals (Timeval value)
1055 return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1058 public static bool operator== (Timeval lhs, Timeval rhs)
1060 return lhs.Equals (rhs);
1063 public static bool operator!= (Timeval lhs, Timeval rhs)
1065 return !lhs.Equals (rhs);
1069 [Map ("struct timezone")]
1070 public struct Timezone
1071 : IEquatable <Timezone>
1073 public int tz_minuteswest; // minutes W of Greenwich
1074 #pragma warning disable 169
1075 private int tz_dsttime; // type of dst correction (OBSOLETE)
1076 #pragma warning restore 169
1078 public override int GetHashCode ()
1080 return tz_minuteswest.GetHashCode ();
1083 public override bool Equals (object obj)
1085 if (obj == null || obj.GetType () != GetType ())
1087 Timezone value = (Timezone) obj;
1088 return value.tz_minuteswest == tz_minuteswest;
1091 public bool Equals (Timezone value)
1093 return value.tz_minuteswest == tz_minuteswest;
1096 public static bool operator== (Timezone lhs, Timezone rhs)
1098 return lhs.Equals (rhs);
1101 public static bool operator!= (Timezone lhs, Timezone rhs)
1103 return !lhs.Equals (rhs);
1107 [Map ("struct utimbuf")]
1108 public struct Utimbuf
1109 : IEquatable <Utimbuf>
1111 [time_t] public long actime; // access time
1112 [time_t] public long modtime; // modification time
1114 public override int GetHashCode ()
1116 return actime.GetHashCode () ^ modtime.GetHashCode ();
1119 public override bool Equals (object obj)
1121 if (obj == null || obj.GetType () != GetType ())
1123 Utimbuf value = (Utimbuf) obj;
1124 return value.actime == actime && value.modtime == modtime;
1127 public bool Equals (Utimbuf value)
1129 return value.actime == actime && value.modtime == modtime;
1132 public static bool operator== (Utimbuf lhs, Utimbuf rhs)
1134 return lhs.Equals (rhs);
1137 public static bool operator!= (Utimbuf lhs, Utimbuf rhs)
1139 return !lhs.Equals (rhs);
1143 [Map ("struct timespec")]
1144 public struct Timespec
1145 : IEquatable <Timespec>
1147 [time_t] public long tv_sec; // Seconds.
1148 public long tv_nsec; // Nanoseconds.
1150 public override int GetHashCode ()
1152 return tv_sec.GetHashCode () ^ tv_nsec.GetHashCode ();
1155 public override bool Equals (object obj)
1157 if (obj == null || obj.GetType () != GetType ())
1159 Timespec value = (Timespec) obj;
1160 return value.tv_sec == tv_sec && value.tv_nsec == tv_nsec;
1163 public bool Equals (Timespec value)
1165 return value.tv_sec == tv_sec && value.tv_nsec == tv_nsec;
1168 public static bool operator== (Timespec lhs, Timespec rhs)
1170 return lhs.Equals (rhs);
1173 public static bool operator!= (Timespec lhs, Timespec rhs)
1175 return !lhs.Equals (rhs);
1179 [Map ("struct iovec")]
1182 public IntPtr iov_base; // Starting address
1183 [CLSCompliant (false)]
1184 public ulong iov_len; // Number of bytes to transfer
1188 public enum EpollFlags {
1189 EPOLL_CLOEXEC = 02000000,
1190 EPOLL_NONBLOCK = 04000,
1194 [CLSCompliant (false)]
1195 public enum EpollEvents : uint {
1199 EPOLLRDNORM = 0x040,
1200 EPOLLRDBAND = 0x080,
1201 EPOLLWRNORM = 0x100,
1202 EPOLLWRBAND = 0x200,
1206 EPOLLRDHUP = 0x2000,
1207 EPOLLONESHOT = 1 << 30,
1208 EPOLLET = unchecked ((uint) (1 << 31))
1211 public enum EpollOp {
1217 [StructLayout (LayoutKind.Explicit, Size=12, Pack=1)]
1218 [CLSCompliant (false)]
1219 public struct EpollEvent {
1221 public EpollEvents events;
1235 public sealed class Dirent
1236 : IEquatable <Dirent>
1238 [CLSCompliant (false)]
1239 public /* ino_t */ ulong d_ino;
1240 public /* off_t */ long d_off;
1241 [CLSCompliant (false)]
1242 public ushort d_reclen;
1244 public string d_name;
1246 public override int GetHashCode ()
1248 return d_ino.GetHashCode () ^ d_off.GetHashCode () ^
1249 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
1250 d_name.GetHashCode ();
1253 public override bool Equals (object obj)
1255 if (obj == null || GetType() != obj.GetType())
1257 Dirent d = (Dirent) obj;
1261 public bool Equals (Dirent value)
1265 return value.d_ino == d_ino && value.d_off == d_off &&
1266 value.d_reclen == d_reclen && value.d_type == d_type &&
1267 value.d_name == d_name;
1270 public override string ToString ()
1275 public static bool operator== (Dirent lhs, Dirent rhs)
1277 return Object.Equals (lhs, rhs);
1280 public static bool operator!= (Dirent lhs, Dirent rhs)
1282 return !Object.Equals (lhs, rhs);
1286 public sealed class Fstab
1287 : IEquatable <Fstab>
1289 public string fs_spec;
1290 public string fs_file;
1291 public string fs_vfstype;
1292 public string fs_mntops;
1293 public string fs_type;
1295 public int fs_passno;
1297 public override int GetHashCode ()
1299 return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
1300 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
1301 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
1304 public override bool Equals (object obj)
1306 if (obj == null || GetType() != obj.GetType())
1308 Fstab f = (Fstab) obj;
1312 public bool Equals (Fstab value)
1316 return value.fs_spec == fs_spec && value.fs_file == fs_file &&
1317 value.fs_vfstype == fs_vfstype && value.fs_mntops == fs_mntops &&
1318 value.fs_type == fs_type && value.fs_freq == fs_freq &&
1319 value.fs_passno == fs_passno;
1322 public override string ToString ()
1327 public static bool operator== (Fstab lhs, Fstab rhs)
1329 return Object.Equals (lhs, rhs);
1332 public static bool operator!= (Fstab lhs, Fstab rhs)
1334 return !Object.Equals (lhs, rhs);
1338 public sealed class Group
1339 : IEquatable <Group>
1341 public string gr_name;
1342 public string gr_passwd;
1343 [CLSCompliant (false)]
1344 public /* gid_t */ uint gr_gid;
1345 public string[] gr_mem;
1347 public override int GetHashCode ()
1350 for (int i = 0; i < gr_mem.Length; ++i)
1351 memhc ^= gr_mem[i].GetHashCode ();
1353 return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^
1354 gr_gid.GetHashCode () ^ memhc;
1357 public override bool Equals (object obj)
1359 if (obj == null || GetType() != obj.GetType())
1361 Group g = (Group) obj;
1365 public bool Equals (Group value)
1369 if (value.gr_gid != gr_gid)
1371 if (value.gr_gid == gr_gid && value.gr_name == gr_name &&
1372 value.gr_passwd == gr_passwd) {
1373 if (value.gr_mem == gr_mem)
1375 if (value.gr_mem == null || gr_mem == null)
1377 if (value.gr_mem.Length != gr_mem.Length)
1379 for (int i = 0; i < gr_mem.Length; ++i)
1380 if (gr_mem[i] != value.gr_mem[i])
1387 // Generate string in /etc/group format
1388 public override string ToString ()
1390 StringBuilder sb = new StringBuilder ();
1391 sb.Append (gr_name).Append (":").Append (gr_passwd).Append (":");
1392 sb.Append (gr_gid).Append (":");
1393 GetMembers (sb, gr_mem);
1394 return sb.ToString ();
1397 private static void GetMembers (StringBuilder sb, string[] members)
1399 if (members.Length > 0)
1400 sb.Append (members[0]);
1401 for (int i = 1; i < members.Length; ++i) {
1403 sb.Append (members[i]);
1407 public static bool operator== (Group lhs, Group rhs)
1409 return Object.Equals (lhs, rhs);
1412 public static bool operator!= (Group lhs, Group rhs)
1414 return !Object.Equals (lhs, rhs);
1418 public sealed class Passwd
1419 : IEquatable <Passwd>
1421 public string pw_name;
1422 public string pw_passwd;
1423 [CLSCompliant (false)]
1424 public /* uid_t */ uint pw_uid;
1425 [CLSCompliant (false)]
1426 public /* gid_t */ uint pw_gid;
1427 public string pw_gecos;
1428 public string pw_dir;
1429 public string pw_shell;
1431 public override int GetHashCode ()
1433 return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^
1434 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
1435 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
1436 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
1439 public override bool Equals (object obj)
1441 if (obj == null || GetType() != obj.GetType())
1443 Passwd p = (Passwd) obj;
1447 public bool Equals (Passwd value)
1451 return value.pw_uid == pw_uid && value.pw_gid == pw_gid &&
1452 value.pw_name == pw_name && value.pw_passwd == pw_passwd &&
1453 value.pw_gecos == pw_gecos && value.pw_dir == pw_dir &&
1454 value.pw_shell == pw_shell;
1457 // Generate string in /etc/passwd format
1458 public override string ToString ()
1460 return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
1461 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
1464 public static bool operator== (Passwd lhs, Passwd rhs)
1466 return Object.Equals (lhs, rhs);
1469 public static bool operator!= (Passwd lhs, Passwd rhs)
1471 return !Object.Equals (lhs, rhs);
1475 public sealed class Utsname
1476 : IEquatable <Utsname>
1478 public string sysname;
1479 public string nodename;
1480 public string release;
1481 public string version;
1482 public string machine;
1483 public string domainname;
1485 public override int GetHashCode ()
1487 return sysname.GetHashCode () ^ nodename.GetHashCode () ^
1488 release.GetHashCode () ^ version.GetHashCode () ^
1489 machine.GetHashCode () ^ domainname.GetHashCode ();
1492 public override bool Equals (object obj)
1494 if (obj == null || GetType() != obj.GetType())
1496 Utsname u = (Utsname) obj;
1500 public bool Equals (Utsname value)
1502 return value.sysname == sysname && value.nodename == nodename &&
1503 value.release == release && value.version == version &&
1504 value.machine == machine && value.domainname == domainname;
1507 // Generate string in /etc/passwd format
1508 public override string ToString ()
1510 return string.Format ("{0} {1} {2} {3} {4}",
1511 sysname, nodename, release, version, machine);
1514 public static bool operator== (Utsname lhs, Utsname rhs)
1516 return Object.Equals (lhs, rhs);
1519 public static bool operator!= (Utsname lhs, Utsname rhs)
1521 return !Object.Equals (lhs, rhs);
1526 // Convention: Functions *not* part of the standard C library AND part of
1527 // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
1529 // For example, the man page should be similar to:
1531 // CONFORMING TO (or CONFORMS TO)
1532 // XPG2, SUSv2, POSIX, etc.
1534 // BSD- and GNU-specific exports can also be placed here.
1536 // Non-POSIX/XPG/etc. functions can also be placed here if:
1537 // (a) They'd be likely to be covered in a Steven's-like book
1538 // (b) The functions would be present in libc.so (or equivalent).
1540 // If a function has its own library, that's a STRONG indicator that the
1541 // function should get a different binding, probably in its own assembly,
1542 // so that package management can work sanely. (That is, we'd like to avoid
1543 // scenarios where FooLib.dll is installed, but it requires libFooLib.so to
1544 // run, and libFooLib.so doesn't exist. That would be confusing.)
1546 // The only methods in here should be:
1547 // (1) low-level functions
1548 // (2) "Trivial" function overloads. For example, if the parameters to a
1549 // function are related (e.g. getgroups(2))
1550 // (3) The return type SHOULD NOT be changed. If you want to provide a
1551 // convenience function with a nicer return type, place it into one of
1552 // the Mono.Unix.Unix* wrapper classes, and give it a .NET-styled name.
1553 // - EXCEPTION: No public functions should have a `void' return type.
1554 // `void' return types should be replaced with `int'.
1555 // Rationality: `void'-return functions typically require a
1556 // complicated call sequence, such as clear errno, then call, then
1557 // check errno to see if any errors occurred. This sequence can't
1558 // be done safely in managed code, as errno may change as part of
1559 // the P/Invoke mechanism.
1560 // Instead, add a MonoPosixHelper export which does:
1563 // return errno == 0 ? 0 : -1;
1564 // This lets managed code check the return value in the usual manner.
1565 // (4) Exceptions SHOULD NOT be thrown. EXCEPTIONS:
1566 // - If you're wrapping *broken* methods which make assumptions about
1567 // input data, such as that an argument refers to N bytes of data.
1568 // This is currently limited to cuserid(3) and encrypt(3).
1569 // - If you call functions which themselves generate exceptions.
1570 // This is the case for using NativeConvert, which will throw an
1571 // exception if an invalid/unsupported value is used.
1573 // Naming Conventions:
1574 // - Syscall method names should have the same name as the function being
1575 // wrapped (e.g. Syscall.read ==> read(2)). This allows people to
1576 // consult the appropriate man page if necessary.
1577 // - Methods need not have the same arguments IF this simplifies or
1578 // permits correct usage. The current example is syslog, in which
1579 // syslog(3)'s single `priority' argument is split into SyslogFacility
1580 // and SyslogLevel arguments.
1581 // - Type names (structures, classes, enumerations) are always PascalCased.
1582 // - Enumerations are named as <MethodName><ArgumentName>, and are located
1583 // in the Mono.Unix.Native namespace. For readability, if ArgumentName
1584 // is "cmd", use Command instead. For example, fcntl(2) takes a
1585 // FcntlCommand argument. This naming convention is to provide an
1586 // assocation between an enumeration and where it should be used, and
1587 // allows a single method to accept multiple different enumerations
1588 // (see mmap(2), which takes MmapProts and MmapFlags).
1589 // - EXCEPTION: if an enumeration is shared between multiple different
1590 // methods, AND/OR the "obvious" enumeration name conflicts with an
1591 // existing .NET type, a more appropriate name should be used.
1592 // Example: FilePermissions
1593 // - EXCEPTION: [Flags] enumerations should get plural names to follow
1594 // .NET name guidelines. Usually this doesn't result in a change
1595 // (OpenFlags is the `flags' parameter for open(2)), but it can
1596 // (mmap(2) prot ==> MmapProts, access(2) mode ==> AccessModes).
1597 // - Enumerations should have the [Map] and (optional) [Flags] attributes.
1598 // [Map] is required for make-map to find the type and generate the
1599 // appropriate NativeConvert conversion functions.
1600 // - Enumeration contents should match the original Unix names. This helps
1601 // with documentation (the existing man pages are still useful), and is
1602 // required for use with the make-map generation program.
1603 // - Structure names should be the PascalCased version of the actual
1604 // structure name (struct flock ==> Flock). Structure members should
1605 // have the same names, or a (reasonably) portable subset (Dirent being
1606 // the poster child for questionable members).
1607 // - Whether the managed type should be a reference type (class) or a
1608 // value type (struct) should be determined on a case-by-case basis:
1609 // if you ever need to be able to use NULL for it (such as with Dirent,
1610 // Group, Passwd, as these are method return types and `null' is used
1611 // to signify the end), it should be a reference type; otherwise, use
1612 // your discretion, and keep any expected usage patterns in mind.
1613 // - Syscall should be a Single Point Of Truth (SPOT). There should be
1614 // only ONE way to do anything. By convention, the Linux function names
1615 // are used, but that need not always be the case (use your discretion).
1616 // It SHOULD NOT be required that developers know what platform they're
1617 // on, and choose among a set of similar functions. In short, anything
1618 // that requires a platform check is BAD -- Mono.Unix is a wrapper, and
1619 // we can afford to clean things up whenever possible.
1621 // - Syscall.statfs: Solaris/Mac OS X provide statfs(2), Linux provides
1622 // statvfs(2). MonoPosixHelper will "thunk" between the two,
1623 // exporting a statvfs that works across platforms.
1624 // - Syscall.getfsent: Glibc export which Solaris lacks, while Solaris
1625 // instead provides getvfsent(3). MonoPosixHelper provides wrappers
1626 // to convert getvfsent(3) into Fstab data.
1627 // - Exception: If it isn't possible to cleanly wrap platforms, then the
1628 // method shouldn't be exported. The user will be expected to do their
1629 // own platform check and their own DllImports.
1630 // Examples: mount(2), umount(2), etc.
1631 // - Note: if a platform doesn't support a function AT ALL, the
1632 // MonoPosixHelper wrapper won't be compiled, resulting in a
1633 // EntryPointNotFoundException. This is also consistent with a missing
1634 // P/Invoke into libc.so.
1636 [CLSCompliant (false)]
1637 public sealed class Syscall : Stdlib
1639 new internal const string LIBC = "libc";
1641 private Syscall () {}
1647 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3),
1648 // aio_return(3), aio_suspend(3), aio_write(3)
1650 // Then update UnixStream.BeginRead to use the aio* functions.
1653 #region <attr/xattr.h> Declarations
1655 // <attr/xattr.h> -- COMPLETE
1659 // int setxattr (const char *path, const char *name,
1660 // const void *value, size_t size, int flags);
1661 [DllImport (MPH, SetLastError=true,
1662 EntryPoint="Mono_Posix_Syscall_setxattr")]
1663 public static extern int setxattr (
1664 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1666 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1667 string name, byte[] value, ulong size, XattrFlags flags);
1669 public static int setxattr (string path, string name, byte [] value, ulong size)
1671 return setxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1674 public static int setxattr (string path, string name, byte [] value, XattrFlags flags)
1676 return setxattr (path, name, value, (ulong) value.Length, flags);
1679 public static int setxattr (string path, string name, byte [] value)
1681 return setxattr (path, name, value, (ulong) value.Length);
1685 // int lsetxattr (const char *path, const char *name,
1686 // const void *value, size_t size, int flags);
1687 [DllImport (MPH, SetLastError=true,
1688 EntryPoint="Mono_Posix_Syscall_lsetxattr")]
1689 public static extern int lsetxattr (
1690 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1692 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1693 string name, byte[] value, ulong size, XattrFlags flags);
1695 public static int lsetxattr (string path, string name, byte [] value, ulong size)
1697 return lsetxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1700 public static int lsetxattr (string path, string name, byte [] value, XattrFlags flags)
1702 return lsetxattr (path, name, value, (ulong) value.Length, flags);
1705 public static int lsetxattr (string path, string name, byte [] value)
1707 return lsetxattr (path, name, value, (ulong) value.Length);
1711 // int fsetxattr (int fd, const char *name,
1712 // const void *value, size_t size, int flags);
1713 [DllImport (MPH, SetLastError=true,
1714 EntryPoint="Mono_Posix_Syscall_fsetxattr")]
1715 public static extern int fsetxattr (int fd,
1716 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1717 string name, byte[] value, ulong size, XattrFlags flags);
1719 public static int fsetxattr (int fd, string name, byte [] value, ulong size)
1721 return fsetxattr (fd, name, value, size, XattrFlags.XATTR_AUTO);
1724 public static int fsetxattr (int fd, string name, byte [] value, XattrFlags flags)
1726 return fsetxattr (fd, name, value, (ulong) value.Length, flags);
1729 public static int fsetxattr (int fd, string name, byte [] value)
1731 return fsetxattr (fd, name, value, (ulong) value.Length);
1735 // ssize_t getxattr (const char *path, const char *name,
1736 // void *value, size_t size);
1737 [DllImport (MPH, SetLastError=true,
1738 EntryPoint="Mono_Posix_Syscall_getxattr")]
1739 public static extern long getxattr (
1740 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1742 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1743 string name, byte[] value, ulong size);
1745 public static long getxattr (string path, string name, byte [] value)
1747 return getxattr (path, name, value, (ulong) value.Length);
1750 public static long getxattr (string path, string name, out byte [] value)
1753 long size = getxattr (path, name, value, 0);
1757 value = new byte [size];
1758 return getxattr (path, name, value, (ulong) size);
1762 // ssize_t lgetxattr (const char *path, const char *name,
1763 // void *value, size_t size);
1764 [DllImport (MPH, SetLastError=true,
1765 EntryPoint="Mono_Posix_Syscall_lgetxattr")]
1766 public static extern long lgetxattr (
1767 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1769 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1770 string name, byte[] value, ulong size);
1772 public static long lgetxattr (string path, string name, byte [] value)
1774 return lgetxattr (path, name, value, (ulong) value.Length);
1777 public static long lgetxattr (string path, string name, out byte [] value)
1780 long size = lgetxattr (path, name, value, 0);
1784 value = new byte [size];
1785 return lgetxattr (path, name, value, (ulong) size);
1789 // ssize_t fgetxattr (int fd, const char *name, void *value, size_t size);
1790 [DllImport (MPH, SetLastError=true,
1791 EntryPoint="Mono_Posix_Syscall_fgetxattr")]
1792 public static extern long fgetxattr (int fd,
1793 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1794 string name, byte[] value, ulong size);
1796 public static long fgetxattr (int fd, string name, byte [] value)
1798 return fgetxattr (fd, name, value, (ulong) value.Length);
1801 public static long fgetxattr (int fd, string name, out byte [] value)
1804 long size = fgetxattr (fd, name, value, 0);
1808 value = new byte [size];
1809 return fgetxattr (fd, name, value, (ulong) size);
1813 // ssize_t listxattr (const char *path, char *list, size_t size);
1814 [DllImport (MPH, SetLastError=true,
1815 EntryPoint="Mono_Posix_Syscall_listxattr")]
1816 public static extern long listxattr (
1817 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1818 string path, byte[] list, ulong size);
1820 // Slight modification: returns 0 on success, negative on error
1821 public static long listxattr (string path, Encoding encoding, out string [] values)
1824 long size = listxattr (path, null, 0);
1826 values = new string [0];
1830 byte[] list = new byte [size];
1831 long ret = listxattr (path, list, (ulong) size);
1835 GetValues (list, encoding, out values);
1839 public static long listxattr (string path, out string[] values)
1841 return listxattr (path, UnixEncoding.Instance, out values);
1844 private static void GetValues (byte[] list, Encoding encoding, out string[] values)
1847 for (int i = 0; i < list.Length; ++i)
1851 values = new string [num_values];
1854 for (int i = 0; i < list.Length; ++i) {
1855 if (list [i] == 0) {
1856 values [num_values++] = encoding.GetString (list, str_start, i - str_start);
1863 // ssize_t llistxattr (const char *path, char *list, size_t size);
1864 [DllImport (MPH, SetLastError=true,
1865 EntryPoint="Mono_Posix_Syscall_llistxattr")]
1866 public static extern long llistxattr (
1867 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1868 string path, byte[] list, ulong size);
1870 // Slight modification: returns 0 on success, negative on error
1871 public static long llistxattr (string path, Encoding encoding, out string [] values)
1874 long size = llistxattr (path, null, 0);
1876 values = new string [0];
1880 byte[] list = new byte [size];
1881 long ret = llistxattr (path, list, (ulong) size);
1885 GetValues (list, encoding, out values);
1889 public static long llistxattr (string path, out string[] values)
1891 return llistxattr (path, UnixEncoding.Instance, out values);
1895 // ssize_t flistxattr (int fd, char *list, size_t size);
1896 [DllImport (MPH, SetLastError=true,
1897 EntryPoint="Mono_Posix_Syscall_flistxattr")]
1898 public static extern long flistxattr (int fd, byte[] list, ulong size);
1900 // Slight modification: returns 0 on success, negative on error
1901 public static long flistxattr (int fd, Encoding encoding, out string [] values)
1904 long size = flistxattr (fd, null, 0);
1906 values = new string [0];
1910 byte[] list = new byte [size];
1911 long ret = flistxattr (fd, list, (ulong) size);
1915 GetValues (list, encoding, out values);
1919 public static long flistxattr (int fd, out string[] values)
1921 return flistxattr (fd, UnixEncoding.Instance, out values);
1924 [DllImport (MPH, SetLastError=true,
1925 EntryPoint="Mono_Posix_Syscall_removexattr")]
1926 public static extern int removexattr (
1927 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1929 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1932 [DllImport (MPH, SetLastError=true,
1933 EntryPoint="Mono_Posix_Syscall_lremovexattr")]
1934 public static extern int lremovexattr (
1935 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1937 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1940 [DllImport (MPH, SetLastError=true,
1941 EntryPoint="Mono_Posix_Syscall_fremovexattr")]
1942 public static extern int fremovexattr (int fd,
1943 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1947 #region <dirent.h> Declarations
1951 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1953 [DllImport (LIBC, SetLastError=true)]
1954 public static extern IntPtr opendir (
1955 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1958 [DllImport (LIBC, SetLastError=true)]
1959 public static extern int closedir (IntPtr dir);
1962 // void seekdir (DIR *dir, off_t offset);
1963 // Slight modification. Returns -1 on error, 0 on success.
1964 [DllImport (MPH, SetLastError=true,
1965 EntryPoint="Mono_Posix_Syscall_seekdir")]
1966 public static extern int seekdir (IntPtr dir, long offset);
1969 // off_t telldir(DIR *dir);
1970 [DllImport (MPH, SetLastError=true,
1971 EntryPoint="Mono_Posix_Syscall_telldir")]
1972 public static extern long telldir (IntPtr dir);
1974 [DllImport (MPH, SetLastError=true,
1975 EntryPoint="Mono_Posix_Syscall_rewinddir")]
1976 public static extern int rewinddir (IntPtr dir);
1978 private struct _Dirent {
1979 [ino_t] public ulong d_ino;
1980 [off_t] public long d_off;
1981 public ushort d_reclen;
1983 public IntPtr d_name;
1986 private static void CopyDirent (Dirent to, ref _Dirent from)
1989 to.d_ino = from.d_ino;
1990 to.d_off = from.d_off;
1991 to.d_reclen = from.d_reclen;
1992 to.d_type = from.d_type;
1993 to.d_name = UnixMarshal.PtrToString (from.d_name);
1996 Stdlib.free (from.d_name);
1997 from.d_name = IntPtr.Zero;
2001 internal static object readdir_lock = new object ();
2003 [DllImport (MPH, SetLastError=true,
2004 EntryPoint="Mono_Posix_Syscall_readdir")]
2005 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
2007 public static Dirent readdir (IntPtr dir)
2011 lock (readdir_lock) {
2012 r = sys_readdir (dir, out dentry);
2016 Dirent d = new Dirent ();
2017 CopyDirent (d, ref dentry);
2021 [DllImport (MPH, SetLastError=true,
2022 EntryPoint="Mono_Posix_Syscall_readdir_r")]
2023 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
2025 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
2031 entry.d_name = null;
2034 int r = sys_readdir_r (dirp, out _d, out result);
2036 if (r == 0 && result != IntPtr.Zero) {
2037 CopyDirent (entry, ref _d);
2043 [DllImport (LIBC, SetLastError=true)]
2044 public static extern int dirfd (IntPtr dir);
2046 [DllImport (LIBC, SetLastError=true)]
2047 public static extern IntPtr fdopendir (int fd);
2050 #region <fcntl.h> Declarations
2052 // <fcntl.h> -- COMPLETE
2055 [DllImport (MPH, SetLastError=true,
2056 EntryPoint="Mono_Posix_Syscall_fcntl")]
2057 public static extern int fcntl (int fd, FcntlCommand cmd);
2059 [DllImport (MPH, SetLastError=true,
2060 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
2061 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
2063 [DllImport (MPH, SetLastError=true,
2064 EntryPoint="Mono_Posix_Syscall_fcntl_arg_int")]
2065 public static extern int fcntl (int fd, FcntlCommand cmd, int arg);
2067 [DllImport (MPH, SetLastError=true,
2068 EntryPoint="Mono_Posix_Syscall_fcntl_arg_ptr")]
2069 public static extern int fcntl (int fd, FcntlCommand cmd, IntPtr ptr);
2071 public static int fcntl (int fd, FcntlCommand cmd, DirectoryNotifyFlags arg)
2073 if (cmd != FcntlCommand.F_NOTIFY) {
2074 SetLastError (Errno.EINVAL);
2077 long _arg = NativeConvert.FromDirectoryNotifyFlags (arg);
2078 return fcntl (fd, FcntlCommand.F_NOTIFY, _arg);
2081 [DllImport (MPH, SetLastError=true,
2082 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
2083 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
2085 [DllImport (MPH, SetLastError=true,
2086 EntryPoint="Mono_Posix_Syscall_open")]
2087 public static extern int open (
2088 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2089 string pathname, OpenFlags flags);
2092 // int open(const char *pathname, int flags, mode_t mode);
2093 [DllImport (MPH, SetLastError=true,
2094 EntryPoint="Mono_Posix_Syscall_open_mode")]
2095 public static extern int open (
2096 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2097 string pathname, OpenFlags flags, FilePermissions mode);
2100 // int creat(const char *pathname, mode_t mode);
2101 [DllImport (MPH, SetLastError=true,
2102 EntryPoint="Mono_Posix_Syscall_creat")]
2103 public static extern int creat (
2104 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2105 string pathname, FilePermissions mode);
2108 // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
2109 [DllImport (MPH, SetLastError=true,
2110 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
2111 public static extern int posix_fadvise (int fd, long offset,
2112 long len, PosixFadviseAdvice advice);
2114 // posix_fallocate(P)
2115 // int posix_fallocate(int fd, off_t offset, size_t len);
2116 [DllImport (MPH, SetLastError=true,
2117 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
2118 public static extern int posix_fallocate (int fd, long offset, ulong len);
2120 [DllImport (LIBC, SetLastError=true,
2121 EntryPoint="openat")]
2122 private static extern int sys_openat (int dirfd,
2123 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2124 string pathname, int flags);
2127 // int openat(int dirfd, const char *pathname, int flags, mode_t mode);
2128 [DllImport (LIBC, SetLastError=true,
2129 EntryPoint="openat")]
2130 private static extern int sys_openat (int dirfd,
2131 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2132 string pathname, int flags, uint mode);
2134 public static int openat (int dirfd, string pathname, OpenFlags flags)
2136 int _flags = NativeConvert.FromOpenFlags (flags);
2137 return sys_openat (dirfd, pathname, _flags);
2140 public static int openat (int dirfd, string pathname, OpenFlags flags, FilePermissions mode)
2142 int _flags = NativeConvert.FromOpenFlags (flags);
2143 uint _mode = NativeConvert.FromFilePermissions (mode);
2144 return sys_openat (dirfd, pathname, _flags, _mode);
2147 [DllImport (MPH, SetLastError=true,
2148 EntryPoint="Mono_Posix_Syscall_get_at_fdcwd")]
2149 private static extern int get_at_fdcwd ();
2151 public static readonly int AT_FDCWD = get_at_fdcwd ();
2155 #region <fstab.h> Declarations
2157 // <fstab.h> -- COMPLETE
2160 private struct _Fstab {
2161 public IntPtr fs_spec;
2162 public IntPtr fs_file;
2163 public IntPtr fs_vfstype;
2164 public IntPtr fs_mntops;
2165 public IntPtr fs_type;
2167 public int fs_passno;
2168 public IntPtr _fs_buf_;
2171 private static void CopyFstab (Fstab to, ref _Fstab from)
2174 to.fs_spec = UnixMarshal.PtrToString (from.fs_spec);
2175 to.fs_file = UnixMarshal.PtrToString (from.fs_file);
2176 to.fs_vfstype = UnixMarshal.PtrToString (from.fs_vfstype);
2177 to.fs_mntops = UnixMarshal.PtrToString (from.fs_mntops);
2178 to.fs_type = UnixMarshal.PtrToString (from.fs_type);
2179 to.fs_freq = from.fs_freq;
2180 to.fs_passno = from.fs_passno;
2183 Stdlib.free (from._fs_buf_);
2184 from._fs_buf_ = IntPtr.Zero;
2188 internal static object fstab_lock = new object ();
2190 [DllImport (MPH, SetLastError=true,
2191 EntryPoint="Mono_Posix_Syscall_endfsent")]
2192 private static extern int sys_endfsent ();
2194 public static int endfsent ()
2197 return sys_endfsent ();
2201 [DllImport (MPH, SetLastError=true,
2202 EntryPoint="Mono_Posix_Syscall_getfsent")]
2203 private static extern int sys_getfsent (out _Fstab fs);
2205 public static Fstab getfsent ()
2210 r = sys_getfsent (out fsbuf);
2214 Fstab fs = new Fstab ();
2215 CopyFstab (fs, ref fsbuf);
2219 [DllImport (MPH, SetLastError=true,
2220 EntryPoint="Mono_Posix_Syscall_getfsfile")]
2221 private static extern int sys_getfsfile (
2222 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2223 string mount_point, out _Fstab fs);
2225 public static Fstab getfsfile (string mount_point)
2230 r = sys_getfsfile (mount_point, out fsbuf);
2234 Fstab fs = new Fstab ();
2235 CopyFstab (fs, ref fsbuf);
2239 [DllImport (MPH, SetLastError=true,
2240 EntryPoint="Mono_Posix_Syscall_getfsspec")]
2241 private static extern int sys_getfsspec (
2242 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2243 string special_file, out _Fstab fs);
2245 public static Fstab getfsspec (string special_file)
2250 r = sys_getfsspec (special_file, out fsbuf);
2254 Fstab fs = new Fstab ();
2255 CopyFstab (fs, ref fsbuf);
2259 [DllImport (MPH, SetLastError=true,
2260 EntryPoint="Mono_Posix_Syscall_setfsent")]
2261 private static extern int sys_setfsent ();
2263 public static int setfsent ()
2266 return sys_setfsent ();
2272 #region <grp.h> Declarations
2276 // TODO: putgrent(3), fgetgrent_r(), initgroups(3)
2279 [DllImport (LIBC, SetLastError=true, EntryPoint="getgrouplist")]
2280 private static extern int sys_getgrouplist (string user, uint grp, uint [] groups,ref int ngroups);
2282 public static Group [] getgrouplist (string username)
2284 if (username == null)
2285 throw new ArgumentNullException ("username");
2286 if (username.Trim () == "")
2287 throw new ArgumentException ("Username cannot be empty", "username");
2288 // Syscall to getpwnam to retrieve user uid
2289 Passwd pw = Syscall.getpwnam (username);
2291 throw new ArgumentException (string.Format ("User {0} does not exist", username), "username");
2292 return getgrouplist (pw);
2295 public static Group [] getgrouplist (Passwd user)
2298 throw new ArgumentNullException ("user");
2299 // initializing ngroups by 16 to get the group count
2302 // allocating buffer to store group uid's
2303 uint [] groups=null;
2305 Array.Resize (ref groups, ngroups*=2);
2306 res = sys_getgrouplist (user.pw_name, user.pw_gid, groups, ref ngroups);
2309 List<Group> result = new List<Group> ();
2311 for (int i = 0; i < res; i++) {
2312 gr = Syscall.getgrgid (groups [i]);
2316 return result.ToArray ();
2320 // int setgroups (size_t size, const gid_t *list);
2321 [DllImport (MPH, SetLastError=true,
2322 EntryPoint="Mono_Posix_Syscall_setgroups")]
2323 public static extern int setgroups (ulong size, uint[] list);
2325 public static int setgroups (uint [] list)
2327 return setgroups ((ulong) list.Length, list);
2331 private struct _Group
2333 public IntPtr gr_name;
2334 public IntPtr gr_passwd;
2335 [gid_t] public uint gr_gid;
2336 public int _gr_nmem_;
2337 public IntPtr gr_mem;
2338 public IntPtr _gr_buf_;
2341 private static void CopyGroup (Group to, ref _Group from)
2344 to.gr_gid = from.gr_gid;
2345 to.gr_name = UnixMarshal.PtrToString (from.gr_name);
2346 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
2347 to.gr_mem = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
2350 Stdlib.free (from.gr_mem);
2351 Stdlib.free (from._gr_buf_);
2352 from.gr_mem = IntPtr.Zero;
2353 from._gr_buf_ = IntPtr.Zero;
2357 internal static object grp_lock = new object ();
2359 [DllImport (MPH, SetLastError=true,
2360 EntryPoint="Mono_Posix_Syscall_getgrnam")]
2361 private static extern int sys_getgrnam (string name, out _Group group);
2363 public static Group getgrnam (string name)
2368 r = sys_getgrnam (name, out group);
2372 Group gr = new Group ();
2373 CopyGroup (gr, ref group);
2378 // struct group *getgrgid(gid_t gid);
2379 [DllImport (MPH, SetLastError=true,
2380 EntryPoint="Mono_Posix_Syscall_getgrgid")]
2381 private static extern int sys_getgrgid (uint uid, out _Group group);
2383 public static Group getgrgid (uint uid)
2388 r = sys_getgrgid (uid, out group);
2392 Group gr = new Group ();
2393 CopyGroup (gr, ref group);
2397 [DllImport (MPH, SetLastError=true,
2398 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
2399 private static extern int sys_getgrnam_r (
2400 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2401 string name, out _Group grbuf, out IntPtr grbufp);
2403 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
2408 int r = sys_getgrnam_r (name, out group, out _grbufp);
2409 if (r == 0 && _grbufp != IntPtr.Zero) {
2410 CopyGroup (grbuf, ref group);
2417 // int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
2418 // size_t buflen, struct group **gbufp);
2419 [DllImport (MPH, SetLastError=true,
2420 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
2421 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
2423 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
2428 int r = sys_getgrgid_r (uid, out group, out _grbufp);
2429 if (r == 0 && _grbufp != IntPtr.Zero) {
2430 CopyGroup (grbuf, ref group);
2436 [DllImport (MPH, SetLastError=true,
2437 EntryPoint="Mono_Posix_Syscall_getgrent")]
2438 private static extern int sys_getgrent (out _Group grbuf);
2440 public static Group getgrent ()
2445 r = sys_getgrent (out group);
2449 Group gr = new Group();
2450 CopyGroup (gr, ref group);
2454 [DllImport (MPH, SetLastError=true,
2455 EntryPoint="Mono_Posix_Syscall_setgrent")]
2456 private static extern int sys_setgrent ();
2458 public static int setgrent ()
2461 return sys_setgrent ();
2465 [DllImport (MPH, SetLastError=true,
2466 EntryPoint="Mono_Posix_Syscall_endgrent")]
2467 private static extern int sys_endgrent ();
2469 public static int endgrent ()
2472 return sys_endgrent ();
2476 [DllImport (MPH, SetLastError=true,
2477 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
2478 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
2480 public static Group fgetgrent (IntPtr stream)
2485 r = sys_fgetgrent (stream, out group);
2489 Group gr = new Group ();
2490 CopyGroup (gr, ref group);
2495 #region <pwd.h> Declarations
2499 // TODO: putpwent(3), fgetpwent_r()
2501 // SKIPPING: getpw(3): it's dangerous. Use getpwuid(3) instead.
2504 private struct _Passwd
2506 public IntPtr pw_name;
2507 public IntPtr pw_passwd;
2508 [uid_t] public uint pw_uid;
2509 [gid_t] public uint pw_gid;
2510 public IntPtr pw_gecos;
2511 public IntPtr pw_dir;
2512 public IntPtr pw_shell;
2513 public IntPtr _pw_buf_;
2516 private static void CopyPasswd (Passwd to, ref _Passwd from)
2519 to.pw_name = UnixMarshal.PtrToString (from.pw_name);
2520 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
2521 to.pw_uid = from.pw_uid;
2522 to.pw_gid = from.pw_gid;
2523 to.pw_gecos = UnixMarshal.PtrToString (from.pw_gecos);
2524 to.pw_dir = UnixMarshal.PtrToString (from.pw_dir);
2525 to.pw_shell = UnixMarshal.PtrToString (from.pw_shell);
2528 Stdlib.free (from._pw_buf_);
2529 from._pw_buf_ = IntPtr.Zero;
2533 internal static object pwd_lock = new object ();
2535 [DllImport (MPH, SetLastError=true,
2536 EntryPoint="Mono_Posix_Syscall_getpwnam")]
2537 private static extern int sys_getpwnam (string name, out _Passwd passwd);
2539 public static Passwd getpwnam (string name)
2544 r = sys_getpwnam (name, out passwd);
2548 Passwd pw = new Passwd ();
2549 CopyPasswd (pw, ref passwd);
2554 // struct passwd *getpwnuid(uid_t uid);
2555 [DllImport (MPH, SetLastError=true,
2556 EntryPoint="Mono_Posix_Syscall_getpwuid")]
2557 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
2559 public static Passwd getpwuid (uint uid)
2564 r = sys_getpwuid (uid, out passwd);
2568 Passwd pw = new Passwd ();
2569 CopyPasswd (pw, ref passwd);
2573 [DllImport (MPH, SetLastError=true,
2574 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
2575 private static extern int sys_getpwnam_r (
2576 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2577 string name, out _Passwd pwbuf, out IntPtr pwbufp);
2579 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
2584 int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
2585 if (r == 0 && _pwbufp != IntPtr.Zero) {
2586 CopyPasswd (pwbuf, ref passwd);
2593 // int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
2594 // buflen, struct passwd **pwbufp);
2595 [DllImport (MPH, SetLastError=true,
2596 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
2597 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
2599 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
2604 int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
2605 if (r == 0 && _pwbufp != IntPtr.Zero) {
2606 CopyPasswd (pwbuf, ref passwd);
2612 [DllImport (MPH, SetLastError=true,
2613 EntryPoint="Mono_Posix_Syscall_getpwent")]
2614 private static extern int sys_getpwent (out _Passwd pwbuf);
2616 public static Passwd getpwent ()
2621 r = sys_getpwent (out passwd);
2625 Passwd pw = new Passwd ();
2626 CopyPasswd (pw, ref passwd);
2630 [DllImport (MPH, SetLastError=true,
2631 EntryPoint="Mono_Posix_Syscall_setpwent")]
2632 private static extern int sys_setpwent ();
2634 public static int setpwent ()
2637 return sys_setpwent ();
2641 [DllImport (MPH, SetLastError=true,
2642 EntryPoint="Mono_Posix_Syscall_endpwent")]
2643 private static extern int sys_endpwent ();
2645 public static int endpwent ()
2648 return sys_endpwent ();
2652 [DllImport (MPH, SetLastError=true,
2653 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
2654 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
2656 public static Passwd fgetpwent (IntPtr stream)
2661 r = sys_fgetpwent (stream, out passwd);
2665 Passwd pw = new Passwd ();
2666 CopyPasswd (pw, ref passwd);
2671 #region <signal.h> Declarations
2675 [DllImport (MPH, SetLastError=true,
2676 EntryPoint="Mono_Posix_Syscall_psignal")]
2677 private static extern int psignal (int sig, string s);
2679 public static int psignal (Signum sig, string s)
2681 int signum = NativeConvert.FromSignum (sig);
2682 return psignal (signum, s);
2686 // int kill(pid_t pid, int sig);
2687 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
2688 private static extern int sys_kill (int pid, int sig);
2690 public static int kill (int pid, Signum sig)
2692 int _sig = NativeConvert.FromSignum (sig);
2693 return sys_kill (pid, _sig);
2696 private static object signal_lock = new object ();
2698 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
2699 private static extern IntPtr sys_strsignal (int sig);
2701 public static string strsignal (Signum sig)
2703 int s = NativeConvert.FromSignum (sig);
2704 lock (signal_lock) {
2705 IntPtr r = sys_strsignal (s);
2706 return UnixMarshal.PtrToString (r);
2710 // TODO: sigaction(2)
2711 // TODO: sigsuspend(2)
2712 // TODO: sigpending(2)
2716 #region <stdio.h> Declarations
2720 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
2721 private static extern int _L_ctermid ();
2723 public static readonly int L_ctermid = _L_ctermid ();
2725 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
2726 private static extern int _L_cuserid ();
2728 public static readonly int L_cuserid = _L_cuserid ();
2730 internal static object getlogin_lock = new object ();
2732 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
2733 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
2735 [Obsolete ("\"Nobody knows precisely what cuserid() does... " +
2736 "DO NOT USE cuserid().\n" +
2737 "`string' must hold L_cuserid characters. Use getlogin_r instead.")]
2738 public static string cuserid (StringBuilder @string)
2740 if (@string.Capacity < L_cuserid) {
2741 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
2743 lock (getlogin_lock) {
2744 IntPtr r = sys_cuserid (@string);
2745 return UnixMarshal.PtrToString (r);
2749 [DllImport (LIBC, SetLastError=true)]
2750 public static extern int renameat (int olddirfd,
2751 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2752 string oldpath, int newdirfd,
2753 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2757 #region <stdlib.h> Declarations
2761 [DllImport (LIBC, SetLastError=true)]
2762 public static extern int mkstemp (StringBuilder template);
2764 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdtemp")]
2765 private static extern IntPtr sys_mkdtemp (StringBuilder template);
2767 public static StringBuilder mkdtemp (StringBuilder template)
2769 if (sys_mkdtemp (template) == IntPtr.Zero)
2774 [DllImport (LIBC, SetLastError=true)]
2775 public static extern int ttyslot ();
2777 [Obsolete ("This is insecure and should not be used", true)]
2778 public static int setkey (string key)
2780 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
2785 #region <string.h> Declarations
2791 // int strerror_r(int errnum, char *buf, size_t n);
2792 [DllImport (MPH, SetLastError=true,
2793 EntryPoint="Mono_Posix_Syscall_strerror_r")]
2794 private static extern int sys_strerror_r (int errnum,
2795 [Out] StringBuilder buf, ulong n);
2797 public static int strerror_r (Errno errnum, StringBuilder buf, ulong n)
2799 int e = NativeConvert.FromErrno (errnum);
2800 return sys_strerror_r (e, buf, n);
2803 public static int strerror_r (Errno errnum, StringBuilder buf)
2805 return strerror_r (errnum, buf, (ulong) buf.Capacity);
2810 #region <sys/epoll.h> Declarations
2812 public static int epoll_create (int size)
2814 return sys_epoll_create (size);
2817 public static int epoll_create (EpollFlags flags)
2819 return sys_epoll_create1 (flags);
2822 public static int epoll_ctl (int epfd, EpollOp op, int fd, EpollEvents events)
2824 EpollEvent ee = new EpollEvent ();
2828 return epoll_ctl (epfd, op, fd, ref ee);
2831 public static int epoll_wait (int epfd, EpollEvent [] events, int max_events, int timeout)
2833 if (events.Length < max_events)
2834 throw new ArgumentOutOfRangeException ("events", "Must refer to at least 'max_events' elements.");
2836 return sys_epoll_wait (epfd, events, max_events, timeout);
2839 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_create")]
2840 private static extern int sys_epoll_create (int size);
2842 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_create1")]
2843 private static extern int sys_epoll_create1 (EpollFlags flags);
2845 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_ctl")]
2846 public static extern int epoll_ctl (int epfd, EpollOp op, int fd, ref EpollEvent ee);
2848 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_wait")]
2849 private static extern int sys_epoll_wait (int epfd, EpollEvent [] ee, int maxevents, int timeout);
2852 #region <sys/mman.h> Declarations
2858 // int posix_madvise(void *addr, size_t len, int advice);
2859 [DllImport (MPH, SetLastError=true,
2860 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
2861 public static extern int posix_madvise (IntPtr addr, ulong len,
2862 PosixMadviseAdvice advice);
2864 public static readonly IntPtr MAP_FAILED = unchecked((IntPtr)(-1));
2866 [DllImport (MPH, SetLastError=true,
2867 EntryPoint="Mono_Posix_Syscall_mmap")]
2868 public static extern IntPtr mmap (IntPtr start, ulong length,
2869 MmapProts prot, MmapFlags flags, int fd, long offset);
2871 [DllImport (MPH, SetLastError=true,
2872 EntryPoint="Mono_Posix_Syscall_munmap")]
2873 public static extern int munmap (IntPtr start, ulong length);
2875 [DllImport (MPH, SetLastError=true,
2876 EntryPoint="Mono_Posix_Syscall_mprotect")]
2877 public static extern int mprotect (IntPtr start, ulong len, MmapProts prot);
2879 [DllImport (MPH, SetLastError=true,
2880 EntryPoint="Mono_Posix_Syscall_msync")]
2881 public static extern int msync (IntPtr start, ulong len, MsyncFlags flags);
2883 [DllImport (MPH, SetLastError=true,
2884 EntryPoint="Mono_Posix_Syscall_mlock")]
2885 public static extern int mlock (IntPtr start, ulong len);
2887 [DllImport (MPH, SetLastError=true,
2888 EntryPoint="Mono_Posix_Syscall_munlock")]
2889 public static extern int munlock (IntPtr start, ulong len);
2891 [DllImport (LIBC, SetLastError=true, EntryPoint="mlockall")]
2892 private static extern int sys_mlockall (int flags);
2894 public static int mlockall (MlockallFlags flags)
2896 int _flags = NativeConvert.FromMlockallFlags (flags);
2897 return sys_mlockall (_flags);
2900 [DllImport (LIBC, SetLastError=true)]
2901 public static extern int munlockall ();
2903 [DllImport (MPH, SetLastError=true,
2904 EntryPoint="Mono_Posix_Syscall_mremap")]
2905 public static extern IntPtr mremap (IntPtr old_address, ulong old_size,
2906 ulong new_size, MremapFlags flags);
2908 [DllImport (MPH, SetLastError=true,
2909 EntryPoint="Mono_Posix_Syscall_mincore")]
2910 public static extern int mincore (IntPtr start, ulong length, byte[] vec);
2912 [DllImport (MPH, SetLastError=true,
2913 EntryPoint="Mono_Posix_Syscall_remap_file_pages")]
2914 public static extern int remap_file_pages (IntPtr start, ulong size,
2915 MmapProts prot, long pgoff, MmapFlags flags);
2919 #region <sys/poll.h> Declarations
2921 // <sys/poll.h> -- COMPLETE
2924 private struct _pollfd {
2926 public short events;
2927 public short revents;
2930 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
2931 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
2933 public static int poll (Pollfd [] fds, uint nfds, int timeout)
2935 if (fds.Length < nfds)
2936 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
2938 _pollfd[] send = new _pollfd[nfds];
2940 for (int i = 0; i < send.Length; i++) {
2941 send [i].fd = fds [i].fd;
2942 send [i].events = NativeConvert.FromPollEvents (fds [i].events);
2945 int r = sys_poll (send, nfds, timeout);
2947 for (int i = 0; i < send.Length; i++) {
2948 fds [i].revents = NativeConvert.ToPollEvents (send [i].revents);
2954 public static int poll (Pollfd [] fds, int timeout)
2956 return poll (fds, (uint) fds.Length, timeout);
2969 // TODO: setrlimit(2)
2970 // TODO: getrlimit(2)
2971 // TODO: getrusage(2)
2975 #region <sys/sendfile.h> Declarations
2977 // <sys/sendfile.h> -- COMPLETE
2980 [DllImport (MPH, SetLastError=true,
2981 EntryPoint="Mono_Posix_Syscall_sendfile")]
2982 public static extern long sendfile (int out_fd, int in_fd,
2983 ref long offset, ulong count);
2987 #region <sys/stat.h> Declarations
2989 // <sys/stat.h> -- COMPLETE
2991 [DllImport (MPH, SetLastError=true,
2992 EntryPoint="Mono_Posix_Syscall_stat")]
2993 public static extern int stat (
2994 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2995 string file_name, out Stat buf);
2997 [DllImport (MPH, SetLastError=true,
2998 EntryPoint="Mono_Posix_Syscall_fstat")]
2999 public static extern int fstat (int filedes, out Stat buf);
3001 [DllImport (MPH, SetLastError=true,
3002 EntryPoint="Mono_Posix_Syscall_lstat")]
3003 public static extern int lstat (
3004 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3005 string file_name, out Stat buf);
3008 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
3009 // All take FilePermissions
3012 // int chmod(const char *path, mode_t mode);
3013 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
3014 private static extern int sys_chmod (
3015 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3016 string path, uint mode);
3018 public static int chmod (string path, FilePermissions mode)
3020 uint _mode = NativeConvert.FromFilePermissions (mode);
3021 return sys_chmod (path, _mode);
3025 // int chmod(int filedes, mode_t mode);
3026 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
3027 private static extern int sys_fchmod (int filedes, uint mode);
3029 public static int fchmod (int filedes, FilePermissions mode)
3031 uint _mode = NativeConvert.FromFilePermissions (mode);
3032 return sys_fchmod (filedes, _mode);
3036 // mode_t umask(mode_t mask);
3037 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
3038 private static extern uint sys_umask (uint mask);
3040 public static FilePermissions umask (FilePermissions mask)
3042 uint _mask = NativeConvert.FromFilePermissions (mask);
3043 uint r = sys_umask (_mask);
3044 return NativeConvert.ToFilePermissions (r);
3048 // int mkdir(const char *pathname, mode_t mode);
3049 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
3050 private static extern int sys_mkdir (
3051 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3052 string oldpath, uint mode);
3054 public static int mkdir (string oldpath, FilePermissions mode)
3056 uint _mode = NativeConvert.FromFilePermissions (mode);
3057 return sys_mkdir (oldpath, _mode);
3061 // int mknod (const char *pathname, mode_t mode, dev_t dev);
3062 [DllImport (MPH, SetLastError=true,
3063 EntryPoint="Mono_Posix_Syscall_mknod")]
3064 public static extern int mknod (
3065 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3066 string pathname, FilePermissions mode, ulong dev);
3069 // int mkfifo(const char *pathname, mode_t mode);
3070 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
3071 private static extern int sys_mkfifo (
3072 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3073 string pathname, uint mode);
3075 public static int mkfifo (string pathname, FilePermissions mode)
3077 uint _mode = NativeConvert.FromFilePermissions (mode);
3078 return sys_mkfifo (pathname, _mode);
3082 // int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
3083 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmodat")]
3084 private static extern int sys_fchmodat (int dirfd,
3085 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3086 string pathname, uint mode, int flags);
3088 public static int fchmodat (int dirfd, string pathname, FilePermissions mode, AtFlags flags)
3090 uint _mode = NativeConvert.FromFilePermissions (mode);
3091 int _flags = NativeConvert.FromAtFlags (flags);
3092 return sys_fchmodat (dirfd, pathname, _mode, _flags);
3095 [DllImport (MPH, SetLastError=true,
3096 EntryPoint="Mono_Posix_Syscall_fstatat")]
3097 public static extern int fstatat (int dirfd,
3098 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3099 string file_name, out Stat buf, AtFlags flags);
3101 [DllImport (MPH, SetLastError=true,
3102 EntryPoint="Mono_Posix_Syscall_get_utime_now")]
3103 private static extern long get_utime_now ();
3105 [DllImport (MPH, SetLastError=true,
3106 EntryPoint="Mono_Posix_Syscall_get_utime_omit")]
3107 private static extern long get_utime_omit ();
3109 public static readonly long UTIME_NOW = get_utime_now ();
3111 public static readonly long UTIME_OMIT = get_utime_omit ();
3113 [DllImport (MPH, SetLastError=true,
3114 EntryPoint="Mono_Posix_Syscall_futimens")]
3115 private static extern int sys_futimens (int fd, Timespec[] times);
3117 public static int futimens (int fd, Timespec[] times)
3119 if (times != null && times.Length != 2) {
3120 SetLastError (Errno.EINVAL);
3123 return sys_futimens (fd, times);
3126 [DllImport (MPH, SetLastError=true,
3127 EntryPoint="Mono_Posix_Syscall_utimensat")]
3128 private static extern int sys_utimensat (int dirfd,
3129 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3130 string pathname, Timespec[] times, int flags);
3132 public static int utimensat (int dirfd, string pathname, Timespec[] times, AtFlags flags)
3134 if (times != null && times.Length != 2) {
3135 SetLastError (Errno.EINVAL);
3138 int _flags = NativeConvert.FromAtFlags (flags);
3139 return sys_utimensat (dirfd, pathname, times, _flags);
3143 // int mkdirat(int dirfd, const char *pathname, mode_t mode);
3144 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdirat")]
3145 private static extern int sys_mkdirat (int dirfd,
3146 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3147 string oldpath, uint mode);
3149 public static int mkdirat (int dirfd, string oldpath, FilePermissions mode)
3151 uint _mode = NativeConvert.FromFilePermissions (mode);
3152 return sys_mkdirat (dirfd, oldpath, _mode);
3156 // int mknodat (int dirfd, const char *pathname, mode_t mode, dev_t dev);
3157 [DllImport (MPH, SetLastError=true,
3158 EntryPoint="Mono_Posix_Syscall_mknodat")]
3159 public static extern int mknodat (int dirfd,
3160 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3161 string pathname, FilePermissions mode, ulong dev);
3164 // int mkfifoat(int dirfd, const char *pathname, mode_t mode);
3165 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifoat")]
3166 private static extern int sys_mkfifoat (int dirfd,
3167 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3168 string pathname, uint mode);
3170 public static int mkfifoat (int dirfd, string pathname, FilePermissions mode)
3172 uint _mode = NativeConvert.FromFilePermissions (mode);
3173 return sys_mkfifoat (dirfd, pathname, _mode);
3177 #region <sys/stat.h> Declarations
3182 [DllImport (MPH, SetLastError=true,
3183 EntryPoint="Mono_Posix_Syscall_statvfs")]
3184 public static extern int statvfs (
3185 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3186 string path, out Statvfs buf);
3188 [DllImport (MPH, SetLastError=true,
3189 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
3190 public static extern int fstatvfs (int fd, out Statvfs buf);
3194 #region <sys/time.h> Declarations
3198 // TODO: adjtime(), getitimer(2), setitimer(2)
3200 [DllImport (MPH, SetLastError=true,
3201 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3202 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
3204 [DllImport (MPH, SetLastError=true,
3205 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3206 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
3208 public static int gettimeofday (out Timeval tv)
3210 return gettimeofday (out tv, IntPtr.Zero);
3213 [DllImport (MPH, SetLastError=true,
3214 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3215 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
3217 public static int gettimeofday (out Timezone tz)
3219 return gettimeofday (IntPtr.Zero, out tz);
3222 [DllImport (MPH, SetLastError=true,
3223 EntryPoint="Mono_Posix_Syscall_settimeofday")]
3224 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
3226 [DllImport (MPH, SetLastError=true,
3227 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3228 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
3230 public static int settimeofday (ref Timeval tv)
3232 return settimeofday (ref tv, IntPtr.Zero);
3235 [DllImport (MPH, SetLastError=true,
3236 EntryPoint="Mono_Posix_Syscall_utimes")]
3237 private static extern int sys_utimes (
3238 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3239 string filename, Timeval[] tvp);
3241 public static int utimes (string filename, Timeval[] tvp)
3243 if (tvp != null && tvp.Length != 2) {
3244 SetLastError (Errno.EINVAL);
3247 return sys_utimes (filename, tvp);
3250 [DllImport (MPH, SetLastError=true,
3251 EntryPoint="Mono_Posix_Syscall_lutimes")]
3252 private static extern int sys_lutimes (
3253 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3254 string filename, Timeval[] tvp);
3256 public static int lutimes (string filename, Timeval[] tvp)
3258 if (tvp != null && tvp.Length != 2) {
3259 SetLastError (Errno.EINVAL);
3262 return sys_lutimes (filename, tvp);
3265 [DllImport (MPH, SetLastError=true,
3266 EntryPoint="Mono_Posix_Syscall_futimes")]
3267 private static extern int sys_futimes (int fd, Timeval[] tvp);
3269 public static int futimes (int fd, Timeval[] tvp)
3271 if (tvp != null && tvp.Length != 2) {
3272 SetLastError (Errno.EINVAL);
3275 return sys_futimes (fd, tvp);
3297 private struct _Utsname
3299 public IntPtr sysname;
3300 public IntPtr nodename;
3301 public IntPtr release;
3302 public IntPtr version;
3303 public IntPtr machine;
3304 public IntPtr domainname;
3305 public IntPtr _buf_;
3308 private static void CopyUtsname (ref Utsname to, ref _Utsname from)
3311 to = new Utsname ();
3312 to.sysname = UnixMarshal.PtrToString (from.sysname);
3313 to.nodename = UnixMarshal.PtrToString (from.nodename);
3314 to.release = UnixMarshal.PtrToString (from.release);
3315 to.version = UnixMarshal.PtrToString (from.version);
3316 to.machine = UnixMarshal.PtrToString (from.machine);
3317 to.domainname = UnixMarshal.PtrToString (from.domainname);
3320 Stdlib.free (from._buf_);
3321 from._buf_ = IntPtr.Zero;
3325 [DllImport (MPH, SetLastError=true,
3326 EntryPoint="Mono_Posix_Syscall_uname")]
3327 private static extern int sys_uname (out _Utsname buf);
3329 public static int uname (out Utsname buf)
3332 int r = sys_uname (out _buf);
3333 buf = new Utsname ();
3335 CopyUtsname (ref buf, ref _buf);
3340 #region <sys/wait.h> Declarations
3346 // pid_t wait(int *status);
3347 [DllImport (LIBC, SetLastError=true)]
3348 public static extern int wait (out int status);
3351 // pid_t waitpid(pid_t pid, int *status, int options);
3352 [DllImport (LIBC, SetLastError=true)]
3353 private static extern int waitpid (int pid, out int status, int options);
3355 public static int waitpid (int pid, out int status, WaitOptions options)
3357 int _options = NativeConvert.FromWaitOptions (options);
3358 return waitpid (pid, out status, _options);
3361 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
3362 private static extern int _WIFEXITED (int status);
3364 public static bool WIFEXITED (int status)
3366 return _WIFEXITED (status) != 0;
3369 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
3370 public static extern int WEXITSTATUS (int status);
3372 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
3373 private static extern int _WIFSIGNALED (int status);
3375 public static bool WIFSIGNALED (int status)
3377 return _WIFSIGNALED (status) != 0;
3380 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
3381 private static extern int _WTERMSIG (int status);
3383 public static Signum WTERMSIG (int status)
3385 int r = _WTERMSIG (status);
3386 return NativeConvert.ToSignum (r);
3389 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
3390 private static extern int _WIFSTOPPED (int status);
3392 public static bool WIFSTOPPED (int status)
3394 return _WIFSTOPPED (status) != 0;
3397 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
3398 private static extern int _WSTOPSIG (int status);
3400 public static Signum WSTOPSIG (int status)
3402 int r = _WSTOPSIG (status);
3403 return NativeConvert.ToSignum (r);
3412 #region <syslog.h> Declarations
3417 [DllImport (MPH, SetLastError=true,
3418 EntryPoint="Mono_Posix_Syscall_openlog")]
3419 private static extern int sys_openlog (IntPtr ident, int option, int facility);
3421 public static int openlog (IntPtr ident, SyslogOptions option,
3422 SyslogFacility defaultFacility)
3424 int _option = NativeConvert.FromSyslogOptions (option);
3425 int _facility = NativeConvert.FromSyslogFacility (defaultFacility);
3427 return sys_openlog (ident, _option, _facility);
3430 [DllImport (MPH, SetLastError=true,
3431 EntryPoint="Mono_Posix_Syscall_syslog")]
3432 private static extern int sys_syslog (int priority, string message);
3434 public static int syslog (SyslogFacility facility, SyslogLevel level, string message)
3436 int _facility = NativeConvert.FromSyslogFacility (facility);
3437 int _level = NativeConvert.FromSyslogLevel (level);
3438 return sys_syslog (_facility | _level, GetSyslogMessage (message));
3441 public static int syslog (SyslogLevel level, string message)
3443 int _level = NativeConvert.FromSyslogLevel (level);
3444 return sys_syslog (_level, GetSyslogMessage (message));
3447 private static string GetSyslogMessage (string message)
3449 return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
3452 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
3453 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
3454 public static int syslog (SyslogFacility facility, SyslogLevel level,
3455 string format, params object[] parameters)
3457 int _facility = NativeConvert.FromSyslogFacility (facility);
3458 int _level = NativeConvert.FromSyslogLevel (level);
3460 object[] _parameters = new object[checked(parameters.Length+2)];
3461 _parameters [0] = _facility | _level;
3462 _parameters [1] = format;
3463 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3464 return (int) XPrintfFunctions.syslog (_parameters);
3467 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
3468 "Use syslog(SyslogLevel, string) instead.")]
3469 public static int syslog (SyslogLevel level, string format,
3470 params object[] parameters)
3472 int _level = NativeConvert.FromSyslogLevel (level);
3474 object[] _parameters = new object[checked(parameters.Length+2)];
3475 _parameters [0] = _level;
3476 _parameters [1] = format;
3477 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3478 return (int) XPrintfFunctions.syslog (_parameters);
3481 [DllImport (MPH, SetLastError=true,
3482 EntryPoint="Mono_Posix_Syscall_closelog")]
3483 public static extern int closelog ();
3485 [DllImport (LIBC, SetLastError=true, EntryPoint="setlogmask")]
3486 private static extern int sys_setlogmask (int mask);
3488 public static int setlogmask (SyslogLevel mask)
3490 int _mask = NativeConvert.FromSyslogLevel (mask);
3491 return sys_setlogmask (_mask);
3496 #region <time.h> Declarations
3503 // int nanosleep(const struct timespec *req, struct timespec *rem);
3504 [DllImport (MPH, SetLastError=true,
3505 EntryPoint="Mono_Posix_Syscall_nanosleep")]
3506 public static extern int nanosleep (ref Timespec req, ref Timespec rem);
3509 // int stime(time_t *t);
3510 [DllImport (MPH, SetLastError=true,
3511 EntryPoint="Mono_Posix_Syscall_stime")]
3512 public static extern int stime (ref long t);
3515 // time_t time(time_t *t);
3516 [DllImport (MPH, SetLastError=true,
3517 EntryPoint="Mono_Posix_Syscall_time")]
3518 public static extern long time (out long t);
3528 #region <unistd.h> Declarations
3532 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
3533 // other TODOs listed below.
3535 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
3536 private static extern int sys_access (
3537 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3538 string pathname, int mode);
3540 public static int access (string pathname, AccessModes mode)
3542 int _mode = NativeConvert.FromAccessModes (mode);
3543 return sys_access (pathname, _mode);
3547 // off_t lseek(int filedes, off_t offset, int whence);
3548 [DllImport (MPH, SetLastError=true,
3549 EntryPoint="Mono_Posix_Syscall_lseek")]
3550 private static extern long sys_lseek (int fd, long offset, int whence);
3552 public static long lseek (int fd, long offset, SeekFlags whence)
3554 short _whence = NativeConvert.FromSeekFlags (whence);
3555 return sys_lseek (fd, offset, _whence);
3558 [DllImport (LIBC, SetLastError=true)]
3559 public static extern int close (int fd);
3562 // ssize_t read(int fd, void *buf, size_t count);
3563 [DllImport (MPH, SetLastError=true,
3564 EntryPoint="Mono_Posix_Syscall_read")]
3565 public static extern long read (int fd, IntPtr buf, ulong count);
3567 public static unsafe long read (int fd, void *buf, ulong count)
3569 return read (fd, (IntPtr) buf, count);
3573 // ssize_t write(int fd, const void *buf, size_t count);
3574 [DllImport (MPH, SetLastError=true,
3575 EntryPoint="Mono_Posix_Syscall_write")]
3576 public static extern long write (int fd, IntPtr buf, ulong count);
3578 public static unsafe long write (int fd, void *buf, ulong count)
3580 return write (fd, (IntPtr) buf, count);
3584 // ssize_t pread(int fd, void *buf, size_t count, off_t offset);
3585 [DllImport (MPH, SetLastError=true,
3586 EntryPoint="Mono_Posix_Syscall_pread")]
3587 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
3589 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
3591 return pread (fd, (IntPtr) buf, count, offset);
3595 // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
3596 [DllImport (MPH, SetLastError=true,
3597 EntryPoint="Mono_Posix_Syscall_pwrite")]
3598 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
3600 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
3602 return pwrite (fd, (IntPtr) buf, count, offset);
3605 [DllImport (MPH, SetLastError=true,
3606 EntryPoint="Mono_Posix_Syscall_pipe")]
3607 public static extern int pipe (out int reading, out int writing);
3609 public static int pipe (int[] filedes)
3611 if (filedes == null || filedes.Length != 2) {
3615 int reading, writing;
3616 int r = pipe (out reading, out writing);
3617 filedes[0] = reading;
3618 filedes[1] = writing;
3622 [DllImport (LIBC, SetLastError=true)]
3623 public static extern uint alarm (uint seconds);
3625 [DllImport (LIBC, SetLastError=true)]
3626 public static extern uint sleep (uint seconds);
3628 [DllImport (LIBC, SetLastError=true)]
3629 public static extern uint ualarm (uint usecs, uint interval);
3631 [DllImport (LIBC, SetLastError=true)]
3632 public static extern int pause ();
3635 // int chown(const char *path, uid_t owner, gid_t group);
3636 [DllImport (LIBC, SetLastError=true)]
3637 public static extern int chown (
3638 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3639 string path, uint owner, uint group);
3642 // int fchown(int fd, uid_t owner, gid_t group);
3643 [DllImport (LIBC, SetLastError=true)]
3644 public static extern int fchown (int fd, uint owner, uint group);
3647 // int lchown(const char *path, uid_t owner, gid_t group);
3648 [DllImport (LIBC, SetLastError=true)]
3649 public static extern int lchown (
3650 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3651 string path, uint owner, uint group);
3653 [DllImport (LIBC, SetLastError=true)]
3654 public static extern int chdir (
3655 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3658 [DllImport (LIBC, SetLastError=true)]
3659 public static extern int fchdir (int fd);
3662 // char *getcwd(char *buf, size_t size);
3663 [DllImport (MPH, SetLastError=true,
3664 EntryPoint="Mono_Posix_Syscall_getcwd")]
3665 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
3667 public static StringBuilder getcwd (StringBuilder buf)
3669 getcwd (buf, (ulong) buf.Capacity);
3673 // getwd(2) is deprecated; don't expose it.
3675 [DllImport (LIBC, SetLastError=true)]
3676 public static extern int dup (int fd);
3678 [DllImport (LIBC, SetLastError=true)]
3679 public static extern int dup2 (int fd, int fd2);
3681 // TODO: does Mono marshal arrays properly?
3682 [DllImport (LIBC, SetLastError=true)]
3683 public static extern int execve (
3684 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3685 string path, string[] argv, string[] envp);
3687 [DllImport (LIBC, SetLastError=true)]
3688 public static extern int fexecve (int fd, string[] argv, string[] envp);
3690 [DllImport (LIBC, SetLastError=true)]
3691 public static extern int execv (
3692 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3693 string path, string[] argv);
3695 // TODO: execle, execl, execlp
3696 [DllImport (LIBC, SetLastError=true)]
3697 public static extern int execvp (
3698 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3699 string path, string[] argv);
3701 [DllImport (LIBC, SetLastError=true)]
3702 public static extern int nice (int inc);
3704 [DllImport (LIBC, SetLastError=true)]
3705 [CLSCompliant (false)]
3706 public static extern int _exit (int status);
3708 [DllImport (MPH, SetLastError=true,
3709 EntryPoint="Mono_Posix_Syscall_fpathconf")]
3710 public static extern long fpathconf (int filedes, PathconfName name, Errno defaultError);
3712 public static long fpathconf (int filedes, PathconfName name)
3714 return fpathconf (filedes, name, (Errno) 0);
3717 [DllImport (MPH, SetLastError=true,
3718 EntryPoint="Mono_Posix_Syscall_pathconf")]
3719 public static extern long pathconf (
3720 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3721 string path, PathconfName name, Errno defaultError);
3723 public static long pathconf (string path, PathconfName name)
3725 return pathconf (path, name, (Errno) 0);
3728 [DllImport (MPH, SetLastError=true,
3729 EntryPoint="Mono_Posix_Syscall_sysconf")]
3730 public static extern long sysconf (SysconfName name, Errno defaultError);
3732 public static long sysconf (SysconfName name)
3734 return sysconf (name, (Errno) 0);
3738 // size_t confstr(int name, char *buf, size_t len);
3739 [DllImport (MPH, SetLastError=true,
3740 EntryPoint="Mono_Posix_Syscall_confstr")]
3741 public static extern ulong confstr (ConfstrName name, [Out] StringBuilder buf, ulong len);
3744 // pid_t getpid(void);
3745 [DllImport (LIBC, SetLastError=true)]
3746 public static extern int getpid ();
3749 // pid_t getppid(void);
3750 [DllImport (LIBC, SetLastError=true)]
3751 public static extern int getppid ();
3754 // int setpgid(pid_t pid, pid_t pgid);
3755 [DllImport (LIBC, SetLastError=true)]
3756 public static extern int setpgid (int pid, int pgid);
3759 // pid_t getpgid(pid_t pid);
3760 [DllImport (LIBC, SetLastError=true)]
3761 public static extern int getpgid (int pid);
3763 [DllImport (LIBC, SetLastError=true)]
3764 public static extern int setpgrp ();
3767 // pid_t getpgrp(void);
3768 [DllImport (LIBC, SetLastError=true)]
3769 public static extern int getpgrp ();
3772 // pid_t setsid(void);
3773 [DllImport (LIBC, SetLastError=true)]
3774 public static extern int setsid ();
3777 // pid_t getsid(pid_t pid);
3778 [DllImport (LIBC, SetLastError=true)]
3779 public static extern int getsid (int pid);
3782 // uid_t getuid(void);
3783 [DllImport (LIBC, SetLastError=true)]
3784 public static extern uint getuid ();
3787 // uid_t geteuid(void);
3788 [DllImport (LIBC, SetLastError=true)]
3789 public static extern uint geteuid ();
3792 // gid_t getgid(void);
3793 [DllImport (LIBC, SetLastError=true)]
3794 public static extern uint getgid ();
3797 // gid_t getgid(void);
3798 [DllImport (LIBC, SetLastError=true)]
3799 public static extern uint getegid ();
3802 // int getgroups(int size, gid_t list[]);
3803 [DllImport (LIBC, SetLastError=true)]
3804 public static extern int getgroups (int size, uint[] list);
3806 public static int getgroups (uint[] list)
3808 return getgroups (list.Length, list);
3812 // int setuid(uid_t uid);
3813 [DllImport (LIBC, SetLastError=true)]
3814 public static extern int setuid (uint uid);
3817 // int setreuid(uid_t ruid, uid_t euid);
3818 [DllImport (LIBC, SetLastError=true)]
3819 public static extern int setreuid (uint ruid, uint euid);
3822 // int setregid(gid_t ruid, gid_t euid);
3823 [DllImport (LIBC, SetLastError=true)]
3824 public static extern int setregid (uint rgid, uint egid);
3827 // int seteuid(uid_t euid);
3828 [DllImport (LIBC, SetLastError=true)]
3829 public static extern int seteuid (uint euid);
3832 // int setegid(gid_t euid);
3833 [DllImport (LIBC, SetLastError=true)]
3834 public static extern int setegid (uint uid);
3837 // int setgid(gid_t gid);
3838 [DllImport (LIBC, SetLastError=true)]
3839 public static extern int setgid (uint gid);
3842 // int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
3843 [DllImport (LIBC, SetLastError=true)]
3844 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
3847 // int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
3848 [DllImport (LIBC, SetLastError=true)]
3849 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
3852 // int setresuid(uid_t ruid, uid_t euid, uid_t suid);
3853 [DllImport (LIBC, SetLastError=true)]
3854 public static extern int setresuid (uint ruid, uint euid, uint suid);
3857 // int setresgid(gid_t ruid, gid_t euid, gid_t suid);
3858 [DllImport (LIBC, SetLastError=true)]
3859 public static extern int setresgid (uint rgid, uint egid, uint sgid);
3863 // pid_t fork(void);
3864 [DllImport (LIBC, SetLastError=true)]
3865 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " +
3866 "shutdown code.\nUse System.Diagnostics.Process instead")]
3867 private static extern int fork ();
3870 // pid_t vfork(void);
3871 [DllImport (LIBC, SetLastError=true)]
3872 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " +
3873 "shutdown code.\nUse System.Diagnostics.Process instead")]
3874 private static extern int vfork ();
3877 private static object tty_lock = new object ();
3879 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
3880 private static extern IntPtr sys_ttyname (int fd);
3882 public static string ttyname (int fd)
3885 IntPtr r = sys_ttyname (fd);
3886 return UnixMarshal.PtrToString (r);
3891 // int ttyname_r(int fd, char *buf, size_t buflen);
3892 [DllImport (MPH, SetLastError=true,
3893 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
3894 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
3896 public static int ttyname_r (int fd, StringBuilder buf)
3898 return ttyname_r (fd, buf, (ulong) buf.Capacity);
3901 [DllImport (LIBC, EntryPoint="isatty")]
3902 private static extern int sys_isatty (int fd);
3904 public static bool isatty (int fd)
3906 return sys_isatty (fd) == 1;
3909 [DllImport (LIBC, SetLastError=true)]
3910 public static extern int link (
3911 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3913 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3916 [DllImport (LIBC, SetLastError=true)]
3917 public static extern int symlink (
3918 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3920 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3923 delegate long DoReadlinkFun (byte[] target);
3925 // Helper function for readlink(string, StringBuilder) and readlinkat (int, string, StringBuilder)
3926 static int ReadlinkIntoStringBuilder (DoReadlinkFun doReadlink, [Out] StringBuilder buf, ulong bufsiz)
3928 // bufsiz > int.MaxValue can't work because StringBuilder can store only int.MaxValue chars
3929 int bufsizInt = checked ((int) bufsiz);
3930 var target = new byte [bufsizInt];
3932 var r = doReadlink (target);
3934 return checked ((int) r);
3937 var chars = UnixEncoding.Instance.GetChars (target, 0, checked ((int) r));
3938 // Make sure that at more bufsiz chars are written
3939 buf.Append (chars, 0, System.Math.Min (bufsizInt, chars.Length));
3940 if (r == bufsizInt) {
3941 // may not have read full contents; fill 'buf' so that caller can properly check
3942 buf.Append (new string ('\x00', bufsizInt - buf.Length));
3948 // ssize_t readlink(const char *path, char *buf, size_t bufsize);
3949 public static int readlink (string path, [Out] StringBuilder buf, ulong bufsiz)
3951 return ReadlinkIntoStringBuilder (target => readlink (path, target), buf, bufsiz);
3954 public static int readlink (string path, [Out] StringBuilder buf)
3956 return readlink (path, buf, (ulong) buf.Capacity);
3959 [DllImport (MPH, SetLastError=true,
3960 EntryPoint="Mono_Posix_Syscall_readlink")]
3961 private static extern long readlink (
3962 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3963 string path, byte[] buf, ulong bufsiz);
3965 public static long readlink (string path, byte[] buf)
3967 return readlink (path, buf, (ulong) buf.LongLength);
3970 [DllImport (LIBC, SetLastError=true)]
3971 public static extern int unlink (
3972 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3975 [DllImport (LIBC, SetLastError=true)]
3976 public static extern int rmdir (
3977 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3981 // pid_t tcgetpgrp(int fd);
3982 [DllImport (LIBC, SetLastError=true)]
3983 public static extern int tcgetpgrp (int fd);
3986 // int tcsetpgrp(int fd, pid_t pgrp);
3987 [DllImport (LIBC, SetLastError=true)]
3988 public static extern int tcsetpgrp (int fd, int pgrp);
3990 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
3991 private static extern IntPtr sys_getlogin ();
3993 public static string getlogin ()
3995 lock (getlogin_lock) {
3996 IntPtr r = sys_getlogin ();
3997 return UnixMarshal.PtrToString (r);
4002 // int getlogin_r(char *buf, size_t bufsize);
4003 [DllImport (MPH, SetLastError=true,
4004 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
4005 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
4007 public static int getlogin_r (StringBuilder name)
4009 return getlogin_r (name, (ulong) name.Capacity);
4012 [DllImport (LIBC, SetLastError=true)]
4013 public static extern int setlogin (string name);
4016 // int gethostname(char *name, size_t len);
4017 [DllImport (MPH, SetLastError=true,
4018 EntryPoint="Mono_Posix_Syscall_gethostname")]
4019 public static extern int gethostname ([Out] StringBuilder name, ulong len);
4021 public static int gethostname (StringBuilder name)
4023 return gethostname (name, (ulong) name.Capacity);
4027 // int gethostname(const char *name, size_t len);
4028 [DllImport (MPH, SetLastError=true,
4029 EntryPoint="Mono_Posix_Syscall_sethostname")]
4030 public static extern int sethostname (string name, ulong len);
4032 public static int sethostname (string name)
4034 return sethostname (name, (ulong) name.Length);
4037 [DllImport (MPH, SetLastError=true,
4038 EntryPoint="Mono_Posix_Syscall_gethostid")]
4039 public static extern long gethostid ();
4041 [DllImport (MPH, SetLastError=true,
4042 EntryPoint="Mono_Posix_Syscall_sethostid")]
4043 public static extern int sethostid (long hostid);
4046 // int getdomainname(char *name, size_t len);
4047 [DllImport (MPH, SetLastError=true,
4048 EntryPoint="Mono_Posix_Syscall_getdomainname")]
4049 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
4051 public static int getdomainname (StringBuilder name)
4053 return getdomainname (name, (ulong) name.Capacity);
4057 // int setdomainname(const char *name, size_t len);
4058 [DllImport (MPH, SetLastError=true,
4059 EntryPoint="Mono_Posix_Syscall_setdomainname")]
4060 public static extern int setdomainname (string name, ulong len);
4062 public static int setdomainname (string name)
4064 return setdomainname (name, (ulong) name.Length);
4067 [DllImport (LIBC, SetLastError=true)]
4068 public static extern int vhangup ();
4070 // Revoke doesn't appear to be POSIX. Include it?
4071 [DllImport (LIBC, SetLastError=true)]
4072 public static extern int revoke (
4073 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4076 // TODO: profil? It's not POSIX.
4078 [DllImport (LIBC, SetLastError=true)]
4079 public static extern int acct (
4080 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4083 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
4084 private static extern IntPtr sys_getusershell ();
4086 internal static object usershell_lock = new object ();
4088 public static string getusershell ()
4090 lock (usershell_lock) {
4091 IntPtr r = sys_getusershell ();
4092 return UnixMarshal.PtrToString (r);
4096 [DllImport (MPH, SetLastError=true,
4097 EntryPoint="Mono_Posix_Syscall_setusershell")]
4098 private static extern int sys_setusershell ();
4100 public static int setusershell ()
4102 lock (usershell_lock) {
4103 return sys_setusershell ();
4107 [DllImport (MPH, SetLastError=true,
4108 EntryPoint="Mono_Posix_Syscall_endusershell")]
4109 private static extern int sys_endusershell ();
4111 public static int endusershell ()
4113 lock (usershell_lock) {
4114 return sys_endusershell ();
4119 [DllImport (LIBC, SetLastError=true)]
4120 private static extern int daemon (int nochdir, int noclose);
4122 // this implicitly forks, and thus isn't safe.
4123 private static int daemon (bool nochdir, bool noclose)
4125 return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
4129 [DllImport (LIBC, SetLastError=true)]
4130 public static extern int chroot (
4131 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4134 // skipping getpass(3) as the man page states:
4135 // This function is obsolete. Do not use it.
4137 [DllImport (LIBC, SetLastError=true)]
4138 public static extern int fsync (int fd);
4140 [DllImport (LIBC, SetLastError=true)]
4141 public static extern int fdatasync (int fd);
4143 [DllImport (MPH, SetLastError=true,
4144 EntryPoint="Mono_Posix_Syscall_sync")]
4145 public static extern int sync ();
4147 [DllImport (LIBC, SetLastError=true)]
4148 [Obsolete ("Dropped in POSIX 1003.1-2001. " +
4149 "Use Syscall.sysconf (SysconfName._SC_PAGESIZE).")]
4150 public static extern int getpagesize ();
4153 // int truncate(const char *path, off_t length);
4154 [DllImport (MPH, SetLastError=true,
4155 EntryPoint="Mono_Posix_Syscall_truncate")]
4156 public static extern int truncate (
4157 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4158 string path, long length);
4161 // int ftruncate(int fd, off_t length);
4162 [DllImport (MPH, SetLastError=true,
4163 EntryPoint="Mono_Posix_Syscall_ftruncate")]
4164 public static extern int ftruncate (int fd, long length);
4166 [DllImport (LIBC, SetLastError=true)]
4167 public static extern int getdtablesize ();
4169 [DllImport (LIBC, SetLastError=true)]
4170 public static extern int brk (IntPtr end_data_segment);
4172 [DllImport (LIBC, SetLastError=true)]
4173 public static extern IntPtr sbrk (IntPtr increment);
4175 // TODO: syscall(2)?
4176 // Probably safer to skip entirely.
4179 // int lockf(int fd, int cmd, off_t len);
4180 [DllImport (MPH, SetLastError=true,
4181 EntryPoint="Mono_Posix_Syscall_lockf")]
4182 public static extern int lockf (int fd, LockfCommand cmd, long len);
4184 [Obsolete ("This is insecure and should not be used", true)]
4185 public static string crypt (string key, string salt)
4187 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
4190 [Obsolete ("This is insecure and should not be used", true)]
4191 public static int encrypt (byte[] block, bool decode)
4193 throw new SecurityException ("crypt(3) has been broken. Use something more secure.");
4197 // void swab(const void *from, void *to, ssize_t n);
4198 [DllImport (MPH, SetLastError=true,
4199 EntryPoint="Mono_Posix_Syscall_swab")]
4200 public static extern int swab (IntPtr from, IntPtr to, long n);
4202 public static unsafe void swab (void* from, void* to, long n)
4204 swab ((IntPtr) from, (IntPtr) to, n);
4207 [DllImport (LIBC, SetLastError=true, EntryPoint="faccessat")]
4208 private static extern int sys_faccessat (int dirfd,
4209 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4210 string pathname, int mode, int flags);
4212 public static int faccessat (int dirfd, string pathname, AccessModes mode, AtFlags flags)
4214 int _mode = NativeConvert.FromAccessModes (mode);
4215 int _flags = NativeConvert.FromAtFlags (flags);
4216 return sys_faccessat (dirfd, pathname, _mode, _flags);
4220 // int fchownat(int dirfd, const char *path, uid_t owner, gid_t group, int flags);
4221 [DllImport (LIBC, SetLastError=true, EntryPoint="fchownat")]
4222 private static extern int sys_fchownat (int dirfd,
4223 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4224 string pathname, uint owner, uint group, int flags);
4226 public static int fchownat (int dirfd, string pathname, uint owner, uint group, AtFlags flags)
4228 int _flags = NativeConvert.FromAtFlags (flags);
4229 return sys_fchownat (dirfd, pathname, owner, group, _flags);
4232 [DllImport (LIBC, SetLastError=true, EntryPoint="linkat")]
4233 private static extern int sys_linkat (int olddirfd,
4234 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4235 string oldpath, int newdirfd,
4236 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4237 string newpath, int flags);
4239 public static int linkat (int olddirfd, string oldpath, int newdirfd, string newpath, AtFlags flags)
4241 int _flags = NativeConvert.FromAtFlags (flags);
4242 return sys_linkat (olddirfd, oldpath, newdirfd, newpath, _flags);
4246 // ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsize);
4247 public static int readlinkat (int dirfd, string pathname, [Out] StringBuilder buf, ulong bufsiz)
4249 return ReadlinkIntoStringBuilder (target => readlinkat (dirfd, pathname, target), buf, bufsiz);
4252 public static int readlinkat (int dirfd, string pathname, [Out] StringBuilder buf)
4254 return readlinkat (dirfd, pathname, buf, (ulong) buf.Capacity);
4257 [DllImport (MPH, SetLastError=true,
4258 EntryPoint="Mono_Posix_Syscall_readlinkat")]
4259 private static extern long readlinkat (int dirfd,
4260 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4261 string pathname, byte[] buf, ulong bufsiz);
4263 public static long readlinkat (int dirfd, string pathname, byte[] buf)
4265 return readlinkat (dirfd, pathname, buf, (ulong) buf.LongLength);
4268 [DllImport (LIBC, SetLastError=true)]
4269 public static extern int symlinkat (
4270 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4271 string oldpath, int dirfd,
4272 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4275 [DllImport (LIBC, SetLastError=true, EntryPoint="unlinkat")]
4276 private static extern int sys_unlinkat (int dirfd,
4277 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4278 string pathname, int flags);
4280 public static int unlinkat (int dirfd, string pathname, AtFlags flags)
4282 int _flags = NativeConvert.FromAtFlags (flags);
4283 return sys_unlinkat (dirfd, pathname, _flags);
4287 #region <utime.h> Declarations
4289 // <utime.h> -- COMPLETE
4292 [DllImport (MPH, SetLastError=true,
4293 EntryPoint="Mono_Posix_Syscall_utime")]
4294 private static extern int sys_utime (
4295 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4296 string filename, ref Utimbuf buf, int use_buf);
4298 public static int utime (string filename, ref Utimbuf buf)
4300 return sys_utime (filename, ref buf, 1);
4303 public static int utime (string filename)
4305 Utimbuf buf = new Utimbuf ();
4306 return sys_utime (filename, ref buf, 0);
4310 #region <sys/uio.h> Declarations
4312 // <sys/uio.h> -- COMPLETE
4316 // ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
4317 [DllImport (MPH, SetLastError=true,
4318 EntryPoint="Mono_Posix_Syscall_readv")]
4319 private static extern long sys_readv (int fd, Iovec[] iov, int iovcnt);
4321 public static long readv (int fd, Iovec[] iov)
4323 return sys_readv (fd, iov, iov.Length);
4327 // ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
4328 [DllImport (MPH, SetLastError=true,
4329 EntryPoint="Mono_Posix_Syscall_writev")]
4330 private static extern long sys_writev (int fd, Iovec[] iov, int iovcnt);
4332 public static long writev (int fd, Iovec[] iov)
4334 return sys_writev (fd, iov, iov.Length);
4338 // ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
4339 [DllImport (MPH, SetLastError=true,
4340 EntryPoint="Mono_Posix_Syscall_preadv")]
4341 private static extern long sys_preadv (int fd, Iovec[] iov, int iovcnt, long offset);
4343 public static long preadv (int fd, Iovec[] iov, long offset)
4345 return sys_preadv (fd, iov, iov.Length, offset);
4349 // ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
4350 [DllImport (MPH, SetLastError=true,
4351 EntryPoint="Mono_Posix_Syscall_pwritev")]
4352 private static extern long sys_pwritev (int fd, Iovec[] iov, int iovcnt, long offset);
4354 public static long pwritev (int fd, Iovec[] iov, long offset)
4356 return sys_pwritev (fd, iov, iov.Length, offset);