root / oslib-posix.c @ 904ebffe
History | View | Annotate | Download (4.1 kB)
1 | c1b0b93b | Jes Sorensen | /*
|
---|---|---|---|
2 | c1b0b93b | Jes Sorensen | * os-posix-lib.c
|
3 | c1b0b93b | Jes Sorensen | *
|
4 | c1b0b93b | Jes Sorensen | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | c1b0b93b | Jes Sorensen | * Copyright (c) 2010 Red Hat, Inc.
|
6 | c1b0b93b | Jes Sorensen | *
|
7 | c1b0b93b | Jes Sorensen | * QEMU library functions on POSIX which are shared between QEMU and
|
8 | c1b0b93b | Jes Sorensen | * the QEMU tools.
|
9 | c1b0b93b | Jes Sorensen | *
|
10 | c1b0b93b | Jes Sorensen | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
11 | c1b0b93b | Jes Sorensen | * of this software and associated documentation files (the "Software"), to deal
|
12 | c1b0b93b | Jes Sorensen | * in the Software without restriction, including without limitation the rights
|
13 | c1b0b93b | Jes Sorensen | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
14 | c1b0b93b | Jes Sorensen | * copies of the Software, and to permit persons to whom the Software is
|
15 | c1b0b93b | Jes Sorensen | * furnished to do so, subject to the following conditions:
|
16 | c1b0b93b | Jes Sorensen | *
|
17 | c1b0b93b | Jes Sorensen | * The above copyright notice and this permission notice shall be included in
|
18 | c1b0b93b | Jes Sorensen | * all copies or substantial portions of the Software.
|
19 | c1b0b93b | Jes Sorensen | *
|
20 | c1b0b93b | Jes Sorensen | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
21 | c1b0b93b | Jes Sorensen | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
22 | c1b0b93b | Jes Sorensen | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
23 | c1b0b93b | Jes Sorensen | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
24 | c1b0b93b | Jes Sorensen | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
25 | c1b0b93b | Jes Sorensen | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
26 | c1b0b93b | Jes Sorensen | * THE SOFTWARE.
|
27 | c1b0b93b | Jes Sorensen | */
|
28 | c1b0b93b | Jes Sorensen | |
29 | c1b0b93b | Jes Sorensen | #include "config-host.h" |
30 | c1b0b93b | Jes Sorensen | #include "sysemu.h" |
31 | c1b0b93b | Jes Sorensen | #include "trace.h" |
32 | 9549e764 | Jes Sorensen | #include "qemu_socket.h" |
33 | c1b0b93b | Jes Sorensen | |
34 | b152aa84 | Jes Sorensen | void *qemu_oom_check(void *ptr) |
35 | c1b0b93b | Jes Sorensen | { |
36 | c1b0b93b | Jes Sorensen | if (ptr == NULL) { |
37 | c1b0b93b | Jes Sorensen | fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
|
38 | c1b0b93b | Jes Sorensen | abort(); |
39 | c1b0b93b | Jes Sorensen | } |
40 | c1b0b93b | Jes Sorensen | return ptr;
|
41 | c1b0b93b | Jes Sorensen | } |
42 | c1b0b93b | Jes Sorensen | |
43 | c1b0b93b | Jes Sorensen | void *qemu_memalign(size_t alignment, size_t size)
|
44 | c1b0b93b | Jes Sorensen | { |
45 | c1b0b93b | Jes Sorensen | void *ptr;
|
46 | c1b0b93b | Jes Sorensen | #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
|
47 | c1b0b93b | Jes Sorensen | int ret;
|
48 | c1b0b93b | Jes Sorensen | ret = posix_memalign(&ptr, alignment, size); |
49 | c1b0b93b | Jes Sorensen | if (ret != 0) { |
50 | c1b0b93b | Jes Sorensen | fprintf(stderr, "Failed to allocate %zu B: %s\n",
|
51 | c1b0b93b | Jes Sorensen | size, strerror(ret)); |
52 | c1b0b93b | Jes Sorensen | abort(); |
53 | c1b0b93b | Jes Sorensen | } |
54 | c1b0b93b | Jes Sorensen | #elif defined(CONFIG_BSD)
|
55 | b152aa84 | Jes Sorensen | ptr = qemu_oom_check(valloc(size)); |
56 | c1b0b93b | Jes Sorensen | #else
|
57 | b152aa84 | Jes Sorensen | ptr = qemu_oom_check(memalign(alignment, size)); |
58 | c1b0b93b | Jes Sorensen | #endif
|
59 | c1b0b93b | Jes Sorensen | trace_qemu_memalign(alignment, size, ptr); |
60 | c1b0b93b | Jes Sorensen | return ptr;
|
61 | c1b0b93b | Jes Sorensen | } |
62 | c1b0b93b | Jes Sorensen | |
63 | c1b0b93b | Jes Sorensen | /* alloc shared memory pages */
|
64 | c1b0b93b | Jes Sorensen | void *qemu_vmalloc(size_t size)
|
65 | c1b0b93b | Jes Sorensen | { |
66 | c1b0b93b | Jes Sorensen | return qemu_memalign(getpagesize(), size);
|
67 | c1b0b93b | Jes Sorensen | } |
68 | c1b0b93b | Jes Sorensen | |
69 | c1b0b93b | Jes Sorensen | void qemu_vfree(void *ptr) |
70 | c1b0b93b | Jes Sorensen | { |
71 | c1b0b93b | Jes Sorensen | trace_qemu_vfree(ptr); |
72 | c1b0b93b | Jes Sorensen | free(ptr); |
73 | c1b0b93b | Jes Sorensen | } |
74 | 9549e764 | Jes Sorensen | |
75 | 9549e764 | Jes Sorensen | void socket_set_nonblock(int fd) |
76 | 9549e764 | Jes Sorensen | { |
77 | 9549e764 | Jes Sorensen | int f;
|
78 | 9549e764 | Jes Sorensen | f = fcntl(fd, F_GETFL); |
79 | 9549e764 | Jes Sorensen | fcntl(fd, F_SETFL, f | O_NONBLOCK); |
80 | 9549e764 | Jes Sorensen | } |
81 | 9549e764 | Jes Sorensen | |
82 | 9549e764 | Jes Sorensen | void qemu_set_cloexec(int fd) |
83 | 9549e764 | Jes Sorensen | { |
84 | 9549e764 | Jes Sorensen | int f;
|
85 | 9549e764 | Jes Sorensen | f = fcntl(fd, F_GETFD); |
86 | 9549e764 | Jes Sorensen | fcntl(fd, F_SETFD, f | FD_CLOEXEC); |
87 | 9549e764 | Jes Sorensen | } |
88 | 70e72ce4 | Jes Sorensen | |
89 | 70e72ce4 | Jes Sorensen | /*
|
90 | 70e72ce4 | Jes Sorensen | * Creates a pipe with FD_CLOEXEC set on both file descriptors
|
91 | 70e72ce4 | Jes Sorensen | */
|
92 | 70e72ce4 | Jes Sorensen | int qemu_pipe(int pipefd[2]) |
93 | 70e72ce4 | Jes Sorensen | { |
94 | 70e72ce4 | Jes Sorensen | int ret;
|
95 | 70e72ce4 | Jes Sorensen | |
96 | 70e72ce4 | Jes Sorensen | #ifdef CONFIG_PIPE2
|
97 | 70e72ce4 | Jes Sorensen | ret = pipe2(pipefd, O_CLOEXEC); |
98 | 70e72ce4 | Jes Sorensen | if (ret != -1 || errno != ENOSYS) { |
99 | 70e72ce4 | Jes Sorensen | return ret;
|
100 | 70e72ce4 | Jes Sorensen | } |
101 | 70e72ce4 | Jes Sorensen | #endif
|
102 | 70e72ce4 | Jes Sorensen | ret = pipe(pipefd); |
103 | 70e72ce4 | Jes Sorensen | if (ret == 0) { |
104 | 70e72ce4 | Jes Sorensen | qemu_set_cloexec(pipefd[0]);
|
105 | 70e72ce4 | Jes Sorensen | qemu_set_cloexec(pipefd[1]);
|
106 | 70e72ce4 | Jes Sorensen | } |
107 | 70e72ce4 | Jes Sorensen | |
108 | 70e72ce4 | Jes Sorensen | return ret;
|
109 | 70e72ce4 | Jes Sorensen | } |
110 | 38671423 | Hidetoshi Seto | |
111 | 38671423 | Hidetoshi Seto | int qemu_utimensat(int dirfd, const char *path, const struct timespec *times, |
112 | 38671423 | Hidetoshi Seto | int flags)
|
113 | 38671423 | Hidetoshi Seto | { |
114 | 38671423 | Hidetoshi Seto | struct timeval tv[2], tv_now; |
115 | 38671423 | Hidetoshi Seto | struct stat st;
|
116 | 38671423 | Hidetoshi Seto | int i;
|
117 | 38671423 | Hidetoshi Seto | #ifdef CONFIG_UTIMENSAT
|
118 | 38671423 | Hidetoshi Seto | int ret;
|
119 | 38671423 | Hidetoshi Seto | |
120 | 38671423 | Hidetoshi Seto | ret = utimensat(dirfd, path, times, flags); |
121 | 38671423 | Hidetoshi Seto | if (ret != -1 || errno != ENOSYS) { |
122 | 38671423 | Hidetoshi Seto | return ret;
|
123 | 38671423 | Hidetoshi Seto | } |
124 | 38671423 | Hidetoshi Seto | #endif
|
125 | 38671423 | Hidetoshi Seto | /* Fallback: use utimes() instead of utimensat() */
|
126 | 38671423 | Hidetoshi Seto | |
127 | 38671423 | Hidetoshi Seto | /* happy if special cases */
|
128 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) { |
129 | 38671423 | Hidetoshi Seto | return 0; |
130 | 38671423 | Hidetoshi Seto | } |
131 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW) { |
132 | 38671423 | Hidetoshi Seto | return utimes(path, NULL); |
133 | 38671423 | Hidetoshi Seto | } |
134 | 38671423 | Hidetoshi Seto | |
135 | 38671423 | Hidetoshi Seto | /* prepare for hard cases */
|
136 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) { |
137 | 38671423 | Hidetoshi Seto | gettimeofday(&tv_now, NULL);
|
138 | 38671423 | Hidetoshi Seto | } |
139 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) { |
140 | 38671423 | Hidetoshi Seto | stat(path, &st); |
141 | 38671423 | Hidetoshi Seto | } |
142 | 38671423 | Hidetoshi Seto | |
143 | 38671423 | Hidetoshi Seto | for (i = 0; i < 2; i++) { |
144 | 38671423 | Hidetoshi Seto | if (times[i].tv_nsec == UTIME_NOW) {
|
145 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = tv_now.tv_sec; |
146 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = tv_now.tv_usec; |
147 | 38671423 | Hidetoshi Seto | } else if (times[i].tv_nsec == UTIME_OMIT) { |
148 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
|
149 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = 0;
|
150 | 38671423 | Hidetoshi Seto | } else {
|
151 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = times[i].tv_sec; |
152 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = times[i].tv_nsec / 1000;
|
153 | 38671423 | Hidetoshi Seto | } |
154 | 38671423 | Hidetoshi Seto | } |
155 | 38671423 | Hidetoshi Seto | |
156 | 38671423 | Hidetoshi Seto | return utimes(path, &tv[0]); |
157 | 38671423 | Hidetoshi Seto | } |