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 bbcba3ed 2022-01-10 op #include "compat.h"
19 fb1a36c0 2022-01-09 op #include <sys/types.h>
20 fb1a36c0 2022-01-09 op #include <sys/uio.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>
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"
33 fb1a36c0 2022-01-09 op xmalloc(size_t size)
37 fb1a36c0 2022-01-09 op if ((r = malloc(size)) == NULL)
38 fb1a36c0 2022-01-09 op fatal("malloc");
43 fb1a36c0 2022-01-09 op xcalloc(size_t nmemb, size_t size)
47 fb1a36c0 2022-01-09 op if ((r = calloc(nmemb, size)) == NULL)
48 fb1a36c0 2022-01-09 op fatal("calloc");
53 fb1a36c0 2022-01-09 op xstrdup(const char *s)
57 fb1a36c0 2022-01-09 op if ((r = strdup(s)) == NULL)
58 fb1a36c0 2022-01-09 op fatal("strdup");
63 fb1a36c0 2022-01-09 op xmemdup(const void *d, size_t len)
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);
74 fb1a36c0 2022-01-09 op pp_msg_type(uint8_t type)
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";
110 fb1a36c0 2022-01-09 op pp_qid_type(uint8_t type)
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";
123 fb1a36c0 2022-01-09 op return "unknown";
127 fb1a36c0 2022-01-09 op hexdump_ppline(int x, uint8_t *data, size_t len)
129 fb1a36c0 2022-01-09 op for (; x < 50; x++)
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]);
141 fb1a36c0 2022-01-09 op printf("|\n");
145 fb1a36c0 2022-01-09 op hexdump(const char *label, uint8_t *data, size_t len)
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.
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) {
165 fb1a36c0 2022-01-09 op if (n == 16) {
166 fb1a36c0 2022-01-09 op hexdump_ppline(x, &data[i - 16], 16);
171 fb1a36c0 2022-01-09 op printf("%02x ", data[i]);
177 fb1a36c0 2022-01-09 op hexdump_ppline(x, &data[i - n], n);
179 fb1a36c0 2022-01-09 op printf("\n");
180 72d6dda0 2022-05-23 op fflush(stdout);
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);