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