blob: 9bfd405c3bd82e5b891c9263d2fe339599ea0651 [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"
19
20static struct mconsole_command commands[] = {
Jeff Dike2d77f6f2006-07-10 04:45:16 -070021 /* With uts namespaces, uts information becomes process-specific, so
22 * we need a process context. If we try handling this in interrupt
23 * context, we may hit an exiting process without a valid uts
24 * namespace.
25 */
26 { "version", mconsole_version, MCONSOLE_PROC },
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 { "halt", mconsole_halt, MCONSOLE_PROC },
28 { "reboot", mconsole_reboot, MCONSOLE_PROC },
29 { "config", mconsole_config, MCONSOLE_PROC },
30 { "remove", mconsole_remove, MCONSOLE_PROC },
Paolo 'Blaisorblade' Giarrussobd9480572005-09-30 11:59:00 -070031 { "sysrq", mconsole_sysrq, MCONSOLE_INTR },
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 { "help", mconsole_help, MCONSOLE_INTR },
33 { "cad", mconsole_cad, MCONSOLE_INTR },
34 { "stop", mconsole_stop, MCONSOLE_PROC },
35 { "go", mconsole_go, MCONSOLE_INTR },
36 { "log", mconsole_log, MCONSOLE_INTR },
37 { "proc", mconsole_proc, MCONSOLE_PROC },
Jeff Dike3eddddc2005-09-16 19:27:46 -070038 { "stack", mconsole_stack, MCONSOLE_INTR },
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
41/* Initialized in mconsole_init, which is an initcall */
42char mconsole_socket_name[256];
43
44int mconsole_reply_v0(struct mc_request *req, char *reply)
45{
46 struct iovec iov;
47 struct msghdr msg;
48
49 iov.iov_base = reply;
50 iov.iov_len = strlen(reply);
51
52 msg.msg_name = &(req->origin);
53 msg.msg_namelen = req->originlen;
54 msg.msg_iov = &iov;
55 msg.msg_iovlen = 1;
56 msg.msg_control = NULL;
57 msg.msg_controllen = 0;
58 msg.msg_flags = 0;
59
60 return sendmsg(req->originating_fd, &msg, 0);
61}
62
63static struct mconsole_command *mconsole_parse(struct mc_request *req)
64{
65 struct mconsole_command *cmd;
66 int i;
67
68 for(i=0;i<sizeof(commands)/sizeof(commands[0]);i++){
69 cmd = &commands[i];
70 if(!strncmp(req->request.data, cmd->command,
71 strlen(cmd->command))){
72 return(cmd);
73 }
74 }
75 return(NULL);
76}
77
78#define MIN(a,b) ((a)<(b) ? (a):(b))
79
80#define STRINGX(x) #x
81#define STRING(x) STRINGX(x)
82
83int mconsole_get_request(int fd, struct mc_request *req)
84{
85 int len;
86
87 req->originlen = sizeof(req->origin);
88 req->len = recvfrom(fd, &req->request, sizeof(req->request), 0,
89 (struct sockaddr *) req->origin, &req->originlen);
90 if (req->len < 0)
91 return 0;
92
93 req->originating_fd = fd;
94
95 if(req->request.magic != MCONSOLE_MAGIC){
96 /* Unversioned request */
97 len = MIN(sizeof(req->request.data) - 1,
98 strlen((char *) &req->request));
99 memmove(req->request.data, &req->request, len);
100 req->request.data[len] = '\0';
101
102 req->request.magic = MCONSOLE_MAGIC;
103 req->request.version = 0;
104 req->request.len = len;
105
106 mconsole_reply_v0(req, "ERR Version 0 mconsole clients are "
107 "not supported by this driver");
108 return(0);
109 }
110
111 if(req->request.len >= MCONSOLE_MAX_DATA){
112 mconsole_reply(req, "Request too large", 1, 0);
113 return(0);
114 }
115 if(req->request.version != MCONSOLE_VERSION){
116 mconsole_reply(req, "This driver only supports version "
117 STRING(MCONSOLE_VERSION) " clients", 1, 0);
118 }
119
120 req->request.data[req->request.len] = '\0';
121 req->cmd = mconsole_parse(req);
122 if(req->cmd == NULL){
123 mconsole_reply(req, "Unknown command", 1, 0);
124 return(0);
125 }
126
127 return(1);
128}
129
Jeff Dike7b033e12006-01-06 00:19:03 -0800130int mconsole_reply_len(struct mc_request *req, const char *str, int total,
131 int err, int more)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 struct mconsole_reply reply;
Jeff Dike7b033e12006-01-06 00:19:03 -0800134 int len, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 do {
137 reply.err = err;
138
139 /* err can only be true on the first packet */
140 err = 0;
141
142 len = MIN(total, MCONSOLE_MAX_DATA - 1);
143
144 if(len == total) reply.more = more;
145 else reply.more = 1;
146
147 memcpy(reply.data, str, len);
148 reply.data[len] = '\0';
149 total -= len;
150 str += len;
151 reply.len = len + 1;
152
153 len = sizeof(reply) + reply.len - sizeof(reply.data);
154
155 n = sendto(req->originating_fd, &reply, len, 0,
156 (struct sockaddr *) req->origin, req->originlen);
157
158 if(n < 0) return(-errno);
159 } while(total > 0);
160 return(0);
161}
162
Jeff Dike7b033e12006-01-06 00:19:03 -0800163int mconsole_reply(struct mc_request *req, const char *str, int err, int more)
164{
165 return mconsole_reply_len(req, str, strlen(str), err, more);
166}
167
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169int mconsole_unlink_socket(void)
170{
171 unlink(mconsole_socket_name);
172 return 0;
173}
174
175static int notify_sock = -1;
176
177int mconsole_notify(char *sock_name, int type, const void *data, int len)
178{
179 struct sockaddr_un target;
180 struct mconsole_notify packet;
181 int n, err = 0;
182
183 lock_notify();
184 if(notify_sock < 0){
185 notify_sock = socket(PF_UNIX, SOCK_DGRAM, 0);
186 if(notify_sock < 0){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 err = -errno;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700188 printk("mconsole_notify - socket failed, errno = %d\n",
189 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191 }
192 unlock_notify();
193
194 if(err)
195 return(err);
196
197 target.sun_family = AF_UNIX;
198 strcpy(target.sun_path, sock_name);
199
200 packet.magic = MCONSOLE_MAGIC;
201 packet.version = MCONSOLE_VERSION;
202 packet.type = type;
203 len = (len > sizeof(packet.data)) ? sizeof(packet.data) : len;
204 packet.len = len;
205 memcpy(packet.data, data, len);
206
207 err = 0;
208 len = sizeof(packet) + packet.len - sizeof(packet.data);
209 n = sendto(notify_sock, &packet, len, 0, (struct sockaddr *) &target,
210 sizeof(target));
211 if(n < 0){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 err = -errno;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700213 printk("mconsole_notify - sendto failed, errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215 return(err);
216}
217
218/*
219 * Overrides for Emacs so that we follow Linus's tabbing style.
220 * Emacs will notice this stuff at the end of the file and automatically
221 * adjust the settings for this buffer only. This must remain at the end
222 * of the file.
223 * ---------------------------------------------------------------------------
224 * Local variables:
225 * c-file-style: "linux"
226 * End:
227 */