blob: 367ac33c566dce89a1a404e285f8a63a41463ec7 [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
22static void copy_stat(struct uml_stat *dst, struct stat64 *src)
23{
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
45 do {
46 err = fstat64(fd, &sbuf);
47 } while((err < 0) && (errno == EINTR)) ;
48
49 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070050 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 if(ubuf != NULL)
53 copy_stat(ubuf, &sbuf);
Jeff Dike108ffa82006-07-10 04:45:14 -070054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
57int os_stat_file(const char *file_name, struct uml_stat *ubuf)
58{
59 struct stat64 sbuf;
60 int err;
61
62 do {
63 err = stat64(file_name, &sbuf);
64 } while((err < 0) && (errno == EINTR)) ;
65
66 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070067 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 if(ubuf != NULL)
70 copy_stat(ubuf, &sbuf);
Jeff Dike108ffa82006-07-10 04:45:14 -070071 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74int os_access(const char* file, int mode)
75{
76 int amode, err;
77
78 amode=(mode&OS_ACC_R_OK ? R_OK : 0) | (mode&OS_ACC_W_OK ? W_OK : 0) |
79 (mode&OS_ACC_X_OK ? X_OK : 0) | (mode&OS_ACC_F_OK ? F_OK : 0) ;
80
81 err = access(file, amode);
82 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -070083 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Jeff Dike108ffa82006-07-10 04:45:14 -070085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88void os_print_error(int error, const char* str)
89{
90 errno = error < 0 ? -error : error;
91
92 perror(str);
93}
94
95/* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
96int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
97{
98 int err;
99
100 err = ioctl(fd, cmd, arg);
101 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700102 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jeff Dike108ffa82006-07-10 04:45:14 -0700104 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107int os_window_size(int fd, int *rows, int *cols)
108{
109 struct winsize size;
110
111 if(ioctl(fd, TIOCGWINSZ, &size) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700112 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 *rows = size.ws_row;
115 *cols = size.ws_col;
116
Jeff Dike108ffa82006-07-10 04:45:14 -0700117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120int os_new_tty_pgrp(int fd, int pid)
121{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700122 if(ioctl(fd, TIOCSCTTY, 0) < 0)
123 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700125 if(tcsetpgrp(fd, pid) < 0)
126 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Jeff Dike108ffa82006-07-10 04:45:14 -0700128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131/* FIXME: ensure namebuf in os_get_if_name is big enough */
132int os_get_ifname(int fd, char* namebuf)
133{
134 if(ioctl(fd, SIOCGIFNAME, namebuf) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700135 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jeff Dike108ffa82006-07-10 04:45:14 -0700137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140int os_set_slip(int fd)
141{
142 int disc, sencap;
143
144 disc = N_SLIP;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700145 if(ioctl(fd, TIOCSETD, &disc) < 0)
146 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 sencap = 0;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700149 if(ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
150 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Jeff Dike108ffa82006-07-10 04:45:14 -0700152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155int os_set_owner(int fd, int pid)
156{
157 if(fcntl(fd, F_SETOWN, pid) < 0){
158 int save_errno = errno;
159
160 if(fcntl(fd, F_GETOWN, 0) != pid)
Jeff Dike108ffa82006-07-10 04:45:14 -0700161 return -save_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163
Jeff Dike108ffa82006-07-10 04:45:14 -0700164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167/* FIXME? moved wholesale from sigio_user.c to get fcntls out of that file */
168int os_sigio_async(int master, int slave)
169{
170 int flags;
171
172 flags = fcntl(master, F_GETFL);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700173 if(flags < 0)
Jeff Dike2ace87b2006-05-01 12:16:00 -0700174 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700177 (fcntl(master, F_SETOWN, os_getpid()) < 0))
178 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700180 if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
181 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 return(0);
184}
185
186int os_mode_fd(int fd, int mode)
187{
188 int err;
189
190 do {
191 err = fchmod(fd, mode);
192 } while((err < 0) && (errno==EINTR)) ;
193
194 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700195 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jeff Dike108ffa82006-07-10 04:45:14 -0700197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200int os_file_type(char *file)
201{
202 struct uml_stat buf;
203 int err;
204
205 err = os_stat_file(file, &buf);
206 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700207 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Jeff Dike108ffa82006-07-10 04:45:14 -0700209 if(S_ISDIR(buf.ust_mode))
210 return OS_TYPE_DIR;
211 else if(S_ISLNK(buf.ust_mode))
212 return OS_TYPE_SYMLINK;
213 else if(S_ISCHR(buf.ust_mode))
214 return OS_TYPE_CHARDEV;
215 else if(S_ISBLK(buf.ust_mode))
216 return OS_TYPE_BLOCKDEV;
217 else if(S_ISFIFO(buf.ust_mode))
218 return OS_TYPE_FIFO;
219 else if(S_ISSOCK(buf.ust_mode))
220 return OS_TYPE_SOCK;
221 else return OS_TYPE_FILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224int os_file_mode(char *file, struct openflags *mode_out)
225{
226 int err;
227
228 *mode_out = OPENFLAGS();
229
230 err = os_access(file, OS_ACC_W_OK);
231 if((err < 0) && (err != -EACCES))
232 return(err);
233
234 *mode_out = of_write(*mode_out);
235
236 err = os_access(file, OS_ACC_R_OK);
237 if((err < 0) && (err != -EACCES))
238 return(err);
239
240 *mode_out = of_read(*mode_out);
241
242 return(0);
243}
244
245int os_open_file(char *file, struct openflags flags, int mode)
246{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700247 int fd, err, f = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if(flags.r && flags.w) f = O_RDWR;
250 else if(flags.r) f = O_RDONLY;
251 else if(flags.w) f = O_WRONLY;
252 else f = 0;
253
254 if(flags.s) f |= O_SYNC;
255 if(flags.c) f |= O_CREAT;
256 if(flags.t) f |= O_TRUNC;
257 if(flags.e) f |= O_EXCL;
258
259 fd = open64(file, f, mode);
260 if(fd < 0)
261 return(-errno);
262
263 if(flags.cl && fcntl(fd, F_SETFD, 1)){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700264 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 os_close_file(fd);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700266 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
269 return(fd);
270}
271
272int os_connect_socket(char *name)
273{
274 struct sockaddr_un sock;
275 int fd, err;
276
277 sock.sun_family = AF_UNIX;
278 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
279
280 fd = socket(AF_UNIX, SOCK_STREAM, 0);
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800281 if(fd < 0) {
282 err = -errno;
283 goto out;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800287 if(err) {
288 err = -errno;
289 goto out_close;
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800292 return fd;
293
294out_close:
295 close(fd);
296out:
297 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300void os_close_file(int fd)
301{
302 close(fd);
303}
304
305int os_seek_file(int fd, __u64 offset)
306{
307 __u64 actual;
308
309 actual = lseek64(fd, offset, SEEK_SET);
310 if(actual != offset)
Jeff Dike108ffa82006-07-10 04:45:14 -0700311 return -errno;
312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315static int fault_buffer(void *start, int len,
316 int (*copy_proc)(void *addr, void *buf, int len))
317{
318 int page = getpagesize(), i;
319 char c;
320
321 for(i = 0; i < len; i += page){
322 if((*copy_proc)(start + i, &c, sizeof(c)))
Jeff Dike108ffa82006-07-10 04:45:14 -0700323 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325 if((len % page) != 0){
326 if((*copy_proc)(start + len - 1, &c, sizeof(c)))
Jeff Dike108ffa82006-07-10 04:45:14 -0700327 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332static int file_io(int fd, void *buf, int len,
333 int (*io_proc)(int fd, void *buf, int len),
334 int (*copy_user_proc)(void *addr, void *buf, int len))
335{
336 int n, err;
337
338 do {
339 n = (*io_proc)(fd, buf, len);
340 if((n < 0) && (errno == EFAULT)){
341 err = fault_buffer(buf, len, copy_user_proc);
342 if(err)
Jeff Dike108ffa82006-07-10 04:45:14 -0700343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 n = (*io_proc)(fd, buf, len);
345 }
346 } while((n < 0) && (errno == EINTR));
347
348 if(n < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700349 return -errno;
350 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353int os_read_file(int fd, void *buf, int len)
354{
Jeff Dike108ffa82006-07-10 04:45:14 -0700355 return file_io(fd, buf, len, (int (*)(int, void *, int)) read,
356 copy_from_user_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359int os_write_file(int fd, const void *buf, int len)
360{
Jeff Dike108ffa82006-07-10 04:45:14 -0700361 return file_io(fd, (void *) buf, len,
362 (int (*)(int, void *, int)) write, copy_to_user_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Jeff Dikeda00d9a2005-06-08 15:48:01 -0700365int os_file_size(char *file, unsigned long long *size_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct uml_stat buf;
368 int err;
369
370 err = os_stat_file(file, &buf);
371 if(err < 0){
372 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
373 return(err);
374 }
375
376 if(S_ISBLK(buf.ust_mode)){
377 int fd, blocks;
378
379 fd = os_open_file(file, of_read(OPENFLAGS()), 0);
380 if(fd < 0){
381 printk("Couldn't open \"%s\", errno = %d\n", file, -fd);
382 return(fd);
383 }
384 if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700385 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 printk("Couldn't get the block size of \"%s\", "
387 "errno = %d\n", file, errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 os_close_file(fd);
389 return(err);
390 }
391 *size_out = ((long long) blocks) * 512;
392 os_close_file(fd);
393 return(0);
394 }
395 *size_out = buf.ust_size;
396 return(0);
397}
398
399int os_file_modtime(char *file, unsigned long *modtime)
400{
401 struct uml_stat buf;
402 int err;
403
404 err = os_stat_file(file, &buf);
405 if(err < 0){
406 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
Jeff Dike108ffa82006-07-10 04:45:14 -0700407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
410 *modtime = buf.ust_mtime;
Jeff Dike108ffa82006-07-10 04:45:14 -0700411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414int os_get_exec_close(int fd, int* close_on_exec)
415{
416 int ret;
417
418 do {
419 ret = fcntl(fd, F_GETFD);
420 } while((ret < 0) && (errno == EINTR)) ;
421
422 if(ret < 0)
423 return(-errno);
424
425 *close_on_exec = (ret&FD_CLOEXEC) ? 1 : 0;
426 return(ret);
427}
428
429int os_set_exec_close(int fd, int close_on_exec)
430{
431 int flag, err;
432
433 if(close_on_exec) flag = FD_CLOEXEC;
434 else flag = 0;
435
436 do {
437 err = fcntl(fd, F_SETFD, flag);
438 } while((err < 0) && (errno == EINTR)) ;
439
440 if(err < 0)
441 return(-errno);
442 return(err);
443}
444
445int os_pipe(int *fds, int stream, int close_on_exec)
446{
447 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
448
449 err = socketpair(AF_UNIX, type, 0, fds);
450 if(err < 0)
451 return(-errno);
452
453 if(!close_on_exec)
454 return(0);
455
456 err = os_set_exec_close(fds[0], 1);
457 if(err < 0)
458 goto error;
459
460 err = os_set_exec_close(fds[1], 1);
461 if(err < 0)
462 goto error;
463
Jeff Dike108ffa82006-07-10 04:45:14 -0700464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 error:
467 printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err);
468 os_close_file(fds[1]);
469 os_close_file(fds[0]);
470 return(err);
471}
472
473int os_set_fd_async(int fd, int owner)
474{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700475 int err;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /* XXX This should do F_GETFL first */
478 if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700479 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 printk("os_set_fd_async : failed to set O_ASYNC and "
481 "O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700482 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484#ifdef notdef
485 if(fcntl(fd, F_SETFD, 1) < 0){
486 printk("os_set_fd_async : Setting FD_CLOEXEC failed, "
487 "errno = %d\n", errno);
488 }
489#endif
490
491 if((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
492 (fcntl(fd, F_SETOWN, owner) < 0)){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700493 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 printk("os_set_fd_async : Failed to fcntl F_SETOWN "
Jeff Dike108ffa82006-07-10 04:45:14 -0700495 "(or F_SETSIG) fd %d to pid %d, errno = %d\n", fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 owner, errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700497 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
Jeff Dike108ffa82006-07-10 04:45:14 -0700500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503int os_clear_fd_async(int fd)
504{
505 int flags = fcntl(fd, F_GETFL);
506
507 flags &= ~(O_ASYNC | O_NONBLOCK);
508 if(fcntl(fd, F_SETFL, flags) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700509 return -errno;
510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513int os_set_fd_block(int fd, int blocking)
514{
515 int flags;
516
517 flags = fcntl(fd, F_GETFL);
518
519 if(blocking) flags &= ~O_NONBLOCK;
520 else flags |= O_NONBLOCK;
521
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700522 if(fcntl(fd, F_SETFL, flags) < 0)
523 return -errno;
524
Jeff Dike108ffa82006-07-10 04:45:14 -0700525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528int os_accept_connection(int fd)
529{
530 int new;
531
532 new = accept(fd, NULL, 0);
Jeff Dike108ffa82006-07-10 04:45:14 -0700533 if(new < 0)
534 return -errno;
535 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538#ifndef SHUT_RD
539#define SHUT_RD 0
540#endif
541
542#ifndef SHUT_WR
543#define SHUT_WR 1
544#endif
545
546#ifndef SHUT_RDWR
547#define SHUT_RDWR 2
548#endif
549
550int os_shutdown_socket(int fd, int r, int w)
551{
552 int what, err;
553
554 if(r && w) what = SHUT_RDWR;
555 else if(r) what = SHUT_RD;
556 else if(w) what = SHUT_WR;
557 else {
558 printk("os_shutdown_socket : neither r or w was set\n");
Jeff Dike108ffa82006-07-10 04:45:14 -0700559 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 err = shutdown(fd, what);
562 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700563 return -errno;
564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567int os_rcv_fd(int fd, int *helper_pid_out)
568{
569 int new, n;
570 char buf[CMSG_SPACE(sizeof(new))];
571 struct msghdr msg;
572 struct cmsghdr *cmsg;
573 struct iovec iov;
574
575 msg.msg_name = NULL;
576 msg.msg_namelen = 0;
577 iov = ((struct iovec) { .iov_base = helper_pid_out,
578 .iov_len = sizeof(*helper_pid_out) });
579 msg.msg_iov = &iov;
580 msg.msg_iovlen = 1;
581 msg.msg_control = buf;
582 msg.msg_controllen = sizeof(buf);
583 msg.msg_flags = 0;
584
585 n = recvmsg(fd, &msg, 0);
586 if(n < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700587 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 else if(n != sizeof(iov.iov_len))
590 *helper_pid_out = -1;
591
592 cmsg = CMSG_FIRSTHDR(&msg);
593 if(cmsg == NULL){
594 printk("rcv_fd didn't receive anything, error = %d\n", errno);
Jeff Dike108ffa82006-07-10 04:45:14 -0700595 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700597 if((cmsg->cmsg_level != SOL_SOCKET) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 (cmsg->cmsg_type != SCM_RIGHTS)){
599 printk("rcv_fd didn't receive a descriptor\n");
Jeff Dike108ffa82006-07-10 04:45:14 -0700600 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
603 new = ((int *) CMSG_DATA(cmsg))[0];
Jeff Dike108ffa82006-07-10 04:45:14 -0700604 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607int os_create_unix_socket(char *file, int len, int close_on_exec)
608{
609 struct sockaddr_un addr;
610 int sock, err;
611
612 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700613 if(sock < 0)
614 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if(close_on_exec) {
617 err = os_set_exec_close(sock, 1);
618 if(err < 0)
619 printk("create_unix_socket : close_on_exec failed, "
620 "err = %d", -err);
621 }
622
623 addr.sun_family = AF_UNIX;
624
625 /* XXX Be more careful about overflow */
626 snprintf(addr.sun_path, len, "%s", file);
627
628 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700629 if(err < 0)
630 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Jeff Dike108ffa82006-07-10 04:45:14 -0700632 return sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635void os_flush_stdout(void)
636{
637 fflush(stdout);
638}
639
640int os_lock_file(int fd, int excl)
641{
642 int type = excl ? F_WRLCK : F_RDLCK;
643 struct flock lock = ((struct flock) { .l_type = type,
644 .l_whence = SEEK_SET,
645 .l_start = 0,
646 .l_len = 0 } );
647 int err, save;
648
649 err = fcntl(fd, F_SETLK, &lock);
650 if(!err)
651 goto out;
652
653 save = -errno;
654 err = fcntl(fd, F_GETLK, &lock);
655 if(err){
656 err = -errno;
657 goto out;
658 }
659
660 printk("F_SETLK failed, file already locked by pid %d\n", lock.l_pid);
661 err = save;
662 out:
Jeff Dike108ffa82006-07-10 04:45:14 -0700663 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}