[PATCH] uml: preserve errno in error paths
[usb.git] / arch / um / os-Linux / file.c
blobf55773c819e64fa6be68fe1d319326b11958c195
1 /*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.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/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"
22 static void copy_stat(struct uml_stat *dst, struct stat64 *src)
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 });
40 int os_stat_fd(const int fd, struct uml_stat *ubuf)
42 struct stat64 sbuf;
43 int err;
45 do {
46 err = fstat64(fd, &sbuf);
47 } while((err < 0) && (errno == EINTR)) ;
49 if(err < 0)
50 return(-errno);
52 if(ubuf != NULL)
53 copy_stat(ubuf, &sbuf);
54 return(err);
57 int os_stat_file(const char *file_name, struct uml_stat *ubuf)
59 struct stat64 sbuf;
60 int err;
62 do {
63 err = stat64(file_name, &sbuf);
64 } while((err < 0) && (errno == EINTR)) ;
66 if(err < 0)
67 return(-errno);
69 if(ubuf != NULL)
70 copy_stat(ubuf, &sbuf);
71 return(err);
74 int os_access(const char* file, int mode)
76 int amode, err;
78 amode=(mode&OS_ACC_R_OK ? R_OK : 0) | (mode&OS_ACC_W_OK ? W_OK : 0) |
79 (mode&OS_ACC_X_OK ? X_OK : 0) | (mode&OS_ACC_F_OK ? F_OK : 0) ;
81 err = access(file, amode);
82 if(err < 0)
83 return(-errno);
85 return(0);
88 void os_print_error(int error, const char* str)
90 errno = error < 0 ? -error : error;
92 perror(str);
95 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
96 int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
98 int err;
100 err = ioctl(fd, cmd, arg);
101 if(err < 0)
102 return(-errno);
104 return(err);
107 int os_window_size(int fd, int *rows, int *cols)
109 struct winsize size;
111 if(ioctl(fd, TIOCGWINSZ, &size) < 0)
112 return(-errno);
114 *rows = size.ws_row;
115 *cols = size.ws_col;
117 return(0);
120 int os_new_tty_pgrp(int fd, int pid)
122 if(ioctl(fd, TIOCSCTTY, 0) < 0)
123 return -errno;
125 if(tcsetpgrp(fd, pid) < 0)
126 return -errno;
128 return(0);
131 /* FIXME: ensure namebuf in os_get_if_name is big enough */
132 int os_get_ifname(int fd, char* namebuf)
134 if(ioctl(fd, SIOCGIFNAME, namebuf) < 0)
135 return(-errno);
137 return(0);
140 int os_set_slip(int fd)
142 int disc, sencap;
144 disc = N_SLIP;
145 if(ioctl(fd, TIOCSETD, &disc) < 0)
146 return -errno;
148 sencap = 0;
149 if(ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
150 return -errno;
152 return(0);
155 int os_set_owner(int fd, int pid)
157 if(fcntl(fd, F_SETOWN, pid) < 0){
158 int save_errno = errno;
160 if(fcntl(fd, F_GETOWN, 0) != pid)
161 return(-save_errno);
164 return(0);
167 /* FIXME? moved wholesale from sigio_user.c to get fcntls out of that file */
168 int os_sigio_async(int master, int slave)
170 int flags;
172 flags = fcntl(master, F_GETFL);
173 if(flags < 0)
174 return errno;
176 if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
177 (fcntl(master, F_SETOWN, os_getpid()) < 0))
178 return -errno;
180 if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
181 return -errno;
183 return(0);
186 int os_mode_fd(int fd, int mode)
188 int err;
190 do {
191 err = fchmod(fd, mode);
192 } while((err < 0) && (errno==EINTR)) ;
194 if(err < 0)
195 return(-errno);
197 return(0);
200 int os_file_type(char *file)
202 struct uml_stat buf;
203 int err;
205 err = os_stat_file(file, &buf);
206 if(err < 0)
207 return(err);
209 if(S_ISDIR(buf.ust_mode)) return(OS_TYPE_DIR);
210 else if(S_ISLNK(buf.ust_mode)) return(OS_TYPE_SYMLINK);
211 else if(S_ISCHR(buf.ust_mode)) return(OS_TYPE_CHARDEV);
212 else if(S_ISBLK(buf.ust_mode)) return(OS_TYPE_BLOCKDEV);
213 else if(S_ISFIFO(buf.ust_mode)) return(OS_TYPE_FIFO);
214 else if(S_ISSOCK(buf.ust_mode)) return(OS_TYPE_SOCK);
215 else return(OS_TYPE_FILE);
218 int os_file_mode(char *file, struct openflags *mode_out)
220 int err;
222 *mode_out = OPENFLAGS();
224 err = os_access(file, OS_ACC_W_OK);
225 if((err < 0) && (err != -EACCES))
226 return(err);
228 *mode_out = of_write(*mode_out);
230 err = os_access(file, OS_ACC_R_OK);
231 if((err < 0) && (err != -EACCES))
232 return(err);
234 *mode_out = of_read(*mode_out);
236 return(0);
239 int os_open_file(char *file, struct openflags flags, int mode)
241 int fd, err, f = 0;
243 if(flags.r && flags.w) f = O_RDWR;
244 else if(flags.r) f = O_RDONLY;
245 else if(flags.w) f = O_WRONLY;
246 else f = 0;
248 if(flags.s) f |= O_SYNC;
249 if(flags.c) f |= O_CREAT;
250 if(flags.t) f |= O_TRUNC;
251 if(flags.e) f |= O_EXCL;
253 fd = open64(file, f, mode);
254 if(fd < 0)
255 return(-errno);
257 if(flags.cl && fcntl(fd, F_SETFD, 1)){
258 err = -errno;
259 os_close_file(fd);
260 return err;
263 return(fd);
266 int os_connect_socket(char *name)
268 struct sockaddr_un sock;
269 int fd, err;
271 sock.sun_family = AF_UNIX;
272 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
274 fd = socket(AF_UNIX, SOCK_STREAM, 0);
275 if(fd < 0)
276 return(fd);
278 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
279 if(err)
280 return(-errno);
282 return(fd);
285 void os_close_file(int fd)
287 close(fd);
290 int os_seek_file(int fd, __u64 offset)
292 __u64 actual;
294 actual = lseek64(fd, offset, SEEK_SET);
295 if(actual != offset)
296 return(-errno);
297 return(0);
300 static int fault_buffer(void *start, int len,
301 int (*copy_proc)(void *addr, void *buf, int len))
303 int page = getpagesize(), i;
304 char c;
306 for(i = 0; i < len; i += page){
307 if((*copy_proc)(start + i, &c, sizeof(c)))
308 return(-EFAULT);
310 if((len % page) != 0){
311 if((*copy_proc)(start + len - 1, &c, sizeof(c)))
312 return(-EFAULT);
314 return(0);
317 static int file_io(int fd, void *buf, int len,
318 int (*io_proc)(int fd, void *buf, int len),
319 int (*copy_user_proc)(void *addr, void *buf, int len))
321 int n, err;
323 do {
324 n = (*io_proc)(fd, buf, len);
325 if((n < 0) && (errno == EFAULT)){
326 err = fault_buffer(buf, len, copy_user_proc);
327 if(err)
328 return(err);
329 n = (*io_proc)(fd, buf, len);
331 } while((n < 0) && (errno == EINTR));
333 if(n < 0)
334 return(-errno);
335 return(n);
338 int os_read_file(int fd, void *buf, int len)
340 return(file_io(fd, buf, len, (int (*)(int, void *, int)) read,
341 copy_from_user_proc));
344 int os_write_file(int fd, const void *buf, int len)
346 return(file_io(fd, (void *) buf, len,
347 (int (*)(int, void *, int)) write, copy_to_user_proc));
350 int os_file_size(char *file, unsigned long long *size_out)
352 struct uml_stat buf;
353 int err;
355 err = os_stat_file(file, &buf);
356 if(err < 0){
357 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
358 return(err);
361 if(S_ISBLK(buf.ust_mode)){
362 int fd, blocks;
364 fd = os_open_file(file, of_read(OPENFLAGS()), 0);
365 if(fd < 0){
366 printk("Couldn't open \"%s\", errno = %d\n", file, -fd);
367 return(fd);
369 if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
370 err = -errno;
371 printk("Couldn't get the block size of \"%s\", "
372 "errno = %d\n", file, errno);
373 os_close_file(fd);
374 return(err);
376 *size_out = ((long long) blocks) * 512;
377 os_close_file(fd);
378 return(0);
380 *size_out = buf.ust_size;
381 return(0);
384 int os_file_modtime(char *file, unsigned long *modtime)
386 struct uml_stat buf;
387 int err;
389 err = os_stat_file(file, &buf);
390 if(err < 0){
391 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
392 return(err);
395 *modtime = buf.ust_mtime;
396 return(0);
399 int os_get_exec_close(int fd, int* close_on_exec)
401 int ret;
403 do {
404 ret = fcntl(fd, F_GETFD);
405 } while((ret < 0) && (errno == EINTR)) ;
407 if(ret < 0)
408 return(-errno);
410 *close_on_exec = (ret&FD_CLOEXEC) ? 1 : 0;
411 return(ret);
414 int os_set_exec_close(int fd, int close_on_exec)
416 int flag, err;
418 if(close_on_exec) flag = FD_CLOEXEC;
419 else flag = 0;
421 do {
422 err = fcntl(fd, F_SETFD, flag);
423 } while((err < 0) && (errno == EINTR)) ;
425 if(err < 0)
426 return(-errno);
427 return(err);
430 int os_pipe(int *fds, int stream, int close_on_exec)
432 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
434 err = socketpair(AF_UNIX, type, 0, fds);
435 if(err < 0)
436 return(-errno);
438 if(!close_on_exec)
439 return(0);
441 err = os_set_exec_close(fds[0], 1);
442 if(err < 0)
443 goto error;
445 err = os_set_exec_close(fds[1], 1);
446 if(err < 0)
447 goto error;
449 return(0);
451 error:
452 printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err);
453 os_close_file(fds[1]);
454 os_close_file(fds[0]);
455 return(err);
458 int os_set_fd_async(int fd, int owner)
460 int err;
462 /* XXX This should do F_GETFL first */
463 if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){
464 err = -errno;
465 printk("os_set_fd_async : failed to set O_ASYNC and "
466 "O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
467 return err;
469 #ifdef notdef
470 if(fcntl(fd, F_SETFD, 1) < 0){
471 printk("os_set_fd_async : Setting FD_CLOEXEC failed, "
472 "errno = %d\n", errno);
474 #endif
476 if((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
477 (fcntl(fd, F_SETOWN, owner) < 0)){
478 err = -errno;
479 printk("os_set_fd_async : Failed to fcntl F_SETOWN "
480 "(or F_SETSIG) fd %d to pid %d, errno = %d\n", fd,
481 owner, errno);
482 return err;
485 return(0);
488 int os_clear_fd_async(int fd)
490 int flags = fcntl(fd, F_GETFL);
492 flags &= ~(O_ASYNC | O_NONBLOCK);
493 if(fcntl(fd, F_SETFL, flags) < 0)
494 return(-errno);
495 return(0);
498 int os_set_fd_block(int fd, int blocking)
500 int flags;
502 flags = fcntl(fd, F_GETFL);
504 if(blocking) flags &= ~O_NONBLOCK;
505 else flags |= O_NONBLOCK;
507 if(fcntl(fd, F_SETFL, flags) < 0)
508 return -errno;
510 return(0);
513 int os_accept_connection(int fd)
515 int new;
517 new = accept(fd, NULL, 0);
518 if(new < 0)
519 return(-errno);
520 return(new);
523 #ifndef SHUT_RD
524 #define SHUT_RD 0
525 #endif
527 #ifndef SHUT_WR
528 #define SHUT_WR 1
529 #endif
531 #ifndef SHUT_RDWR
532 #define SHUT_RDWR 2
533 #endif
535 int os_shutdown_socket(int fd, int r, int w)
537 int what, err;
539 if(r && w) what = SHUT_RDWR;
540 else if(r) what = SHUT_RD;
541 else if(w) what = SHUT_WR;
542 else {
543 printk("os_shutdown_socket : neither r or w was set\n");
544 return(-EINVAL);
546 err = shutdown(fd, what);
547 if(err < 0)
548 return(-errno);
549 return(0);
552 int os_rcv_fd(int fd, int *helper_pid_out)
554 int new, n;
555 char buf[CMSG_SPACE(sizeof(new))];
556 struct msghdr msg;
557 struct cmsghdr *cmsg;
558 struct iovec iov;
560 msg.msg_name = NULL;
561 msg.msg_namelen = 0;
562 iov = ((struct iovec) { .iov_base = helper_pid_out,
563 .iov_len = sizeof(*helper_pid_out) });
564 msg.msg_iov = &iov;
565 msg.msg_iovlen = 1;
566 msg.msg_control = buf;
567 msg.msg_controllen = sizeof(buf);
568 msg.msg_flags = 0;
570 n = recvmsg(fd, &msg, 0);
571 if(n < 0)
572 return(-errno);
574 else if(n != sizeof(iov.iov_len))
575 *helper_pid_out = -1;
577 cmsg = CMSG_FIRSTHDR(&msg);
578 if(cmsg == NULL){
579 printk("rcv_fd didn't receive anything, error = %d\n", errno);
580 return(-1);
582 if((cmsg->cmsg_level != SOL_SOCKET) ||
583 (cmsg->cmsg_type != SCM_RIGHTS)){
584 printk("rcv_fd didn't receive a descriptor\n");
585 return(-1);
588 new = ((int *) CMSG_DATA(cmsg))[0];
589 return(new);
592 int os_create_unix_socket(char *file, int len, int close_on_exec)
594 struct sockaddr_un addr;
595 int sock, err;
597 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
598 if(sock < 0)
599 return -errno;
601 if(close_on_exec) {
602 err = os_set_exec_close(sock, 1);
603 if(err < 0)
604 printk("create_unix_socket : close_on_exec failed, "
605 "err = %d", -err);
608 addr.sun_family = AF_UNIX;
610 /* XXX Be more careful about overflow */
611 snprintf(addr.sun_path, len, "%s", file);
613 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
614 if(err < 0)
615 return -errno;
617 return(sock);
620 void os_flush_stdout(void)
622 fflush(stdout);
625 int os_lock_file(int fd, int excl)
627 int type = excl ? F_WRLCK : F_RDLCK;
628 struct flock lock = ((struct flock) { .l_type = type,
629 .l_whence = SEEK_SET,
630 .l_start = 0,
631 .l_len = 0 } );
632 int err, save;
634 err = fcntl(fd, F_SETLK, &lock);
635 if(!err)
636 goto out;
638 save = -errno;
639 err = fcntl(fd, F_GETLK, &lock);
640 if(err){
641 err = -errno;
642 goto out;
645 printk("F_SETLK failed, file already locked by pid %d\n", lock.l_pid);
646 err = save;
647 out:
648 return(err);
652 * Overrides for Emacs so that we follow Linus's tabbing style.
653 * Emacs will notice this stuff at the end of the file and automatically
654 * adjust the settings for this buffer only. This must remain at the end
655 * of the file.
656 * ---------------------------------------------------------------------------
657 * Local variables:
658 * c-file-style: "linux"
659 * End: