]>
git.proxmox.com Git - mirror_qemu.git/blob - fsdev/virtfs-proxy-helper.c
2 * Helper for QEMU Proxy FS Driver
3 * Copyright IBM, Corp. 2011
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 "qemu/osdep.h"
13 #include <glib/gstdio.h>
14 #include <sys/resource.h>
17 #include <sys/fsuid.h>
19 #include <sys/ioctl.h>
21 #ifdef CONFIG_LINUX_MAGIC_H
22 #include <linux/magic.h>
25 #include "qemu/sockets.h"
26 #include "qemu/xattr.h"
27 #include "9p-iov-marshal.h"
28 #include "hw/9pfs/9p-proxy.h"
29 #include "hw/9pfs/9p-util.h"
30 #include "fsdev/9p-iov-marshal.h"
32 #define PROGNAME "virtfs-proxy-helper"
34 #ifndef XFS_SUPER_MAGIC
35 #define XFS_SUPER_MAGIC 0x58465342
37 #ifndef EXT2_SUPER_MAGIC
38 #define EXT2_SUPER_MAGIC 0xEF53
40 #ifndef REISERFS_SUPER_MAGIC
41 #define REISERFS_SUPER_MAGIC 0x52654973
43 #ifndef BTRFS_SUPER_MAGIC
44 #define BTRFS_SUPER_MAGIC 0x9123683E
47 static const struct option helper_opts
[] = {
48 {"fd", required_argument
, NULL
, 'f'},
49 {"path", required_argument
, NULL
, 'p'},
50 {"nodaemon", no_argument
, NULL
, 'n'},
51 {"socket", required_argument
, NULL
, 's'},
52 {"uid", required_argument
, NULL
, 'u'},
53 {"gid", required_argument
, NULL
, 'g'},
57 static bool is_daemon
;
58 static bool get_version
; /* IOC getversion IOCTL supported */
59 static char *prog_name
;
61 static void G_GNUC_PRINTF(2, 3) do_log(int loglevel
, const char *format
, ...)
67 vsyslog(LOG_CRIT
, format
, ap
);
69 vfprintf(stderr
, format
, ap
);
74 static void do_perror(const char *string
)
77 syslog(LOG_CRIT
, "%s:%s", string
, strerror(errno
));
79 fprintf(stderr
, "%s:%s\n", string
, strerror(errno
));
83 static int init_capabilities(void)
85 /* helper needs following capabilities only */
97 capng_clear(CAPNG_SELECT_BOTH
);
98 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++) {
99 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
101 do_perror("capng_update");
105 if (capng_apply(CAPNG_SELECT_BOTH
) < 0) {
106 do_perror("capng_apply");
110 /* Prepare effective set for setugid. */
111 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++) {
112 if (cap_list
[i
] == CAP_DAC_OVERRIDE
) {
116 if (capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
,
118 do_perror("capng_update");
125 static int socket_read(int sockfd
, void *buff
, ssize_t size
)
127 ssize_t retval
, total
= 0;
130 retval
= read(sockfd
, buff
, size
);
135 if (errno
== EINTR
) {
147 static int socket_write(int sockfd
, void *buff
, ssize_t size
)
149 ssize_t retval
, total
= 0;
152 retval
= write(sockfd
, buff
, size
);
154 if (errno
== EINTR
) {
166 static int read_request(int sockfd
, struct iovec
*iovec
, ProxyHeader
*header
)
171 * read the request header.
174 retval
= socket_read(sockfd
, iovec
->iov_base
, PROXY_HDR_SZ
);
178 iovec
->iov_len
= PROXY_HDR_SZ
;
179 retval
= proxy_unmarshal(iovec
, 0, "dd", &header
->type
, &header
->size
);
184 * We can't process message.size > PROXY_MAX_IO_SZ.
185 * Treat it as fatal error
187 if (header
->size
> PROXY_MAX_IO_SZ
) {
190 retval
= socket_read(sockfd
, iovec
->iov_base
+ PROXY_HDR_SZ
, header
->size
);
194 iovec
->iov_len
+= header
->size
;
198 static int send_fd(int sockfd
, int fd
)
203 struct cmsghdr
*cmsg
;
204 union MsgControl msg_control
;
206 iov
.iov_base
= &data
;
207 iov
.iov_len
= sizeof(data
);
209 memset(&msg
, 0, sizeof(msg
));
212 /* No ancillary data on error */
214 /* fd is really negative errno if the request failed */
217 data
= V9FS_FD_VALID
;
218 msg
.msg_control
= &msg_control
;
219 msg
.msg_controllen
= sizeof(msg_control
);
221 cmsg
= &msg_control
.cmsg
;
222 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
223 cmsg
->cmsg_level
= SOL_SOCKET
;
224 cmsg
->cmsg_type
= SCM_RIGHTS
;
225 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(fd
));
229 retval
= sendmsg(sockfd
, &msg
, 0);
230 } while (retval
< 0 && errno
== EINTR
);
240 static int send_status(int sockfd
, struct iovec
*iovec
, int status
)
243 int retval
, msg_size
;
246 header
.type
= T_ERROR
;
248 header
.type
= T_SUCCESS
;
250 header
.size
= sizeof(status
);
252 * marshal the return status. We don't check error.
253 * because we are sure we have enough space for the status
255 msg_size
= proxy_marshal(iovec
, 0, "ddd", header
.type
,
256 header
.size
, status
);
260 retval
= socket_write(sockfd
, iovec
->iov_base
, msg_size
);
268 * from man 7 capabilities, section
269 * Effect of User ID Changes on Capabilities:
270 * If the effective user ID is changed from nonzero to 0, then the permitted
271 * set is copied to the effective set. If the effective user ID is changed
272 * from 0 to nonzero, then all capabilities are are cleared from the effective
275 * The setfsuid/setfsgid man pages warn that changing the effective user ID may
276 * expose the program to unwanted signals, but this is not true anymore: for an
277 * unprivileged (without CAP_KILL) program to send a signal, the real or
278 * effective user ID of the sending process must equal the real or saved user
279 * ID of the target process. Even when dropping privileges, it is enough to
280 * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
281 * be exposed to signals. So just use setresuid/setresgid.
283 static int setugid(int uid
, int gid
, int *suid
, int *sgid
)
290 if (setresgid(-1, gid
, *sgid
) == -1) {
294 if (setresuid(-1, uid
, *suid
) == -1) {
299 if (uid
== 0 && gid
== 0) {
300 /* Linux has already copied the permitted set to the effective set. */
305 * All capabilities have been cleared from the effective set. However
306 * we still need DAC_OVERRIDE because we don't change supplementary
307 * group ids, and hence may be subject to DAC rules. init_capabilities
308 * left the set of capabilities that we want in libcap-ng's state.
310 if (capng_apply(CAPNG_SELECT_CAPS
) < 0) {
312 do_perror("capng_apply");
318 if (setresuid(-1, *suid
, *suid
) == -1) {
322 if (setresgid(-1, *sgid
, *sgid
) == -1) {
329 * This is used to reset the ugid back with the saved values
330 * There is nothing much we can do checking error values here.
332 static void resetugid(int suid
, int sgid
)
334 if (setresgid(-1, sgid
, sgid
) == -1) {
337 if (setresuid(-1, suid
, suid
) == -1) {
343 * Open regular file or directory. Attempts to open any special file are
346 * returns file descriptor or -1 on error
348 static int open_regular(const char *pathname
, int flags
, mode_t mode
)
352 fd
= open(pathname
, flags
, mode
);
357 if (close_if_special_file(fd
) < 0) {
365 * send response in two parts
367 * 2) Response or error status
368 * This function should be called with marshaled response
369 * send_response constructs header part and error part only.
370 * send response sends {ProxyHeader,Response} if the request was success
371 * otherwise sends {ProxyHeader,error status}
373 static int send_response(int sock
, struct iovec
*iovec
, int size
)
379 * If response size exceeds available iovec->iov_len,
382 if (size
> PROXY_MAX_IO_SZ
) {
388 * In case of error we would not have got the error encoded
389 * already so encode the error here.
391 header
.type
= T_ERROR
;
392 header
.size
= sizeof(size
);
393 proxy_marshal(iovec
, PROXY_HDR_SZ
, "d", size
);
395 header
.type
= T_SUCCESS
;
398 proxy_marshal(iovec
, 0, "dd", header
.type
, header
.size
);
399 retval
= socket_write(sock
, iovec
->iov_base
, header
.size
+ PROXY_HDR_SZ
);
407 * gets generation number
408 * returns -errno on failure and sizeof(generation number) on success
410 static int do_getversion(struct iovec
*iovec
, struct iovec
*out_iovec
)
413 int retval
= -ENOTTY
;
414 #ifdef FS_IOC_GETVERSION
420 /* no need to issue ioctl */
423 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "q", version
);
426 #ifdef FS_IOC_GETVERSION
427 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "s", &path
);
432 fd
= open(path
.data
, O_RDONLY
);
437 if (ioctl(fd
, FS_IOC_GETVERSION
, &version
) < 0) {
440 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "q", version
);
444 v9fs_string_free(&path
);
449 static int do_getxattr(int type
, struct iovec
*iovec
, struct iovec
*out_iovec
)
451 int size
= 0, offset
, retval
;
452 V9fsString path
, name
, xattr
;
454 v9fs_string_init(&xattr
);
455 v9fs_string_init(&path
);
456 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "ds", &size
, &path
);
460 offset
= PROXY_HDR_SZ
+ retval
;
463 xattr
.data
= g_malloc(size
);
468 v9fs_string_init(&name
);
469 retval
= proxy_unmarshal(iovec
, offset
, "s", &name
);
471 retval
= lgetxattr(path
.data
, name
.data
, xattr
.data
, size
);
478 v9fs_string_free(&name
);
481 retval
= llistxattr(path
.data
, xattr
.data
, size
);
494 proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "d", retval
);
495 retval
= sizeof(retval
);
497 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "s", &xattr
);
500 v9fs_string_free(&xattr
);
501 v9fs_string_free(&path
);
505 static void stat_to_prstat(ProxyStat
*pr_stat
, struct stat
*stat
)
507 memset(pr_stat
, 0, sizeof(*pr_stat
));
508 pr_stat
->st_dev
= stat
->st_dev
;
509 pr_stat
->st_ino
= stat
->st_ino
;
510 pr_stat
->st_nlink
= stat
->st_nlink
;
511 pr_stat
->st_mode
= stat
->st_mode
;
512 pr_stat
->st_uid
= stat
->st_uid
;
513 pr_stat
->st_gid
= stat
->st_gid
;
514 pr_stat
->st_rdev
= stat
->st_rdev
;
515 pr_stat
->st_size
= stat
->st_size
;
516 pr_stat
->st_blksize
= stat
->st_blksize
;
517 pr_stat
->st_blocks
= stat
->st_blocks
;
518 pr_stat
->st_atim_sec
= stat
->st_atim
.tv_sec
;
519 pr_stat
->st_atim_nsec
= stat
->st_atim
.tv_nsec
;
520 pr_stat
->st_mtim_sec
= stat
->st_mtim
.tv_sec
;
521 pr_stat
->st_mtim_nsec
= stat
->st_mtim
.tv_nsec
;
522 pr_stat
->st_ctim_sec
= stat
->st_ctim
.tv_sec
;
523 pr_stat
->st_ctim_nsec
= stat
->st_ctim
.tv_nsec
;
526 static void statfs_to_prstatfs(ProxyStatFS
*pr_stfs
, struct statfs
*stfs
)
528 memset(pr_stfs
, 0, sizeof(*pr_stfs
));
529 pr_stfs
->f_type
= stfs
->f_type
;
530 pr_stfs
->f_bsize
= stfs
->f_bsize
;
531 pr_stfs
->f_blocks
= stfs
->f_blocks
;
532 pr_stfs
->f_bfree
= stfs
->f_bfree
;
533 pr_stfs
->f_bavail
= stfs
->f_bavail
;
534 pr_stfs
->f_files
= stfs
->f_files
;
535 pr_stfs
->f_ffree
= stfs
->f_ffree
;
536 pr_stfs
->f_fsid
[0] = stfs
->f_fsid
.__val
[0];
537 pr_stfs
->f_fsid
[1] = stfs
->f_fsid
.__val
[1];
538 pr_stfs
->f_namelen
= stfs
->f_namelen
;
539 pr_stfs
->f_frsize
= stfs
->f_frsize
;
543 * Gets stat/statfs information and packs in out_iovec structure
544 * on success returns number of bytes packed in out_iovec structure
545 * otherwise returns -errno
547 static int do_stat(int type
, struct iovec
*iovec
, struct iovec
*out_iovec
)
554 struct statfs stfs_buf
;
556 v9fs_string_init(&path
);
557 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "s", &path
);
564 retval
= lstat(path
.data
, &st_buf
);
568 stat_to_prstat(&pr_stat
, &st_buf
);
569 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
,
570 "qqqdddqqqqqqqqqq", pr_stat
.st_dev
,
571 pr_stat
.st_ino
, pr_stat
.st_nlink
,
572 pr_stat
.st_mode
, pr_stat
.st_uid
,
573 pr_stat
.st_gid
, pr_stat
.st_rdev
,
574 pr_stat
.st_size
, pr_stat
.st_blksize
,
576 pr_stat
.st_atim_sec
, pr_stat
.st_atim_nsec
,
577 pr_stat
.st_mtim_sec
, pr_stat
.st_mtim_nsec
,
578 pr_stat
.st_ctim_sec
, pr_stat
.st_ctim_nsec
);
582 retval
= statfs(path
.data
, &stfs_buf
);
586 statfs_to_prstatfs(&pr_stfs
, &stfs_buf
);
587 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
,
588 "qqqqqqqqqqq", pr_stfs
.f_type
,
589 pr_stfs
.f_bsize
, pr_stfs
.f_blocks
,
590 pr_stfs
.f_bfree
, pr_stfs
.f_bavail
,
591 pr_stfs
.f_files
, pr_stfs
.f_ffree
,
592 pr_stfs
.f_fsid
[0], pr_stfs
.f_fsid
[1],
593 pr_stfs
.f_namelen
, pr_stfs
.f_frsize
);
597 v9fs_string_free(&path
);
601 static int do_readlink(struct iovec
*iovec
, struct iovec
*out_iovec
)
605 V9fsString target
, path
;
607 v9fs_string_init(&path
);
608 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sd", &path
, &size
);
610 v9fs_string_free(&path
);
613 buffer
= g_malloc(size
);
614 v9fs_string_init(&target
);
615 retval
= readlink(path
.data
, buffer
, size
- 1);
617 buffer
[retval
] = '\0';
618 v9fs_string_sprintf(&target
, "%s", buffer
);
619 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "s", &target
);
624 v9fs_string_free(&target
);
625 v9fs_string_free(&path
);
630 * create other filesystem objects and send 0 on success
631 * return -errno on error
633 static int do_create_others(int type
, struct iovec
*iovec
)
637 int offset
= PROXY_HDR_SZ
;
638 V9fsString oldpath
, path
;
639 int mode
, uid
, gid
, cur_uid
, cur_gid
;
641 v9fs_string_init(&path
);
642 v9fs_string_init(&oldpath
);
644 retval
= proxy_unmarshal(iovec
, offset
, "dd", &uid
, &gid
);
649 retval
= setugid(uid
, gid
, &cur_uid
, &cur_gid
);
651 goto unmarshal_err_out
;
655 retval
= proxy_unmarshal(iovec
, offset
, "sdq", &path
, &mode
, &rdev
);
659 retval
= mknod(path
.data
, mode
, rdev
);
662 retval
= proxy_unmarshal(iovec
, offset
, "sd", &path
, &mode
);
666 retval
= g_mkdir(path
.data
, mode
);
669 retval
= proxy_unmarshal(iovec
, offset
, "ss", &oldpath
, &path
);
673 retval
= symlink(oldpath
.data
, path
.data
);
681 resetugid(cur_uid
, cur_gid
);
683 v9fs_string_free(&path
);
684 v9fs_string_free(&oldpath
);
689 * create a file and send fd on success
690 * return -errno on error
692 static int do_create(struct iovec
*iovec
)
696 int flags
, mode
, uid
, gid
, cur_uid
, cur_gid
;
698 v9fs_string_init(&path
);
699 ret
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sdddd",
700 &path
, &flags
, &mode
, &uid
, &gid
);
702 goto unmarshal_err_out
;
704 ret
= setugid(uid
, gid
, &cur_uid
, &cur_gid
);
706 goto unmarshal_err_out
;
708 ret
= open_regular(path
.data
, flags
, mode
);
713 resetugid(cur_uid
, cur_gid
);
715 v9fs_string_free(&path
);
720 * open a file and send fd on success
721 * return -errno on error
723 static int do_open(struct iovec
*iovec
)
728 v9fs_string_init(&path
);
729 ret
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sd", &path
, &flags
);
733 ret
= open_regular(path
.data
, flags
, 0);
738 v9fs_string_free(&path
);
742 /* create unix domain socket and return the descriptor */
743 static int proxy_socket(const char *path
, uid_t uid
, gid_t gid
)
746 struct sockaddr_un proxy
, qemu
;
749 /* requested socket already exists, refuse to start */
750 if (!access(path
, F_OK
)) {
751 do_log(LOG_CRIT
, "socket already exists\n");
755 if (strlen(path
) >= sizeof(proxy
.sun_path
)) {
756 do_log(LOG_CRIT
, "UNIX domain socket path exceeds %zu characters\n",
757 sizeof(proxy
.sun_path
));
761 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
767 /* mask other part of mode bits */
770 proxy
.sun_family
= AF_UNIX
;
771 strcpy(proxy
.sun_path
, path
);
772 if (bind(sock
, (struct sockaddr
*)&proxy
,
773 sizeof(struct sockaddr_un
)) < 0) {
777 if (chown(proxy
.sun_path
, uid
, gid
) < 0) {
781 if (listen(sock
, 1) < 0) {
787 client
= accept(sock
, (struct sockaddr
*)&qemu
, &size
);
800 static void usage(void)
802 fprintf(stderr
, "usage: %s\n"
803 " -p|--path <path> 9p path to export\n"
804 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
805 " {-s|--socket <socketname> socket file used for communication\n"
806 " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
807 " access to this socket\n"
808 " \tNote: -s & -f can not be used together\n"
809 " [-n|--nodaemon] Run as a normal program\n",
813 static int process_reply(int sock
, int type
,
814 struct iovec
*out_iovec
, int retval
)
819 if (send_fd(sock
, retval
) < 0) {
835 if (send_status(sock
, out_iovec
, retval
) < 0) {
845 if (send_response(sock
, out_iovec
, retval
) < 0) {
856 static int process_requests(int sock
)
864 V9fsString name
, value
;
865 struct timespec spec
[2];
866 V9fsString oldpath
, path
;
867 struct iovec in_iovec
, out_iovec
;
869 in_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
870 in_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
871 out_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
872 out_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
876 * initialize the header type, so that we send
877 * response to proper request type.
880 retval
= read_request(sock
, &in_iovec
, &header
);
885 switch (header
.type
) {
887 retval
= do_open(&in_iovec
);
890 retval
= do_create(&in_iovec
);
895 retval
= do_create_others(header
.type
, &in_iovec
);
898 v9fs_string_init(&path
);
899 v9fs_string_init(&oldpath
);
900 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
901 "ss", &oldpath
, &path
);
903 retval
= link(oldpath
.data
, path
.data
);
908 v9fs_string_free(&oldpath
);
909 v9fs_string_free(&path
);
913 retval
= do_stat(header
.type
, &in_iovec
, &out_iovec
);
916 retval
= do_readlink(&in_iovec
, &out_iovec
);
919 v9fs_string_init(&path
);
920 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
923 retval
= chmod(path
.data
, mode
);
928 v9fs_string_free(&path
);
931 v9fs_string_init(&path
);
932 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sdd", &path
,
935 retval
= lchown(path
.data
, uid
, gid
);
940 v9fs_string_free(&path
);
943 v9fs_string_init(&path
);
944 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sq",
947 retval
= truncate(path
.data
, offset
);
952 v9fs_string_free(&path
);
955 v9fs_string_init(&path
);
956 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sqqqq", &path
,
957 &spec
[0].tv_sec
, &spec
[0].tv_nsec
,
958 &spec
[1].tv_sec
, &spec
[1].tv_nsec
);
960 retval
= utimensat(AT_FDCWD
, path
.data
, spec
,
961 AT_SYMLINK_NOFOLLOW
);
966 v9fs_string_free(&path
);
969 v9fs_string_init(&path
);
970 v9fs_string_init(&oldpath
);
971 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
972 "ss", &oldpath
, &path
);
974 retval
= rename(oldpath
.data
, path
.data
);
979 v9fs_string_free(&oldpath
);
980 v9fs_string_free(&path
);
983 v9fs_string_init(&path
);
984 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "s", &path
);
986 retval
= remove(path
.data
);
991 v9fs_string_free(&path
);
995 retval
= do_getxattr(header
.type
, &in_iovec
, &out_iovec
);
998 v9fs_string_init(&path
);
999 v9fs_string_init(&name
);
1000 v9fs_string_init(&value
);
1001 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sssdd", &path
,
1002 &name
, &value
, &size
, &flags
);
1004 retval
= lsetxattr(path
.data
,
1005 name
.data
, value
.data
, size
, flags
);
1010 v9fs_string_free(&path
);
1011 v9fs_string_free(&name
);
1012 v9fs_string_free(&value
);
1014 case T_LREMOVEXATTR
:
1015 v9fs_string_init(&path
);
1016 v9fs_string_init(&name
);
1017 retval
= proxy_unmarshal(&in_iovec
,
1018 PROXY_HDR_SZ
, "ss", &path
, &name
);
1020 retval
= lremovexattr(path
.data
, name
.data
);
1025 v9fs_string_free(&path
);
1026 v9fs_string_free(&name
);
1029 retval
= do_getversion(&in_iovec
, &out_iovec
);
1036 if (process_reply(sock
, header
.type
, &out_iovec
, retval
) < 0) {
1041 g_free(in_iovec
.iov_base
);
1042 g_free(out_iovec
.iov_base
);
1046 int main(int argc
, char **argv
)
1052 char *sock_name
= NULL
;
1054 int c
, option_index
;
1055 #ifdef FS_IOC_GETVERSION
1057 struct statfs st_fs
;
1060 prog_name
= g_path_get_basename(argv
[0]);
1067 c
= getopt_long(argc
, argv
, "p:nh?f:s:u:g:", helper_opts
,
1074 rpath
= g_strdup(optarg
);
1080 sock
= atoi(optarg
);
1083 sock_name
= g_strdup(optarg
);
1086 own_u
= atoi(optarg
);
1089 own_g
= atoi(optarg
);
1099 /* Parameter validation */
1100 if ((sock_name
== NULL
&& sock
== -1) || rpath
== NULL
) {
1101 fprintf(stderr
, "socket, socket descriptor or path not specified\n");
1106 if (sock_name
&& sock
!= -1) {
1107 fprintf(stderr
, "both named socket and socket descriptor specified\n");
1112 if (sock_name
&& (own_u
== -1 || own_g
== -1)) {
1113 fprintf(stderr
, "owner uid:gid not specified, ");
1115 "owner uid:gid specifies who can access the socket file\n");
1120 if (lstat(rpath
, &stbuf
) < 0) {
1121 fprintf(stderr
, "invalid path \"%s\" specified, %s\n",
1122 rpath
, strerror(errno
));
1126 if (!S_ISDIR(stbuf
.st_mode
)) {
1127 fprintf(stderr
, "specified path \"%s\" is not directory\n", rpath
);
1132 if (daemon(0, 0) < 0) {
1133 fprintf(stderr
, "daemon call failed\n");
1136 openlog(PROGNAME
, LOG_PID
, LOG_DAEMON
);
1139 do_log(LOG_INFO
, "Started\n");
1141 sock
= proxy_socket(sock_name
, own_u
, own_g
);
1147 if (chroot(rpath
) < 0) {
1148 do_perror("chroot");
1151 if (chdir("/") < 0) {
1156 get_version
= false;
1157 #ifdef FS_IOC_GETVERSION
1158 /* check whether underlying FS support IOC_GETVERSION */
1159 retval
= statfs("/", &st_fs
);
1161 switch (st_fs
.f_type
) {
1162 case EXT2_SUPER_MAGIC
:
1163 case BTRFS_SUPER_MAGIC
:
1164 case REISERFS_SUPER_MAGIC
:
1165 case XFS_SUPER_MAGIC
:
1173 if (init_capabilities() < 0) {
1177 process_requests(sock
);
1181 do_log(LOG_INFO
, "Done\n");