| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" | 
| Al Viro | 3a51237 | 2006-10-24 11:15:29 +0100 | [diff] [blame] | 17 | #include "sysdep/ptrace.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "mconsole.h" | 
| Jeff Dike | de5fe76 | 2007-02-10 01:44:25 -0800 | [diff] [blame] | 19 | #include "os.h" | 
| Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 20 | #include "user_util.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 |  | 
 | 22 | static struct mconsole_command commands[] = { | 
| Jeff Dike | 2d77f6f | 2006-07-10 04:45:16 -0700 | [diff] [blame] | 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 | 
 | 26 | 	 * namespace. | 
 | 27 | 	 */ | 
 | 28 | 	{ "version", mconsole_version, MCONSOLE_PROC }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | 	{ "halt", mconsole_halt, MCONSOLE_PROC }, | 
 | 30 | 	{ "reboot", mconsole_reboot, MCONSOLE_PROC }, | 
 | 31 | 	{ "config", mconsole_config, MCONSOLE_PROC }, | 
 | 32 | 	{ "remove", mconsole_remove, MCONSOLE_PROC }, | 
| Paolo 'Blaisorblade' Giarrusso | bd94805 | 2005-09-30 11:59:00 -0700 | [diff] [blame] | 33 | 	{ "sysrq", mconsole_sysrq, MCONSOLE_INTR }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 }, | 
| Jeff Dike | 3eddddc | 2005-09-16 19:27:46 -0700 | [diff] [blame] | 40 |         { "stack", mconsole_stack, MCONSOLE_INTR }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | }; | 
 | 42 |  | 
 | 43 | /* Initialized in mconsole_init, which is an initcall */ | 
 | 44 | char mconsole_socket_name[256]; | 
 | 45 |  | 
 | 46 | int mconsole_reply_v0(struct mc_request *req, char *reply) | 
 | 47 | { | 
 | 48 |         struct iovec iov; | 
 | 49 |         struct msghdr msg; | 
 | 50 |  | 
 | 51 |         iov.iov_base = reply; | 
 | 52 |         iov.iov_len = strlen(reply); | 
 | 53 |  | 
 | 54 |         msg.msg_name = &(req->origin); | 
 | 55 |         msg.msg_namelen = req->originlen; | 
 | 56 |         msg.msg_iov = &iov; | 
 | 57 |         msg.msg_iovlen = 1; | 
 | 58 |         msg.msg_control = NULL; | 
 | 59 |         msg.msg_controllen = 0; | 
 | 60 |         msg.msg_flags = 0; | 
 | 61 |  | 
 | 62 |         return sendmsg(req->originating_fd, &msg, 0); | 
 | 63 | } | 
 | 64 |  | 
 | 65 | static struct mconsole_command *mconsole_parse(struct mc_request *req) | 
 | 66 | { | 
 | 67 | 	struct mconsole_command *cmd; | 
 | 68 | 	int i; | 
 | 69 |  | 
| Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 70 | 	for(i = 0; i < ARRAY_SIZE(commands); i++){ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | 		cmd = &commands[i]; | 
 | 72 | 		if(!strncmp(req->request.data, cmd->command,  | 
 | 73 | 			    strlen(cmd->command))){ | 
| Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 74 | 			return cmd; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | 		} | 
 | 76 | 	} | 
| Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 77 | 	return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } | 
 | 79 |  | 
 | 80 | #define MIN(a,b) ((a)<(b) ? (a):(b)) | 
 | 81 |  | 
 | 82 | #define STRINGX(x) #x | 
 | 83 | #define STRING(x) STRINGX(x) | 
 | 84 |  | 
 | 85 | int mconsole_get_request(int fd, struct mc_request *req) | 
 | 86 | { | 
 | 87 | 	int len; | 
 | 88 |  | 
 | 89 | 	req->originlen = sizeof(req->origin); | 
 | 90 | 	req->len = recvfrom(fd, &req->request, sizeof(req->request), 0, | 
 | 91 | 			    (struct sockaddr *) req->origin, &req->originlen); | 
 | 92 | 	if (req->len < 0) | 
 | 93 | 		return 0; | 
 | 94 |  | 
 | 95 | 	req->originating_fd = fd; | 
 | 96 |  | 
 | 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'; | 
 | 103 |  | 
 | 104 | 		req->request.magic = MCONSOLE_MAGIC; | 
 | 105 | 		req->request.version = 0; | 
 | 106 | 		req->request.len = len; | 
 | 107 |  | 
 | 108 | 		mconsole_reply_v0(req, "ERR Version 0 mconsole clients are " | 
 | 109 | 				  "not supported by this driver"); | 
 | 110 | 		return(0); | 
 | 111 | 	} | 
 | 112 |  | 
 | 113 | 	if(req->request.len >= MCONSOLE_MAX_DATA){ | 
 | 114 | 		mconsole_reply(req, "Request too large", 1, 0); | 
 | 115 | 		return(0); | 
 | 116 | 	} | 
 | 117 | 	if(req->request.version != MCONSOLE_VERSION){ | 
 | 118 | 		mconsole_reply(req, "This driver only supports version "  | 
 | 119 |                                STRING(MCONSOLE_VERSION) " clients", 1, 0); | 
 | 120 | 	} | 
 | 121 | 	 | 
 | 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); | 
 | 126 | 		return(0); | 
 | 127 | 	} | 
 | 128 |  | 
 | 129 | 	return(1); | 
 | 130 | } | 
 | 131 |  | 
| Jeff Dike | 7b033e1 | 2006-01-06 00:19:03 -0800 | [diff] [blame] | 132 | int mconsole_reply_len(struct mc_request *req, const char *str, int total, | 
 | 133 | 		       int err, int more) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { | 
| Jeff Dike | f92afe5 | 2006-09-29 01:58:52 -0700 | [diff] [blame] | 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. | 
 | 138 | 	 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | 	struct mconsole_reply reply; | 
| Jeff Dike | 7b033e1 | 2006-01-06 00:19:03 -0800 | [diff] [blame] | 140 | 	int len, n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | 	do { | 
 | 143 | 		reply.err = err; | 
 | 144 |  | 
 | 145 | 		/* err can only be true on the first packet */ | 
 | 146 | 		err = 0; | 
 | 147 |  | 
 | 148 | 		len = MIN(total, MCONSOLE_MAX_DATA - 1); | 
 | 149 |  | 
 | 150 | 		if(len == total) reply.more = more; | 
 | 151 | 		else reply.more = 1; | 
 | 152 |  | 
 | 153 | 		memcpy(reply.data, str, len); | 
 | 154 | 		reply.data[len] = '\0'; | 
 | 155 | 		total -= len; | 
 | 156 | 		str += len; | 
 | 157 | 		reply.len = len + 1; | 
 | 158 |  | 
 | 159 | 		len = sizeof(reply) + reply.len - sizeof(reply.data); | 
 | 160 |  | 
 | 161 | 		n = sendto(req->originating_fd, &reply, len, 0, | 
 | 162 | 			   (struct sockaddr *) req->origin, req->originlen); | 
 | 163 |  | 
 | 164 | 		if(n < 0) return(-errno); | 
 | 165 | 	} while(total > 0); | 
 | 166 | 	return(0); | 
 | 167 | } | 
 | 168 |  | 
| Jeff Dike | 7b033e1 | 2006-01-06 00:19:03 -0800 | [diff] [blame] | 169 | int mconsole_reply(struct mc_request *req, const char *str, int err, int more) | 
 | 170 | { | 
 | 171 | 	return mconsole_reply_len(req, str, strlen(str), err, more); | 
 | 172 | } | 
 | 173 |  | 
 | 174 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | int mconsole_unlink_socket(void) | 
 | 176 | { | 
 | 177 | 	unlink(mconsole_socket_name); | 
 | 178 | 	return 0; | 
 | 179 | } | 
 | 180 |  | 
 | 181 | static int notify_sock = -1; | 
 | 182 |  | 
 | 183 | int mconsole_notify(char *sock_name, int type, const void *data, int len) | 
 | 184 | { | 
 | 185 | 	struct sockaddr_un target; | 
 | 186 | 	struct mconsole_notify packet; | 
 | 187 | 	int n, err = 0; | 
 | 188 |  | 
 | 189 | 	lock_notify(); | 
 | 190 | 	if(notify_sock < 0){ | 
 | 191 | 		notify_sock = socket(PF_UNIX, SOCK_DGRAM, 0); | 
 | 192 | 		if(notify_sock < 0){ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | 			err = -errno; | 
| Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 194 | 			printk("mconsole_notify - socket failed, errno = %d\n", | 
 | 195 | 			       err); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | 		} | 
 | 197 | 	} | 
 | 198 | 	unlock_notify(); | 
 | 199 | 	 | 
 | 200 | 	if(err) | 
 | 201 | 		return(err); | 
 | 202 |  | 
 | 203 | 	target.sun_family = AF_UNIX; | 
 | 204 | 	strcpy(target.sun_path, sock_name); | 
 | 205 |  | 
 | 206 | 	packet.magic = MCONSOLE_MAGIC; | 
 | 207 | 	packet.version = MCONSOLE_VERSION; | 
 | 208 | 	packet.type = type; | 
 | 209 | 	len = (len > sizeof(packet.data)) ? sizeof(packet.data) : len; | 
 | 210 | 	packet.len = len; | 
 | 211 | 	memcpy(packet.data, data, len); | 
 | 212 |  | 
 | 213 | 	err = 0; | 
 | 214 | 	len = sizeof(packet) + packet.len - sizeof(packet.data); | 
 | 215 | 	n = sendto(notify_sock, &packet, len, 0, (struct sockaddr *) &target,  | 
 | 216 | 		   sizeof(target)); | 
 | 217 | 	if(n < 0){ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | 		err = -errno; | 
| Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 219 | 		printk("mconsole_notify - sendto failed, errno = %d\n", errno); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | 	} | 
 | 221 | 	return(err); | 
 | 222 | } | 
 | 223 |  | 
 | 224 | /* | 
 | 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 | 
 | 228 |  * of the file. | 
 | 229 |  * --------------------------------------------------------------------------- | 
 | 230 |  * Local variables: | 
 | 231 |  * c-file-style: "linux" | 
 | 232 |  * End: | 
 | 233 |  */ |