x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / arch / um / os-Linux / file.c
blobc0197097c86e5075c146ad17142895e70b9fe25c
1 /*
2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <sys/ioctl.h>
12 #include <sys/mount.h>
13 #include <sys/socket.h>
14 #include <sys/stat.h>
15 #include <sys/sysmacros.h>
16 #include <sys/un.h>
17 #include <sys/types.h>
18 #include <os.h>
20 static void copy_stat(struct uml_stat *dst, const struct stat64 *src)
22 *dst = ((struct uml_stat) {
23 .ust_dev = src->st_dev, /* device */
24 .ust_ino = src->st_ino, /* inode */
25 .ust_mode = src->st_mode, /* protection */
26 .ust_nlink = src->st_nlink, /* number of hard links */
27 .ust_uid = src->st_uid, /* user ID of owner */
28 .ust_gid = src->st_gid, /* group ID of owner */
29 .ust_size = src->st_size, /* total size, in bytes */
30 .ust_blksize = src->st_blksize, /* blocksize for filesys I/O */
31 .ust_blocks = src->st_blocks, /* number of blocks allocated */
32 .ust_atime = src->st_atime, /* time of last access */
33 .ust_mtime = src->st_mtime, /* time of last modification */
34 .ust_ctime = src->st_ctime, /* time of last change */
35 });
38 int os_stat_fd(const int fd, struct uml_stat *ubuf)
40 struct stat64 sbuf;
41 int err;
43 CATCH_EINTR(err = fstat64(fd, &sbuf));
44 if (err < 0)
45 return -errno;
47 if (ubuf != NULL)
48 copy_stat(ubuf, &sbuf);
49 return err;
52 int os_stat_file(const char *file_name, struct uml_stat *ubuf)
54 struct stat64 sbuf;
55 int err;
57 CATCH_EINTR(err = stat64(file_name, &sbuf));
58 if (err < 0)
59 return -errno;
61 if (ubuf != NULL)
62 copy_stat(ubuf, &sbuf);
63 return err;
66 int os_access(const char *file, int mode)
68 int amode, err;
70 amode = (mode & OS_ACC_R_OK ? R_OK : 0) |
71 (mode & OS_ACC_W_OK ? W_OK : 0) |
72 (mode & OS_ACC_X_OK ? X_OK : 0) |
73 (mode & OS_ACC_F_OK ? F_OK : 0);
75 err = access(file, amode);
76 if (err < 0)
77 return -errno;
79 return 0;
82 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
83 int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
85 int err;
87 err = ioctl(fd, cmd, arg);
88 if (err < 0)
89 return -errno;
91 return err;
94 /* FIXME: ensure namebuf in os_get_if_name is big enough */
95 int os_get_ifname(int fd, char* namebuf)
97 if (ioctl(fd, SIOCGIFNAME, namebuf) < 0)
98 return -errno;
100 return 0;
103 int os_set_slip(int fd)
105 int disc, sencap;
107 disc = N_SLIP;
108 if (ioctl(fd, TIOCSETD, &disc) < 0)
109 return -errno;
111 sencap = 0;
112 if (ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
113 return -errno;
115 return 0;
118 int os_mode_fd(int fd, int mode)
120 int err;
122 CATCH_EINTR(err = fchmod(fd, mode));
123 if (err < 0)
124 return -errno;
126 return 0;
129 int os_file_type(char *file)
131 struct uml_stat buf;
132 int err;
134 err = os_stat_file(file, &buf);
135 if (err < 0)
136 return err;
138 if (S_ISDIR(buf.ust_mode))
139 return OS_TYPE_DIR;
140 else if (S_ISLNK(buf.ust_mode))
141 return OS_TYPE_SYMLINK;
142 else if (S_ISCHR(buf.ust_mode))
143 return OS_TYPE_CHARDEV;
144 else if (S_ISBLK(buf.ust_mode))
145 return OS_TYPE_BLOCKDEV;
146 else if (S_ISFIFO(buf.ust_mode))
147 return OS_TYPE_FIFO;
148 else if (S_ISSOCK(buf.ust_mode))
149 return OS_TYPE_SOCK;
150 else return OS_TYPE_FILE;
153 int os_file_mode(const char *file, struct openflags *mode_out)
155 int err;
157 *mode_out = OPENFLAGS();
159 err = access(file, W_OK);
160 if (err && (errno != EACCES))
161 return -errno;
162 else if (!err)
163 *mode_out = of_write(*mode_out);
165 err = access(file, R_OK);
166 if (err && (errno != EACCES))
167 return -errno;
168 else if (!err)
169 *mode_out = of_read(*mode_out);
171 return err;
174 int os_open_file(const char *file, struct openflags flags, int mode)
176 int fd, err, f = 0;
178 if (flags.r && flags.w)
179 f = O_RDWR;
180 else if (flags.r)
181 f = O_RDONLY;
182 else if (flags.w)
183 f = O_WRONLY;
184 else f = 0;
186 if (flags.s)
187 f |= O_SYNC;
188 if (flags.c)
189 f |= O_CREAT;
190 if (flags.t)
191 f |= O_TRUNC;
192 if (flags.e)
193 f |= O_EXCL;
194 if (flags.a)
195 f |= O_APPEND;
197 fd = open64(file, f, mode);
198 if (fd < 0)
199 return -errno;
201 if (flags.cl && fcntl(fd, F_SETFD, 1)) {
202 err = -errno;
203 close(fd);
204 return err;
207 return fd;
210 int os_connect_socket(const char *name)
212 struct sockaddr_un sock;
213 int fd, err;
215 sock.sun_family = AF_UNIX;
216 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
218 fd = socket(AF_UNIX, SOCK_STREAM, 0);
219 if (fd < 0) {
220 err = -errno;
221 goto out;
224 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
225 if (err) {
226 err = -errno;
227 goto out_close;
230 return fd;
232 out_close:
233 close(fd);
234 out:
235 return err;
238 void os_close_file(int fd)
240 close(fd);
242 int os_fsync_file(int fd)
244 if (fsync(fd) < 0)
245 return -errno;
246 return 0;
249 int os_seek_file(int fd, unsigned long long offset)
251 unsigned long long actual;
253 actual = lseek64(fd, offset, SEEK_SET);
254 if (actual != offset)
255 return -errno;
256 return 0;
259 int os_read_file(int fd, void *buf, int len)
261 int n = read(fd, buf, len);
263 if (n < 0)
264 return -errno;
265 return n;
268 int os_pread_file(int fd, void *buf, int len, unsigned long long offset)
270 int n = pread(fd, buf, len, offset);
272 if (n < 0)
273 return -errno;
274 return n;
277 int os_write_file(int fd, const void *buf, int len)
279 int n = write(fd, (void *) buf, len);
281 if (n < 0)
282 return -errno;
283 return n;
286 int os_sync_file(int fd)
288 int n = fsync(fd);
290 if (n < 0)
291 return -errno;
292 return n;
295 int os_pwrite_file(int fd, const void *buf, int len, unsigned long long offset)
297 int n = pwrite(fd, (void *) buf, len, offset);
299 if (n < 0)
300 return -errno;
301 return n;
305 int os_file_size(const char *file, unsigned long long *size_out)
307 struct uml_stat buf;
308 int err;
310 err = os_stat_file(file, &buf);
311 if (err < 0) {
312 printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
313 -err);
314 return err;
317 if (S_ISBLK(buf.ust_mode)) {
318 int fd;
319 long blocks;
321 fd = open(file, O_RDONLY, 0);
322 if (fd < 0) {
323 err = -errno;
324 printk(UM_KERN_ERR "Couldn't open \"%s\", "
325 "errno = %d\n", file, errno);
326 return err;
328 if (ioctl(fd, BLKGETSIZE, &blocks) < 0) {
329 err = -errno;
330 printk(UM_KERN_ERR "Couldn't get the block size of "
331 "\"%s\", errno = %d\n", file, errno);
332 close(fd);
333 return err;
335 *size_out = ((long long) blocks) * 512;
336 close(fd);
338 else *size_out = buf.ust_size;
340 return 0;
343 int os_file_modtime(const char *file, unsigned long *modtime)
345 struct uml_stat buf;
346 int err;
348 err = os_stat_file(file, &buf);
349 if (err < 0) {
350 printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
351 -err);
352 return err;
355 *modtime = buf.ust_mtime;
356 return 0;
359 int os_set_exec_close(int fd)
361 int err;
363 CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
365 if (err < 0)
366 return -errno;
367 return err;
370 int os_pipe(int *fds, int stream, int close_on_exec)
372 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
374 err = socketpair(AF_UNIX, type, 0, fds);
375 if (err < 0)
376 return -errno;
378 if (!close_on_exec)
379 return 0;
381 err = os_set_exec_close(fds[0]);
382 if (err < 0)
383 goto error;
385 err = os_set_exec_close(fds[1]);
386 if (err < 0)
387 goto error;
389 return 0;
391 error:
392 printk(UM_KERN_ERR "os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
393 -err);
394 close(fds[1]);
395 close(fds[0]);
396 return err;
399 int os_set_fd_async(int fd)
401 int err, flags;
403 flags = fcntl(fd, F_GETFL);
404 if (flags < 0)
405 return -errno;
407 flags |= O_ASYNC | O_NONBLOCK;
408 if (fcntl(fd, F_SETFL, flags) < 0) {
409 err = -errno;
410 printk(UM_KERN_ERR "os_set_fd_async : failed to set O_ASYNC "
411 "and O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
412 return err;
415 if ((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
416 (fcntl(fd, F_SETOWN, os_getpid()) < 0)) {
417 err = -errno;
418 printk(UM_KERN_ERR "os_set_fd_async : Failed to fcntl F_SETOWN "
419 "(or F_SETSIG) fd %d, errno = %d\n", fd, errno);
420 return err;
423 return 0;
426 int os_clear_fd_async(int fd)
428 int flags;
430 flags = fcntl(fd, F_GETFL);
431 if (flags < 0)
432 return -errno;
434 flags &= ~(O_ASYNC | O_NONBLOCK);
435 if (fcntl(fd, F_SETFL, flags) < 0)
436 return -errno;
437 return 0;
440 int os_set_fd_block(int fd, int blocking)
442 int flags;
444 flags = fcntl(fd, F_GETFL);
445 if (flags < 0)
446 return -errno;
448 if (blocking)
449 flags &= ~O_NONBLOCK;
450 else
451 flags |= O_NONBLOCK;
453 if (fcntl(fd, F_SETFL, flags) < 0)
454 return -errno;
456 return 0;
459 int os_accept_connection(int fd)
461 int new;
463 new = accept(fd, NULL, 0);
464 if (new < 0)
465 return -errno;
466 return new;
469 #ifndef SHUT_RD
470 #define SHUT_RD 0
471 #endif
473 #ifndef SHUT_WR
474 #define SHUT_WR 1
475 #endif
477 #ifndef SHUT_RDWR
478 #define SHUT_RDWR 2
479 #endif
481 int os_shutdown_socket(int fd, int r, int w)
483 int what, err;
485 if (r && w)
486 what = SHUT_RDWR;
487 else if (r)
488 what = SHUT_RD;
489 else if (w)
490 what = SHUT_WR;
491 else
492 return -EINVAL;
494 err = shutdown(fd, what);
495 if (err < 0)
496 return -errno;
497 return 0;
500 int os_rcv_fd(int fd, int *helper_pid_out)
502 int new, n;
503 char buf[CMSG_SPACE(sizeof(new))];
504 struct msghdr msg;
505 struct cmsghdr *cmsg;
506 struct iovec iov;
508 msg.msg_name = NULL;
509 msg.msg_namelen = 0;
510 iov = ((struct iovec) { .iov_base = helper_pid_out,
511 .iov_len = sizeof(*helper_pid_out) });
512 msg.msg_iov = &iov;
513 msg.msg_iovlen = 1;
514 msg.msg_control = buf;
515 msg.msg_controllen = sizeof(buf);
516 msg.msg_flags = 0;
518 n = recvmsg(fd, &msg, 0);
519 if (n < 0)
520 return -errno;
521 else if (n != iov.iov_len)
522 *helper_pid_out = -1;
524 cmsg = CMSG_FIRSTHDR(&msg);
525 if (cmsg == NULL) {
526 printk(UM_KERN_ERR "rcv_fd didn't receive anything, "
527 "error = %d\n", errno);
528 return -1;
530 if ((cmsg->cmsg_level != SOL_SOCKET) ||
531 (cmsg->cmsg_type != SCM_RIGHTS)) {
532 printk(UM_KERN_ERR "rcv_fd didn't receive a descriptor\n");
533 return -1;
536 new = ((int *) CMSG_DATA(cmsg))[0];
537 return new;
540 int os_create_unix_socket(const char *file, int len, int close_on_exec)
542 struct sockaddr_un addr;
543 int sock, err;
545 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
546 if (sock < 0)
547 return -errno;
549 if (close_on_exec) {
550 err = os_set_exec_close(sock);
551 if (err < 0)
552 printk(UM_KERN_ERR "create_unix_socket : "
553 "close_on_exec failed, err = %d", -err);
556 addr.sun_family = AF_UNIX;
558 snprintf(addr.sun_path, len, "%s", file);
560 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
561 if (err < 0)
562 return -errno;
564 return sock;
567 void os_flush_stdout(void)
569 fflush(stdout);
572 int os_lock_file(int fd, int excl)
574 int type = excl ? F_WRLCK : F_RDLCK;
575 struct flock lock = ((struct flock) { .l_type = type,
576 .l_whence = SEEK_SET,
577 .l_start = 0,
578 .l_len = 0 } );
579 int err, save;
581 err = fcntl(fd, F_SETLK, &lock);
582 if (!err)
583 goto out;
585 save = -errno;
586 err = fcntl(fd, F_GETLK, &lock);
587 if (err) {
588 err = -errno;
589 goto out;
592 printk(UM_KERN_ERR "F_SETLK failed, file already locked by pid %d\n",
593 lock.l_pid);
594 err = save;
595 out:
596 return err;
599 unsigned os_major(unsigned long long dev)
601 return major(dev);
604 unsigned os_minor(unsigned long long dev)
606 return minor(dev);
609 unsigned long long os_makedev(unsigned major, unsigned minor)
611 return makedev(major, minor);