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