VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
97870c34b4532 (Alex Dewar                     2019-08-25 10:49:18 +0100   1) // SPDX-License-Identifier: GPL-2.0
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700   2) /*
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800   3)  * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   4)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   5) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   6) #include <stdio.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   7) #include <unistd.h>
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100   8) #include <stdlib.h>
38bccfbeb0af0 (Zach van Rijn                  2020-04-01 16:30:48 -0500   9) #include <string.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  10) #include <errno.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  11) #include <fcntl.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  12) #include <signal.h>
35f3401317a3b (Alan Maguire                   2020-03-17 17:35:34 +0000  13) #include <linux/falloc.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  14) #include <sys/ioctl.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  15) #include <sys/mount.h>
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  16) #include <sys/socket.h>
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  17) #include <sys/stat.h>
530ba6c7cb3c2 (Jason A. Donenfeld             2017-12-14 03:23:37 +0100  18) #include <sys/sysmacros.h>
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  19) #include <sys/un.h>
8eeba4e9a76cd (Hans-Werner Hilse              2015-06-11 11:29:20 +0200  20) #include <sys/types.h>
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200  21) #include <sys/eventfd.h>
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100  22) #include <poll.h>
37185b3324087 (Al Viro                        2012-10-08 03:27:32 +0100  23) #include <os.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  24) 
c9a3072d13e4b (WANG Cong                      2008-02-04 22:30:35 -0800  25) static void copy_stat(struct uml_stat *dst, const struct stat64 *src)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  26) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  27) 	*dst = ((struct uml_stat) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  28) 		.ust_dev     = src->st_dev,     /* device */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  29) 		.ust_ino     = src->st_ino,     /* inode */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  30) 		.ust_mode    = src->st_mode,    /* protection */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  31) 		.ust_nlink   = src->st_nlink,   /* number of hard links */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  32) 		.ust_uid     = src->st_uid,     /* user ID of owner */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  33) 		.ust_gid     = src->st_gid,     /* group ID of owner */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  34) 		.ust_size    = src->st_size,    /* total size, in bytes */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  35) 		.ust_blksize = src->st_blksize, /* blocksize for filesys I/O */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  36) 		.ust_blocks  = src->st_blocks,  /* number of blocks allocated */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  37) 		.ust_atime   = src->st_atime,   /* time of last access */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  38) 		.ust_mtime   = src->st_mtime,   /* time of last modification */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  39) 		.ust_ctime   = src->st_ctime,   /* time of last change */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  40) 	});
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  41) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  42) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  43) int os_stat_fd(const int fd, struct uml_stat *ubuf)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  44) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  45) 	struct stat64 sbuf;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  46) 	int err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  47) 
9ead6feedd280 (Jeff Dike                      2006-07-10 04:45:15 -0700  48) 	CATCH_EINTR(err = fstat64(fd, &sbuf));
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  49) 	if (err < 0)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700  50) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  51) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  52) 	if (ubuf != NULL)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  53) 		copy_stat(ubuf, &sbuf);
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700  54) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  55) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  56) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  57) int os_stat_file(const char *file_name, struct uml_stat *ubuf)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  58) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  59) 	struct stat64 sbuf;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  60) 	int err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  61) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  62) 	CATCH_EINTR(err = stat64(file_name, &sbuf));
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  63) 	if (err < 0)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700  64) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  65) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  66) 	if (ubuf != NULL)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  67) 		copy_stat(ubuf, &sbuf);
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700  68) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  69) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  70) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  71) int os_access(const char *file, int mode)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  72) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  73) 	int amode, err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  74) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  75) 	amode = (mode & OS_ACC_R_OK ? R_OK : 0) |
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  76) 		(mode & OS_ACC_W_OK ? W_OK : 0) |
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  77) 		(mode & OS_ACC_X_OK ? X_OK : 0) |
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  78) 		(mode & OS_ACC_F_OK ? F_OK : 0);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  79) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  80) 	err = access(file, amode);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  81) 	if (err < 0)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700  82) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  83) 
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700  84) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  85) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  86) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  87) /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  88) int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  89) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  90) 	int err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  91) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  92) 	err = ioctl(fd, cmd, arg);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800  93) 	if (err < 0)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700  94) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  95) 
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700  96) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  97) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  98) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  99) /* FIXME: ensure namebuf in os_get_if_name is big enough */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 100) int os_get_ifname(int fd, char* namebuf)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 101) {
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 102) 	if (ioctl(fd, SIOCGIFNAME, namebuf) < 0)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 103) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 104) 
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 105) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 106) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 107) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 108) int os_set_slip(int fd)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 109) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 110) 	int disc, sencap;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 111) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 112) 	disc = N_SLIP;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 113) 	if (ioctl(fd, TIOCSETD, &disc) < 0)
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 114) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 115) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 116) 	sencap = 0;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 117) 	if (ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 118) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 119) 
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 120) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 121) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 122) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 123) int os_mode_fd(int fd, int mode)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 124) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 125) 	int err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 126) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 127) 	CATCH_EINTR(err = fchmod(fd, mode));
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 128) 	if (err < 0)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 129) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 130) 
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 131) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 132) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 133) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 134) int os_file_type(char *file)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 135) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 136) 	struct uml_stat buf;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 137) 	int err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 138) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 139) 	err = os_stat_file(file, &buf);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 140) 	if (err < 0)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 141) 		return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 142) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 143) 	if (S_ISDIR(buf.ust_mode))
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 144) 		return OS_TYPE_DIR;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 145) 	else if (S_ISLNK(buf.ust_mode))
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 146) 		return OS_TYPE_SYMLINK;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 147) 	else if (S_ISCHR(buf.ust_mode))
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 148) 		return OS_TYPE_CHARDEV;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 149) 	else if (S_ISBLK(buf.ust_mode))
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 150) 		return OS_TYPE_BLOCKDEV;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 151) 	else if (S_ISFIFO(buf.ust_mode))
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 152) 		return OS_TYPE_FIFO;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 153) 	else if (S_ISSOCK(buf.ust_mode))
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 154) 		return OS_TYPE_SOCK;
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 155) 	else return OS_TYPE_FILE;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 156) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 157) 
c9a3072d13e4b (WANG Cong                      2008-02-04 22:30:35 -0800 158) int os_file_mode(const char *file, struct openflags *mode_out)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 159) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 160) 	int err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 161) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 162) 	*mode_out = OPENFLAGS();
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 163) 
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 164) 	err = access(file, W_OK);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 165) 	if (err && (errno != EACCES))
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 166) 		return -errno;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 167) 	else if (!err)
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 168) 		*mode_out = of_write(*mode_out);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 169) 
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 170) 	err = access(file, R_OK);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 171) 	if (err && (errno != EACCES))
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 172) 		return -errno;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 173) 	else if (!err)
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 174) 		*mode_out = of_read(*mode_out);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 175) 
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 176) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 177) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 178) 
c9a3072d13e4b (WANG Cong                      2008-02-04 22:30:35 -0800 179) int os_open_file(const char *file, struct openflags flags, int mode)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 180) {
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 181) 	int fd, err, f = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 182) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 183) 	if (flags.r && flags.w)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 184) 		f = O_RDWR;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 185) 	else if (flags.r)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 186) 		f = O_RDONLY;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 187) 	else if (flags.w)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 188) 		f = O_WRONLY;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 189) 	else f = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 190) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 191) 	if (flags.s)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 192) 		f |= O_SYNC;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 193) 	if (flags.c)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 194) 		f |= O_CREAT;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 195) 	if (flags.t)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 196) 		f |= O_TRUNC;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 197) 	if (flags.e)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 198) 		f |= O_EXCL;
bf53d85ec20c2 (Jeff Dike                      2008-02-04 22:31:18 -0800 199) 	if (flags.a)
bf53d85ec20c2 (Jeff Dike                      2008-02-04 22:31:18 -0800 200) 		f |= O_APPEND;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 201) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 202) 	fd = open64(file, f, mode);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 203) 	if (fd < 0)
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 204) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 205) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 206) 	if (flags.cl && fcntl(fd, F_SETFD, 1)) {
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 207) 		err = -errno;
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 208) 		close(fd);
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 209) 		return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 210) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 211) 
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 212) 	return fd;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 213) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 214) 
c9a3072d13e4b (WANG Cong                      2008-02-04 22:30:35 -0800 215) int os_connect_socket(const char *name)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 216) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 217) 	struct sockaddr_un sock;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 218) 	int fd, err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 219) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 220) 	sock.sun_family = AF_UNIX;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 221) 	snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 222) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 223) 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 224) 	if (fd < 0) {
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 225) 		err = -errno;
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 226) 		goto out;
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 227) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 228) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 229) 	err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 230) 	if (err) {
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 231) 		err = -errno;
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 232) 		goto out_close;
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 233) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 234) 
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 235) 	return fd;
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 236) 
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 237) out_close:
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 238) 	close(fd);
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 239) out:
dc1561ac019ff (Paolo 'Blaisorblade' Giarrusso 2006-02-24 13:03:56 -0800 240) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 241) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 242) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 243) void os_close_file(int fd)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 244) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 245) 	close(fd);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 246) }
0565103d1adbd (Anton Ivanov                   2014-03-07 18:37:47 +0000 247) int os_fsync_file(int fd)
0565103d1adbd (Anton Ivanov                   2014-03-07 18:37:47 +0000 248) {
0565103d1adbd (Anton Ivanov                   2014-03-07 18:37:47 +0000 249) 	if (fsync(fd) < 0)
0565103d1adbd (Anton Ivanov                   2014-03-07 18:37:47 +0000 250) 	    return -errno;
0565103d1adbd (Anton Ivanov                   2014-03-07 18:37:47 +0000 251) 	return 0;
0565103d1adbd (Anton Ivanov                   2014-03-07 18:37:47 +0000 252) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 253) 
ba180fd437156 (Jeff Dike                      2007-10-16 01:27:00 -0700 254) int os_seek_file(int fd, unsigned long long offset)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 255) {
ba180fd437156 (Jeff Dike                      2007-10-16 01:27:00 -0700 256) 	unsigned long long actual;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 257) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 258) 	actual = lseek64(fd, offset, SEEK_SET);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 259) 	if (actual != offset)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 260) 		return -errno;
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 261) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 262) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 263) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 264) int os_read_file(int fd, void *buf, int len)
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 265) {
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 266) 	int n = read(fd, buf, len);
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 267) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 268) 	if (n < 0)
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 269) 		return -errno;
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 270) 	return n;
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 271) }
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 272) 
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 273) int os_pread_file(int fd, void *buf, int len, unsigned long long offset)
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 274) {
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 275) 	int n = pread(fd, buf, len, offset);
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 276) 
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 277) 	if (n < 0)
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 278) 		return -errno;
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 279) 	return n;
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 280) }
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 281) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 282) int os_write_file(int fd, const void *buf, int len)
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 283) {
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 284) 	int n = write(fd, (void *) buf, len);
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 285) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 286) 	if (n < 0)
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 287) 		return -errno;
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 288) 	return n;
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 289) }
3d564047a5f45 (Jeff Dike                      2007-05-06 14:51:32 -0700 290) 
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 291) int os_sync_file(int fd)
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 292) {
54ebe4060fe6c (Anton Ivanov                   2020-04-22 17:00:01 +0100 293) 	int n = fdatasync(fd);
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 294) 
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 295) 	if (n < 0)
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 296) 		return -errno;
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 297) 	return n;
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 298) }
805f11a0d5156 (Richard Weinberger             2013-08-18 13:30:06 +0200 299) 
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 300) int os_pwrite_file(int fd, const void *buf, int len, unsigned long long offset)
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 301) {
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 302) 	int n = pwrite(fd, (void *) buf, len, offset);
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 303) 
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 304) 	if (n < 0)
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 305) 		return -errno;
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 306) 	return n;
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 307) }
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 308) 
8c6157b6b30a7 (Anton Ivanov                   2015-12-21 18:54:00 +0000 309) 
c9a3072d13e4b (WANG Cong                      2008-02-04 22:30:35 -0800 310) int os_file_size(const char *file, unsigned long long *size_out)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 311) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 312) 	struct uml_stat buf;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 313) 	int err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 314) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 315) 	err = os_stat_file(file, &buf);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 316) 	if (err < 0) {
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 317) 		printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 318) 		       -err);
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 319) 		return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 320) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 321) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 322) 	if (S_ISBLK(buf.ust_mode)) {
2c392a4f47f41 (Nicolas George                 2007-09-18 22:46:21 -0700 323) 		int fd;
2c392a4f47f41 (Nicolas George                 2007-09-18 22:46:21 -0700 324) 		long blocks;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 325) 
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 326) 		fd = open(file, O_RDONLY, 0);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 327) 		if (fd < 0) {
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 328) 			err = -errno;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 329) 			printk(UM_KERN_ERR "Couldn't open \"%s\", "
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 330) 			       "errno = %d\n", file, errno);
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 331) 			return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 332) 		}
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 333) 		if (ioctl(fd, BLKGETSIZE, &blocks) < 0) {
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 334) 			err = -errno;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 335) 			printk(UM_KERN_ERR "Couldn't get the block size of "
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 336) 			       "\"%s\", errno = %d\n", file, errno);
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 337) 			close(fd);
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 338) 			return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 339) 		}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 340) 		*size_out = ((long long) blocks) * 512;
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 341) 		close(fd);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 342) 	}
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 343) 	else *size_out = buf.ust_size;
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 344) 
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 345) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 346) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 347) 
853bc0ab341b0 (Arnd Bergmann                  2019-11-05 09:39:51 +0100 348) int os_file_modtime(const char *file, long long *modtime)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 349) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 350) 	struct uml_stat buf;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 351) 	int err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 352) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 353) 	err = os_stat_file(file, &buf);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 354) 	if (err < 0) {
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 355) 		printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 356) 		       -err);
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 357) 		return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 358) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 359) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 360) 	*modtime = buf.ust_mtime;
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 361) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 362) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 363) 
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 364) int os_set_exec_close(int fd)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 365) {
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 366) 	int err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 367) 
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 368) 	CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 369) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 370) 	if (err < 0)
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 371) 		return -errno;
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 372) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 373) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 374) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 375) int os_pipe(int *fds, int stream, int close_on_exec)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 376) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 377) 	int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 378) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 379) 	err = socketpair(AF_UNIX, type, 0, fds);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 380) 	if (err < 0)
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 381) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 382) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 383) 	if (!close_on_exec)
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 384) 		return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 385) 
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 386) 	err = os_set_exec_close(fds[0]);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 387) 	if (err < 0)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 388) 		goto error;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 389) 
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 390) 	err = os_set_exec_close(fds[1]);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 391) 	if (err < 0)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 392) 		goto error;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 393) 
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 394) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 395) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 396)  error:
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 397) 	printk(UM_KERN_ERR "os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 398) 	       -err);
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 399) 	close(fds[1]);
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 400) 	close(fds[0]);
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 401) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 402) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 403) 
bf8fde785b872 (Jeff Dike                      2008-02-04 22:31:04 -0800 404) int os_set_fd_async(int fd)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 405) {
bf8fde785b872 (Jeff Dike                      2008-02-04 22:31:04 -0800 406) 	int err, flags;
bf8fde785b872 (Jeff Dike                      2008-02-04 22:31:04 -0800 407) 
bf8fde785b872 (Jeff Dike                      2008-02-04 22:31:04 -0800 408) 	flags = fcntl(fd, F_GETFL);
bf8fde785b872 (Jeff Dike                      2008-02-04 22:31:04 -0800 409) 	if (flags < 0)
bf8fde785b872 (Jeff Dike                      2008-02-04 22:31:04 -0800 410) 		return -errno;
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 411) 
bf8fde785b872 (Jeff Dike                      2008-02-04 22:31:04 -0800 412) 	flags |= O_ASYNC | O_NONBLOCK;
bf8fde785b872 (Jeff Dike                      2008-02-04 22:31:04 -0800 413) 	if (fcntl(fd, F_SETFL, flags) < 0) {
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 414) 		err = -errno;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 415) 		printk(UM_KERN_ERR "os_set_fd_async : failed to set O_ASYNC "
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 416) 		       "and O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 417) 		return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 418) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 419) 
bf8fde785b872 (Jeff Dike                      2008-02-04 22:31:04 -0800 420) 	if ((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
bf8fde785b872 (Jeff Dike                      2008-02-04 22:31:04 -0800 421) 	    (fcntl(fd, F_SETOWN, os_getpid()) < 0)) {
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 422) 		err = -errno;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 423) 		printk(UM_KERN_ERR "os_set_fd_async : Failed to fcntl F_SETOWN "
bf8fde785b872 (Jeff Dike                      2008-02-04 22:31:04 -0800 424) 		       "(or F_SETSIG) fd %d, errno = %d\n", fd, errno);
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 425) 		return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 426) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 427) 
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 428) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 429) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 430) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 431) int os_clear_fd_async(int fd)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 432) {
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 433) 	int flags;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 434) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 435) 	flags = fcntl(fd, F_GETFL);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 436) 	if (flags < 0)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 437) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 438) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 439) 	flags &= ~(O_ASYNC | O_NONBLOCK);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 440) 	if (fcntl(fd, F_SETFL, flags) < 0)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 441) 		return -errno;
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 442) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 443) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 444) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 445) int os_set_fd_block(int fd, int blocking)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 446) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 447) 	int flags;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 448) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 449) 	flags = fcntl(fd, F_GETFL);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 450) 	if (flags < 0)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 451) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 452) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 453) 	if (blocking)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 454) 		flags &= ~O_NONBLOCK;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 455) 	else
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 456) 		flags |= O_NONBLOCK;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 457) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 458) 	if (fcntl(fd, F_SETFL, flags) < 0)
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 459) 		return -errno;
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 460) 
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 461) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 462) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 463) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 464) int os_accept_connection(int fd)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 465) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 466) 	int new;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 467) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 468) 	new = accept(fd, NULL, 0);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 469) 	if (new < 0)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 470) 		return -errno;
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 471) 	return new;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 472) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 473) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 474) #ifndef SHUT_RD
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 475) #define SHUT_RD 0
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 476) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 477) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 478) #ifndef SHUT_WR
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 479) #define SHUT_WR 1
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 480) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 481) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 482) #ifndef SHUT_RDWR
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 483) #define SHUT_RDWR 2
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 484) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 485) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 486) int os_shutdown_socket(int fd, int r, int w)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 487) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 488) 	int what, err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 489) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 490) 	if (r && w)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 491) 		what = SHUT_RDWR;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 492) 	else if (r)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 493) 		what = SHUT_RD;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 494) 	else if (w)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 495) 		what = SHUT_WR;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 496) 	else
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 497) 		return -EINVAL;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 498) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 499) 	err = shutdown(fd, what);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 500) 	if (err < 0)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 501) 		return -errno;
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 502) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 503) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 504) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 505) int os_rcv_fd(int fd, int *helper_pid_out)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 506) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 507) 	int new, n;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 508) 	char buf[CMSG_SPACE(sizeof(new))];
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 509) 	struct msghdr msg;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 510) 	struct cmsghdr *cmsg;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 511) 	struct iovec iov;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 512) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 513) 	msg.msg_name = NULL;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 514) 	msg.msg_namelen = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 515) 	iov = ((struct iovec) { .iov_base  = helper_pid_out,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 516) 				.iov_len   = sizeof(*helper_pid_out) });
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 517) 	msg.msg_iov = &iov;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 518) 	msg.msg_iovlen = 1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 519) 	msg.msg_control = buf;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 520) 	msg.msg_controllen = sizeof(buf);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 521) 	msg.msg_flags = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 522) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 523) 	n = recvmsg(fd, &msg, 0);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 524) 	if (n < 0)
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 525) 		return -errno;
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 526) 	else if (n != iov.iov_len)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 527) 		*helper_pid_out = -1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 528) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 529) 	cmsg = CMSG_FIRSTHDR(&msg);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 530) 	if (cmsg == NULL) {
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 531) 		printk(UM_KERN_ERR "rcv_fd didn't receive anything, "
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 532) 		       "error = %d\n", errno);
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 533) 		return -1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 534) 	}
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 535) 	if ((cmsg->cmsg_level != SOL_SOCKET) ||
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 536) 	    (cmsg->cmsg_type != SCM_RIGHTS)) {
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 537) 		printk(UM_KERN_ERR "rcv_fd didn't receive a descriptor\n");
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 538) 		return -1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 539) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 540) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 541) 	new = ((int *) CMSG_DATA(cmsg))[0];
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 542) 	return new;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 543) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 544) 
c9a3072d13e4b (WANG Cong                      2008-02-04 22:30:35 -0800 545) int os_create_unix_socket(const char *file, int len, int close_on_exec)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 546) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 547) 	struct sockaddr_un addr;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 548) 	int sock, err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 549) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 550) 	sock = socket(PF_UNIX, SOCK_DGRAM, 0);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 551) 	if (sock < 0)
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 552) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 553) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 554) 	if (close_on_exec) {
512b6fb1c14d4 (Jeff Dike                      2007-10-16 01:27:11 -0700 555) 		err = os_set_exec_close(sock);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 556) 		if (err < 0)
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 557) 			printk(UM_KERN_ERR "create_unix_socket : "
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 558) 			       "close_on_exec failed, err = %d", -err);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 559) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 560) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 561) 	addr.sun_family = AF_UNIX;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 562) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 563) 	snprintf(addr.sun_path, len, "%s", file);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 564) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 565) 	err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 566) 	if (err < 0)
b4fd310e16347 (Jeff Dike                      2005-09-16 19:27:49 -0700 567) 		return -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 568) 
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 569) 	return sock;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 570) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 571) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 572) void os_flush_stdout(void)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 573) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 574) 	fflush(stdout);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 575) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 576) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 577) int os_lock_file(int fd, int excl)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 578) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 579) 	int type = excl ? F_WRLCK : F_RDLCK;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 580) 	struct flock lock = ((struct flock) { .l_type	= type,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 581) 					      .l_whence	= SEEK_SET,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 582) 					      .l_start	= 0,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 583) 					      .l_len	= 0 } );
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 584) 	int err, save;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 585) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 586) 	err = fcntl(fd, F_SETLK, &lock);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 587) 	if (!err)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 588) 		goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 589) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 590) 	save = -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 591) 	err = fcntl(fd, F_GETLK, &lock);
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 592) 	if (err) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 593) 		err = -errno;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 594) 		goto out;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 595) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 596) 
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 597) 	printk(UM_KERN_ERR "F_SETLK failed, file already locked by pid %d\n",
1adfd6095e1c6 (Jeff Dike                      2008-02-04 22:31:05 -0800 598) 	       lock.l_pid);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 599) 	err = save;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 600)  out:
108ffa8cbfa32 (Jeff Dike                      2006-07-10 04:45:14 -0700 601) 	return err;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 602) }
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 603) 
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 604) unsigned os_major(unsigned long long dev)
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 605) {
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 606) 	return major(dev);
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 607) }
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 608) 
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 609) unsigned os_minor(unsigned long long dev)
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 610) {
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 611) 	return minor(dev);
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 612) }
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 613) 
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 614) unsigned long long os_makedev(unsigned major, unsigned minor)
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 615) {
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 616) 	return makedev(major, minor);
005a59ec745d2 (Al Viro                        2009-04-21 01:27:08 -0400 617) }
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 618) 
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 619) int os_falloc_punch(int fd, unsigned long long offset, int len)
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 620) {
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 621) 	int n = fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, len);
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 622) 
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 623) 	if (n < 0)
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 624) 		return -errno;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 625) 	return n;
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 626) }
50109b5a03b40 (Anton Ivanov                   2018-11-14 18:41:09 +0000 627) 
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 628) int os_eventfd(unsigned int initval, int flags)
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 629) {
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 630) 	int fd = eventfd(initval, flags);
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 631) 
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 632) 	if (fd < 0)
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 633) 		return -errno;
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 634) 	return fd;
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 635) }
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 636) 
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 637) int os_sendmsg_fds(int fd, const void *buf, unsigned int len, const int *fds,
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 638) 		   unsigned int fds_num)
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 639) {
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 640) 	struct iovec iov = {
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 641) 		.iov_base = (void *) buf,
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 642) 		.iov_len = len,
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 643) 	};
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 644) 	union {
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 645) 		char control[CMSG_SPACE(sizeof(*fds) * OS_SENDMSG_MAX_FDS)];
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 646) 		struct cmsghdr align;
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 647) 	} u;
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 648) 	unsigned int fds_size = sizeof(*fds) * fds_num;
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 649) 	struct msghdr msg = {
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 650) 		.msg_iov = &iov,
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 651) 		.msg_iovlen = 1,
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 652) 		.msg_control = u.control,
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 653) 		.msg_controllen = CMSG_SPACE(fds_size),
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 654) 	};
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 655) 	struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 656) 	int err;
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 657) 
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 658) 	if (fds_num > OS_SENDMSG_MAX_FDS)
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 659) 		return -EINVAL;
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 660) 	memset(u.control, 0, sizeof(u.control));
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 661) 	cmsg->cmsg_level = SOL_SOCKET;
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 662) 	cmsg->cmsg_type = SCM_RIGHTS;
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 663) 	cmsg->cmsg_len = CMSG_LEN(fds_size);
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 664) 	memcpy(CMSG_DATA(cmsg), fds, fds_size);
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 665) 	err = sendmsg(fd, &msg, 0);
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 666) 
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 667) 	if (err < 0)
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 668) 		return -errno;
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 669) 	return err;
5d38f324993f4 (Erel Geron                     2019-09-11 14:51:20 +0200 670) }
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 671) 
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 672) int os_poll(unsigned int n, const int *fds)
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 673) {
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 674) 	/* currently need 2 FDs at most so avoid dynamic allocation */
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 675) 	struct pollfd pollfds[2] = {};
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 676) 	unsigned int i;
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 677) 	int ret;
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 678) 
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 679) 	if (n > ARRAY_SIZE(pollfds))
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 680) 		return -EINVAL;
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 681) 
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 682) 	for (i = 0; i < n; i++) {
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 683) 		pollfds[i].fd = fds[i];
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 684) 		pollfds[i].events = POLLIN;
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 685) 	}
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 686) 
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 687) 	ret = poll(pollfds, n, -1);
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 688) 	if (ret < 0)
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 689) 		return -errno;
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 690) 
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 691) 	/* Return the index of the available FD */
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 692) 	for (i = 0; i < n; i++) {
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 693) 		if (pollfds[i].revents)
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 694) 			return i;
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 695) 	}
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 696) 
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 697) 	return -EIO;
88ce642492339 (Johannes Berg                  2020-02-13 14:26:47 +0100 698) }