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 <sys/resource.h>
16 #include <sys/fsuid.h>
18 #include <sys/ioctl.h>
20 #ifdef CONFIG_LINUX_MAGIC_H
21 #include <linux/magic.h>
24 #include "qemu-common.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 "fsdev/9p-iov-marshal.h"
31 #define PROGNAME "virtfs-proxy-helper"
33 #ifndef XFS_SUPER_MAGIC
34 #define XFS_SUPER_MAGIC 0x58465342
36 #ifndef EXT2_SUPER_MAGIC
37 #define EXT2_SUPER_MAGIC 0xEF53
39 #ifndef REISERFS_SUPER_MAGIC
40 #define REISERFS_SUPER_MAGIC 0x52654973
42 #ifndef BTRFS_SUPER_MAGIC
43 #define BTRFS_SUPER_MAGIC 0x9123683E
46 static struct option helper_opts
[] = {
47 {"fd", required_argument
, NULL
, 'f'},
48 {"path", required_argument
, NULL
, 'p'},
49 {"nodaemon", no_argument
, NULL
, 'n'},
50 {"socket", required_argument
, NULL
, 's'},
51 {"uid", required_argument
, NULL
, 'u'},
52 {"gid", required_argument
, NULL
, 'g'},
56 static bool is_daemon
;
57 static bool get_version
; /* IOC getversion IOCTL supported */
58 static char *prog_name
;
60 static void GCC_FMT_ATTR(2, 3) do_log(int loglevel
, const char *format
, ...)
66 vsyslog(LOG_CRIT
, format
, ap
);
68 vfprintf(stderr
, format
, ap
);
73 static void do_perror(const char *string
)
76 syslog(LOG_CRIT
, "%s:%s", string
, strerror(errno
));
78 fprintf(stderr
, "%s:%s\n", string
, strerror(errno
));
82 static int init_capabilities(void)
84 /* helper needs following capabilities only */
96 capng_clear(CAPNG_SELECT_BOTH
);
97 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++) {
98 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
100 do_perror("capng_update");
104 if (capng_apply(CAPNG_SELECT_BOTH
) < 0) {
105 do_perror("capng_apply");
109 /* Prepare effective set for setugid. */
110 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++) {
111 if (cap_list
[i
] == CAP_DAC_OVERRIDE
) {
115 if (capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
,
117 do_perror("capng_update");
124 static int socket_read(int sockfd
, void *buff
, ssize_t size
)
126 ssize_t retval
, total
= 0;
129 retval
= read(sockfd
, buff
, size
);
134 if (errno
== EINTR
) {
146 static int socket_write(int sockfd
, void *buff
, ssize_t size
)
148 ssize_t retval
, total
= 0;
151 retval
= write(sockfd
, buff
, size
);
153 if (errno
== EINTR
) {
165 static int read_request(int sockfd
, struct iovec
*iovec
, ProxyHeader
*header
)
170 * read the request header.
173 retval
= socket_read(sockfd
, iovec
->iov_base
, PROXY_HDR_SZ
);
177 iovec
->iov_len
= PROXY_HDR_SZ
;
178 retval
= proxy_unmarshal(iovec
, 0, "dd", &header
->type
, &header
->size
);
183 * We can't process message.size > PROXY_MAX_IO_SZ.
184 * Treat it as fatal error
186 if (header
->size
> PROXY_MAX_IO_SZ
) {
189 retval
= socket_read(sockfd
, iovec
->iov_base
+ PROXY_HDR_SZ
, header
->size
);
193 iovec
->iov_len
+= header
->size
;
197 static int send_fd(int sockfd
, int fd
)
202 struct cmsghdr
*cmsg
;
203 union MsgControl msg_control
;
205 iov
.iov_base
= &data
;
206 iov
.iov_len
= sizeof(data
);
208 memset(&msg
, 0, sizeof(msg
));
211 /* No ancillary data on error */
213 /* fd is really negative errno if the request failed */
216 data
= V9FS_FD_VALID
;
217 msg
.msg_control
= &msg_control
;
218 msg
.msg_controllen
= sizeof(msg_control
);
220 cmsg
= &msg_control
.cmsg
;
221 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
222 cmsg
->cmsg_level
= SOL_SOCKET
;
223 cmsg
->cmsg_type
= SCM_RIGHTS
;
224 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(fd
));
228 retval
= sendmsg(sockfd
, &msg
, 0);
229 } while (retval
< 0 && errno
== EINTR
);
239 static int send_status(int sockfd
, struct iovec
*iovec
, int status
)
242 int retval
, msg_size
;
245 header
.type
= T_ERROR
;
247 header
.type
= T_SUCCESS
;
249 header
.size
= sizeof(status
);
251 * marshal the return status. We don't check error.
252 * because we are sure we have enough space for the status
254 msg_size
= proxy_marshal(iovec
, 0, "ddd", header
.type
,
255 header
.size
, status
);
259 retval
= socket_write(sockfd
, iovec
->iov_base
, msg_size
);
267 * from man 7 capabilities, section
268 * Effect of User ID Changes on Capabilities:
269 * If the effective user ID is changed from nonzero to 0, then the permitted
270 * set is copied to the effective set. If the effective user ID is changed
271 * from 0 to nonzero, then all capabilities are are cleared from the effective
274 * The setfsuid/setfsgid man pages warn that changing the effective user ID may
275 * expose the program to unwanted signals, but this is not true anymore: for an
276 * unprivileged (without CAP_KILL) program to send a signal, the real or
277 * effective user ID of the sending process must equal the real or saved user
278 * ID of the target process. Even when dropping privileges, it is enough to
279 * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
280 * be exposed to signals. So just use setresuid/setresgid.
282 static int setugid(int uid
, int gid
, int *suid
, int *sgid
)
289 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) {
330 * This is used to reset the ugid back with the saved values
331 * There is nothing much we can do checking error values here.
333 static void resetugid(int suid
, int sgid
)
335 if (setresgid(-1, sgid
, sgid
) == -1) {
338 if (setresuid(-1, suid
, suid
) == -1) {
344 * send response in two parts
346 * 2) Response or error status
347 * This function should be called with marshaled response
348 * send_response constructs header part and error part only.
349 * send response sends {ProxyHeader,Response} if the request was success
350 * otherwise sends {ProxyHeader,error status}
352 static int send_response(int sock
, struct iovec
*iovec
, int size
)
358 * If response size exceeds available iovec->iov_len,
361 if (size
> PROXY_MAX_IO_SZ
) {
367 * In case of error we would not have got the error encoded
368 * already so encode the error here.
370 header
.type
= T_ERROR
;
371 header
.size
= sizeof(size
);
372 proxy_marshal(iovec
, PROXY_HDR_SZ
, "d", size
);
374 header
.type
= T_SUCCESS
;
377 proxy_marshal(iovec
, 0, "dd", header
.type
, header
.size
);
378 retval
= socket_write(sock
, iovec
->iov_base
, header
.size
+ PROXY_HDR_SZ
);
386 * gets generation number
387 * returns -errno on failure and sizeof(generation number) on success
389 static int do_getversion(struct iovec
*iovec
, struct iovec
*out_iovec
)
392 int retval
= -ENOTTY
;
393 #ifdef FS_IOC_GETVERSION
399 /* no need to issue ioctl */
402 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "q", version
);
405 #ifdef FS_IOC_GETVERSION
406 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "s", &path
);
411 fd
= open(path
.data
, O_RDONLY
);
416 if (ioctl(fd
, FS_IOC_GETVERSION
, &version
) < 0) {
419 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "q", version
);
423 v9fs_string_free(&path
);
428 static int do_getxattr(int type
, struct iovec
*iovec
, struct iovec
*out_iovec
)
430 int size
= 0, offset
, retval
;
431 V9fsString path
, name
, xattr
;
433 v9fs_string_init(&xattr
);
434 v9fs_string_init(&path
);
435 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "ds", &size
, &path
);
439 offset
= PROXY_HDR_SZ
+ retval
;
442 xattr
.data
= g_malloc(size
);
447 v9fs_string_init(&name
);
448 retval
= proxy_unmarshal(iovec
, offset
, "s", &name
);
450 retval
= lgetxattr(path
.data
, name
.data
, xattr
.data
, size
);
457 v9fs_string_free(&name
);
460 retval
= llistxattr(path
.data
, xattr
.data
, size
);
473 proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "d", retval
);
474 retval
= sizeof(retval
);
476 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "s", &xattr
);
479 v9fs_string_free(&xattr
);
480 v9fs_string_free(&path
);
484 static void stat_to_prstat(ProxyStat
*pr_stat
, struct stat
*stat
)
486 memset(pr_stat
, 0, sizeof(*pr_stat
));
487 pr_stat
->st_dev
= stat
->st_dev
;
488 pr_stat
->st_ino
= stat
->st_ino
;
489 pr_stat
->st_nlink
= stat
->st_nlink
;
490 pr_stat
->st_mode
= stat
->st_mode
;
491 pr_stat
->st_uid
= stat
->st_uid
;
492 pr_stat
->st_gid
= stat
->st_gid
;
493 pr_stat
->st_rdev
= stat
->st_rdev
;
494 pr_stat
->st_size
= stat
->st_size
;
495 pr_stat
->st_blksize
= stat
->st_blksize
;
496 pr_stat
->st_blocks
= stat
->st_blocks
;
497 pr_stat
->st_atim_sec
= stat
->st_atim
.tv_sec
;
498 pr_stat
->st_atim_nsec
= stat
->st_atim
.tv_nsec
;
499 pr_stat
->st_mtim_sec
= stat
->st_mtim
.tv_sec
;
500 pr_stat
->st_mtim_nsec
= stat
->st_mtim
.tv_nsec
;
501 pr_stat
->st_ctim_sec
= stat
->st_ctim
.tv_sec
;
502 pr_stat
->st_ctim_nsec
= stat
->st_ctim
.tv_nsec
;
505 static void statfs_to_prstatfs(ProxyStatFS
*pr_stfs
, struct statfs
*stfs
)
507 memset(pr_stfs
, 0, sizeof(*pr_stfs
));
508 pr_stfs
->f_type
= stfs
->f_type
;
509 pr_stfs
->f_bsize
= stfs
->f_bsize
;
510 pr_stfs
->f_blocks
= stfs
->f_blocks
;
511 pr_stfs
->f_bfree
= stfs
->f_bfree
;
512 pr_stfs
->f_bavail
= stfs
->f_bavail
;
513 pr_stfs
->f_files
= stfs
->f_files
;
514 pr_stfs
->f_ffree
= stfs
->f_ffree
;
515 pr_stfs
->f_fsid
[0] = stfs
->f_fsid
.__val
[0];
516 pr_stfs
->f_fsid
[1] = stfs
->f_fsid
.__val
[1];
517 pr_stfs
->f_namelen
= stfs
->f_namelen
;
518 pr_stfs
->f_frsize
= stfs
->f_frsize
;
522 * Gets stat/statfs information and packs in out_iovec structure
523 * on success returns number of bytes packed in out_iovec struture
524 * otherwise returns -errno
526 static int do_stat(int type
, struct iovec
*iovec
, struct iovec
*out_iovec
)
533 struct statfs stfs_buf
;
535 v9fs_string_init(&path
);
536 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "s", &path
);
543 retval
= lstat(path
.data
, &st_buf
);
547 stat_to_prstat(&pr_stat
, &st_buf
);
548 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
,
549 "qqqdddqqqqqqqqqq", pr_stat
.st_dev
,
550 pr_stat
.st_ino
, pr_stat
.st_nlink
,
551 pr_stat
.st_mode
, pr_stat
.st_uid
,
552 pr_stat
.st_gid
, pr_stat
.st_rdev
,
553 pr_stat
.st_size
, pr_stat
.st_blksize
,
555 pr_stat
.st_atim_sec
, pr_stat
.st_atim_nsec
,
556 pr_stat
.st_mtim_sec
, pr_stat
.st_mtim_nsec
,
557 pr_stat
.st_ctim_sec
, pr_stat
.st_ctim_nsec
);
561 retval
= statfs(path
.data
, &stfs_buf
);
565 statfs_to_prstatfs(&pr_stfs
, &stfs_buf
);
566 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
,
567 "qqqqqqqqqqq", pr_stfs
.f_type
,
568 pr_stfs
.f_bsize
, pr_stfs
.f_blocks
,
569 pr_stfs
.f_bfree
, pr_stfs
.f_bavail
,
570 pr_stfs
.f_files
, pr_stfs
.f_ffree
,
571 pr_stfs
.f_fsid
[0], pr_stfs
.f_fsid
[1],
572 pr_stfs
.f_namelen
, pr_stfs
.f_frsize
);
576 v9fs_string_free(&path
);
580 static int do_readlink(struct iovec
*iovec
, struct iovec
*out_iovec
)
584 V9fsString target
, path
;
586 v9fs_string_init(&path
);
587 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sd", &path
, &size
);
589 v9fs_string_free(&path
);
592 buffer
= g_malloc(size
);
593 v9fs_string_init(&target
);
594 retval
= readlink(path
.data
, buffer
, size
- 1);
596 buffer
[retval
] = '\0';
597 v9fs_string_sprintf(&target
, "%s", buffer
);
598 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "s", &target
);
603 v9fs_string_free(&target
);
604 v9fs_string_free(&path
);
609 * create other filesystem objects and send 0 on success
610 * return -errno on error
612 static int do_create_others(int type
, struct iovec
*iovec
)
616 int offset
= PROXY_HDR_SZ
;
617 V9fsString oldpath
, path
;
618 int mode
, uid
, gid
, cur_uid
, cur_gid
;
620 v9fs_string_init(&path
);
621 v9fs_string_init(&oldpath
);
623 retval
= proxy_unmarshal(iovec
, offset
, "dd", &uid
, &gid
);
628 retval
= setugid(uid
, gid
, &cur_uid
, &cur_gid
);
630 goto unmarshal_err_out
;
634 retval
= proxy_unmarshal(iovec
, offset
, "sdq", &path
, &mode
, &rdev
);
638 retval
= mknod(path
.data
, mode
, rdev
);
641 retval
= proxy_unmarshal(iovec
, offset
, "sd", &path
, &mode
);
645 retval
= mkdir(path
.data
, mode
);
648 retval
= proxy_unmarshal(iovec
, offset
, "ss", &oldpath
, &path
);
652 retval
= symlink(oldpath
.data
, path
.data
);
660 resetugid(cur_uid
, cur_gid
);
662 v9fs_string_free(&path
);
663 v9fs_string_free(&oldpath
);
668 * create a file and send fd on success
669 * return -errno on error
671 static int do_create(struct iovec
*iovec
)
675 int flags
, mode
, uid
, gid
, cur_uid
, cur_gid
;
677 v9fs_string_init(&path
);
678 ret
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sdddd",
679 &path
, &flags
, &mode
, &uid
, &gid
);
681 goto unmarshal_err_out
;
683 ret
= setugid(uid
, gid
, &cur_uid
, &cur_gid
);
685 goto unmarshal_err_out
;
687 ret
= open(path
.data
, flags
, mode
);
692 resetugid(cur_uid
, cur_gid
);
694 v9fs_string_free(&path
);
699 * open a file and send fd on success
700 * return -errno on error
702 static int do_open(struct iovec
*iovec
)
707 v9fs_string_init(&path
);
708 ret
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sd", &path
, &flags
);
712 ret
= open(path
.data
, flags
);
717 v9fs_string_free(&path
);
721 /* create unix domain socket and return the descriptor */
722 static int proxy_socket(const char *path
, uid_t uid
, gid_t gid
)
725 struct sockaddr_un proxy
, qemu
;
728 /* requested socket already exists, refuse to start */
729 if (!access(path
, F_OK
)) {
730 do_log(LOG_CRIT
, "socket already exists\n");
734 if (strlen(path
) >= sizeof(proxy
.sun_path
)) {
735 do_log(LOG_CRIT
, "UNIX domain socket path exceeds %zu characters\n",
736 sizeof(proxy
.sun_path
));
740 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
746 /* mask other part of mode bits */
749 proxy
.sun_family
= AF_UNIX
;
750 strcpy(proxy
.sun_path
, path
);
751 if (bind(sock
, (struct sockaddr
*)&proxy
,
752 sizeof(struct sockaddr_un
)) < 0) {
756 if (chown(proxy
.sun_path
, uid
, gid
) < 0) {
760 if (listen(sock
, 1) < 0) {
766 client
= accept(sock
, (struct sockaddr
*)&qemu
, &size
);
779 static void usage(void)
781 fprintf(stderr
, "usage: %s\n"
782 " -p|--path <path> 9p path to export\n"
783 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
784 " {-s|--socket <socketname> socket file used for communication\n"
785 " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
786 " access to this socket\n"
787 " \tNote: -s & -f can not be used together\n"
788 " [-n|--nodaemon] Run as a normal program\n",
792 static int process_reply(int sock
, int type
,
793 struct iovec
*out_iovec
, int retval
)
798 if (send_fd(sock
, retval
) < 0) {
814 if (send_status(sock
, out_iovec
, retval
) < 0) {
824 if (send_response(sock
, out_iovec
, retval
) < 0) {
835 static int process_requests(int sock
)
843 V9fsString name
, value
;
844 struct timespec spec
[2];
845 V9fsString oldpath
, path
;
846 struct iovec in_iovec
, out_iovec
;
848 in_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
849 in_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
850 out_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
851 out_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
855 * initialize the header type, so that we send
856 * response to proper request type.
859 retval
= read_request(sock
, &in_iovec
, &header
);
864 switch (header
.type
) {
866 retval
= do_open(&in_iovec
);
869 retval
= do_create(&in_iovec
);
874 retval
= do_create_others(header
.type
, &in_iovec
);
877 v9fs_string_init(&path
);
878 v9fs_string_init(&oldpath
);
879 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
880 "ss", &oldpath
, &path
);
882 retval
= link(oldpath
.data
, path
.data
);
887 v9fs_string_free(&oldpath
);
888 v9fs_string_free(&path
);
892 retval
= do_stat(header
.type
, &in_iovec
, &out_iovec
);
895 retval
= do_readlink(&in_iovec
, &out_iovec
);
898 v9fs_string_init(&path
);
899 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
902 retval
= chmod(path
.data
, mode
);
907 v9fs_string_free(&path
);
910 v9fs_string_init(&path
);
911 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sdd", &path
,
914 retval
= lchown(path
.data
, uid
, gid
);
919 v9fs_string_free(&path
);
922 v9fs_string_init(&path
);
923 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sq",
926 retval
= truncate(path
.data
, offset
);
931 v9fs_string_free(&path
);
934 v9fs_string_init(&path
);
935 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sqqqq", &path
,
936 &spec
[0].tv_sec
, &spec
[0].tv_nsec
,
937 &spec
[1].tv_sec
, &spec
[1].tv_nsec
);
939 retval
= utimensat(AT_FDCWD
, path
.data
, spec
,
940 AT_SYMLINK_NOFOLLOW
);
945 v9fs_string_free(&path
);
948 v9fs_string_init(&path
);
949 v9fs_string_init(&oldpath
);
950 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
951 "ss", &oldpath
, &path
);
953 retval
= rename(oldpath
.data
, path
.data
);
958 v9fs_string_free(&oldpath
);
959 v9fs_string_free(&path
);
962 v9fs_string_init(&path
);
963 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "s", &path
);
965 retval
= remove(path
.data
);
970 v9fs_string_free(&path
);
974 retval
= do_getxattr(header
.type
, &in_iovec
, &out_iovec
);
977 v9fs_string_init(&path
);
978 v9fs_string_init(&name
);
979 v9fs_string_init(&value
);
980 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sssdd", &path
,
981 &name
, &value
, &size
, &flags
);
983 retval
= lsetxattr(path
.data
,
984 name
.data
, value
.data
, size
, flags
);
989 v9fs_string_free(&path
);
990 v9fs_string_free(&name
);
991 v9fs_string_free(&value
);
994 v9fs_string_init(&path
);
995 v9fs_string_init(&name
);
996 retval
= proxy_unmarshal(&in_iovec
,
997 PROXY_HDR_SZ
, "ss", &path
, &name
);
999 retval
= lremovexattr(path
.data
, name
.data
);
1004 v9fs_string_free(&path
);
1005 v9fs_string_free(&name
);
1008 retval
= do_getversion(&in_iovec
, &out_iovec
);
1015 if (process_reply(sock
, header
.type
, &out_iovec
, retval
) < 0) {
1020 g_free(in_iovec
.iov_base
);
1021 g_free(out_iovec
.iov_base
);
1025 int main(int argc
, char **argv
)
1031 char *sock_name
= NULL
;
1033 int c
, option_index
;
1034 #ifdef FS_IOC_GETVERSION
1036 struct statfs st_fs
;
1039 prog_name
= g_path_get_basename(argv
[0]);
1046 c
= getopt_long(argc
, argv
, "p:nh?f:s:u:g:", helper_opts
,
1053 rpath
= g_strdup(optarg
);
1059 sock
= atoi(optarg
);
1062 sock_name
= g_strdup(optarg
);
1065 own_u
= atoi(optarg
);
1068 own_g
= atoi(optarg
);
1078 /* Parameter validation */
1079 if ((sock_name
== NULL
&& sock
== -1) || rpath
== NULL
) {
1080 fprintf(stderr
, "socket, socket descriptor or path not specified\n");
1085 if (sock_name
&& sock
!= -1) {
1086 fprintf(stderr
, "both named socket and socket descriptor specified\n");
1091 if (sock_name
&& (own_u
== -1 || own_g
== -1)) {
1092 fprintf(stderr
, "owner uid:gid not specified, ");
1094 "owner uid:gid specifies who can access the socket file\n");
1099 if (lstat(rpath
, &stbuf
) < 0) {
1100 fprintf(stderr
, "invalid path \"%s\" specified, %s\n",
1101 rpath
, strerror(errno
));
1105 if (!S_ISDIR(stbuf
.st_mode
)) {
1106 fprintf(stderr
, "specified path \"%s\" is not directory\n", rpath
);
1111 if (daemon(0, 0) < 0) {
1112 fprintf(stderr
, "daemon call failed\n");
1115 openlog(PROGNAME
, LOG_PID
, LOG_DAEMON
);
1118 do_log(LOG_INFO
, "Started\n");
1120 sock
= proxy_socket(sock_name
, own_u
, own_g
);
1126 if (chroot(rpath
) < 0) {
1127 do_perror("chroot");
1130 if (chdir("/") < 0) {
1135 get_version
= false;
1136 #ifdef FS_IOC_GETVERSION
1137 /* check whether underlying FS support IOC_GETVERSION */
1138 retval
= statfs("/", &st_fs
);
1140 switch (st_fs
.f_type
) {
1141 case EXT2_SUPER_MAGIC
:
1142 case BTRFS_SUPER_MAGIC
:
1143 case REISERFS_SUPER_MAGIC
:
1144 case XFS_SUPER_MAGIC
:
1152 if (init_capabilities() < 0) {
1156 process_requests(sock
);
1160 do_log(LOG_INFO
, "Done\n");