usb: usb-uas QOMify
[qemu/rayw.git] / fsdev / virtfs-proxy-helper.c
bloba698e2dbb37b6e9363ae20e2af4a4be36ebf7c97
1 /*
2 * Helper for QEMU Proxy FS Driver
3 * Copyright IBM, Corp. 2011
5 * Authors:
6 * M. Mohan Kumar <mohan@in.ibm.com>
8 * This work is licensed under the terms of the GNU GPL, version 2. See
9 * the COPYING file in the top-level directory.
12 #include <sys/resource.h>
13 #include <getopt.h>
14 #include <syslog.h>
15 #include <sys/capability.h>
16 #include <sys/fsuid.h>
17 #include <sys/vfs.h>
18 #include <sys/ioctl.h>
19 #include <linux/fs.h>
20 #ifdef CONFIG_LINUX_MAGIC_H
21 #include <linux/magic.h>
22 #endif
23 #include "qemu-common.h"
24 #include "qemu/sockets.h"
25 #include "qemu/xattr.h"
26 #include "virtio-9p-marshal.h"
27 #include "hw/9pfs/virtio-9p-proxy.h"
28 #include "fsdev/virtio-9p-marshal.h"
30 #define PROGNAME "virtfs-proxy-helper"
32 #ifndef XFS_SUPER_MAGIC
33 #define XFS_SUPER_MAGIC 0x58465342
34 #endif
35 #ifndef EXT2_SUPER_MAGIC
36 #define EXT2_SUPER_MAGIC 0xEF53
37 #endif
38 #ifndef REISERFS_SUPER_MAGIC
39 #define REISERFS_SUPER_MAGIC 0x52654973
40 #endif
41 #ifndef BTRFS_SUPER_MAGIC
42 #define BTRFS_SUPER_MAGIC 0x9123683E
43 #endif
45 static struct option helper_opts[] = {
46 {"fd", required_argument, NULL, 'f'},
47 {"path", required_argument, NULL, 'p'},
48 {"nodaemon", no_argument, NULL, 'n'},
49 {"socket", required_argument, NULL, 's'},
50 {"uid", required_argument, NULL, 'u'},
51 {"gid", required_argument, NULL, 'g'},
54 static bool is_daemon;
55 static bool get_version; /* IOC getversion IOCTL supported */
57 static void GCC_FMT_ATTR(2, 3) do_log(int loglevel, const char *format, ...)
59 va_list ap;
61 va_start(ap, format);
62 if (is_daemon) {
63 vsyslog(LOG_CRIT, format, ap);
64 } else {
65 vfprintf(stderr, format, ap);
67 va_end(ap);
70 static void do_perror(const char *string)
72 if (is_daemon) {
73 syslog(LOG_CRIT, "%s:%s", string, strerror(errno));
74 } else {
75 fprintf(stderr, "%s:%s\n", string, strerror(errno));
79 static int do_cap_set(cap_value_t *cap_value, int size, int reset)
81 cap_t caps;
82 if (reset) {
84 * Start with an empty set and set permitted and effective
86 caps = cap_init();
87 if (caps == NULL) {
88 do_perror("cap_init");
89 return -1;
91 if (cap_set_flag(caps, CAP_PERMITTED, size, cap_value, CAP_SET) < 0) {
92 do_perror("cap_set_flag");
93 goto error;
95 } else {
96 caps = cap_get_proc();
97 if (!caps) {
98 do_perror("cap_get_proc");
99 return -1;
102 if (cap_set_flag(caps, CAP_EFFECTIVE, size, cap_value, CAP_SET) < 0) {
103 do_perror("cap_set_flag");
104 goto error;
106 if (cap_set_proc(caps) < 0) {
107 do_perror("cap_set_proc");
108 goto error;
110 cap_free(caps);
111 return 0;
113 error:
114 cap_free(caps);
115 return -1;
118 static int init_capabilities(void)
120 /* helper needs following capabilities only */
121 cap_value_t cap_list[] = {
122 CAP_CHOWN,
123 CAP_DAC_OVERRIDE,
124 CAP_FOWNER,
125 CAP_FSETID,
126 CAP_SETGID,
127 CAP_MKNOD,
128 CAP_SETUID,
130 return do_cap_set(cap_list, ARRAY_SIZE(cap_list), 1);
133 static int socket_read(int sockfd, void *buff, ssize_t size)
135 ssize_t retval, total = 0;
137 while (size) {
138 retval = read(sockfd, buff, size);
139 if (retval == 0) {
140 return -EIO;
142 if (retval < 0) {
143 if (errno == EINTR) {
144 continue;
146 return -errno;
148 size -= retval;
149 buff += retval;
150 total += retval;
152 return total;
155 static int socket_write(int sockfd, void *buff, ssize_t size)
157 ssize_t retval, total = 0;
159 while (size) {
160 retval = write(sockfd, buff, size);
161 if (retval < 0) {
162 if (errno == EINTR) {
163 continue;
165 return -errno;
167 size -= retval;
168 buff += retval;
169 total += retval;
171 return total;
174 static int read_request(int sockfd, struct iovec *iovec, ProxyHeader *header)
176 int retval;
179 * read the request header.
181 iovec->iov_len = 0;
182 retval = socket_read(sockfd, iovec->iov_base, PROXY_HDR_SZ);
183 if (retval < 0) {
184 return retval;
186 iovec->iov_len = PROXY_HDR_SZ;
187 retval = proxy_unmarshal(iovec, 0, "dd", &header->type, &header->size);
188 if (retval < 0) {
189 return retval;
192 * We can't process message.size > PROXY_MAX_IO_SZ.
193 * Treat it as fatal error
195 if (header->size > PROXY_MAX_IO_SZ) {
196 return -ENOBUFS;
198 retval = socket_read(sockfd, iovec->iov_base + PROXY_HDR_SZ, header->size);
199 if (retval < 0) {
200 return retval;
202 iovec->iov_len += header->size;
203 return 0;
206 static int send_fd(int sockfd, int fd)
208 struct msghdr msg;
209 struct iovec iov;
210 int retval, data;
211 struct cmsghdr *cmsg;
212 union MsgControl msg_control;
214 iov.iov_base = &data;
215 iov.iov_len = sizeof(data);
217 memset(&msg, 0, sizeof(msg));
218 msg.msg_iov = &iov;
219 msg.msg_iovlen = 1;
220 /* No ancillary data on error */
221 if (fd < 0) {
222 /* fd is really negative errno if the request failed */
223 data = fd;
224 } else {
225 data = V9FS_FD_VALID;
226 msg.msg_control = &msg_control;
227 msg.msg_controllen = sizeof(msg_control);
229 cmsg = &msg_control.cmsg;
230 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
231 cmsg->cmsg_level = SOL_SOCKET;
232 cmsg->cmsg_type = SCM_RIGHTS;
233 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
236 do {
237 retval = sendmsg(sockfd, &msg, 0);
238 } while (retval < 0 && errno == EINTR);
239 if (fd >= 0) {
240 close(fd);
242 if (retval < 0) {
243 return retval;
245 return 0;
248 static int send_status(int sockfd, struct iovec *iovec, int status)
250 ProxyHeader header;
251 int retval, msg_size;
253 if (status < 0) {
254 header.type = T_ERROR;
255 } else {
256 header.type = T_SUCCESS;
258 header.size = sizeof(status);
260 * marshal the return status. We don't check error.
261 * because we are sure we have enough space for the status
263 msg_size = proxy_marshal(iovec, 0, "ddd", header.type,
264 header.size, status);
265 if (msg_size < 0) {
266 return msg_size;
268 retval = socket_write(sockfd, iovec->iov_base, msg_size);
269 if (retval < 0) {
270 return retval;
272 return 0;
276 * from man 7 capabilities, section
277 * Effect of User ID Changes on Capabilities:
278 * If the effective user ID is changed from nonzero to 0, then the permitted
279 * set is copied to the effective set. If the effective user ID is changed
280 * from 0 to nonzero, then all capabilities are are cleared from the effective
281 * set.
283 * The setfsuid/setfsgid man pages warn that changing the effective user ID may
284 * expose the program to unwanted signals, but this is not true anymore: for an
285 * unprivileged (without CAP_KILL) program to send a signal, the real or
286 * effective user ID of the sending process must equal the real or saved user
287 * ID of the target process. Even when dropping privileges, it is enough to
288 * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
289 * be exposed to signals. So just use setresuid/setresgid.
291 static int setugid(int uid, int gid, int *suid, int *sgid)
293 int retval;
296 * We still need DAC_OVERRIDE because we don't change
297 * supplementary group ids, and hence may be subjected DAC rules
299 cap_value_t cap_list[] = {
300 CAP_DAC_OVERRIDE,
303 *suid = geteuid();
304 *sgid = getegid();
306 if (setresgid(-1, gid, *sgid) == -1) {
307 retval = -errno;
308 goto err_out;
311 if (setresuid(-1, uid, *suid) == -1) {
312 retval = -errno;
313 goto err_sgid;
316 if (uid != 0 || gid != 0) {
317 if (do_cap_set(cap_list, ARRAY_SIZE(cap_list), 0) < 0) {
318 retval = -errno;
319 goto err_suid;
322 return 0;
324 err_suid:
325 if (setresuid(-1, *suid, *suid) == -1) {
326 abort();
328 err_sgid:
329 if (setresgid(-1, *sgid, *sgid) == -1) {
330 abort();
332 err_out:
333 return retval;
337 * This is used to reset the ugid back with the saved values
338 * There is nothing much we can do checking error values here.
340 static void resetugid(int suid, int sgid)
342 if (setresgid(-1, sgid, sgid) == -1) {
343 abort();
345 if (setresuid(-1, suid, suid) == -1) {
346 abort();
351 * send response in two parts
352 * 1) ProxyHeader
353 * 2) Response or error status
354 * This function should be called with marshaled response
355 * send_response constructs header part and error part only.
356 * send response sends {ProxyHeader,Response} if the request was success
357 * otherwise sends {ProxyHeader,error status}
359 static int send_response(int sock, struct iovec *iovec, int size)
361 int retval;
362 ProxyHeader header;
365 * If response size exceeds available iovec->iov_len,
366 * we return ENOBUFS
368 if (size > PROXY_MAX_IO_SZ) {
369 size = -ENOBUFS;
372 if (size < 0) {
374 * In case of error we would not have got the error encoded
375 * already so encode the error here.
377 header.type = T_ERROR;
378 header.size = sizeof(size);
379 proxy_marshal(iovec, PROXY_HDR_SZ, "d", size);
380 } else {
381 header.type = T_SUCCESS;
382 header.size = size;
384 proxy_marshal(iovec, 0, "dd", header.type, header.size);
385 retval = socket_write(sock, iovec->iov_base, header.size + PROXY_HDR_SZ);
386 if (retval < 0) {
387 return retval;
389 return 0;
393 * gets generation number
394 * returns -errno on failure and sizeof(generation number) on success
396 static int do_getversion(struct iovec *iovec, struct iovec *out_iovec)
398 uint64_t version;
399 int retval = -ENOTTY;
400 #ifdef FS_IOC_GETVERSION
401 int fd;
402 V9fsString path;
403 #endif
406 /* no need to issue ioctl */
407 if (!get_version) {
408 version = 0;
409 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
410 return retval;
412 #ifdef FS_IOC_GETVERSION
413 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
414 if (retval < 0) {
415 return retval;
418 fd = open(path.data, O_RDONLY);
419 if (fd < 0) {
420 retval = -errno;
421 goto err_out;
423 if (ioctl(fd, FS_IOC_GETVERSION, &version) < 0) {
424 retval = -errno;
425 } else {
426 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
428 close(fd);
429 err_out:
430 v9fs_string_free(&path);
431 #endif
432 return retval;
435 static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec)
437 int size = 0, offset, retval;
438 V9fsString path, name, xattr;
440 v9fs_string_init(&xattr);
441 v9fs_string_init(&path);
442 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "ds", &size, &path);
443 if (retval < 0) {
444 return retval;
446 offset = PROXY_HDR_SZ + retval;
448 if (size) {
449 xattr.data = g_malloc(size);
450 xattr.size = size;
452 switch (type) {
453 case T_LGETXATTR:
454 v9fs_string_init(&name);
455 retval = proxy_unmarshal(iovec, offset, "s", &name);
456 if (retval > 0) {
457 retval = lgetxattr(path.data, name.data, xattr.data, size);
458 if (retval < 0) {
459 retval = -errno;
460 } else {
461 xattr.size = retval;
464 v9fs_string_free(&name);
465 break;
466 case T_LLISTXATTR:
467 retval = llistxattr(path.data, xattr.data, size);
468 if (retval < 0) {
469 retval = -errno;
470 } else {
471 xattr.size = retval;
473 break;
475 if (retval < 0) {
476 goto err_out;
479 if (!size) {
480 proxy_marshal(out_iovec, PROXY_HDR_SZ, "d", retval);
481 retval = sizeof(retval);
482 } else {
483 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &xattr);
485 err_out:
486 v9fs_string_free(&xattr);
487 v9fs_string_free(&path);
488 return retval;
491 static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat)
493 memset(pr_stat, 0, sizeof(*pr_stat));
494 pr_stat->st_dev = stat->st_dev;
495 pr_stat->st_ino = stat->st_ino;
496 pr_stat->st_nlink = stat->st_nlink;
497 pr_stat->st_mode = stat->st_mode;
498 pr_stat->st_uid = stat->st_uid;
499 pr_stat->st_gid = stat->st_gid;
500 pr_stat->st_rdev = stat->st_rdev;
501 pr_stat->st_size = stat->st_size;
502 pr_stat->st_blksize = stat->st_blksize;
503 pr_stat->st_blocks = stat->st_blocks;
504 pr_stat->st_atim_sec = stat->st_atim.tv_sec;
505 pr_stat->st_atim_nsec = stat->st_atim.tv_nsec;
506 pr_stat->st_mtim_sec = stat->st_mtim.tv_sec;
507 pr_stat->st_mtim_nsec = stat->st_mtim.tv_nsec;
508 pr_stat->st_ctim_sec = stat->st_ctim.tv_sec;
509 pr_stat->st_ctim_nsec = stat->st_ctim.tv_nsec;
512 static void statfs_to_prstatfs(ProxyStatFS *pr_stfs, struct statfs *stfs)
514 memset(pr_stfs, 0, sizeof(*pr_stfs));
515 pr_stfs->f_type = stfs->f_type;
516 pr_stfs->f_bsize = stfs->f_bsize;
517 pr_stfs->f_blocks = stfs->f_blocks;
518 pr_stfs->f_bfree = stfs->f_bfree;
519 pr_stfs->f_bavail = stfs->f_bavail;
520 pr_stfs->f_files = stfs->f_files;
521 pr_stfs->f_ffree = stfs->f_ffree;
522 pr_stfs->f_fsid[0] = stfs->f_fsid.__val[0];
523 pr_stfs->f_fsid[1] = stfs->f_fsid.__val[1];
524 pr_stfs->f_namelen = stfs->f_namelen;
525 pr_stfs->f_frsize = stfs->f_frsize;
529 * Gets stat/statfs information and packs in out_iovec structure
530 * on success returns number of bytes packed in out_iovec struture
531 * otherwise returns -errno
533 static int do_stat(int type, struct iovec *iovec, struct iovec *out_iovec)
535 int retval;
536 V9fsString path;
537 ProxyStat pr_stat;
538 ProxyStatFS pr_stfs;
539 struct stat st_buf;
540 struct statfs stfs_buf;
542 v9fs_string_init(&path);
543 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
544 if (retval < 0) {
545 return retval;
548 switch (type) {
549 case T_LSTAT:
550 retval = lstat(path.data, &st_buf);
551 if (retval < 0) {
552 retval = -errno;
553 } else {
554 stat_to_prstat(&pr_stat, &st_buf);
555 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
556 "qqqdddqqqqqqqqqq", pr_stat.st_dev,
557 pr_stat.st_ino, pr_stat.st_nlink,
558 pr_stat.st_mode, pr_stat.st_uid,
559 pr_stat.st_gid, pr_stat.st_rdev,
560 pr_stat.st_size, pr_stat.st_blksize,
561 pr_stat.st_blocks,
562 pr_stat.st_atim_sec, pr_stat.st_atim_nsec,
563 pr_stat.st_mtim_sec, pr_stat.st_mtim_nsec,
564 pr_stat.st_ctim_sec, pr_stat.st_ctim_nsec);
566 break;
567 case T_STATFS:
568 retval = statfs(path.data, &stfs_buf);
569 if (retval < 0) {
570 retval = -errno;
571 } else {
572 statfs_to_prstatfs(&pr_stfs, &stfs_buf);
573 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
574 "qqqqqqqqqqq", pr_stfs.f_type,
575 pr_stfs.f_bsize, pr_stfs.f_blocks,
576 pr_stfs.f_bfree, pr_stfs.f_bavail,
577 pr_stfs.f_files, pr_stfs.f_ffree,
578 pr_stfs.f_fsid[0], pr_stfs.f_fsid[1],
579 pr_stfs.f_namelen, pr_stfs.f_frsize);
581 break;
583 v9fs_string_free(&path);
584 return retval;
587 static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
589 char *buffer;
590 int size, retval;
591 V9fsString target, path;
593 v9fs_string_init(&path);
594 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &size);
595 if (retval < 0) {
596 v9fs_string_free(&path);
597 return retval;
599 buffer = g_malloc(size);
600 v9fs_string_init(&target);
601 retval = readlink(path.data, buffer, size - 1);
602 if (retval > 0) {
603 buffer[retval] = '\0';
604 v9fs_string_sprintf(&target, "%s", buffer);
605 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &target);
606 } else {
607 retval = -errno;
609 g_free(buffer);
610 v9fs_string_free(&target);
611 v9fs_string_free(&path);
612 return retval;
616 * create other filesystem objects and send 0 on success
617 * return -errno on error
619 static int do_create_others(int type, struct iovec *iovec)
621 dev_t rdev;
622 int retval = 0;
623 int offset = PROXY_HDR_SZ;
624 V9fsString oldpath, path;
625 int mode, uid, gid, cur_uid, cur_gid;
627 v9fs_string_init(&path);
628 v9fs_string_init(&oldpath);
630 retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid);
631 if (retval < 0) {
632 return retval;
634 offset += retval;
635 retval = setugid(uid, gid, &cur_uid, &cur_gid);
636 if (retval < 0) {
637 goto unmarshal_err_out;
639 switch (type) {
640 case T_MKNOD:
641 retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev);
642 if (retval < 0) {
643 goto err_out;
645 retval = mknod(path.data, mode, rdev);
646 break;
647 case T_MKDIR:
648 retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode);
649 if (retval < 0) {
650 goto err_out;
652 retval = mkdir(path.data, mode);
653 break;
654 case T_SYMLINK:
655 retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path);
656 if (retval < 0) {
657 goto err_out;
659 retval = symlink(oldpath.data, path.data);
660 break;
662 if (retval < 0) {
663 retval = -errno;
666 err_out:
667 resetugid(cur_uid, cur_gid);
668 unmarshal_err_out:
669 v9fs_string_free(&path);
670 v9fs_string_free(&oldpath);
671 return retval;
675 * create a file and send fd on success
676 * return -errno on error
678 static int do_create(struct iovec *iovec)
680 int ret;
681 V9fsString path;
682 int flags, mode, uid, gid, cur_uid, cur_gid;
684 v9fs_string_init(&path);
685 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sdddd",
686 &path, &flags, &mode, &uid, &gid);
687 if (ret < 0) {
688 goto unmarshal_err_out;
690 ret = setugid(uid, gid, &cur_uid, &cur_gid);
691 if (ret < 0) {
692 goto unmarshal_err_out;
694 ret = open(path.data, flags, mode);
695 if (ret < 0) {
696 ret = -errno;
699 resetugid(cur_uid, cur_gid);
700 unmarshal_err_out:
701 v9fs_string_free(&path);
702 return ret;
706 * open a file and send fd on success
707 * return -errno on error
709 static int do_open(struct iovec *iovec)
711 int flags, ret;
712 V9fsString path;
714 v9fs_string_init(&path);
715 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &flags);
716 if (ret < 0) {
717 goto err_out;
719 ret = open(path.data, flags);
720 if (ret < 0) {
721 ret = -errno;
723 err_out:
724 v9fs_string_free(&path);
725 return ret;
728 /* create unix domain socket and return the descriptor */
729 static int proxy_socket(const char *path, uid_t uid, gid_t gid)
731 int sock, client;
732 struct sockaddr_un proxy, qemu;
733 socklen_t size;
735 /* requested socket already exists, refuse to start */
736 if (!access(path, F_OK)) {
737 do_log(LOG_CRIT, "socket already exists\n");
738 return -1;
741 g_assert(strlen(path) < sizeof(proxy.sun_path));
742 sock = socket(AF_UNIX, SOCK_STREAM, 0);
743 if (sock < 0) {
744 do_perror("socket");
745 return -1;
748 /* mask other part of mode bits */
749 umask(7);
751 proxy.sun_family = AF_UNIX;
752 strcpy(proxy.sun_path, path);
753 if (bind(sock, (struct sockaddr *)&proxy,
754 sizeof(struct sockaddr_un)) < 0) {
755 do_perror("bind");
756 goto error;
758 if (chown(proxy.sun_path, uid, gid) < 0) {
759 do_perror("chown");
760 goto error;
762 if (listen(sock, 1) < 0) {
763 do_perror("listen");
764 goto error;
767 size = sizeof(qemu);
768 client = accept(sock, (struct sockaddr *)&qemu, &size);
769 if (client < 0) {
770 do_perror("accept");
771 goto error;
773 close(sock);
774 return client;
776 error:
777 close(sock);
778 return -1;
781 static void usage(char *prog)
783 fprintf(stderr, "usage: %s\n"
784 " -p|--path <path> 9p path to export\n"
785 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
786 " {-s|--socket <socketname> socket file used for communication\n"
787 " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
788 " access to this socket\n"
789 " \tNote: -s & -f can not be used together\n"
790 " [-n|--nodaemon] Run as a normal program\n",
791 basename(prog));
794 static int process_reply(int sock, int type,
795 struct iovec *out_iovec, int retval)
797 switch (type) {
798 case T_OPEN:
799 case T_CREATE:
800 if (send_fd(sock, retval) < 0) {
801 return -1;
803 break;
804 case T_MKNOD:
805 case T_MKDIR:
806 case T_SYMLINK:
807 case T_LINK:
808 case T_CHMOD:
809 case T_CHOWN:
810 case T_TRUNCATE:
811 case T_UTIME:
812 case T_RENAME:
813 case T_REMOVE:
814 case T_LSETXATTR:
815 case T_LREMOVEXATTR:
816 if (send_status(sock, out_iovec, retval) < 0) {
817 return -1;
819 break;
820 case T_LSTAT:
821 case T_STATFS:
822 case T_READLINK:
823 case T_LGETXATTR:
824 case T_LLISTXATTR:
825 case T_GETVERSION:
826 if (send_response(sock, out_iovec, retval) < 0) {
827 return -1;
829 break;
830 default:
831 return -1;
832 break;
834 return 0;
837 static int process_requests(int sock)
839 int flags;
840 int size = 0;
841 int retval = 0;
842 uint64_t offset;
843 ProxyHeader header;
844 int mode, uid, gid;
845 V9fsString name, value;
846 struct timespec spec[2];
847 V9fsString oldpath, path;
848 struct iovec in_iovec, out_iovec;
850 in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
851 in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
852 out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
853 out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
855 while (1) {
857 * initialize the header type, so that we send
858 * response to proper request type.
860 header.type = 0;
861 retval = read_request(sock, &in_iovec, &header);
862 if (retval < 0) {
863 goto err_out;
866 switch (header.type) {
867 case T_OPEN:
868 retval = do_open(&in_iovec);
869 break;
870 case T_CREATE:
871 retval = do_create(&in_iovec);
872 break;
873 case T_MKNOD:
874 case T_MKDIR:
875 case T_SYMLINK:
876 retval = do_create_others(header.type, &in_iovec);
877 break;
878 case T_LINK:
879 v9fs_string_init(&path);
880 v9fs_string_init(&oldpath);
881 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
882 "ss", &oldpath, &path);
883 if (retval > 0) {
884 retval = link(oldpath.data, path.data);
885 if (retval < 0) {
886 retval = -errno;
889 v9fs_string_free(&oldpath);
890 v9fs_string_free(&path);
891 break;
892 case T_LSTAT:
893 case T_STATFS:
894 retval = do_stat(header.type, &in_iovec, &out_iovec);
895 break;
896 case T_READLINK:
897 retval = do_readlink(&in_iovec, &out_iovec);
898 break;
899 case T_CHMOD:
900 v9fs_string_init(&path);
901 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
902 "sd", &path, &mode);
903 if (retval > 0) {
904 retval = chmod(path.data, mode);
905 if (retval < 0) {
906 retval = -errno;
909 v9fs_string_free(&path);
910 break;
911 case T_CHOWN:
912 v9fs_string_init(&path);
913 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path,
914 &uid, &gid);
915 if (retval > 0) {
916 retval = lchown(path.data, uid, gid);
917 if (retval < 0) {
918 retval = -errno;
921 v9fs_string_free(&path);
922 break;
923 case T_TRUNCATE:
924 v9fs_string_init(&path);
925 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq",
926 &path, &offset);
927 if (retval > 0) {
928 retval = truncate(path.data, offset);
929 if (retval < 0) {
930 retval = -errno;
933 v9fs_string_free(&path);
934 break;
935 case T_UTIME:
936 v9fs_string_init(&path);
937 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sqqqq", &path,
938 &spec[0].tv_sec, &spec[0].tv_nsec,
939 &spec[1].tv_sec, &spec[1].tv_nsec);
940 if (retval > 0) {
941 retval = qemu_utimens(path.data, spec);
942 if (retval < 0) {
943 retval = -errno;
946 v9fs_string_free(&path);
947 break;
948 case T_RENAME:
949 v9fs_string_init(&path);
950 v9fs_string_init(&oldpath);
951 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
952 "ss", &oldpath, &path);
953 if (retval > 0) {
954 retval = rename(oldpath.data, path.data);
955 if (retval < 0) {
956 retval = -errno;
959 v9fs_string_free(&oldpath);
960 v9fs_string_free(&path);
961 break;
962 case T_REMOVE:
963 v9fs_string_init(&path);
964 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path);
965 if (retval > 0) {
966 retval = remove(path.data);
967 if (retval < 0) {
968 retval = -errno;
971 v9fs_string_free(&path);
972 break;
973 case T_LGETXATTR:
974 case T_LLISTXATTR:
975 retval = do_getxattr(header.type, &in_iovec, &out_iovec);
976 break;
977 case T_LSETXATTR:
978 v9fs_string_init(&path);
979 v9fs_string_init(&name);
980 v9fs_string_init(&value);
981 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path,
982 &name, &value, &size, &flags);
983 if (retval > 0) {
984 retval = lsetxattr(path.data,
985 name.data, value.data, size, flags);
986 if (retval < 0) {
987 retval = -errno;
990 v9fs_string_free(&path);
991 v9fs_string_free(&name);
992 v9fs_string_free(&value);
993 break;
994 case T_LREMOVEXATTR:
995 v9fs_string_init(&path);
996 v9fs_string_init(&name);
997 retval = proxy_unmarshal(&in_iovec,
998 PROXY_HDR_SZ, "ss", &path, &name);
999 if (retval > 0) {
1000 retval = lremovexattr(path.data, name.data);
1001 if (retval < 0) {
1002 retval = -errno;
1005 v9fs_string_free(&path);
1006 v9fs_string_free(&name);
1007 break;
1008 case T_GETVERSION:
1009 retval = do_getversion(&in_iovec, &out_iovec);
1010 break;
1011 default:
1012 goto err_out;
1013 break;
1016 if (process_reply(sock, header.type, &out_iovec, retval) < 0) {
1017 goto err_out;
1020 err_out:
1021 g_free(in_iovec.iov_base);
1022 g_free(out_iovec.iov_base);
1023 return -1;
1026 int main(int argc, char **argv)
1028 int sock;
1029 uid_t own_u;
1030 gid_t own_g;
1031 char *rpath = NULL;
1032 char *sock_name = NULL;
1033 struct stat stbuf;
1034 int c, option_index;
1035 #ifdef FS_IOC_GETVERSION
1036 int retval;
1037 struct statfs st_fs;
1038 #endif
1040 is_daemon = true;
1041 sock = -1;
1042 own_u = own_g = -1;
1043 while (1) {
1044 option_index = 0;
1045 c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts,
1046 &option_index);
1047 if (c == -1) {
1048 break;
1050 switch (c) {
1051 case 'p':
1052 rpath = g_strdup(optarg);
1053 break;
1054 case 'n':
1055 is_daemon = false;
1056 break;
1057 case 'f':
1058 sock = atoi(optarg);
1059 break;
1060 case 's':
1061 sock_name = g_strdup(optarg);
1062 break;
1063 case 'u':
1064 own_u = atoi(optarg);
1065 break;
1066 case 'g':
1067 own_g = atoi(optarg);
1068 break;
1069 case '?':
1070 case 'h':
1071 default:
1072 usage(argv[0]);
1073 exit(EXIT_FAILURE);
1077 /* Parameter validation */
1078 if ((sock_name == NULL && sock == -1) || rpath == NULL) {
1079 fprintf(stderr, "socket, socket descriptor or path not specified\n");
1080 usage(argv[0]);
1081 return -1;
1084 if (sock_name && sock != -1) {
1085 fprintf(stderr, "both named socket and socket descriptor specified\n");
1086 usage(argv[0]);
1087 exit(EXIT_FAILURE);
1090 if (sock_name && (own_u == -1 || own_g == -1)) {
1091 fprintf(stderr, "owner uid:gid not specified, ");
1092 fprintf(stderr,
1093 "owner uid:gid specifies who can access the socket file\n");
1094 usage(argv[0]);
1095 exit(EXIT_FAILURE);
1098 if (lstat(rpath, &stbuf) < 0) {
1099 fprintf(stderr, "invalid path \"%s\" specified, %s\n",
1100 rpath, strerror(errno));
1101 exit(EXIT_FAILURE);
1104 if (!S_ISDIR(stbuf.st_mode)) {
1105 fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
1106 exit(EXIT_FAILURE);
1109 if (is_daemon) {
1110 if (daemon(0, 0) < 0) {
1111 fprintf(stderr, "daemon call failed\n");
1112 exit(EXIT_FAILURE);
1114 openlog(PROGNAME, LOG_PID, LOG_DAEMON);
1117 do_log(LOG_INFO, "Started\n");
1118 if (sock_name) {
1119 sock = proxy_socket(sock_name, own_u, own_g);
1120 if (sock < 0) {
1121 goto error;
1125 get_version = false;
1126 #ifdef FS_IOC_GETVERSION
1127 /* check whether underlying FS support IOC_GETVERSION */
1128 retval = statfs(rpath, &st_fs);
1129 if (!retval) {
1130 switch (st_fs.f_type) {
1131 case EXT2_SUPER_MAGIC:
1132 case BTRFS_SUPER_MAGIC:
1133 case REISERFS_SUPER_MAGIC:
1134 case XFS_SUPER_MAGIC:
1135 get_version = true;
1136 break;
1139 #endif
1141 if (chdir("/") < 0) {
1142 do_perror("chdir");
1143 goto error;
1145 if (chroot(rpath) < 0) {
1146 do_perror("chroot");
1147 goto error;
1149 umask(0);
1151 if (init_capabilities() < 0) {
1152 goto error;
1155 process_requests(sock);
1156 error:
1157 do_log(LOG_INFO, "Done\n");
1158 closelog();
1159 return 0;