blob: 5b2f5fe9e426d3d45043bae9a49badb804a9426e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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"
Jeff Dike91b165c2006-09-25 23:33:00 -070019#include "user_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21static struct mconsole_command commands[] = {
Jeff Dike2d77f6f2006-07-10 04:45:16 -070022 /* 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
25 * namespace.
26 */
27 { "version", mconsole_version, MCONSOLE_PROC },
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 { "halt", mconsole_halt, MCONSOLE_PROC },
29 { "reboot", mconsole_reboot, MCONSOLE_PROC },
30 { "config", mconsole_config, MCONSOLE_PROC },
31 { "remove", mconsole_remove, MCONSOLE_PROC },
Paolo 'Blaisorblade' Giarrussobd9480572005-09-30 11:59:00 -070032 { "sysrq", mconsole_sysrq, MCONSOLE_INTR },
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 { "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 },
Jeff Dike3eddddc2005-09-16 19:27:46 -070039 { "stack", mconsole_stack, MCONSOLE_INTR },
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
42/* Initialized in mconsole_init, which is an initcall */
43char mconsole_socket_name[256];
44
45int mconsole_reply_v0(struct mc_request *req, char *reply)
46{
47 struct iovec iov;
48 struct msghdr msg;
49
50 iov.iov_base = reply;
51 iov.iov_len = strlen(reply);
52
53 msg.msg_name = &(req->origin);
54 msg.msg_namelen = req->originlen;
55 msg.msg_iov = &iov;
56 msg.msg_iovlen = 1;
57 msg.msg_control = NULL;
58 msg.msg_controllen = 0;
59 msg.msg_flags = 0;
60
61 return sendmsg(req->originating_fd, &msg, 0);
62}
63
64static struct mconsole_command *mconsole_parse(struct mc_request *req)
65{
66 struct mconsole_command *cmd;
67 int i;
68
Jeff Dike91b165c2006-09-25 23:33:00 -070069 for(i = 0; i < ARRAY_SIZE(commands); i++){
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 cmd = &commands[i];
71 if(!strncmp(req->request.data, cmd->command,
72 strlen(cmd->command))){
Jeff Dike91b165c2006-09-25 23:33:00 -070073 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 }
Jeff Dike91b165c2006-09-25 23:33:00 -070076 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79#define MIN(a,b) ((a)<(b) ? (a):(b))
80
81#define STRINGX(x) #x
82#define STRING(x) STRINGX(x)
83
84int mconsole_get_request(int fd, struct mc_request *req)
85{
86 int len;
87
88 req->originlen = sizeof(req->origin);
89 req->len = recvfrom(fd, &req->request, sizeof(req->request), 0,
90 (struct sockaddr *) req->origin, &req->originlen);
91 if (req->len < 0)
92 return 0;
93
94 req->originating_fd = fd;
95
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';
102
103 req->request.magic = MCONSOLE_MAGIC;
104 req->request.version = 0;
105 req->request.len = len;
106
107 mconsole_reply_v0(req, "ERR Version 0 mconsole clients are "
108 "not supported by this driver");
109 return(0);
110 }
111
112 if(req->request.len >= MCONSOLE_MAX_DATA){
113 mconsole_reply(req, "Request too large", 1, 0);
114 return(0);
115 }
116 if(req->request.version != MCONSOLE_VERSION){
117 mconsole_reply(req, "This driver only supports version "
118 STRING(MCONSOLE_VERSION) " clients", 1, 0);
119 }
120
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);
125 return(0);
126 }
127
128 return(1);
129}
130
Jeff Dike7b033e12006-01-06 00:19:03 -0800131int mconsole_reply_len(struct mc_request *req, const char *str, int total,
132 int err, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 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
145 if(len == total) reply.more = more;
146 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
159 if(n < 0) return(-errno);
160 } while(total > 0);
161 return(0);
162}
163
Jeff Dike7b033e12006-01-06 00:19:03 -0800164int mconsole_reply(struct mc_request *req, const char *str, int err, int more)
165{
166 return mconsole_reply_len(req, str, strlen(str), err, more);
167}
168
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170int mconsole_unlink_socket(void)
171{
172 unlink(mconsole_socket_name);
173 return 0;
174}
175
176static int notify_sock = -1;
177
178int mconsole_notify(char *sock_name, int type, const void *data, int len)
179{
180 struct sockaddr_un target;
181 struct mconsole_notify packet;
182 int n, err = 0;
183
184 lock_notify();
185 if(notify_sock < 0){
186 notify_sock = socket(PF_UNIX, SOCK_DGRAM, 0);
187 if(notify_sock < 0){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 err = -errno;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700189 printk("mconsole_notify - socket failed, errno = %d\n",
190 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192 }
193 unlock_notify();
194
195 if(err)
196 return(err);
197
198 target.sun_family = AF_UNIX;
199 strcpy(target.sun_path, sock_name);
200
201 packet.magic = MCONSOLE_MAGIC;
202 packet.version = MCONSOLE_VERSION;
203 packet.type = type;
204 len = (len > sizeof(packet.data)) ? sizeof(packet.data) : len;
205 packet.len = len;
206 memcpy(packet.data, data, len);
207
208 err = 0;
209 len = sizeof(packet) + packet.len - sizeof(packet.data);
210 n = sendto(notify_sock, &packet, len, 0, (struct sockaddr *) &target,
211 sizeof(target));
212 if(n < 0){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 err = -errno;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700214 printk("mconsole_notify - sendto failed, errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216 return(err);
217}
218
219/*
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
223 * of the file.
224 * ---------------------------------------------------------------------------
225 * Local variables:
226 * c-file-style: "linux"
227 * End:
228 */