2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
11 #include <sys/socket.h>
13 #include <sys/ioctl.h>
14 #include <sys/mount.h>
18 #include "kern_util.h"
20 int os_file_type(char *file)
24 if(stat64(file, &buf) == -1)
27 if(S_ISDIR(buf.st_mode)) return(OS_TYPE_DIR);
28 else if(S_ISLNK(buf.st_mode)) return(OS_TYPE_SYMLINK);
29 else if(S_ISCHR(buf.st_mode)) return(OS_TYPE_CHARDEV);
30 else if(S_ISBLK(buf.st_mode)) return(OS_TYPE_BLOCKDEV);
31 else if(S_ISFIFO(buf.st_mode)) return(OS_TYPE_FIFO);
32 else if(S_ISSOCK(buf.st_mode)) return(OS_TYPE_SOCK);
33 else return(OS_TYPE_FILE);
36 int os_file_mode(char *file, struct openflags *mode_out)
38 *mode_out = OPENFLAGS();
40 if(!access(file, W_OK)) *mode_out = of_write(*mode_out);
41 else if(errno != EACCES)
44 if(!access(file, R_OK)) *mode_out = of_read(*mode_out);
45 else if(errno != EACCES)
51 int os_open_file(char *file, struct openflags flags, int mode)
55 if(flags.r && flags.w) f = O_RDWR;
56 else if(flags.r) f = O_RDONLY;
57 else if(flags.w) f = O_WRONLY;
60 if(flags.s) f |= O_SYNC;
61 if(flags.c) f |= O_CREAT;
62 if(flags.t) f |= O_TRUNC;
63 if(flags.e) f |= O_EXCL;
65 fd = open64(file, f, mode);
66 if(fd < 0) return(-errno);
69 if(fcntl(fd, F_SETFD, 1)){
79 int os_connect_socket(char *name)
81 struct sockaddr_un sock;
84 sock.sun_family = AF_UNIX;
85 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
87 fd = socket(AF_UNIX, SOCK_STREAM, 0);
91 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
98 void os_close_file(int fd)
103 int os_seek_file(int fd, __u64 offset)
107 actual = lseek64(fd, offset, SEEK_SET);
108 if(actual != offset) return(-errno);
112 int os_read_file(int fd, void *buf, int len)
116 /* Force buf into memory if it's not already. */
118 /* XXX This fails if buf is kernel memory */
120 if(copy_to_user_proc(buf, &c, sizeof(c)))
124 n = read(fd, buf, len);
130 int os_write_file(int fd, void *buf, int count)
134 /* Force buf into memory if it's not already. */
136 /* XXX This fails if buf is kernel memory */
138 if(copy_to_user_proc(buf, buf, buf[0]))
142 n = write(fd, buf, count);
148 int os_file_size(char *file, long long *size_out)
152 if(stat64(file, &buf) == -1){
153 printk("Couldn't stat \"%s\" : errno = %d\n", file, errno);
156 if(S_ISBLK(buf.st_mode)){
159 if((fd = open64(file, O_RDONLY)) < 0){
160 printk("Couldn't open \"%s\", errno = %d\n", file,
164 if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
165 printk("Couldn't get the block size of \"%s\", "
166 "errno = %d\n", file, errno);
170 *size_out = ((long long) blocks) * 512;
174 *size_out = buf.st_size;
178 int os_pipe(int *fds, int stream, int close_on_exec)
180 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
182 err = socketpair(AF_UNIX, type, 0, fds);
189 if((fcntl(fds[0], F_SETFD, 1) < 0) || (fcntl(fds[1], F_SETFD, 1) < 0))
190 printk("os_pipe : Setting FD_CLOEXEC failed, errno = %d",
196 int os_set_fd_async(int fd, int owner)
198 /* XXX This should do F_GETFL first */
199 if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){
200 printk("os_set_fd_async : failed to set O_ASYNC and "
201 "O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
205 if(fcntl(fd, F_SETFD, 1) < 0){
206 printk("os_set_fd_async : Setting FD_CLOEXEC failed, "
207 "errno = %d\n", errno);
211 if((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
212 (fcntl(fd, F_SETOWN, owner) < 0)){
213 printk("os_set_fd_async : Failed to fcntl F_SETOWN "
214 "(or F_SETSIG) fd %d to pid %d, errno = %d\n", fd,
222 int os_set_fd_block(int fd, int blocking)
226 flags = fcntl(fd, F_GETFL);
228 if(blocking) flags &= ~O_NONBLOCK;
229 else flags |= O_NONBLOCK;
231 if(fcntl(fd, F_SETFL, flags) < 0){
232 printk("Failed to change blocking on fd # %d, errno = %d\n",
239 int os_accept_connection(int fd)
243 new = accept(fd, NULL, 0);
261 int os_shutdown_socket(int fd, int r, int w)
265 if(r && w) what = SHUT_RDWR;
266 else if(r) what = SHUT_RD;
267 else if(w) what = SHUT_WR;
269 printk("os_shutdown_socket : neither r or w was set\n");
272 err = shutdown(fd, what);
278 int os_rcv_fd(int fd, int *helper_pid_out)
281 char buf[CMSG_SPACE(sizeof(new))];
283 struct cmsghdr *cmsg;
288 iov = ((struct iovec) { .iov_base = helper_pid_out,
289 .iov_len = sizeof(*helper_pid_out) });
292 msg.msg_control = buf;
293 msg.msg_controllen = sizeof(buf);
296 n = recvmsg(fd, &msg, 0);
300 else if(n != sizeof(iov.iov_len))
301 *helper_pid_out = -1;
303 cmsg = CMSG_FIRSTHDR(&msg);
305 printk("rcv_fd didn't receive anything, error = %d\n", errno);
308 if((cmsg->cmsg_level != SOL_SOCKET) ||
309 (cmsg->cmsg_type != SCM_RIGHTS)){
310 printk("rcv_fd didn't receive a descriptor\n");
314 new = ((int *) CMSG_DATA(cmsg))[0];
318 int create_unix_socket(char *file, int len)
320 struct sockaddr_un addr;
323 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
325 printk("create_unix_socket - socket failed, errno = %d\n",
330 addr.sun_family = AF_UNIX;
332 /* XXX Be more careful about overflow */
333 snprintf(addr.sun_path, len, "%s", file);
335 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
337 printk("create_listening_socket - bind failed, errno = %d\n",
346 * Overrides for Emacs so that we follow Linus's tabbing style.
347 * Emacs will notice this stuff at the end of the file and automatically
348 * adjust the settings for this buffer only. This must remain at the end
350 * ---------------------------------------------------------------------------
352 * c-file-style: "linux"