2 1e84c7b7 2022-01-10 op * Copyright (c) 2021, 2022 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/stat.h>
20 fb1a36c0 2022-01-09 op #include <sys/types.h>
21 fb1a36c0 2022-01-09 op #include <sys/uio.h>
23 fb1a36c0 2022-01-09 op #include <dirent.h>
24 fb1a36c0 2022-01-09 op #include <endian.h>
25 fb1a36c0 2022-01-09 op #include <errno.h>
26 fb1a36c0 2022-01-09 op #include <fcntl.h>
27 fb1a36c0 2022-01-09 op #include <pwd.h>
28 fb1a36c0 2022-01-09 op #include <signal.h>
29 fb1a36c0 2022-01-09 op #include <stdint.h>
30 fb1a36c0 2022-01-09 op #include <stdlib.h>
31 fb1a36c0 2022-01-09 op #include <string.h>
32 fb1a36c0 2022-01-09 op #include <syslog.h>
33 fb1a36c0 2022-01-09 op #include <unistd.h>
35 fb1a36c0 2022-01-09 op #include "client.h"
36 fb1a36c0 2022-01-09 op #include "kami.h"
37 fb1a36c0 2022-01-09 op #include "kamid.h"
38 fb1a36c0 2022-01-09 op #include "log.h"
39 fb1a36c0 2022-01-09 op #include "sandbox.h"
40 fb1a36c0 2022-01-09 op #include "utils.h"
43 fb1a36c0 2022-01-09 op * XXX: atm is difficult to accept messages bigger than MAX_IMSGSIZE
44 fb1a36c0 2022-01-09 op * minus IMSG_HEADER_SIZE, we need something to split messages into
45 fb1a36c0 2022-01-09 op * chunks and receive them one by the other.
47 fb1a36c0 2022-01-09 op * CLIENT_MSIZE is thus the maximum message size we can handle now.
49 fb1a36c0 2022-01-09 op #define CLIENT_MSIZE (MAX_IMSGSIZE - IMSG_HEADER_SIZE)
51 fb1a36c0 2022-01-09 op #define DEBUG_PACKETS 0
53 fb1a36c0 2022-01-09 op /* straight outta /src/usr.bin/ssh/scp.c */
54 fb1a36c0 2022-01-09 op #define TYPE_OVERFLOW(type, val) \
55 fb1a36c0 2022-01-09 op ((sizeof(type) == 4 && (val) > INT32_MAX) || \
56 fb1a36c0 2022-01-09 op (sizeof(type) == 8 && (val) > INT64_MAX) || \
57 fb1a36c0 2022-01-09 op (sizeof(type) != 4 && sizeof(type) != 8))
59 fb1a36c0 2022-01-09 op STAILQ_HEAD(dirhead, dir) dirs;
63 fb1a36c0 2022-01-09 op STAILQ_ENTRY(dir) entries;
66 fb1a36c0 2022-01-09 op STAILQ_HEAD(fidhead, fid) fids;
70 fb1a36c0 2022-01-09 op char fpath[PATH_MAX];
73 fb1a36c0 2022-01-09 op * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
74 fb1a36c0 2022-01-09 op * is to unlink the file upon Tclunk.
79 fb1a36c0 2022-01-09 op * if fd is not -1 this fid was opened, fd represents its
80 fb1a36c0 2022-01-09 op * file descriptor and iomode the flags passed to open(2).
84 fb1a36c0 2022-01-09 op struct evbuffer *evb;
87 fb1a36c0 2022-01-09 op * expected offset for Tread against a directory.
89 fb1a36c0 2022-01-09 op uint64_t offset;
91 fb1a36c0 2022-01-09 op struct qid qid;
92 fb1a36c0 2022-01-09 op struct dir *dir;
93 fb1a36c0 2022-01-09 op STAILQ_ENTRY(fid) entries;
96 fb1a36c0 2022-01-09 op static struct imsgev *iev_listener;
97 fb1a36c0 2022-01-09 op static struct evbuffer *evb;
98 fb1a36c0 2022-01-09 op static uint32_t peerid;
100 fb1a36c0 2022-01-09 op static int handshaked;
101 fb1a36c0 2022-01-09 op uint32_t msize;
103 fb1a36c0 2022-01-09 op static __dead void client_shutdown(void);
104 fb1a36c0 2022-01-09 op static void client_sig_handler(int, short, void *);
105 fb1a36c0 2022-01-09 op static void client_dispatch_listener(int, short, void *);
106 fb1a36c0 2022-01-09 op static void client_privdrop(const char *, const char *);
108 fb1a36c0 2022-01-09 op static int client_send_listener(int, const void *, uint16_t);
110 fb1a36c0 2022-01-09 op static void qid_update_from_sb(struct qid *, struct stat *);
112 fb1a36c0 2022-01-09 op static struct dir *new_dir(int);
113 fb1a36c0 2022-01-09 op static struct dir *dir_incref(struct dir *);
114 fb1a36c0 2022-01-09 op static void dir_decref(struct dir *);
116 fb1a36c0 2022-01-09 op static struct fid *new_fid(struct dir *, uint32_t, const char *, struct qid *);
117 fb1a36c0 2022-01-09 op static struct fid *fid_by_id(uint32_t);
118 fb1a36c0 2022-01-09 op static void free_fid(struct fid *);
120 fb1a36c0 2022-01-09 op static void parse_message(const uint8_t *, size_t,
121 fb1a36c0 2022-01-09 op struct np_msg_header *, uint8_t **);
123 fb1a36c0 2022-01-09 op static void np_write16(struct evbuffer *, uint16_t);
124 fb1a36c0 2022-01-09 op static void np_write32(struct evbuffer *, uint32_t);
125 fb1a36c0 2022-01-09 op static void np_write64(struct evbuffer *, uint64_t);
126 fb1a36c0 2022-01-09 op static void np_header(uint32_t, uint8_t, uint16_t);
127 fb1a36c0 2022-01-09 op static void np_string(struct evbuffer *, uint16_t, const char *);
128 fb1a36c0 2022-01-09 op static void np_qid(struct evbuffer *, struct qid *);
129 fb1a36c0 2022-01-09 op static void do_send(void);
131 fb1a36c0 2022-01-09 op static void np_version(uint16_t, uint32_t, const char *);
132 fb1a36c0 2022-01-09 op static void np_attach(uint16_t, struct qid *);
133 fb1a36c0 2022-01-09 op static void np_clunk(uint16_t);
134 fb1a36c0 2022-01-09 op static void np_flush(uint16_t);
135 fb1a36c0 2022-01-09 op static void np_walk(uint16_t, int, struct qid *);
136 fb1a36c0 2022-01-09 op static void np_open(uint16_t, struct qid *, uint32_t);
137 fb1a36c0 2022-01-09 op static void np_create(uint16_t, struct qid *, uint32_t);
138 fb1a36c0 2022-01-09 op static void np_read(uint16_t, uint32_t, void *);
139 fb1a36c0 2022-01-09 op static void np_write(uint16_t, uint32_t);
140 fb1a36c0 2022-01-09 op static void np_stat(uint16_t, uint32_t, void *);
141 fb1a36c0 2022-01-09 op static void np_remove(uint16_t);
142 fb1a36c0 2022-01-09 op static void np_error(uint16_t, const char *);
143 fb1a36c0 2022-01-09 op static void np_errno(uint16_t);
145 fb1a36c0 2022-01-09 op static int np_read8(const char *, const char *, uint8_t *,
146 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
147 fb1a36c0 2022-01-09 op static int np_read16(const char *, const char *, uint16_t *,
148 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
149 fb1a36c0 2022-01-09 op static int np_read32(const char *, const char *, uint32_t *,
150 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
151 fb1a36c0 2022-01-09 op static int np_read64(const char *, const char *, uint64_t *,
152 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
154 fb1a36c0 2022-01-09 op #define READSTRERR -1
155 fb1a36c0 2022-01-09 op #define READSTRTRUNC -2
156 fb1a36c0 2022-01-09 op static int np_readstr(const char *, const char *, char *, size_t,
157 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
159 fb1a36c0 2022-01-09 op #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
160 fb1a36c0 2022-01-09 op #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
161 fb1a36c0 2022-01-09 op #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
162 fb1a36c0 2022-01-09 op #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
164 fb1a36c0 2022-01-09 op #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
166 fb1a36c0 2022-01-09 op static void tversion(struct np_msg_header *, const uint8_t *, size_t);
167 fb1a36c0 2022-01-09 op static void tattach(struct np_msg_header *, const uint8_t *, size_t);
168 fb1a36c0 2022-01-09 op static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
169 fb1a36c0 2022-01-09 op static void tflush(struct np_msg_header *, const uint8_t *, size_t);
170 fb1a36c0 2022-01-09 op static void twalk(struct np_msg_header *, const uint8_t *, size_t);
171 fb1a36c0 2022-01-09 op static void topen(struct np_msg_header *, const uint8_t *, size_t);
172 fb1a36c0 2022-01-09 op static void tcreate(struct np_msg_header *, const uint8_t *, size_t);
173 fb1a36c0 2022-01-09 op static void tread(struct np_msg_header *, const uint8_t *, size_t);
174 fb1a36c0 2022-01-09 op static void twrite(struct np_msg_header *, const uint8_t *, size_t);
175 fb1a36c0 2022-01-09 op static void tstat(struct np_msg_header *, const uint8_t *, size_t);
176 fb1a36c0 2022-01-09 op static void tremove(struct np_msg_header *, const uint8_t *, size_t);
177 fb1a36c0 2022-01-09 op static void handle_message(struct imsg *, size_t);
180 fb1a36c0 2022-01-09 op client(int debug, int verbose)
182 fb1a36c0 2022-01-09 op struct event ev_sigint, ev_sigterm;
184 fb1a36c0 2022-01-09 op log_init(debug, LOG_DAEMON);
185 fb1a36c0 2022-01-09 op log_setverbose(verbose);
187 fb1a36c0 2022-01-09 op setproctitle("client");
188 fb1a36c0 2022-01-09 op log_procinit("client");
190 fb1a36c0 2022-01-09 op log_debug("warming up");
192 fb1a36c0 2022-01-09 op event_init();
194 fb1a36c0 2022-01-09 op /* Setup signal handlers */
195 fb1a36c0 2022-01-09 op signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
196 fb1a36c0 2022-01-09 op signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
198 fb1a36c0 2022-01-09 op signal_add(&ev_sigint, NULL);
199 fb1a36c0 2022-01-09 op signal_add(&ev_sigterm, NULL);
201 fb1a36c0 2022-01-09 op signal(SIGPIPE, SIG_IGN);
202 fb1a36c0 2022-01-09 op signal(SIGHUP, SIG_IGN);
204 fb1a36c0 2022-01-09 op /* Setup pipe and event handler to the listener process */
205 fb1a36c0 2022-01-09 op if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
208 fb1a36c0 2022-01-09 op imsg_init(&iev_listener->ibuf, 3);
209 fb1a36c0 2022-01-09 op iev_listener->handler = client_dispatch_listener;
211 fb1a36c0 2022-01-09 op /* Setup event handlers. */
212 fb1a36c0 2022-01-09 op iev_listener->events = EV_READ;
213 fb1a36c0 2022-01-09 op event_set(&iev_listener->ev, iev_listener->ibuf.fd,
214 fb1a36c0 2022-01-09 op iev_listener->events, iev_listener->handler, iev_listener);
215 fb1a36c0 2022-01-09 op event_add(&iev_listener->ev, NULL);
217 fb1a36c0 2022-01-09 op event_dispatch();
218 fb1a36c0 2022-01-09 op client_shutdown();
221 fb1a36c0 2022-01-09 op static __dead void
222 fb1a36c0 2022-01-09 op client_shutdown(void)
224 fb1a36c0 2022-01-09 op if (evb != NULL)
225 fb1a36c0 2022-01-09 op evbuffer_free(evb);
227 fb1a36c0 2022-01-09 op msgbuf_clear(&iev_listener->ibuf.w);
228 fb1a36c0 2022-01-09 op close(iev_listener->ibuf.fd);
230 fb1a36c0 2022-01-09 op free(iev_listener);
232 fb1a36c0 2022-01-09 op log_debug("client exiting");
237 fb1a36c0 2022-01-09 op client_sig_handler(int sig, short event, void *d)
240 fb1a36c0 2022-01-09 op * Normal signal handler rules don't apply because libevent
241 fb1a36c0 2022-01-09 op * decouples for us.
244 fb1a36c0 2022-01-09 op switch (sig) {
246 fb1a36c0 2022-01-09 op case SIGTERM:
247 fb1a36c0 2022-01-09 op client_shutdown();
249 fb1a36c0 2022-01-09 op fatalx("unexpected signal %d", sig);
254 fb1a36c0 2022-01-09 op client_dispatch_listener(int fd, short event, void *d)
256 1e84c7b7 2022-01-10 op static int auth = 0;
257 1e84c7b7 2022-01-10 op struct kd_auth_proc rauth;
258 fb1a36c0 2022-01-09 op struct imsg imsg;
259 fb1a36c0 2022-01-09 op struct imsgev *iev = d;
260 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
262 fb1a36c0 2022-01-09 op int shut = 0;
264 fb1a36c0 2022-01-09 op ibuf = &iev->ibuf;
266 fb1a36c0 2022-01-09 op if (event & EV_READ) {
267 fb1a36c0 2022-01-09 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
268 fb1a36c0 2022-01-09 op fatal("imsg_read error");
269 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed */
272 fb1a36c0 2022-01-09 op if (event & EV_WRITE) {
273 fb1a36c0 2022-01-09 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
274 fb1a36c0 2022-01-09 op fatal("msgbuf_write");
275 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed */
280 fb1a36c0 2022-01-09 op if ((n = imsg_get(ibuf, &imsg)) == -1)
281 fb1a36c0 2022-01-09 op fatal("%s: imsg_get error", __func__);
282 fb1a36c0 2022-01-09 op if (n == 0) /* No more messages. */
285 fb1a36c0 2022-01-09 op switch (imsg.hdr.type) {
286 fb1a36c0 2022-01-09 op case IMSG_AUTH:
287 fb1a36c0 2022-01-09 op peerid = imsg.hdr.peerid;
289 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_AUTH already done", __func__);
292 1e84c7b7 2022-01-10 op if (IMSG_DATA_SIZE(imsg) != sizeof(rauth))
293 1e84c7b7 2022-01-10 op fatalx("mismatching size for IMSG_AUTH");
294 1e84c7b7 2022-01-10 op memcpy(&rauth, imsg.data, sizeof(rauth));
295 1e84c7b7 2022-01-10 op if (rauth.uname[sizeof(rauth.uname)-1] != '\0' ||
296 1e84c7b7 2022-01-10 op rauth.dir[sizeof(rauth.dir)-1] != '\0')
297 1e84c7b7 2022-01-10 op fatalx("IMSG_AUTH strings not NUL-terminated");
299 1e84c7b7 2022-01-10 op client_privdrop(rauth.uname, rauth.dir);
300 1e84c7b7 2022-01-10 op explicit_bzero(&rauth, sizeof(rauth));
302 fb1a36c0 2022-01-09 op case IMSG_BUF:
304 fb1a36c0 2022-01-09 op fatalx("%s: can't handle messages before"
305 fb1a36c0 2022-01-09 op " doing the auth", __func__);
306 fb1a36c0 2022-01-09 op handle_message(&imsg, IMSG_DATA_SIZE(imsg));
308 fb1a36c0 2022-01-09 op case IMSG_CONN_GONE:
309 fb1a36c0 2022-01-09 op log_debug("closing");
313 fb1a36c0 2022-01-09 op log_debug("%s: unexpected imsg %d",
314 fb1a36c0 2022-01-09 op __func__, imsg.hdr.type);
317 fb1a36c0 2022-01-09 op imsg_free(&imsg);
321 fb1a36c0 2022-01-09 op imsg_event_add(iev);
323 fb1a36c0 2022-01-09 op /* This pipe is dead. Remove its event handler. */
324 fb1a36c0 2022-01-09 op event_del(&iev->ev);
325 fb1a36c0 2022-01-09 op log_debug("pipe closed, shutting down...");
326 fb1a36c0 2022-01-09 op event_loopexit(NULL);
331 fb1a36c0 2022-01-09 op client_privdrop(const char *username, const char *dir)
333 fb1a36c0 2022-01-09 op struct passwd *pw;
335 fb1a36c0 2022-01-09 op setproctitle("client %s", username);
337 fb1a36c0 2022-01-09 op if ((pw = getpwnam(username)) == NULL)
338 fb1a36c0 2022-01-09 op fatalx("getpwnam(%s) failed", username);
340 fb1a36c0 2022-01-09 op if (chroot(dir) == -1)
341 fb1a36c0 2022-01-09 op fatal("chroot");
342 fb1a36c0 2022-01-09 op if (chdir("/") == -1)
343 fb1a36c0 2022-01-09 op fatal("chdir(\"/\")");
345 fb1a36c0 2022-01-09 op if (setgroups(1, &pw->pw_gid) ||
346 fb1a36c0 2022-01-09 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
347 fb1a36c0 2022-01-09 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
348 fb1a36c0 2022-01-09 op fatal("can't drop privileges");
350 fb1a36c0 2022-01-09 op sandbox_client();
351 fb1a36c0 2022-01-09 op log_debug("client ready; user=%s dir=%s", username, dir);
353 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
354 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
358 fb1a36c0 2022-01-09 op client_send_listener(int type, const void *data, uint16_t len)
362 fb1a36c0 2022-01-09 op if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
363 fb1a36c0 2022-01-09 op data, len)) != -1)
364 fb1a36c0 2022-01-09 op imsg_event_add(iev_listener);
369 fb1a36c0 2022-01-09 op /* set qid fields from sb */
371 fb1a36c0 2022-01-09 op qid_update_from_sb(struct qid *qid, struct stat *sb)
373 fb1a36c0 2022-01-09 op qid->path = sb->st_ino;
376 fb1a36c0 2022-01-09 op * Theoretically (and hopefully!) this should be a 64 bit
377 fb1a36c0 2022-01-09 op * number. Unfortunately, 9P uses 32 bit timestamps.
379 fb1a36c0 2022-01-09 op qid->vers = sb->st_mtim.tv_sec;
381 fb1a36c0 2022-01-09 op if (S_ISREG(sb->st_mode))
382 fb1a36c0 2022-01-09 op qid->type = QTFILE;
383 fb1a36c0 2022-01-09 op else if (S_ISDIR(sb->st_mode))
384 fb1a36c0 2022-01-09 op qid->type = QTDIR;
385 fb1a36c0 2022-01-09 op else if (S_ISLNK(sb->st_mode))
386 fb1a36c0 2022-01-09 op qid->type = QTSYMLINK;
389 fb1a36c0 2022-01-09 op /* creates a qid given a fd */
390 fb1a36c0 2022-01-09 op static struct dir *
391 fb1a36c0 2022-01-09 op new_dir(int fd)
393 fb1a36c0 2022-01-09 op struct dir *dir;
395 fb1a36c0 2022-01-09 op if ((dir = calloc(1, sizeof(*dir))) == NULL)
398 fb1a36c0 2022-01-09 op dir->fd = fd;
399 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&dirs, dir, entries);
403 fb1a36c0 2022-01-09 op static struct dir *
404 fb1a36c0 2022-01-09 op dir_incref(struct dir *dir)
406 fb1a36c0 2022-01-09 op dir->refcount++;
411 fb1a36c0 2022-01-09 op dir_decref(struct dir *dir)
413 fb1a36c0 2022-01-09 op if (--dir->refcount > 0)
416 fb1a36c0 2022-01-09 op STAILQ_REMOVE(&dirs, dir, dir, entries);
418 fb1a36c0 2022-01-09 op close(dir->fd);
422 fb1a36c0 2022-01-09 op static struct fid *
423 fb1a36c0 2022-01-09 op new_fid(struct dir *dir, uint32_t fid, const char *path, struct qid *qid)
425 fb1a36c0 2022-01-09 op struct fid *f;
426 fb1a36c0 2022-01-09 op struct qid q;
427 fb1a36c0 2022-01-09 op struct stat sb;
429 fb1a36c0 2022-01-09 op if (qid == NULL) {
430 fb1a36c0 2022-01-09 op if (fstatat(dir->fd, path, &sb, 0)) {
431 fb1a36c0 2022-01-09 op log_warn("fstatat(%s)", path);
434 fb1a36c0 2022-01-09 op qid_update_from_sb(&q, &sb);
438 fb1a36c0 2022-01-09 op if ((f = calloc(1, sizeof(*f))) == NULL)
441 fb1a36c0 2022-01-09 op f->dir = dir_incref(dir);
442 fb1a36c0 2022-01-09 op f->fid = fid;
445 fb1a36c0 2022-01-09 op strlcpy(f->fpath, path, sizeof(f->fpath));
447 fb1a36c0 2022-01-09 op memcpy(&f->qid, qid, sizeof(f->qid));
449 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&fids, f, entries);
454 fb1a36c0 2022-01-09 op static struct fid *
455 fb1a36c0 2022-01-09 op fid_by_id(uint32_t fid)
457 fb1a36c0 2022-01-09 op struct fid *f;
459 fb1a36c0 2022-01-09 op STAILQ_FOREACH(f, &fids, entries) {
460 fb1a36c0 2022-01-09 op if (f->fid == fid)
468 fb1a36c0 2022-01-09 op free_fid(struct fid *f)
472 fb1a36c0 2022-01-09 op if (f->fd != -1) {
473 fb1a36c0 2022-01-09 op if (f->d != NULL)
474 fb1a36c0 2022-01-09 op r = closedir(f->d);
476 fb1a36c0 2022-01-09 op r = close(f->fd);
479 fb1a36c0 2022-01-09 op fatal("can't close fid %d", f->fid);
481 fb1a36c0 2022-01-09 op if (f->evb != NULL)
482 fb1a36c0 2022-01-09 op evbuffer_free(f->evb);
484 fb1a36c0 2022-01-09 op /* try to honour ORCLOSE if requested */
485 fb1a36c0 2022-01-09 op if (f->iomode & O_CLOEXEC)
486 fb1a36c0 2022-01-09 op unlinkat(f->dir->fd, f->fpath, 0);
489 fb1a36c0 2022-01-09 op dir_decref(f->dir);
491 fb1a36c0 2022-01-09 op STAILQ_REMOVE(&fids, f, fid, entries);
496 fb1a36c0 2022-01-09 op parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
497 fb1a36c0 2022-01-09 op uint8_t **cnt)
499 fb1a36c0 2022-01-09 op size_t olen = len;
501 fb1a36c0 2022-01-09 op if (!NPREAD32("len", &hdr->len, &data, &len) ||
502 fb1a36c0 2022-01-09 op !NPREAD8("type", &hdr->type, &data, &len) ||
503 fb1a36c0 2022-01-09 op !NPREAD16("tag", &hdr->tag, &data, &len))
506 fb1a36c0 2022-01-09 op if (olen != hdr->len)
509 fb1a36c0 2022-01-09 op if (hdr->type < Tversion ||
510 fb1a36c0 2022-01-09 op hdr->type >= Tmax ||
511 fb1a36c0 2022-01-09 op hdr->type == Terror ||
512 fb1a36c0 2022-01-09 op (hdr->type & 0x1) != 0) /* cannot recv a R* */
515 fb1a36c0 2022-01-09 op hdr->tag = le32toh(hdr->tag);
517 fb1a36c0 2022-01-09 op *cnt = (uint8_t *)data;
521 fb1a36c0 2022-01-09 op /* TODO: send a proper message to terminate the connection. */
522 fb1a36c0 2022-01-09 op fatalx("got invalid message");
526 fb1a36c0 2022-01-09 op np_write16(struct evbuffer *e, uint16_t x)
528 fb1a36c0 2022-01-09 op x = htole16(x);
529 fb1a36c0 2022-01-09 op evbuffer_add(e, &x, sizeof(x));
533 fb1a36c0 2022-01-09 op np_write32(struct evbuffer *e, uint32_t x)
535 fb1a36c0 2022-01-09 op x = htole32(x);
536 fb1a36c0 2022-01-09 op evbuffer_add(e, &x, sizeof(x));
540 fb1a36c0 2022-01-09 op np_write64(struct evbuffer *e, uint64_t x)
542 fb1a36c0 2022-01-09 op x = htole64(x);
543 fb1a36c0 2022-01-09 op evbuffer_add(e, &x, sizeof(x));
547 fb1a36c0 2022-01-09 op np_writebuf(struct evbuffer *e, size_t len, void *data)
549 fb1a36c0 2022-01-09 op evbuffer_add(e, data, len);
553 fb1a36c0 2022-01-09 op np_header(uint32_t len, uint8_t type, uint16_t tag)
555 fb1a36c0 2022-01-09 op len += HEADERSIZE;
557 fb1a36c0 2022-01-09 op len = htole32(len);
558 fb1a36c0 2022-01-09 op tag = htole16(tag);
560 fb1a36c0 2022-01-09 op evbuffer_add(evb, &len, sizeof(len));
561 fb1a36c0 2022-01-09 op evbuffer_add(evb, &type, sizeof(type));
562 fb1a36c0 2022-01-09 op evbuffer_add(evb, &tag, sizeof(tag));
566 fb1a36c0 2022-01-09 op np_string(struct evbuffer *e, uint16_t len, const char *str)
568 fb1a36c0 2022-01-09 op uint16_t l = len;
570 fb1a36c0 2022-01-09 op len = htole16(len);
571 fb1a36c0 2022-01-09 op evbuffer_add(e, &len, sizeof(len));
572 fb1a36c0 2022-01-09 op evbuffer_add(e, str, l);
576 fb1a36c0 2022-01-09 op np_qid(struct evbuffer *e, struct qid *qid)
578 fb1a36c0 2022-01-09 op uint64_t path;
579 fb1a36c0 2022-01-09 op uint32_t vers;
581 fb1a36c0 2022-01-09 op path = htole64(qid->path);
582 fb1a36c0 2022-01-09 op vers = htole32(qid->vers);
584 fb1a36c0 2022-01-09 op evbuffer_add(e, &qid->type, sizeof(qid->type));
585 fb1a36c0 2022-01-09 op evbuffer_add(e, &vers, sizeof(vers));
586 fb1a36c0 2022-01-09 op evbuffer_add(e, &path, sizeof(path));
590 fb1a36c0 2022-01-09 op do_send(void)
595 fb1a36c0 2022-01-09 op len = EVBUFFER_LENGTH(evb);
596 fb1a36c0 2022-01-09 op data = EVBUFFER_DATA(evb);
598 fb1a36c0 2022-01-09 op #if DEBUG_PACKETS
599 fb1a36c0 2022-01-09 op hexdump("outgoing packet", data, len);
601 fb1a36c0 2022-01-09 op client_send_listener(IMSG_BUF, data, len);
602 fb1a36c0 2022-01-09 op evbuffer_drain(evb, len);
606 fb1a36c0 2022-01-09 op np_version(uint16_t tag, uint32_t msize, const char *version)
610 fb1a36c0 2022-01-09 op l = strlen(version);
612 fb1a36c0 2022-01-09 op msize = htole32(msize);
614 fb1a36c0 2022-01-09 op np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
615 fb1a36c0 2022-01-09 op evbuffer_add(evb, &msize, sizeof(msize));
616 fb1a36c0 2022-01-09 op np_string(evb, l, version);
621 fb1a36c0 2022-01-09 op np_attach(uint16_t tag, struct qid *qid)
623 fb1a36c0 2022-01-09 op np_header(QIDSIZE, Rattach, tag);
624 fb1a36c0 2022-01-09 op np_qid(evb, qid);
629 fb1a36c0 2022-01-09 op np_clunk(uint16_t tag)
631 fb1a36c0 2022-01-09 op np_header(0, Rclunk, tag);
636 fb1a36c0 2022-01-09 op np_flush(uint16_t tag)
638 fb1a36c0 2022-01-09 op np_header(0, Rflush, tag);
643 fb1a36c0 2022-01-09 op np_walk(uint16_t tag, int nwqid, struct qid *wqid)
647 fb1a36c0 2022-01-09 op /* two bytes for the counter */
648 fb1a36c0 2022-01-09 op np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
649 fb1a36c0 2022-01-09 op np_write16(evb, nwqid);
650 fb1a36c0 2022-01-09 op for (i = 0; i < nwqid; ++i)
651 fb1a36c0 2022-01-09 op np_qid(evb, wqid + i);
657 fb1a36c0 2022-01-09 op np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
659 fb1a36c0 2022-01-09 op np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
660 fb1a36c0 2022-01-09 op np_qid(evb, qid);
661 fb1a36c0 2022-01-09 op np_write32(evb, iounit);
666 fb1a36c0 2022-01-09 op np_create(uint16_t tag, struct qid *qid, uint32_t iounit)
668 fb1a36c0 2022-01-09 op np_header(QIDSIZE + sizeof(iounit), Rcreate, tag);
669 fb1a36c0 2022-01-09 op np_qid(evb, qid);
670 fb1a36c0 2022-01-09 op np_write32(evb, iounit);
675 fb1a36c0 2022-01-09 op np_read(uint16_t tag, uint32_t count, void *data)
677 fb1a36c0 2022-01-09 op if (sizeof(count) + count + HEADERSIZE >= msize) {
678 fb1a36c0 2022-01-09 op np_error(tag, "Rread would overflow");
682 fb1a36c0 2022-01-09 op np_header(sizeof(count) + count, Rread, tag);
683 fb1a36c0 2022-01-09 op np_write32(evb, count);
684 fb1a36c0 2022-01-09 op np_writebuf(evb, count, data);
689 fb1a36c0 2022-01-09 op np_write(uint16_t tag, uint32_t count)
691 fb1a36c0 2022-01-09 op np_header(sizeof(count), Rwrite, tag);
692 fb1a36c0 2022-01-09 op np_write32(evb, count);
697 fb1a36c0 2022-01-09 op np_stat(uint16_t tag, uint32_t count, void *data)
699 fb1a36c0 2022-01-09 op if (sizeof(count) + count + HEADERSIZE >= msize) {
700 fb1a36c0 2022-01-09 op np_error(tag, "Rstat would overflow");
704 fb1a36c0 2022-01-09 op np_header(count, Rstat, tag);
705 fb1a36c0 2022-01-09 op np_writebuf(evb, count, data);
710 fb1a36c0 2022-01-09 op np_remove(uint16_t tag)
712 fb1a36c0 2022-01-09 op np_header(0, Rremove, tag);
717 fb1a36c0 2022-01-09 op np_error(uint16_t tag, const char *errstr)
721 fb1a36c0 2022-01-09 op l = strlen(errstr);
723 fb1a36c0 2022-01-09 op np_header(sizeof(l) + l, Rerror, tag);
724 fb1a36c0 2022-01-09 op np_string(evb, l, errstr);
729 fb1a36c0 2022-01-09 op np_errno(uint16_t tag)
731 fb1a36c0 2022-01-09 op int saved_errno;
732 fb1a36c0 2022-01-09 op char buf[NL_TEXTMAX] = {0};
734 fb1a36c0 2022-01-09 op saved_errno = errno;
736 fb1a36c0 2022-01-09 op strerror_r(errno, buf, sizeof(buf));
737 fb1a36c0 2022-01-09 op np_error(tag, buf);
739 fb1a36c0 2022-01-09 op errno = saved_errno;
743 fb1a36c0 2022-01-09 op np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
746 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
747 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
748 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
752 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
753 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
754 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
760 fb1a36c0 2022-01-09 op np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
763 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
764 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
765 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
769 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
770 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
771 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
772 fb1a36c0 2022-01-09 op *dst = le16toh(*dst);
778 fb1a36c0 2022-01-09 op np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
781 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
782 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
783 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
787 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
788 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
789 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
790 fb1a36c0 2022-01-09 op *dst = le32toh(*dst);
796 fb1a36c0 2022-01-09 op np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
799 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
800 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
801 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
805 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
806 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
807 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
808 fb1a36c0 2022-01-09 op *dst = le64toh(*dst);
814 fb1a36c0 2022-01-09 op np_readstr(const char *t, const char *f, char *res, size_t reslen,
815 fb1a36c0 2022-01-09 op const uint8_t **src, size_t *len)
818 fb1a36c0 2022-01-09 op char buf[32];
820 fb1a36c0 2022-01-09 op strlcpy(buf, f, sizeof(buf));
821 fb1a36c0 2022-01-09 op strlcat(buf, "-len", sizeof(buf));
823 fb1a36c0 2022-01-09 op if (!np_read16(t, buf, &sl, src, len))
824 fb1a36c0 2022-01-09 op return READSTRERR;
826 fb1a36c0 2022-01-09 op if (*len < sl) {
827 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %d bytes for the %s field but only "
828 fb1a36c0 2022-01-09 op "%zu are available.", t, sl, f, *len);
829 fb1a36c0 2022-01-09 op return READSTRERR;
832 fb1a36c0 2022-01-09 op if (*len > reslen-1)
833 fb1a36c0 2022-01-09 op return READSTRTRUNC;
835 fb1a36c0 2022-01-09 op memcpy(res, *src, sl);
836 fb1a36c0 2022-01-09 op res[sl] = '\0';
844 fb1a36c0 2022-01-09 op tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
846 fb1a36c0 2022-01-09 op char *dot, version[32];
848 fb1a36c0 2022-01-09 op if (handshaked)
851 fb1a36c0 2022-01-09 op /* msize[4] version[s] */
852 fb1a36c0 2022-01-09 op if (!NPREAD32("msize", &msize, &data, &len))
855 fb1a36c0 2022-01-09 op switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
856 fb1a36c0 2022-01-09 op case READSTRERR:
858 fb1a36c0 2022-01-09 op case READSTRTRUNC:
859 fb1a36c0 2022-01-09 op log_warnx("9P version string too long, truncated");
860 fb1a36c0 2022-01-09 op goto mismatch;
863 fb1a36c0 2022-01-09 op if ((dot = strchr(version, '.')) != NULL)
866 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P) != 0 ||
868 fb1a36c0 2022-01-09 op goto mismatch;
870 fb1a36c0 2022-01-09 op /* version matched */
871 fb1a36c0 2022-01-09 op handshaked = 1;
872 fb1a36c0 2022-01-09 op msize = MIN(msize, CLIENT_MSIZE);
873 fb1a36c0 2022-01-09 op client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
874 fb1a36c0 2022-01-09 op np_version(hdr->tag, msize, VERSION9P);
878 fb1a36c0 2022-01-09 op log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
880 fb1a36c0 2022-01-09 op np_version(hdr->tag, MSIZE9P, "unknown");
884 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
885 fb1a36c0 2022-01-09 op client_shutdown();
889 fb1a36c0 2022-01-09 op tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
891 fb1a36c0 2022-01-09 op struct dir *dir;
892 fb1a36c0 2022-01-09 op struct fid *f;
893 fb1a36c0 2022-01-09 op uint32_t fid, afid;
895 fb1a36c0 2022-01-09 op char aname[PATH_MAX];
897 fb1a36c0 2022-01-09 op /* fid[4] afid[4] uname[s] aname[s] */
899 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
900 fb1a36c0 2022-01-09 op !NPREAD32("afid", &afid, &data, &len))
903 fb1a36c0 2022-01-09 op /* read the uname but don't actually use it */
904 fb1a36c0 2022-01-09 op switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
905 fb1a36c0 2022-01-09 op case READSTRERR:
907 fb1a36c0 2022-01-09 op case READSTRTRUNC:
908 fb1a36c0 2022-01-09 op np_error(hdr->tag, "name too long");
912 fb1a36c0 2022-01-09 op switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
913 fb1a36c0 2022-01-09 op case READSTRERR:
915 fb1a36c0 2022-01-09 op case READSTRTRUNC:
916 fb1a36c0 2022-01-09 op np_error(hdr->tag, "name too long");
920 fb1a36c0 2022-01-09 op if (fid_by_id(fid) != NULL || afid != NOFID) {
921 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid or afid");
925 fb1a36c0 2022-01-09 op if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
928 fb1a36c0 2022-01-09 op if ((dir = new_dir(fd)) == NULL)
931 fb1a36c0 2022-01-09 op log_debug("attached %s to %d", aname, fid);
933 fb1a36c0 2022-01-09 op if ((f = new_fid(dir, fid, aname, NULL)) == NULL) {
934 fb1a36c0 2022-01-09 op dir_decref(dir);
938 fb1a36c0 2022-01-09 op np_attach(hdr->tag, &f->qid);
942 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
943 fb1a36c0 2022-01-09 op log_warn("failed to attach %s", aname);
947 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
948 fb1a36c0 2022-01-09 op client_shutdown();
952 fb1a36c0 2022-01-09 op tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
954 fb1a36c0 2022-01-09 op struct fid *f;
955 fb1a36c0 2022-01-09 op uint32_t fid;
958 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len)) {
959 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
960 fb1a36c0 2022-01-09 op client_shutdown();
964 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
965 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
970 fb1a36c0 2022-01-09 op np_clunk(hdr->tag);
974 fb1a36c0 2022-01-09 op tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
976 fb1a36c0 2022-01-09 op uint16_t oldtag;
979 fb1a36c0 2022-01-09 op * We're doing only synchronous I/O. Tflush is implemented
980 fb1a36c0 2022-01-09 op * only because it's illegal to reply with a Rerror.
983 fb1a36c0 2022-01-09 op /* oldtag[2] */
984 fb1a36c0 2022-01-09 op if (len != sizeof(oldtag)) {
985 fb1a36c0 2022-01-09 op log_warnx("Tflush with the wrong size: got %zu want %zu",
986 fb1a36c0 2022-01-09 op len, sizeof(oldtag));
987 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
988 fb1a36c0 2022-01-09 op client_shutdown();
992 fb1a36c0 2022-01-09 op np_flush(hdr->tag);
996 fb1a36c0 2022-01-09 op twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
998 fb1a36c0 2022-01-09 op struct stat sb;
999 fb1a36c0 2022-01-09 op struct dir *dir;
1000 fb1a36c0 2022-01-09 op struct qid wqid[MAXWELEM] = {0};
1001 fb1a36c0 2022-01-09 op struct fid *f, *nf;
1002 fb1a36c0 2022-01-09 op uint32_t fid, newfid;
1003 fb1a36c0 2022-01-09 op uint16_t nwname;
1004 fb1a36c0 2022-01-09 op int fd, oldfd, no, nwqid = 0;
1005 fb1a36c0 2022-01-09 op char wnam[PATH_MAX];
1007 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1008 fb1a36c0 2022-01-09 op !NPREAD32("newfid", &newfid, &data, &len) ||
1009 fb1a36c0 2022-01-09 op !NPREAD16("nwname", &nwname, &data, &len))
1012 fb1a36c0 2022-01-09 op if (nwname > MAXWELEM) {
1013 fb1a36c0 2022-01-09 op log_warnx("Twalk: more than %d path elements: %d",
1014 fb1a36c0 2022-01-09 op MAXWELEM, nwname);
1018 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
1019 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1023 fb1a36c0 2022-01-09 op if (f->fd != -1) {
1024 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid already opened for I/O");
1028 fb1a36c0 2022-01-09 op if (fid == newfid)
1030 fb1a36c0 2022-01-09 op else if ((nf = fid_by_id(newfid)) != NULL) {
1031 fb1a36c0 2022-01-09 op np_error(hdr->tag, "newfid already in use");
1036 fb1a36c0 2022-01-09 op /* special case: fid duplication */
1037 fb1a36c0 2022-01-09 op if (nwname == 0) {
1039 fb1a36c0 2022-01-09 op * TODO: should we forbid fids duplication when fid ==
1042 fb1a36c0 2022-01-09 op if (nf == NULL &&
1043 fb1a36c0 2022-01-09 op (nf = new_fid(f->dir, newfid, f->fpath, &f->qid)) == NULL)
1044 fb1a36c0 2022-01-09 op fatal("new_fid duplication");
1046 fb1a36c0 2022-01-09 op np_walk(hdr->tag, 0, NULL);
1050 fb1a36c0 2022-01-09 op if (!(f->qid.type & QTDIR)) {
1051 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid doesn't represent a directory");
1055 fb1a36c0 2022-01-09 op oldfd = f->dir->fd;
1057 fb1a36c0 2022-01-09 op for (nwqid = 0; nwqid < nwname; nwqid++) {
1058 fb1a36c0 2022-01-09 op switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1059 fb1a36c0 2022-01-09 op case READSTRERR:
1061 fb1a36c0 2022-01-09 op case READSTRTRUNC:
1062 fb1a36c0 2022-01-09 op np_error(hdr->tag, "wname too long");
1066 fb1a36c0 2022-01-09 op if (*wnam == '\0' ||
1067 fb1a36c0 2022-01-09 op strchr(wnam, '/') != NULL ||
1068 fb1a36c0 2022-01-09 op !strcmp(wnam, ".")) {
1069 fb1a36c0 2022-01-09 op errno = EINVAL;
1070 fb1a36c0 2022-01-09 op goto cantopen;
1073 fb1a36c0 2022-01-09 op if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1074 fb1a36c0 2022-01-09 op errno != ENOTDIR)
1075 fb1a36c0 2022-01-09 op goto cantopen;
1077 fb1a36c0 2022-01-09 op if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1078 fb1a36c0 2022-01-09 op (fd != -1 && fstat(fd, &sb) == -1))
1079 fb1a36c0 2022-01-09 op goto cantopen;
1081 fb1a36c0 2022-01-09 op qid_update_from_sb(&wqid[nwqid], &sb);
1083 fb1a36c0 2022-01-09 op /* reached a file but we still have other components */
1084 fb1a36c0 2022-01-09 op if (fd == -1 && nwqid+1 < nwname)
1085 fb1a36c0 2022-01-09 op goto cantopen;
1087 fb1a36c0 2022-01-09 op /* reached the end and found a file */
1088 fb1a36c0 2022-01-09 op if (fd == -1 && nwqid+1 == nwname)
1091 fb1a36c0 2022-01-09 op if (oldfd != f->dir->fd)
1092 fb1a36c0 2022-01-09 op close(oldfd);
1097 fb1a36c0 2022-01-09 op * If fd is -1 we've reached a file, otherwise we've just
1098 fb1a36c0 2022-01-09 op * reached another directory. We must pay attention to what
1099 fb1a36c0 2022-01-09 op * file descriptor we use to create the dir, because if we've
1100 fb1a36c0 2022-01-09 op * reached a file and oldfd is f->dir->fd then we *must* share
1101 fb1a36c0 2022-01-09 op * the same dir (it was a walk of one path from a directory to a
1102 fb1a36c0 2022-01-09 op * file, otherwise fun is bound to happen as soon as the client
1103 fb1a36c0 2022-01-09 op * closes the fid for the directory but keeps the one for the
1106 fb1a36c0 2022-01-09 op if (fd == -1 && oldfd == f->dir->fd)
1107 fb1a36c0 2022-01-09 op dir = f->dir;
1108 fb1a36c0 2022-01-09 op else if (fd == -1)
1109 fb1a36c0 2022-01-09 op dir = new_dir(oldfd);
1111 fb1a36c0 2022-01-09 op dir = new_dir(fd);
1113 fb1a36c0 2022-01-09 op if (dir == NULL)
1114 fb1a36c0 2022-01-09 op fatal("new_dir");
1116 fb1a36c0 2022-01-09 op if (nf == NULL) {
1117 fb1a36c0 2022-01-09 op if ((nf = new_fid(dir, newfid, wnam, &wqid[nwqid-1])) == NULL)
1118 fb1a36c0 2022-01-09 op fatal("new fid");
1120 fb1a36c0 2022-01-09 op /* update the dir */
1121 fb1a36c0 2022-01-09 op dir_decref(nf->dir);
1122 fb1a36c0 2022-01-09 op nf->dir = dir_incref(dir);
1125 fb1a36c0 2022-01-09 op np_walk(hdr->tag, nwqid, wqid);
1129 fb1a36c0 2022-01-09 op if (oldfd != f->dir->fd)
1130 fb1a36c0 2022-01-09 op close(oldfd);
1132 fb1a36c0 2022-01-09 op if (nwqid == 0)
1133 fb1a36c0 2022-01-09 op np_error(hdr->tag, strerror(no));
1135 fb1a36c0 2022-01-09 op np_walk(hdr->tag, nwqid, wqid);
1139 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1140 fb1a36c0 2022-01-09 op client_shutdown();
1143 fb1a36c0 2022-01-09 op static inline int
1144 fb1a36c0 2022-01-09 op npmode_to_unix(uint8_t mode, int *flags)
1146 fb1a36c0 2022-01-09 op switch (mode & 0x0F) {
1147 fb1a36c0 2022-01-09 op case KOREAD:
1148 fb1a36c0 2022-01-09 op *flags = O_RDONLY;
1150 fb1a36c0 2022-01-09 op case KOWRITE:
1151 fb1a36c0 2022-01-09 op *flags = O_WRONLY;
1153 fb1a36c0 2022-01-09 op case KORDWR:
1154 fb1a36c0 2022-01-09 op *flags = O_RDWR;
1156 fb1a36c0 2022-01-09 op case KOEXEC:
1157 fb1a36c0 2022-01-09 op log_warnx("tried to open something with KOEXEC");
1158 fb1a36c0 2022-01-09 op /* fallthrough */
1163 fb1a36c0 2022-01-09 op if (mode & KOTRUNC)
1164 fb1a36c0 2022-01-09 op *flags |= O_TRUNC;
1165 fb1a36c0 2022-01-09 op if (mode & KORCLOSE)
1166 fb1a36c0 2022-01-09 op *flags |= O_CLOEXEC;
1172 fb1a36c0 2022-01-09 op topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1174 fb1a36c0 2022-01-09 op struct stat sb;
1175 fb1a36c0 2022-01-09 op struct qid qid;
1176 fb1a36c0 2022-01-09 op struct fid *f;
1177 fb1a36c0 2022-01-09 op uint32_t fid;
1178 fb1a36c0 2022-01-09 op uint8_t mode;
1179 fb1a36c0 2022-01-09 op const char *path;
1181 fb1a36c0 2022-01-09 op /* fid[4] mode[1] */
1182 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1183 fb1a36c0 2022-01-09 op !NPREAD8("mode", &mode, &data, &len)) {
1184 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1185 fb1a36c0 2022-01-09 op client_shutdown();
1189 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1190 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1194 fb1a36c0 2022-01-09 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1195 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid mode");
1199 fb1a36c0 2022-01-09 op path = f->fpath;
1200 fb1a36c0 2022-01-09 op if (f->qid.type & QTDIR)
1203 fb1a36c0 2022-01-09 op if ((f->fd = openat(f->dir->fd, path, f->iomode)) == -1) {
1204 fb1a36c0 2022-01-09 op np_error(hdr->tag, strerror(errno));
1208 fb1a36c0 2022-01-09 op if (fstat(f->fd, &sb) == -1)
1209 fb1a36c0 2022-01-09 op fatal("fstat");
1211 fb1a36c0 2022-01-09 op if (S_ISDIR(sb.st_mode)) {
1212 fb1a36c0 2022-01-09 op if ((f->d = fdopendir(f->fd)) == NULL) {
1213 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1214 fb1a36c0 2022-01-09 op close(f->fd);
1219 fb1a36c0 2022-01-09 op if ((f->evb = evbuffer_new()) == NULL) {
1220 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1221 fb1a36c0 2022-01-09 op closedir(f->d);
1222 fb1a36c0 2022-01-09 op f->d = NULL;
1227 fb1a36c0 2022-01-09 op f->offset = 0;
1229 fb1a36c0 2022-01-09 op qid_update_from_sb(&qid, &sb);
1230 fb1a36c0 2022-01-09 op np_open(hdr->tag, &qid, sb.st_blksize);
1234 fb1a36c0 2022-01-09 op tcreate(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1236 fb1a36c0 2022-01-09 op struct stat sb;
1237 fb1a36c0 2022-01-09 op struct qid qid;
1238 fb1a36c0 2022-01-09 op struct fid *f;
1239 fb1a36c0 2022-01-09 op uint32_t fid, perm;
1240 fb1a36c0 2022-01-09 op uint8_t mode;
1241 fb1a36c0 2022-01-09 op char name[PATH_MAX];
1243 fb1a36c0 2022-01-09 op /* fid[4] name[s] perm[4] mode[1] */
1244 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len))
1246 fb1a36c0 2022-01-09 op switch (NPREADSTR("name", name, sizeof(name), &data, &len)) {
1247 fb1a36c0 2022-01-09 op case READSTRERR:
1249 fb1a36c0 2022-01-09 op case READSTRTRUNC:
1250 fb1a36c0 2022-01-09 op np_error(hdr->tag, "name too long");
1253 fb1a36c0 2022-01-09 op if (!NPREAD32("perm", &perm, &data, &len) ||
1254 fb1a36c0 2022-01-09 op !NPREAD8("mode", &mode, &data, &len))
1257 fb1a36c0 2022-01-09 op if (!strcmp(name, ".") || !strcmp(name, "..") ||
1258 fb1a36c0 2022-01-09 op strchr(name, '/') != NULL) {
1259 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid name");
1263 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1264 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1268 fb1a36c0 2022-01-09 op if (!(f->qid.type & QTDIR)) {
1269 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid doesn't identify a directory");
1273 fb1a36c0 2022-01-09 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1274 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid mode");
1278 fb1a36c0 2022-01-09 op if (f->iomode & O_RDONLY) {
1279 fb1a36c0 2022-01-09 op np_error(hdr->tag, "can't create a read-only file");
1283 fb1a36c0 2022-01-09 op /* TODO: parse the mode */
1285 fb1a36c0 2022-01-09 op if (perm & 0x80000000) {
1286 fb1a36c0 2022-01-09 op /* create a directory */
1287 fb1a36c0 2022-01-09 op f->fd = mkdirat(f->dir->fd, name, 0755);
1289 fb1a36c0 2022-01-09 op /* create a file */
1290 fb1a36c0 2022-01-09 op f->fd = openat(f->dir->fd, name, f->iomode | O_CREAT | O_TRUNC,
1294 fb1a36c0 2022-01-09 op if (f->fd == -1) {
1295 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1299 fb1a36c0 2022-01-09 op if (fstat(f->fd, &sb) == -1)
1300 fb1a36c0 2022-01-09 op fatal("fstat");
1302 fb1a36c0 2022-01-09 op if (S_ISDIR(sb.st_mode)) {
1303 fb1a36c0 2022-01-09 op if ((f->d = fdopendir(f->fd)) == NULL) {
1304 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1305 fb1a36c0 2022-01-09 op close(f->fd);
1310 fb1a36c0 2022-01-09 op if ((f->evb = evbuffer_new()) == NULL) {
1311 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1312 fb1a36c0 2022-01-09 op closedir(f->d);
1313 fb1a36c0 2022-01-09 op f->d = NULL;
1318 fb1a36c0 2022-01-09 op f->offset = 0;
1320 fb1a36c0 2022-01-09 op qid_update_from_sb(&qid, &sb);
1321 fb1a36c0 2022-01-09 op np_create(hdr->tag, &qid, sb.st_blksize);
1326 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1327 fb1a36c0 2022-01-09 op client_shutdown();
1330 fb1a36c0 2022-01-09 op static inline void
1331 fb1a36c0 2022-01-09 op serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1333 fb1a36c0 2022-01-09 op struct qid qid;
1334 fb1a36c0 2022-01-09 op const char *uid, *gid, *muid;
1336 fb1a36c0 2022-01-09 op uint16_t namlen, uidlen, gidlen, ulen;
1338 fb1a36c0 2022-01-09 op qid_update_from_sb(&qid, sb);
1340 fb1a36c0 2022-01-09 op /* TODO: fill these fields */
1345 fb1a36c0 2022-01-09 op namlen = strlen(fname);
1346 fb1a36c0 2022-01-09 op uidlen = strlen(uid);
1347 fb1a36c0 2022-01-09 op gidlen = strlen(gid);
1348 fb1a36c0 2022-01-09 op ulen = strlen(muid);
1350 fb1a36c0 2022-01-09 op tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1351 fb1a36c0 2022-01-09 op if (tot > UINT32_MAX) {
1352 fb1a36c0 2022-01-09 op log_warnx("stat info for dir entry %s would overflow",
1357 fb1a36c0 2022-01-09 op np_write16(evb, tot); /* size[2] */
1358 fb1a36c0 2022-01-09 op np_write16(evb, sb->st_rdev); /* type[2] */
1359 fb1a36c0 2022-01-09 op np_write32(evb, sb->st_dev); /* dev[4] */
1360 fb1a36c0 2022-01-09 op np_qid(evb, &qid); /* qid[13] */
1362 fb1a36c0 2022-01-09 op /* XXX: translate? */
1363 fb1a36c0 2022-01-09 op np_write32(evb, sb->st_mode); /* mode[4] */
1365 fb1a36c0 2022-01-09 op np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1366 fb1a36c0 2022-01-09 op np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1367 fb1a36c0 2022-01-09 op np_write64(evb, sb->st_size); /* length[8] */
1368 fb1a36c0 2022-01-09 op np_string(evb, namlen, fname); /* name[s] */
1369 fb1a36c0 2022-01-09 op np_string(evb, uidlen, uid); /* uid[s] */
1370 fb1a36c0 2022-01-09 op np_string(evb, gidlen, gid); /* gid[s] */
1371 fb1a36c0 2022-01-09 op np_string(evb, ulen, muid); /* muid[s] */
1375 fb1a36c0 2022-01-09 op tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1377 fb1a36c0 2022-01-09 op struct fid *f;
1379 fb1a36c0 2022-01-09 op size_t howmuch;
1380 fb1a36c0 2022-01-09 op uint64_t off;
1381 fb1a36c0 2022-01-09 op uint32_t fid, count;
1382 fb1a36c0 2022-01-09 op char buf[2048];
1384 fb1a36c0 2022-01-09 op /* fid[4] offset[8] count[4] */
1385 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1386 fb1a36c0 2022-01-09 op !NPREAD64("offset", &off, &data, &len) ||
1387 fb1a36c0 2022-01-09 op !NPREAD32("count", &count, &data, &len)) {
1388 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1389 fb1a36c0 2022-01-09 op client_shutdown();
1393 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1394 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1398 fb1a36c0 2022-01-09 op if (TYPE_OVERFLOW(off_t, off)) {
1399 fb1a36c0 2022-01-09 op log_warnx("unexpected off_t size");
1400 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid offset");
1404 fb1a36c0 2022-01-09 op if (f->d == NULL) {
1405 fb1a36c0 2022-01-09 op /* read a file */
1406 fb1a36c0 2022-01-09 op howmuch = MIN(sizeof(buf), count);
1407 fb1a36c0 2022-01-09 op r = pread(f->fd, buf, howmuch, (off_t)off);
1408 fb1a36c0 2022-01-09 op if (r == -1)
1409 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1411 fb1a36c0 2022-01-09 op np_read(hdr->tag, r, buf);
1413 fb1a36c0 2022-01-09 op if (off == 0 && f->offset != 0) {
1414 fb1a36c0 2022-01-09 op rewinddir(f->d);
1415 fb1a36c0 2022-01-09 op f->offset = 0;
1416 fb1a36c0 2022-01-09 op evbuffer_drain(f->evb, EVBUFFER_LENGTH(f->evb));
1419 fb1a36c0 2022-01-09 op if (off != f->offset) {
1420 fb1a36c0 2022-01-09 op np_error(hdr->tag, "can't seek in directories");
1424 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(f->evb) < count) {
1425 fb1a36c0 2022-01-09 op struct dirent *d;
1426 fb1a36c0 2022-01-09 op struct stat sb;
1428 fb1a36c0 2022-01-09 op if ((d = readdir(f->d)) == NULL)
1430 fb1a36c0 2022-01-09 op if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1431 fb1a36c0 2022-01-09 op warn("fstatat");
1434 fb1a36c0 2022-01-09 op serialize_stat(d->d_name, &sb, f->evb);
1437 fb1a36c0 2022-01-09 op count = MIN(count, EVBUFFER_LENGTH(f->evb));
1438 fb1a36c0 2022-01-09 op np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1439 fb1a36c0 2022-01-09 op evbuffer_drain(f->evb, count);
1441 fb1a36c0 2022-01-09 op f->offset += count;
1446 fb1a36c0 2022-01-09 op twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1448 fb1a36c0 2022-01-09 op struct fid *f;
1450 fb1a36c0 2022-01-09 op uint64_t off;
1451 fb1a36c0 2022-01-09 op uint32_t fid, count;
1453 fb1a36c0 2022-01-09 op /* fid[4] offset[8] count[4] data[count] */
1454 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1455 fb1a36c0 2022-01-09 op !NPREAD64("off", &off, &data, &len) ||
1456 fb1a36c0 2022-01-09 op !NPREAD32("count", &count, &data, &len) ||
1457 fb1a36c0 2022-01-09 op len != count) {
1458 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1459 fb1a36c0 2022-01-09 op client_shutdown();
1463 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1464 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1468 fb1a36c0 2022-01-09 op if (!(f->iomode & O_WRONLY) &&
1469 fb1a36c0 2022-01-09 op !(f->iomode & O_RDWR)) {
1470 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid not opened for writing");
1474 fb1a36c0 2022-01-09 op if (TYPE_OVERFLOW(off_t, off)) {
1475 fb1a36c0 2022-01-09 op log_warnx("unexpected off_t size");
1476 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid offset");
1480 fb1a36c0 2022-01-09 op if ((r = pwrite(f->fd, data, len, off)) == -1)
1481 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1483 fb1a36c0 2022-01-09 op np_write(hdr->tag, r);
1487 fb1a36c0 2022-01-09 op tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1489 fb1a36c0 2022-01-09 op struct evbuffer *evb;
1490 fb1a36c0 2022-01-09 op struct stat sb;
1491 fb1a36c0 2022-01-09 op struct fid *f;
1493 fb1a36c0 2022-01-09 op uint32_t fid;
1495 fb1a36c0 2022-01-09 op /* fid[4] */
1496 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len)) {
1497 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1498 fb1a36c0 2022-01-09 op client_shutdown();
1503 fb1a36c0 2022-01-09 op * plan9' stat(9P) is not clear on whether the stat is allowed
1504 fb1a36c0 2022-01-09 op * on opened fids or not. We're allowing stat regardless of the
1505 fb1a36c0 2022-01-09 op * status of the fid.
1508 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
1509 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1513 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1514 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1516 fb1a36c0 2022-01-09 op if (f->fd != -1)
1517 fb1a36c0 2022-01-09 op r = fstat(f->fd, &sb);
1518 fb1a36c0 2022-01-09 op else if (f->qid.type & QTDIR)
1519 fb1a36c0 2022-01-09 op r = fstat(f->dir->fd, &sb);
1521 fb1a36c0 2022-01-09 op r = fstatat(f->dir->fd, f->fpath, &sb, 0);
1523 fb1a36c0 2022-01-09 op if (r == -1) {
1524 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1525 fb1a36c0 2022-01-09 op evbuffer_free(evb);
1529 fb1a36c0 2022-01-09 op serialize_stat(f->fpath, &sb, evb);
1530 fb1a36c0 2022-01-09 op np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1531 fb1a36c0 2022-01-09 op evbuffer_free(evb);
1535 fb1a36c0 2022-01-09 op tremove(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1537 fb1a36c0 2022-01-09 op struct fid *f;
1538 fb1a36c0 2022-01-09 op uint32_t fid;
1540 fb1a36c0 2022-01-09 op char dirpath[PATH_MAX + 3];
1542 fb1a36c0 2022-01-09 op /* fid[4] */
1543 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len)) {
1544 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1545 fb1a36c0 2022-01-09 op client_shutdown();
1549 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
1550 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1554 fb1a36c0 2022-01-09 op if (f->qid.type & QTDIR) { /* directory */
1555 fb1a36c0 2022-01-09 op strlcpy(dirpath, "../", sizeof(dirpath));
1556 fb1a36c0 2022-01-09 op strlcat(dirpath, f->fpath, sizeof(dirpath));
1557 fb1a36c0 2022-01-09 op r = unlinkat(f->dir->fd, dirpath, AT_REMOVEDIR);
1558 fb1a36c0 2022-01-09 op } else /* file */
1559 fb1a36c0 2022-01-09 op r = unlinkat(f->dir->fd, f->fpath, 0);
1561 fb1a36c0 2022-01-09 op if (r == -1)
1562 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1564 fb1a36c0 2022-01-09 op np_remove(hdr->tag);
1566 fb1a36c0 2022-01-09 op free_fid(f);
1570 fb1a36c0 2022-01-09 op handle_message(struct imsg *imsg, size_t len)
1572 fb1a36c0 2022-01-09 op struct msg {
1573 fb1a36c0 2022-01-09 op uint8_t type;
1574 fb1a36c0 2022-01-09 op void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1575 fb1a36c0 2022-01-09 op } msgs[] = {
1576 fb1a36c0 2022-01-09 op {Tversion, tversion},
1577 fb1a36c0 2022-01-09 op {Tattach, tattach},
1578 fb1a36c0 2022-01-09 op {Tclunk, tclunk},
1579 fb1a36c0 2022-01-09 op {Tflush, tflush},
1580 fb1a36c0 2022-01-09 op {Twalk, twalk},
1581 fb1a36c0 2022-01-09 op {Topen, topen},
1582 fb1a36c0 2022-01-09 op {Tcreate, tcreate},
1583 fb1a36c0 2022-01-09 op {Tread, tread},
1584 fb1a36c0 2022-01-09 op {Twrite, twrite},
1585 fb1a36c0 2022-01-09 op {Tstat, tstat},
1586 fb1a36c0 2022-01-09 op {Tremove, tremove},
1588 fb1a36c0 2022-01-09 op struct np_msg_header hdr;
1590 fb1a36c0 2022-01-09 op uint8_t *data;
1592 fb1a36c0 2022-01-09 op #if DEBUG_PACKETS
1593 fb1a36c0 2022-01-09 op hexdump("incoming packet", imsg->data, len);
1596 fb1a36c0 2022-01-09 op parse_message(imsg->data, len, &hdr, &data);
1597 fb1a36c0 2022-01-09 op len -= HEADERSIZE;
1599 fb1a36c0 2022-01-09 op log_debug("got request: len=%d type=%d[%s] tag=%d",
1600 fb1a36c0 2022-01-09 op hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1602 fb1a36c0 2022-01-09 op if (!handshaked && hdr.type != Tversion) {
1603 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1604 fb1a36c0 2022-01-09 op client_shutdown();
1608 fb1a36c0 2022-01-09 op for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1609 fb1a36c0 2022-01-09 op if (msgs[i].type != hdr.type)
1612 fb1a36c0 2022-01-09 op msgs[i].fn(&hdr, data, len);
1616 fb1a36c0 2022-01-09 op np_error(hdr.tag, "Not supported.");