diff options
Diffstat (limited to 'arch/um/drivers/mconsole_user.c')
-rw-r--r-- | arch/um/drivers/mconsole_user.c | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/arch/um/drivers/mconsole_user.c b/arch/um/drivers/mconsole_user.c new file mode 100644 index 000000000000..fe5afb13252c --- /dev/null +++ b/arch/um/drivers/mconsole_user.c | |||
@@ -0,0 +1,215 @@ | |||
1 | /* | ||
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 | |||
20 | static struct mconsole_command commands[] = { | ||
21 | { "version", mconsole_version, MCONSOLE_INTR }, | ||
22 | { "halt", mconsole_halt, MCONSOLE_PROC }, | ||
23 | { "reboot", mconsole_reboot, MCONSOLE_PROC }, | ||
24 | { "config", mconsole_config, MCONSOLE_PROC }, | ||
25 | { "remove", mconsole_remove, MCONSOLE_PROC }, | ||
26 | { "sysrq", mconsole_sysrq, MCONSOLE_INTR }, | ||
27 | { "help", mconsole_help, MCONSOLE_INTR }, | ||
28 | { "cad", mconsole_cad, MCONSOLE_INTR }, | ||
29 | { "stop", mconsole_stop, MCONSOLE_PROC }, | ||
30 | { "go", mconsole_go, MCONSOLE_INTR }, | ||
31 | { "log", mconsole_log, MCONSOLE_INTR }, | ||
32 | { "proc", mconsole_proc, MCONSOLE_PROC }, | ||
33 | }; | ||
34 | |||
35 | /* Initialized in mconsole_init, which is an initcall */ | ||
36 | char mconsole_socket_name[256]; | ||
37 | |||
38 | int mconsole_reply_v0(struct mc_request *req, char *reply) | ||
39 | { | ||
40 | struct iovec iov; | ||
41 | struct msghdr msg; | ||
42 | |||
43 | iov.iov_base = reply; | ||
44 | iov.iov_len = strlen(reply); | ||
45 | |||
46 | msg.msg_name = &(req->origin); | ||
47 | msg.msg_namelen = req->originlen; | ||
48 | msg.msg_iov = &iov; | ||
49 | msg.msg_iovlen = 1; | ||
50 | msg.msg_control = NULL; | ||
51 | msg.msg_controllen = 0; | ||
52 | msg.msg_flags = 0; | ||
53 | |||
54 | return sendmsg(req->originating_fd, &msg, 0); | ||
55 | } | ||
56 | |||
57 | static struct mconsole_command *mconsole_parse(struct mc_request *req) | ||
58 | { | ||
59 | struct mconsole_command *cmd; | ||
60 | int i; | ||
61 | |||
62 | for(i=0;i<sizeof(commands)/sizeof(commands[0]);i++){ | ||
63 | cmd = &commands[i]; | ||
64 | if(!strncmp(req->request.data, cmd->command, | ||
65 | strlen(cmd->command))){ | ||
66 | return(cmd); | ||
67 | } | ||
68 | } | ||
69 | return(NULL); | ||
70 | } | ||
71 | |||
72 | #define MIN(a,b) ((a)<(b) ? (a):(b)) | ||
73 | |||
74 | #define STRINGX(x) #x | ||
75 | #define STRING(x) STRINGX(x) | ||
76 | |||
77 | int mconsole_get_request(int fd, struct mc_request *req) | ||
78 | { | ||
79 | int len; | ||
80 | |||
81 | req->originlen = sizeof(req->origin); | ||
82 | req->len = recvfrom(fd, &req->request, sizeof(req->request), 0, | ||
83 | (struct sockaddr *) req->origin, &req->originlen); | ||
84 | if (req->len < 0) | ||
85 | return 0; | ||
86 | |||
87 | req->originating_fd = fd; | ||
88 | |||
89 | if(req->request.magic != MCONSOLE_MAGIC){ | ||
90 | /* Unversioned request */ | ||
91 | len = MIN(sizeof(req->request.data) - 1, | ||
92 | strlen((char *) &req->request)); | ||
93 | memmove(req->request.data, &req->request, len); | ||
94 | req->request.data[len] = '\0'; | ||
95 | |||
96 | req->request.magic = MCONSOLE_MAGIC; | ||
97 | req->request.version = 0; | ||
98 | req->request.len = len; | ||
99 | |||
100 | mconsole_reply_v0(req, "ERR Version 0 mconsole clients are " | ||
101 | "not supported by this driver"); | ||
102 | return(0); | ||
103 | } | ||
104 | |||
105 | if(req->request.len >= MCONSOLE_MAX_DATA){ | ||
106 | mconsole_reply(req, "Request too large", 1, 0); | ||
107 | return(0); | ||
108 | } | ||
109 | if(req->request.version != MCONSOLE_VERSION){ | ||
110 | mconsole_reply(req, "This driver only supports version " | ||
111 | STRING(MCONSOLE_VERSION) " clients", 1, 0); | ||
112 | } | ||
113 | |||
114 | req->request.data[req->request.len] = '\0'; | ||
115 | req->cmd = mconsole_parse(req); | ||
116 | if(req->cmd == NULL){ | ||
117 | mconsole_reply(req, "Unknown command", 1, 0); | ||
118 | return(0); | ||
119 | } | ||
120 | |||
121 | return(1); | ||
122 | } | ||
123 | |||
124 | int mconsole_reply(struct mc_request *req, char *str, int err, int more) | ||
125 | { | ||
126 | struct mconsole_reply reply; | ||
127 | int total, len, n; | ||
128 | |||
129 | total = strlen(str); | ||
130 | do { | ||
131 | reply.err = err; | ||
132 | |||
133 | /* err can only be true on the first packet */ | ||
134 | err = 0; | ||
135 | |||
136 | len = MIN(total, MCONSOLE_MAX_DATA - 1); | ||
137 | |||
138 | if(len == total) reply.more = more; | ||
139 | else reply.more = 1; | ||
140 | |||
141 | memcpy(reply.data, str, len); | ||
142 | reply.data[len] = '\0'; | ||
143 | total -= len; | ||
144 | str += len; | ||
145 | reply.len = len + 1; | ||
146 | |||
147 | len = sizeof(reply) + reply.len - sizeof(reply.data); | ||
148 | |||
149 | n = sendto(req->originating_fd, &reply, len, 0, | ||
150 | (struct sockaddr *) req->origin, req->originlen); | ||
151 | |||
152 | if(n < 0) return(-errno); | ||
153 | } while(total > 0); | ||
154 | return(0); | ||
155 | } | ||
156 | |||
157 | int mconsole_unlink_socket(void) | ||
158 | { | ||
159 | unlink(mconsole_socket_name); | ||
160 | return 0; | ||
161 | } | ||
162 | |||
163 | static int notify_sock = -1; | ||
164 | |||
165 | int mconsole_notify(char *sock_name, int type, const void *data, int len) | ||
166 | { | ||
167 | struct sockaddr_un target; | ||
168 | struct mconsole_notify packet; | ||
169 | int n, err = 0; | ||
170 | |||
171 | lock_notify(); | ||
172 | if(notify_sock < 0){ | ||
173 | notify_sock = socket(PF_UNIX, SOCK_DGRAM, 0); | ||
174 | if(notify_sock < 0){ | ||
175 | printk("mconsole_notify - socket failed, errno = %d\n", | ||
176 | errno); | ||
177 | err = -errno; | ||
178 | } | ||
179 | } | ||
180 | unlock_notify(); | ||
181 | |||
182 | if(err) | ||
183 | return(err); | ||
184 | |||
185 | target.sun_family = AF_UNIX; | ||
186 | strcpy(target.sun_path, sock_name); | ||
187 | |||
188 | packet.magic = MCONSOLE_MAGIC; | ||
189 | packet.version = MCONSOLE_VERSION; | ||
190 | packet.type = type; | ||
191 | len = (len > sizeof(packet.data)) ? sizeof(packet.data) : len; | ||
192 | packet.len = len; | ||
193 | memcpy(packet.data, data, len); | ||
194 | |||
195 | err = 0; | ||
196 | len = sizeof(packet) + packet.len - sizeof(packet.data); | ||
197 | n = sendto(notify_sock, &packet, len, 0, (struct sockaddr *) &target, | ||
198 | sizeof(target)); | ||
199 | if(n < 0){ | ||
200 | printk("mconsole_notify - sendto failed, errno = %d\n", errno); | ||
201 | err = -errno; | ||
202 | } | ||
203 | return(err); | ||
204 | } | ||
205 | |||
206 | /* | ||
207 | * Overrides for Emacs so that we follow Linus's tabbing style. | ||
208 | * Emacs will notice this stuff at the end of the file and automatically | ||
209 | * adjust the settings for this buffer only. This must remain at the end | ||
210 | * of the file. | ||
211 | * --------------------------------------------------------------------------- | ||
212 | * Local variables: | ||
213 | * c-file-style: "linux" | ||
214 | * End: | ||
215 | */ | ||