Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.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/ioctl.h>
 12#include <sys/mount.h>
 13#include <sys/socket.h>
 14#include <sys/stat.h>
 
 15#include <sys/un.h>
 16#include <sys/types.h>
 17#include <os.h>
 18
 19static void copy_stat(struct uml_stat *dst, const struct stat64 *src)
 20{
 21	*dst = ((struct uml_stat) {
 22		.ust_dev     = src->st_dev,     /* device */
 23		.ust_ino     = src->st_ino,     /* inode */
 24		.ust_mode    = src->st_mode,    /* protection */
 25		.ust_nlink   = src->st_nlink,   /* number of hard links */
 26		.ust_uid     = src->st_uid,     /* user ID of owner */
 27		.ust_gid     = src->st_gid,     /* group ID of owner */
 28		.ust_size    = src->st_size,    /* total size, in bytes */
 29		.ust_blksize = src->st_blksize, /* blocksize for filesys I/O */
 30		.ust_blocks  = src->st_blocks,  /* number of blocks allocated */
 31		.ust_atime   = src->st_atime,   /* time of last access */
 32		.ust_mtime   = src->st_mtime,   /* time of last modification */
 33		.ust_ctime   = src->st_ctime,   /* time of last change */
 34	});
 35}
 36
 37int os_stat_fd(const int fd, struct uml_stat *ubuf)
 38{
 39	struct stat64 sbuf;
 40	int err;
 41
 42	CATCH_EINTR(err = fstat64(fd, &sbuf));
 43	if (err < 0)
 44		return -errno;
 45
 46	if (ubuf != NULL)
 47		copy_stat(ubuf, &sbuf);
 48	return err;
 49}
 50
 51int os_stat_file(const char *file_name, struct uml_stat *ubuf)
 52{
 53	struct stat64 sbuf;
 54	int err;
 55
 56	CATCH_EINTR(err = stat64(file_name, &sbuf));
 57	if (err < 0)
 58		return -errno;
 59
 60	if (ubuf != NULL)
 61		copy_stat(ubuf, &sbuf);
 62	return err;
 63}
 64
 65int os_access(const char *file, int mode)
 66{
 67	int amode, err;
 68
 69	amode = (mode & OS_ACC_R_OK ? R_OK : 0) |
 70		(mode & OS_ACC_W_OK ? W_OK : 0) |
 71		(mode & OS_ACC_X_OK ? X_OK : 0) |
 72		(mode & OS_ACC_F_OK ? F_OK : 0);
 73
 74	err = access(file, amode);
 75	if (err < 0)
 76		return -errno;
 77
 78	return 0;
 79}
 80
 81/* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
 82int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
 83{
 84	int err;
 85
 86	err = ioctl(fd, cmd, arg);
 87	if (err < 0)
 88		return -errno;
 89
 90	return err;
 91}
 92
 93/* FIXME: ensure namebuf in os_get_if_name is big enough */
 94int os_get_ifname(int fd, char* namebuf)
 95{
 96	if (ioctl(fd, SIOCGIFNAME, namebuf) < 0)
 97		return -errno;
 98
 99	return 0;
100}
101
102int os_set_slip(int fd)
103{
104	int disc, sencap;
105
106	disc = N_SLIP;
107	if (ioctl(fd, TIOCSETD, &disc) < 0)
108		return -errno;
109
110	sencap = 0;
111	if (ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
112		return -errno;
113
114	return 0;
115}
116
117int os_mode_fd(int fd, int mode)
118{
119	int err;
120
121	CATCH_EINTR(err = fchmod(fd, mode));
122	if (err < 0)
123		return -errno;
124
125	return 0;
126}
127
128int os_file_type(char *file)
129{
130	struct uml_stat buf;
131	int err;
132
133	err = os_stat_file(file, &buf);
134	if (err < 0)
135		return err;
136
137	if (S_ISDIR(buf.ust_mode))
138		return OS_TYPE_DIR;
139	else if (S_ISLNK(buf.ust_mode))
140		return OS_TYPE_SYMLINK;
141	else if (S_ISCHR(buf.ust_mode))
142		return OS_TYPE_CHARDEV;
143	else if (S_ISBLK(buf.ust_mode))
144		return OS_TYPE_BLOCKDEV;
145	else if (S_ISFIFO(buf.ust_mode))
146		return OS_TYPE_FIFO;
147	else if (S_ISSOCK(buf.ust_mode))
148		return OS_TYPE_SOCK;
149	else return OS_TYPE_FILE;
150}
151
152int os_file_mode(const char *file, struct openflags *mode_out)
153{
154	int err;
155
156	*mode_out = OPENFLAGS();
157
158	err = access(file, W_OK);
159	if (err && (errno != EACCES))
160		return -errno;
161	else if (!err)
162		*mode_out = of_write(*mode_out);
163
164	err = access(file, R_OK);
165	if (err && (errno != EACCES))
166		return -errno;
167	else if (!err)
168		*mode_out = of_read(*mode_out);
169
170	return err;
171}
172
173int os_open_file(const char *file, struct openflags flags, int mode)
174{
175	int fd, err, f = 0;
176
177	if (flags.r && flags.w)
178		f = O_RDWR;
179	else if (flags.r)
180		f = O_RDONLY;
181	else if (flags.w)
182		f = O_WRONLY;
183	else f = 0;
184
185	if (flags.s)
186		f |= O_SYNC;
187	if (flags.c)
188		f |= O_CREAT;
189	if (flags.t)
190		f |= O_TRUNC;
191	if (flags.e)
192		f |= O_EXCL;
193	if (flags.a)
194		f |= O_APPEND;
195
196	fd = open64(file, f, mode);
197	if (fd < 0)
198		return -errno;
199
200	if (flags.cl && fcntl(fd, F_SETFD, 1)) {
201		err = -errno;
202		close(fd);
203		return err;
204	}
205
206	return fd;
207}
208
209int os_connect_socket(const char *name)
210{
211	struct sockaddr_un sock;
212	int fd, err;
213
214	sock.sun_family = AF_UNIX;
215	snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
216
217	fd = socket(AF_UNIX, SOCK_STREAM, 0);
218	if (fd < 0) {
219		err = -errno;
220		goto out;
221	}
222
223	err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
224	if (err) {
225		err = -errno;
226		goto out_close;
227	}
228
229	return fd;
230
231out_close:
232	close(fd);
233out:
234	return err;
235}
236
237void os_close_file(int fd)
238{
239	close(fd);
240}
241int os_fsync_file(int fd)
242{
243	if (fsync(fd) < 0)
244	    return -errno;
245	return 0;
246}
247
248int os_seek_file(int fd, unsigned long long offset)
249{
250	unsigned long long actual;
251
252	actual = lseek64(fd, offset, SEEK_SET);
253	if (actual != offset)
254		return -errno;
255	return 0;
256}
257
258int os_read_file(int fd, void *buf, int len)
259{
260	int n = read(fd, buf, len);
261
262	if (n < 0)
263		return -errno;
264	return n;
265}
266
267int os_pread_file(int fd, void *buf, int len, unsigned long long offset)
268{
269	int n = pread(fd, buf, len, offset);
270
271	if (n < 0)
272		return -errno;
273	return n;
274}
275
276int os_write_file(int fd, const void *buf, int len)
277{
278	int n = write(fd, (void *) buf, len);
279
280	if (n < 0)
281		return -errno;
282	return n;
283}
284
285int os_sync_file(int fd)
286{
287	int n = fsync(fd);
288
289	if (n < 0)
290		return -errno;
291	return n;
292}
293
294int os_pwrite_file(int fd, const void *buf, int len, unsigned long long offset)
295{
296	int n = pwrite(fd, (void *) buf, len, offset);
297
298	if (n < 0)
299		return -errno;
300	return n;
301}
302
303
304int os_file_size(const char *file, unsigned long long *size_out)
305{
306	struct uml_stat buf;
307	int err;
308
309	err = os_stat_file(file, &buf);
310	if (err < 0) {
311		printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
312		       -err);
313		return err;
314	}
315
316	if (S_ISBLK(buf.ust_mode)) {
317		int fd;
318		long blocks;
319
320		fd = open(file, O_RDONLY, 0);
321		if (fd < 0) {
322			err = -errno;
323			printk(UM_KERN_ERR "Couldn't open \"%s\", "
324			       "errno = %d\n", file, errno);
325			return err;
326		}
327		if (ioctl(fd, BLKGETSIZE, &blocks) < 0) {
328			err = -errno;
329			printk(UM_KERN_ERR "Couldn't get the block size of "
330			       "\"%s\", errno = %d\n", file, errno);
331			close(fd);
332			return err;
333		}
334		*size_out = ((long long) blocks) * 512;
335		close(fd);
336	}
337	else *size_out = buf.ust_size;
338
339	return 0;
340}
341
342int os_file_modtime(const char *file, unsigned long *modtime)
343{
344	struct uml_stat buf;
345	int err;
346
347	err = os_stat_file(file, &buf);
348	if (err < 0) {
349		printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
350		       -err);
351		return err;
352	}
353
354	*modtime = buf.ust_mtime;
355	return 0;
356}
357
358int os_set_exec_close(int fd)
359{
360	int err;
361
362	CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
363
364	if (err < 0)
365		return -errno;
366	return err;
367}
368
369int os_pipe(int *fds, int stream, int close_on_exec)
370{
371	int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
372
373	err = socketpair(AF_UNIX, type, 0, fds);
374	if (err < 0)
375		return -errno;
376
377	if (!close_on_exec)
378		return 0;
379
380	err = os_set_exec_close(fds[0]);
381	if (err < 0)
382		goto error;
383
384	err = os_set_exec_close(fds[1]);
385	if (err < 0)
386		goto error;
387
388	return 0;
389
390 error:
391	printk(UM_KERN_ERR "os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
392	       -err);
393	close(fds[1]);
394	close(fds[0]);
395	return err;
396}
397
398int os_set_fd_async(int fd)
399{
400	int err, flags;
401
402	flags = fcntl(fd, F_GETFL);
403	if (flags < 0)
404		return -errno;
405
406	flags |= O_ASYNC | O_NONBLOCK;
407	if (fcntl(fd, F_SETFL, flags) < 0) {
408		err = -errno;
409		printk(UM_KERN_ERR "os_set_fd_async : failed to set O_ASYNC "
410		       "and O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
411		return err;
412	}
413
414	if ((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
415	    (fcntl(fd, F_SETOWN, os_getpid()) < 0)) {
416		err = -errno;
417		printk(UM_KERN_ERR "os_set_fd_async : Failed to fcntl F_SETOWN "
418		       "(or F_SETSIG) fd %d, errno = %d\n", fd, errno);
419		return err;
420	}
421
422	return 0;
423}
424
425int os_clear_fd_async(int fd)
426{
427	int flags;
428
429	flags = fcntl(fd, F_GETFL);
430	if (flags < 0)
431		return -errno;
432
433	flags &= ~(O_ASYNC | O_NONBLOCK);
434	if (fcntl(fd, F_SETFL, flags) < 0)
435		return -errno;
436	return 0;
437}
438
439int os_set_fd_block(int fd, int blocking)
440{
441	int flags;
442
443	flags = fcntl(fd, F_GETFL);
444	if (flags < 0)
445		return -errno;
446
447	if (blocking)
448		flags &= ~O_NONBLOCK;
449	else
450		flags |= O_NONBLOCK;
451
452	if (fcntl(fd, F_SETFL, flags) < 0)
453		return -errno;
454
455	return 0;
456}
457
458int os_accept_connection(int fd)
459{
460	int new;
461
462	new = accept(fd, NULL, 0);
463	if (new < 0)
464		return -errno;
465	return new;
466}
467
468#ifndef SHUT_RD
469#define SHUT_RD 0
470#endif
471
472#ifndef SHUT_WR
473#define SHUT_WR 1
474#endif
475
476#ifndef SHUT_RDWR
477#define SHUT_RDWR 2
478#endif
479
480int os_shutdown_socket(int fd, int r, int w)
481{
482	int what, err;
483
484	if (r && w)
485		what = SHUT_RDWR;
486	else if (r)
487		what = SHUT_RD;
488	else if (w)
489		what = SHUT_WR;
490	else
491		return -EINVAL;
492
493	err = shutdown(fd, what);
494	if (err < 0)
495		return -errno;
496	return 0;
497}
498
499int os_rcv_fd(int fd, int *helper_pid_out)
500{
501	int new, n;
502	char buf[CMSG_SPACE(sizeof(new))];
503	struct msghdr msg;
504	struct cmsghdr *cmsg;
505	struct iovec iov;
506
507	msg.msg_name = NULL;
508	msg.msg_namelen = 0;
509	iov = ((struct iovec) { .iov_base  = helper_pid_out,
510				.iov_len   = sizeof(*helper_pid_out) });
511	msg.msg_iov = &iov;
512	msg.msg_iovlen = 1;
513	msg.msg_control = buf;
514	msg.msg_controllen = sizeof(buf);
515	msg.msg_flags = 0;
516
517	n = recvmsg(fd, &msg, 0);
518	if (n < 0)
519		return -errno;
520	else if (n != iov.iov_len)
521		*helper_pid_out = -1;
522
523	cmsg = CMSG_FIRSTHDR(&msg);
524	if (cmsg == NULL) {
525		printk(UM_KERN_ERR "rcv_fd didn't receive anything, "
526		       "error = %d\n", errno);
527		return -1;
528	}
529	if ((cmsg->cmsg_level != SOL_SOCKET) ||
530	    (cmsg->cmsg_type != SCM_RIGHTS)) {
531		printk(UM_KERN_ERR "rcv_fd didn't receive a descriptor\n");
532		return -1;
533	}
534
535	new = ((int *) CMSG_DATA(cmsg))[0];
536	return new;
537}
538
539int os_create_unix_socket(const char *file, int len, int close_on_exec)
540{
541	struct sockaddr_un addr;
542	int sock, err;
543
544	sock = socket(PF_UNIX, SOCK_DGRAM, 0);
545	if (sock < 0)
546		return -errno;
547
548	if (close_on_exec) {
549		err = os_set_exec_close(sock);
550		if (err < 0)
551			printk(UM_KERN_ERR "create_unix_socket : "
552			       "close_on_exec failed, err = %d", -err);
553	}
554
555	addr.sun_family = AF_UNIX;
556
557	snprintf(addr.sun_path, len, "%s", file);
558
559	err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
560	if (err < 0)
561		return -errno;
562
563	return sock;
564}
565
566void os_flush_stdout(void)
567{
568	fflush(stdout);
569}
570
571int os_lock_file(int fd, int excl)
572{
573	int type = excl ? F_WRLCK : F_RDLCK;
574	struct flock lock = ((struct flock) { .l_type	= type,
575					      .l_whence	= SEEK_SET,
576					      .l_start	= 0,
577					      .l_len	= 0 } );
578	int err, save;
579
580	err = fcntl(fd, F_SETLK, &lock);
581	if (!err)
582		goto out;
583
584	save = -errno;
585	err = fcntl(fd, F_GETLK, &lock);
586	if (err) {
587		err = -errno;
588		goto out;
589	}
590
591	printk(UM_KERN_ERR "F_SETLK failed, file already locked by pid %d\n",
592	       lock.l_pid);
593	err = save;
594 out:
595	return err;
596}
597
598unsigned os_major(unsigned long long dev)
599{
600	return major(dev);
601}
602
603unsigned os_minor(unsigned long long dev)
604{
605	return minor(dev);
606}
607
608unsigned long long os_makedev(unsigned major, unsigned minor)
609{
610	return makedev(major, minor);
611}
v4.17
  1/*
  2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.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/ioctl.h>
 12#include <sys/mount.h>
 13#include <sys/socket.h>
 14#include <sys/stat.h>
 15#include <sys/sysmacros.h>
 16#include <sys/un.h>
 17#include <sys/types.h>
 18#include <os.h>
 19
 20static void copy_stat(struct uml_stat *dst, const struct stat64 *src)
 21{
 22	*dst = ((struct uml_stat) {
 23		.ust_dev     = src->st_dev,     /* device */
 24		.ust_ino     = src->st_ino,     /* inode */
 25		.ust_mode    = src->st_mode,    /* protection */
 26		.ust_nlink   = src->st_nlink,   /* number of hard links */
 27		.ust_uid     = src->st_uid,     /* user ID of owner */
 28		.ust_gid     = src->st_gid,     /* group ID of owner */
 29		.ust_size    = src->st_size,    /* total size, in bytes */
 30		.ust_blksize = src->st_blksize, /* blocksize for filesys I/O */
 31		.ust_blocks  = src->st_blocks,  /* number of blocks allocated */
 32		.ust_atime   = src->st_atime,   /* time of last access */
 33		.ust_mtime   = src->st_mtime,   /* time of last modification */
 34		.ust_ctime   = src->st_ctime,   /* time of last change */
 35	});
 36}
 37
 38int os_stat_fd(const int fd, struct uml_stat *ubuf)
 39{
 40	struct stat64 sbuf;
 41	int err;
 42
 43	CATCH_EINTR(err = fstat64(fd, &sbuf));
 44	if (err < 0)
 45		return -errno;
 46
 47	if (ubuf != NULL)
 48		copy_stat(ubuf, &sbuf);
 49	return err;
 50}
 51
 52int os_stat_file(const char *file_name, struct uml_stat *ubuf)
 53{
 54	struct stat64 sbuf;
 55	int err;
 56
 57	CATCH_EINTR(err = stat64(file_name, &sbuf));
 58	if (err < 0)
 59		return -errno;
 60
 61	if (ubuf != NULL)
 62		copy_stat(ubuf, &sbuf);
 63	return err;
 64}
 65
 66int os_access(const char *file, int mode)
 67{
 68	int amode, err;
 69
 70	amode = (mode & OS_ACC_R_OK ? R_OK : 0) |
 71		(mode & OS_ACC_W_OK ? W_OK : 0) |
 72		(mode & OS_ACC_X_OK ? X_OK : 0) |
 73		(mode & OS_ACC_F_OK ? F_OK : 0);
 74
 75	err = access(file, amode);
 76	if (err < 0)
 77		return -errno;
 78
 79	return 0;
 80}
 81
 82/* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
 83int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
 84{
 85	int err;
 86
 87	err = ioctl(fd, cmd, arg);
 88	if (err < 0)
 89		return -errno;
 90
 91	return err;
 92}
 93
 94/* FIXME: ensure namebuf in os_get_if_name is big enough */
 95int os_get_ifname(int fd, char* namebuf)
 96{
 97	if (ioctl(fd, SIOCGIFNAME, namebuf) < 0)
 98		return -errno;
 99
100	return 0;
101}
102
103int os_set_slip(int fd)
104{
105	int disc, sencap;
106
107	disc = N_SLIP;
108	if (ioctl(fd, TIOCSETD, &disc) < 0)
109		return -errno;
110
111	sencap = 0;
112	if (ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
113		return -errno;
114
115	return 0;
116}
117
118int os_mode_fd(int fd, int mode)
119{
120	int err;
121
122	CATCH_EINTR(err = fchmod(fd, mode));
123	if (err < 0)
124		return -errno;
125
126	return 0;
127}
128
129int os_file_type(char *file)
130{
131	struct uml_stat buf;
132	int err;
133
134	err = os_stat_file(file, &buf);
135	if (err < 0)
136		return err;
137
138	if (S_ISDIR(buf.ust_mode))
139		return OS_TYPE_DIR;
140	else if (S_ISLNK(buf.ust_mode))
141		return OS_TYPE_SYMLINK;
142	else if (S_ISCHR(buf.ust_mode))
143		return OS_TYPE_CHARDEV;
144	else if (S_ISBLK(buf.ust_mode))
145		return OS_TYPE_BLOCKDEV;
146	else if (S_ISFIFO(buf.ust_mode))
147		return OS_TYPE_FIFO;
148	else if (S_ISSOCK(buf.ust_mode))
149		return OS_TYPE_SOCK;
150	else return OS_TYPE_FILE;
151}
152
153int os_file_mode(const char *file, struct openflags *mode_out)
154{
155	int err;
156
157	*mode_out = OPENFLAGS();
158
159	err = access(file, W_OK);
160	if (err && (errno != EACCES))
161		return -errno;
162	else if (!err)
163		*mode_out = of_write(*mode_out);
164
165	err = access(file, R_OK);
166	if (err && (errno != EACCES))
167		return -errno;
168	else if (!err)
169		*mode_out = of_read(*mode_out);
170
171	return err;
172}
173
174int os_open_file(const char *file, struct openflags flags, int mode)
175{
176	int fd, err, f = 0;
177
178	if (flags.r && flags.w)
179		f = O_RDWR;
180	else if (flags.r)
181		f = O_RDONLY;
182	else if (flags.w)
183		f = O_WRONLY;
184	else f = 0;
185
186	if (flags.s)
187		f |= O_SYNC;
188	if (flags.c)
189		f |= O_CREAT;
190	if (flags.t)
191		f |= O_TRUNC;
192	if (flags.e)
193		f |= O_EXCL;
194	if (flags.a)
195		f |= O_APPEND;
196
197	fd = open64(file, f, mode);
198	if (fd < 0)
199		return -errno;
200
201	if (flags.cl && fcntl(fd, F_SETFD, 1)) {
202		err = -errno;
203		close(fd);
204		return err;
205	}
206
207	return fd;
208}
209
210int os_connect_socket(const char *name)
211{
212	struct sockaddr_un sock;
213	int fd, err;
214
215	sock.sun_family = AF_UNIX;
216	snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
217
218	fd = socket(AF_UNIX, SOCK_STREAM, 0);
219	if (fd < 0) {
220		err = -errno;
221		goto out;
222	}
223
224	err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
225	if (err) {
226		err = -errno;
227		goto out_close;
228	}
229
230	return fd;
231
232out_close:
233	close(fd);
234out:
235	return err;
236}
237
238void os_close_file(int fd)
239{
240	close(fd);
241}
242int os_fsync_file(int fd)
243{
244	if (fsync(fd) < 0)
245	    return -errno;
246	return 0;
247}
248
249int os_seek_file(int fd, unsigned long long offset)
250{
251	unsigned long long actual;
252
253	actual = lseek64(fd, offset, SEEK_SET);
254	if (actual != offset)
255		return -errno;
256	return 0;
257}
258
259int os_read_file(int fd, void *buf, int len)
260{
261	int n = read(fd, buf, len);
262
263	if (n < 0)
264		return -errno;
265	return n;
266}
267
268int os_pread_file(int fd, void *buf, int len, unsigned long long offset)
269{
270	int n = pread(fd, buf, len, offset);
271
272	if (n < 0)
273		return -errno;
274	return n;
275}
276
277int os_write_file(int fd, const void *buf, int len)
278{
279	int n = write(fd, (void *) buf, len);
280
281	if (n < 0)
282		return -errno;
283	return n;
284}
285
286int os_sync_file(int fd)
287{
288	int n = fsync(fd);
289
290	if (n < 0)
291		return -errno;
292	return n;
293}
294
295int os_pwrite_file(int fd, const void *buf, int len, unsigned long long offset)
296{
297	int n = pwrite(fd, (void *) buf, len, offset);
298
299	if (n < 0)
300		return -errno;
301	return n;
302}
303
304
305int os_file_size(const char *file, unsigned long long *size_out)
306{
307	struct uml_stat buf;
308	int err;
309
310	err = os_stat_file(file, &buf);
311	if (err < 0) {
312		printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
313		       -err);
314		return err;
315	}
316
317	if (S_ISBLK(buf.ust_mode)) {
318		int fd;
319		long blocks;
320
321		fd = open(file, O_RDONLY, 0);
322		if (fd < 0) {
323			err = -errno;
324			printk(UM_KERN_ERR "Couldn't open \"%s\", "
325			       "errno = %d\n", file, errno);
326			return err;
327		}
328		if (ioctl(fd, BLKGETSIZE, &blocks) < 0) {
329			err = -errno;
330			printk(UM_KERN_ERR "Couldn't get the block size of "
331			       "\"%s\", errno = %d\n", file, errno);
332			close(fd);
333			return err;
334		}
335		*size_out = ((long long) blocks) * 512;
336		close(fd);
337	}
338	else *size_out = buf.ust_size;
339
340	return 0;
341}
342
343int os_file_modtime(const char *file, unsigned long *modtime)
344{
345	struct uml_stat buf;
346	int err;
347
348	err = os_stat_file(file, &buf);
349	if (err < 0) {
350		printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
351		       -err);
352		return err;
353	}
354
355	*modtime = buf.ust_mtime;
356	return 0;
357}
358
359int os_set_exec_close(int fd)
360{
361	int err;
362
363	CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
364
365	if (err < 0)
366		return -errno;
367	return err;
368}
369
370int os_pipe(int *fds, int stream, int close_on_exec)
371{
372	int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
373
374	err = socketpair(AF_UNIX, type, 0, fds);
375	if (err < 0)
376		return -errno;
377
378	if (!close_on_exec)
379		return 0;
380
381	err = os_set_exec_close(fds[0]);
382	if (err < 0)
383		goto error;
384
385	err = os_set_exec_close(fds[1]);
386	if (err < 0)
387		goto error;
388
389	return 0;
390
391 error:
392	printk(UM_KERN_ERR "os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
393	       -err);
394	close(fds[1]);
395	close(fds[0]);
396	return err;
397}
398
399int os_set_fd_async(int fd)
400{
401	int err, flags;
402
403	flags = fcntl(fd, F_GETFL);
404	if (flags < 0)
405		return -errno;
406
407	flags |= O_ASYNC | O_NONBLOCK;
408	if (fcntl(fd, F_SETFL, flags) < 0) {
409		err = -errno;
410		printk(UM_KERN_ERR "os_set_fd_async : failed to set O_ASYNC "
411		       "and O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
412		return err;
413	}
414
415	if ((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
416	    (fcntl(fd, F_SETOWN, os_getpid()) < 0)) {
417		err = -errno;
418		printk(UM_KERN_ERR "os_set_fd_async : Failed to fcntl F_SETOWN "
419		       "(or F_SETSIG) fd %d, errno = %d\n", fd, errno);
420		return err;
421	}
422
423	return 0;
424}
425
426int os_clear_fd_async(int fd)
427{
428	int flags;
429
430	flags = fcntl(fd, F_GETFL);
431	if (flags < 0)
432		return -errno;
433
434	flags &= ~(O_ASYNC | O_NONBLOCK);
435	if (fcntl(fd, F_SETFL, flags) < 0)
436		return -errno;
437	return 0;
438}
439
440int os_set_fd_block(int fd, int blocking)
441{
442	int flags;
443
444	flags = fcntl(fd, F_GETFL);
445	if (flags < 0)
446		return -errno;
447
448	if (blocking)
449		flags &= ~O_NONBLOCK;
450	else
451		flags |= O_NONBLOCK;
452
453	if (fcntl(fd, F_SETFL, flags) < 0)
454		return -errno;
455
456	return 0;
457}
458
459int os_accept_connection(int fd)
460{
461	int new;
462
463	new = accept(fd, NULL, 0);
464	if (new < 0)
465		return -errno;
466	return new;
467}
468
469#ifndef SHUT_RD
470#define SHUT_RD 0
471#endif
472
473#ifndef SHUT_WR
474#define SHUT_WR 1
475#endif
476
477#ifndef SHUT_RDWR
478#define SHUT_RDWR 2
479#endif
480
481int os_shutdown_socket(int fd, int r, int w)
482{
483	int what, err;
484
485	if (r && w)
486		what = SHUT_RDWR;
487	else if (r)
488		what = SHUT_RD;
489	else if (w)
490		what = SHUT_WR;
491	else
492		return -EINVAL;
493
494	err = shutdown(fd, what);
495	if (err < 0)
496		return -errno;
497	return 0;
498}
499
500int os_rcv_fd(int fd, int *helper_pid_out)
501{
502	int new, n;
503	char buf[CMSG_SPACE(sizeof(new))];
504	struct msghdr msg;
505	struct cmsghdr *cmsg;
506	struct iovec iov;
507
508	msg.msg_name = NULL;
509	msg.msg_namelen = 0;
510	iov = ((struct iovec) { .iov_base  = helper_pid_out,
511				.iov_len   = sizeof(*helper_pid_out) });
512	msg.msg_iov = &iov;
513	msg.msg_iovlen = 1;
514	msg.msg_control = buf;
515	msg.msg_controllen = sizeof(buf);
516	msg.msg_flags = 0;
517
518	n = recvmsg(fd, &msg, 0);
519	if (n < 0)
520		return -errno;
521	else if (n != iov.iov_len)
522		*helper_pid_out = -1;
523
524	cmsg = CMSG_FIRSTHDR(&msg);
525	if (cmsg == NULL) {
526		printk(UM_KERN_ERR "rcv_fd didn't receive anything, "
527		       "error = %d\n", errno);
528		return -1;
529	}
530	if ((cmsg->cmsg_level != SOL_SOCKET) ||
531	    (cmsg->cmsg_type != SCM_RIGHTS)) {
532		printk(UM_KERN_ERR "rcv_fd didn't receive a descriptor\n");
533		return -1;
534	}
535
536	new = ((int *) CMSG_DATA(cmsg))[0];
537	return new;
538}
539
540int os_create_unix_socket(const char *file, int len, int close_on_exec)
541{
542	struct sockaddr_un addr;
543	int sock, err;
544
545	sock = socket(PF_UNIX, SOCK_DGRAM, 0);
546	if (sock < 0)
547		return -errno;
548
549	if (close_on_exec) {
550		err = os_set_exec_close(sock);
551		if (err < 0)
552			printk(UM_KERN_ERR "create_unix_socket : "
553			       "close_on_exec failed, err = %d", -err);
554	}
555
556	addr.sun_family = AF_UNIX;
557
558	snprintf(addr.sun_path, len, "%s", file);
559
560	err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
561	if (err < 0)
562		return -errno;
563
564	return sock;
565}
566
567void os_flush_stdout(void)
568{
569	fflush(stdout);
570}
571
572int os_lock_file(int fd, int excl)
573{
574	int type = excl ? F_WRLCK : F_RDLCK;
575	struct flock lock = ((struct flock) { .l_type	= type,
576					      .l_whence	= SEEK_SET,
577					      .l_start	= 0,
578					      .l_len	= 0 } );
579	int err, save;
580
581	err = fcntl(fd, F_SETLK, &lock);
582	if (!err)
583		goto out;
584
585	save = -errno;
586	err = fcntl(fd, F_GETLK, &lock);
587	if (err) {
588		err = -errno;
589		goto out;
590	}
591
592	printk(UM_KERN_ERR "F_SETLK failed, file already locked by pid %d\n",
593	       lock.l_pid);
594	err = save;
595 out:
596	return err;
597}
598
599unsigned os_major(unsigned long long dev)
600{
601	return major(dev);
602}
603
604unsigned os_minor(unsigned long long dev)
605{
606	return minor(dev);
607}
608
609unsigned long long os_makedev(unsigned major, unsigned minor)
610{
611	return makedev(major, minor);
612}