blob: 140e587bc0adedf23debeb7183ad24c0c49d9267 [file] [log] [blame]
Jeff Dike108ffa82006-07-10 04:45:14 -07001/*
Jeff Dike1adfd602008-02-04 22:31:05 -08002 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <unistd.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <sys/ioctl.h>
12#include <sys/mount.h>
Jeff Dike1adfd602008-02-04 22:31:05 -080013#include <sys/socket.h>
14#include <sys/stat.h>
15#include <sys/un.h>
16#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "os.h"
18#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
WANG Congc9a30722008-02-04 22:30:35 -080020static void copy_stat(struct uml_stat *dst, const struct stat64 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
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 });
36}
37
38int os_stat_fd(const int fd, struct uml_stat *ubuf)
39{
40 struct stat64 sbuf;
41 int err;
42
Jeff Dike9ead6fe2006-07-10 04:45:15 -070043 CATCH_EINTR(err = fstat64(fd, &sbuf));
Jeff Dike1adfd602008-02-04 22:31:05 -080044 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070045 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jeff Dike1adfd602008-02-04 22:31:05 -080047 if (ubuf != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 copy_stat(ubuf, &sbuf);
Jeff Dike108ffa82006-07-10 04:45:14 -070049 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52int os_stat_file(const char *file_name, struct uml_stat *ubuf)
53{
54 struct stat64 sbuf;
55 int err;
56
Jeff Dike1adfd602008-02-04 22:31:05 -080057 CATCH_EINTR(err = stat64(file_name, &sbuf));
58 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070059 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Jeff Dike1adfd602008-02-04 22:31:05 -080061 if (ubuf != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 copy_stat(ubuf, &sbuf);
Jeff Dike108ffa82006-07-10 04:45:14 -070063 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Jeff Dike1adfd602008-02-04 22:31:05 -080066int os_access(const char *file, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 int amode, err;
69
Jeff Dike1adfd602008-02-04 22:31:05 -080070 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 err = access(file, amode);
Jeff Dike1adfd602008-02-04 22:31:05 -080076 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070077 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Jeff Dike108ffa82006-07-10 04:45:14 -070079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
83int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
84{
85 int err;
86
87 err = ioctl(fd, cmd, arg);
Jeff Dike1adfd602008-02-04 22:31:05 -080088 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070089 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Jeff Dike108ffa82006-07-10 04:45:14 -070091 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* FIXME: ensure namebuf in os_get_if_name is big enough */
95int os_get_ifname(int fd, char* namebuf)
96{
Jeff Dike1adfd602008-02-04 22:31:05 -080097 if (ioctl(fd, SIOCGIFNAME, namebuf) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070098 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jeff Dike108ffa82006-07-10 04:45:14 -0700100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103int os_set_slip(int fd)
104{
105 int disc, sencap;
106
107 disc = N_SLIP;
Jeff Dike1adfd602008-02-04 22:31:05 -0800108 if (ioctl(fd, TIOCSETD, &disc) < 0)
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700109 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 sencap = 0;
Jeff Dike1adfd602008-02-04 22:31:05 -0800112 if (ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700113 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Jeff Dike108ffa82006-07-10 04:45:14 -0700115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118int os_mode_fd(int fd, int mode)
119{
120 int err;
121
Jeff Dike1adfd602008-02-04 22:31:05 -0800122 CATCH_EINTR(err = fchmod(fd, mode));
123 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700124 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Jeff Dike108ffa82006-07-10 04:45:14 -0700126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129int os_file_type(char *file)
130{
131 struct uml_stat buf;
132 int err;
133
134 err = os_stat_file(file, &buf);
Jeff Dike1adfd602008-02-04 22:31:05 -0800135 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700136 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jeff Dike1adfd602008-02-04 22:31:05 -0800138 if (S_ISDIR(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700139 return OS_TYPE_DIR;
Jeff Dike1adfd602008-02-04 22:31:05 -0800140 else if (S_ISLNK(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700141 return OS_TYPE_SYMLINK;
Jeff Dike1adfd602008-02-04 22:31:05 -0800142 else if (S_ISCHR(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700143 return OS_TYPE_CHARDEV;
Jeff Dike1adfd602008-02-04 22:31:05 -0800144 else if (S_ISBLK(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700145 return OS_TYPE_BLOCKDEV;
Jeff Dike1adfd602008-02-04 22:31:05 -0800146 else if (S_ISFIFO(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700147 return OS_TYPE_FIFO;
Jeff Dike1adfd602008-02-04 22:31:05 -0800148 else if (S_ISSOCK(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700149 return OS_TYPE_SOCK;
150 else return OS_TYPE_FILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
WANG Congc9a30722008-02-04 22:30:35 -0800153int os_file_mode(const char *file, struct openflags *mode_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 int err;
156
157 *mode_out = OPENFLAGS();
158
Jeff Dike512b6fb2007-10-16 01:27:11 -0700159 err = access(file, W_OK);
Jeff Dike1adfd602008-02-04 22:31:05 -0800160 if (err && (errno != EACCES))
Jeff Dike512b6fb2007-10-16 01:27:11 -0700161 return -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800162 else if (!err)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700163 *mode_out = of_write(*mode_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jeff Dike512b6fb2007-10-16 01:27:11 -0700165 err = access(file, R_OK);
Jeff Dike1adfd602008-02-04 22:31:05 -0800166 if (err && (errno != EACCES))
Jeff Dike512b6fb2007-10-16 01:27:11 -0700167 return -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800168 else if (!err)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700169 *mode_out = of_read(*mode_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jeff Dike512b6fb2007-10-16 01:27:11 -0700171 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
WANG Congc9a30722008-02-04 22:30:35 -0800174int os_open_file(const char *file, struct openflags flags, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700176 int fd, err, f = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Jeff Dike1adfd602008-02-04 22:31:05 -0800178 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 else f = 0;
185
Jeff Dike1adfd602008-02-04 22:31:05 -0800186 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;
Jeff Dikebf53d852008-02-04 22:31:18 -0800194 if (flags.a)
195 f |= O_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 fd = open64(file, f, mode);
Jeff Dike1adfd602008-02-04 22:31:05 -0800198 if (fd < 0)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700199 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Jeff Dike1adfd602008-02-04 22:31:05 -0800201 if (flags.cl && fcntl(fd, F_SETFD, 1)) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700202 err = -errno;
Jeff Dike512b6fb2007-10-16 01:27:11 -0700203 close(fd);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700204 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
Jeff Dike512b6fb2007-10-16 01:27:11 -0700207 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
WANG Congc9a30722008-02-04 22:30:35 -0800210int os_connect_socket(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct sockaddr_un sock;
213 int fd, err;
214
215 sock.sun_family = AF_UNIX;
216 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
217
218 fd = socket(AF_UNIX, SOCK_STREAM, 0);
Jeff Dike1adfd602008-02-04 22:31:05 -0800219 if (fd < 0) {
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800220 err = -errno;
221 goto out;
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
Jeff Dike1adfd602008-02-04 22:31:05 -0800225 if (err) {
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800226 err = -errno;
227 goto out_close;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800230 return fd;
231
232out_close:
233 close(fd);
234out:
235 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238void os_close_file(int fd)
239{
240 close(fd);
241}
242
Jeff Dikeba180fd2007-10-16 01:27:00 -0700243int os_seek_file(int fd, unsigned long long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700245 unsigned long long actual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 actual = lseek64(fd, offset, SEEK_SET);
Jeff Dike1adfd602008-02-04 22:31:05 -0800248 if (actual != offset)
Jeff Dike108ffa82006-07-10 04:45:14 -0700249 return -errno;
250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253int os_read_file(int fd, void *buf, int len)
254{
Jeff Dike3d564042007-05-06 14:51:32 -0700255 int n = read(fd, buf, len);
256
Jeff Dike1adfd602008-02-04 22:31:05 -0800257 if (n < 0)
Jeff Dike3d564042007-05-06 14:51:32 -0700258 return -errno;
259 return n;
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262int os_write_file(int fd, const void *buf, int len)
263{
Jeff Dike3d564042007-05-06 14:51:32 -0700264 int n = write(fd, (void *) buf, len);
265
Jeff Dike1adfd602008-02-04 22:31:05 -0800266 if (n < 0)
Jeff Dike3d564042007-05-06 14:51:32 -0700267 return -errno;
268 return n;
269}
270
WANG Congc9a30722008-02-04 22:30:35 -0800271int os_file_size(const char *file, unsigned long long *size_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct uml_stat buf;
274 int err;
275
276 err = os_stat_file(file, &buf);
Jeff Dike1adfd602008-02-04 22:31:05 -0800277 if (err < 0) {
278 printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
279 -err);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
Jeff Dike1adfd602008-02-04 22:31:05 -0800283 if (S_ISBLK(buf.ust_mode)) {
Nicolas George2c392a42007-09-18 22:46:21 -0700284 int fd;
285 long blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Jeff Dike512b6fb2007-10-16 01:27:11 -0700287 fd = open(file, O_RDONLY, 0);
Jeff Dike1adfd602008-02-04 22:31:05 -0800288 if (fd < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700289 err = -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800290 printk(UM_KERN_ERR "Couldn't open \"%s\", "
291 "errno = %d\n", file, errno);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700292 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Jeff Dike1adfd602008-02-04 22:31:05 -0800294 if (ioctl(fd, BLKGETSIZE, &blocks) < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700295 err = -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800296 printk(UM_KERN_ERR "Couldn't get the block size of "
297 "\"%s\", errno = %d\n", file, errno);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700298 close(fd);
299 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 *size_out = ((long long) blocks) * 512;
Jeff Dike512b6fb2007-10-16 01:27:11 -0700302 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Jeff Dike512b6fb2007-10-16 01:27:11 -0700304 else *size_out = buf.ust_size;
305
306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
WANG Congc9a30722008-02-04 22:30:35 -0800309int os_file_modtime(const char *file, unsigned long *modtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct uml_stat buf;
312 int err;
313
314 err = os_stat_file(file, &buf);
Jeff Dike1adfd602008-02-04 22:31:05 -0800315 if (err < 0) {
316 printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
317 -err);
Jeff Dike108ffa82006-07-10 04:45:14 -0700318 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
321 *modtime = buf.ust_mtime;
Jeff Dike108ffa82006-07-10 04:45:14 -0700322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Jeff Dike512b6fb2007-10-16 01:27:11 -0700325int os_set_exec_close(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Jeff Dike512b6fb2007-10-16 01:27:11 -0700327 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Jeff Dike512b6fb2007-10-16 01:27:11 -0700329 CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Jeff Dike1adfd602008-02-04 22:31:05 -0800331 if (err < 0)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700332 return -errno;
333 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336int os_pipe(int *fds, int stream, int close_on_exec)
337{
338 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
339
340 err = socketpair(AF_UNIX, type, 0, fds);
Jeff Dike1adfd602008-02-04 22:31:05 -0800341 if (err < 0)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700342 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jeff Dike1adfd602008-02-04 22:31:05 -0800344 if (!close_on_exec)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Jeff Dike512b6fb2007-10-16 01:27:11 -0700347 err = os_set_exec_close(fds[0]);
Jeff Dike1adfd602008-02-04 22:31:05 -0800348 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 goto error;
350
Jeff Dike512b6fb2007-10-16 01:27:11 -0700351 err = os_set_exec_close(fds[1]);
Jeff Dike1adfd602008-02-04 22:31:05 -0800352 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto error;
354
Jeff Dike108ffa82006-07-10 04:45:14 -0700355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 error:
Jeff Dike1adfd602008-02-04 22:31:05 -0800358 printk(UM_KERN_ERR "os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
359 -err);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700360 close(fds[1]);
361 close(fds[0]);
362 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Jeff Dikebf8fde72008-02-04 22:31:04 -0800365int os_set_fd_async(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Jeff Dikebf8fde72008-02-04 22:31:04 -0800367 int err, flags;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700368
Jeff Dikebf8fde72008-02-04 22:31:04 -0800369 flags = fcntl(fd, F_GETFL);
370 if (flags < 0)
371 return -errno;
372
373 flags |= O_ASYNC | O_NONBLOCK;
374 if (fcntl(fd, F_SETFL, flags) < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700375 err = -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800376 printk(UM_KERN_ERR "os_set_fd_async : failed to set O_ASYNC "
377 "and O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700378 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Jeff Dikebf8fde72008-02-04 22:31:04 -0800381 if ((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
382 (fcntl(fd, F_SETOWN, os_getpid()) < 0)) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700383 err = -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800384 printk(UM_KERN_ERR "os_set_fd_async : Failed to fcntl F_SETOWN "
Jeff Dikebf8fde72008-02-04 22:31:04 -0800385 "(or F_SETSIG) fd %d, errno = %d\n", fd, errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
Jeff Dike108ffa82006-07-10 04:45:14 -0700389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392int os_clear_fd_async(int fd)
393{
Jeff Dike1adfd602008-02-04 22:31:05 -0800394 int flags;
395
396 flags = fcntl(fd, F_GETFL);
397 if (flags < 0)
398 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 flags &= ~(O_ASYNC | O_NONBLOCK);
Jeff Dike1adfd602008-02-04 22:31:05 -0800401 if (fcntl(fd, F_SETFL, flags) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700402 return -errno;
403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406int os_set_fd_block(int fd, int blocking)
407{
408 int flags;
409
410 flags = fcntl(fd, F_GETFL);
Jeff Dike1adfd602008-02-04 22:31:05 -0800411 if (flags < 0)
412 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Jeff Dike1adfd602008-02-04 22:31:05 -0800414 if (blocking)
415 flags &= ~O_NONBLOCK;
416 else
417 flags |= O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Jeff Dike1adfd602008-02-04 22:31:05 -0800419 if (fcntl(fd, F_SETFL, flags) < 0)
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700420 return -errno;
421
Jeff Dike108ffa82006-07-10 04:45:14 -0700422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425int os_accept_connection(int fd)
426{
427 int new;
428
429 new = accept(fd, NULL, 0);
Jeff Dike1adfd602008-02-04 22:31:05 -0800430 if (new < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700431 return -errno;
432 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435#ifndef SHUT_RD
436#define SHUT_RD 0
437#endif
438
439#ifndef SHUT_WR
440#define SHUT_WR 1
441#endif
442
443#ifndef SHUT_RDWR
444#define SHUT_RDWR 2
445#endif
446
447int os_shutdown_socket(int fd, int r, int w)
448{
449 int what, err;
450
Jeff Dike1adfd602008-02-04 22:31:05 -0800451 if (r && w)
452 what = SHUT_RDWR;
453 else if (r)
454 what = SHUT_RD;
455 else if (w)
456 what = SHUT_WR;
457 else
Jeff Dike108ffa82006-07-10 04:45:14 -0700458 return -EINVAL;
Jeff Dike1adfd602008-02-04 22:31:05 -0800459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 err = shutdown(fd, what);
Jeff Dike1adfd602008-02-04 22:31:05 -0800461 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700462 return -errno;
463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466int os_rcv_fd(int fd, int *helper_pid_out)
467{
468 int new, n;
469 char buf[CMSG_SPACE(sizeof(new))];
470 struct msghdr msg;
471 struct cmsghdr *cmsg;
472 struct iovec iov;
473
474 msg.msg_name = NULL;
475 msg.msg_namelen = 0;
476 iov = ((struct iovec) { .iov_base = helper_pid_out,
477 .iov_len = sizeof(*helper_pid_out) });
478 msg.msg_iov = &iov;
479 msg.msg_iovlen = 1;
480 msg.msg_control = buf;
481 msg.msg_controllen = sizeof(buf);
482 msg.msg_flags = 0;
483
484 n = recvmsg(fd, &msg, 0);
Jeff Dike1adfd602008-02-04 22:31:05 -0800485 if (n < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700486 return -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800487 else if (n != iov.iov_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *helper_pid_out = -1;
489
490 cmsg = CMSG_FIRSTHDR(&msg);
Jeff Dike1adfd602008-02-04 22:31:05 -0800491 if (cmsg == NULL) {
492 printk(UM_KERN_ERR "rcv_fd didn't receive anything, "
493 "error = %d\n", errno);
Jeff Dike108ffa82006-07-10 04:45:14 -0700494 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Jeff Dike1adfd602008-02-04 22:31:05 -0800496 if ((cmsg->cmsg_level != SOL_SOCKET) ||
497 (cmsg->cmsg_type != SCM_RIGHTS)) {
498 printk(UM_KERN_ERR "rcv_fd didn't receive a descriptor\n");
Jeff Dike108ffa82006-07-10 04:45:14 -0700499 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
502 new = ((int *) CMSG_DATA(cmsg))[0];
Jeff Dike108ffa82006-07-10 04:45:14 -0700503 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
WANG Congc9a30722008-02-04 22:30:35 -0800506int os_create_unix_socket(const char *file, int len, int close_on_exec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct sockaddr_un addr;
509 int sock, err;
510
511 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
Jeff Dike1adfd602008-02-04 22:31:05 -0800512 if (sock < 0)
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700513 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Jeff Dike1adfd602008-02-04 22:31:05 -0800515 if (close_on_exec) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700516 err = os_set_exec_close(sock);
Jeff Dike1adfd602008-02-04 22:31:05 -0800517 if (err < 0)
518 printk(UM_KERN_ERR "create_unix_socket : "
519 "close_on_exec failed, err = %d", -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 addr.sun_family = AF_UNIX;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 snprintf(addr.sun_path, len, "%s", file);
525
526 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Jeff Dike1adfd602008-02-04 22:31:05 -0800527 if (err < 0)
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700528 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Jeff Dike108ffa82006-07-10 04:45:14 -0700530 return sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533void os_flush_stdout(void)
534{
535 fflush(stdout);
536}
537
538int os_lock_file(int fd, int excl)
539{
540 int type = excl ? F_WRLCK : F_RDLCK;
541 struct flock lock = ((struct flock) { .l_type = type,
542 .l_whence = SEEK_SET,
543 .l_start = 0,
544 .l_len = 0 } );
545 int err, save;
546
547 err = fcntl(fd, F_SETLK, &lock);
Jeff Dike1adfd602008-02-04 22:31:05 -0800548 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 goto out;
550
551 save = -errno;
552 err = fcntl(fd, F_GETLK, &lock);
Jeff Dike1adfd602008-02-04 22:31:05 -0800553 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 err = -errno;
555 goto out;
556 }
557
Jeff Dike1adfd602008-02-04 22:31:05 -0800558 printk(UM_KERN_ERR "F_SETLK failed, file already locked by pid %d\n",
559 lock.l_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 err = save;
561 out:
Jeff Dike108ffa82006-07-10 04:45:14 -0700562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
Al Viro005a59e2009-04-21 01:27:08 -0400564
565unsigned os_major(unsigned long long dev)
566{
567 return major(dev);
568}
569
570unsigned os_minor(unsigned long long dev)
571{
572 return minor(dev);
573}
574
575unsigned long long os_makedev(unsigned major, unsigned minor)
576{
577 return makedev(major, minor);
578}