mconsole_user.c 5.8 KB

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