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