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