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