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