blob: 99209826adb15daffcd8ed299e00b42ae8ea752d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "mconsole.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15static struct mconsole_command commands[] = {
Jeff Dikecb8fa612007-10-16 01:27:34 -070016 /*
17 * With uts namespaces, uts information becomes process-specific, so
Jeff Dike2d77f6f2006-07-10 04:45:16 -070018 * we need a process context. If we try handling this in interrupt
19 * context, we may hit an exiting process without a valid uts
20 * namespace.
21 */
22 { "version", mconsole_version, MCONSOLE_PROC },
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 { "halt", mconsole_halt, MCONSOLE_PROC },
24 { "reboot", mconsole_reboot, MCONSOLE_PROC },
25 { "config", mconsole_config, MCONSOLE_PROC },
26 { "remove", mconsole_remove, MCONSOLE_PROC },
Paolo 'Blaisorblade' Giarrussobd9480572005-09-30 11:59:00 -070027 { "sysrq", mconsole_sysrq, MCONSOLE_INTR },
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 { "help", mconsole_help, MCONSOLE_INTR },
29 { "cad", mconsole_cad, MCONSOLE_INTR },
30 { "stop", mconsole_stop, MCONSOLE_PROC },
31 { "go", mconsole_go, MCONSOLE_INTR },
32 { "log", mconsole_log, MCONSOLE_INTR },
33 { "proc", mconsole_proc, MCONSOLE_PROC },
Jeff Dikecb8fa612007-10-16 01:27:34 -070034 { "stack", mconsole_stack, MCONSOLE_INTR },
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
37/* Initialized in mconsole_init, which is an initcall */
38char mconsole_socket_name[256];
39
WANG Cong1605ec02008-04-28 02:13:56 -070040static int mconsole_reply_v0(struct mc_request *req, char *reply)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Jeff Dikecb8fa612007-10-16 01:27:34 -070042 struct iovec iov;
43 struct msghdr msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jeff Dikecb8fa612007-10-16 01:27:34 -070045 iov.iov_base = reply;
46 iov.iov_len = strlen(reply);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jeff Dikecb8fa612007-10-16 01:27:34 -070048 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jeff Dikecb8fa612007-10-16 01:27:34 -070056 return sendmsg(req->originating_fd, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59static struct mconsole_command *mconsole_parse(struct mc_request *req)
60{
61 struct mconsole_command *cmd;
62 int i;
63
Jeff Dikecb8fa612007-10-16 01:27:34 -070064 for (i = 0; i < ARRAY_SIZE(commands); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 cmd = &commands[i];
Jeff Dikecb8fa612007-10-16 01:27:34 -070066 if (!strncmp(req->request.data, cmd->command,
67 strlen(cmd->command))) {
Jeff Dike91b165c2006-09-25 23:33:00 -070068 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70 }
Jeff Dike91b165c2006-09-25 23:33:00 -070071 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74#define MIN(a,b) ((a)<(b) ? (a):(b))
75
76#define STRINGX(x) #x
77#define STRING(x) STRINGX(x)
78
79int mconsole_get_request(int fd, struct mc_request *req)
80{
81 int len;
82
83 req->originlen = sizeof(req->origin);
Karol Swietlicki7b5cc6e2008-02-04 22:31:04 -080084 req->len = recvfrom(fd, &req->request, sizeof(req->request), 0,
85 (struct sockaddr *) req->origin, &req->originlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (req->len < 0)
87 return 0;
88
89 req->originating_fd = fd;
90
Jeff Dikecb8fa612007-10-16 01:27:34 -070091 if (req->request.magic != MCONSOLE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /* Unversioned request */
Jeff Dikecb8fa612007-10-16 01:27:34 -070093 len = MIN(sizeof(req->request.data) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 strlen((char *) &req->request));
95 memmove(req->request.data, &req->request, len);
96 req->request.data[len] = '\0';
97
98 req->request.magic = MCONSOLE_MAGIC;
99 req->request.version = 0;
100 req->request.len = len;
101
102 mconsole_reply_v0(req, "ERR Version 0 mconsole clients are "
103 "not supported by this driver");
Jeff Dikecb8fa612007-10-16 01:27:34 -0700104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
Jeff Dikecb8fa612007-10-16 01:27:34 -0700107 if (req->request.len >= MCONSOLE_MAX_DATA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 mconsole_reply(req, "Request too large", 1, 0);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Jeff Dikecb8fa612007-10-16 01:27:34 -0700111 if (req->request.version != MCONSOLE_VERSION) {
112 mconsole_reply(req, "This driver only supports version "
113 STRING(MCONSOLE_VERSION) " clients", 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Jeff Dikecb8fa612007-10-16 01:27:34 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 req->request.data[req->request.len] = '\0';
117 req->cmd = mconsole_parse(req);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700118 if (req->cmd == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 mconsole_reply(req, "Unknown command", 1, 0);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
Jeff Dikecb8fa612007-10-16 01:27:34 -0700123 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Jeff Dike7b033e12006-01-06 00:19:03 -0800126int mconsole_reply_len(struct mc_request *req, const char *str, int total,
127 int err, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Jeff Dikecb8fa612007-10-16 01:27:34 -0700129 /*
130 * XXX This is a stack consumption problem. It'd be nice to
Jeff Dikef92afe52006-09-29 01:58:52 -0700131 * make it global and serialize access to it, but there are a
132 * ton of callers to this function.
133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct mconsole_reply reply;
Jeff Dike7b033e12006-01-06 00:19:03 -0800135 int len, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 do {
138 reply.err = err;
139
140 /* err can only be true on the first packet */
141 err = 0;
142
143 len = MIN(total, MCONSOLE_MAX_DATA - 1);
144
Jeff Dikecb8fa612007-10-16 01:27:34 -0700145 if (len == total) reply.more = more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 else reply.more = 1;
147
148 memcpy(reply.data, str, len);
149 reply.data[len] = '\0';
150 total -= len;
151 str += len;
152 reply.len = len + 1;
153
154 len = sizeof(reply) + reply.len - sizeof(reply.data);
155
156 n = sendto(req->originating_fd, &reply, len, 0,
157 (struct sockaddr *) req->origin, req->originlen);
158
Jeff Dikecb8fa612007-10-16 01:27:34 -0700159 if (n < 0)
160 return -errno;
161 } while (total > 0);
162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Jeff Dike7b033e12006-01-06 00:19:03 -0800165int mconsole_reply(struct mc_request *req, const char *str, int err, int more)
166{
167 return mconsole_reply_len(req, str, strlen(str), err, more);
168}
169
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171int mconsole_unlink_socket(void)
172{
173 unlink(mconsole_socket_name);
174 return 0;
175}
176
177static int notify_sock = -1;
178
179int mconsole_notify(char *sock_name, int type, const void *data, int len)
180{
181 struct sockaddr_un target;
182 struct mconsole_notify packet;
183 int n, err = 0;
184
185 lock_notify();
Jeff Dikecb8fa612007-10-16 01:27:34 -0700186 if (notify_sock < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 notify_sock = socket(PF_UNIX, SOCK_DGRAM, 0);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700188 if (notify_sock < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 err = -errno;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700190 printk(UM_KERN_ERR "mconsole_notify - socket failed, "
191 "errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193 }
194 unlock_notify();
Jeff Dikecb8fa612007-10-16 01:27:34 -0700195
196 if (err)
197 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 target.sun_family = AF_UNIX;
200 strcpy(target.sun_path, sock_name);
201
202 packet.magic = MCONSOLE_MAGIC;
203 packet.version = MCONSOLE_VERSION;
204 packet.type = type;
205 len = (len > sizeof(packet.data)) ? sizeof(packet.data) : len;
206 packet.len = len;
207 memcpy(packet.data, data, len);
208
209 err = 0;
210 len = sizeof(packet) + packet.len - sizeof(packet.data);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700211 n = sendto(notify_sock, &packet, len, 0, (struct sockaddr *) &target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 sizeof(target));
Jeff Dikecb8fa612007-10-16 01:27:34 -0700213 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 err = -errno;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700215 printk(UM_KERN_ERR "mconsole_notify - sendto failed, "
216 "errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Jeff Dikecb8fa612007-10-16 01:27:34 -0700218 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}