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