mconsole_user.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org)
  3. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. * Licensed under the GPL
  5. */
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sys/socket.h>
  10. #include <sys/uio.h>
  11. #include <sys/un.h>
  12. #include "mconsole.h"
  13. static struct mconsole_command commands[] = {
  14. /*
  15. * With uts namespaces, uts information becomes process-specific, so
  16. * we need a process context. If we try handling this in interrupt
  17. * context, we may hit an exiting process without a valid uts
  18. * namespace.
  19. */
  20. { "version", mconsole_version, MCONSOLE_PROC },
  21. { "halt", mconsole_halt, MCONSOLE_PROC },
  22. { "reboot", mconsole_reboot, MCONSOLE_PROC },
  23. { "config", mconsole_config, MCONSOLE_PROC },
  24. { "remove", mconsole_remove, MCONSOLE_PROC },
  25. { "sysrq", mconsole_sysrq, MCONSOLE_INTR },
  26. { "help", mconsole_help, MCONSOLE_INTR },
  27. { "cad", mconsole_cad, MCONSOLE_INTR },
  28. { "stop", mconsole_stop, MCONSOLE_PROC },
  29. { "go", mconsole_go, MCONSOLE_INTR },
  30. { "log", mconsole_log, MCONSOLE_INTR },
  31. { "proc", mconsole_proc, MCONSOLE_PROC },
  32. { "stack", mconsole_stack, MCONSOLE_INTR },
  33. };
  34. /* Initialized in mconsole_init, which is an initcall */
  35. char mconsole_socket_name[256];
  36. static int mconsole_reply_v0(struct mc_request *req, char *reply)
  37. {
  38. struct iovec iov;
  39. struct msghdr msg;
  40. iov.iov_base = reply;
  41. iov.iov_len = strlen(reply);
  42. msg.msg_name = &(req->origin);
  43. msg.msg_namelen = req->originlen;
  44. msg.msg_iov = &iov;
  45. msg.msg_iovlen = 1;
  46. msg.msg_control = NULL;
  47. msg.msg_controllen = 0;
  48. msg.msg_flags = 0;
  49. return sendmsg(req->originating_fd, &msg, 0);
  50. }
  51. static struct mconsole_command *mconsole_parse(struct mc_request *req)
  52. {
  53. struct mconsole_command *cmd;
  54. int i;
  55. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  56. cmd = &commands[i];
  57. if (!strncmp(req->request.data, cmd->command,
  58. strlen(cmd->command))) {
  59. return cmd;
  60. }
  61. }
  62. return NULL;
  63. }
  64. #define MIN(a,b) ((a)<(b) ? (a):(b))
  65. #define STRINGX(x) #x
  66. #define STRING(x) STRINGX(x)
  67. int mconsole_get_request(int fd, struct mc_request *req)
  68. {
  69. int len;
  70. req->originlen = sizeof(req->origin);
  71. req->len = recvfrom(fd, &req->request, sizeof(req->request), 0,
  72. (struct sockaddr *) req->origin, &req->originlen);
  73. if (req->len < 0)
  74. return 0;
  75. req->originating_fd = fd;
  76. if (req->request.magic != MCONSOLE_MAGIC) {
  77. /* Unversioned request */
  78. len = MIN(sizeof(req->request.data) - 1,
  79. strlen((char *) &req->request));
  80. memmove(req->request.data, &req->request, len);
  81. req->request.data[len] = '\0';
  82. req->request.magic = MCONSOLE_MAGIC;
  83. req->request.version = 0;
  84. req->request.len = len;
  85. mconsole_reply_v0(req, "ERR Version 0 mconsole clients are "
  86. "not supported by this driver");
  87. return 0;
  88. }
  89. if (req->request.len >= MCONSOLE_MAX_DATA) {
  90. mconsole_reply(req, "Request too large", 1, 0);
  91. return 0;
  92. }
  93. if (req->request.version != MCONSOLE_VERSION) {
  94. mconsole_reply(req, "This driver only supports version "
  95. STRING(MCONSOLE_VERSION) " clients", 1, 0);
  96. }
  97. req->request.data[req->request.len] = '\0';
  98. req->cmd = mconsole_parse(req);
  99. if (req->cmd == NULL) {
  100. mconsole_reply(req, "Unknown command", 1, 0);
  101. return 0;
  102. }
  103. return 1;
  104. }
  105. int mconsole_reply_len(struct mc_request *req, const char *str, int total,
  106. int err, int more)
  107. {
  108. /*
  109. * XXX This is a stack consumption problem. It'd be nice to
  110. * make it global and serialize access to it, but there are a
  111. * ton of callers to this function.
  112. */
  113. struct mconsole_reply reply;
  114. int len, n;
  115. do {
  116. reply.err = err;
  117. /* err can only be true on the first packet */
  118. err = 0;
  119. len = MIN(total, MCONSOLE_MAX_DATA - 1);
  120. if (len == total) reply.more = more;
  121. else reply.more = 1;
  122. memcpy(reply.data, str, len);
  123. reply.data[len] = '\0';
  124. total -= len;
  125. str += len;
  126. reply.len = len + 1;
  127. len = sizeof(reply) + reply.len - sizeof(reply.data);
  128. n = sendto(req->originating_fd, &reply, len, 0,
  129. (struct sockaddr *) req->origin, req->originlen);
  130. if (n < 0)
  131. return -errno;
  132. } while (total > 0);
  133. return 0;
  134. }
  135. int mconsole_reply(struct mc_request *req, const char *str, int err, int more)
  136. {
  137. return mconsole_reply_len(req, str, strlen(str), err, more);
  138. }
  139. int mconsole_unlink_socket(void)
  140. {
  141. unlink(mconsole_socket_name);
  142. return 0;
  143. }
  144. static int notify_sock = -1;
  145. int mconsole_notify(char *sock_name, int type, const void *data, int len)
  146. {
  147. struct sockaddr_un target;
  148. struct mconsole_notify packet;
  149. int n, err = 0;
  150. lock_notify();
  151. if (notify_sock < 0) {
  152. notify_sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  153. if (notify_sock < 0) {
  154. err = -errno;
  155. printk(UM_KERN_ERR "mconsole_notify - socket failed, "
  156. "errno = %d\n", errno);
  157. }
  158. }
  159. unlock_notify();
  160. if (err)
  161. return err;
  162. target.sun_family = AF_UNIX;
  163. strcpy(target.sun_path, sock_name);
  164. packet.magic = MCONSOLE_MAGIC;
  165. packet.version = MCONSOLE_VERSION;
  166. packet.type = type;
  167. len = (len > sizeof(packet.data)) ? sizeof(packet.data) : len;
  168. packet.len = len;
  169. memcpy(packet.data, data, len);
  170. err = 0;
  171. len = sizeof(packet) + packet.len - sizeof(packet.data);
  172. n = sendto(notify_sock, &packet, len, 0, (struct sockaddr *) &target,
  173. sizeof(target));
  174. if (n < 0) {
  175. err = -errno;
  176. printk(UM_KERN_ERR "mconsole_notify - sendto failed, "
  177. "errno = %d\n", errno);
  178. }
  179. return err;
  180. }