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