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