Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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) |
| 50 | return(-errno); |
| 51 | |
| 52 | if(ubuf != NULL) |
| 53 | copy_stat(ubuf, &sbuf); |
| 54 | return(err); |
| 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) |
| 67 | return(-errno); |
| 68 | |
| 69 | if(ubuf != NULL) |
| 70 | copy_stat(ubuf, &sbuf); |
| 71 | return(err); |
| 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) |
| 83 | return(-errno); |
| 84 | |
| 85 | return(0); |
| 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) |
| 102 | return(-errno); |
| 103 | |
| 104 | return(err); |
| 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) |
| 112 | return(-errno); |
| 113 | |
| 114 | *rows = size.ws_row; |
| 115 | *cols = size.ws_col; |
| 116 | |
| 117 | return(0); |
| 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 | |
| 128 | return(0); |
| 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) |
| 135 | return(-errno); |
| 136 | |
| 137 | return(0); |
| 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 | |
| 152 | return(0); |
| 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) |
| 161 | return(-save_errno); |
| 162 | } |
| 163 | |
| 164 | return(0); |
| 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) |
| 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) |
| 195 | return(-errno); |
| 196 | |
| 197 | return(0); |
| 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) |
| 207 | return(err); |
| 208 | |
| 209 | if(S_ISDIR(buf.ust_mode)) return(OS_TYPE_DIR); |
| 210 | else if(S_ISLNK(buf.ust_mode)) return(OS_TYPE_SYMLINK); |
| 211 | else if(S_ISCHR(buf.ust_mode)) return(OS_TYPE_CHARDEV); |
| 212 | else if(S_ISBLK(buf.ust_mode)) return(OS_TYPE_BLOCKDEV); |
| 213 | else if(S_ISFIFO(buf.ust_mode)) return(OS_TYPE_FIFO); |
| 214 | else if(S_ISSOCK(buf.ust_mode)) return(OS_TYPE_SOCK); |
| 215 | else return(OS_TYPE_FILE); |
| 216 | } |
| 217 | |
| 218 | int os_file_mode(char *file, struct openflags *mode_out) |
| 219 | { |
| 220 | int err; |
| 221 | |
| 222 | *mode_out = OPENFLAGS(); |
| 223 | |
| 224 | err = os_access(file, OS_ACC_W_OK); |
| 225 | if((err < 0) && (err != -EACCES)) |
| 226 | return(err); |
| 227 | |
| 228 | *mode_out = of_write(*mode_out); |
| 229 | |
| 230 | err = os_access(file, OS_ACC_R_OK); |
| 231 | if((err < 0) && (err != -EACCES)) |
| 232 | return(err); |
| 233 | |
| 234 | *mode_out = of_read(*mode_out); |
| 235 | |
| 236 | return(0); |
| 237 | } |
| 238 | |
| 239 | int os_open_file(char *file, struct openflags flags, int mode) |
| 240 | { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 241 | int fd, err, f = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | if(flags.r && flags.w) f = O_RDWR; |
| 244 | else if(flags.r) f = O_RDONLY; |
| 245 | else if(flags.w) f = O_WRONLY; |
| 246 | else f = 0; |
| 247 | |
| 248 | if(flags.s) f |= O_SYNC; |
| 249 | if(flags.c) f |= O_CREAT; |
| 250 | if(flags.t) f |= O_TRUNC; |
| 251 | if(flags.e) f |= O_EXCL; |
| 252 | |
| 253 | fd = open64(file, f, mode); |
| 254 | if(fd < 0) |
| 255 | return(-errno); |
| 256 | |
| 257 | if(flags.cl && fcntl(fd, F_SETFD, 1)){ |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 258 | err = -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | os_close_file(fd); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 260 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | return(fd); |
| 264 | } |
| 265 | |
| 266 | int os_connect_socket(char *name) |
| 267 | { |
| 268 | struct sockaddr_un sock; |
| 269 | int fd, err; |
| 270 | |
| 271 | sock.sun_family = AF_UNIX; |
| 272 | snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name); |
| 273 | |
| 274 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
Paolo 'Blaisorblade' Giarrusso | dc1561a | 2006-02-24 13:03:56 -0800 | [diff] [blame] | 275 | if(fd < 0) { |
| 276 | err = -errno; |
| 277 | goto out; |
| 278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | err = connect(fd, (struct sockaddr *) &sock, sizeof(sock)); |
Paolo 'Blaisorblade' Giarrusso | dc1561a | 2006-02-24 13:03:56 -0800 | [diff] [blame] | 281 | if(err) { |
| 282 | err = -errno; |
| 283 | goto out_close; |
| 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Paolo 'Blaisorblade' Giarrusso | dc1561a | 2006-02-24 13:03:56 -0800 | [diff] [blame] | 286 | return fd; |
| 287 | |
| 288 | out_close: |
| 289 | close(fd); |
| 290 | out: |
| 291 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void os_close_file(int fd) |
| 295 | { |
| 296 | close(fd); |
| 297 | } |
| 298 | |
| 299 | int os_seek_file(int fd, __u64 offset) |
| 300 | { |
| 301 | __u64 actual; |
| 302 | |
| 303 | actual = lseek64(fd, offset, SEEK_SET); |
| 304 | if(actual != offset) |
| 305 | return(-errno); |
| 306 | return(0); |
| 307 | } |
| 308 | |
| 309 | static int fault_buffer(void *start, int len, |
| 310 | int (*copy_proc)(void *addr, void *buf, int len)) |
| 311 | { |
| 312 | int page = getpagesize(), i; |
| 313 | char c; |
| 314 | |
| 315 | for(i = 0; i < len; i += page){ |
| 316 | if((*copy_proc)(start + i, &c, sizeof(c))) |
| 317 | return(-EFAULT); |
| 318 | } |
| 319 | if((len % page) != 0){ |
| 320 | if((*copy_proc)(start + len - 1, &c, sizeof(c))) |
| 321 | return(-EFAULT); |
| 322 | } |
| 323 | return(0); |
| 324 | } |
| 325 | |
| 326 | static int file_io(int fd, void *buf, int len, |
| 327 | int (*io_proc)(int fd, void *buf, int len), |
| 328 | int (*copy_user_proc)(void *addr, void *buf, int len)) |
| 329 | { |
| 330 | int n, err; |
| 331 | |
| 332 | do { |
| 333 | n = (*io_proc)(fd, buf, len); |
| 334 | if((n < 0) && (errno == EFAULT)){ |
| 335 | err = fault_buffer(buf, len, copy_user_proc); |
| 336 | if(err) |
| 337 | return(err); |
| 338 | n = (*io_proc)(fd, buf, len); |
| 339 | } |
| 340 | } while((n < 0) && (errno == EINTR)); |
| 341 | |
| 342 | if(n < 0) |
| 343 | return(-errno); |
| 344 | return(n); |
| 345 | } |
| 346 | |
| 347 | int os_read_file(int fd, void *buf, int len) |
| 348 | { |
| 349 | return(file_io(fd, buf, len, (int (*)(int, void *, int)) read, |
| 350 | copy_from_user_proc)); |
| 351 | } |
| 352 | |
| 353 | int os_write_file(int fd, const void *buf, int len) |
| 354 | { |
| 355 | return(file_io(fd, (void *) buf, len, |
| 356 | (int (*)(int, void *, int)) write, copy_to_user_proc)); |
| 357 | } |
| 358 | |
Jeff Dike | da00d9a | 2005-06-08 15:48:01 -0700 | [diff] [blame] | 359 | int os_file_size(char *file, unsigned long long *size_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
| 361 | struct uml_stat buf; |
| 362 | int err; |
| 363 | |
| 364 | err = os_stat_file(file, &buf); |
| 365 | if(err < 0){ |
| 366 | printk("Couldn't stat \"%s\" : err = %d\n", file, -err); |
| 367 | return(err); |
| 368 | } |
| 369 | |
| 370 | if(S_ISBLK(buf.ust_mode)){ |
| 371 | int fd, blocks; |
| 372 | |
| 373 | fd = os_open_file(file, of_read(OPENFLAGS()), 0); |
| 374 | if(fd < 0){ |
| 375 | printk("Couldn't open \"%s\", errno = %d\n", file, -fd); |
| 376 | return(fd); |
| 377 | } |
| 378 | if(ioctl(fd, BLKGETSIZE, &blocks) < 0){ |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 379 | err = -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | printk("Couldn't get the block size of \"%s\", " |
| 381 | "errno = %d\n", file, errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | os_close_file(fd); |
| 383 | return(err); |
| 384 | } |
| 385 | *size_out = ((long long) blocks) * 512; |
| 386 | os_close_file(fd); |
| 387 | return(0); |
| 388 | } |
| 389 | *size_out = buf.ust_size; |
| 390 | return(0); |
| 391 | } |
| 392 | |
| 393 | int os_file_modtime(char *file, unsigned long *modtime) |
| 394 | { |
| 395 | struct uml_stat buf; |
| 396 | int err; |
| 397 | |
| 398 | err = os_stat_file(file, &buf); |
| 399 | if(err < 0){ |
| 400 | printk("Couldn't stat \"%s\" : err = %d\n", file, -err); |
| 401 | return(err); |
| 402 | } |
| 403 | |
| 404 | *modtime = buf.ust_mtime; |
| 405 | return(0); |
| 406 | } |
| 407 | |
| 408 | int os_get_exec_close(int fd, int* close_on_exec) |
| 409 | { |
| 410 | int ret; |
| 411 | |
| 412 | do { |
| 413 | ret = fcntl(fd, F_GETFD); |
| 414 | } while((ret < 0) && (errno == EINTR)) ; |
| 415 | |
| 416 | if(ret < 0) |
| 417 | return(-errno); |
| 418 | |
| 419 | *close_on_exec = (ret&FD_CLOEXEC) ? 1 : 0; |
| 420 | return(ret); |
| 421 | } |
| 422 | |
| 423 | int os_set_exec_close(int fd, int close_on_exec) |
| 424 | { |
| 425 | int flag, err; |
| 426 | |
| 427 | if(close_on_exec) flag = FD_CLOEXEC; |
| 428 | else flag = 0; |
| 429 | |
| 430 | do { |
| 431 | err = fcntl(fd, F_SETFD, flag); |
| 432 | } while((err < 0) && (errno == EINTR)) ; |
| 433 | |
| 434 | if(err < 0) |
| 435 | return(-errno); |
| 436 | return(err); |
| 437 | } |
| 438 | |
| 439 | int os_pipe(int *fds, int stream, int close_on_exec) |
| 440 | { |
| 441 | int err, type = stream ? SOCK_STREAM : SOCK_DGRAM; |
| 442 | |
| 443 | err = socketpair(AF_UNIX, type, 0, fds); |
| 444 | if(err < 0) |
| 445 | return(-errno); |
| 446 | |
| 447 | if(!close_on_exec) |
| 448 | return(0); |
| 449 | |
| 450 | err = os_set_exec_close(fds[0], 1); |
| 451 | if(err < 0) |
| 452 | goto error; |
| 453 | |
| 454 | err = os_set_exec_close(fds[1], 1); |
| 455 | if(err < 0) |
| 456 | goto error; |
| 457 | |
| 458 | return(0); |
| 459 | |
| 460 | error: |
| 461 | printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err); |
| 462 | os_close_file(fds[1]); |
| 463 | os_close_file(fds[0]); |
| 464 | return(err); |
| 465 | } |
| 466 | |
| 467 | int os_set_fd_async(int fd, int owner) |
| 468 | { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 469 | int err; |
| 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | /* XXX This should do F_GETFL first */ |
| 472 | if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){ |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 473 | err = -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | printk("os_set_fd_async : failed to set O_ASYNC and " |
| 475 | "O_NONBLOCK on fd # %d, errno = %d\n", fd, errno); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 476 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | #ifdef notdef |
| 479 | if(fcntl(fd, F_SETFD, 1) < 0){ |
| 480 | printk("os_set_fd_async : Setting FD_CLOEXEC failed, " |
| 481 | "errno = %d\n", errno); |
| 482 | } |
| 483 | #endif |
| 484 | |
| 485 | if((fcntl(fd, F_SETSIG, SIGIO) < 0) || |
| 486 | (fcntl(fd, F_SETOWN, owner) < 0)){ |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 487 | err = -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | printk("os_set_fd_async : Failed to fcntl F_SETOWN " |
| 489 | "(or F_SETSIG) fd %d to pid %d, errno = %d\n", fd, |
| 490 | owner, errno); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 491 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | return(0); |
| 495 | } |
| 496 | |
| 497 | int os_clear_fd_async(int fd) |
| 498 | { |
| 499 | int flags = fcntl(fd, F_GETFL); |
| 500 | |
| 501 | flags &= ~(O_ASYNC | O_NONBLOCK); |
| 502 | if(fcntl(fd, F_SETFL, flags) < 0) |
| 503 | return(-errno); |
| 504 | return(0); |
| 505 | } |
| 506 | |
| 507 | int os_set_fd_block(int fd, int blocking) |
| 508 | { |
| 509 | int flags; |
| 510 | |
| 511 | flags = fcntl(fd, F_GETFL); |
| 512 | |
| 513 | if(blocking) flags &= ~O_NONBLOCK; |
| 514 | else flags |= O_NONBLOCK; |
| 515 | |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 516 | if(fcntl(fd, F_SETFL, flags) < 0) |
| 517 | return -errno; |
| 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | return(0); |
| 520 | } |
| 521 | |
| 522 | int os_accept_connection(int fd) |
| 523 | { |
| 524 | int new; |
| 525 | |
| 526 | new = accept(fd, NULL, 0); |
| 527 | if(new < 0) |
| 528 | return(-errno); |
| 529 | return(new); |
| 530 | } |
| 531 | |
| 532 | #ifndef SHUT_RD |
| 533 | #define SHUT_RD 0 |
| 534 | #endif |
| 535 | |
| 536 | #ifndef SHUT_WR |
| 537 | #define SHUT_WR 1 |
| 538 | #endif |
| 539 | |
| 540 | #ifndef SHUT_RDWR |
| 541 | #define SHUT_RDWR 2 |
| 542 | #endif |
| 543 | |
| 544 | int os_shutdown_socket(int fd, int r, int w) |
| 545 | { |
| 546 | int what, err; |
| 547 | |
| 548 | if(r && w) what = SHUT_RDWR; |
| 549 | else if(r) what = SHUT_RD; |
| 550 | else if(w) what = SHUT_WR; |
| 551 | else { |
| 552 | printk("os_shutdown_socket : neither r or w was set\n"); |
| 553 | return(-EINVAL); |
| 554 | } |
| 555 | err = shutdown(fd, what); |
| 556 | if(err < 0) |
| 557 | return(-errno); |
| 558 | return(0); |
| 559 | } |
| 560 | |
| 561 | int os_rcv_fd(int fd, int *helper_pid_out) |
| 562 | { |
| 563 | int new, n; |
| 564 | char buf[CMSG_SPACE(sizeof(new))]; |
| 565 | struct msghdr msg; |
| 566 | struct cmsghdr *cmsg; |
| 567 | struct iovec iov; |
| 568 | |
| 569 | msg.msg_name = NULL; |
| 570 | msg.msg_namelen = 0; |
| 571 | iov = ((struct iovec) { .iov_base = helper_pid_out, |
| 572 | .iov_len = sizeof(*helper_pid_out) }); |
| 573 | msg.msg_iov = &iov; |
| 574 | msg.msg_iovlen = 1; |
| 575 | msg.msg_control = buf; |
| 576 | msg.msg_controllen = sizeof(buf); |
| 577 | msg.msg_flags = 0; |
| 578 | |
| 579 | n = recvmsg(fd, &msg, 0); |
| 580 | if(n < 0) |
| 581 | return(-errno); |
| 582 | |
| 583 | else if(n != sizeof(iov.iov_len)) |
| 584 | *helper_pid_out = -1; |
| 585 | |
| 586 | cmsg = CMSG_FIRSTHDR(&msg); |
| 587 | if(cmsg == NULL){ |
| 588 | printk("rcv_fd didn't receive anything, error = %d\n", errno); |
| 589 | return(-1); |
| 590 | } |
| 591 | if((cmsg->cmsg_level != SOL_SOCKET) || |
| 592 | (cmsg->cmsg_type != SCM_RIGHTS)){ |
| 593 | printk("rcv_fd didn't receive a descriptor\n"); |
| 594 | return(-1); |
| 595 | } |
| 596 | |
| 597 | new = ((int *) CMSG_DATA(cmsg))[0]; |
| 598 | return(new); |
| 599 | } |
| 600 | |
| 601 | int os_create_unix_socket(char *file, int len, int close_on_exec) |
| 602 | { |
| 603 | struct sockaddr_un addr; |
| 604 | int sock, err; |
| 605 | |
| 606 | sock = socket(PF_UNIX, SOCK_DGRAM, 0); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 607 | if(sock < 0) |
| 608 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
| 610 | if(close_on_exec) { |
| 611 | err = os_set_exec_close(sock, 1); |
| 612 | if(err < 0) |
| 613 | printk("create_unix_socket : close_on_exec failed, " |
| 614 | "err = %d", -err); |
| 615 | } |
| 616 | |
| 617 | addr.sun_family = AF_UNIX; |
| 618 | |
| 619 | /* XXX Be more careful about overflow */ |
| 620 | snprintf(addr.sun_path, len, "%s", file); |
| 621 | |
| 622 | err = bind(sock, (struct sockaddr *) &addr, sizeof(addr)); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 623 | if(err < 0) |
| 624 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
| 626 | return(sock); |
| 627 | } |
| 628 | |
| 629 | void os_flush_stdout(void) |
| 630 | { |
| 631 | fflush(stdout); |
| 632 | } |
| 633 | |
| 634 | int os_lock_file(int fd, int excl) |
| 635 | { |
| 636 | int type = excl ? F_WRLCK : F_RDLCK; |
| 637 | struct flock lock = ((struct flock) { .l_type = type, |
| 638 | .l_whence = SEEK_SET, |
| 639 | .l_start = 0, |
| 640 | .l_len = 0 } ); |
| 641 | int err, save; |
| 642 | |
| 643 | err = fcntl(fd, F_SETLK, &lock); |
| 644 | if(!err) |
| 645 | goto out; |
| 646 | |
| 647 | save = -errno; |
| 648 | err = fcntl(fd, F_GETLK, &lock); |
| 649 | if(err){ |
| 650 | err = -errno; |
| 651 | goto out; |
| 652 | } |
| 653 | |
| 654 | printk("F_SETLK failed, file already locked by pid %d\n", lock.l_pid); |
| 655 | err = save; |
| 656 | out: |
| 657 | return(err); |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 662 | * Emacs will notice this stuff at the end of the file and automatically |
| 663 | * adjust the settings for this buffer only. This must remain at the end |
| 664 | * of the file. |
| 665 | * --------------------------------------------------------------------------- |
| 666 | * Local variables: |
| 667 | * c-file-style: "linux" |
| 668 | * End: |
| 669 | */ |