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