harddog_user.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include "os.h"
  9. #include "user.h"
  10. struct dog_data {
  11. int stdin;
  12. int stdout;
  13. int close_me[2];
  14. };
  15. static void pre_exec(void *d)
  16. {
  17. struct dog_data *data = d;
  18. dup2(data->stdin, 0);
  19. dup2(data->stdout, 1);
  20. dup2(data->stdout, 2);
  21. close(data->stdin);
  22. close(data->stdout);
  23. close(data->close_me[0]);
  24. close(data->close_me[1]);
  25. }
  26. int start_watchdog(int *in_fd_ret, int *out_fd_ret, char *sock)
  27. {
  28. struct dog_data data;
  29. int in_fds[2], out_fds[2], pid, n, err;
  30. char pid_buf[sizeof("nnnnn\0")], c;
  31. char *pid_args[] = { "/usr/bin/uml_watchdog", "-pid", pid_buf, NULL };
  32. char *mconsole_args[] = { "/usr/bin/uml_watchdog", "-mconsole", NULL,
  33. NULL };
  34. char **args = NULL;
  35. err = os_pipe(in_fds, 1, 0);
  36. if (err < 0) {
  37. printk("harddog_open - os_pipe failed, err = %d\n", -err);
  38. goto out;
  39. }
  40. err = os_pipe(out_fds, 1, 0);
  41. if (err < 0) {
  42. printk("harddog_open - os_pipe failed, err = %d\n", -err);
  43. goto out_close_in;
  44. }
  45. data.stdin = out_fds[0];
  46. data.stdout = in_fds[1];
  47. data.close_me[0] = out_fds[1];
  48. data.close_me[1] = in_fds[0];
  49. if (sock != NULL) {
  50. mconsole_args[2] = sock;
  51. args = mconsole_args;
  52. }
  53. else {
  54. /* XXX The os_getpid() is not SMP correct */
  55. sprintf(pid_buf, "%d", os_getpid());
  56. args = pid_args;
  57. }
  58. pid = run_helper(pre_exec, &data, args);
  59. close(out_fds[0]);
  60. close(in_fds[1]);
  61. if (pid < 0) {
  62. err = -pid;
  63. printk("harddog_open - run_helper failed, errno = %d\n", -err);
  64. goto out_close_out;
  65. }
  66. n = read(in_fds[0], &c, sizeof(c));
  67. if (n == 0) {
  68. printk("harddog_open - EOF on watchdog pipe\n");
  69. helper_wait(pid);
  70. err = -EIO;
  71. goto out_close_out;
  72. }
  73. else if (n < 0) {
  74. printk("harddog_open - read of watchdog pipe failed, "
  75. "err = %d\n", errno);
  76. helper_wait(pid);
  77. err = n;
  78. goto out_close_out;
  79. }
  80. *in_fd_ret = in_fds[0];
  81. *out_fd_ret = out_fds[1];
  82. return 0;
  83. out_close_in:
  84. close(in_fds[0]);
  85. close(in_fds[1]);
  86. out_close_out:
  87. close(out_fds[0]);
  88. close(out_fds[1]);
  89. out:
  90. return err;
  91. }
  92. void stop_watchdog(int in_fd, int out_fd)
  93. {
  94. close(in_fd);
  95. close(out_fd);
  96. }
  97. int ping_watchdog(int fd)
  98. {
  99. int n;
  100. char c = '\n';
  101. n = write(fd, &c, sizeof(c));
  102. if (n != sizeof(c)) {
  103. printk("ping_watchdog - write failed, ret = %d, err = %d\n",
  104. n, errno);
  105. if (n < 0)
  106. return n;
  107. return -EIO;
  108. }
  109. return 1;
  110. }