2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
12 #include <sys/types.h>
14 #include <sys/socket.h>
16 #include <sys/ioctl.h>
17 #include <sys/mount.h>
21 #include "kern_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)
47 err = fstat64(fd, &sbuf);
48 } while((err < 0) && (errno == EINTR)) ;
54 copy_stat(ubuf, &sbuf);
58 int os_stat_file(const char *file_name, struct uml_stat *ubuf)
64 err = stat64(file_name, &sbuf);
65 } while((err < 0) && (errno == EINTR)) ;
71 copy_stat(ubuf, &sbuf);
75 int os_access(const char* file, int mode)
79 amode=(mode&OS_ACC_R_OK ? R_OK : 0) | (mode&OS_ACC_W_OK ? W_OK : 0) |
80 (mode&OS_ACC_X_OK ? X_OK : 0) | (mode&OS_ACC_F_OK ? F_OK : 0) ;
82 err = access(file, amode);
89 void os_print_error(int error, const char* str)
91 errno = error < 0 ? -error : error;
96 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
97 int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
101 err = ioctl(fd, cmd, arg);
108 int os_window_size(int fd, int *rows, int *cols)
112 if(ioctl(fd, TIOCGWINSZ, &size) < 0)
121 int os_new_tty_pgrp(int fd, int pid)
123 if(ioctl(fd, TIOCSCTTY, 0) < 0){
124 printk("TIOCSCTTY failed, errno = %d\n", errno);
128 if(tcsetpgrp(fd, pid) < 0){
129 printk("tcsetpgrp failed, errno = %d\n", errno);
136 int os_make_pty_raw(int master)
141 while (((err = tcgetattr(master, &tt)) < 0) && errno == EINTR)
144 printk("os_make_pty_raw : tcgetattr failed, errno = %d\n", errno);
150 while (((err = tcsetattr(master, TCSADRAIN, &tt)) < 0) && errno == EINTR)
153 printk("os_make_pty_raw : tcsetattr failed, errno = %d\n", errno);
162 /* FIXME: ensure namebuf in os_get_if_name is big enough */
163 int os_get_ifname(int fd, char* namebuf)
165 if(ioctl(fd, SIOCGIFNAME, namebuf) < 0)
171 int os_set_slip(int fd)
176 if(ioctl(fd, TIOCSETD, &disc) < 0){
177 printk("Failed to set slip line discipline - "
178 "errno = %d\n", errno);
183 if(ioctl(fd, SIOCSIFENCAP, &sencap) < 0){
184 printk("Failed to set slip encapsulation - "
185 "errno = %d\n", errno);
192 int os_set_owner(int fd, int pid)
194 if(fcntl(fd, F_SETOWN, pid) < 0){
195 int save_errno = errno;
197 if(fcntl(fd, F_GETOWN, 0) != pid)
204 /* FIXME? moved wholesale from sigio_user.c to get fcntls out of that file */
205 int os_sigio_async(int master, int slave)
209 flags = fcntl(master, F_GETFL);
211 printk("fcntl F_GETFL failed, errno = %d\n", errno);
215 if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
216 (fcntl(master, F_SETOWN, os_getpid()) < 0)){
217 printk("fcntl F_SETFL or F_SETOWN failed, errno = %d\n", errno);
221 if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0)){
222 printk("fcntl F_SETFL failed, errno = %d\n", errno);
229 int os_mode_fd(int fd, int mode)
234 err = fchmod(fd, mode);
235 } while((err < 0) && (errno==EINTR)) ;
243 int os_file_type(char *file)
248 err = os_stat_file(file, &buf);
252 if(S_ISDIR(buf.ust_mode)) return(OS_TYPE_DIR);
253 else if(S_ISLNK(buf.ust_mode)) return(OS_TYPE_SYMLINK);
254 else if(S_ISCHR(buf.ust_mode)) return(OS_TYPE_CHARDEV);
255 else if(S_ISBLK(buf.ust_mode)) return(OS_TYPE_BLOCKDEV);
256 else if(S_ISFIFO(buf.ust_mode)) return(OS_TYPE_FIFO);
257 else if(S_ISSOCK(buf.ust_mode)) return(OS_TYPE_SOCK);
258 else return(OS_TYPE_FILE);
261 int os_file_mode(char *file, struct openflags *mode_out)
265 *mode_out = OPENFLAGS();
267 err = os_access(file, OS_ACC_W_OK);
268 if((err < 0) && (err != -EACCES))
271 *mode_out = of_write(*mode_out);
273 err = os_access(file, OS_ACC_R_OK);
274 if((err < 0) && (err != -EACCES))
277 *mode_out = of_read(*mode_out);
282 int os_open_file(char *file, struct openflags flags, int mode)
286 if(flags.r && flags.w) f = O_RDWR;
287 else if(flags.r) f = O_RDONLY;
288 else if(flags.w) f = O_WRONLY;
291 if(flags.s) f |= O_SYNC;
292 if(flags.c) f |= O_CREAT;
293 if(flags.t) f |= O_TRUNC;
294 if(flags.e) f |= O_EXCL;
296 fd = open64(file, f, mode);
300 if(flags.cl && fcntl(fd, F_SETFD, 1)){
308 int os_connect_socket(char *name)
310 struct sockaddr_un sock;
313 sock.sun_family = AF_UNIX;
314 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
316 fd = socket(AF_UNIX, SOCK_STREAM, 0);
320 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
327 void os_close_file(int fd)
332 int os_seek_file(int fd, __u64 offset)
336 actual = lseek64(fd, offset, SEEK_SET);
337 if(actual != offset) return(-errno);
341 static int fault_buffer(void *start, int len,
342 int (*copy_proc)(void *addr, void *buf, int len))
344 int page = getpagesize(), i;
347 for(i = 0; i < len; i += page){
348 if((*copy_proc)(start + i, &c, sizeof(c)))
351 if((len % page) != 0){
352 if((*copy_proc)(start + len - 1, &c, sizeof(c)))
358 static int file_io(int fd, void *buf, int len,
359 int (*io_proc)(int fd, void *buf, int len),
360 int (*copy_user_proc)(void *addr, void *buf, int len))
365 n = (*io_proc)(fd, buf, len);
366 if((n < 0) && (errno == EFAULT)){
367 err = fault_buffer(buf, len, copy_user_proc);
370 n = (*io_proc)(fd, buf, len);
372 } while((n < 0) && (errno == EINTR));
379 int os_read_file(int fd, void *buf, int len)
381 return(file_io(fd, buf, len, (int (*)(int, void *, int)) read,
382 copy_from_user_proc));
385 int os_write_file(int fd, const void *buf, int len)
387 return(file_io(fd, (void *) buf, len,
388 (int (*)(int, void *, int)) write, copy_to_user_proc));
391 int os_file_size(char *file, long long *size_out)
396 err = os_stat_file(file, &buf);
398 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
402 if(S_ISBLK(buf.ust_mode)){
405 fd = os_open_file(file, of_read(OPENFLAGS()), 0);
407 printk("Couldn't open \"%s\", errno = %d\n", file, -fd);
410 if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
411 printk("Couldn't get the block size of \"%s\", "
412 "errno = %d\n", file, errno);
417 *size_out = ((long long) blocks) * 512;
421 *size_out = buf.ust_size;
425 int os_file_modtime(char *file, unsigned long *modtime)
430 err = os_stat_file(file, &buf);
432 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
436 *modtime = buf.ust_mtime;
440 int os_get_exec_close(int fd, int* close_on_exec)
445 ret = fcntl(fd, F_GETFD);
446 } while((ret < 0) && (errno == EINTR)) ;
451 *close_on_exec = (ret&FD_CLOEXEC) ? 1 : 0;
455 int os_set_exec_close(int fd, int close_on_exec)
459 if(close_on_exec) flag = FD_CLOEXEC;
463 err = fcntl(fd, F_SETFD, flag);
464 } while((err < 0) && (errno == EINTR)) ;
471 int os_pipe(int *fds, int stream, int close_on_exec)
473 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
475 err = socketpair(AF_UNIX, type, 0, fds);
482 err = os_set_exec_close(fds[0], 1);
486 err = os_set_exec_close(fds[1], 1);
493 printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err);
494 os_close_file(fds[1]);
495 os_close_file(fds[0]);
499 int os_set_fd_async(int fd, int owner)
501 /* XXX This should do F_GETFL first */
502 if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){
503 printk("os_set_fd_async : failed to set O_ASYNC and "
504 "O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
508 if(fcntl(fd, F_SETFD, 1) < 0){
509 printk("os_set_fd_async : Setting FD_CLOEXEC failed, "
510 "errno = %d\n", errno);
514 if((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
515 (fcntl(fd, F_SETOWN, owner) < 0)){
516 printk("os_set_fd_async : Failed to fcntl F_SETOWN "
517 "(or F_SETSIG) fd %d to pid %d, errno = %d\n", fd,
525 int os_set_fd_block(int fd, int blocking)
529 flags = fcntl(fd, F_GETFL);
531 if(blocking) flags &= ~O_NONBLOCK;
532 else flags |= O_NONBLOCK;
534 if(fcntl(fd, F_SETFL, flags) < 0){
535 printk("Failed to change blocking on fd # %d, errno = %d\n",
542 int os_accept_connection(int fd)
546 new = accept(fd, NULL, 0);
564 int os_shutdown_socket(int fd, int r, int w)
568 if(r && w) what = SHUT_RDWR;
569 else if(r) what = SHUT_RD;
570 else if(w) what = SHUT_WR;
572 printk("os_shutdown_socket : neither r or w was set\n");
575 err = shutdown(fd, what);
581 int os_rcv_fd(int fd, int *helper_pid_out)
584 char buf[CMSG_SPACE(sizeof(new))];
586 struct cmsghdr *cmsg;
591 iov = ((struct iovec) { .iov_base = helper_pid_out,
592 .iov_len = sizeof(*helper_pid_out) });
595 msg.msg_control = buf;
596 msg.msg_controllen = sizeof(buf);
599 n = recvmsg(fd, &msg, 0);
603 else if(n != sizeof(iov.iov_len))
604 *helper_pid_out = -1;
606 cmsg = CMSG_FIRSTHDR(&msg);
608 printk("rcv_fd didn't receive anything, error = %d\n", errno);
611 if((cmsg->cmsg_level != SOL_SOCKET) ||
612 (cmsg->cmsg_type != SCM_RIGHTS)){
613 printk("rcv_fd didn't receive a descriptor\n");
617 new = ((int *) CMSG_DATA(cmsg))[0];
621 int os_create_unix_socket(char *file, int len, int close_on_exec)
623 struct sockaddr_un addr;
626 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
628 printk("create_unix_socket - socket failed, errno = %d\n",
634 err = os_set_exec_close(sock, 1);
636 printk("create_unix_socket : close_on_exec failed, "
640 addr.sun_family = AF_UNIX;
642 /* XXX Be more careful about overflow */
643 snprintf(addr.sun_path, len, "%s", file);
645 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
647 printk("create_listening_socket at '%s' - bind failed, "
648 "errno = %d\n", file, errno);
655 void os_flush_stdout(void)
660 int os_lock_file(int fd, int excl)
662 int type = excl ? F_WRLCK : F_RDLCK;
663 struct flock lock = ((struct flock) { .l_type = type,
664 .l_whence = SEEK_SET,
669 err = fcntl(fd, F_SETLK, &lock);
674 err = fcntl(fd, F_GETLK, &lock);
680 printk("F_SETLK failed, file already locked by pid %d\n", lock.l_pid);
687 * Overrides for Emacs so that we follow Linus's tabbing style.
688 * Emacs will notice this stuff at the end of the file and automatically
689 * adjust the settings for this buffer only. This must remain at the end
691 * ---------------------------------------------------------------------------
693 * c-file-style: "linux"