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