2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
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>
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>
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"
34 fb1a36c0 2022-01-09 op xmalloc(size_t size)
38 fb1a36c0 2022-01-09 op if ((r = malloc(size)) == NULL)
39 fb1a36c0 2022-01-09 op fatal("malloc");
44 fb1a36c0 2022-01-09 op xcalloc(size_t nmemb, size_t size)
48 fb1a36c0 2022-01-09 op if ((r = calloc(nmemb, size)) == NULL)
49 fb1a36c0 2022-01-09 op fatal("calloc");
54 fb1a36c0 2022-01-09 op xstrdup(const char *s)
58 fb1a36c0 2022-01-09 op if ((r = strdup(s)) == NULL)
59 fb1a36c0 2022-01-09 op fatal("strdup");
64 fb1a36c0 2022-01-09 op xmemdup(const void *d, size_t len)
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);
75 fb1a36c0 2022-01-09 op pp_msg_type(uint8_t type)
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";
111 fb1a36c0 2022-01-09 op pp_qid_type(uint8_t type)
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";
124 fb1a36c0 2022-01-09 op return "unknown";
128 fb1a36c0 2022-01-09 op hexdump_ppline(int x, uint8_t *data, size_t len)
130 fb1a36c0 2022-01-09 op for (; x < 50; x++)
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]);
142 fb1a36c0 2022-01-09 op printf("|\n");
146 fb1a36c0 2022-01-09 op hexdump(const char *label, uint8_t *data, size_t len)
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.
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) {
166 fb1a36c0 2022-01-09 op if (n == 16) {
167 fb1a36c0 2022-01-09 op hexdump_ppline(x, &data[i - 16], 16);
172 fb1a36c0 2022-01-09 op printf("%02x ", data[i]);
178 fb1a36c0 2022-01-09 op hexdump_ppline(x, &data[i - n], n);
180 fb1a36c0 2022-01-09 op printf("\n");
184 fb1a36c0 2022-01-09 op imsg_event_add(struct imsgev *iev)
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;
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);
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)
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);