blob: 371b4335f46dd2985fa852ef4a97abccc03080e5 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165int os_mode_fd(int fd, int mode)
166{
167 int err;
168
169 do {
170 err = fchmod(fd, mode);
171 } while((err < 0) && (errno==EINTR)) ;
172
173 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700174 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jeff Dike108ffa82006-07-10 04:45:14 -0700176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179int os_file_type(char *file)
180{
181 struct uml_stat buf;
182 int err;
183
184 err = os_stat_file(file, &buf);
185 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Jeff Dike108ffa82006-07-10 04:45:14 -0700188 if(S_ISDIR(buf.ust_mode))
189 return OS_TYPE_DIR;
190 else if(S_ISLNK(buf.ust_mode))
191 return OS_TYPE_SYMLINK;
192 else if(S_ISCHR(buf.ust_mode))
193 return OS_TYPE_CHARDEV;
194 else if(S_ISBLK(buf.ust_mode))
195 return OS_TYPE_BLOCKDEV;
196 else if(S_ISFIFO(buf.ust_mode))
197 return OS_TYPE_FIFO;
198 else if(S_ISSOCK(buf.ust_mode))
199 return OS_TYPE_SOCK;
200 else return OS_TYPE_FILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203int os_file_mode(char *file, struct openflags *mode_out)
204{
205 int err;
206
207 *mode_out = OPENFLAGS();
208
209 err = os_access(file, OS_ACC_W_OK);
210 if((err < 0) && (err != -EACCES))
211 return(err);
212
213 *mode_out = of_write(*mode_out);
214
215 err = os_access(file, OS_ACC_R_OK);
216 if((err < 0) && (err != -EACCES))
217 return(err);
218
219 *mode_out = of_read(*mode_out);
220
221 return(0);
222}
223
224int os_open_file(char *file, struct openflags flags, int mode)
225{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700226 int fd, err, f = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if(flags.r && flags.w) f = O_RDWR;
229 else if(flags.r) f = O_RDONLY;
230 else if(flags.w) f = O_WRONLY;
231 else f = 0;
232
233 if(flags.s) f |= O_SYNC;
234 if(flags.c) f |= O_CREAT;
235 if(flags.t) f |= O_TRUNC;
236 if(flags.e) f |= O_EXCL;
237
238 fd = open64(file, f, mode);
239 if(fd < 0)
240 return(-errno);
241
242 if(flags.cl && fcntl(fd, F_SETFD, 1)){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700243 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 os_close_file(fd);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700245 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
248 return(fd);
249}
250
251int os_connect_socket(char *name)
252{
253 struct sockaddr_un sock;
254 int fd, err;
255
256 sock.sun_family = AF_UNIX;
257 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
258
259 fd = socket(AF_UNIX, SOCK_STREAM, 0);
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800260 if(fd < 0) {
261 err = -errno;
262 goto out;
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800266 if(err) {
267 err = -errno;
268 goto out_close;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Paolo 'Blaisorblade' Giarrussodc1561a2006-02-24 13:03:56 -0800271 return fd;
272
273out_close:
274 close(fd);
275out:
276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279void os_close_file(int fd)
280{
281 close(fd);
282}
283
284int os_seek_file(int fd, __u64 offset)
285{
286 __u64 actual;
287
288 actual = lseek64(fd, offset, SEEK_SET);
289 if(actual != offset)
Jeff Dike108ffa82006-07-10 04:45:14 -0700290 return -errno;
291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294static int fault_buffer(void *start, int len,
295 int (*copy_proc)(void *addr, void *buf, int len))
296{
297 int page = getpagesize(), i;
298 char c;
299
300 for(i = 0; i < len; i += page){
301 if((*copy_proc)(start + i, &c, sizeof(c)))
Jeff Dike108ffa82006-07-10 04:45:14 -0700302 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304 if((len % page) != 0){
305 if((*copy_proc)(start + len - 1, &c, sizeof(c)))
Jeff Dike108ffa82006-07-10 04:45:14 -0700306 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311static int file_io(int fd, void *buf, int len,
312 int (*io_proc)(int fd, void *buf, int len),
313 int (*copy_user_proc)(void *addr, void *buf, int len))
314{
315 int n, err;
316
317 do {
318 n = (*io_proc)(fd, buf, len);
319 if((n < 0) && (errno == EFAULT)){
320 err = fault_buffer(buf, len, copy_user_proc);
321 if(err)
Jeff Dike108ffa82006-07-10 04:45:14 -0700322 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 n = (*io_proc)(fd, buf, len);
324 }
325 } while((n < 0) && (errno == EINTR));
326
327 if(n < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700328 return -errno;
329 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332int os_read_file(int fd, void *buf, int len)
333{
Jeff Dike108ffa82006-07-10 04:45:14 -0700334 return file_io(fd, buf, len, (int (*)(int, void *, int)) read,
335 copy_from_user_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338int os_write_file(int fd, const void *buf, int len)
339{
Jeff Dike108ffa82006-07-10 04:45:14 -0700340 return file_io(fd, (void *) buf, len,
341 (int (*)(int, void *, int)) write, copy_to_user_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Jeff Dikeda00d9a2005-06-08 15:48:01 -0700344int os_file_size(char *file, unsigned long long *size_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct uml_stat buf;
347 int err;
348
349 err = os_stat_file(file, &buf);
350 if(err < 0){
351 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
352 return(err);
353 }
354
355 if(S_ISBLK(buf.ust_mode)){
356 int fd, blocks;
357
358 fd = os_open_file(file, of_read(OPENFLAGS()), 0);
359 if(fd < 0){
360 printk("Couldn't open \"%s\", errno = %d\n", file, -fd);
361 return(fd);
362 }
363 if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700364 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 printk("Couldn't get the block size of \"%s\", "
366 "errno = %d\n", file, errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 os_close_file(fd);
368 return(err);
369 }
370 *size_out = ((long long) blocks) * 512;
371 os_close_file(fd);
372 return(0);
373 }
374 *size_out = buf.ust_size;
375 return(0);
376}
377
378int os_file_modtime(char *file, unsigned long *modtime)
379{
380 struct uml_stat buf;
381 int err;
382
383 err = os_stat_file(file, &buf);
384 if(err < 0){
385 printk("Couldn't stat \"%s\" : err = %d\n", file, -err);
Jeff Dike108ffa82006-07-10 04:45:14 -0700386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 *modtime = buf.ust_mtime;
Jeff Dike108ffa82006-07-10 04:45:14 -0700390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393int os_get_exec_close(int fd, int* close_on_exec)
394{
395 int ret;
396
397 do {
398 ret = fcntl(fd, F_GETFD);
399 } while((ret < 0) && (errno == EINTR)) ;
400
401 if(ret < 0)
402 return(-errno);
403
404 *close_on_exec = (ret&FD_CLOEXEC) ? 1 : 0;
405 return(ret);
406}
407
408int os_set_exec_close(int fd, int close_on_exec)
409{
410 int flag, err;
411
412 if(close_on_exec) flag = FD_CLOEXEC;
413 else flag = 0;
414
415 do {
416 err = fcntl(fd, F_SETFD, flag);
417 } while((err < 0) && (errno == EINTR)) ;
418
419 if(err < 0)
420 return(-errno);
421 return(err);
422}
423
424int os_pipe(int *fds, int stream, int close_on_exec)
425{
426 int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
427
428 err = socketpair(AF_UNIX, type, 0, fds);
429 if(err < 0)
430 return(-errno);
431
432 if(!close_on_exec)
433 return(0);
434
435 err = os_set_exec_close(fds[0], 1);
436 if(err < 0)
437 goto error;
438
439 err = os_set_exec_close(fds[1], 1);
440 if(err < 0)
441 goto error;
442
Jeff Dike108ffa82006-07-10 04:45:14 -0700443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 error:
446 printk("os_pipe : Setting FD_CLOEXEC failed, err = %d\n", -err);
447 os_close_file(fds[1]);
448 os_close_file(fds[0]);
449 return(err);
450}
451
452int os_set_fd_async(int fd, int owner)
453{
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700454 int err;
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* XXX This should do F_GETFL first */
457 if(fcntl(fd, F_SETFL, O_ASYNC | O_NONBLOCK) < 0){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700458 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 printk("os_set_fd_async : failed to set O_ASYNC and "
460 "O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700461 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463#ifdef notdef
464 if(fcntl(fd, F_SETFD, 1) < 0){
465 printk("os_set_fd_async : Setting FD_CLOEXEC failed, "
466 "errno = %d\n", errno);
467 }
468#endif
469
470 if((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
471 (fcntl(fd, F_SETOWN, owner) < 0)){
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700472 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 printk("os_set_fd_async : Failed to fcntl F_SETOWN "
Jeff Dike108ffa82006-07-10 04:45:14 -0700474 "(or F_SETSIG) fd %d to pid %d, errno = %d\n", fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 owner, errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700476 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
Jeff Dike108ffa82006-07-10 04:45:14 -0700479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482int os_clear_fd_async(int fd)
483{
484 int flags = fcntl(fd, F_GETFL);
485
486 flags &= ~(O_ASYNC | O_NONBLOCK);
487 if(fcntl(fd, F_SETFL, flags) < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700488 return -errno;
489 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492int os_set_fd_block(int fd, int blocking)
493{
494 int flags;
495
496 flags = fcntl(fd, F_GETFL);
497
498 if(blocking) flags &= ~O_NONBLOCK;
499 else flags |= O_NONBLOCK;
500
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700501 if(fcntl(fd, F_SETFL, flags) < 0)
502 return -errno;
503
Jeff Dike108ffa82006-07-10 04:45:14 -0700504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507int os_accept_connection(int fd)
508{
509 int new;
510
511 new = accept(fd, NULL, 0);
Jeff Dike108ffa82006-07-10 04:45:14 -0700512 if(new < 0)
513 return -errno;
514 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517#ifndef SHUT_RD
518#define SHUT_RD 0
519#endif
520
521#ifndef SHUT_WR
522#define SHUT_WR 1
523#endif
524
525#ifndef SHUT_RDWR
526#define SHUT_RDWR 2
527#endif
528
529int os_shutdown_socket(int fd, int r, int w)
530{
531 int what, err;
532
533 if(r && w) what = SHUT_RDWR;
534 else if(r) what = SHUT_RD;
535 else if(w) what = SHUT_WR;
536 else {
537 printk("os_shutdown_socket : neither r or w was set\n");
Jeff Dike108ffa82006-07-10 04:45:14 -0700538 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 err = shutdown(fd, what);
541 if(err < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700542 return -errno;
543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546int os_rcv_fd(int fd, int *helper_pid_out)
547{
548 int new, n;
549 char buf[CMSG_SPACE(sizeof(new))];
550 struct msghdr msg;
551 struct cmsghdr *cmsg;
552 struct iovec iov;
553
554 msg.msg_name = NULL;
555 msg.msg_namelen = 0;
556 iov = ((struct iovec) { .iov_base = helper_pid_out,
557 .iov_len = sizeof(*helper_pid_out) });
558 msg.msg_iov = &iov;
559 msg.msg_iovlen = 1;
560 msg.msg_control = buf;
561 msg.msg_controllen = sizeof(buf);
562 msg.msg_flags = 0;
563
564 n = recvmsg(fd, &msg, 0);
565 if(n < 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700566 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 else if(n != sizeof(iov.iov_len))
569 *helper_pid_out = -1;
570
571 cmsg = CMSG_FIRSTHDR(&msg);
572 if(cmsg == NULL){
573 printk("rcv_fd didn't receive anything, error = %d\n", errno);
Jeff Dike108ffa82006-07-10 04:45:14 -0700574 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700576 if((cmsg->cmsg_level != SOL_SOCKET) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 (cmsg->cmsg_type != SCM_RIGHTS)){
578 printk("rcv_fd didn't receive a descriptor\n");
Jeff Dike108ffa82006-07-10 04:45:14 -0700579 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
582 new = ((int *) CMSG_DATA(cmsg))[0];
Jeff Dike108ffa82006-07-10 04:45:14 -0700583 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586int os_create_unix_socket(char *file, int len, int close_on_exec)
587{
588 struct sockaddr_un addr;
589 int sock, err;
590
591 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700592 if(sock < 0)
593 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if(close_on_exec) {
596 err = os_set_exec_close(sock, 1);
597 if(err < 0)
598 printk("create_unix_socket : close_on_exec failed, "
599 "err = %d", -err);
600 }
601
602 addr.sun_family = AF_UNIX;
603
604 /* XXX Be more careful about overflow */
605 snprintf(addr.sun_path, len, "%s", file);
606
607 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700608 if(err < 0)
609 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Jeff Dike108ffa82006-07-10 04:45:14 -0700611 return sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614void os_flush_stdout(void)
615{
616 fflush(stdout);
617}
618
619int os_lock_file(int fd, int excl)
620{
621 int type = excl ? F_WRLCK : F_RDLCK;
622 struct flock lock = ((struct flock) { .l_type = type,
623 .l_whence = SEEK_SET,
624 .l_start = 0,
625 .l_len = 0 } );
626 int err, save;
627
628 err = fcntl(fd, F_SETLK, &lock);
629 if(!err)
630 goto out;
631
632 save = -errno;
633 err = fcntl(fd, F_GETLK, &lock);
634 if(err){
635 err = -errno;
636 goto out;
637 }
638
639 printk("F_SETLK failed, file already locked by pid %d\n", lock.l_pid);
640 err = save;
641 out:
Jeff Dike108ffa82006-07-10 04:45:14 -0700642 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}