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>
17 #include "sysdep/ptrace.h"
20 #include "user_util.h"
22 static struct mconsole_command commands
[] = {
23 /* With uts namespaces, uts information becomes process-specific, so
24 * we need a process context. If we try handling this in interrupt
25 * context, we may hit an exiting process without a valid uts
28 { "version", mconsole_version
, MCONSOLE_PROC
},
29 { "halt", mconsole_halt
, MCONSOLE_PROC
},
30 { "reboot", mconsole_reboot
, MCONSOLE_PROC
},
31 { "config", mconsole_config
, MCONSOLE_PROC
},
32 { "remove", mconsole_remove
, MCONSOLE_PROC
},
33 { "sysrq", mconsole_sysrq
, MCONSOLE_INTR
},
34 { "help", mconsole_help
, MCONSOLE_INTR
},
35 { "cad", mconsole_cad
, MCONSOLE_INTR
},
36 { "stop", mconsole_stop
, MCONSOLE_PROC
},
37 { "go", mconsole_go
, MCONSOLE_INTR
},
38 { "log", mconsole_log
, MCONSOLE_INTR
},
39 { "proc", mconsole_proc
, MCONSOLE_PROC
},
40 { "stack", mconsole_stack
, MCONSOLE_INTR
},
43 /* Initialized in mconsole_init, which is an initcall */
44 char mconsole_socket_name
[256];
46 int mconsole_reply_v0(struct mc_request
*req
, char *reply
)
52 iov
.iov_len
= strlen(reply
);
54 msg
.msg_name
= &(req
->origin
);
55 msg
.msg_namelen
= req
->originlen
;
58 msg
.msg_control
= NULL
;
59 msg
.msg_controllen
= 0;
62 return sendmsg(req
->originating_fd
, &msg
, 0);
65 static struct mconsole_command
*mconsole_parse(struct mc_request
*req
)
67 struct mconsole_command
*cmd
;
70 for(i
= 0; i
< ARRAY_SIZE(commands
); i
++){
72 if(!strncmp(req
->request
.data
, cmd
->command
,
73 strlen(cmd
->command
))){
80 #define MIN(a,b) ((a)<(b) ? (a):(b))
83 #define STRING(x) STRINGX(x)
85 int mconsole_get_request(int fd
, struct mc_request
*req
)
89 req
->originlen
= sizeof(req
->origin
);
90 req
->len
= recvfrom(fd
, &req
->request
, sizeof(req
->request
), 0,
91 (struct sockaddr
*) req
->origin
, &req
->originlen
);
95 req
->originating_fd
= fd
;
97 if(req
->request
.magic
!= MCONSOLE_MAGIC
){
98 /* Unversioned request */
99 len
= MIN(sizeof(req
->request
.data
) - 1,
100 strlen((char *) &req
->request
));
101 memmove(req
->request
.data
, &req
->request
, len
);
102 req
->request
.data
[len
] = '\0';
104 req
->request
.magic
= MCONSOLE_MAGIC
;
105 req
->request
.version
= 0;
106 req
->request
.len
= len
;
108 mconsole_reply_v0(req
, "ERR Version 0 mconsole clients are "
109 "not supported by this driver");
113 if(req
->request
.len
>= MCONSOLE_MAX_DATA
){
114 mconsole_reply(req
, "Request too large", 1, 0);
117 if(req
->request
.version
!= MCONSOLE_VERSION
){
118 mconsole_reply(req
, "This driver only supports version "
119 STRING(MCONSOLE_VERSION
) " clients", 1, 0);
122 req
->request
.data
[req
->request
.len
] = '\0';
123 req
->cmd
= mconsole_parse(req
);
124 if(req
->cmd
== NULL
){
125 mconsole_reply(req
, "Unknown command", 1, 0);
132 int mconsole_reply_len(struct mc_request
*req
, const char *str
, int total
,
135 /* XXX This is a stack consumption problem. It'd be nice to
136 * make it global and serialize access to it, but there are a
137 * ton of callers to this function.
139 struct mconsole_reply reply
;
145 /* err can only be true on the first packet */
148 len
= MIN(total
, MCONSOLE_MAX_DATA
- 1);
150 if(len
== total
) reply
.more
= more
;
153 memcpy(reply
.data
, str
, len
);
154 reply
.data
[len
] = '\0';
159 len
= sizeof(reply
) + reply
.len
- sizeof(reply
.data
);
161 n
= sendto(req
->originating_fd
, &reply
, len
, 0,
162 (struct sockaddr
*) req
->origin
, req
->originlen
);
164 if(n
< 0) return(-errno
);
169 int mconsole_reply(struct mc_request
*req
, const char *str
, int err
, int more
)
171 return mconsole_reply_len(req
, str
, strlen(str
), err
, more
);
175 int mconsole_unlink_socket(void)
177 unlink(mconsole_socket_name
);
181 static int notify_sock
= -1;
183 int mconsole_notify(char *sock_name
, int type
, const void *data
, int len
)
185 struct sockaddr_un target
;
186 struct mconsole_notify packet
;
191 notify_sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
194 printk("mconsole_notify - socket failed, errno = %d\n",
203 target
.sun_family
= AF_UNIX
;
204 strcpy(target
.sun_path
, sock_name
);
206 packet
.magic
= MCONSOLE_MAGIC
;
207 packet
.version
= MCONSOLE_VERSION
;
209 len
= (len
> sizeof(packet
.data
)) ? sizeof(packet
.data
) : len
;
211 memcpy(packet
.data
, data
, len
);
214 len
= sizeof(packet
) + packet
.len
- sizeof(packet
.data
);
215 n
= sendto(notify_sock
, &packet
, len
, 0, (struct sockaddr
*) &target
,
219 printk("mconsole_notify - sendto failed, errno = %d\n", errno
);
225 * Overrides for Emacs so that we follow Linus's tabbing style.
226 * Emacs will notice this stuff at the end of the file and automatically
227 * adjust the settings for this buffer only. This must remain at the end
229 * ---------------------------------------------------------------------------
231 * c-file-style: "linux"