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 Jonathan Pryor
11 // This file implements the low-level syscall interface to the POSIX
14 // This file tries to stay close to the low-level API as much as possible
15 // using enumerations, structures and in a few cases, using existing .NET
18 // Implementation notes:
20 // Since the values for the various constants on the API changes
21 // from system to system (even Linux on different architectures will
22 // have different values), we define our own set of values, and we
23 // use a set of C helper routines to map from the constants we define
24 // to the values of the native OS.
26 // Bitfields are flagged with the [Map] attribute, and a helper program
27 // generates a set of routines that we can call to convert from our value
28 // definitions to the value definitions expected by the OS; see
29 // UnixConvert for the conversion routines.
31 // Methods that require tuning are bound as `private sys_NAME' methods
32 // and then a `NAME' method is exposed.
36 // Permission is hereby granted, free of charge, to any person obtaining
37 // a copy of this software and associated documentation files (the
38 // "Software"), to deal in the Software without restriction, including
39 // without limitation the rights to use, copy, modify, merge, publish,
40 // distribute, sublicense, and/or sell copies of the Software, and to
41 // permit persons to whom the Software is furnished to do so, subject to
42 // the following conditions:
44 // The above copyright notice and this permission notice shall be
45 // included in all copies or substantial portions of the Software.
47 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
51 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
52 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
53 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57 using System.Collections;
58 using System.Runtime.InteropServices;
62 [assembly:Mono.Unix.IncludeAttribute (
63 new string [] {"sys/types.h", "sys/stat.h", "sys/poll.h", "sys/wait.h",
64 "unistd.h", "fcntl.h", "signal.h", "poll.h", "grp.h", "errno.h"},
65 new string [] {"_GNU_SOURCE", "_XOPEN_SOURCE"})]
72 public enum Error : int {
73 // errors & their values liberally copied from
74 // FC2 /usr/include/asm/errno.h
76 EPERM = 1, // Operation not permitted
77 ENOENT = 2, // No such file or directory
78 ESRCH = 3, // No such process
79 EINTR = 4, // Interrupted system call
81 ENXIO = 6, // No such device or address
82 E2BIG = 7, // Arg list too long
83 ENOEXEC = 8, // Exec format error
84 EBADF = 9, // Bad file number
85 ECHILD = 10, // No child processes
86 EAGAIN = 11, // Try again
87 ENOMEM = 12, // Out of memory
88 EACCES = 13, // Permission denied
89 EFAULT = 14, // Bad address
90 ENOTBLK = 15, // Block device required
91 EBUSY = 16, // Device or resource busy
92 EEXIST = 17, // File exists
93 EXDEV = 18, // Cross-device link
94 ENODEV = 19, // No such device
95 ENOTDIR = 20, // Not a directory
96 EISDIR = 21, // Is a directory
97 EINVAL = 22, // Invalid argument
98 ENFILE = 23, // File table overflow
99 EMFILE = 24, // Too many open files
100 ENOTTY = 25, // Not a typewriter
101 ETXTBSY = 26, // Text file busy
102 EFBIG = 27, // File too large
103 ENOSPC = 28, // No space left on device
104 ESPIPE = 29, // Illegal seek
105 EROFS = 30, // Read-only file system
106 EMLINK = 31, // Too many links
107 EPIPE = 32, // Broken pipe
108 EDOM = 33, // Math argument out of domain of func
109 ERANGE = 34, // Math result not representable
110 EDEADLK = 35, // Resource deadlock would occur
111 ENAMETOOLONG = 36, // File name too long
112 ENOLCK = 37, // No record locks available
113 ENOSYS = 38, // Function not implemented
114 ENOTEMPTY = 39, // Directory not empty
115 ELOOP = 40, // Too many symbolic links encountered
116 EWOULDBLOCK = EAGAIN, // Operation would block
117 ENOMSG = 42, // No message of desired type
118 EIDRM = 43, // Identifier removed
119 ECHRNG = 44, // Channel number out of range
120 EL2NSYNC = 45, // Level 2 not synchronized
121 EL3HLT = 46, // Level 3 halted
122 EL3RST = 47, // Level 3 reset
123 ELNRNG = 48, // Link number out of range
124 EUNATCH = 49, // Protocol driver not attached
125 ENOCSI = 50, // No CSI structure available
126 EL2HLT = 51, // Level 2 halted
127 EBADE = 52, // Invalid exchange
128 EBADR = 53, // Invalid request descriptor
129 EXFULL = 54, // Exchange full
130 ENOANO = 55, // No anode
131 EBADRQC = 56, // Invalid request code
132 EBADSLT = 57, // Invalid slot
136 EBFONT = 59, // Bad font file format
137 ENOSTR = 60, // Device not a stream
138 ENODATA = 61, // No data available
139 ETIME = 62, // Timer expired
140 ENOSR = 63, // Out of streams resources
141 ENONET = 64, // Machine is not on the network
142 ENOPKG = 65, // Package not installed
143 EREMOTE = 66, // Object is remote
144 ENOLINK = 67, // Link has been severed
145 EADV = 68, // Advertise error
146 ESRMNT = 69, // Srmount error
147 ECOMM = 70, // Communication error on send
148 EPROTO = 71, // Protocol error
149 EMULTIHOP = 72, // Multihop attempted
150 EDOTDOT = 73, // RFS specific error
151 EBADMSG = 74, // Not a data message
152 EOVERFLOW = 75, // Value too large for defined data type
153 ENOTUNIQ = 76, // Name not unique on network
154 EBADFD = 77, // File descriptor in bad state
155 EREMCHG = 78, // Remote address changed
156 ELIBACC = 79, // Can not access a needed shared library
157 ELIBBAD = 80, // Accessing a corrupted shared library
158 ELIBSCN = 81, // .lib section in a.out corrupted
159 ELIBMAX = 82, // Attempting to link in too many shared libraries
160 ELIBEXEC = 83, // Cannot exec a shared library directly
161 EILSEQ = 84, // Illegal byte sequence
162 ERESTART = 85, // Interrupted system call should be restarted
163 ESTRPIPE = 86, // Streams pipe error
164 EUSERS = 87, // Too many users
165 ENOTSOCK = 88, // Socket operation on non-socket
166 EDESTADDRREQ = 89, // Destination address required
167 EMSGSIZE = 90, // Message too long
168 EPROTOTYPE = 91, // Protocol wrong type for socket
169 ENOPROTOOPT = 92, // Protocol not available
170 EPROTONOSUPPORT = 93, // Protocol not supported
171 ESOCKTNOSUPPORT = 94, // Socket type not supported
172 EOPNOTSUPP = 95, // Operation not supported on transport endpoint
173 EPFNOSUPPORT = 96, // Protocol family not supported
174 EAFNOSUPPORT = 97, // Address family not supported by protocol
175 EADDRINUSE = 98, // Address already in use
176 EADDRNOTAVAIL = 99, // Cannot assign requested address
177 ENETDOWN = 100, // Network is down
178 ENETUNREACH = 101, // Network is unreachable
179 ENETRESET = 102, // Network dropped connection because of reset
180 ECONNABORTED = 103, // Software caused connection abort
181 ECONNRESET = 104, // Connection reset by peer
182 ENOBUFS = 105, // No buffer space available
183 EISCONN = 106, // Transport endpoint is already connected
184 ENOTCONN = 107, // Transport endpoint is not connected
185 ESHUTDOWN = 108, // Cannot send after transport endpoint shutdown
186 ETOOMANYREFS = 109, // Too many references: cannot splice
187 ETIMEDOUT = 110, // Connection timed out
188 ECONNREFUSED = 111, // Connection refused
189 EHOSTDOWN = 112, // Host is down
190 EHOSTUNREACH = 113, // No route to host
191 EALREADY = 114, // Operation already in progress
192 EINPROGRESS = 115, // Operation now in progress
193 ESTALE = 116, // Stale NFS file handle
194 EUCLEAN = 117, // Structure needs cleaning
195 ENOTNAM = 118, // Not a XENIX named type file
196 ENAVAIL = 119, // No XENIX semaphores available
197 EISNAM = 120, // Is a named type file
198 EREMOTEIO = 121, // Remote I/O error
199 EDQUOT = 122, // Quota exceeded
201 ENOMEDIUM = 123, // No medium found
202 EMEDIUMTYPE = 124, // Wrong medium type
206 public enum SyslogOptions {
207 LOG_PID = 0x01, // log the pid with each message
208 LOG_CONS = 0x02, // log on the console if errors in sending
209 LOG_ODELAY = 0x04, // delay open until first syslog (default)
210 LOG_NDELAY = 0x08, // don't delay open
211 LOG_NOWAIT = 0x10, // don't wait for console forks; DEPRECATED
212 LOG_PERROR = 0x20 // log to stderr as well
216 public enum SyslogFacility {
227 LOG_AUTHPRIV = 10 << 3,
229 LOG_LOCAL0 = 16 << 3,
230 LOG_LOCAL1 = 17 << 3,
231 LOG_LOCAL2 = 18 << 3,
232 LOG_LOCAL3 = 19 << 3,
233 LOG_LOCAL4 = 20 << 3,
234 LOG_LOCAL5 = 21 << 3,
235 LOG_LOCAL6 = 22 << 3,
236 LOG_LOCAL7 = 23 << 3,
240 public enum SyslogLevel {
241 LOG_EMERG = 0, // system is unusable
242 LOG_ALERT = 1, // action must be taken immediately
243 LOG_CRIT = 2, // critical conditions
244 LOG_ERR = 3, // warning conditions
245 LOG_WARNING = 4, // warning conditions
246 LOG_NOTICE = 5, // normal but significant condition
247 LOG_INFO = 6, // informational
248 LOG_DEBUG = 7 // debug-level messages
252 public enum OpenFlags : int {
261 // Or-ed with zero or more of these
272 // These are non-Posix. Using them will result in errors/exceptions on
273 // non-supported platforms.
275 // (For example, "C-wrapped" system calls -- calls with implementation in
276 // MonoPosixHelper -- will return -1 with errno=EINVAL. C#-wrapped system
277 // calls will generate an exception in UnixConvert, as the value can't be
278 // converted on the target platform.)
290 public enum FilePermissions : uint {
291 S_ISUID = 0x0800, // Set user ID on execution
292 S_ISGID = 0x0400, // Set gorup ID on execution
293 S_ISVTX = 0x0200, // Save swapped text after use (sticky).
294 S_IRUSR = 0x0100, // Read by owner
295 S_IWUSR = 0x0080, // Write by owner
296 S_IXUSR = 0x0040, // Execute by owner
297 S_IRGRP = 0x0020, // Read by group
298 S_IWGRP = 0x0010, // Write by group
299 S_IXGRP = 0x0008, // Execute by group
300 S_IROTH = 0x0004, // Read by other
301 S_IWOTH = 0x0002, // Write by other
302 S_IXOTH = 0x0001, // Execute by other
304 S_IRWXG = (S_IRGRP | S_IWGRP | S_IXGRP),
305 S_IRWXU = (S_IRUSR | S_IWUSR | S_IXUSR),
306 S_IRWXO = (S_IROTH | S_IWOTH | S_IXOTH),
307 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
308 ALLPERMS = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
309 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
312 // Why these are held in "mode_t" is beyond me...
313 S_IFMT = 0xF000, // Bits which determine file type
314 S_IFDIR = 0x4000, // Directory
315 S_IFCHR = 0x2000, // Character device
316 S_IFBLK = 0x6000, // Block device
317 S_IFREG = 0x8000, // Regular file
318 S_IFIFO = 0x1000, // FIFO
319 S_IFLNK = 0xA000, // Symbolic link
320 S_IFSOCK = 0xC000, // Socket
324 public enum FcntlCommand : int {
325 // Form /usr/include/bits/fcntl.h
326 F_DUPFD = 0, // Duplicate file descriptor.
327 F_GETFD = 1, // Get file descriptor flags.
328 F_SETFD = 2, // Set file descriptor flags.
329 F_GETFL = 3, // Get file status flags.
330 F_SETFL = 4, // Set file status flags.
331 F_GETLK = 12, // Get record locking info. [64]
332 F_SETLK = 13, // Set record locking info (non-blocking). [64]
333 F_SETLKW = 14, // Set record locking info (blocking). [64]
334 F_SETOWN = 8, // Set owner of socket (receiver of SIGIO).
335 F_GETOWN = 9, // Get owner of socket (receiver of SIGIO).
336 F_SETSIG = 10, // Set number of signal to be sent.
337 F_GETSIG = 11, // Get number of signal to be sent.
338 F_SETLEASE = 1024, // Set a lease.
339 F_GETLEASE = 1025, // Enquire what lease is active.
340 F_NOTIFY = 1026, // Required notifications on a directory
344 public enum LockType : short {
345 F_RDLCK = 0, // Read lock.
346 F_WRLCK = 1, // Write lock.
347 F_UNLCK = 2, // Remove lock.
351 public enum SeekFlags : short {
352 // values liberally copied from /usr/include/unistd.h
353 SEEK_SET = 0, // Seek from beginning of file.
354 SEEK_CUR = 1, // Seek from current position.
355 SEEK_END = 2, // Seek from end of file.
357 L_SET = SEEK_SET, // BSD alias for SEEK_SET
358 L_INCR = SEEK_CUR, // BSD alias for SEEK_CUR
359 L_XTND = SEEK_END, // BSD alias for SEEK_END
363 public enum DirectoryNotifyFlags : int {
364 // from /usr/include/bits/fcntl.h
365 DN_ACCESS = 0x00000001, // File accessed.
366 DN_MODIFY = 0x00000002, // File modified.
367 DN_CREATE = 0x00000004, // File created.
368 DN_DELETE = 0x00000008, // File removed.
369 DN_RENAME = 0x00000010, // File renamed.
370 DN_ATTRIB = 0x00000020, // File changed attributes.
371 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
375 public enum PosixFadviseAdvice : int {
376 POSIX_FADV_NORMAL = 0, // No further special treatment.
377 POSIX_FADV_RANDOM = 1, // Expect random page references.
378 POSIX_FADV_SEQUENTIAL = 2, // Expect sequential page references.
379 POSIX_FADV_WILLNEED = 3, // Will need these pages.
380 POSIX_FADV_DONTNEED = 4, // Don't need these pages.
381 POSIX_FADV_NOREUSE = 5, // Data will be accessed once.
385 public enum PosixMadviseAdvice : int {
386 POSIX_MADV_NORMAL = 0, // No further special treatment.
387 POSIX_MADV_RANDOM = 1, // Expect random page references.
388 POSIX_MADV_SEQUENTIAL = 2, // Expect sequential page references.
389 POSIX_MADV_WILLNEED = 3, // Will need these pages.
390 POSIX_MADV_DONTNEED = 4, // Don't need these pages.
394 public enum Signum : int {
395 SIGHUP = 1, // Hangup (POSIX).
396 SIGINT = 2, // Interrupt (ANSI).
397 SIGQUIT = 3, // Quit (POSIX).
398 SIGILL = 4, // Illegal instruction (ANSI).
399 SIGTRAP = 5, // Trace trap (POSIX).
400 SIGABRT = 6, // Abort (ANSI).
401 SIGIOT = 6, // IOT trap (4.2 BSD).
402 SIGBUS = 7, // BUS error (4.2 BSD).
403 SIGFPE = 8, // Floating-point exception (ANSI).
404 SIGKILL = 9, // Kill, unblockable (POSIX).
405 SIGUSR1 = 10, // User-defined signal 1 (POSIX).
406 SIGSEGV = 11, // Segmentation violation (ANSI).
407 SIGUSR2 = 12, // User-defined signal 2 (POSIX).
408 SIGPIPE = 13, // Broken pipe (POSIX).
409 SIGALRM = 14, // Alarm clock (POSIX).
410 SIGTERM = 15, // Termination (ANSI).
411 SIGSTKFLT = 16, // Stack fault.
412 SIGCLD = SIGCHLD, // Same as SIGCHLD (System V).
413 SIGCHLD = 17, // Child status has changed (POSIX).
414 SIGCONT = 18, // Continue (POSIX).
415 SIGSTOP = 19, // Stop, unblockable (POSIX).
416 SIGTSTP = 20, // Keyboard stop (POSIX).
417 SIGTTIN = 21, // Background read from tty (POSIX).
418 SIGTTOU = 22, // Background write to tty (POSIX).
419 SIGURG = 23, // Urgent condition on socket (4.2 BSD).
420 SIGXCPU = 24, // CPU limit exceeded (4.2 BSD).
421 SIGXFSZ = 25, // File size limit exceeded (4.2 BSD).
422 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
423 SIGPROF = 27, // Profiling alarm clock (4.2 BSD).
424 SIGWINCH = 28, // Window size change (4.3 BSD, Sun).
425 SIGPOLL = SIGIO, // Pollable event occurred (System V).
426 SIGIO = 29, // I/O now possible (4.2 BSD).
427 SIGPWR = 30, // Power failure restart (System V).
428 SIGSYS = 31, // Bad system call.
433 public enum WaitOptions : int {
434 WNOHANG = 1, // Don't block waiting
435 WUNTRACED = 2, // Report status of stopped children
439 public enum AccessMode : int {
447 public enum PathConf : int {
454 _PC_CHOWN_RESTRICTED,
462 _PC_REC_INCR_XFER_SIZE,
463 _PC_REC_MAX_XFER_SIZE,
464 _PC_REC_MIN_XFER_SIZE,
472 public enum SysConf : int {
482 _SC_REALTIME_SIGNALS,
483 _SC_PRIORITY_SCHEDULING,
492 _SC_MEMORY_PROTECTION,
495 _SC_SHARED_MEMORY_OBJECTS,
498 _SC_AIO_PRIO_DELTA_MAX,
509 /* Values for the argument to `sysconf'
510 corresponding to _POSIX2_* symbols. */
515 _SC_COLL_WEIGHTS_MAX,
520 _SC_CHARCLASS_NAME_MAX,
536 _SC_IOV_MAX = _SC_UIO_MAXIOV,
537 _SC_PII_INTERNET_STREAM,
538 _SC_PII_INTERNET_DGRAM,
543 /* Values according to POSIX 1003.1c (POSIX threads). */
545 _SC_THREAD_SAFE_FUNCTIONS,
546 _SC_GETGR_R_SIZE_MAX,
547 _SC_GETPW_R_SIZE_MAX,
550 _SC_THREAD_DESTRUCTOR_ITERATIONS,
552 _SC_THREAD_STACK_MIN,
553 _SC_THREAD_THREADS_MAX,
554 _SC_THREAD_ATTR_STACKADDR,
555 _SC_THREAD_ATTR_STACKSIZE,
556 _SC_THREAD_PRIORITY_SCHEDULING,
557 _SC_THREAD_PRIO_INHERIT,
558 _SC_THREAD_PRIO_PROTECT,
559 _SC_THREAD_PROCESS_SHARED,
560 _SC_NPROCESSORS_CONF,
561 _SC_NPROCESSORS_ONLN,
567 _SC_XOPEN_XCU_VERSION,
602 _SC_XBS5_ILP32_OFF32,
603 _SC_XBS5_ILP32_OFFBIG,
605 _SC_XBS5_LPBIG_OFFBIG,
608 _SC_XOPEN_REALTIME_THREADS,
613 _SC_C_LANG_SUPPORT_R,
619 _SC_DEVICE_SPECIFIC_R,
630 _SC_READER_WRITER_LOCKS,
638 _SC_THREAD_SPORADIC_SERVER,
640 _SC_SYSTEM_DATABASE_R,
642 _SC_TYPED_MEMORY_OBJECTS,
646 _SC_2_PBS_ACCOUNTING,
652 _SC_2_PBS_CHECKPOINT,
659 _SC_TRACE_EVENT_FILTER,
662 _SC_LEVEL1_ICACHE_SIZE,
663 _SC_LEVEL1_ICACHE_ASSOC,
664 _SC_LEVEL1_ICACHE_LINESIZE,
665 _SC_LEVEL1_DCACHE_SIZE,
666 _SC_LEVEL1_DCACHE_ASSOC,
667 _SC_LEVEL1_DCACHE_LINESIZE,
668 _SC_LEVEL2_CACHE_SIZE,
669 _SC_LEVEL2_CACHE_ASSOC,
670 _SC_LEVEL2_CACHE_LINESIZE,
671 _SC_LEVEL3_CACHE_SIZE,
672 _SC_LEVEL3_CACHE_ASSOC,
673 _SC_LEVEL3_CACHE_LINESIZE,
674 _SC_LEVEL4_CACHE_SIZE,
675 _SC_LEVEL4_CACHE_ASSOC,
676 _SC_LEVEL4_CACHE_LINESIZE
680 public enum ConfStr : int {
681 _CS_PATH, /* The default search path. */
682 _CS_V6_WIDTH_RESTRICTED_ENVS,
683 _CS_GNU_LIBC_VERSION,
684 _CS_GNU_LIBPTHREAD_VERSION,
685 _CS_LFS_CFLAGS = 1000,
693 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
694 _CS_XBS5_ILP32_OFF32_LDFLAGS,
695 _CS_XBS5_ILP32_OFF32_LIBS,
696 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
697 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
698 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
699 _CS_XBS5_ILP32_OFFBIG_LIBS,
700 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
701 _CS_XBS5_LP64_OFF64_CFLAGS,
702 _CS_XBS5_LP64_OFF64_LDFLAGS,
703 _CS_XBS5_LP64_OFF64_LIBS,
704 _CS_XBS5_LP64_OFF64_LINTFLAGS,
705 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
706 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
707 _CS_XBS5_LPBIG_OFFBIG_LIBS,
708 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
709 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
710 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
711 _CS_POSIX_V6_ILP32_OFF32_LIBS,
712 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
713 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
714 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
715 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
716 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
717 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
718 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
719 _CS_POSIX_V6_LP64_OFF64_LIBS,
720 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
721 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
722 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
723 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
724 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
728 public enum LockFlags : int {
729 F_ULOCK = 0, // Unlock a previously locked region.
730 F_LOCK = 1, // Lock a region for exclusive use.
731 F_TLOCK = 2, // Test and lock a region for exclusive use.
732 F_TEST = 3, // Test a region for other process locks.
736 public enum PollEvents : short {
737 POLLIN = 0x0001, // There is data to read
738 POLLPRI = 0x0002, // There is urgent data to read
739 POLLOUT = 0x0004, // Writing now will not block
740 POLLERR = 0x0008, // Error condition
741 POLLHUP = 0x0010, // Hung up
742 POLLNVAL = 0x0020, // Invalid request; fd not open
743 // XPG4.2 definitions (via _XOPEN_SOURCE)
744 POLLRDNORM = 0x0040, // Normal data bay be read
745 POLLRDBAND = 0x0080, // Priority data may be read
746 POLLWRNORM = 0x0100, // Writing now will not block
747 POLLWRBAND = 0x0200, // Priority data may be written
754 public struct Flock {
755 public LockType l_type; // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
756 public SeekFlags l_whence; // How to interpret l_start
757 public /* off_t */ long l_start; // Starting offset for lock
758 public /* off_t */ long l_len; // Number of bytes to lock
759 public /* pid_t */ int l_pid; // PID of process blocking our lock (F_GETLK only)
762 [StructLayout(LayoutKind.Sequential)]
763 public struct Pollfd {
765 public PollEvents events;
766 public PollEvents revents;
770 public /* dev_t */ ulong st_dev; // device
771 public /* ino_t */ ulong st_ino; // inode
772 public FilePermissions st_mode; // protection
773 private uint _padding_; // padding for structure alignment
774 public /* nlink_t */ ulong st_nlink; // number of hard links
775 public /* uid_t */ uint st_uid; // user ID of owner
776 public /* gid_t */ uint st_gid; // group ID of owner
777 public /* dev_t */ ulong st_rdev; // device type (if inode device)
778 public /* off_t */ long st_size; // total size, in bytes
779 public /* blksize_t */ long st_blksize; // blocksize for filesystem I/O
780 public /* blkcnt_t */ long st_blocks; // number of blocks allocated
781 public /* time_t */ long st_atime; // time of last access
782 public /* time_t */ long st_mtime; // time of last modification
783 public /* time_t */ long st_ctime; // time of last status change
786 public struct Statvfs {
787 public ulong f_bsize; // file system block size
788 public ulong f_frsize; // fragment size
789 public /* fsblkcnt_t */ ulong f_blocks; // size of fs in f_frsize units
790 public /* fsblkcnt_t */ ulong f_bfree; // # free blocks
791 public /* fsblkcnt_t */ ulong f_bavail; // # free blocks for non-root
792 public /* fsfilcnt_t */ ulong f_files; // # inodes
793 public /* fsfilcnt_t */ ulong f_ffree; // # free inodes
794 public /* fsfilcnt_t */ ulong f_favail; // # free inodes for non-root
795 public ulong f_fsid; // file system id
796 public ulong f_flag; // mount flags
797 public ulong f_namemax; // maximum filename length
800 public struct Timeval {
801 public /* time_t */ long tv_sec; // seconds
802 public /* suseconds_t */ long tv_usec; // microseconds
805 public struct Timezone {
806 public int tz_minuteswest; // minutes W of Greenwich
807 private int tz_dsttime; // type of dst correction (OBSOLETE)
810 public struct Utimbuf {
811 public /* time_t */ long actime; // access time
812 public /* time_t */ long modtime; // modification time
819 public sealed class Dirent
821 public /* ino_t */ ulong d_ino;
822 public /* off_t */ long d_off;
823 public ushort d_reclen;
825 public string d_name;
827 public override int GetHashCode ()
829 return d_ino.GetHashCode () ^ d_off.GetHashCode () ^
830 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
831 d_name.GetHashCode ();
834 public override bool Equals (object obj)
836 if (obj == null || GetType() != obj.GetType())
838 Dirent d = (Dirent) obj;
839 return d.d_ino == d_ino && d.d_off == d_off &&
840 d.d_reclen == d_reclen && d.d_type == d_type &&
844 public override string ToString ()
849 public static bool operator== (Dirent lhs, Dirent rhs)
851 return Object.Equals (lhs, rhs);
854 public static bool operator!= (Dirent lhs, Dirent rhs)
856 return !Object.Equals (lhs, rhs);
860 public sealed class Fstab
862 public string fs_spec;
863 public string fs_file;
864 public string fs_vfstype;
865 public string fs_mntops;
866 public string fs_type;
868 public int fs_passno;
870 public override int GetHashCode ()
872 return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
873 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
874 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
877 public override bool Equals (object obj)
879 if (obj == null || GetType() != obj.GetType())
881 Fstab f = (Fstab) obj;
882 return f.fs_spec == fs_spec && f.fs_file == fs_file &&
883 f.fs_vfstype == fs_vfstype && f.fs_mntops == fs_mntops &&
884 f.fs_type == fs_type && f.fs_freq == fs_freq &&
885 f.fs_passno == fs_passno;
888 public override string ToString ()
893 public static bool operator== (Fstab lhs, Fstab rhs)
895 return Object.Equals (lhs, rhs);
898 public static bool operator!= (Fstab lhs, Fstab rhs)
900 return !Object.Equals (lhs, rhs);
904 public sealed class Group
906 public string gr_name;
907 public string gr_passwd;
908 public /* gid_t */ uint gr_gid;
909 public string[] gr_mem;
911 public override int GetHashCode ()
914 for (int i = 0; i < gr_mem.Length; ++i)
915 memhc ^= gr_mem[i].GetHashCode ();
917 return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^
918 gr_gid.GetHashCode () ^ memhc;
921 public override bool Equals (object obj)
923 if (obj == null || GetType() != obj.GetType())
925 Group g = (Group) obj;
926 if (g.gr_gid != gr_gid)
928 if (g.gr_gid == gr_gid && g.gr_name == gr_name &&
929 g.gr_passwd == gr_passwd) {
930 if (g.gr_mem == gr_mem)
932 if (g.gr_mem == null || gr_mem == null)
934 if (g.gr_mem.Length != gr_mem.Length)
936 for (int i = 0; i < gr_mem.Length; ++i)
937 if (gr_mem[i] != g.gr_mem[i])
944 // Generate string in /etc/group format
945 public override string ToString ()
947 StringBuilder sb = new StringBuilder ();
948 sb.AppendFormat ("{0}:{1}:{2}:", gr_name, gr_passwd, gr_gid);
949 GetMembers (sb, gr_mem);
950 return sb.ToString ();
953 private static void GetMembers (StringBuilder sb, string[] members)
955 if (members.Length > 0)
956 sb.Append (members[0]);
957 for (int i = 1; i < members.Length; ++i) {
959 sb.Append (members[i]);
963 public static bool operator== (Group lhs, Group rhs)
965 return Object.Equals (lhs, rhs);
968 public static bool operator!= (Group lhs, Group rhs)
970 return !Object.Equals (lhs, rhs);
974 public sealed class Passwd
976 public string pw_name;
977 public string pw_passwd;
978 public /* uid_t */ uint pw_uid;
979 public /* gid_t */ uint pw_gid;
980 public string pw_gecos;
981 public string pw_dir;
982 public string pw_shell;
984 public override int GetHashCode ()
986 return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^
987 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
988 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
989 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
992 public override bool Equals (object obj)
994 if (obj == null || GetType() != obj.GetType())
996 Passwd p = (Passwd) obj;
997 return p.pw_uid == pw_uid && p.pw_gid == pw_gid && p.pw_name == pw_name &&
998 p.pw_passwd == pw_passwd && p.pw_gecos == pw_gecos &&
999 p.pw_dir == pw_dir && p.pw_shell == pw_shell;
1002 // Generate string in /etc/passwd format
1003 public override string ToString ()
1005 return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
1006 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
1009 public static bool operator== (Passwd lhs, Passwd rhs)
1011 return Object.Equals (lhs, rhs);
1014 public static bool operator!= (Passwd lhs, Passwd rhs)
1016 return !Object.Equals (lhs, rhs);
1021 // Convention: Functions *not* part of the standard C library AND part of
1022 // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
1024 // For example, the man page should be similar to:
1026 // CONFORMING TO (or CONFORMS TO)
1027 // XPG2, SUSv2, POSIX, etc.
1029 // BSD- and GNU-specific exports can also be placed here.
1031 // The only methods in here should be:
1032 // (1) low-level functions
1033 // (2) "Trivial" function overloads. For example, if the parameters to a
1034 // function are related (e.g. getgroups(2))
1035 // (3) The return type SHOULD NOT be changed. If you want to provide a
1036 // convenience function with a nicer return type, place it into one of
1037 // the Unix* wrapper classes, and give it a .NET-styled name.
1038 // (4) Exceptions SHOULD NOT be thrown. EXCEPTIONS:
1039 // - If you're wrapping *broken* methods which make assumptions about
1040 // input data, such as that an argument refers to N bytes of data.
1041 // This is currently limited to cuserid(3) and encrypt(3).
1042 // - If you call functions which themselves generate exceptions.
1043 // This is the case for using UnixConvert, which will throw an
1044 // exception if an invalid/unsupported value is used.
1046 public sealed class Syscall : Stdlib
1048 private const string LIBC = "libc";
1049 private const string MPH = "MonoPosixHelper";
1050 private const string CRYPT = "crypt";
1052 private Syscall () {}
1058 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3),
1059 // aio_return(3), aio_suspend(3), aio_write(3)
1061 // Then update UnixStream.BeginRead to use the aio* functions.
1063 #region <dirent.h> Declarations
1067 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1069 [DllImport (LIBC, SetLastError=true)]
1070 public static extern IntPtr opendir (string name);
1072 [DllImport (LIBC, SetLastError=true)]
1073 public static extern int closedir (IntPtr dir);
1076 // void seekdir (DIR *dir, off_t offset);
1077 // Slight modification. Returns -1 on error, 0 on success.
1078 [DllImport (MPH, SetLastError=true,
1079 EntryPoint="Mono_Posix_Syscall_seekdir")]
1080 public static extern int seekdir (IntPtr dir, long offset);
1083 // off_t telldir(DIR *dir);
1084 [DllImport (MPH, SetLastError=true,
1085 EntryPoint="Mono_Posix_Syscall_telldir")]
1086 public static extern long telldir (IntPtr dir);
1088 [DllImport (LIBC, SetLastError=true)]
1089 public static extern void rewinddir (IntPtr dir);
1091 private struct _Dirent {
1092 public /* ino_t */ ulong d_ino;
1093 public /* off_t */ long d_off;
1094 public ushort d_reclen;
1096 public IntPtr d_name;
1099 private static void CopyDirent (Dirent to, ref _Dirent from)
1102 to.d_ino = from.d_ino;
1103 to.d_off = from.d_off;
1104 to.d_reclen = from.d_reclen;
1105 to.d_type = from.d_type;
1106 to.d_name = UnixMarshal.PtrToString (from.d_name);
1109 Stdlib.free (from.d_name);
1110 from.d_name = IntPtr.Zero;
1114 [DllImport (MPH, SetLastError=true,
1115 EntryPoint="Mono_Posix_Syscall_readdir")]
1116 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
1118 public static Dirent readdir (IntPtr dir)
1121 int r = sys_readdir (dir, out dentry);
1124 Dirent d = new Dirent ();
1125 CopyDirent (d, ref dentry);
1129 [DllImport (MPH, SetLastError=true,
1130 EntryPoint="Mono_Posix_Syscall_readdir_r")]
1131 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
1133 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
1139 entry.d_name = null;
1142 int r = sys_readdir_r (dirp, out _d, out result);
1144 if (r == 0 && result != IntPtr.Zero) {
1145 CopyDirent (entry, ref _d);
1151 [DllImport (LIBC, SetLastError=true)]
1152 public static extern int dirfd (IntPtr dir);
1155 #region <errno.h> Declarations
1157 // <errno.h> -- COMPLETE
1160 public static Error GetLastError ()
1162 int errno = Marshal.GetLastWin32Error ();
1163 return UnixConvert.ToError (errno);
1166 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_SetLastError")]
1167 private static extern void SetLastError (int error);
1169 public static void SetLastError (Error error)
1171 int _error = UnixConvert.FromError (error);
1172 SetLastError (_error);
1176 #region <fcntl.h> Declarations
1178 // <fcntl.h> -- COMPLETE
1181 [DllImport (MPH, SetLastError=true,
1182 EntryPoint="Mono_Posix_Syscall_fcntl")]
1183 public static extern int fcntl (int fd, FcntlCommand cmd);
1185 [DllImport (MPH, SetLastError=true,
1186 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
1187 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
1189 [DllImport (MPH, SetLastError=true,
1190 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
1191 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
1193 [DllImport (MPH, SetLastError=true,
1194 EntryPoint="Mono_Posix_Syscall_open")]
1195 public static extern int open (string pathname, OpenFlags flags);
1198 // int open(const char *pathname, int flags, mode_t mode);
1199 [DllImport (MPH, SetLastError=true,
1200 EntryPoint="Mono_Posix_Syscall_open_mode")]
1201 public static extern int open (string pathname, OpenFlags flags, FilePermissions mode);
1204 // int creat(const char *pathname, mode_t mode);
1205 [DllImport (MPH, SetLastError=true,
1206 EntryPoint="Mono_Posix_Syscall_creat")]
1207 public static extern int creat (string pathname, FilePermissions mode);
1210 // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
1211 [DllImport (MPH, SetLastError=true,
1212 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
1213 public static extern int posix_fadvise (int fd, long offset,
1214 long len, PosixFadviseAdvice advice);
1216 // posix_fallocate(P)
1217 // int posix_fallocate(int fd, off_t offset, size_t len);
1218 [DllImport (MPH, SetLastError=true,
1219 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
1220 public static extern int posix_fallocate (int fd, long offset, long len);
1223 #region <fstab.h> Declarations
1225 // <fstab.h> -- COMPLETE
1227 private struct _Fstab {
1228 public IntPtr fs_spec;
1229 public IntPtr fs_file;
1230 public IntPtr fs_vfstype;
1231 public IntPtr fs_mntops;
1232 public IntPtr fs_type;
1234 public int fs_passno;
1235 public IntPtr _fs_buf_;
1238 private static void CopyFstab (Fstab to, ref _Fstab from)
1241 to.fs_spec = UnixMarshal.PtrToString (from.fs_spec);
1242 to.fs_file = UnixMarshal.PtrToString (from.fs_file);
1243 to.fs_vfstype = UnixMarshal.PtrToString (from.fs_vfstype);
1244 to.fs_mntops = UnixMarshal.PtrToString (from.fs_mntops);
1245 to.fs_type = UnixMarshal.PtrToString (from.fs_type);
1246 to.fs_freq = from.fs_freq;
1247 to.fs_passno = from.fs_passno;
1250 Stdlib.free (from._fs_buf_);
1251 from._fs_buf_ = IntPtr.Zero;
1255 [DllImport (LIBC, SetLastError=true)]
1256 public static extern void endfsent ();
1258 [DllImport (MPH, SetLastError=true,
1259 EntryPoint="Mono_Posix_Syscall_getfsent")]
1260 private static extern int sys_getfsent (out _Fstab fs);
1262 public static Fstab getfsent ()
1265 int r = sys_getfsent (out fsbuf);
1268 Fstab fs = new Fstab ();
1269 CopyFstab (fs, ref fsbuf);
1273 [DllImport (MPH, SetLastError=true,
1274 EntryPoint="Mono_Posix_Syscall_getfsfile")]
1275 private static extern int sys_getfsfile (string mount_point, out _Fstab fs);
1277 public static Fstab getfsfile (string mount_point)
1280 int r = sys_getfsfile (mount_point, out fsbuf);
1283 Fstab fs = new Fstab ();
1284 CopyFstab (fs, ref fsbuf);
1288 [DllImport (MPH, SetLastError=true,
1289 EntryPoint="Mono_Posix_Syscall_getfsspec")]
1290 private static extern int sys_getfsspec (string special_file, out _Fstab fs);
1292 public static Fstab getfsspec (string special_file)
1295 int r = sys_getfsspec (special_file, out fsbuf);
1298 Fstab fs = new Fstab ();
1299 CopyFstab (fs, ref fsbuf);
1303 [DllImport (LIBC, SetLastError=true)]
1304 public static extern int setfsent ();
1308 #region <grp.h> Declarations
1312 // TODO: putgrent(3), fgetgrent_r(), getgrouplist(2), initgroups(3)
1315 // int setgroups (size_t size, const gid_t *list);
1316 [DllImport (MPH, SetLastError=true,
1317 EntryPoint="Mono_Posix_Syscall_setgroups")]
1318 public static extern int setgroups (ulong size, uint[] list);
1320 public static int setgroups (uint [] list)
1322 return setgroups ((ulong) list.Length, list);
1325 private struct _Group
1327 public IntPtr gr_name;
1328 public IntPtr gr_passwd;
1329 public /* gid_t */ uint gr_gid;
1330 public int _gr_nmem_;
1331 public IntPtr gr_mem;
1332 public IntPtr _gr_buf_;
1335 private static void CopyGroup (Group to, ref _Group from)
1338 to.gr_gid = from.gr_gid;
1339 to.gr_name = UnixMarshal.PtrToString (from.gr_name);
1340 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
1341 to.gr_mem = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
1344 Stdlib.free (from.gr_mem);
1345 Stdlib.free (from._gr_buf_);
1346 from.gr_mem = IntPtr.Zero;
1347 from._gr_buf_ = IntPtr.Zero;
1351 [DllImport (MPH, SetLastError=true,
1352 EntryPoint="Mono_Posix_Syscall_getgrnam")]
1353 private static extern int sys_getgrnam (string name, out _Group group);
1355 public static Group getgrnam (string name)
1358 int r = sys_getgrnam (name, out group);
1361 Group gr = new Group ();
1362 CopyGroup (gr, ref group);
1367 // struct group *getgrgid(gid_t gid);
1368 [DllImport (MPH, SetLastError=true,
1369 EntryPoint="Mono_Posix_Syscall_getgrgid")]
1370 private static extern int sys_getgrgid (uint uid, out _Group group);
1372 public static Group getgrgid (uint uid)
1375 int r = sys_getgrgid (uid, out group);
1378 Group gr = new Group ();
1379 CopyGroup (gr, ref group);
1383 [DllImport (MPH, SetLastError=true,
1384 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
1385 private static extern int sys_getgrnam_r (string name, out _Group grbuf, out IntPtr grbufp);
1387 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
1392 int r = sys_getgrnam_r (name, out group, out _grbufp);
1393 if (r == 0 && _grbufp != IntPtr.Zero) {
1394 CopyGroup (grbuf, ref group);
1401 // int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
1402 // size_t buflen, struct group **gbufp);
1403 [DllImport (MPH, SetLastError=true,
1404 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
1405 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
1407 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
1412 int r = sys_getgrgid_r (uid, out group, out _grbufp);
1413 if (r == 0 && _grbufp != IntPtr.Zero) {
1414 CopyGroup (grbuf, ref group);
1420 [DllImport (MPH, SetLastError=true,
1421 EntryPoint="Mono_Posix_Syscall_getgrent")]
1422 private static extern int sys_getgrent (out _Group grbuf);
1424 public static Group getgrent ()
1427 int r = sys_getgrent (out group);
1430 Group gr = new Group();
1431 CopyGroup (gr, ref group);
1435 [DllImport (LIBC, SetLastError=true)]
1436 public static extern void setgrent ();
1438 [DllImport (LIBC, SetLastError=true)]
1439 public static extern void endgrent ();
1441 [DllImport (MPH, SetLastError=true,
1442 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
1443 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
1445 public static Group fgetgrent (IntPtr stream)
1448 int r = sys_fgetgrent (stream, out group);
1451 Group gr = new Group ();
1452 CopyGroup (gr, ref group);
1457 #region <pwd.h> Declarations
1461 // TODO: putpwent(3), fgetpwent_r()
1463 // SKIPPING: getpw(3): it's dangerous. Use getpwuid(3) instead.
1465 private struct _Passwd
1467 public IntPtr pw_name;
1468 public IntPtr pw_passwd;
1469 public /* uid_t */ uint pw_uid;
1470 public /* gid_t */ uint pw_gid;
1471 public IntPtr pw_gecos;
1472 public IntPtr pw_dir;
1473 public IntPtr pw_shell;
1474 public IntPtr _pw_buf_;
1477 private static void CopyPasswd (Passwd to, ref _Passwd from)
1480 to.pw_name = UnixMarshal.PtrToString (from.pw_name);
1481 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
1482 to.pw_uid = from.pw_uid;
1483 to.pw_gid = from.pw_gid;
1484 to.pw_gecos = UnixMarshal.PtrToString (from.pw_gecos);
1485 to.pw_dir = UnixMarshal.PtrToString (from.pw_dir);
1486 to.pw_shell = UnixMarshal.PtrToString (from.pw_shell);
1489 Stdlib.free (from._pw_buf_);
1490 from._pw_buf_ = IntPtr.Zero;
1494 [DllImport (MPH, SetLastError=true,
1495 EntryPoint="Mono_Posix_Syscall_getpwnam")]
1496 private static extern int sys_getpwnam (string name, out _Passwd passwd);
1498 public static Passwd getpwnam (string name)
1501 int r = sys_getpwnam (name, out passwd);
1504 Passwd pw = new Passwd ();
1505 CopyPasswd (pw, ref passwd);
1510 // struct passwd *getpwnuid(uid_t uid);
1511 [DllImport (MPH, SetLastError=true,
1512 EntryPoint="Mono_Posix_Syscall_getpwuid")]
1513 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
1515 public static Passwd getpwuid (uint uid)
1518 int r = sys_getpwuid (uid, out passwd);
1521 Passwd pw = new Passwd ();
1522 CopyPasswd (pw, ref passwd);
1526 [DllImport (MPH, SetLastError=true,
1527 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
1528 private static extern int sys_getpwnam_r (string name, out _Passwd pwbuf, out IntPtr pwbufp);
1530 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
1535 int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
1536 if (r == 0 && _pwbufp != IntPtr.Zero) {
1537 CopyPasswd (pwbuf, ref passwd);
1544 // int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
1545 // buflen, struct passwd **pwbufp);
1546 [DllImport (MPH, SetLastError=true,
1547 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
1548 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
1550 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
1555 int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
1556 if (r == 0 && _pwbufp != IntPtr.Zero) {
1557 CopyPasswd (pwbuf, ref passwd);
1563 [DllImport (MPH, SetLastError=true,
1564 EntryPoint="Mono_Posix_Syscall_getpwent")]
1565 private static extern int sys_getpwent (out _Passwd pwbuf);
1567 public static Passwd getpwent ()
1570 int r = sys_getpwent (out passwd);
1573 Passwd pw = new Passwd ();
1574 CopyPasswd (pw, ref passwd);
1578 [DllImport (LIBC, SetLastError=true)]
1579 public static extern void setpwent ();
1581 [DllImport (LIBC, SetLastError=true)]
1582 public static extern void endpwent ();
1584 [DllImport (MPH, SetLastError=true,
1585 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
1586 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
1588 public static Passwd fgetpwent (IntPtr stream)
1591 int r = sys_fgetpwent (stream, out passwd);
1594 Passwd pw = new Passwd ();
1595 CopyPasswd (pw, ref passwd);
1600 #region <signal.h> Declarations
1604 [DllImport (LIBC, SetLastError=true)]
1605 private static extern void psignal (int sig, string s);
1607 public static void psignal (Signum sig, string s)
1609 int signum = UnixConvert.FromSignum (sig);
1610 psignal (signum, s);
1614 // int kill(pid_t pid, int sig);
1615 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
1616 private static extern int sys_kill (int pid, int sig);
1618 public static int kill (int pid, Signum sig)
1620 int _sig = UnixConvert.FromSignum (sig);
1621 return sys_kill (pid, _sig);
1624 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
1625 private static extern IntPtr sys_strsignal (int sig);
1627 public static IntPtr sys_strsignal (Signum sig)
1629 int s = UnixConvert.FromSignum (sig);
1630 return sys_strsignal (s);
1633 public static string strsignal (Signum sig)
1635 IntPtr r = sys_strsignal (sig);
1636 return UnixMarshal.PtrToString (r);
1639 // TODO: sigaction(2)
1640 // TODO: sigsuspend(2)
1641 // TODO: sigpending(2)
1645 #region <stdio.h> Declarations
1649 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
1650 private static extern int _L_ctermid ();
1652 public static readonly int L_ctermid = _L_ctermid ();
1654 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
1655 private static extern int _L_cuserid ();
1657 public static readonly int L_cuserid = _L_cuserid ();
1659 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
1660 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
1662 [Obsolete ("\"Nobody knows precisely what cuserid() does... " +
1663 "DO NOT USE cuserid().\n" +
1664 "`string' must hold L_cuserid characters. Use getlogin_r instead.")]
1665 public static string cuserid (StringBuilder @string)
1667 if (@string.Capacity < L_cuserid) {
1668 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
1670 IntPtr r = sys_cuserid (@string);
1671 return UnixMarshal.PtrToString (r);
1676 #region <stdlib.h> Declarations
1680 [DllImport (LIBC, SetLastError=true)]
1681 public static extern int ttyslot ();
1683 [DllImport (CRYPT, SetLastError=true)]
1684 public static extern void setkey (string key);
1688 #region <string.h> Declarations
1694 // int strerror_r(int errnum, char *buf, size_t n);
1695 [DllImport (MPH, SetLastError=true,
1696 EntryPoint="Mono_Posix_Syscall_strerror_r")]
1697 private static extern int sys_strerror_r (int errnum,
1698 [Out] StringBuilder buf, ulong n);
1700 public static int strerror_r (Error errnum, StringBuilder buf, ulong n)
1702 int e = UnixConvert.FromError (errnum);
1703 return sys_strerror_r (e, buf, n);
1706 public static int strerror_r (Error errnum, StringBuilder buf)
1708 return strerror_r (errnum, buf, (ulong) buf.Capacity);
1713 #region <sys/mman.h> Declarations
1719 // int posix_madvise(void *addr, size_t len, int advice);
1720 [DllImport (MPH, SetLastError=true,
1721 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
1722 public static extern int posix_madvise (IntPtr addr, ulong len,
1723 PosixMadviseAdvice advice);
1727 #region <sys/poll.h> Declarations
1729 // <sys/poll.h> -- COMPLETE
1732 private struct _pollfd {
1734 public short events;
1735 public short revents;
1738 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
1739 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
1741 public static int poll (Pollfd [] fds, uint nfds, int timeout)
1743 if (fds.Length < nfds)
1744 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
1746 _pollfd[] send = new _pollfd[nfds];
1748 for (int i = 0; i < send.Length; i++) {
1749 send [i].fd = fds [i].fd;
1750 send [i].events = UnixConvert.FromPollEvents (fds [i].events);
1753 int r = sys_poll (send, nfds, timeout);
1755 for (int i = 0; i < send.Length; i++) {
1756 fds [i].revents = UnixConvert.ToPollEvents (send [i].revents);
1762 public static int poll (Pollfd [] fds, int timeout)
1764 return poll (fds, (uint) fds.Length, timeout);
1777 // TODO: setrlimit(2)
1778 // TODO: getrlimit(2)
1779 // TODO: getrusage(2)
1783 #region <sys/sendfile.h> Declarations
1785 // <sys/sendfile.h> -- COMPLETE
1788 [DllImport (MPH, SetLastError=true,
1789 EntryPoint="Mono_Posix_Syscall_sendfile")]
1790 public static extern long sendfile (int out_fd, int in_fd,
1791 ref long offset, ulong count);
1795 #region <sys/stat.h> Declarations
1797 // <sys/stat.h> -- COMPLETE
1799 [DllImport (MPH, SetLastError=true,
1800 EntryPoint="Mono_Posix_Syscall_stat")]
1801 public static extern int stat (string file_name, out Stat buf);
1803 [DllImport (MPH, SetLastError=true,
1804 EntryPoint="Mono_Posix_Syscall_fstat")]
1805 public static extern int fstat (int filedes, out Stat buf);
1807 [DllImport (MPH, SetLastError=true,
1808 EntryPoint="Mono_Posix_Syscall_lstat")]
1809 public static extern int lstat (string file_name, out Stat buf);
1812 // int chmod(const char *path, mode_t mode);
1813 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
1814 private static extern int sys_chmod (string path, uint mode);
1816 public static int chmod (string path, FilePermissions mode)
1818 uint _mode = UnixConvert.FromFilePermissions (mode);
1819 return sys_chmod (path, _mode);
1823 // int chmod(int filedes, mode_t mode);
1824 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
1825 private static extern int sys_fchmod (int filedes, uint mode);
1827 public static int fchmod (int filedes, FilePermissions mode)
1829 uint _mode = UnixConvert.FromFilePermissions (mode);
1830 return sys_fchmod (filedes, _mode);
1834 // mode_t umask(mode_t mask);
1835 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
1836 private static extern uint sys_umask (uint mask);
1838 public static FilePermissions umask (FilePermissions mask)
1840 uint _mask = UnixConvert.FromFilePermissions (mask);
1841 uint r = sys_umask (_mask);
1842 return UnixConvert.ToFilePermissions (r);
1846 // int mkdir(const char *pathname, mode_t mode);
1847 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
1848 public static extern int sys_mkdir (string oldpath, uint mode);
1850 public static int mkdir (string oldpath, FilePermissions mode)
1852 uint _mode = UnixConvert.FromFilePermissions (mode);
1853 return sys_mkdir (oldpath, _mode);
1857 // int mknod (const char *pathname, mode_t mode, dev_t dev);
1858 [DllImport (MPH, SetLastError=true,
1859 EntryPoint="Mono_Posix_Syscall_mknod")]
1860 public static extern int mknod (string pathname, FilePermissions mode, ulong dev);
1863 // int mkfifo(const char *pathname, mode_t mode);
1864 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
1865 private static extern int sys_mkfifo (string pathname, uint mode);
1867 public static int mkfifo (string pathname, FilePermissions mode)
1869 uint _mode = UnixConvert.FromFilePermissions (mode);
1870 return sys_mkfifo (pathname, _mode);
1875 #region <sys/stat.h> Declarations
1880 [DllImport (MPH, SetLastError=true,
1881 EntryPoint="Mono_Posix_Syscall_statvfs")]
1882 public static extern int statvfs (string path, out Statvfs buf);
1884 [DllImport (MPH, SetLastError=true,
1885 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
1886 public static extern int fstatvfs (int fd, out Statvfs buf);
1890 #region <sys/time.h> Declarations
1894 // TODO: adjtime(), getitimer(2), setitimer(2), lutimes(), futimes()
1896 [DllImport (MPH, SetLastError=true,
1897 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1898 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
1900 [DllImport (MPH, SetLastError=true,
1901 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1902 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
1904 public static int gettimeofday (out Timeval tv)
1906 return gettimeofday (out tv, IntPtr.Zero);
1909 [DllImport (MPH, SetLastError=true,
1910 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1911 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
1913 public static int gettimeofday (out Timezone tz)
1915 return gettimeofday (IntPtr.Zero, out tz);
1918 [DllImport (MPH, SetLastError=true,
1919 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1920 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
1922 [DllImport (MPH, SetLastError=true,
1923 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1924 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
1926 public static int settimeofday (ref Timeval tv)
1928 return settimeofday (ref tv, IntPtr.Zero);
1931 [DllImport (MPH, SetLastError=true,
1932 EntryPoint="Mono_Posix_Syscall_utimes")]
1933 public static extern int utimes (string filename, ref Timeval tvp);
1949 #region <sys/wait.h> Declarations
1955 // pid_t wait(int *status);
1956 [DllImport (LIBC, SetLastError=true)]
1957 public static extern int wait (out int status);
1960 // pid_t waitpid(pid_t pid, int *status, int options);
1961 [DllImport (LIBC, SetLastError=true)]
1962 private static extern int waitpid (int pid, out int status, int options);
1964 public static int waitpid (int pid, out int status, WaitOptions options)
1966 int _options = UnixConvert.FromWaitOptions (options);
1967 return waitpid (pid, out status, _options);
1970 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
1971 private static extern int _WIFEXITED (int status);
1973 public static bool WIFEXITED (int status)
1975 return _WIFEXITED (status) != 0;
1978 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
1979 public static extern int WEXITSTATUS (int status);
1981 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
1982 private static extern int _WIFSIGNALED (int status);
1984 public static bool WIFSIGNALED (int status)
1986 return _WIFSIGNALED (status) != 0;
1989 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
1990 private static extern int _WTERMSIG (int status);
1992 public static Signum WTERMSIG (int status)
1994 int r = _WTERMSIG (status);
1995 return UnixConvert.ToSignum (r);
1998 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
1999 private static extern int _WIFSTOPPED (int status);
2001 public static bool WIFSTOPPED (int status)
2003 return _WIFSTOPPED (status) != 0;
2006 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
2007 private static extern int _WSTOPSIG (int status);
2009 public static Signum WSTOPSIG (int status)
2011 int r = _WSTOPSIG (status);
2012 return UnixConvert.ToSignum (r);
2021 #region <syslog.h> Declarations
2026 [DllImport (LIBC, EntryPoint="openlog")]
2027 private static extern void sys_openlog (IntPtr ident, int option, int facility);
2029 public static void openlog (IntPtr ident, SyslogOptions option,
2030 SyslogFacility defaultFacility)
2032 int _option = UnixConvert.FromSyslogOptions (option);
2033 int _facility = UnixConvert.FromSyslogFacility (defaultFacility);
2035 sys_openlog (ident, _option, _facility);
2039 private static extern void sys_syslog (int priority, string message);
2041 public static void syslog (SyslogFacility facility, SyslogLevel level, string message)
2043 int _facility = UnixConvert.FromSyslogFacility (facility);
2044 int _level = UnixConvert.FromSyslogLevel (level);
2045 sys_syslog (_facility | _level, GetSyslogMessage (message));
2048 public static void syslog (SyslogLevel level, string message)
2050 int _level = UnixConvert.FromSyslogLevel (level);
2051 sys_syslog (_level, GetSyslogMessage (message));
2054 private static string GetSyslogMessage (string message)
2056 return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
2059 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2060 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
2061 public static void syslog (SyslogFacility facility, SyslogLevel level,
2062 string format, params object[] parameters)
2064 int _facility = UnixConvert.FromSyslogFacility (facility);
2065 int _level = UnixConvert.FromSyslogLevel (level);
2067 object[] _parameters = new object[checked(parameters.Length+2)];
2068 _parameters [0] = _facility | _level;
2069 _parameters [1] = format;
2070 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2071 XPrintfFunctions.syslog (_parameters);
2074 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2075 "Use syslog(SyslogLevel, string) instead.")]
2076 public static void syslog (SyslogLevel level, string format,
2077 params object[] parameters)
2079 int _level = UnixConvert.FromSyslogLevel (level);
2081 object[] _parameters = new object[checked(parameters.Length+2)];
2082 _parameters [0] = _level;
2083 _parameters [1] = format;
2084 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2085 XPrintfFunctions.syslog (_parameters);
2089 public static extern void closelog ();
2091 [DllImport (LIBC, EntryPoint="setlogmask")]
2092 private static extern int sys_setlogmask (int mask);
2094 public static int setlogmask (SyslogLevel mask)
2096 int _mask = UnixConvert.FromSyslogLevel (mask);
2097 return sys_setlogmask (_mask);
2102 #region <time.h> Declarations
2109 // int stime(time_t *t);
2110 [DllImport (MPH, SetLastError=true,
2111 EntryPoint="Mono_Posix_Syscall_stime")]
2112 public static extern int stime (ref long t);
2115 // time_t time(time_t *t);
2116 [DllImport (MPH, SetLastError=true,
2117 EntryPoint="Mono_Posix_Syscall_time")]
2118 public static extern long time (out long t);
2128 #region <unistd.h> Declarations
2132 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
2133 // other TODOs listed below.
2135 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
2136 private static extern int sys_access (string pathname, int mode);
2138 public static int access (string pathname, AccessMode mode)
2140 int _mode = UnixConvert.FromAccessMode (mode);
2141 return sys_access (pathname, _mode);
2145 // off_t lseek(int filedes, off_t offset, int whence);
2146 [DllImport (MPH, SetLastError=true,
2147 EntryPoint="Mono_Posix_Syscall_lseek")]
2148 public static extern long lseek (int fd, long offset, SeekFlags whence);
2150 [DllImport (LIBC, SetLastError=true)]
2151 public static extern int close (int fd);
2154 // ssize_t read(int fd, void *buf, size_t count);
2155 [DllImport (MPH, SetLastError=true,
2156 EntryPoint="Mono_Posix_Syscall_read")]
2157 public static extern long read (int fd, IntPtr buf, ulong count);
2159 public static unsafe long read (int fd, void *buf, ulong count)
2161 return read (fd, (IntPtr) buf, count);
2165 // ssize_t write(int fd, const void *buf, size_t count);
2166 [DllImport (MPH, SetLastError=true,
2167 EntryPoint="Mono_Posix_Syscall_write")]
2168 public static extern long write (int fd, IntPtr buf, ulong count);
2170 public static unsafe long write (int fd, void *buf, ulong count)
2172 return write (fd, (IntPtr) buf, count);
2176 // ssize_t pread(int fd, void *buf, size_t count, off_t offset);
2177 [DllImport (MPH, SetLastError=true,
2178 EntryPoint="Mono_Posix_Syscall_pread")]
2179 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
2181 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
2183 return pread (fd, (IntPtr) buf, count, offset);
2187 // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
2188 [DllImport (MPH, SetLastError=true,
2189 EntryPoint="Mono_Posix_Syscall_pwrite")]
2190 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
2192 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
2194 return pwrite (fd, (IntPtr) buf, count, offset);
2197 [DllImport (MPH, SetLastError=true,
2198 EntryPoint="Mono_Posix_Syscall_pipe")]
2199 public static extern int pipe (out int reading, out int writing);
2201 public static int pipe (int[] filedes)
2203 if (filedes == null || filedes.Length != 2) {
2207 int reading, writing;
2208 int r = pipe (out reading, out writing);
2209 filedes[0] = reading;
2210 filedes[1] = writing;
2214 [DllImport (LIBC, SetLastError=true)]
2215 public static extern uint alarm (uint seconds);
2217 [DllImport (LIBC, SetLastError=true)]
2218 public static extern uint sleep (uint seconds);
2220 [DllImport (LIBC, SetLastError=true)]
2221 public static extern uint ualarm (uint usecs, uint interval);
2223 [DllImport (LIBC, SetLastError=true)]
2224 public static extern int pause ();
2227 // int chown(const char *path, uid_t owner, gid_t group);
2228 [DllImport (LIBC, SetLastError=true)]
2229 public static extern int chown (string path, uint owner, uint group);
2232 // int fchown(int fd, uid_t owner, gid_t group);
2233 [DllImport (LIBC, SetLastError=true)]
2234 public static extern int fchown (int fd, uint owner, uint group);
2237 // int lchown(const char *path, uid_t owner, gid_t group);
2238 [DllImport (LIBC, SetLastError=true)]
2239 public static extern int lchown (string path, uint owner, uint group);
2241 [DllImport (LIBC, SetLastError=true)]
2242 public static extern int chdir (string path);
2244 [DllImport (LIBC, SetLastError=true)]
2245 public static extern int fchdir (int fd);
2248 // char *getcwd(char *buf, size_t size);
2249 [DllImport (MPH, SetLastError=true,
2250 EntryPoint="Mono_Posix_Syscall_getcwd")]
2251 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
2253 public static StringBuilder getcwd (StringBuilder buf)
2255 getcwd (buf, (ulong) buf.Capacity);
2259 // getwd(2) is deprecated; don't expose it.
2261 [DllImport (LIBC, SetLastError=true)]
2262 public static extern int dup (int fd);
2264 [DllImport (LIBC, SetLastError=true)]
2265 public static extern int dup2 (int fd, int fd2);
2267 // TODO: does Mono marshal arrays properly?
2268 [DllImport (LIBC, SetLastError=true)]
2269 public static extern int execve (string path, string[] argv, string[] envp);
2271 [DllImport (LIBC, SetLastError=true)]
2272 public static extern int fexecve (int fd, string[] argv, string[] envp);
2274 [DllImport (LIBC, SetLastError=true)]
2275 public static extern int execv (string path, string[] argv);
2277 // TODO: execle, execl, execlp
2278 [DllImport (LIBC, SetLastError=true)]
2279 public static extern int execvp (string path, string[] argv);
2281 [DllImport (LIBC, SetLastError=true)]
2282 public static extern int nice (int inc);
2284 [DllImport (LIBC, SetLastError=true)]
2285 public static extern int _exit (int status);
2287 [DllImport (MPH, SetLastError=true,
2288 EntryPoint="Mono_Posix_Syscall_fpathconf")]
2289 public static extern long fpathconf (int filedes, PathConf name);
2291 [DllImport (MPH, SetLastError=true,
2292 EntryPoint="Mono_Posix_Syscall_pathconf")]
2293 public static extern long pathconf (string path, PathConf name);
2295 [DllImport (MPH, SetLastError=true,
2296 EntryPoint="Mono_Posix_Syscall_sysconf")]
2297 public static extern long sysconf (SysConf name);
2300 // size_t confstr(int name, char *buf, size_t len);
2301 [DllImport (MPH, SetLastError=true,
2302 EntryPoint="Mono_Posix_Syscall_confstr")]
2303 public static extern ulong confstr (ConfStr name, [Out] StringBuilder buf, ulong len);
2306 // pid_t getpid(void);
2307 [DllImport (LIBC, SetLastError=true)]
2308 public static extern int getpid ();
2311 // pid_t getppid(void);
2312 [DllImport (LIBC, SetLastError=true)]
2313 public static extern int getppid ();
2316 // int setpgid(pid_t pid, pid_t pgid);
2317 [DllImport (LIBC, SetLastError=true)]
2318 public static extern int setpgid (int pid, int pgid);
2321 // pid_t getpgid(pid_t pid);
2322 [DllImport (LIBC, SetLastError=true)]
2323 public static extern int getpgid (int pid);
2325 [DllImport (LIBC, SetLastError=true)]
2326 public static extern int setpgrp ();
2329 // pid_t getpgrp(void);
2330 [DllImport (LIBC, SetLastError=true)]
2331 public static extern int getpgrp ();
2334 // pid_t setsid(void);
2335 [DllImport (LIBC, SetLastError=true)]
2336 public static extern int setsid ();
2339 // pid_t getsid(pid_t pid);
2340 [DllImport (LIBC, SetLastError=true)]
2341 public static extern int getsid (int pid);
2344 // uid_t getuid(void);
2345 [DllImport (LIBC, SetLastError=true)]
2346 public static extern uint getuid ();
2349 // uid_t geteuid(void);
2350 [DllImport (LIBC, SetLastError=true)]
2351 public static extern uint geteuid ();
2354 // gid_t getgid(void);
2355 [DllImport (LIBC, SetLastError=true)]
2356 public static extern uint getgid ();
2359 // gid_t getgid(void);
2360 [DllImport (LIBC, SetLastError=true)]
2361 public static extern uint getegid ();
2364 // int getgroups(int size, gid_t list[]);
2365 [DllImport (LIBC, SetLastError=true)]
2366 public static extern int getgroups (int size, uint[] list);
2368 public static int getgroups (uint[] list)
2370 return getgroups (list.Length, list);
2374 // int setuid(uid_t uid);
2375 [DllImport (LIBC, SetLastError=true)]
2376 public static extern int setuid (uint uid);
2379 // int setreuid(uid_t ruid, uid_t euid);
2380 [DllImport (LIBC, SetLastError=true)]
2381 public static extern int setreuid (uint ruid, uint euid);
2384 // int setregid(gid_t ruid, gid_t euid);
2385 [DllImport (LIBC, SetLastError=true)]
2386 public static extern int setregid (uint rgid, uint egid);
2389 // int seteuid(uid_t euid);
2390 [DllImport (LIBC, SetLastError=true)]
2391 public static extern int seteuid (uint euid);
2394 // int setegid(gid_t euid);
2395 [DllImport (LIBC, SetLastError=true)]
2396 public static extern int setegid (uint uid);
2399 // int setgid(gid_t gid);
2400 [DllImport (LIBC, SetLastError=true)]
2401 public static extern int setgid (uint gid);
2404 // int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
2405 [DllImport (LIBC, SetLastError=true)]
2406 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
2409 // int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
2410 [DllImport (LIBC, SetLastError=true)]
2411 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
2414 // int setresuid(uid_t ruid, uid_t euid, uid_t suid);
2415 [DllImport (LIBC, SetLastError=true)]
2416 public static extern int setresuid (uint ruid, uint euid, uint suid);
2419 // int setresgid(gid_t ruid, gid_t euid, gid_t suid);
2420 [DllImport (LIBC, SetLastError=true)]
2421 public static extern int setresgid (uint rgid, uint egid, uint sgid);
2424 // pid_t fork(void);
2425 [DllImport (LIBC, SetLastError=true)]
2426 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " +
2427 "shutdown code.\nUse System.Diagnostics.Process instead")]
2428 public static extern int fork ();
2431 // pid_t vfork(void);
2432 [DllImport (LIBC, SetLastError=true)]
2433 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " +
2434 "shutdown code.\nUse System.Diagnostics.Process instead")]
2435 public static extern int vfork ();
2437 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
2438 [Obsolete ("Not re-entrant. Use ttyname_r instead.")]
2439 public static extern IntPtr sys_ttyname (int fd);
2441 [Obsolete ("Not re-entrant. Use ttyname_r instead.")]
2442 public static string ttyname (int fd)
2444 IntPtr r = sys_ttyname (fd);
2445 return UnixMarshal.PtrToString (r);
2449 // int ttyname_r(int fd, char *buf, size_t buflen);
2450 [DllImport (MPH, SetLastError=true,
2451 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
2452 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
2454 public static int ttyname_r (int fd, StringBuilder buf)
2456 return ttyname_r (fd, buf, (ulong) buf.Capacity);
2459 [DllImport (LIBC, EntryPoint="isatty")]
2460 private static extern int sys_isatty (int fd);
2462 public static bool isatty (int fd)
2464 return sys_isatty (fd) == 1;
2467 [DllImport (LIBC, SetLastError=true)]
2468 public static extern int link (string oldpath, string newpath);
2470 [DllImport (LIBC, SetLastError=true)]
2471 public static extern int symlink (string oldpath, string newpath);
2474 // int readlink(const char *path, char *buf, size_t bufsize);
2475 [DllImport (MPH, SetLastError=true,
2476 EntryPoint="Mono_Posix_Syscall_readlink")]
2477 public static extern int readlink (string path, [Out] StringBuilder buf, ulong bufsiz);
2479 public static int readlink (string path, [Out] StringBuilder buf)
2481 return readlink (path, buf, (ulong) buf.Capacity);
2484 [DllImport (LIBC, SetLastError=true)]
2485 public static extern int unlink (string pathname);
2487 [DllImport (LIBC, SetLastError=true)]
2488 public static extern int rmdir (string pathname);
2491 // pid_t tcgetpgrp(int fd);
2492 [DllImport (LIBC, SetLastError=true)]
2493 public static extern int tcgetpgrp (int fd);
2496 // int tcsetpgrp(int fd, pid_t pgrp);
2497 [DllImport (LIBC, SetLastError=true)]
2498 public static extern int tcsetpgrp (int fd, int pgrp);
2500 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
2501 [Obsolete ("Not re-entrant. Use getlogin_r instead.")]
2502 public static extern IntPtr sys_getlogin ();
2504 [Obsolete ("Not re-entrant. Use getlogin_r instead.")]
2505 public static string getlogin ()
2507 IntPtr r = sys_getlogin ();
2508 return UnixMarshal.PtrToString (r);
2512 // int getlogin_r(char *buf, size_t bufsize);
2513 [DllImport (MPH, SetLastError=true,
2514 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
2515 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
2517 public static int getlogin_r (StringBuilder name)
2519 return getlogin_r (name, (ulong) name.Capacity);
2522 [DllImport (LIBC, SetLastError=true)]
2523 public static extern int setlogin (string name);
2526 // int gethostname(char *name, size_t len);
2527 [DllImport (MPH, SetLastError=true,
2528 EntryPoint="Mono_Posix_Syscall_gethostname")]
2529 public static extern int gethostname ([Out] StringBuilder name, ulong len);
2531 public static int gethostname (StringBuilder name)
2533 return gethostname (name, (ulong) name.Capacity);
2537 // int gethostname(const char *name, size_t len);
2538 [DllImport (MPH, SetLastError=true,
2539 EntryPoint="Mono_Posix_Syscall_gethostname")]
2540 public static extern int sethostname (string name, ulong len);
2542 public static int sethostname (string name)
2544 return sethostname (name, (ulong) name.Length);
2547 [DllImport (MPH, SetLastError=true,
2548 EntryPoint="Mono_Posix_Syscall_gethostid")]
2549 public static extern long gethostid ();
2551 [DllImport (MPH, SetLastError=true,
2552 EntryPoint="Mono_Posix_Syscall_sethostid")]
2553 public static extern int sethostid (long hostid);
2556 // int getdomainname(char *name, size_t len);
2557 [DllImport (MPH, SetLastError=true,
2558 EntryPoint="Mono_Posix_Syscall_getdomainname")]
2559 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
2561 public static int getdomainname (StringBuilder name)
2563 return getdomainname (name, (ulong) name.Capacity);
2567 // int setdomainname(const char *name, size_t len);
2568 [DllImport (MPH, SetLastError=true,
2569 EntryPoint="Mono_Posix_Syscall_setdomainname")]
2570 public static extern int setdomainname (string name, ulong len);
2572 public static int setdomainname (string name)
2574 return setdomainname (name, (ulong) name.Length);
2577 [DllImport (LIBC, SetLastError=true)]
2578 public static extern int vhangup ();
2580 // Revoke doesn't appear to be POSIX. Include it?
2581 [DllImport (LIBC, SetLastError=true)]
2582 public static extern int revoke (string file);
2584 // TODO: profil? It's not POSIX.
2586 [DllImport (LIBC, SetLastError=true)]
2587 public static extern int acct (string filename);
2589 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
2590 public static extern IntPtr sys_getusershell ();
2592 public static string getusershell ()
2594 IntPtr r = sys_getusershell ();
2595 return UnixMarshal.PtrToString (r);
2598 [DllImport (LIBC, SetLastError=true)]
2599 public static extern void setusershell ();
2601 [DllImport (LIBC, SetLastError=true)]
2602 public static extern void endusershell ();
2604 [DllImport (LIBC, SetLastError=true)]
2605 private static extern int daemon (int nochdir, int noclose);
2607 public static int daemon (bool nochdir, bool noclose)
2609 return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
2612 [DllImport (LIBC, SetLastError=true)]
2613 public static extern int chroot (string path);
2615 // skipping getpass(3) as the man page states:
2616 // This function is obsolete. Do not use it.
2618 [DllImport (LIBC, SetLastError=true)]
2619 public static extern int fsync (int fd);
2621 [DllImport (LIBC, SetLastError=true)]
2622 public static extern int fdatasync (int fd);
2624 [DllImport (LIBC, SetLastError=true)]
2625 public static extern void sync ();
2627 [DllImport (LIBC, SetLastError=true)]
2628 [Obsolete ("Dropped in POSIX 1003.1-2001. " +
2629 "Use Unistd.sysconf (SysConf._SC_PAGESIZE).")]
2630 public static extern int getpagesize ();
2633 // int truncate(const char *path, off_t length);
2634 [DllImport (MPH, SetLastError=true,
2635 EntryPoint="Mono_Posix_Syscall_truncate")]
2636 public static extern int truncate (string path, long length);
2639 // int ftruncate(int fd, off_t length);
2640 [DllImport (MPH, SetLastError=true,
2641 EntryPoint="Mono_Posix_Syscall_ftruncate")]
2642 public static extern int ftruncate (int fd, long length);
2644 [DllImport (LIBC, SetLastError=true)]
2645 public static extern int getdtablesize ();
2647 [DllImport (LIBC, SetLastError=true)]
2648 public static extern int brk (IntPtr end_data_segment);
2650 [DllImport (LIBC, SetLastError=true)]
2651 public static extern IntPtr sbrk (IntPtr increment);
2653 // TODO: syscall(2)?
2654 // Probably safer to skip entirely.
2657 // int lockf(int fd, int cmd, off_t len);
2658 [DllImport (MPH, SetLastError=true,
2659 EntryPoint="Mono_Posix_Syscall_lockf")]
2660 public static extern int lockf (int fd, LockFlags cmd, long len);
2662 [DllImport (CRYPT, SetLastError=true, EntryPoint="crypt")]
2663 public static extern IntPtr sys_crypt (string key, string salt);
2665 public static string crypt (string key, string salt)
2667 IntPtr r = sys_crypt (key, salt);
2668 return UnixMarshal.PtrToString (r);
2671 [DllImport (CRYPT, SetLastError=true, EntryPoint="encrypt")]
2672 private static extern void sys_encrypt ([In, Out] byte[] block, int edflag);
2674 public static void encrypt (byte[] block, bool decode)
2676 if (block.Length < 64)
2677 throw new ArgumentOutOfRangeException ("block", "Must refer to at least 64 bytes");
2678 sys_encrypt (block, decode ? 1 : 0);
2682 // void swab(const void *from, void *to, ssize_t n);
2683 [DllImport (MPH, SetLastError=true,
2684 EntryPoint="Mono_Posix_Syscall_swab")]
2685 public static extern void swab (IntPtr from, IntPtr to, long n);
2687 public static unsafe void swab (void* from, void* to, long n)
2689 swab ((IntPtr) from, (IntPtr) to, n);
2694 #region <utime.h> Declarations
2696 // <utime.h> -- COMPLETE
2699 [DllImport (MPH, SetLastError=true,
2700 EntryPoint="Mono_Posix_Syscall_utime")]
2701 public static extern int utime (string filename, ref Utimbuf buf);
2703 [DllImport (MPH, SetLastError=true,
2704 EntryPoint="Mono_Posix_Syscall_utime")]
2705 private static extern int utime (string filename, IntPtr buf);
2707 public static int utime (string filename)
2709 return utime (filename, IntPtr.Zero);