2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
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>
20 static void copy_stat(struct uml_stat
*dst
, const struct stat64
*src
)
22 *dst
= ((struct uml_stat
) {
23 .ust_dev
= src
->st_dev
, /* device */
24 .ust_ino
= src
->st_ino
, /* inode */
25 .ust_mode
= src
->st_mode
, /* protection */
26 .ust_nlink
= src
->st_nlink
, /* number of hard links */
27 .ust_uid
= src
->st_uid
, /* user ID of owner */
28 .ust_gid
= src
->st_gid
, /* group ID of owner */
29 .ust_size
= src
->st_size
, /* total size, in bytes */
30 .ust_blksize
= src
->st_blksize
, /* blocksize for filesys I/O */
31 .ust_blocks
= src
->st_blocks
, /* number of blocks allocated */
32 .ust_atime
= src
->st_atime
, /* time of last access */
33 .ust_mtime
= src
->st_mtime
, /* time of last modification */
34 .ust_ctime
= src
->st_ctime
, /* time of last change */
38 int os_stat_fd(const int fd
, struct uml_stat
*ubuf
)
43 CATCH_EINTR(err
= fstat64(fd
, &sbuf
));
48 copy_stat(ubuf
, &sbuf
);
52 int os_stat_file(const char *file_name
, struct uml_stat
*ubuf
)
57 CATCH_EINTR(err
= stat64(file_name
, &sbuf
));
62 copy_stat(ubuf
, &sbuf
);
66 int os_access(const char *file
, int mode
)
70 amode
= (mode
& OS_ACC_R_OK
? R_OK
: 0) |
71 (mode
& OS_ACC_W_OK
? W_OK
: 0) |
72 (mode
& OS_ACC_X_OK
? X_OK
: 0) |
73 (mode
& OS_ACC_F_OK
? F_OK
: 0);
75 err
= access(file
, amode
);
82 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
83 int os_ioctl_generic(int fd
, unsigned int cmd
, unsigned long arg
)
87 err
= ioctl(fd
, cmd
, arg
);
94 /* FIXME: ensure namebuf in os_get_if_name is big enough */
95 int os_get_ifname(int fd
, char* namebuf
)
97 if (ioctl(fd
, SIOCGIFNAME
, namebuf
) < 0)
103 int os_set_slip(int fd
)
108 if (ioctl(fd
, TIOCSETD
, &disc
) < 0)
112 if (ioctl(fd
, SIOCSIFENCAP
, &sencap
) < 0)
118 int os_mode_fd(int fd
, int mode
)
122 CATCH_EINTR(err
= fchmod(fd
, mode
));
129 int os_file_type(char *file
)
134 err
= os_stat_file(file
, &buf
);
138 if (S_ISDIR(buf
.ust_mode
))
140 else if (S_ISLNK(buf
.ust_mode
))
141 return OS_TYPE_SYMLINK
;
142 else if (S_ISCHR(buf
.ust_mode
))
143 return OS_TYPE_CHARDEV
;
144 else if (S_ISBLK(buf
.ust_mode
))
145 return OS_TYPE_BLOCKDEV
;
146 else if (S_ISFIFO(buf
.ust_mode
))
148 else if (S_ISSOCK(buf
.ust_mode
))
150 else return OS_TYPE_FILE
;
153 int os_file_mode(const char *file
, struct openflags
*mode_out
)
157 *mode_out
= OPENFLAGS();
159 err
= access(file
, W_OK
);
160 if (err
&& (errno
!= EACCES
))
163 *mode_out
= of_write(*mode_out
);
165 err
= access(file
, R_OK
);
166 if (err
&& (errno
!= EACCES
))
169 *mode_out
= of_read(*mode_out
);
174 int os_open_file(const char *file
, struct openflags flags
, int mode
)
178 if (flags
.r
&& flags
.w
)
197 fd
= open64(file
, f
, mode
);
201 if (flags
.cl
&& fcntl(fd
, F_SETFD
, 1)) {
210 int os_connect_socket(const char *name
)
212 struct sockaddr_un sock
;
215 sock
.sun_family
= AF_UNIX
;
216 snprintf(sock
.sun_path
, sizeof(sock
.sun_path
), "%s", name
);
218 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
224 err
= connect(fd
, (struct sockaddr
*) &sock
, sizeof(sock
));
238 void os_close_file(int fd
)
242 int os_fsync_file(int fd
)
249 int os_seek_file(int fd
, unsigned long long offset
)
251 unsigned long long actual
;
253 actual
= lseek64(fd
, offset
, SEEK_SET
);
254 if (actual
!= offset
)
259 int os_read_file(int fd
, void *buf
, int len
)
261 int n
= read(fd
, buf
, len
);
268 int os_pread_file(int fd
, void *buf
, int len
, unsigned long long offset
)
270 int n
= pread(fd
, buf
, len
, offset
);
277 int os_write_file(int fd
, const void *buf
, int len
)
279 int n
= write(fd
, (void *) buf
, len
);
286 int os_sync_file(int fd
)
295 int os_pwrite_file(int fd
, const void *buf
, int len
, unsigned long long offset
)
297 int n
= pwrite(fd
, (void *) buf
, len
, offset
);
305 int os_file_size(const char *file
, unsigned long long *size_out
)
310 err
= os_stat_file(file
, &buf
);
312 printk(UM_KERN_ERR
"Couldn't stat \"%s\" : err = %d\n", file
,
317 if (S_ISBLK(buf
.ust_mode
)) {
321 fd
= open(file
, O_RDONLY
, 0);
324 printk(UM_KERN_ERR
"Couldn't open \"%s\", "
325 "errno = %d\n", file
, errno
);
328 if (ioctl(fd
, BLKGETSIZE
, &blocks
) < 0) {
330 printk(UM_KERN_ERR
"Couldn't get the block size of "
331 "\"%s\", errno = %d\n", file
, errno
);
335 *size_out
= ((long long) blocks
) * 512;
338 else *size_out
= buf
.ust_size
;
343 int os_file_modtime(const char *file
, unsigned long *modtime
)
348 err
= os_stat_file(file
, &buf
);
350 printk(UM_KERN_ERR
"Couldn't stat \"%s\" : err = %d\n", file
,
355 *modtime
= buf
.ust_mtime
;
359 int os_set_exec_close(int fd
)
363 CATCH_EINTR(err
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
));
370 int os_pipe(int *fds
, int stream
, int close_on_exec
)
372 int err
, type
= stream
? SOCK_STREAM
: SOCK_DGRAM
;
374 err
= socketpair(AF_UNIX
, type
, 0, fds
);
381 err
= os_set_exec_close(fds
[0]);
385 err
= os_set_exec_close(fds
[1]);
392 printk(UM_KERN_ERR
"os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
399 int os_set_fd_async(int fd
)
403 flags
= fcntl(fd
, F_GETFL
);
407 flags
|= O_ASYNC
| O_NONBLOCK
;
408 if (fcntl(fd
, F_SETFL
, flags
) < 0) {
410 printk(UM_KERN_ERR
"os_set_fd_async : failed to set O_ASYNC "
411 "and O_NONBLOCK on fd # %d, errno = %d\n", fd
, errno
);
415 if ((fcntl(fd
, F_SETSIG
, SIGIO
) < 0) ||
416 (fcntl(fd
, F_SETOWN
, os_getpid()) < 0)) {
418 printk(UM_KERN_ERR
"os_set_fd_async : Failed to fcntl F_SETOWN "
419 "(or F_SETSIG) fd %d, errno = %d\n", fd
, errno
);
426 int os_clear_fd_async(int fd
)
430 flags
= fcntl(fd
, F_GETFL
);
434 flags
&= ~(O_ASYNC
| O_NONBLOCK
);
435 if (fcntl(fd
, F_SETFL
, flags
) < 0)
440 int os_set_fd_block(int fd
, int blocking
)
444 flags
= fcntl(fd
, F_GETFL
);
449 flags
&= ~O_NONBLOCK
;
453 if (fcntl(fd
, F_SETFL
, flags
) < 0)
459 int os_accept_connection(int fd
)
463 new = accept(fd
, NULL
, 0);
481 int os_shutdown_socket(int fd
, int r
, int w
)
494 err
= shutdown(fd
, what
);
500 int os_rcv_fd(int fd
, int *helper_pid_out
)
503 char buf
[CMSG_SPACE(sizeof(new))];
505 struct cmsghdr
*cmsg
;
510 iov
= ((struct iovec
) { .iov_base
= helper_pid_out
,
511 .iov_len
= sizeof(*helper_pid_out
) });
514 msg
.msg_control
= buf
;
515 msg
.msg_controllen
= sizeof(buf
);
518 n
= recvmsg(fd
, &msg
, 0);
521 else if (n
!= iov
.iov_len
)
522 *helper_pid_out
= -1;
524 cmsg
= CMSG_FIRSTHDR(&msg
);
526 printk(UM_KERN_ERR
"rcv_fd didn't receive anything, "
527 "error = %d\n", errno
);
530 if ((cmsg
->cmsg_level
!= SOL_SOCKET
) ||
531 (cmsg
->cmsg_type
!= SCM_RIGHTS
)) {
532 printk(UM_KERN_ERR
"rcv_fd didn't receive a descriptor\n");
536 new = ((int *) CMSG_DATA(cmsg
))[0];
540 int os_create_unix_socket(const char *file
, int len
, int close_on_exec
)
542 struct sockaddr_un addr
;
545 sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
550 err
= os_set_exec_close(sock
);
552 printk(UM_KERN_ERR
"create_unix_socket : "
553 "close_on_exec failed, err = %d", -err
);
556 addr
.sun_family
= AF_UNIX
;
558 snprintf(addr
.sun_path
, len
, "%s", file
);
560 err
= bind(sock
, (struct sockaddr
*) &addr
, sizeof(addr
));
567 void os_flush_stdout(void)
572 int os_lock_file(int fd
, int excl
)
574 int type
= excl
? F_WRLCK
: F_RDLCK
;
575 struct flock lock
= ((struct flock
) { .l_type
= type
,
576 .l_whence
= SEEK_SET
,
581 err
= fcntl(fd
, F_SETLK
, &lock
);
586 err
= fcntl(fd
, F_GETLK
, &lock
);
592 printk(UM_KERN_ERR
"F_SETLK failed, file already locked by pid %d\n",
599 unsigned os_major(unsigned long long dev
)
604 unsigned os_minor(unsigned long long dev
)
609 unsigned long long os_makedev(unsigned major
, unsigned minor
)
611 return makedev(major
, minor
);
614 int os_falloc_punch(int fd
, unsigned long long offset
, int len
)
616 int n
= fallocate(fd
, FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
, offset
, len
);