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