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