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