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>
18 static void copy_stat(struct uml_stat
*dst
, const struct stat64
*src
)
20 *dst
= ((struct uml_stat
) {
21 .ust_dev
= src
->st_dev
, /* device */
22 .ust_ino
= src
->st_ino
, /* inode */
23 .ust_mode
= src
->st_mode
, /* protection */
24 .ust_nlink
= src
->st_nlink
, /* number of hard links */
25 .ust_uid
= src
->st_uid
, /* user ID of owner */
26 .ust_gid
= src
->st_gid
, /* group ID of owner */
27 .ust_size
= src
->st_size
, /* total size, in bytes */
28 .ust_blksize
= src
->st_blksize
, /* blocksize for filesys I/O */
29 .ust_blocks
= src
->st_blocks
, /* number of blocks allocated */
30 .ust_atime
= src
->st_atime
, /* time of last access */
31 .ust_mtime
= src
->st_mtime
, /* time of last modification */
32 .ust_ctime
= src
->st_ctime
, /* time of last change */
36 int os_stat_fd(const int fd
, struct uml_stat
*ubuf
)
41 CATCH_EINTR(err
= fstat64(fd
, &sbuf
));
46 copy_stat(ubuf
, &sbuf
);
50 int os_stat_file(const char *file_name
, struct uml_stat
*ubuf
)
55 CATCH_EINTR(err
= stat64(file_name
, &sbuf
));
60 copy_stat(ubuf
, &sbuf
);
64 int os_access(const char *file
, int mode
)
68 amode
= (mode
& OS_ACC_R_OK
? R_OK
: 0) |
69 (mode
& OS_ACC_W_OK
? W_OK
: 0) |
70 (mode
& OS_ACC_X_OK
? X_OK
: 0) |
71 (mode
& OS_ACC_F_OK
? F_OK
: 0);
73 err
= access(file
, amode
);
80 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
81 int os_ioctl_generic(int fd
, unsigned int cmd
, unsigned long arg
)
85 err
= ioctl(fd
, cmd
, arg
);
92 /* FIXME: ensure namebuf in os_get_if_name is big enough */
93 int os_get_ifname(int fd
, char* namebuf
)
95 if (ioctl(fd
, SIOCGIFNAME
, namebuf
) < 0)
101 int os_set_slip(int fd
)
106 if (ioctl(fd
, TIOCSETD
, &disc
) < 0)
110 if (ioctl(fd
, SIOCSIFENCAP
, &sencap
) < 0)
116 int os_mode_fd(int fd
, int mode
)
120 CATCH_EINTR(err
= fchmod(fd
, mode
));
127 int os_file_type(char *file
)
132 err
= os_stat_file(file
, &buf
);
136 if (S_ISDIR(buf
.ust_mode
))
138 else if (S_ISLNK(buf
.ust_mode
))
139 return OS_TYPE_SYMLINK
;
140 else if (S_ISCHR(buf
.ust_mode
))
141 return OS_TYPE_CHARDEV
;
142 else if (S_ISBLK(buf
.ust_mode
))
143 return OS_TYPE_BLOCKDEV
;
144 else if (S_ISFIFO(buf
.ust_mode
))
146 else if (S_ISSOCK(buf
.ust_mode
))
148 else return OS_TYPE_FILE
;
151 int os_file_mode(const char *file
, struct openflags
*mode_out
)
155 *mode_out
= OPENFLAGS();
157 err
= access(file
, W_OK
);
158 if (err
&& (errno
!= EACCES
))
161 *mode_out
= of_write(*mode_out
);
163 err
= access(file
, R_OK
);
164 if (err
&& (errno
!= EACCES
))
167 *mode_out
= of_read(*mode_out
);
172 int os_open_file(const char *file
, struct openflags flags
, int mode
)
176 if (flags
.r
&& flags
.w
)
195 fd
= open64(file
, f
, mode
);
199 if (flags
.cl
&& fcntl(fd
, F_SETFD
, 1)) {
208 int os_connect_socket(const char *name
)
210 struct sockaddr_un sock
;
213 sock
.sun_family
= AF_UNIX
;
214 snprintf(sock
.sun_path
, sizeof(sock
.sun_path
), "%s", name
);
216 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
222 err
= connect(fd
, (struct sockaddr
*) &sock
, sizeof(sock
));
236 void os_close_file(int fd
)
240 int os_fsync_file(int fd
)
247 int os_seek_file(int fd
, unsigned long long offset
)
249 unsigned long long actual
;
251 actual
= lseek64(fd
, offset
, SEEK_SET
);
252 if (actual
!= offset
)
257 int os_read_file(int fd
, void *buf
, int len
)
259 int n
= read(fd
, buf
, len
);
266 int os_write_file(int fd
, const void *buf
, int len
)
268 int n
= write(fd
, (void *) buf
, len
);
275 int os_sync_file(int fd
)
284 int os_file_size(const char *file
, unsigned long long *size_out
)
289 err
= os_stat_file(file
, &buf
);
291 printk(UM_KERN_ERR
"Couldn't stat \"%s\" : err = %d\n", file
,
296 if (S_ISBLK(buf
.ust_mode
)) {
300 fd
= open(file
, O_RDONLY
, 0);
303 printk(UM_KERN_ERR
"Couldn't open \"%s\", "
304 "errno = %d\n", file
, errno
);
307 if (ioctl(fd
, BLKGETSIZE
, &blocks
) < 0) {
309 printk(UM_KERN_ERR
"Couldn't get the block size of "
310 "\"%s\", errno = %d\n", file
, errno
);
314 *size_out
= ((long long) blocks
) * 512;
317 else *size_out
= buf
.ust_size
;
322 int os_file_modtime(const char *file
, unsigned long *modtime
)
327 err
= os_stat_file(file
, &buf
);
329 printk(UM_KERN_ERR
"Couldn't stat \"%s\" : err = %d\n", file
,
334 *modtime
= buf
.ust_mtime
;
338 int os_set_exec_close(int fd
)
342 CATCH_EINTR(err
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
));
349 int os_pipe(int *fds
, int stream
, int close_on_exec
)
351 int err
, type
= stream
? SOCK_STREAM
: SOCK_DGRAM
;
353 err
= socketpair(AF_UNIX
, type
, 0, fds
);
360 err
= os_set_exec_close(fds
[0]);
364 err
= os_set_exec_close(fds
[1]);
371 printk(UM_KERN_ERR
"os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
378 int os_set_fd_async(int fd
)
382 flags
= fcntl(fd
, F_GETFL
);
386 flags
|= O_ASYNC
| O_NONBLOCK
;
387 if (fcntl(fd
, F_SETFL
, flags
) < 0) {
389 printk(UM_KERN_ERR
"os_set_fd_async : failed to set O_ASYNC "
390 "and O_NONBLOCK on fd # %d, errno = %d\n", fd
, errno
);
394 if ((fcntl(fd
, F_SETSIG
, SIGIO
) < 0) ||
395 (fcntl(fd
, F_SETOWN
, os_getpid()) < 0)) {
397 printk(UM_KERN_ERR
"os_set_fd_async : Failed to fcntl F_SETOWN "
398 "(or F_SETSIG) fd %d, errno = %d\n", fd
, errno
);
405 int os_clear_fd_async(int fd
)
409 flags
= fcntl(fd
, F_GETFL
);
413 flags
&= ~(O_ASYNC
| O_NONBLOCK
);
414 if (fcntl(fd
, F_SETFL
, flags
) < 0)
419 int os_set_fd_block(int fd
, int blocking
)
423 flags
= fcntl(fd
, F_GETFL
);
428 flags
&= ~O_NONBLOCK
;
432 if (fcntl(fd
, F_SETFL
, flags
) < 0)
438 int os_accept_connection(int fd
)
442 new = accept(fd
, NULL
, 0);
460 int os_shutdown_socket(int fd
, int r
, int w
)
473 err
= shutdown(fd
, what
);
479 int os_rcv_fd(int fd
, int *helper_pid_out
)
482 char buf
[CMSG_SPACE(sizeof(new))];
484 struct cmsghdr
*cmsg
;
489 iov
= ((struct iovec
) { .iov_base
= helper_pid_out
,
490 .iov_len
= sizeof(*helper_pid_out
) });
493 msg
.msg_control
= buf
;
494 msg
.msg_controllen
= sizeof(buf
);
497 n
= recvmsg(fd
, &msg
, 0);
500 else if (n
!= iov
.iov_len
)
501 *helper_pid_out
= -1;
503 cmsg
= CMSG_FIRSTHDR(&msg
);
505 printk(UM_KERN_ERR
"rcv_fd didn't receive anything, "
506 "error = %d\n", errno
);
509 if ((cmsg
->cmsg_level
!= SOL_SOCKET
) ||
510 (cmsg
->cmsg_type
!= SCM_RIGHTS
)) {
511 printk(UM_KERN_ERR
"rcv_fd didn't receive a descriptor\n");
515 new = ((int *) CMSG_DATA(cmsg
))[0];
519 int os_create_unix_socket(const char *file
, int len
, int close_on_exec
)
521 struct sockaddr_un addr
;
524 sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
529 err
= os_set_exec_close(sock
);
531 printk(UM_KERN_ERR
"create_unix_socket : "
532 "close_on_exec failed, err = %d", -err
);
535 addr
.sun_family
= AF_UNIX
;
537 snprintf(addr
.sun_path
, len
, "%s", file
);
539 err
= bind(sock
, (struct sockaddr
*) &addr
, sizeof(addr
));
546 void os_flush_stdout(void)
551 int os_lock_file(int fd
, int excl
)
553 int type
= excl
? F_WRLCK
: F_RDLCK
;
554 struct flock lock
= ((struct flock
) { .l_type
= type
,
555 .l_whence
= SEEK_SET
,
560 err
= fcntl(fd
, F_SETLK
, &lock
);
565 err
= fcntl(fd
, F_GETLK
, &lock
);
571 printk(UM_KERN_ERR
"F_SETLK failed, file already locked by pid %d\n",
578 unsigned os_major(unsigned long long dev
)
583 unsigned os_minor(unsigned long long dev
)
588 unsigned long long os_makedev(unsigned major
, unsigned minor
)
590 return makedev(major
, minor
);