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