1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
13 #include <linux/falloc.h>
14 #include <sys/ioctl.h>
15 #include <sys/mount.h>
16 #include <sys/socket.h>
18 #include <sys/sysmacros.h>
21 #include <sys/types.h>
22 #include <sys/eventfd.h>
26 static void copy_stat(struct uml_stat
*dst
, const struct stat64
*src
)
28 *dst
= ((struct uml_stat
) {
29 .ust_dev
= src
->st_dev
, /* device */
30 .ust_ino
= src
->st_ino
, /* inode */
31 .ust_mode
= src
->st_mode
, /* protection */
32 .ust_nlink
= src
->st_nlink
, /* number of hard links */
33 .ust_uid
= src
->st_uid
, /* user ID of owner */
34 .ust_gid
= src
->st_gid
, /* group ID of owner */
35 .ust_size
= src
->st_size
, /* total size, in bytes */
36 .ust_blksize
= src
->st_blksize
, /* blocksize for filesys I/O */
37 .ust_blocks
= src
->st_blocks
, /* number of blocks allocated */
38 .ust_atime
= src
->st_atime
, /* time of last access */
39 .ust_mtime
= src
->st_mtime
, /* time of last modification */
40 .ust_ctime
= src
->st_ctime
, /* time of last change */
44 int os_stat_fd(const int fd
, struct uml_stat
*ubuf
)
49 CATCH_EINTR(err
= fstat64(fd
, &sbuf
));
54 copy_stat(ubuf
, &sbuf
);
58 int os_stat_file(const char *file_name
, struct uml_stat
*ubuf
)
63 CATCH_EINTR(err
= stat64(file_name
, &sbuf
));
68 copy_stat(ubuf
, &sbuf
);
72 int os_access(const char *file
, int mode
)
76 amode
= (mode
& OS_ACC_R_OK
? R_OK
: 0) |
77 (mode
& OS_ACC_W_OK
? W_OK
: 0) |
78 (mode
& OS_ACC_X_OK
? X_OK
: 0) |
79 (mode
& OS_ACC_F_OK
? F_OK
: 0);
81 err
= access(file
, amode
);
88 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
89 int os_ioctl_generic(int fd
, unsigned int cmd
, unsigned long arg
)
93 err
= ioctl(fd
, cmd
, arg
);
100 /* FIXME: ensure namebuf in os_get_if_name is big enough */
101 int os_get_ifname(int fd
, char* namebuf
)
103 if (ioctl(fd
, SIOCGIFNAME
, namebuf
) < 0)
109 int os_set_slip(int fd
)
114 if (ioctl(fd
, TIOCSETD
, &disc
) < 0)
118 if (ioctl(fd
, SIOCSIFENCAP
, &sencap
) < 0)
124 int os_mode_fd(int fd
, int mode
)
128 CATCH_EINTR(err
= fchmod(fd
, mode
));
135 int os_file_type(char *file
)
140 err
= os_stat_file(file
, &buf
);
144 if (S_ISDIR(buf
.ust_mode
))
146 else if (S_ISLNK(buf
.ust_mode
))
147 return OS_TYPE_SYMLINK
;
148 else if (S_ISCHR(buf
.ust_mode
))
149 return OS_TYPE_CHARDEV
;
150 else if (S_ISBLK(buf
.ust_mode
))
151 return OS_TYPE_BLOCKDEV
;
152 else if (S_ISFIFO(buf
.ust_mode
))
154 else if (S_ISSOCK(buf
.ust_mode
))
156 else return OS_TYPE_FILE
;
159 int os_file_mode(const char *file
, struct openflags
*mode_out
)
163 *mode_out
= OPENFLAGS();
165 err
= access(file
, W_OK
);
166 if (err
&& (errno
!= EACCES
))
169 *mode_out
= of_write(*mode_out
);
171 err
= access(file
, R_OK
);
172 if (err
&& (errno
!= EACCES
))
175 *mode_out
= of_read(*mode_out
);
180 int os_open_file(const char *file
, struct openflags flags
, int mode
)
184 if (flags
.r
&& flags
.w
)
203 fd
= open64(file
, f
, mode
);
207 if (flags
.cl
&& fcntl(fd
, F_SETFD
, 1)) {
216 int os_connect_socket(const char *name
)
218 struct sockaddr_un sock
;
221 sock
.sun_family
= AF_UNIX
;
222 snprintf(sock
.sun_path
, sizeof(sock
.sun_path
), "%s", name
);
224 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
230 err
= connect(fd
, (struct sockaddr
*) &sock
, sizeof(sock
));
244 int os_dup_file(int fd
)
246 int new_fd
= dup(fd
);
254 void os_close_file(int fd
)
258 int os_fsync_file(int fd
)
265 int os_seek_file(int fd
, unsigned long long offset
)
267 unsigned long long actual
;
269 actual
= lseek64(fd
, offset
, SEEK_SET
);
270 if (actual
!= offset
)
275 int os_read_file(int fd
, void *buf
, int len
)
277 int n
= read(fd
, buf
, len
);
284 int os_pread_file(int fd
, void *buf
, int len
, unsigned long long offset
)
286 int n
= pread(fd
, buf
, len
, offset
);
293 int os_write_file(int fd
, const void *buf
, int len
)
295 int n
= write(fd
, (void *) buf
, len
);
302 int os_sync_file(int fd
)
304 int n
= fdatasync(fd
);
311 int os_pwrite_file(int fd
, const void *buf
, int len
, unsigned long long offset
)
313 int n
= pwrite(fd
, (void *) buf
, len
, offset
);
321 int os_file_size(const char *file
, unsigned long long *size_out
)
326 err
= os_stat_file(file
, &buf
);
328 printk(UM_KERN_ERR
"Couldn't stat \"%s\" : err = %d\n", file
,
333 if (S_ISBLK(buf
.ust_mode
)) {
337 fd
= open(file
, O_RDONLY
, 0);
340 printk(UM_KERN_ERR
"Couldn't open \"%s\", "
341 "errno = %d\n", file
, errno
);
344 if (ioctl(fd
, BLKGETSIZE
, &blocks
) < 0) {
346 printk(UM_KERN_ERR
"Couldn't get the block size of "
347 "\"%s\", errno = %d\n", file
, errno
);
351 *size_out
= ((long long) blocks
) * 512;
354 else *size_out
= buf
.ust_size
;
359 int os_file_modtime(const char *file
, long long *modtime
)
364 err
= os_stat_file(file
, &buf
);
366 printk(UM_KERN_ERR
"Couldn't stat \"%s\" : err = %d\n", file
,
371 *modtime
= buf
.ust_mtime
;
375 int os_set_exec_close(int fd
)
379 CATCH_EINTR(err
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
));
386 int os_pipe(int *fds
, int stream
, int close_on_exec
)
388 int err
, type
= stream
? SOCK_STREAM
: SOCK_DGRAM
;
390 err
= socketpair(AF_UNIX
, type
, 0, fds
);
397 err
= os_set_exec_close(fds
[0]);
401 err
= os_set_exec_close(fds
[1]);
408 printk(UM_KERN_ERR
"os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
415 int os_set_fd_async(int fd
)
419 flags
= fcntl(fd
, F_GETFL
);
423 flags
|= O_ASYNC
| O_NONBLOCK
;
424 if (fcntl(fd
, F_SETFL
, flags
) < 0) {
426 printk(UM_KERN_ERR
"os_set_fd_async : failed to set O_ASYNC "
427 "and O_NONBLOCK on fd # %d, errno = %d\n", fd
, errno
);
431 if ((fcntl(fd
, F_SETSIG
, SIGIO
) < 0) ||
432 (fcntl(fd
, F_SETOWN
, os_getpid()) < 0)) {
434 printk(UM_KERN_ERR
"os_set_fd_async : Failed to fcntl F_SETOWN "
435 "(or F_SETSIG) fd %d, errno = %d\n", fd
, errno
);
442 int os_clear_fd_async(int fd
)
446 flags
= fcntl(fd
, F_GETFL
);
450 flags
&= ~(O_ASYNC
| O_NONBLOCK
);
451 if (fcntl(fd
, F_SETFL
, flags
) < 0)
456 int os_set_fd_block(int fd
, int blocking
)
460 flags
= fcntl(fd
, F_GETFL
);
465 flags
&= ~O_NONBLOCK
;
469 if (fcntl(fd
, F_SETFL
, flags
) < 0)
475 int os_accept_connection(int fd
)
479 new = accept(fd
, NULL
, 0);
497 int os_shutdown_socket(int fd
, int r
, int w
)
510 err
= shutdown(fd
, what
);
517 * os_rcv_fd_msg - receive message with (optional) FDs
518 * @fd: the FD to receive from
519 * @fds: the array for FDs to write to
520 * @n_fds: number of FDs to receive (@fds array size)
521 * @data: the message buffer
522 * @data_len: the size of the message to receive
524 * Receive a message with FDs.
526 * Returns: the size of the received message, or an error code
528 ssize_t
os_rcv_fd_msg(int fd
, int *fds
, unsigned int n_fds
,
529 void *data
, size_t data_len
)
531 #define MAX_RCV_FDS 2
532 char buf
[CMSG_SPACE(sizeof(*fds
) * MAX_RCV_FDS
)];
533 struct cmsghdr
*cmsg
;
538 struct msghdr msg
= {
542 .msg_controllen
= CMSG_SPACE(sizeof(*fds
) * n_fds
),
546 if (n_fds
> MAX_RCV_FDS
)
549 n
= recvmsg(fd
, &msg
, 0);
553 cmsg
= CMSG_FIRSTHDR(&msg
);
555 cmsg
->cmsg_level
!= SOL_SOCKET
||
556 cmsg
->cmsg_type
!= SCM_RIGHTS
)
559 memcpy(fds
, CMSG_DATA(cmsg
), cmsg
->cmsg_len
);
563 int os_create_unix_socket(const char *file
, int len
, int close_on_exec
)
565 struct sockaddr_un addr
;
568 sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
573 err
= os_set_exec_close(sock
);
575 printk(UM_KERN_ERR
"create_unix_socket : "
576 "close_on_exec failed, err = %d", -err
);
579 addr
.sun_family
= AF_UNIX
;
581 snprintf(addr
.sun_path
, len
, "%s", file
);
583 err
= bind(sock
, (struct sockaddr
*) &addr
, sizeof(addr
));
590 void os_flush_stdout(void)
595 int os_lock_file(int fd
, int excl
)
597 int type
= excl
? F_WRLCK
: F_RDLCK
;
598 struct flock lock
= ((struct flock
) { .l_type
= type
,
599 .l_whence
= SEEK_SET
,
604 err
= fcntl(fd
, F_SETLK
, &lock
);
609 err
= fcntl(fd
, F_GETLK
, &lock
);
615 printk(UM_KERN_ERR
"F_SETLK failed, file already locked by pid %d\n",
622 unsigned os_major(unsigned long long dev
)
627 unsigned os_minor(unsigned long long dev
)
632 unsigned long long os_makedev(unsigned major
, unsigned minor
)
634 return makedev(major
, minor
);
637 int os_falloc_punch(int fd
, unsigned long long offset
, int len
)
639 int n
= fallocate(fd
, FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
, offset
, len
);
646 int os_falloc_zeroes(int fd
, unsigned long long offset
, int len
)
648 int n
= fallocate(fd
, FALLOC_FL_ZERO_RANGE
|FALLOC_FL_KEEP_SIZE
, offset
, len
);
655 int os_eventfd(unsigned int initval
, int flags
)
657 int fd
= eventfd(initval
, flags
);
664 int os_sendmsg_fds(int fd
, const void *buf
, unsigned int len
, const int *fds
,
665 unsigned int fds_num
)
668 .iov_base
= (void *) buf
,
672 char control
[CMSG_SPACE(sizeof(*fds
) * OS_SENDMSG_MAX_FDS
)];
673 struct cmsghdr align
;
675 unsigned int fds_size
= sizeof(*fds
) * fds_num
;
676 struct msghdr msg
= {
679 .msg_control
= u
.control
,
680 .msg_controllen
= CMSG_SPACE(fds_size
),
682 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(&msg
);
685 if (fds_num
> OS_SENDMSG_MAX_FDS
)
687 memset(u
.control
, 0, sizeof(u
.control
));
688 cmsg
->cmsg_level
= SOL_SOCKET
;
689 cmsg
->cmsg_type
= SCM_RIGHTS
;
690 cmsg
->cmsg_len
= CMSG_LEN(fds_size
);
691 memcpy(CMSG_DATA(cmsg
), fds
, fds_size
);
692 err
= sendmsg(fd
, &msg
, 0);
699 int os_poll(unsigned int n
, const int *fds
)
701 /* currently need 2 FDs at most so avoid dynamic allocation */
702 struct pollfd pollfds
[2] = {};
706 if (n
> ARRAY_SIZE(pollfds
))
709 for (i
= 0; i
< n
; i
++) {
710 pollfds
[i
].fd
= fds
[i
];
711 pollfds
[i
].events
= POLLIN
;
714 ret
= poll(pollfds
, n
, -1);
718 /* Return the index of the available FD */
719 for (i
= 0; i
< n
; i
++) {
720 if (pollfds
[i
].revents
)
727 void *os_mmap_rw_shared(int fd
, size_t size
)
729 void *res
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
731 if (res
== MAP_FAILED
)
737 void *os_mremap_rw_shared(void *old_addr
, size_t old_size
, size_t new_size
)
741 res
= mremap(old_addr
, old_size
, new_size
, MREMAP_MAYMOVE
, NULL
);
743 if (res
== MAP_FAILED
)