^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <sys/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <sys/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <sys/un.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "mconsole.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static struct mconsole_command commands[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * With uts namespaces, uts information becomes process-specific, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * we need a process context. If we try handling this in interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * context, we may hit an exiting process without a valid uts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) { "version", mconsole_version, MCONSOLE_PROC },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) { "halt", mconsole_halt, MCONSOLE_PROC },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) { "reboot", mconsole_reboot, MCONSOLE_PROC },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) { "config", mconsole_config, MCONSOLE_PROC },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) { "remove", mconsole_remove, MCONSOLE_PROC },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) { "sysrq", mconsole_sysrq, MCONSOLE_INTR },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) { "help", mconsole_help, MCONSOLE_INTR },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) { "cad", mconsole_cad, MCONSOLE_INTR },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) { "stop", mconsole_stop, MCONSOLE_PROC },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) { "go", mconsole_go, MCONSOLE_INTR },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) { "log", mconsole_log, MCONSOLE_INTR },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) { "proc", mconsole_proc, MCONSOLE_PROC },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) { "stack", mconsole_stack, MCONSOLE_INTR },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* Initialized in mconsole_init, which is an initcall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) char mconsole_socket_name[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static int mconsole_reply_v0(struct mc_request *req, char *reply)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct iovec iov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct msghdr msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) iov.iov_base = reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) iov.iov_len = strlen(reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) msg.msg_name = &(req->origin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) msg.msg_namelen = req->originlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) msg.msg_iov = &iov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) msg.msg_iovlen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) msg.msg_control = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) msg.msg_controllen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) msg.msg_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return sendmsg(req->originating_fd, &msg, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static struct mconsole_command *mconsole_parse(struct mc_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct mconsole_command *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) for (i = 0; i < ARRAY_SIZE(commands); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) cmd = &commands[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!strncmp(req->request.data, cmd->command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) strlen(cmd->command))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define MIN(a,b) ((a)<(b) ? (a):(b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define STRINGX(x) #x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define STRING(x) STRINGX(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int mconsole_get_request(int fd, struct mc_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) req->originlen = sizeof(req->origin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) req->len = recvfrom(fd, &req->request, sizeof(req->request), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) (struct sockaddr *) req->origin, &req->originlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (req->len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) req->originating_fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (req->request.magic != MCONSOLE_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* Unversioned request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) len = MIN(sizeof(req->request.data) - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) strlen((char *) &req->request));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) memmove(req->request.data, &req->request, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) req->request.data[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) req->request.magic = MCONSOLE_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) req->request.version = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) req->request.len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) mconsole_reply_v0(req, "ERR Version 0 mconsole clients are "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) "not supported by this driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (req->request.len >= MCONSOLE_MAX_DATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) mconsole_reply(req, "Request too large", 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (req->request.version != MCONSOLE_VERSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) mconsole_reply(req, "This driver only supports version "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) STRING(MCONSOLE_VERSION) " clients", 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) req->request.data[req->request.len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) req->cmd = mconsole_parse(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (req->cmd == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) mconsole_reply(req, "Unknown command", 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int mconsole_reply_len(struct mc_request *req, const char *str, int total,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int err, int more)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * XXX This is a stack consumption problem. It'd be nice to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * make it global and serialize access to it, but there are a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * ton of callers to this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct mconsole_reply reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int len, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) reply.err = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* err can only be true on the first packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) len = MIN(total, MCONSOLE_MAX_DATA - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (len == total) reply.more = more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) else reply.more = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) memcpy(reply.data, str, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) reply.data[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) total -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) str += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) reply.len = len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) len = sizeof(reply) + reply.len - sizeof(reply.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) n = sendto(req->originating_fd, &reply, len, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) (struct sockaddr *) req->origin, req->originlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (n < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) } while (total > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int mconsole_reply(struct mc_request *req, const char *str, int err, int more)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return mconsole_reply_len(req, str, strlen(str), err, more);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) int mconsole_unlink_socket(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) unlink(mconsole_socket_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static int notify_sock = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int mconsole_notify(char *sock_name, int type, const void *data, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct sockaddr_un target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct mconsole_notify packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int n, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) lock_notify();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (notify_sock < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) notify_sock = socket(PF_UNIX, SOCK_DGRAM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (notify_sock < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) printk(UM_KERN_ERR "mconsole_notify - socket failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) "errno = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) unlock_notify();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) target.sun_family = AF_UNIX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) strcpy(target.sun_path, sock_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) packet.magic = MCONSOLE_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) packet.version = MCONSOLE_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) packet.type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) len = (len > sizeof(packet.data)) ? sizeof(packet.data) : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) packet.len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) memcpy(packet.data, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) len = sizeof(packet) + packet.len - sizeof(packet.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) n = sendto(notify_sock, &packet, len, 0, (struct sockaddr *) &target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) sizeof(target));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (n < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) printk(UM_KERN_ERR "mconsole_notify - sendto failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) "errno = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }