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>
16 #include <sys/types.h>
19 static void copy_stat(struct uml_stat
*dst
, const struct stat64
*src
)
21 *dst
= ((struct uml_stat
) {
22 .ust_dev
= src
->st_dev
, /* device */
23 .ust_ino
= src
->st_ino
, /* inode */
24 .ust_mode
= src
->st_mode
, /* protection */
25 .ust_nlink
= src
->st_nlink
, /* number of hard links */
26 .ust_uid
= src
->st_uid
, /* user ID of owner */
27 .ust_gid
= src
->st_gid
, /* group ID of owner */
28 .ust_size
= src
->st_size
, /* total size, in bytes */
29 .ust_blksize
= src
->st_blksize
, /* blocksize for filesys I/O */
30 .ust_blocks
= src
->st_blocks
, /* number of blocks allocated */
31 .ust_atime
= src
->st_atime
, /* time of last access */
32 .ust_mtime
= src
->st_mtime
, /* time of last modification */
33 .ust_ctime
= src
->st_ctime
, /* time of last change */
37 int os_stat_fd(const int fd
, struct uml_stat
*ubuf
)
42 CATCH_EINTR(err
= fstat64(fd
, &sbuf
));
47 copy_stat(ubuf
, &sbuf
);
51 int os_stat_file(const char *file_name
, struct uml_stat
*ubuf
)
56 CATCH_EINTR(err
= stat64(file_name
, &sbuf
));
61 copy_stat(ubuf
, &sbuf
);
65 int os_access(const char *file
, int mode
)
69 amode
= (mode
& OS_ACC_R_OK
? R_OK
: 0) |
70 (mode
& OS_ACC_W_OK
? W_OK
: 0) |
71 (mode
& OS_ACC_X_OK
? X_OK
: 0) |
72 (mode
& OS_ACC_F_OK
? F_OK
: 0);
74 err
= access(file
, amode
);
81 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
82 int os_ioctl_generic(int fd
, unsigned int cmd
, unsigned long arg
)
86 err
= ioctl(fd
, cmd
, arg
);
93 /* FIXME: ensure namebuf in os_get_if_name is big enough */
94 int os_get_ifname(int fd
, char* namebuf
)
96 if (ioctl(fd
, SIOCGIFNAME
, namebuf
) < 0)
102 int os_set_slip(int fd
)
107 if (ioctl(fd
, TIOCSETD
, &disc
) < 0)
111 if (ioctl(fd
, SIOCSIFENCAP
, &sencap
) < 0)
117 int os_mode_fd(int fd
, int mode
)
121 CATCH_EINTR(err
= fchmod(fd
, mode
));
128 int os_file_type(char *file
)
133 err
= os_stat_file(file
, &buf
);
137 if (S_ISDIR(buf
.ust_mode
))
139 else if (S_ISLNK(buf
.ust_mode
))
140 return OS_TYPE_SYMLINK
;
141 else if (S_ISCHR(buf
.ust_mode
))
142 return OS_TYPE_CHARDEV
;
143 else if (S_ISBLK(buf
.ust_mode
))
144 return OS_TYPE_BLOCKDEV
;
145 else if (S_ISFIFO(buf
.ust_mode
))
147 else if (S_ISSOCK(buf
.ust_mode
))
149 else return OS_TYPE_FILE
;
152 int os_file_mode(const char *file
, struct openflags
*mode_out
)
156 *mode_out
= OPENFLAGS();
158 err
= access(file
, W_OK
);
159 if (err
&& (errno
!= EACCES
))
162 *mode_out
= of_write(*mode_out
);
164 err
= access(file
, R_OK
);
165 if (err
&& (errno
!= EACCES
))
168 *mode_out
= of_read(*mode_out
);
173 int os_open_file(const char *file
, struct openflags flags
, int mode
)
177 if (flags
.r
&& flags
.w
)
196 fd
= open64(file
, f
, mode
);
200 if (flags
.cl
&& fcntl(fd
, F_SETFD
, 1)) {
209 int os_connect_socket(const char *name
)
211 struct sockaddr_un sock
;
214 sock
.sun_family
= AF_UNIX
;
215 snprintf(sock
.sun_path
, sizeof(sock
.sun_path
), "%s", name
);
217 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
223 err
= connect(fd
, (struct sockaddr
*) &sock
, sizeof(sock
));
237 void os_close_file(int fd
)
241 int os_fsync_file(int fd
)
248 int os_seek_file(int fd
, unsigned long long offset
)
250 unsigned long long actual
;
252 actual
= lseek64(fd
, offset
, SEEK_SET
);
253 if (actual
!= offset
)
258 int os_read_file(int fd
, void *buf
, int len
)
260 int n
= read(fd
, buf
, len
);
267 int os_pread_file(int fd
, void *buf
, int len
, unsigned long long offset
)
269 int n
= pread(fd
, buf
, len
, offset
);
276 int os_write_file(int fd
, const void *buf
, int len
)
278 int n
= write(fd
, (void *) buf
, len
);
285 int os_sync_file(int fd
)
294 int os_pwrite_file(int fd
, const void *buf
, int len
, unsigned long long offset
)
296 int n
= pwrite(fd
, (void *) buf
, len
, offset
);
304 int os_file_size(const char *file
, unsigned long long *size_out
)
309 err
= os_stat_file(file
, &buf
);
311 printk(UM_KERN_ERR
"Couldn't stat \"%s\" : err = %d\n", file
,
316 if (S_ISBLK(buf
.ust_mode
)) {
320 fd
= open(file
, O_RDONLY
, 0);
323 printk(UM_KERN_ERR
"Couldn't open \"%s\", "
324 "errno = %d\n", file
, errno
);
327 if (ioctl(fd
, BLKGETSIZE
, &blocks
) < 0) {
329 printk(UM_KERN_ERR
"Couldn't get the block size of "
330 "\"%s\", errno = %d\n", file
, errno
);
334 *size_out
= ((long long) blocks
) * 512;
337 else *size_out
= buf
.ust_size
;
342 int os_file_modtime(const char *file
, unsigned long *modtime
)
347 err
= os_stat_file(file
, &buf
);
349 printk(UM_KERN_ERR
"Couldn't stat \"%s\" : err = %d\n", file
,
354 *modtime
= buf
.ust_mtime
;
358 int os_set_exec_close(int fd
)
362 CATCH_EINTR(err
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
));
369 int os_pipe(int *fds
, int stream
, int close_on_exec
)
371 int err
, type
= stream
? SOCK_STREAM
: SOCK_DGRAM
;
373 err
= socketpair(AF_UNIX
, type
, 0, fds
);
380 err
= os_set_exec_close(fds
[0]);
384 err
= os_set_exec_close(fds
[1]);
391 printk(UM_KERN_ERR
"os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
398 int os_set_fd_async(int fd
)
402 flags
= fcntl(fd
, F_GETFL
);
406 flags
|= O_ASYNC
| O_NONBLOCK
;
407 if (fcntl(fd
, F_SETFL
, flags
) < 0) {
409 printk(UM_KERN_ERR
"os_set_fd_async : failed to set O_ASYNC "
410 "and O_NONBLOCK on fd # %d, errno = %d\n", fd
, errno
);
414 if ((fcntl(fd
, F_SETSIG
, SIGIO
) < 0) ||
415 (fcntl(fd
, F_SETOWN
, os_getpid()) < 0)) {
417 printk(UM_KERN_ERR
"os_set_fd_async : Failed to fcntl F_SETOWN "
418 "(or F_SETSIG) fd %d, errno = %d\n", fd
, errno
);
425 int os_clear_fd_async(int fd
)
429 flags
= fcntl(fd
, F_GETFL
);
433 flags
&= ~(O_ASYNC
| O_NONBLOCK
);
434 if (fcntl(fd
, F_SETFL
, flags
) < 0)
439 int os_set_fd_block(int fd
, int blocking
)
443 flags
= fcntl(fd
, F_GETFL
);
448 flags
&= ~O_NONBLOCK
;
452 if (fcntl(fd
, F_SETFL
, flags
) < 0)
458 int os_accept_connection(int fd
)
462 new = accept(fd
, NULL
, 0);
480 int os_shutdown_socket(int fd
, int r
, int w
)
493 err
= shutdown(fd
, what
);
499 int os_rcv_fd(int fd
, int *helper_pid_out
)
502 char buf
[CMSG_SPACE(sizeof(new))];
504 struct cmsghdr
*cmsg
;
509 iov
= ((struct iovec
) { .iov_base
= helper_pid_out
,
510 .iov_len
= sizeof(*helper_pid_out
) });
513 msg
.msg_control
= buf
;
514 msg
.msg_controllen
= sizeof(buf
);
517 n
= recvmsg(fd
, &msg
, 0);
520 else if (n
!= iov
.iov_len
)
521 *helper_pid_out
= -1;
523 cmsg
= CMSG_FIRSTHDR(&msg
);
525 printk(UM_KERN_ERR
"rcv_fd didn't receive anything, "
526 "error = %d\n", errno
);
529 if ((cmsg
->cmsg_level
!= SOL_SOCKET
) ||
530 (cmsg
->cmsg_type
!= SCM_RIGHTS
)) {
531 printk(UM_KERN_ERR
"rcv_fd didn't receive a descriptor\n");
535 new = ((int *) CMSG_DATA(cmsg
))[0];
539 int os_create_unix_socket(const char *file
, int len
, int close_on_exec
)
541 struct sockaddr_un addr
;
544 sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
549 err
= os_set_exec_close(sock
);
551 printk(UM_KERN_ERR
"create_unix_socket : "
552 "close_on_exec failed, err = %d", -err
);
555 addr
.sun_family
= AF_UNIX
;
557 snprintf(addr
.sun_path
, len
, "%s", file
);
559 err
= bind(sock
, (struct sockaddr
*) &addr
, sizeof(addr
));
566 void os_flush_stdout(void)
571 int os_lock_file(int fd
, int excl
)
573 int type
= excl
? F_WRLCK
: F_RDLCK
;
574 struct flock lock
= ((struct flock
) { .l_type
= type
,
575 .l_whence
= SEEK_SET
,
580 err
= fcntl(fd
, F_SETLK
, &lock
);
585 err
= fcntl(fd
, F_GETLK
, &lock
);
591 printk(UM_KERN_ERR
"F_SETLK failed, file already locked by pid %d\n",
598 unsigned os_major(unsigned long long dev
)
603 unsigned os_minor(unsigned long long dev
)
608 unsigned long long os_makedev(unsigned major
, unsigned minor
)
610 return makedev(major
, minor
);