2 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org)
3 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
10 #include <sys/socket.h>
15 static struct mconsole_command commands[] = {
17 * With uts namespaces, uts information becomes process-specific, so
18 * we need a process context. If we try handling this in interrupt
19 * context, we may hit an exiting process without a valid uts
22 { "version", mconsole_version, MCONSOLE_PROC },
23 { "halt", mconsole_halt, MCONSOLE_PROC },
24 { "reboot", mconsole_reboot, MCONSOLE_PROC },
25 { "config", mconsole_config, MCONSOLE_PROC },
26 { "remove", mconsole_remove, MCONSOLE_PROC },
27 { "sysrq", mconsole_sysrq, MCONSOLE_INTR },
28 { "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 },
34 { "stack", mconsole_stack, MCONSOLE_INTR },
37 /* Initialized in mconsole_init, which is an initcall */
38 char mconsole_socket_name[256];
40 static int mconsole_reply_v0(struct mc_request *req, char *reply)
46 iov.iov_len = strlen(reply);
48 msg.msg_name = &(req->origin);
49 msg.msg_namelen = req->originlen;
52 msg.msg_control = NULL;
53 msg.msg_controllen = 0;
56 return sendmsg(req->originating_fd, &msg, 0);
59 static struct mconsole_command *mconsole_parse(struct mc_request *req)
61 struct mconsole_command *cmd;
64 for (i = 0; i < ARRAY_SIZE(commands); i++) {
66 if (!strncmp(req->request.data, cmd->command,
67 strlen(cmd->command))) {
74 #define MIN(a,b) ((a)<(b) ? (a):(b))
77 #define STRING(x) STRINGX(x)
79 int mconsole_get_request(int fd, struct mc_request *req)
83 req->originlen = sizeof(req->origin);
84 req->len = recvfrom(fd, &req->request, sizeof(req->request), 0,
85 (struct sockaddr *) req->origin, &req->originlen);
89 req->originating_fd = fd;
91 if (req->request.magic != MCONSOLE_MAGIC) {
92 /* Unversioned request */
93 len = MIN(sizeof(req->request.data) - 1,
94 strlen((char *) &req->request));
95 memmove(req->request.data, &req->request, len);
96 req->request.data[len] = '\0';
98 req->request.magic = MCONSOLE_MAGIC;
99 req->request.version = 0;
100 req->request.len = len;
102 mconsole_reply_v0(req, "ERR Version 0 mconsole clients are "
103 "not supported by this driver");
107 if (req->request.len >= MCONSOLE_MAX_DATA) {
108 mconsole_reply(req, "Request too large", 1, 0);
111 if (req->request.version != MCONSOLE_VERSION) {
112 mconsole_reply(req, "This driver only supports version "
113 STRING(MCONSOLE_VERSION) " clients", 1, 0);
116 req->request.data[req->request.len] = '\0';
117 req->cmd = mconsole_parse(req);
118 if (req->cmd == NULL) {
119 mconsole_reply(req, "Unknown command", 1, 0);
126 int mconsole_reply_len(struct mc_request *req, const char *str, int total,
130 * XXX This is a stack consumption problem. It'd be nice to
131 * make it global and serialize access to it, but there are a
132 * ton of callers to this function.
134 struct mconsole_reply reply;
140 /* err can only be true on the first packet */
143 len = MIN(total, MCONSOLE_MAX_DATA - 1);
145 if (len == total) reply.more = more;
148 memcpy(reply.data, str, len);
149 reply.data[len] = '\0';
154 len = sizeof(reply) + reply.len - sizeof(reply.data);
156 n = sendto(req->originating_fd, &reply, len, 0,
157 (struct sockaddr *) req->origin, req->originlen);
165 int mconsole_reply(struct mc_request *req, const char *str, int err, int more)
167 return mconsole_reply_len(req, str, strlen(str), err, more);
171 int mconsole_unlink_socket(void)
173 unlink(mconsole_socket_name);
177 static int notify_sock = -1;
179 int mconsole_notify(char *sock_name, int type, const void *data, int len)
181 struct sockaddr_un target;
182 struct mconsole_notify packet;
186 if (notify_sock < 0) {
187 notify_sock = socket(PF_UNIX, SOCK_DGRAM, 0);
188 if (notify_sock < 0) {
190 printk(UM_KERN_ERR "mconsole_notify - socket failed, "
191 "errno = %d\n", errno);
199 target.sun_family = AF_UNIX;
200 strcpy(target.sun_path, sock_name);
202 packet.magic = MCONSOLE_MAGIC;
203 packet.version = MCONSOLE_VERSION;
205 len = (len > sizeof(packet.data)) ? sizeof(packet.data) : len;
207 memcpy(packet.data, data, len);
210 len = sizeof(packet) + packet.len - sizeof(packet.data);
211 n = sendto(notify_sock, &packet, len, 0, (struct sockaddr *) &target,
215 printk(UM_KERN_ERR "mconsole_notify - sendto failed, "
216 "errno = %d\n", errno);