root/arch/um/os-Linux/file.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. copy_stat
  2. os_stat_fd
  3. os_stat_file
  4. os_access
  5. os_ioctl_generic
  6. os_get_ifname
  7. os_set_slip
  8. os_mode_fd
  9. os_file_type
  10. os_file_mode
  11. os_open_file
  12. os_connect_socket
  13. os_close_file
  14. os_fsync_file
  15. os_seek_file
  16. os_read_file
  17. os_pread_file
  18. os_write_file
  19. os_sync_file
  20. os_pwrite_file
  21. os_file_size
  22. os_file_modtime
  23. os_set_exec_close
  24. os_pipe
  25. os_set_fd_async
  26. os_clear_fd_async
  27. os_set_fd_block
  28. os_accept_connection
  29. os_shutdown_socket
  30. os_rcv_fd
  31. os_create_unix_socket
  32. os_flush_stdout
  33. os_lock_file
  34. os_major
  35. os_minor
  36. os_makedev
  37. os_falloc_punch
  38. os_eventfd
  39. os_sendmsg_fds

   1 // SPDX-License-Identifier: GPL-2.0
   2 /*
   3  * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
   4  */
   5 
   6 #include <stdio.h>
   7 #include <unistd.h>
   8 #include <errno.h>
   9 #include <fcntl.h>
  10 #include <signal.h>
  11 #include <linux/falloc.h>
  12 #include <sys/ioctl.h>
  13 #include <sys/mount.h>
  14 #include <sys/socket.h>
  15 #include <sys/stat.h>
  16 #include <sys/sysmacros.h>
  17 #include <sys/un.h>
  18 #include <sys/types.h>
  19 #include <sys/eventfd.h>
  20 #include <os.h>
  21 
  22 static void copy_stat(struct uml_stat *dst, const 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         CATCH_EINTR(err = stat64(file_name, &sbuf));
  60         if (err < 0)
  61                 return -errno;
  62 
  63         if (ubuf != NULL)
  64                 copy_stat(ubuf, &sbuf);
  65         return err;
  66 }
  67 
  68 int os_access(const char *file, int mode)
  69 {
  70         int amode, err;
  71 
  72         amode = (mode & OS_ACC_R_OK ? R_OK : 0) |
  73                 (mode & OS_ACC_W_OK ? W_OK : 0) |
  74                 (mode & OS_ACC_X_OK ? X_OK : 0) |
  75                 (mode & OS_ACC_F_OK ? F_OK : 0);
  76 
  77         err = access(file, amode);
  78         if (err < 0)
  79                 return -errno;
  80 
  81         return 0;
  82 }
  83 
  84 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
  85 int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
  86 {
  87         int err;
  88 
  89         err = ioctl(fd, cmd, arg);
  90         if (err < 0)
  91                 return -errno;
  92 
  93         return err;
  94 }
  95 
  96 /* FIXME: ensure namebuf in os_get_if_name is big enough */
  97 int os_get_ifname(int fd, char* namebuf)
  98 {
  99         if (ioctl(fd, SIOCGIFNAME, namebuf) < 0)
 100                 return -errno;
 101 
 102         return 0;
 103 }
 104 
 105 int os_set_slip(int fd)
 106 {
 107         int disc, sencap;
 108 
 109         disc = N_SLIP;
 110         if (ioctl(fd, TIOCSETD, &disc) < 0)
 111                 return -errno;
 112 
 113         sencap = 0;
 114         if (ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
 115                 return -errno;
 116 
 117         return 0;
 118 }
 119 
 120 int os_mode_fd(int fd, int mode)
 121 {
 122         int err;
 123 
 124         CATCH_EINTR(err = fchmod(fd, mode));
 125         if (err < 0)
 126                 return -errno;
 127 
 128         return 0;
 129 }
 130 
 131 int os_file_type(char *file)
 132 {
 133         struct uml_stat buf;
 134         int err;
 135 
 136         err = os_stat_file(file, &buf);
 137         if (err < 0)
 138                 return err;
 139 
 140         if (S_ISDIR(buf.ust_mode))
 141                 return OS_TYPE_DIR;
 142         else if (S_ISLNK(buf.ust_mode))
 143                 return OS_TYPE_SYMLINK;
 144         else if (S_ISCHR(buf.ust_mode))
 145                 return OS_TYPE_CHARDEV;
 146         else if (S_ISBLK(buf.ust_mode))
 147                 return OS_TYPE_BLOCKDEV;
 148         else if (S_ISFIFO(buf.ust_mode))
 149                 return OS_TYPE_FIFO;
 150         else if (S_ISSOCK(buf.ust_mode))
 151                 return OS_TYPE_SOCK;
 152         else return OS_TYPE_FILE;
 153 }
 154 
 155 int os_file_mode(const char *file, struct openflags *mode_out)
 156 {
 157         int err;
 158 
 159         *mode_out = OPENFLAGS();
 160 
 161         err = access(file, W_OK);
 162         if (err && (errno != EACCES))
 163                 return -errno;
 164         else if (!err)
 165                 *mode_out = of_write(*mode_out);
 166 
 167         err = access(file, R_OK);
 168         if (err && (errno != EACCES))
 169                 return -errno;
 170         else if (!err)
 171                 *mode_out = of_read(*mode_out);
 172 
 173         return err;
 174 }
 175 
 176 int os_open_file(const char *file, struct openflags flags, int mode)
 177 {
 178         int fd, err, f = 0;
 179 
 180         if (flags.r && flags.w)
 181                 f = O_RDWR;
 182         else if (flags.r)
 183                 f = O_RDONLY;
 184         else if (flags.w)
 185                 f = O_WRONLY;
 186         else f = 0;
 187 
 188         if (flags.s)
 189                 f |= O_SYNC;
 190         if (flags.c)
 191                 f |= O_CREAT;
 192         if (flags.t)
 193                 f |= O_TRUNC;
 194         if (flags.e)
 195                 f |= O_EXCL;
 196         if (flags.a)
 197                 f |= O_APPEND;
 198 
 199         fd = open64(file, f, mode);
 200         if (fd < 0)
 201                 return -errno;
 202 
 203         if (flags.cl && fcntl(fd, F_SETFD, 1)) {
 204                 err = -errno;
 205                 close(fd);
 206                 return err;
 207         }
 208 
 209         return fd;
 210 }
 211 
 212 int os_connect_socket(const char *name)
 213 {
 214         struct sockaddr_un sock;
 215         int fd, err;
 216 
 217         sock.sun_family = AF_UNIX;
 218         snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
 219 
 220         fd = socket(AF_UNIX, SOCK_STREAM, 0);
 221         if (fd < 0) {
 222                 err = -errno;
 223                 goto out;
 224         }
 225 
 226         err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
 227         if (err) {
 228                 err = -errno;
 229                 goto out_close;
 230         }
 231 
 232         return fd;
 233 
 234 out_close:
 235         close(fd);
 236 out:
 237         return err;
 238 }
 239 
 240 void os_close_file(int fd)
 241 {
 242         close(fd);
 243 }
 244 int os_fsync_file(int fd)
 245 {
 246         if (fsync(fd) < 0)
 247             return -errno;
 248         return 0;
 249 }
 250 
 251 int os_seek_file(int fd, unsigned long long offset)
 252 {
 253         unsigned long long actual;
 254 
 255         actual = lseek64(fd, offset, SEEK_SET);
 256         if (actual != offset)
 257                 return -errno;
 258         return 0;
 259 }
 260 
 261 int os_read_file(int fd, void *buf, int len)
 262 {
 263         int n = read(fd, buf, len);
 264 
 265         if (n < 0)
 266                 return -errno;
 267         return n;
 268 }
 269 
 270 int os_pread_file(int fd, void *buf, int len, unsigned long long offset)
 271 {
 272         int n = pread(fd, buf, len, offset);
 273 
 274         if (n < 0)
 275                 return -errno;
 276         return n;
 277 }
 278 
 279 int os_write_file(int fd, const void *buf, int len)
 280 {
 281         int n = write(fd, (void *) buf, len);
 282 
 283         if (n < 0)
 284                 return -errno;
 285         return n;
 286 }
 287 
 288 int os_sync_file(int fd)
 289 {
 290         int n = fsync(fd);
 291 
 292         if (n < 0)
 293                 return -errno;
 294         return n;
 295 }
 296 
 297 int os_pwrite_file(int fd, const void *buf, int len, unsigned long long offset)
 298 {
 299         int n = pwrite(fd, (void *) buf, len, offset);
 300 
 301         if (n < 0)
 302                 return -errno;
 303         return n;
 304 }
 305 
 306 
 307 int os_file_size(const char *file, unsigned long long *size_out)
 308 {
 309         struct uml_stat buf;
 310         int err;
 311 
 312         err = os_stat_file(file, &buf);
 313         if (err < 0) {
 314                 printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
 315                        -err);
 316                 return err;
 317         }
 318 
 319         if (S_ISBLK(buf.ust_mode)) {
 320                 int fd;
 321                 long blocks;
 322 
 323                 fd = open(file, O_RDONLY, 0);
 324                 if (fd < 0) {
 325                         err = -errno;
 326                         printk(UM_KERN_ERR "Couldn't open \"%s\", "
 327                                "errno = %d\n", file, errno);
 328                         return err;
 329                 }
 330                 if (ioctl(fd, BLKGETSIZE, &blocks) < 0) {
 331                         err = -errno;
 332                         printk(UM_KERN_ERR "Couldn't get the block size of "
 333                                "\"%s\", errno = %d\n", file, errno);
 334                         close(fd);
 335                         return err;
 336                 }
 337                 *size_out = ((long long) blocks) * 512;
 338                 close(fd);
 339         }
 340         else *size_out = buf.ust_size;
 341 
 342         return 0;
 343 }
 344 
 345 int os_file_modtime(const char *file, unsigned long *modtime)
 346 {
 347         struct uml_stat buf;
 348         int err;
 349 
 350         err = os_stat_file(file, &buf);
 351         if (err < 0) {
 352                 printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
 353                        -err);
 354                 return err;
 355         }
 356 
 357         *modtime = buf.ust_mtime;
 358         return 0;
 359 }
 360 
 361 int os_set_exec_close(int fd)
 362 {
 363         int err;
 364 
 365         CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
 366 
 367         if (err < 0)
 368                 return -errno;
 369         return err;
 370 }
 371 
 372 int os_pipe(int *fds, int stream, int close_on_exec)
 373 {
 374         int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
 375 
 376         err = socketpair(AF_UNIX, type, 0, fds);
 377         if (err < 0)
 378                 return -errno;
 379 
 380         if (!close_on_exec)
 381                 return 0;
 382 
 383         err = os_set_exec_close(fds[0]);
 384         if (err < 0)
 385                 goto error;
 386 
 387         err = os_set_exec_close(fds[1]);
 388         if (err < 0)
 389                 goto error;
 390 
 391         return 0;
 392 
 393  error:
 394         printk(UM_KERN_ERR "os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
 395                -err);
 396         close(fds[1]);
 397         close(fds[0]);
 398         return err;
 399 }
 400 
 401 int os_set_fd_async(int fd)
 402 {
 403         int err, flags;
 404 
 405         flags = fcntl(fd, F_GETFL);
 406         if (flags < 0)
 407                 return -errno;
 408 
 409         flags |= O_ASYNC | O_NONBLOCK;
 410         if (fcntl(fd, F_SETFL, flags) < 0) {
 411                 err = -errno;
 412                 printk(UM_KERN_ERR "os_set_fd_async : failed to set O_ASYNC "
 413                        "and O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
 414                 return err;
 415         }
 416 
 417         if ((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
 418             (fcntl(fd, F_SETOWN, os_getpid()) < 0)) {
 419                 err = -errno;
 420                 printk(UM_KERN_ERR "os_set_fd_async : Failed to fcntl F_SETOWN "
 421                        "(or F_SETSIG) fd %d, errno = %d\n", fd, errno);
 422                 return err;
 423         }
 424 
 425         return 0;
 426 }
 427 
 428 int os_clear_fd_async(int fd)
 429 {
 430         int flags;
 431 
 432         flags = fcntl(fd, F_GETFL);
 433         if (flags < 0)
 434                 return -errno;
 435 
 436         flags &= ~(O_ASYNC | O_NONBLOCK);
 437         if (fcntl(fd, F_SETFL, flags) < 0)
 438                 return -errno;
 439         return 0;
 440 }
 441 
 442 int os_set_fd_block(int fd, int blocking)
 443 {
 444         int flags;
 445 
 446         flags = fcntl(fd, F_GETFL);
 447         if (flags < 0)
 448                 return -errno;
 449 
 450         if (blocking)
 451                 flags &= ~O_NONBLOCK;
 452         else
 453                 flags |= O_NONBLOCK;
 454 
 455         if (fcntl(fd, F_SETFL, flags) < 0)
 456                 return -errno;
 457 
 458         return 0;
 459 }
 460 
 461 int os_accept_connection(int fd)
 462 {
 463         int new;
 464 
 465         new = accept(fd, NULL, 0);
 466         if (new < 0)
 467                 return -errno;
 468         return new;
 469 }
 470 
 471 #ifndef SHUT_RD
 472 #define SHUT_RD 0
 473 #endif
 474 
 475 #ifndef SHUT_WR
 476 #define SHUT_WR 1
 477 #endif
 478 
 479 #ifndef SHUT_RDWR
 480 #define SHUT_RDWR 2
 481 #endif
 482 
 483 int os_shutdown_socket(int fd, int r, int w)
 484 {
 485         int what, err;
 486 
 487         if (r && w)
 488                 what = SHUT_RDWR;
 489         else if (r)
 490                 what = SHUT_RD;
 491         else if (w)
 492                 what = SHUT_WR;
 493         else
 494                 return -EINVAL;
 495 
 496         err = shutdown(fd, what);
 497         if (err < 0)
 498                 return -errno;
 499         return 0;
 500 }
 501 
 502 int os_rcv_fd(int fd, int *helper_pid_out)
 503 {
 504         int new, n;
 505         char buf[CMSG_SPACE(sizeof(new))];
 506         struct msghdr msg;
 507         struct cmsghdr *cmsg;
 508         struct iovec iov;
 509 
 510         msg.msg_name = NULL;
 511         msg.msg_namelen = 0;
 512         iov = ((struct iovec) { .iov_base  = helper_pid_out,
 513                                 .iov_len   = sizeof(*helper_pid_out) });
 514         msg.msg_iov = &iov;
 515         msg.msg_iovlen = 1;
 516         msg.msg_control = buf;
 517         msg.msg_controllen = sizeof(buf);
 518         msg.msg_flags = 0;
 519 
 520         n = recvmsg(fd, &msg, 0);
 521         if (n < 0)
 522                 return -errno;
 523         else if (n != iov.iov_len)
 524                 *helper_pid_out = -1;
 525 
 526         cmsg = CMSG_FIRSTHDR(&msg);
 527         if (cmsg == NULL) {
 528                 printk(UM_KERN_ERR "rcv_fd didn't receive anything, "
 529                        "error = %d\n", errno);
 530                 return -1;
 531         }
 532         if ((cmsg->cmsg_level != SOL_SOCKET) ||
 533             (cmsg->cmsg_type != SCM_RIGHTS)) {
 534                 printk(UM_KERN_ERR "rcv_fd didn't receive a descriptor\n");
 535                 return -1;
 536         }
 537 
 538         new = ((int *) CMSG_DATA(cmsg))[0];
 539         return new;
 540 }
 541 
 542 int os_create_unix_socket(const char *file, int len, int close_on_exec)
 543 {
 544         struct sockaddr_un addr;
 545         int sock, err;
 546 
 547         sock = socket(PF_UNIX, SOCK_DGRAM, 0);
 548         if (sock < 0)
 549                 return -errno;
 550 
 551         if (close_on_exec) {
 552                 err = os_set_exec_close(sock);
 553                 if (err < 0)
 554                         printk(UM_KERN_ERR "create_unix_socket : "
 555                                "close_on_exec failed, err = %d", -err);
 556         }
 557 
 558         addr.sun_family = AF_UNIX;
 559 
 560         snprintf(addr.sun_path, len, "%s", file);
 561 
 562         err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
 563         if (err < 0)
 564                 return -errno;
 565 
 566         return sock;
 567 }
 568 
 569 void os_flush_stdout(void)
 570 {
 571         fflush(stdout);
 572 }
 573 
 574 int os_lock_file(int fd, int excl)
 575 {
 576         int type = excl ? F_WRLCK : F_RDLCK;
 577         struct flock lock = ((struct flock) { .l_type   = type,
 578                                               .l_whence = SEEK_SET,
 579                                               .l_start  = 0,
 580                                               .l_len    = 0 } );
 581         int err, save;
 582 
 583         err = fcntl(fd, F_SETLK, &lock);
 584         if (!err)
 585                 goto out;
 586 
 587         save = -errno;
 588         err = fcntl(fd, F_GETLK, &lock);
 589         if (err) {
 590                 err = -errno;
 591                 goto out;
 592         }
 593 
 594         printk(UM_KERN_ERR "F_SETLK failed, file already locked by pid %d\n",
 595                lock.l_pid);
 596         err = save;
 597  out:
 598         return err;
 599 }
 600 
 601 unsigned os_major(unsigned long long dev)
 602 {
 603         return major(dev);
 604 }
 605 
 606 unsigned os_minor(unsigned long long dev)
 607 {
 608         return minor(dev);
 609 }
 610 
 611 unsigned long long os_makedev(unsigned major, unsigned minor)
 612 {
 613         return makedev(major, minor);
 614 }
 615 
 616 int os_falloc_punch(int fd, unsigned long long offset, int len)
 617 {
 618         int n = fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, len);
 619 
 620         if (n < 0)
 621                 return -errno;
 622         return n;
 623 }
 624 
 625 int os_eventfd(unsigned int initval, int flags)
 626 {
 627         int fd = eventfd(initval, flags);
 628 
 629         if (fd < 0)
 630                 return -errno;
 631         return fd;
 632 }
 633 
 634 int os_sendmsg_fds(int fd, const void *buf, unsigned int len, const int *fds,
 635                    unsigned int fds_num)
 636 {
 637         struct iovec iov = {
 638                 .iov_base = (void *) buf,
 639                 .iov_len = len,
 640         };
 641         union {
 642                 char control[CMSG_SPACE(sizeof(*fds) * OS_SENDMSG_MAX_FDS)];
 643                 struct cmsghdr align;
 644         } u;
 645         unsigned int fds_size = sizeof(*fds) * fds_num;
 646         struct msghdr msg = {
 647                 .msg_iov = &iov,
 648                 .msg_iovlen = 1,
 649                 .msg_control = u.control,
 650                 .msg_controllen = CMSG_SPACE(fds_size),
 651         };
 652         struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
 653         int err;
 654 
 655         if (fds_num > OS_SENDMSG_MAX_FDS)
 656                 return -EINVAL;
 657         memset(u.control, 0, sizeof(u.control));
 658         cmsg->cmsg_level = SOL_SOCKET;
 659         cmsg->cmsg_type = SCM_RIGHTS;
 660         cmsg->cmsg_len = CMSG_LEN(fds_size);
 661         memcpy(CMSG_DATA(cmsg), fds, fds_size);
 662         err = sendmsg(fd, &msg, 0);
 663 
 664         if (err < 0)
 665                 return -errno;
 666         return err;
 667 }

/* [<][>][^][v][top][bottom][index][help] */