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