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 bbcba3ed 2022-01-10 op #include "compat.h"
18 bbcba3ed 2022-01-10 op
19 fb1a36c0 2022-01-09 op #include <sys/types.h>
20 fb1a36c0 2022-01-09 op #include <sys/uio.h>
21 fb1a36c0 2022-01-09 op
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
28 fb1a36c0 2022-01-09 op #include "kami.h"
29 fb1a36c0 2022-01-09 op #include "log.h"
30 fb1a36c0 2022-01-09 op #include "utils.h"
31 fb1a36c0 2022-01-09 op
32 fb1a36c0 2022-01-09 op void *
33 fb1a36c0 2022-01-09 op xmalloc(size_t size)
34 fb1a36c0 2022-01-09 op {
35 fb1a36c0 2022-01-09 op void *r;
36 fb1a36c0 2022-01-09 op
37 fb1a36c0 2022-01-09 op if ((r = malloc(size)) == NULL)
38 fb1a36c0 2022-01-09 op fatal("malloc");
39 fb1a36c0 2022-01-09 op return r;
40 fb1a36c0 2022-01-09 op }
41 fb1a36c0 2022-01-09 op
42 fb1a36c0 2022-01-09 op void *
43 fb1a36c0 2022-01-09 op xcalloc(size_t nmemb, size_t size)
44 fb1a36c0 2022-01-09 op {
45 fb1a36c0 2022-01-09 op void *r;
46 fb1a36c0 2022-01-09 op
47 fb1a36c0 2022-01-09 op if ((r = calloc(nmemb, size)) == NULL)
48 fb1a36c0 2022-01-09 op fatal("calloc");
49 fb1a36c0 2022-01-09 op return r;
50 fb1a36c0 2022-01-09 op }
51 fb1a36c0 2022-01-09 op
52 fb1a36c0 2022-01-09 op char *
53 fb1a36c0 2022-01-09 op xstrdup(const char *s)
54 fb1a36c0 2022-01-09 op {
55 fb1a36c0 2022-01-09 op char *r;
56 fb1a36c0 2022-01-09 op
57 fb1a36c0 2022-01-09 op if ((r = strdup(s)) == NULL)
58 fb1a36c0 2022-01-09 op fatal("strdup");
59 fb1a36c0 2022-01-09 op return r;
60 fb1a36c0 2022-01-09 op }
61 fb1a36c0 2022-01-09 op
62 fb1a36c0 2022-01-09 op void *
63 fb1a36c0 2022-01-09 op xmemdup(const void *d, size_t len)
64 fb1a36c0 2022-01-09 op {
65 fb1a36c0 2022-01-09 op void *r;
66 fb1a36c0 2022-01-09 op
67 fb1a36c0 2022-01-09 op if ((r = malloc(len)) == NULL)
68 fb1a36c0 2022-01-09 op fatal("malloc");
69 fb1a36c0 2022-01-09 op memcpy(r, d, len);
70 fb1a36c0 2022-01-09 op return r;
71 fb1a36c0 2022-01-09 op }
72 fb1a36c0 2022-01-09 op
73 fb1a36c0 2022-01-09 op const char *
74 fb1a36c0 2022-01-09 op pp_msg_type(uint8_t type)
75 fb1a36c0 2022-01-09 op {
76 fb1a36c0 2022-01-09 op switch (type) {
77 fb1a36c0 2022-01-09 op case Tversion: return "Tversion";
78 fb1a36c0 2022-01-09 op case Rversion: return "Rversion";
79 fb1a36c0 2022-01-09 op case Tauth: return "Tauth";
80 fb1a36c0 2022-01-09 op case Rauth: return "Rauth";
81 fb1a36c0 2022-01-09 op case Tattach: return "Tattach";
82 fb1a36c0 2022-01-09 op case Rattach: return "Rattach";
83 fb1a36c0 2022-01-09 op case Terror: return "Terror"; /* illegal */
84 fb1a36c0 2022-01-09 op case Rerror: return "Rerror";
85 fb1a36c0 2022-01-09 op case Tflush: return "Tflush";
86 fb1a36c0 2022-01-09 op case Rflush: return "Rflush";
87 fb1a36c0 2022-01-09 op case Twalk: return "Twalk";
88 fb1a36c0 2022-01-09 op case Rwalk: return "Rwalk";
89 fb1a36c0 2022-01-09 op case Topen: return "Topen";
90 fb1a36c0 2022-01-09 op case Ropen: return "Ropen";
91 fb1a36c0 2022-01-09 op case Tcreate: return "Tcreate";
92 fb1a36c0 2022-01-09 op case Rcreate: return "Rcreate";
93 fb1a36c0 2022-01-09 op case Tread: return "Tread";
94 fb1a36c0 2022-01-09 op case Rread: return "Rread";
95 fb1a36c0 2022-01-09 op case Twrite: return "Twrite";
96 fb1a36c0 2022-01-09 op case Rwrite: return "Rwrite";
97 fb1a36c0 2022-01-09 op case Tclunk: return "Tclunk";
98 fb1a36c0 2022-01-09 op case Rclunk: return "Rclunk";
99 fb1a36c0 2022-01-09 op case Tremove: return "Tremove";
100 fb1a36c0 2022-01-09 op case Rremove: return "Rremove";
101 fb1a36c0 2022-01-09 op case Tstat: return "Tstat";
102 fb1a36c0 2022-01-09 op case Rstat: return "Rstat";
103 fb1a36c0 2022-01-09 op case Twstat: return "Twstat";
104 fb1a36c0 2022-01-09 op case Rwstat: return "Rwstat";
105 fb1a36c0 2022-01-09 op default: return "unknown";
106 fb1a36c0 2022-01-09 op }
107 fb1a36c0 2022-01-09 op }
108 fb1a36c0 2022-01-09 op
109 fb1a36c0 2022-01-09 op const char *
110 fb1a36c0 2022-01-09 op pp_qid_type(uint8_t type)
111 fb1a36c0 2022-01-09 op {
112 fb1a36c0 2022-01-09 op switch (type) {
113 fb1a36c0 2022-01-09 op case QTDIR: return "dir";
114 fb1a36c0 2022-01-09 op case QTAPPEND: return "append-only";
115 fb1a36c0 2022-01-09 op case QTEXCL: return "exclusive";
116 fb1a36c0 2022-01-09 op case QTMOUNT: return "mounted-channel";
117 fb1a36c0 2022-01-09 op case QTAUTH: return "authentication";
118 fb1a36c0 2022-01-09 op case QTTMP: return "non-backed-up";
119 fb1a36c0 2022-01-09 op case QTSYMLINK: return "symlink";
120 fb1a36c0 2022-01-09 op case QTFILE: return "file";
121 fb1a36c0 2022-01-09 op }
122 fb1a36c0 2022-01-09 op
123 fb1a36c0 2022-01-09 op return "unknown";
124 fb1a36c0 2022-01-09 op }
125 fb1a36c0 2022-01-09 op
126 fb1a36c0 2022-01-09 op static void
127 fb1a36c0 2022-01-09 op hexdump_ppline(int x, uint8_t *data, size_t len)
128 fb1a36c0 2022-01-09 op {
129 fb1a36c0 2022-01-09 op for (; x < 50; x++)
130 fb1a36c0 2022-01-09 op printf(" ");
131 fb1a36c0 2022-01-09 op
132 fb1a36c0 2022-01-09 op printf("|");
133 fb1a36c0 2022-01-09 op
134 fb1a36c0 2022-01-09 op for (x = 0; x < (int)len; ++x) {
135 fb1a36c0 2022-01-09 op if (isgraph(data[x]))
136 fb1a36c0 2022-01-09 op printf("%c", data[x]);
137 fb1a36c0 2022-01-09 op else
138 fb1a36c0 2022-01-09 op printf(".");
139 fb1a36c0 2022-01-09 op }
140 fb1a36c0 2022-01-09 op
141 fb1a36c0 2022-01-09 op printf("|\n");
142 fb1a36c0 2022-01-09 op }
143 fb1a36c0 2022-01-09 op
144 fb1a36c0 2022-01-09 op void
145 fb1a36c0 2022-01-09 op hexdump(const char *label, uint8_t *data, size_t len)
146 fb1a36c0 2022-01-09 op {
147 fb1a36c0 2022-01-09 op size_t i;
148 fb1a36c0 2022-01-09 op int x, n;
149 fb1a36c0 2022-01-09 op
150 fb1a36c0 2022-01-09 op /*
151 fb1a36c0 2022-01-09 op * Layout:
152 fb1a36c0 2022-01-09 op * === first block === == second block == |........|\n
153 fb1a36c0 2022-01-09 op * first and second block are 8 bytes long (for a total of 48
154 fb1a36c0 2022-01-09 op * columns), plus two separator plus two | plus 16 chars, for
155 fb1a36c0 2022-01-09 op * a total of 68 characters.
156 fb1a36c0 2022-01-09 op */
157 fb1a36c0 2022-01-09 op
158 fb1a36c0 2022-01-09 op printf("\nhexdump \"%s\": (%zu bytes)\n", label, len);
159 fb1a36c0 2022-01-09 op for (x = 0, n = 0, i = 0; i < len; ++i) {
160 fb1a36c0 2022-01-09 op if (i != 0 && i % 8 == 0) {
161 fb1a36c0 2022-01-09 op printf(" ");
162 fb1a36c0 2022-01-09 op x++;
163 fb1a36c0 2022-01-09 op }
164 fb1a36c0 2022-01-09 op
165 fb1a36c0 2022-01-09 op if (n == 16) {
166 fb1a36c0 2022-01-09 op hexdump_ppline(x, &data[i - 16], 16);
167 fb1a36c0 2022-01-09 op x = 0;
168 fb1a36c0 2022-01-09 op n = 0;
169 fb1a36c0 2022-01-09 op }
170 fb1a36c0 2022-01-09 op
171 fb1a36c0 2022-01-09 op printf("%02x ", data[i]);
172 fb1a36c0 2022-01-09 op x += 3;
173 fb1a36c0 2022-01-09 op n++;
174 fb1a36c0 2022-01-09 op }
175 fb1a36c0 2022-01-09 op
176 fb1a36c0 2022-01-09 op if (n != 0)
177 fb1a36c0 2022-01-09 op hexdump_ppline(x, &data[i - n], n);
178 fb1a36c0 2022-01-09 op
179 fb1a36c0 2022-01-09 op printf("\n");
180 72d6dda0 2022-05-23 op fflush(stdout);
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 }