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