2 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org)
3 * Copyright (C) 2001 - 2003 Jeff Dike (jdike@addtoit.com)
4 * Licensed under the GPL
11 #include <sys/socket.h>
12 #include <sys/types.h>
19 #include "user_util.h"
21 static struct mconsole_command commands[] = {
22 /* With uts namespaces, uts information becomes process-specific, so
23 * we need a process context. If we try handling this in interrupt
24 * context, we may hit an exiting process without a valid uts
27 { "version", mconsole_version, MCONSOLE_PROC },
28 { "halt", mconsole_halt, MCONSOLE_PROC },
29 { "reboot", mconsole_reboot, MCONSOLE_PROC },
30 { "config", mconsole_config, MCONSOLE_PROC },
31 { "remove", mconsole_remove, MCONSOLE_PROC },
32 { "sysrq", mconsole_sysrq, MCONSOLE_INTR },
33 { "help", mconsole_help, MCONSOLE_INTR },
34 { "cad", mconsole_cad, MCONSOLE_INTR },
35 { "stop", mconsole_stop, MCONSOLE_PROC },
36 { "go", mconsole_go, MCONSOLE_INTR },
37 { "log", mconsole_log, MCONSOLE_INTR },
38 { "proc", mconsole_proc, MCONSOLE_PROC },
39 { "stack", mconsole_stack, MCONSOLE_INTR },
42 /* Initialized in mconsole_init, which is an initcall */
43 char mconsole_socket_name[256];
45 int mconsole_reply_v0(struct mc_request *req, char *reply)
51 iov.iov_len = strlen(reply);
53 msg.msg_name = &(req->origin);
54 msg.msg_namelen = req->originlen;
57 msg.msg_control = NULL;
58 msg.msg_controllen = 0;
61 return sendmsg(req->originating_fd, &msg, 0);
64 static struct mconsole_command *mconsole_parse(struct mc_request *req)
66 struct mconsole_command *cmd;
69 for(i = 0; i < ARRAY_SIZE(commands); i++){
71 if(!strncmp(req->request.data, cmd->command,
72 strlen(cmd->command))){
79 #define MIN(a,b) ((a)<(b) ? (a):(b))
82 #define STRING(x) STRINGX(x)
84 int mconsole_get_request(int fd, struct mc_request *req)
88 req->originlen = sizeof(req->origin);
89 req->len = recvfrom(fd, &req->request, sizeof(req->request), 0,
90 (struct sockaddr *) req->origin, &req->originlen);
94 req->originating_fd = fd;
96 if(req->request.magic != MCONSOLE_MAGIC){
97 /* Unversioned request */
98 len = MIN(sizeof(req->request.data) - 1,
99 strlen((char *) &req->request));
100 memmove(req->request.data, &req->request, len);
101 req->request.data[len] = '\0';
103 req->request.magic = MCONSOLE_MAGIC;
104 req->request.version = 0;
105 req->request.len = len;
107 mconsole_reply_v0(req, "ERR Version 0 mconsole clients are "
108 "not supported by this driver");
112 if(req->request.len >= MCONSOLE_MAX_DATA){
113 mconsole_reply(req, "Request too large", 1, 0);
116 if(req->request.version != MCONSOLE_VERSION){
117 mconsole_reply(req, "This driver only supports version "
118 STRING(MCONSOLE_VERSION) " clients", 1, 0);
121 req->request.data[req->request.len] = '\0';
122 req->cmd = mconsole_parse(req);
123 if(req->cmd == NULL){
124 mconsole_reply(req, "Unknown command", 1, 0);
131 int mconsole_reply_len(struct mc_request *req, const char *str, int total,
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);
159 if(n < 0) return(-errno);
164 int mconsole_reply(struct mc_request *req, const char *str, int err, int more)
166 return mconsole_reply_len(req, str, strlen(str), err, more);
170 int mconsole_unlink_socket(void)
172 unlink(mconsole_socket_name);
176 static int notify_sock = -1;
178 int mconsole_notify(char *sock_name, int type, const void *data, int len)
180 struct sockaddr_un target;
181 struct mconsole_notify packet;
186 notify_sock = socket(PF_UNIX, SOCK_DGRAM, 0);
189 printk("mconsole_notify - socket failed, errno = %d\n",
198 target.sun_family = AF_UNIX;
199 strcpy(target.sun_path, sock_name);
201 packet.magic = MCONSOLE_MAGIC;
202 packet.version = MCONSOLE_VERSION;
204 len = (len > sizeof(packet.data)) ? sizeof(packet.data) : len;
206 memcpy(packet.data, data, len);
209 len = sizeof(packet) + packet.len - sizeof(packet.data);
210 n = sendto(notify_sock, &packet, len, 0, (struct sockaddr *) &target,
214 printk("mconsole_notify - sendto failed, errno = %d\n", errno);
220 * Overrides for Emacs so that we follow Linus's tabbing style.
221 * Emacs will notice this stuff at the end of the file and automatically
222 * adjust the settings for this buffer only. This must remain at the end
224 * ---------------------------------------------------------------------------
226 * c-file-style: "linux"