Blame


1 fb1a36c0 2022-01-09 op /*
2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 fb1a36c0 2022-01-09 op *
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
7 fb1a36c0 2022-01-09 op *
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 fb1a36c0 2022-01-09 op */
16 fb1a36c0 2022-01-09 op
17 fb1a36c0 2022-01-09 op #include <sys/types.h>
18 fb1a36c0 2022-01-09 op #include <sys/queue.h>
19 fb1a36c0 2022-01-09 op #include <sys/uio.h>
20 fb1a36c0 2022-01-09 op
21 fb1a36c0 2022-01-09 op #include <event.h>
22 fb1a36c0 2022-01-09 op #include <ctype.h>
23 fb1a36c0 2022-01-09 op #include <stdint.h>
24 fb1a36c0 2022-01-09 op #include <stdio.h>
25 fb1a36c0 2022-01-09 op #include <stdlib.h>
26 fb1a36c0 2022-01-09 op #include <string.h>
27 fb1a36c0 2022-01-09 op #include <imsg.h>
28 fb1a36c0 2022-01-09 op
29 fb1a36c0 2022-01-09 op #include "kami.h"
30 fb1a36c0 2022-01-09 op #include "log.h"
31 fb1a36c0 2022-01-09 op #include "utils.h"
32 fb1a36c0 2022-01-09 op
33 fb1a36c0 2022-01-09 op void *
34 fb1a36c0 2022-01-09 op xmalloc(size_t size)
35 fb1a36c0 2022-01-09 op {
36 fb1a36c0 2022-01-09 op void *r;
37 fb1a36c0 2022-01-09 op
38 fb1a36c0 2022-01-09 op if ((r = malloc(size)) == NULL)
39 fb1a36c0 2022-01-09 op fatal("malloc");
40 fb1a36c0 2022-01-09 op return r;
41 fb1a36c0 2022-01-09 op }
42 fb1a36c0 2022-01-09 op
43 fb1a36c0 2022-01-09 op void *
44 fb1a36c0 2022-01-09 op xcalloc(size_t nmemb, size_t size)
45 fb1a36c0 2022-01-09 op {
46 fb1a36c0 2022-01-09 op void *r;
47 fb1a36c0 2022-01-09 op
48 fb1a36c0 2022-01-09 op if ((r = calloc(nmemb, size)) == NULL)
49 fb1a36c0 2022-01-09 op fatal("calloc");
50 fb1a36c0 2022-01-09 op return r;
51 fb1a36c0 2022-01-09 op }
52 fb1a36c0 2022-01-09 op
53 fb1a36c0 2022-01-09 op char *
54 fb1a36c0 2022-01-09 op xstrdup(const char *s)
55 fb1a36c0 2022-01-09 op {
56 fb1a36c0 2022-01-09 op char *r;
57 fb1a36c0 2022-01-09 op
58 fb1a36c0 2022-01-09 op if ((r = strdup(s)) == NULL)
59 fb1a36c0 2022-01-09 op fatal("strdup");
60 fb1a36c0 2022-01-09 op return r;
61 fb1a36c0 2022-01-09 op }
62 fb1a36c0 2022-01-09 op
63 fb1a36c0 2022-01-09 op void *
64 fb1a36c0 2022-01-09 op xmemdup(const void *d, size_t len)
65 fb1a36c0 2022-01-09 op {
66 fb1a36c0 2022-01-09 op void *r;
67 fb1a36c0 2022-01-09 op
68 fb1a36c0 2022-01-09 op if ((r = malloc(len)) == NULL)
69 fb1a36c0 2022-01-09 op fatal("malloc");
70 fb1a36c0 2022-01-09 op memcpy(r, d, len);
71 fb1a36c0 2022-01-09 op return r;
72 fb1a36c0 2022-01-09 op }
73 fb1a36c0 2022-01-09 op
74 fb1a36c0 2022-01-09 op const char *
75 fb1a36c0 2022-01-09 op pp_msg_type(uint8_t type)
76 fb1a36c0 2022-01-09 op {
77 fb1a36c0 2022-01-09 op switch (type) {
78 fb1a36c0 2022-01-09 op case Tversion: return "Tversion";
79 fb1a36c0 2022-01-09 op case Rversion: return "Rversion";
80 fb1a36c0 2022-01-09 op case Tauth: return "Tauth";
81 fb1a36c0 2022-01-09 op case Rauth: return "Rauth";
82 fb1a36c0 2022-01-09 op case Tattach: return "Tattach";
83 fb1a36c0 2022-01-09 op case Rattach: return "Rattach";
84 fb1a36c0 2022-01-09 op case Terror: return "Terror"; /* illegal */
85 fb1a36c0 2022-01-09 op case Rerror: return "Rerror";
86 fb1a36c0 2022-01-09 op case Tflush: return "Tflush";
87 fb1a36c0 2022-01-09 op case Rflush: return "Rflush";
88 fb1a36c0 2022-01-09 op case Twalk: return "Twalk";
89 fb1a36c0 2022-01-09 op case Rwalk: return "Rwalk";
90 fb1a36c0 2022-01-09 op case Topen: return "Topen";
91 fb1a36c0 2022-01-09 op case Ropen: return "Ropen";
92 fb1a36c0 2022-01-09 op case Tcreate: return "Tcreate";
93 fb1a36c0 2022-01-09 op case Rcreate: return "Rcreate";
94 fb1a36c0 2022-01-09 op case Tread: return "Tread";
95 fb1a36c0 2022-01-09 op case Rread: return "Rread";
96 fb1a36c0 2022-01-09 op case Twrite: return "Twrite";
97 fb1a36c0 2022-01-09 op case Rwrite: return "Rwrite";
98 fb1a36c0 2022-01-09 op case Tclunk: return "Tclunk";
99 fb1a36c0 2022-01-09 op case Rclunk: return "Rclunk";
100 fb1a36c0 2022-01-09 op case Tremove: return "Tremove";
101 fb1a36c0 2022-01-09 op case Rremove: return "Rremove";
102 fb1a36c0 2022-01-09 op case Tstat: return "Tstat";
103 fb1a36c0 2022-01-09 op case Rstat: return "Rstat";
104 fb1a36c0 2022-01-09 op case Twstat: return "Twstat";
105 fb1a36c0 2022-01-09 op case Rwstat: return "Rwstat";
106 fb1a36c0 2022-01-09 op default: return "unknown";
107 fb1a36c0 2022-01-09 op }
108 fb1a36c0 2022-01-09 op }
109 fb1a36c0 2022-01-09 op
110 fb1a36c0 2022-01-09 op const char *
111 fb1a36c0 2022-01-09 op pp_qid_type(uint8_t type)
112 fb1a36c0 2022-01-09 op {
113 fb1a36c0 2022-01-09 op switch (type) {
114 fb1a36c0 2022-01-09 op case QTDIR: return "dir";
115 fb1a36c0 2022-01-09 op case QTAPPEND: return "append-only";
116 fb1a36c0 2022-01-09 op case QTEXCL: return "exclusive";
117 fb1a36c0 2022-01-09 op case QTMOUNT: return "mounted-channel";
118 fb1a36c0 2022-01-09 op case QTAUTH: return "authentication";
119 fb1a36c0 2022-01-09 op case QTTMP: return "non-backed-up";
120 fb1a36c0 2022-01-09 op case QTSYMLINK: return "symlink";
121 fb1a36c0 2022-01-09 op case QTFILE: return "file";
122 fb1a36c0 2022-01-09 op }
123 fb1a36c0 2022-01-09 op
124 fb1a36c0 2022-01-09 op return "unknown";
125 fb1a36c0 2022-01-09 op }
126 fb1a36c0 2022-01-09 op
127 fb1a36c0 2022-01-09 op static void
128 fb1a36c0 2022-01-09 op hexdump_ppline(int x, uint8_t *data, size_t len)
129 fb1a36c0 2022-01-09 op {
130 fb1a36c0 2022-01-09 op for (; x < 50; x++)
131 fb1a36c0 2022-01-09 op printf(" ");
132 fb1a36c0 2022-01-09 op
133 fb1a36c0 2022-01-09 op printf("|");
134 fb1a36c0 2022-01-09 op
135 fb1a36c0 2022-01-09 op for (x = 0; x < (int)len; ++x) {
136 fb1a36c0 2022-01-09 op if (isgraph(data[x]))
137 fb1a36c0 2022-01-09 op printf("%c", data[x]);
138 fb1a36c0 2022-01-09 op else
139 fb1a36c0 2022-01-09 op printf(".");
140 fb1a36c0 2022-01-09 op }
141 fb1a36c0 2022-01-09 op
142 fb1a36c0 2022-01-09 op printf("|\n");
143 fb1a36c0 2022-01-09 op }
144 fb1a36c0 2022-01-09 op
145 fb1a36c0 2022-01-09 op void
146 fb1a36c0 2022-01-09 op hexdump(const char *label, uint8_t *data, size_t len)
147 fb1a36c0 2022-01-09 op {
148 fb1a36c0 2022-01-09 op size_t i;
149 fb1a36c0 2022-01-09 op int x, n;
150 fb1a36c0 2022-01-09 op
151 fb1a36c0 2022-01-09 op /*
152 fb1a36c0 2022-01-09 op * Layout:
153 fb1a36c0 2022-01-09 op * === first block === == second block == |........|\n
154 fb1a36c0 2022-01-09 op * first and second block are 8 bytes long (for a total of 48
155 fb1a36c0 2022-01-09 op * columns), plus two separator plus two | plus 16 chars, for
156 fb1a36c0 2022-01-09 op * a total of 68 characters.
157 fb1a36c0 2022-01-09 op */
158 fb1a36c0 2022-01-09 op
159 fb1a36c0 2022-01-09 op printf("\nhexdump \"%s\": (%zu bytes)\n", label, len);
160 fb1a36c0 2022-01-09 op for (x = 0, n = 0, i = 0; i < len; ++i) {
161 fb1a36c0 2022-01-09 op if (i != 0 && i % 8 == 0) {
162 fb1a36c0 2022-01-09 op printf(" ");
163 fb1a36c0 2022-01-09 op x++;
164 fb1a36c0 2022-01-09 op }
165 fb1a36c0 2022-01-09 op
166 fb1a36c0 2022-01-09 op if (n == 16) {
167 fb1a36c0 2022-01-09 op hexdump_ppline(x, &data[i - 16], 16);
168 fb1a36c0 2022-01-09 op x = 0;
169 fb1a36c0 2022-01-09 op n = 0;
170 fb1a36c0 2022-01-09 op }
171 fb1a36c0 2022-01-09 op
172 fb1a36c0 2022-01-09 op printf("%02x ", data[i]);
173 fb1a36c0 2022-01-09 op x += 3;
174 fb1a36c0 2022-01-09 op n++;
175 fb1a36c0 2022-01-09 op }
176 fb1a36c0 2022-01-09 op
177 fb1a36c0 2022-01-09 op if (n != 0)
178 fb1a36c0 2022-01-09 op hexdump_ppline(x, &data[i - n], n);
179 fb1a36c0 2022-01-09 op
180 fb1a36c0 2022-01-09 op printf("\n");
181 fb1a36c0 2022-01-09 op }
182 fb1a36c0 2022-01-09 op
183 fb1a36c0 2022-01-09 op void
184 fb1a36c0 2022-01-09 op imsg_event_add(struct imsgev *iev)
185 fb1a36c0 2022-01-09 op {
186 fb1a36c0 2022-01-09 op iev->events = EV_READ;
187 fb1a36c0 2022-01-09 op if (iev->ibuf.w.queued)
188 fb1a36c0 2022-01-09 op iev->events |= EV_WRITE;
189 fb1a36c0 2022-01-09 op
190 fb1a36c0 2022-01-09 op event_del(&iev->ev);
191 fb1a36c0 2022-01-09 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
192 fb1a36c0 2022-01-09 op event_add(&iev->ev, NULL);
193 fb1a36c0 2022-01-09 op }
194 fb1a36c0 2022-01-09 op
195 fb1a36c0 2022-01-09 op int
196 fb1a36c0 2022-01-09 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
197 fb1a36c0 2022-01-09 op pid_t pid, int fd, const void *data, uint16_t datalen)
198 fb1a36c0 2022-01-09 op {
199 fb1a36c0 2022-01-09 op int ret;
200 fb1a36c0 2022-01-09 op
201 fb1a36c0 2022-01-09 op if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
202 fb1a36c0 2022-01-09 op datalen) != -1))
203 fb1a36c0 2022-01-09 op imsg_event_add(iev);
204 fb1a36c0 2022-01-09 op
205 fb1a36c0 2022-01-09 op return ret;
206 fb1a36c0 2022-01-09 op }