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/sockets.h"
25 #include "qemu/xattr.h"
26 #include "9p-iov-marshal.h"
27 #include "hw/9pfs/9p-proxy.h"
28 #include "fsdev/9p-iov-marshal.h"
30 #define PROGNAME "virtfs-proxy-helper"
32 #ifndef XFS_SUPER_MAGIC
33 #define XFS_SUPER_MAGIC 0x58465342
35 #ifndef EXT2_SUPER_MAGIC
36 #define EXT2_SUPER_MAGIC 0xEF53
38 #ifndef REISERFS_SUPER_MAGIC
39 #define REISERFS_SUPER_MAGIC 0x52654973
41 #ifndef BTRFS_SUPER_MAGIC
42 #define BTRFS_SUPER_MAGIC 0x9123683E
45 static const 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'},
55 static bool is_daemon
;
56 static bool get_version
; /* IOC getversion IOCTL supported */
57 static char *prog_name
;
59 static void G_GNUC_PRINTF(2, 3) do_log(int loglevel
, const char *format
, ...)
65 vsyslog(LOG_CRIT
, format
, ap
);
67 vfprintf(stderr
, format
, ap
);
72 static void do_perror(const char *string
)
75 syslog(LOG_CRIT
, "%s:%s", string
, strerror(errno
));
77 fprintf(stderr
, "%s:%s\n", string
, strerror(errno
));
81 static int init_capabilities(void)
83 /* helper needs following capabilities only */
95 capng_clear(CAPNG_SELECT_BOTH
);
96 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++) {
97 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
99 do_perror("capng_update");
103 if (capng_apply(CAPNG_SELECT_BOTH
) < 0) {
104 do_perror("capng_apply");
108 /* Prepare effective set for setugid. */
109 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++) {
110 if (cap_list
[i
] == CAP_DAC_OVERRIDE
) {
114 if (capng_update(CAPNG_DROP
, CAPNG_EFFECTIVE
,
116 do_perror("capng_update");
123 static int socket_read(int sockfd
, void *buff
, ssize_t size
)
125 ssize_t retval
, total
= 0;
128 retval
= read(sockfd
, buff
, size
);
133 if (errno
== EINTR
) {
145 static int socket_write(int sockfd
, void *buff
, ssize_t size
)
147 ssize_t retval
, total
= 0;
150 retval
= write(sockfd
, buff
, size
);
152 if (errno
== EINTR
) {
164 static int read_request(int sockfd
, struct iovec
*iovec
, ProxyHeader
*header
)
169 * read the request header.
172 retval
= socket_read(sockfd
, iovec
->iov_base
, PROXY_HDR_SZ
);
176 iovec
->iov_len
= PROXY_HDR_SZ
;
177 retval
= proxy_unmarshal(iovec
, 0, "dd", &header
->type
, &header
->size
);
182 * We can't process message.size > PROXY_MAX_IO_SZ.
183 * Treat it as fatal error
185 if (header
->size
> PROXY_MAX_IO_SZ
) {
188 retval
= socket_read(sockfd
, iovec
->iov_base
+ PROXY_HDR_SZ
, header
->size
);
192 iovec
->iov_len
+= header
->size
;
196 static int send_fd(int sockfd
, int fd
)
201 struct cmsghdr
*cmsg
;
202 union MsgControl msg_control
;
204 iov
.iov_base
= &data
;
205 iov
.iov_len
= sizeof(data
);
207 memset(&msg
, 0, sizeof(msg
));
210 /* No ancillary data on error */
212 /* fd is really negative errno if the request failed */
215 data
= V9FS_FD_VALID
;
216 msg
.msg_control
= &msg_control
;
217 msg
.msg_controllen
= sizeof(msg_control
);
219 cmsg
= &msg_control
.cmsg
;
220 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
221 cmsg
->cmsg_level
= SOL_SOCKET
;
222 cmsg
->cmsg_type
= SCM_RIGHTS
;
223 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(fd
));
227 retval
= sendmsg(sockfd
, &msg
, 0);
228 } while (retval
< 0 && errno
== EINTR
);
238 static int send_status(int sockfd
, struct iovec
*iovec
, int status
)
241 int retval
, msg_size
;
244 header
.type
= T_ERROR
;
246 header
.type
= T_SUCCESS
;
248 header
.size
= sizeof(status
);
250 * marshal the return status. We don't check error.
251 * because we are sure we have enough space for the status
253 msg_size
= proxy_marshal(iovec
, 0, "ddd", header
.type
,
254 header
.size
, status
);
258 retval
= socket_write(sockfd
, iovec
->iov_base
, msg_size
);
266 * from man 7 capabilities, section
267 * Effect of User ID Changes on Capabilities:
268 * If the effective user ID is changed from nonzero to 0, then the permitted
269 * set is copied to the effective set. If the effective user ID is changed
270 * from 0 to nonzero, then all capabilities are are cleared from the effective
273 * The setfsuid/setfsgid man pages warn that changing the effective user ID may
274 * expose the program to unwanted signals, but this is not true anymore: for an
275 * unprivileged (without CAP_KILL) program to send a signal, the real or
276 * effective user ID of the sending process must equal the real or saved user
277 * ID of the target process. Even when dropping privileges, it is enough to
278 * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
279 * be exposed to signals. So just use setresuid/setresgid.
281 static int setugid(int uid
, int gid
, int *suid
, int *sgid
)
288 if (setresgid(-1, gid
, *sgid
) == -1) {
292 if (setresuid(-1, uid
, *suid
) == -1) {
297 if (uid
== 0 && gid
== 0) {
298 /* Linux has already copied the permitted set to the effective set. */
303 * All capabilities have been cleared from the effective set. However
304 * we still need DAC_OVERRIDE because we don't change supplementary
305 * group ids, and hence may be subject to DAC rules. init_capabilities
306 * left the set of capabilities that we want in libcap-ng's state.
308 if (capng_apply(CAPNG_SELECT_CAPS
) < 0) {
310 do_perror("capng_apply");
316 if (setresuid(-1, *suid
, *suid
) == -1) {
320 if (setresgid(-1, *sgid
, *sgid
) == -1) {
327 * This is used to reset the ugid back with the saved values
328 * There is nothing much we can do checking error values here.
330 static void resetugid(int suid
, int sgid
)
332 if (setresgid(-1, sgid
, sgid
) == -1) {
335 if (setresuid(-1, suid
, suid
) == -1) {
341 * send response in two parts
343 * 2) Response or error status
344 * This function should be called with marshaled response
345 * send_response constructs header part and error part only.
346 * send response sends {ProxyHeader,Response} if the request was success
347 * otherwise sends {ProxyHeader,error status}
349 static int send_response(int sock
, struct iovec
*iovec
, int size
)
355 * If response size exceeds available iovec->iov_len,
358 if (size
> PROXY_MAX_IO_SZ
) {
364 * In case of error we would not have got the error encoded
365 * already so encode the error here.
367 header
.type
= T_ERROR
;
368 header
.size
= sizeof(size
);
369 proxy_marshal(iovec
, PROXY_HDR_SZ
, "d", size
);
371 header
.type
= T_SUCCESS
;
374 proxy_marshal(iovec
, 0, "dd", header
.type
, header
.size
);
375 retval
= socket_write(sock
, iovec
->iov_base
, header
.size
+ PROXY_HDR_SZ
);
383 * gets generation number
384 * returns -errno on failure and sizeof(generation number) on success
386 static int do_getversion(struct iovec
*iovec
, struct iovec
*out_iovec
)
389 int retval
= -ENOTTY
;
390 #ifdef FS_IOC_GETVERSION
396 /* no need to issue ioctl */
399 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "q", version
);
402 #ifdef FS_IOC_GETVERSION
403 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "s", &path
);
408 fd
= open(path
.data
, O_RDONLY
);
413 if (ioctl(fd
, FS_IOC_GETVERSION
, &version
) < 0) {
416 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "q", version
);
420 v9fs_string_free(&path
);
425 static int do_getxattr(int type
, struct iovec
*iovec
, struct iovec
*out_iovec
)
427 int size
= 0, offset
, retval
;
428 V9fsString path
, name
, xattr
;
430 v9fs_string_init(&xattr
);
431 v9fs_string_init(&path
);
432 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "ds", &size
, &path
);
436 offset
= PROXY_HDR_SZ
+ retval
;
439 xattr
.data
= g_malloc(size
);
444 v9fs_string_init(&name
);
445 retval
= proxy_unmarshal(iovec
, offset
, "s", &name
);
447 retval
= lgetxattr(path
.data
, name
.data
, xattr
.data
, size
);
454 v9fs_string_free(&name
);
457 retval
= llistxattr(path
.data
, xattr
.data
, size
);
470 proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "d", retval
);
471 retval
= sizeof(retval
);
473 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "s", &xattr
);
476 v9fs_string_free(&xattr
);
477 v9fs_string_free(&path
);
481 static void stat_to_prstat(ProxyStat
*pr_stat
, struct stat
*stat
)
483 memset(pr_stat
, 0, sizeof(*pr_stat
));
484 pr_stat
->st_dev
= stat
->st_dev
;
485 pr_stat
->st_ino
= stat
->st_ino
;
486 pr_stat
->st_nlink
= stat
->st_nlink
;
487 pr_stat
->st_mode
= stat
->st_mode
;
488 pr_stat
->st_uid
= stat
->st_uid
;
489 pr_stat
->st_gid
= stat
->st_gid
;
490 pr_stat
->st_rdev
= stat
->st_rdev
;
491 pr_stat
->st_size
= stat
->st_size
;
492 pr_stat
->st_blksize
= stat
->st_blksize
;
493 pr_stat
->st_blocks
= stat
->st_blocks
;
494 pr_stat
->st_atim_sec
= stat
->st_atim
.tv_sec
;
495 pr_stat
->st_atim_nsec
= stat
->st_atim
.tv_nsec
;
496 pr_stat
->st_mtim_sec
= stat
->st_mtim
.tv_sec
;
497 pr_stat
->st_mtim_nsec
= stat
->st_mtim
.tv_nsec
;
498 pr_stat
->st_ctim_sec
= stat
->st_ctim
.tv_sec
;
499 pr_stat
->st_ctim_nsec
= stat
->st_ctim
.tv_nsec
;
502 static void statfs_to_prstatfs(ProxyStatFS
*pr_stfs
, struct statfs
*stfs
)
504 memset(pr_stfs
, 0, sizeof(*pr_stfs
));
505 pr_stfs
->f_type
= stfs
->f_type
;
506 pr_stfs
->f_bsize
= stfs
->f_bsize
;
507 pr_stfs
->f_blocks
= stfs
->f_blocks
;
508 pr_stfs
->f_bfree
= stfs
->f_bfree
;
509 pr_stfs
->f_bavail
= stfs
->f_bavail
;
510 pr_stfs
->f_files
= stfs
->f_files
;
511 pr_stfs
->f_ffree
= stfs
->f_ffree
;
512 pr_stfs
->f_fsid
[0] = stfs
->f_fsid
.__val
[0];
513 pr_stfs
->f_fsid
[1] = stfs
->f_fsid
.__val
[1];
514 pr_stfs
->f_namelen
= stfs
->f_namelen
;
515 pr_stfs
->f_frsize
= stfs
->f_frsize
;
519 * Gets stat/statfs information and packs in out_iovec structure
520 * on success returns number of bytes packed in out_iovec structure
521 * otherwise returns -errno
523 static int do_stat(int type
, struct iovec
*iovec
, struct iovec
*out_iovec
)
530 struct statfs stfs_buf
;
532 v9fs_string_init(&path
);
533 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "s", &path
);
540 retval
= lstat(path
.data
, &st_buf
);
544 stat_to_prstat(&pr_stat
, &st_buf
);
545 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
,
546 "qqqdddqqqqqqqqqq", pr_stat
.st_dev
,
547 pr_stat
.st_ino
, pr_stat
.st_nlink
,
548 pr_stat
.st_mode
, pr_stat
.st_uid
,
549 pr_stat
.st_gid
, pr_stat
.st_rdev
,
550 pr_stat
.st_size
, pr_stat
.st_blksize
,
552 pr_stat
.st_atim_sec
, pr_stat
.st_atim_nsec
,
553 pr_stat
.st_mtim_sec
, pr_stat
.st_mtim_nsec
,
554 pr_stat
.st_ctim_sec
, pr_stat
.st_ctim_nsec
);
558 retval
= statfs(path
.data
, &stfs_buf
);
562 statfs_to_prstatfs(&pr_stfs
, &stfs_buf
);
563 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
,
564 "qqqqqqqqqqq", pr_stfs
.f_type
,
565 pr_stfs
.f_bsize
, pr_stfs
.f_blocks
,
566 pr_stfs
.f_bfree
, pr_stfs
.f_bavail
,
567 pr_stfs
.f_files
, pr_stfs
.f_ffree
,
568 pr_stfs
.f_fsid
[0], pr_stfs
.f_fsid
[1],
569 pr_stfs
.f_namelen
, pr_stfs
.f_frsize
);
573 v9fs_string_free(&path
);
577 static int do_readlink(struct iovec
*iovec
, struct iovec
*out_iovec
)
581 V9fsString target
, path
;
583 v9fs_string_init(&path
);
584 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sd", &path
, &size
);
586 v9fs_string_free(&path
);
589 buffer
= g_malloc(size
);
590 v9fs_string_init(&target
);
591 retval
= readlink(path
.data
, buffer
, size
- 1);
593 buffer
[retval
] = '\0';
594 v9fs_string_sprintf(&target
, "%s", buffer
);
595 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "s", &target
);
600 v9fs_string_free(&target
);
601 v9fs_string_free(&path
);
606 * create other filesystem objects and send 0 on success
607 * return -errno on error
609 static int do_create_others(int type
, struct iovec
*iovec
)
613 int offset
= PROXY_HDR_SZ
;
614 V9fsString oldpath
, path
;
615 int mode
, uid
, gid
, cur_uid
, cur_gid
;
617 v9fs_string_init(&path
);
618 v9fs_string_init(&oldpath
);
620 retval
= proxy_unmarshal(iovec
, offset
, "dd", &uid
, &gid
);
625 retval
= setugid(uid
, gid
, &cur_uid
, &cur_gid
);
627 goto unmarshal_err_out
;
631 retval
= proxy_unmarshal(iovec
, offset
, "sdq", &path
, &mode
, &rdev
);
635 retval
= mknod(path
.data
, mode
, rdev
);
638 retval
= proxy_unmarshal(iovec
, offset
, "sd", &path
, &mode
);
642 retval
= mkdir(path
.data
, mode
);
645 retval
= proxy_unmarshal(iovec
, offset
, "ss", &oldpath
, &path
);
649 retval
= symlink(oldpath
.data
, path
.data
);
657 resetugid(cur_uid
, cur_gid
);
659 v9fs_string_free(&path
);
660 v9fs_string_free(&oldpath
);
665 * create a file and send fd on success
666 * return -errno on error
668 static int do_create(struct iovec
*iovec
)
672 int flags
, mode
, uid
, gid
, cur_uid
, cur_gid
;
674 v9fs_string_init(&path
);
675 ret
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sdddd",
676 &path
, &flags
, &mode
, &uid
, &gid
);
678 goto unmarshal_err_out
;
680 ret
= setugid(uid
, gid
, &cur_uid
, &cur_gid
);
682 goto unmarshal_err_out
;
684 ret
= open(path
.data
, flags
, mode
);
689 resetugid(cur_uid
, cur_gid
);
691 v9fs_string_free(&path
);
696 * open a file and send fd on success
697 * return -errno on error
699 static int do_open(struct iovec
*iovec
)
704 v9fs_string_init(&path
);
705 ret
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sd", &path
, &flags
);
709 ret
= open(path
.data
, flags
);
714 v9fs_string_free(&path
);
718 /* create unix domain socket and return the descriptor */
719 static int proxy_socket(const char *path
, uid_t uid
, gid_t gid
)
722 struct sockaddr_un proxy
, qemu
;
725 /* requested socket already exists, refuse to start */
726 if (!access(path
, F_OK
)) {
727 do_log(LOG_CRIT
, "socket already exists\n");
731 if (strlen(path
) >= sizeof(proxy
.sun_path
)) {
732 do_log(LOG_CRIT
, "UNIX domain socket path exceeds %zu characters\n",
733 sizeof(proxy
.sun_path
));
737 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
743 /* mask other part of mode bits */
746 proxy
.sun_family
= AF_UNIX
;
747 strcpy(proxy
.sun_path
, path
);
748 if (bind(sock
, (struct sockaddr
*)&proxy
,
749 sizeof(struct sockaddr_un
)) < 0) {
753 if (chown(proxy
.sun_path
, uid
, gid
) < 0) {
757 if (listen(sock
, 1) < 0) {
763 client
= accept(sock
, (struct sockaddr
*)&qemu
, &size
);
776 static void usage(void)
778 fprintf(stderr
, "usage: %s\n"
779 " -p|--path <path> 9p path to export\n"
780 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
781 " {-s|--socket <socketname> socket file used for communication\n"
782 " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
783 " access to this socket\n"
784 " \tNote: -s & -f can not be used together\n"
785 " [-n|--nodaemon] Run as a normal program\n",
789 static int process_reply(int sock
, int type
,
790 struct iovec
*out_iovec
, int retval
)
795 if (send_fd(sock
, retval
) < 0) {
811 if (send_status(sock
, out_iovec
, retval
) < 0) {
821 if (send_response(sock
, out_iovec
, retval
) < 0) {
832 static int process_requests(int sock
)
840 V9fsString name
, value
;
841 struct timespec spec
[2];
842 V9fsString oldpath
, path
;
843 struct iovec in_iovec
, out_iovec
;
845 in_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
846 in_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
847 out_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
848 out_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
852 * initialize the header type, so that we send
853 * response to proper request type.
856 retval
= read_request(sock
, &in_iovec
, &header
);
861 switch (header
.type
) {
863 retval
= do_open(&in_iovec
);
866 retval
= do_create(&in_iovec
);
871 retval
= do_create_others(header
.type
, &in_iovec
);
874 v9fs_string_init(&path
);
875 v9fs_string_init(&oldpath
);
876 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
877 "ss", &oldpath
, &path
);
879 retval
= link(oldpath
.data
, path
.data
);
884 v9fs_string_free(&oldpath
);
885 v9fs_string_free(&path
);
889 retval
= do_stat(header
.type
, &in_iovec
, &out_iovec
);
892 retval
= do_readlink(&in_iovec
, &out_iovec
);
895 v9fs_string_init(&path
);
896 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
899 retval
= chmod(path
.data
, mode
);
904 v9fs_string_free(&path
);
907 v9fs_string_init(&path
);
908 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sdd", &path
,
911 retval
= lchown(path
.data
, uid
, gid
);
916 v9fs_string_free(&path
);
919 v9fs_string_init(&path
);
920 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sq",
923 retval
= truncate(path
.data
, offset
);
928 v9fs_string_free(&path
);
931 v9fs_string_init(&path
);
932 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sqqqq", &path
,
933 &spec
[0].tv_sec
, &spec
[0].tv_nsec
,
934 &spec
[1].tv_sec
, &spec
[1].tv_nsec
);
936 retval
= utimensat(AT_FDCWD
, path
.data
, spec
,
937 AT_SYMLINK_NOFOLLOW
);
942 v9fs_string_free(&path
);
945 v9fs_string_init(&path
);
946 v9fs_string_init(&oldpath
);
947 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
948 "ss", &oldpath
, &path
);
950 retval
= rename(oldpath
.data
, path
.data
);
955 v9fs_string_free(&oldpath
);
956 v9fs_string_free(&path
);
959 v9fs_string_init(&path
);
960 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "s", &path
);
962 retval
= remove(path
.data
);
967 v9fs_string_free(&path
);
971 retval
= do_getxattr(header
.type
, &in_iovec
, &out_iovec
);
974 v9fs_string_init(&path
);
975 v9fs_string_init(&name
);
976 v9fs_string_init(&value
);
977 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sssdd", &path
,
978 &name
, &value
, &size
, &flags
);
980 retval
= lsetxattr(path
.data
,
981 name
.data
, value
.data
, size
, flags
);
986 v9fs_string_free(&path
);
987 v9fs_string_free(&name
);
988 v9fs_string_free(&value
);
991 v9fs_string_init(&path
);
992 v9fs_string_init(&name
);
993 retval
= proxy_unmarshal(&in_iovec
,
994 PROXY_HDR_SZ
, "ss", &path
, &name
);
996 retval
= lremovexattr(path
.data
, name
.data
);
1001 v9fs_string_free(&path
);
1002 v9fs_string_free(&name
);
1005 retval
= do_getversion(&in_iovec
, &out_iovec
);
1012 if (process_reply(sock
, header
.type
, &out_iovec
, retval
) < 0) {
1017 g_free(in_iovec
.iov_base
);
1018 g_free(out_iovec
.iov_base
);
1022 int main(int argc
, char **argv
)
1028 char *sock_name
= NULL
;
1030 int c
, option_index
;
1031 #ifdef FS_IOC_GETVERSION
1033 struct statfs st_fs
;
1036 prog_name
= g_path_get_basename(argv
[0]);
1043 c
= getopt_long(argc
, argv
, "p:nh?f:s:u:g:", helper_opts
,
1050 rpath
= g_strdup(optarg
);
1056 sock
= atoi(optarg
);
1059 sock_name
= g_strdup(optarg
);
1062 own_u
= atoi(optarg
);
1065 own_g
= atoi(optarg
);
1075 /* Parameter validation */
1076 if ((sock_name
== NULL
&& sock
== -1) || rpath
== NULL
) {
1077 fprintf(stderr
, "socket, socket descriptor or path not specified\n");
1082 if (sock_name
&& sock
!= -1) {
1083 fprintf(stderr
, "both named socket and socket descriptor specified\n");
1088 if (sock_name
&& (own_u
== -1 || own_g
== -1)) {
1089 fprintf(stderr
, "owner uid:gid not specified, ");
1091 "owner uid:gid specifies who can access the socket file\n");
1096 if (lstat(rpath
, &stbuf
) < 0) {
1097 fprintf(stderr
, "invalid path \"%s\" specified, %s\n",
1098 rpath
, strerror(errno
));
1102 if (!S_ISDIR(stbuf
.st_mode
)) {
1103 fprintf(stderr
, "specified path \"%s\" is not directory\n", rpath
);
1108 if (daemon(0, 0) < 0) {
1109 fprintf(stderr
, "daemon call failed\n");
1112 openlog(PROGNAME
, LOG_PID
, LOG_DAEMON
);
1115 do_log(LOG_INFO
, "Started\n");
1117 sock
= proxy_socket(sock_name
, own_u
, own_g
);
1123 if (chroot(rpath
) < 0) {
1124 do_perror("chroot");
1127 if (chdir("/") < 0) {
1132 get_version
= false;
1133 #ifdef FS_IOC_GETVERSION
1134 /* check whether underlying FS support IOC_GETVERSION */
1135 retval
= statfs("/", &st_fs
);
1137 switch (st_fs
.f_type
) {
1138 case EXT2_SUPER_MAGIC
:
1139 case BTRFS_SUPER_MAGIC
:
1140 case REISERFS_SUPER_MAGIC
:
1141 case XFS_SUPER_MAGIC
:
1149 if (init_capabilities() < 0) {
1153 process_requests(sock
);
1157 do_log(LOG_INFO
, "Done\n");