blob: 189fa677085a7880d9d8c7bdbcd4593de04a3ea1 [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"
Jeff Dike9ead6fe2006-07-10 04:45:15 -070021#include "user_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23static void copy_stat(struct uml_stat *dst, struct stat64 *src)
24{
25 *dst = ((struct uml_stat) {
26 .ust_dev = src->st_dev, /* device */
27 .ust_ino = src->st_ino, /* inode */
28 .ust_mode = src->st_mode, /* protection */
29 .ust_nlink = src->st_nlink, /* number of hard links */
30 .ust_uid = src->st_uid, /* user ID of owner */
31 .ust_gid = src->st_gid, /* group ID of owner */
32 .ust_size = src->st_size, /* total size, in bytes */
33 .ust_blksize = src->st_blksize, /* blocksize for filesys I/O */
34 .ust_blocks = src->st_blocks, /* number of blocks allocated */
35 .ust_atime = src->st_atime, /* time of last access */
36 .ust_mtime = src->st_mtime, /* time of last modification */
37 .ust_ctime = src->st_ctime, /* time of last change */
38 });
39}
40
41int os_stat_fd(const int fd, struct uml_stat *ubuf)
42{
43 struct stat64 sbuf;
44 int err;
45
Jeff Dike9ead6fe2006-07-10 04:45:15 -070046 CATCH_EINTR(err = fstat64(fd, &sbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070048 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 if(ubuf != NULL)
51 copy_stat(ubuf, &sbuf);
Jeff Dike108ffa82006-07-10 04:45:14 -070052 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
55int os_stat_file(const char *file_name, struct uml_stat *ubuf)
56{
57 struct stat64 sbuf;
58 int err;
59
60 do {
61 err = stat64(file_name, &sbuf);
62 } while((err < 0) && (errno == EINTR)) ;
63
64 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070065 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 if(ubuf != NULL)
68 copy_stat(ubuf, &sbuf);
Jeff Dike108ffa82006-07-10 04:45:14 -070069 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72int os_access(const char* file, int mode)
73{
74 int amode, err;
75
76 amode=(mode&OS_ACC_R_OK ? R_OK : 0) | (mode&OS_ACC_W_OK ? W_OK : 0) |
77 (mode&OS_ACC_X_OK ? X_OK : 0) | (mode&OS_ACC_F_OK ? F_OK : 0) ;
78
79 err = access(file, amode);
80 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070081 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Jeff Dike108ffa82006-07-10 04:45:14 -070083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86void os_print_error(int error, const char* str)
87{
88 errno = error < 0 ? -error : error;
89
90 perror(str);
91}
92
93/* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
94int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
95{
96 int err;
97
98 err = ioctl(fd, cmd, arg);
99 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700100 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Jeff Dike108ffa82006-07-10 04:45:14 -0700102 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105int os_window_size(int fd, int *rows, int *cols)
106{
107 struct winsize size;
108
109 if(ioctl(fd, TIOCGWINSZ, &size) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700110 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 *rows = size.ws_row;
113 *cols = size.ws_col;
114
Jeff Dike108ffa82006-07-10 04:45:14 -0700115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118int os_new_tty_pgrp(int fd, int pid)
119{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700120 if(ioctl(fd, TIOCSCTTY, 0) < 0)
121 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700123 if(tcsetpgrp(fd, pid) < 0)
124 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
129/* FIXME: ensure namebuf in os_get_if_name is big enough */
130int os_get_ifname(int fd, char* namebuf)
131{
132 if(ioctl(fd, SIOCGIFNAME, namebuf) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700133 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Jeff Dike108ffa82006-07-10 04:45:14 -0700135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138int os_set_slip(int fd)
139{
140 int disc, sencap;
141
142 disc = N_SLIP;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700143 if(ioctl(fd, TIOCSETD, &disc) < 0)
144 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 sencap = 0;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700147 if(ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
148 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Jeff Dike108ffa82006-07-10 04:45:14 -0700150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153int os_set_owner(int fd, int pid)
154{
155 if(fcntl(fd, F_SETOWN, pid) < 0){
156 int save_errno = errno;
157
158 if(fcntl(fd, F_GETOWN, 0) != pid)
Jeff Dike108ffa82006-07-10 04:45:14 -0700159 return -save_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
Jeff Dike108ffa82006-07-10 04:45:14 -0700162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165/* FIXME? moved wholesale from sigio_user.c to get fcntls out of that file */
166int os_sigio_async(int master, int slave)
167{
168 int flags;
169
170 flags = fcntl(master, F_GETFL);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700171 if(flags < 0)
Jeff Dike2ace87b2006-05-01 12:16:00 -0700172 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700175 (fcntl(master, F_SETOWN, os_getpid()) < 0))
176 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700178 if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
179 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 return(0);
182}
183
184int os_mode_fd(int fd, int mode)
185{
186 int err;
187
188 do {
189 err = fchmod(fd, mode);
190 } while((err < 0) && (errno==EINTR)) ;
191
192 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700193 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Jeff Dike108ffa82006-07-10 04:45:14 -0700195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198int os_file_type(char *file)
199{
200 struct uml_stat buf;
201 int err;
202
203 err = os_stat_file(file, &buf);
204 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700205 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Jeff Dike108ffa82006-07-10 04:45:14 -0700207 if(S_ISDIR(buf.ust_mode))
208 return OS_TYPE_DIR;
209 else if(S_ISLNK(buf.ust_mode))
210 return OS_TYPE_SYMLINK;
211 else if(S_ISCHR(buf.ust_mode))
212 return OS_TYPE_CHARDEV;
213 else if(S_ISBLK(buf.ust_mode))
214 return OS_TYPE_BLOCKDEV;
215 else if(S_ISFIFO(buf.ust_mode))
216 return OS_TYPE_FIFO;
217 else if(S_ISSOCK(buf.ust_mode))
218 return OS_TYPE_SOCK;
219 else return OS_TYPE_FILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222int os_file_mode(char *file, struct openflags *mode_out)
223{
224 int err;
225
226 *mode_out = OPENFLAGS();
227
228 err = os_access(file, OS_ACC_W_OK);
229 if((err < 0) && (err != -EACCES))
230 return(err);
231
232 *mode_out = of_write(*mode_out);
233
234 err = os_access(file, OS_ACC_R_OK);
235 if((err < 0) && (err != -EACCES))
236 return(err);
237
238 *mode_out = of_read(*mode_out);
239
240 return(0);
241}
242
243int os_open_file(char *file, struct openflags flags, int mode)
244{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700245 int fd, err, f = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 if(flags.r && flags.w) f = O_RDWR;
248 else if(flags.r) f = O_RDONLY;
249 else if(flags.w) f = O_WRONLY;
250 else f = 0;
251
252 if(flags.s) f |= O_SYNC;
253 if(flags.c) f |= O_CREAT;
254 if(flags.t) f |= O_TRUNC;
255 if(flags.e) f |= O_EXCL;
256
257 fd = open64(file, f, mode);
258 if(fd < 0)
259 return(-errno);
260
261 if(flags.cl && fcntl(fd, F_SETFD, 1)){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700262 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 os_close_file(fd);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
267 return(fd);
268}
269
270int os_connect_socket(char *name)
271{
272 struct sockaddr_un sock;
273 int fd, err;
274
275 sock.sun_family = AF_UNIX;
276 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
277
278 fd = socket(AF_UNIX, SOCK_STREAM, 0);
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800279 if(fd < 0) {
280 err = -errno;
281 goto out;
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800285 if(err) {
286 err = -errno;
287 goto out_close;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800290 return fd;
291
292out_close:
293 close(fd);
294out:
295 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298void os_close_file(int fd)
299{
300 close(fd);
301}
302
303int os_seek_file(int fd, __u64 offset)
304{
305 __u64 actual;
306
307 actual = lseek64(fd, offset, SEEK_SET);
308 if(actual != offset)
Jeff Dike108ffa82006-07-10 04:45:14 -0700309 return -errno;
310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313static int fault_buffer(void *start, int len,
314 int (*copy_proc)(void *addr, void *buf, int len))
315{
316 int page = getpagesize(), i;
317 char c;
318
319 for(i = 0; i < len; i += page){
320 if((*copy_proc)(start + i, &c, sizeof(c)))
Jeff Dike108ffa82006-07-10 04:45:14 -0700321 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323 if((len % page) != 0){
324 if((*copy_proc)(start + len - 1, &c, sizeof(c)))
Jeff Dike108ffa82006-07-10 04:45:14 -0700325 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330static int file_io(int fd, void *buf, int len,
331 int (*io_proc)(int fd, void *buf, int len),
332 int (*copy_user_proc)(void *addr, void *buf, int len))
333{
334 int n, err;
335
336 do {
337 n = (*io_proc)(fd, buf, len);
338 if((n < 0) && (errno == EFAULT)){
339 err = fault_buffer(buf, len, copy_user_proc);
340 if(err)
Jeff Dike108ffa82006-07-10 04:45:14 -0700341 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 n = (*io_proc)(fd, buf, len);
343 }
344 } while((n < 0) && (errno == EINTR));
345
346 if(n < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700347 return -errno;
348 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351int os_read_file(int fd, void *buf, int len)
352{
Jeff Dike108ffa82006-07-10 04:45:14 -0700353 return file_io(fd, buf, len, (int (*)(int, void *, int)) read,
354 copy_from_user_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357int os_write_file(int fd, const void *buf, int len)
358{
Jeff Dike108ffa82006-07-10 04:45:14 -0700359 return file_io(fd, (void *) buf, len,
360 (int (*)(int, void *, int)) write, copy_to_user_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Jeff Dikeda00d9a2005-06-08 15:48:01 -0700363int os_file_size(char *file, unsigned long long *size_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct uml_stat buf;
366 int err;
367
368 err = os_stat_file(file, &buf);
369 if(err < 0){
370 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
371 return(err);
372 }
373
374 if(S_ISBLK(buf.ust_mode)){
375 int fd, blocks;
376
377 fd = os_open_file(file, of_read(OPENFLAGS()), 0);
378 if(fd < 0){
379 printk("Couldn't open \"%s\", errno = %d\n", file, -fd);
380 return(fd);
381 }
382 if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700383 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 printk("Couldn't get the block size of \"%s\", "
385 "errno = %d\n", file, errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 os_close_file(fd);
387 return(err);
388 }
389 *size_out = ((long long) blocks) * 512;
390 os_close_file(fd);
391 return(0);
392 }
393 *size_out = buf.ust_size;
394 return(0);
395}
396
397int os_file_modtime(char *file, unsigned long *modtime)
398{
399 struct uml_stat buf;
400 int err;
401
402 err = os_stat_file(file, &buf);
403 if(err < 0){
404 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
Jeff Dike108ffa82006-07-10 04:45:14 -0700405 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 *modtime = buf.ust_mtime;
Jeff Dike108ffa82006-07-10 04:45:14 -0700409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412int os_get_exec_close(int fd, int* close_on_exec)
413{
414 int ret;
415
416 do {
417 ret = fcntl(fd, F_GETFD);
418 } while((ret < 0) && (errno == EINTR)) ;
419
420 if(ret < 0)
421 return(-errno);
422
423 *close_on_exec = (ret&FD_CLOEXEC) ? 1 : 0;
424 return(ret);
425}
426
427int os_set_exec_close(int fd, int close_on_exec)
428{
429 int flag, err;
430
431 if(close_on_exec) flag = FD_CLOEXEC;
432 else flag = 0;
433
434 do {
435 err = fcntl(fd, F_SETFD, flag);
436 } while((err < 0) && (errno == EINTR)) ;
437
438 if(err < 0)
439 return(-errno);
440 return(err);
441}
442
443int os_pipe(int *fds, int stream, int close_on_exec)
444{
445 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
446
447 err = socketpair(AF_UNIX, type, 0, fds);
448 if(err < 0)
449 return(-errno);
450
451 if(!close_on_exec)
452 return(0);
453
454 err = os_set_exec_close(fds[0], 1);
455 if(err < 0)
456 goto error;
457
458 err = os_set_exec_close(fds[1], 1);
459 if(err < 0)
460 goto error;
461
Jeff Dike108ffa82006-07-10 04:45:14 -0700462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 error:
465 printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err);
466 os_close_file(fds[1]);
467 os_close_file(fds[0]);
468 return(err);
469}
470
471int os_set_fd_async(int fd, int owner)
472{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700473 int err;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /* XXX This should do F_GETFL first */
476 if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700477 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 printk("os_set_fd_async : failed to set O_ASYNC and "
479 "O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700480 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482#ifdef notdef
483 if(fcntl(fd, F_SETFD, 1) < 0){
484 printk("os_set_fd_async : Setting FD_CLOEXEC failed, "
485 "errno = %d\n", errno);
486 }
487#endif
488
489 if((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
490 (fcntl(fd, F_SETOWN, owner) < 0)){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700491 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 printk("os_set_fd_async : Failed to fcntl F_SETOWN "
Jeff Dike108ffa82006-07-10 04:45:14 -0700493 "(or F_SETSIG) fd %d to pid %d, errno = %d\n", fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 owner, errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700495 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
Jeff Dike108ffa82006-07-10 04:45:14 -0700498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501int os_clear_fd_async(int fd)
502{
503 int flags = fcntl(fd, F_GETFL);
504
505 flags &= ~(O_ASYNC | O_NONBLOCK);
506 if(fcntl(fd, F_SETFL, flags) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700507 return -errno;
508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511int os_set_fd_block(int fd, int blocking)
512{
513 int flags;
514
515 flags = fcntl(fd, F_GETFL);
516
517 if(blocking) flags &= ~O_NONBLOCK;
518 else flags |= O_NONBLOCK;
519
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700520 if(fcntl(fd, F_SETFL, flags) < 0)
521 return -errno;
522
Jeff Dike108ffa82006-07-10 04:45:14 -0700523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526int os_accept_connection(int fd)
527{
528 int new;
529
530 new = accept(fd, NULL, 0);
Jeff Dike108ffa82006-07-10 04:45:14 -0700531 if(new < 0)
532 return -errno;
533 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536#ifndef SHUT_RD
537#define SHUT_RD 0
538#endif
539
540#ifndef SHUT_WR
541#define SHUT_WR 1
542#endif
543
544#ifndef SHUT_RDWR
545#define SHUT_RDWR 2
546#endif
547
548int os_shutdown_socket(int fd, int r, int w)
549{
550 int what, err;
551
552 if(r && w) what = SHUT_RDWR;
553 else if(r) what = SHUT_RD;
554 else if(w) what = SHUT_WR;
555 else {
556 printk("os_shutdown_socket : neither r or w was set\n");
Jeff Dike108ffa82006-07-10 04:45:14 -0700557 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 err = shutdown(fd, what);
560 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700561 return -errno;
562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565int os_rcv_fd(int fd, int *helper_pid_out)
566{
567 int new, n;
568 char buf[CMSG_SPACE(sizeof(new))];
569 struct msghdr msg;
570 struct cmsghdr *cmsg;
571 struct iovec iov;
572
573 msg.msg_name = NULL;
574 msg.msg_namelen = 0;
575 iov = ((struct iovec) { .iov_base = helper_pid_out,
576 .iov_len = sizeof(*helper_pid_out) });
577 msg.msg_iov = &iov;
578 msg.msg_iovlen = 1;
579 msg.msg_control = buf;
580 msg.msg_controllen = sizeof(buf);
581 msg.msg_flags = 0;
582
583 n = recvmsg(fd, &msg, 0);
584 if(n < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700585 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 else if(n != sizeof(iov.iov_len))
588 *helper_pid_out = -1;
589
590 cmsg = CMSG_FIRSTHDR(&msg);
591 if(cmsg == NULL){
592 printk("rcv_fd didn't receive anything, error = %d\n", errno);
Jeff Dike108ffa82006-07-10 04:45:14 -0700593 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700595 if((cmsg->cmsg_level != SOL_SOCKET) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 (cmsg->cmsg_type != SCM_RIGHTS)){
597 printk("rcv_fd didn't receive a descriptor\n");
Jeff Dike108ffa82006-07-10 04:45:14 -0700598 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
601 new = ((int *) CMSG_DATA(cmsg))[0];
Jeff Dike108ffa82006-07-10 04:45:14 -0700602 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605int os_create_unix_socket(char *file, int len, int close_on_exec)
606{
607 struct sockaddr_un addr;
608 int sock, err;
609
610 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700611 if(sock < 0)
612 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if(close_on_exec) {
615 err = os_set_exec_close(sock, 1);
616 if(err < 0)
617 printk("create_unix_socket : close_on_exec failed, "
618 "err = %d", -err);
619 }
620
621 addr.sun_family = AF_UNIX;
622
623 /* XXX Be more careful about overflow */
624 snprintf(addr.sun_path, len, "%s", file);
625
626 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700627 if(err < 0)
628 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Jeff Dike108ffa82006-07-10 04:45:14 -0700630 return sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633void os_flush_stdout(void)
634{
635 fflush(stdout);
636}
637
638int os_lock_file(int fd, int excl)
639{
640 int type = excl ? F_WRLCK : F_RDLCK;
641 struct flock lock = ((struct flock) { .l_type = type,
642 .l_whence = SEEK_SET,
643 .l_start = 0,
644 .l_len = 0 } );
645 int err, save;
646
647 err = fcntl(fd, F_SETLK, &lock);
648 if(!err)
649 goto out;
650
651 save = -errno;
652 err = fcntl(fd, F_GETLK, &lock);
653 if(err){
654 err = -errno;
655 goto out;
656 }
657
658 printk("F_SETLK failed, file already locked by pid %d\n", lock.l_pid);
659 err = save;
660 out:
Jeff Dike108ffa82006-07-10 04:45:14 -0700661 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}