**** Merged r40732-r40872 from MCS ****
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / 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-2005 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 //    UnixConvert 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.Text;
60 using Mono.Unix;
61
62 [assembly:Mono.Unix.IncludeAttribute (
63         new string [] {"sys/types.h", "sys/stat.h", "sys/poll.h", "sys/wait.h",
64                 "unistd.h", "fcntl.h", "signal.h", "poll.h", "grp.h", "errno.h"}, 
65         new string [] {"_GNU_SOURCE", "_XOPEN_SOURCE"})]
66
67 namespace Mono.Unix {
68
69         #region Enumerations
70
71         [Flags][Map]
72         public enum SyslogOptions {
73                 LOG_PID    = 0x01,  // log the pid with each message
74                 LOG_CONS   = 0x02,  // log on the console if errors in sending
75                 LOG_ODELAY = 0x04,  // delay open until first syslog (default)
76                 LOG_NDELAY = 0x08,  // don't delay open
77                 LOG_NOWAIT = 0x10,  // don't wait for console forks; DEPRECATED
78                 LOG_PERROR = 0x20   // log to stderr as well
79         }
80
81         [Flags][Map]
82         public enum SyslogFacility {
83                 LOG_KERN      = 0 << 3,
84                 LOG_USRE      = 1 << 3,
85                 LOG_MAIL      = 2 << 3,
86                 LOG_DAEMON    = 3 << 3,
87                 LOG_AUTH      = 4 << 3,
88                 LOG_SYSLOG    = 5 << 3,
89                 LOG_LPR       = 6 << 3,
90                 LOG_NEWS      = 7 << 3,
91                 LOG_UUCP      = 8 << 3,
92                 LOG_CRON      = 8 << 3,
93                 LOG_AUTHPRIV  = 10 << 3,
94                 LOG_FTP       = 11 << 3,
95                 LOG_LOCAL0    = 16 << 3,
96                 LOG_LOCAL1    = 17 << 3,
97                 LOG_LOCAL2    = 18 << 3,
98                 LOG_LOCAL3    = 19 << 3,
99                 LOG_LOCAL4    = 20 << 3,
100                 LOG_LOCAL5    = 21 << 3,
101                 LOG_LOCAL6    = 22 << 3,
102                 LOG_LOCAL7    = 23 << 3,
103         }
104
105         [Flags][Map]
106         public enum SyslogLevel {
107                 LOG_EMERG   = 0,  // system is unusable
108                 LOG_ALERT   = 1,  // action must be taken immediately
109                 LOG_CRIT    = 2,  // critical conditions
110                 LOG_ERR     = 3,  // warning conditions
111                 LOG_WARNING = 4,  // warning conditions
112                 LOG_NOTICE  = 5,  // normal but significant condition
113                 LOG_INFO    = 6,  // informational
114                 LOG_DEBUG   = 7   // debug-level messages
115         }
116
117         [Map][Flags]
118         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 UnixConvert, 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         public enum FilePermissions : uint {
157                 S_ISUID     = 0x0800, // Set user ID on execution
158                 S_ISGID     = 0x0400, // Set gorup ID on execution
159                 S_ISVTX     = 0x0200, // Save swapped text after use (sticky).
160                 S_IRUSR     = 0x0100, // Read by owner
161                 S_IWUSR     = 0x0080, // Write by owner
162                 S_IXUSR     = 0x0040, // Execute by owner
163                 S_IRGRP     = 0x0020, // Read by group
164                 S_IWGRP     = 0x0010, // Write by group
165                 S_IXGRP     = 0x0008, // Execute by group
166                 S_IROTH     = 0x0004, // Read by other
167                 S_IWOTH     = 0x0002, // Write by other
168                 S_IXOTH     = 0x0001, // Execute by other
169
170                 S_IRWXG     = (S_IRGRP | S_IWGRP | S_IXGRP),
171                 S_IRWXU     = (S_IRUSR | S_IWUSR | S_IXUSR),
172                 S_IRWXO     = (S_IROTH | S_IWOTH | S_IXOTH),
173                 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
174                 ALLPERMS    = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
175                 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
176
177                 // Device types
178                 // Why these are held in "mode_t" is beyond me...
179                 S_IFMT      = 0xF000, // Bits which determine file type
180                 S_IFDIR     = 0x4000, // Directory
181                 S_IFCHR     = 0x2000, // Character device
182                 S_IFBLK     = 0x6000, // Block device
183                 S_IFREG     = 0x8000, // Regular file
184                 S_IFIFO     = 0x1000, // FIFO
185                 S_IFLNK     = 0xA000, // Symbolic link
186                 S_IFSOCK    = 0xC000, // Socket
187         }
188
189         [Map]
190         public enum FcntlCommand : int {
191                 // Form /usr/include/bits/fcntl.h
192                 F_DUPFD    =    0, // Duplicate file descriptor.
193                 F_GETFD    =    1, // Get file descriptor flags.
194                 F_SETFD    =    2, // Set file descriptor flags.
195                 F_GETFL    =    3, // Get file status flags.
196                 F_SETFL    =    4, // Set file status flags.
197                 F_GETLK    =   12, // Get record locking info. [64]
198                 F_SETLK    =   13, // Set record locking info (non-blocking). [64]
199                 F_SETLKW   =   14, // Set record locking info (blocking). [64]
200                 F_SETOWN   =    8, // Set owner of socket (receiver of SIGIO).
201                 F_GETOWN   =    9, // Get owner of socket (receiver of SIGIO).
202                 F_SETSIG   =   10, // Set number of signal to be sent.
203                 F_GETSIG   =   11, // Get number of signal to be sent.
204                 F_SETLEASE = 1024, // Set a lease.
205                 F_GETLEASE = 1025, // Enquire what lease is active.
206                 F_NOTIFY   = 1026, // Required notifications on a directory
207         }
208
209         [Map]
210         public enum LockType : short {
211                 F_RDLCK = 0, // Read lock.
212                 F_WRLCK = 1, // Write lock.
213                 F_UNLCK = 2, // Remove lock.
214         }
215
216         [Map]
217         public enum SeekFlags : short {
218                 // values liberally copied from /usr/include/unistd.h
219                 SEEK_SET = 0, // Seek from beginning of file.
220                 SEEK_CUR = 1, // Seek from current position.
221                 SEEK_END = 2, // Seek from end of file.
222
223                 L_SET    = SEEK_SET, // BSD alias for SEEK_SET
224                 L_INCR   = SEEK_CUR, // BSD alias for SEEK_CUR
225                 L_XTND   = SEEK_END, // BSD alias for SEEK_END
226         }
227         
228         [Map, Flags]
229         public enum DirectoryNotifyFlags : int {
230                 // from /usr/include/bits/fcntl.h
231                 DN_ACCESS    = 0x00000001, // File accessed.
232                 DN_MODIFY    = 0x00000002, // File modified.
233                 DN_CREATE    = 0x00000004, // File created.
234                 DN_DELETE    = 0x00000008, // File removed.
235                 DN_RENAME    = 0x00000010, // File renamed.
236                 DN_ATTRIB    = 0x00000020, // File changed attributes.
237                 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
238         }
239
240         [Map]
241         public enum PosixFadviseAdvice : int {
242                 POSIX_FADV_NORMAL     = 0,  // No further special treatment.
243                 POSIX_FADV_RANDOM     = 1,  // Expect random page references.
244                 POSIX_FADV_SEQUENTIAL = 2,  // Expect sequential page references.
245                 POSIX_FADV_WILLNEED   = 3,  // Will need these pages.
246                 POSIX_FADV_DONTNEED   = 4,  // Don't need these pages.
247                 POSIX_FADV_NOREUSE    = 5,  // Data will be accessed once.
248         }
249
250         [Map]
251         public enum PosixMadviseAdvice : int {
252                 POSIX_MADV_NORMAL     = 0,  // No further special treatment.
253                 POSIX_MADV_RANDOM     = 1,  // Expect random page references.
254                 POSIX_MADV_SEQUENTIAL = 2,  // Expect sequential page references.
255                 POSIX_MADV_WILLNEED   = 3,  // Will need these pages.
256                 POSIX_MADV_DONTNEED   = 4,  // Don't need these pages.
257         }
258
259         [Map]
260         public enum Signum : int {
261                 SIGHUP    =  1, // Hangup (POSIX).
262                 SIGINT    =  2, // Interrupt (ANSI).
263                 SIGQUIT   =  3, // Quit (POSIX).
264                 SIGILL    =  4, // Illegal instruction (ANSI).
265                 SIGTRAP   =  5, // Trace trap (POSIX).
266                 SIGABRT   =  6, // Abort (ANSI).
267                 SIGIOT    =  6, // IOT trap (4.2 BSD).
268                 SIGBUS    =  7, // BUS error (4.2 BSD).
269                 SIGFPE    =  8, // Floating-point exception (ANSI).
270                 SIGKILL   =  9, // Kill, unblockable (POSIX).
271                 SIGUSR1   = 10, // User-defined signal 1 (POSIX).
272                 SIGSEGV   = 11, // Segmentation violation (ANSI).
273                 SIGUSR2   = 12, // User-defined signal 2 (POSIX).
274                 SIGPIPE   = 13, // Broken pipe (POSIX).
275                 SIGALRM   = 14, // Alarm clock (POSIX).
276                 SIGTERM   = 15, // Termination (ANSI).
277                 SIGSTKFLT = 16, // Stack fault.
278                 SIGCLD    = SIGCHLD, // Same as SIGCHLD (System V).
279                 SIGCHLD   = 17, // Child status has changed (POSIX).
280                 SIGCONT   = 18, // Continue (POSIX).
281                 SIGSTOP   = 19, // Stop, unblockable (POSIX).
282                 SIGTSTP   = 20, // Keyboard stop (POSIX).
283                 SIGTTIN   = 21, // Background read from tty (POSIX).
284                 SIGTTOU   = 22, // Background write to tty (POSIX).
285                 SIGURG    = 23, // Urgent condition on socket (4.2 BSD).
286                 SIGXCPU   = 24, // CPU limit exceeded (4.2 BSD).
287                 SIGXFSZ   = 25, // File size limit exceeded (4.2 BSD).
288                 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
289                 SIGPROF   = 27, // Profiling alarm clock (4.2 BSD).
290                 SIGWINCH  = 28, // Window size change (4.3 BSD, Sun).
291                 SIGPOLL   = SIGIO, // Pollable event occurred (System V).
292                 SIGIO     = 29, // I/O now possible (4.2 BSD).
293                 SIGPWR    = 30, // Power failure restart (System V).
294                 SIGSYS    = 31, // Bad system call.
295                 SIGUNUSED = 31
296         }
297
298         [Flags][Map]
299         public enum WaitOptions : int {
300                 WNOHANG   = 1,  // Don't block waiting
301                 WUNTRACED = 2,  // Report status of stopped children
302         }
303
304   [Flags][Map]
305         public enum AccessMode : int {
306                 R_OK = 1,
307                 W_OK = 2,
308                 X_OK = 4,
309                 F_OK = 8,
310         }
311
312         [Map]
313         public enum PathConf : int {
314                 _PC_LINK_MAX,
315                 _PC_MAX_CANON,
316                 _PC_MAX_INPUT,
317                 _PC_NAME_MAX,
318                 _PC_PATH_MAX,
319                 _PC_PIPE_BUF,
320                 _PC_CHOWN_RESTRICTED,
321                 _PC_NO_TRUNC,
322                 _PC_VDISABLE,
323                 _PC_SYNC_IO,
324                 _PC_ASYNC_IO,
325                 _PC_PRIO_IO,
326                 _PC_SOCK_MAXBUF,
327                 _PC_FILESIZEBITS,
328                 _PC_REC_INCR_XFER_SIZE,
329                 _PC_REC_MAX_XFER_SIZE,
330                 _PC_REC_MIN_XFER_SIZE,
331                 _PC_REC_XFER_ALIGN,
332                 _PC_ALLOC_SIZE_MIN,
333                 _PC_SYMLINK_MAX,
334                 _PC_2_SYMLINKS
335         }
336
337         [Map]
338         public enum SysConf : int {
339                 _SC_ARG_MAX,
340                 _SC_CHILD_MAX,
341                 _SC_CLK_TCK,
342                 _SC_NGROUPS_MAX,
343                 _SC_OPEN_MAX,
344                 _SC_STREAM_MAX,
345                 _SC_TZNAME_MAX,
346                 _SC_JOB_CONTROL,
347                 _SC_SAVED_IDS,
348                 _SC_REALTIME_SIGNALS,
349                 _SC_PRIORITY_SCHEDULING,
350                 _SC_TIMERS,
351                 _SC_ASYNCHRONOUS_IO,
352                 _SC_PRIORITIZED_IO,
353                 _SC_SYNCHRONIZED_IO,
354                 _SC_FSYNC,
355                 _SC_MAPPED_FILES,
356                 _SC_MEMLOCK,
357                 _SC_MEMLOCK_RANGE,
358                 _SC_MEMORY_PROTECTION,
359                 _SC_MESSAGE_PASSING,
360                 _SC_SEMAPHORES,
361                 _SC_SHARED_MEMORY_OBJECTS,
362                 _SC_AIO_LISTIO_MAX,
363                 _SC_AIO_MAX,
364                 _SC_AIO_PRIO_DELTA_MAX,
365                 _SC_DELAYTIMER_MAX,
366                 _SC_MQ_OPEN_MAX,
367                 _SC_MQ_PRIO_MAX,
368                 _SC_VERSION,
369                 _SC_PAGESIZE,
370                 _SC_RTSIG_MAX,
371                 _SC_SEM_NSEMS_MAX,
372                 _SC_SEM_VALUE_MAX,
373                 _SC_SIGQUEUE_MAX,
374                 _SC_TIMER_MAX,
375                 /* Values for the argument to `sysconf'
376                          corresponding to _POSIX2_* symbols.  */
377                 _SC_BC_BASE_MAX,
378                 _SC_BC_DIM_MAX,
379                 _SC_BC_SCALE_MAX,
380                 _SC_BC_STRING_MAX,
381                 _SC_COLL_WEIGHTS_MAX,
382                 _SC_EQUIV_CLASS_MAX,
383                 _SC_EXPR_NEST_MAX,
384                 _SC_LINE_MAX,
385                 _SC_RE_DUP_MAX,
386                 _SC_CHARCLASS_NAME_MAX,
387                 _SC_2_VERSION,
388                 _SC_2_C_BIND,
389                 _SC_2_C_DEV,
390                 _SC_2_FORT_DEV,
391                 _SC_2_FORT_RUN,
392                 _SC_2_SW_DEV,
393                 _SC_2_LOCALEDEF,
394                 _SC_PII,
395                 _SC_PII_XTI,
396                 _SC_PII_SOCKET,
397                 _SC_PII_INTERNET,
398                 _SC_PII_OSI,
399                 _SC_POLL,
400                 _SC_SELECT,
401                 _SC_UIO_MAXIOV,
402                 _SC_IOV_MAX = _SC_UIO_MAXIOV,
403                 _SC_PII_INTERNET_STREAM,
404                 _SC_PII_INTERNET_DGRAM,
405                 _SC_PII_OSI_COTS,
406                 _SC_PII_OSI_CLTS,
407                 _SC_PII_OSI_M,
408                 _SC_T_IOV_MAX,
409                 /* Values according to POSIX 1003.1c (POSIX threads).  */
410                 _SC_THREADS,
411                 _SC_THREAD_SAFE_FUNCTIONS,
412                 _SC_GETGR_R_SIZE_MAX,
413                 _SC_GETPW_R_SIZE_MAX,
414                 _SC_LOGIN_NAME_MAX,
415                 _SC_TTY_NAME_MAX,
416                 _SC_THREAD_DESTRUCTOR_ITERATIONS,
417                 _SC_THREAD_KEYS_MAX,
418                 _SC_THREAD_STACK_MIN,
419                 _SC_THREAD_THREADS_MAX,
420                 _SC_THREAD_ATTR_STACKADDR,
421                 _SC_THREAD_ATTR_STACKSIZE,
422                 _SC_THREAD_PRIORITY_SCHEDULING,
423                 _SC_THREAD_PRIO_INHERIT,
424                 _SC_THREAD_PRIO_PROTECT,
425                 _SC_THREAD_PROCESS_SHARED,
426                 _SC_NPROCESSORS_CONF,
427                 _SC_NPROCESSORS_ONLN,
428                 _SC_PHYS_PAGES,
429                 _SC_AVPHYS_PAGES,
430                 _SC_ATEXIT_MAX,
431                 _SC_PASS_MAX,
432                 _SC_XOPEN_VERSION,
433                 _SC_XOPEN_XCU_VERSION,
434                 _SC_XOPEN_UNIX,
435                 _SC_XOPEN_CRYPT,
436                 _SC_XOPEN_ENH_I18N,
437                 _SC_XOPEN_SHM,
438                 _SC_2_CHAR_TERM,
439                 _SC_2_C_VERSION,
440                 _SC_2_UPE,
441                 _SC_XOPEN_XPG2,
442                 _SC_XOPEN_XPG3,
443                 _SC_XOPEN_XPG4,
444                 _SC_CHAR_BIT,
445                 _SC_CHAR_MAX,
446                 _SC_CHAR_MIN,
447                 _SC_INT_MAX,
448                 _SC_INT_MIN,
449                 _SC_LONG_BIT,
450                 _SC_WORD_BIT,
451                 _SC_MB_LEN_MAX,
452                 _SC_NZERO,
453                 _SC_SSIZE_MAX,
454                 _SC_SCHAR_MAX,
455                 _SC_SCHAR_MIN,
456                 _SC_SHRT_MAX,
457                 _SC_SHRT_MIN,
458                 _SC_UCHAR_MAX,
459                 _SC_UINT_MAX,
460                 _SC_ULONG_MAX,
461                 _SC_USHRT_MAX,
462                 _SC_NL_ARGMAX,
463                 _SC_NL_LANGMAX,
464                 _SC_NL_MSGMAX,
465                 _SC_NL_NMAX,
466                 _SC_NL_SETMAX,
467                 _SC_NL_TEXTMAX,
468                 _SC_XBS5_ILP32_OFF32,
469                 _SC_XBS5_ILP32_OFFBIG,
470                 _SC_XBS5_LP64_OFF64,
471                 _SC_XBS5_LPBIG_OFFBIG,
472                 _SC_XOPEN_LEGACY,
473                 _SC_XOPEN_REALTIME,
474                 _SC_XOPEN_REALTIME_THREADS,
475                 _SC_ADVISORY_INFO,
476                 _SC_BARRIERS,
477                 _SC_BASE,
478                 _SC_C_LANG_SUPPORT,
479                 _SC_C_LANG_SUPPORT_R,
480                 _SC_CLOCK_SELECTION,
481                 _SC_CPUTIME,
482                 _SC_THREAD_CPUTIME,
483                 _SC_DEVICE_IO,
484                 _SC_DEVICE_SPECIFIC,
485                 _SC_DEVICE_SPECIFIC_R,
486                 _SC_FD_MGMT,
487                 _SC_FIFO,
488                 _SC_PIPE,
489                 _SC_FILE_ATTRIBUTES,
490                 _SC_FILE_LOCKING,
491                 _SC_FILE_SYSTEM,
492                 _SC_MONOTONIC_CLOCK,
493                 _SC_MULTI_PROCESS,
494                 _SC_SINGLE_PROCESS,
495                 _SC_NETWORKING,
496                 _SC_READER_WRITER_LOCKS,
497                 _SC_SPIN_LOCKS,
498                 _SC_REGEXP,
499                 _SC_REGEX_VERSION,
500                 _SC_SHELL,
501                 _SC_SIGNALS,
502                 _SC_SPAWN,
503                 _SC_SPORADIC_SERVER,
504                 _SC_THREAD_SPORADIC_SERVER,
505                 _SC_SYSTEM_DATABASE,
506                 _SC_SYSTEM_DATABASE_R,
507                 _SC_TIMEOUTS,
508                 _SC_TYPED_MEMORY_OBJECTS,
509                 _SC_USER_GROUPS,
510                 _SC_USER_GROUPS_R,
511                 _SC_2_PBS,
512                 _SC_2_PBS_ACCOUNTING,
513                 _SC_2_PBS_LOCATE,
514                 _SC_2_PBS_MESSAGE,
515                 _SC_2_PBS_TRACK,
516                 _SC_SYMLOOP_MAX,
517                 _SC_STREAMS,
518                 _SC_2_PBS_CHECKPOINT,
519                 _SC_V6_ILP32_OFF32,
520                 _SC_V6_ILP32_OFFBIG,
521                 _SC_V6_LP64_OFF64,
522                 _SC_V6_LPBIG_OFFBIG,
523                 _SC_HOST_NAME_MAX,
524                 _SC_TRACE,
525                 _SC_TRACE_EVENT_FILTER,
526                 _SC_TRACE_INHERIT,
527                 _SC_TRACE_LOG,
528                 _SC_LEVEL1_ICACHE_SIZE,
529                 _SC_LEVEL1_ICACHE_ASSOC,
530                 _SC_LEVEL1_ICACHE_LINESIZE,
531                 _SC_LEVEL1_DCACHE_SIZE,
532                 _SC_LEVEL1_DCACHE_ASSOC,
533                 _SC_LEVEL1_DCACHE_LINESIZE,
534                 _SC_LEVEL2_CACHE_SIZE,
535                 _SC_LEVEL2_CACHE_ASSOC,
536                 _SC_LEVEL2_CACHE_LINESIZE,
537                 _SC_LEVEL3_CACHE_SIZE,
538                 _SC_LEVEL3_CACHE_ASSOC,
539                 _SC_LEVEL3_CACHE_LINESIZE,
540                 _SC_LEVEL4_CACHE_SIZE,
541                 _SC_LEVEL4_CACHE_ASSOC,
542                 _SC_LEVEL4_CACHE_LINESIZE
543         }
544
545         [Map]
546         public enum ConfStr : int {
547                 _CS_PATH,                       /* The default search path.  */
548                 _CS_V6_WIDTH_RESTRICTED_ENVS,
549                 _CS_GNU_LIBC_VERSION,
550                 _CS_GNU_LIBPTHREAD_VERSION,
551                 _CS_LFS_CFLAGS = 1000,
552                 _CS_LFS_LDFLAGS,
553                 _CS_LFS_LIBS,
554                 _CS_LFS_LINTFLAGS,
555                 _CS_LFS64_CFLAGS,
556                 _CS_LFS64_LDFLAGS,
557                 _CS_LFS64_LIBS,
558                 _CS_LFS64_LINTFLAGS,
559                 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
560                 _CS_XBS5_ILP32_OFF32_LDFLAGS,
561                 _CS_XBS5_ILP32_OFF32_LIBS,
562                 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
563                 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
564                 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
565                 _CS_XBS5_ILP32_OFFBIG_LIBS,
566                 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
567                 _CS_XBS5_LP64_OFF64_CFLAGS,
568                 _CS_XBS5_LP64_OFF64_LDFLAGS,
569                 _CS_XBS5_LP64_OFF64_LIBS,
570                 _CS_XBS5_LP64_OFF64_LINTFLAGS,
571                 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
572                 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
573                 _CS_XBS5_LPBIG_OFFBIG_LIBS,
574                 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
575                 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
576                 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
577                 _CS_POSIX_V6_ILP32_OFF32_LIBS,
578                 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
579                 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
580                 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
581                 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
582                 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
583                 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
584                 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
585                 _CS_POSIX_V6_LP64_OFF64_LIBS,
586                 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
587                 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
588                 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
589                 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
590                 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
591         }
592
593         [Map]
594         public enum LockFlags : int {
595                 F_ULOCK = 0, // Unlock a previously locked region.
596                 F_LOCK  = 1, // Lock a region for exclusive use.
597                 F_TLOCK = 2, // Test and lock a region for exclusive use.
598                 F_TEST  = 3, // Test a region for other process locks.
599         }
600
601         [Map][Flags]
602         public enum PollEvents : short {
603                 POLLIN      = 0x0001, // There is data to read
604                 POLLPRI     = 0x0002, // There is urgent data to read
605                 POLLOUT     = 0x0004, // Writing now will not block
606                 POLLERR     = 0x0008, // Error condition
607                 POLLHUP     = 0x0010, // Hung up
608                 POLLNVAL    = 0x0020, // Invalid request; fd not open
609                 // XPG4.2 definitions (via _XOPEN_SOURCE)
610                 POLLRDNORM  = 0x0040, // Normal data bay be read
611                 POLLRDBAND  = 0x0080, // Priority data may be read
612                 POLLWRNORM  = 0x0100, // Writing now will not block
613                 POLLWRBAND  = 0x0200, // Priority data may be written
614         }
615
616         #endregion
617
618         #region Structures
619
620         public struct Flock {
621                 public LockType         l_type;    // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
622                 public SeekFlags        l_whence;  // How to interpret l_start
623                 public /* off_t */ long l_start;   // Starting offset for lock
624                 public /* off_t */ long l_len;     // Number of bytes to lock
625                 public /* pid_t */ int  l_pid;     // PID of process blocking our lock (F_GETLK only)
626         }
627
628         [StructLayout(LayoutKind.Sequential)]
629         public struct Pollfd {
630                 public int fd;
631                 public PollEvents events;
632                 public PollEvents revents;
633         }
634
635         public struct Stat {
636                 public  /* dev_t */     ulong   st_dev;     // device
637                 public  /* ino_t */     ulong   st_ino;     // inode
638                 public  FilePermissions         st_mode;    // protection
639                 private uint                    _padding_;  // padding for structure alignment
640                 public  /* nlink_t */   ulong   st_nlink;   // number of hard links
641                 public  /* uid_t */     uint    st_uid;     // user ID of owner
642                 public  /* gid_t */     uint    st_gid;     // group ID of owner
643                 public  /* dev_t */     ulong   st_rdev;    // device type (if inode device)
644                 public  /* off_t */     long    st_size;    // total size, in bytes
645                 public  /* blksize_t */ long    st_blksize; // blocksize for filesystem I/O
646                 public  /* blkcnt_t */  long    st_blocks;  // number of blocks allocated
647                 public  /* time_t */    long    st_atime;   // time of last access
648                 public  /* time_t */    long    st_mtime;   // time of last modification
649                 public  /* time_t */    long    st_ctime;   // time of last status change
650         }
651
652         public struct Statvfs {
653                 public                  ulong f_bsize;    // file system block size
654                 public                  ulong f_frsize;   // fragment size
655                 public /* fsblkcnt_t */ ulong f_blocks;   // size of fs in f_frsize units
656                 public /* fsblkcnt_t */ ulong f_bfree;    // # free blocks
657                 public /* fsblkcnt_t */ ulong f_bavail;   // # free blocks for non-root
658                 public /* fsfilcnt_t */ ulong f_files;    // # inodes
659                 public /* fsfilcnt_t */ ulong f_ffree;    // # free inodes
660                 public /* fsfilcnt_t */ ulong f_favail;   // # free inodes for non-root
661                 public                  ulong f_fsid;     // file system id
662                 public                  ulong f_flag;     // mount flags
663                 public                  ulong f_namemax;  // maximum filename length
664         }
665
666         public struct Timeval {
667                 public  /* time_t */      long    tv_sec;   // seconds
668                 public  /* suseconds_t */ long    tv_usec;  // microseconds
669         }
670
671         public struct Timezone {
672                 public  int tz_minuteswest; // minutes W of Greenwich
673                 private int tz_dsttime;     // type of dst correction (OBSOLETE)
674         }
675
676         public struct Utimbuf {
677                 public  /* time_t */      long    actime;   // access time
678                 public  /* time_t */      long    modtime;  // modification time
679         }
680
681         #endregion
682
683         #region Classes
684
685         public sealed class Dirent
686         {
687                 public /* ino_t */ ulong  d_ino;
688                 public /* off_t */ long   d_off;
689                 public ushort             d_reclen;
690                 public byte               d_type;
691                 public string             d_name;
692
693                 public override int GetHashCode ()
694                 {
695                         return d_ino.GetHashCode () ^ d_off.GetHashCode () ^ 
696                                 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
697                                 d_name.GetHashCode ();
698                 }
699
700                 public override bool Equals (object obj)
701                 {
702                         if (obj == null || GetType() != obj.GetType())
703                                 return false;
704                         Dirent d = (Dirent) obj;
705                         return d.d_ino == d_ino && d.d_off == d_off &&
706                                 d.d_reclen == d_reclen && d.d_type == d_type &&
707                                 d.d_name == d_name;
708                 }
709
710                 public override string ToString ()
711                 {
712                         return d_name;
713                 }
714
715                 public static bool operator== (Dirent lhs, Dirent rhs)
716                 {
717                         return Object.Equals (lhs, rhs);
718                 }
719
720                 public static bool operator!= (Dirent lhs, Dirent rhs)
721                 {
722                         return !Object.Equals (lhs, rhs);
723                 }
724         }
725
726         public sealed class Fstab
727         {
728                 public string fs_spec;
729                 public string fs_file;
730                 public string fs_vfstype;
731                 public string fs_mntops;
732                 public string fs_type;
733                 public int    fs_freq;
734                 public int    fs_passno;
735
736                 public override int GetHashCode ()
737                 {
738                         return fs_spec.GetHashCode () ^ fs_file.GetHashCode () ^
739                                 fs_vfstype.GetHashCode () ^ fs_mntops.GetHashCode () ^
740                                 fs_type.GetHashCode () ^ fs_freq ^ fs_passno;
741                 }
742
743                 public override bool Equals (object obj)
744                 {
745                         if (obj == null || GetType() != obj.GetType())
746                                 return false;
747                         Fstab  f = (Fstab) obj;
748                         return f.fs_spec == fs_spec && f.fs_file == fs_file &&
749                                 f.fs_vfstype == fs_vfstype && f.fs_mntops == fs_mntops &&
750                                 f.fs_type == fs_type && f.fs_freq == fs_freq && 
751                                 f.fs_passno == fs_passno;
752                 }
753
754                 public override string ToString ()
755                 {
756                         return fs_spec;
757                 }
758
759                 public static bool operator== (Fstab lhs, Fstab rhs)
760                 {
761                         return Object.Equals (lhs, rhs);
762                 }
763
764                 public static bool operator!= (Fstab lhs, Fstab rhs)
765                 {
766                         return !Object.Equals (lhs, rhs);
767                 }
768         }
769
770         public sealed class Group
771         {
772                 public string           gr_name;
773                 public string           gr_passwd;
774                 public /* gid_t */ uint gr_gid;
775                 public string[]         gr_mem;
776
777                 public override int GetHashCode ()
778                 {
779                         int memhc = 0;
780                         for (int i = 0; i < gr_mem.Length; ++i)
781                                 memhc ^= gr_mem[i].GetHashCode ();
782
783                         return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^ 
784                                 gr_gid.GetHashCode () ^ memhc;
785                 }
786
787                 public override bool Equals (object obj)
788                 {
789                         if (obj == null || GetType() != obj.GetType())
790                                 return false;
791                         Group g = (Group) obj;
792                         if (g.gr_gid != gr_gid)
793                                 return false;
794                         if (g.gr_gid == gr_gid && g.gr_name == gr_name &&
795                                 g.gr_passwd == gr_passwd) {
796                                 if (g.gr_mem == gr_mem)
797                                         return true;
798                                 if (g.gr_mem == null || gr_mem == null)
799                                         return false;
800                                 if (g.gr_mem.Length != gr_mem.Length)
801                                         return false;
802                                 for (int i = 0; i < gr_mem.Length; ++i)
803                                         if (gr_mem[i] != g.gr_mem[i])
804                                                 return false;
805                                 return true;
806                         }
807                         return false;
808                 }
809
810                 // Generate string in /etc/group format
811                 public override string ToString ()
812                 {
813                         StringBuilder sb = new StringBuilder ();
814                         sb.AppendFormat ("{0}:{1}:{2}:", gr_name, gr_passwd, gr_gid);
815                         GetMembers (sb, gr_mem);
816                         return sb.ToString ();
817                 }
818
819                 private static void GetMembers (StringBuilder sb, string[] members)
820                 {
821                         if (members.Length > 0)
822                                 sb.Append (members[0]);
823                         for (int i = 1; i < members.Length; ++i) {
824                                 sb.Append (",");
825                                 sb.Append (members[i]);
826                         }
827                 }
828
829                 public static bool operator== (Group lhs, Group rhs)
830                 {
831                         return Object.Equals (lhs, rhs);
832                 }
833
834                 public static bool operator!= (Group lhs, Group rhs)
835                 {
836                         return !Object.Equals (lhs, rhs);
837                 }
838         }
839
840         public sealed class Passwd
841         {
842                 public string           pw_name;
843                 public string           pw_passwd;
844                 public /* uid_t */ uint pw_uid;
845                 public /* gid_t */ uint pw_gid;
846                 public string           pw_gecos;
847                 public string           pw_dir;
848                 public string           pw_shell;
849
850                 public override int GetHashCode ()
851                 {
852                         return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^ 
853                                 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
854                                 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
855                                 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
856                 }
857
858                 public override bool Equals (object obj)
859                 {
860                         if (obj == null || GetType() != obj.GetType())
861                                 return false;
862                         Passwd p = (Passwd) obj;
863                         return p.pw_uid == pw_uid && p.pw_gid == pw_gid && p.pw_name == pw_name && 
864                                 p.pw_passwd == pw_passwd && p.pw_gecos == pw_gecos && 
865                                 p.pw_dir == pw_dir && p.pw_shell == pw_shell;
866                 }
867
868                 // Generate string in /etc/passwd format
869                 public override string ToString ()
870                 {
871                         return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
872                                 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
873                 }
874
875                 public static bool operator== (Passwd lhs, Passwd rhs)
876                 {
877                         return Object.Equals (lhs, rhs);
878                 }
879
880                 public static bool operator!= (Passwd lhs, Passwd rhs)
881                 {
882                         return !Object.Equals (lhs, rhs);
883                 }
884         }
885
886         //
887         // Convention: Functions *not* part of the standard C library AND part of
888         // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
889         //
890         // For example, the man page should be similar to:
891         //
892         //    CONFORMING TO (or CONFORMS TO)
893         //           XPG2, SUSv2, POSIX, etc.
894         //
895         // BSD- and GNU-specific exports can also be placed here.
896         //
897         // The only methods in here should be:
898         //  (1) low-level functions
899         //  (2) "Trivial" function overloads.  For example, if the parameters to a
900         //      function are related (e.g. getgroups(2))
901         //  (3) The return type SHOULD NOT be changed.  If you want to provide a
902         //      convenience function with a nicer return type, place it into one of
903         //      the Unix* wrapper classes, and give it a .NET-styled name.
904         //  (4) Exceptions SHOULD NOT be thrown.  EXCEPTIONS: 
905         //      - If you're wrapping *broken* methods which make assumptions about 
906         //        input data, such as that an argument refers to N bytes of data.  
907         //        This is currently limited to cuserid(3) and encrypt(3).
908         //      - If you call functions which themselves generate exceptions.  
909         //        This is the case for using UnixConvert, which will throw an
910         //        exception if an invalid/unsupported value is used.
911         //
912         public sealed class Syscall : Stdlib
913         {
914                 new internal const string LIBC  = "libc";
915                     private  const string CRYPT = "crypt";
916
917                 private Syscall () {}
918
919                 //
920                 // <aio.h>
921                 //
922
923                 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3), 
924                 // aio_return(3), aio_suspend(3), aio_write(3)
925                 //
926                 // Then update UnixStream.BeginRead to use the aio* functions.
927
928                 #region <dirent.h> Declarations
929                 //
930                 // <dirent.h>
931                 //
932                 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
933
934                 [DllImport (LIBC, SetLastError=true)]
935                 public static extern IntPtr opendir (string name);
936
937                 [DllImport (LIBC, SetLastError=true)]
938                 public static extern int closedir (IntPtr dir);
939
940                 // seekdir(3):
941                 //    void seekdir (DIR *dir, off_t offset);
942                 //    Slight modification.  Returns -1 on error, 0 on success.
943                 [DllImport (MPH, SetLastError=true,
944                                 EntryPoint="Mono_Posix_Syscall_seekdir")]
945                 public static extern int seekdir (IntPtr dir, long offset);
946
947                 // telldir(3)
948                 //    off_t telldir(DIR *dir);
949                 [DllImport (MPH, SetLastError=true,
950                                 EntryPoint="Mono_Posix_Syscall_telldir")]
951                 public static extern long telldir (IntPtr dir);
952
953                 [DllImport (LIBC, SetLastError=true)]
954                 public static extern void rewinddir (IntPtr dir);
955
956                 private struct _Dirent {
957                         public /* ino_t */ ulong  d_ino;
958                         public /* off_t */ long   d_off;
959                         public ushort             d_reclen;
960                         public byte               d_type;
961                         public IntPtr             d_name;
962                 }
963
964                 private static void CopyDirent (Dirent to, ref _Dirent from)
965                 {
966                         try {
967                                 to.d_ino    = from.d_ino;
968                                 to.d_off    = from.d_off;
969                                 to.d_reclen = from.d_reclen;
970                                 to.d_type   = from.d_type;
971                                 to.d_name   = UnixMarshal.PtrToString (from.d_name);
972                         }
973                         finally {
974                                 Stdlib.free (from.d_name);
975                                 from.d_name = IntPtr.Zero;
976                         }
977                 }
978
979                 [DllImport (MPH, SetLastError=true,
980                                 EntryPoint="Mono_Posix_Syscall_readdir")]
981                 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
982
983                 public static Dirent readdir (IntPtr dir)
984                 {
985                         _Dirent dentry;
986                         int r = sys_readdir (dir, out dentry);
987                         if (r != 0)
988                                 return null;
989                         Dirent d = new Dirent ();
990                         CopyDirent (d, ref dentry);
991                         return d;
992                 }
993
994                 [DllImport (MPH, SetLastError=true,
995                                 EntryPoint="Mono_Posix_Syscall_readdir_r")]
996                 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
997
998                 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
999                 {
1000                         entry.d_ino    = 0;
1001                         entry.d_off    = 0;
1002                         entry.d_reclen = 0;
1003                         entry.d_type   = 0;
1004                         entry.d_name   = null;
1005
1006                         _Dirent _d;
1007                         int r = sys_readdir_r (dirp, out _d, out result);
1008
1009                         if (r == 0 && result != IntPtr.Zero) {
1010                                 CopyDirent (entry, ref _d);
1011                         }
1012
1013                         return r;
1014                 }
1015
1016                 [DllImport (LIBC, SetLastError=true)]
1017                 public static extern int dirfd (IntPtr dir);
1018                 #endregion
1019
1020                 #region <fcntl.h> Declarations
1021                 //
1022                 // <fcntl.h> -- COMPLETE
1023                 //
1024
1025                 [DllImport (MPH, SetLastError=true, 
1026                                 EntryPoint="Mono_Posix_Syscall_fcntl")]
1027                 public static extern int fcntl (int fd, FcntlCommand cmd);
1028
1029                 [DllImport (MPH, SetLastError=true, 
1030                                 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
1031                 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
1032
1033                 [DllImport (MPH, SetLastError=true, 
1034                                 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
1035                 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
1036
1037                 [DllImport (MPH, SetLastError=true, 
1038                                 EntryPoint="Mono_Posix_Syscall_open")]
1039                 public static extern int open (string pathname, OpenFlags flags);
1040
1041                 // open(2)
1042                 //    int open(const char *pathname, int flags, mode_t mode);
1043                 [DllImport (MPH, SetLastError=true, 
1044                                 EntryPoint="Mono_Posix_Syscall_open_mode")]
1045                 public static extern int open (string pathname, OpenFlags flags, FilePermissions mode);
1046
1047                 // creat(2)
1048                 //    int creat(const char *pathname, mode_t mode);
1049                 [DllImport (MPH, SetLastError=true, 
1050                                 EntryPoint="Mono_Posix_Syscall_creat")]
1051                 public static extern int creat (string pathname, FilePermissions mode);
1052
1053                 // posix_fadvise(2)
1054                 //    int posix_fadvise(int fd, off_t offset, off_t len, int advice);
1055                 [DllImport (MPH, SetLastError=true, 
1056                                 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
1057                 public static extern int posix_fadvise (int fd, long offset, 
1058                         long len, PosixFadviseAdvice advice);
1059
1060                 // posix_fallocate(P)
1061                 //    int posix_fallocate(int fd, off_t offset, size_t len);
1062                 [DllImport (MPH, SetLastError=true, 
1063                                 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
1064                 public static extern int posix_fallocate (int fd, long offset, long len);
1065                 #endregion
1066
1067                 #region <fstab.h> Declarations
1068                 //
1069                 // <fstab.h>  -- COMPLETE
1070                 //
1071                 private struct _Fstab {
1072                         public IntPtr fs_spec;
1073                         public IntPtr fs_file;
1074                         public IntPtr fs_vfstype;
1075                         public IntPtr fs_mntops;
1076                         public IntPtr fs_type;
1077                         public int    fs_freq;
1078                         public int    fs_passno;
1079                         public IntPtr _fs_buf_;
1080                 }
1081
1082                 private static void CopyFstab (Fstab to, ref _Fstab from)
1083                 {
1084                         try {
1085                                 to.fs_spec     = UnixMarshal.PtrToString (from.fs_spec);
1086                                 to.fs_file     = UnixMarshal.PtrToString (from.fs_file);
1087                                 to.fs_vfstype  = UnixMarshal.PtrToString (from.fs_vfstype);
1088                                 to.fs_mntops   = UnixMarshal.PtrToString (from.fs_mntops);
1089                                 to.fs_type     = UnixMarshal.PtrToString (from.fs_type);
1090                                 to.fs_freq     = from.fs_freq;
1091                                 to.fs_passno   = from.fs_passno;
1092                         }
1093                         finally {
1094                                 Stdlib.free (from._fs_buf_);
1095                                 from._fs_buf_ = IntPtr.Zero;
1096                         }
1097                 }
1098
1099                 [DllImport (MPH, SetLastError=true,
1100                                 EntryPoint="Mono_Posix_Syscall_endfsent")]
1101                 public static extern void endfsent ();
1102
1103                 [DllImport (MPH, SetLastError=true,
1104                                 EntryPoint="Mono_Posix_Syscall_getfsent")]
1105                 private static extern int sys_getfsent (out _Fstab fs);
1106
1107                 public static Fstab getfsent ()
1108                 {
1109                         _Fstab fsbuf;
1110                         int r = sys_getfsent (out fsbuf);
1111                         if (r != 0)
1112                                 return null;
1113                         Fstab fs = new Fstab ();
1114                         CopyFstab (fs, ref fsbuf);
1115                         return fs;
1116                 }
1117
1118                 [DllImport (MPH, SetLastError=true,
1119                                 EntryPoint="Mono_Posix_Syscall_getfsfile")]
1120                 private static extern int sys_getfsfile (string mount_point, out _Fstab fs);
1121
1122                 public static Fstab getfsfile (string mount_point)
1123                 {
1124                         _Fstab fsbuf;
1125                         int r = sys_getfsfile (mount_point, out fsbuf);
1126                         if (r != 0)
1127                                 return null;
1128                         Fstab fs = new Fstab ();
1129                         CopyFstab (fs, ref fsbuf);
1130                         return fs;
1131                 }
1132
1133                 [DllImport (MPH, SetLastError=true,
1134                                 EntryPoint="Mono_Posix_Syscall_getfsspec")]
1135                 private static extern int sys_getfsspec (string special_file, out _Fstab fs);
1136
1137                 public static Fstab getfsspec (string special_file)
1138                 {
1139                         _Fstab fsbuf;
1140                         int r = sys_getfsspec (special_file, out fsbuf);
1141                         if (r != 0)
1142                                 return null;
1143                         Fstab fs = new Fstab ();
1144                         CopyFstab (fs, ref fsbuf);
1145                         return fs;
1146                 }
1147
1148                 [DllImport (MPH, SetLastError=true,
1149                                 EntryPoint="Mono_Posix_Syscall_setfsent")]
1150                 public static extern int setfsent ();
1151
1152                 #endregion
1153
1154                 #region <grp.h> Declarations
1155                 //
1156                 // <grp.h>
1157                 //
1158                 // TODO: putgrent(3), fgetgrent_r(), getgrouplist(2), initgroups(3)
1159
1160                 // setgroups(2)
1161                 //    int setgroups (size_t size, const gid_t *list);
1162                 [DllImport (MPH, SetLastError=true, 
1163                                 EntryPoint="Mono_Posix_Syscall_setgroups")]
1164                 public static extern int setgroups (ulong size, uint[] list);
1165
1166                 public static int setgroups (uint [] list)
1167                 {
1168                         return setgroups ((ulong) list.Length, list);
1169                 }
1170
1171                 private struct _Group
1172                 {
1173                         public IntPtr           gr_name;
1174                         public IntPtr           gr_passwd;
1175                         public /* gid_t */ uint gr_gid;
1176                         public int              _gr_nmem_;
1177                         public IntPtr           gr_mem;
1178                         public IntPtr           _gr_buf_;
1179                 }
1180
1181                 private static void CopyGroup (Group to, ref _Group from)
1182                 {
1183                         try {
1184                                 to.gr_gid    = from.gr_gid;
1185                                 to.gr_name   = UnixMarshal.PtrToString (from.gr_name);
1186                                 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
1187                                 to.gr_mem    = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
1188                         }
1189                         finally {
1190                                 Stdlib.free (from.gr_mem);
1191                                 Stdlib.free (from._gr_buf_);
1192                                 from.gr_mem   = IntPtr.Zero;
1193                                 from._gr_buf_ = IntPtr.Zero;
1194                         }
1195                 }
1196
1197                 [DllImport (MPH, SetLastError=true,
1198                                 EntryPoint="Mono_Posix_Syscall_getgrnam")]
1199                 private static extern int sys_getgrnam (string name, out _Group group);
1200
1201                 public static Group getgrnam (string name)
1202                 {
1203                         _Group group;
1204                         int r = sys_getgrnam (name, out group);
1205                         if (r != 0)
1206                                 return null;
1207                         Group gr = new Group ();
1208                         CopyGroup (gr, ref group);
1209                         return gr;
1210                 }
1211
1212                 // getgrgid(3)
1213                 //    struct group *getgrgid(gid_t gid);
1214                 [DllImport (MPH, SetLastError=true,
1215                                 EntryPoint="Mono_Posix_Syscall_getgrgid")]
1216                 private static extern int sys_getgrgid (uint uid, out _Group group);
1217
1218                 public static Group getgrgid (uint uid)
1219                 {
1220                         _Group group;
1221                         int r = sys_getgrgid (uid, out group);
1222                         if (r != 0)
1223                                 return null;
1224                         Group gr = new Group ();
1225                         CopyGroup (gr, ref group);
1226                         return gr;
1227                 }
1228
1229                 [DllImport (MPH, SetLastError=true,
1230                                 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
1231                 private static extern int sys_getgrnam_r (string name, out _Group grbuf, out IntPtr grbufp);
1232
1233                 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
1234                 {
1235                         grbufp = null;
1236                         _Group group;
1237                         IntPtr _grbufp;
1238                         int r = sys_getgrnam_r (name, out group, out _grbufp);
1239                         if (r == 0 && _grbufp != IntPtr.Zero) {
1240                                 CopyGroup (grbuf, ref group);
1241                                 grbufp = grbuf;
1242                         }
1243                         return r;
1244                 }
1245
1246                 // getgrgid_r(3)
1247                 //    int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
1248                 //        size_t buflen, struct group **gbufp);
1249                 [DllImport (MPH, SetLastError=true,
1250                                 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
1251                 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
1252
1253                 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
1254                 {
1255                         grbufp = null;
1256                         _Group group;
1257                         IntPtr _grbufp;
1258                         int r = sys_getgrgid_r (uid, out group, out _grbufp);
1259                         if (r == 0 && _grbufp != IntPtr.Zero) {
1260                                 CopyGroup (grbuf, ref group);
1261                                 grbufp = grbuf;
1262                         }
1263                         return r;
1264                 }
1265
1266                 [DllImport (MPH, SetLastError=true,
1267                                 EntryPoint="Mono_Posix_Syscall_getgrent")]
1268                 private static extern int sys_getgrent (out _Group grbuf);
1269
1270                 public static Group getgrent ()
1271                 {
1272                         _Group group;
1273                         int r = sys_getgrent (out group);
1274                         if (r != 0)
1275                                 return null;
1276                         Group gr = new Group();
1277                         CopyGroup (gr, ref group);
1278                         return gr;
1279                 }
1280
1281                 [DllImport (LIBC, SetLastError=true)]
1282                 public static extern void setgrent ();
1283
1284                 [DllImport (LIBC, SetLastError=true)]
1285                 public static extern void endgrent ();
1286
1287                 [DllImport (MPH, SetLastError=true,
1288                                 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
1289                 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
1290
1291                 public static Group fgetgrent (IntPtr stream)
1292                 {
1293                         _Group group;
1294                         int r = sys_fgetgrent (stream, out group);
1295                         if (r != 0)
1296                                 return null;
1297                         Group gr = new Group ();
1298                         CopyGroup (gr, ref group);
1299                         return gr;
1300                 }
1301                 #endregion
1302
1303                 #region <pwd.h> Declarations
1304                 //
1305                 // <pwd.h>
1306                 //
1307                 // TODO: putpwent(3), fgetpwent_r()
1308                 //
1309                 // SKIPPING: getpw(3): it's dangerous.  Use getpwuid(3) instead.
1310
1311                 private struct _Passwd
1312                 {
1313                         public IntPtr           pw_name;
1314                         public IntPtr           pw_passwd;
1315                         public /* uid_t */ uint pw_uid;
1316                         public /* gid_t */ uint pw_gid;
1317                         public IntPtr           pw_gecos;
1318                         public IntPtr           pw_dir;
1319                         public IntPtr           pw_shell;
1320                         public IntPtr           _pw_buf_;
1321                 }
1322
1323                 private static void CopyPasswd (Passwd to, ref _Passwd from)
1324                 {
1325                         try {
1326                                 to.pw_name   = UnixMarshal.PtrToString (from.pw_name);
1327                                 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
1328                                 to.pw_uid    = from.pw_uid;
1329                                 to.pw_gid    = from.pw_gid;
1330                                 to.pw_gecos  = UnixMarshal.PtrToString (from.pw_gecos);
1331                                 to.pw_dir    = UnixMarshal.PtrToString (from.pw_dir);
1332                                 to.pw_shell  = UnixMarshal.PtrToString (from.pw_shell);
1333                         }
1334                         finally {
1335                                 Stdlib.free (from._pw_buf_);
1336                                 from._pw_buf_ = IntPtr.Zero;
1337                         }
1338                 }
1339
1340                 [DllImport (MPH, SetLastError=true,
1341                                 EntryPoint="Mono_Posix_Syscall_getpwnam")]
1342                 private static extern int sys_getpwnam (string name, out _Passwd passwd);
1343
1344                 public static Passwd getpwnam (string name)
1345                 {
1346                         _Passwd passwd;
1347                         int r = sys_getpwnam (name, out passwd);
1348                         if (r != 0)
1349                                 return null;
1350                         Passwd pw = new Passwd ();
1351                         CopyPasswd (pw, ref passwd);
1352                         return pw;
1353                 }
1354
1355                 // getpwuid(3)
1356                 //    struct passwd *getpwnuid(uid_t uid);
1357                 [DllImport (MPH, SetLastError=true,
1358                                 EntryPoint="Mono_Posix_Syscall_getpwuid")]
1359                 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
1360
1361                 public static Passwd getpwuid (uint uid)
1362                 {
1363                         _Passwd passwd;
1364                         int r = sys_getpwuid (uid, out passwd);
1365                         if (r != 0)
1366                                 return null;
1367                         Passwd pw = new Passwd ();
1368                         CopyPasswd (pw, ref passwd);
1369                         return pw;
1370                 }
1371
1372                 [DllImport (MPH, SetLastError=true,
1373                                 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
1374                 private static extern int sys_getpwnam_r (string name, out _Passwd pwbuf, out IntPtr pwbufp);
1375
1376                 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
1377                 {
1378                         pwbufp = null;
1379                         _Passwd passwd;
1380                         IntPtr _pwbufp;
1381                         int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
1382                         if (r == 0 && _pwbufp != IntPtr.Zero) {
1383                                 CopyPasswd (pwbuf, ref passwd);
1384                                 pwbufp = pwbuf;
1385                         }
1386                         return r;
1387                 }
1388
1389                 // getpwuid_r(3)
1390                 //    int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
1391                 //        buflen, struct passwd **pwbufp);
1392                 [DllImport (MPH, SetLastError=true,
1393                                 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
1394                 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
1395
1396                 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
1397                 {
1398                         pwbufp = null;
1399                         _Passwd passwd;
1400                         IntPtr _pwbufp;
1401                         int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
1402                         if (r == 0 && _pwbufp != IntPtr.Zero) {
1403                                 CopyPasswd (pwbuf, ref passwd);
1404                                 pwbufp = pwbuf;
1405                         }
1406                         return r;
1407                 }
1408
1409                 [DllImport (MPH, SetLastError=true,
1410                                 EntryPoint="Mono_Posix_Syscall_getpwent")]
1411                 private static extern int sys_getpwent (out _Passwd pwbuf);
1412
1413                 public static Passwd getpwent ()
1414                 {
1415                         _Passwd passwd;
1416                         int r = sys_getpwent (out passwd);
1417                         if (r != 0)
1418                                 return null;
1419                         Passwd pw = new Passwd ();
1420                         CopyPasswd (pw, ref passwd);
1421                         return pw;
1422                 }
1423
1424                 [DllImport (LIBC, SetLastError=true)]
1425                 public static extern void setpwent ();
1426
1427                 [DllImport (LIBC, SetLastError=true)]
1428                 public static extern void endpwent ();
1429
1430                 [DllImport (MPH, SetLastError=true,
1431                                 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
1432                 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
1433
1434                 public static Passwd fgetpwent (IntPtr stream)
1435                 {
1436                         _Passwd passwd;
1437                         int r = sys_fgetpwent (stream, out passwd);
1438                         if (r != 0)
1439                                 return null;
1440                         Passwd pw = new Passwd ();
1441                         CopyPasswd (pw, ref passwd);
1442                         return pw;
1443                 }
1444                 #endregion
1445
1446                 #region <signal.h> Declarations
1447                 //
1448                 // <signal.h>
1449                 //
1450                 [DllImport (LIBC, SetLastError=true)]
1451                 private static extern void psignal (int sig, string s);
1452
1453                 public static void psignal (Signum sig, string s)
1454                 {
1455                         int signum = UnixConvert.FromSignum (sig);
1456                         psignal (signum, s);
1457                 }
1458
1459                 // kill(2)
1460                 //    int kill(pid_t pid, int sig);
1461                 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
1462                 private static extern int sys_kill (int pid, int sig);
1463
1464                 public static int kill (int pid, Signum sig)
1465                 {
1466                         int _sig = UnixConvert.FromSignum (sig);
1467                         return sys_kill (pid, _sig);
1468                 }
1469
1470                 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
1471                 private static extern IntPtr sys_strsignal (int sig);
1472
1473                 public static string strsignal (Signum sig)
1474                 {
1475                         int s = UnixConvert.FromSignum (sig);
1476                         IntPtr r = sys_strsignal (s);
1477                         return UnixMarshal.PtrToString (r);
1478                 }
1479
1480                 // TODO: sigaction(2)
1481                 // TODO: sigsuspend(2)
1482                 // TODO: sigpending(2)
1483
1484                 #endregion
1485
1486                 #region <stdio.h> Declarations
1487                 //
1488                 // <stdio.h>
1489                 //
1490                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
1491                 private static extern int _L_ctermid ();
1492
1493                 public static readonly int L_ctermid = _L_ctermid ();
1494
1495                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
1496                 private static extern int _L_cuserid ();
1497
1498                 public static readonly int L_cuserid = _L_cuserid ();
1499
1500                 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
1501                 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
1502
1503                 [Obsolete ("\"Nobody knows precisely what cuserid() does... " + 
1504                                 "DO NOT USE cuserid().\n" +
1505                                 "`string' must hold L_cuserid characters.  Use getlogin_r instead.")]
1506                 public static string cuserid (StringBuilder @string)
1507                 {
1508                         if (@string.Capacity < L_cuserid) {
1509                                 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
1510                         }
1511                         IntPtr r = sys_cuserid (@string);
1512                         return UnixMarshal.PtrToString (r);
1513                 }
1514
1515                 #endregion
1516
1517                 #region <stdlib.h> Declarations
1518                 //
1519                 // <stdlib.h>
1520                 //
1521                 [DllImport (LIBC, SetLastError=true)]
1522                 public static extern int ttyslot ();
1523
1524                 [DllImport (CRYPT, SetLastError=true)]
1525                 public static extern void setkey (string key);
1526
1527                 #endregion
1528
1529                 #region <string.h> Declarations
1530                 //
1531                 // <string.h>
1532                 //
1533
1534                 // strerror_r(3)
1535                 //    int strerror_r(int errnum, char *buf, size_t n);
1536                 [DllImport (MPH, SetLastError=true, 
1537                                 EntryPoint="Mono_Posix_Syscall_strerror_r")]
1538                 private static extern int sys_strerror_r (int errnum, 
1539                                 [Out] StringBuilder buf, ulong n);
1540
1541                 public static int strerror_r (Error errnum, StringBuilder buf, ulong n)
1542                 {
1543                         int e = UnixConvert.FromError (errnum);
1544                         return sys_strerror_r (e, buf, n);
1545                 }
1546
1547                 public static int strerror_r (Error errnum, StringBuilder buf)
1548                 {
1549                         return strerror_r (errnum, buf, (ulong) buf.Capacity);
1550                 }
1551
1552                 #endregion
1553
1554                 #region <sys/mman.h> Declarations
1555                 //
1556                 // <sys/mman.h>
1557                 //
1558
1559                 // posix_madvise(P)
1560                 //    int posix_madvise(void *addr, size_t len, int advice);
1561                 [DllImport (MPH, SetLastError=true, 
1562                                 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
1563                 public static extern int posix_madvise (IntPtr addr, ulong len, 
1564                         PosixMadviseAdvice advice);
1565
1566                 #endregion
1567
1568                 #region <sys/poll.h> Declarations
1569                 //
1570                 // <sys/poll.h> -- COMPLETE
1571                 //
1572
1573                 private struct _pollfd {
1574                         public int fd;
1575                         public short events;
1576                         public short revents;
1577                 }
1578
1579                 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
1580                 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
1581
1582                 public static int poll (Pollfd [] fds, uint nfds, int timeout)
1583                 {
1584                         if (fds.Length < nfds)
1585                                 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
1586
1587                         _pollfd[] send = new _pollfd[nfds];
1588
1589                         for (int i = 0; i < send.Length; i++) {
1590                                 send [i].fd     = fds [i].fd;
1591                                 send [i].events = UnixConvert.FromPollEvents (fds [i].events);
1592                         }
1593
1594                         int r = sys_poll (send, nfds, timeout);
1595
1596                         for (int i = 0; i < send.Length; i++) {
1597                                 fds [i].revents = UnixConvert.ToPollEvents (send [i].revents);
1598                         }
1599
1600                         return r;
1601                 }
1602
1603                 public static int poll (Pollfd [] fds, int timeout)
1604                 {
1605                         return poll (fds, (uint) fds.Length, timeout);
1606                 }
1607
1608                 //
1609                 // <sys/ptrace.h>
1610                 //
1611
1612                 // TODO: ptrace(2)
1613
1614                 //
1615                 // <sys/resource.h>
1616                 //
1617
1618                 // TODO: setrlimit(2)
1619                 // TODO: getrlimit(2)
1620                 // TODO: getrusage(2)
1621
1622                 #endregion
1623
1624                 #region <sys/sendfile.h> Declarations
1625                 //
1626                 // <sys/sendfile.h> -- COMPLETE
1627                 //
1628
1629                 [DllImport (MPH, SetLastError=true,
1630                                 EntryPoint="Mono_Posix_Syscall_sendfile")]
1631                 public static extern long sendfile (int out_fd, int in_fd, 
1632                                 ref long offset, ulong count);
1633
1634                 #endregion
1635
1636                 #region <sys/stat.h> Declarations
1637                 //
1638                 // <sys/stat.h>  -- COMPLETE
1639                 //
1640                 [DllImport (MPH, SetLastError=true, 
1641                                 EntryPoint="Mono_Posix_Syscall_stat")]
1642                 public static extern int stat (string file_name, out Stat buf);
1643
1644                 [DllImport (MPH, SetLastError=true, 
1645                                 EntryPoint="Mono_Posix_Syscall_fstat")]
1646                 public static extern int fstat (int filedes, out Stat buf);
1647
1648                 [DllImport (MPH, SetLastError=true, 
1649                                 EntryPoint="Mono_Posix_Syscall_lstat")]
1650                 public static extern int lstat (string file_name, out Stat buf);
1651
1652                 // TODO:
1653                 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
1654                 // All take FilePermissions
1655
1656                 // chmod(2)
1657                 //    int chmod(const char *path, mode_t mode);
1658                 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
1659                 private static extern int sys_chmod (string path, uint mode);
1660
1661                 public static int chmod (string path, FilePermissions mode)
1662                 {
1663                         uint _mode = UnixConvert.FromFilePermissions (mode);
1664                         return sys_chmod (path, _mode);
1665                 }
1666
1667                 // fchmod(2)
1668                 //    int chmod(int filedes, mode_t mode);
1669                 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
1670                 private static extern int sys_fchmod (int filedes, uint mode);
1671
1672                 public static int fchmod (int filedes, FilePermissions mode)
1673                 {
1674                         uint _mode = UnixConvert.FromFilePermissions (mode);
1675                         return sys_fchmod (filedes, _mode);
1676                 }
1677
1678                 // umask(2)
1679                 //    mode_t umask(mode_t mask);
1680                 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
1681                 private static extern uint sys_umask (uint mask);
1682
1683                 public static FilePermissions umask (FilePermissions mask)
1684                 {
1685                         uint _mask = UnixConvert.FromFilePermissions (mask);
1686                         uint r = sys_umask (_mask);
1687                         return UnixConvert.ToFilePermissions (r);
1688                 }
1689
1690                 // mkdir(2)
1691                 //    int mkdir(const char *pathname, mode_t mode);
1692                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
1693                 private static extern int sys_mkdir (string oldpath, uint mode);
1694
1695                 public static int mkdir (string oldpath, FilePermissions mode)
1696                 {
1697                         uint _mode = UnixConvert.FromFilePermissions (mode);
1698                         return sys_mkdir (oldpath, _mode);
1699                 }
1700
1701                 // mknod(2)
1702                 //    int mknod (const char *pathname, mode_t mode, dev_t dev);
1703                 [DllImport (MPH, SetLastError=true,
1704                                 EntryPoint="Mono_Posix_Syscall_mknod")]
1705                 public static extern int mknod (string pathname, FilePermissions mode, ulong dev);
1706
1707                 // mkfifo(3)
1708                 //    int mkfifo(const char *pathname, mode_t mode);
1709                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
1710                 private static extern int sys_mkfifo (string pathname, uint mode);
1711
1712                 public static int mkfifo (string pathname, FilePermissions mode)
1713                 {
1714                         uint _mode = UnixConvert.FromFilePermissions (mode);
1715                         return sys_mkfifo (pathname, _mode);
1716                 }
1717
1718                 #endregion
1719
1720                 #region <sys/stat.h> Declarations
1721                 //
1722                 // <sys/statvfs.h>
1723                 //
1724
1725                 [DllImport (MPH, SetLastError=true,
1726                                 EntryPoint="Mono_Posix_Syscall_statvfs")]
1727                 public static extern int statvfs (string path, out Statvfs buf);
1728
1729                 [DllImport (MPH, SetLastError=true,
1730                                 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
1731                 public static extern int fstatvfs (int fd, out Statvfs buf);
1732
1733                 #endregion
1734
1735                 #region <sys/time.h> Declarations
1736                 //
1737                 // <sys/time.h>
1738                 //
1739                 // TODO: adjtime(), getitimer(2), setitimer(2), lutimes(), futimes()
1740
1741                 [DllImport (MPH, SetLastError=true, 
1742                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1743                 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
1744
1745                 [DllImport (MPH, SetLastError=true,
1746                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1747                 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
1748
1749                 public static int gettimeofday (out Timeval tv)
1750                 {
1751                         return gettimeofday (out tv, IntPtr.Zero);
1752                 }
1753
1754                 [DllImport (MPH, SetLastError=true,
1755                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1756                 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
1757
1758                 public static int gettimeofday (out Timezone tz)
1759                 {
1760                         return gettimeofday (IntPtr.Zero, out tz);
1761                 }
1762
1763                 [DllImport (MPH, SetLastError=true,
1764                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1765                 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
1766
1767                 [DllImport (MPH, SetLastError=true,
1768                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1769                 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
1770
1771                 public static int settimeofday (ref Timeval tv)
1772                 {
1773                         return settimeofday (ref tv, IntPtr.Zero);
1774                 }
1775
1776                 [DllImport (MPH, SetLastError=true, 
1777                                 EntryPoint="Mono_Posix_Syscall_utimes")]
1778                 public static extern int utimes (string filename, ref Timeval tvp);
1779
1780                 #endregion
1781
1782                 //
1783                 // <sys/timeb.h>
1784                 //
1785
1786                 // TODO: ftime(3)
1787
1788                 //
1789                 // <sys/times.h>
1790                 //
1791
1792                 // TODO: times(2)
1793
1794                 #region <sys/wait.h> Declarations
1795                 //
1796                 // <sys/wait.h>
1797                 //
1798
1799                 // wait(2)
1800                 //    pid_t wait(int *status);
1801                 [DllImport (LIBC, SetLastError=true)]
1802                 public static extern int wait (out int status);
1803
1804                 // waitpid(2)
1805                 //    pid_t waitpid(pid_t pid, int *status, int options);
1806                 [DllImport (LIBC, SetLastError=true)]
1807                 private static extern int waitpid (int pid, out int status, int options);
1808
1809                 public static int waitpid (int pid, out int status, WaitOptions options)
1810                 {
1811                         int _options = UnixConvert.FromWaitOptions (options);
1812                         return waitpid (pid, out status, _options);
1813                 }
1814
1815                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
1816                 private static extern int _WIFEXITED (int status);
1817
1818                 public static bool WIFEXITED (int status)
1819                 {
1820                         return _WIFEXITED (status) != 0;
1821                 }
1822
1823                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
1824                 public static extern int WEXITSTATUS (int status);
1825
1826                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
1827                 private static extern int _WIFSIGNALED (int status);
1828
1829                 public static bool WIFSIGNALED (int status)
1830                 {
1831                         return _WIFSIGNALED (status) != 0;
1832                 }
1833
1834                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
1835                 private static extern int _WTERMSIG (int status);
1836
1837                 public static Signum WTERMSIG (int status)
1838                 {
1839                         int r = _WTERMSIG (status);
1840                         return UnixConvert.ToSignum (r);
1841                 }
1842
1843                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
1844                 private static extern int _WIFSTOPPED (int status);
1845
1846                 public static bool WIFSTOPPED (int status)
1847                 {
1848                         return _WIFSTOPPED (status) != 0;
1849                 }
1850
1851                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
1852                 private static extern int _WSTOPSIG (int status);
1853
1854                 public static Signum WSTOPSIG (int status)
1855                 {
1856                         int r = _WSTOPSIG (status);
1857                         return UnixConvert.ToSignum (r);
1858                 }
1859
1860                 //
1861                 // <termios.h>
1862                 //
1863
1864                 #endregion
1865
1866                 #region <syslog.h> Declarations
1867                 //
1868                 // <syslog.h>
1869                 //
1870
1871                 [DllImport (LIBC, EntryPoint="openlog")]
1872                 private static extern void sys_openlog (IntPtr ident, int option, int facility);
1873
1874                 public static void openlog (IntPtr ident, SyslogOptions option, 
1875                                 SyslogFacility defaultFacility)
1876                 {
1877                         int _option   = UnixConvert.FromSyslogOptions (option);
1878                         int _facility = UnixConvert.FromSyslogFacility (defaultFacility);
1879
1880                         sys_openlog (ident, _option, _facility);
1881                 }
1882
1883                 [DllImport (LIBC)]
1884                 private static extern void sys_syslog (int priority, string message);
1885
1886                 public static void syslog (SyslogFacility facility, SyslogLevel level, string message)
1887                 {
1888                         int _facility = UnixConvert.FromSyslogFacility (facility);
1889                         int _level = UnixConvert.FromSyslogLevel (level);
1890                         sys_syslog (_facility | _level, GetSyslogMessage (message));
1891                 }
1892
1893                 public static void syslog (SyslogLevel level, string message)
1894                 {
1895                         int _level = UnixConvert.FromSyslogLevel (level);
1896                         sys_syslog (_level, GetSyslogMessage (message));
1897                 }
1898
1899                 private static string GetSyslogMessage (string message)
1900                 {
1901                         return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
1902                 }
1903
1904                 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
1905                                 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
1906                 public static void syslog (SyslogFacility facility, SyslogLevel level, 
1907                                 string format, params object[] parameters)
1908                 {
1909                         int _facility = UnixConvert.FromSyslogFacility (facility);
1910                         int _level = UnixConvert.FromSyslogLevel (level);
1911
1912                         object[] _parameters = new object[checked(parameters.Length+2)];
1913                         _parameters [0] = _facility | _level;
1914                         _parameters [1] = format;
1915                         Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
1916                         XPrintfFunctions.syslog (_parameters);
1917                 }
1918
1919                 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
1920                                 "Use syslog(SyslogLevel, string) instead.")]
1921                 public static void syslog (SyslogLevel level, string format, 
1922                                 params object[] parameters)
1923                 {
1924                         int _level = UnixConvert.FromSyslogLevel (level);
1925
1926                         object[] _parameters = new object[checked(parameters.Length+2)];
1927                         _parameters [0] = _level;
1928                         _parameters [1] = format;
1929                         Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
1930                         XPrintfFunctions.syslog (_parameters);
1931                 }
1932
1933                 [DllImport (LIBC)]
1934                 public static extern void closelog ();
1935
1936                 [DllImport (LIBC, EntryPoint="setlogmask")]
1937                 private static extern int sys_setlogmask (int mask);
1938
1939                 public static int setlogmask (SyslogLevel mask)
1940                 {
1941                         int _mask = UnixConvert.FromSyslogLevel (mask);
1942                         return sys_setlogmask (_mask);
1943                 }
1944
1945                 #endregion
1946
1947                 #region <time.h> Declarations
1948
1949                 //
1950                 // <time.h>
1951                 //
1952
1953                 // stime(2)
1954                 //    int stime(time_t *t);
1955                 [DllImport (MPH, SetLastError=true,
1956                                 EntryPoint="Mono_Posix_Syscall_stime")]
1957                 public static extern int stime (ref long t);
1958
1959                 // time(2)
1960                 //    time_t time(time_t *t);
1961                 [DllImport (MPH, SetLastError=true,
1962                                 EntryPoint="Mono_Posix_Syscall_time")]
1963                 public static extern long time (out long t);
1964
1965                 //
1966                 // <ulimit.h>
1967                 //
1968
1969                 // TODO: ulimit(3)
1970
1971                 #endregion
1972
1973                 #region <unistd.h> Declarations
1974                 //
1975                 // <unistd.h>
1976                 //
1977                 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
1978                 //       other TODOs listed below.
1979
1980                 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
1981                 private static extern int sys_access (string pathname, int mode);
1982
1983                 public static int access (string pathname, AccessMode mode)
1984                 {
1985                         int _mode = UnixConvert.FromAccessMode (mode);
1986                         return sys_access (pathname, _mode);
1987                 }
1988
1989                 // lseek(2)
1990                 //    off_t lseek(int filedes, off_t offset, int whence);
1991                 [DllImport (MPH, SetLastError=true, 
1992                                 EntryPoint="Mono_Posix_Syscall_lseek")]
1993                 public static extern long lseek (int fd, long offset, SeekFlags whence);
1994
1995     [DllImport (LIBC, SetLastError=true)]
1996                 public static extern int close (int fd);
1997
1998                 // read(2)
1999                 //    ssize_t read(int fd, void *buf, size_t count);
2000                 [DllImport (MPH, SetLastError=true, 
2001                                 EntryPoint="Mono_Posix_Syscall_read")]
2002                 public static extern long read (int fd, IntPtr buf, ulong count);
2003
2004                 public static unsafe long read (int fd, void *buf, ulong count)
2005                 {
2006                         return read (fd, (IntPtr) buf, count);
2007                 }
2008
2009                 // write(2)
2010                 //    ssize_t write(int fd, const void *buf, size_t count);
2011                 [DllImport (MPH, SetLastError=true, 
2012                                 EntryPoint="Mono_Posix_Syscall_write")]
2013                 public static extern long write (int fd, IntPtr buf, ulong count);
2014
2015                 public static unsafe long write (int fd, void *buf, ulong count)
2016                 {
2017                         return write (fd, (IntPtr) buf, count);
2018                 }
2019
2020                 // pread(2)
2021                 //    ssize_t pread(int fd, void *buf, size_t count, off_t offset);
2022                 [DllImport (MPH, SetLastError=true, 
2023                                 EntryPoint="Mono_Posix_Syscall_pread")]
2024                 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
2025
2026                 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
2027                 {
2028                         return pread (fd, (IntPtr) buf, count, offset);
2029                 }
2030
2031                 // pwrite(2)
2032                 //    ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
2033                 [DllImport (MPH, SetLastError=true, 
2034                                 EntryPoint="Mono_Posix_Syscall_pwrite")]
2035                 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
2036
2037                 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
2038                 {
2039                         return pwrite (fd, (IntPtr) buf, count, offset);
2040                 }
2041
2042                 [DllImport (MPH, SetLastError=true, 
2043                                 EntryPoint="Mono_Posix_Syscall_pipe")]
2044                 public static extern int pipe (out int reading, out int writing);
2045
2046                 public static int pipe (int[] filedes)
2047                 {
2048                         if (filedes == null || filedes.Length != 2) {
2049                                 // TODO: set errno
2050                                 return -1;
2051                         }
2052                         int reading, writing;
2053                         int r = pipe (out reading, out writing);
2054                         filedes[0] = reading;
2055                         filedes[1] = writing;
2056                         return r;
2057                 }
2058
2059                 [DllImport (LIBC, SetLastError=true)]
2060                 public static extern uint alarm (uint seconds);
2061
2062                 [DllImport (LIBC, SetLastError=true)]
2063                 public static extern uint sleep (uint seconds);
2064
2065                 [DllImport (LIBC, SetLastError=true)]
2066                 public static extern uint ualarm (uint usecs, uint interval);
2067
2068                 [DllImport (LIBC, SetLastError=true)]
2069                 public static extern int pause ();
2070
2071                 // chown(2)
2072                 //    int chown(const char *path, uid_t owner, gid_t group);
2073                 [DllImport (LIBC, SetLastError=true)]
2074                 public static extern int chown (string path, uint owner, uint group);
2075
2076                 // fchown(2)
2077                 //    int fchown(int fd, uid_t owner, gid_t group);
2078                 [DllImport (LIBC, SetLastError=true)]
2079                 public static extern int fchown (int fd, uint owner, uint group);
2080
2081                 // lchown(2)
2082                 //    int lchown(const char *path, uid_t owner, gid_t group);
2083                 [DllImport (LIBC, SetLastError=true)]
2084                 public static extern int lchown (string path, uint owner, uint group);
2085
2086                 [DllImport (LIBC, SetLastError=true)]
2087                 public static extern int chdir (string path);
2088
2089                 [DllImport (LIBC, SetLastError=true)]
2090                 public static extern int fchdir (int fd);
2091
2092                 // getcwd(3)
2093                 //    char *getcwd(char *buf, size_t size);
2094                 [DllImport (MPH, SetLastError=true,
2095                                 EntryPoint="Mono_Posix_Syscall_getcwd")]
2096                 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
2097
2098                 public static StringBuilder getcwd (StringBuilder buf)
2099                 {
2100                         getcwd (buf, (ulong) buf.Capacity);
2101                         return buf;
2102                 }
2103
2104                 // getwd(2) is deprecated; don't expose it.
2105
2106                 [DllImport (LIBC, SetLastError=true)]
2107                 public static extern int dup (int fd);
2108
2109                 [DllImport (LIBC, SetLastError=true)]
2110                 public static extern int dup2 (int fd, int fd2);
2111
2112                 // TODO: does Mono marshal arrays properly?
2113                 [DllImport (LIBC, SetLastError=true)]
2114                 public static extern int execve (string path, string[] argv, string[] envp);
2115
2116                 [DllImport (LIBC, SetLastError=true)]
2117                 public static extern int fexecve (int fd, string[] argv, string[] envp);
2118
2119                 [DllImport (LIBC, SetLastError=true)]
2120                 public static extern int execv (string path, string[] argv);
2121
2122                 // TODO: execle, execl, execlp
2123                 [DllImport (LIBC, SetLastError=true)]
2124                 public static extern int execvp (string path, string[] argv);
2125
2126                 [DllImport (LIBC, SetLastError=true)]
2127                 public static extern int nice (int inc);
2128
2129                 [DllImport (LIBC, SetLastError=true)]
2130                 public static extern int _exit (int status);
2131
2132                 [DllImport (MPH, SetLastError=true,
2133                                 EntryPoint="Mono_Posix_Syscall_fpathconf")]
2134                 public static extern long fpathconf (int filedes, PathConf name);
2135
2136                 [DllImport (MPH, SetLastError=true,
2137                                 EntryPoint="Mono_Posix_Syscall_pathconf")]
2138                 public static extern long pathconf (string path, PathConf name);
2139
2140                 [DllImport (MPH, SetLastError=true,
2141                                 EntryPoint="Mono_Posix_Syscall_sysconf")]
2142                 public static extern long sysconf (SysConf name);
2143
2144                 // confstr(3)
2145                 //    size_t confstr(int name, char *buf, size_t len);
2146                 [DllImport (MPH, SetLastError=true,
2147                                 EntryPoint="Mono_Posix_Syscall_confstr")]
2148                 public static extern ulong confstr (ConfStr name, [Out] StringBuilder buf, ulong len);
2149
2150                 // getpid(2)
2151                 //    pid_t getpid(void);
2152                 [DllImport (LIBC, SetLastError=true)]
2153                 public static extern int getpid ();
2154
2155                 // getppid(2)
2156                 //    pid_t getppid(void);
2157                 [DllImport (LIBC, SetLastError=true)]
2158                 public static extern int getppid ();
2159
2160                 // setpgid(2)
2161                 //    int setpgid(pid_t pid, pid_t pgid);
2162                 [DllImport (LIBC, SetLastError=true)]
2163                 public static extern int setpgid (int pid, int pgid);
2164
2165                 // getpgid(2)
2166                 //    pid_t getpgid(pid_t pid);
2167                 [DllImport (LIBC, SetLastError=true)]
2168                 public static extern int getpgid (int pid);
2169
2170                 [DllImport (LIBC, SetLastError=true)]
2171                 public static extern int setpgrp ();
2172
2173                 // getpgrp(2)
2174                 //    pid_t getpgrp(void);
2175                 [DllImport (LIBC, SetLastError=true)]
2176                 public static extern int getpgrp ();
2177
2178                 // setsid(2)
2179                 //    pid_t setsid(void);
2180                 [DllImport (LIBC, SetLastError=true)]
2181                 public static extern int setsid ();
2182
2183                 // getsid(2)
2184                 //    pid_t getsid(pid_t pid);
2185                 [DllImport (LIBC, SetLastError=true)]
2186                 public static extern int getsid (int pid);
2187
2188                 // getuid(2)
2189                 //    uid_t getuid(void);
2190                 [DllImport (LIBC, SetLastError=true)]
2191                 public static extern uint getuid ();
2192
2193                 // geteuid(2)
2194                 //    uid_t geteuid(void);
2195                 [DllImport (LIBC, SetLastError=true)]
2196                 public static extern uint geteuid ();
2197
2198                 // getgid(2)
2199                 //    gid_t getgid(void);
2200                 [DllImport (LIBC, SetLastError=true)]
2201                 public static extern uint getgid ();
2202
2203                 // getegid(2)
2204                 //    gid_t getgid(void);
2205                 [DllImport (LIBC, SetLastError=true)]
2206                 public static extern uint getegid ();
2207
2208                 // getgroups(2)
2209                 //    int getgroups(int size, gid_t list[]);
2210                 [DllImport (LIBC, SetLastError=true)]
2211                 public static extern int getgroups (int size, uint[] list);
2212
2213                 public static int getgroups (uint[] list)
2214                 {
2215                         return getgroups (list.Length, list);
2216                 }
2217
2218                 // setuid(2)
2219                 //    int setuid(uid_t uid);
2220                 [DllImport (LIBC, SetLastError=true)]
2221                 public static extern int setuid (uint uid);
2222
2223                 // setreuid(2)
2224                 //    int setreuid(uid_t ruid, uid_t euid);
2225                 [DllImport (LIBC, SetLastError=true)]
2226                 public static extern int setreuid (uint ruid, uint euid);
2227
2228                 // setregid(2)
2229                 //    int setregid(gid_t ruid, gid_t euid);
2230                 [DllImport (LIBC, SetLastError=true)]
2231                 public static extern int setregid (uint rgid, uint egid);
2232
2233                 // seteuid(2)
2234                 //    int seteuid(uid_t euid);
2235                 [DllImport (LIBC, SetLastError=true)]
2236                 public static extern int seteuid (uint euid);
2237
2238                 // setegid(2)
2239                 //    int setegid(gid_t euid);
2240                 [DllImport (LIBC, SetLastError=true)]
2241                 public static extern int setegid (uint uid);
2242
2243                 // setgid(2)
2244                 //    int setgid(gid_t gid);
2245                 [DllImport (LIBC, SetLastError=true)]
2246                 public static extern int setgid (uint gid);
2247
2248                 // getresuid(2)
2249                 //    int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
2250                 [DllImport (LIBC, SetLastError=true)]
2251                 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
2252
2253                 // getresgid(2)
2254                 //    int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
2255                 [DllImport (LIBC, SetLastError=true)]
2256                 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
2257
2258                 // setresuid(2)
2259                 //    int setresuid(uid_t ruid, uid_t euid, uid_t suid);
2260                 [DllImport (LIBC, SetLastError=true)]
2261                 public static extern int setresuid (uint ruid, uint euid, uint suid);
2262
2263                 // setresgid(2)
2264                 //    int setresgid(gid_t ruid, gid_t euid, gid_t suid);
2265                 [DllImport (LIBC, SetLastError=true)]
2266                 public static extern int setresgid (uint rgid, uint egid, uint sgid);
2267
2268                 // fork(2)
2269                 //    pid_t fork(void);
2270                 [DllImport (LIBC, SetLastError=true)]
2271                 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " + 
2272                                 "shutdown code.\nUse System.Diagnostics.Process instead")]
2273                 public static extern int fork ();
2274
2275                 // vfork(2)
2276                 //    pid_t vfork(void);
2277                 [DllImport (LIBC, SetLastError=true)]
2278                 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " + 
2279                                 "shutdown code.\nUse System.Diagnostics.Process instead")]
2280                 public static extern int vfork ();
2281
2282                 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
2283                 private static extern IntPtr sys_ttyname (int fd);
2284
2285                 [Obsolete ("Not re-entrant.  Use ttyname_r instead.")]
2286                 public static string ttyname (int fd)
2287                 {
2288                         IntPtr r = sys_ttyname (fd);
2289                         return UnixMarshal.PtrToString (r);
2290                 }
2291
2292                 // ttyname_r(3)
2293                 //    int ttyname_r(int fd, char *buf, size_t buflen);
2294                 [DllImport (MPH, SetLastError=true,
2295                                 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
2296                 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
2297
2298                 public static int ttyname_r (int fd, StringBuilder buf)
2299                 {
2300                         return ttyname_r (fd, buf, (ulong) buf.Capacity);
2301                 }
2302
2303                 [DllImport (LIBC, EntryPoint="isatty")]
2304                 private static extern int sys_isatty (int fd);
2305
2306                 public static bool isatty (int fd)
2307                 {
2308                         return sys_isatty (fd) == 1;
2309                 }
2310
2311                 [DllImport (LIBC, SetLastError=true)]
2312                 public static extern int link (string oldpath, string newpath);
2313
2314                 [DllImport (LIBC, SetLastError=true)]
2315                 public static extern int symlink (string oldpath, string newpath);
2316
2317                 // readlink(2)
2318                 //    int readlink(const char *path, char *buf, size_t bufsize);
2319                 [DllImport (MPH, SetLastError=true,
2320                                 EntryPoint="Mono_Posix_Syscall_readlink")]
2321                 public static extern int readlink (string path, [Out] StringBuilder buf, ulong bufsiz);
2322
2323                 public static int readlink (string path, [Out] StringBuilder buf)
2324                 {
2325                         return readlink (path, buf, (ulong) buf.Capacity);
2326                 }
2327
2328                 [DllImport (LIBC, SetLastError=true)]
2329                 public static extern int unlink (string pathname);
2330
2331                 [DllImport (LIBC, SetLastError=true)]
2332                 public static extern int rmdir (string pathname);
2333
2334                 // tcgetpgrp(3)
2335                 //    pid_t tcgetpgrp(int fd);
2336                 [DllImport (LIBC, SetLastError=true)]
2337                 public static extern int tcgetpgrp (int fd);
2338
2339                 // tcsetpgrp(3)
2340                 //    int tcsetpgrp(int fd, pid_t pgrp);
2341                 [DllImport (LIBC, SetLastError=true)]
2342                 public static extern int tcsetpgrp (int fd, int pgrp);
2343
2344                 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
2345                 private static extern IntPtr sys_getlogin ();
2346
2347                 [Obsolete ("Not re-entrant.  Use getlogin_r instead.")]
2348                 public static string getlogin ()
2349                 {
2350                         IntPtr r = sys_getlogin ();
2351                         return UnixMarshal.PtrToString (r);
2352                 }
2353
2354                 // getlogin_r(3)
2355                 //    int getlogin_r(char *buf, size_t bufsize);
2356                 [DllImport (MPH, SetLastError=true,
2357                                 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
2358                 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
2359
2360                 public static int getlogin_r (StringBuilder name)
2361                 {
2362                         return getlogin_r (name, (ulong) name.Capacity);
2363                 }
2364
2365                 [DllImport (LIBC, SetLastError=true)]
2366                 public static extern int setlogin (string name);
2367
2368                 // gethostname(2)
2369                 //    int gethostname(char *name, size_t len);
2370                 [DllImport (MPH, SetLastError=true,
2371                                 EntryPoint="Mono_Posix_Syscall_gethostname")]
2372                 public static extern int gethostname ([Out] StringBuilder name, ulong len);
2373
2374                 public static int gethostname (StringBuilder name)
2375                 {
2376                         return gethostname (name, (ulong) name.Capacity);
2377                 }
2378
2379                 // sethostname(2)
2380                 //    int gethostname(const char *name, size_t len);
2381                 [DllImport (MPH, SetLastError=true,
2382                                 EntryPoint="Mono_Posix_Syscall_gethostname")]
2383                 public static extern int sethostname (string name, ulong len);
2384
2385                 public static int sethostname (string name)
2386                 {
2387                         return sethostname (name, (ulong) name.Length);
2388                 }
2389
2390                 [DllImport (MPH, SetLastError=true,
2391                                 EntryPoint="Mono_Posix_Syscall_gethostid")]
2392                 public static extern long gethostid ();
2393
2394                 [DllImport (MPH, SetLastError=true,
2395                                 EntryPoint="Mono_Posix_Syscall_sethostid")]
2396                 public static extern int sethostid (long hostid);
2397
2398                 // getdomainname(2)
2399                 //    int getdomainname(char *name, size_t len);
2400                 [DllImport (MPH, SetLastError=true,
2401                                 EntryPoint="Mono_Posix_Syscall_getdomainname")]
2402                 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
2403
2404                 public static int getdomainname (StringBuilder name)
2405                 {
2406                         return getdomainname (name, (ulong) name.Capacity);
2407                 }
2408
2409                 // setdomainname(2)
2410                 //    int setdomainname(const char *name, size_t len);
2411                 [DllImport (MPH, SetLastError=true,
2412                                 EntryPoint="Mono_Posix_Syscall_setdomainname")]
2413                 public static extern int setdomainname (string name, ulong len);
2414
2415                 public static int setdomainname (string name)
2416                 {
2417                         return setdomainname (name, (ulong) name.Length);
2418                 }
2419
2420                 [DllImport (LIBC, SetLastError=true)]
2421                 public static extern int vhangup ();
2422
2423                 // Revoke doesn't appear to be POSIX.  Include it?
2424                 [DllImport (LIBC, SetLastError=true)]
2425                 public static extern int revoke (string file);
2426
2427                 // TODO: profil?  It's not POSIX.
2428
2429                 [DllImport (LIBC, SetLastError=true)]
2430                 public static extern int acct (string filename);
2431
2432                 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
2433                 private static extern IntPtr sys_getusershell ();
2434
2435                 public static string getusershell ()
2436                 {
2437                         IntPtr r = sys_getusershell ();
2438                         return UnixMarshal.PtrToString (r);
2439                 }
2440
2441                 [DllImport (LIBC, SetLastError=true)]
2442                 public static extern void setusershell ();
2443
2444                 [DllImport (LIBC, SetLastError=true)]
2445                 public static extern void endusershell ();
2446
2447                 [DllImport (LIBC, SetLastError=true)]
2448                 private static extern int daemon (int nochdir, int noclose);
2449
2450                 public static int daemon (bool nochdir, bool noclose)
2451                 {
2452                         return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
2453                 }
2454
2455                 [DllImport (LIBC, SetLastError=true)]
2456                 public static extern int chroot (string path);
2457
2458                 // skipping getpass(3) as the man page states:
2459                 //   This function is obsolete.  Do not use it.
2460
2461                 [DllImport (LIBC, SetLastError=true)]
2462                 public static extern int fsync (int fd);
2463
2464                 [DllImport (LIBC, SetLastError=true)]
2465                 public static extern int fdatasync (int fd);
2466
2467                 [DllImport (LIBC, SetLastError=true)]
2468                 public static extern void sync ();
2469
2470                 [DllImport (LIBC, SetLastError=true)]
2471                 [Obsolete ("Dropped in POSIX 1003.1-2001.  " +
2472                                 "Use Unistd.sysconf (SysConf._SC_PAGESIZE).")]
2473                 public static extern int getpagesize ();
2474
2475                 // truncate(2)
2476                 //    int truncate(const char *path, off_t length);
2477                 [DllImport (MPH, SetLastError=true, 
2478                                 EntryPoint="Mono_Posix_Syscall_truncate")]
2479                 public static extern int truncate (string path, long length);
2480
2481                 // ftruncate(2)
2482                 //    int ftruncate(int fd, off_t length);
2483                 [DllImport (MPH, SetLastError=true, 
2484                                 EntryPoint="Mono_Posix_Syscall_ftruncate")]
2485                 public static extern int ftruncate (int fd, long length);
2486
2487                 [DllImport (LIBC, SetLastError=true)]
2488                 public static extern int getdtablesize ();
2489
2490                 [DllImport (LIBC, SetLastError=true)]
2491                 public static extern int brk (IntPtr end_data_segment);
2492
2493                 [DllImport (LIBC, SetLastError=true)]
2494                 public static extern IntPtr sbrk (IntPtr increment);
2495
2496                 // TODO: syscall(2)?
2497                 // Probably safer to skip entirely.
2498
2499                 // lockf(3)
2500                 //    int lockf(int fd, int cmd, off_t len);
2501                 [DllImport (MPH, SetLastError=true, 
2502                                 EntryPoint="Mono_Posix_Syscall_lockf")]
2503                 public static extern int lockf (int fd, LockFlags cmd, long len);
2504
2505                 [DllImport (CRYPT, SetLastError=true, EntryPoint="crypt")]
2506                 private static extern IntPtr sys_crypt (string key, string salt);
2507
2508                 public static string crypt (string key, string salt)
2509                 {
2510                         IntPtr r = sys_crypt (key, salt);
2511                         return UnixMarshal.PtrToString (r);
2512                 }
2513
2514                 [DllImport (CRYPT, SetLastError=true, EntryPoint="encrypt")]
2515                 private static extern void sys_encrypt ([In, Out] byte[] block, int edflag);
2516
2517                 public static void encrypt (byte[] block, bool decode)
2518                 {
2519                         if (block.Length < 64)
2520                                 throw new ArgumentOutOfRangeException ("block", "Must refer to at least 64 bytes");
2521                         sys_encrypt (block, decode ? 1 : 0);
2522                 }
2523
2524                 // swab(3)
2525                 //    void swab(const void *from, void *to, ssize_t n);
2526                 [DllImport (MPH, SetLastError=true, 
2527                                 EntryPoint="Mono_Posix_Syscall_swab")]
2528                 public static extern void swab (IntPtr from, IntPtr to, long n);
2529
2530                 public static unsafe void swab (void* from, void* to, long n)
2531                 {
2532                         swab ((IntPtr) from, (IntPtr) to, n);
2533                 }
2534
2535                 #endregion
2536
2537                 #region <utime.h> Declarations
2538                 //
2539                 // <utime.h>  -- COMPLETE
2540                 //
2541
2542                 [DllImport (MPH, SetLastError=true, 
2543                                 EntryPoint="Mono_Posix_Syscall_utime")]
2544                 public static extern int utime (string filename, ref Utimbuf buf);
2545
2546                 [DllImport (MPH, SetLastError=true, 
2547                                 EntryPoint="Mono_Posix_Syscall_utime")]
2548                 private static extern int utime (string filename, IntPtr buf);
2549
2550                 public static int utime (string filename)
2551                 {
2552                         return utime (filename, IntPtr.Zero);
2553                 }
2554                 #endregion
2555         }
2556
2557         #endregion
2558 }
2559
2560 // vim: noexpandtab