[Mono.Posix] Add Syscall.getgrouplist().
[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         }
154         
155         // mode_t
156         [Flags][Map]
157         [CLSCompliant (false)]
158         public enum FilePermissions : uint {
159                 S_ISUID     = 0x0800, // Set user ID on execution
160                 S_ISGID     = 0x0400, // Set group ID on execution
161                 S_ISVTX     = 0x0200, // Save swapped text after use (sticky).
162                 S_IRUSR     = 0x0100, // Read by owner
163                 S_IWUSR     = 0x0080, // Write by owner
164                 S_IXUSR     = 0x0040, // Execute by owner
165                 S_IRGRP     = 0x0020, // Read by group
166                 S_IWGRP     = 0x0010, // Write by group
167                 S_IXGRP     = 0x0008, // Execute by group
168                 S_IROTH     = 0x0004, // Read by other
169                 S_IWOTH     = 0x0002, // Write by other
170                 S_IXOTH     = 0x0001, // Execute by other
171
172                 S_IRWXG     = (S_IRGRP | S_IWGRP | S_IXGRP),
173                 S_IRWXU     = (S_IRUSR | S_IWUSR | S_IXUSR),
174                 S_IRWXO     = (S_IROTH | S_IWOTH | S_IXOTH),
175                 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
176                 ALLPERMS    = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
177                 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
178
179                 // Device types
180                 // Why these are held in "mode_t" is beyond me...
181                 S_IFMT      = 0xF000, // Bits which determine file type
182                 [Map(SuppressFlags="S_IFMT")]
183                 S_IFDIR     = 0x4000, // Directory
184                 [Map(SuppressFlags="S_IFMT")]
185                 S_IFCHR     = 0x2000, // Character device
186                 [Map(SuppressFlags="S_IFMT")]
187                 S_IFBLK     = 0x6000, // Block device
188                 [Map(SuppressFlags="S_IFMT")]
189                 S_IFREG     = 0x8000, // Regular file
190                 [Map(SuppressFlags="S_IFMT")]
191                 S_IFIFO     = 0x1000, // FIFO
192                 [Map(SuppressFlags="S_IFMT")]
193                 S_IFLNK     = 0xA000, // Symbolic link
194                 [Map(SuppressFlags="S_IFMT")]
195                 S_IFSOCK    = 0xC000, // Socket
196         }
197
198         [Map]
199         [CLSCompliant (false)]
200         public enum FcntlCommand : int {
201                 // Form /usr/include/bits/fcntl.h
202                 F_DUPFD    =    0, // Duplicate file descriptor.
203                 F_GETFD    =    1, // Get file descriptor flags.
204                 F_SETFD    =    2, // Set file descriptor flags.
205                 F_GETFL    =    3, // Get file status flags.
206                 F_SETFL    =    4, // Set file status flags.
207                 F_GETLK    =   12, // Get record locking info. [64]
208                 F_SETLK    =   13, // Set record locking info (non-blocking). [64]
209                 F_SETLKW   =   14, // Set record locking info (blocking). [64]
210                 F_SETOWN   =    8, // Set owner of socket (receiver of SIGIO).
211                 F_GETOWN   =    9, // Get owner of socket (receiver of SIGIO).
212                 F_SETSIG   =   10, // Set number of signal to be sent.
213                 F_GETSIG   =   11, // Get number of signal to be sent.
214                 F_SETLEASE = 1024, // Set a lease.
215                 F_GETLEASE = 1025, // Enquire what lease is active.
216                 F_NOTIFY   = 1026, // Required notifications on a directory
217         }
218
219         [Map]
220         [CLSCompliant (false)]
221         public enum LockType : short {
222                 F_RDLCK = 0, // Read lock.
223                 F_WRLCK = 1, // Write lock.
224                 F_UNLCK = 2, // Remove lock.
225         }
226
227         [Map]
228         [CLSCompliant (false)]
229         public enum SeekFlags : short {
230                 // values liberally copied from /usr/include/unistd.h
231                 SEEK_SET = 0, // Seek from beginning of file.
232                 SEEK_CUR = 1, // Seek from current position.
233                 SEEK_END = 2, // Seek from end of file.
234
235                 L_SET    = SEEK_SET, // BSD alias for SEEK_SET
236                 L_INCR   = SEEK_CUR, // BSD alias for SEEK_CUR
237                 L_XTND   = SEEK_END, // BSD alias for SEEK_END
238         }
239         
240         [Map, Flags]
241         [CLSCompliant (false)]
242         public enum DirectoryNotifyFlags : int {
243                 // from /usr/include/bits/fcntl.h
244                 DN_ACCESS    = 0x00000001, // File accessed.
245                 DN_MODIFY    = 0x00000002, // File modified.
246                 DN_CREATE    = 0x00000004, // File created.
247                 DN_DELETE    = 0x00000008, // File removed.
248                 DN_RENAME    = 0x00000010, // File renamed.
249                 DN_ATTRIB    = 0x00000020, // File changed attributes.
250                 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
251         }
252
253         [Map]
254         [CLSCompliant (false)]
255         public enum PosixFadviseAdvice : int {
256                 POSIX_FADV_NORMAL     = 0,  // No further special treatment.
257                 POSIX_FADV_RANDOM     = 1,  // Expect random page references.
258                 POSIX_FADV_SEQUENTIAL = 2,  // Expect sequential page references.
259                 POSIX_FADV_WILLNEED   = 3,  // Will need these pages.
260                 POSIX_FADV_DONTNEED   = 4,  // Don't need these pages.
261                 POSIX_FADV_NOREUSE    = 5,  // Data will be accessed once.
262         }
263
264         [Map]
265         [CLSCompliant (false)]
266         public enum PosixMadviseAdvice : int {
267                 POSIX_MADV_NORMAL     = 0,  // No further special treatment.
268                 POSIX_MADV_RANDOM     = 1,  // Expect random page references.
269                 POSIX_MADV_SEQUENTIAL = 2,  // Expect sequential page references.
270                 POSIX_MADV_WILLNEED   = 3,  // Will need these pages.
271                 POSIX_MADV_DONTNEED   = 4,  // Don't need these pages.
272         }
273
274         [Map]
275         public enum Signum : int {
276                 SIGHUP    =  1, // Hangup (POSIX).
277                 SIGINT    =  2, // Interrupt (ANSI).
278                 SIGQUIT   =  3, // Quit (POSIX).
279                 SIGILL    =  4, // Illegal instruction (ANSI).
280                 SIGTRAP   =  5, // Trace trap (POSIX).
281                 SIGABRT   =  6, // Abort (ANSI).
282                 SIGIOT    =  6, // IOT trap (4.2 BSD).
283                 SIGBUS    =  7, // BUS error (4.2 BSD).
284                 SIGFPE    =  8, // Floating-point exception (ANSI).
285                 SIGKILL   =  9, // Kill, unblockable (POSIX).
286                 SIGUSR1   = 10, // User-defined signal 1 (POSIX).
287                 SIGSEGV   = 11, // Segmentation violation (ANSI).
288                 SIGUSR2   = 12, // User-defined signal 2 (POSIX).
289                 SIGPIPE   = 13, // Broken pipe (POSIX).
290                 SIGALRM   = 14, // Alarm clock (POSIX).
291                 SIGTERM   = 15, // Termination (ANSI).
292                 SIGSTKFLT = 16, // Stack fault.
293                 SIGCLD    = SIGCHLD, // Same as SIGCHLD (System V).
294                 SIGCHLD   = 17, // Child status has changed (POSIX).
295                 SIGCONT   = 18, // Continue (POSIX).
296                 SIGSTOP   = 19, // Stop, unblockable (POSIX).
297                 SIGTSTP   = 20, // Keyboard stop (POSIX).
298                 SIGTTIN   = 21, // Background read from tty (POSIX).
299                 SIGTTOU   = 22, // Background write to tty (POSIX).
300                 SIGURG    = 23, // Urgent condition on socket (4.2 BSD).
301                 SIGXCPU   = 24, // CPU limit exceeded (4.2 BSD).
302                 SIGXFSZ   = 25, // File size limit exceeded (4.2 BSD).
303                 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
304                 SIGPROF   = 27, // Profiling alarm clock (4.2 BSD).
305                 SIGWINCH  = 28, // Window size change (4.3 BSD, Sun).
306                 SIGPOLL   = SIGIO, // Pollable event occurred (System V).
307                 SIGIO     = 29, // I/O now possible (4.2 BSD).
308                 SIGPWR    = 30, // Power failure restart (System V).
309                 SIGSYS    = 31, // Bad system call.
310                 SIGUNUSED = 31
311         }
312
313         [Flags][Map]
314         public enum WaitOptions : int {
315                 WNOHANG   = 1,  // Don't block waiting
316                 WUNTRACED = 2,  // Report status of stopped children
317         }
318
319   [Flags][Map]
320         [CLSCompliant (false)]
321         public enum AccessModes : int {
322                 R_OK = 1,
323                 W_OK = 2,
324                 X_OK = 4,
325                 F_OK = 8,
326         }
327
328         [Map]
329         [CLSCompliant (false)]
330         public enum PathconfName : int {
331                 _PC_LINK_MAX,
332                 _PC_MAX_CANON,
333                 _PC_MAX_INPUT,
334                 _PC_NAME_MAX,
335                 _PC_PATH_MAX,
336                 _PC_PIPE_BUF,
337                 _PC_CHOWN_RESTRICTED,
338                 _PC_NO_TRUNC,
339                 _PC_VDISABLE,
340                 _PC_SYNC_IO,
341                 _PC_ASYNC_IO,
342                 _PC_PRIO_IO,
343                 _PC_SOCK_MAXBUF,
344                 _PC_FILESIZEBITS,
345                 _PC_REC_INCR_XFER_SIZE,
346                 _PC_REC_MAX_XFER_SIZE,
347                 _PC_REC_MIN_XFER_SIZE,
348                 _PC_REC_XFER_ALIGN,
349                 _PC_ALLOC_SIZE_MIN,
350                 _PC_SYMLINK_MAX,
351                 _PC_2_SYMLINKS
352         }
353
354         [Map]
355         [CLSCompliant (false)]
356         public enum SysconfName : int {
357                 _SC_ARG_MAX,
358                 _SC_CHILD_MAX,
359                 _SC_CLK_TCK,
360                 _SC_NGROUPS_MAX,
361                 _SC_OPEN_MAX,
362                 _SC_STREAM_MAX,
363                 _SC_TZNAME_MAX,
364                 _SC_JOB_CONTROL,
365                 _SC_SAVED_IDS,
366                 _SC_REALTIME_SIGNALS,
367                 _SC_PRIORITY_SCHEDULING,
368                 _SC_TIMERS,
369                 _SC_ASYNCHRONOUS_IO,
370                 _SC_PRIORITIZED_IO,
371                 _SC_SYNCHRONIZED_IO,
372                 _SC_FSYNC,
373                 _SC_MAPPED_FILES,
374                 _SC_MEMLOCK,
375                 _SC_MEMLOCK_RANGE,
376                 _SC_MEMORY_PROTECTION,
377                 _SC_MESSAGE_PASSING,
378                 _SC_SEMAPHORES,
379                 _SC_SHARED_MEMORY_OBJECTS,
380                 _SC_AIO_LISTIO_MAX,
381                 _SC_AIO_MAX,
382                 _SC_AIO_PRIO_DELTA_MAX,
383                 _SC_DELAYTIMER_MAX,
384                 _SC_MQ_OPEN_MAX,
385                 _SC_MQ_PRIO_MAX,
386                 _SC_VERSION,
387                 _SC_PAGESIZE,
388                 _SC_RTSIG_MAX,
389                 _SC_SEM_NSEMS_MAX,
390                 _SC_SEM_VALUE_MAX,
391                 _SC_SIGQUEUE_MAX,
392                 _SC_TIMER_MAX,
393                 /* Values for the argument to `sysconf'
394                          corresponding to _POSIX2_* symbols.  */
395                 _SC_BC_BASE_MAX,
396                 _SC_BC_DIM_MAX,
397                 _SC_BC_SCALE_MAX,
398                 _SC_BC_STRING_MAX,
399                 _SC_COLL_WEIGHTS_MAX,
400                 _SC_EQUIV_CLASS_MAX,
401                 _SC_EXPR_NEST_MAX,
402                 _SC_LINE_MAX,
403                 _SC_RE_DUP_MAX,
404                 _SC_CHARCLASS_NAME_MAX,
405                 _SC_2_VERSION,
406                 _SC_2_C_BIND,
407                 _SC_2_C_DEV,
408                 _SC_2_FORT_DEV,
409                 _SC_2_FORT_RUN,
410                 _SC_2_SW_DEV,
411                 _SC_2_LOCALEDEF,
412                 _SC_PII,
413                 _SC_PII_XTI,
414                 _SC_PII_SOCKET,
415                 _SC_PII_INTERNET,
416                 _SC_PII_OSI,
417                 _SC_POLL,
418                 _SC_SELECT,
419                 _SC_UIO_MAXIOV,
420                 _SC_IOV_MAX = _SC_UIO_MAXIOV,
421                 _SC_PII_INTERNET_STREAM,
422                 _SC_PII_INTERNET_DGRAM,
423                 _SC_PII_OSI_COTS,
424                 _SC_PII_OSI_CLTS,
425                 _SC_PII_OSI_M,
426                 _SC_T_IOV_MAX,
427                 /* Values according to POSIX 1003.1c (POSIX threads).  */
428                 _SC_THREADS,
429                 _SC_THREAD_SAFE_FUNCTIONS,
430                 _SC_GETGR_R_SIZE_MAX,
431                 _SC_GETPW_R_SIZE_MAX,
432                 _SC_LOGIN_NAME_MAX,
433                 _SC_TTY_NAME_MAX,
434                 _SC_THREAD_DESTRUCTOR_ITERATIONS,
435                 _SC_THREAD_KEYS_MAX,
436                 _SC_THREAD_STACK_MIN,
437                 _SC_THREAD_THREADS_MAX,
438                 _SC_THREAD_ATTR_STACKADDR,
439                 _SC_THREAD_ATTR_STACKSIZE,
440                 _SC_THREAD_PRIORITY_SCHEDULING,
441                 _SC_THREAD_PRIO_INHERIT,
442                 _SC_THREAD_PRIO_PROTECT,
443                 _SC_THREAD_PROCESS_SHARED,
444                 _SC_NPROCESSORS_CONF,
445                 _SC_NPROCESSORS_ONLN,
446                 _SC_PHYS_PAGES,
447                 _SC_AVPHYS_PAGES,
448                 _SC_ATEXIT_MAX,
449                 _SC_PASS_MAX,
450                 _SC_XOPEN_VERSION,
451                 _SC_XOPEN_XCU_VERSION,
452                 _SC_XOPEN_UNIX,
453                 _SC_XOPEN_CRYPT,
454                 _SC_XOPEN_ENH_I18N,
455                 _SC_XOPEN_SHM,
456                 _SC_2_CHAR_TERM,
457                 _SC_2_C_VERSION,
458                 _SC_2_UPE,
459                 _SC_XOPEN_XPG2,
460                 _SC_XOPEN_XPG3,
461                 _SC_XOPEN_XPG4,
462                 _SC_CHAR_BIT,
463                 _SC_CHAR_MAX,
464                 _SC_CHAR_MIN,
465                 _SC_INT_MAX,
466                 _SC_INT_MIN,
467                 _SC_LONG_BIT,
468                 _SC_WORD_BIT,
469                 _SC_MB_LEN_MAX,
470                 _SC_NZERO,
471                 _SC_SSIZE_MAX,
472                 _SC_SCHAR_MAX,
473                 _SC_SCHAR_MIN,
474                 _SC_SHRT_MAX,
475                 _SC_SHRT_MIN,
476                 _SC_UCHAR_MAX,
477                 _SC_UINT_MAX,
478                 _SC_ULONG_MAX,
479                 _SC_USHRT_MAX,
480                 _SC_NL_ARGMAX,
481                 _SC_NL_LANGMAX,
482                 _SC_NL_MSGMAX,
483                 _SC_NL_NMAX,
484                 _SC_NL_SETMAX,
485                 _SC_NL_TEXTMAX,
486                 _SC_XBS5_ILP32_OFF32,
487                 _SC_XBS5_ILP32_OFFBIG,
488                 _SC_XBS5_LP64_OFF64,
489                 _SC_XBS5_LPBIG_OFFBIG,
490                 _SC_XOPEN_LEGACY,
491                 _SC_XOPEN_REALTIME,
492                 _SC_XOPEN_REALTIME_THREADS,
493                 _SC_ADVISORY_INFO,
494                 _SC_BARRIERS,
495                 _SC_BASE,
496                 _SC_C_LANG_SUPPORT,
497                 _SC_C_LANG_SUPPORT_R,
498                 _SC_CLOCK_SELECTION,
499                 _SC_CPUTIME,
500                 _SC_THREAD_CPUTIME,
501                 _SC_DEVICE_IO,
502                 _SC_DEVICE_SPECIFIC,
503                 _SC_DEVICE_SPECIFIC_R,
504                 _SC_FD_MGMT,
505                 _SC_FIFO,
506                 _SC_PIPE,
507                 _SC_FILE_ATTRIBUTES,
508                 _SC_FILE_LOCKING,
509                 _SC_FILE_SYSTEM,
510                 _SC_MONOTONIC_CLOCK,
511                 _SC_MULTI_PROCESS,
512                 _SC_SINGLE_PROCESS,
513                 _SC_NETWORKING,
514                 _SC_READER_WRITER_LOCKS,
515                 _SC_SPIN_LOCKS,
516                 _SC_REGEXP,
517                 _SC_REGEX_VERSION,
518                 _SC_SHELL,
519                 _SC_SIGNALS,
520                 _SC_SPAWN,
521                 _SC_SPORADIC_SERVER,
522                 _SC_THREAD_SPORADIC_SERVER,
523                 _SC_SYSTEM_DATABASE,
524                 _SC_SYSTEM_DATABASE_R,
525                 _SC_TIMEOUTS,
526                 _SC_TYPED_MEMORY_OBJECTS,
527                 _SC_USER_GROUPS,
528                 _SC_USER_GROUPS_R,
529                 _SC_2_PBS,
530                 _SC_2_PBS_ACCOUNTING,
531                 _SC_2_PBS_LOCATE,
532                 _SC_2_PBS_MESSAGE,
533                 _SC_2_PBS_TRACK,
534                 _SC_SYMLOOP_MAX,
535                 _SC_STREAMS,
536                 _SC_2_PBS_CHECKPOINT,
537                 _SC_V6_ILP32_OFF32,
538                 _SC_V6_ILP32_OFFBIG,
539                 _SC_V6_LP64_OFF64,
540                 _SC_V6_LPBIG_OFFBIG,
541                 _SC_HOST_NAME_MAX,
542                 _SC_TRACE,
543                 _SC_TRACE_EVENT_FILTER,
544                 _SC_TRACE_INHERIT,
545                 _SC_TRACE_LOG,
546                 _SC_LEVEL1_ICACHE_SIZE,
547                 _SC_LEVEL1_ICACHE_ASSOC,
548                 _SC_LEVEL1_ICACHE_LINESIZE,
549                 _SC_LEVEL1_DCACHE_SIZE,
550                 _SC_LEVEL1_DCACHE_ASSOC,
551                 _SC_LEVEL1_DCACHE_LINESIZE,
552                 _SC_LEVEL2_CACHE_SIZE,
553                 _SC_LEVEL2_CACHE_ASSOC,
554                 _SC_LEVEL2_CACHE_LINESIZE,
555                 _SC_LEVEL3_CACHE_SIZE,
556                 _SC_LEVEL3_CACHE_ASSOC,
557                 _SC_LEVEL3_CACHE_LINESIZE,
558                 _SC_LEVEL4_CACHE_SIZE,
559                 _SC_LEVEL4_CACHE_ASSOC,
560                 _SC_LEVEL4_CACHE_LINESIZE
561         }
562
563         [Map]
564         [CLSCompliant (false)]
565         public enum ConfstrName : int {
566                 _CS_PATH,                       /* The default search path.  */
567                 _CS_V6_WIDTH_RESTRICTED_ENVS,
568                 _CS_GNU_LIBC_VERSION,
569                 _CS_GNU_LIBPTHREAD_VERSION,
570                 _CS_LFS_CFLAGS = 1000,
571                 _CS_LFS_LDFLAGS,
572                 _CS_LFS_LIBS,
573                 _CS_LFS_LINTFLAGS,
574                 _CS_LFS64_CFLAGS,
575                 _CS_LFS64_LDFLAGS,
576                 _CS_LFS64_LIBS,
577                 _CS_LFS64_LINTFLAGS,
578                 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
579                 _CS_XBS5_ILP32_OFF32_LDFLAGS,
580                 _CS_XBS5_ILP32_OFF32_LIBS,
581                 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
582                 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
583                 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
584                 _CS_XBS5_ILP32_OFFBIG_LIBS,
585                 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
586                 _CS_XBS5_LP64_OFF64_CFLAGS,
587                 _CS_XBS5_LP64_OFF64_LDFLAGS,
588                 _CS_XBS5_LP64_OFF64_LIBS,
589                 _CS_XBS5_LP64_OFF64_LINTFLAGS,
590                 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
591                 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
592                 _CS_XBS5_LPBIG_OFFBIG_LIBS,
593                 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
594                 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
595                 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
596                 _CS_POSIX_V6_ILP32_OFF32_LIBS,
597                 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
598                 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
599                 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
600                 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
601                 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
602                 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
603                 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
604                 _CS_POSIX_V6_LP64_OFF64_LIBS,
605                 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
606                 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
607                 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
608                 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
609                 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
610         }
611
612         [Map]
613         [CLSCompliant (false)]
614         public enum LockfCommand : int {
615                 F_ULOCK = 0, // Unlock a previously locked region.
616                 F_LOCK  = 1, // Lock a region for exclusive use.
617                 F_TLOCK = 2, // Test and lock a region for exclusive use.
618                 F_TEST  = 3, // Test a region for other process locks.
619         }
620
621         [Map][Flags]
622         public enum PollEvents : short {
623                 POLLIN      = 0x0001, // There is data to read
624                 POLLPRI     = 0x0002, // There is urgent data to read
625                 POLLOUT     = 0x0004, // Writing now will not block
626                 POLLERR     = 0x0008, // Error condition
627                 POLLHUP     = 0x0010, // Hung up
628                 POLLNVAL    = 0x0020, // Invalid request; fd not open
629                 // XPG4.2 definitions (via _XOPEN_SOURCE)
630                 POLLRDNORM  = 0x0040, // Normal data may be read
631                 POLLRDBAND  = 0x0080, // Priority data may be read
632                 POLLWRNORM  = 0x0100, // Writing now will not block
633                 POLLWRBAND  = 0x0200, // Priority data may be written
634         }
635
636         [Map][Flags]
637         [CLSCompliant (false)]
638         public enum XattrFlags : int {
639                 XATTR_AUTO = 0,
640                 XATTR_CREATE = 1,
641                 XATTR_REPLACE = 2,
642         }
643
644         [Map][Flags]
645         [CLSCompliant (false)]
646         public enum MountFlags : ulong {
647                 ST_RDONLY      =    1,  // Mount read-only
648                 ST_NOSUID      =    2,  // Ignore suid and sgid bits
649                 ST_NODEV       =    4,  // Disallow access to device special files
650                 ST_NOEXEC      =    8,  // Disallow program execution
651                 ST_SYNCHRONOUS =   16,  // Writes are synced at once
652                 ST_REMOUNT     =   32,  // Alter flags of a mounted FS
653                 ST_MANDLOCK    =   64,  // Allow mandatory locks on an FS
654                 ST_WRITE       =  128,  // Write on file/directory/symlink
655                 ST_APPEND      =  256,  // Append-only file
656                 ST_IMMUTABLE   =  512,  // Immutable file
657                 ST_NOATIME     = 1024,  // Do not update access times
658                 ST_NODIRATIME  = 2048,  // Do not update directory access times
659                 ST_BIND        = 4096,  // Bind directory at different place
660         }
661
662         [Map][Flags]
663         [CLSCompliant (false)]
664         public enum MmapFlags : int {
665                 MAP_SHARED      = 0x01,     // Share changes.
666                 MAP_PRIVATE     = 0x02,     // Changes are private.
667                 MAP_TYPE        = 0x0f,     // Mask for type of mapping.
668                 MAP_FIXED       = 0x10,     // Interpret addr exactly.
669                 MAP_FILE        = 0,
670                 MAP_ANONYMOUS   = 0x20,     // Don't use a file.
671                 MAP_ANON        = MAP_ANONYMOUS,
672
673                 // These are Linux-specific.
674                 MAP_GROWSDOWN   = 0x00100,  // Stack-like segment.
675                 MAP_DENYWRITE   = 0x00800,  // ETXTBSY
676                 MAP_EXECUTABLE  = 0x01000,  // Mark it as an executable.
677                 MAP_LOCKED      = 0x02000,  // Lock the mapping.
678                 MAP_NORESERVE   = 0x04000,  // Don't check for reservations.
679                 MAP_POPULATE    = 0x08000,  // Populate (prefault) pagetables.
680                 MAP_NONBLOCK    = 0x10000,  // Do not block on IO.
681         }
682
683         [Map][Flags]
684         [CLSCompliant (false)]
685         public enum MmapProts : int {
686                 PROT_READ       = 0x1,  // Page can be read.
687                 PROT_WRITE      = 0x2,  // Page can be written.
688                 PROT_EXEC       = 0x4,  // Page can be executed.
689                 PROT_NONE       = 0x0,  // Page can not be accessed.
690                 PROT_GROWSDOWN  = 0x01000000, // Extend change to start of
691                                               //   growsdown vma (mprotect only).
692                 PROT_GROWSUP    = 0x02000000, // Extend change to start of
693                                               //   growsup vma (mprotect only).
694         }
695
696         [Map][Flags]
697         [CLSCompliant (false)]
698         public enum MsyncFlags : int {
699                 MS_ASYNC      = 0x1,  // Sync memory asynchronously.
700                 MS_SYNC       = 0x4,  // Synchronous memory sync.
701                 MS_INVALIDATE = 0x2,  // Invalidate the caches.
702         }
703
704         [Map][Flags]
705         [CLSCompliant (false)]
706         public enum MlockallFlags : int {
707                 MCL_CURRENT     = 0x1,  // Lock all currently mapped pages.
708                 MCL_FUTURE  = 0x2,      // Lock all additions to address
709         }
710
711         [Map][Flags]
712         [CLSCompliant (false)]
713         public enum MremapFlags : ulong {
714                 MREMAP_MAYMOVE = 0x1,
715         }
716
717         #endregion
718
719         #region Structures
720
721         [Map ("struct flock")]
722         public struct Flock
723 #if NET_2_0
724                 : IEquatable <Flock>
725 #endif
726         {
727                 [CLSCompliant (false)]
728                 public LockType         l_type;    // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
729                 [CLSCompliant (false)]
730                 public SeekFlags        l_whence;  // How to interpret l_start
731                 [off_t] public long     l_start;   // Starting offset for lock
732                 [off_t] public long     l_len;     // Number of bytes to lock
733                 [pid_t] public int      l_pid;     // PID of process blocking our lock (F_GETLK only)
734
735                 public override int GetHashCode ()
736                 {
737                         return l_type.GetHashCode () ^ l_whence.GetHashCode () ^ 
738                                 l_start.GetHashCode () ^ l_len.GetHashCode () ^
739                                 l_pid.GetHashCode ();
740                 }
741
742                 public override bool Equals (object obj)
743                 {
744                         if ((obj == null) || (obj.GetType () != GetType ()))
745                                 return false;
746                         Flock value = (Flock) obj;
747                         return l_type == value.l_type && l_whence == value.l_whence && 
748                                 l_start == value.l_start && l_len == value.l_len && 
749                                 l_pid == value.l_pid;
750                 }
751
752                 public bool Equals (Flock value)
753                 {
754                         return l_type == value.l_type && l_whence == value.l_whence && 
755                                 l_start == value.l_start && l_len == value.l_len && 
756                                 l_pid == value.l_pid;
757                 }
758
759                 public static bool operator== (Flock lhs, Flock rhs)
760                 {
761                         return lhs.Equals (rhs);
762                 }
763
764                 public static bool operator!= (Flock lhs, Flock rhs)
765                 {
766                         return !lhs.Equals (rhs);
767                 }
768         }
769
770         [Map ("struct pollfd")]
771         public struct Pollfd
772 #if NET_2_0
773                 : IEquatable <Pollfd>
774 #endif
775         {
776                 public int fd;
777                 [CLSCompliant (false)]
778                 public PollEvents events;
779                 [CLSCompliant (false)]
780                 public PollEvents revents;
781
782                 public override int GetHashCode ()
783                 {
784                         return events.GetHashCode () ^ revents.GetHashCode ();
785                 }
786
787                 public override bool Equals (object obj)
788                 {
789                         if (obj == null || obj.GetType () != GetType ())
790                                 return false;
791                         Pollfd value = (Pollfd) obj;
792                         return value.events == events && value.revents == revents;
793                 }
794
795                 public bool Equals (Pollfd value)
796                 {
797                         return value.events == events && value.revents == revents;
798                 }
799
800                 public static bool operator== (Pollfd lhs, Pollfd rhs)
801                 {
802                         return lhs.Equals (rhs);
803                 }
804
805                 public static bool operator!= (Pollfd lhs, Pollfd rhs)
806                 {
807                         return !lhs.Equals (rhs);
808                 }
809         }
810
811         [Map ("struct stat")]
812         public struct Stat
813 #if NET_2_0
814                 : IEquatable <Stat>
815 #endif
816         {
817                 [CLSCompliant (false)]
818                 [dev_t]     public ulong    st_dev;     // device
819                 [CLSCompliant (false)]
820                 [ino_t]     public  ulong   st_ino;     // inode
821                 [CLSCompliant (false)]
822                 public  FilePermissions     st_mode;    // protection
823                 [NonSerialized]
824 #pragma warning disable 169             
825                 private uint                _padding_;  // padding for structure alignment
826 #pragma warning restore 169             
827                 [CLSCompliant (false)]
828                 [nlink_t]   public  ulong   st_nlink;   // number of hard links
829                 [CLSCompliant (false)]
830                 [uid_t]     public  uint    st_uid;     // user ID of owner
831                 [CLSCompliant (false)]
832                 [gid_t]     public  uint    st_gid;     // group ID of owner
833                 [CLSCompliant (false)]
834                 [dev_t]     public  ulong   st_rdev;    // device type (if inode device)
835                 [off_t]     public  long    st_size;    // total size, in bytes
836                 [blksize_t] public  long    st_blksize; // blocksize for filesystem I/O
837                 [blkcnt_t]  public  long    st_blocks;  // number of blocks allocated
838                 [time_t]    public  long    st_atime;   // time of last access
839                 [time_t]    public  long    st_mtime;   // time of last modification
840                 [time_t]    public  long    st_ctime;   // time of last status change
841
842                 public override int GetHashCode ()
843                 {
844                         return st_dev.GetHashCode () ^
845                                 st_ino.GetHashCode () ^
846                                 st_mode.GetHashCode () ^
847                                 st_nlink.GetHashCode () ^
848                                 st_uid.GetHashCode () ^
849                                 st_gid.GetHashCode () ^
850                                 st_rdev.GetHashCode () ^
851                                 st_size.GetHashCode () ^
852                                 st_blksize.GetHashCode () ^
853                                 st_blocks.GetHashCode () ^
854                                 st_atime.GetHashCode () ^
855                                 st_mtime.GetHashCode () ^
856                                 st_ctime.GetHashCode ();
857                 }
858
859                 public override bool Equals (object obj)
860                 {
861                         if (obj == null || obj.GetType() != GetType ())
862                                 return false;
863                         Stat value = (Stat) obj;
864                         return value.st_dev == st_dev &&
865                                 value.st_ino == st_ino &&
866                                 value.st_mode == st_mode &&
867                                 value.st_nlink == st_nlink &&
868                                 value.st_uid == st_uid &&
869                                 value.st_gid == st_gid &&
870                                 value.st_rdev == st_rdev &&
871                                 value.st_size == st_size &&
872                                 value.st_blksize == st_blksize &&
873                                 value.st_blocks == st_blocks &&
874                                 value.st_atime == st_atime &&
875                                 value.st_mtime == st_mtime &&
876                                 value.st_ctime == st_ctime;
877                 }
878
879                 public bool Equals (Stat value)
880                 {
881                         return value.st_dev == st_dev &&
882                                 value.st_ino == st_ino &&
883                                 value.st_mode == st_mode &&
884                                 value.st_nlink == st_nlink &&
885                                 value.st_uid == st_uid &&
886                                 value.st_gid == st_gid &&
887                                 value.st_rdev == st_rdev &&
888                                 value.st_size == st_size &&
889                                 value.st_blksize == st_blksize &&
890                                 value.st_blocks == st_blocks &&
891                                 value.st_atime == st_atime &&
892                                 value.st_mtime == st_mtime &&
893                                 value.st_ctime == st_ctime;
894                 }
895
896                 public static bool operator== (Stat lhs, Stat rhs)
897                 {
898                         return lhs.Equals (rhs);
899                 }
900
901                 public static bool operator!= (Stat lhs, Stat rhs)
902                 {
903                         return !lhs.Equals (rhs);
904                 }
905         }
906
907         // `struct statvfs' isn't portable, so don't generate To/From methods.
908         [Map]
909         [CLSCompliant (false)]
910         public struct Statvfs
911 #if NET_2_0
912                 : IEquatable <Statvfs>
913 #endif
914         {
915                 public                  ulong f_bsize;    // file system block size
916                 public                  ulong f_frsize;   // fragment size
917                 [fsblkcnt_t] public     ulong f_blocks;   // size of fs in f_frsize units
918                 [fsblkcnt_t] public     ulong f_bfree;    // # free blocks
919                 [fsblkcnt_t] public     ulong f_bavail;   // # free blocks for non-root
920                 [fsfilcnt_t] public     ulong f_files;    // # inodes
921                 [fsfilcnt_t] public     ulong f_ffree;    // # free inodes
922                 [fsfilcnt_t] public     ulong f_favail;   // # free inodes for non-root
923                 public                  ulong f_fsid;     // file system id
924                 public MountFlags             f_flag;     // mount flags
925                 public                  ulong f_namemax;  // maximum filename length
926
927                 public override int GetHashCode ()
928                 {
929                         return f_bsize.GetHashCode () ^
930                                 f_frsize.GetHashCode () ^
931                                 f_blocks.GetHashCode () ^
932                                 f_bfree.GetHashCode () ^
933                                 f_bavail.GetHashCode () ^
934                                 f_files.GetHashCode () ^
935                                 f_ffree.GetHashCode () ^
936                                 f_favail.GetHashCode () ^
937                                 f_fsid.GetHashCode () ^
938                                 f_flag.GetHashCode () ^
939                                 f_namemax.GetHashCode ();
940                 }
941
942                 public override bool Equals (object obj)
943                 {
944                         if (obj == null || obj.GetType() != GetType ())
945                                 return false;
946                         Statvfs value = (Statvfs) obj;
947                         return value.f_bsize == f_bsize &&
948                                 value.f_frsize == f_frsize &&
949                                 value.f_blocks == f_blocks &&
950                                 value.f_bfree == f_bfree &&
951                                 value.f_bavail == f_bavail &&
952                                 value.f_files == f_files &&
953                                 value.f_ffree == f_ffree &&
954                                 value.f_favail == f_favail &&
955                                 value.f_fsid == f_fsid &&
956                                 value.f_flag == f_flag &&
957                                 value.f_namemax == f_namemax;
958                 }
959
960                 public bool Equals (Statvfs value)
961                 {
962                         return value.f_bsize == f_bsize &&
963                                 value.f_frsize == f_frsize &&
964                                 value.f_blocks == f_blocks &&
965                                 value.f_bfree == f_bfree &&
966                                 value.f_bavail == f_bavail &&
967                                 value.f_files == f_files &&
968                                 value.f_ffree == f_ffree &&
969                                 value.f_favail == f_favail &&
970                                 value.f_fsid == f_fsid &&
971                                 value.f_flag == f_flag &&
972                                 value.f_namemax == f_namemax;
973                 }
974
975                 public static bool operator== (Statvfs lhs, Statvfs rhs)
976                 {
977                         return lhs.Equals (rhs);
978                 }
979
980                 public static bool operator!= (Statvfs lhs, Statvfs rhs)
981                 {
982                         return !lhs.Equals (rhs);
983                 }
984         }
985
986         [Map ("struct timeval")]
987         public struct Timeval
988 #if NET_2_0
989                 : IEquatable <Timeval>
990 #endif
991         {
992                 [time_t]      public long tv_sec;   // seconds
993                 [suseconds_t] public long tv_usec;  // microseconds
994
995                 public override int GetHashCode ()
996                 {
997                         return tv_sec.GetHashCode () ^ tv_usec.GetHashCode ();
998                 }
999
1000                 public override bool Equals (object obj)
1001                 {
1002                         if (obj == null || obj.GetType () != GetType ())
1003                                 return false;
1004                         Timeval value = (Timeval) obj;
1005                         return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1006                 }
1007
1008                 public bool Equals (Timeval value)
1009                 {
1010                         return value.tv_sec == tv_sec && value.tv_usec == tv_usec;
1011                 }
1012
1013                 public static bool operator== (Timeval lhs, Timeval rhs)
1014                 {
1015                         return lhs.Equals (rhs);
1016                 }
1017
1018                 public static bool operator!= (Timeval lhs, Timeval rhs)
1019                 {
1020                         return !lhs.Equals (rhs);
1021                 }
1022         }
1023
1024         [Map ("struct timezone")]
1025         public struct Timezone
1026 #if NET_2_0
1027                 : IEquatable <Timezone>
1028 #endif
1029         {
1030                 public  int tz_minuteswest; // minutes W of Greenwich
1031 #pragma warning disable 169             
1032                 private int tz_dsttime;     // type of dst correction (OBSOLETE)
1033 #pragma warning restore 169             
1034
1035                 public override int GetHashCode ()
1036                 {
1037                         return tz_minuteswest.GetHashCode ();
1038                 }
1039
1040                 public override bool Equals (object obj)
1041                 {
1042                         if (obj == null || obj.GetType () != GetType ())
1043                                 return false;
1044                         Timezone value = (Timezone) obj;
1045                         return value.tz_minuteswest == tz_minuteswest;
1046                 }
1047
1048                 public bool Equals (Timezone value)
1049                 {
1050                         return value.tz_minuteswest == tz_minuteswest;
1051                 }
1052
1053                 public static bool operator== (Timezone lhs, Timezone rhs)
1054                 {
1055                         return lhs.Equals (rhs);
1056                 }
1057
1058                 public static bool operator!= (Timezone lhs, Timezone rhs)
1059                 {
1060                         return !lhs.Equals (rhs);
1061                 }
1062         }
1063
1064         [Map ("struct utimbuf")]
1065         public struct Utimbuf
1066 #if NET_2_0
1067                 : IEquatable <Utimbuf>
1068 #endif
1069         {
1070                 [time_t] public long    actime;   // access time
1071                 [time_t] public long    modtime;  // modification time
1072
1073                 public override int GetHashCode ()
1074                 {
1075                         return actime.GetHashCode () ^ modtime.GetHashCode ();
1076                 }
1077
1078                 public override bool Equals (object obj)
1079                 {
1080                         if (obj == null || obj.GetType () != GetType ())
1081                                 return false;
1082                         Utimbuf value = (Utimbuf) obj;
1083                         return value.actime == actime && value.modtime == modtime;
1084                 }
1085
1086                 public bool Equals (Utimbuf value)
1087                 {
1088                         return value.actime == actime && value.modtime == modtime;
1089                 }
1090
1091                 public static bool operator== (Utimbuf lhs, Utimbuf rhs)
1092                 {
1093                         return lhs.Equals (rhs);
1094                 }
1095
1096                 public static bool operator!= (Utimbuf lhs, Utimbuf rhs)
1097                 {
1098                         return !lhs.Equals (rhs);
1099                 }
1100         }
1101
1102         [Map ("struct timespec")]
1103         public struct Timespec
1104 #if NET_2_0
1105                 : IEquatable <Timespec>
1106 #endif
1107         {
1108                 [time_t] public long    tv_sec;   // Seconds.
1109                 public          long    tv_nsec;  // Nanoseconds.
1110
1111                 public override int GetHashCode ()
1112                 {
1113                         return tv_sec.GetHashCode () ^ tv_nsec.GetHashCode ();
1114                 }
1115
1116                 public override bool Equals (object obj)
1117                 {
1118                         if (obj == null || obj.GetType () != GetType ())
1119                                 return false;
1120                         Timespec value = (Timespec) obj;
1121                         return value.tv_sec == tv_sec && value.tv_nsec == tv_nsec;
1122                 }
1123
1124                 public bool Equals (Timespec value)
1125                 {
1126                         return value.tv_sec == tv_sec && value.tv_nsec == tv_nsec;
1127                 }
1128
1129                 public static bool operator== (Timespec lhs, Timespec rhs)
1130                 {
1131                         return lhs.Equals (rhs);
1132                 }
1133
1134                 public static bool operator!= (Timespec lhs, Timespec rhs)
1135                 {
1136                         return !lhs.Equals (rhs);
1137                 }
1138         }
1139
1140         [Flags][Map]
1141         public enum EpollFlags {
1142                 EPOLL_CLOEXEC = 02000000,
1143                 EPOLL_NONBLOCK = 04000,
1144         }
1145
1146         [Flags][Map]
1147         [CLSCompliant (false)]
1148         public enum EpollEvents : uint {
1149                 EPOLLIN = 0x001,
1150                 EPOLLPRI = 0x002,
1151                 EPOLLOUT = 0x004,
1152                 EPOLLRDNORM = 0x040,
1153                 EPOLLRDBAND = 0x080,
1154                 EPOLLWRNORM = 0x100,
1155                 EPOLLWRBAND = 0x200,
1156                 EPOLLMSG = 0x400,
1157                 EPOLLERR = 0x008,
1158                 EPOLLHUP = 0x010,
1159                 EPOLLRDHUP = 0x2000,
1160                 EPOLLONESHOT = 1 << 30,
1161                 EPOLLET = unchecked ((uint) (1 << 31))
1162         }
1163
1164         public enum EpollOp {
1165                 EPOLL_CTL_ADD = 1,
1166                 EPOLL_CTL_DEL = 2,
1167                 EPOLL_CTL_MOD = 3,
1168         }
1169
1170         [StructLayout (LayoutKind.Explicit, Size=12, Pack=1)]
1171         [CLSCompliant (false)]
1172         public struct EpollEvent {
1173                 [FieldOffset (0)]
1174                 public EpollEvents events;
1175                 [FieldOffset (4)]
1176                 public int fd;
1177                 [FieldOffset (4)]
1178                 public IntPtr ptr;
1179                 [FieldOffset (4)]
1180                 public uint u32;
1181                 [FieldOffset (4)]
1182                 public ulong u64;
1183         }
1184         #endregion
1185
1186         #region Classes
1187
1188         public sealed class Dirent
1189 #if NET_2_0
1190                 : IEquatable <Dirent>
1191 #endif
1192         {
1193                 [CLSCompliant (false)]
1194                 public /* ino_t */ ulong  d_ino;
1195                 public /* off_t */ long   d_off;
1196                 [CLSCompliant (false)]
1197                 public ushort             d_reclen;
1198                 public byte               d_type;
1199                 public string             d_name;
1200
1201                 public override int GetHashCode ()
1202                 {
1203                         return d_ino.GetHashCode () ^ d_off.GetHashCode () ^ 
1204                                 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
1205                                 d_name.GetHashCode ();
1206                 }
1207
1208                 public override bool Equals (object obj)
1209                 {
1210                         if (obj == null || GetType() != obj.GetType())
1211                                 return false;
1212                         Dirent d = (Dirent) obj;
1213                         return Equals (d);
1214                 }
1215
1216                 public bool Equals (Dirent value)
1217                 {
1218                         if (value == null)
1219                                 return false;
1220                         return value.d_ino == d_ino && value.d_off == d_off &&
1221                                 value.d_reclen == d_reclen && value.d_type == d_type &&
1222                                 value.d_name == d_name;
1223                 }
1224
1225                 public override string ToString ()
1226                 {
1227                         return d_name;
1228                 }
1229
1230                 public static bool operator== (Dirent lhs, Dirent rhs)
1231                 {
1232                         return Object.Equals (lhs, rhs);
1233                 }
1234
1235                 public static bool operator!= (Dirent lhs, Dirent rhs)
1236                 {
1237                         return !Object.Equals (lhs, rhs);
1238                 }
1239         }
1240
1241         public sealed class Fstab
1242 #if NET_2_0
1243                 : IEquatable <Fstab>
1244 #endif
1245         {
1246                 public string fs_spec;
1247                 public string fs_file;
1248                 public string fs_vfstype;
1249                 public string fs_mntops;
1250                 public string fs_type;
1251                 public int    fs_freq;
1252                 public int    fs_passno;
1253
1254                 public override int GetHashCode ()
1255                 {
1256                         return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
1257                                 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
1258                                 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
1259                 }
1260
1261                 public override bool Equals (object obj)
1262                 {
1263                         if (obj == null || GetType() != obj.GetType())
1264                                 return false;
1265                         Fstab f = (Fstab) obj;
1266                         return Equals (f);
1267                 }
1268
1269                 public bool Equals (Fstab value)
1270                 {
1271                         if (value == null)
1272                                 return false;
1273                         return value.fs_spec == fs_spec && value.fs_file == fs_file &&
1274                                 value.fs_vfstype == fs_vfstype && value.fs_mntops == fs_mntops &&
1275                                 value.fs_type == fs_type && value.fs_freq == fs_freq && 
1276                                 value.fs_passno == fs_passno;
1277                 }
1278
1279                 public override string ToString ()
1280                 {
1281                         return fs_spec;
1282                 }
1283
1284                 public static bool operator== (Fstab lhs, Fstab rhs)
1285                 {
1286                         return Object.Equals (lhs, rhs);
1287                 }
1288
1289                 public static bool operator!= (Fstab lhs, Fstab rhs)
1290                 {
1291                         return !Object.Equals (lhs, rhs);
1292                 }
1293         }
1294
1295         public sealed class Group
1296 #if NET_2_0
1297                 : IEquatable <Group>
1298 #endif
1299         {
1300                 public string           gr_name;
1301                 public string           gr_passwd;
1302                 [CLSCompliant (false)]
1303                 public /* gid_t */ uint gr_gid;
1304                 public string[]         gr_mem;
1305
1306                 public override int GetHashCode ()
1307                 {
1308                         int memhc = 0;
1309                         for (int i = 0; i < gr_mem.Length; ++i)
1310                                 memhc ^= gr_mem[i].GetHashCode ();
1311
1312                         return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^ 
1313                                 gr_gid.GetHashCode () ^ memhc;
1314                 }
1315
1316                 public override bool Equals (object obj)
1317                 {
1318                         if (obj == null || GetType() != obj.GetType())
1319                                 return false;
1320                         Group g = (Group) obj;
1321                         return Equals (g);
1322                 }
1323
1324                 public bool Equals (Group value)
1325                 {
1326                         if (value == null)
1327                                 return false;
1328                         if (value.gr_gid != gr_gid)
1329                                 return false;
1330                         if (value.gr_gid == gr_gid && value.gr_name == gr_name &&
1331                                 value.gr_passwd == gr_passwd) {
1332                                 if (value.gr_mem == gr_mem)
1333                                         return true;
1334                                 if (value.gr_mem == null || gr_mem == null)
1335                                         return false;
1336                                 if (value.gr_mem.Length != gr_mem.Length)
1337                                         return false;
1338                                 for (int i = 0; i < gr_mem.Length; ++i)
1339                                         if (gr_mem[i] != value.gr_mem[i])
1340                                                 return false;
1341                                 return true;
1342                         }
1343                         return false;
1344                 }
1345
1346                 // Generate string in /etc/group format
1347                 public override string ToString ()
1348                 {
1349                         StringBuilder sb = new StringBuilder ();
1350                         sb.Append (gr_name).Append (":").Append (gr_passwd).Append (":");
1351                         sb.Append (gr_gid).Append (":");
1352                         GetMembers (sb, gr_mem);
1353                         return sb.ToString ();
1354                 }
1355
1356                 private static void GetMembers (StringBuilder sb, string[] members)
1357                 {
1358                         if (members.Length > 0)
1359                                 sb.Append (members[0]);
1360                         for (int i = 1; i < members.Length; ++i) {
1361                                 sb.Append (",");
1362                                 sb.Append (members[i]);
1363                         }
1364                 }
1365
1366                 public static bool operator== (Group lhs, Group rhs)
1367                 {
1368                         return Object.Equals (lhs, rhs);
1369                 }
1370
1371                 public static bool operator!= (Group lhs, Group rhs)
1372                 {
1373                         return !Object.Equals (lhs, rhs);
1374                 }
1375         }
1376
1377         public sealed class Passwd
1378 #if NET_2_0
1379                 : IEquatable <Passwd>
1380 #endif
1381         {
1382                 public string           pw_name;
1383                 public string           pw_passwd;
1384                 [CLSCompliant (false)]
1385                 public /* uid_t */ uint pw_uid;
1386                 [CLSCompliant (false)]
1387                 public /* gid_t */ uint pw_gid;
1388                 public string           pw_gecos;
1389                 public string           pw_dir;
1390                 public string           pw_shell;
1391
1392                 public override int GetHashCode ()
1393                 {
1394                         return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^ 
1395                                 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
1396                                 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
1397                                 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
1398                 }
1399
1400                 public override bool Equals (object obj)
1401                 {
1402                         if (obj == null || GetType() != obj.GetType())
1403                                 return false;
1404                         Passwd p = (Passwd) obj;
1405                         return Equals (p);
1406                 }
1407
1408                 public bool Equals (Passwd value)
1409                 {
1410                         if (value == null)
1411                                 return false;
1412                         return value.pw_uid == pw_uid && value.pw_gid == pw_gid && 
1413                                 value.pw_name == pw_name && value.pw_passwd == pw_passwd && 
1414                                 value.pw_gecos == pw_gecos && value.pw_dir == pw_dir && 
1415                                 value.pw_shell == pw_shell;
1416                 }
1417
1418                 // Generate string in /etc/passwd format
1419                 public override string ToString ()
1420                 {
1421                         return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
1422                                 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
1423                 }
1424
1425                 public static bool operator== (Passwd lhs, Passwd rhs)
1426                 {
1427                         return Object.Equals (lhs, rhs);
1428                 }
1429
1430                 public static bool operator!= (Passwd lhs, Passwd rhs)
1431                 {
1432                         return !Object.Equals (lhs, rhs);
1433                 }
1434         }
1435
1436         public sealed class Utsname
1437 #if NET_2_0
1438                 : IEquatable <Utsname>
1439 #endif
1440         {
1441                 public string sysname;
1442                 public string nodename;
1443                 public string release;
1444                 public string version;
1445                 public string machine;
1446                 public string domainname;
1447
1448                 public override int GetHashCode ()
1449                 {
1450                         return sysname.GetHashCode () ^ nodename.GetHashCode () ^ 
1451                                 release.GetHashCode () ^ version.GetHashCode () ^
1452                                 machine.GetHashCode () ^ domainname.GetHashCode ();
1453                 }
1454
1455                 public override bool Equals (object obj)
1456                 {
1457                         if (obj == null || GetType() != obj.GetType())
1458                                 return false;
1459                         Utsname u = (Utsname) obj;
1460                         return Equals (u);
1461                 }
1462
1463                 public bool Equals (Utsname value)
1464                 {
1465                         return value.sysname == sysname && value.nodename == nodename && 
1466                                 value.release == release && value.version == version && 
1467                                 value.machine == machine && value.domainname == domainname;
1468                 }
1469
1470                 // Generate string in /etc/passwd format
1471                 public override string ToString ()
1472                 {
1473                         return string.Format ("{0} {1} {2} {3} {4}",
1474                                 sysname, nodename, release, version, machine);
1475                 }
1476
1477                 public static bool operator== (Utsname lhs, Utsname rhs)
1478                 {
1479                         return Object.Equals (lhs, rhs);
1480                 }
1481
1482                 public static bool operator!= (Utsname lhs, Utsname rhs)
1483                 {
1484                         return !Object.Equals (lhs, rhs);
1485                 }
1486         }
1487
1488         //
1489         // Convention: Functions *not* part of the standard C library AND part of
1490         // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
1491         //
1492         // For example, the man page should be similar to:
1493         //
1494         //    CONFORMING TO (or CONFORMS TO)
1495         //           XPG2, SUSv2, POSIX, etc.
1496         //
1497         // BSD- and GNU-specific exports can also be placed here.
1498         //
1499         // Non-POSIX/XPG/etc. functions can also be placed here if:
1500         //  (a) They'd be likely to be covered in a Steven's-like book
1501         //  (b) The functions would be present in libc.so (or equivalent).
1502         //
1503         // If a function has its own library, that's a STRONG indicator that the
1504         // function should get a different binding, probably in its own assembly, 
1505         // so that package management can work sanely.  (That is, we'd like to avoid
1506         // scenarios where FooLib.dll is installed, but it requires libFooLib.so to
1507         // run, and libFooLib.so doesn't exist.  That would be confusing.)
1508         //
1509         // The only methods in here should be:
1510         //  (1) low-level functions
1511         //  (2) "Trivial" function overloads.  For example, if the parameters to a
1512         //      function are related (e.g. getgroups(2))
1513         //  (3) The return type SHOULD NOT be changed.  If you want to provide a
1514         //      convenience function with a nicer return type, place it into one of
1515         //      the Mono.Unix.Unix* wrapper classes, and give it a .NET-styled name.
1516         //      - EXCEPTION: No public functions should have a `void' return type.
1517         //        `void' return types should be replaced with `int'.
1518         //        Rationality: `void'-return functions typically require a
1519         //        complicated call sequence, such as clear errno, then call, then
1520         //        check errno to see if any errors occurred.  This sequence can't 
1521         //        be done safely in managed code, as errno may change as part of 
1522         //        the P/Invoke mechanism.
1523         //        Instead, add a MonoPosixHelper export which does:
1524         //          errno = 0;
1525         //          INVOKE SYSCALL;
1526         //          return errno == 0 ? 0 : -1;
1527         //        This lets managed code check the return value in the usual manner.
1528         //  (4) Exceptions SHOULD NOT be thrown.  EXCEPTIONS: 
1529         //      - If you're wrapping *broken* methods which make assumptions about 
1530         //        input data, such as that an argument refers to N bytes of data.  
1531         //        This is currently limited to cuserid(3) and encrypt(3).
1532         //      - If you call functions which themselves generate exceptions.  
1533         //        This is the case for using NativeConvert, which will throw an
1534         //        exception if an invalid/unsupported value is used.
1535         //
1536         // Naming Conventions:
1537         //  - Syscall method names should have the same name as the function being
1538         //    wrapped (e.g. Syscall.read ==> read(2)).  This allows people to
1539         //    consult the appropriate man page if necessary.
1540         //  - Methods need not have the same arguments IF this simplifies or
1541         //    permits correct usage.  The current example is syslog, in which
1542         //    syslog(3)'s single `priority' argument is split into SyslogFacility
1543         //    and SyslogLevel arguments.
1544         //  - Type names (structures, classes, enumerations) are always PascalCased.
1545         //  - Enumerations are named as <MethodName><ArgumentName>, and are located
1546         //    in the Mono.Unix.Native namespace.  For readability, if ArgumentName 
1547         //    is "cmd", use Command instead.  For example, fcntl(2) takes a
1548         //    FcntlCommand argument.  This naming convention is to provide an
1549         //    assocation between an enumeration and where it should be used, and
1550         //    allows a single method to accept multiple different enumerations 
1551         //    (see mmap(2), which takes MmapProts and MmapFlags).
1552         //    - EXCEPTION: if an enumeration is shared between multiple different
1553         //      methods, AND/OR the "obvious" enumeration name conflicts with an
1554         //      existing .NET type, a more appropriate name should be used.
1555         //      Example: FilePermissions
1556         //    - EXCEPTION: [Flags] enumerations should get plural names to follow
1557         //      .NET name guidelines.  Usually this doesn't result in a change
1558         //      (OpenFlags is the `flags' parameter for open(2)), but it can
1559         //      (mmap(2) prot ==> MmapProts, access(2) mode ==> AccessModes).
1560         //  - Enumerations should have the [Map] and (optional) [Flags] attributes.
1561         //    [Map] is required for make-map to find the type and generate the
1562         //    appropriate NativeConvert conversion functions.
1563         //  - Enumeration contents should match the original Unix names.  This helps
1564         //    with documentation (the existing man pages are still useful), and is
1565         //    required for use with the make-map generation program.
1566         //  - Structure names should be the PascalCased version of the actual
1567         //    structure name (struct flock ==> Flock).  Structure members should
1568         //    have the same names, or a (reasonably) portable subset (Dirent being
1569         //    the poster child for questionable members).
1570         //    - Whether the managed type should be a reference type (class) or a 
1571         //      value type (struct) should be determined on a case-by-case basis: 
1572         //      if you ever need to be able to use NULL for it (such as with Dirent, 
1573         //      Group, Passwd, as these are method return types and `null' is used 
1574         //      to signify the end), it should be a reference type; otherwise, use 
1575         //      your discretion, and keep any expected usage patterns in mind.
1576         //  - Syscall should be a Single Point Of Truth (SPOT).  There should be
1577         //    only ONE way to do anything.  By convention, the Linux function names
1578         //    are used, but that need not always be the case (use your discretion).
1579         //    It SHOULD NOT be required that developers know what platform they're
1580         //    on, and choose among a set of similar functions.  In short, anything
1581         //    that requires a platform check is BAD -- Mono.Unix is a wrapper, and
1582         //    we can afford to clean things up whenever possible.
1583         //    - Examples: 
1584         //      - Syscall.statfs: Solaris/Mac OS X provide statfs(2), Linux provides
1585         //        statvfs(2).  MonoPosixHelper will "thunk" between the two,
1586         //        exporting a statvfs that works across platforms.
1587         //      - Syscall.getfsent: Glibc export which Solaris lacks, while Solaris
1588         //        instead provides getvfsent(3).  MonoPosixHelper provides wrappers
1589         //        to convert getvfsent(3) into Fstab data.
1590         //    - Exception: If it isn't possible to cleanly wrap platforms, then the
1591         //      method shouldn't be exported.  The user will be expected to do their
1592         //      own platform check and their own DllImports.
1593         //      Examples: mount(2), umount(2), etc.
1594         //    - Note: if a platform doesn't support a function AT ALL, the
1595         //      MonoPosixHelper wrapper won't be compiled, resulting in a
1596         //      EntryPointNotFoundException.  This is also consistent with a missing 
1597         //      P/Invoke into libc.so.
1598         //
1599         [CLSCompliant (false)]
1600         public sealed class Syscall : Stdlib
1601         {
1602                 new internal const string LIBC  = "libc";
1603
1604                 private Syscall () {}
1605
1606                 //
1607                 // <aio.h>
1608                 //
1609
1610                 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3), 
1611                 // aio_return(3), aio_suspend(3), aio_write(3)
1612                 //
1613                 // Then update UnixStream.BeginRead to use the aio* functions.
1614
1615
1616                 #region <attr/xattr.h> Declarations
1617                 //
1618                 // <attr/xattr.h> -- COMPLETE
1619                 //
1620
1621                 // setxattr(2)
1622                 //    int setxattr (const char *path, const char *name,
1623                 //        const void *value, size_t size, int flags);
1624                 [DllImport (MPH, SetLastError=true,
1625                                 EntryPoint="Mono_Posix_Syscall_setxattr")]
1626                 public static extern int setxattr (
1627                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1628                                 string path, 
1629                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1630                                 string name, byte[] value, ulong size, XattrFlags flags);
1631
1632                 public static int setxattr (string path, string name, byte [] value, ulong size)
1633                 {
1634                         return setxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1635                 }
1636
1637                 public static int setxattr (string path, string name, byte [] value, XattrFlags flags)
1638                 {
1639                         return setxattr (path, name, value, (ulong) value.Length, flags);
1640                 }
1641
1642                 public static int setxattr (string path, string name, byte [] value)
1643                 {
1644                         return setxattr (path, name, value, (ulong) value.Length);
1645                 }
1646
1647                 // lsetxattr(2)
1648                 //        int lsetxattr (const char *path, const char *name,
1649                 //                   const void *value, size_t size, int flags);
1650                 [DllImport (MPH, SetLastError=true,
1651                                 EntryPoint="Mono_Posix_Syscall_lsetxattr")]
1652                 public static extern int lsetxattr (
1653                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1654                                 string path, 
1655                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1656                                 string name, byte[] value, ulong size, XattrFlags flags);
1657
1658                 public static int lsetxattr (string path, string name, byte [] value, ulong size)
1659                 {
1660                         return lsetxattr (path, name, value, size, XattrFlags.XATTR_AUTO);
1661                 }
1662
1663                 public static int lsetxattr (string path, string name, byte [] value, XattrFlags flags)
1664                 {
1665                         return lsetxattr (path, name, value, (ulong) value.Length, flags);
1666                 }
1667
1668                 public static int lsetxattr (string path, string name, byte [] value)
1669                 {
1670                         return lsetxattr (path, name, value, (ulong) value.Length);
1671                 }
1672
1673                 // fsetxattr(2)
1674                 //        int fsetxattr (int fd, const char *name,
1675                 //                   const void *value, size_t size, int flags);
1676                 [DllImport (MPH, SetLastError=true,
1677                                 EntryPoint="Mono_Posix_Syscall_fsetxattr")]
1678                 public static extern int fsetxattr (int fd, 
1679                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1680                                 string name, byte[] value, ulong size, XattrFlags flags);
1681
1682                 public static int fsetxattr (int fd, string name, byte [] value, ulong size)
1683                 {
1684                         return fsetxattr (fd, name, value, size, XattrFlags.XATTR_AUTO);
1685                 }
1686
1687                 public static int fsetxattr (int fd, string name, byte [] value, XattrFlags flags)
1688                 {
1689                         return fsetxattr (fd, name, value, (ulong) value.Length, flags);
1690                 }
1691
1692                 public static int fsetxattr (int fd, string name, byte [] value)
1693                 {
1694                         return fsetxattr (fd, name, value, (ulong) value.Length);
1695                 }
1696
1697                 // getxattr(2)
1698                 //        ssize_t getxattr (const char *path, const char *name,
1699                 //                      void *value, size_t size);
1700                 [DllImport (MPH, SetLastError=true,
1701                                 EntryPoint="Mono_Posix_Syscall_getxattr")]
1702                 public static extern long getxattr (
1703                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1704                                 string path, 
1705                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1706                                 string name, byte[] value, ulong size);
1707
1708                 public static long getxattr (string path, string name, byte [] value)
1709                 {
1710                         return getxattr (path, name, value, (ulong) value.Length);
1711                 }
1712
1713                 public static long getxattr (string path, string name, out byte [] value)
1714                 {
1715                         value = null;
1716                         long size = getxattr (path, name, value, 0);
1717                         if (size <= 0)
1718                                 return size;
1719
1720                         value = new byte [size];
1721                         return getxattr (path, name, value, (ulong) size);
1722                 }
1723
1724                 // lgetxattr(2)
1725                 //        ssize_t lgetxattr (const char *path, const char *name,
1726                 //                       void *value, size_t size);
1727                 [DllImport (MPH, SetLastError=true,
1728                                 EntryPoint="Mono_Posix_Syscall_lgetxattr")]
1729                 public static extern long lgetxattr (
1730                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1731                                 string path, 
1732                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1733                                 string name, byte[] value, ulong size);
1734
1735                 public static long lgetxattr (string path, string name, byte [] value)
1736                 {
1737                         return lgetxattr (path, name, value, (ulong) value.Length);
1738                 }
1739
1740                 public static long lgetxattr (string path, string name, out byte [] value)
1741                 {
1742                         value = null;
1743                         long size = lgetxattr (path, name, value, 0);
1744                         if (size <= 0)
1745                                 return size;
1746
1747                         value = new byte [size];
1748                         return lgetxattr (path, name, value, (ulong) size);
1749                 }
1750
1751                 // fgetxattr(2)
1752                 //        ssize_t fgetxattr (int fd, const char *name, void *value, size_t size);
1753                 [DllImport (MPH, SetLastError=true,
1754                                 EntryPoint="Mono_Posix_Syscall_fgetxattr")]
1755                 public static extern long fgetxattr (int fd, 
1756                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1757                                 string name, byte[] value, ulong size);
1758
1759                 public static long fgetxattr (int fd, string name, byte [] value)
1760                 {
1761                         return fgetxattr (fd, name, value, (ulong) value.Length);
1762                 }
1763
1764                 public static long fgetxattr (int fd, string name, out byte [] value)
1765                 {
1766                         value = null;
1767                         long size = fgetxattr (fd, name, value, 0);
1768                         if (size <= 0)
1769                                 return size;
1770
1771                         value = new byte [size];
1772                         return fgetxattr (fd, name, value, (ulong) size);
1773                 }
1774
1775                 // listxattr(2)
1776                 //        ssize_t listxattr (const char *path, char *list, size_t size);
1777                 [DllImport (MPH, SetLastError=true,
1778                                 EntryPoint="Mono_Posix_Syscall_listxattr")]
1779                 public static extern long listxattr (
1780                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1781                                 string path, byte[] list, ulong size);
1782
1783                 // Slight modification: returns 0 on success, negative on error
1784                 public static long listxattr (string path, Encoding encoding, out string [] values)
1785                 {
1786                         values = null;
1787                         long size = listxattr (path, null, 0);
1788                         if (size == 0)
1789                                 values = new string [0];
1790                         if (size <= 0)
1791                                 return (int) size;
1792
1793                         byte[] list = new byte [size];
1794                         long ret = listxattr (path, list, (ulong) size);
1795                         if (ret < 0)
1796                                 return (int) ret;
1797
1798                         GetValues (list, encoding, out values);
1799                         return 0;
1800                 }
1801
1802                 public static long listxattr (string path, out string[] values)
1803                 {
1804                         return listxattr (path, UnixEncoding.Instance, out values);
1805                 }
1806
1807                 private static void GetValues (byte[] list, Encoding encoding, out string[] values)
1808                 {
1809                         int num_values = 0;
1810                         for (int i = 0; i < list.Length; ++i)
1811                                 if (list [i] == 0)
1812                                         ++num_values;
1813
1814                         values = new string [num_values];
1815                         num_values = 0;
1816                         int str_start = 0;
1817                         for (int i = 0; i < list.Length; ++i) {
1818                                 if (list [i] == 0) {
1819                                         values [num_values++] = encoding.GetString (list, str_start, i - str_start);
1820                                         str_start = i+1;
1821                                 }
1822                         }
1823                 }
1824
1825                 // llistxattr(2)
1826                 //        ssize_t llistxattr (const char *path, char *list, size_t size);
1827                 [DllImport (MPH, SetLastError=true,
1828                                 EntryPoint="Mono_Posix_Syscall_llistxattr")]
1829                 public static extern long llistxattr (
1830                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1831                                 string path, byte[] list, ulong size);
1832
1833                 // Slight modification: returns 0 on success, negative on error
1834                 public static long llistxattr (string path, Encoding encoding, out string [] values)
1835                 {
1836                         values = null;
1837                         long size = llistxattr (path, null, 0);
1838                         if (size == 0)
1839                                 values = new string [0];
1840                         if (size <= 0)
1841                                 return (int) size;
1842
1843                         byte[] list = new byte [size];
1844                         long ret = llistxattr (path, list, (ulong) size);
1845                         if (ret < 0)
1846                                 return (int) ret;
1847
1848                         GetValues (list, encoding, out values);
1849                         return 0;
1850                 }
1851
1852                 public static long llistxattr (string path, out string[] values)
1853                 {
1854                         return llistxattr (path, UnixEncoding.Instance, out values);
1855                 }
1856
1857                 // flistxattr(2)
1858                 //        ssize_t flistxattr (int fd, char *list, size_t size);
1859                 [DllImport (MPH, SetLastError=true,
1860                                 EntryPoint="Mono_Posix_Syscall_flistxattr")]
1861                 public static extern long flistxattr (int fd, byte[] list, ulong size);
1862
1863                 // Slight modification: returns 0 on success, negative on error
1864                 public static long flistxattr (int fd, Encoding encoding, out string [] values)
1865                 {
1866                         values = null;
1867                         long size = flistxattr (fd, null, 0);
1868                         if (size == 0)
1869                                 values = new string [0];
1870                         if (size <= 0)
1871                                 return (int) size;
1872
1873                         byte[] list = new byte [size];
1874                         long ret = flistxattr (fd, list, (ulong) size);
1875                         if (ret < 0)
1876                                 return (int) ret;
1877
1878                         GetValues (list, encoding, out values);
1879                         return 0;
1880                 }
1881
1882                 public static long flistxattr (int fd, out string[] values)
1883                 {
1884                         return flistxattr (fd, UnixEncoding.Instance, out values);
1885                 }
1886
1887                 [DllImport (MPH, SetLastError=true,
1888                                 EntryPoint="Mono_Posix_Syscall_removexattr")]
1889                 public static extern int removexattr (
1890                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1891                                 string path, 
1892                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1893                                 string name);
1894
1895                 [DllImport (MPH, SetLastError=true,
1896                                 EntryPoint="Mono_Posix_Syscall_lremovexattr")]
1897                 public static extern int lremovexattr (
1898                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1899                                 string path, 
1900                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1901                                 string name);
1902
1903                 [DllImport (MPH, SetLastError=true,
1904                                 EntryPoint="Mono_Posix_Syscall_fremovexattr")]
1905                 public static extern int fremovexattr (int fd, 
1906                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1907                                 string name);
1908                 #endregion
1909
1910                 #region <dirent.h> Declarations
1911                 //
1912                 // <dirent.h>
1913                 //
1914                 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
1915
1916                 [DllImport (LIBC, SetLastError=true)]
1917                 public static extern IntPtr opendir (
1918                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
1919                                 string name);
1920
1921                 [DllImport (LIBC, SetLastError=true)]
1922                 public static extern int closedir (IntPtr dir);
1923
1924                 // seekdir(3):
1925                 //    void seekdir (DIR *dir, off_t offset);
1926                 //    Slight modification.  Returns -1 on error, 0 on success.
1927                 [DllImport (MPH, SetLastError=true,
1928                                 EntryPoint="Mono_Posix_Syscall_seekdir")]
1929                 public static extern int seekdir (IntPtr dir, long offset);
1930
1931                 // telldir(3)
1932                 //    off_t telldir(DIR *dir);
1933                 [DllImport (MPH, SetLastError=true,
1934                                 EntryPoint="Mono_Posix_Syscall_telldir")]
1935                 public static extern long telldir (IntPtr dir);
1936
1937                 [DllImport (MPH, SetLastError=true,
1938                                 EntryPoint="Mono_Posix_Syscall_rewinddir")]
1939                 public static extern int rewinddir (IntPtr dir);
1940
1941                 private struct _Dirent {
1942                         [ino_t] public ulong      d_ino;
1943                         [off_t] public long       d_off;
1944                         public ushort             d_reclen;
1945                         public byte               d_type;
1946                         public IntPtr             d_name;
1947                 }
1948
1949                 private static void CopyDirent (Dirent to, ref _Dirent from)
1950                 {
1951                         try {
1952                                 to.d_ino    = from.d_ino;
1953                                 to.d_off    = from.d_off;
1954                                 to.d_reclen = from.d_reclen;
1955                                 to.d_type   = from.d_type;
1956                                 to.d_name   = UnixMarshal.PtrToString (from.d_name);
1957                         }
1958                         finally {
1959                                 Stdlib.free (from.d_name);
1960                                 from.d_name = IntPtr.Zero;
1961                         }
1962                 }
1963
1964                 internal static object readdir_lock = new object ();
1965
1966                 [DllImport (MPH, SetLastError=true,
1967                                 EntryPoint="Mono_Posix_Syscall_readdir")]
1968                 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
1969
1970                 public static Dirent readdir (IntPtr dir)
1971                 {
1972                         _Dirent dentry;
1973                         int r;
1974                         lock (readdir_lock) {
1975                                 r = sys_readdir (dir, out dentry);
1976                         }
1977                         if (r != 0)
1978                                 return null;
1979                         Dirent d = new Dirent ();
1980                         CopyDirent (d, ref dentry);
1981                         return d;
1982                 }
1983
1984                 [DllImport (MPH, SetLastError=true,
1985                                 EntryPoint="Mono_Posix_Syscall_readdir_r")]
1986                 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
1987
1988                 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
1989                 {
1990                         entry.d_ino    = 0;
1991                         entry.d_off    = 0;
1992                         entry.d_reclen = 0;
1993                         entry.d_type   = 0;
1994                         entry.d_name   = null;
1995
1996                         _Dirent _d;
1997                         int r = sys_readdir_r (dirp, out _d, out result);
1998
1999                         if (r == 0 && result != IntPtr.Zero) {
2000                                 CopyDirent (entry, ref _d);
2001                         }
2002
2003                         return r;
2004                 }
2005
2006                 [DllImport (LIBC, SetLastError=true)]
2007                 public static extern int dirfd (IntPtr dir);
2008                 #endregion
2009
2010                 #region <fcntl.h> Declarations
2011                 //
2012                 // <fcntl.h> -- COMPLETE
2013                 //
2014
2015                 [DllImport (MPH, SetLastError=true, 
2016                                 EntryPoint="Mono_Posix_Syscall_fcntl")]
2017                 public static extern int fcntl (int fd, FcntlCommand cmd);
2018
2019                 [DllImport (MPH, SetLastError=true, 
2020                                 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
2021                 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
2022
2023                 public static int fcntl (int fd, FcntlCommand cmd, DirectoryNotifyFlags arg)
2024                 {
2025                         if (cmd != FcntlCommand.F_NOTIFY) {
2026                                 SetLastError (Errno.EINVAL);
2027                                 return -1;
2028                         }
2029                         long _arg = NativeConvert.FromDirectoryNotifyFlags (arg);
2030                         return fcntl (fd, FcntlCommand.F_NOTIFY, _arg);
2031                 }
2032
2033                 [DllImport (MPH, SetLastError=true, 
2034                                 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
2035                 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
2036
2037                 [DllImport (MPH, SetLastError=true, 
2038                                 EntryPoint="Mono_Posix_Syscall_open")]
2039                 public static extern int open (
2040                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2041                                 string pathname, OpenFlags flags);
2042
2043                 // open(2)
2044                 //    int open(const char *pathname, int flags, mode_t mode);
2045                 [DllImport (MPH, SetLastError=true, 
2046                                 EntryPoint="Mono_Posix_Syscall_open_mode")]
2047                 public static extern int open (
2048                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2049                                 string pathname, OpenFlags flags, FilePermissions mode);
2050
2051                 // creat(2)
2052                 //    int creat(const char *pathname, mode_t mode);
2053                 [DllImport (MPH, SetLastError=true, 
2054                                 EntryPoint="Mono_Posix_Syscall_creat")]
2055                 public static extern int creat (
2056                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2057                                 string pathname, FilePermissions mode);
2058
2059                 // posix_fadvise(2)
2060                 //    int posix_fadvise(int fd, off_t offset, off_t len, int advice);
2061                 [DllImport (MPH, SetLastError=true, 
2062                                 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
2063                 public static extern int posix_fadvise (int fd, long offset, 
2064                         long len, PosixFadviseAdvice advice);
2065
2066                 // posix_fallocate(P)
2067                 //    int posix_fallocate(int fd, off_t offset, size_t len);
2068                 [DllImport (MPH, SetLastError=true, 
2069                                 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
2070                 public static extern int posix_fallocate (int fd, long offset, ulong len);
2071                 #endregion
2072
2073                 #region <fstab.h> Declarations
2074                 //
2075                 // <fstab.h>  -- COMPLETE
2076                 //
2077                 [Map]
2078                 private struct _Fstab {
2079                         public IntPtr fs_spec;
2080                         public IntPtr fs_file;
2081                         public IntPtr fs_vfstype;
2082                         public IntPtr fs_mntops;
2083                         public IntPtr fs_type;
2084                         public int    fs_freq;
2085                         public int    fs_passno;
2086                         public IntPtr _fs_buf_;
2087                 }
2088
2089                 private static void CopyFstab (Fstab to, ref _Fstab from)
2090                 {
2091                         try {
2092                                 to.fs_spec     = UnixMarshal.PtrToString (from.fs_spec);
2093                                 to.fs_file     = UnixMarshal.PtrToString (from.fs_file);
2094                                 to.fs_vfstype  = UnixMarshal.PtrToString (from.fs_vfstype);
2095                                 to.fs_mntops   = UnixMarshal.PtrToString (from.fs_mntops);
2096                                 to.fs_type     = UnixMarshal.PtrToString (from.fs_type);
2097                                 to.fs_freq     = from.fs_freq;
2098                                 to.fs_passno   = from.fs_passno;
2099                         }
2100                         finally {
2101                                 Stdlib.free (from._fs_buf_);
2102                                 from._fs_buf_ = IntPtr.Zero;
2103                         }
2104                 }
2105
2106                 internal static object fstab_lock = new object ();
2107
2108                 [DllImport (MPH, SetLastError=true,
2109                                 EntryPoint="Mono_Posix_Syscall_endfsent")]
2110                 private static extern int sys_endfsent ();
2111
2112                 public static int endfsent ()
2113                 {
2114                         lock (fstab_lock) {
2115                                 return sys_endfsent ();
2116                         }
2117                 }
2118
2119                 [DllImport (MPH, SetLastError=true,
2120                                 EntryPoint="Mono_Posix_Syscall_getfsent")]
2121                 private static extern int sys_getfsent (out _Fstab fs);
2122
2123                 public static Fstab getfsent ()
2124                 {
2125                         _Fstab fsbuf;
2126                         int r;
2127                         lock (fstab_lock) {
2128                                 r = sys_getfsent (out fsbuf);
2129                         }
2130                         if (r != 0)
2131                                 return null;
2132                         Fstab fs = new Fstab ();
2133                         CopyFstab (fs, ref fsbuf);
2134                         return fs;
2135                 }
2136
2137                 [DllImport (MPH, SetLastError=true,
2138                                 EntryPoint="Mono_Posix_Syscall_getfsfile")]
2139                 private static extern int sys_getfsfile (
2140                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2141                                 string mount_point, out _Fstab fs);
2142
2143                 public static Fstab getfsfile (string mount_point)
2144                 {
2145                         _Fstab fsbuf;
2146                         int r;
2147                         lock (fstab_lock) {
2148                                 r = sys_getfsfile (mount_point, out fsbuf);
2149                         }
2150                         if (r != 0)
2151                                 return null;
2152                         Fstab fs = new Fstab ();
2153                         CopyFstab (fs, ref fsbuf);
2154                         return fs;
2155                 }
2156
2157                 [DllImport (MPH, SetLastError=true,
2158                                 EntryPoint="Mono_Posix_Syscall_getfsspec")]
2159                 private static extern int sys_getfsspec (
2160                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2161                                 string special_file, out _Fstab fs);
2162
2163                 public static Fstab getfsspec (string special_file)
2164                 {
2165                         _Fstab fsbuf;
2166                         int r;
2167                         lock (fstab_lock) {
2168                                 r = sys_getfsspec (special_file, out fsbuf);
2169                         }
2170                         if (r != 0)
2171                                 return null;
2172                         Fstab fs = new Fstab ();
2173                         CopyFstab (fs, ref fsbuf);
2174                         return fs;
2175                 }
2176
2177                 [DllImport (MPH, SetLastError=true,
2178                                 EntryPoint="Mono_Posix_Syscall_setfsent")]
2179                 private static extern int sys_setfsent ();
2180
2181                 public static int setfsent ()
2182                 {
2183                         lock (fstab_lock) {
2184                                 return sys_setfsent ();
2185                         }
2186                 }
2187
2188                 #endregion
2189
2190                 #region <grp.h> Declarations
2191                 //
2192                 // <grp.h>
2193                 //
2194                 // TODO: putgrent(3), fgetgrent_r(), initgroups(3)
2195
2196                 // getgrouplist(2) 
2197                 [DllImport (LIBC, SetLastError=true, EntryPoint="getgrouplist")]
2198                 private static extern int sys_getgrouplist (string user, uint grp, uint [] groups,ref int ngroups);
2199
2200                 public static Group [] getgrouplist (string username)
2201                 {
2202                         if (username == null)
2203                                 throw new ArgumentNullException ("username");
2204                         if (username.Trim () == "")
2205                                 throw new ArgumentException ("Username cannot be empty", "username");
2206                         // Syscall to getpwnam to retrieve user uid
2207                         Passwd pw = Syscall.getpwnam (username);
2208                         if (pw == null)
2209                                 throw new ArgumentException (string.Format ("User {0} does not exists",username), "username");
2210                         return getgrouplist (pw);
2211                 }
2212
2213                 public static Group [] getgrouplist (Passwd user)
2214                 {
2215                         if (user == null)
2216                                 throw new ArgumentNullException ("user");
2217                         // initializing ngroups by 16 to get the group count
2218                         int ngroups = 8;
2219                         int res = -1;
2220                         // allocating buffer to store group uid's
2221                         uint [] groups=null;
2222                         do {
2223                                 Array.Resize (ref groups, ngroups*=2);
2224                                 res = sys_getgrouplist (user.pw_name, user.pw_gid, groups, ref ngroups);
2225                         }
2226                         while (res == -1);
2227                         List<Group> result = new List<Group> ();
2228                         Group gr = null;
2229                         for (int i = 0; i < res; i++) {
2230                                 gr = Syscall.getgrgid (groups [i]);
2231                                 if (gr != null) 
2232                                         result.Add (gr);
2233                         }
2234                         return result.ToArray ();
2235                 }
2236
2237                 // setgroups(2)
2238                 //    int setgroups (size_t size, const gid_t *list);
2239                 [DllImport (MPH, SetLastError=true, EntryPoint="Mono_Posix_Syscall_setgroups")]
2240                 public static extern int setgroups (ulong size, uint[] list);
2241
2242                 public static int setgroups (uint [] list)
2243                 {
2244                         return setgroups ((ulong) list.Length, list);
2245                 }
2246
2247                 [Map]
2248                 private struct _Group
2249                 {
2250                         public IntPtr           gr_name;
2251                         public IntPtr           gr_passwd;
2252                         [gid_t] public uint     gr_gid;
2253                         public int              _gr_nmem_;
2254                         public IntPtr           gr_mem;
2255                         public IntPtr           _gr_buf_;
2256                 }
2257
2258                 private static void CopyGroup (Group to, ref _Group from)
2259                 {
2260                         try {
2261                                 to.gr_gid    = from.gr_gid;
2262                                 to.gr_name   = UnixMarshal.PtrToString (from.gr_name);
2263                                 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
2264                                 to.gr_mem    = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
2265                         }
2266                         finally {
2267                                 Stdlib.free (from.gr_mem);
2268                                 Stdlib.free (from._gr_buf_);
2269                                 from.gr_mem   = IntPtr.Zero;
2270                                 from._gr_buf_ = IntPtr.Zero;
2271                         }
2272                 }
2273
2274                 internal static object grp_lock = new object ();
2275
2276                 [DllImport (MPH, SetLastError=true,
2277                                 EntryPoint="Mono_Posix_Syscall_getgrnam")]
2278                 private static extern int sys_getgrnam (string name, out _Group group);
2279
2280                 public static Group getgrnam (string name)
2281                 {
2282                         _Group group;
2283                         int r;
2284                         lock (grp_lock) {
2285                                 r = sys_getgrnam (name, out group);
2286                         }
2287                         if (r != 0)
2288                                 return null;
2289                         Group gr = new Group ();
2290                         CopyGroup (gr, ref group);
2291                         return gr;
2292                 }
2293
2294                 // getgrgid(3)
2295                 //    struct group *getgrgid(gid_t gid);
2296                 [DllImport (MPH, SetLastError=true,
2297                                 EntryPoint="Mono_Posix_Syscall_getgrgid")]
2298                 private static extern int sys_getgrgid (uint uid, out _Group group);
2299
2300                 public static Group getgrgid (uint uid)
2301                 {
2302                         _Group group;
2303                         int r;
2304                         lock (grp_lock) {
2305                                 r = sys_getgrgid (uid, out group);
2306                         }
2307                         if (r != 0)
2308                                 return null;
2309                         Group gr = new Group ();
2310                         CopyGroup (gr, ref group);
2311                         return gr;
2312                 }
2313
2314                 [DllImport (MPH, SetLastError=true,
2315                                 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
2316                 private static extern int sys_getgrnam_r (
2317                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2318                                 string name, out _Group grbuf, out IntPtr grbufp);
2319
2320                 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
2321                 {
2322                         grbufp = null;
2323                         _Group group;
2324                         IntPtr _grbufp;
2325                         int r = sys_getgrnam_r (name, out group, out _grbufp);
2326                         if (r == 0 && _grbufp != IntPtr.Zero) {
2327                                 CopyGroup (grbuf, ref group);
2328                                 grbufp = grbuf;
2329                         }
2330                         return r;
2331                 }
2332
2333                 // getgrgid_r(3)
2334                 //    int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
2335                 //        size_t buflen, struct group **gbufp);
2336                 [DllImport (MPH, SetLastError=true,
2337                                 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
2338                 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
2339
2340                 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
2341                 {
2342                         grbufp = null;
2343                         _Group group;
2344                         IntPtr _grbufp;
2345                         int r = sys_getgrgid_r (uid, out group, out _grbufp);
2346                         if (r == 0 && _grbufp != IntPtr.Zero) {
2347                                 CopyGroup (grbuf, ref group);
2348                                 grbufp = grbuf;
2349                         }
2350                         return r;
2351                 }
2352
2353                 [DllImport (MPH, SetLastError=true,
2354                                 EntryPoint="Mono_Posix_Syscall_getgrent")]
2355                 private static extern int sys_getgrent (out _Group grbuf);
2356
2357                 public static Group getgrent ()
2358                 {
2359                         _Group group;
2360                         int r;
2361                         lock (grp_lock) {
2362                                 r = sys_getgrent (out group);
2363                         }
2364                         if (r != 0)
2365                                 return null;
2366                         Group gr = new Group();
2367                         CopyGroup (gr, ref group);
2368                         return gr;
2369                 }
2370
2371                 [DllImport (MPH, SetLastError=true, 
2372                                 EntryPoint="Mono_Posix_Syscall_setgrent")]
2373                 private static extern int sys_setgrent ();
2374
2375                 public static int setgrent ()
2376                 {
2377                         lock (grp_lock) {
2378                                 return sys_setgrent ();
2379                         }
2380                 }
2381
2382                 [DllImport (MPH, SetLastError=true, 
2383                                 EntryPoint="Mono_Posix_Syscall_endgrent")]
2384                 private static extern int sys_endgrent ();
2385
2386                 public static int endgrent ()
2387                 {
2388                         lock (grp_lock) {
2389                                 return sys_endgrent ();
2390                         }
2391                 }
2392
2393                 [DllImport (MPH, SetLastError=true,
2394                                 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
2395                 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
2396
2397                 public static Group fgetgrent (IntPtr stream)
2398                 {
2399                         _Group group;
2400                         int r;
2401                         lock (grp_lock) {
2402                                 r = sys_fgetgrent (stream, out group);
2403                         }
2404                         if (r != 0)
2405                                 return null;
2406                         Group gr = new Group ();
2407                         CopyGroup (gr, ref group);
2408                         return gr;
2409                 }
2410                 #endregion
2411
2412                 #region <pwd.h> Declarations
2413                 //
2414                 // <pwd.h>
2415                 //
2416                 // TODO: putpwent(3), fgetpwent_r()
2417                 //
2418                 // SKIPPING: getpw(3): it's dangerous.  Use getpwuid(3) instead.
2419
2420                 [Map]
2421                 private struct _Passwd
2422                 {
2423                         public IntPtr           pw_name;
2424                         public IntPtr           pw_passwd;
2425                         [uid_t] public uint     pw_uid;
2426                         [gid_t] public uint     pw_gid;
2427                         public IntPtr           pw_gecos;
2428                         public IntPtr           pw_dir;
2429                         public IntPtr           pw_shell;
2430                         public IntPtr           _pw_buf_;
2431                 }
2432
2433                 private static void CopyPasswd (Passwd to, ref _Passwd from)
2434                 {
2435                         try {
2436                                 to.pw_name   = UnixMarshal.PtrToString (from.pw_name);
2437                                 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
2438                                 to.pw_uid    = from.pw_uid;
2439                                 to.pw_gid    = from.pw_gid;
2440                                 to.pw_gecos  = UnixMarshal.PtrToString (from.pw_gecos);
2441                                 to.pw_dir    = UnixMarshal.PtrToString (from.pw_dir);
2442                                 to.pw_shell  = UnixMarshal.PtrToString (from.pw_shell);
2443                         }
2444                         finally {
2445                                 Stdlib.free (from._pw_buf_);
2446                                 from._pw_buf_ = IntPtr.Zero;
2447                         }
2448                 }
2449
2450                 internal static object pwd_lock = new object ();
2451
2452                 [DllImport (MPH, SetLastError=true,
2453                                 EntryPoint="Mono_Posix_Syscall_getpwnam")]
2454                 private static extern int sys_getpwnam (string name, out _Passwd passwd);
2455
2456                 public static Passwd getpwnam (string name)
2457                 {
2458                         _Passwd passwd;
2459                         int r;
2460                         lock (pwd_lock) {
2461                                 r = sys_getpwnam (name, out passwd);
2462                         }
2463                         if (r != 0)
2464                                 return null;
2465                         Passwd pw = new Passwd ();
2466                         CopyPasswd (pw, ref passwd);
2467                         return pw;
2468                 }
2469
2470                 // getpwuid(3)
2471                 //    struct passwd *getpwnuid(uid_t uid);
2472                 [DllImport (MPH, SetLastError=true,
2473                                 EntryPoint="Mono_Posix_Syscall_getpwuid")]
2474                 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
2475
2476                 public static Passwd getpwuid (uint uid)
2477                 {
2478                         _Passwd passwd;
2479                         int r;
2480                         lock (pwd_lock) {
2481                                 r = sys_getpwuid (uid, out passwd);
2482                         }
2483                         if (r != 0)
2484                                 return null;
2485                         Passwd pw = new Passwd ();
2486                         CopyPasswd (pw, ref passwd);
2487                         return pw;
2488                 }
2489
2490                 [DllImport (MPH, SetLastError=true,
2491                                 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
2492                 private static extern int sys_getpwnam_r (
2493                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2494                                 string name, out _Passwd pwbuf, out IntPtr pwbufp);
2495
2496                 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
2497                 {
2498                         pwbufp = null;
2499                         _Passwd passwd;
2500                         IntPtr _pwbufp;
2501                         int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
2502                         if (r == 0 && _pwbufp != IntPtr.Zero) {
2503                                 CopyPasswd (pwbuf, ref passwd);
2504                                 pwbufp = pwbuf;
2505                         }
2506                         return r;
2507                 }
2508
2509                 // getpwuid_r(3)
2510                 //    int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
2511                 //        buflen, struct passwd **pwbufp);
2512                 [DllImport (MPH, SetLastError=true,
2513                                 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
2514                 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
2515
2516                 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
2517                 {
2518                         pwbufp = null;
2519                         _Passwd passwd;
2520                         IntPtr _pwbufp;
2521                         int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
2522                         if (r == 0 && _pwbufp != IntPtr.Zero) {
2523                                 CopyPasswd (pwbuf, ref passwd);
2524                                 pwbufp = pwbuf;
2525                         }
2526                         return r;
2527                 }
2528
2529                 [DllImport (MPH, SetLastError=true,
2530                                 EntryPoint="Mono_Posix_Syscall_getpwent")]
2531                 private static extern int sys_getpwent (out _Passwd pwbuf);
2532
2533                 public static Passwd getpwent ()
2534                 {
2535                         _Passwd passwd;
2536                         int r;
2537                         lock (pwd_lock) {
2538                                 r = sys_getpwent (out passwd);
2539                         }
2540                         if (r != 0)
2541                                 return null;
2542                         Passwd pw = new Passwd ();
2543                         CopyPasswd (pw, ref passwd);
2544                         return pw;
2545                 }
2546
2547                 [DllImport (MPH, SetLastError=true, 
2548                                 EntryPoint="Mono_Posix_Syscall_setpwent")]
2549                 private static extern int sys_setpwent ();
2550
2551                 public static int setpwent ()
2552                 {
2553                         lock (pwd_lock) {
2554                                 return sys_setpwent ();
2555                         }
2556                 }
2557
2558                 [DllImport (MPH, SetLastError=true, 
2559                                 EntryPoint="Mono_Posix_Syscall_endpwent")]
2560                 private static extern int sys_endpwent ();
2561
2562                 public static int endpwent ()
2563                 {
2564                         lock (pwd_lock) {
2565                                 return sys_endpwent ();
2566                         }
2567                 }
2568
2569                 [DllImport (MPH, SetLastError=true,
2570                                 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
2571                 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
2572
2573                 public static Passwd fgetpwent (IntPtr stream)
2574                 {
2575                         _Passwd passwd;
2576                         int r;
2577                         lock (pwd_lock) {
2578                                 r = sys_fgetpwent (stream, out passwd);
2579                         }
2580                         if (r != 0)
2581                                 return null;
2582                         Passwd pw = new Passwd ();
2583                         CopyPasswd (pw, ref passwd);
2584                         return pw;
2585                 }
2586                 #endregion
2587
2588                 #region <signal.h> Declarations
2589                 //
2590                 // <signal.h>
2591                 //
2592                 [DllImport (MPH, SetLastError=true,
2593                                 EntryPoint="Mono_Posix_Syscall_psignal")]
2594                 private static extern int psignal (int sig, string s);
2595
2596                 public static int psignal (Signum sig, string s)
2597                 {
2598                         int signum = NativeConvert.FromSignum (sig);
2599                         return psignal (signum, s);
2600                 }
2601
2602                 // kill(2)
2603                 //    int kill(pid_t pid, int sig);
2604                 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
2605                 private static extern int sys_kill (int pid, int sig);
2606
2607                 public static int kill (int pid, Signum sig)
2608                 {
2609                         int _sig = NativeConvert.FromSignum (sig);
2610                         return sys_kill (pid, _sig);
2611                 }
2612
2613                 private static object signal_lock = new object ();
2614
2615                 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
2616                 private static extern IntPtr sys_strsignal (int sig);
2617
2618                 public static string strsignal (Signum sig)
2619                 {
2620                         int s = NativeConvert.FromSignum (sig);
2621                         lock (signal_lock) {
2622                                 IntPtr r = sys_strsignal (s);
2623                                 return UnixMarshal.PtrToString (r);
2624                         }
2625                 }
2626
2627                 // TODO: sigaction(2)
2628                 // TODO: sigsuspend(2)
2629                 // TODO: sigpending(2)
2630
2631                 #endregion
2632
2633                 #region <stdio.h> Declarations
2634                 //
2635                 // <stdio.h>
2636                 //
2637                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
2638                 private static extern int _L_ctermid ();
2639
2640                 public static readonly int L_ctermid = _L_ctermid ();
2641
2642                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
2643                 private static extern int _L_cuserid ();
2644
2645                 public static readonly int L_cuserid = _L_cuserid ();
2646
2647                 internal static object getlogin_lock = new object ();
2648
2649                 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
2650                 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
2651
2652                 [Obsolete ("\"Nobody knows precisely what cuserid() does... " + 
2653                                 "DO NOT USE cuserid().\n" +
2654                                 "`string' must hold L_cuserid characters.  Use getlogin_r instead.")]
2655                 public static string cuserid (StringBuilder @string)
2656                 {
2657                         if (@string.Capacity < L_cuserid) {
2658                                 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
2659                         }
2660                         lock (getlogin_lock) {
2661                                 IntPtr r = sys_cuserid (@string);
2662                                 return UnixMarshal.PtrToString (r);
2663                         }
2664                 }
2665
2666                 #endregion
2667
2668                 #region <stdlib.h> Declarations
2669                 //
2670                 // <stdlib.h>
2671                 //
2672                 [DllImport (LIBC, SetLastError=true)]
2673                 public static extern int mkstemp (StringBuilder template);
2674
2675                 [DllImport (LIBC, SetLastError=true)]
2676                 public static extern int ttyslot ();
2677
2678                 [Obsolete ("This is insecure and should not be used", true)]
2679                 public static int setkey (string key)
2680                 {
2681                         throw new SecurityException ("crypt(3) has been broken.  Use something more secure.");
2682                 }
2683
2684                 #endregion
2685
2686                 #region <string.h> Declarations
2687                 //
2688                 // <string.h>
2689                 //
2690
2691                 // strerror_r(3)
2692                 //    int strerror_r(int errnum, char *buf, size_t n);
2693                 [DllImport (MPH, SetLastError=true, 
2694                                 EntryPoint="Mono_Posix_Syscall_strerror_r")]
2695                 private static extern int sys_strerror_r (int errnum, 
2696                                 [Out] StringBuilder buf, ulong n);
2697
2698                 public static int strerror_r (Errno errnum, StringBuilder buf, ulong n)
2699                 {
2700                         int e = NativeConvert.FromErrno (errnum);
2701                         return sys_strerror_r (e, buf, n);
2702                 }
2703
2704                 public static int strerror_r (Errno errnum, StringBuilder buf)
2705                 {
2706                         return strerror_r (errnum, buf, (ulong) buf.Capacity);
2707                 }
2708
2709                 #endregion
2710
2711                 #region <sys/epoll.h> Declarations
2712
2713                 public static int epoll_create (int size)
2714                 {
2715                         return sys_epoll_create (size);
2716                 }
2717
2718                 public static int epoll_create (EpollFlags flags)
2719                 {
2720                         return sys_epoll_create1 (flags);
2721                 }
2722
2723                 public static int epoll_ctl (int epfd, EpollOp op, int fd, EpollEvents events)
2724                 {
2725                         EpollEvent ee = new EpollEvent ();
2726                         ee.events = events;
2727                         ee.fd = fd;
2728
2729                         return sys_epoll_ctl (epfd, op, fd, ref ee);
2730                 }
2731
2732                 public static int epoll_wait (int epfd, EpollEvent [] events, int max_events, int timeout)
2733                 {
2734                         if (events.Length < max_events)
2735                                 throw new ArgumentOutOfRangeException ("events", "Must refer to at least 'max_events' elements.");
2736
2737                         return sys_epoll_wait (epfd, events, max_events, timeout);
2738                 }
2739
2740                 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_create")]
2741                 private static extern int sys_epoll_create (int size);
2742
2743                 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_create1")]
2744                 private static extern int sys_epoll_create1 (EpollFlags flags);
2745
2746                 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_ctl")]
2747                 private static extern int sys_epoll_ctl (int epfd, EpollOp op, int fd, ref EpollEvent ee);
2748
2749                 [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_wait")]
2750                 private static extern int sys_epoll_wait (int epfd, EpollEvent [] ee, int maxevents, int timeout);
2751                 #endregion
2752                 
2753                 #region <sys/mman.h> Declarations
2754                 //
2755                 // <sys/mman.h>
2756                 //
2757
2758                 // posix_madvise(P)
2759                 //    int posix_madvise(void *addr, size_t len, int advice);
2760                 [DllImport (MPH, SetLastError=true, 
2761                                 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
2762                 public static extern int posix_madvise (IntPtr addr, ulong len, 
2763                         PosixMadviseAdvice advice);
2764
2765                 public static readonly IntPtr MAP_FAILED = unchecked((IntPtr)(-1));
2766
2767                 [DllImport (MPH, SetLastError=true, 
2768                                 EntryPoint="Mono_Posix_Syscall_mmap")]
2769                 public static extern IntPtr mmap (IntPtr start, ulong length, 
2770                                 MmapProts prot, MmapFlags flags, int fd, long offset);
2771
2772                 [DllImport (MPH, SetLastError=true, 
2773                                 EntryPoint="Mono_Posix_Syscall_munmap")]
2774                 public static extern int munmap (IntPtr start, ulong length);
2775
2776                 [DllImport (MPH, SetLastError=true, 
2777                                 EntryPoint="Mono_Posix_Syscall_mprotect")]
2778                 public static extern int mprotect (IntPtr start, ulong len, MmapProts prot);
2779
2780                 [DllImport (MPH, SetLastError=true, 
2781                                 EntryPoint="Mono_Posix_Syscall_msync")]
2782                 public static extern int msync (IntPtr start, ulong len, MsyncFlags flags);
2783
2784                 [DllImport (MPH, SetLastError=true, 
2785                                 EntryPoint="Mono_Posix_Syscall_mlock")]
2786                 public static extern int mlock (IntPtr start, ulong len);
2787
2788                 [DllImport (MPH, SetLastError=true, 
2789                                 EntryPoint="Mono_Posix_Syscall_munlock")]
2790                 public static extern int munlock (IntPtr start, ulong len);
2791
2792                 [DllImport (LIBC, SetLastError=true, EntryPoint="mlockall")]
2793                 private static extern int sys_mlockall (int flags);
2794
2795                 public static int mlockall (MlockallFlags flags)
2796                 {
2797                         int _flags = NativeConvert.FromMlockallFlags (flags);
2798                         return sys_mlockall (_flags);
2799                 }
2800
2801                 [DllImport (LIBC, SetLastError=true)]
2802                 public static extern int munlockall ();
2803
2804                 [DllImport (MPH, SetLastError=true, 
2805                                 EntryPoint="Mono_Posix_Syscall_mremap")]
2806                 public static extern IntPtr mremap (IntPtr old_address, ulong old_size, 
2807                                 ulong new_size, MremapFlags flags);
2808
2809                 [DllImport (MPH, SetLastError=true, 
2810                                 EntryPoint="Mono_Posix_Syscall_mincore")]
2811                 public static extern int mincore (IntPtr start, ulong length, byte[] vec);
2812
2813                 [DllImport (MPH, SetLastError=true, 
2814                                 EntryPoint="Mono_Posix_Syscall_remap_file_pages")]
2815                 public static extern int remap_file_pages (IntPtr start, ulong size,
2816                                 MmapProts prot, long pgoff, MmapFlags flags);
2817
2818                 #endregion
2819
2820                 #region <sys/poll.h> Declarations
2821                 //
2822                 // <sys/poll.h> -- COMPLETE
2823                 //
2824
2825                 private struct _pollfd {
2826                         public int fd;
2827                         public short events;
2828                         public short revents;
2829                 }
2830
2831                 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
2832                 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
2833
2834                 public static int poll (Pollfd [] fds, uint nfds, int timeout)
2835                 {
2836                         if (fds.Length < nfds)
2837                                 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
2838
2839                         _pollfd[] send = new _pollfd[nfds];
2840
2841                         for (int i = 0; i < send.Length; i++) {
2842                                 send [i].fd     = fds [i].fd;
2843                                 send [i].events = NativeConvert.FromPollEvents (fds [i].events);
2844                         }
2845
2846                         int r = sys_poll (send, nfds, timeout);
2847
2848                         for (int i = 0; i < send.Length; i++) {
2849                                 fds [i].revents = NativeConvert.ToPollEvents (send [i].revents);
2850                         }
2851
2852                         return r;
2853                 }
2854
2855                 public static int poll (Pollfd [] fds, int timeout)
2856                 {
2857                         return poll (fds, (uint) fds.Length, timeout);
2858                 }
2859
2860                 //
2861                 // <sys/ptrace.h>
2862                 //
2863
2864                 // TODO: ptrace(2)
2865
2866                 //
2867                 // <sys/resource.h>
2868                 //
2869
2870                 // TODO: setrlimit(2)
2871                 // TODO: getrlimit(2)
2872                 // TODO: getrusage(2)
2873
2874                 #endregion
2875
2876                 #region <sys/sendfile.h> Declarations
2877                 //
2878                 // <sys/sendfile.h> -- COMPLETE
2879                 //
2880
2881                 [DllImport (MPH, SetLastError=true,
2882                                 EntryPoint="Mono_Posix_Syscall_sendfile")]
2883                 public static extern long sendfile (int out_fd, int in_fd, 
2884                                 ref long offset, ulong count);
2885
2886                 #endregion
2887
2888                 #region <sys/stat.h> Declarations
2889                 //
2890                 // <sys/stat.h>  -- COMPLETE
2891                 //
2892                 [DllImport (MPH, SetLastError=true, 
2893                                 EntryPoint="Mono_Posix_Syscall_stat")]
2894                 public static extern int stat (
2895                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2896                                 string file_name, out Stat buf);
2897
2898                 [DllImport (MPH, SetLastError=true, 
2899                                 EntryPoint="Mono_Posix_Syscall_fstat")]
2900                 public static extern int fstat (int filedes, out Stat buf);
2901
2902                 [DllImport (MPH, SetLastError=true, 
2903                                 EntryPoint="Mono_Posix_Syscall_lstat")]
2904                 public static extern int lstat (
2905                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2906                                 string file_name, out Stat buf);
2907
2908                 // TODO:
2909                 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
2910                 // All take FilePermissions
2911
2912                 // chmod(2)
2913                 //    int chmod(const char *path, mode_t mode);
2914                 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
2915                 private static extern int sys_chmod (
2916                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2917                                 string path, uint mode);
2918
2919                 public static int chmod (string path, FilePermissions mode)
2920                 {
2921                         uint _mode = NativeConvert.FromFilePermissions (mode);
2922                         return sys_chmod (path, _mode);
2923                 }
2924
2925                 // fchmod(2)
2926                 //    int chmod(int filedes, mode_t mode);
2927                 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
2928                 private static extern int sys_fchmod (int filedes, uint mode);
2929
2930                 public static int fchmod (int filedes, FilePermissions mode)
2931                 {
2932                         uint _mode = NativeConvert.FromFilePermissions (mode);
2933                         return sys_fchmod (filedes, _mode);
2934                 }
2935
2936                 // umask(2)
2937                 //    mode_t umask(mode_t mask);
2938                 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
2939                 private static extern uint sys_umask (uint mask);
2940
2941                 public static FilePermissions umask (FilePermissions mask)
2942                 {
2943                         uint _mask = NativeConvert.FromFilePermissions (mask);
2944                         uint r = sys_umask (_mask);
2945                         return NativeConvert.ToFilePermissions (r);
2946                 }
2947
2948                 // mkdir(2)
2949                 //    int mkdir(const char *pathname, mode_t mode);
2950                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
2951                 private static extern int sys_mkdir (
2952                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2953                                 string oldpath, uint mode);
2954
2955                 public static int mkdir (string oldpath, FilePermissions mode)
2956                 {
2957                         uint _mode = NativeConvert.FromFilePermissions (mode);
2958                         return sys_mkdir (oldpath, _mode);
2959                 }
2960
2961                 // mknod(2)
2962                 //    int mknod (const char *pathname, mode_t mode, dev_t dev);
2963                 [DllImport (MPH, SetLastError=true,
2964                                 EntryPoint="Mono_Posix_Syscall_mknod")]
2965                 public static extern int mknod (
2966                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2967                                 string pathname, FilePermissions mode, ulong dev);
2968
2969                 // mkfifo(3)
2970                 //    int mkfifo(const char *pathname, mode_t mode);
2971                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
2972                 private static extern int sys_mkfifo (
2973                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2974                                 string pathname, uint mode);
2975
2976                 public static int mkfifo (string pathname, FilePermissions mode)
2977                 {
2978                         uint _mode = NativeConvert.FromFilePermissions (mode);
2979                         return sys_mkfifo (pathname, _mode);
2980                 }
2981
2982                 #endregion
2983
2984                 #region <sys/stat.h> Declarations
2985                 //
2986                 // <sys/statvfs.h>
2987                 //
2988
2989                 [DllImport (MPH, SetLastError=true,
2990                                 EntryPoint="Mono_Posix_Syscall_statvfs")]
2991                 public static extern int statvfs (
2992                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
2993                                 string path, out Statvfs buf);
2994
2995                 [DllImport (MPH, SetLastError=true,
2996                                 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
2997                 public static extern int fstatvfs (int fd, out Statvfs buf);
2998
2999                 #endregion
3000
3001                 #region <sys/time.h> Declarations
3002                 //
3003                 // <sys/time.h>
3004                 //
3005                 // TODO: adjtime(), getitimer(2), setitimer(2)
3006
3007                 [DllImport (MPH, SetLastError=true, 
3008                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3009                 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
3010
3011                 [DllImport (MPH, SetLastError=true,
3012                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3013                 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
3014
3015                 public static int gettimeofday (out Timeval tv)
3016                 {
3017                         return gettimeofday (out tv, IntPtr.Zero);
3018                 }
3019
3020                 [DllImport (MPH, SetLastError=true,
3021                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3022                 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
3023
3024                 public static int gettimeofday (out Timezone tz)
3025                 {
3026                         return gettimeofday (IntPtr.Zero, out tz);
3027                 }
3028
3029                 [DllImport (MPH, SetLastError=true,
3030                                 EntryPoint="Mono_Posix_Syscall_settimeofday")]
3031                 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
3032
3033                 [DllImport (MPH, SetLastError=true,
3034                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
3035                 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
3036
3037                 public static int settimeofday (ref Timeval tv)
3038                 {
3039                         return settimeofday (ref tv, IntPtr.Zero);
3040                 }
3041
3042                 [DllImport (MPH, SetLastError=true, 
3043                                 EntryPoint="Mono_Posix_Syscall_utimes")]
3044                 private static extern int sys_utimes (
3045                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3046                                 string filename, Timeval[] tvp);
3047
3048                 public static int utimes (string filename, Timeval[] tvp)
3049                 {
3050                         if (tvp != null && tvp.Length != 2) {
3051                                 SetLastError (Errno.EINVAL);
3052                                 return -1;
3053                         }
3054                         return sys_utimes (filename, tvp);
3055                 }
3056
3057                 [DllImport (MPH, SetLastError=true, 
3058                                 EntryPoint="Mono_Posix_Syscall_lutimes")]
3059                 private static extern int sys_lutimes (
3060                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3061                                 string filename, Timeval[] tvp);
3062
3063                 public static int lutimes (string filename, Timeval[] tvp)
3064                 {
3065                         if (tvp != null && tvp.Length != 2) {
3066                                 SetLastError (Errno.EINVAL);
3067                                 return -1;
3068                         }
3069                         return sys_lutimes (filename, tvp);
3070                 }
3071
3072                 [DllImport (MPH, SetLastError=true, 
3073                                 EntryPoint="Mono_Posix_Syscall_futimes")]
3074                 private static extern int sys_futimes (int fd, Timeval[] tvp);
3075
3076                 public static int futimes (int fd, Timeval[] tvp)
3077                 {
3078                         if (tvp != null && tvp.Length != 2) {
3079                                 SetLastError (Errno.EINVAL);
3080                                 return -1;
3081                         }
3082                         return sys_futimes (fd, tvp);
3083                 }
3084
3085                 #endregion
3086
3087                 //
3088                 // <sys/timeb.h>
3089                 //
3090
3091                 // TODO: ftime(3)
3092
3093                 //
3094                 // <sys/times.h>
3095                 //
3096
3097                 // TODO: times(2)
3098
3099                 //
3100                 // <sys/utsname.h>
3101                 //
3102
3103                 [Map]
3104                 private struct _Utsname
3105                 {
3106                         public IntPtr sysname;
3107                         public IntPtr nodename;
3108                         public IntPtr release;
3109                         public IntPtr version;
3110                         public IntPtr machine;
3111                         public IntPtr domainname;
3112                         public IntPtr _buf_;
3113                 }
3114
3115                 private static void CopyUtsname (ref Utsname to, ref _Utsname from)
3116                 {
3117                         try {
3118                                 to = new Utsname ();
3119                                 to.sysname     = UnixMarshal.PtrToString (from.sysname);
3120                                 to.nodename    = UnixMarshal.PtrToString (from.nodename);
3121                                 to.release     = UnixMarshal.PtrToString (from.release);
3122                                 to.version     = UnixMarshal.PtrToString (from.version);
3123                                 to.machine     = UnixMarshal.PtrToString (from.machine);
3124                                 to.domainname  = UnixMarshal.PtrToString (from.domainname);
3125                         }
3126                         finally {
3127                                 Stdlib.free (from._buf_);
3128                                 from._buf_ = IntPtr.Zero;
3129                         }
3130                 }
3131
3132                 [DllImport (MPH, SetLastError=true, 
3133                                 EntryPoint="Mono_Posix_Syscall_uname")]
3134                 private static extern int sys_uname (out _Utsname buf);
3135
3136                 public static int uname (out Utsname buf)
3137                 {
3138                         _Utsname _buf;
3139                         int r = sys_uname (out _buf);
3140                         buf = new Utsname ();
3141                         if (r == 0) {
3142                                 CopyUtsname (ref buf, ref _buf);
3143                         }
3144                         return r;
3145                 }
3146
3147                 #region <sys/wait.h> Declarations
3148                 //
3149                 // <sys/wait.h>
3150                 //
3151
3152                 // wait(2)
3153                 //    pid_t wait(int *status);
3154                 [DllImport (LIBC, SetLastError=true)]
3155                 public static extern int wait (out int status);
3156
3157                 // waitpid(2)
3158                 //    pid_t waitpid(pid_t pid, int *status, int options);
3159                 [DllImport (LIBC, SetLastError=true)]
3160                 private static extern int waitpid (int pid, out int status, int options);
3161
3162                 public static int waitpid (int pid, out int status, WaitOptions options)
3163                 {
3164                         int _options = NativeConvert.FromWaitOptions (options);
3165                         return waitpid (pid, out status, _options);
3166                 }
3167
3168                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
3169                 private static extern int _WIFEXITED (int status);
3170
3171                 public static bool WIFEXITED (int status)
3172                 {
3173                         return _WIFEXITED (status) != 0;
3174                 }
3175
3176                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
3177                 public static extern int WEXITSTATUS (int status);
3178
3179                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
3180                 private static extern int _WIFSIGNALED (int status);
3181
3182                 public static bool WIFSIGNALED (int status)
3183                 {
3184                         return _WIFSIGNALED (status) != 0;
3185                 }
3186
3187                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
3188                 private static extern int _WTERMSIG (int status);
3189
3190                 public static Signum WTERMSIG (int status)
3191                 {
3192                         int r = _WTERMSIG (status);
3193                         return NativeConvert.ToSignum (r);
3194                 }
3195
3196                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
3197                 private static extern int _WIFSTOPPED (int status);
3198
3199                 public static bool WIFSTOPPED (int status)
3200                 {
3201                         return _WIFSTOPPED (status) != 0;
3202                 }
3203
3204                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
3205                 private static extern int _WSTOPSIG (int status);
3206
3207                 public static Signum WSTOPSIG (int status)
3208                 {
3209                         int r = _WSTOPSIG (status);
3210                         return NativeConvert.ToSignum (r);
3211                 }
3212
3213                 //
3214                 // <termios.h>
3215                 //
3216
3217                 #endregion
3218
3219                 #region <syslog.h> Declarations
3220                 //
3221                 // <syslog.h>
3222                 //
3223
3224                 [DllImport (MPH, SetLastError=true,
3225                                 EntryPoint="Mono_Posix_Syscall_openlog")]
3226                 private static extern int sys_openlog (IntPtr ident, int option, int facility);
3227
3228                 public static int openlog (IntPtr ident, SyslogOptions option, 
3229                                 SyslogFacility defaultFacility)
3230                 {
3231                         int _option   = NativeConvert.FromSyslogOptions (option);
3232                         int _facility = NativeConvert.FromSyslogFacility (defaultFacility);
3233
3234                         return sys_openlog (ident, _option, _facility);
3235                 }
3236
3237                 [DllImport (MPH, SetLastError=true,
3238                                 EntryPoint="Mono_Posix_Syscall_syslog")]
3239                 private static extern int sys_syslog (int priority, string message);
3240
3241                 public static int syslog (SyslogFacility facility, SyslogLevel level, string message)
3242                 {
3243                         int _facility = NativeConvert.FromSyslogFacility (facility);
3244                         int _level = NativeConvert.FromSyslogLevel (level);
3245                         return sys_syslog (_facility | _level, GetSyslogMessage (message));
3246                 }
3247
3248                 public static int syslog (SyslogLevel level, string message)
3249                 {
3250                         int _level = NativeConvert.FromSyslogLevel (level);
3251                         return sys_syslog (_level, GetSyslogMessage (message));
3252                 }
3253
3254                 private static string GetSyslogMessage (string message)
3255                 {
3256                         return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
3257                 }
3258
3259                 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
3260                                 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
3261                 public static int syslog (SyslogFacility facility, SyslogLevel level, 
3262                                 string format, params object[] parameters)
3263                 {
3264                         int _facility = NativeConvert.FromSyslogFacility (facility);
3265                         int _level = NativeConvert.FromSyslogLevel (level);
3266
3267                         object[] _parameters = new object[checked(parameters.Length+2)];
3268                         _parameters [0] = _facility | _level;
3269                         _parameters [1] = format;
3270                         Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3271                         return (int) XPrintfFunctions.syslog (_parameters);
3272                 }
3273
3274                 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
3275                                 "Use syslog(SyslogLevel, string) instead.")]
3276                 public static int syslog (SyslogLevel level, string format, 
3277                                 params object[] parameters)
3278                 {
3279                         int _level = NativeConvert.FromSyslogLevel (level);
3280
3281                         object[] _parameters = new object[checked(parameters.Length+2)];
3282                         _parameters [0] = _level;
3283                         _parameters [1] = format;
3284                         Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
3285                         return (int) XPrintfFunctions.syslog (_parameters);
3286                 }
3287
3288                 [DllImport (MPH, SetLastError=true,
3289                                 EntryPoint="Mono_Posix_Syscall_closelog")]
3290                 public static extern int closelog ();
3291
3292                 [DllImport (LIBC, SetLastError=true, EntryPoint="setlogmask")]
3293                 private static extern int sys_setlogmask (int mask);
3294
3295                 public static int setlogmask (SyslogLevel mask)
3296                 {
3297                         int _mask = NativeConvert.FromSyslogLevel (mask);
3298                         return sys_setlogmask (_mask);
3299                 }
3300
3301                 #endregion
3302
3303                 #region <time.h> Declarations
3304
3305                 //
3306                 // <time.h>
3307                 //
3308
3309                 // nanosleep(2)
3310                 //    int nanosleep(const struct timespec *req, struct timespec *rem);
3311                 [DllImport (MPH, SetLastError=true,
3312                                 EntryPoint="Mono_Posix_Syscall_nanosleep")]
3313                 public static extern int nanosleep (ref Timespec req, ref Timespec rem);
3314
3315                 // stime(2)
3316                 //    int stime(time_t *t);
3317                 [DllImport (MPH, SetLastError=true,
3318                                 EntryPoint="Mono_Posix_Syscall_stime")]
3319                 public static extern int stime (ref long t);
3320
3321                 // time(2)
3322                 //    time_t time(time_t *t);
3323                 [DllImport (MPH, SetLastError=true,
3324                                 EntryPoint="Mono_Posix_Syscall_time")]
3325                 public static extern long time (out long t);
3326
3327                 //
3328                 // <ulimit.h>
3329                 //
3330
3331                 // TODO: ulimit(3)
3332
3333                 #endregion
3334
3335                 #region <unistd.h> Declarations
3336                 //
3337                 // <unistd.h>
3338                 //
3339                 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
3340                 //       other TODOs listed below.
3341
3342                 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
3343                 private static extern int sys_access (
3344                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3345                                 string pathname, int mode);
3346
3347                 public static int access (string pathname, AccessModes mode)
3348                 {
3349                         int _mode = NativeConvert.FromAccessModes (mode);
3350                         return sys_access (pathname, _mode);
3351                 }
3352
3353                 // lseek(2)
3354                 //    off_t lseek(int filedes, off_t offset, int whence);
3355                 [DllImport (MPH, SetLastError=true, 
3356                                 EntryPoint="Mono_Posix_Syscall_lseek")]
3357                 private static extern long sys_lseek (int fd, long offset, int whence);
3358
3359                 public static long lseek (int fd, long offset, SeekFlags whence)
3360                 {
3361                         short _whence = NativeConvert.FromSeekFlags (whence);
3362                         return sys_lseek (fd, offset, _whence);
3363                 }
3364
3365     [DllImport (LIBC, SetLastError=true)]
3366                 public static extern int close (int fd);
3367
3368                 // read(2)
3369                 //    ssize_t read(int fd, void *buf, size_t count);
3370                 [DllImport (MPH, SetLastError=true, 
3371                                 EntryPoint="Mono_Posix_Syscall_read")]
3372                 public static extern long read (int fd, IntPtr buf, ulong count);
3373
3374                 public static unsafe long read (int fd, void *buf, ulong count)
3375                 {
3376                         return read (fd, (IntPtr) buf, count);
3377                 }
3378
3379                 // write(2)
3380                 //    ssize_t write(int fd, const void *buf, size_t count);
3381                 [DllImport (MPH, SetLastError=true, 
3382                                 EntryPoint="Mono_Posix_Syscall_write")]
3383                 public static extern long write (int fd, IntPtr buf, ulong count);
3384
3385                 public static unsafe long write (int fd, void *buf, ulong count)
3386                 {
3387                         return write (fd, (IntPtr) buf, count);
3388                 }
3389
3390                 // pread(2)
3391                 //    ssize_t pread(int fd, void *buf, size_t count, off_t offset);
3392                 [DllImport (MPH, SetLastError=true, 
3393                                 EntryPoint="Mono_Posix_Syscall_pread")]
3394                 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
3395
3396                 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
3397                 {
3398                         return pread (fd, (IntPtr) buf, count, offset);
3399                 }
3400
3401                 // pwrite(2)
3402                 //    ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
3403                 [DllImport (MPH, SetLastError=true, 
3404                                 EntryPoint="Mono_Posix_Syscall_pwrite")]
3405                 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
3406
3407                 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
3408                 {
3409                         return pwrite (fd, (IntPtr) buf, count, offset);
3410                 }
3411
3412                 [DllImport (MPH, SetLastError=true, 
3413                                 EntryPoint="Mono_Posix_Syscall_pipe")]
3414                 public static extern int pipe (out int reading, out int writing);
3415
3416                 public static int pipe (int[] filedes)
3417                 {
3418                         if (filedes == null || filedes.Length != 2) {
3419                                 // TODO: set errno
3420                                 return -1;
3421                         }
3422                         int reading, writing;
3423                         int r = pipe (out reading, out writing);
3424                         filedes[0] = reading;
3425                         filedes[1] = writing;
3426                         return r;
3427                 }
3428
3429                 [DllImport (LIBC, SetLastError=true)]
3430                 public static extern uint alarm (uint seconds);
3431
3432                 [DllImport (LIBC, SetLastError=true)]
3433                 public static extern uint sleep (uint seconds);
3434
3435                 [DllImport (LIBC, SetLastError=true)]
3436                 public static extern uint ualarm (uint usecs, uint interval);
3437
3438                 [DllImport (LIBC, SetLastError=true)]
3439                 public static extern int pause ();
3440
3441                 // chown(2)
3442                 //    int chown(const char *path, uid_t owner, gid_t group);
3443                 [DllImport (LIBC, SetLastError=true)]
3444                 public static extern int chown (
3445                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3446                                 string path, uint owner, uint group);
3447
3448                 // fchown(2)
3449                 //    int fchown(int fd, uid_t owner, gid_t group);
3450                 [DllImport (LIBC, SetLastError=true)]
3451                 public static extern int fchown (int fd, uint owner, uint group);
3452
3453                 // lchown(2)
3454                 //    int lchown(const char *path, uid_t owner, gid_t group);
3455                 [DllImport (LIBC, SetLastError=true)]
3456                 public static extern int lchown (
3457                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3458                                 string path, uint owner, uint group);
3459
3460                 [DllImport (LIBC, SetLastError=true)]
3461                 public static extern int chdir (
3462                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3463                                 string path);
3464
3465                 [DllImport (LIBC, SetLastError=true)]
3466                 public static extern int fchdir (int fd);
3467
3468                 // getcwd(3)
3469                 //    char *getcwd(char *buf, size_t size);
3470                 [DllImport (MPH, SetLastError=true,
3471                                 EntryPoint="Mono_Posix_Syscall_getcwd")]
3472                 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
3473
3474                 public static StringBuilder getcwd (StringBuilder buf)
3475                 {
3476                         getcwd (buf, (ulong) buf.Capacity);
3477                         return buf;
3478                 }
3479
3480                 // getwd(2) is deprecated; don't expose it.
3481
3482                 [DllImport (LIBC, SetLastError=true)]
3483                 public static extern int dup (int fd);
3484
3485                 [DllImport (LIBC, SetLastError=true)]
3486                 public static extern int dup2 (int fd, int fd2);
3487
3488                 // TODO: does Mono marshal arrays properly?
3489                 [DllImport (LIBC, SetLastError=true)]
3490                 public static extern int execve (
3491                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3492                                 string path, string[] argv, string[] envp);
3493
3494                 [DllImport (LIBC, SetLastError=true)]
3495                 public static extern int fexecve (int fd, string[] argv, string[] envp);
3496
3497                 [DllImport (LIBC, SetLastError=true)]
3498                 public static extern int execv (
3499                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3500                                 string path, string[] argv);
3501
3502                 // TODO: execle, execl, execlp
3503                 [DllImport (LIBC, SetLastError=true)]
3504                 public static extern int execvp (
3505                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3506                                 string path, string[] argv);
3507
3508                 [DllImport (LIBC, SetLastError=true)]
3509                 public static extern int nice (int inc);
3510
3511                 [DllImport (LIBC, SetLastError=true)]
3512                 [CLSCompliant (false)]
3513                 public static extern int _exit (int status);
3514
3515                 [DllImport (MPH, SetLastError=true,
3516                                 EntryPoint="Mono_Posix_Syscall_fpathconf")]
3517                 public static extern long fpathconf (int filedes, PathconfName name, Errno defaultError);
3518
3519                 public static long fpathconf (int filedes, PathconfName name)
3520                 {
3521                         return fpathconf (filedes, name, (Errno) 0);
3522                 }
3523
3524                 [DllImport (MPH, SetLastError=true,
3525                                 EntryPoint="Mono_Posix_Syscall_pathconf")]
3526                 public static extern long pathconf (
3527                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3528                                 string path, PathconfName name, Errno defaultError);
3529
3530                 public static long pathconf (string path, PathconfName name)
3531                 {
3532                         return pathconf (path, name, (Errno) 0);
3533                 }
3534
3535                 [DllImport (MPH, SetLastError=true,
3536                                 EntryPoint="Mono_Posix_Syscall_sysconf")]
3537                 public static extern long sysconf (SysconfName name, Errno defaultError);
3538
3539                 public static long sysconf (SysconfName name)
3540                 {
3541                         return sysconf (name, (Errno) 0);
3542                 }
3543
3544                 // confstr(3)
3545                 //    size_t confstr(int name, char *buf, size_t len);
3546                 [DllImport (MPH, SetLastError=true,
3547                                 EntryPoint="Mono_Posix_Syscall_confstr")]
3548                 public static extern ulong confstr (ConfstrName name, [Out] StringBuilder buf, ulong len);
3549
3550                 // getpid(2)
3551                 //    pid_t getpid(void);
3552                 [DllImport (LIBC, SetLastError=true)]
3553                 public static extern int getpid ();
3554
3555                 // getppid(2)
3556                 //    pid_t getppid(void);
3557                 [DllImport (LIBC, SetLastError=true)]
3558                 public static extern int getppid ();
3559
3560                 // setpgid(2)
3561                 //    int setpgid(pid_t pid, pid_t pgid);
3562                 [DllImport (LIBC, SetLastError=true)]
3563                 public static extern int setpgid (int pid, int pgid);
3564
3565                 // getpgid(2)
3566                 //    pid_t getpgid(pid_t pid);
3567                 [DllImport (LIBC, SetLastError=true)]
3568                 public static extern int getpgid (int pid);
3569
3570                 [DllImport (LIBC, SetLastError=true)]
3571                 public static extern int setpgrp ();
3572
3573                 // getpgrp(2)
3574                 //    pid_t getpgrp(void);
3575                 [DllImport (LIBC, SetLastError=true)]
3576                 public static extern int getpgrp ();
3577
3578                 // setsid(2)
3579                 //    pid_t setsid(void);
3580                 [DllImport (LIBC, SetLastError=true)]
3581                 public static extern int setsid ();
3582
3583                 // getsid(2)
3584                 //    pid_t getsid(pid_t pid);
3585                 [DllImport (LIBC, SetLastError=true)]
3586                 public static extern int getsid (int pid);
3587
3588                 // getuid(2)
3589                 //    uid_t getuid(void);
3590                 [DllImport (LIBC, SetLastError=true)]
3591                 public static extern uint getuid ();
3592
3593                 // geteuid(2)
3594                 //    uid_t geteuid(void);
3595                 [DllImport (LIBC, SetLastError=true)]
3596                 public static extern uint geteuid ();
3597
3598                 // getgid(2)
3599                 //    gid_t getgid(void);
3600                 [DllImport (LIBC, SetLastError=true)]
3601                 public static extern uint getgid ();
3602
3603                 // getegid(2)
3604                 //    gid_t getgid(void);
3605                 [DllImport (LIBC, SetLastError=true)]
3606                 public static extern uint getegid ();
3607
3608                 // getgroups(2)
3609                 //    int getgroups(int size, gid_t list[]);
3610                 [DllImport (LIBC, SetLastError=true)]
3611                 public static extern int getgroups (int size, uint[] list);
3612
3613                 public static int getgroups (uint[] list)
3614                 {
3615                         return getgroups (list.Length, list);
3616                 }
3617
3618                 // setuid(2)
3619                 //    int setuid(uid_t uid);
3620                 [DllImport (LIBC, SetLastError=true)]
3621                 public static extern int setuid (uint uid);
3622
3623                 // setreuid(2)
3624                 //    int setreuid(uid_t ruid, uid_t euid);
3625                 [DllImport (LIBC, SetLastError=true)]
3626                 public static extern int setreuid (uint ruid, uint euid);
3627
3628                 // setregid(2)
3629                 //    int setregid(gid_t ruid, gid_t euid);
3630                 [DllImport (LIBC, SetLastError=true)]
3631                 public static extern int setregid (uint rgid, uint egid);
3632
3633                 // seteuid(2)
3634                 //    int seteuid(uid_t euid);
3635                 [DllImport (LIBC, SetLastError=true)]
3636                 public static extern int seteuid (uint euid);
3637
3638                 // setegid(2)
3639                 //    int setegid(gid_t euid);
3640                 [DllImport (LIBC, SetLastError=true)]
3641                 public static extern int setegid (uint uid);
3642
3643                 // setgid(2)
3644                 //    int setgid(gid_t gid);
3645                 [DllImport (LIBC, SetLastError=true)]
3646                 public static extern int setgid (uint gid);
3647
3648                 // getresuid(2)
3649                 //    int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
3650                 [DllImport (LIBC, SetLastError=true)]
3651                 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
3652
3653                 // getresgid(2)
3654                 //    int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
3655                 [DllImport (LIBC, SetLastError=true)]
3656                 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
3657
3658                 // setresuid(2)
3659                 //    int setresuid(uid_t ruid, uid_t euid, uid_t suid);
3660                 [DllImport (LIBC, SetLastError=true)]
3661                 public static extern int setresuid (uint ruid, uint euid, uint suid);
3662
3663                 // setresgid(2)
3664                 //    int setresgid(gid_t ruid, gid_t euid, gid_t suid);
3665                 [DllImport (LIBC, SetLastError=true)]
3666                 public static extern int setresgid (uint rgid, uint egid, uint sgid);
3667
3668 #if false
3669                 // fork(2)
3670                 //    pid_t fork(void);
3671                 [DllImport (LIBC, SetLastError=true)]
3672                 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " + 
3673                                 "shutdown code.\nUse System.Diagnostics.Process instead")]
3674                 private static extern int fork ();
3675
3676                 // vfork(2)
3677                 //    pid_t vfork(void);
3678                 [DllImport (LIBC, SetLastError=true)]
3679                 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " + 
3680                                 "shutdown code.\nUse System.Diagnostics.Process instead")]
3681                 private static extern int vfork ();
3682 #endif
3683
3684                 private static object tty_lock = new object ();
3685
3686                 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
3687                 private static extern IntPtr sys_ttyname (int fd);
3688
3689                 public static string ttyname (int fd)
3690                 {
3691                         lock (tty_lock) {
3692                                 IntPtr r = sys_ttyname (fd);
3693                                 return UnixMarshal.PtrToString (r);
3694                         }
3695                 }
3696
3697                 // ttyname_r(3)
3698                 //    int ttyname_r(int fd, char *buf, size_t buflen);
3699                 [DllImport (MPH, SetLastError=true,
3700                                 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
3701                 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
3702
3703                 public static int ttyname_r (int fd, StringBuilder buf)
3704                 {
3705                         return ttyname_r (fd, buf, (ulong) buf.Capacity);
3706                 }
3707
3708                 [DllImport (LIBC, EntryPoint="isatty")]
3709                 private static extern int sys_isatty (int fd);
3710
3711                 public static bool isatty (int fd)
3712                 {
3713                         return sys_isatty (fd) == 1;
3714                 }
3715
3716                 [DllImport (LIBC, SetLastError=true)]
3717                 public static extern int link (
3718                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3719                                 string oldpath, 
3720                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3721                                 string newpath);
3722
3723                 [DllImport (LIBC, SetLastError=true)]
3724                 public static extern int symlink (
3725                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3726                                 string oldpath, 
3727                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3728                                 string newpath);
3729
3730                 // readlink(2)
3731                 //    int readlink(const char *path, char *buf, size_t bufsize);
3732                 [DllImport (MPH, SetLastError=true,
3733                                 EntryPoint="Mono_Posix_Syscall_readlink")]
3734                 public static extern int readlink (
3735                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3736                                 string path, [Out] StringBuilder buf, ulong bufsiz);
3737
3738                 public static int readlink (string path, [Out] StringBuilder buf)
3739                 {
3740                         return readlink (path, buf, (ulong) buf.Capacity);
3741                 }
3742
3743                 [DllImport (LIBC, SetLastError=true)]
3744                 public static extern int unlink (
3745                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3746                                 string pathname);
3747
3748                 [DllImport (LIBC, SetLastError=true)]
3749                 public static extern int rmdir (
3750                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3751                                 string pathname);
3752
3753                 // tcgetpgrp(3)
3754                 //    pid_t tcgetpgrp(int fd);
3755                 [DllImport (LIBC, SetLastError=true)]
3756                 public static extern int tcgetpgrp (int fd);
3757
3758                 // tcsetpgrp(3)
3759                 //    int tcsetpgrp(int fd, pid_t pgrp);
3760                 [DllImport (LIBC, SetLastError=true)]
3761                 public static extern int tcsetpgrp (int fd, int pgrp);
3762
3763                 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
3764                 private static extern IntPtr sys_getlogin ();
3765
3766                 public static string getlogin ()
3767                 {
3768                         lock (getlogin_lock) {
3769                                 IntPtr r = sys_getlogin ();
3770                                 return UnixMarshal.PtrToString (r);
3771                         }
3772                 }
3773
3774                 // getlogin_r(3)
3775                 //    int getlogin_r(char *buf, size_t bufsize);
3776                 [DllImport (MPH, SetLastError=true,
3777                                 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
3778                 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
3779
3780                 public static int getlogin_r (StringBuilder name)
3781                 {
3782                         return getlogin_r (name, (ulong) name.Capacity);
3783                 }
3784
3785                 [DllImport (LIBC, SetLastError=true)]
3786                 public static extern int setlogin (string name);
3787
3788                 // gethostname(2)
3789                 //    int gethostname(char *name, size_t len);
3790                 [DllImport (MPH, SetLastError=true,
3791                                 EntryPoint="Mono_Posix_Syscall_gethostname")]
3792                 public static extern int gethostname ([Out] StringBuilder name, ulong len);
3793
3794                 public static int gethostname (StringBuilder name)
3795                 {
3796                         return gethostname (name, (ulong) name.Capacity);
3797                 }
3798
3799                 // sethostname(2)
3800                 //    int gethostname(const char *name, size_t len);
3801                 [DllImport (MPH, SetLastError=true,
3802                                 EntryPoint="Mono_Posix_Syscall_sethostname")]
3803                 public static extern int sethostname (string name, ulong len);
3804
3805                 public static int sethostname (string name)
3806                 {
3807                         return sethostname (name, (ulong) name.Length);
3808                 }
3809
3810                 [DllImport (MPH, SetLastError=true,
3811                                 EntryPoint="Mono_Posix_Syscall_gethostid")]
3812                 public static extern long gethostid ();
3813
3814                 [DllImport (MPH, SetLastError=true,
3815                                 EntryPoint="Mono_Posix_Syscall_sethostid")]
3816                 public static extern int sethostid (long hostid);
3817
3818                 // getdomainname(2)
3819                 //    int getdomainname(char *name, size_t len);
3820                 [DllImport (MPH, SetLastError=true,
3821                                 EntryPoint="Mono_Posix_Syscall_getdomainname")]
3822                 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
3823
3824                 public static int getdomainname (StringBuilder name)
3825                 {
3826                         return getdomainname (name, (ulong) name.Capacity);
3827                 }
3828
3829                 // setdomainname(2)
3830                 //    int setdomainname(const char *name, size_t len);
3831                 [DllImport (MPH, SetLastError=true,
3832                                 EntryPoint="Mono_Posix_Syscall_setdomainname")]
3833                 public static extern int setdomainname (string name, ulong len);
3834
3835                 public static int setdomainname (string name)
3836                 {
3837                         return setdomainname (name, (ulong) name.Length);
3838                 }
3839
3840                 [DllImport (LIBC, SetLastError=true)]
3841                 public static extern int vhangup ();
3842
3843                 // Revoke doesn't appear to be POSIX.  Include it?
3844                 [DllImport (LIBC, SetLastError=true)]
3845                 public static extern int revoke (
3846                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3847                                 string file);
3848
3849                 // TODO: profil?  It's not POSIX.
3850
3851                 [DllImport (LIBC, SetLastError=true)]
3852                 public static extern int acct (
3853                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3854                                 string filename);
3855
3856                 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
3857                 private static extern IntPtr sys_getusershell ();
3858
3859                 internal static object usershell_lock = new object ();
3860
3861                 public static string getusershell ()
3862                 {
3863                         lock (usershell_lock) {
3864                                 IntPtr r = sys_getusershell ();
3865                                 return UnixMarshal.PtrToString (r);
3866                         }
3867                 }
3868
3869                 [DllImport (MPH, SetLastError=true, 
3870                                 EntryPoint="Mono_Posix_Syscall_setusershell")]
3871                 private static extern int sys_setusershell ();
3872
3873                 public static int setusershell ()
3874                 {
3875                         lock (usershell_lock) {
3876                                 return sys_setusershell ();
3877                         }
3878                 }
3879
3880                 [DllImport (MPH, SetLastError=true, 
3881                                 EntryPoint="Mono_Posix_Syscall_endusershell")]
3882                 private static extern int sys_endusershell ();
3883
3884                 public static int endusershell ()
3885                 {
3886                         lock (usershell_lock) {
3887                                 return sys_endusershell ();
3888                         }
3889                 }
3890
3891 #if false
3892                 [DllImport (LIBC, SetLastError=true)]
3893                 private static extern int daemon (int nochdir, int noclose);
3894
3895                 // this implicitly forks, and thus isn't safe.
3896                 private static int daemon (bool nochdir, bool noclose)
3897                 {
3898                         return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
3899                 }
3900 #endif
3901
3902                 [DllImport (LIBC, SetLastError=true)]
3903                 public static extern int chroot (
3904                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3905                                 string path);
3906
3907                 // skipping getpass(3) as the man page states:
3908                 //   This function is obsolete.  Do not use it.
3909
3910                 [DllImport (LIBC, SetLastError=true)]
3911                 public static extern int fsync (int fd);
3912
3913                 [DllImport (LIBC, SetLastError=true)]
3914                 public static extern int fdatasync (int fd);
3915
3916                 [DllImport (MPH, SetLastError=true,
3917                                 EntryPoint="Mono_Posix_Syscall_sync")]
3918                 public static extern int sync ();
3919
3920                 [DllImport (LIBC, SetLastError=true)]
3921                 [Obsolete ("Dropped in POSIX 1003.1-2001.  " +
3922                                 "Use Syscall.sysconf (SysconfName._SC_PAGESIZE).")]
3923                 public static extern int getpagesize ();
3924
3925                 // truncate(2)
3926                 //    int truncate(const char *path, off_t length);
3927                 [DllImport (MPH, SetLastError=true, 
3928                                 EntryPoint="Mono_Posix_Syscall_truncate")]
3929                 public static extern int truncate (
3930                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3931                                 string path, long length);
3932
3933                 // ftruncate(2)
3934                 //    int ftruncate(int fd, off_t length);
3935                 [DllImport (MPH, SetLastError=true, 
3936                                 EntryPoint="Mono_Posix_Syscall_ftruncate")]
3937                 public static extern int ftruncate (int fd, long length);
3938
3939                 [DllImport (LIBC, SetLastError=true)]
3940                 public static extern int getdtablesize ();
3941
3942                 [DllImport (LIBC, SetLastError=true)]
3943                 public static extern int brk (IntPtr end_data_segment);
3944
3945                 [DllImport (LIBC, SetLastError=true)]
3946                 public static extern IntPtr sbrk (IntPtr increment);
3947
3948                 // TODO: syscall(2)?
3949                 // Probably safer to skip entirely.
3950
3951                 // lockf(3)
3952                 //    int lockf(int fd, int cmd, off_t len);
3953                 [DllImport (MPH, SetLastError=true, 
3954                                 EntryPoint="Mono_Posix_Syscall_lockf")]
3955                 public static extern int lockf (int fd, LockfCommand cmd, long len);
3956
3957                 [Obsolete ("This is insecure and should not be used", true)]
3958                 public static string crypt (string key, string salt)
3959                 {
3960                         throw new SecurityException ("crypt(3) has been broken.  Use something more secure.");
3961                 }
3962
3963                 [Obsolete ("This is insecure and should not be used", true)]
3964                 public static int encrypt (byte[] block, bool decode)
3965                 {
3966                         throw new SecurityException ("crypt(3) has been broken.  Use something more secure.");
3967                 }
3968
3969                 // swab(3)
3970                 //    void swab(const void *from, void *to, ssize_t n);
3971                 [DllImport (MPH, SetLastError=true, 
3972                                 EntryPoint="Mono_Posix_Syscall_swab")]
3973                 public static extern int swab (IntPtr from, IntPtr to, long n);
3974
3975                 public static unsafe void swab (void* from, void* to, long n)
3976                 {
3977                         swab ((IntPtr) from, (IntPtr) to, n);
3978                 }
3979
3980                 #endregion
3981
3982                 #region <utime.h> Declarations
3983                 //
3984                 // <utime.h>  -- COMPLETE
3985                 //
3986
3987                 [DllImport (MPH, SetLastError=true, 
3988                                 EntryPoint="Mono_Posix_Syscall_utime")]
3989                 private static extern int sys_utime (
3990                                 [MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
3991                                 string filename, ref Utimbuf buf, int use_buf);
3992
3993                 public static int utime (string filename, ref Utimbuf buf)
3994                 {
3995                         return sys_utime (filename, ref buf, 1);
3996                 }
3997
3998                 public static int utime (string filename)
3999                 {
4000                         Utimbuf buf = new Utimbuf ();
4001                         return sys_utime (filename, ref buf, 0);
4002                 }
4003                 #endregion
4004         }
4005
4006         #endregion
4007 }
4008
4009 // vim: noexpandtab