svn path=/branches/mono-1-1-9/mcs/; revision=51216
[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 PathConf : 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 SysConf : 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 ConfStr : 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 (LIBC, SetLastError=true)]
1395                 public static extern void rewinddir (IntPtr dir);
1396
1397                 private struct _Dirent {
1398                         public /* ino_t */ ulong  d_ino;
1399                         public /* off_t */ long   d_off;
1400                         public ushort             d_reclen;
1401                         public byte               d_type;
1402                         public IntPtr             d_name;
1403                 }
1404
1405                 private static void CopyDirent (Dirent to, ref _Dirent from)
1406                 {
1407                         try {
1408                                 to.d_ino    = from.d_ino;
1409                                 to.d_off    = from.d_off;
1410                                 to.d_reclen = from.d_reclen;
1411                                 to.d_type   = from.d_type;
1412                                 to.d_name   = UnixMarshal.PtrToString (from.d_name);
1413                         }
1414                         finally {
1415                                 Stdlib.free (from.d_name);
1416                                 from.d_name = IntPtr.Zero;
1417                         }
1418                 }
1419
1420                 [DllImport (MPH, SetLastError=true,
1421                                 EntryPoint="Mono_Posix_Syscall_readdir")]
1422                 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
1423
1424                 public static Dirent readdir (IntPtr dir)
1425                 {
1426                         _Dirent dentry;
1427                         int r = sys_readdir (dir, out dentry);
1428                         if (r != 0)
1429                                 return null;
1430                         Dirent d = new Dirent ();
1431                         CopyDirent (d, ref dentry);
1432                         return d;
1433                 }
1434
1435                 [DllImport (MPH, SetLastError=true,
1436                                 EntryPoint="Mono_Posix_Syscall_readdir_r")]
1437                 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
1438
1439                 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
1440                 {
1441                         entry.d_ino    = 0;
1442                         entry.d_off    = 0;
1443                         entry.d_reclen = 0;
1444                         entry.d_type   = 0;
1445                         entry.d_name   = null;
1446
1447                         _Dirent _d;
1448                         int r = sys_readdir_r (dirp, out _d, out result);
1449
1450                         if (r == 0 && result != IntPtr.Zero) {
1451                                 CopyDirent (entry, ref _d);
1452                         }
1453
1454                         return r;
1455                 }
1456
1457                 [DllImport (LIBC, SetLastError=true)]
1458                 public static extern int dirfd (IntPtr dir);
1459                 #endregion
1460
1461                 #region <fcntl.h> Declarations
1462                 //
1463                 // <fcntl.h> -- COMPLETE
1464                 //
1465
1466                 [DllImport (MPH, SetLastError=true, 
1467                                 EntryPoint="Mono_Posix_Syscall_fcntl")]
1468                 public static extern int fcntl (int fd, FcntlCommand cmd);
1469
1470                 [DllImport (MPH, SetLastError=true, 
1471                                 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
1472                 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
1473
1474                 public static int fcntl (int fd, FcntlCommand cmd, DirectoryNotifyFlags arg)
1475                 {
1476                         if (cmd != FcntlCommand.F_NOTIFY) {
1477                                 SetLastError (Errno.EINVAL);
1478                                 return -1;
1479                         }
1480                         long _arg = NativeConvert.FromDirectoryNotifyFlags (arg);
1481                         return fcntl (fd, FcntlCommand.F_NOTIFY, _arg);
1482                 }
1483
1484                 [DllImport (MPH, SetLastError=true, 
1485                                 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
1486                 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
1487
1488                 [DllImport (MPH, SetLastError=true, 
1489                                 EntryPoint="Mono_Posix_Syscall_open")]
1490                 public static extern int open (string pathname, OpenFlags flags);
1491
1492                 // open(2)
1493                 //    int open(const char *pathname, int flags, mode_t mode);
1494                 [DllImport (MPH, SetLastError=true, 
1495                                 EntryPoint="Mono_Posix_Syscall_open_mode")]
1496                 public static extern int open (string pathname, OpenFlags flags, FilePermissions mode);
1497
1498                 // creat(2)
1499                 //    int creat(const char *pathname, mode_t mode);
1500                 [DllImport (MPH, SetLastError=true, 
1501                                 EntryPoint="Mono_Posix_Syscall_creat")]
1502                 public static extern int creat (string pathname, FilePermissions mode);
1503
1504                 // posix_fadvise(2)
1505                 //    int posix_fadvise(int fd, off_t offset, off_t len, int advice);
1506                 [DllImport (MPH, SetLastError=true, 
1507                                 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
1508                 public static extern int posix_fadvise (int fd, long offset, 
1509                         long len, PosixFadviseAdvice advice);
1510
1511                 // posix_fallocate(P)
1512                 //    int posix_fallocate(int fd, off_t offset, size_t len);
1513                 [DllImport (MPH, SetLastError=true, 
1514                                 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
1515                 public static extern int posix_fallocate (int fd, long offset, ulong len);
1516                 #endregion
1517
1518                 #region <fstab.h> Declarations
1519                 //
1520                 // <fstab.h>  -- COMPLETE
1521                 //
1522                 private struct _Fstab {
1523                         public IntPtr fs_spec;
1524                         public IntPtr fs_file;
1525                         public IntPtr fs_vfstype;
1526                         public IntPtr fs_mntops;
1527                         public IntPtr fs_type;
1528                         public int    fs_freq;
1529                         public int    fs_passno;
1530                         public IntPtr _fs_buf_;
1531                 }
1532
1533                 private static void CopyFstab (Fstab to, ref _Fstab from)
1534                 {
1535                         try {
1536                                 to.fs_spec     = UnixMarshal.PtrToString (from.fs_spec);
1537                                 to.fs_file     = UnixMarshal.PtrToString (from.fs_file);
1538                                 to.fs_vfstype  = UnixMarshal.PtrToString (from.fs_vfstype);
1539                                 to.fs_mntops   = UnixMarshal.PtrToString (from.fs_mntops);
1540                                 to.fs_type     = UnixMarshal.PtrToString (from.fs_type);
1541                                 to.fs_freq     = from.fs_freq;
1542                                 to.fs_passno   = from.fs_passno;
1543                         }
1544                         finally {
1545                                 Stdlib.free (from._fs_buf_);
1546                                 from._fs_buf_ = IntPtr.Zero;
1547                         }
1548                 }
1549
1550                 internal static object fstab_lock = new object ();
1551
1552                 [DllImport (MPH, SetLastError=true,
1553                                 EntryPoint="Mono_Posix_Syscall_endfsent")]
1554                 private static extern void sys_endfsent ();
1555
1556                 public static void endfsent ()
1557                 {
1558                         lock (fstab_lock) {
1559                                 sys_endfsent ();
1560                         }
1561                 }
1562
1563                 [DllImport (MPH, SetLastError=true,
1564                                 EntryPoint="Mono_Posix_Syscall_getfsent")]
1565                 private static extern int sys_getfsent (out _Fstab fs);
1566
1567                 public static Fstab getfsent ()
1568                 {
1569                         _Fstab fsbuf;
1570                         int r;
1571                         lock (fstab_lock) {
1572                                 r = sys_getfsent (out fsbuf);
1573                         }
1574                         if (r != 0)
1575                                 return null;
1576                         Fstab fs = new Fstab ();
1577                         CopyFstab (fs, ref fsbuf);
1578                         return fs;
1579                 }
1580
1581                 [DllImport (MPH, SetLastError=true,
1582                                 EntryPoint="Mono_Posix_Syscall_getfsfile")]
1583                 private static extern int sys_getfsfile (string mount_point, out _Fstab fs);
1584
1585                 public static Fstab getfsfile (string mount_point)
1586                 {
1587                         _Fstab fsbuf;
1588                         int r;
1589                         lock (fstab_lock) {
1590                                 r = sys_getfsfile (mount_point, out fsbuf);
1591                         }
1592                         if (r != 0)
1593                                 return null;
1594                         Fstab fs = new Fstab ();
1595                         CopyFstab (fs, ref fsbuf);
1596                         return fs;
1597                 }
1598
1599                 [DllImport (MPH, SetLastError=true,
1600                                 EntryPoint="Mono_Posix_Syscall_getfsspec")]
1601                 private static extern int sys_getfsspec (string special_file, out _Fstab fs);
1602
1603                 public static Fstab getfsspec (string special_file)
1604                 {
1605                         _Fstab fsbuf;
1606                         int r;
1607                         lock (fstab_lock) {
1608                                 r = sys_getfsspec (special_file, out fsbuf);
1609                         }
1610                         if (r != 0)
1611                                 return null;
1612                         Fstab fs = new Fstab ();
1613                         CopyFstab (fs, ref fsbuf);
1614                         return fs;
1615                 }
1616
1617                 [DllImport (MPH, SetLastError=true,
1618                                 EntryPoint="Mono_Posix_Syscall_setfsent")]
1619                 private static extern int sys_setfsent ();
1620
1621                 public static int setfsent ()
1622                 {
1623                         lock (fstab_lock) {
1624                                 return sys_setfsent ();
1625                         }
1626                 }
1627
1628                 #endregion
1629
1630                 #region <grp.h> Declarations
1631                 //
1632                 // <grp.h>
1633                 //
1634                 // TODO: putgrent(3), fgetgrent_r(), getgrouplist(2), initgroups(3)
1635
1636                 // setgroups(2)
1637                 //    int setgroups (size_t size, const gid_t *list);
1638                 [DllImport (MPH, SetLastError=true, 
1639                                 EntryPoint="Mono_Posix_Syscall_setgroups")]
1640                 public static extern int setgroups (ulong size, uint[] list);
1641
1642                 public static int setgroups (uint [] list)
1643                 {
1644                         return setgroups ((ulong) list.Length, list);
1645                 }
1646
1647                 private struct _Group
1648                 {
1649                         public IntPtr           gr_name;
1650                         public IntPtr           gr_passwd;
1651                         public /* gid_t */ uint gr_gid;
1652                         public int              _gr_nmem_;
1653                         public IntPtr           gr_mem;
1654                         public IntPtr           _gr_buf_;
1655                 }
1656
1657                 private static void CopyGroup (Group to, ref _Group from)
1658                 {
1659                         try {
1660                                 to.gr_gid    = from.gr_gid;
1661                                 to.gr_name   = UnixMarshal.PtrToString (from.gr_name);
1662                                 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
1663                                 to.gr_mem    = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
1664                         }
1665                         finally {
1666                                 Stdlib.free (from.gr_mem);
1667                                 Stdlib.free (from._gr_buf_);
1668                                 from.gr_mem   = IntPtr.Zero;
1669                                 from._gr_buf_ = IntPtr.Zero;
1670                         }
1671                 }
1672
1673                 internal static object grp_lock = new object ();
1674
1675                 [DllImport (MPH, SetLastError=true,
1676                                 EntryPoint="Mono_Posix_Syscall_getgrnam")]
1677                 private static extern int sys_getgrnam (string name, out _Group group);
1678
1679                 public static Group getgrnam (string name)
1680                 {
1681                         _Group group;
1682                         int r;
1683                         lock (grp_lock) {
1684                                 r = sys_getgrnam (name, out group);
1685                         }
1686                         if (r != 0)
1687                                 return null;
1688                         Group gr = new Group ();
1689                         CopyGroup (gr, ref group);
1690                         return gr;
1691                 }
1692
1693                 // getgrgid(3)
1694                 //    struct group *getgrgid(gid_t gid);
1695                 [DllImport (MPH, SetLastError=true,
1696                                 EntryPoint="Mono_Posix_Syscall_getgrgid")]
1697                 private static extern int sys_getgrgid (uint uid, out _Group group);
1698
1699                 public static Group getgrgid (uint uid)
1700                 {
1701                         _Group group;
1702                         int r;
1703                         lock (grp_lock) {
1704                                 r = sys_getgrgid (uid, out group);
1705                         }
1706                         if (r != 0)
1707                                 return null;
1708                         Group gr = new Group ();
1709                         CopyGroup (gr, ref group);
1710                         return gr;
1711                 }
1712
1713                 [DllImport (MPH, SetLastError=true,
1714                                 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
1715                 private static extern int sys_getgrnam_r (string name, out _Group grbuf, out IntPtr grbufp);
1716
1717                 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
1718                 {
1719                         grbufp = null;
1720                         _Group group;
1721                         IntPtr _grbufp;
1722                         int r = sys_getgrnam_r (name, out group, out _grbufp);
1723                         if (r == 0 && _grbufp != IntPtr.Zero) {
1724                                 CopyGroup (grbuf, ref group);
1725                                 grbufp = grbuf;
1726                         }
1727                         return r;
1728                 }
1729
1730                 // getgrgid_r(3)
1731                 //    int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
1732                 //        size_t buflen, struct group **gbufp);
1733                 [DllImport (MPH, SetLastError=true,
1734                                 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
1735                 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
1736
1737                 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
1738                 {
1739                         grbufp = null;
1740                         _Group group;
1741                         IntPtr _grbufp;
1742                         int r = sys_getgrgid_r (uid, out group, out _grbufp);
1743                         if (r == 0 && _grbufp != IntPtr.Zero) {
1744                                 CopyGroup (grbuf, ref group);
1745                                 grbufp = grbuf;
1746                         }
1747                         return r;
1748                 }
1749
1750                 [DllImport (MPH, SetLastError=true,
1751                                 EntryPoint="Mono_Posix_Syscall_getgrent")]
1752                 private static extern int sys_getgrent (out _Group grbuf);
1753
1754                 public static Group getgrent ()
1755                 {
1756                         _Group group;
1757                         int r;
1758                         lock (grp_lock) {
1759                                 r = sys_getgrent (out group);
1760                         }
1761                         if (r != 0)
1762                                 return null;
1763                         Group gr = new Group();
1764                         CopyGroup (gr, ref group);
1765                         return gr;
1766                 }
1767
1768                 [DllImport (LIBC, SetLastError=true, EntryPoint="setgrent")]
1769                 private static extern void sys_setgrent ();
1770
1771                 public static void setgrent ()
1772                 {
1773                         lock (grp_lock) {
1774                                 sys_setgrent ();
1775                         }
1776                 }
1777
1778                 [DllImport (LIBC, SetLastError=true, EntryPoint="endgrent")]
1779                 private static extern void sys_endgrent ();
1780
1781                 public static void endgrent ()
1782                 {
1783                         lock (grp_lock) {
1784                                 sys_endgrent ();
1785                         }
1786                 }
1787
1788                 [DllImport (MPH, SetLastError=true,
1789                                 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
1790                 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
1791
1792                 public static Group fgetgrent (IntPtr stream)
1793                 {
1794                         _Group group;
1795                         int r;
1796                         lock (grp_lock) {
1797                                 r = sys_fgetgrent (stream, out group);
1798                         }
1799                         if (r != 0)
1800                                 return null;
1801                         Group gr = new Group ();
1802                         CopyGroup (gr, ref group);
1803                         return gr;
1804                 }
1805                 #endregion
1806
1807                 #region <pwd.h> Declarations
1808                 //
1809                 // <pwd.h>
1810                 //
1811                 // TODO: putpwent(3), fgetpwent_r()
1812                 //
1813                 // SKIPPING: getpw(3): it's dangerous.  Use getpwuid(3) instead.
1814
1815                 private struct _Passwd
1816                 {
1817                         public IntPtr           pw_name;
1818                         public IntPtr           pw_passwd;
1819                         public /* uid_t */ uint pw_uid;
1820                         public /* gid_t */ uint pw_gid;
1821                         public IntPtr           pw_gecos;
1822                         public IntPtr           pw_dir;
1823                         public IntPtr           pw_shell;
1824                         public IntPtr           _pw_buf_;
1825                 }
1826
1827                 private static void CopyPasswd (Passwd to, ref _Passwd from)
1828                 {
1829                         try {
1830                                 to.pw_name   = UnixMarshal.PtrToString (from.pw_name);
1831                                 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
1832                                 to.pw_uid    = from.pw_uid;
1833                                 to.pw_gid    = from.pw_gid;
1834                                 to.pw_gecos  = UnixMarshal.PtrToString (from.pw_gecos);
1835                                 to.pw_dir    = UnixMarshal.PtrToString (from.pw_dir);
1836                                 to.pw_shell  = UnixMarshal.PtrToString (from.pw_shell);
1837                         }
1838                         finally {
1839                                 Stdlib.free (from._pw_buf_);
1840                                 from._pw_buf_ = IntPtr.Zero;
1841                         }
1842                 }
1843
1844                 internal static object pwd_lock = new object ();
1845
1846                 [DllImport (MPH, SetLastError=true,
1847                                 EntryPoint="Mono_Posix_Syscall_getpwnam")]
1848                 private static extern int sys_getpwnam (string name, out _Passwd passwd);
1849
1850                 public static Passwd getpwnam (string name)
1851                 {
1852                         _Passwd passwd;
1853                         int r;
1854                         lock (pwd_lock) {
1855                                 r = sys_getpwnam (name, out passwd);
1856                         }
1857                         if (r != 0)
1858                                 return null;
1859                         Passwd pw = new Passwd ();
1860                         CopyPasswd (pw, ref passwd);
1861                         return pw;
1862                 }
1863
1864                 // getpwuid(3)
1865                 //    struct passwd *getpwnuid(uid_t uid);
1866                 [DllImport (MPH, SetLastError=true,
1867                                 EntryPoint="Mono_Posix_Syscall_getpwuid")]
1868                 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
1869
1870                 public static Passwd getpwuid (uint uid)
1871                 {
1872                         _Passwd passwd;
1873                         int r;
1874                         lock (pwd_lock) {
1875                                 r = sys_getpwuid (uid, out passwd);
1876                         }
1877                         if (r != 0)
1878                                 return null;
1879                         Passwd pw = new Passwd ();
1880                         CopyPasswd (pw, ref passwd);
1881                         return pw;
1882                 }
1883
1884                 [DllImport (MPH, SetLastError=true,
1885                                 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
1886                 private static extern int sys_getpwnam_r (string name, out _Passwd pwbuf, out IntPtr pwbufp);
1887
1888                 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
1889                 {
1890                         pwbufp = null;
1891                         _Passwd passwd;
1892                         IntPtr _pwbufp;
1893                         int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
1894                         if (r == 0 && _pwbufp != IntPtr.Zero) {
1895                                 CopyPasswd (pwbuf, ref passwd);
1896                                 pwbufp = pwbuf;
1897                         }
1898                         return r;
1899                 }
1900
1901                 // getpwuid_r(3)
1902                 //    int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
1903                 //        buflen, struct passwd **pwbufp);
1904                 [DllImport (MPH, SetLastError=true,
1905                                 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
1906                 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
1907
1908                 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
1909                 {
1910                         pwbufp = null;
1911                         _Passwd passwd;
1912                         IntPtr _pwbufp;
1913                         int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
1914                         if (r == 0 && _pwbufp != IntPtr.Zero) {
1915                                 CopyPasswd (pwbuf, ref passwd);
1916                                 pwbufp = pwbuf;
1917                         }
1918                         return r;
1919                 }
1920
1921                 [DllImport (MPH, SetLastError=true,
1922                                 EntryPoint="Mono_Posix_Syscall_getpwent")]
1923                 private static extern int sys_getpwent (out _Passwd pwbuf);
1924
1925                 public static Passwd getpwent ()
1926                 {
1927                         _Passwd passwd;
1928                         int r;
1929                         lock (pwd_lock) {
1930                                 r = sys_getpwent (out passwd);
1931                         }
1932                         if (r != 0)
1933                                 return null;
1934                         Passwd pw = new Passwd ();
1935                         CopyPasswd (pw, ref passwd);
1936                         return pw;
1937                 }
1938
1939                 [DllImport (LIBC, SetLastError=true, EntryPoint="setpwent")]
1940                 private static extern void sys_setpwent ();
1941
1942                 public static void setpwent ()
1943                 {
1944                         lock (pwd_lock) {
1945                                 sys_setpwent ();
1946                         }
1947                 }
1948
1949                 [DllImport (LIBC, SetLastError=true, EntryPoint="endpwent")]
1950                 private static extern void sys_endpwent ();
1951
1952                 public static void endpwent ()
1953                 {
1954                         lock (pwd_lock) {
1955                                 sys_endpwent ();
1956                         }
1957                 }
1958
1959                 [DllImport (MPH, SetLastError=true,
1960                                 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
1961                 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
1962
1963                 public static Passwd fgetpwent (IntPtr stream)
1964                 {
1965                         _Passwd passwd;
1966                         int r;
1967                         lock (pwd_lock) {
1968                                 r = sys_fgetpwent (stream, out passwd);
1969                         }
1970                         if (r != 0)
1971                                 return null;
1972                         Passwd pw = new Passwd ();
1973                         CopyPasswd (pw, ref passwd);
1974                         return pw;
1975                 }
1976                 #endregion
1977
1978                 #region <signal.h> Declarations
1979                 //
1980                 // <signal.h>
1981                 //
1982                 [DllImport (LIBC, SetLastError=true)]
1983                 private static extern void psignal (int sig, string s);
1984
1985                 public static void psignal (Signum sig, string s)
1986                 {
1987                         int signum = NativeConvert.FromSignum (sig);
1988                         psignal (signum, s);
1989                 }
1990
1991                 // kill(2)
1992                 //    int kill(pid_t pid, int sig);
1993                 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
1994                 private static extern int sys_kill (int pid, int sig);
1995
1996                 public static int kill (int pid, Signum sig)
1997                 {
1998                         int _sig = NativeConvert.FromSignum (sig);
1999                         return sys_kill (pid, _sig);
2000                 }
2001
2002                 private static object signal_lock = new object ();
2003
2004                 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
2005                 private static extern IntPtr sys_strsignal (int sig);
2006
2007                 public static string strsignal (Signum sig)
2008                 {
2009                         int s = NativeConvert.FromSignum (sig);
2010                         lock (signal_lock) {
2011                                 IntPtr r = sys_strsignal (s);
2012                                 return UnixMarshal.PtrToString (r);
2013                         }
2014                 }
2015
2016                 // TODO: sigaction(2)
2017                 // TODO: sigsuspend(2)
2018                 // TODO: sigpending(2)
2019
2020                 #endregion
2021
2022                 #region <stdio.h> Declarations
2023                 //
2024                 // <stdio.h>
2025                 //
2026                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
2027                 private static extern int _L_ctermid ();
2028
2029                 public static readonly int L_ctermid = _L_ctermid ();
2030
2031                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
2032                 private static extern int _L_cuserid ();
2033
2034                 public static readonly int L_cuserid = _L_cuserid ();
2035
2036                 internal static object getlogin_lock = new object ();
2037
2038                 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
2039                 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
2040
2041                 [Obsolete ("\"Nobody knows precisely what cuserid() does... " + 
2042                                 "DO NOT USE cuserid().\n" +
2043                                 "`string' must hold L_cuserid characters.  Use getlogin_r instead.")]
2044                 public static string cuserid (StringBuilder @string)
2045                 {
2046                         if (@string.Capacity < L_cuserid) {
2047                                 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
2048                         }
2049                         lock (getlogin_lock) {
2050                                 IntPtr r = sys_cuserid (@string);
2051                                 return UnixMarshal.PtrToString (r);
2052                         }
2053                 }
2054
2055                 #endregion
2056
2057                 #region <stdlib.h> Declarations
2058                 //
2059                 // <stdlib.h>
2060                 //
2061                 [DllImport (LIBC, SetLastError=true)]
2062                 public static extern int mkstemp (StringBuilder template);
2063
2064                 [DllImport (LIBC, SetLastError=true)]
2065                 public static extern int ttyslot ();
2066
2067                 [DllImport (CRYPT, SetLastError=true)]
2068                 public static extern void setkey (string key);
2069
2070                 #endregion
2071
2072                 #region <string.h> Declarations
2073                 //
2074                 // <string.h>
2075                 //
2076
2077                 // strerror_r(3)
2078                 //    int strerror_r(int errnum, char *buf, size_t n);
2079                 [DllImport (MPH, SetLastError=true, 
2080                                 EntryPoint="Mono_Posix_Syscall_strerror_r")]
2081                 private static extern int sys_strerror_r (int errnum, 
2082                                 [Out] StringBuilder buf, ulong n);
2083
2084                 public static int strerror_r (Errno errnum, StringBuilder buf, ulong n)
2085                 {
2086                         int e = NativeConvert.FromErrno (errnum);
2087                         return sys_strerror_r (e, buf, n);
2088                 }
2089
2090                 public static int strerror_r (Errno errnum, StringBuilder buf)
2091                 {
2092                         return strerror_r (errnum, buf, (ulong) buf.Capacity);
2093                 }
2094
2095                 #endregion
2096
2097                 #region <sys/mman.h> Declarations
2098                 //
2099                 // <sys/mman.h>
2100                 //
2101
2102                 // posix_madvise(P)
2103                 //    int posix_madvise(void *addr, size_t len, int advice);
2104                 [DllImport (MPH, SetLastError=true, 
2105                                 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
2106                 public static extern int posix_madvise (IntPtr addr, ulong len, 
2107                         PosixMadviseAdvice advice);
2108
2109                 public static readonly IntPtr MAP_FAILED = unchecked((IntPtr)(-1));
2110
2111                 [DllImport (MPH, SetLastError=true, 
2112                                 EntryPoint="Mono_Posix_Syscall_mmap")]
2113                 public static extern IntPtr mmap (IntPtr start, ulong length, 
2114                                 MmapProts prot, MmapFlags flags, int fd, long offset);
2115
2116                 [DllImport (MPH, SetLastError=true, 
2117                                 EntryPoint="Mono_Posix_Syscall_munmap")]
2118                 public static extern int munmap (IntPtr start, ulong length);
2119
2120                 [DllImport (MPH, SetLastError=true, 
2121                                 EntryPoint="Mono_Posix_Syscall_mprotect")]
2122                 public static extern int mprotect (IntPtr start, ulong len, MmapProts prot);
2123
2124                 [DllImport (MPH, SetLastError=true, 
2125                                 EntryPoint="Mono_Posix_Syscall_msync")]
2126                 public static extern int msync (IntPtr start, ulong len, MsyncFlags flags);
2127
2128                 [DllImport (MPH, SetLastError=true, 
2129                                 EntryPoint="Mono_Posix_Syscall_mlock")]
2130                 public static extern int mlock (IntPtr start, ulong len);
2131
2132                 [DllImport (MPH, SetLastError=true, 
2133                                 EntryPoint="Mono_Posix_Syscall_munlock")]
2134                 public static extern int munlock (IntPtr start, ulong len);
2135
2136                 [DllImport (LIBC, SetLastError=true, EntryPoint="mlockall")]
2137                 private static extern int sys_mlockall (int flags);
2138
2139                 public static int mlockall (MlockallFlags flags)
2140                 {
2141                         int _flags = NativeConvert.FromMlockallFlags (flags);
2142                         return sys_mlockall (_flags);
2143                 }
2144
2145                 [DllImport (LIBC, SetLastError=true)]
2146                 public static extern int munlockall ();
2147
2148                 [DllImport (MPH, SetLastError=true, 
2149                                 EntryPoint="Mono_Posix_Syscall_mremap")]
2150                 public static extern IntPtr mremap (IntPtr old_address, ulong old_size, 
2151                                 ulong new_size, MremapFlags flags);
2152
2153                 [DllImport (MPH, SetLastError=true, 
2154                                 EntryPoint="Mono_Posix_Syscall_mincore")]
2155                 public static extern int mincore (IntPtr start, ulong length, byte[] vec);
2156
2157                 [DllImport (MPH, SetLastError=true, 
2158                                 EntryPoint="Mono_Posix_Syscall_remap_file_pages")]
2159                 public static extern int remap_file_pages (IntPtr start, ulong size,
2160                                 MmapProts prot, long pgoff, MmapFlags flags);
2161
2162                 #endregion
2163
2164                 #region <sys/poll.h> Declarations
2165                 //
2166                 // <sys/poll.h> -- COMPLETE
2167                 //
2168
2169                 private struct _pollfd {
2170                         public int fd;
2171                         public short events;
2172                         public short revents;
2173                 }
2174
2175                 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
2176                 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
2177
2178                 public static int poll (Pollfd [] fds, uint nfds, int timeout)
2179                 {
2180                         if (fds.Length < nfds)
2181                                 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
2182
2183                         _pollfd[] send = new _pollfd[nfds];
2184
2185                         for (int i = 0; i < send.Length; i++) {
2186                                 send [i].fd     = fds [i].fd;
2187                                 send [i].events = NativeConvert.FromPollEvents (fds [i].events);
2188                         }
2189
2190                         int r = sys_poll (send, nfds, timeout);
2191
2192                         for (int i = 0; i < send.Length; i++) {
2193                                 fds [i].revents = NativeConvert.ToPollEvents (send [i].revents);
2194                         }
2195
2196                         return r;
2197                 }
2198
2199                 public static int poll (Pollfd [] fds, int timeout)
2200                 {
2201                         return poll (fds, (uint) fds.Length, timeout);
2202                 }
2203
2204                 //
2205                 // <sys/ptrace.h>
2206                 //
2207
2208                 // TODO: ptrace(2)
2209
2210                 //
2211                 // <sys/resource.h>
2212                 //
2213
2214                 // TODO: setrlimit(2)
2215                 // TODO: getrlimit(2)
2216                 // TODO: getrusage(2)
2217
2218                 #endregion
2219
2220                 #region <sys/sendfile.h> Declarations
2221                 //
2222                 // <sys/sendfile.h> -- COMPLETE
2223                 //
2224
2225                 [DllImport (MPH, SetLastError=true,
2226                                 EntryPoint="Mono_Posix_Syscall_sendfile")]
2227                 public static extern long sendfile (int out_fd, int in_fd, 
2228                                 ref long offset, ulong count);
2229
2230                 #endregion
2231
2232                 #region <sys/stat.h> Declarations
2233                 //
2234                 // <sys/stat.h>  -- COMPLETE
2235                 //
2236                 [DllImport (MPH, SetLastError=true, 
2237                                 EntryPoint="Mono_Posix_Syscall_stat")]
2238                 public static extern int stat (string file_name, out Stat buf);
2239
2240                 [DllImport (MPH, SetLastError=true, 
2241                                 EntryPoint="Mono_Posix_Syscall_fstat")]
2242                 public static extern int fstat (int filedes, out Stat buf);
2243
2244                 [DllImport (MPH, SetLastError=true, 
2245                                 EntryPoint="Mono_Posix_Syscall_lstat")]
2246                 public static extern int lstat (string file_name, out Stat buf);
2247
2248                 // TODO:
2249                 // S_ISDIR, S_ISCHR, S_ISBLK, S_ISREG, S_ISFIFO, S_ISLNK, S_ISSOCK
2250                 // All take FilePermissions
2251
2252                 // chmod(2)
2253                 //    int chmod(const char *path, mode_t mode);
2254                 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
2255                 private static extern int sys_chmod (string path, uint mode);
2256
2257                 public static int chmod (string path, FilePermissions mode)
2258                 {
2259                         uint _mode = NativeConvert.FromFilePermissions (mode);
2260                         return sys_chmod (path, _mode);
2261                 }
2262
2263                 // fchmod(2)
2264                 //    int chmod(int filedes, mode_t mode);
2265                 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
2266                 private static extern int sys_fchmod (int filedes, uint mode);
2267
2268                 public static int fchmod (int filedes, FilePermissions mode)
2269                 {
2270                         uint _mode = NativeConvert.FromFilePermissions (mode);
2271                         return sys_fchmod (filedes, _mode);
2272                 }
2273
2274                 // umask(2)
2275                 //    mode_t umask(mode_t mask);
2276                 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
2277                 private static extern uint sys_umask (uint mask);
2278
2279                 public static FilePermissions umask (FilePermissions mask)
2280                 {
2281                         uint _mask = NativeConvert.FromFilePermissions (mask);
2282                         uint r = sys_umask (_mask);
2283                         return NativeConvert.ToFilePermissions (r);
2284                 }
2285
2286                 // mkdir(2)
2287                 //    int mkdir(const char *pathname, mode_t mode);
2288                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
2289                 private static extern int sys_mkdir (string oldpath, uint mode);
2290
2291                 public static int mkdir (string oldpath, FilePermissions mode)
2292                 {
2293                         uint _mode = NativeConvert.FromFilePermissions (mode);
2294                         return sys_mkdir (oldpath, _mode);
2295                 }
2296
2297                 // mknod(2)
2298                 //    int mknod (const char *pathname, mode_t mode, dev_t dev);
2299                 [DllImport (MPH, SetLastError=true,
2300                                 EntryPoint="Mono_Posix_Syscall_mknod")]
2301                 public static extern int mknod (string pathname, FilePermissions mode, ulong dev);
2302
2303                 // mkfifo(3)
2304                 //    int mkfifo(const char *pathname, mode_t mode);
2305                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
2306                 private static extern int sys_mkfifo (string pathname, uint mode);
2307
2308                 public static int mkfifo (string pathname, FilePermissions mode)
2309                 {
2310                         uint _mode = NativeConvert.FromFilePermissions (mode);
2311                         return sys_mkfifo (pathname, _mode);
2312                 }
2313
2314                 #endregion
2315
2316                 #region <sys/stat.h> Declarations
2317                 //
2318                 // <sys/statvfs.h>
2319                 //
2320
2321                 [DllImport (MPH, SetLastError=true,
2322                                 EntryPoint="Mono_Posix_Syscall_statvfs")]
2323                 public static extern int statvfs (string path, out Statvfs buf);
2324
2325                 [DllImport (MPH, SetLastError=true,
2326                                 EntryPoint="Mono_Posix_Syscall_fstatvfs")]
2327                 public static extern int fstatvfs (int fd, out Statvfs buf);
2328
2329                 #endregion
2330
2331                 #region <sys/time.h> Declarations
2332                 //
2333                 // <sys/time.h>
2334                 //
2335                 // TODO: adjtime(), getitimer(2), setitimer(2), lutimes(), futimes()
2336
2337                 [DllImport (MPH, SetLastError=true, 
2338                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2339                 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
2340
2341                 [DllImport (MPH, SetLastError=true,
2342                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2343                 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
2344
2345                 public static int gettimeofday (out Timeval tv)
2346                 {
2347                         return gettimeofday (out tv, IntPtr.Zero);
2348                 }
2349
2350                 [DllImport (MPH, SetLastError=true,
2351                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2352                 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
2353
2354                 public static int gettimeofday (out Timezone tz)
2355                 {
2356                         return gettimeofday (IntPtr.Zero, out tz);
2357                 }
2358
2359                 [DllImport (MPH, SetLastError=true,
2360                                 EntryPoint="Mono_Posix_Syscall_settimeofday")]
2361                 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
2362
2363                 [DllImport (MPH, SetLastError=true,
2364                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
2365                 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
2366
2367                 public static int settimeofday (ref Timeval tv)
2368                 {
2369                         return settimeofday (ref tv, IntPtr.Zero);
2370                 }
2371
2372                 [DllImport (MPH, SetLastError=true, 
2373                                 EntryPoint="Mono_Posix_Syscall_utimes")]
2374                 public static extern int utimes (string filename, ref Timeval tvp);
2375
2376                 #endregion
2377
2378                 //
2379                 // <sys/timeb.h>
2380                 //
2381
2382                 // TODO: ftime(3)
2383
2384                 //
2385                 // <sys/times.h>
2386                 //
2387
2388                 // TODO: times(2)
2389
2390                 //
2391                 // <sys/utsname.h>
2392                 //
2393
2394                 // TODO: uname(2)
2395
2396                 #region <sys/wait.h> Declarations
2397                 //
2398                 // <sys/wait.h>
2399                 //
2400
2401                 // wait(2)
2402                 //    pid_t wait(int *status);
2403                 [DllImport (LIBC, SetLastError=true)]
2404                 public static extern int wait (out int status);
2405
2406                 // waitpid(2)
2407                 //    pid_t waitpid(pid_t pid, int *status, int options);
2408                 [DllImport (LIBC, SetLastError=true)]
2409                 private static extern int waitpid (int pid, out int status, int options);
2410
2411                 public static int waitpid (int pid, out int status, WaitOptions options)
2412                 {
2413                         int _options = NativeConvert.FromWaitOptions (options);
2414                         return waitpid (pid, out status, _options);
2415                 }
2416
2417                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
2418                 private static extern int _WIFEXITED (int status);
2419
2420                 public static bool WIFEXITED (int status)
2421                 {
2422                         return _WIFEXITED (status) != 0;
2423                 }
2424
2425                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
2426                 public static extern int WEXITSTATUS (int status);
2427
2428                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
2429                 private static extern int _WIFSIGNALED (int status);
2430
2431                 public static bool WIFSIGNALED (int status)
2432                 {
2433                         return _WIFSIGNALED (status) != 0;
2434                 }
2435
2436                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
2437                 private static extern int _WTERMSIG (int status);
2438
2439                 public static Signum WTERMSIG (int status)
2440                 {
2441                         int r = _WTERMSIG (status);
2442                         return NativeConvert.ToSignum (r);
2443                 }
2444
2445                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
2446                 private static extern int _WIFSTOPPED (int status);
2447
2448                 public static bool WIFSTOPPED (int status)
2449                 {
2450                         return _WIFSTOPPED (status) != 0;
2451                 }
2452
2453                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
2454                 private static extern int _WSTOPSIG (int status);
2455
2456                 public static Signum WSTOPSIG (int status)
2457                 {
2458                         int r = _WSTOPSIG (status);
2459                         return NativeConvert.ToSignum (r);
2460                 }
2461
2462                 //
2463                 // <termios.h>
2464                 //
2465
2466                 #endregion
2467
2468                 #region <syslog.h> Declarations
2469                 //
2470                 // <syslog.h>
2471                 //
2472
2473                 [DllImport (LIBC, EntryPoint="openlog")]
2474                 private static extern void sys_openlog (IntPtr ident, int option, int facility);
2475
2476                 public static void openlog (IntPtr ident, SyslogOptions option, 
2477                                 SyslogFacility defaultFacility)
2478                 {
2479                         int _option   = NativeConvert.FromSyslogOptions (option);
2480                         int _facility = NativeConvert.FromSyslogFacility (defaultFacility);
2481
2482                         sys_openlog (ident, _option, _facility);
2483                 }
2484
2485                 [DllImport (LIBC, EntryPoint="syslog")]
2486                 private static extern void sys_syslog (int priority, string message);
2487
2488                 public static void syslog (SyslogFacility facility, SyslogLevel level, string message)
2489                 {
2490                         int _facility = NativeConvert.FromSyslogFacility (facility);
2491                         int _level = NativeConvert.FromSyslogLevel (level);
2492                         sys_syslog (_facility | _level, GetSyslogMessage (message));
2493                 }
2494
2495                 public static void syslog (SyslogLevel level, string message)
2496                 {
2497                         int _level = NativeConvert.FromSyslogLevel (level);
2498                         sys_syslog (_level, GetSyslogMessage (message));
2499                 }
2500
2501                 private static string GetSyslogMessage (string message)
2502                 {
2503                         return UnixMarshal.EscapeFormatString (message, new char[]{'m'});
2504                 }
2505
2506                 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2507                                 "Use syslog(SyslogFacility, SyslogLevel, string) instead.")]
2508                 public static void syslog (SyslogFacility facility, SyslogLevel level, 
2509                                 string format, params object[] parameters)
2510                 {
2511                         int _facility = NativeConvert.FromSyslogFacility (facility);
2512                         int _level = NativeConvert.FromSyslogLevel (level);
2513
2514                         object[] _parameters = new object[checked(parameters.Length+2)];
2515                         _parameters [0] = _facility | _level;
2516                         _parameters [1] = format;
2517                         Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2518                         XPrintfFunctions.syslog (_parameters);
2519                 }
2520
2521                 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
2522                                 "Use syslog(SyslogLevel, string) instead.")]
2523                 public static void syslog (SyslogLevel level, string format, 
2524                                 params object[] parameters)
2525                 {
2526                         int _level = NativeConvert.FromSyslogLevel (level);
2527
2528                         object[] _parameters = new object[checked(parameters.Length+2)];
2529                         _parameters [0] = _level;
2530                         _parameters [1] = format;
2531                         Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
2532                         XPrintfFunctions.syslog (_parameters);
2533                 }
2534
2535                 [DllImport (LIBC)]
2536                 public static extern void closelog ();
2537
2538                 [DllImport (LIBC, EntryPoint="setlogmask")]
2539                 private static extern int sys_setlogmask (int mask);
2540
2541                 public static int setlogmask (SyslogLevel mask)
2542                 {
2543                         int _mask = NativeConvert.FromSyslogLevel (mask);
2544                         return sys_setlogmask (_mask);
2545                 }
2546
2547                 #endregion
2548
2549                 #region <time.h> Declarations
2550
2551                 //
2552                 // <time.h>
2553                 //
2554
2555                 // stime(2)
2556                 //    int stime(time_t *t);
2557                 [DllImport (MPH, SetLastError=true,
2558                                 EntryPoint="Mono_Posix_Syscall_stime")]
2559                 public static extern int stime (ref long t);
2560
2561                 // time(2)
2562                 //    time_t time(time_t *t);
2563                 [DllImport (MPH, SetLastError=true,
2564                                 EntryPoint="Mono_Posix_Syscall_time")]
2565                 public static extern long time (out long t);
2566
2567                 //
2568                 // <ulimit.h>
2569                 //
2570
2571                 // TODO: ulimit(3)
2572
2573                 #endregion
2574
2575                 #region <unistd.h> Declarations
2576                 //
2577                 // <unistd.h>
2578                 //
2579                 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
2580                 //       other TODOs listed below.
2581
2582                 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
2583                 private static extern int sys_access (string pathname, int mode);
2584
2585                 public static int access (string pathname, AccessModes mode)
2586                 {
2587                         int _mode = NativeConvert.FromAccessModes (mode);
2588                         return sys_access (pathname, _mode);
2589                 }
2590
2591                 // lseek(2)
2592                 //    off_t lseek(int filedes, off_t offset, int whence);
2593                 [DllImport (MPH, SetLastError=true, 
2594                                 EntryPoint="Mono_Posix_Syscall_lseek")]
2595                 private static extern long sys_lseek (int fd, long offset, int whence);
2596
2597                 public static long lseek (int fd, long offset, SeekFlags whence)
2598                 {
2599                         short _whence = NativeConvert.FromSeekFlags (whence);
2600                         return sys_lseek (fd, offset, _whence);
2601                 }
2602
2603     [DllImport (LIBC, SetLastError=true)]
2604                 public static extern int close (int fd);
2605
2606                 // read(2)
2607                 //    ssize_t read(int fd, void *buf, size_t count);
2608                 [DllImport (MPH, SetLastError=true, 
2609                                 EntryPoint="Mono_Posix_Syscall_read")]
2610                 public static extern long read (int fd, IntPtr buf, ulong count);
2611
2612                 public static unsafe long read (int fd, void *buf, ulong count)
2613                 {
2614                         return read (fd, (IntPtr) buf, count);
2615                 }
2616
2617                 // write(2)
2618                 //    ssize_t write(int fd, const void *buf, size_t count);
2619                 [DllImport (MPH, SetLastError=true, 
2620                                 EntryPoint="Mono_Posix_Syscall_write")]
2621                 public static extern long write (int fd, IntPtr buf, ulong count);
2622
2623                 public static unsafe long write (int fd, void *buf, ulong count)
2624                 {
2625                         return write (fd, (IntPtr) buf, count);
2626                 }
2627
2628                 // pread(2)
2629                 //    ssize_t pread(int fd, void *buf, size_t count, off_t offset);
2630                 [DllImport (MPH, SetLastError=true, 
2631                                 EntryPoint="Mono_Posix_Syscall_pread")]
2632                 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
2633
2634                 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
2635                 {
2636                         return pread (fd, (IntPtr) buf, count, offset);
2637                 }
2638
2639                 // pwrite(2)
2640                 //    ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
2641                 [DllImport (MPH, SetLastError=true, 
2642                                 EntryPoint="Mono_Posix_Syscall_pwrite")]
2643                 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
2644
2645                 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
2646                 {
2647                         return pwrite (fd, (IntPtr) buf, count, offset);
2648                 }
2649
2650                 [DllImport (MPH, SetLastError=true, 
2651                                 EntryPoint="Mono_Posix_Syscall_pipe")]
2652                 public static extern int pipe (out int reading, out int writing);
2653
2654                 public static int pipe (int[] filedes)
2655                 {
2656                         if (filedes == null || filedes.Length != 2) {
2657                                 // TODO: set errno
2658                                 return -1;
2659                         }
2660                         int reading, writing;
2661                         int r = pipe (out reading, out writing);
2662                         filedes[0] = reading;
2663                         filedes[1] = writing;
2664                         return r;
2665                 }
2666
2667                 [DllImport (LIBC, SetLastError=true)]
2668                 public static extern uint alarm (uint seconds);
2669
2670                 [DllImport (LIBC, SetLastError=true)]
2671                 public static extern uint sleep (uint seconds);
2672
2673                 [DllImport (LIBC, SetLastError=true)]
2674                 public static extern uint ualarm (uint usecs, uint interval);
2675
2676                 [DllImport (LIBC, SetLastError=true)]
2677                 public static extern int pause ();
2678
2679                 // chown(2)
2680                 //    int chown(const char *path, uid_t owner, gid_t group);
2681                 [DllImport (LIBC, SetLastError=true)]
2682                 public static extern int chown (string path, uint owner, uint group);
2683
2684                 // fchown(2)
2685                 //    int fchown(int fd, uid_t owner, gid_t group);
2686                 [DllImport (LIBC, SetLastError=true)]
2687                 public static extern int fchown (int fd, uint owner, uint group);
2688
2689                 // lchown(2)
2690                 //    int lchown(const char *path, uid_t owner, gid_t group);
2691                 [DllImport (LIBC, SetLastError=true)]
2692                 public static extern int lchown (string path, uint owner, uint group);
2693
2694                 [DllImport (LIBC, SetLastError=true)]
2695                 public static extern int chdir (string path);
2696
2697                 [DllImport (LIBC, SetLastError=true)]
2698                 public static extern int fchdir (int fd);
2699
2700                 // getcwd(3)
2701                 //    char *getcwd(char *buf, size_t size);
2702                 [DllImport (MPH, SetLastError=true,
2703                                 EntryPoint="Mono_Posix_Syscall_getcwd")]
2704                 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
2705
2706                 public static StringBuilder getcwd (StringBuilder buf)
2707                 {
2708                         getcwd (buf, (ulong) buf.Capacity);
2709                         return buf;
2710                 }
2711
2712                 // getwd(2) is deprecated; don't expose it.
2713
2714                 [DllImport (LIBC, SetLastError=true)]
2715                 public static extern int dup (int fd);
2716
2717                 [DllImport (LIBC, SetLastError=true)]
2718                 public static extern int dup2 (int fd, int fd2);
2719
2720                 // TODO: does Mono marshal arrays properly?
2721                 [DllImport (LIBC, SetLastError=true)]
2722                 public static extern int execve (string path, string[] argv, string[] envp);
2723
2724                 [DllImport (LIBC, SetLastError=true)]
2725                 public static extern int fexecve (int fd, string[] argv, string[] envp);
2726
2727                 [DllImport (LIBC, SetLastError=true)]
2728                 public static extern int execv (string path, string[] argv);
2729
2730                 // TODO: execle, execl, execlp
2731                 [DllImport (LIBC, SetLastError=true)]
2732                 public static extern int execvp (string path, string[] argv);
2733
2734                 [DllImport (LIBC, SetLastError=true)]
2735                 public static extern int nice (int inc);
2736
2737                 [DllImport (LIBC, SetLastError=true)]
2738                 [CLSCompliant (false)]
2739                 public static extern int _exit (int status);
2740
2741                 [DllImport (MPH, SetLastError=true,
2742                                 EntryPoint="Mono_Posix_Syscall_fpathconf")]
2743                 public static extern long fpathconf (int filedes, PathConf name);
2744
2745                 [DllImport (MPH, SetLastError=true,
2746                                 EntryPoint="Mono_Posix_Syscall_pathconf")]
2747                 public static extern long pathconf (string path, PathConf name);
2748
2749                 [DllImport (MPH, SetLastError=true,
2750                                 EntryPoint="Mono_Posix_Syscall_sysconf")]
2751                 public static extern long sysconf (SysConf name);
2752
2753                 // confstr(3)
2754                 //    size_t confstr(int name, char *buf, size_t len);
2755                 [DllImport (MPH, SetLastError=true,
2756                                 EntryPoint="Mono_Posix_Syscall_confstr")]
2757                 public static extern ulong confstr (ConfStr name, [Out] StringBuilder buf, ulong len);
2758
2759                 // getpid(2)
2760                 //    pid_t getpid(void);
2761                 [DllImport (LIBC, SetLastError=true)]
2762                 public static extern int getpid ();
2763
2764                 // getppid(2)
2765                 //    pid_t getppid(void);
2766                 [DllImport (LIBC, SetLastError=true)]
2767                 public static extern int getppid ();
2768
2769                 // setpgid(2)
2770                 //    int setpgid(pid_t pid, pid_t pgid);
2771                 [DllImport (LIBC, SetLastError=true)]
2772                 public static extern int setpgid (int pid, int pgid);
2773
2774                 // getpgid(2)
2775                 //    pid_t getpgid(pid_t pid);
2776                 [DllImport (LIBC, SetLastError=true)]
2777                 public static extern int getpgid (int pid);
2778
2779                 [DllImport (LIBC, SetLastError=true)]
2780                 public static extern int setpgrp ();
2781
2782                 // getpgrp(2)
2783                 //    pid_t getpgrp(void);
2784                 [DllImport (LIBC, SetLastError=true)]
2785                 public static extern int getpgrp ();
2786
2787                 // setsid(2)
2788                 //    pid_t setsid(void);
2789                 [DllImport (LIBC, SetLastError=true)]
2790                 public static extern int setsid ();
2791
2792                 // getsid(2)
2793                 //    pid_t getsid(pid_t pid);
2794                 [DllImport (LIBC, SetLastError=true)]
2795                 public static extern int getsid (int pid);
2796
2797                 // getuid(2)
2798                 //    uid_t getuid(void);
2799                 [DllImport (LIBC, SetLastError=true)]
2800                 public static extern uint getuid ();
2801
2802                 // geteuid(2)
2803                 //    uid_t geteuid(void);
2804                 [DllImport (LIBC, SetLastError=true)]
2805                 public static extern uint geteuid ();
2806
2807                 // getgid(2)
2808                 //    gid_t getgid(void);
2809                 [DllImport (LIBC, SetLastError=true)]
2810                 public static extern uint getgid ();
2811
2812                 // getegid(2)
2813                 //    gid_t getgid(void);
2814                 [DllImport (LIBC, SetLastError=true)]
2815                 public static extern uint getegid ();
2816
2817                 // getgroups(2)
2818                 //    int getgroups(int size, gid_t list[]);
2819                 [DllImport (LIBC, SetLastError=true)]
2820                 public static extern int getgroups (int size, uint[] list);
2821
2822                 public static int getgroups (uint[] list)
2823                 {
2824                         return getgroups (list.Length, list);
2825                 }
2826
2827                 // setuid(2)
2828                 //    int setuid(uid_t uid);
2829                 [DllImport (LIBC, SetLastError=true)]
2830                 public static extern int setuid (uint uid);
2831
2832                 // setreuid(2)
2833                 //    int setreuid(uid_t ruid, uid_t euid);
2834                 [DllImport (LIBC, SetLastError=true)]
2835                 public static extern int setreuid (uint ruid, uint euid);
2836
2837                 // setregid(2)
2838                 //    int setregid(gid_t ruid, gid_t euid);
2839                 [DllImport (LIBC, SetLastError=true)]
2840                 public static extern int setregid (uint rgid, uint egid);
2841
2842                 // seteuid(2)
2843                 //    int seteuid(uid_t euid);
2844                 [DllImport (LIBC, SetLastError=true)]
2845                 public static extern int seteuid (uint euid);
2846
2847                 // setegid(2)
2848                 //    int setegid(gid_t euid);
2849                 [DllImport (LIBC, SetLastError=true)]
2850                 public static extern int setegid (uint uid);
2851
2852                 // setgid(2)
2853                 //    int setgid(gid_t gid);
2854                 [DllImport (LIBC, SetLastError=true)]
2855                 public static extern int setgid (uint gid);
2856
2857                 // getresuid(2)
2858                 //    int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
2859                 [DllImport (LIBC, SetLastError=true)]
2860                 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
2861
2862                 // getresgid(2)
2863                 //    int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
2864                 [DllImport (LIBC, SetLastError=true)]
2865                 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
2866
2867                 // setresuid(2)
2868                 //    int setresuid(uid_t ruid, uid_t euid, uid_t suid);
2869                 [DllImport (LIBC, SetLastError=true)]
2870                 public static extern int setresuid (uint ruid, uint euid, uint suid);
2871
2872                 // setresgid(2)
2873                 //    int setresgid(gid_t ruid, gid_t euid, gid_t suid);
2874                 [DllImport (LIBC, SetLastError=true)]
2875                 public static extern int setresgid (uint rgid, uint egid, uint sgid);
2876
2877                 // fork(2)
2878                 //    pid_t fork(void);
2879                 [DllImport (LIBC, SetLastError=true)]
2880                 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " + 
2881                                 "shutdown code.\nUse System.Diagnostics.Process instead")]
2882                 private static extern int fork ();
2883
2884                 // vfork(2)
2885                 //    pid_t vfork(void);
2886                 [DllImport (LIBC, SetLastError=true)]
2887                 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " + 
2888                                 "shutdown code.\nUse System.Diagnostics.Process instead")]
2889                 private static extern int vfork ();
2890
2891                 private static object tty_lock = new object ();
2892
2893                 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
2894                 private static extern IntPtr sys_ttyname (int fd);
2895
2896                 public static string ttyname (int fd)
2897                 {
2898                         lock (tty_lock) {
2899                                 IntPtr r = sys_ttyname (fd);
2900                                 return UnixMarshal.PtrToString (r);
2901                         }
2902                 }
2903
2904                 // ttyname_r(3)
2905                 //    int ttyname_r(int fd, char *buf, size_t buflen);
2906                 [DllImport (MPH, SetLastError=true,
2907                                 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
2908                 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
2909
2910                 public static int ttyname_r (int fd, StringBuilder buf)
2911                 {
2912                         return ttyname_r (fd, buf, (ulong) buf.Capacity);
2913                 }
2914
2915                 [DllImport (LIBC, EntryPoint="isatty")]
2916                 private static extern int sys_isatty (int fd);
2917
2918                 public static bool isatty (int fd)
2919                 {
2920                         return sys_isatty (fd) == 1;
2921                 }
2922
2923                 [DllImport (LIBC, SetLastError=true)]
2924                 public static extern int link (string oldpath, string newpath);
2925
2926                 [DllImport (LIBC, SetLastError=true)]
2927                 public static extern int symlink (string oldpath, string newpath);
2928
2929                 // readlink(2)
2930                 //    int readlink(const char *path, char *buf, size_t bufsize);
2931                 [DllImport (MPH, SetLastError=true,
2932                                 EntryPoint="Mono_Posix_Syscall_readlink")]
2933                 public static extern int readlink (string path, [Out] StringBuilder buf, ulong bufsiz);
2934
2935                 public static int readlink (string path, [Out] StringBuilder buf)
2936                 {
2937                         return readlink (path, buf, (ulong) buf.Capacity);
2938                 }
2939
2940                 [DllImport (LIBC, SetLastError=true)]
2941                 public static extern int unlink (string pathname);
2942
2943                 [DllImport (LIBC, SetLastError=true)]
2944                 public static extern int rmdir (string pathname);
2945
2946                 // tcgetpgrp(3)
2947                 //    pid_t tcgetpgrp(int fd);
2948                 [DllImport (LIBC, SetLastError=true)]
2949                 public static extern int tcgetpgrp (int fd);
2950
2951                 // tcsetpgrp(3)
2952                 //    int tcsetpgrp(int fd, pid_t pgrp);
2953                 [DllImport (LIBC, SetLastError=true)]
2954                 public static extern int tcsetpgrp (int fd, int pgrp);
2955
2956                 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
2957                 private static extern IntPtr sys_getlogin ();
2958
2959                 public static string getlogin ()
2960                 {
2961                         lock (getlogin_lock) {
2962                                 IntPtr r = sys_getlogin ();
2963                                 return UnixMarshal.PtrToString (r);
2964                         }
2965                 }
2966
2967                 // getlogin_r(3)
2968                 //    int getlogin_r(char *buf, size_t bufsize);
2969                 [DllImport (MPH, SetLastError=true,
2970                                 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
2971                 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
2972
2973                 public static int getlogin_r (StringBuilder name)
2974                 {
2975                         return getlogin_r (name, (ulong) name.Capacity);
2976                 }
2977
2978                 [DllImport (LIBC, SetLastError=true)]
2979                 public static extern int setlogin (string name);
2980
2981                 // gethostname(2)
2982                 //    int gethostname(char *name, size_t len);
2983                 [DllImport (MPH, SetLastError=true,
2984                                 EntryPoint="Mono_Posix_Syscall_gethostname")]
2985                 public static extern int gethostname ([Out] StringBuilder name, ulong len);
2986
2987                 public static int gethostname (StringBuilder name)
2988                 {
2989                         return gethostname (name, (ulong) name.Capacity);
2990                 }
2991
2992                 // sethostname(2)
2993                 //    int gethostname(const char *name, size_t len);
2994                 [DllImport (MPH, SetLastError=true,
2995                                 EntryPoint="Mono_Posix_Syscall_sethostname")]
2996                 public static extern int sethostname (string name, ulong len);
2997
2998                 public static int sethostname (string name)
2999                 {
3000                         return sethostname (name, (ulong) name.Length);
3001                 }
3002
3003                 [DllImport (MPH, SetLastError=true,
3004                                 EntryPoint="Mono_Posix_Syscall_gethostid")]
3005                 public static extern long gethostid ();
3006
3007                 [DllImport (MPH, SetLastError=true,
3008                                 EntryPoint="Mono_Posix_Syscall_sethostid")]
3009                 public static extern int sethostid (long hostid);
3010
3011                 // getdomainname(2)
3012                 //    int getdomainname(char *name, size_t len);
3013                 [DllImport (MPH, SetLastError=true,
3014                                 EntryPoint="Mono_Posix_Syscall_getdomainname")]
3015                 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
3016
3017                 public static int getdomainname (StringBuilder name)
3018                 {
3019                         return getdomainname (name, (ulong) name.Capacity);
3020                 }
3021
3022                 // setdomainname(2)
3023                 //    int setdomainname(const char *name, size_t len);
3024                 [DllImport (MPH, SetLastError=true,
3025                                 EntryPoint="Mono_Posix_Syscall_setdomainname")]
3026                 public static extern int setdomainname (string name, ulong len);
3027
3028                 public static int setdomainname (string name)
3029                 {
3030                         return setdomainname (name, (ulong) name.Length);
3031                 }
3032
3033                 [DllImport (LIBC, SetLastError=true)]
3034                 public static extern int vhangup ();
3035
3036                 // Revoke doesn't appear to be POSIX.  Include it?
3037                 [DllImport (LIBC, SetLastError=true)]
3038                 public static extern int revoke (string file);
3039
3040                 // TODO: profil?  It's not POSIX.
3041
3042                 [DllImport (LIBC, SetLastError=true)]
3043                 public static extern int acct (string filename);
3044
3045                 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
3046                 private static extern IntPtr sys_getusershell ();
3047
3048                 internal static object usershell_lock = new object ();
3049
3050                 public static string getusershell ()
3051                 {
3052                         lock (usershell_lock) {
3053                                 IntPtr r = sys_getusershell ();
3054                                 return UnixMarshal.PtrToString (r);
3055                         }
3056                 }
3057
3058                 [DllImport (LIBC, SetLastError=true, EntryPoint="setusershell")]
3059                 private static extern void sys_setusershell ();
3060
3061                 public static void setusershell ()
3062                 {
3063                         lock (usershell_lock) {
3064                                 sys_setusershell ();
3065                         }
3066                 }
3067
3068                 [DllImport (LIBC, SetLastError=true, EntryPoint="endusershell")]
3069                 private static extern void sys_endusershell ();
3070
3071                 public static void endusershell ()
3072                 {
3073                         lock (usershell_lock) {
3074                                 sys_endusershell ();
3075                         }
3076                 }
3077
3078                 [DllImport (LIBC, SetLastError=true)]
3079                 private static extern int daemon (int nochdir, int noclose);
3080
3081                 public static int daemon (bool nochdir, bool noclose)
3082                 {
3083                         return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
3084                 }
3085
3086                 [DllImport (LIBC, SetLastError=true)]
3087                 public static extern int chroot (string path);
3088
3089                 // skipping getpass(3) as the man page states:
3090                 //   This function is obsolete.  Do not use it.
3091
3092                 [DllImport (LIBC, SetLastError=true)]
3093                 public static extern int fsync (int fd);
3094
3095                 [DllImport (LIBC, SetLastError=true)]
3096                 public static extern int fdatasync (int fd);
3097
3098                 [DllImport (LIBC, SetLastError=true)]
3099                 public static extern void sync ();
3100
3101                 [DllImport (LIBC, SetLastError=true)]
3102                 [Obsolete ("Dropped in POSIX 1003.1-2001.  " +
3103                                 "Use Unistd.sysconf (SysConf._SC_PAGESIZE).")]
3104                 public static extern int getpagesize ();
3105
3106                 // truncate(2)
3107                 //    int truncate(const char *path, off_t length);
3108                 [DllImport (MPH, SetLastError=true, 
3109                                 EntryPoint="Mono_Posix_Syscall_truncate")]
3110                 public static extern int truncate (string path, long length);
3111
3112                 // ftruncate(2)
3113                 //    int ftruncate(int fd, off_t length);
3114                 [DllImport (MPH, SetLastError=true, 
3115                                 EntryPoint="Mono_Posix_Syscall_ftruncate")]
3116                 public static extern int ftruncate (int fd, long length);
3117
3118                 [DllImport (LIBC, SetLastError=true)]
3119                 public static extern int getdtablesize ();
3120
3121                 [DllImport (LIBC, SetLastError=true)]
3122                 public static extern int brk (IntPtr end_data_segment);
3123
3124                 [DllImport (LIBC, SetLastError=true)]
3125                 public static extern IntPtr sbrk (IntPtr increment);
3126
3127                 // TODO: syscall(2)?
3128                 // Probably safer to skip entirely.
3129
3130                 // lockf(3)
3131                 //    int lockf(int fd, int cmd, off_t len);
3132                 [DllImport (MPH, SetLastError=true, 
3133                                 EntryPoint="Mono_Posix_Syscall_lockf")]
3134                 public static extern int lockf (int fd, LockfCommand cmd, long len);
3135
3136                 internal static object crypt_lock = new object ();
3137
3138                 [DllImport (CRYPT, SetLastError=true, EntryPoint="crypt")]
3139                 private static extern IntPtr sys_crypt (string key, string salt);
3140
3141                 public static string crypt (string key, string salt)
3142                 {
3143                         lock (crypt_lock) {
3144                                 IntPtr r = sys_crypt (key, salt);
3145                                 return UnixMarshal.PtrToString (r);
3146                         }
3147                 }
3148
3149                 internal static object encrypt_lock = new object ();
3150
3151                 [DllImport (CRYPT, SetLastError=true, EntryPoint="encrypt")]
3152                 private static extern void sys_encrypt ([In, Out] byte[] block, int edflag);
3153
3154                 public static void encrypt (byte[] block, bool decode)
3155                 {
3156                         if (block.Length < 64)
3157                                 throw new ArgumentOutOfRangeException ("block", "Must refer to at least 64 bytes");
3158                         lock (encrypt_lock) {
3159                                 sys_encrypt (block, decode ? 1 : 0);
3160                         }
3161                 }
3162
3163                 // swab(3)
3164                 //    void swab(const void *from, void *to, ssize_t n);
3165                 [DllImport (MPH, SetLastError=true, 
3166                                 EntryPoint="Mono_Posix_Syscall_swab")]
3167                 public static extern void swab (IntPtr from, IntPtr to, long n);
3168
3169                 public static unsafe void swab (void* from, void* to, long n)
3170                 {
3171                         swab ((IntPtr) from, (IntPtr) to, n);
3172                 }
3173
3174                 #endregion
3175
3176                 #region <utime.h> Declarations
3177                 //
3178                 // <utime.h>  -- COMPLETE
3179                 //
3180
3181                 [DllImport (MPH, SetLastError=true, 
3182                                 EntryPoint="Mono_Posix_Syscall_utime")]
3183                 private static extern int sys_utime (string filename, ref Utimbuf buf, int use_buf);
3184
3185                 public static int utime (string filename, ref Utimbuf buf)
3186                 {
3187                         return sys_utime (filename, ref buf, 1);
3188                 }
3189
3190                 public static int utime (string filename)
3191                 {
3192                         Utimbuf buf = new Utimbuf ();
3193                         return sys_utime (filename, ref buf, 0);
3194                 }
3195                 #endregion
3196         }
3197
3198         #endregion
3199 }
3200
3201 // vim: noexpandtab