2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
13 #include <linux/kdev_t.h>
14 #include <sys/types.h>
16 #include <sys/socket.h>
18 #include <sys/ioctl.h>
19 #include <sys/mount.h>
21 #include <sys/utsname.h>
25 #include "kern_util.h"
27 static void copy_stat(struct uml_stat *dst, struct stat64 *src)
29 *dst = ((struct uml_stat) {
30 .ust_major = MAJOR(src->st_dev), /* device */
31 .ust_minor = MINOR(src->st_dev),
32 .ust_ino = src->st_ino, /* inode */
33 .ust_mode = src->st_mode, /* protection */
34 .ust_nlink = src->st_nlink, /* number of hard links */
35 .ust_uid = src->st_uid, /* user ID of owner */
36 .ust_gid = src->st_gid, /* group ID of owner */
37 .ust_size = src->st_size, /* total size, in bytes */
38 .ust_blksize = src->st_blksize, /* blocksize for filesys I/O */
39 .ust_blocks = src->st_blocks, /* number of blocks allocated */
40 .ust_atime = src->st_atime, /* time of last access */
41 .ust_mtime = src->st_mtime, /* time of last modification */
42 .ust_ctime = src->st_ctime, /* time of last change */
43 .ust_rmajor = MAJOR(src->st_rdev),
44 .ust_rminor = MINOR(src->st_rdev),
48 int os_stat_fd(const int fd, struct uml_stat *ubuf)
54 err = fstat64(fd, &sbuf);
55 } while((err < 0) && (errno == EINTR)) ;
61 copy_stat(ubuf, &sbuf);
65 int os_stat_file(const char *file_name, struct uml_stat *ubuf)
71 err = stat64(file_name, &sbuf);
72 } while((err < 0) && (errno == EINTR)) ;
78 copy_stat(ubuf, &sbuf);
82 int os_lstat_file(const char *file_name, struct uml_stat *ubuf)
88 err = lstat64(file_name, &sbuf);
89 } while((err < 0) && (errno == EINTR)) ;
95 copy_stat(ubuf, &sbuf);
99 int os_access(const char *file, int mode)
103 amode=(mode& OS_ACC_R_OK ? R_OK : 0) | (mode& OS_ACC_W_OK ? W_OK : 0) |
104 (mode& OS_ACC_X_OK ? X_OK : 0) | (mode& OS_ACC_F_OK ? F_OK : 0) ;
106 err = access(file, amode);
113 int os_set_file_time(const char *file, unsigned long access, unsigned long mod)
115 struct utimbuf buf = ((struct utimbuf){ .actime = access,
119 err = utime(file, &buf);
126 int os_set_file_perms(const char *file, int mode)
130 err = chmod(file, mode);
137 int os_set_file_owner(const char *file, int owner, int group)
141 err = chown(file, owner, group);
148 void os_print_error(int error, const char* str)
150 errno = error < 0 ? -error : error;
155 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
156 int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
160 err = ioctl(fd, cmd, arg);
167 int os_window_size(int fd, int *rows, int *cols)
171 if(ioctl(fd, TIOCGWINSZ, &size) < 0)
180 int os_new_tty_pgrp(int fd, int pid)
182 if(ioctl(fd, TIOCSCTTY, 0) < 0){
183 printk("TIOCSCTTY failed, errno = %d\n", errno);
187 if(tcsetpgrp(fd, pid) < 0){
188 printk("tcsetpgrp failed, errno = %d\n", errno);
195 /* FIXME: ensure namebuf in os_get_if_name is big enough */
196 int os_get_ifname(int fd, char* namebuf)
198 if(ioctl(fd, SIOCGIFNAME, namebuf) < 0)
204 int os_set_slip(int fd)
209 if(ioctl(fd, TIOCSETD, &disc) < 0){
210 printk("Failed to set slip line discipline - "
211 "errno = %d\n", errno);
216 if(ioctl(fd, SIOCSIFENCAP, &sencap) < 0){
217 printk("Failed to set slip encapsulation - "
218 "errno = %d\n", errno);
225 int os_set_owner(int fd, int pid)
227 if(fcntl(fd, F_SETOWN, pid) < 0){
228 int save_errno = errno;
230 if(fcntl(fd, F_GETOWN, 0) != pid)
237 /* FIXME? moved wholesale from sigio_user.c to get fcntls out of that file */
238 int os_sigio_async(int master, int slave)
242 flags = fcntl(master, F_GETFL);
244 printk("fcntl F_GETFL failed, errno = %d\n", errno);
248 if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
249 (fcntl(master, F_SETOWN, os_getpid()) < 0)){
250 printk("fcntl F_SETFL or F_SETOWN failed, errno = %d\n",
255 if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0)){
256 printk("fcntl F_SETFL failed, errno = %d\n", errno);
263 int os_mode_fd(int fd, int mode)
268 err = fchmod(fd, mode);
269 } while((err < 0) && (errno==EINTR)) ;
277 int os_file_type(char *file)
282 err = os_lstat_file(file, &buf);
286 if(S_ISDIR(buf.ust_mode)) return(OS_TYPE_DIR);
287 else if(S_ISLNK(buf.ust_mode)) return(OS_TYPE_SYMLINK);
288 else if(S_ISCHR(buf.ust_mode)) return(OS_TYPE_CHARDEV);
289 else if(S_ISBLK(buf.ust_mode)) return(OS_TYPE_BLOCKDEV);
290 else if(S_ISFIFO(buf.ust_mode)) return(OS_TYPE_FIFO);
291 else if(S_ISSOCK(buf.ust_mode)) return(OS_TYPE_SOCK);
292 else return(OS_TYPE_FILE);
295 int os_file_mode(char *file, struct openflags *mode_out)
299 *mode_out = OPENFLAGS();
301 err = os_access(file, OS_ACC_W_OK);
302 if((err < 0) && (err != -EACCES))
305 *mode_out = of_write(*mode_out);
307 err = os_access(file, OS_ACC_R_OK);
308 if((err < 0) && (err != -EACCES))
311 *mode_out = of_read(*mode_out);
316 int os_open_file(char *file, struct openflags flags, int mode)
320 if(flags.r && flags.w) f = O_RDWR;
321 else if(flags.r) f = O_RDONLY;
322 else if(flags.w) f = O_WRONLY;
325 if(flags.s) f |= O_SYNC;
326 if(flags.c) f |= O_CREAT;
327 if(flags.t) f |= O_TRUNC;
328 if(flags.e) f |= O_EXCL;
329 if(flags.d) f |= O_DIRECT;
331 fd = open64(file, f, mode);
335 if(flags.cl && fcntl(fd, F_SETFD, 1)){
343 void *os_open_dir(char *path, int *err_out)
352 int os_seek_dir(void *stream, unsigned long long pos)
354 seekdir(stream, pos);
358 int os_read_dir(void *stream, unsigned long long *ino_out, char **name_out)
363 ent = readdir(stream);
371 *ino_out = ent->d_ino;
372 *name_out = ent->d_name;
376 int os_tell_dir(void *stream)
378 return(telldir(stream));
381 int os_close_dir(void *stream)
385 err = closedir(stream);
391 int os_remove_file(const char *file)
402 int os_move_file(const char *from, const char *to)
406 err = rename(from, to);
413 int os_truncate_fd(int fd, unsigned long long len)
417 err = ftruncate(fd, len);
423 int os_truncate_file(const char *file, unsigned long long len)
427 err = truncate(file, len);
433 int os_connect_socket(char *name)
435 struct sockaddr_un sock;
438 sock.sun_family = AF_UNIX;
439 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
441 fd = socket(AF_UNIX, SOCK_STREAM, 0);
445 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
452 void os_close_file(int fd)
457 int os_seek_file(int fd, __u64 offset)
461 actual = lseek64(fd, offset, SEEK_SET);
467 static int fault_buffer(void *start, int len,
468 int (*copy_proc)(void *addr, void *buf, int len))
470 int page = getpagesize(), i;
473 for(i = 0; i < len; i += page){
474 if((*copy_proc)(start + i, &c, sizeof(c)))
477 if((len % page) != 0){
478 if((*copy_proc)(start + len - 1, &c, sizeof(c)))
484 static int file_io(int fd, void *buf, int len,
485 int (*io_proc)(int fd, void *buf, int len),
486 int (*copy_user_proc)(void *addr, void *buf, int len))
491 n = (*io_proc)(fd, buf, len);
492 if((n < 0) && (errno == EFAULT)){
493 err = fault_buffer(buf, len, copy_user_proc);
496 n = (*io_proc)(fd, buf, len);
498 } while((n < 0) && (errno == EINTR));
505 int os_read_file(int fd, void *buf, int len)
507 return(file_io(fd, buf, len, (int (*)(int, void *, int)) read,
508 copy_from_user_proc));
511 int os_write_file(int fd, const void *buf, int len)
513 return(file_io(fd, (void *) buf, len,
514 (int (*)(int, void *, int)) write, copy_to_user_proc));
517 int os_file_size(char *file, long long *size_out)
522 err = os_stat_file(file, &buf);
524 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
528 if(S_ISBLK(buf.ust_mode)){
531 fd = os_open_file(file, of_read(OPENFLAGS()), 0);
533 printk("Couldn't open \"%s\", errno = %d\n", file, -fd);
536 if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
537 printk("Couldn't get the block size of \"%s\", "
538 "errno = %d\n", file, errno);
543 *size_out = ((long long) blocks) * 512;
547 *size_out = buf.ust_size;
551 int os_fd_size(int fd, long long *size_out)
556 err = fstat(fd, &buf);
560 *size_out = buf.st_size;
564 int os_file_modtime(char *file, unsigned long *modtime)
569 err = os_stat_file(file, &buf);
571 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
575 *modtime = buf.ust_mtime;
579 int os_get_exec_close(int fd, int* close_on_exec)
584 ret = fcntl(fd, F_GETFD);
585 } while((ret < 0) && (errno == EINTR)) ;
590 *close_on_exec = (ret&FD_CLOEXEC) ? 1 : 0;
594 int os_set_exec_close(int fd, int close_on_exec)
598 if(close_on_exec) flag = FD_CLOEXEC;
602 err = fcntl(fd, F_SETFD, flag);
603 } while((err < 0) && (errno == EINTR)) ;
610 int os_pipe(int *fds, int stream, int close_on_exec)
612 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
614 err = socketpair(AF_UNIX, type, 0, fds);
621 err = os_set_exec_close(fds[0], 1);
625 err = os_set_exec_close(fds[1], 1);
632 printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err);
633 os_close_file(fds[1]);
634 os_close_file(fds[0]);
638 int os_set_fd_async(int fd, int owner)
640 /* XXX This should do F_GETFL first */
641 if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){
642 printk("os_set_fd_async : failed to set O_ASYNC and "
643 "O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
647 if(fcntl(fd, F_SETFD, 1) < 0){
648 printk("os_set_fd_async : Setting FD_CLOEXEC failed, "
649 "errno = %d\n", errno);
653 if((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
654 (fcntl(fd, F_SETOWN, owner) < 0)){
655 printk("os_set_fd_async : Failed to fcntl F_SETOWN "
656 "(or F_SETSIG) fd %d to pid %d, errno = %d\n", fd,
664 int os_clear_fd_async(int fd)
666 int flags = fcntl(fd, F_GETFL);
668 flags &= ~(O_ASYNC | O_NONBLOCK);
669 if(fcntl(fd, F_SETFL, flags) < 0)
674 int os_set_fd_block(int fd, int blocking)
678 flags = fcntl(fd, F_GETFL);
680 if(blocking) flags &= ~O_NONBLOCK;
681 else flags |= O_NONBLOCK;
683 if(fcntl(fd, F_SETFL, flags) < 0){
684 printk("Failed to change blocking on fd # %d, errno = %d\n",
691 int os_accept_connection(int fd)
695 new = accept(fd, NULL, 0);
713 int os_shutdown_socket(int fd, int r, int w)
717 if(r && w) what = SHUT_RDWR;
718 else if(r) what = SHUT_RD;
719 else if(w) what = SHUT_WR;
721 printk("os_shutdown_socket : neither r or w was set\n");
724 err = shutdown(fd, what);
730 int os_rcv_fd(int fd, int *helper_pid_out)
733 char buf[CMSG_SPACE(sizeof(new))];
735 struct cmsghdr *cmsg;
740 iov = ((struct iovec) { .iov_base = helper_pid_out,
741 .iov_len = sizeof(*helper_pid_out) });
744 msg.msg_control = buf;
745 msg.msg_controllen = sizeof(buf);
748 n = recvmsg(fd, &msg, 0);
752 else if(n != sizeof(iov.iov_len))
753 *helper_pid_out = -1;
755 cmsg = CMSG_FIRSTHDR(&msg);
757 printk("rcv_fd didn't receive anything, error = %d\n", errno);
760 if((cmsg->cmsg_level != SOL_SOCKET) ||
761 (cmsg->cmsg_type != SCM_RIGHTS)){
762 printk("rcv_fd didn't receive a descriptor\n");
766 new = ((int *) CMSG_DATA(cmsg))[0];
770 int os_create_unix_socket(char *file, int len, int close_on_exec)
772 struct sockaddr_un addr;
775 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
777 printk("create_unix_socket - socket failed, errno = %d\n",
783 err = os_set_exec_close(sock, 1);
785 printk("create_unix_socket : close_on_exec failed, "
789 addr.sun_family = AF_UNIX;
791 /* XXX Be more careful about overflow */
792 snprintf(addr.sun_path, len, "%s", file);
794 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
796 printk("create_listening_socket at '%s' - bind failed, "
797 "errno = %d\n", file, errno);
804 int os_make_symlink(const char *to, const char *from)
808 err = symlink(to, from);
815 int os_read_symlink(const char *file, char *buf, int size)
819 err = readlink(file, buf, size);
826 int os_link_file(const char *to, const char *from)
830 err = link(to, from);
837 int os_make_dir(const char *dir, int mode)
841 err = mkdir(dir, mode);
848 int os_make_dev(const char *name, int mode, int major, int minor)
852 err = mknod(name, mode, MKDEV(major, minor));
859 int os_remove_dir(const char *dir)
870 void os_flush_stdout(void)
875 int os_lock_file(int fd, int excl)
877 int type = excl ? F_WRLCK : F_RDLCK;
878 struct flock lock = ((struct flock) { .l_type = type,
879 .l_whence = SEEK_SET,
884 err = fcntl(fd, F_SETLK, &lock);
889 err = fcntl(fd, F_GETLK, &lock);
895 printk("F_SETLK failed, file already locked by pid %d\n", lock.l_pid);
901 int os_stat_filesystem(char *path, long *bsize_out, long long *blocks_out,
902 long long *bfree_out, long long *bavail_out,
903 long long *files_out, long long *ffree_out,
904 void *fsid_out, int fsid_size, long *namelen_out,
910 err = statfs64(path, &buf);
914 *bsize_out = buf.f_bsize;
915 *blocks_out = buf.f_blocks;
916 *bfree_out = buf.f_bfree;
917 *bavail_out = buf.f_bavail;
918 *files_out = buf.f_files;
919 *ffree_out = buf.f_ffree;
920 memcpy(fsid_out, &buf.f_fsid,
921 sizeof(buf.f_fsid) > fsid_size ? fsid_size :
923 *namelen_out = buf.f_namelen;
924 spare_out[0] = buf.f_spare[0];
925 spare_out[1] = buf.f_spare[1];
926 spare_out[2] = buf.f_spare[2];
927 spare_out[3] = buf.f_spare[3];
928 spare_out[4] = buf.f_spare[4];
929 spare_out[5] = buf.f_spare[5];
934 * Overrides for Emacs so that we follow Linus's tabbing style.
935 * Emacs will notice this stuff at the end of the file and automatically
936 * adjust the settings for this buffer only. This must remain at the end
938 * ---------------------------------------------------------------------------
940 * c-file-style: "linux"