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