2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 if ((r = malloc(size)) == NULL)
39 xcalloc(size_t nmemb, size_t size)
43 if ((r = calloc(nmemb, size)) == NULL)
49 xstrdup(const char *s)
53 if ((r = strdup(s)) == NULL)
59 xmemdup(const void *d, size_t len)
63 if ((r = malloc(len)) == NULL)
70 pp_msg_type(uint8_t type)
73 case Tversion: return "Tversion";
74 case Rversion: return "Rversion";
75 case Tauth: return "Tauth";
76 case Rauth: return "Rauth";
77 case Tattach: return "Tattach";
78 case Rattach: return "Rattach";
79 case Terror: return "Terror"; /* illegal */
80 case Rerror: return "Rerror";
81 case Tflush: return "Tflush";
82 case Rflush: return "Rflush";
83 case Twalk: return "Twalk";
84 case Rwalk: return "Rwalk";
85 case Topen: return "Topen";
86 case Ropen: return "Ropen";
87 case Tcreate: return "Tcreate";
88 case Rcreate: return "Rcreate";
89 case Tread: return "Tread";
90 case Rread: return "Rread";
91 case Twrite: return "Twrite";
92 case Rwrite: return "Rwrite";
93 case Tclunk: return "Tclunk";
94 case Rclunk: return "Rclunk";
95 case Tremove: return "Tremove";
96 case Rremove: return "Rremove";
97 case Tstat: return "Tstat";
98 case Rstat: return "Rstat";
99 case Twstat: return "Twstat";
100 case Rwstat: return "Rwstat";
101 default: return "unknown";
106 hexdump_ppline(int x, uint8_t *data, size_t len)
113 for (x = 0; x < (int)len; ++x) {
114 if (isgraph(data[x]))
115 printf("%c", data[x]);
124 hexdump(const char *label, uint8_t *data, size_t len)
131 * === first block === == second block == |........|\n
132 * first and second block are 8 bytes long (for a total of 48
133 * columns), plus two separator plus two | plus 16 chars, for
134 * a total of 68 characters.
137 printf("\nhexdump \"%s\": (%zu bytes)\n", label, len);
138 for (x = 0, n = 0, i = 0; i < len; ++i) {
139 if (i != 0 && i % 8 == 0) {
145 hexdump_ppline(x, &data[i - 16], 16);
150 printf("%02x ", data[i]);
156 hexdump_ppline(x, &data[i - n], n);
162 imsg_event_add(struct imsgev *iev)
164 iev->events = EV_READ;
165 if (iev->ibuf.w.queued)
166 iev->events |= EV_WRITE;
169 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
170 event_add(&iev->ev, NULL);
174 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
175 pid_t pid, int fd, const void *data, uint16_t datalen)
179 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,