blob: c3bb5ce70c95280e0fe65896d16f2dfb18546074 [file] [log] [blame]
Jeff Dike108ffa82006-07-10 04:45:14 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * 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>
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"
21
WANG Congc9a30722008-02-04 22:30:35 -080022static void copy_stat(struct uml_stat *dst, const struct stat64 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
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 });
38}
39
40int os_stat_fd(const int fd, struct uml_stat *ubuf)
41{
42 struct stat64 sbuf;
43 int err;
44
Jeff Dike9ead6fe2006-07-10 04:45:15 -070045 CATCH_EINTR(err = fstat64(fd, &sbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070047 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 if(ubuf != NULL)
50 copy_stat(ubuf, &sbuf);
Jeff Dike108ffa82006-07-10 04:45:14 -070051 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
54int os_stat_file(const char *file_name, struct uml_stat *ubuf)
55{
56 struct stat64 sbuf;
57 int err;
58
59 do {
60 err = stat64(file_name, &sbuf);
61 } while((err < 0) && (errno == EINTR)) ;
62
63 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070064 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 if(ubuf != NULL)
67 copy_stat(ubuf, &sbuf);
Jeff Dike108ffa82006-07-10 04:45:14 -070068 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71int os_access(const char* file, int mode)
72{
73 int amode, err;
74
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) ;
77
78 err = access(file, amode);
79 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070080 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Jeff Dike108ffa82006-07-10 04:45:14 -070082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
86int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
87{
88 int err;
89
90 err = ioctl(fd, cmd, arg);
91 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070092 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Jeff Dike108ffa82006-07-10 04:45:14 -070094 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* FIXME: ensure namebuf in os_get_if_name is big enough */
98int os_get_ifname(int fd, char* namebuf)
99{
100 if(ioctl(fd, SIOCGIFNAME, namebuf) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700101 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jeff Dike108ffa82006-07-10 04:45:14 -0700103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106int os_set_slip(int fd)
107{
108 int disc, sencap;
109
110 disc = N_SLIP;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700111 if(ioctl(fd, TIOCSETD, &disc) < 0)
112 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 sencap = 0;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700115 if(ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
116 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Jeff Dike108ffa82006-07-10 04:45:14 -0700118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121int os_set_owner(int fd, int pid)
122{
123 if(fcntl(fd, F_SETOWN, pid) < 0){
124 int save_errno = errno;
125
126 if(fcntl(fd, F_GETOWN, 0) != pid)
Jeff Dike108ffa82006-07-10 04:45:14 -0700127 return -save_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
Jeff Dike108ffa82006-07-10 04:45:14 -0700130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133int os_mode_fd(int fd, int mode)
134{
135 int err;
136
137 do {
138 err = fchmod(fd, mode);
139 } while((err < 0) && (errno==EINTR)) ;
140
141 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700142 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jeff Dike108ffa82006-07-10 04:45:14 -0700144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147int os_file_type(char *file)
148{
149 struct uml_stat buf;
150 int err;
151
152 err = os_stat_file(file, &buf);
153 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700154 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Jeff Dike108ffa82006-07-10 04:45:14 -0700156 if(S_ISDIR(buf.ust_mode))
157 return OS_TYPE_DIR;
158 else if(S_ISLNK(buf.ust_mode))
159 return OS_TYPE_SYMLINK;
160 else if(S_ISCHR(buf.ust_mode))
161 return OS_TYPE_CHARDEV;
162 else if(S_ISBLK(buf.ust_mode))
163 return OS_TYPE_BLOCKDEV;
164 else if(S_ISFIFO(buf.ust_mode))
165 return OS_TYPE_FIFO;
166 else if(S_ISSOCK(buf.ust_mode))
167 return OS_TYPE_SOCK;
168 else return OS_TYPE_FILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
WANG Congc9a30722008-02-04 22:30:35 -0800171int os_file_mode(const char *file, struct openflags *mode_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 int err;
174
175 *mode_out = OPENFLAGS();
176
Jeff Dike512b6fb2007-10-16 01:27:11 -0700177 err = access(file, W_OK);
178 if(err && (errno != EACCES))
179 return -errno;
180 else if(!err)
181 *mode_out = of_write(*mode_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jeff Dike512b6fb2007-10-16 01:27:11 -0700183 err = access(file, R_OK);
184 if(err && (errno != EACCES))
185 return -errno;
186 else if(!err)
187 *mode_out = of_read(*mode_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Jeff Dike512b6fb2007-10-16 01:27:11 -0700189 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
WANG Congc9a30722008-02-04 22:30:35 -0800192int os_open_file(const char *file, struct openflags flags, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700194 int fd, err, f = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if(flags.r && flags.w) f = O_RDWR;
197 else if(flags.r) f = O_RDONLY;
198 else if(flags.w) f = O_WRONLY;
199 else f = 0;
200
201 if(flags.s) f |= O_SYNC;
202 if(flags.c) f |= O_CREAT;
203 if(flags.t) f |= O_TRUNC;
204 if(flags.e) f |= O_EXCL;
205
206 fd = open64(file, f, mode);
207 if(fd < 0)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700208 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 if(flags.cl && fcntl(fd, F_SETFD, 1)){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700211 err = -errno;
Jeff Dike512b6fb2007-10-16 01:27:11 -0700212 close(fd);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Jeff Dike512b6fb2007-10-16 01:27:11 -0700216 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
WANG Congc9a30722008-02-04 22:30:35 -0800219int os_connect_socket(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 struct sockaddr_un sock;
222 int fd, err;
223
224 sock.sun_family = AF_UNIX;
225 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
226
227 fd = socket(AF_UNIX, SOCK_STREAM, 0);
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800228 if(fd < 0) {
229 err = -errno;
230 goto out;
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800234 if(err) {
235 err = -errno;
236 goto out_close;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800239 return fd;
240
241out_close:
242 close(fd);
243out:
244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247void os_close_file(int fd)
248{
249 close(fd);
250}
251
Jeff Dikeba180fd2007-10-16 01:27:00 -0700252int os_seek_file(int fd, unsigned long long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700254 unsigned long long actual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 actual = lseek64(fd, offset, SEEK_SET);
257 if(actual != offset)
Jeff Dike108ffa82006-07-10 04:45:14 -0700258 return -errno;
259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262int os_read_file(int fd, void *buf, int len)
263{
Jeff Dike3d564042007-05-06 14:51:32 -0700264 int n = read(fd, buf, len);
265
266 if(n < 0)
267 return -errno;
268 return n;
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271int os_write_file(int fd, const void *buf, int len)
272{
Jeff Dike3d564042007-05-06 14:51:32 -0700273 int n = write(fd, (void *) buf, len);
274
275 if(n < 0)
276 return -errno;
277 return n;
278}
279
WANG Congc9a30722008-02-04 22:30:35 -0800280int os_file_size(const char *file, unsigned long long *size_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct uml_stat buf;
283 int err;
284
285 err = os_stat_file(file, &buf);
286 if(err < 0){
287 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700288 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
291 if(S_ISBLK(buf.ust_mode)){
Nicolas George2c392a42007-09-18 22:46:21 -0700292 int fd;
293 long blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Jeff Dike512b6fb2007-10-16 01:27:11 -0700295 fd = open(file, O_RDONLY, 0);
296 if(fd < 0) {
297 err = -errno;
298 printk("Couldn't open \"%s\", errno = %d\n", file,
299 errno);
300 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700303 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 printk("Couldn't get the block size of \"%s\", "
305 "errno = %d\n", file, errno);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700306 close(fd);
307 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309 *size_out = ((long long) blocks) * 512;
Jeff Dike512b6fb2007-10-16 01:27:11 -0700310 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Jeff Dike512b6fb2007-10-16 01:27:11 -0700312 else *size_out = buf.ust_size;
313
314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
WANG Congc9a30722008-02-04 22:30:35 -0800317int os_file_modtime(const char *file, unsigned long *modtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct uml_stat buf;
320 int err;
321
322 err = os_stat_file(file, &buf);
323 if(err < 0){
324 printk("Couldn't stat \"%s\" : err = %d\n", file, -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_get_exec_close(int fd, int *close_on_exec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 int ret;
335
Jeff Dike512b6fb2007-10-16 01:27:11 -0700336 CATCH_EINTR(ret = fcntl(fd, F_GETFD));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if(ret < 0)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700339 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Jeff Dike512b6fb2007-10-16 01:27:11 -0700341 *close_on_exec = (ret & FD_CLOEXEC) ? 1 : 0;
342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Jeff Dike512b6fb2007-10-16 01:27:11 -0700345int os_set_exec_close(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Jeff Dike512b6fb2007-10-16 01:27:11 -0700347 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Jeff Dike512b6fb2007-10-16 01:27:11 -0700349 CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if(err < 0)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700352 return -errno;
353 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356int os_pipe(int *fds, int stream, int close_on_exec)
357{
358 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
359
360 err = socketpair(AF_UNIX, type, 0, fds);
361 if(err < 0)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700362 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if(!close_on_exec)
Jeff Dike512b6fb2007-10-16 01:27:11 -0700365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Jeff Dike512b6fb2007-10-16 01:27:11 -0700367 err = os_set_exec_close(fds[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if(err < 0)
369 goto error;
370
Jeff Dike512b6fb2007-10-16 01:27:11 -0700371 err = os_set_exec_close(fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if(err < 0)
373 goto error;
374
Jeff Dike108ffa82006-07-10 04:45:14 -0700375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 error:
378 printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err);
Jeff Dike512b6fb2007-10-16 01:27:11 -0700379 close(fds[1]);
380 close(fds[0]);
381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384int os_set_fd_async(int fd, int owner)
385{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700386 int err;
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* XXX This should do F_GETFL first */
389 if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700390 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 printk("os_set_fd_async : failed to set O_ASYNC and "
392 "O_NONBLOCK on 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#ifdef notdef
396 if(fcntl(fd, F_SETFD, 1) < 0){
397 printk("os_set_fd_async : Setting FD_CLOEXEC failed, "
398 "errno = %d\n", errno);
399 }
400#endif
401
402 if((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
403 (fcntl(fd, F_SETOWN, owner) < 0)){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700404 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 printk("os_set_fd_async : Failed to fcntl F_SETOWN "
Jeff Dike108ffa82006-07-10 04:45:14 -0700406 "(or F_SETSIG) fd %d to pid %d, errno = %d\n", fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 owner, errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700408 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
Jeff Dike108ffa82006-07-10 04:45:14 -0700411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414int os_clear_fd_async(int fd)
415{
416 int flags = fcntl(fd, F_GETFL);
417
418 flags &= ~(O_ASYNC | O_NONBLOCK);
419 if(fcntl(fd, F_SETFL, flags) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700420 return -errno;
421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424int os_set_fd_block(int fd, int blocking)
425{
426 int flags;
427
428 flags = fcntl(fd, F_GETFL);
429
430 if(blocking) flags &= ~O_NONBLOCK;
431 else flags |= O_NONBLOCK;
432
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700433 if(fcntl(fd, F_SETFL, flags) < 0)
434 return -errno;
435
Jeff Dike108ffa82006-07-10 04:45:14 -0700436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439int os_accept_connection(int fd)
440{
441 int new;
442
443 new = accept(fd, NULL, 0);
Jeff Dike108ffa82006-07-10 04:45:14 -0700444 if(new < 0)
445 return -errno;
446 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449#ifndef SHUT_RD
450#define SHUT_RD 0
451#endif
452
453#ifndef SHUT_WR
454#define SHUT_WR 1
455#endif
456
457#ifndef SHUT_RDWR
458#define SHUT_RDWR 2
459#endif
460
461int os_shutdown_socket(int fd, int r, int w)
462{
463 int what, err;
464
465 if(r && w) what = SHUT_RDWR;
466 else if(r) what = SHUT_RD;
467 else if(w) what = SHUT_WR;
468 else {
469 printk("os_shutdown_socket : neither r or w was set\n");
Jeff Dike108ffa82006-07-10 04:45:14 -0700470 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 err = shutdown(fd, what);
473 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700474 return -errno;
475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478int os_rcv_fd(int fd, int *helper_pid_out)
479{
480 int new, n;
481 char buf[CMSG_SPACE(sizeof(new))];
482 struct msghdr msg;
483 struct cmsghdr *cmsg;
484 struct iovec iov;
485
486 msg.msg_name = NULL;
487 msg.msg_namelen = 0;
488 iov = ((struct iovec) { .iov_base = helper_pid_out,
489 .iov_len = sizeof(*helper_pid_out) });
490 msg.msg_iov = &iov;
491 msg.msg_iovlen = 1;
492 msg.msg_control = buf;
493 msg.msg_controllen = sizeof(buf);
494 msg.msg_flags = 0;
495
496 n = recvmsg(fd, &msg, 0);
497 if(n < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700498 return -errno;
Jeff Diked4d5d202007-11-14 16:58:51 -0800499 else if(n != iov.iov_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 *helper_pid_out = -1;
501
502 cmsg = CMSG_FIRSTHDR(&msg);
503 if(cmsg == NULL){
504 printk("rcv_fd didn't receive anything, error = %d\n", errno);
Jeff Dike108ffa82006-07-10 04:45:14 -0700505 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700507 if((cmsg->cmsg_level != SOL_SOCKET) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 (cmsg->cmsg_type != SCM_RIGHTS)){
509 printk("rcv_fd didn't receive a descriptor\n");
Jeff Dike108ffa82006-07-10 04:45:14 -0700510 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
513 new = ((int *) CMSG_DATA(cmsg))[0];
Jeff Dike108ffa82006-07-10 04:45:14 -0700514 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
WANG Congc9a30722008-02-04 22:30:35 -0800517int os_create_unix_socket(const char *file, int len, int close_on_exec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct sockaddr_un addr;
520 int sock, err;
521
522 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700523 if(sock < 0)
524 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if(close_on_exec) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700527 err = os_set_exec_close(sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if(err < 0)
529 printk("create_unix_socket : close_on_exec failed, "
530 "err = %d", -err);
531 }
532
533 addr.sun_family = AF_UNIX;
534
535 /* XXX Be more careful about overflow */
536 snprintf(addr.sun_path, len, "%s", file);
537
538 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700539 if(err < 0)
540 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Jeff Dike108ffa82006-07-10 04:45:14 -0700542 return sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545void os_flush_stdout(void)
546{
547 fflush(stdout);
548}
549
550int os_lock_file(int fd, int excl)
551{
552 int type = excl ? F_WRLCK : F_RDLCK;
553 struct flock lock = ((struct flock) { .l_type = type,
554 .l_whence = SEEK_SET,
555 .l_start = 0,
556 .l_len = 0 } );
557 int err, save;
558
559 err = fcntl(fd, F_SETLK, &lock);
560 if(!err)
561 goto out;
562
563 save = -errno;
564 err = fcntl(fd, F_GETLK, &lock);
565 if(err){
566 err = -errno;
567 goto out;
568 }
569
570 printk("F_SETLK failed, file already locked by pid %d\n", lock.l_pid);
571 err = save;
572 out:
Jeff Dike108ffa82006-07-10 04:45:14 -0700573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}