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