1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
11 #include <sys/ioctl.h>
12 #include <sys/mount.h>
13 #include <sys/socket.h>
15 #include <sys/sysmacros.h>
17 #include <sys/types.h>
18 #include <sys/eventfd.h>
21 static void copy_stat(struct uml_stat
*dst
, const struct stat64
*src
)
23 *dst
= ((struct uml_stat
) {
24 .ust_dev
= src
->st_dev
, /* device */
25 .ust_ino
= src
->st_ino
, /* inode */
26 .ust_mode
= src
->st_mode
, /* protection */
27 .ust_nlink
= src
->st_nlink
, /* number of hard links */
28 .ust_uid
= src
->st_uid
, /* user ID of owner */
29 .ust_gid
= src
->st_gid
, /* group ID of owner */
30 .ust_size
= src
->st_size
, /* total size, in bytes */
31 .ust_blksize
= src
->st_blksize
, /* blocksize for filesys I/O */
32 .ust_blocks
= src
->st_blocks
, /* number of blocks allocated */
33 .ust_atime
= src
->st_atime
, /* time of last access */
34 .ust_mtime
= src
->st_mtime
, /* time of last modification */
35 .ust_ctime
= src
->st_ctime
, /* time of last change */
39 int os_stat_fd(const int fd
, struct uml_stat
*ubuf
)
44 CATCH_EINTR(err
= fstat64(fd
, &sbuf
));
49 copy_stat(ubuf
, &sbuf
);
53 int os_stat_file(const char *file_name
, struct uml_stat
*ubuf
)
58 CATCH_EINTR(err
= stat64(file_name
, &sbuf
));
63 copy_stat(ubuf
, &sbuf
);
67 int os_access(const char *file
, int mode
)
71 amode
= (mode
& OS_ACC_R_OK
? R_OK
: 0) |
72 (mode
& OS_ACC_W_OK
? W_OK
: 0) |
73 (mode
& OS_ACC_X_OK
? X_OK
: 0) |
74 (mode
& OS_ACC_F_OK
? F_OK
: 0);
76 err
= access(file
, amode
);
83 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
84 int os_ioctl_generic(int fd
, unsigned int cmd
, unsigned long arg
)
88 err
= ioctl(fd
, cmd
, arg
);
95 /* FIXME: ensure namebuf in os_get_if_name is big enough */
96 int os_get_ifname(int fd
, char* namebuf
)
98 if (ioctl(fd
, SIOCGIFNAME
, namebuf
) < 0)
104 int os_set_slip(int fd
)
109 if (ioctl(fd
, TIOCSETD
, &disc
) < 0)
113 if (ioctl(fd
, SIOCSIFENCAP
, &sencap
) < 0)
119 int os_mode_fd(int fd
, int mode
)
123 CATCH_EINTR(err
= fchmod(fd
, mode
));
130 int os_file_type(char *file
)
135 err
= os_stat_file(file
, &buf
);
139 if (S_ISDIR(buf
.ust_mode
))
141 else if (S_ISLNK(buf
.ust_mode
))
142 return OS_TYPE_SYMLINK
;
143 else if (S_ISCHR(buf
.ust_mode
))
144 return OS_TYPE_CHARDEV
;
145 else if (S_ISBLK(buf
.ust_mode
))
146 return OS_TYPE_BLOCKDEV
;
147 else if (S_ISFIFO(buf
.ust_mode
))
149 else if (S_ISSOCK(buf
.ust_mode
))
151 else return OS_TYPE_FILE
;
154 int os_file_mode(const char *file
, struct openflags
*mode_out
)
158 *mode_out
= OPENFLAGS();
160 err
= access(file
, W_OK
);
161 if (err
&& (errno
!= EACCES
))
164 *mode_out
= of_write(*mode_out
);
166 err
= access(file
, R_OK
);
167 if (err
&& (errno
!= EACCES
))
170 *mode_out
= of_read(*mode_out
);
175 int os_open_file(const char *file
, struct openflags flags
, int mode
)
179 if (flags
.r
&& flags
.w
)
198 fd
= open64(file
, f
, mode
);
202 if (flags
.cl
&& fcntl(fd
, F_SETFD
, 1)) {
211 int os_connect_socket(const char *name
)
213 struct sockaddr_un sock
;
216 sock
.sun_family
= AF_UNIX
;
217 snprintf(sock
.sun_path
, sizeof(sock
.sun_path
), "%s", name
);
219 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
225 err
= connect(fd
, (struct sockaddr
*) &sock
, sizeof(sock
));
239 void os_close_file(int fd
)
243 int os_fsync_file(int fd
)
250 int os_seek_file(int fd
, unsigned long long offset
)
252 unsigned long long actual
;
254 actual
= lseek64(fd
, offset
, SEEK_SET
);
255 if (actual
!= offset
)
260 int os_read_file(int fd
, void *buf
, int len
)
262 int n
= read(fd
, buf
, len
);
269 int os_pread_file(int fd
, void *buf
, int len
, unsigned long long offset
)
271 int n
= pread(fd
, buf
, len
, offset
);
278 int os_write_file(int fd
, const void *buf
, int len
)
280 int n
= write(fd
, (void *) buf
, len
);
287 int os_sync_file(int fd
)
296 int os_pwrite_file(int fd
, const void *buf
, int len
, unsigned long long offset
)
298 int n
= pwrite(fd
, (void *) buf
, len
, offset
);
306 int os_file_size(const char *file
, unsigned long long *size_out
)
311 err
= os_stat_file(file
, &buf
);
313 printk(UM_KERN_ERR
"Couldn't stat \"%s\" : err = %d\n", file
,
318 if (S_ISBLK(buf
.ust_mode
)) {
322 fd
= open(file
, O_RDONLY
, 0);
325 printk(UM_KERN_ERR
"Couldn't open \"%s\", "
326 "errno = %d\n", file
, errno
);
329 if (ioctl(fd
, BLKGETSIZE
, &blocks
) < 0) {
331 printk(UM_KERN_ERR
"Couldn't get the block size of "
332 "\"%s\", errno = %d\n", file
, errno
);
336 *size_out
= ((long long) blocks
) * 512;
339 else *size_out
= buf
.ust_size
;
344 int os_file_modtime(const char *file
, long long *modtime
)
349 err
= os_stat_file(file
, &buf
);
351 printk(UM_KERN_ERR
"Couldn't stat \"%s\" : err = %d\n", file
,
356 *modtime
= buf
.ust_mtime
;
360 int os_set_exec_close(int fd
)
364 CATCH_EINTR(err
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
));
371 int os_pipe(int *fds
, int stream
, int close_on_exec
)
373 int err
, type
= stream
? SOCK_STREAM
: SOCK_DGRAM
;
375 err
= socketpair(AF_UNIX
, type
, 0, fds
);
382 err
= os_set_exec_close(fds
[0]);
386 err
= os_set_exec_close(fds
[1]);
393 printk(UM_KERN_ERR
"os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
400 int os_set_fd_async(int fd
)
404 flags
= fcntl(fd
, F_GETFL
);
408 flags
|= O_ASYNC
| O_NONBLOCK
;
409 if (fcntl(fd
, F_SETFL
, flags
) < 0) {
411 printk(UM_KERN_ERR
"os_set_fd_async : failed to set O_ASYNC "
412 "and O_NONBLOCK on fd # %d, errno = %d\n", fd
, errno
);
416 if ((fcntl(fd
, F_SETSIG
, SIGIO
) < 0) ||
417 (fcntl(fd
, F_SETOWN
, os_getpid()) < 0)) {
419 printk(UM_KERN_ERR
"os_set_fd_async : Failed to fcntl F_SETOWN "
420 "(or F_SETSIG) fd %d, errno = %d\n", fd
, errno
);
427 int os_clear_fd_async(int fd
)
431 flags
= fcntl(fd
, F_GETFL
);
435 flags
&= ~(O_ASYNC
| O_NONBLOCK
);
436 if (fcntl(fd
, F_SETFL
, flags
) < 0)
441 int os_set_fd_block(int fd
, int blocking
)
445 flags
= fcntl(fd
, F_GETFL
);
450 flags
&= ~O_NONBLOCK
;
454 if (fcntl(fd
, F_SETFL
, flags
) < 0)
460 int os_accept_connection(int fd
)
464 new = accept(fd
, NULL
, 0);
482 int os_shutdown_socket(int fd
, int r
, int w
)
495 err
= shutdown(fd
, what
);
501 int os_rcv_fd(int fd
, int *helper_pid_out
)
504 char buf
[CMSG_SPACE(sizeof(new))];
506 struct cmsghdr
*cmsg
;
511 iov
= ((struct iovec
) { .iov_base
= helper_pid_out
,
512 .iov_len
= sizeof(*helper_pid_out
) });
515 msg
.msg_control
= buf
;
516 msg
.msg_controllen
= sizeof(buf
);
519 n
= recvmsg(fd
, &msg
, 0);
522 else if (n
!= iov
.iov_len
)
523 *helper_pid_out
= -1;
525 cmsg
= CMSG_FIRSTHDR(&msg
);
527 printk(UM_KERN_ERR
"rcv_fd didn't receive anything, "
528 "error = %d\n", errno
);
531 if ((cmsg
->cmsg_level
!= SOL_SOCKET
) ||
532 (cmsg
->cmsg_type
!= SCM_RIGHTS
)) {
533 printk(UM_KERN_ERR
"rcv_fd didn't receive a descriptor\n");
537 new = ((int *) CMSG_DATA(cmsg
))[0];
541 int os_create_unix_socket(const char *file
, int len
, int close_on_exec
)
543 struct sockaddr_un addr
;
546 sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
551 err
= os_set_exec_close(sock
);
553 printk(UM_KERN_ERR
"create_unix_socket : "
554 "close_on_exec failed, err = %d", -err
);
557 addr
.sun_family
= AF_UNIX
;
559 snprintf(addr
.sun_path
, len
, "%s", file
);
561 err
= bind(sock
, (struct sockaddr
*) &addr
, sizeof(addr
));
568 void os_flush_stdout(void)
573 int os_lock_file(int fd
, int excl
)
575 int type
= excl
? F_WRLCK
: F_RDLCK
;
576 struct flock lock
= ((struct flock
) { .l_type
= type
,
577 .l_whence
= SEEK_SET
,
582 err
= fcntl(fd
, F_SETLK
, &lock
);
587 err
= fcntl(fd
, F_GETLK
, &lock
);
593 printk(UM_KERN_ERR
"F_SETLK failed, file already locked by pid %d\n",
600 unsigned os_major(unsigned long long dev
)
605 unsigned os_minor(unsigned long long dev
)
610 unsigned long long os_makedev(unsigned major
, unsigned minor
)
612 return makedev(major
, minor
);
615 int os_falloc_punch(int fd
, unsigned long long offset
, int len
)
617 int n
= fallocate(fd
, FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
, offset
, len
);
624 int os_eventfd(unsigned int initval
, int flags
)
626 int fd
= eventfd(initval
, flags
);
633 int os_sendmsg_fds(int fd
, const void *buf
, unsigned int len
, const int *fds
,
634 unsigned int fds_num
)
637 .iov_base
= (void *) buf
,
641 char control
[CMSG_SPACE(sizeof(*fds
) * OS_SENDMSG_MAX_FDS
)];
642 struct cmsghdr align
;
644 unsigned int fds_size
= sizeof(*fds
) * fds_num
;
645 struct msghdr msg
= {
648 .msg_control
= u
.control
,
649 .msg_controllen
= CMSG_SPACE(fds_size
),
651 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(&msg
);
654 if (fds_num
> OS_SENDMSG_MAX_FDS
)
656 memset(u
.control
, 0, sizeof(u
.control
));
657 cmsg
->cmsg_level
= SOL_SOCKET
;
658 cmsg
->cmsg_type
= SCM_RIGHTS
;
659 cmsg
->cmsg_len
= CMSG_LEN(fds_size
);
660 memcpy(CMSG_DATA(cmsg
), fds
, fds_size
);
661 err
= sendmsg(fd
, &msg
, 0);