[IPV4]: Correct rp_filter help text.
[linux-2.6/verdex.git] / arch / um / os-Linux / file.c
blob6f92f732d25346aef20f43eda05efe8d7f9d8c12
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 CATCH_EINTR(err = fstat64(fd, &sbuf));
46 if(err < 0)
47 return -errno;
49 if(ubuf != NULL)
50 copy_stat(ubuf, &sbuf);
51 return err;
54 int os_stat_file(const char *file_name, struct uml_stat *ubuf)
56 struct stat64 sbuf;
57 int err;
59 do {
60 err = stat64(file_name, &sbuf);
61 } while((err < 0) && (errno == EINTR)) ;
63 if(err < 0)
64 return -errno;
66 if(ubuf != NULL)
67 copy_stat(ubuf, &sbuf);
68 return err;
71 int os_access(const char* file, int mode)
73 int amode, err;
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) ;
78 err = access(file, amode);
79 if(err < 0)
80 return -errno;
82 return 0;
85 void os_print_error(int error, const char* str)
87 errno = error < 0 ? -error : error;
89 perror(str);
92 /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
93 int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
95 int err;
97 err = ioctl(fd, cmd, arg);
98 if(err < 0)
99 return -errno;
101 return err;
104 int os_window_size(int fd, int *rows, int *cols)
106 struct winsize size;
108 if(ioctl(fd, TIOCGWINSZ, &size) < 0)
109 return -errno;
111 *rows = size.ws_row;
112 *cols = size.ws_col;
114 return 0;
117 int os_new_tty_pgrp(int fd, int pid)
119 if(ioctl(fd, TIOCSCTTY, 0) < 0)
120 return -errno;
122 if(tcsetpgrp(fd, pid) < 0)
123 return -errno;
125 return 0;
128 /* FIXME: ensure namebuf in os_get_if_name is big enough */
129 int os_get_ifname(int fd, char* namebuf)
131 if(ioctl(fd, SIOCGIFNAME, namebuf) < 0)
132 return -errno;
134 return 0;
137 int os_set_slip(int fd)
139 int disc, sencap;
141 disc = N_SLIP;
142 if(ioctl(fd, TIOCSETD, &disc) < 0)
143 return -errno;
145 sencap = 0;
146 if(ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
147 return -errno;
149 return 0;
152 int os_set_owner(int fd, int pid)
154 if(fcntl(fd, F_SETOWN, pid) < 0){
155 int save_errno = errno;
157 if(fcntl(fd, F_GETOWN, 0) != pid)
158 return -save_errno;
161 return 0;
164 int os_mode_fd(int fd, int mode)
166 int err;
168 do {
169 err = fchmod(fd, mode);
170 } while((err < 0) && (errno==EINTR)) ;
172 if(err < 0)
173 return -errno;
175 return 0;
178 int os_file_type(char *file)
180 struct uml_stat buf;
181 int err;
183 err = os_stat_file(file, &buf);
184 if(err < 0)
185 return err;
187 if(S_ISDIR(buf.ust_mode))
188 return OS_TYPE_DIR;
189 else if(S_ISLNK(buf.ust_mode))
190 return OS_TYPE_SYMLINK;
191 else if(S_ISCHR(buf.ust_mode))
192 return OS_TYPE_CHARDEV;
193 else if(S_ISBLK(buf.ust_mode))
194 return OS_TYPE_BLOCKDEV;
195 else if(S_ISFIFO(buf.ust_mode))
196 return OS_TYPE_FIFO;
197 else if(S_ISSOCK(buf.ust_mode))
198 return OS_TYPE_SOCK;
199 else return OS_TYPE_FILE;
202 int os_file_mode(char *file, struct openflags *mode_out)
204 int err;
206 *mode_out = OPENFLAGS();
208 err = os_access(file, OS_ACC_W_OK);
209 if((err < 0) && (err != -EACCES))
210 return(err);
212 *mode_out = of_write(*mode_out);
214 err = os_access(file, OS_ACC_R_OK);
215 if((err < 0) && (err != -EACCES))
216 return(err);
218 *mode_out = of_read(*mode_out);
220 return(0);
223 int os_open_file(char *file, struct openflags flags, int mode)
225 int fd, err, f = 0;
227 if(flags.r && flags.w) f = O_RDWR;
228 else if(flags.r) f = O_RDONLY;
229 else if(flags.w) f = O_WRONLY;
230 else f = 0;
232 if(flags.s) f |= O_SYNC;
233 if(flags.c) f |= O_CREAT;
234 if(flags.t) f |= O_TRUNC;
235 if(flags.e) f |= O_EXCL;
237 fd = open64(file, f, mode);
238 if(fd < 0)
239 return(-errno);
241 if(flags.cl && fcntl(fd, F_SETFD, 1)){
242 err = -errno;
243 os_close_file(fd);
244 return err;
247 return(fd);
250 int os_connect_socket(char *name)
252 struct sockaddr_un sock;
253 int fd, err;
255 sock.sun_family = AF_UNIX;
256 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
258 fd = socket(AF_UNIX, SOCK_STREAM, 0);
259 if(fd < 0) {
260 err = -errno;
261 goto out;
264 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
265 if(err) {
266 err = -errno;
267 goto out_close;
270 return fd;
272 out_close:
273 close(fd);
274 out:
275 return err;
278 void os_close_file(int fd)
280 close(fd);
283 int os_seek_file(int fd, __u64 offset)
285 __u64 actual;
287 actual = lseek64(fd, offset, SEEK_SET);
288 if(actual != offset)
289 return -errno;
290 return 0;
293 int os_read_file(int fd, void *buf, int len)
295 int n = read(fd, buf, len);
297 if(n < 0)
298 return -errno;
299 return n;
302 int os_write_file(int fd, const void *buf, int len)
304 int n = write(fd, (void *) buf, len);
306 if(n < 0)
307 return -errno;
308 return n;
311 int os_file_size(char *file, unsigned long long *size_out)
313 struct uml_stat buf;
314 int err;
316 err = os_stat_file(file, &buf);
317 if(err < 0){
318 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
319 return(err);
322 if(S_ISBLK(buf.ust_mode)){
323 int fd, blocks;
325 fd = os_open_file(file, of_read(OPENFLAGS()), 0);
326 if(fd < 0){
327 printk("Couldn't open \"%s\", errno = %d\n", file, -fd);
328 return(fd);
330 if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
331 err = -errno;
332 printk("Couldn't get the block size of \"%s\", "
333 "errno = %d\n", file, errno);
334 os_close_file(fd);
335 return(err);
337 *size_out = ((long long) blocks) * 512;
338 os_close_file(fd);
339 return(0);
341 *size_out = buf.ust_size;
342 return(0);
345 int os_file_modtime(char *file, unsigned long *modtime)
347 struct uml_stat buf;
348 int err;
350 err = os_stat_file(file, &buf);
351 if(err < 0){
352 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
353 return err;
356 *modtime = buf.ust_mtime;
357 return 0;
360 int os_get_exec_close(int fd, int* close_on_exec)
362 int ret;
364 do {
365 ret = fcntl(fd, F_GETFD);
366 } while((ret < 0) && (errno == EINTR)) ;
368 if(ret < 0)
369 return(-errno);
371 *close_on_exec = (ret&FD_CLOEXEC) ? 1 : 0;
372 return(ret);
375 int os_set_exec_close(int fd, int close_on_exec)
377 int flag, err;
379 if(close_on_exec) flag = FD_CLOEXEC;
380 else flag = 0;
382 do {
383 err = fcntl(fd, F_SETFD, flag);
384 } while((err < 0) && (errno == EINTR)) ;
386 if(err < 0)
387 return(-errno);
388 return(err);
391 int os_pipe(int *fds, int stream, int close_on_exec)
393 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
395 err = socketpair(AF_UNIX, type, 0, fds);
396 if(err < 0)
397 return(-errno);
399 if(!close_on_exec)
400 return(0);
402 err = os_set_exec_close(fds[0], 1);
403 if(err < 0)
404 goto error;
406 err = os_set_exec_close(fds[1], 1);
407 if(err < 0)
408 goto error;
410 return 0;
412 error:
413 printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err);
414 os_close_file(fds[1]);
415 os_close_file(fds[0]);
416 return(err);
419 int os_set_fd_async(int fd, int owner)
421 int err;
423 /* XXX This should do F_GETFL first */
424 if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){
425 err = -errno;
426 printk("os_set_fd_async : failed to set O_ASYNC and "
427 "O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
428 return err;
430 #ifdef notdef
431 if(fcntl(fd, F_SETFD, 1) < 0){
432 printk("os_set_fd_async : Setting FD_CLOEXEC failed, "
433 "errno = %d\n", errno);
435 #endif
437 if((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
438 (fcntl(fd, F_SETOWN, owner) < 0)){
439 err = -errno;
440 printk("os_set_fd_async : Failed to fcntl F_SETOWN "
441 "(or F_SETSIG) fd %d to pid %d, errno = %d\n", fd,
442 owner, errno);
443 return err;
446 return 0;
449 int os_clear_fd_async(int fd)
451 int flags = fcntl(fd, F_GETFL);
453 flags &= ~(O_ASYNC | O_NONBLOCK);
454 if(fcntl(fd, F_SETFL, flags) < 0)
455 return -errno;
456 return 0;
459 int os_set_fd_block(int fd, int blocking)
461 int flags;
463 flags = fcntl(fd, F_GETFL);
465 if(blocking) flags &= ~O_NONBLOCK;
466 else flags |= O_NONBLOCK;
468 if(fcntl(fd, F_SETFL, flags) < 0)
469 return -errno;
471 return 0;
474 int os_accept_connection(int fd)
476 int new;
478 new = accept(fd, NULL, 0);
479 if(new < 0)
480 return -errno;
481 return new;
484 #ifndef SHUT_RD
485 #define SHUT_RD 0
486 #endif
488 #ifndef SHUT_WR
489 #define SHUT_WR 1
490 #endif
492 #ifndef SHUT_RDWR
493 #define SHUT_RDWR 2
494 #endif
496 int os_shutdown_socket(int fd, int r, int w)
498 int what, err;
500 if(r && w) what = SHUT_RDWR;
501 else if(r) what = SHUT_RD;
502 else if(w) what = SHUT_WR;
503 else {
504 printk("os_shutdown_socket : neither r or w was set\n");
505 return -EINVAL;
507 err = shutdown(fd, what);
508 if(err < 0)
509 return -errno;
510 return 0;
513 int os_rcv_fd(int fd, int *helper_pid_out)
515 int new, n;
516 char buf[CMSG_SPACE(sizeof(new))];
517 struct msghdr msg;
518 struct cmsghdr *cmsg;
519 struct iovec iov;
521 msg.msg_name = NULL;
522 msg.msg_namelen = 0;
523 iov = ((struct iovec) { .iov_base = helper_pid_out,
524 .iov_len = sizeof(*helper_pid_out) });
525 msg.msg_iov = &iov;
526 msg.msg_iovlen = 1;
527 msg.msg_control = buf;
528 msg.msg_controllen = sizeof(buf);
529 msg.msg_flags = 0;
531 n = recvmsg(fd, &msg, 0);
532 if(n < 0)
533 return -errno;
535 else if(n != sizeof(iov.iov_len))
536 *helper_pid_out = -1;
538 cmsg = CMSG_FIRSTHDR(&msg);
539 if(cmsg == NULL){
540 printk("rcv_fd didn't receive anything, error = %d\n", errno);
541 return -1;
543 if((cmsg->cmsg_level != SOL_SOCKET) ||
544 (cmsg->cmsg_type != SCM_RIGHTS)){
545 printk("rcv_fd didn't receive a descriptor\n");
546 return -1;
549 new = ((int *) CMSG_DATA(cmsg))[0];
550 return new;
553 int os_create_unix_socket(char *file, int len, int close_on_exec)
555 struct sockaddr_un addr;
556 int sock, err;
558 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
559 if(sock < 0)
560 return -errno;
562 if(close_on_exec) {
563 err = os_set_exec_close(sock, 1);
564 if(err < 0)
565 printk("create_unix_socket : close_on_exec failed, "
566 "err = %d", -err);
569 addr.sun_family = AF_UNIX;
571 /* XXX Be more careful about overflow */
572 snprintf(addr.sun_path, len, "%s", file);
574 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
575 if(err < 0)
576 return -errno;
578 return sock;
581 void os_flush_stdout(void)
583 fflush(stdout);
586 int os_lock_file(int fd, int excl)
588 int type = excl ? F_WRLCK : F_RDLCK;
589 struct flock lock = ((struct flock) { .l_type = type,
590 .l_whence = SEEK_SET,
591 .l_start = 0,
592 .l_len = 0 } );
593 int err, save;
595 err = fcntl(fd, F_SETLK, &lock);
596 if(!err)
597 goto out;
599 save = -errno;
600 err = fcntl(fd, F_GETLK, &lock);
601 if(err){
602 err = -errno;
603 goto out;
606 printk("F_SETLK failed, file already locked by pid %d\n", lock.l_pid);
607 err = save;
608 out:
609 return err;