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