[Mono.Posix] Add support for OSX F_NOCACHE
[mono.git] / mcs / class / Mono.Posix / Mono.Unix.Native / Syscall.cs
1 //
2 // Mono.Unix/Syscall.cs
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@novell.com)
6 //   Jonathan Pryor (jonpryor@vt.edu)
7 //
8 // (C) 2003 Novell, Inc.
9 // (C) 2004-2006 Jonathan Pryor
10 //
11 // This file implements the low-level syscall interface to the POSIX
12 // subsystem.
13 //
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
16 // data types.
17 //
18 // Implementation notes:
19 //
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.
25 //
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.
30 //
31 //    Methods that require tuning are bound as `private sys_NAME' methods
32 //    and then a `NAME' method is exposed.
33 //
34
35 //
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:
43 // 
44 // The above copyright notice and this permission notice shall be
45 // included in all copies or substantial portions of the Software.
46 // 
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.
54 //
55
56 using System;
57 using System.Collections;
58 using System.Collections.Generic;
59 using System.Runtime.InteropServices;
60 using System.Security;
61 using System.Text;
62 using Mono.Unix.Native;
63
64 namespace Mono.Unix.Native {
65
66         #region Enumerations
67
68         [Flags][Map]
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
77         }
78
79         [Map]
80         [CLSCompliant (false)]
81         public enum SyslogFacility {
82                 LOG_KERN      = 0 << 3,
83                 LOG_USER      = 1 << 3,
84                 LOG_MAIL      = 2 << 3,
85                 LOG_DAEMON    = 3 << 3,
86                 LOG_AUTH      = 4 << 3,
87                 LOG_SYSLOG    = 5 << 3,
88                 LOG_LPR       = 6 << 3,
89                 LOG_NEWS      = 7 << 3,
90                 LOG_UUCP      = 8 << 3,
91                 LOG_CRON      = 9 << 3,
92                 LOG_AUTHPRIV  = 10 << 3,
93                 LOG_FTP       = 11 << 3,
94                 LOG_LOCAL0    = 16 << 3,
95                 LOG_LOCAL1    = 17 << 3,
96                 LOG_LOCAL2    = 18 << 3,
97                 LOG_LOCAL3    = 19 << 3,
98                 LOG_LOCAL4    = 20 << 3,
99                 LOG_LOCAL5    = 21 << 3,
100                 LOG_LOCAL6    = 22 << 3,
101                 LOG_LOCAL7    = 23 << 3,
102         }
103
104         [Map]
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
115         }
116
117         [Map][Flags]
118         [CLSCompliant (false)]
119         public enum OpenFlags : int {
120                 //
121                 // One of these
122                 //
123                 O_RDONLY    = 0x00000000,
124                 O_WRONLY    = 0x00000001,
125                 O_RDWR      = 0x00000002,
126
127                 //
128                 // Or-ed with zero or more of these
129                 //
130                 O_CREAT     = 0x00000040,
131                 O_EXCL      = 0x00000080,
132                 O_NOCTTY    = 0x00000100,
133                 O_TRUNC     = 0x00000200,
134                 O_APPEND    = 0x00000400,
135                 O_NONBLOCK  = 0x00000800,
136                 O_SYNC      = 0x00001000,
137
138                 //
139                 // These are non-Posix.  Using them will result in errors/exceptions on
140                 // non-supported platforms.
141                 //
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.)
146                 //
147                 
148                 O_NOFOLLOW  = 0x00020000,
149                 O_DIRECTORY = 0x00010000,
150                 O_DIRECT    = 0x00004000,
151                 O_ASYNC     = 0x00002000,
152                 O_LARGEFILE = 0x00008000,
153                 O_CLOEXEC   = 0x00080000,
154                 O_PATH      = 0x00200000
155         }
156         
157         [Map][Flags]
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
165         }
166         
167         // mode_t
168         [Flags][Map]
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
183
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
190
191                 // Device types
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
208         }
209
210         [Map]
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
230         }
231
232         [Map]
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.
238         }
239
240         [Map]
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.
247
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
251         }
252         
253         [Map, Flags]
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
264         }
265
266         [Map]
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.
275         }
276
277         [Map]
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.
285         }
286
287         [Map]
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.
323                 SIGUNUSED = 31
324         }
325
326         [Flags][Map]
327         public enum WaitOptions : int {
328                 WNOHANG   = 1,  // Don't block waiting
329                 WUNTRACED = 2,  // Report status of stopped children
330         }
331
332   [Flags][Map]
333         [CLSCompliant (false)]
334         public enum AccessModes : int {
335                 R_OK = 1,
336                 W_OK = 2,
337                 X_OK = 4,
338                 F_OK = 8,
339         }
340
341         [Map]
342         [CLSCompliant (false)]
343         public enum PathconfName : int {
344                 _PC_LINK_MAX,
345                 _PC_MAX_CANON,
346                 _PC_MAX_INPUT,
347                 _PC_NAME_MAX,
348                 _PC_PATH_MAX,
349                 _PC_PIPE_BUF,
350                 _PC_CHOWN_RESTRICTED,
351                 _PC_NO_TRUNC,
352                 _PC_VDISABLE,
353                 _PC_SYNC_IO,
354                 _PC_ASYNC_IO,
355                 _PC_PRIO_IO,
356                 _PC_SOCK_MAXBUF,
357                 _PC_FILESIZEBITS,
358                 _PC_REC_INCR_XFER_SIZE,
359                 _PC_REC_MAX_XFER_SIZE,
360                 _PC_REC_MIN_XFER_SIZE,
361                 _PC_REC_XFER_ALIGN,
362                 _PC_ALLOC_SIZE_MIN,
363                 _PC_SYMLINK_MAX,
364                 _PC_2_SYMLINKS
365         }
366
367         [Map]
368         [CLSCompliant (false)]
369         public enum SysconfName : int {
370                 _SC_ARG_MAX,
371                 _SC_CHILD_MAX,
372                 _SC_CLK_TCK,
373                 _SC_NGROUPS_MAX,
374                 _SC_OPEN_MAX,
375                 _SC_STREAM_MAX,
376                 _SC_TZNAME_MAX,
377                 _SC_JOB_CONTROL,
378                 _SC_SAVED_IDS,
379                 _SC_REALTIME_SIGNALS,
380                 _SC_PRIORITY_SCHEDULING,
381                 _SC_TIMERS,
382                 _SC_ASYNCHRONOUS_IO,
383                 _SC_PRIORITIZED_IO,
384                 _SC_SYNCHRONIZED_IO,
385                 _SC_FSYNC,
386                 _SC_MAPPED_FILES,
387                 _SC_MEMLOCK,
388                 _SC_MEMLOCK_RANGE,
389                 _SC_MEMORY_PROTECTION,
390                 _SC_MESSAGE_PASSING,
391                 _SC_SEMAPHORES,
392                 _SC_SHARED_MEMORY_OBJECTS,
393                 _SC_AIO_LISTIO_MAX,
394                 _SC_AIO_MAX,
395                 _SC_AIO_PRIO_DELTA_MAX,
396                 _SC_DELAYTIMER_MAX,
397                 _SC_MQ_OPEN_MAX,
398                 _SC_MQ_PRIO_MAX,
399                 _SC_VERSION,
400                 _SC_PAGESIZE,
401                 _SC_RTSIG_MAX,
402                 _SC_SEM_NSEMS_MAX,
403                 _SC_SEM_VALUE_MAX,
404                 _SC_SIGQUEUE_MAX,
405                 _SC_TIMER_MAX,
406                 /* Values for the argument to `sysconf'
407                          corresponding to _POSIX2_* symbols.  */
408                 _SC_BC_BASE_MAX,
409                 _SC_BC_DIM_MAX,
410                 _SC_BC_SCALE_MAX,
411                 _SC_BC_STRING_MAX,
412                 _SC_COLL_WEIGHTS_MAX,
413                 _SC_EQUIV_CLASS_MAX,
414                 _SC_EXPR_NEST_MAX,
415                 _SC_LINE_MAX,
416                 _SC_RE_DUP_MAX,
417                 _SC_CHARCLASS_NAME_MAX,
418                 _SC_2_VERSION,
419                 _SC_2_C_BIND,
420                 _SC_2_C_DEV,
421                 _SC_2_FORT_DEV,
422                 _SC_2_FORT_RUN,
423                 _SC_2_SW_DEV,
424                 _SC_2_LOCALEDEF,
425                 _SC_PII,
426                 _SC_PII_XTI,
427                 _SC_PII_SOCKET,
428                 _SC_PII_INTERNET,
429                 _SC_PII_OSI,
430                 _SC_POLL,
431                 _SC_SELECT,
432                 _SC_UIO_MAXIOV,
433                 _SC_IOV_MAX = _SC_UIO_MAXIOV,
434                 _SC_PII_INTERNET_STREAM,
435                 _SC_PII_INTERNET_DGRAM,
436                 _SC_PII_OSI_COTS,
437                 _SC_PII_OSI_CLTS,
438                 _SC_PII_OSI_M,
439                 _SC_T_IOV_MAX,
440                 /* Values according to POSIX 1003.1c (POSIX threads).  */
441                 _SC_THREADS,
442                 _SC_THREAD_SAFE_FUNCTIONS,
443                 _SC_GETGR_R_SIZE_MAX,
444                 _SC_GETPW_R_SIZE_MAX,
445                 _SC_LOGIN_NAME_MAX,
446                 _SC_TTY_NAME_MAX,
447                 _SC_THREAD_DESTRUCTOR_ITERATIONS,
448                 _SC_THREAD_KEYS_MAX,
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,
459                 _SC_PHYS_PAGES,
460                 _SC_AVPHYS_PAGES,
461                 _SC_ATEXIT_MAX,
462                 _SC_PASS_MAX,
463                 _SC_XOPEN_VERSION,
464                 _SC_XOPEN_XCU_VERSION,
465                 _SC_XOPEN_UNIX,
466                 _SC_XOPEN_CRYPT,
467                 _SC_XOPEN_ENH_I18N,
468                 _SC_XOPEN_SHM,
469                 _SC_2_CHAR_TERM,
470                 _SC_2_C_VERSION,
471                 _SC_2_UPE,
472                 _SC_XOPEN_XPG2,
473                 _SC_XOPEN_XPG3,
474                 _SC_XOPEN_XPG4,
475                 _SC_CHAR_BIT,
476                 _SC_CHAR_MAX,
477                 _SC_CHAR_MIN,
478                 _SC_INT_MAX,
479                 _SC_INT_MIN,
480                 _SC_LONG_BIT,
481                 _SC_WORD_BIT,
482                 _SC_MB_LEN_MAX,
483                 _SC_NZERO,
484                 _SC_SSIZE_MAX,
485                 _SC_SCHAR_MAX,
486                 _SC_SCHAR_MIN,
487                 _SC_SHRT_MAX,
488                 _SC_SHRT_MIN,
489                 _SC_UCHAR_MAX,
490                 _SC_UINT_MAX,
491                 _SC_ULONG_MAX,
492                 _SC_USHRT_MAX,
493                 _SC_NL_ARGMAX,
494                 _SC_NL_LANGMAX,
495                 _SC_NL_MSGMAX,
496                 _SC_NL_NMAX,
497                 _SC_NL_SETMAX,
498                 _SC_NL_TEXTMAX,
499                 _SC_XBS5_ILP32_OFF32,
500                 _SC_XBS5_ILP32_OFFBIG,
501                 _SC_XBS5_LP64_OFF64,
502                 _SC_XBS5_LPBIG_OFFBIG,
503                 _SC_XOPEN_LEGACY,
504                 _SC_XOPEN_REALTIME,
505                 _SC_XOPEN_REALTIME_THREADS,
506                 _SC_ADVISORY_INFO,
507                 _SC_BARRIERS,
508                 _SC_BASE,
509                 _SC_C_LANG_SUPPORT,
510                 _SC_C_LANG_SUPPORT_R,
511                 _SC_CLOCK_SELECTION,
512                 _SC_CPUTIME,
513                 _SC_THREAD_CPUTIME,
514                 _SC_DEVICE_IO,
515                 _SC_DEVICE_SPECIFIC,
516                 _SC_DEVICE_SPECIFIC_R,
517                 _SC_FD_MGMT,
518                 _SC_FIFO,
519                 _SC_PIPE,
520                 _SC_FILE_ATTRIBUTES,
521                 _SC_FILE_LOCKING,
522                 _SC_FILE_SYSTEM,
523                 _SC_MONOTONIC_CLOCK,
524                 _SC_MULTI_PROCESS,
525                 _SC_SINGLE_PROCESS,
526                 _SC_NETWORKING,
527                 _SC_READER_WRITER_LOCKS,
528                 _SC_SPIN_LOCKS,
529                 _SC_REGEXP,
530                 _SC_REGEX_VERSION,
531                 _SC_SHELL,
532                 _SC_SIGNALS,
533                 _SC_SPAWN,
534                 _SC_SPORADIC_SERVER,
535                 _SC_THREAD_SPORADIC_SERVER,
536                 _SC_SYSTEM_DATABASE,
537                 _SC_SYSTEM_DATABASE_R,
538                 _SC_TIMEOUTS,
539                 _SC_TYPED_MEMORY_OBJECTS,
540                 _SC_USER_GROUPS,
541                 _SC_USER_GROUPS_R,
542                 _SC_2_PBS,
543                 _SC_2_PBS_ACCOUNTING,
544                 _SC_2_PBS_LOCATE,
545                 _SC_2_PBS_MESSAGE,
546                 _SC_2_PBS_TRACK,
547                 _SC_SYMLOOP_MAX,
548                 _SC_STREAMS,
549                 _SC_2_PBS_CHECKPOINT,
550                 _SC_V6_ILP32_OFF32,
551                 _SC_V6_ILP32_OFFBIG,
552                 _SC_V6_LP64_OFF64,
553                 _SC_V6_LPBIG_OFFBIG,
554                 _SC_HOST_NAME_MAX,
555                 _SC_TRACE,
556                 _SC_TRACE_EVENT_FILTER,
557                 _SC_TRACE_INHERIT,
558                 _SC_TRACE_LOG,
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
574         }
575
576         [Map]
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,
584                 _CS_LFS_LDFLAGS,
585                 _CS_LFS_LIBS,
586                 _CS_LFS_LINTFLAGS,
587                 _CS_LFS64_CFLAGS,
588                 _CS_LFS64_LDFLAGS,
589                 _CS_LFS64_LIBS,
590                 _CS_LFS64_LINTFLAGS,
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
623         }
624
625         [Map]
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.
632         }
633
634         [Map][Flags]
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
647         }
648
649         [Map][Flags]
650         [CLSCompliant (false)]
651         public enum XattrFlags : int {
652                 XATTR_AUTO = 0,
653                 XATTR_CREATE = 1,
654                 XATTR_REPLACE = 2,
655         }
656
657         [Map][Flags]
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
673         }
674
675         [Map][Flags]
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.
682                 MAP_FILE        = 0,
683                 MAP_ANONYMOUS   = 0x20,     // Don't use a file.
684                 MAP_ANON        = MAP_ANONYMOUS,
685
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.
694         }
695
696         [Map][Flags]
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).
707         }
708
709         [Map][Flags]
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.
715         }
716
717         [Map][Flags]
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
722         }
723
724         [Map][Flags]
725         [CLSCompliant (false)]
726         public enum MremapFlags : ulong {
727                 MREMAP_MAYMOVE = 0x1,
728         }
729
730         #endregion
731
732         #region Structures
733
734         [Map ("struct flock")]
735         public struct Flock
736 #if NET_2_0
737                 : IEquatable <Flock>
738 #endif
739         {
740                 [CLSCompliant (false)]
741                 public LockType         l_type;    // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
742                 [CLSCompliant (false)]
743                 public SeekFlags        l_whence;  // How to interpret l_start
744                 [off_t] public long     l_start;   // Starting offset for lock
745                 [off_t] public long     l_len;     // Number of bytes to lock
746                 [pid_t] public int      l_pid;     // PID of process blocking our lock (F_GETLK only)
747
748                 public override int GetHashCode ()
749                 {
750                         return l_type.GetHashCode () ^ l_whence.GetHashCode () ^ 
751                                 l_start.GetHashCode () ^ l_len.GetHashCode () ^
752                                 l_pid.GetHashCode ();
753                 }
754
755                 public override bool Equals (object obj)
756                 {
757                         if ((obj == null) || (obj.GetType () != GetType ()))
758                                 return false;
759                         Flock value = (Flock) obj;
760                         return l_type == value.l_type && l_whence == value.l_whence && 
761                                 l_start == value.l_start && l_len == value.l_len && 
762                                 l_pid == value.l_pid;
763                 }
764
765                 public bool Equals (Flock value)
766                 {
767                         return l_type == value.l_type && l_whence == value.l_whence && 
768                                 l_start == value.l_start && l_len == value.l_len && 
769                                 l_pid == value.l_pid;
770                 }
771
772                 public static bool operator== (Flock lhs, Flock rhs)
773                 {
774                         return lhs.Equals (rhs);
775                 }
776
777                 public static bool operator!= (Flock lhs, Flock rhs)
778                 {
779                         return !lhs.Equals (rhs);
780                 }
781         }
782
783         [Map ("struct pollfd")]
784         public struct Pollfd
785 #if NET_2_0
786                 : IEquatable <Pollfd>
787 #endif
788         {
789                 public int fd;
790                 [CLSCompliant (false)]
791                 public PollEvents events;
792                 [CLSCompliant (false)]
793                 public PollEvents revents;
794
795                 public override int GetHashCode ()
796                 {
797                         return events.GetHashCode () ^ revents.GetHashCode ();
798                 }
799
800                 public override bool Equals (object obj)
801                 {
802                         if (obj == null || obj.GetType () != GetType ())
803                                 return false;
804                         Pollfd value = (Pollfd) obj;
805                         return value.events == events && value.revents == revents;
806                 }
807
808                 public bool Equals (Pollfd value)
809                 {
810                         return value.events == events && value.revents == revents;
811                 }
812
813                 public static bool operator== (Pollfd lhs, Pollfd rhs)
814                 {
815                         return lhs.Equals (rhs);
816                 }
817
818                 public static bool operator!= (Pollfd lhs, Pollfd rhs)
819                 {
820                         return !lhs.Equals (rhs);
821                 }
822         }
823
824         // Use manually written To/From methods to handle fields st_atime_nsec etc.
825         public struct Stat
826 #if NET_2_0
827                 : IEquatable <Stat>
828 #endif
829         {
830                 [CLSCompliant (false)]
831                 [dev_t]     public ulong    st_dev;     // device
832                 [CLSCompliant (false)]
833                 [ino_t]     public  ulong   st_ino;     // inode
834                 [CLSCompliant (false)]
835                 public  FilePermissions     st_mode;    // protection
836                 [NonSerialized]
837 #pragma warning disable 169             
838                 private uint                _padding_;  // padding for structure alignment
839 #pragma warning restore 169             
840                 [CLSCompliant (false)]
841                 [nlink_t]   public  ulong   st_nlink;   // number of hard links
842                 [CLSCompliant (false)]
843                 [uid_t]     public  uint    st_uid;     // user ID of owner
844                 [CLSCompliant (false)]
845                 [gid_t]     public  uint    st_gid;     // group ID of owner
846                 [CLSCompliant (false)]
847                 [dev_t]     public  ulong   st_rdev;    // device type (if inode device)
848                 [off_t]     public  long    st_size;    // total size, in bytes
849                 [blksize_t] public  long    st_blksize; // blocksize for filesystem I/O
850                 [blkcnt_t]  public  long    st_blocks;  // number of blocks allocated
851                 [time_t]    public  long    st_atime;   // time of last access
852                 [time_t]    public  long    st_mtime;   // time of last modification
853                 [time_t]    public  long    st_ctime;   // time of last status change
854                 public  long             st_atime_nsec; // Timespec.tv_nsec partner to st_atime
855                 public  long             st_mtime_nsec; // Timespec.tv_nsec partner to st_mtime
856                 public  long             st_ctime_nsec; // Timespec.tv_nsec partner to st_ctime
857
858                 public Timespec st_atim {
859                         get {
860                                 return new Timespec { tv_sec = st_atime, tv_nsec = st_atime_nsec };
861                         }
862                         set {
863                                 st_atime = value.tv_sec;
864                                 st_atime_nsec = value.tv_nsec;
865                         }
866                 }
867
868                 public Timespec st_mtim {
869                         get {
870                                 return new Timespec { tv_sec = st_mtime, tv_nsec = st_mtime_nsec };
871                         }
872                         set {
873                                 st_mtime = value.tv_sec;
874                                 st_mtime_nsec = value.tv_nsec;
875                         }
876                 }
877
878                 public Timespec st_ctim {
879                         get {
880                                 return new Timespec { tv_sec = st_ctime, tv_nsec = st_ctime_nsec };
881                         }
882                         set {
883                                 st_ctime = value.tv_sec;
884                                 st_ctime_nsec = value.tv_nsec;
885                         }
886                 }
887
888                 public override int GetHashCode ()
889                 {
890                         return st_dev.GetHashCode () ^
891                                 st_ino.GetHashCode () ^
892                                 st_mode.GetHashCode () ^
893                                 st_nlink.GetHashCode () ^
894                                 st_uid.GetHashCode () ^
895                                 st_gid.GetHashCode () ^
896                                 st_rdev.GetHashCode () ^
897                                 st_size.GetHashCode () ^
898                                 st_blksize.GetHashCode () ^
899                                 st_blocks.GetHashCode () ^
900                                 st_atime.GetHashCode () ^
901                                 st_mtime.GetHashCode () ^
902                                 st_ctime.GetHashCode () ^
903                                 st_atime_nsec.GetHashCode () ^
904                                 st_mtime_nsec.GetHashCode () ^
905                                 st_ctime_nsec.GetHashCode ();
906                 }
907
908                 public override bool Equals (object obj)
909                 {
910                         if (obj == null || obj.GetType() != GetType ())
911                                 return false;
912                         Stat value = (Stat) obj;
913                         return value.st_dev == st_dev &&
914                                 value.st_ino == st_ino &&
915                                 value.st_mode == st_mode &&
916                                 value.st_nlink == st_nlink &&
917                                 value.st_uid == st_uid &&
918                                 value.st_gid == st_gid &&
919                                 value.st_rdev == st_rdev &&
920                                 value.st_size == st_size &&
921                                 value.st_blksize == st_blksize &&
922                                 value.st_blocks == st_blocks &&
923                                 value.st_atime == st_atime &&
924                                 value.st_mtime == st_mtime &&
925                                 value.st_ctime == st_ctime &&
926                                 value.st_atime_nsec == st_atime_nsec &&
927                                 value.st_mtime_nsec == st_mtime_nsec &&
928                                 value.st_ctime_nsec == st_ctime_nsec;
929                 }
930
931                 public bool Equals (Stat value)
932                 {
933                         return value.st_dev == st_dev &&
934                                 value.st_ino == st_ino &&
935                                 value.st_mode == st_mode &&
936                                 value.st_nlink == st_nlink &&
937                                 value.st_uid == st_uid &&
938                                 value.st_gid == st_gid &&
939                                 value.st_rdev == st_rdev &&
940                                 value.st_size == st_size &&
941                                 value.st_blksize == st_blksize &&
942                                 value.st_blocks == st_blocks &&
943                                 value.st_atime == st_atime &&
944                                 value.st_mtime == st_mtime &&
945                                 value.st_ctime == st_ctime &&
946                                 value.st_atime_nsec == st_atime_nsec &&
947                                 value.st_mtime_nsec == st_mtime_nsec &&
948                                 value.st_ctime_nsec == st_ctime_nsec;
949                 }
950
951                 public static bool operator== (Stat lhs, Stat rhs)
952                 {
953                         return lhs.Equals (rhs);
954                 }
955
956                 public static bool operator!= (Stat lhs, Stat rhs)
957                 {
958                         return !lhs.Equals (rhs);
959                 }
960         }
961
962         // `struct statvfs' isn't portable, so don't generate To/From methods.
963         [Map]
964         [CLSCompliant (false)]
965         public struct Statvfs
966 #if NET_2_0
967                 : IEquatable <Statvfs>
968 #endif
969         {
970                 public                  ulong f_bsize;    // file system block size
971                 public                  ulong f_frsize;   // fragment size
972                 [fsblkcnt_t] public     ulong f_blocks;   // size of fs in f_frsize units
973                 [fsblkcnt_t] public     ulong f_bfree;    // # free blocks
974                 [fsblkcnt_t] public     ulong f_bavail;   // # free blocks for non-root
975                 [fsfilcnt_t] public     ulong f_files;    // # inodes
976                 [fsfilcnt_t] public     ulong f_ffree;    // # free inodes
977                 [fsfilcnt_t] public     ulong f_favail;   // # free inodes for non-root
978                 public                  ulong f_fsid;     // file system id
979                 public MountFlags             f_flag;     // mount flags
980                 public                  ulong f_namemax;  // maximum filename length
981
982                 public override int GetHashCode ()
983                 {
984                         return f_bsize.GetHashCode () ^
985                                 f_frsize.GetHashCode () ^
986                                 f_blocks.GetHashCode () ^
987                                 f_bfree.GetHashCode () ^
988                                 f_bavail.GetHashCode () ^
989                                 f_files.GetHashCode () ^
990                                 f_ffree.GetHashCode () ^
991                                 f_favail.GetHashCode () ^
992                                 f_fsid.GetHashCode () ^
993                                 f_flag.GetHashCode () ^
994                                 f_namemax.GetHashCode ();
995                 }
996
997                 public override bool Equals (object obj)
998                 {
999                         if (obj == null || obj.GetType() != GetType ())
1000                                 return false;
1001                         Statvfs value = (Statvfs) obj;
1002                         return value.f_bsize == f_bsize &&
1003                                 value.f_frsize == f_frsize &&
1004                                 value.f_blocks == f_blocks &&
1005                                 value.f_bfree == f_bfree &&
1006                                 value.f_bavail == f_bavail &&
1007                                 value.f_files == f_files &&
1008                                 value.f_ffree == f_ffree &&
1009                                 value.f_favail == f_favail &&
1010                                 value.f_fsid == f_fsid &&
1011                                 value.f_flag == f_flag &&
1012                                 value.f_namemax == f_namemax;
1013                 }
1014
1015                 public bool Equals (Statvfs value)
1016                 {
1017                         return value.f_bsize == f_bsize &&
1018                                 value.f_frsize == f_frsize &&
1019                                 value.f_blocks == f_blocks &&
1020                                 value.f_bfree == f_bfree &&
1021                                 value.f_bavail == f_bavail &&
1022                                 value.f_files == f_files &&
1023                                 value.f_ffree == f_ffree &&
1024                                 value.f_favail == f_favail &&
1025                                 value.f_fsid == f_fsid &&
1026                                 value.f_flag == f_flag &&
1027                                 value.f_namemax == f_namemax;
1028                 }
1029
1030                 public static bool operator== (Statvfs lhs, Statvfs rhs)
1031                 {
1032                         return lhs.Equals (rhs);
1033                 }
1034
1035                 public static bool operator!= (Statvfs lhs, Statvfs rhs)
1036                 {
1037                         return !lhs.Equals (rhs);
1038                 }
1039         }
1040
1041         [Map ("struct timeval")]
1042         public struct Timeval
1043 #if NET_2_0
1044                 : IEquatable <Timeval>
1045 #endif
1046         {
1047                 [time_t]      public long tv_sec;   // seconds
1048                 [suseconds_t] public long tv_usec;  // microseconds
1049
1050                 public override int GetHashCode ()
1051                 {
1052                         return tv_sec.GetHashCode () ^ tv_usec.GetHashCode ();
1053                 }
1054
1055                 public override bool Equals (object obj)
1056                 {
1057                         if (obj == null || obj.GetType () != GetType ())
1058                                 return false;
1059                         Timeval value = (Timeval) obj;
1060                         return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1061                 }
1062
1063                 public bool Equals (Timeval value)
1064                 {
1065                         return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1066                 }
1067
1068                 public static bool operator== (Timeval lhs, Timeval rhs)
1069                 {
1070                         return lhs.Equals (rhs);
1071                 }
1072
1073                 public static bool operator!= (Timeval lhs, Timeval rhs)
1074                 {
1075                         return !lhs.Equals (rhs);
1076                 }
1077         }
1078
1079         [Map ("struct timezone")]
1080         public struct Timezone
1081 #if NET_2_0
1082                 : IEquatable <Timezone>
1083 #endif
1084         {
1085                 public  int tz_minuteswest; // minutes W of Greenwich
1086 #pragma warning disable 169             
1087                 private int tz_dsttime;     // type of dst correction (OBSOLETE)
1088 #pragma warning restore 169             
1089
1090                 public override int GetHashCode ()
1091                 {
1092                         return tz_minuteswest.GetHashCode ();
1093                 }
1094
1095                 public override bool Equals (object obj)
1096                 {
1097                         if (obj == null || obj.GetType () != GetType ())
1098                                 return false;
1099                         Timezone value = (Timezone) obj;
1100                         return value.tz_minuteswest == tz_minuteswest;
1101                 }
1102
1103                 public bool Equals (Timezone value)
1104                 {
1105                         return value.tz_minuteswest == tz_minuteswest;
1106                 }
1107
1108                 public static bool operator== (Timezone lhs, Timezone rhs)
1109                 {
1110                         return lhs.Equals (rhs);
1111                 }
1112
1113                 public static bool operator!= (Timezone lhs, Timezone rhs)
1114                 {
1115                         return !lhs.Equals (rhs);
1116                 }
1117         }
1118
1119         [Map ("struct utimbuf")]
1120         public struct Utimbuf
1121 #if NET_2_0
1122                 : IEquatable <Utimbuf>
1123 #endif
1124         {
1125                 [time_t] public long    actime;   // access time
1126                 [time_t] public long    modtime;  // modification time
1127
1128                 public override int GetHashCode ()
1129                 {
1130                         return actime.GetHashCode () ^ modtime.GetHashCode ();
1131                 }
1132
1133                 public override bool Equals (object obj)
1134                 {
1135                         if (obj == null || obj.GetType () != GetType ())
1136                                 return false;
1137                         Utimbuf value = (Utimbuf) obj;
1138                         return value.actime == actime && value.modtime == modtime;
1139                 }
1140
1141                 public bool Equals (Utimbuf value)
1142                 {
1143                         return value.actime == actime && value.modtime == modtime;
1144                 }
1145
1146                 public static bool operator== (Utimbuf lhs, Utimbuf rhs)
1147                 {
1148                         return lhs.Equals (rhs);
1149                 }
1150
1151                 public static bool operator!= (Utimbuf lhs, Utimbuf rhs)
1152                 {
1153                         return !lhs.Equals (rhs);
1154                 }
1155         }
1156
1157         [Map ("struct timespec")]
1158         public struct Timespec
1159 #if NET_2_0
1160                 : IEquatable <Timespec>
1161 #endif
1162         {
1163                 [time_t] public long    tv_sec;   // Seconds.
1164                 public          long    tv_nsec;  // Nanoseconds.
1165
1166                 public override int GetHashCode ()
1167                 {
1168                         return tv_sec.GetHashCode () ^ tv_nsec.GetHashCode ();
1169                 }
1170
1171                 public override bool Equals (object obj)
1172                 {
1173                         if (obj == null || obj.GetType () != GetType ())
1174                                 return false;
1175                         Timespec value = (Timespec) obj;
1176                         return value.tv_sec == tv_sec && value.tv_nsec == tv_nsec;
1177                 }
1178
1179                 public bool Equals (Timespec value)
1180                 {
1181                         return value.tv_sec == tv_sec && value.tv_nsec == tv_nsec;
1182                 }
1183
1184                 public static bool operator== (Timespec lhs, Timespec rhs)
1185                 {
1186                         return lhs.Equals (rhs);
1187                 }
1188
1189                 public static bool operator!= (Timespec lhs, Timespec rhs)
1190                 {
1191                         return !lhs.Equals (rhs);
1192                 }
1193         }
1194
1195         [Map ("struct iovec")]
1196         public struct Iovec
1197         {
1198                 public IntPtr   iov_base; // Starting address
1199                 [CLSCompliant (false)]
1200                 public ulong    iov_len;  // Number of bytes to transfer
1201         }
1202
1203         [Flags][Map]
1204         public enum EpollFlags {
1205                 EPOLL_CLOEXEC = 02000000,
1206                 EPOLL_NONBLOCK = 04000,
1207         }
1208
1209         [Flags][Map]
1210         [CLSCompliant (false)]
1211         public enum EpollEvents : uint {
1212                 EPOLLIN = 0x001,
1213                 EPOLLPRI = 0x002,
1214                 EPOLLOUT = 0x004,
1215                 EPOLLRDNORM = 0x040,
1216                 EPOLLRDBAND = 0x080,
1217                 EPOLLWRNORM = 0x100,
1218                 EPOLLWRBAND = 0x200,
1219                 EPOLLMSG = 0x400,
1220                 EPOLLERR = 0x008,
1221                 EPOLLHUP = 0x010,
1222                 EPOLLRDHUP = 0x2000,
1223                 EPOLLONESHOT = 1 << 30,
1224                 EPOLLET = unchecked ((uint) (1 << 31))
1225         }
1226
1227         public enum EpollOp {
1228                 EPOLL_CTL_ADD = 1,
1229                 EPOLL_CTL_DEL = 2,
1230                 EPOLL_CTL_MOD = 3,
1231         }
1232
1233         [StructLayout (LayoutKind.Explicit, Size=12, Pack=1)]
1234         [CLSCompliant (false)]
1235         public struct EpollEvent {
1236                 [FieldOffset (0)]
1237                 public EpollEvents events;
1238                 [FieldOffset (4)]
1239                 public int fd;
1240                 [FieldOffset (4)]
1241                 public IntPtr ptr;
1242                 [FieldOffset (4)]
1243                 public uint u32;
1244                 [FieldOffset (4)]
1245                 public ulong u64;
1246         }
1247         #endregion
1248
1249         #region Classes
1250
1251         public sealed class Dirent
1252 #if NET_2_0
1253                 : IEquatable <Dirent>
1254 #endif
1255         {
1256                 [CLSCompliant (false)]
1257                 public /* ino_t */ ulong  d_ino;
1258                 public /* off_t */ long   d_off;
1259                 [CLSCompliant (false)]
1260                 public ushort             d_reclen;
1261                 public byte               d_type;
1262                 public string             d_name;
1263
1264                 public override int GetHashCode ()
1265                 {
1266                         return d_ino.GetHashCode () ^ d_off.GetHashCode () ^ 
1267                                 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
1268                                 d_name.GetHashCode ();
1269                 }
1270
1271                 public override bool Equals (object obj)
1272                 {
1273                         if (obj == null || GetType() != obj.GetType())
1274                                 return false;
1275                         Dirent d = (Dirent) obj;
1276                         return Equals (d);
1277                 }
1278
1279                 public bool Equals (Dirent value)
1280                 {
1281                         if (value == null)
1282                                 return false;
1283                         return value.d_ino == d_ino && value.d_off == d_off &&
1284                                 value.d_reclen == d_reclen && value.d_type == d_type &&
1285                                 value.d_name == d_name;
1286                 }
1287
1288                 public override string ToString ()
1289                 {
1290                         return d_name;
1291                 }
1292
1293                 public static bool operator== (Dirent lhs, Dirent rhs)
1294                 {
1295                         return Object.Equals (lhs, rhs);
1296                 }
1297
1298                 public static bool operator!= (Dirent lhs, Dirent rhs)
1299                 {
1300                         return !Object.Equals (lhs, rhs);
1301                 }
1302         }
1303
1304         public sealed class Fstab
1305 #if NET_2_0
1306                 : IEquatable <Fstab>
1307 #endif
1308         {
1309                 public string fs_spec;
1310                 public string fs_file;
1311                 public string fs_vfstype;
1312                 public string fs_mntops;
1313                 public string fs_type;
1314                 public int    fs_freq;
1315                 public int    fs_passno;
1316
1317                 public override int GetHashCode ()
1318                 {
1319                         return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
1320                                 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
1321                                 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
1322                 }
1323
1324                 public override bool Equals (object obj)
1325                 {
1326                         if (obj == null || GetType() != obj.GetType())
1327                                 return false;
1328                         Fstab f = (Fstab) obj;
1329                         return Equals (f);
1330                 }
1331
1332                 public bool Equals (Fstab value)
1333                 {
1334                         if (value == null)
1335                                 return false;
1336                         return value.fs_spec == fs_spec && value.fs_file == fs_file &&
1337                                 value.fs_vfstype == fs_vfstype && value.fs_mntops == fs_mntops &&
1338                                 value.fs_type == fs_type && value.fs_freq == fs_freq && 
1339                                 value.fs_passno == fs_passno;
1340                 }
1341
1342                 public override string ToString ()
1343                 {
1344                         return fs_spec;
1345                 }
1346
1347                 public static bool operator== (Fstab lhs, Fstab rhs)
1348                 {
1349                         return Object.Equals (lhs, rhs);
1350                 }
1351
1352                 public static bool operator!= (Fstab lhs, Fstab rhs)
1353                 {
1354                         return !Object.Equals (lhs, rhs);
1355                 }
1356         }
1357
1358         public sealed class Group
1359 #if NET_2_0
1360                 : IEquatable <Group>
1361 #endif
1362         {
1363                 public string           gr_name;
1364                 public string           gr_passwd;
1365                 [CLSCompliant (false)]
1366                 public /* gid_t */ uint gr_gid;
1367                 public string[]         gr_mem;
1368
1369                 public override int GetHashCode ()
1370                 {
1371                         int memhc = 0;
1372                         for (int i = 0; i < gr_mem.Length; ++i)
1373                                 memhc ^= gr_mem[i].GetHashCode ();
1374
1375                         return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^ 
1376                                 gr_gid.GetHashCode () ^ memhc;
1377                 }
1378
1379                 public override bool Equals (object obj)
1380                 {
1381                         if (obj == null || GetType() != obj.GetType())
1382                                 return false;
1383                         Group g = (Group) obj;
1384                         return Equals (g);
1385                 }
1386
1387                 public bool Equals (Group value)
1388                 {
1389                         if (value == null)
1390                                 return false;
1391                         if (value.gr_gid != gr_gid)
1392                                 return false;
1393                         if (value.gr_gid == gr_gid && value.gr_name == gr_name &&
1394                                 value.gr_passwd == gr_passwd) {
1395                                 if (value.gr_mem == gr_mem)
1396                                         return true;
1397                                 if (value.gr_mem == null || gr_mem == null)
1398                                         return false;
1399                                 if (value.gr_mem.Length != gr_mem.Length)
1400                                         return false;
1401                                 for (int i = 0; i < gr_mem.Length; ++i)
1402                                         if (gr_mem[i] != value.gr_mem[i])
1403                                                 return false;
1404                                 return true;
1405                         }
1406                         return false;
1407                 }
1408
1409                 // Generate string in /etc/group format
1410                 public override string ToString ()
1411                 {
1412                         StringBuilder sb = new StringBuilder ();
1413                         sb.Append (gr_name).Append (":").Append (gr_passwd).Append (":");
1414                         sb.Append (gr_gid).Append (":");
1415                         GetMembers (sb, gr_mem);
1416                         return sb.ToString ();
1417                 }
1418
1419                 private static void GetMembers (StringBuilder sb, string[] members)
1420                 {
1421                         if (members.Length > 0)
1422                                 sb.Append (members[0]);
1423                         for (int i = 1; i < members.Length; ++i) {
1424                                 sb.Append (",");
1425                                 sb.Append (members[i]);
1426                         }
1427                 }
1428
1429                 public static bool operator== (Group lhs, Group rhs)
1430                 {
1431                         return Object.Equals (lhs, rhs);
1432                 }
1433
1434                 public static bool operator!= (Group lhs, Group rhs)
1435                 {
1436                         return !Object.Equals (lhs, rhs);
1437                 }
1438         }
1439
1440         public sealed class Passwd
1441 #if NET_2_0
1442                 : IEquatable <Passwd>
1443 #endif
1444         {
1445                 public string           pw_name;
1446                 public string           pw_passwd;
1447                 [CLSCompliant (false)]
1448                 public /* uid_t */ uint pw_uid;
1449                 [CLSCompliant (false)]
1450                 public /* gid_t */ uint pw_gid;
1451                 public string           pw_gecos;
1452                 public string           pw_dir;
1453                 public string           pw_shell;
1454
1455                 public override int GetHashCode ()
1456                 {
1457                         return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^ 
1458                                 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
1459                                 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
1460                                 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
1461                 }
1462
1463                 public override bool Equals (object obj)
1464                 {
1465                         if (obj == null || GetType() != obj.GetType())
1466                                 return false;
1467                         Passwd p = (Passwd) obj;
1468                         return Equals (p);
1469                 }
1470
1471                 public bool Equals (Passwd value)
1472                 {
1473                         if (value == null)
1474                                 return false;
1475                         return value.pw_uid == pw_uid && value.pw_gid == pw_gid && 
1476                                 value.pw_name == pw_name && value.pw_passwd == pw_passwd && 
1477                                 value.pw_gecos == pw_gecos && value.pw_dir == pw_dir && 
1478                                 value.pw_shell == pw_shell;
1479                 }
1480
1481                 // Generate string in /etc/passwd format
1482                 public override string ToString ()
1483                 {
1484                         return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
1485                                 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
1486                 }
1487
1488                 public static bool operator== (Passwd lhs, Passwd rhs)
1489                 {
1490                         return Object.Equals (lhs, rhs);
1491                 }
1492
1493                 public static bool operator!= (Passwd lhs, Passwd rhs)
1494                 {
1495                         return !Object.Equals (lhs, rhs);
1496                 }
1497         }
1498
1499         public sealed class Utsname
1500 #if NET_2_0
1501                 : IEquatable <Utsname>
1502 #endif
1503         {
1504                 public string sysname;
1505                 public string nodename;
1506                 public string release;
1507                 public string version;
1508                 public string machine;
1509                 public string domainname;
1510
1511                 public override int GetHashCode ()
1512                 {
1513                         return sysname.GetHashCode () ^ nodename.GetHashCode () ^ 
1514                                 release.GetHashCode () ^ version.GetHashCode () ^
1515                                 machine.GetHashCode () ^ domainname.GetHashCode ();
1516                 }
1517
1518                 public override bool Equals (object obj)
1519                 {
1520                         if (obj == null || GetType() != obj.GetType())
1521                                 return false;
1522                         Utsname u = (Utsname) obj;
1523                         return Equals (u);
1524                 }
1525
1526                 public bool Equals (Utsname value)
1527                 {
1528                         return value.sysname == sysname && value.nodename == nodename && 
1529                                 value.release == release && value.version == version && 
1530                                 value.machine == machine && value.domainname == domainname;
1531                 }
1532
1533                 // Generate string in /etc/passwd format
1534                 public override string ToString ()
1535                 {
1536                         return string.Format ("{0} {1} {2} {3} {4}",
1537                                 sysname, nodename, release, version, machine);
1538                 }
1539
1540                 public static bool operator== (Utsname lhs, Utsname rhs)
1541                 {
1542                         return Object.Equals (lhs, rhs);
1543                 }
1544
1545                 public static bool operator!= (Utsname lhs, Utsname rhs)
1546                 {
1547                         return !Object.Equals (lhs, rhs);
1548                 }
1549         }
1550
1551         //
1552         // Convention: Functions *not* part of the standard C library AND part of
1553         // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
1554         //
1555         // For example, the man page should be similar to:
1556         //
1557         //    CONFORMING TO (or CONFORMS TO)
1558         //           XPG2, SUSv2, POSIX, etc.
1559         //
1560         // BSD- and GNU-specific exports can also be placed here.
1561         //
1562         // Non-POSIX/XPG/etc. functions can also be placed here if:
1563         //  (a) They'd be likely to be covered in a Steven's-like book
1564         //  (b) The functions would be present in libc.so (or equivalent).
1565         //
1566         // If a function has its own library, that's a STRONG indicator that the
1567         // function should get a different binding, probably in its own assembly, 
1568         // so that package management can work sanely.  (That is, we'd like to avoid
1569         // scenarios where FooLib.dll is installed, but it requires libFooLib.so to
1570         // run, and libFooLib.so doesn't exist.  That would be confusing.)
1571         //
1572         // The only methods in here should be:
1573         //  (1) low-level functions
1574         //  (2) "Trivial" function overloads.  For example, if the parameters to a
1575         //      function are related (e.g. getgroups(2))
1576         //  (3) The return type SHOULD NOT be changed.  If you want to provide a
1577         //      convenience function with a nicer return type, place it into one of
1578         //      the Mono.Unix.Unix* wrapper classes, and give it a .NET-styled name.
1579         //      - EXCEPTION: No public functions should have a `void' return type.
1580         //        `void' return types should be replaced with `int'.
1581         //        Rationality: `void'-return functions typically require a
1582         //        complicated call sequence, such as clear errno, then call, then
1583         //        check errno to see if any errors occurred.  This sequence can't 
1584         //        be done safely in managed code, as errno may change as part of 
1585         //        the P/Invoke mechanism.
1586         //        Instead, add a MonoPosixHelper export which does:
1587         //          errno = 0;
1588         //          INVOKE SYSCALL;
1589         //          return errno == 0 ? 0 : -1;
1590         //        This lets managed code check the return value in the usual manner.
1591         //  (4) Exceptions SHOULD NOT be thrown.  EXCEPTIONS: 
1592         //      - If you're wrapping *broken* methods which make assumptions about 
1593         //        input data, such as that an argument refers to N bytes of data.  
1594         //        This is currently limited to cuserid(3) and encrypt(3).
1595         //      - If you call functions which themselves generate exceptions.  
1596         //        This is the case for using NativeConvert, which will throw an
1597         //        exception if an invalid/unsupported value is used.
1598         //
1599         // Naming Conventions:
1600         //  - Syscall method names should have the same name as the function being
1601         //    wrapped (e.g. Syscall.read ==> read(2)).  This allows people to
1602         //    consult the appropriate man page if necessary.
1603         //  - Methods need not have the same arguments IF this simplifies or
1604         //    permits correct usage.  The current example is syslog, in which
1605         //    syslog(3)'s single `priority' argument is split into SyslogFacility
1606         //    and SyslogLevel arguments.
1607         //  - Type names (structures, classes, enumerations) are always PascalCased.
1608         //  - Enumerations are named as <MethodName><ArgumentName>, and are located
1609         //    in the Mono.Unix.Native namespace.  For readability, if ArgumentName 
1610         //    is "cmd", use Command instead.  For example, fcntl(2) takes a
1611         //    FcntlCommand argument.  This naming convention is to provide an
1612         //    assocation between an enumeration and where it should be used, and
1613         //    allows a single method to accept multiple different enumerations 
1614         //    (see mmap(2), which takes MmapProts and MmapFlags).
1615         //    - EXCEPTION: if an enumeration is shared between multiple different
1616         //      methods, AND/OR the "obvious" enumeration name conflicts with an
1617         //      existing .NET type, a more appropriate name should be used.
1618         //      Example: FilePermissions
1619         //    - EXCEPTION: [Flags] enumerations should get plural names to follow
1620         //      .NET name guidelines.  Usually this doesn't result in a change
1621         //      (OpenFlags is the `flags' parameter for open(2)), but it can
1622         //      (mmap(2) prot ==> MmapProts, access(2) mode ==> AccessModes).
1623         //  - Enumerations should have the [Map] and (optional) [Flags] attributes.
1624         //    [Map] is required for make-map to find the type and generate the
1625         //    appropriate NativeConvert conversion functions.
1626         //  - Enumeration contents should match the original Unix names.  This helps
1627         //    with documentation (the existing man pages are still useful), and is
1628         //    required for use with the make-map generation program.
1629         //  - Structure names should be the PascalCased version of the actual
1630         //    structure name (struct flock ==> Flock).  Structure members should
1631         //    have the same names, or a (reasonably) portable subset (Dirent being
1632         //    the poster child for questionable members).
1633         //    - Whether the managed type should be a reference type (class) or a 
1634         //      value type (struct) should be determined on a case-by-case basis: 
1635         //      if you ever need to be able to use NULL for it (such as with Dirent, 
1636         //      Group, Passwd, as these are method return types and `null' is used 
1637         //      to signify the end), it should be a reference type; otherwise, use 
1638         //      your discretion, and keep any expected usage patterns in mind.
1639         //  - Syscall should be a Single Point Of Truth (SPOT).  There should be
1640         //    only ONE way to do anything.  By convention, the Linux function names
1641         //    are used, but that need not always be the case (use your discretion).
1642         //    It SHOULD NOT be required that developers know what platform they're
1643         //    on, and choose among a set of similar functions.  In short, anything
1644         //    that requires a platform check is BAD -- Mono.Unix is a wrapper, and
1645         //    we can afford to clean things up whenever possible.
1646         //    - Examples: 
1647         //      - Syscall.statfs: Solaris/Mac OS X provide statfs(2), Linux provides
1648         //        statvfs(2).  MonoPosixHelper will "thunk" between the two,
1649         //        exporting a statvfs that works across platforms.
1650         //      - Syscall.getfsent: Glibc export which Solaris lacks, while Solaris
1651         //        instead provides getvfsent(3).  MonoPosixHelper provides wrappers
1652         //        to convert getvfsent(3) into Fstab data.
1653         //    - Exception: If it isn't possible to cleanly wrap platforms, then the
1654         //      method shouldn't be exported.  The user will be expected to do their
1655         //      own platform check and their own DllImports.
1656         //      Examples: mount(2), umount(2), etc.
1657         //    - Note: if a platform doesn't support a function AT ALL, the
1658         //      MonoPosixHelper wrapper won't be compiled, resulting in a
1659         //      EntryPointNotFoundException.  This is also consistent with a missing 
1660         //      P/Invoke into libc.so.
1661         //
1662         [CLSCompliant (false)]
1663         public sealed class Syscall : Stdlib
1664         {
1665                 new internal const string LIBC  = "libc";
1666
1667                 private Syscall () {}
1668
1669                 //
1670                 // <aio.h>
1671                 //
1672
1673                 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3), 
1674                 // aio_return(3), aio_suspend(3), aio_write(3)
1675                 //
1676                 // Then update UnixStream.BeginRead to use the aio* functions.
1677
1678
1679                 #region <attr/xattr.h> Declarations
1680                 //
1681                 // <attr/xattr.h> -- COMPLETE
1682                 //
1683
1684                 // setxattr(2)
1685                 //    int setxattr (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_setxattr")]
1689                 public static extern int setxattr (
1690                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1691                                 string path, 
1692                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1693                                 string name, byte[] value, ulong size, XattrFlags flags);
1694
1695                 public static int setxattr (string path, string name, byte [] value, ulong size)
1696                 {
1697                         return setxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1698                 }
1699
1700                 public static int setxattr (string path, string name, byte [] value, XattrFlags flags)
1701                 {
1702                         return setxattr (path, name, value, (ulong) value.Length, flags);
1703                 }
1704
1705                 public static int setxattr (string path, string name, byte [] value)
1706                 {
1707                         return setxattr (path, name, value, (ulong) value.Length);
1708                 }
1709
1710                 // lsetxattr(2)
1711                 //        int lsetxattr (const char *path, const char *name,
1712                 //                   const void *value, size_t size, int flags);
1713                 [DllImport (MPH, SetLastError=true,
1714                                 EntryPoint="Mono_Posix_Syscall_lsetxattr")]
1715                 public static extern int lsetxattr (
1716                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1717                                 string path, 
1718                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1719                                 string name, byte[] value, ulong size, XattrFlags flags);
1720
1721                 public static int lsetxattr (string path, string name, byte [] value, ulong size)
1722                 {
1723                         return lsetxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1724                 }
1725
1726                 public static int lsetxattr (string path, string name, byte [] value, XattrFlags flags)
1727                 {
1728                         return lsetxattr (path, name, value, (ulong) value.Length, flags);
1729                 }
1730
1731                 public static int lsetxattr (string path, string name, byte [] value)
1732                 {
1733                         return lsetxattr (path, name, value, (ulong) value.Length);
1734                 }
1735
1736                 // fsetxattr(2)
1737                 //        int fsetxattr (int fd, const char *name,
1738                 //                   const void *value, size_t size, int flags);
1739                 [DllImport (MPH, SetLastError=true,
1740                                 EntryPoint="Mono_Posix_Syscall_fsetxattr")]
1741                 public static extern int fsetxattr (int fd, 
1742                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1743                                 string name, byte[] value, ulong size, XattrFlags flags);
1744
1745                 public static int fsetxattr (int fd, string name, byte [] value, ulong size)
1746                 {
1747                         return fsetxattr (fd, name, value, size, XattrFlags.XATTR_AUTO);
1748                 }
1749
1750                 public static int fsetxattr (int fd, string name, byte [] value, XattrFlags flags)
1751                 {
1752                         return fsetxattr (fd, name, value, (ulong) value.Length, flags);
1753                 }
1754
1755                 public static int fsetxattr (int fd, string name, byte [] value)
1756                 {
1757                         return fsetxattr (fd, name, value, (ulong) value.Length);
1758                 }
1759
1760                 // getxattr(2)
1761                 //        ssize_t getxattr (const char *path, const char *name,
1762                 //                      void *value, size_t size);
1763                 [DllImport (MPH, SetLastError=true,
1764                                 EntryPoint="Mono_Posix_Syscall_getxattr")]
1765                 public static extern long getxattr (
1766                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1767                                 string path, 
1768                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1769                                 string name, byte[] value, ulong size);
1770
1771                 public static long getxattr (string path, string name, byte [] value)
1772                 {
1773                         return getxattr (path, name, value, (ulong) value.Length);
1774                 }
1775
1776                 public static long getxattr (string path, string name, out byte [] value)
1777                 {
1778                         value = null;
1779                         long size = getxattr (path, name, value, 0);
1780                         if (size <= 0)
1781                                 return size;
1782
1783                         value = new byte [size];
1784                         return getxattr (path, name, value, (ulong) size);
1785                 }
1786
1787                 // lgetxattr(2)
1788                 //        ssize_t lgetxattr (const char *path, const char *name,
1789                 //                       void *value, size_t size);
1790                 [DllImport (MPH, SetLastError=true,
1791                                 EntryPoint="Mono_Posix_Syscall_lgetxattr")]
1792                 public static extern long lgetxattr (
1793                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1794                                 string path, 
1795                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1796                                 string name, byte[] value, ulong size);
1797
1798                 public static long lgetxattr (string path, string name, byte [] value)
1799                 {
1800                         return lgetxattr (path, name, value, (ulong) value.Length);
1801                 }
1802
1803                 public static long lgetxattr (string path, string name, out byte [] value)
1804                 {
1805                         value = null;
1806                         long size = lgetxattr (path, name, value, 0);
1807                         if (size <= 0)
1808                                 return size;
1809
1810                         value = new byte [size];
1811                         return lgetxattr (path, name, value, (ulong) size);
1812                 }
1813
1814                 // fgetxattr(2)
1815                 //        ssize_t fgetxattr (int fd, const char *name, void *value, size_t size);
1816                 [DllImport (MPH, SetLastError=true,
1817                                 EntryPoint="Mono_Posix_Syscall_fgetxattr")]
1818                 public static extern long fgetxattr (int fd, 
1819                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1820                                 string name, byte[] value, ulong size);
1821
1822                 public static long fgetxattr (int fd, string name, byte [] value)
1823                 {
1824                         return fgetxattr (fd, name, value, (ulong) value.Length);
1825                 }
1826
1827                 public static long fgetxattr (int fd, string name, out byte [] value)
1828                 {
1829                         value = null;
1830                         long size = fgetxattr (fd, name, value, 0);
1831                         if (size <= 0)
1832                                 return size;
1833
1834                         value = new byte [size];
1835                         return fgetxattr (fd, name, value, (ulong) size);
1836                 }
1837
1838                 // listxattr(2)
1839                 //        ssize_t listxattr (const char *path, char *list, size_t size);
1840                 [DllImport (MPH, SetLastError=true,
1841                                 EntryPoint="Mono_Posix_Syscall_listxattr")]
1842                 public static extern long listxattr (
1843                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1844                                 string path, byte[] list, ulong size);
1845
1846                 // Slight modification: returns 0 on success, negative on error
1847                 public static long listxattr (string path, Encoding encoding, out string [] values)
1848                 {
1849                         values = null;
1850                         long size = listxattr (path, null, 0);
1851                         if (size == 0)
1852                                 values = new string [0];
1853                         if (size <= 0)
1854                                 return (int) size;
1855
1856                         byte[] list = new byte [size];
1857                         long ret = listxattr (path, list, (ulong) size);
1858                         if (ret < 0)
1859                                 return (int) ret;
1860
1861                         GetValues (list, encoding, out values);
1862                         return 0;
1863                 }
1864
1865                 public static long listxattr (string path, out string[] values)
1866                 {
1867                         return listxattr (path, UnixEncoding.Instance, out values);
1868                 }
1869
1870                 private static void GetValues (byte[] list, Encoding encoding, out string[] values)
1871                 {
1872                         int num_values = 0;
1873                         for (int i = 0; i < list.Length; ++i)
1874                                 if (list [i] == 0)
1875                                         ++num_values;
1876
1877                         values = new string [num_values];
1878                         num_values = 0;
1879                         int str_start = 0;
1880                         for (int i = 0; i < list.Length; ++i) {
1881                                 if (list [i] == 0) {
1882                                         values [num_values++] = encoding.GetString (list, str_start, i - str_start);
1883                                         str_start = i+1;
1884                                 }
1885                         }
1886                 }
1887
1888                 // llistxattr(2)
1889                 //        ssize_t llistxattr (const char *path, char *list, size_t size);
1890                 [DllImport (MPH, SetLastError=true,
1891                                 EntryPoint="Mono_Posix_Syscall_llistxattr")]
1892                 public static extern long llistxattr (
1893                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1894                                 string path, byte[] list, ulong size);
1895
1896                 // Slight modification: returns 0 on success, negative on error
1897                 public static long llistxattr (string path, Encoding encoding, out string [] values)
1898                 {
1899                         values = null;
1900                         long size = llistxattr (path, null, 0);
1901                         if (size == 0)
1902                                 values = new string [0];
1903                         if (size <= 0)
1904                                 return (int) size;
1905
1906                         byte[] list = new byte [size];
1907                         long ret = llistxattr (path, list, (ulong) size);
1908                         if (ret < 0)
1909                                 return (int) ret;
1910
1911                         GetValues (list, encoding, out values);
1912                         return 0;
1913                 }
1914
1915                 public static long llistxattr (string path, out string[] values)
1916                 {
1917                         return llistxattr (path, UnixEncoding.Instance, out values);
1918                 }
1919
1920                 // flistxattr(2)
1921                 //        ssize_t flistxattr (int fd, char *list, size_t size);
1922                 [DllImport (MPH, SetLastError=true,
1923                                 EntryPoint="Mono_Posix_Syscall_flistxattr")]
1924                 public static extern long flistxattr (int fd, byte[] list, ulong size);
1925
1926                 // Slight modification: returns 0 on success, negative on error
1927                 public static long flistxattr (int fd, Encoding encoding, out string [] values)
1928                 {
1929                         values = null;
1930                         long size = flistxattr (fd, null, 0);
1931                         if (size == 0)
1932                                 values = new string [0];
1933                         if (size <= 0)
1934                                 return (int) size;
1935
1936                         byte[] list = new byte [size];
1937                         long ret = flistxattr (fd, list, (ulong) size);
1938                         if (ret < 0)
1939                                 return (int) ret;
1940
1941                         GetValues (list, encoding, out values);
1942                         return 0;
1943                 }
1944
1945                 public static long flistxattr (int fd, out string[] values)
1946                 {
1947                         return flistxattr (fd, UnixEncoding.Instance, out values);
1948                 }
1949
1950                 [DllImport (MPH, SetLastError=true,
1951                                 EntryPoint="Mono_Posix_Syscall_removexattr")]
1952                 public static extern int removexattr (
1953                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1954                                 string path, 
1955                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1956                                 string name);
1957
1958                 [DllImport (MPH, SetLastError=true,
1959                                 EntryPoint="Mono_Posix_Syscall_lremovexattr")]
1960                 public static extern int lremovexattr (
1961                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1962                                 string path, 
1963                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1964                                 string name);
1965
1966                 [DllImport (MPH, SetLastError=true,
1967                                 EntryPoint="Mono_Posix_Syscall_fremovexattr")]
1968                 public static extern int fremovexattr (int fd, 
1969                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1970                                 string name);
1971                 #endregion
1972
1973                 #region <dirent.h> Declarations
1974                 //
1975                 // <dirent.h>
1976                 //
1977                 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1978
1979                 [DllImport (LIBC, SetLastError=true)]
1980                 public static extern IntPtr opendir (
1981                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1982                                 string name);
1983
1984                 [DllImport (LIBC, SetLastError=true)]
1985                 public static extern int closedir (IntPtr dir);
1986
1987                 // seekdir(3):
1988                 //    void seekdir (DIR *dir, off_t offset);
1989                 //    Slight modification.  Returns -1 on error, 0 on success.
1990                 [DllImport (MPH, SetLastError=true,
1991                                 EntryPoint="Mono_Posix_Syscall_seekdir")]
1992                 public static extern int seekdir (IntPtr dir, long offset);
1993
1994                 // telldir(3)
1995                 //    off_t telldir(DIR *dir);
1996                 [DllImport (MPH, SetLastError=true,
1997                                 EntryPoint="Mono_Posix_Syscall_telldir")]
1998                 public static extern long telldir (IntPtr dir);
1999
2000                 [DllImport (MPH, SetLastError=true,
2001                                 EntryPoint="Mono_Posix_Syscall_rewinddir")]
2002                 public static extern int rewinddir (IntPtr dir);
2003
2004                 private struct _Dirent {
2005                         [ino_t] public ulong      d_ino;
2006                         [off_t] public long       d_off;
2007                         public ushort             d_reclen;
2008                         public byte               d_type;
2009                         public IntPtr             d_name;
2010                 }
2011
2012                 private static void CopyDirent (Dirent to, ref _Dirent from)
2013                 {
2014                         try {
2015                                 to.d_ino    = from.d_ino;
2016                                 to.d_off    = from.d_off;
2017                                 to.d_reclen = from.d_reclen;
2018                                 to.d_type   = from.d_type;
2019                                 to.d_name   = UnixMarshal.PtrToString (from.d_name);
2020                         }
2021                         finally {
2022                                 Stdlib.free (from.d_name);
2023                                 from.d_name = IntPtr.Zero;
2024                         }
2025                 }
2026
2027                 internal static object readdir_lock = new object ();
2028
2029                 [DllImport (MPH, SetLastError=true,
2030                                 EntryPoint="Mono_Posix_Syscall_readdir")]
2031                 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
2032
2033                 public static Dirent readdir (IntPtr dir)
2034                 {
2035                         _Dirent dentry;
2036                         int r;
2037                         lock (readdir_lock) {
2038                                 r = sys_readdir (dir, out dentry);
2039                         }
2040                         if (r != 0)
2041                                 return null;
2042                         Dirent d = new Dirent ();
2043                         CopyDirent (d, ref dentry);
2044                         return d;
2045                 }
2046
2047                 [DllImport (MPH, SetLastError=true,
2048                                 EntryPoint="Mono_Posix_Syscall_readdir_r")]
2049                 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
2050
2051                 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
2052                 {
2053                         entry.d_ino    = 0;
2054                         entry.d_off    = 0;
2055                         entry.d_reclen = 0;
2056                         entry.d_type   = 0;
2057                         entry.d_name   = null;
2058
2059                         _Dirent _d;
2060                         int r = sys_readdir_r (dirp, out _d, out result);
2061
2062                         if (r == 0 && result != IntPtr.Zero) {
2063                                 CopyDirent (entry, ref _d);
2064                         }
2065
2066                         return r;
2067                 }
2068
2069                 [DllImport (LIBC, SetLastError=true)]
2070                 public static extern int dirfd (IntPtr dir);
2071
2072                 [DllImport (LIBC, SetLastError=true)]
2073                 public static extern IntPtr fdopendir (int fd);
2074                 #endregion
2075
2076                 #region <fcntl.h> Declarations
2077                 //
2078                 // <fcntl.h> -- COMPLETE
2079                 //
2080
2081                 [DllImport (MPH, SetLastError=true, 
2082                                 EntryPoint="Mono_Posix_Syscall_fcntl")]
2083                 public static extern int fcntl (int fd, FcntlCommand cmd);
2084
2085                 [DllImport (MPH, SetLastError=true, 
2086                                 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
2087                 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
2088
2089                 public static int fcntl (int fd, FcntlCommand cmd, DirectoryNotifyFlags arg)
2090                 {
2091                         if (cmd != FcntlCommand.F_NOTIFY) {
2092                                 SetLastError (Errno.EINVAL);
2093                                 return -1;
2094                         }
2095                         long _arg = NativeConvert.FromDirectoryNotifyFlags (arg);
2096                         return fcntl (fd, FcntlCommand.F_NOTIFY, _arg);
2097                 }
2098
2099                 [DllImport (MPH, SetLastError=true, 
2100                                 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
2101                 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
2102
2103                 [DllImport (MPH, SetLastError=true, 
2104                                 EntryPoint="Mono_Posix_Syscall_open")]
2105                 public static extern int open (
2106                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2107                                 string pathname, OpenFlags flags);
2108
2109                 // open(2)
2110                 //    int open(const char *pathname, int flags, mode_t mode);
2111                 [DllImport (MPH, SetLastError=true, 
2112                                 EntryPoint="Mono_Posix_Syscall_open_mode")]
2113                 public static extern int open (
2114                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2115                                 string pathname, OpenFlags flags, FilePermissions mode);
2116
2117                 // creat(2)
2118                 //    int creat(const char *pathname, mode_t mode);
2119                 [DllImport (MPH, SetLastError=true, 
2120                                 EntryPoint="Mono_Posix_Syscall_creat")]
2121                 public static extern int creat (
2122                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2123                                 string pathname, FilePermissions mode);
2124
2125                 // posix_fadvise(2)
2126                 //    int posix_fadvise(int fd, off_t offset, off_t len, int advice);
2127                 [DllImport (MPH, SetLastError=true, 
2128                                 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
2129                 public static extern int posix_fadvise (int fd, long offset, 
2130                         long len, PosixFadviseAdvice advice);
2131
2132                 // posix_fallocate(P)
2133                 //    int posix_fallocate(int fd, off_t offset, size_t len);
2134                 [DllImport (MPH, SetLastError=true, 
2135                                 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
2136                 public static extern int posix_fallocate (int fd, long offset, ulong len);
2137
2138                 [DllImport (LIBC, SetLastError=true, 
2139                                 EntryPoint="openat")]
2140                 private static extern int sys_openat (int dirfd,
2141                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2142                                 string pathname, int flags);
2143
2144                 // openat(2)
2145                 //    int openat(int dirfd, const char *pathname, int flags, mode_t mode);
2146                 [DllImport (LIBC, SetLastError=true, 
2147                                 EntryPoint="openat")]
2148                 private static extern int sys_openat (int dirfd,
2149                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2150                                 string pathname, int flags, uint mode);
2151
2152                 public static int openat (int dirfd, string pathname, OpenFlags flags)
2153                 {
2154                         int _flags = NativeConvert.FromOpenFlags (flags);
2155                         return sys_openat (dirfd, pathname, _flags);
2156                 }
2157
2158                 public static int openat (int dirfd, string pathname, OpenFlags flags, FilePermissions mode)
2159                 {
2160                         int _flags = NativeConvert.FromOpenFlags (flags);
2161                         uint _mode = NativeConvert.FromFilePermissions (mode);
2162                         return sys_openat (dirfd, pathname, _flags, _mode);
2163                 }
2164
2165                 [DllImport (MPH, SetLastError=true, 
2166                                 EntryPoint="Mono_Posix_Syscall_get_at_fdcwd")]
2167                 private static extern int get_at_fdcwd ();
2168
2169                 public static readonly int AT_FDCWD = get_at_fdcwd ();
2170
2171                 #endregion
2172
2173                 #region <fstab.h> Declarations
2174                 //
2175                 // <fstab.h>  -- COMPLETE
2176                 //
2177                 [Map]
2178                 private struct _Fstab {
2179                         public IntPtr fs_spec;
2180                         public IntPtr fs_file;
2181                         public IntPtr fs_vfstype;
2182                         public IntPtr fs_mntops;
2183                         public IntPtr fs_type;
2184                         public int    fs_freq;
2185                         public int    fs_passno;
2186                         public IntPtr _fs_buf_;
2187                 }
2188
2189                 private static void CopyFstab (Fstab to, ref _Fstab from)
2190                 {
2191                         try {
2192                                 to.fs_spec     = UnixMarshal.PtrToString (from.fs_spec);
2193                                 to.fs_file     = UnixMarshal.PtrToString (from.fs_file);
2194                                 to.fs_vfstype  = UnixMarshal.PtrToString (from.fs_vfstype);
2195                                 to.fs_mntops   = UnixMarshal.PtrToString (from.fs_mntops);
2196                                 to.fs_type     = UnixMarshal.PtrToString (from.fs_type);
2197                                 to.fs_freq     = from.fs_freq;
2198                                 to.fs_passno   = from.fs_passno;
2199                         }
2200                         finally {
2201                                 Stdlib.free (from._fs_buf_);
2202                                 from._fs_buf_ = IntPtr.Zero;
2203                         }
2204                 }
2205
2206                 internal static object fstab_lock = new object ();
2207
2208                 [DllImport (MPH, SetLastError=true,
2209                                 EntryPoint="Mono_Posix_Syscall_endfsent")]
2210                 private static extern int sys_endfsent ();
2211
2212                 public static int endfsent ()
2213                 {
2214                         lock (fstab_lock) {
2215                                 return sys_endfsent ();
2216                         }
2217                 }
2218
2219                 [DllImport (MPH, SetLastError=true,
2220                                 EntryPoint="Mono_Posix_Syscall_getfsent")]
2221                 private static extern int sys_getfsent (out _Fstab fs);
2222
2223                 public static Fstab getfsent ()
2224                 {
2225                         _Fstab fsbuf;
2226                         int r;
2227                         lock (fstab_lock) {
2228                                 r = sys_getfsent (out fsbuf);
2229                         }
2230                         if (r != 0)
2231                                 return null;
2232                         Fstab fs = new Fstab ();
2233                         CopyFstab (fs, ref fsbuf);
2234                         return fs;
2235                 }
2236
2237                 [DllImport (MPH, SetLastError=true,
2238                                 EntryPoint="Mono_Posix_Syscall_getfsfile")]
2239                 private static extern int sys_getfsfile (
2240                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2241                                 string mount_point, out _Fstab fs);
2242
2243                 public static Fstab getfsfile (string mount_point)
2244                 {
2245                         _Fstab fsbuf;
2246                         int r;
2247                         lock (fstab_lock) {
2248                                 r = sys_getfsfile (mount_point, out fsbuf);
2249                         }
2250                         if (r != 0)
2251                                 return null;
2252                         Fstab fs = new Fstab ();
2253                         CopyFstab (fs, ref fsbuf);
2254                         return fs;
2255                 }
2256
2257                 [DllImport (MPH, SetLastError=true,
2258                                 EntryPoint="Mono_Posix_Syscall_getfsspec")]
2259                 private static extern int sys_getfsspec (
2260                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2261                                 string special_file, out _Fstab fs);
2262
2263                 public static Fstab getfsspec (string special_file)
2264                 {
2265                         _Fstab fsbuf;
2266                         int r;
2267                         lock (fstab_lock) {
2268                                 r = sys_getfsspec (special_file, out fsbuf);
2269                         }
2270                         if (r != 0)
2271                                 return null;
2272                         Fstab fs = new Fstab ();
2273                         CopyFstab (fs, ref fsbuf);
2274                         return fs;
2275                 }
2276
2277                 [DllImport (MPH, SetLastError=true,
2278                                 EntryPoint="Mono_Posix_Syscall_setfsent")]
2279                 private static extern int sys_setfsent ();
2280
2281                 public static int setfsent ()
2282                 {
2283                         lock (fstab_lock) {
2284                                 return sys_setfsent ();
2285                         }
2286                 }
2287
2288                 #endregion
2289
2290                 #region <grp.h> Declarations
2291                 //
2292                 // <grp.h>
2293                 //
2294                 // TODO: putgrent(3), fgetgrent_r(), initgroups(3)
2295
2296                 // getgrouplist(2)
2297                 [DllImport (LIBC, SetLastError=true, EntryPoint="getgrouplist")]
2298                 private static extern int sys_getgrouplist (string user, uint grp, uint [] groups,ref int ngroups);
2299
2300                 public static Group [] getgrouplist (string username)
2301                 {
2302                         if (username == null)
2303                                 throw new ArgumentNullException ("username");
2304                         if (username.Trim () == "")
2305                                 throw new ArgumentException ("Username cannot be empty", "username");
2306                         // Syscall to getpwnam to retrieve user uid
2307                         Passwd pw = Syscall.getpwnam (username);
2308                         if (pw == null)
2309                                 throw new ArgumentException (string.Format ("User {0} does not exist", username), "username");
2310                         return getgrouplist (pw);
2311                 }
2312
2313                 public static Group [] getgrouplist (Passwd user)
2314                 {
2315                         if (user == null)
2316                                 throw new ArgumentNullException ("user");
2317                         // initializing ngroups by 16 to get the group count
2318                         int ngroups = 8;
2319                         int res = -1;
2320                         // allocating buffer to store group uid's
2321                         uint [] groups=null;
2322                         do {
2323                                 Array.Resize (ref groups, ngroups*=2);
2324                                 res = sys_getgrouplist (user.pw_name, user.pw_gid, groups, ref ngroups);
2325                         }
2326                         while (res == -1);
2327                         List<Group> result = new List<Group> ();
2328                         Group gr = null;
2329                         for (int i = 0; i < res; i++) {
2330                                 gr = Syscall.getgrgid (groups [i]);
2331                                 if (gr != null)
2332                                         result.Add (gr);
2333                         }
2334                         return result.ToArray ();
2335                 }
2336
2337                 // setgroups(2)
2338                 //    int setgroups (size_t size, const gid_t *list);
2339                 [DllImport (MPH, SetLastError=true,
2340                                 EntryPoint="Mono_Posix_Syscall_setgroups")]
2341                 public static extern int setgroups (ulong size, uint[] list);
2342
2343                 public static int setgroups (uint [] list)
2344                 {
2345                         return setgroups ((ulong) list.Length, list);
2346                 }
2347
2348                 [Map]
2349                 private struct _Group
2350                 {
2351                         public IntPtr           gr_name;
2352                         public IntPtr           gr_passwd;
2353                         [gid_t] public uint     gr_gid;
2354                         public int              _gr_nmem_;
2355                         public IntPtr           gr_mem;
2356                         public IntPtr           _gr_buf_;
2357                 }
2358
2359                 private static void CopyGroup (Group to, ref _Group from)
2360                 {
2361                         try {
2362                                 to.gr_gid    = from.gr_gid;
2363                                 to.gr_name   = UnixMarshal.PtrToString (from.gr_name);
2364                                 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
2365                                 to.gr_mem    = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
2366                         }
2367                         finally {
2368                                 Stdlib.free (from.gr_mem);
2369                                 Stdlib.free (from._gr_buf_);
2370                                 from.gr_mem   = IntPtr.Zero;
2371                                 from._gr_buf_ = IntPtr.Zero;
2372                         }
2373                 }
2374
2375                 internal static object grp_lock = new object ();
2376
2377                 [DllImport (MPH, SetLastError=true,
2378                                 EntryPoint="Mono_Posix_Syscall_getgrnam")]
2379                 private static extern int sys_getgrnam (string name, out _Group group);
2380
2381                 public static Group getgrnam (string name)
2382                 {
2383                         _Group group;
2384                         int r;
2385                         lock (grp_lock) {
2386                                 r = sys_getgrnam (name, out group);
2387                         }
2388                         if (r != 0)
2389                                 return null;
2390                         Group gr = new Group ();
2391                         CopyGroup (gr, ref group);
2392                         return gr;
2393                 }
2394
2395                 // getgrgid(3)
2396                 //    struct group *getgrgid(gid_t gid);
2397                 [DllImport (MPH, SetLastError=true,
2398                                 EntryPoint="Mono_Posix_Syscall_getgrgid")]
2399                 private static extern int sys_getgrgid (uint uid, out _Group group);
2400
2401                 public static Group getgrgid (uint uid)
2402                 {
2403                         _Group group;
2404                         int r;
2405                         lock (grp_lock) {
2406                                 r = sys_getgrgid (uid, out group);
2407                         }
2408                         if (r != 0)
2409                                 return null;
2410                         Group gr = new Group ();
2411                         CopyGroup (gr, ref group);
2412                         return gr;
2413                 }
2414
2415                 [DllImport (MPH, SetLastError=true,
2416                                 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
2417                 private static extern int sys_getgrnam_r (
2418                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2419                                 string name, out _Group grbuf, out IntPtr grbufp);
2420
2421                 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
2422                 {
2423                         grbufp = null;
2424                         _Group group;
2425                         IntPtr _grbufp;
2426                         int r = sys_getgrnam_r (name, out group, out _grbufp);
2427                         if (r == 0 && _grbufp != IntPtr.Zero) {
2428                                 CopyGroup (grbuf, ref group);
2429                                 grbufp = grbuf;
2430                         }
2431                         return r;
2432                 }
2433
2434                 // getgrgid_r(3)
2435                 //    int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
2436                 //        size_t buflen, struct group **gbufp);
2437                 [DllImport (MPH, SetLastError=true,
2438                                 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
2439                 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
2440
2441                 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
2442                 {
2443                         grbufp = null;
2444                         _Group group;
2445                         IntPtr _grbufp;
2446                         int r = sys_getgrgid_r (uid, out group, out _grbufp);
2447                         if (r == 0 && _grbufp != IntPtr.Zero) {
2448                                 CopyGroup (grbuf, ref group);
2449                                 grbufp = grbuf;
2450                         }
2451                         return r;
2452                 }
2453
2454                 [DllImport (MPH, SetLastError=true,
2455                                 EntryPoint="Mono_Posix_Syscall_getgrent")]
2456                 private static extern int sys_getgrent (out _Group grbuf);
2457
2458                 public static Group getgrent ()
2459                 {
2460                         _Group group;
2461                         int r;
2462                         lock (grp_lock) {
2463                                 r = sys_getgrent (out group);
2464                         }
2465                         if (r != 0)
2466                                 return null;
2467                         Group gr = new Group();
2468                         CopyGroup (gr, ref group);
2469                         return gr;
2470                 }
2471
2472                 [DllImport (MPH, SetLastError=true, 
2473                                 EntryPoint="Mono_Posix_Syscall_setgrent")]
2474                 private static extern int sys_setgrent ();
2475
2476                 public static int setgrent ()
2477                 {
2478                         lock (grp_lock) {
2479                                 return sys_setgrent ();
2480                         }
2481                 }
2482
2483                 [DllImport (MPH, SetLastError=true, 
2484                                 EntryPoint="Mono_Posix_Syscall_endgrent")]
2485                 private static extern int sys_endgrent ();
2486
2487                 public static int endgrent ()
2488                 {
2489                         lock (grp_lock) {
2490                                 return sys_endgrent ();
2491                         }
2492                 }
2493
2494                 [DllImport (MPH, SetLastError=true,
2495                                 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
2496                 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
2497
2498                 public static Group fgetgrent (IntPtr stream)
2499                 {
2500                         _Group group;
2501                         int r;
2502                         lock (grp_lock) {
2503                                 r = sys_fgetgrent (stream, out group);
2504                         }
2505                         if (r != 0)
2506                                 return null;
2507                         Group gr = new Group ();
2508                         CopyGroup (gr, ref group);
2509                         return gr;
2510                 }
2511                 #endregion
2512
2513                 #region <pwd.h> Declarations
2514                 //
2515                 // <pwd.h>
2516                 //
2517                 // TODO: putpwent(3), fgetpwent_r()
2518                 //
2519                 // SKIPPING: getpw(3): it's dangerous.  Use getpwuid(3) instead.
2520
2521                 [Map]
2522                 private struct _Passwd
2523                 {
2524                         public IntPtr           pw_name;
2525                         public IntPtr           pw_passwd;
2526                         [uid_t] public uint     pw_uid;
2527                         [gid_t] public uint     pw_gid;
2528                         public IntPtr           pw_gecos;
2529                         public IntPtr           pw_dir;
2530                         public IntPtr           pw_shell;
2531                         public IntPtr           _pw_buf_;
2532                 }
2533
2534                 private static void CopyPasswd (Passwd to, ref _Passwd from)
2535                 {
2536                         try {
2537                                 to.pw_name   = UnixMarshal.PtrToString (from.pw_name);
2538                                 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
2539                                 to.pw_uid    = from.pw_uid;
2540                                 to.pw_gid    = from.pw_gid;
2541                                 to.pw_gecos  = UnixMarshal.PtrToString (from.pw_gecos);
2542                                 to.pw_dir    = UnixMarshal.PtrToString (from.pw_dir);
2543                                 to.pw_shell  = UnixMarshal.PtrToString (from.pw_shell);
2544                         }
2545                         finally {
2546                                 Stdlib.free (from._pw_buf_);
2547                                 from._pw_buf_ = IntPtr.Zero;
2548                         }
2549                 }
2550
2551                 internal static object pwd_lock = new object ();
2552
2553                 [DllImport (MPH, SetLastError=true,
2554                                 EntryPoint="Mono_Posix_Syscall_getpwnam")]
2555                 private static extern int sys_getpwnam (string name, out _Passwd passwd);
2556
2557                 public static Passwd getpwnam (string name)
2558                 {
2559                         _Passwd passwd;
2560                         int r;
2561                         lock (pwd_lock) {
2562                                 r = sys_getpwnam (name, out passwd);
2563                         }
2564                         if (r != 0)
2565                                 return null;
2566                         Passwd pw = new Passwd ();
2567                         CopyPasswd (pw, ref passwd);
2568                         return pw;
2569                 }
2570
2571                 // getpwuid(3)
2572                 //    struct passwd *getpwnuid(uid_t uid);
2573                 [DllImport (MPH, SetLastError=true,
2574                                 EntryPoint="Mono_Posix_Syscall_getpwuid")]
2575                 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
2576
2577                 public static Passwd getpwuid (uint uid)
2578                 {
2579                         _Passwd passwd;
2580                         int r;
2581                         lock (pwd_lock) {
2582                                 r = sys_getpwuid (uid, out passwd);
2583                         }
2584                         if (r != 0)
2585                                 return null;
2586                         Passwd pw = new Passwd ();
2587                         CopyPasswd (pw, ref passwd);
2588                         return pw;
2589                 }
2590
2591                 [DllImport (MPH, SetLastError=true,
2592                                 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
2593                 private static extern int sys_getpwnam_r (
2594                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2595                                 string name, out _Passwd pwbuf, out IntPtr pwbufp);
2596
2597                 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
2598                 {
2599                         pwbufp = null;
2600                         _Passwd passwd;
2601                         IntPtr _pwbufp;
2602                         int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
2603                         if (r == 0 && _pwbufp != IntPtr.Zero) {
2604                                 CopyPasswd (pwbuf, ref passwd);
2605                                 pwbufp = pwbuf;
2606                         }
2607                         return r;
2608                 }
2609
2610                 // getpwuid_r(3)
2611                 //    int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
2612                 //        buflen, struct passwd **pwbufp);
2613                 [DllImport (MPH, SetLastError=true,
2614                                 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
2615                 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
2616
2617                 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
2618                 {
2619                         pwbufp = null;
2620                         _Passwd passwd;
2621                         IntPtr _pwbufp;
2622                         int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
2623                         if (r == 0 && _pwbufp != IntPtr.Zero) {
2624                                 CopyPasswd (pwbuf, ref passwd);
2625                                 pwbufp = pwbuf;
2626                         }
2627                         return r;
2628                 }
2629
2630                 [DllImport (MPH, SetLastError=true,
2631                                 EntryPoint="Mono_Posix_Syscall_getpwent")]
2632                 private static extern int sys_getpwent (out _Passwd pwbuf);
2633
2634                 public static Passwd getpwent ()
2635                 {
2636                         _Passwd passwd;
2637                         int r;
2638                         lock (pwd_lock) {
2639                                 r = sys_getpwent (out passwd);
2640                         }
2641                         if (r != 0)
2642                                 return null;
2643                         Passwd pw = new Passwd ();
2644                         CopyPasswd (pw, ref passwd);
2645                         return pw;
2646                 }
2647
2648                 [DllImport (MPH, SetLastError=true, 
2649                                 EntryPoint="Mono_Posix_Syscall_setpwent")]
2650                 private static extern int sys_setpwent ();
2651
2652                 public static int setpwent ()
2653                 {
2654                         lock (pwd_lock) {
2655                                 return sys_setpwent ();
2656                         }
2657                 }
2658
2659                 [DllImport (MPH, SetLastError=true, 
2660                                 EntryPoint="Mono_Posix_Syscall_endpwent")]
2661                 private static extern int sys_endpwent ();
2662
2663                 public static int endpwent ()
2664                 {
2665                         lock (pwd_lock) {
2666                                 return sys_endpwent ();
2667                         }
2668                 }
2669
2670                 [DllImport (MPH, SetLastError=true,
2671                                 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
2672                 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
2673
2674                 public static Passwd fgetpwent (IntPtr stream)
2675                 {
2676                         _Passwd passwd;
2677                         int r;
2678                         lock (pwd_lock) {
2679                                 r = sys_fgetpwent (stream, out passwd);
2680                         }
2681                         if (r != 0)
2682                                 return null;
2683                         Passwd pw = new Passwd ();
2684                         CopyPasswd (pw, ref passwd);
2685                         return pw;
2686                 }
2687                 #endregion
2688
2689                 #region <signal.h> Declarations
2690                 //
2691                 // <signal.h>
2692                 //
2693                 [DllImport (MPH, SetLastError=true,
2694                                 EntryPoint="Mono_Posix_Syscall_psignal")]
2695                 private static extern int psignal (int sig, string s);
2696
2697                 public static int psignal (Signum sig, string s)
2698                 {
2699                         int signum = NativeConvert.FromSignum (sig);
2700                         return psignal (signum, s);
2701                 }
2702
2703                 // kill(2)
2704                 //    int kill(pid_t pid, int sig);
2705                 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
2706                 private static extern int sys_kill (int pid, int sig);
2707
2708                 public static int kill (int pid, Signum sig)
2709                 {
2710                         int _sig = NativeConvert.FromSignum (sig);
2711                         return sys_kill (pid, _sig);
2712                 }
2713
2714                 private static object signal_lock = new object ();
2715
2716                 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
2717                 private static extern IntPtr sys_strsignal (int sig);
2718
2719                 public static string strsignal (Signum sig)
2720                 {
2721                         int s = NativeConvert.FromSignum (sig);
2722                         lock (signal_lock) {
2723                                 IntPtr r = sys_strsignal (s);
2724                                 return UnixMarshal.PtrToString (r);
2725                         }
2726                 }
2727
2728                 // TODO: sigaction(2)
2729                 // TODO: sigsuspend(2)
2730                 // TODO: sigpending(2)
2731
2732                 #endregion
2733
2734                 #region <stdio.h> Declarations
2735                 //
2736                 // <stdio.h>
2737                 //
2738                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
2739                 private static extern int _L_ctermid ();
2740
2741                 public static readonly int L_ctermid = _L_ctermid ();
2742
2743                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
2744                 private static extern int _L_cuserid ();
2745
2746                 public static readonly int L_cuserid = _L_cuserid ();
2747
2748                 internal static object getlogin_lock = new object ();
2749
2750                 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
2751                 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
2752
2753                 [Obsolete ("\"Nobody knows precisely what cuserid() does... " + 
2754                                 "DO NOT USE cuserid().\n" +
2755                                 "`string' must hold L_cuserid characters.  Use getlogin_r instead.")]
2756                 public static string cuserid (StringBuilder @string)
2757                 {
2758                         if (@string.Capacity < L_cuserid) {
2759                                 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
2760                         }
2761                         lock (getlogin_lock) {
2762                                 IntPtr r = sys_cuserid (@string);
2763                                 return UnixMarshal.PtrToString (r);
2764                         }
2765                 }
2766
2767                 [DllImport (LIBC, SetLastError=true)]
2768                 public static extern int renameat (int olddirfd,
2769                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2770                                 string oldpath, int newdirfd,
2771                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2772                                 string newpath);
2773                 #endregion
2774
2775                 #region <stdlib.h> Declarations
2776                 //
2777                 // <stdlib.h>
2778                 //
2779                 [DllImport (LIBC, SetLastError=true)]
2780                 public static extern int mkstemp (StringBuilder template);
2781
2782                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdtemp")]
2783                 private static extern IntPtr sys_mkdtemp (StringBuilder template);
2784
2785                 public static StringBuilder mkdtemp (StringBuilder template)
2786                 {
2787                         if (sys_mkdtemp (template) == IntPtr.Zero)
2788                                 return null;
2789                         return template;
2790                 }
2791
2792                 [DllImport (LIBC, SetLastError=true)]
2793                 public static extern int ttyslot ();
2794
2795                 [Obsolete ("This is insecure and should not be used", true)]
2796                 public static int setkey (string key)
2797                 {
2798                         throw new SecurityException ("crypt(3) has been broken.  Use something more secure.");
2799                 }
2800
2801                 #endregion
2802
2803                 #region <string.h> Declarations
2804                 //
2805                 // <string.h>
2806                 //
2807
2808                 // strerror_r(3)
2809                 //    int strerror_r(int errnum, char *buf, size_t n);
2810                 [DllImport (MPH, SetLastError=true, 
2811                                 EntryPoint="Mono_Posix_Syscall_strerror_r")]
2812                 private static extern int sys_strerror_r (int errnum, 
2813                                 [Out] StringBuilder buf, ulong n);
2814
2815                 public static int strerror_r (Errno errnum, StringBuilder buf, ulong n)
2816                 {
2817                         int e = NativeConvert.FromErrno (errnum);
2818                         return sys_strerror_r (e, buf, n);
2819                 }
2820
2821                 public static int strerror_r (Errno errnum, StringBuilder buf)
2822                 {
2823                         return strerror_r (errnum, buf, (ulong) buf.Capacity);
2824                 }
2825
2826                 #endregion
2827
2828                 #region <sys/epoll.h> Declarations
2829
2830                 public static int epoll_create (int size)
2831                 {
2832                         return sys_epoll_create (size);
2833                 }
2834
2835                 public static int epoll_create (EpollFlags flags)
2836                 {
2837                         return sys_epoll_create1 (flags);
2838                 }
2839
2840                 public static int epoll_ctl (int epfd, EpollOp op, int fd, EpollEvents events)
2841                 {
2842                         EpollEvent ee = new EpollEvent ();
2843                         ee.events = events;
2844                         ee.fd = fd;
2845
2846                         return epoll_ctl (epfd, op, fd, ref ee);
2847                 }
2848
2849                 public static int epoll_wait (int epfd, EpollEvent [] events, int max_events, int timeout)
2850                 {
2851                         if (events.Length < max_events)
2852                                 throw new ArgumentOutOfRangeException ("events", "Must refer to at least 'max_events' elements.");
2853
2854                         return sys_epoll_wait (epfd, events, max_events, timeout);
2855                 }
2856
2857                 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_create")]
2858                 private static extern int sys_epoll_create (int size);
2859
2860                 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_create1")]
2861                 private static extern int sys_epoll_create1 (EpollFlags flags);
2862
2863                 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_ctl")]
2864                 public static extern int epoll_ctl (int epfd, EpollOp op, int fd, ref EpollEvent ee);
2865
2866                 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_wait")]
2867                 private static extern int sys_epoll_wait (int epfd, EpollEvent [] ee, int maxevents, int timeout);
2868                 #endregion
2869                 
2870                 #region <sys/mman.h> Declarations
2871                 //
2872                 // <sys/mman.h>
2873                 //
2874
2875                 // posix_madvise(P)
2876                 //    int posix_madvise(void *addr, size_t len, int advice);
2877                 [DllImport (MPH, SetLastError=true, 
2878                                 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
2879                 public static extern int posix_madvise (IntPtr addr, ulong len, 
2880                         PosixMadviseAdvice advice);
2881
2882                 public static readonly IntPtr MAP_FAILED = unchecked((IntPtr)(-1));
2883
2884                 [DllImport (MPH, SetLastError=true, 
2885                                 EntryPoint="Mono_Posix_Syscall_mmap")]
2886                 public static extern IntPtr mmap (IntPtr start, ulong length, 
2887                                 MmapProts prot, MmapFlags flags, int fd, long offset);
2888
2889                 [DllImport (MPH, SetLastError=true, 
2890                                 EntryPoint="Mono_Posix_Syscall_munmap")]
2891                 public static extern int munmap (IntPtr start, ulong length);
2892
2893                 [DllImport (MPH, SetLastError=true, 
2894                                 EntryPoint="Mono_Posix_Syscall_mprotect")]
2895                 public static extern int mprotect (IntPtr start, ulong len, MmapProts prot);
2896
2897                 [DllImport (MPH, SetLastError=true, 
2898                                 EntryPoint="Mono_Posix_Syscall_msync")]
2899                 public static extern int msync (IntPtr start, ulong len, MsyncFlags flags);
2900
2901                 [DllImport (MPH, SetLastError=true, 
2902                                 EntryPoint="Mono_Posix_Syscall_mlock")]
2903                 public static extern int mlock (IntPtr start, ulong len);
2904
2905                 [DllImport (MPH, SetLastError=true, 
2906                                 EntryPoint="Mono_Posix_Syscall_munlock")]
2907                 public static extern int munlock (IntPtr start, ulong len);
2908
2909                 [DllImport (LIBC, SetLastError=true, EntryPoint="mlockall")]
2910                 private static extern int sys_mlockall (int flags);
2911
2912                 public static int mlockall (MlockallFlags flags)
2913                 {
2914                         int _flags = NativeConvert.FromMlockallFlags (flags);
2915                         return sys_mlockall (_flags);
2916                 }
2917
2918                 [DllImport (LIBC, SetLastError=true)]
2919                 public static extern int munlockall ();
2920
2921                 [DllImport (MPH, SetLastError=true, 
2922                                 EntryPoint="Mono_Posix_Syscall_mremap")]
2923                 public static extern IntPtr mremap (IntPtr old_address, ulong old_size, 
2924                                 ulong new_size, MremapFlags flags);
2925
2926                 [DllImport (MPH, SetLastError=true, 
2927                                 EntryPoint="Mono_Posix_Syscall_mincore")]
2928                 public static extern int mincore (IntPtr start, ulong length, byte[] vec);
2929
2930                 [DllImport (MPH, SetLastError=true, 
2931                                 EntryPoint="Mono_Posix_Syscall_remap_file_pages")]
2932                 public static extern int remap_file_pages (IntPtr start, ulong size,
2933                                 MmapProts prot, long pgoff, MmapFlags flags);
2934
2935                 #endregion
2936
2937                 #region <sys/poll.h> Declarations
2938                 //
2939                 // <sys/poll.h> -- COMPLETE
2940                 //
2941
2942                 private struct _pollfd {
2943                         public int fd;
2944                         public short events;
2945                         public short revents;
2946                 }
2947
2948                 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
2949                 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
2950
2951                 public static int poll (Pollfd [] fds, uint nfds, int timeout)
2952                 {
2953                         if (fds.Length < nfds)
2954                                 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
2955
2956                         _pollfd[] send = new _pollfd[nfds];
2957
2958                         for (int i = 0; i < send.Length; i++) {
2959                                 send [i].fd     = fds [i].fd;
2960                                 send [i].events = NativeConvert.FromPollEvents (fds [i].events);
2961                         }
2962
2963                         int r = sys_poll (send, nfds, timeout);
2964
2965                         for (int i = 0; i < send.Length; i++) {
2966                                 fds [i].revents = NativeConvert.ToPollEvents (send [i].revents);
2967                         }
2968
2969                         return r;
2970                 }
2971
2972                 public static int poll (Pollfd [] fds, int timeout)
2973                 {
2974                         return poll (fds, (uint) fds.Length, timeout);
2975                 }
2976
2977                 //
2978                 // <sys/ptrace.h>
2979                 //
2980
2981                 // TODO: ptrace(2)
2982
2983                 //
2984                 // <sys/resource.h>
2985                 //
2986
2987                 // TODO: setrlimit(2)
2988                 // TODO: getrlimit(2)
2989                 // TODO: getrusage(2)
2990
2991                 #endregion
2992
2993                 #region <sys/sendfile.h> Declarations
2994                 //
2995                 // <sys/sendfile.h> -- COMPLETE
2996                 //
2997
2998                 [DllImport (MPH, SetLastError=true,
2999                                 EntryPoint="Mono_Posix_Syscall_sendfile")]
3000                 public static extern long sendfile (int out_fd, int in_fd, 
3001                                 ref long offset, ulong count);
3002
3003                 #endregion
3004
3005                 #region <sys/stat.h> Declarations
3006                 //
3007                 // <sys/stat.h>  -- COMPLETE
3008                 //
3009                 [DllImport (MPH, SetLastError=true, 
3010                                 EntryPoint="Mono_Posix_Syscall_stat")]
3011                 public static extern int stat (
3012                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3013                                 string file_name, out Stat buf);
3014
3015                 [DllImport (MPH, SetLastError=true, 
3016                                 EntryPoint="Mono_Posix_Syscall_fstat")]
3017                 public static extern int fstat (int filedes, out Stat buf);
3018
3019                 [DllImport (MPH, SetLastError=true, 
3020                                 EntryPoint="Mono_Posix_Syscall_lstat")]
3021                 public static extern int lstat (
3022                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3023                                 string file_name, out Stat buf);
3024
3025                 // TODO:
3026                 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
3027                 // All take FilePermissions
3028
3029                 // chmod(2)
3030                 //    int chmod(const char *path, mode_t mode);
3031                 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
3032                 private static extern int sys_chmod (
3033                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3034                                 string path, uint mode);
3035
3036                 public static int chmod (string path, FilePermissions mode)
3037                 {
3038                         uint _mode = NativeConvert.FromFilePermissions (mode);
3039                         return sys_chmod (path, _mode);
3040                 }
3041
3042                 // fchmod(2)
3043                 //    int chmod(int filedes, mode_t mode);
3044                 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
3045                 private static extern int sys_fchmod (int filedes, uint mode);
3046
3047                 public static int fchmod (int filedes, FilePermissions mode)
3048                 {
3049                         uint _mode = NativeConvert.FromFilePermissions (mode);
3050                         return sys_fchmod (filedes, _mode);
3051                 }
3052
3053                 // umask(2)
3054                 //    mode_t umask(mode_t mask);
3055                 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
3056                 private static extern uint sys_umask (uint mask);
3057
3058                 public static FilePermissions umask (FilePermissions mask)
3059                 {
3060                         uint _mask = NativeConvert.FromFilePermissions (mask);
3061                         uint r = sys_umask (_mask);
3062                         return NativeConvert.ToFilePermissions (r);
3063                 }
3064
3065                 // mkdir(2)
3066                 //    int mkdir(const char *pathname, mode_t mode);
3067                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
3068                 private static extern int sys_mkdir (
3069                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3070                                 string oldpath, uint mode);
3071
3072                 public static int mkdir (string oldpath, FilePermissions mode)
3073                 {
3074                         uint _mode = NativeConvert.FromFilePermissions (mode);
3075                         return sys_mkdir (oldpath, _mode);
3076                 }
3077
3078                 // mknod(2)
3079                 //    int mknod (const char *pathname, mode_t mode, dev_t dev);
3080                 [DllImport (MPH, SetLastError=true,
3081                                 EntryPoint="Mono_Posix_Syscall_mknod")]
3082                 public static extern int mknod (
3083                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3084                                 string pathname, FilePermissions mode, ulong dev);
3085
3086                 // mkfifo(3)
3087                 //    int mkfifo(const char *pathname, mode_t mode);
3088                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
3089                 private static extern int sys_mkfifo (
3090                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3091                                 string pathname, uint mode);
3092
3093                 public static int mkfifo (string pathname, FilePermissions mode)
3094                 {
3095                         uint _mode = NativeConvert.FromFilePermissions (mode);
3096                         return sys_mkfifo (pathname, _mode);
3097                 }
3098
3099                 // fchmodat(2)
3100                 //    int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
3101                 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmodat")]
3102                 private static extern int sys_fchmodat (int dirfd,
3103                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3104                                 string pathname, uint mode, int flags);
3105
3106                 public static int fchmodat (int dirfd, string pathname, FilePermissions mode, AtFlags flags)
3107                 {
3108                         uint _mode = NativeConvert.FromFilePermissions (mode);
3109                         int _flags = NativeConvert.FromAtFlags (flags);
3110                         return sys_fchmodat (dirfd, pathname, _mode, _flags);
3111                 }
3112
3113                 [DllImport (MPH, SetLastError=true, 
3114                                 EntryPoint="Mono_Posix_Syscall_fstatat")]
3115                 public static extern int fstatat (int dirfd,
3116                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3117                                 string file_name, out Stat buf, AtFlags flags);
3118
3119                 [DllImport (MPH, SetLastError=true, 
3120                                 EntryPoint="Mono_Posix_Syscall_get_utime_now")]
3121                 private static extern long get_utime_now ();
3122
3123                 [DllImport (MPH, SetLastError=true, 
3124                                 EntryPoint="Mono_Posix_Syscall_get_utime_omit")]
3125                 private static extern long get_utime_omit ();
3126
3127                 public static readonly long UTIME_NOW = get_utime_now ();
3128
3129                 public static readonly long UTIME_OMIT = get_utime_omit ();
3130
3131                 [DllImport (MPH, SetLastError=true, 
3132                                 EntryPoint="Mono_Posix_Syscall_futimens")]
3133                 private static extern int sys_futimens (int fd, Timespec[] times);
3134
3135                 public static int futimens (int fd, Timespec[] times)
3136                 {
3137                         if (times != null && times.Length != 2) {
3138                                 SetLastError (Errno.EINVAL);
3139                                 return -1;
3140                         }
3141                         return sys_futimens (fd, times);
3142                 }
3143
3144                 [DllImport (MPH, SetLastError=true, 
3145                                 EntryPoint="Mono_Posix_Syscall_utimensat")]
3146                 private static extern int sys_utimensat (int dirfd,
3147                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3148                                 string pathname, Timespec[] times, int flags);
3149
3150                 public static int utimensat (int dirfd, string pathname, Timespec[] times, AtFlags flags)
3151                 {
3152                         if (times != null && times.Length != 2) {
3153                                 SetLastError (Errno.EINVAL);
3154                                 return -1;
3155                         }
3156                         int _flags = NativeConvert.FromAtFlags (flags);
3157                         return sys_utimensat (dirfd, pathname, times, _flags);
3158                 }
3159
3160                 // mkdirat(2)
3161                 //    int mkdirat(int dirfd, const char *pathname, mode_t mode);
3162                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdirat")]
3163                 private static extern int sys_mkdirat (int dirfd,
3164                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3165                                 string oldpath, uint mode);
3166
3167                 public static int mkdirat (int dirfd, string oldpath, FilePermissions mode)
3168                 {
3169                         uint _mode = NativeConvert.FromFilePermissions (mode);
3170                         return sys_mkdirat (dirfd, oldpath, _mode);
3171                 }
3172
3173                 // mknodat(2)
3174                 //    int mknodat (int dirfd, const char *pathname, mode_t mode, dev_t dev);
3175                 [DllImport (MPH, SetLastError=true,
3176                                 EntryPoint="Mono_Posix_Syscall_mknodat")]
3177                 public static extern int mknodat (int dirfd,
3178                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3179                                 string pathname, FilePermissions mode, ulong dev);
3180
3181                 // mkfifoat(3)
3182                 //    int mkfifoat(int dirfd, const char *pathname, mode_t mode);
3183                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifoat")]
3184                 private static extern int sys_mkfifoat (int dirfd,
3185                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3186                                 string pathname, uint mode);
3187
3188                 public static int mkfifoat (int dirfd, string pathname, FilePermissions mode)
3189                 {
3190                         uint _mode = NativeConvert.FromFilePermissions (mode);
3191                         return sys_mkfifoat (dirfd, pathname, _mode);
3192                 }
3193                 #endregion
3194
3195                 #region <sys/stat.h> Declarations
3196                 //
3197                 // <sys/statvfs.h>
3198                 //
3199
3200                 [DllImport (MPH, SetLastError=true,
3201                                 EntryPoint="Mono_Posix_Syscall_statvfs")]
3202                 public static extern int statvfs (
3203                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3204                                 string path, out Statvfs buf);
3205
3206                 [DllImport (MPH, SetLastError=true,
3207                                 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
3208                 public static extern int fstatvfs (int fd, out Statvfs buf);
3209
3210                 #endregion
3211
3212                 #region <sys/time.h> Declarations
3213                 //
3214                 // <sys/time.h>
3215                 //
3216                 // TODO: adjtime(), getitimer(2), setitimer(2)
3217
3218                 [DllImport (MPH, SetLastError=true, 
3219                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3220                 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
3221
3222                 [DllImport (MPH, SetLastError=true,
3223                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3224                 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
3225
3226                 public static int gettimeofday (out Timeval tv)
3227                 {
3228                         return gettimeofday (out tv, IntPtr.Zero);
3229                 }
3230
3231                 [DllImport (MPH, SetLastError=true,
3232                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3233                 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
3234
3235                 public static int gettimeofday (out Timezone tz)
3236                 {
3237                         return gettimeofday (IntPtr.Zero, out tz);
3238                 }
3239
3240                 [DllImport (MPH, SetLastError=true,
3241                                 EntryPoint="Mono_Posix_Syscall_settimeofday")]
3242                 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
3243
3244                 [DllImport (MPH, SetLastError=true,
3245                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3246                 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
3247
3248                 public static int settimeofday (ref Timeval tv)
3249                 {
3250                         return settimeofday (ref tv, IntPtr.Zero);
3251                 }
3252
3253                 [DllImport (MPH, SetLastError=true, 
3254                                 EntryPoint="Mono_Posix_Syscall_utimes")]
3255                 private static extern int sys_utimes (
3256                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3257                                 string filename, Timeval[] tvp);
3258
3259                 public static int utimes (string filename, Timeval[] tvp)
3260                 {
3261                         if (tvp != null && tvp.Length != 2) {
3262                                 SetLastError (Errno.EINVAL);
3263                                 return -1;
3264                         }
3265                         return sys_utimes (filename, tvp);
3266                 }
3267
3268                 [DllImport (MPH, SetLastError=true, 
3269                                 EntryPoint="Mono_Posix_Syscall_lutimes")]
3270                 private static extern int sys_lutimes (
3271                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3272                                 string filename, Timeval[] tvp);
3273
3274                 public static int lutimes (string filename, Timeval[] tvp)
3275                 {
3276                         if (tvp != null && tvp.Length != 2) {
3277                                 SetLastError (Errno.EINVAL);
3278                                 return -1;
3279                         }
3280                         return sys_lutimes (filename, tvp);
3281                 }
3282
3283                 [DllImport (MPH, SetLastError=true, 
3284                                 EntryPoint="Mono_Posix_Syscall_futimes")]
3285                 private static extern int sys_futimes (int fd, Timeval[] tvp);
3286
3287                 public static int futimes (int fd, Timeval[] tvp)
3288                 {
3289                         if (tvp != null && tvp.Length != 2) {
3290                                 SetLastError (Errno.EINVAL);
3291                                 return -1;
3292                         }
3293                         return sys_futimes (fd, tvp);
3294                 }
3295
3296                 #endregion
3297
3298                 //
3299                 // <sys/timeb.h>
3300                 //
3301
3302                 // TODO: ftime(3)
3303
3304                 //
3305                 // <sys/times.h>
3306                 //
3307
3308                 // TODO: times(2)
3309
3310                 //
3311                 // <sys/utsname.h>
3312                 //
3313
3314                 [Map]
3315                 private struct _Utsname
3316                 {
3317                         public IntPtr sysname;
3318                         public IntPtr nodename;
3319                         public IntPtr release;
3320                         public IntPtr version;
3321                         public IntPtr machine;
3322                         public IntPtr domainname;
3323                         public IntPtr _buf_;
3324                 }
3325
3326                 private static void CopyUtsname (ref Utsname to, ref _Utsname from)
3327                 {
3328                         try {
3329                                 to = new Utsname ();
3330                                 to.sysname     = UnixMarshal.PtrToString (from.sysname);
3331                                 to.nodename    = UnixMarshal.PtrToString (from.nodename);
3332                                 to.release     = UnixMarshal.PtrToString (from.release);
3333                                 to.version     = UnixMarshal.PtrToString (from.version);
3334                                 to.machine     = UnixMarshal.PtrToString (from.machine);
3335                                 to.domainname  = UnixMarshal.PtrToString (from.domainname);
3336                         }
3337                         finally {
3338                                 Stdlib.free (from._buf_);
3339                                 from._buf_ = IntPtr.Zero;
3340                         }
3341                 }
3342
3343                 [DllImport (MPH, SetLastError=true, 
3344                                 EntryPoint="Mono_Posix_Syscall_uname")]
3345                 private static extern int sys_uname (out _Utsname buf);
3346
3347                 public static int uname (out Utsname buf)
3348                 {
3349                         _Utsname _buf;
3350                         int r = sys_uname (out _buf);
3351                         buf = new Utsname ();
3352                         if (r == 0) {
3353                                 CopyUtsname (ref buf, ref _buf);
3354                         }
3355                         return r;
3356                 }
3357
3358                 #region <sys/wait.h> Declarations
3359                 //
3360                 // <sys/wait.h>
3361                 //
3362
3363                 // wait(2)
3364                 //    pid_t wait(int *status);
3365                 [DllImport (LIBC, SetLastError=true)]
3366                 public static extern int wait (out int status);
3367
3368                 // waitpid(2)
3369                 //    pid_t waitpid(pid_t pid, int *status, int options);
3370                 [DllImport (LIBC, SetLastError=true)]
3371                 private static extern int waitpid (int pid, out int status, int options);
3372
3373                 public static int waitpid (int pid, out int status, WaitOptions options)
3374                 {
3375                         int _options = NativeConvert.FromWaitOptions (options);
3376                         return waitpid (pid, out status, _options);
3377                 }
3378
3379                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
3380                 private static extern int _WIFEXITED (int status);
3381
3382                 public static bool WIFEXITED (int status)
3383                 {
3384                         return _WIFEXITED (status) != 0;
3385                 }
3386
3387                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
3388                 public static extern int WEXITSTATUS (int status);
3389
3390                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
3391                 private static extern int _WIFSIGNALED (int status);
3392
3393                 public static bool WIFSIGNALED (int status)
3394                 {
3395                         return _WIFSIGNALED (status) != 0;
3396                 }
3397
3398                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
3399                 private static extern int _WTERMSIG (int status);
3400
3401                 public static Signum WTERMSIG (int status)
3402                 {
3403                         int r = _WTERMSIG (status);
3404                         return NativeConvert.ToSignum (r);
3405                 }
3406
3407                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
3408                 private static extern int _WIFSTOPPED (int status);
3409
3410                 public static bool WIFSTOPPED (int status)
3411                 {
3412                         return _WIFSTOPPED (status) != 0;
3413                 }
3414
3415                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
3416                 private static extern int _WSTOPSIG (int status);
3417
3418                 public static Signum WSTOPSIG (int status)
3419                 {
3420                         int r = _WSTOPSIG (status);
3421                         return NativeConvert.ToSignum (r);
3422                 }
3423
3424                 //
3425                 // <termios.h>
3426                 //
3427
3428                 #endregion
3429
3430                 #region <syslog.h> Declarations
3431                 //
3432                 // <syslog.h>
3433                 //
3434
3435                 [DllImport (MPH, SetLastError=true,
3436                                 EntryPoint="Mono_Posix_Syscall_openlog")]
3437                 private static extern int sys_openlog (IntPtr ident, int option, int facility);
3438
3439                 public static int openlog (IntPtr ident, SyslogOptions option, 
3440                                 SyslogFacility defaultFacility)
3441                 {
3442                         int _option   = NativeConvert.FromSyslogOptions (option);
3443                         int _facility = NativeConvert.FromSyslogFacility (defaultFacility);
3444
3445                         return sys_openlog (ident, _option, _facility);
3446                 }
3447
3448                 [DllImport (MPH, SetLastError=true,
3449                                 EntryPoint="Mono_Posix_Syscall_syslog")]
3450                 private static extern int sys_syslog (int priority, string message);
3451
3452                 public static int syslog (SyslogFacility facility, SyslogLevel level, string message)
3453                 {
3454                         int _facility = NativeConvert.FromSyslogFacility (facility);
3455                         int _level = NativeConvert.FromSyslogLevel (level);
3456                         return sys_syslog (_facility | _level, GetSyslogMessage (message));
3457                 }
3458
3459                 public static int syslog (SyslogLevel level, string message)
3460                 {
3461                         int _level = NativeConvert.FromSyslogLevel (level);
3462                         return sys_syslog (_level, GetSyslogMessage (message));
3463                 }
3464
3465                 private static string GetSyslogMessage (string message)
3466                 {
3467                         return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
3468                 }
3469
3470                 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
3471                                 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
3472                 public static int syslog (SyslogFacility facility, SyslogLevel level, 
3473                                 string format, params object[] parameters)
3474                 {
3475                         int _facility = NativeConvert.FromSyslogFacility (facility);
3476                         int _level = NativeConvert.FromSyslogLevel (level);
3477
3478                         object[] _parameters = new object[checked(parameters.Length+2)];
3479                         _parameters [0] = _facility | _level;
3480                         _parameters [1] = format;
3481                         Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3482                         return (int) XPrintfFunctions.syslog (_parameters);
3483                 }
3484
3485                 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
3486                                 "Use syslog(SyslogLevel, string) instead.")]
3487                 public static int syslog (SyslogLevel level, string format, 
3488                                 params object[] parameters)
3489                 {
3490                         int _level = NativeConvert.FromSyslogLevel (level);
3491
3492                         object[] _parameters = new object[checked(parameters.Length+2)];
3493                         _parameters [0] = _level;
3494                         _parameters [1] = format;
3495                         Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3496                         return (int) XPrintfFunctions.syslog (_parameters);
3497                 }
3498
3499                 [DllImport (MPH, SetLastError=true,
3500                                 EntryPoint="Mono_Posix_Syscall_closelog")]
3501                 public static extern int closelog ();
3502
3503                 [DllImport (LIBC, SetLastError=true, EntryPoint="setlogmask")]
3504                 private static extern int sys_setlogmask (int mask);
3505
3506                 public static int setlogmask (SyslogLevel mask)
3507                 {
3508                         int _mask = NativeConvert.FromSyslogLevel (mask);
3509                         return sys_setlogmask (_mask);
3510                 }
3511
3512                 #endregion
3513
3514                 #region <time.h> Declarations
3515
3516                 //
3517                 // <time.h>
3518                 //
3519
3520                 // nanosleep(2)
3521                 //    int nanosleep(const struct timespec *req, struct timespec *rem);
3522                 [DllImport (MPH, SetLastError=true,
3523                                 EntryPoint="Mono_Posix_Syscall_nanosleep")]
3524                 public static extern int nanosleep (ref Timespec req, ref Timespec rem);
3525
3526                 // stime(2)
3527                 //    int stime(time_t *t);
3528                 [DllImport (MPH, SetLastError=true,
3529                                 EntryPoint="Mono_Posix_Syscall_stime")]
3530                 public static extern int stime (ref long t);
3531
3532                 // time(2)
3533                 //    time_t time(time_t *t);
3534                 [DllImport (MPH, SetLastError=true,
3535                                 EntryPoint="Mono_Posix_Syscall_time")]
3536                 public static extern long time (out long t);
3537
3538                 //
3539                 // <ulimit.h>
3540                 //
3541
3542                 // TODO: ulimit(3)
3543
3544                 #endregion
3545
3546                 #region <unistd.h> Declarations
3547                 //
3548                 // <unistd.h>
3549                 //
3550                 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
3551                 //       other TODOs listed below.
3552
3553                 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
3554                 private static extern int sys_access (
3555                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3556                                 string pathname, int mode);
3557
3558                 public static int access (string pathname, AccessModes mode)
3559                 {
3560                         int _mode = NativeConvert.FromAccessModes (mode);
3561                         return sys_access (pathname, _mode);
3562                 }
3563
3564                 // lseek(2)
3565                 //    off_t lseek(int filedes, off_t offset, int whence);
3566                 [DllImport (MPH, SetLastError=true, 
3567                                 EntryPoint="Mono_Posix_Syscall_lseek")]
3568                 private static extern long sys_lseek (int fd, long offset, int whence);
3569
3570                 public static long lseek (int fd, long offset, SeekFlags whence)
3571                 {
3572                         short _whence = NativeConvert.FromSeekFlags (whence);
3573                         return sys_lseek (fd, offset, _whence);
3574                 }
3575
3576     [DllImport (LIBC, SetLastError=true)]
3577                 public static extern int close (int fd);
3578
3579                 // read(2)
3580                 //    ssize_t read(int fd, void *buf, size_t count);
3581                 [DllImport (MPH, SetLastError=true, 
3582                                 EntryPoint="Mono_Posix_Syscall_read")]
3583                 public static extern long read (int fd, IntPtr buf, ulong count);
3584
3585                 public static unsafe long read (int fd, void *buf, ulong count)
3586                 {
3587                         return read (fd, (IntPtr) buf, count);
3588                 }
3589
3590                 // write(2)
3591                 //    ssize_t write(int fd, const void *buf, size_t count);
3592                 [DllImport (MPH, SetLastError=true, 
3593                                 EntryPoint="Mono_Posix_Syscall_write")]
3594                 public static extern long write (int fd, IntPtr buf, ulong count);
3595
3596                 public static unsafe long write (int fd, void *buf, ulong count)
3597                 {
3598                         return write (fd, (IntPtr) buf, count);
3599                 }
3600
3601                 // pread(2)
3602                 //    ssize_t pread(int fd, void *buf, size_t count, off_t offset);
3603                 [DllImport (MPH, SetLastError=true, 
3604                                 EntryPoint="Mono_Posix_Syscall_pread")]
3605                 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
3606
3607                 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
3608                 {
3609                         return pread (fd, (IntPtr) buf, count, offset);
3610                 }
3611
3612                 // pwrite(2)
3613                 //    ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
3614                 [DllImport (MPH, SetLastError=true, 
3615                                 EntryPoint="Mono_Posix_Syscall_pwrite")]
3616                 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
3617
3618                 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
3619                 {
3620                         return pwrite (fd, (IntPtr) buf, count, offset);
3621                 }
3622
3623                 [DllImport (MPH, SetLastError=true, 
3624                                 EntryPoint="Mono_Posix_Syscall_pipe")]
3625                 public static extern int pipe (out int reading, out int writing);
3626
3627                 public static int pipe (int[] filedes)
3628                 {
3629                         if (filedes == null || filedes.Length != 2) {
3630                                 // TODO: set errno
3631                                 return -1;
3632                         }
3633                         int reading, writing;
3634                         int r = pipe (out reading, out writing);
3635                         filedes[0] = reading;
3636                         filedes[1] = writing;
3637                         return r;
3638                 }
3639
3640                 [DllImport (LIBC, SetLastError=true)]
3641                 public static extern uint alarm (uint seconds);
3642
3643                 [DllImport (LIBC, SetLastError=true)]
3644                 public static extern uint sleep (uint seconds);
3645
3646                 [DllImport (LIBC, SetLastError=true)]
3647                 public static extern uint ualarm (uint usecs, uint interval);
3648
3649                 [DllImport (LIBC, SetLastError=true)]
3650                 public static extern int pause ();
3651
3652                 // chown(2)
3653                 //    int chown(const char *path, uid_t owner, gid_t group);
3654                 [DllImport (LIBC, SetLastError=true)]
3655                 public static extern int chown (
3656                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3657                                 string path, uint owner, uint group);
3658
3659                 // fchown(2)
3660                 //    int fchown(int fd, uid_t owner, gid_t group);
3661                 [DllImport (LIBC, SetLastError=true)]
3662                 public static extern int fchown (int fd, uint owner, uint group);
3663
3664                 // lchown(2)
3665                 //    int lchown(const char *path, uid_t owner, gid_t group);
3666                 [DllImport (LIBC, SetLastError=true)]
3667                 public static extern int lchown (
3668                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3669                                 string path, uint owner, uint group);
3670
3671                 [DllImport (LIBC, SetLastError=true)]
3672                 public static extern int chdir (
3673                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3674                                 string path);
3675
3676                 [DllImport (LIBC, SetLastError=true)]
3677                 public static extern int fchdir (int fd);
3678
3679                 // getcwd(3)
3680                 //    char *getcwd(char *buf, size_t size);
3681                 [DllImport (MPH, SetLastError=true,
3682                                 EntryPoint="Mono_Posix_Syscall_getcwd")]
3683                 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
3684
3685                 public static StringBuilder getcwd (StringBuilder buf)
3686                 {
3687                         getcwd (buf, (ulong) buf.Capacity);
3688                         return buf;
3689                 }
3690
3691                 // getwd(2) is deprecated; don't expose it.
3692
3693                 [DllImport (LIBC, SetLastError=true)]
3694                 public static extern int dup (int fd);
3695
3696                 [DllImport (LIBC, SetLastError=true)]
3697                 public static extern int dup2 (int fd, int fd2);
3698
3699                 // TODO: does Mono marshal arrays properly?
3700                 [DllImport (LIBC, SetLastError=true)]
3701                 public static extern int execve (
3702                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3703                                 string path, string[] argv, string[] envp);
3704
3705                 [DllImport (LIBC, SetLastError=true)]
3706                 public static extern int fexecve (int fd, string[] argv, string[] envp);
3707
3708                 [DllImport (LIBC, SetLastError=true)]
3709                 public static extern int execv (
3710                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3711                                 string path, string[] argv);
3712
3713                 // TODO: execle, execl, execlp
3714                 [DllImport (LIBC, SetLastError=true)]
3715                 public static extern int execvp (
3716                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3717                                 string path, string[] argv);
3718
3719                 [DllImport (LIBC, SetLastError=true)]
3720                 public static extern int nice (int inc);
3721
3722                 [DllImport (LIBC, SetLastError=true)]
3723                 [CLSCompliant (false)]
3724                 public static extern int _exit (int status);
3725
3726                 [DllImport (MPH, SetLastError=true,
3727                                 EntryPoint="Mono_Posix_Syscall_fpathconf")]
3728                 public static extern long fpathconf (int filedes, PathconfName name, Errno defaultError);
3729
3730                 public static long fpathconf (int filedes, PathconfName name)
3731                 {
3732                         return fpathconf (filedes, name, (Errno) 0);
3733                 }
3734
3735                 [DllImport (MPH, SetLastError=true,
3736                                 EntryPoint="Mono_Posix_Syscall_pathconf")]
3737                 public static extern long pathconf (
3738                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3739                                 string path, PathconfName name, Errno defaultError);
3740
3741                 public static long pathconf (string path, PathconfName name)
3742                 {
3743                         return pathconf (path, name, (Errno) 0);
3744                 }
3745
3746                 [DllImport (MPH, SetLastError=true,
3747                                 EntryPoint="Mono_Posix_Syscall_sysconf")]
3748                 public static extern long sysconf (SysconfName name, Errno defaultError);
3749
3750                 public static long sysconf (SysconfName name)
3751                 {
3752                         return sysconf (name, (Errno) 0);
3753                 }
3754
3755                 // confstr(3)
3756                 //    size_t confstr(int name, char *buf, size_t len);
3757                 [DllImport (MPH, SetLastError=true,
3758                                 EntryPoint="Mono_Posix_Syscall_confstr")]
3759                 public static extern ulong confstr (ConfstrName name, [Out] StringBuilder buf, ulong len);
3760
3761                 // getpid(2)
3762                 //    pid_t getpid(void);
3763                 [DllImport (LIBC, SetLastError=true)]
3764                 public static extern int getpid ();
3765
3766                 // getppid(2)
3767                 //    pid_t getppid(void);
3768                 [DllImport (LIBC, SetLastError=true)]
3769                 public static extern int getppid ();
3770
3771                 // setpgid(2)
3772                 //    int setpgid(pid_t pid, pid_t pgid);
3773                 [DllImport (LIBC, SetLastError=true)]
3774                 public static extern int setpgid (int pid, int pgid);
3775
3776                 // getpgid(2)
3777                 //    pid_t getpgid(pid_t pid);
3778                 [DllImport (LIBC, SetLastError=true)]
3779                 public static extern int getpgid (int pid);
3780
3781                 [DllImport (LIBC, SetLastError=true)]
3782                 public static extern int setpgrp ();
3783
3784                 // getpgrp(2)
3785                 //    pid_t getpgrp(void);
3786                 [DllImport (LIBC, SetLastError=true)]
3787                 public static extern int getpgrp ();
3788
3789                 // setsid(2)
3790                 //    pid_t setsid(void);
3791                 [DllImport (LIBC, SetLastError=true)]
3792                 public static extern int setsid ();
3793
3794                 // getsid(2)
3795                 //    pid_t getsid(pid_t pid);
3796                 [DllImport (LIBC, SetLastError=true)]
3797                 public static extern int getsid (int pid);
3798
3799                 // getuid(2)
3800                 //    uid_t getuid(void);
3801                 [DllImport (LIBC, SetLastError=true)]
3802                 public static extern uint getuid ();
3803
3804                 // geteuid(2)
3805                 //    uid_t geteuid(void);
3806                 [DllImport (LIBC, SetLastError=true)]
3807                 public static extern uint geteuid ();
3808
3809                 // getgid(2)
3810                 //    gid_t getgid(void);
3811                 [DllImport (LIBC, SetLastError=true)]
3812                 public static extern uint getgid ();
3813
3814                 // getegid(2)
3815                 //    gid_t getgid(void);
3816                 [DllImport (LIBC, SetLastError=true)]
3817                 public static extern uint getegid ();
3818
3819                 // getgroups(2)
3820                 //    int getgroups(int size, gid_t list[]);
3821                 [DllImport (LIBC, SetLastError=true)]
3822                 public static extern int getgroups (int size, uint[] list);
3823
3824                 public static int getgroups (uint[] list)
3825                 {
3826                         return getgroups (list.Length, list);
3827                 }
3828
3829                 // setuid(2)
3830                 //    int setuid(uid_t uid);
3831                 [DllImport (LIBC, SetLastError=true)]
3832                 public static extern int setuid (uint uid);
3833
3834                 // setreuid(2)
3835                 //    int setreuid(uid_t ruid, uid_t euid);
3836                 [DllImport (LIBC, SetLastError=true)]
3837                 public static extern int setreuid (uint ruid, uint euid);
3838
3839                 // setregid(2)
3840                 //    int setregid(gid_t ruid, gid_t euid);
3841                 [DllImport (LIBC, SetLastError=true)]
3842                 public static extern int setregid (uint rgid, uint egid);
3843
3844                 // seteuid(2)
3845                 //    int seteuid(uid_t euid);
3846                 [DllImport (LIBC, SetLastError=true)]
3847                 public static extern int seteuid (uint euid);
3848
3849                 // setegid(2)
3850                 //    int setegid(gid_t euid);
3851                 [DllImport (LIBC, SetLastError=true)]
3852                 public static extern int setegid (uint uid);
3853
3854                 // setgid(2)
3855                 //    int setgid(gid_t gid);
3856                 [DllImport (LIBC, SetLastError=true)]
3857                 public static extern int setgid (uint gid);
3858
3859                 // getresuid(2)
3860                 //    int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
3861                 [DllImport (LIBC, SetLastError=true)]
3862                 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
3863
3864                 // getresgid(2)
3865                 //    int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
3866                 [DllImport (LIBC, SetLastError=true)]
3867                 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
3868
3869                 // setresuid(2)
3870                 //    int setresuid(uid_t ruid, uid_t euid, uid_t suid);
3871                 [DllImport (LIBC, SetLastError=true)]
3872                 public static extern int setresuid (uint ruid, uint euid, uint suid);
3873
3874                 // setresgid(2)
3875                 //    int setresgid(gid_t ruid, gid_t euid, gid_t suid);
3876                 [DllImport (LIBC, SetLastError=true)]
3877                 public static extern int setresgid (uint rgid, uint egid, uint sgid);
3878
3879 #if false
3880                 // fork(2)
3881                 //    pid_t fork(void);
3882                 [DllImport (LIBC, SetLastError=true)]
3883                 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " + 
3884                                 "shutdown code.\nUse System.Diagnostics.Process instead")]
3885                 private static extern int fork ();
3886
3887                 // vfork(2)
3888                 //    pid_t vfork(void);
3889                 [DllImport (LIBC, SetLastError=true)]
3890                 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " + 
3891                                 "shutdown code.\nUse System.Diagnostics.Process instead")]
3892                 private static extern int vfork ();
3893 #endif
3894
3895                 private static object tty_lock = new object ();
3896
3897                 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
3898                 private static extern IntPtr sys_ttyname (int fd);
3899
3900                 public static string ttyname (int fd)
3901                 {
3902                         lock (tty_lock) {
3903                                 IntPtr r = sys_ttyname (fd);
3904                                 return UnixMarshal.PtrToString (r);
3905                         }
3906                 }
3907
3908                 // ttyname_r(3)
3909                 //    int ttyname_r(int fd, char *buf, size_t buflen);
3910                 [DllImport (MPH, SetLastError=true,
3911                                 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
3912                 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
3913
3914                 public static int ttyname_r (int fd, StringBuilder buf)
3915                 {
3916                         return ttyname_r (fd, buf, (ulong) buf.Capacity);
3917                 }
3918
3919                 [DllImport (LIBC, EntryPoint="isatty")]
3920                 private static extern int sys_isatty (int fd);
3921
3922                 public static bool isatty (int fd)
3923                 {
3924                         return sys_isatty (fd) == 1;
3925                 }
3926
3927                 [DllImport (LIBC, SetLastError=true)]
3928                 public static extern int link (
3929                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3930                                 string oldpath, 
3931                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3932                                 string newpath);
3933
3934                 [DllImport (LIBC, SetLastError=true)]
3935                 public static extern int symlink (
3936                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3937                                 string oldpath, 
3938                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3939                                 string newpath);
3940
3941                 delegate long DoReadlinkFun (byte[] target);
3942
3943                 // Helper function for readlink(string, StringBuilder) and readlinkat (int, string, StringBuilder)
3944                 static int ReadlinkIntoStringBuilder (DoReadlinkFun doReadlink, [Out] StringBuilder buf, ulong bufsiz)
3945                 {
3946                         // bufsiz > int.MaxValue can't work because StringBuilder can store only int.MaxValue chars
3947                         int bufsizInt = checked ((int) bufsiz);
3948                         var target = new byte [bufsizInt];
3949
3950                         var r = doReadlink (target);
3951                         if (r < 0)
3952                                 return checked ((int) r);
3953
3954                         buf.Length = 0;
3955                         var chars = UnixEncoding.Instance.GetChars (target, 0, checked ((int) r));
3956                         // Make sure that at more bufsiz chars are written
3957                         buf.Append (chars, 0, System.Math.Min (bufsizInt, chars.Length));
3958                         if (r == bufsizInt) {
3959                                 // may not have read full contents; fill 'buf' so that caller can properly check
3960                                 buf.Append (new string ('\x00', bufsizInt - buf.Length));
3961                         }
3962                         return buf.Length;
3963                 }
3964
3965                 // readlink(2)
3966                 //    ssize_t readlink(const char *path, char *buf, size_t bufsize);
3967                 public static int readlink (string path, [Out] StringBuilder buf, ulong bufsiz)
3968                 {
3969                         return ReadlinkIntoStringBuilder (target => readlink (path, target), buf, bufsiz);
3970                 }
3971
3972                 public static int readlink (string path, [Out] StringBuilder buf)
3973                 {
3974                         return readlink (path, buf, (ulong) buf.Capacity);
3975                 }
3976
3977                 [DllImport (MPH, SetLastError=true,
3978                                 EntryPoint="Mono_Posix_Syscall_readlink")]
3979                 private static extern long readlink (
3980                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3981                                 string path, byte[] buf, ulong bufsiz);
3982
3983                 public static long readlink (string path, byte[] buf)
3984                 {
3985                         return readlink (path, buf, (ulong) buf.LongLength);
3986                 }
3987
3988                 [DllImport (LIBC, SetLastError=true)]
3989                 public static extern int unlink (
3990                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3991                                 string pathname);
3992
3993                 [DllImport (LIBC, SetLastError=true)]
3994                 public static extern int rmdir (
3995                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3996                                 string pathname);
3997
3998                 // tcgetpgrp(3)
3999                 //    pid_t tcgetpgrp(int fd);
4000                 [DllImport (LIBC, SetLastError=true)]
4001                 public static extern int tcgetpgrp (int fd);
4002
4003                 // tcsetpgrp(3)
4004                 //    int tcsetpgrp(int fd, pid_t pgrp);
4005                 [DllImport (LIBC, SetLastError=true)]
4006                 public static extern int tcsetpgrp (int fd, int pgrp);
4007
4008                 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
4009                 private static extern IntPtr sys_getlogin ();
4010
4011                 public static string getlogin ()
4012                 {
4013                         lock (getlogin_lock) {
4014                                 IntPtr r = sys_getlogin ();
4015                                 return UnixMarshal.PtrToString (r);
4016                         }
4017                 }
4018
4019                 // getlogin_r(3)
4020                 //    int getlogin_r(char *buf, size_t bufsize);
4021                 [DllImport (MPH, SetLastError=true,
4022                                 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
4023                 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
4024
4025                 public static int getlogin_r (StringBuilder name)
4026                 {
4027                         return getlogin_r (name, (ulong) name.Capacity);
4028                 }
4029
4030                 [DllImport (LIBC, SetLastError=true)]
4031                 public static extern int setlogin (string name);
4032
4033                 // gethostname(2)
4034                 //    int gethostname(char *name, size_t len);
4035                 [DllImport (MPH, SetLastError=true,
4036                                 EntryPoint="Mono_Posix_Syscall_gethostname")]
4037                 public static extern int gethostname ([Out] StringBuilder name, ulong len);
4038
4039                 public static int gethostname (StringBuilder name)
4040                 {
4041                         return gethostname (name, (ulong) name.Capacity);
4042                 }
4043
4044                 // sethostname(2)
4045                 //    int gethostname(const char *name, size_t len);
4046                 [DllImport (MPH, SetLastError=true,
4047                                 EntryPoint="Mono_Posix_Syscall_sethostname")]
4048                 public static extern int sethostname (string name, ulong len);
4049
4050                 public static int sethostname (string name)
4051                 {
4052                         return sethostname (name, (ulong) name.Length);
4053                 }
4054
4055                 [DllImport (MPH, SetLastError=true,
4056                                 EntryPoint="Mono_Posix_Syscall_gethostid")]
4057                 public static extern long gethostid ();
4058
4059                 [DllImport (MPH, SetLastError=true,
4060                                 EntryPoint="Mono_Posix_Syscall_sethostid")]
4061                 public static extern int sethostid (long hostid);
4062
4063                 // getdomainname(2)
4064                 //    int getdomainname(char *name, size_t len);
4065                 [DllImport (MPH, SetLastError=true,
4066                                 EntryPoint="Mono_Posix_Syscall_getdomainname")]
4067                 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
4068
4069                 public static int getdomainname (StringBuilder name)
4070                 {
4071                         return getdomainname (name, (ulong) name.Capacity);
4072                 }
4073
4074                 // setdomainname(2)
4075                 //    int setdomainname(const char *name, size_t len);
4076                 [DllImport (MPH, SetLastError=true,
4077                                 EntryPoint="Mono_Posix_Syscall_setdomainname")]
4078                 public static extern int setdomainname (string name, ulong len);
4079
4080                 public static int setdomainname (string name)
4081                 {
4082                         return setdomainname (name, (ulong) name.Length);
4083                 }
4084
4085                 [DllImport (LIBC, SetLastError=true)]
4086                 public static extern int vhangup ();
4087
4088                 // Revoke doesn't appear to be POSIX.  Include it?
4089                 [DllImport (LIBC, SetLastError=true)]
4090                 public static extern int revoke (
4091                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4092                                 string file);
4093
4094                 // TODO: profil?  It's not POSIX.
4095
4096                 [DllImport (LIBC, SetLastError=true)]
4097                 public static extern int acct (
4098                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4099                                 string filename);
4100
4101                 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
4102                 private static extern IntPtr sys_getusershell ();
4103
4104                 internal static object usershell_lock = new object ();
4105
4106                 public static string getusershell ()
4107                 {
4108                         lock (usershell_lock) {
4109                                 IntPtr r = sys_getusershell ();
4110                                 return UnixMarshal.PtrToString (r);
4111                         }
4112                 }
4113
4114                 [DllImport (MPH, SetLastError=true, 
4115                                 EntryPoint="Mono_Posix_Syscall_setusershell")]
4116                 private static extern int sys_setusershell ();
4117
4118                 public static int setusershell ()
4119                 {
4120                         lock (usershell_lock) {
4121                                 return sys_setusershell ();
4122                         }
4123                 }
4124
4125                 [DllImport (MPH, SetLastError=true, 
4126                                 EntryPoint="Mono_Posix_Syscall_endusershell")]
4127                 private static extern int sys_endusershell ();
4128
4129                 public static int endusershell ()
4130                 {
4131                         lock (usershell_lock) {
4132                                 return sys_endusershell ();
4133                         }
4134                 }
4135
4136 #if false
4137                 [DllImport (LIBC, SetLastError=true)]
4138                 private static extern int daemon (int nochdir, int noclose);
4139
4140                 // this implicitly forks, and thus isn't safe.
4141                 private static int daemon (bool nochdir, bool noclose)
4142                 {
4143                         return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
4144                 }
4145 #endif
4146
4147                 [DllImport (LIBC, SetLastError=true)]
4148                 public static extern int chroot (
4149                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4150                                 string path);
4151
4152                 // skipping getpass(3) as the man page states:
4153                 //   This function is obsolete.  Do not use it.
4154
4155                 [DllImport (LIBC, SetLastError=true)]
4156                 public static extern int fsync (int fd);
4157
4158                 [DllImport (LIBC, SetLastError=true)]
4159                 public static extern int fdatasync (int fd);
4160
4161                 [DllImport (MPH, SetLastError=true,
4162                                 EntryPoint="Mono_Posix_Syscall_sync")]
4163                 public static extern int sync ();
4164
4165                 [DllImport (LIBC, SetLastError=true)]
4166                 [Obsolete ("Dropped in POSIX 1003.1-2001.  " +
4167                                 "Use Syscall.sysconf (SysconfName._SC_PAGESIZE).")]
4168                 public static extern int getpagesize ();
4169
4170                 // truncate(2)
4171                 //    int truncate(const char *path, off_t length);
4172                 [DllImport (MPH, SetLastError=true, 
4173                                 EntryPoint="Mono_Posix_Syscall_truncate")]
4174                 public static extern int truncate (
4175                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4176                                 string path, long length);
4177
4178                 // ftruncate(2)
4179                 //    int ftruncate(int fd, off_t length);
4180                 [DllImport (MPH, SetLastError=true, 
4181                                 EntryPoint="Mono_Posix_Syscall_ftruncate")]
4182                 public static extern int ftruncate (int fd, long length);
4183
4184                 [DllImport (LIBC, SetLastError=true)]
4185                 public static extern int getdtablesize ();
4186
4187                 [DllImport (LIBC, SetLastError=true)]
4188                 public static extern int brk (IntPtr end_data_segment);
4189
4190                 [DllImport (LIBC, SetLastError=true)]
4191                 public static extern IntPtr sbrk (IntPtr increment);
4192
4193                 // TODO: syscall(2)?
4194                 // Probably safer to skip entirely.
4195
4196                 // lockf(3)
4197                 //    int lockf(int fd, int cmd, off_t len);
4198                 [DllImport (MPH, SetLastError=true, 
4199                                 EntryPoint="Mono_Posix_Syscall_lockf")]
4200                 public static extern int lockf (int fd, LockfCommand cmd, long len);
4201
4202                 [Obsolete ("This is insecure and should not be used", true)]
4203                 public static string crypt (string key, string salt)
4204                 {
4205                         throw new SecurityException ("crypt(3) has been broken.  Use something more secure.");
4206                 }
4207
4208                 [Obsolete ("This is insecure and should not be used", true)]
4209                 public static int encrypt (byte[] block, bool decode)
4210                 {
4211                         throw new SecurityException ("crypt(3) has been broken.  Use something more secure.");
4212                 }
4213
4214                 // swab(3)
4215                 //    void swab(const void *from, void *to, ssize_t n);
4216                 [DllImport (MPH, SetLastError=true, 
4217                                 EntryPoint="Mono_Posix_Syscall_swab")]
4218                 public static extern int swab (IntPtr from, IntPtr to, long n);
4219
4220                 public static unsafe void swab (void* from, void* to, long n)
4221                 {
4222                         swab ((IntPtr) from, (IntPtr) to, n);
4223                 }
4224
4225                 [DllImport (LIBC, SetLastError=true, EntryPoint="faccessat")]
4226                 private static extern int sys_faccessat (int dirfd,
4227                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4228                                 string pathname, int mode, int flags);
4229
4230                 public static int faccessat (int dirfd, string pathname, AccessModes mode, AtFlags flags)
4231                 {
4232                         int _mode = NativeConvert.FromAccessModes (mode);
4233                         int _flags = NativeConvert.FromAtFlags (flags);
4234                         return sys_faccessat (dirfd, pathname, _mode, _flags);
4235                 }
4236
4237                 // fchownat(2)
4238                 //    int fchownat(int dirfd, const char *path, uid_t owner, gid_t group, int flags);
4239                 [DllImport (LIBC, SetLastError=true, EntryPoint="fchownat")]
4240                 private static extern int sys_fchownat (int dirfd,
4241                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4242                                 string pathname, uint owner, uint group, int flags);
4243
4244                 public static int fchownat (int dirfd, string pathname, uint owner, uint group, AtFlags flags)
4245                 {
4246                         int _flags = NativeConvert.FromAtFlags (flags);
4247                         return sys_fchownat (dirfd, pathname, owner, group, _flags);
4248                 }
4249
4250                 [DllImport (LIBC, SetLastError=true, EntryPoint="linkat")]
4251                 private static extern int sys_linkat (int olddirfd,
4252                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4253                                 string oldpath, int newdirfd,
4254                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4255                                 string newpath, int flags);
4256
4257                 public static int linkat (int olddirfd, string oldpath, int newdirfd, string newpath, AtFlags flags)
4258                 {
4259                         int _flags = NativeConvert.FromAtFlags (flags);
4260                         return sys_linkat (olddirfd, oldpath, newdirfd, newpath, _flags);
4261                 }
4262
4263                 // readlinkat(2)
4264                 //    ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsize);
4265                 public static int readlinkat (int dirfd, string pathname, [Out] StringBuilder buf, ulong bufsiz)
4266                 {
4267                         return ReadlinkIntoStringBuilder (target => readlinkat (dirfd, pathname, target), buf, bufsiz);
4268                 }
4269
4270                 public static int readlinkat (int dirfd, string pathname, [Out] StringBuilder buf)
4271                 {
4272                         return readlinkat (dirfd, pathname, buf, (ulong) buf.Capacity);
4273                 }
4274
4275                 [DllImport (MPH, SetLastError=true,
4276                                 EntryPoint="Mono_Posix_Syscall_readlinkat")]
4277                 private static extern long readlinkat (int dirfd,
4278                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4279                                 string pathname, byte[] buf, ulong bufsiz);
4280
4281                 public static long readlinkat (int dirfd, string pathname, byte[] buf)
4282                 {
4283                         return readlinkat (dirfd, pathname, buf, (ulong) buf.LongLength);
4284                 }
4285
4286                 [DllImport (LIBC, SetLastError=true)]
4287                 public static extern int symlinkat (
4288                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4289                                 string oldpath, int dirfd,
4290                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4291                                 string newpath);
4292
4293                 [DllImport (LIBC, SetLastError=true, EntryPoint="unlinkat")]
4294                 private static extern int sys_unlinkat (int dirfd,
4295                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4296                                 string pathname, int flags);
4297
4298                 public static int unlinkat (int dirfd, string pathname, AtFlags flags)
4299                 {
4300                         int _flags = NativeConvert.FromAtFlags (flags);
4301                         return sys_unlinkat (dirfd, pathname, _flags);
4302                 }
4303                 #endregion
4304
4305                 #region <utime.h> Declarations
4306                 //
4307                 // <utime.h>  -- COMPLETE
4308                 //
4309
4310                 [DllImport (MPH, SetLastError=true, 
4311                                 EntryPoint="Mono_Posix_Syscall_utime")]
4312                 private static extern int sys_utime (
4313                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
4314                                 string filename, ref Utimbuf buf, int use_buf);
4315
4316                 public static int utime (string filename, ref Utimbuf buf)
4317                 {
4318                         return sys_utime (filename, ref buf, 1);
4319                 }
4320
4321                 public static int utime (string filename)
4322                 {
4323                         Utimbuf buf = new Utimbuf ();
4324                         return sys_utime (filename, ref buf, 0);
4325                 }
4326                 #endregion
4327
4328                 #region <sys/uio.h> Declarations
4329                 //
4330                 // <sys/uio.h> -- COMPLETE
4331                 //
4332
4333                 // readv(2)
4334                 //    ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
4335                 [DllImport (MPH, SetLastError=true,
4336                                 EntryPoint="Mono_Posix_Syscall_readv")]
4337                 private static extern long sys_readv (int fd, Iovec[] iov, int iovcnt);
4338
4339                 public static long readv (int fd, Iovec[] iov)
4340                 {
4341                         return sys_readv (fd, iov, iov.Length);
4342                 }
4343
4344                 // writev(2)
4345                 //    ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
4346                 [DllImport (MPH, SetLastError=true,
4347                                 EntryPoint="Mono_Posix_Syscall_writev")]
4348                 private static extern long sys_writev (int fd, Iovec[] iov, int iovcnt);
4349
4350                 public static long writev (int fd, Iovec[] iov)
4351                 {
4352                         return sys_writev (fd, iov, iov.Length);
4353                 }
4354
4355                 // preadv(2)
4356                 //    ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
4357                 [DllImport (MPH, SetLastError=true,
4358                                 EntryPoint="Mono_Posix_Syscall_preadv")]
4359                 private static extern long sys_preadv (int fd, Iovec[] iov, int iovcnt, long offset);
4360
4361                 public static long preadv (int fd, Iovec[] iov, long offset)
4362                 {
4363                         return sys_preadv (fd, iov, iov.Length, offset);
4364                 }
4365
4366                 // pwritev(2)
4367                 //    ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
4368                 [DllImport (MPH, SetLastError=true,
4369                                 EntryPoint="Mono_Posix_Syscall_pwritev")]
4370                 private static extern long sys_pwritev (int fd, Iovec[] iov, int iovcnt, long offset);
4371
4372                 public static long pwritev (int fd, Iovec[] iov, long offset)
4373                 {
4374                         return sys_pwritev (fd, iov, iov.Length, offset);
4375                 }
4376                 #endregion
4377         }
4378
4379         #endregion
4380 }
4381
4382 // vim: noexpandtab