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