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