ksft.h 1006 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #if !defined(__NET_KSFT_H__)
  3. #define __NET_KSFT_H__
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. static inline void ksft_ready(void)
  8. {
  9. const char msg[7] = "ready\n";
  10. char *env_str;
  11. int fd;
  12. env_str = getenv("KSFT_READY_FD");
  13. if (env_str) {
  14. fd = atoi(env_str);
  15. if (!fd) {
  16. fprintf(stderr, "invalid KSFT_READY_FD = '%s'\n",
  17. env_str);
  18. return;
  19. }
  20. } else {
  21. fd = STDOUT_FILENO;
  22. }
  23. if (write(fd, msg, sizeof(msg)) < 0)
  24. perror("write()");
  25. if (fd != STDOUT_FILENO)
  26. close(fd);
  27. }
  28. static inline void ksft_wait(void)
  29. {
  30. char *env_str;
  31. char byte;
  32. int fd;
  33. env_str = getenv("KSFT_WAIT_FD");
  34. if (env_str) {
  35. fd = atoi(env_str);
  36. if (!fd) {
  37. fprintf(stderr, "invalid KSFT_WAIT_FD = '%s'\n",
  38. env_str);
  39. return;
  40. }
  41. } else {
  42. /* Not running in KSFT env, wait for input from STDIN instead */
  43. fd = STDIN_FILENO;
  44. }
  45. if (read(fd, &byte, sizeof(byte)) < 0)
  46. perror("read()");
  47. if (fd != STDIN_FILENO)
  48. close(fd);
  49. }
  50. #endif