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