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