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