blob: 07a750197bb09d3b5ce59b631aed874d58603386 [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>
Al Viro37185b32012-10-08 03:27:32 +010016#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
WANG Congc9a30722008-02-04 22:30:35 -080018static void copy_stat(struct uml_stat *dst, const struct stat64 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
20 *dst = ((struct uml_stat) {
21 .ust_dev = src->st_dev, /* device */
22 .ust_ino = src->st_ino, /* inode */
23 .ust_mode = src->st_mode, /* protection */
24 .ust_nlink = src->st_nlink, /* number of hard links */
25 .ust_uid = src->st_uid, /* user ID of owner */
26 .ust_gid = src->st_gid, /* group ID of owner */
27 .ust_size = src->st_size, /* total size, in bytes */
28 .ust_blksize = src->st_blksize, /* blocksize for filesys I/O */
29 .ust_blocks = src->st_blocks, /* number of blocks allocated */
30 .ust_atime = src->st_atime, /* time of last access */
31 .ust_mtime = src->st_mtime, /* time of last modification */
32 .ust_ctime = src->st_ctime, /* time of last change */
33 });
34}
35
36int os_stat_fd(const int fd, struct uml_stat *ubuf)
37{
38 struct stat64 sbuf;
39 int err;
40
Jeff Dike9ead6fe2006-07-10 04:45:15 -070041 CATCH_EINTR(err = fstat64(fd, &sbuf));
Jeff Dike1adfd602008-02-04 22:31:05 -080042 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070043 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jeff Dike1adfd602008-02-04 22:31:05 -080045 if (ubuf != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 copy_stat(ubuf, &sbuf);
Jeff Dike108ffa82006-07-10 04:45:14 -070047 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50int os_stat_file(const char *file_name, struct uml_stat *ubuf)
51{
52 struct stat64 sbuf;
53 int err;
54
Jeff Dike1adfd602008-02-04 22:31:05 -080055 CATCH_EINTR(err = stat64(file_name, &sbuf));
56 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070057 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jeff Dike1adfd602008-02-04 22:31:05 -080059 if (ubuf != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 copy_stat(ubuf, &sbuf);
Jeff Dike108ffa82006-07-10 04:45:14 -070061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
Jeff Dike1adfd602008-02-04 22:31:05 -080064int os_access(const char *file, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 int amode, err;
67
Jeff Dike1adfd602008-02-04 22:31:05 -080068 amode = (mode & OS_ACC_R_OK ? R_OK : 0) |
69 (mode & OS_ACC_W_OK ? W_OK : 0) |
70 (mode & OS_ACC_X_OK ? X_OK : 0) |
71 (mode & OS_ACC_F_OK ? F_OK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 err = access(file, amode);
Jeff Dike1adfd602008-02-04 22:31:05 -080074 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070075 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jeff Dike108ffa82006-07-10 04:45:14 -070077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
81int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
82{
83 int err;
84
85 err = ioctl(fd, cmd, arg);
Jeff Dike1adfd602008-02-04 22:31:05 -080086 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070087 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Jeff Dike108ffa82006-07-10 04:45:14 -070089 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* FIXME: ensure namebuf in os_get_if_name is big enough */
93int os_get_ifname(int fd, char* namebuf)
94{
Jeff Dike1adfd602008-02-04 22:31:05 -080095 if (ioctl(fd, SIOCGIFNAME, namebuf) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070096 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jeff Dike108ffa82006-07-10 04:45:14 -070098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101int os_set_slip(int fd)
102{
103 int disc, sencap;
104
105 disc = N_SLIP;
Jeff Dike1adfd602008-02-04 22:31:05 -0800106 if (ioctl(fd, TIOCSETD, &disc) < 0)
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700107 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 sencap = 0;
Jeff Dike1adfd602008-02-04 22:31:05 -0800110 if (ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700111 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Jeff Dike108ffa82006-07-10 04:45:14 -0700113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116int os_mode_fd(int fd, int mode)
117{
118 int err;
119
Jeff Dike1adfd602008-02-04 22:31:05 -0800120 CATCH_EINTR(err = fchmod(fd, mode));
121 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700122 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Jeff Dike108ffa82006-07-10 04:45:14 -0700124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127int os_file_type(char *file)
128{
129 struct uml_stat buf;
130 int err;
131
132 err = os_stat_file(file, &buf);
Jeff Dike1adfd602008-02-04 22:31:05 -0800133 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700134 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jeff Dike1adfd602008-02-04 22:31:05 -0800136 if (S_ISDIR(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700137 return OS_TYPE_DIR;
Jeff Dike1adfd602008-02-04 22:31:05 -0800138 else if (S_ISLNK(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700139 return OS_TYPE_SYMLINK;
Jeff Dike1adfd602008-02-04 22:31:05 -0800140 else if (S_ISCHR(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700141 return OS_TYPE_CHARDEV;
Jeff Dike1adfd602008-02-04 22:31:05 -0800142 else if (S_ISBLK(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700143 return OS_TYPE_BLOCKDEV;
Jeff Dike1adfd602008-02-04 22:31:05 -0800144 else if (S_ISFIFO(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700145 return OS_TYPE_FIFO;
Jeff Dike1adfd602008-02-04 22:31:05 -0800146 else if (S_ISSOCK(buf.ust_mode))
Jeff Dike108ffa82006-07-10 04:45:14 -0700147 return OS_TYPE_SOCK;
148 else return OS_TYPE_FILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
WANG Congc9a30722008-02-04 22:30:35 -0800151int os_file_mode(const char *file, struct openflags *mode_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int err;
154
155 *mode_out = OPENFLAGS();
156
Jeff Dike512b6fb2007-10-16 01:27:11 -0700157 err = access(file, W_OK);
Jeff Dike1adfd602008-02-04 22:31:05 -0800158 if (err && (errno != EACCES))
Jeff Dike512b6fb2007-10-16 01:27:11 -0700159 return -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800160 else if (!err)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700161 *mode_out = of_write(*mode_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jeff Dike512b6fb2007-10-16 01:27:11 -0700163 err = access(file, R_OK);
Jeff Dike1adfd602008-02-04 22:31:05 -0800164 if (err && (errno != EACCES))
Jeff Dike512b6fb2007-10-16 01:27:11 -0700165 return -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800166 else if (!err)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700167 *mode_out = of_read(*mode_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jeff Dike512b6fb2007-10-16 01:27:11 -0700169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
WANG Congc9a30722008-02-04 22:30:35 -0800172int os_open_file(const char *file, struct openflags flags, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700174 int fd, err, f = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jeff Dike1adfd602008-02-04 22:31:05 -0800176 if (flags.r && flags.w)
177 f = O_RDWR;
178 else if (flags.r)
179 f = O_RDONLY;
180 else if (flags.w)
181 f = O_WRONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 else f = 0;
183
Jeff Dike1adfd602008-02-04 22:31:05 -0800184 if (flags.s)
185 f |= O_SYNC;
186 if (flags.c)
187 f |= O_CREAT;
188 if (flags.t)
189 f |= O_TRUNC;
190 if (flags.e)
191 f |= O_EXCL;
Jeff Dikebf53d852008-02-04 22:31:18 -0800192 if (flags.a)
193 f |= O_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 fd = open64(file, f, mode);
Jeff Dike1adfd602008-02-04 22:31:05 -0800196 if (fd < 0)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700197 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Jeff Dike1adfd602008-02-04 22:31:05 -0800199 if (flags.cl && fcntl(fd, F_SETFD, 1)) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700200 err = -errno;
Jeff Dike512b6fb2007-10-16 01:27:11 -0700201 close(fd);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
Jeff Dike512b6fb2007-10-16 01:27:11 -0700205 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
WANG Congc9a30722008-02-04 22:30:35 -0800208int os_connect_socket(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 struct sockaddr_un sock;
211 int fd, err;
212
213 sock.sun_family = AF_UNIX;
214 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
215
216 fd = socket(AF_UNIX, SOCK_STREAM, 0);
Jeff Dike1adfd602008-02-04 22:31:05 -0800217 if (fd < 0) {
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800218 err = -errno;
219 goto out;
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
Jeff Dike1adfd602008-02-04 22:31:05 -0800223 if (err) {
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800224 err = -errno;
225 goto out_close;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800228 return fd;
229
230out_close:
231 close(fd);
232out:
233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236void os_close_file(int fd)
237{
238 close(fd);
239}
240
Jeff Dikeba180fd2007-10-16 01:27:00 -0700241int os_seek_file(int fd, unsigned long long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700243 unsigned long long actual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 actual = lseek64(fd, offset, SEEK_SET);
Jeff Dike1adfd602008-02-04 22:31:05 -0800246 if (actual != offset)
Jeff Dike108ffa82006-07-10 04:45:14 -0700247 return -errno;
248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251int os_read_file(int fd, void *buf, int len)
252{
Jeff Dike3d564042007-05-06 14:51:32 -0700253 int n = read(fd, buf, len);
254
Jeff Dike1adfd602008-02-04 22:31:05 -0800255 if (n < 0)
Jeff Dike3d564042007-05-06 14:51:32 -0700256 return -errno;
257 return n;
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260int os_write_file(int fd, const void *buf, int len)
261{
Jeff Dike3d564042007-05-06 14:51:32 -0700262 int n = write(fd, (void *) buf, len);
263
Jeff Dike1adfd602008-02-04 22:31:05 -0800264 if (n < 0)
Jeff Dike3d564042007-05-06 14:51:32 -0700265 return -errno;
266 return n;
267}
268
Richard Weinberger805f11a2013-08-18 13:30:06 +0200269int os_sync_file(int fd)
270{
271 int n = fsync(fd);
272
273 if (n < 0)
274 return -errno;
275 return n;
276}
277
WANG Congc9a30722008-02-04 22:30:35 -0800278int os_file_size(const char *file, unsigned long long *size_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct uml_stat buf;
281 int err;
282
283 err = os_stat_file(file, &buf);
Jeff Dike1adfd602008-02-04 22:31:05 -0800284 if (err < 0) {
285 printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
286 -err);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700287 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
Jeff Dike1adfd602008-02-04 22:31:05 -0800290 if (S_ISBLK(buf.ust_mode)) {
Nicolas George2c392a42007-09-18 22:46:21 -0700291 int fd;
292 long blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Jeff Dike512b6fb2007-10-16 01:27:11 -0700294 fd = open(file, O_RDONLY, 0);
Jeff Dike1adfd602008-02-04 22:31:05 -0800295 if (fd < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700296 err = -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800297 printk(UM_KERN_ERR "Couldn't open \"%s\", "
298 "errno = %d\n", file, errno);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700299 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Jeff Dike1adfd602008-02-04 22:31:05 -0800301 if (ioctl(fd, BLKGETSIZE, &blocks) < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700302 err = -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800303 printk(UM_KERN_ERR "Couldn't get the block size of "
304 "\"%s\", errno = %d\n", file, errno);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700305 close(fd);
306 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 *size_out = ((long long) blocks) * 512;
Jeff Dike512b6fb2007-10-16 01:27:11 -0700309 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Jeff Dike512b6fb2007-10-16 01:27:11 -0700311 else *size_out = buf.ust_size;
312
313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
WANG Congc9a30722008-02-04 22:30:35 -0800316int os_file_modtime(const char *file, unsigned long *modtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 struct uml_stat buf;
319 int err;
320
321 err = os_stat_file(file, &buf);
Jeff Dike1adfd602008-02-04 22:31:05 -0800322 if (err < 0) {
323 printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
324 -err);
Jeff Dike108ffa82006-07-10 04:45:14 -0700325 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
328 *modtime = buf.ust_mtime;
Jeff Dike108ffa82006-07-10 04:45:14 -0700329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Jeff Dike512b6fb2007-10-16 01:27:11 -0700332int os_set_exec_close(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Jeff Dike512b6fb2007-10-16 01:27:11 -0700334 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Jeff Dike512b6fb2007-10-16 01:27:11 -0700336 CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Jeff Dike1adfd602008-02-04 22:31:05 -0800338 if (err < 0)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700339 return -errno;
340 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343int os_pipe(int *fds, int stream, int close_on_exec)
344{
345 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
346
347 err = socketpair(AF_UNIX, type, 0, fds);
Jeff Dike1adfd602008-02-04 22:31:05 -0800348 if (err < 0)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700349 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Jeff Dike1adfd602008-02-04 22:31:05 -0800351 if (!close_on_exec)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Jeff Dike512b6fb2007-10-16 01:27:11 -0700354 err = os_set_exec_close(fds[0]);
Jeff Dike1adfd602008-02-04 22:31:05 -0800355 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto error;
357
Jeff Dike512b6fb2007-10-16 01:27:11 -0700358 err = os_set_exec_close(fds[1]);
Jeff Dike1adfd602008-02-04 22:31:05 -0800359 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 goto error;
361
Jeff Dike108ffa82006-07-10 04:45:14 -0700362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 error:
Jeff Dike1adfd602008-02-04 22:31:05 -0800365 printk(UM_KERN_ERR "os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
366 -err);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700367 close(fds[1]);
368 close(fds[0]);
369 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Jeff Dikebf8fde72008-02-04 22:31:04 -0800372int os_set_fd_async(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Jeff Dikebf8fde72008-02-04 22:31:04 -0800374 int err, flags;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700375
Jeff Dikebf8fde72008-02-04 22:31:04 -0800376 flags = fcntl(fd, F_GETFL);
377 if (flags < 0)
378 return -errno;
379
380 flags |= O_ASYNC | O_NONBLOCK;
381 if (fcntl(fd, F_SETFL, flags) < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700382 err = -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800383 printk(UM_KERN_ERR "os_set_fd_async : failed to set O_ASYNC "
384 "and O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Jeff Dikebf8fde72008-02-04 22:31:04 -0800388 if ((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
389 (fcntl(fd, F_SETOWN, os_getpid()) < 0)) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700390 err = -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800391 printk(UM_KERN_ERR "os_set_fd_async : Failed to fcntl F_SETOWN "
Jeff Dikebf8fde72008-02-04 22:31:04 -0800392 "(or F_SETSIG) fd %d, errno = %d\n", fd, errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700393 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
Jeff Dike108ffa82006-07-10 04:45:14 -0700396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399int os_clear_fd_async(int fd)
400{
Jeff Dike1adfd602008-02-04 22:31:05 -0800401 int flags;
402
403 flags = fcntl(fd, F_GETFL);
404 if (flags < 0)
405 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 flags &= ~(O_ASYNC | O_NONBLOCK);
Jeff Dike1adfd602008-02-04 22:31:05 -0800408 if (fcntl(fd, F_SETFL, flags) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700409 return -errno;
410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413int os_set_fd_block(int fd, int blocking)
414{
415 int flags;
416
417 flags = fcntl(fd, F_GETFL);
Jeff Dike1adfd602008-02-04 22:31:05 -0800418 if (flags < 0)
419 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Jeff Dike1adfd602008-02-04 22:31:05 -0800421 if (blocking)
422 flags &= ~O_NONBLOCK;
423 else
424 flags |= O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Jeff Dike1adfd602008-02-04 22:31:05 -0800426 if (fcntl(fd, F_SETFL, flags) < 0)
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700427 return -errno;
428
Jeff Dike108ffa82006-07-10 04:45:14 -0700429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432int os_accept_connection(int fd)
433{
434 int new;
435
436 new = accept(fd, NULL, 0);
Jeff Dike1adfd602008-02-04 22:31:05 -0800437 if (new < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700438 return -errno;
439 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442#ifndef SHUT_RD
443#define SHUT_RD 0
444#endif
445
446#ifndef SHUT_WR
447#define SHUT_WR 1
448#endif
449
450#ifndef SHUT_RDWR
451#define SHUT_RDWR 2
452#endif
453
454int os_shutdown_socket(int fd, int r, int w)
455{
456 int what, err;
457
Jeff Dike1adfd602008-02-04 22:31:05 -0800458 if (r && w)
459 what = SHUT_RDWR;
460 else if (r)
461 what = SHUT_RD;
462 else if (w)
463 what = SHUT_WR;
464 else
Jeff Dike108ffa82006-07-10 04:45:14 -0700465 return -EINVAL;
Jeff Dike1adfd602008-02-04 22:31:05 -0800466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 err = shutdown(fd, what);
Jeff Dike1adfd602008-02-04 22:31:05 -0800468 if (err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700469 return -errno;
470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473int os_rcv_fd(int fd, int *helper_pid_out)
474{
475 int new, n;
476 char buf[CMSG_SPACE(sizeof(new))];
477 struct msghdr msg;
478 struct cmsghdr *cmsg;
479 struct iovec iov;
480
481 msg.msg_name = NULL;
482 msg.msg_namelen = 0;
483 iov = ((struct iovec) { .iov_base = helper_pid_out,
484 .iov_len = sizeof(*helper_pid_out) });
485 msg.msg_iov = &iov;
486 msg.msg_iovlen = 1;
487 msg.msg_control = buf;
488 msg.msg_controllen = sizeof(buf);
489 msg.msg_flags = 0;
490
491 n = recvmsg(fd, &msg, 0);
Jeff Dike1adfd602008-02-04 22:31:05 -0800492 if (n < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700493 return -errno;
Jeff Dike1adfd602008-02-04 22:31:05 -0800494 else if (n != iov.iov_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 *helper_pid_out = -1;
496
497 cmsg = CMSG_FIRSTHDR(&msg);
Jeff Dike1adfd602008-02-04 22:31:05 -0800498 if (cmsg == NULL) {
499 printk(UM_KERN_ERR "rcv_fd didn't receive anything, "
500 "error = %d\n", errno);
Jeff Dike108ffa82006-07-10 04:45:14 -0700501 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Jeff Dike1adfd602008-02-04 22:31:05 -0800503 if ((cmsg->cmsg_level != SOL_SOCKET) ||
504 (cmsg->cmsg_type != SCM_RIGHTS)) {
505 printk(UM_KERN_ERR "rcv_fd didn't receive a descriptor\n");
Jeff Dike108ffa82006-07-10 04:45:14 -0700506 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
509 new = ((int *) CMSG_DATA(cmsg))[0];
Jeff Dike108ffa82006-07-10 04:45:14 -0700510 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
WANG Congc9a30722008-02-04 22:30:35 -0800513int os_create_unix_socket(const char *file, int len, int close_on_exec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct sockaddr_un addr;
516 int sock, err;
517
518 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
Jeff Dike1adfd602008-02-04 22:31:05 -0800519 if (sock < 0)
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700520 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Jeff Dike1adfd602008-02-04 22:31:05 -0800522 if (close_on_exec) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700523 err = os_set_exec_close(sock);
Jeff Dike1adfd602008-02-04 22:31:05 -0800524 if (err < 0)
525 printk(UM_KERN_ERR "create_unix_socket : "
526 "close_on_exec failed, err = %d", -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
529 addr.sun_family = AF_UNIX;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 snprintf(addr.sun_path, len, "%s", file);
532
533 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Jeff Dike1adfd602008-02-04 22:31:05 -0800534 if (err < 0)
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700535 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jeff Dike108ffa82006-07-10 04:45:14 -0700537 return sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540void os_flush_stdout(void)
541{
542 fflush(stdout);
543}
544
545int os_lock_file(int fd, int excl)
546{
547 int type = excl ? F_WRLCK : F_RDLCK;
548 struct flock lock = ((struct flock) { .l_type = type,
549 .l_whence = SEEK_SET,
550 .l_start = 0,
551 .l_len = 0 } );
552 int err, save;
553
554 err = fcntl(fd, F_SETLK, &lock);
Jeff Dike1adfd602008-02-04 22:31:05 -0800555 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto out;
557
558 save = -errno;
559 err = fcntl(fd, F_GETLK, &lock);
Jeff Dike1adfd602008-02-04 22:31:05 -0800560 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 err = -errno;
562 goto out;
563 }
564
Jeff Dike1adfd602008-02-04 22:31:05 -0800565 printk(UM_KERN_ERR "F_SETLK failed, file already locked by pid %d\n",
566 lock.l_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 err = save;
568 out:
Jeff Dike108ffa82006-07-10 04:45:14 -0700569 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
Al Viro005a59e2009-04-21 01:27:08 -0400571
572unsigned os_major(unsigned long long dev)
573{
574 return major(dev);
575}
576
577unsigned os_minor(unsigned long long dev)
578{
579 return minor(dev);
580}
581
582unsigned long long os_makedev(unsigned major, unsigned minor)
583{
584 return makedev(major, minor);
585}