io-read.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <gnu/syscall.h>
  21. struct mach_msg_int_pointer
  22. {
  23. mach_msg_header_t header;
  24. mach_msg_type_t type_one; int one;
  25. mach_msg_type_long_t type_two; union {char *two; char pointer[2048];};
  26. };
  27. kern_return_t
  28. __io_read (io_t io, data_t *data, mach_msg_type_number_t *read, loff_t offset, vm_size_t size)
  29. {
  30. union message
  31. {
  32. struct mach_msg_loff_int request;
  33. struct mach_msg_int_pointer reply;
  34. };
  35. union message message = {0};
  36. message.request.header.msgh_size = sizeof (message.request);
  37. message.request.type_one = mach_msg_type_int64;
  38. message.request.one = offset;
  39. message.request.type_two = mach_msg_type_int32;
  40. message.request.two = size;
  41. kern_return_t result = __syscall_put (io, SYS__io_read,
  42. &message.request.header,
  43. sizeof (message.reply));
  44. if (result == KERN_SUCCESS)
  45. {
  46. *read = message.reply.type_two.msgtl_number;
  47. *data = message.reply.pointer;
  48. }
  49. else
  50. {
  51. *read = 0;
  52. *data = 0;
  53. }
  54. return result;
  55. }