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