2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
11 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <sys/ioctl.h>
16 #include <sys/mount.h>
20 #include "kern_util.h"
21 #include "user_util.h"
23 static void copy_stat(struct uml_stat
*dst
, struct stat64
*src
)
25 *dst
= ((struct uml_stat
) {
26 .ust_dev
= src
->st_dev
, /* device */
27 .ust_ino
= src
->st_ino
, /* inode */
28 .ust_mode
= src
->st_mode
, /* protection */
29 .ust_nlink
= src
->st_nlink
, /* number of hard links */
30 .ust_uid
= src
->st_uid
, /* user ID of owner */
31 .ust_gid
= src
->st_gid
, /* group ID of owner */
32 .ust_size
= src
->st_size
, /* total size, in bytes */
33 .ust_blksize
= src
->st_blksize
, /* blocksize for filesys I/O */
34 .ust_blocks
= src
->st_blocks
, /* number of blocks allocated */
35 .ust_atime
= src
->st_atime
, /* time of last access */
36 .ust_mtime
= src
->st_mtime
, /* time of last modification */
37 .ust_ctime
= src
->st_ctime
, /* time of last change */
41 int os_stat_fd(const int fd
, struct uml_stat
*ubuf
)
46 CATCH_EINTR(err
= fstat64(fd
, &sbuf
));
51 copy_stat(ubuf
, &sbuf
);
55 int os_stat_file(const char *file_name
, struct uml_stat
*ubuf
)
61 err
= stat64(file_name
, &sbuf
);
62 } while((err
< 0) && (errno
== EINTR
)) ;
68 copy_stat(ubuf
, &sbuf
);
72 int os_access(const char* file
, int mode
)
76 amode
=(mode
&OS_ACC_R_OK
? R_OK
: 0) | (mode
&OS_ACC_W_OK
? W_OK
: 0) |
77 (mode
&OS_ACC_X_OK
? X_OK
: 0) | (mode
&OS_ACC_F_OK
? F_OK
: 0) ;
79 err
= access(file
, amode
);
86 void os_print_error(int error
, const char* str
)
88 errno
= error
< 0 ? -error
: error
;
93 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
94 int os_ioctl_generic(int fd
, unsigned int cmd
, unsigned long arg
)
98 err
= ioctl(fd
, cmd
, arg
);
105 int os_window_size(int fd
, int *rows
, int *cols
)
109 if(ioctl(fd
, TIOCGWINSZ
, &size
) < 0)
118 int os_new_tty_pgrp(int fd
, int pid
)
120 if(ioctl(fd
, TIOCSCTTY
, 0) < 0)
123 if(tcsetpgrp(fd
, pid
) < 0)
129 /* FIXME: ensure namebuf in os_get_if_name is big enough */
130 int os_get_ifname(int fd
, char* namebuf
)
132 if(ioctl(fd
, SIOCGIFNAME
, namebuf
) < 0)
138 int os_set_slip(int fd
)
143 if(ioctl(fd
, TIOCSETD
, &disc
) < 0)
147 if(ioctl(fd
, SIOCSIFENCAP
, &sencap
) < 0)
153 int os_set_owner(int fd
, int pid
)
155 if(fcntl(fd
, F_SETOWN
, pid
) < 0){
156 int save_errno
= errno
;
158 if(fcntl(fd
, F_GETOWN
, 0) != pid
)
165 /* FIXME? moved wholesale from sigio_user.c to get fcntls out of that file */
166 int os_sigio_async(int master
, int slave
)
170 flags
= fcntl(master
, F_GETFL
);
174 if((fcntl(master
, F_SETFL
, flags
| O_NONBLOCK
| O_ASYNC
) < 0) ||
175 (fcntl(master
, F_SETOWN
, os_getpid()) < 0))
178 if((fcntl(slave
, F_SETFL
, flags
| O_NONBLOCK
) < 0))
184 int os_mode_fd(int fd
, int mode
)
189 err
= fchmod(fd
, mode
);
190 } while((err
< 0) && (errno
==EINTR
)) ;
198 int os_file_type(char *file
)
203 err
= os_stat_file(file
, &buf
);
207 if(S_ISDIR(buf
.ust_mode
))
209 else if(S_ISLNK(buf
.ust_mode
))
210 return OS_TYPE_SYMLINK
;
211 else if(S_ISCHR(buf
.ust_mode
))
212 return OS_TYPE_CHARDEV
;
213 else if(S_ISBLK(buf
.ust_mode
))
214 return OS_TYPE_BLOCKDEV
;
215 else if(S_ISFIFO(buf
.ust_mode
))
217 else if(S_ISSOCK(buf
.ust_mode
))
219 else return OS_TYPE_FILE
;
222 int os_file_mode(char *file
, struct openflags
*mode_out
)
226 *mode_out
= OPENFLAGS();
228 err
= os_access(file
, OS_ACC_W_OK
);
229 if((err
< 0) && (err
!= -EACCES
))
232 *mode_out
= of_write(*mode_out
);
234 err
= os_access(file
, OS_ACC_R_OK
);
235 if((err
< 0) && (err
!= -EACCES
))
238 *mode_out
= of_read(*mode_out
);
243 int os_open_file(char *file
, struct openflags flags
, int mode
)
247 if(flags
.r
&& flags
.w
) f
= O_RDWR
;
248 else if(flags
.r
) f
= O_RDONLY
;
249 else if(flags
.w
) f
= O_WRONLY
;
252 if(flags
.s
) f
|= O_SYNC
;
253 if(flags
.c
) f
|= O_CREAT
;
254 if(flags
.t
) f
|= O_TRUNC
;
255 if(flags
.e
) f
|= O_EXCL
;
257 fd
= open64(file
, f
, mode
);
261 if(flags
.cl
&& fcntl(fd
, F_SETFD
, 1)){
270 int os_connect_socket(char *name
)
272 struct sockaddr_un sock
;
275 sock
.sun_family
= AF_UNIX
;
276 snprintf(sock
.sun_path
, sizeof(sock
.sun_path
), "%s", name
);
278 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
284 err
= connect(fd
, (struct sockaddr
*) &sock
, sizeof(sock
));
298 void os_close_file(int fd
)
303 int os_seek_file(int fd
, __u64 offset
)
307 actual
= lseek64(fd
, offset
, SEEK_SET
);
313 static int fault_buffer(void *start
, int len
,
314 int (*copy_proc
)(void *addr
, void *buf
, int len
))
316 int page
= getpagesize(), i
;
319 for(i
= 0; i
< len
; i
+= page
){
320 if((*copy_proc
)(start
+ i
, &c
, sizeof(c
)))
323 if((len
% page
) != 0){
324 if((*copy_proc
)(start
+ len
- 1, &c
, sizeof(c
)))
330 static int file_io(int fd
, void *buf
, int len
,
331 int (*io_proc
)(int fd
, void *buf
, int len
),
332 int (*copy_user_proc
)(void *addr
, void *buf
, int len
))
337 n
= (*io_proc
)(fd
, buf
, len
);
338 if((n
< 0) && (errno
== EFAULT
)){
339 err
= fault_buffer(buf
, len
, copy_user_proc
);
342 n
= (*io_proc
)(fd
, buf
, len
);
344 } while((n
< 0) && (errno
== EINTR
));
351 int os_read_file(int fd
, void *buf
, int len
)
353 return file_io(fd
, buf
, len
, (int (*)(int, void *, int)) read
,
354 copy_from_user_proc
);
357 int os_write_file(int fd
, const void *buf
, int len
)
359 return file_io(fd
, (void *) buf
, len
,
360 (int (*)(int, void *, int)) write
, copy_to_user_proc
);
363 int os_file_size(char *file
, unsigned long long *size_out
)
368 err
= os_stat_file(file
, &buf
);
370 printk("Couldn't stat \"%s\" : err = %d\n", file
, -err
);
374 if(S_ISBLK(buf
.ust_mode
)){
377 fd
= os_open_file(file
, of_read(OPENFLAGS()), 0);
379 printk("Couldn't open \"%s\", errno = %d\n", file
, -fd
);
382 if(ioctl(fd
, BLKGETSIZE
, &blocks
) < 0){
384 printk("Couldn't get the block size of \"%s\", "
385 "errno = %d\n", file
, errno
);
389 *size_out
= ((long long) blocks
) * 512;
393 *size_out
= buf
.ust_size
;
397 int os_file_modtime(char *file
, unsigned long *modtime
)
402 err
= os_stat_file(file
, &buf
);
404 printk("Couldn't stat \"%s\" : err = %d\n", file
, -err
);
408 *modtime
= buf
.ust_mtime
;
412 int os_get_exec_close(int fd
, int* close_on_exec
)
417 ret
= fcntl(fd
, F_GETFD
);
418 } while((ret
< 0) && (errno
== EINTR
)) ;
423 *close_on_exec
= (ret
&FD_CLOEXEC
) ? 1 : 0;
427 int os_set_exec_close(int fd
, int close_on_exec
)
431 if(close_on_exec
) flag
= FD_CLOEXEC
;
435 err
= fcntl(fd
, F_SETFD
, flag
);
436 } while((err
< 0) && (errno
== EINTR
)) ;
443 int os_pipe(int *fds
, int stream
, int close_on_exec
)
445 int err
, type
= stream
? SOCK_STREAM
: SOCK_DGRAM
;
447 err
= socketpair(AF_UNIX
, type
, 0, fds
);
454 err
= os_set_exec_close(fds
[0], 1);
458 err
= os_set_exec_close(fds
[1], 1);
465 printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err
);
466 os_close_file(fds
[1]);
467 os_close_file(fds
[0]);
471 int os_set_fd_async(int fd
, int owner
)
475 /* XXX This should do F_GETFL first */
476 if(fcntl(fd
, F_SETFL
, O_ASYNC
| O_NONBLOCK
) < 0){
478 printk("os_set_fd_async : failed to set O_ASYNC and "
479 "O_NONBLOCK on fd # %d, errno = %d\n", fd
, errno
);
483 if(fcntl(fd
, F_SETFD
, 1) < 0){
484 printk("os_set_fd_async : Setting FD_CLOEXEC failed, "
485 "errno = %d\n", errno
);
489 if((fcntl(fd
, F_SETSIG
, SIGIO
) < 0) ||
490 (fcntl(fd
, F_SETOWN
, owner
) < 0)){
492 printk("os_set_fd_async : Failed to fcntl F_SETOWN "
493 "(or F_SETSIG) fd %d to pid %d, errno = %d\n", fd
,
501 int os_clear_fd_async(int fd
)
503 int flags
= fcntl(fd
, F_GETFL
);
505 flags
&= ~(O_ASYNC
| O_NONBLOCK
);
506 if(fcntl(fd
, F_SETFL
, flags
) < 0)
511 int os_set_fd_block(int fd
, int blocking
)
515 flags
= fcntl(fd
, F_GETFL
);
517 if(blocking
) flags
&= ~O_NONBLOCK
;
518 else flags
|= O_NONBLOCK
;
520 if(fcntl(fd
, F_SETFL
, flags
) < 0)
526 int os_accept_connection(int fd
)
530 new = accept(fd
, NULL
, 0);
548 int os_shutdown_socket(int fd
, int r
, int w
)
552 if(r
&& w
) what
= SHUT_RDWR
;
553 else if(r
) what
= SHUT_RD
;
554 else if(w
) what
= SHUT_WR
;
556 printk("os_shutdown_socket : neither r or w was set\n");
559 err
= shutdown(fd
, what
);
565 int os_rcv_fd(int fd
, int *helper_pid_out
)
568 char buf
[CMSG_SPACE(sizeof(new))];
570 struct cmsghdr
*cmsg
;
575 iov
= ((struct iovec
) { .iov_base
= helper_pid_out
,
576 .iov_len
= sizeof(*helper_pid_out
) });
579 msg
.msg_control
= buf
;
580 msg
.msg_controllen
= sizeof(buf
);
583 n
= recvmsg(fd
, &msg
, 0);
587 else if(n
!= sizeof(iov
.iov_len
))
588 *helper_pid_out
= -1;
590 cmsg
= CMSG_FIRSTHDR(&msg
);
592 printk("rcv_fd didn't receive anything, error = %d\n", errno
);
595 if((cmsg
->cmsg_level
!= SOL_SOCKET
) ||
596 (cmsg
->cmsg_type
!= SCM_RIGHTS
)){
597 printk("rcv_fd didn't receive a descriptor\n");
601 new = ((int *) CMSG_DATA(cmsg
))[0];
605 int os_create_unix_socket(char *file
, int len
, int close_on_exec
)
607 struct sockaddr_un addr
;
610 sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
615 err
= os_set_exec_close(sock
, 1);
617 printk("create_unix_socket : close_on_exec failed, "
621 addr
.sun_family
= AF_UNIX
;
623 /* XXX Be more careful about overflow */
624 snprintf(addr
.sun_path
, len
, "%s", file
);
626 err
= bind(sock
, (struct sockaddr
*) &addr
, sizeof(addr
));
633 void os_flush_stdout(void)
638 int os_lock_file(int fd
, int excl
)
640 int type
= excl
? F_WRLCK
: F_RDLCK
;
641 struct flock lock
= ((struct flock
) { .l_type
= type
,
642 .l_whence
= SEEK_SET
,
647 err
= fcntl(fd
, F_SETLK
, &lock
);
652 err
= fcntl(fd
, F_GETLK
, &lock
);
658 printk("F_SETLK failed, file already locked by pid %d\n", lock
.l_pid
);