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