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