blob: f8cf4c8bedef3eb91f161a6da67e474ae508464d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org)
Jeff Dikecb8fa612007-10-16 01:27:34 -07003 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Licensed under the GPL
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <errno.h>
Jeff Dikecb8fa612007-10-16 01:27:34 -07008#include <string.h>
9#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <sys/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <sys/uio.h>
12#include <sys/un.h>
Jeff Dikecb8fa612007-10-16 01:27:34 -070013#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "mconsole.h"
Jeff Dikecb8fa612007-10-16 01:27:34 -070015#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17static struct mconsole_command commands[] = {
Jeff Dikecb8fa612007-10-16 01:27:34 -070018 /*
19 * With uts namespaces, uts information becomes process-specific, so
Jeff Dike2d77f6f2006-07-10 04:45:16 -070020 * 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 Torvalds1da177e2005-04-16 15:20:36 -070025 { "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' Giarrussobd9480572005-09-30 11:59:00 -070029 { "sysrq", mconsole_sysrq, MCONSOLE_INTR },
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 { "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 Dikecb8fa612007-10-16 01:27:34 -070036 { "stack", mconsole_stack, MCONSOLE_INTR },
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
39/* Initialized in mconsole_init, which is an initcall */
40char mconsole_socket_name[256];
41
WANG Cong1605ec02008-04-28 02:13:56 -070042static int mconsole_reply_v0(struct mc_request *req, char *reply)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Jeff Dikecb8fa612007-10-16 01:27:34 -070044 struct iovec iov;
45 struct msghdr msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jeff Dikecb8fa612007-10-16 01:27:34 -070047 iov.iov_base = reply;
48 iov.iov_len = strlen(reply);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Jeff Dikecb8fa612007-10-16 01:27:34 -070050 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 Torvalds1da177e2005-04-16 15:20:36 -070057
Jeff Dikecb8fa612007-10-16 01:27:34 -070058 return sendmsg(req->originating_fd, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61static struct mconsole_command *mconsole_parse(struct mc_request *req)
62{
63 struct mconsole_command *cmd;
64 int i;
65
Jeff Dikecb8fa612007-10-16 01:27:34 -070066 for (i = 0; i < ARRAY_SIZE(commands); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 cmd = &commands[i];
Jeff Dikecb8fa612007-10-16 01:27:34 -070068 if (!strncmp(req->request.data, cmd->command,
69 strlen(cmd->command))) {
Jeff Dike91b165c2006-09-25 23:33:00 -070070 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72 }
Jeff Dike91b165c2006-09-25 23:33:00 -070073 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76#define MIN(a,b) ((a)<(b) ? (a):(b))
77
78#define STRINGX(x) #x
79#define STRING(x) STRINGX(x)
80
81int mconsole_get_request(int fd, struct mc_request *req)
82{
83 int len;
84
85 req->originlen = sizeof(req->origin);
Karol Swietlicki7b5cc6e2008-02-04 22:31:04 -080086 req->len = recvfrom(fd, &req->request, sizeof(req->request), 0,
87 (struct sockaddr *) req->origin, &req->originlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (req->len < 0)
89 return 0;
90
91 req->originating_fd = fd;
92
Jeff Dikecb8fa612007-10-16 01:27:34 -070093 if (req->request.magic != MCONSOLE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /* Unversioned request */
Jeff Dikecb8fa612007-10-16 01:27:34 -070095 len = MIN(sizeof(req->request.data) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 strlen((char *) &req->request));
97 memmove(req->request.data, &req->request, len);
98 req->request.data[len] = '\0';
99
100 req->request.magic = MCONSOLE_MAGIC;
101 req->request.version = 0;
102 req->request.len = len;
103
104 mconsole_reply_v0(req, "ERR Version 0 mconsole clients are "
105 "not supported by this driver");
Jeff Dikecb8fa612007-10-16 01:27:34 -0700106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Jeff Dikecb8fa612007-10-16 01:27:34 -0700109 if (req->request.len >= MCONSOLE_MAX_DATA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 mconsole_reply(req, "Request too large", 1, 0);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Jeff Dikecb8fa612007-10-16 01:27:34 -0700113 if (req->request.version != MCONSOLE_VERSION) {
114 mconsole_reply(req, "This driver only supports version "
115 STRING(MCONSOLE_VERSION) " clients", 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Jeff Dikecb8fa612007-10-16 01:27:34 -0700117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 req->request.data[req->request.len] = '\0';
119 req->cmd = mconsole_parse(req);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700120 if (req->cmd == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 mconsole_reply(req, "Unknown command", 1, 0);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Jeff Dikecb8fa612007-10-16 01:27:34 -0700125 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Jeff Dike7b033e12006-01-06 00:19:03 -0800128int mconsole_reply_len(struct mc_request *req, const char *str, int total,
129 int err, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Jeff Dikecb8fa612007-10-16 01:27:34 -0700131 /*
132 * XXX This is a stack consumption problem. It'd be nice to
Jeff Dikef92afe52006-09-29 01:58:52 -0700133 * make it global and serialize access to it, but there are a
134 * ton of callers to this function.
135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct mconsole_reply reply;
Jeff Dike7b033e12006-01-06 00:19:03 -0800137 int len, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 do {
140 reply.err = err;
141
142 /* err can only be true on the first packet */
143 err = 0;
144
145 len = MIN(total, MCONSOLE_MAX_DATA - 1);
146
Jeff Dikecb8fa612007-10-16 01:27:34 -0700147 if (len == total) reply.more = more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 else reply.more = 1;
149
150 memcpy(reply.data, str, len);
151 reply.data[len] = '\0';
152 total -= len;
153 str += len;
154 reply.len = len + 1;
155
156 len = sizeof(reply) + reply.len - sizeof(reply.data);
157
158 n = sendto(req->originating_fd, &reply, len, 0,
159 (struct sockaddr *) req->origin, req->originlen);
160
Jeff Dikecb8fa612007-10-16 01:27:34 -0700161 if (n < 0)
162 return -errno;
163 } while (total > 0);
164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Jeff Dike7b033e12006-01-06 00:19:03 -0800167int mconsole_reply(struct mc_request *req, const char *str, int err, int more)
168{
169 return mconsole_reply_len(req, str, strlen(str), err, more);
170}
171
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173int mconsole_unlink_socket(void)
174{
175 unlink(mconsole_socket_name);
176 return 0;
177}
178
179static int notify_sock = -1;
180
181int mconsole_notify(char *sock_name, int type, const void *data, int len)
182{
183 struct sockaddr_un target;
184 struct mconsole_notify packet;
185 int n, err = 0;
186
187 lock_notify();
Jeff Dikecb8fa612007-10-16 01:27:34 -0700188 if (notify_sock < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 notify_sock = socket(PF_UNIX, SOCK_DGRAM, 0);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700190 if (notify_sock < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 err = -errno;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700192 printk(UM_KERN_ERR "mconsole_notify - socket failed, "
193 "errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 }
196 unlock_notify();
Jeff Dikecb8fa612007-10-16 01:27:34 -0700197
198 if (err)
199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 target.sun_family = AF_UNIX;
202 strcpy(target.sun_path, sock_name);
203
204 packet.magic = MCONSOLE_MAGIC;
205 packet.version = MCONSOLE_VERSION;
206 packet.type = type;
207 len = (len > sizeof(packet.data)) ? sizeof(packet.data) : len;
208 packet.len = len;
209 memcpy(packet.data, data, len);
210
211 err = 0;
212 len = sizeof(packet) + packet.len - sizeof(packet.data);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700213 n = sendto(notify_sock, &packet, len, 0, (struct sockaddr *) &target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 sizeof(target));
Jeff Dikecb8fa612007-10-16 01:27:34 -0700215 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 err = -errno;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700217 printk(UM_KERN_ERR "mconsole_notify - sendto failed, "
218 "errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Jeff Dikecb8fa612007-10-16 01:27:34 -0700220 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}