PageRenderTime 57ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/Mono.Posix/Mono.Unix.Native/Syscall.cs

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