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