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