ubd_user.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016 Anton Ivanov (aivanov@brocade.com)
  4. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  5. * Copyright (C) 2001 Ridgerun,Inc (glonnon@ridgerun.com)
  6. */
  7. #include <stddef.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <sched.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <netinet/in.h>
  14. #include <sys/time.h>
  15. #include <sys/socket.h>
  16. #include <sys/mman.h>
  17. #include <sys/param.h>
  18. #include <endian.h>
  19. #include <byteswap.h>
  20. #include "ubd.h"
  21. #include <os.h>
  22. #include <poll.h>
  23. struct pollfd kernel_pollfd;
  24. int start_io_thread(unsigned long sp, int *fd_out)
  25. {
  26. int pid, fds[2], err;
  27. err = os_pipe(fds, 1, 1);
  28. if(err < 0){
  29. printk("start_io_thread - os_pipe failed, err = %d\n", -err);
  30. goto out;
  31. }
  32. kernel_fd = fds[0];
  33. kernel_pollfd.fd = kernel_fd;
  34. kernel_pollfd.events = POLLIN;
  35. *fd_out = fds[1];
  36. err = os_set_fd_block(*fd_out, 0);
  37. err = os_set_fd_block(kernel_fd, 0);
  38. if (err) {
  39. printk("start_io_thread - failed to set nonblocking I/O.\n");
  40. goto out_close;
  41. }
  42. pid = clone(io_thread, (void *) sp, CLONE_FILES | CLONE_VM, NULL);
  43. if(pid < 0){
  44. err = -errno;
  45. printk("start_io_thread - clone failed : errno = %d\n", errno);
  46. goto out_close;
  47. }
  48. return(pid);
  49. out_close:
  50. os_close_file(fds[0]);
  51. os_close_file(fds[1]);
  52. kernel_fd = -1;
  53. *fd_out = -1;
  54. out:
  55. return err;
  56. }
  57. int ubd_read_poll(int timeout)
  58. {
  59. kernel_pollfd.events = POLLIN;
  60. return poll(&kernel_pollfd, 1, timeout);
  61. }
  62. int ubd_write_poll(int timeout)
  63. {
  64. kernel_pollfd.events = POLLOUT;
  65. return poll(&kernel_pollfd, 1, timeout);
  66. }