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_write_file(int fd, const void *buf, int len)
268{
269	int n = write(fd, (void *) buf, len);
270
271	if (n < 0)
272		return -errno;
273	return n;
274}
275
276int os_sync_file(int fd)
277{
278	int n = fsync(fd);
279
280	if (n < 0)
281		return -errno;
282	return n;
283}
284
285int os_file_size(const char *file, unsigned long long *size_out)
286{
287	struct uml_stat buf;
288	int err;
289
290	err = os_stat_file(file, &buf);
291	if (err < 0) {
292		printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
293		       -err);
294		return err;
295	}
296
297	if (S_ISBLK(buf.ust_mode)) {
298		int fd;
299		long blocks;
300
301		fd = open(file, O_RDONLY, 0);
302		if (fd < 0) {
303			err = -errno;
304			printk(UM_KERN_ERR "Couldn't open \"%s\", "
305			       "errno = %d\n", file, errno);
306			return err;
307		}
308		if (ioctl(fd, BLKGETSIZE, &blocks) < 0) {
309			err = -errno;
310			printk(UM_KERN_ERR "Couldn't get the block size of "
311			       "\"%s\", errno = %d\n", file, errno);
312			close(fd);
313			return err;
314		}
315		*size_out = ((long long) blocks) * 512;
316		close(fd);
317	}
318	else *size_out = buf.ust_size;
319
320	return 0;
321}
322
323int os_file_modtime(const char *file, unsigned long *modtime)
324{
325	struct uml_stat buf;
326	int err;
327
328	err = os_stat_file(file, &buf);
329	if (err < 0) {
330		printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
331		       -err);
332		return err;
333	}
334
335	*modtime = buf.ust_mtime;
336	return 0;
337}
338
339int os_set_exec_close(int fd)
340{
341	int err;
342
343	CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
344
345	if (err < 0)
346		return -errno;
347	return err;
348}
349
350int os_pipe(int *fds, int stream, int close_on_exec)
351{
352	int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
353
354	err = socketpair(AF_UNIX, type, 0, fds);
355	if (err < 0)
356		return -errno;
357
358	if (!close_on_exec)
359		return 0;
360
361	err = os_set_exec_close(fds[0]);
362	if (err < 0)
363		goto error;
364
365	err = os_set_exec_close(fds[1]);
366	if (err < 0)
367		goto error;
368
369	return 0;
370
371 error:
372	printk(UM_KERN_ERR "os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
373	       -err);
374	close(fds[1]);
375	close(fds[0]);
376	return err;
377}
378
379int os_set_fd_async(int fd)
380{
381	int err, flags;
382
383	flags = fcntl(fd, F_GETFL);
384	if (flags < 0)
385		return -errno;
386
387	flags |= O_ASYNC | O_NONBLOCK;
388	if (fcntl(fd, F_SETFL, flags) < 0) {
389		err = -errno;
390		printk(UM_KERN_ERR "os_set_fd_async : failed to set O_ASYNC "
391		       "and O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
392		return err;
393	}
394
395	if ((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
396	    (fcntl(fd, F_SETOWN, os_getpid()) < 0)) {
397		err = -errno;
398		printk(UM_KERN_ERR "os_set_fd_async : Failed to fcntl F_SETOWN "
399		       "(or F_SETSIG) fd %d, errno = %d\n", fd, errno);
400		return err;
401	}
402
403	return 0;
404}
405
406int os_clear_fd_async(int fd)
407{
408	int flags;
409
410	flags = fcntl(fd, F_GETFL);
411	if (flags < 0)
412		return -errno;
413
414	flags &= ~(O_ASYNC | O_NONBLOCK);
415	if (fcntl(fd, F_SETFL, flags) < 0)
416		return -errno;
417	return 0;
418}
419
420int os_set_fd_block(int fd, int blocking)
421{
422	int flags;
423
424	flags = fcntl(fd, F_GETFL);
425	if (flags < 0)
426		return -errno;
427
428	if (blocking)
429		flags &= ~O_NONBLOCK;
430	else
431		flags |= O_NONBLOCK;
432
433	if (fcntl(fd, F_SETFL, flags) < 0)
434		return -errno;
435
436	return 0;
437}
438
439int os_accept_connection(int fd)
440{
441	int new;
442
443	new = accept(fd, NULL, 0);
444	if (new < 0)
445		return -errno;
446	return new;
447}
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)
466		what = SHUT_RDWR;
467	else if (r)
468		what = SHUT_RD;
469	else if (w)
470		what = SHUT_WR;
471	else
472		return -EINVAL;
473
474	err = shutdown(fd, what);
475	if (err < 0)
476		return -errno;
477	return 0;
478}
479
480int os_rcv_fd(int fd, int *helper_pid_out)
481{
482	int new, n;
483	char buf[CMSG_SPACE(sizeof(new))];
484	struct msghdr msg;
485	struct cmsghdr *cmsg;
486	struct iovec iov;
487
488	msg.msg_name = NULL;
489	msg.msg_namelen = 0;
490	iov = ((struct iovec) { .iov_base  = helper_pid_out,
491				.iov_len   = sizeof(*helper_pid_out) });
492	msg.msg_iov = &iov;
493	msg.msg_iovlen = 1;
494	msg.msg_control = buf;
495	msg.msg_controllen = sizeof(buf);
496	msg.msg_flags = 0;
497
498	n = recvmsg(fd, &msg, 0);
499	if (n < 0)
500		return -errno;
501	else if (n != iov.iov_len)
502		*helper_pid_out = -1;
503
504	cmsg = CMSG_FIRSTHDR(&msg);
505	if (cmsg == NULL) {
506		printk(UM_KERN_ERR "rcv_fd didn't receive anything, "
507		       "error = %d\n", errno);
508		return -1;
509	}
510	if ((cmsg->cmsg_level != SOL_SOCKET) ||
511	    (cmsg->cmsg_type != SCM_RIGHTS)) {
512		printk(UM_KERN_ERR "rcv_fd didn't receive a descriptor\n");
513		return -1;
514	}
515
516	new = ((int *) CMSG_DATA(cmsg))[0];
517	return new;
518}
519
520int os_create_unix_socket(const char *file, int len, int close_on_exec)
521{
522	struct sockaddr_un addr;
523	int sock, err;
524
525	sock = socket(PF_UNIX, SOCK_DGRAM, 0);
526	if (sock < 0)
527		return -errno;
528
529	if (close_on_exec) {
530		err = os_set_exec_close(sock);
531		if (err < 0)
532			printk(UM_KERN_ERR "create_unix_socket : "
533			       "close_on_exec failed, err = %d", -err);
534	}
535
536	addr.sun_family = AF_UNIX;
537
538	snprintf(addr.sun_path, len, "%s", file);
539
540	err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
541	if (err < 0)
542		return -errno;
543
544	return sock;
545}
546
547void os_flush_stdout(void)
548{
549	fflush(stdout);
550}
551
552int os_lock_file(int fd, int excl)
553{
554	int type = excl ? F_WRLCK : F_RDLCK;
555	struct flock lock = ((struct flock) { .l_type	= type,
556					      .l_whence	= SEEK_SET,
557					      .l_start	= 0,
558					      .l_len	= 0 } );
559	int err, save;
560
561	err = fcntl(fd, F_SETLK, &lock);
562	if (!err)
563		goto out;
564
565	save = -errno;
566	err = fcntl(fd, F_GETLK, &lock);
567	if (err) {
568		err = -errno;
569		goto out;
570	}
571
572	printk(UM_KERN_ERR "F_SETLK failed, file already locked by pid %d\n",
573	       lock.l_pid);
574	err = save;
575 out:
576	return err;
577}
578
579unsigned os_major(unsigned long long dev)
580{
581	return major(dev);
582}
583
584unsigned os_minor(unsigned long long dev)
585{
586	return minor(dev);
587}
588
589unsigned long long os_makedev(unsigned major, unsigned minor)
590{
591	return makedev(major, minor);
592}
593